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
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
00029 MCParticleIOImpl* particle = new MCParticleIOImpl ;
00030 *objP = particle ;
00031
00032
00033
00034
00035 SIO_PTAG( stream , dynamic_cast<MCParticle*>(particle) ) ;
00036
00037
00038
00039
00040 int numberOfParents ;
00041 SIO_DATA( stream , &numberOfParents , 1 ) ;
00042
00043
00044
00045
00046
00047
00048
00049 particle->_parents.resize( numberOfParents ) ;
00050
00051 for(int i=0;i<numberOfParents;i++){
00052 SIO_PNTR( stream , &(particle->_parents[i] ) ) ;
00053 }
00054
00055
00056
00057
00058
00059
00060
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
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 ) )
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
00096 unsigned int status ;
00097
00098 const MCParticle* particle = dynamic_cast<const MCParticle*>(obj) ;
00099 SIO_PTAG( stream , particle ) ;
00100
00101
00102
00103
00104
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
00119
00120
00121
00122
00123
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
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
00143 if( particle->getSimulatorStatus() & (1<<MCParticle::BITEndpoint) )
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
00156 MCParticleIOImpl* particle = new MCParticleIOImpl ;
00157 *objP = particle ;
00158
00159
00160
00161
00162 SIO_PTAG( stream , dynamic_cast<MCParticle*>(particle) ) ;
00163
00164 int ignorePointer ;
00165
00166
00167 SIO_DATA( stream , &ignorePointer , 1 ) ;
00168 SIO_DATA( stream , &ignorePointer , 1 ) ;
00169
00170
00171
00172 int numberOfDaughters ;
00173 SIO_DATA( stream , &numberOfDaughters , 1 ) ;
00174
00175
00176
00177
00178
00179
00180 particle->_daughters.resize( numberOfDaughters ) ;
00181
00182 for(int i=0;i<numberOfDaughters;i++){
00183 SIO_PNTR( stream , &(particle->_daughters[i] ) ) ;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 SIO_DATA( stream , &(particle->_pdg) , 1 ) ;
00194 SIO_DATA( stream , &(particle->_genstatus) , 1 ) ;
00195 particle->_genstatus = 0 ;
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
00202 if( numberOfDaughters == 0 ) {
00203 SIO_DATA( stream , particle->_endpoint , 3 ) ;
00204 particle->_genstatus = 0x80000000 ;
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
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
00241
00242
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
00253
00254
00255 mom->_daughters.push_back( mcp ) ;
00256
00257 }
00258 }
00259
00260 }
00261
00262 }
00263 }
00264 }
00265 }