SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
TrackManager.cc
Go to the documentation of this file.
1 #include "TrackManager.hh"
2 
3 // SLIC
4 #include "LcioManager.hh"
5 #include "MCParticleManager.hh"
6 #include "StdHepGenerator.hh"
7 
8 // LCDD
9 #include "lcdd/hits/TrackerHit.hh"
10 
11 // LCIO
12 #include "EVENT/LCIO.h"
13 #include "EVENT/LCEvent.h"
14 
15 #include <cassert>
16 
17 using IMPL::LCCollectionVec;
18 using EVENT::LCCollection;
19 using EVENT::LCEvent;
20 
21 namespace slic {
22 
23 void TrackManager::saveTrackSummaries(const G4Event* anEvent, LCEvent* lcEvent) {
24 
25  /* If Geant4 event generation was used, then an empty MCParticle collection needs to be created. */
26  G4bool geant4Generator = false;
27  if (MCParticleManager::instance()->getMCParticleCollection() == NULL) {
28  //std::cout << "creating empty MCParticle collection" << std::endl;
29  MCParticleManager::instance()->createMCParticleCollection();
30  geant4Generator = true;
31  }
32 
33  /* Copy MCParticle pointers into the corresponding TrackSummary objects for primary particles. */
34  PrimaryParticleMap* particleMap = MCParticleManager::instance()->getPrimaryParticleMap();
35  for (PrimaryParticleMapIterator it = particleMap->begin(); it != particleMap->end(); it++) {
36  if (it->second == 0) {
37  G4Exception("", "", FatalException, "MCParticle was mapped to null G4PrimaryParticle.");
38  }
39  if (it->second->GetTrackID() < 0)
40  continue;
41  for (int j = _trackSummaries->size() - 1; j >= 0; j--) {
42  if (_trackSummaries->operator[](j)->getTrackID() == it->second->GetTrackID()) {
43  //std::cout << "setting MCParticle " << it->first << " on TrackSummary from primary" << std::endl;
44  _trackSummaries->operator[](j)->setMCParticle(dynamic_cast<MCParticleImpl*>(it->first));
45 
46 #ifdef SLIC_LOG
47  log() << LOG:: debug << "primary " << MCParticleManager::instance()->indexOf(it->second)
48  << " had track ID " << _trackSummaries->operator[](j)->getTrackID() << LOG::done;
49 #endif
50 
51  break;
52  }
53  }
54  }
55 
56  G4bool writeCompleteEvent = LcioManager::instance()->getWriteCompleteEvent();
57 
58  // Set parents to be saved on particles that will be persisted.
59  for (G4int k = _trackSummaries->size() - 1; k >= 0; k--) {
60  // Save all TrackSummary objects if full event is being written.
61  //if (writeCompleteEvent)
62  // (*_trackSummaries)[k]->setToBeSaved();
63  // Force parents to be saved if TrackSummary will be saved.
64  if (((*_trackSummaries)[k])->getToBeSaved())
65  ((*_trackSummaries)[k])->setParentToBeSaved();
66  }
67 
68  /* Get the MCParticle collection created by event generation. */
69  LCCollectionVec* mcpVec = MCParticleManager::instance()->getMCParticleCollection();
70 
71  /* Save TrackSummary objects to LCIO collection. */
72 #ifdef SLIC_LOG
73  /*log() << LOG::okay << "TrackManager processing " << _trackSummaries->size() << " TrackSummary objects." << LOG::done;*/
74 #endif
75  size_t l;
76  TrackSummary* trackSummary;
77  for (l = 0; l < _trackSummaries->size(); l++) {
78  trackSummary = (*_trackSummaries)[l];
79  //| writeCompleteEvent
80  if (trackSummary->getToBeSaved()) {
81 
82  /* Build the MCParticle in case it hasn't been created yet. */
83  if (trackSummary->getMCParticle() == 0) {
84  trackSummary->buildMCParticle();
85  }
86 
87  /* Associate the track ID to the MCParticle for the hit maker. */
88  MCParticleManager::instance()->addMCParticleTrackID(trackSummary->getTrackID(), trackSummary->getMCParticle());
89 
90  /*
91  * Only particles created in the simulation need to be added, as the generator MCParticles
92  * associated with primaries were already. But if a Geant4 internal generator was used
93  * such as the GPS or ParticleGun, then all particles with the save flag on should be added
94  * here, as they will all be flagged as sim particles and would not have been added yet.
95  */
96  if (trackSummary->getMCParticle()->getGeneratorStatus() == 0 || geant4Generator) {
97  if (trackSummary->getMCParticle() != NULL) {
98  mcpVec->push_back(trackSummary->getMCParticle());
99  // DEBUG
100  //if (trackSummary->getParentID() <= 0)
101  // G4cout << "WARNING: sim particle with track ID " << trackSummary->getTrackID() << " has no parent!" << G4endl;
102  }
103  }
104  } else {
105  // If the track will not be saved then associate the information to its first persisted ancestor.
106  // This will allow association between track IDs from sensitive detector's in LCDD to their appropriate
107  // MCParticle objects in the output collection.
108  findFirstSavedAncestor(trackSummary);
109  }
110  }
111 
112  /* Set flag so it is saved. */
113  mcpVec->setFlag(0xe0000000);
114 
115  /* Save the MCParticle collection into the event. */
116  lcEvent->addCollection((LCCollection*) mcpVec, "MCParticle");
117 
118 #ifdef SLIC_LOG
119  //log() << LOG::always << "Saved " << mcpVec->getNumberOfElements() << " MCParticles." << LOG::done;
120 #endif
121 
122 }
123 
125  TrackSummary* parent = trackSummary->findParent();
126  while (parent != 0) {
127  if (parent->getToBeSaved() == true) {
128  /* Associate the track ID to the first MCParticle up the tree that will be saved. */
129  MCParticleManager::instance()->addMCParticleTrackID(trackSummary->getTrackID(), parent->getMCParticle());
130  break;
131  }
132  parent = parent->findParent();
133  }
134  return parent;
135 }
136 
137 }
static MCParticleManager * instance()
LogStream & log()
Definition: Module.hh:99
TrackSummary * findFirstSavedAncestor(TrackSummary *trackSummary)
TrackSummaryVector * _trackSummaries
void saveTrackSummaries(const G4Event *event, LCEvent *lcevent)
Definition: TrackManager.cc:23
Container for all information from G4Track that may be persisted to MCParticle output collection.
Definition: TrackSummary.hh:33
MCParticleImpl * getMCParticle()
G4bool getToBeSaved() const
TrackSummary * findParent() const
G4int getTrackID() const
@ debug
Definition: LogStream.hh:33
@ done
Definition: LogStream.hh:26
map< MCParticle *, G4PrimaryParticle * > PrimaryParticleMap
PrimaryParticleMap::iterator PrimaryParticleMapIterator