LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIOParticleHandler.cc
Go to the documentation of this file.
2 #include "EVENT/MCParticle.h"
4 #include "EVENT/LCCollection.h"
5 
6 // -- sio headers
7 #include <sio/io_device.h>
8 #include <sio/version.h>
9 
10 namespace SIO {
11 
13  SIOObjectHandler( EVENT::LCIO::MCPARTICLE ) {
14  /* nop */
15  }
16 
17  //----------------------------------------------------------------------------
18 
19  void SIOParticleHandler::read( sio::read_device& device, EVENT::LCObject* objP, sio::version_type vers ) {
20  auto particle = dynamic_cast<IOIMPL::MCParticleIOImpl*>(objP) ;
21  SIO_PTAG( device , dynamic_cast<EVENT::MCParticle*>(particle) ) ;
22  int numberOfParents ;
23  SIO_SDATA( device , numberOfParents ) ;
24  particle->_parents.resize( numberOfParents ) ;
25  for( int i=0 ; i<numberOfParents ; i++ ) {
26  SIO_PNTR( device , &(particle->_parents[i] ) ) ;
27  }
28  SIO_DATA( device , &(particle->_pdg) , 1 ) ;
29  SIO_DATA( device , &(particle->_genstatus) , 1 ) ;
30  int simstatus ;
31  SIO_DATA( device , &simstatus , 1 ) ;
32  particle->_simstatus = simstatus ;
33  SIO_DATA( device , particle->_vertex , 3 ) ;
34  if( vers > SIO_VERSION_ENCODE( 1, 2 ) ) {
35  SIO_DATA( device , &(particle->_time) , 1 ) ;
36  }
37  float momentum[3] ;
38  SIO_DATA( device , momentum , 3 ) ;
39  particle->setMomentum( momentum ) ;
40  float mass ;
41  SIO_DATA( device , &mass , 1 ) ;
42  particle->setMass( mass ) ;
43  SIO_DATA( device , &(particle->_charge) , 1 ) ;
44  if( particle->_simstatus.test( EVENT::MCParticle::BITEndpoint ) ) { // bit 31
45  SIO_DATA( device , particle->_endpoint , 3 ) ;
46  if( vers > SIO_VERSION_ENCODE( 2, 6 ) ) {
47  float mom[3] ;
48  SIO_DATA( device , mom , 3 ) ;
49  particle->setMomentumAtEndpoint( mom ) ;
50  }
51  }
52  if( vers > SIO_VERSION_ENCODE( 1, 51 ) ) {
53  SIO_DATA( device , particle->_spin , 3 ) ;
54  SIO_DATA( device , particle->_colorFlow , 2 ) ;
55  }
56  }
57 
58  //----------------------------------------------------------------------------
59 
60  void SIOParticleHandler::write( sio::write_device& device, const EVENT::LCObject* obj ) {
61  auto particle = dynamic_cast<const EVENT::MCParticle*>( obj ) ;
62  SIO_PTAG( device , particle ) ;
63  int numberOfParents = particle->getParents().size() ;
64  SIO_DATA( device , &numberOfParents , 1 ) ;
65  for(int i=0;i<numberOfParents;i++){
66  auto part = particle->getParents()[i] ;
67  SIO_PNTR( device , &part );
68  }
69  SIO_SDATA( device, particle->getPDG() ) ;
70  SIO_SDATA( device, particle->getGeneratorStatus() ) ;
71  SIO_SDATA( device, particle->getSimulatorStatus() ) ;
72  SIO_DATA( device, particle->getVertex(), 3 ) ;
73  SIO_SDATA( device, particle->getTime() ) ;
74  SIO_SDATA( device, static_cast<float>(particle->getMomentum()[0]) ) ;
75  SIO_SDATA( device, static_cast<float>(particle->getMomentum()[1]) ) ;
76  SIO_SDATA( device, static_cast<float>(particle->getMomentum()[2]) ) ;
77  SIO_SDATA( device, static_cast<float>(particle->getMass() ) ) ;
78  SIO_SDATA( device, particle->getCharge() ) ;
79  if( particle->getSimulatorStatus() & (1<<EVENT::MCParticle::BITEndpoint) ) { // endpoint set !
80  SIO_DATA( device, particle->getEndpoint() , 3 ) ;
81  SIO_SDATA( device, static_cast<float>(particle->getMomentumAtEndpoint()[0]) ) ;
82  SIO_SDATA( device, static_cast<float>(particle->getMomentumAtEndpoint()[1]) ) ;
83  SIO_SDATA( device, static_cast<float>(particle->getMomentumAtEndpoint()[2]) ) ;
84  }
85  SIO_SDATA( device, (float) particle->getSpin()[0] ) ;
86  SIO_SDATA( device, (float) particle->getSpin()[1] ) ;
87  SIO_SDATA( device, (float) particle->getSpin()[2] ) ;
88  SIO_SDATA( device, (int) particle->getColorFlow()[0] ) ;
89  SIO_SDATA( device, (int) particle->getColorFlow()[1] ) ;
90  }
91 
92  //----------------------------------------------------------------------------
93 
95  return new IOIMPL::MCParticleIOImpl() ;
96  }
97 
98  //----------------------------------------------------------------------------
99 
101  auto strVec = evt->getCollectionNames() ;
102  for( auto name = strVec->begin() ; name != strVec->end() ; name++ ) {
103  EVENT::LCCollection* col = evt->getCollection( *name ) ;
104  if( (col->getTypeName() == EVENT::LCIO::MCPARTICLE) && ! ( col->getFlag() & ( 1 << EVENT::LCCollection::BITSubset) ) ) {
105 
106  int nDaughtersTotal = 0 ;
107  int nParentsTotal = 0 ;
108  for(int i=0; i < col->getNumberOfElements() ; i++) {
109  auto mcp = dynamic_cast<IOIMPL::MCParticleIOImpl*>( col->getElementAt(i) ) ;
110  nDaughtersTotal += mcp->getDaughters().size() ;
111  nParentsTotal += mcp->getParents().size() ;
112  }
113  for(int i=0; i < col->getNumberOfElements() ; i++) {
114  auto mcp = dynamic_cast<IOIMPL::MCParticleIOImpl*>( col->getElementAt(i) ) ;
115  // for version v00-08 we restore parents from daughters
116  if ( nParentsTotal == 0 && nDaughtersTotal > 0 ) {
117  int nDaughters = mcp->getDaughters().size() ;
118  for( int j=0; j<nDaughters; j++) {
119  auto dgh = dynamic_cast<IOIMPL::MCParticleIOImpl*>( mcp->getDaughters()[j] ) ;
120  dgh->_parents.push_back( mcp ) ;
121  }
122  }
123  else if ( nParentsTotal > 0 && nDaughtersTotal == 0 ) {
124  int nParents = mcp->getParents().size() ;
125  for( int j=0; j<nParents; j++){
126  auto mom = dynamic_cast<IOIMPL::MCParticleIOImpl*>( mcp->getParents()[j] ) ;
127  mom->_daughters.push_back( mcp ) ;
128  }
129  }
130  } // loop over particles
131  } // if( MCPARTICLE )
132  } // loop over collections
133  }
134 
135 } // namespace
The generic object that is held in an LCCollection.
Definition: LCObject.h:30
EVENT::MCParticleVec _daughters
void write(sio::write_device &device, const EVENT::LCObject *obj) override
Writes lcio objects to an SIO stream.
static void restoreParentDaughterRelations(EVENT::LCEvent *evt)
Restore the MCParticle parent &lt;-&gt; daughter relations.
SIOParticleHandler()
Constructor.
virtual const std::vector< std::string > * getCollectionNames() const =0
Returns the names of the collections in the event.
Interface for all lcio object SIO-handlers, has to be implemented for all event entities (hits...
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
virtual int getFlag() const =0
Returns flag word for collection.
T push_back(T...args)
virtual LCCollection * getCollection(const std::string &name) const =0
Returns the collection for the given name.
virtual const std::string & getTypeName() const =0
Returns the type name of the collection - valid names are defined in LCIO.
void read(sio::read_device &device, EVENT::LCObject *objP, sio::version_type vers) override
Reads lcio objects from an SIO stream.
virtual const EVENT::MCParticleVec & getDaughters() const
Returns the daughters of this particle.
The LCIO Monte Carlo particle.
Definition: MCParticle.h:27
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
T size(T...args)
The main event interface.
Definition: LCEvent.h:31
The generic collection used in LCIO.
Definition: LCCollection.h:29
Adding stuff needed for io (friend declarations, etc.)
EVENT::MCParticleVec _parents
EVENT::LCObject * create() const override
Factory method to create an object of the type of the collection.
static const int BITSubset
Definition: LCCollection.h:70
static const int BITEndpoint
Definition: MCParticle.h:87