/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/SIO/SIOParticleHandler.cc

Go to the documentation of this file.
00001 #include "SIO/SIOParticleHandler.h"
00002 #include "SIO/LCSIO.h"
00003 
00004 #include "EVENT/MCParticle.h"
00005 #include "IOIMPL/MCParticleIOImpl.h"
00006 
00007 #include "SIO_functions.h"
00008 #include "SIO_block.h"
00009 //#include <iostream>
00010 
00011 #include "EVENT/LCCollection.h"
00012 
00013 using namespace EVENT ;
00014 
00015 using namespace IOIMPL ;
00016 
00017 namespace SIO {
00018   
00019   unsigned int SIOParticleHandler::read(SIO_stream* stream, 
00020                                         LCObject** objP){
00021 
00022     if ( SIO_VERSION_MAJOR(_vers)==0 && SIO_VERSION_MINOR(_vers)==8)  
00023       return readv00_08( stream, objP ) ;
00024     
00025     
00026     unsigned int status ; 
00027     
00028     // create a new object :
00029     MCParticleIOImpl* particle  = new MCParticleIOImpl ;
00030     *objP = particle ;
00031     
00032     // tell SIO the address of particle as an abstract  MCParticle ...
00033     // this is important, as SIO takes the bare address 
00034     // (int)(MCParticle*) particle != (int)particle !!!!
00035     SIO_PTAG( stream , dynamic_cast<MCParticle*>(particle) ) ;
00036     //     SIO_PNTR( stream , &(particle->_mother0) ) ;
00037     //     SIO_PNTR( stream , &(particle->_mother1) ) ;
00038     
00039     // parents
00040     int numberOfParents ; 
00041     SIO_DATA( stream ,  &numberOfParents , 1  ) ;
00042     
00043     //     //    particle->prepareArrayOfParents( numberOfParents ) ;
00044     
00045     // ensure vector has correct number of elements
00046 //     for(int i=0;i<numberOfParents;i++){
00047 //       particle->_parents.push_back( 0 ) ;
00048 //     }
00049     particle->_parents.resize( numberOfParents ) ;
00050 
00051     for(int i=0;i<numberOfParents;i++){
00052       SIO_PNTR( stream , &(particle->_parents[i] ) ) ;
00053     }
00054 //     for(int i=0;i<numberOfParents;i++){
00055       
00056 //       // create a pointer to a pointer to a MCParticle 
00057 //       // as SIO need the address of the pointer for pointer reallocation....
00058 //       MCParticle** pD = new (MCParticle*) ;
00059 //       SIO_PNTR( stream , pD ) ;
00060 //       particle->_parents.push_back( pD ) ;
00061 //     }
00062     
00063     SIO_DATA( stream ,  &(particle->_pdg) , 1  ) ;
00064     SIO_DATA( stream ,  &(particle->_genstatus) , 1  ) ;
00065     int simstatus ;
00066     SIO_DATA( stream ,  &simstatus , 1  ) ;
00067     particle->_simstatus = simstatus ;
00068     //    SIO_DATA( stream ,  &(particle->_simstatus) , 1  ) ;
00069 
00070     SIO_DATA( stream ,  particle->_vertex  , 3 ) ;
00071 
00072     if( _vers > SIO_VERSION_ENCODE( 1, 2)   ) {
00073       SIO_DATA( stream ,  &(particle->_time)  , 1 ) ;
00074     }
00075     
00076     float momentum[3] ;
00077     SIO_DATA( stream ,  momentum  , 3 ) ;
00078     particle->setMomentum( momentum ) ;
00079 
00080     float mass ;
00081     SIO_DATA( stream , &mass , 1  ) ;
00082     particle->setMass( mass )  ;
00083 
00084     SIO_DATA( stream ,  &(particle->_charge) , 1  ) ;
00085     
00086     if( particle->_simstatus.test( MCParticle::BITEndpoint ) )  // bit 31
00087       SIO_DATA( stream ,  particle->_endpoint  , 3 ) ;
00088     
00089     return ( SIO_BLOCK_SUCCESS ) ;
00090   }
00091   
00092   unsigned int SIOParticleHandler::write(SIO_stream* stream, 
00093                                          const LCObject* obj){
00094 
00095     //fg 20030609 changed to use MCParticle    
00096     unsigned int status ; 
00097     
00098     const MCParticle* particle  = dynamic_cast<const MCParticle*>(obj)  ;
00099     SIO_PTAG( stream , particle ) ;
00100     
00101 //     const MCParticle* myMom0 = particle->getParent() ;
00102 //     const MCParticle* myMom1 = particle->getSecondParent() ;
00103 //     SIO_PNTR( stream , &myMom0 ) ;
00104 //     SIO_PNTR( stream , &myMom1 ) ;
00105 
00106     int numberOfParents = particle->getParents().size() ;
00107     
00108     SIO_DATA( stream , &numberOfParents  , 1 ) ;
00109     
00110     for(int i=0;i<numberOfParents;i++){
00111       const MCParticle* part = particle->getParents()[i] ;
00112       SIO_PNTR( stream ,  &part  ); 
00113     }
00114 
00115     LCSIO_WRITE( stream, particle->getPDG() ) ;
00116     LCSIO_WRITE( stream, particle->getGeneratorStatus() ) ;
00117 
00118 //     if( particle->getEndpoint() != 0 ) {
00119 //       int simstatus = particle->getSimulatorStatus()  ;
00120 //       simstatus |= 0x80000000 ; // set bit 31
00121 //       LCSIO_WRITE( stream, simstatus ) ;
00122 //     } else {
00123 //       LCSIO_WRITE( stream, particle->getSimulatorStatus() ) ;
00124 //     }
00125 
00126     LCSIO_WRITE( stream, particle->getSimulatorStatus() ) ;
00127 
00128     SIO_DATA( stream, const_cast<double*>( particle->getVertex() ) , 3 ) ;
00129 
00130     LCSIO_WRITE( stream, particle->getTime() ) ;
00131 
00132 //     SIO_DATA( stream, const_cast<float*>( particle->getMomentum()), 3 ) ;
00133 
00134     LCSIO_WRITE( stream, (float) particle->getMomentum()[0] ) ;
00135     LCSIO_WRITE( stream, (float) particle->getMomentum()[1] ) ;
00136     LCSIO_WRITE( stream, (float) particle->getMomentum()[2] ) ;
00137 
00138     LCSIO_WRITE( stream, (float) particle->getMass() ) ;
00139     LCSIO_WRITE( stream, particle->getCharge() ) ;
00140 
00141 
00142 //     if( particle->getEndpoint() != 0 ) 
00143     if( particle->getSimulatorStatus() &  (1<<MCParticle::BITEndpoint) )  // endpoint set !
00144       SIO_DATA( stream, const_cast<double*>( particle->getEndpoint() ) , 3 ) ;
00145 
00146 
00147     return ( SIO_BLOCK_SUCCESS ) ;
00148 
00149   }
00150 
00151   unsigned int SIOParticleHandler::readv00_08(SIO_stream* stream, 
00152                                               LCObject** objP){
00153     unsigned int status ; 
00154     
00155     // create a new object :
00156     MCParticleIOImpl* particle  = new MCParticleIOImpl ;
00157     *objP = particle ;
00158     
00159     // tell SIO the address of particle as an abstract  MCParticle ...
00160     // this is important, as SIO takes the bare address 
00161     // (int)(MCParticle*) particle != (int)particle !!!!
00162     SIO_PTAG( stream , dynamic_cast<MCParticle*>(particle) ) ;
00163 
00164     int ignorePointer ;
00165 //     SIO_PNTR( stream , &(particle->_mother0) ) ;
00166 //     SIO_PNTR( stream , &(particle->_mother1) ) ;
00167     SIO_DATA( stream , &ignorePointer  , 1  ) ;
00168     SIO_DATA( stream , &ignorePointer  , 1  ) ;
00169 
00170 
00171     // daughters
00172     int numberOfDaughters ; 
00173     SIO_DATA( stream ,  &numberOfDaughters , 1  ) ;
00174 
00175     //    particle->prepareArrayOfDaughters( numberOfDaughters ) ;
00176 
00177 
00178 //     for(int i=0;i<numberOfDaughters;i++){
00179 //     }
00180     particle->_daughters.resize( numberOfDaughters ) ;
00181 
00182     for(int i=0;i<numberOfDaughters;i++){
00183       SIO_PNTR( stream , &(particle->_daughters[i] ) ) ;
00184     }
00185 //     for(int i=0;i<numberOfDaughters;i++){
00186 //       // create a pointer to a pointer to a MCParticle 
00187 //       // as SIO need the address of the pointer for pointer reallocation....
00188 //       MCParticle** pD = new (MCParticle*) ;
00189 //       SIO_PNTR( stream , pD ) ;
00190 //       particle->_daughters.push_back( pD ) ;
00191 //     }
00192 
00193     SIO_DATA( stream ,  &(particle->_pdg) , 1  ) ;
00194     SIO_DATA( stream ,  &(particle->_genstatus) , 1  ) ;
00195     particle->_genstatus = 0 ;   // default value
00196     SIO_DATA( stream ,  particle->_vertex  , 3 ) ;
00197     SIO_DATA( stream ,  particle->_p  , 3 ) ;
00198     SIO_DATA( stream ,  &(particle->_mass) , 1  ) ;
00199     SIO_DATA( stream ,  &(particle->_charge) , 1  ) ;
00200     
00201     // if the particles doesn't have daughters we read its endpoint
00202     if( numberOfDaughters == 0 ) {
00203       SIO_DATA( stream ,  particle->_endpoint  , 3 ) ;
00204       particle->_genstatus = 0x80000000 ; // set bit31
00205     }
00206     return ( SIO_BLOCK_SUCCESS ) ;
00207   }
00208 
00209 
00210   void SIOParticleHandler::restoreParentDaughterRelations( EVENT::LCEvent* evt){
00211     
00212     const std::vector< std::string >* strVec = evt->getCollectionNames() ;
00213     std::vector< std::string >::const_iterator name ;
00214     
00215     for( name = strVec->begin() ; name != strVec->end() ; name++){
00216     
00217       LCCollection* col ;
00218       if( (col = evt->getCollection( *name ))->getTypeName() == LCIO::MCPARTICLE 
00219           &&  ! ( col->getFlag() & ( 1 << LCCollection::BITSubset) ) ) {
00220         
00221         int nDaughtersTotal = 0 ;
00222         int nParentsTotal = 0 ; 
00223         for(int i=0; i < col->getNumberOfElements() ; i++){
00224           MCParticleIOImpl* mcp = dynamic_cast<MCParticleIOImpl*>( col->getElementAt(i) ) ;
00225           nDaughtersTotal += mcp->getDaughters().size()  ;
00226           nParentsTotal += mcp->getParents().size() ;
00227         }
00228 
00229         for(int i=0; i < col->getNumberOfElements() ; i++){
00230           
00231           MCParticleIOImpl* mcp = dynamic_cast<MCParticleIOImpl*>( col->getElementAt(i) ) ;
00232           
00233           // for version v00-08 we restore parents from daughters
00234           if ( nParentsTotal == 0 &&  nDaughtersTotal > 0 ){
00235             
00236             int nDaughters = mcp->getDaughters().size() ;
00237             for( int j=0; j<nDaughters; j++){
00238               MCParticleIOImpl* dgh = dynamic_cast<MCParticleIOImpl*>( mcp->getDaughter(j) ) ;
00239               
00240 //            MCParticle ** mcpP = new (MCParticle*) ;
00241 //            *mcpP = mcp ;
00242 //            dgh->_parents.push_back( mcpP ) ;
00243               dgh->_parents.push_back( mcp ) ;
00244             }
00245             
00246           } 
00247           else if ( nParentsTotal > 0 && nDaughtersTotal == 0 ) {
00248             
00249             int nParents = mcp->getParents().size() ;
00250             for( int j=0; j<nParents; j++){
00251               MCParticleIOImpl* mom = dynamic_cast<MCParticleIOImpl*>( mcp->getParents()[j] ) ;
00252 //            MCParticle ** mcpP = new (MCParticle*) ;
00253 //            *mcpP = mcp ;
00254 //            mom->_daughters.push_back( mcpP ) ;
00255               mom->_daughters.push_back( mcp ) ;
00256               
00257             }
00258           }
00259 
00260         } // loop over particles
00261 
00262       } // if( MCPARTICLE ) 
00263     } // loop over collections
00264   }
00265 } // namespace

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7