00001
00002 #include "LcioMcpFactory.hh"
00003
00004
00005 #include "EventSourceManager.hh"
00006 #include "LcioManager.hh"
00007 #include "LcioMcpManager.hh"
00008 #include "LcioMcpStatusSetter.hh"
00009 #include "LcioMcpUtil.hh"
00010 #include "Trajectory.hh"
00011 #include "TrajectoryManager.hh"
00012
00013
00014 #include "globals.hh"
00015 #include "G4Event.hh"
00016 #include "G4EventManager.hh"
00017 #include "G4TrajectoryContainer.hh"
00018
00019
00020 #include "IMPL/LCCollectionVec.h"
00021 #include "IMPL/LCGenericObjectImpl.h"
00022
00023 using IMPL::LCCollectionVec;
00024 using IMPL::LCGenericObjectImpl;
00025 using IMPL::MCParticleImpl;
00026 using EVENT::LCIO;
00027 using EVENT::MCParticle;
00028
00029 namespace slic
00030 {
00031 LcioMcpFactory::LcioMcpFactory(LcioMcpManager* manager)
00032 : Module( "LcioMcpFactory" ),
00033 m_manager(manager),
00034 m_finalColl(0),
00035 m_currentTrajectoryContainer(0)
00036 {}
00037
00038 LcioMcpFactory::~LcioMcpFactory()
00039 {}
00040
00041 void LcioMcpFactory::createFinalMcpCollection(const G4Event* event)
00042 {
00043
00044 m_finalColl = static_cast<LCCollectionVec*>(m_manager->getFinalMcpCollection());
00045
00046
00047 m_currentTrajectoryContainer = event->GetTrajectoryContainer();
00048
00049
00050 if ( EventSourceManager::instance()->isFileSource() ) {
00051
00052 createFinalMcpCollectionFromInitial( m_manager->getInitialMcpCollection() );
00053 }
00054
00055 else
00056 {
00057
00058 createFinalMcpCollectionFromTrajectoryContainer( m_currentTrajectoryContainer );
00059 }
00060
00061
00062 fillMcpEndPointEnergy(m_finalColl);
00063 }
00064
00065 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromTrajectory( Trajectory* trj)
00066 {
00067 #ifdef SLIC_LOG
00068 log() << LOG::debug << "createMcpFromTrajectory() - trackId <" << trj->GetTrackID() << ">." << LOG::done;
00069 #endif
00070
00071
00072 MCParticleImpl* mcp = new MCParticleImpl();
00073
00074
00075 mcp->setPDG( trj->GetPDGEncoding() );
00076
00077
00078 G4ThreeVector mom = trj->GetInitialMomentum() / GeV;
00079 float fmom[3] = { mom.x(), mom.y(), mom.z() };
00080 mcp->setMomentum( fmom );
00081
00082
00083 mcp->setMass( trj->GetParticleDefinition()->GetPDGMass() / GeV );
00084
00085
00086 mcp->setCharge( trj->GetCharge() );
00087
00088
00089 G4int npts = trj->GetPointEntries();
00090
00091
00092 G4VTrajectoryPoint* beginTrjPnt = trj->GetPoint( 0 );
00093 G4ThreeVector beginPos = beginTrjPnt->GetPosition() / mm;
00094 double fBeginPos[3] = { beginPos.x(), beginPos.y(), beginPos.z() };
00095 mcp->setVertex( fBeginPos );
00096
00097
00098 G4VTrajectoryPoint* endTrjPnt = trj->GetPoint( npts - 1 );
00099 G4ThreeVector endPos = endTrjPnt->GetPosition() / mm;
00100 double fEndPos[3] = { endPos.x(), endPos.y(), endPos.z() };
00101 mcp->setEndpoint( fEndPos );
00102
00103
00104 mcp->setTime( trj->GetGlobalTime() );
00105
00106
00107 LcioMcpStatusSetter::setMcpStatusCodesFromTrajectory( trj, mcp );
00108
00109
00110 #ifdef SLIC_LOG
00111 log() << LOG::debug << "adding trackId <" << trj->GetTrackID() << "> to MCP <" << mcp << "> link." << LOG::done;
00112 #endif
00113 m_manager->getMaps()->addTrackIDToMcpLink( trj->GetTrackID(), mcp );
00114
00115 return mcp;
00116 }
00117
00118
00119 void LcioMcpFactory::createFinalMcpCollectionFromTrajectoryContainer(G4TrajectoryContainer* m_currentTrajectoryContainer)
00120 {
00121 if ( m_currentTrajectoryContainer )
00122 {
00123
00124
00125 int n_trj = m_currentTrajectoryContainer->entries();
00126 for ( int i = 0;
00127 i < n_trj;
00128 i++ )
00129 {
00130 Trajectory* trj = static_cast<Trajectory*> ( ( *m_currentTrajectoryContainer )[i] );
00131
00132
00133 if ( trj->GetParentID() == 0 )
00134 {
00135 #ifdef SLIC_LOG
00136 log() << LOG::debug << "Making primary MCParticle for trajectory with trackID <" << trj->GetTrackID() << ">." << LOG::done;
00137 #endif
00138
00139
00140 MCParticleImpl* mcp = createMcpFromTrajectory( trj );
00141
00142
00143 m_finalColl->addElement( mcp );
00144
00145
00146 #ifdef SLIC_LOG
00147 log() << LOG::debug << "Making primary MCParticle for trajectory with trackID <" << trj->GetTrackID() << ">." << LOG::done;
00148 #endif
00149 addMcpDaughtersFromTrajectoryContainer( mcp, trj->GetTrackID() );
00150 }
00151 }
00152 }
00153 else
00154 {
00155 G4Exception("G4TrajectoryContainer is null!");
00156 }
00157
00158
00159 LcioMcpManager::instance()->getMaps()->printTrackToMcpMap();
00160
00161 }
00162
00163 void LcioMcpFactory::addMcpDaughtersFromTrajectoryContainer( MCParticleImpl* parMcp,
00164 int parTrkID)
00165 {
00166 #ifdef SLIC_LOG
00167 log() << LOG::debug << "addMcpDaughtersFromTraj - parTrkId <" << parTrkID << ">." << LOG::done;
00168 #endif
00169
00170
00171 int n_trj = m_currentTrajectoryContainer->entries();
00172 for ( int i = 0;
00173 i < n_trj;
00174 i++ )
00175 {
00176 Trajectory* trj = static_cast<Trajectory*> ( ( *m_currentTrajectoryContainer)[i] );
00177
00178 if ( trj->GetParentID() == parTrkID )
00179 {
00180
00181
00182 MCParticleImpl* dauMcp = m_manager->getMaps()->findMcpFromTrackID( trj->GetTrackID() );
00183
00184 if ( dauMcp == 0 )
00185 {
00186 dauMcp = createMcpFromTrajectory( trj );
00187 }
00188
00189 if ( dauMcp == 0 )
00190 G4Exception("Failed to create MCParticle.");
00191
00192
00193 m_finalColl->addElement( dauMcp );
00194
00195
00196 addMcpDaughtersFromTrajectoryContainer( dauMcp, trj->GetTrackID() );
00197
00198
00199 dauMcp->addParent( parMcp );
00200 }
00201 }
00202 }
00203
00204
00205 void LcioMcpFactory::createFinalMcpCollectionFromInitial(EVENT::LCCollection* mcpVecInitial)
00206 {
00207
00208 if ( mcpVecInitial ) {
00209
00210
00211 int numInitMcp = mcpVecInitial->getNumberOfElements();
00212
00213
00214 if ( numInitMcp > 0 ) {
00215
00216
00217 for ( int i=0; i < numInitMcp; i++ ) {
00218
00219 #ifdef SLIC_LOG
00220 log() << LOG::debug << "proc initial MCP: " << i << LOG::done;
00221 #endif
00222
00223
00224 MCParticleImpl* mcp = static_cast<MCParticleImpl*> ( mcpVecInitial->getElementAt( i ) );
00225
00226
00227 if ( LcioMcpUtil::isPrimary( mcp ) ) {
00228
00229 #ifdef SLIC_LOG
00230 log() << LOG::debug << "isPrimary" << LOG::done;
00231 #endif
00232
00233 createMcpFromInitialRecurse( mcp );
00234 }
00235 }
00236 }
00237 else {
00238 G4Exception("Initial McpVec has no members.");
00239 }
00240 }
00241 else {
00242 G4Exception("Initial McpVec ptr is null.");
00243 }
00244 }
00245
00246 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromInitialRecurse(EVENT::MCParticle* mcpInit)
00247 {
00248 #ifdef SLIC_LOG
00249 log() << LOG::debug << "createMcpFromInitialRecurse: " << m_manager->getMCParticleIndex(m_manager->getInitialMcpCollection(), mcpInit) << LOG::done;
00250 #endif
00251
00252 MCParticleImpl* mcp = 0;
00253
00254
00255 G4PrimaryParticle* g4primary = m_manager->getMaps()->findPrimaryFromMcp( mcpInit );
00256
00257
00258 if( !g4primary ) {
00259
00260 #ifdef SLIC_LOG
00261 log() << LOG::debug << "initialOnly" << LOG::done;
00262 #endif
00263
00264
00265 mcp = createMcpFromInitialOnly( mcpInit );
00266 }
00267
00268 else {
00269
00270 #ifdef SLIC_LOG
00271 log() << LOG::debug << "fromPrimary" << LOG::done;
00272 #endif
00273
00274
00275 mcp = createMcpFromPrimary( g4primary, mcpInit );
00276 }
00277
00278
00279 return mcp;
00280 }
00281
00282 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromInitialOnly(EVENT::MCParticle* mcpInit)
00283 {
00284 log() << LOG::debug << "createMcpFromInitialOnly: " << m_manager->getMCParticleIndex(m_manager->getInitialMcpCollection(), mcpInit) << LOG::done;
00285
00286
00287 MCParticleImpl* mcp = createMcpShallowCopy( mcpInit );
00288
00289
00290 m_manager->getMaps()->addInitialMcpToFinalMcpLink( mcpInit, mcp);
00291
00292
00293 addMcpDaughtersFromInitial( mcp, mcpInit );
00294
00295
00296 m_finalColl->addElement( mcp );
00297
00298 return mcp;
00299 }
00300
00301 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromPrimary(G4PrimaryParticle* primary,
00302 EVENT::MCParticle* mcpInit)
00303 {
00304 #ifdef SLIC_LOG
00305 log() << LOG::done << "createMcpFromPrimary - MCP idx <" << m_manager->getMCParticleIndex( m_manager->getInitialMcpCollection(), mcpInit) << ">" << LOG::done;
00306 #endif
00307
00308 int trkID = primary->GetTrackID();
00309
00310 #ifdef SLIC_LOG
00311 log() << LOG::done << "primary TID <" << trkID << ">" << LOG::done;
00312 #endif
00313
00314 MCParticleImpl* mcp = 0;
00315 Trajectory* trj = TrajectoryManager::instance()->findTrajectory( trkID );
00316
00317
00318 if ( !trj ) {
00319
00320 #ifdef SLIC_LOG
00321 log() << LOG::debug << "initialAndPrimary" << LOG::done;
00322 #endif
00323
00324
00325 mcp = createMcpFromInitialAndPrimary( primary, mcpInit );
00326
00327 }
00328
00329 else {
00330
00331 #ifdef SLIC_LOG
00332 log() << LOG::debug << "initialAndTrajectory" << LOG::done;
00333 #endif
00334
00335
00336 mcp = createMcpFromInitialAndTrajectory( trj, mcpInit );
00337
00338 }
00339
00340
00341 mcp->setGeneratorStatus( mcpInit->getGeneratorStatus() );
00342
00343
00344 mcp->setCreatedInSimulation( false );
00345
00346
00347 m_manager->getMaps()->addInitialMcpToFinalMcpLink( mcpInit, mcp);
00348
00349
00350 #ifdef SLIC_LOG
00351 log() << LOG::debug << "createMcpFromPrimary() - adding trackId <" << trj->GetTrackID() << "> to MCP <" << mcp << "> link." << LOG::done;
00352 #endif
00353
00354
00355 m_manager->getMaps()->addTrackIDToMcpLink( trkID, mcp );
00356
00357 return mcp;
00358 }
00359
00360 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromInitialAndPrimary(G4PrimaryParticle* primary,
00361 EVENT::MCParticle* mcpInit)
00362 {
00363 log() << LOG::done << "createMcpFromInitialAndPrimary: " << m_manager->getMCParticleIndex( m_manager->getInitialMcpCollection(), mcpInit) << LOG::done;
00364
00365
00366
00367
00368
00369 MCParticleImpl* mcp = createMcpFromPrimaryShallowCopy( primary );
00370
00371
00372 double vtx[3] = { mcpInit->getVertex()[0], mcpInit->getVertex()[1], mcpInit->getVertex()[2] };
00373 mcp->setVertex( vtx );
00374
00375
00376 createDaughtersFromPrimary( primary,
00377 mcpInit,
00378 mcp
00379 );
00380
00381 #ifdef SLIC_DEBUG
00382 if ( mcp->getDaughters().size() == 0 )
00383 {
00384 #ifdef SLIC_LOG
00385 log() << LOG::debug << "No Mcp daughters added." << LOG::done;
00386 #endif
00387 }
00388 #endif
00389
00390
00391 m_finalColl->addElement( mcp );
00392
00393 return mcp;
00394 }
00395
00396 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromInitialAndTrajectory(Trajectory* trj,
00397 EVENT::MCParticle* mcpInit)
00398 {
00399 #ifdef SLIC_LOG
00400 log() << LOG::done << "createMcpFromInitialAndTrajectory: " << m_manager->getMCParticleIndex( m_manager->getInitialMcpCollection(), mcpInit ) << LOG::done;
00401 #endif
00402
00403
00404 IMPL::MCParticleImpl* mcp = createMcpFromTrajectory( trj );
00405
00406
00407 LcioMcpStatusSetter::setGeneratorStatus( mcpInit, mcp );
00408
00409
00410 int numTrj = m_currentTrajectoryContainer->entries();
00411
00412 #ifdef SLIC_LOG
00413 log() << LOG::done << "nTrajectoryDau: " << numTrj << LOG::done;
00414 #endif
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 for( int j=0; j < numTrj; j++ )
00425 {
00426
00427 Trajectory* thisTrj = static_cast<Trajectory*> ( ( *m_currentTrajectoryContainer ) [j] );
00428
00429
00430 if ( thisTrj->GetParentID() == trj->GetTrackID() )
00431 {
00432 #ifdef SLIC_LOG
00433 log() << LOG::debug << "Trajectory with trackID <" << trj->GetTrackID() << "> has parent with trackID <" << thisTrj->GetParentID() << ">" << LOG::done;
00434 #endif
00435
00436
00437 MCParticle* mcpPrim = m_manager->getMaps()->findPrimaryInitialMcpFromTrajectory( thisTrj );
00438
00439
00440 MCParticleImpl* mcpDauNew = createMcpFromInitialAndTrajectory( thisTrj, mcpPrim );
00441
00442
00443 mcpDauNew->addParent( mcp );
00444 }
00445
00446
00447
00448
00449
00450
00451 }
00452
00453
00454 m_finalColl->addElement( mcp );
00455
00456 return mcp;
00457 }
00458
00459 IMPL::MCParticleImpl* LcioMcpFactory::createMcpShallowCopy(EVENT::MCParticle* mcp)
00460 {
00461
00462 IMPL::MCParticleImpl* mcpNew = new IMPL::MCParticleImpl();
00463
00464 mcpNew->setPDG( mcp->getPDG() );
00465
00466 float p[3] = { mcp->getMomentum()[0], mcp->getMomentum()[1], mcp->getMomentum()[2] };
00467 mcpNew->setMomentum( p );
00468
00469 mcpNew->setMass( mcp->getMass() );
00470
00471 double vtx[3] = { mcp->getVertex()[0], mcp->getVertex()[1], mcp->getVertex()[2] };
00472 mcpNew->setVertex( vtx );
00473
00474 mcpNew->setGeneratorStatus( mcp->getGeneratorStatus() );
00475
00476 mcpNew->setSimulatorStatus( 0 );
00477
00478 mcpNew->setCharge( mcp->getCharge() );
00479
00480 mcpNew->setTime( mcp->getTime() );
00481
00482 return mcpNew;
00483 }
00484
00485 IMPL::MCParticleImpl* LcioMcpFactory::createMcpFromPrimaryShallowCopy(G4PrimaryParticle* primary)
00486 {
00487
00488 MCParticleImpl* mcp = new MCParticleImpl();
00489
00490
00491 mcp->setPDG( primary->GetPDGcode() );
00492
00493
00494 G4ThreeVector pVec = primary->GetMomentum() / GeV;
00495 float p[3] = { pVec.x(), pVec.y(), pVec.z() };
00496 mcp->setMomentum( p );
00497
00498
00499 mcp->setMass( primary->GetMass() / GeV );
00500
00501
00502 mcp->setCharge( LcioMcpManager::m_NAN );
00503
00504 return mcp;
00505 }
00506
00507
00508 void LcioMcpFactory::createDaughtersFromPrimary(G4PrimaryParticle* primary,
00509 EVENT::MCParticle* mcpInit,
00510 IMPL::MCParticleImpl* mcpPar)
00511 {
00512
00513 #ifdef SLIC_LOG
00514 log() << LOG::debug << "LcioMcpFactory::createDaughtersFromPrimary()" << LOG::done;
00515 log() << LOG::debug << "primary <" << primary << ">" << LOG::done;
00516 log() << LOG::debug << "trkID <" << primary->GetTrackID() << ">" << LOG::done;
00517 log() << LOG::debug << "mcpInit <" << mcpInit << ">" << LOG::done;
00518 log() << LOG::debug << "mcpPar <" << mcpPar << ">" << LOG::done;
00519 #endif
00520
00521
00522 G4PrimaryParticle* primDau = primary->GetDaughter();
00523 while ( primDau ) {
00524
00525
00526 MCParticle* mcpDau = m_manager->getMaps()->findDaughterMcpFromPrimary( mcpInit,
00527 primDau );
00528
00529
00530 if ( 0 == mcpDau ) {
00531 G4Exception( "Mcp daughter was not found." );
00532 }
00533
00534
00535 MCParticleImpl* mcpDauNew = createMcpFromPrimary( primDau, mcpDau );
00536
00537
00538 mcpDauNew->addParent( mcpPar );
00539
00540
00541 primDau = primDau->GetNext();
00542 }
00543 }
00544
00545 void LcioMcpFactory::addMcpDaughtersFromInitial(IMPL::MCParticleImpl* mcpNew,
00546 EVENT::MCParticle* mcpInit)
00547 {
00548 #ifdef SLIC_LOG
00549 log() << LOG::debug << "addMcpDaughtersFromInitial: " << m_manager->getMCParticleIndex(m_manager->getInitialMcpCollection(), mcpInit) << LOG::done;
00550 #endif
00551
00552 int numDau = mcpInit->getDaughters().size();
00553
00554 for ( int i=0; i < numDau; i++ ) {
00555
00556
00557 MCParticle* mcpChildInit = mcpInit->getDaughters()[i];
00558
00559
00560 MCParticleImpl* mcpChildFinal = m_manager->getMaps()->findFinalParticleFromInitial( mcpChildInit );
00561
00562
00563 if ( mcpChildFinal == 0 ) {
00564
00565
00566 mcpChildFinal = createMcpFromInitialRecurse( mcpChildInit );
00567 }
00568
00569
00570 mcpChildFinal->addParent( mcpNew );
00571 }
00572 }
00573
00574 void LcioMcpFactory::fillMcpEndPointEnergy(LCCollectionVec* mcpColl)
00575 {
00576
00577 LcioMcpMaps* maps = LcioMcpManager::instance()->getMaps();
00578 LCCollectionVec* epColl = new LCCollectionVec( LCIO::LCGENERICOBJECT );
00579 for ( LCCollectionVec::iterator it = mcpColl->begin();
00580 it != mcpColl->end();
00581 it++ ) {
00582
00583 MCParticle* mcp = static_cast<MCParticle*>(*it);
00584 G4int trkID = maps->findTrackIDFromFinalMcp(mcp);
00585 double epE = -1.0;
00586 if ( trkID != -1 ) {
00587 Trajectory* trj = TrajectoryManager::instance()->findTrajectory(trkID);
00588
00589 if ( 0 != trj ) {
00590 epE = trj->getEndPointEnergy();
00591 }
00592 }
00593
00594 LCGenericObjectImpl* obj = new LCGenericObjectImpl();
00595 obj->setFloatVal( 0, epE );
00596 epColl->push_back( obj );
00597 }
00598 LcioManager::instance()->getCurrentLCEvent()->addCollection( epColl, "MCParticleEndPointEnergy" );
00599 }
00600 }