CbmRoot
CbmStack.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmStack source file -----
3 // ----- Created 10/08/04 by D. Bertini / V. Friese -----
4 // -------------------------------------------------------------------------
5 #include "CbmStack.h"
6 
7 #include "CbmMCTrack.h"
8 #include "FairDetector.h"
9 #include "FairMCPoint.h"
10 
11 #include "FairRootManager.h"
12 
13 #include "TClonesArray.h"
14 #include "TLorentzVector.h"
15 #include "TParticle.h"
16 #include "TRefArray.h"
17 
18 #include <cassert>
19 #include <list>
20 
21 
22 using std::make_pair;
23 using std::pair;
24 using std::vector;
25 
26 
27 // ----- Default constructor -------------------------------------------
28 CbmStack::CbmStack(Int_t size)
29  : FairGenericStack()
30  , fStack()
31  , fFilter(new CbmStackFilter)
32  , fParticles(new TClonesArray("TParticle", size))
33  , fTracks(new TClonesArray("CbmMCTrack", size))
34  , fIndexMap()
35  , fIndexIter()
36  , fPointsMap()
37  , fCurrentTrack(-1)
38  , fNPrimaries(0)
39  , fNParticles(0)
40  , fNTracks(0)
41  , fIndex(0)
42  , fStoreSecondaries(kTRUE)
43  , fMinPoints(1)
44  , fEnergyCut(0.)
45  , fStoreMothers(kTRUE)
46  , fdoTracking(kTRUE) {}
47 
48 // -------------------------------------------------------------------------
49 
50 
51 // ----- Destructor ----------------------------------------------------
53  if (fParticles) {
54  fParticles->Delete();
55  delete fParticles;
56  }
57  if (fTracks) {
58  fTracks->Delete();
59  delete fTracks;
60  }
61 }
62 // -------------------------------------------------------------------------
63 
64 
65 // ----- Push track (pure virtual from TVirtualMCStack) ----------------
66 void CbmStack::PushTrack(Int_t toBeDone,
67  Int_t parentId,
68  Int_t pdgCode,
69  Double_t px,
70  Double_t py,
71  Double_t pz,
72  Double_t e,
73  Double_t vx,
74  Double_t vy,
75  Double_t vz,
76  Double_t time,
77  Double_t polx,
78  Double_t poly,
79  Double_t polz,
80  TMCProcess proc,
81  Int_t& ntr,
82  Double_t weight,
83  Int_t is) {
84 
85 
86  // Channel all arguments to the method declared in FairGenericStack.
87  // Generator parent ID is set to -1.
88  PushTrack(toBeDone,
89  parentId,
90  pdgCode,
91  px,
92  py,
93  pz,
94  e,
95  vx,
96  vy,
97  vz,
98  time,
99  polx,
100  poly,
101  polz,
102  proc,
103  ntr,
104  weight,
105  is,
106  -1);
107 }
108 // -------------------------------------------------------------------------
109 
110 
111 // ----- PushTrack (pure virtual from FairGenericStack -----------------
112 void CbmStack::PushTrack(Int_t toBeDone,
113  Int_t parentId,
114  Int_t pdgCode,
115  Double_t px,
116  Double_t py,
117  Double_t pz,
118  Double_t e,
119  Double_t vx,
120  Double_t vy,
121  Double_t vz,
122  Double_t time,
123  Double_t polx,
124  Double_t poly,
125  Double_t polz,
126  TMCProcess proc,
127  Int_t& ntr,
128  Double_t weight,
129  Int_t /*status*/,
130  Int_t generatorParentId) {
131 
132  // --> If primary, increment counter
133  if (parentId < 0) fNPrimaries++;
134 
135  // ---> Set parent ID to the generator parent ID for primaries
136  if (parentId == -1 && generatorParentId < fNParticles)
137  parentId = generatorParentId;
138 
139  // --> Create new TParticle and add it to the TParticle array
140  Int_t trackId = fNParticles;
141  Int_t nPoints = 0;
142  Int_t daughter1Id = -1;
143  Int_t daughter2Id = -1;
144  TParticle* particle =
145  new ((*fParticles)[fNParticles++]) TParticle(pdgCode,
146  trackId,
147  parentId,
148  nPoints,
149  daughter1Id,
150  daughter2Id,
151  px,
152  py,
153  pz,
154  e,
155  vx,
156  vy,
157  vz,
158  time);
159  particle->SetPolarisation(polx, poly, polz);
160  particle->SetWeight(weight);
161  // We use the TObject unique ID to store the creation process of a particle.
162  // According to the ROOT talk, the unique ID can be used at will if the object
163  // is not used via TRef or TRefArray.
164  particle->SetUniqueID(proc);
165 
166  // --> Set return argument
167  ntr = trackId;
168 
169  // --> Push particle on the stack if toBeDone is set
170  // Note that for particles created by TGeant4, toBeDone is kFALSE,
171  // meaning that particles will not be put onto the internal stack.
172  // Geant4 seems to have a separate, internal stack administration.
173  if (fdoTracking && toBeDone) fStack.push(particle);
174 }
175 // -------------------------------------------------------------------------
176 
177 
178 // ----- Virtual method PopNextTrack -----------------------------------
179 TParticle* CbmStack::PopNextTrack(Int_t& iTrack) {
180 
181  // If end of stack: Return empty pointer
182  if (fStack.empty()) {
183  iTrack = -1;
184  return nullptr;
185  }
186 
187  // If not, get next particle from stack
188  TParticle* thisParticle = fStack.top();
189  fStack.pop();
190 
191  if (!thisParticle) {
192  iTrack = 0;
193  return nullptr;
194  }
195 
196  fCurrentTrack = thisParticle->GetStatusCode();
197  iTrack = fCurrentTrack;
198 
199  return thisParticle;
200 }
201 // -------------------------------------------------------------------------
202 
203 
204 // ----- Virtual method PopPrimaryForTracking --------------------------
205 TParticle* CbmStack::PopPrimaryForTracking(Int_t iPrim) {
206 
207  // Get the iPrimth particle from the fStack TClonesArray. This
208  // should be a primary (if the index is correct).
209  assert(iPrim >= 0 && iPrim < fNPrimaries);
210 
211  // Return the iPrim-th TParticle from the fParticle array. This should be
212  // a primary.
213  TParticle* part = (TParticle*) fParticles->At(iPrim);
214  assert(part->GetUniqueID() == kPPrimary);
215  return part;
216 }
217 // -------------------------------------------------------------------------
218 
219 
220 // ----- Virtual public method GetCurrentTrack -------------------------
221 TParticle* CbmStack::GetCurrentTrack() const {
222  TParticle* currentPart = GetParticle(fCurrentTrack);
223  if (!currentPart) { LOG(warn) << "Current track not found in stack!"; }
224  return currentPart;
225 }
226 // -------------------------------------------------------------------------
227 
228 
229 // ----- Public method AddParticle -------------------------------------
230 void CbmStack::AddParticle(TParticle* oldPart) {
231  TClonesArray& array = *fParticles;
232  TParticle* newPart = new (array[fIndex]) TParticle(*oldPart);
233  newPart->SetWeight(oldPart->GetWeight());
234  newPart->SetUniqueID(oldPart->GetUniqueID());
235  fIndex++;
236 }
237 // -------------------------------------------------------------------------
238 
239 
240 // ----- Fill the output MCTrack array ---------------------------------
242 
243  // Call the stack filter
244  assert(fFilter);
245  vector<Bool_t> store = fFilter->Select(*fParticles, fPointsMap);
246  auto nParticles = static_cast<std::size_t>(fParticles->GetEntriesFast());
247  assert(store.size() == nParticles);
248 
249  // Reset index map
250  fIndexMap.clear();
251  fIndexMap[-1] = -1; // Map index for primary mothers
252 
253  // Copy selected particles to the output
254  for (Int_t indexP = 0; indexP < fParticles->GetEntriesFast(); indexP++) {
255 
256  if (store[indexP]) {
257 
258  // Create a new MCTrack in the output from the particle
259  Int_t indexT = fTracks->GetEntriesFast();
260  CbmMCTrack* track =
261  new ((*fTracks)[indexT]) CbmMCTrack(GetParticle(indexP));
262 
263  // Map the particle index to the track index
264  fIndexMap[indexP] = indexT;
265 
266  // Set the number of points in the detectors for this track
267  for (ECbmModuleId detector = ECbmModuleId::kRef;
268  detector <= ECbmModuleId::kPsd;
269  ++detector) {
270  auto it = fPointsMap.find(make_pair(indexP, detector));
271  if (it != fPointsMap.end()) track->SetNPoints(detector, it->second);
272  } //# detectors
273 
274  } //? store particle
275 
276  // For particles discarded from storage, the index is set to -2.
277  else {
278  fIndexMap[indexP] = -2;
279  } //? do not store particle
280 
281  } //# stack particles
282 
283  fNTracks = fTracks->GetEntriesFast();
284  LOG(info) << "CbmStack: " << fParticles->GetEntriesFast() << " particles, "
285  << fNTracks << " written to output.";
286 }
287 // -------------------------------------------------------------------------
288 
289 
290 // ----- Public method UpdateTrackIndex --------------------------------
291 void CbmStack::UpdateTrackIndex(TRefArray* detList) {
292 
293  LOG(debug) << "Updating track indizes...";
294  Int_t nColl = 0;
295 
296  // First update mother ID in MCTracks
297  for (Int_t i = 0; i < fNTracks; i++) {
298  CbmMCTrack* track = (CbmMCTrack*) fTracks->At(i);
299  Int_t iMotherOld = track->GetMotherId();
300  fIndexIter = fIndexMap.find(iMotherOld);
301  if (fIndexIter == fIndexMap.end()) {
302  LOG(fatal) << "Particle index " << iMotherOld
303  << " not found in dex map! ";
304  }
305  track->SetMotherId((*fIndexIter).second);
306  }
307 
308  // Now iterate through all active detectors
309  TIterator* detIter = detList->MakeIterator();
310  detIter->Reset();
311  FairDetector* det = nullptr;
312  while ((det = (FairDetector*) detIter->Next())) {
313 
314 
315  // --> Get hit collections from detector
316  Int_t iColl = 0;
317  TClonesArray* hitArray;
318  while ((hitArray = det->GetCollection(iColl++))) {
319  nColl++;
320  Int_t nPoints = hitArray->GetEntriesFast();
321 
322  // --> Update track index for all MCPoints in the collection
323  for (Int_t iPoint = 0; iPoint < nPoints; iPoint++) {
324  FairMCPoint* point = (FairMCPoint*) hitArray->At(iPoint);
325  Int_t iTrack = point->GetTrackID();
326 
327  fIndexIter = fIndexMap.find(iTrack);
328  if (fIndexIter == fIndexMap.end()) {
329  LOG(fatal) << "Particle index " << iTrack
330  << " not found in index map! ";
331  }
332  point->SetTrackID((*fIndexIter).second);
333  point->SetLink(FairLink("MCTrack", (*fIndexIter).second));
334  }
335 
336  } // Collections of this detector
337  } // List of active detectors
338 
339  delete detIter;
340  LOG(debug) << "...stack and " << nColl << " collections updated.";
341 }
342 // -------------------------------------------------------------------------
343 
344 
345 // ----- Public method Reset -------------------------------------------
347  fIndex = 0;
348  fCurrentTrack = -1;
350  while (!fStack.empty()) {
351  fStack.pop();
352  }
353  fParticles->Clear();
354  fTracks->Clear();
355  fPointsMap.clear();
356 }
357 // -------------------------------------------------------------------------
358 
359 
360 // ----- Public method Register ----------------------------------------
362  FairRootManager::Instance()->Register("MCTrack", "Stack", fTracks, kTRUE);
363 }
364 // -------------------------------------------------------------------------
365 
366 
367 // ----- Public method Print --------------------------------------------
368 void CbmStack::Print(Option_t*) const {
369  LOG(debug) << "Number of primaries = " << fNPrimaries;
370  LOG(debug) << "Total number of particles = " << fNParticles;
371  LOG(debug) << "Number of tracks in output = " << fNTracks;
372  if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug1)) {
373  for (Int_t iTrack = 0; iTrack < fNTracks; iTrack++) {
374  LOG(debug1) << "MCTrack " << iTrack
375  << ((CbmMCTrack*) fTracks->At(iTrack))->ToString();
376  }
377  }
378 }
379 // -------------------------------------------------------------------------
380 
381 
382 // ----- Public method AddPoint (for current track) --------------------
384  pair<Int_t, ECbmModuleId> a(fCurrentTrack, detId);
385  if (fPointsMap.find(a) == fPointsMap.end()) {
386  fPointsMap[a] = 1;
387  } else {
388  fPointsMap[a]++;
389  }
390 }
391 // -------------------------------------------------------------------------
392 
393 
394 // ----- Public method AddPoint (for arbitrary track) -------------------
395 void CbmStack::AddPoint(ECbmModuleId detId, Int_t iTrack) {
396  if (iTrack < 0) { return; }
397  pair<Int_t, ECbmModuleId> a(iTrack, detId);
398  if (fPointsMap.find(a) == fPointsMap.end()) {
399  fPointsMap[a] = 1;
400  } else {
401  fPointsMap[a]++;
402  }
403 }
404 // -------------------------------------------------------------------------
405 
406 
407 // ----- Virtual method GetCurrentParentTrackNumber --------------------
409  TParticle* currentPart = GetCurrentTrack();
410  if (currentPart) {
411  return currentPart->GetFirstMother();
412  } else {
413  return -1;
414  }
415 }
416 // -------------------------------------------------------------------------
417 
418 
419 // ----- Public method GetParticle -------------------------------------
420 TParticle* CbmStack::GetParticle(Int_t trackID) const {
421  if (trackID < 0 || trackID >= fNParticles) {
422  LOG(fatal) << "Particle index " << trackID << " out of range.";
423  }
424  return (TParticle*) fParticles->At(trackID);
425 }
426 // -------------------------------------------------------------------------
427 
428 
CbmMCTrack::GetMotherId
Int_t GetMotherId() const
Definition: CbmMCTrack.h:71
CbmStack::fNTracks
Int_t fNTracks
Number of entries in fParticles.
Definition: CbmStack.h:298
CbmStack::fIndexMap
std::map< Int_t, Int_t > fIndexMap
Definition: CbmStack.h:286
CbmStackFilter
Class for filtering the stack before writing.
Definition: CbmStackFilter.h:61
CbmStack::fPointsMap
std::map< std::pair< Int_t, ECbmModuleId >, Int_t > fPointsMap
Definition: CbmStack.h:291
CbmStack::GetCurrentTrack
virtual TParticle * GetCurrentTrack() const
Definition: CbmStack.cxx:221
CbmStack::CbmStack
CbmStack(Int_t size=100)
Definition: CbmStack.cxx:28
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmStack::PopNextTrack
virtual TParticle * PopNextTrack(Int_t &iTrack)
Definition: CbmStack.cxx:179
ECbmModuleId
ECbmModuleId
Definition: CbmDefs.h:33
CbmStack::Reset
virtual void Reset()
Definition: CbmStack.cxx:346
CbmStack::~CbmStack
virtual ~CbmStack()
Definition: CbmStack.cxx:52
fFilter
TrackUpdatePtr fFilter
Definition: CbmGlobalTrackingTof.cxx:21
CbmStack::fTracks
TClonesArray * fTracks
Definition: CbmStack.h:282
CbmMCTrack::SetNPoints
void SetNPoints(ECbmModuleId iDet, Int_t np)
Definition: CbmMCTrack.cxx:214
CbmStack::fNPrimaries
Int_t fNPrimaries
Index of current track.
Definition: CbmStack.h:296
CbmStack.h
CbmStack::FillTrackArray
virtual void FillTrackArray()
Definition: CbmStack.cxx:241
CbmStack::AddPoint
void AddPoint(ECbmModuleId iDet)
Definition: CbmStack.cxx:383
CbmStack::fFilter
std::unique_ptr< CbmStackFilter > fFilter
Definition: CbmStack.h:272
CbmStack::fCurrentTrack
Int_t fCurrentTrack
Definition: CbmStack.h:295
CbmMCTrack::SetMotherId
void SetMotherId(Int_t id)
Definition: CbmMCTrack.h:114
ECbmModuleId::kRef
@ kRef
Reference plane.
CbmStack::PopPrimaryForTracking
virtual TParticle * PopPrimaryForTracking(Int_t iPrim)
Definition: CbmStack.cxx:205
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmStack::UpdateTrackIndex
virtual void UpdateTrackIndex(TRefArray *detArray)
Definition: CbmStack.cxx:291
CbmStack::fNParticles
Int_t fNParticles
Number of primary particles.
Definition: CbmStack.h:297
CbmStack::fIndex
Int_t fIndex
Number of entries in fTracks.
Definition: CbmStack.h:299
CbmMCTrack.h
CbmMCTrack
Definition: CbmMCTrack.h:34
CbmStack::GetParticle
TParticle * GetParticle(Int_t trackId) const
Definition: CbmStack.cxx:420
CbmStack::Register
virtual void Register()
Definition: CbmStack.cxx:361
CbmStack::fStack
std::stack< TParticle * > fStack
Definition: CbmStack.h:269
CbmStack::AddParticle
virtual void AddParticle(TParticle *part)
Definition: CbmStack.cxx:230
CbmStack::fdoTracking
Bool_t fdoTracking
Definition: CbmStack.h:309
CbmStack::PushTrack
virtual void PushTrack(Int_t toBeDone, Int_t parentId, Int_t pdgCode, Double_t px, Double_t py, Double_t pz, Double_t e, Double_t vx, Double_t vy, Double_t vz, Double_t time, Double_t polx, Double_t poly, Double_t polz, TMCProcess process, Int_t &ntr, Double_t weight, Int_t status)
Add a track to the stack (TVirtualMCStack)
Definition: CbmStack.cxx:66
ECbmModuleId::kPsd
@ kPsd
Projectile spectator detector.
CbmStack::fParticles
TClonesArray * fParticles
Stack filter class.
Definition: CbmStack.h:278
CbmStack::Print
virtual void Print(Option_t *="") const
Definition: CbmStack.cxx:368
CbmStack
Definition: CbmStack.h:45
CbmStack::GetCurrentParentTrackNumber
virtual Int_t GetCurrentParentTrackNumber() const
Definition: CbmStack.cxx:408
Cbm::ToString
std::string ToString(const T &value)
Definition: CbmUtils.h:16
CbmStack::fIndexIter
std::map< Int_t, Int_t >::iterator fIndexIter
Definition: CbmStack.h:287