SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
TrackSummary.cc
Go to the documentation of this file.
1 #include "TrackSummary.hh"
2 
3 // SLIC
4 #include "TrackUtil.hh"
5 #include "TrackManager.hh"
6 
7 // Geant4
8 #include "G4Track.hh"
9 #include "G4DecayProducts.hh"
10 #include "G4VProcess.hh"
11 #include "G4SystemOfUnits.hh"
12 
13 // LCIO
14 #include "EVENT/MCParticle.h"
15 
16 using EVENT::MCParticle;
17 using EVENT::MCParticleVec;
18 
19 namespace slic {
20 
21 G4Allocator<TrackSummary> TrackSummaryAllocator;
22 
24 
25 TrackSummary::TrackSummary(const G4Track* track, G4bool toBeSaved, G4bool backScattering) :
26  _parent(NULL), _toBeSaved(toBeSaved), _backScattering(backScattering), _mcparticle(NULL), _hasTrackerHit(false), _mcParticleIsUpToDate(false), _trackLength(0), _hepEvtStatus(0) {
27 
28  /* Set information of track that is immutable. */
29  _charge = track->GetDefinition()->GetPDGCharge();
30  _mass = track->GetDynamicParticle()->GetMass();
31  _PDG = track->GetDefinition()->GetPDGEncoding();
32  _trackID = track->GetTrackID();
33  _parentTrackID = track->GetParentID();
34 
35  /* The momentum appears to be set correctly before tracking occurs so set here along with energy. */
36  _momentum = track->GetMomentum();
37  _energy = track->GetTotalEnergy();
38 
39  /* Assign track time. */
40  // G4cout << "setting track time to " << track->GetGlobalTime() << G4endl;
41  _globalTime = track->GetGlobalTime();
42 }
43 
44 void TrackSummary::update(const G4Track* track) {
45 
46  //G4cout << "updating track " << this->_trackID << " from track " << track->GetTrackID() << G4endl;
47 
48  // TODO: Status flags to check; x means verified
49  // x BITCreatedInSimulation
50  // x BITBackscatter
51  // x BITEndpoint (seems to be always turned on for sim particles)
52  // x BITDecayedInTracker
53  // x BITLeftDetector
54  // x BITStopped
55  // BITDecayedInCalorimeter (this is the default!)
56  // BITVertexIsNotEndpointOfParent
57  // BITOverlay
58 
59  /* Set the vertex position. */
60  _vertex = track->GetVertexPosition();
61 
62  /* Set the end point. */
63  _endPoint = track->GetPosition();
64 
65  /* Set the track length. */
66  _trackLength = track->GetTrackLength();
67 
68  /* Set the generator status. */
69  _hepEvtStatus = 0;
70  if (_parentTrackID == 0) {
71  const G4DecayProducts* preAssignedDecayProducts = track->GetDynamicParticle()->GetPreAssignedDecayProducts();
72  if (preAssignedDecayProducts && preAssignedDecayProducts->entries() > 0)
73  _hepEvtStatus = 2;
74  else
75  _hepEvtStatus = 1;
76  }
77 
78  /* Initialize sim status. */
79  _simstatus = 0;
80 
81  /* This indicates the particle has an endpoint. */
82  _simstatus[MCParticle::BITEndpoint] = 1;
83 
84  /*
85  * Get the last step point.
86  */
87  const G4Step* lastStep = track->GetStep();
88  G4StepPoint* lastPostStepPoint = NULL;
89  if (lastStep)
90  lastPostStepPoint = lastStep->GetPostStepPoint();
91 
92  /* Check for back scattering. */
93  if (_hepEvtStatus == 0) {
94  if (getBackScattering())
95  _simstatus[MCParticle::BITBackscatter] = 1;
96  }
97 
98  /* Check if left detector. */
99  G4bool leftDetector = false;
100  if (lastPostStepPoint) {
101  /* Tracks with the last step on the world boundary are flagged as LeftDetector. */
102  if ((leftDetector = (lastPostStepPoint->GetStepStatus() == fWorldBoundary))) {
103  _simstatus[MCParticle::BITLeftDetector] = 1;
104  }
105  }
106 
107  // BITStopped
108  if (track->GetKineticEnergy() <= 0.)
109  _simstatus[MCParticle::BITStopped] = 1;
110 
111  G4bool insideTrackerRegion = TrackUtil::getRegionInformation(track)->getStoreSecondaries();
112 
113  if (insideTrackerRegion)
114  _simstatus[MCParticle::BITDecayedInTracker] = 1;
115 
116  // BITDecayedInCalorimeter
117  // FIXME: This should not be the default.
118  if (!insideTrackerRegion && !leftDetector)
119  _simstatus[MCParticle::BITDecayedInCalorimeter] = 1;
120 
121  // BITVertexIsNotEndpointOfParent
122  TrackSummary* myParent = findParent();
123  // TODO: Check if parent is EVER available here.
124  if (myParent)
125  if (myParent->getEndPoint().isNear(getVertex()))
126  _simstatus[MCParticle::BITVertexIsNotEndpointOfParent] = 1;
127 
128  // set momentum at endpoint from the current momentum
129  _momentumAtEndpoint = track->GetMomentum();
130 }
131 
134  buildMCParticle();
135  else
136  // In LCIO mode, if the theMCParticle exists we have already
137  // passed by here.
138  return;
139 
140  TrackSummary* myParent = findParent();
141  /*
142  if (myParent == this) {
143  G4cout << "summary for " << this->getTrackID() << " is its own parent (probably a primary)" << G4endl;
144  }
145  */
146  if (myParent) {
147 
148  // std::cout << " **** TrackSummary::SetParentToBeSaved for trackID: " << myParent->GetTrackID() << " this trackid: " << GetTrackID() << std::endl ;
149 
150  myParent->_toBeSaved = true;
151  myParent->setParentToBeSaved();
152 
153  MCParticle* theParentMCParticle = myParent->getMCParticle();
154 
155  // fg: need to check if particle already has this parent assigned ...
156  // this will be fixed in a future (>1.6) LCIO version
157  const MCParticleVec& parents = _mcparticle->getParents();
158 
159  // I think this check for existing parent can probably be removed. --JM
160  if (std::find(parents.begin(), parents.end(), theParentMCParticle) == parents.end()) {
161 
162  // fix B.Vormwald: only add parent if particle created in simulation
163  if (_mcparticle->getGeneratorStatus() == 0) {
164  _mcparticle->addParent(theParentMCParticle);
165  }
166  }
167  // DEBUG
168  } else {
169  if (this->getParentID() != 0)
170  G4cout << "WARNING: no parent track ID " << this->getParentID() << " found for " << this->getTrackID() << G4endl;
171  }
172  }
173 
175 
176  TrackSummary* trackSummary = 0;
177  if (_parentTrackID == 0) {
178  return trackSummary;
179  }
180 
182 }
183 
185  // if we have an MCParticle already we just need to update it
186  // otherwise we need to create one ...
187  if (_mcparticle == 0) {
188  _mcparticle = new MCParticleImpl;
189  _simstatus[MCParticle::BITCreatedInSimulation] = 1; // created in simulation
190  _mcparticle->setGeneratorStatus(getHepEvtStatus());
191  _mcparticle->setPDG(getPDG());
192  }
193 
194  /* Set charge. */
195  _mcparticle->setCharge(getCharge());
196 
197  /* Set endpoint. */
198  double endpoint[3];
199  endpoint[0] = getEndPoint()(0);
200  endpoint[1] = getEndPoint()(1);
201  endpoint[2] = getEndPoint()(2);
202  _mcparticle->setEndpoint(endpoint);
203 
204  /* Set mass. */
205  _mcparticle->setMass(_mass / GeV);
206 
207  /* Set simulator status. */
208  _mcparticle->setSimulatorStatus(getSimulatorStatus());
209 
210  /* Set momentum. */
211  float p[3];
212  G4ThreeVector momentum = getMomentum();
213  p[0] = momentum(0) / GeV;
214  p[1] = momentum(1) / GeV;
215  p[2] = momentum(2) / GeV;
216  _mcparticle->setMomentum(p);
217 
218  /* Set vertex. */
219  double vertex[3];
220  vertex[0] = getVertex()(0);
221  vertex[1] = getVertex()(1);
222  vertex[2] = getVertex()(2);
223  _mcparticle->setVertex(vertex);
224 
225  /* Set global time. */
226  _mcparticle->setTime(_globalTime);
227 
228  /* Set momentum at endpoint. */
229  float momentumAtEndpoint[3];
230  momentumAtEndpoint[0] = _momentumAtEndpoint(0) / GeV;
231  momentumAtEndpoint[1] = _momentumAtEndpoint(1) / GeV;
232  momentumAtEndpoint[2] = _momentumAtEndpoint(2) / GeV;
233  _mcparticle->setMomentumAtEndpoint(momentumAtEndpoint);
234 
235  /* Set up to date. */
236  _mcParticleIsUpToDate = true;
237 }
238 
239 }
static TrackManager * instance()
Manages access to stored track information used for MCParticle output.
Definition: TrackManager.hh:23
TrackSummary * findTrackSummary(G4int trackID)
Definition: TrackManager.hh:96
Container for all information from G4Track that may be persisted to MCParticle output collection.
Definition: TrackSummary.hh:33
G4int getPDG() const
G4double getCharge() const
Definition: TrackSummary.hh:68
G4int getSimulatorStatus() const
void update(const G4Track *track)
Definition: TrackSummary.cc:44
MCParticleImpl * getMCParticle()
TrackSummary(const G4Track *track, G4bool toBeSaved, G4bool backScattering=false)
Definition: TrackSummary.cc:25
G4bool _mcParticleIsUpToDate
G4ThreeVector getEndPoint() const
Definition: TrackSummary.hh:76
G4ThreeVector _momentumAtEndpoint
G4int getParentID() const
std::bitset< 32 > _simstatus
G4ThreeVector _momentum
const G4ThreeVector & getVertex() const
G4ThreeVector getMomentum() const
G4ThreeVector _endPoint
static TrackManager * m_trackManager
TrackSummary * findParent() const
MCParticleImpl * _mcparticle
G4bool getBackScattering() const
G4int getTrackID() const
G4int getHepEvtStatus() const
G4ThreeVector _vertex
static UserRegionInformation * getRegionInformation(const G4Track *track)
Definition: TrackUtil.hh:29
G4Allocator< TrackSummary > TrackSummaryAllocator
Definition: TrackSummary.cc:21