FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MyProjStack.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 // -------------------------------------------------------------------------
10 // ----- MyProjStack source file -----
11 // ----- M. Al-Turany June 2014 -----
12 // -------------------------------------------------------------------------
13 
14 #include "MyProjStack.h"
15 
16 #include "FairDetector.h" // for FairDetector
17 #include "FairLink.h" // for FairLink
18 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
19 #include "FairMCPoint.h" // for FairMCPoint
20 #include "FairRootManager.h" // for FairRootManager
21 #include "MyProjMCTrack.h" // for MyProjMCTrack
22 
23 #include <TClonesArray.h> // for TClonesArray
24 #include <TIterator.h> // for TIterator
25 #include <TLorentzVector.h> // for TLorentzVector
26 #include <TParticle.h> // for TParticle
27 #include <TRefArray.h> // for TRefArray
28 #include <TVirtualMC.h> // for gMC
29 #include <iosfwd> // for ostream
30 #include <iostream> // for operator<<, etc
31 #include <stddef.h> // for NULL
32 
33 using std::cout;
34 using std::endl;
35 using std::pair;
36 
37 // ----- Default constructor -------------------------------------------
40  , fStack()
41  , fParticles(new TClonesArray("TParticle", size))
42  , fTracks(new TClonesArray("MyProjMCTrack", size))
43  , fStoreMap()
44  , fStoreIter()
45  , fIndexMap()
46  , fIndexIter()
47  , fPointsMap()
48  , fCurrentTrack(-1)
49  , fNPrimaries(0)
50  , fNParticles(0)
51  , fNTracks(0)
52  , fIndex(0)
53  , fStoreSecondaries(kTRUE)
54  , fMinPoints(1)
55  , fEnergyCut(0.)
56  , fStoreMothers(kTRUE)
57 {}
58 
59 // -------------------------------------------------------------------------
60 
62  : FairGenericStack(right)
63  , fStack()
64  , fParticles(new TClonesArray("TParticle", 100))
65  , fTracks(new TClonesArray("MyProjMCTrack", 100))
66  , fStoreMap()
67  , fStoreIter()
68  , fIndexMap()
69  , fIndexIter()
70  , fPointsMap()
71  , fCurrentTrack()
72  , fNPrimaries()
73  , fNParticles()
74  , fNTracks()
75  , fIndex()
76  , fStoreSecondaries(right.fStoreSecondaries)
77  , fMinPoints(right.fMinPoints)
78  , fEnergyCut(right.fEnergyCut)
79  , fStoreMothers(right.fStoreMothers)
80 {}
81 
82 // ----- Destructor ----------------------------------------------------
84 {
85  if (fParticles) {
86  fParticles->Delete();
87  delete fParticles;
88  }
89  if (fTracks) {
90  fTracks->Delete();
91  delete fTracks;
92  }
93 }
94 // -------------------------------------------------------------------------
95 
96 void MyProjStack::PushTrack(Int_t toBeDone,
97  Int_t parentId,
98  Int_t pdgCode,
99  Double_t px,
100  Double_t py,
101  Double_t pz,
102  Double_t e,
103  Double_t vx,
104  Double_t vy,
105  Double_t vz,
106  Double_t time,
107  Double_t polx,
108  Double_t poly,
109  Double_t polz,
110  TMCProcess proc,
111  Int_t& ntr,
112  Double_t weight,
113  Int_t is)
114 {
115 
116  PushTrack(
117  toBeDone, parentId, pdgCode, px, py, pz, e, vx, vy, vz, time, polx, poly, polz, proc, ntr, weight, is, -1);
118 }
119 
120 // ----- Virtual public method PushTrack -------------------------------
121 void MyProjStack::PushTrack(Int_t toBeDone,
122  Int_t parentId,
123  Int_t pdgCode,
124  Double_t px,
125  Double_t py,
126  Double_t pz,
127  Double_t e,
128  Double_t vx,
129  Double_t vy,
130  Double_t vz,
131  Double_t time,
132  Double_t polx,
133  Double_t poly,
134  Double_t polz,
135  TMCProcess proc,
136  Int_t& ntr,
137  Double_t weight,
138  Int_t is,
139  Int_t secondparentID)
140 {
141 
142  // --> Get TParticle array
143  TClonesArray& partArray = *fParticles;
144 
145  // --> Create new TParticle and add it to the TParticle array
146  Int_t trackId = fNParticles;
147  Int_t nPoints = 0;
148  Int_t daughter1Id = -1;
149  Int_t daughter2Id = -1;
150  TParticle* particle = new (partArray[fNParticles++])
151  TParticle(pdgCode, trackId, parentId, nPoints, daughter1Id, daughter2Id, px, py, pz, e, vx, vy, vz, time);
152  particle->SetPolarisation(polx, poly, polz);
153  particle->SetWeight(weight);
154  particle->SetUniqueID(proc);
155 
156  // --> Increment counter
157  if (parentId < 0) {
158  fNPrimaries++;
159  }
160 
161  // --> Set argument variable
162  ntr = trackId;
163 
164  // --> Push particle on the stack if toBeDone is set
165  if (toBeDone == 1) {
166  fStack.push(particle);
167  }
168 }
169 // -------------------------------------------------------------------------
170 
171 // ----- Virtual method PopNextTrack -----------------------------------
172 TParticle* MyProjStack::PopNextTrack(Int_t& iTrack)
173 {
174 
175  // If end of stack: Return empty pointer
176  if (fStack.empty()) {
177  iTrack = -1;
178  return NULL;
179  }
180 
181  // If not, get next particle from stack
182  TParticle* thisParticle = fStack.top();
183  fStack.pop();
184 
185  if (!thisParticle) {
186  iTrack = 0;
187  return NULL;
188  }
189 
190  fCurrentTrack = thisParticle->GetStatusCode();
191  iTrack = fCurrentTrack;
192 
193  return thisParticle;
194 }
195 // -------------------------------------------------------------------------
196 
197 // ----- Virtual method PopPrimaryForTracking --------------------------
198 TParticle* MyProjStack::PopPrimaryForTracking(Int_t iPrim)
199 {
200 
201  // Get the iPrimth particle from the fStack TClonesArray. This
202  // should be a primary (if the index is correct).
203 
204  // Test for index
205  if (iPrim < 0 || iPrim >= fNPrimaries) {
206  LOG(fatal) << "MyProjStack: Primary index out of range! " << iPrim;
207  }
208 
209  // Return the iPrim-th TParticle from the fParticle array. This should be
210  // a primary.
211  TParticle* part = (TParticle*)fParticles->At(iPrim);
212  if (!(part->GetMother(0) < 0)) {
213  LOG(fatal) << "MyProjStack:: Not a primary track! " << iPrim;
214  }
215 
216  return part;
217 }
218 // -------------------------------------------------------------------------
219 
220 // ----- Virtual public method GetCurrentTrack -------------------------
222 {
223  TParticle* currentPart = GetParticle(fCurrentTrack);
224  if (!currentPart) {
225  LOG(warning) << "MyProjStack: Current track not found in stack!";
226  }
227  return currentPart;
228 }
229 // -------------------------------------------------------------------------
230 
231 // ----- Public method AddParticle -------------------------------------
232 void MyProjStack::AddParticle(TParticle* oldPart)
233 {
234  TClonesArray& array = *fParticles;
235  TParticle* newPart = new (array[fIndex]) TParticle(*oldPart);
236  newPart->SetWeight(oldPart->GetWeight());
237  newPart->SetUniqueID(oldPart->GetUniqueID());
238  fIndex++;
239 }
240 // -------------------------------------------------------------------------
241 
242 // ----- Public method FillTrackArray ----------------------------------
244 {
245 
246  LOG(debug) << "MyProjStack: Filling MCTrack array...";
247 
248  // --> Reset index map and number of output tracks
249  fIndexMap.clear();
250  fNTracks = 0;
251 
252  // --> Check tracks for selection criteria
253  SelectTracks();
254 
255  // --> Loop over fParticles array and copy selected tracks
256  for (Int_t iPart = 0; iPart < fNParticles; iPart++) {
257 
258  fStoreIter = fStoreMap.find(iPart);
259  if (fStoreIter == fStoreMap.end()) {
260  LOG(fatal) << "MyProjStack: Particle " << iPart << " not found in storage map! ";
261  }
262  Bool_t store = (*fStoreIter).second;
263 
264  if (store) {
265  MyProjMCTrack* track = new ((*fTracks)[fNTracks]) MyProjMCTrack(GetParticle(iPart));
266  fIndexMap[iPart] = fNTracks;
267  // --> Set the number of points in the detectors for this track
268  for (Int_t iDet = kNewDetector; iDet < kSTOPHERE; iDet++) {
269  pair<Int_t, Int_t> a(iPart, iDet);
270  track->SetNPoints(iDet, fPointsMap[a]);
271  }
272  fNTracks++;
273  } else {
274  fIndexMap[iPart] = -2;
275  }
276  }
277 
278  // --> Map index for primary mothers
279  fIndexMap[-1] = -1;
280 
281  // --> Screen output
282  Print(1);
283 }
284 // -------------------------------------------------------------------------
285 
286 // ----- Public method UpdateTrackIndex --------------------------------
287 void MyProjStack::UpdateTrackIndex(TRefArray* detList)
288 {
289 
290  LOG(debug) << "MyProjStack: Updating track indizes...";
291  Int_t nColl = 0;
292 
293  // First update mother ID in MCTracks
294  for (Int_t i = 0; i < fNTracks; i++) {
295  MyProjMCTrack* track = (MyProjMCTrack*)fTracks->At(i);
296  Int_t iMotherOld = track->GetMotherId();
297  fIndexIter = fIndexMap.find(iMotherOld);
298  if (fIndexIter == fIndexMap.end()) {
299  LOG(fatal) << "MyProjStack: Particle index " << iMotherOld << " not found in dex map! ";
300  }
301  track->SetMotherId((*fIndexIter).second);
302  }
303 
304  if (fDetList == 0) {
305  // Now iterate through all active detectors
306  fDetIter = detList->MakeIterator();
307  fDetIter->Reset();
308  } else {
309  fDetIter->Reset();
310  }
311 
312  FairDetector* det = NULL;
313  while ((det = (FairDetector*)fDetIter->Next())) {
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) << "MyProjStack: Particle index " << iTrack << " not found in index map! ";
330  }
331  point->SetTrackID((*fIndexIter).second);
332  point->SetLink(FairLink("MCTrack", (*fIndexIter).second));
333  }
334 
335  } // Collections of this detector
336  } // List of active detectors
337  LOG(debug) << "...stack and " << nColl << " collections updated.";
338 }
339 // -------------------------------------------------------------------------
340 
341 // ----- Public method Reset -------------------------------------------
343 {
344  fIndex = 0;
345  fCurrentTrack = -1;
346  fNPrimaries = fNParticles = fNTracks = 0;
347  while (!fStack.empty()) {
348  fStack.pop();
349  }
350  fParticles->Clear();
351  fTracks->Clear();
352  fPointsMap.clear();
353 }
354 // -------------------------------------------------------------------------
355 
356 // ----- Public method Register ----------------------------------------
358 {
359  if (gMC && (!gMC->IsMT())) {
360  FairRootManager::Instance()->Register("MCTrack", "Stack", fTracks, kTRUE);
361  } else {
362  FairRootManager::Instance()->RegisterAny("MCTrack", fTracks, kTRUE);
363  }
364 }
365 // -------------------------------------------------------------------------
366 
367 // ----- Public method Print --------------------------------------------
368 void MyProjStack::Print(Int_t iVerbose) const
369 {
370  cout << "-I- MyProjStack: Number of primaries = " << fNPrimaries << endl;
371  cout << " Total number of particles = " << fNParticles << endl;
372  cout << " Number of tracks in output = " << fNTracks << endl;
373  if (iVerbose) {
374  for (Int_t iTrack = 0; iTrack < fNTracks; iTrack++) {
375  ((MyProjMCTrack*)fTracks->At(iTrack))->Print(iTrack);
376  }
377  }
378 }
379 // -------------------------------------------------------------------------
380 
381 // ----- Public method AddPoint (for current track) --------------------
383 {
384  Int_t iDet = detId;
385  // cout << "Add point for Detektor" << iDet << endl;
386  pair<Int_t, Int_t> a(fCurrentTrack, iDet);
387  if (fPointsMap.find(a) == fPointsMap.end()) {
388  fPointsMap[a] = 1;
389  } else {
390  fPointsMap[a]++;
391  }
392 }
393 // -------------------------------------------------------------------------
394 
395 // ----- Public method AddPoint (for arbitrary track) -------------------
396 void MyProjStack::AddPoint(DetectorId detId, Int_t iTrack)
397 {
398  if (iTrack < 0) {
399  return;
400  }
401  Int_t iDet = detId;
402  pair<Int_t, Int_t> a(iTrack, iDet);
403  if (fPointsMap.find(a) == fPointsMap.end()) {
404  fPointsMap[a] = 1;
405  } else {
406  fPointsMap[a]++;
407  }
408 }
409 // -------------------------------------------------------------------------
410 
411 // ----- Virtual method GetCurrentParentTrackNumber --------------------
413 {
414  TParticle* currentPart = GetCurrentTrack();
415  if (currentPart) {
416  return currentPart->GetFirstMother();
417  } else {
418  return -1;
419  }
420 }
421 // -------------------------------------------------------------------------
422 
423 // ----- Public method GetParticle -------------------------------------
424 TParticle* MyProjStack::GetParticle(Int_t trackID) const
425 {
426  if (trackID < 0 || trackID >= fNParticles) {
427  LOG(fatal) << "MyProjStack: Particle index " << trackID << " out of range.";
428  }
429  return (TParticle*)fParticles->At(trackID);
430 }
431 // -------------------------------------------------------------------------
432 
433 // ----- Private method SelectTracks -----------------------------------
434 void MyProjStack::SelectTracks()
435 {
436 
437  // --> Clear storage map
438  fStoreMap.clear();
439 
440  // --> Check particles in the fParticle array
441  for (Int_t i = 0; i < fNParticles; i++) {
442 
443  TParticle* thisPart = GetParticle(i);
444  Bool_t store = kTRUE;
445 
446  // --> Get track parameters
447  Int_t iMother = thisPart->GetMother(0);
448  TLorentzVector p;
449  thisPart->Momentum(p);
450  Double_t energy = p.E();
451  Double_t mass = p.M();
452  // Double_t mass = thisPart->GetMass();
453  Double_t eKin = energy - mass;
454 
455  // --> Calculate number of points
456  Int_t nPoints = 0;
457  for (Int_t iDet = kNewDetector; iDet < kSTOPHERE; iDet++) {
458  pair<Int_t, Int_t> a(i, iDet);
459  if (fPointsMap.find(a) != fPointsMap.end()) {
460  nPoints += fPointsMap[a];
461  }
462  }
463 
464  // --> Check for cuts (store primaries in any case)
465  if (iMother < 0) {
466  store = kTRUE;
467  } else {
468  if (!fStoreSecondaries) {
469  store = kFALSE;
470  }
471  if (nPoints < fMinPoints) {
472  store = kFALSE;
473  }
474  if (eKin < fEnergyCut) {
475  store = kFALSE;
476  }
477  }
478 
479  // --> Set storage flag
480  fStoreMap[i] = store;
481  }
482 
483  // --> If flag is set, flag recursively mothers of selected tracks
484  if (fStoreMothers) {
485  for (Int_t i = 0; i < fNParticles; i++) {
486  if (fStoreMap[i]) {
487  Int_t iMother = GetParticle(i)->GetMother(0);
488  while (iMother >= 0) {
489  fStoreMap[iMother] = kTRUE;
490  iMother = GetParticle(iMother)->GetMother(0);
491  }
492  }
493  }
494  }
495 }
496 // -------------------------------------------------------------------------
497 
DetectorId
TIterator * fDetIter
Int_t GetMotherId() const
Definition: MyProjMCTrack.h:66
virtual void FillTrackArray()
void SetLink(FairLink link)
Sets the Links with a single FairLink.
virtual TParticle * PopPrimaryForTracking(Int_t iPrim)
void SetMotherId(Int_t id)
Definition: MyProjMCTrack.h:87
static FairRootManager * Instance()
virtual TParticle * PopNextTrack(Int_t &iTrack)
ClassImp(FairEventBuilder)
TParticle * GetParticle(Int_t trackId) const
virtual void Register()
Int_t GetTrackID() const
event identifier
Definition: FairMCPoint.h:58
virtual TParticle * GetCurrentTrack() const
virtual void Print(Int_t iVerbose=0) const
virtual void AddParticle(TParticle *part)
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 proc, Int_t &ntr, Double_t weight, Int_t is)
Definition: MyProjStack.cxx:96
virtual void SetTrackID(Int_t id)
Definition: FairMCPoint.h:74
void SetNPoints(Int_t iDet, Int_t np)
virtual ~MyProjStack()
Definition: MyProjStack.cxx:83
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
TRefArray * fDetList
virtual TClonesArray * GetCollection(Int_t iColl) const =0
virtual void UpdateTrackIndex(TRefArray *detArray=0)
virtual Int_t GetCurrentParentTrackNumber() const
void AddPoint(DetectorId iDet)
virtual void Reset()
void RegisterAny(const char *name, T *&obj, Bool_t toFile)
MyProjStack(Int_t size=100)
Definition: MyProjStack.cxx:38