FairRoot/PandaRoot
PndStack.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndStack source file -----
3 // ----- Created 10/08/04 by D. Bertini / V. Friese -----
4 // -------------------------------------------------------------------------
5 #include "PndStack.h"
6 
7 #include "FairDetector.h"
8 #include "FairMCPoint.h"
9 #include "PndMCTrack.h"
10 #include "FairRootManager.h"
11 #include "FairMCEventHeader.h"
12 
13 #include "TError.h"
14 #include "TLorentzVector.h"
15 #include "TParticle.h"
16 #include "TRefArray.h"
17 
18 #include <list>
19 #include <iostream>
20 
21 using std::cout;
22 using std::endl;
23 using std::pair;
24 
25 
26 // ----- Default constructor -------------------------------------------
27 PndStack::PndStack(Int_t size):
28 fStack(), fParticles(), fTracks(), fStoreMap(), fStoreIter(), fIndexMap(), fIndexIter(), fPointsMap(), fCurrentTrack(-1), fNPrimaries(0), fNParticles(0), fNTracks(0), fIndex(0), fStoreSecondaries(kTRUE), fMinPoints(1), fEnergyCut(0), fStoreMothers(kTRUE), fPersistence(kTRUE)
29 {
30  fStoreMap.clear();
31  fIndexMap.clear();
32  fPointsMap.clear();
33  fParticles = new TClonesArray("TParticle", size);
34  fTracks = new TClonesArray("PndMCTrack", size);
35 }
36 
37 // -------------------------------------------------------------------------
38 
39 
40 
41 // ----- Destructor ----------------------------------------------------
43  if (fParticles) {
44  fParticles->Delete();
45  delete fParticles;
46  }
47  if (fTracks) {
48  fTracks->Delete();
49  delete fTracks;
50  }
51 }
52 // -------------------------------------------------------------------------
53 
54 
55 
56 // ----- Virtual public method PushTrack -------------------------------
57 void PndStack::PushTrack(Int_t toBeDone, Int_t parentId, Int_t pdgCode,
58  Double_t px, Double_t py, Double_t pz,
59  Double_t e, Double_t vx, Double_t vy, Double_t vz,
60  Double_t time, Double_t polx, Double_t poly,
61  Double_t polz, TMCProcess proc, Int_t& ntr,
62  Double_t weight, Int_t is) {
63 
64  PushTrack( toBeDone, parentId, pdgCode,
65  px, py, pz,
66  e, vx, vy, vz,
67  time, polx, poly,
68  polz, proc, ntr,
69  weight, is, -1);
70 }
71 
72 // ----- Virtual public method PushTrack -------------------------------
73 void PndStack::PushTrack(Int_t toBeDone, Int_t parentId, Int_t pdgCode,
74  Double_t px, Double_t py, Double_t pz,
75  Double_t e, Double_t vx, Double_t vy, Double_t vz,
76  Double_t time, Double_t polx, Double_t poly,
77  Double_t polz, TMCProcess proc, Int_t& ntr,
78  Double_t weight, Int_t is,Int_t secondparentID) {
79 
80  (void)is; // To remove "unused" warnings
81  // --> Get TParticle array
82  TClonesArray& partArray = *fParticles;
83 
84  // --> Create new TParticle and add it to the TParticle array
85  Int_t trackId = fNParticles;
86  Int_t nPoints = 0;
87  Int_t daughter1Id = -1;
88  Int_t daughter2Id = -1;
89  TParticle* particle =
90  new(partArray[fNParticles++]) TParticle(pdgCode, trackId, parentId,
91  nPoints, daughter1Id,
92  daughter2Id, px, py, pz, e,
93  vx, vy, vz, time);
94  particle->SetLastMother(secondparentID);
95  particle->SetPolarisation(polx, poly, polz);
96  particle->SetWeight(weight);
97  particle->SetUniqueID(proc);
98 
99  // --> Increment counter
100  if (parentId < 0) fNPrimaries++;
101 
102  // --> Set argument variable
103  ntr = trackId;
104 
105  // --> Push particle on the stack if toBeDone is set
106  if (toBeDone == 1) {
107  particle->SetBit(kDoneBit);
108  fStack.push(particle);
109  }
110 }
111 // -------------------------------------------------------------------------
112 
113 
114 
115 // ----- Virtual method PopNextTrack -----------------------------------
116 TParticle* PndStack::PopNextTrack(Int_t& iTrack) {
117 
118  // If end of stack: Return empty pointer
119  if (fStack.empty()) {
120  iTrack = -1;
121  return NULL;
122  }
123 
124  // If not, get next particle from stack
125  TParticle* thisParticle = fStack.top();
126  fStack.pop();
127 
128  if ( !thisParticle) {
129  iTrack = 0;
130  return NULL;
131  }
132 
133  fCurrentTrack = thisParticle->GetStatusCode();
134  iTrack = fCurrentTrack;
135 
136  return thisParticle;
137 
138 }
139 // -------------------------------------------------------------------------
140 
141 
142 
143 // ----- Virtual method PopPrimaryForTracking --------------------------
144 TParticle* PndStack::PopPrimaryForTracking(Int_t iPrim) {
145 
146  // Get the iPrimth particle from the fStack TClonesArray. This
147  // should be a primary (if the index is correct).
148 
149  // Test for index
150  if (iPrim < 0 || iPrim >= fNPrimaries) {
151  LOG(ERROR) << "PndStack: Primary index out of range! " << iPrim ;
152  Fatal("PndStack::PopPrimaryForTracking", "Index out of range");
153  }
154 
155  // Return the iPrim-th TParticle from the fParticle array. This should be
156  // a primary.
157  TParticle* part = (TParticle*)fParticles->At(iPrim);
158 
159  if ( ! (part->GetMother(0) < 0) ) {
160  LOG(ERROR) << "PndStack:: Not a primary track! , " <<iPrim ;
161  Fatal("PndStack::PopPrimaryForTracking", "Not a primary track");
162  }
163 
164  if(!part->TestBit(kDoneBit)) return NULL;
165  else return part;
166 
167 }
168 // -------------------------------------------------------------------------
169 
170 
171 
172 // ----- Virtual public method GetCurrentTrack -------------------------
173 TParticle* PndStack::GetCurrentTrack() const {
174  TParticle* currentPart = GetParticle(fCurrentTrack);
175  if ( ! currentPart) {
176  LOG(WARNING) << "PndStack: Current track not found in stack!" ;
177  Warning("PndStack::GetCurrentTrack", "Track not found in stack");
178  }
179  return currentPart;
180 }
181 // -------------------------------------------------------------------------
182 
183 
184 
185 // ----- Public method AddParticle -------------------------------------
186 void PndStack::AddParticle(TParticle* oldPart) {
187  TClonesArray& array = *fParticles;
188  TParticle* newPart = new(array[fIndex]) TParticle(*oldPart);
189  newPart->SetWeight(oldPart->GetWeight());
190  newPart->SetUniqueID(oldPart->GetUniqueID());
191  fIndex++;
192 }
193 // -------------------------------------------------------------------------
194 
195 
196 
197 // ----- Public method FillTrackArray ----------------------------------
199 
200  LOG(DEBUG) << "PndStack: Filling MCTrack array..." ;
201 
202  // --> Reset index map and number of output tracks
203  fIndexMap.clear();
204  fNTracks = 0;
205 
206  // --> Check tracks for selection criteria
207  SelectTracks();
208 
209  // --> Loop over fParticles array and copy selected tracks
210  for (Int_t iPart=0; iPart<fNParticles; iPart++) {
211 
212  fStoreIter = fStoreMap.find(iPart);
213  if (fStoreIter == fStoreMap.end() ) {
214  LOG(ERROR) << "PndStack: Particle "<<iPart<<" not found in storage map!" ;
215  Fatal("PndStack::FillTrackArray","Particle not found in storage map.");
216  }
217  Bool_t store = (*fStoreIter).second;
218 
219  if (store) {
220  PndMCTrack* track =
221  new( (*fTracks)[fNTracks]) PndMCTrack(GetParticle(iPart));
222  fIndexMap[iPart] = fNTracks;
223  // --> Set the number of points in the detectors for this track
224  for (Int_t iDet=kRICH; iDet<=kHYP; iDet++) {
225  pair<Int_t, Int_t> a(iPart, iDet);
226  track->SetNPoints(iDet, fPointsMap[a]);
227  }
228 
229  SetGeneratorFlags(iPart);
230 
231  fNTracks++;
232 
233  }else{
234  fIndexMap[iPart] = -2;
235  }
236 
237  }
238 
239  // --> Map index for primary mothers
240  fIndexMap[-1] = -1;
241 
242  // --> Screen output
243  Print(0);
244 
245 }
246 // -------------------------------------------------------------------------
247 
249 {
250  if(myid<0) return;
251 
252  PndMCTrack* mytrack;
253  {
254  Int_t myid2=fIndexMap[myid];
255  if(myid2<0){
256  LOG(ERROR)<<"=== This should not happen negative index in MAP!!" ;
257  return;
258  }
259 
260  mytrack = (PndMCTrack*)fTracks->At(myid2);
261  }
262 
263  Int_t n;
264  Int_t daughters=0, daughtersp=0;
265  // fParticles; // TParticle
266  n=fParticles->GetEntries();
267  for (Int_t i=0; i<n; i++) {
268  TParticle* part = (TParticle*)fParticles->At(i);
269  Int_t m;
270  m=part->GetMother(0);
271  if(myid==m){
272  daughters++;
273  }else if(m==-1){
274  m=part->GetMother(1);
275  if(myid==m){
276  daughtersp++;
277  }
278  }else if(m==-2){
279  // removed should not happen before this is called
280  // and anyway not on the TParticle Level
281  LOG(ERROR)<<"=== Problem!!! part mother -2" ;
282  }
283  }
284 
285  Int_t mymo1=mytrack->GetMotherID();
286 
287  if( ((TParticle*)fParticles->At(myid))->GetMother(0)!=mymo1){
288  LOG(ERROR)<<"=== Problem: Mothers != "<< myid ;
289  }
290  if(mymo1==-1){
291  if(daughters!=0 && daughtersp!=0){
292  LOG(ERROR)<<"=== Problem: particle with index "<<myid<<" has daughters= "<<daughters<<" && daughtersp= "<<daughtersp ;
293  }
294 
295  mytrack->SetGeneratorCreated();
296  if(daughtersp>0) mytrack->SetGeneratorDecayed();
297 // cout << myid <<" ("<<mytrack->GetPdgCode()<<"): "<<daughters<<","<<daughtersp<<" ==> " <<mytrack->IsGeneratorCreated()<<" "<<mytrack->IsGeneratorDecayed()<<" "<<mytrack->IsGeneratorLast()<<endl;
298  }
299 }
300 
301 
302 // ----- Public method UpdateTrackIndex --------------------------------
303 void PndStack::UpdateTrackIndex(TRefArray* detList) {
304 
305  LOG(DEBUG) << "PndStack: Updating track indizes..." ;
306  Int_t nColl = 0;
307 
308  FairMCEventHeader* header = (FairMCEventHeader*)FairRootManager::Instance()->GetObject("MCEventHeader.");
309 
310  // First update mother ID in MCTracks
311  for (Int_t i=0; i<fNTracks; i++) {
312  PndMCTrack* track = (PndMCTrack*)fTracks->At(i);
313  Int_t iMotherOld = track->GetMotherID();
314  fIndexIter = fIndexMap.find(iMotherOld);
315  if (fIndexIter == fIndexMap.end()) {
316  LOG(ERROR) << "PndStack: Particle index "<<iMotherOld<<" not found in dex map! " ;
317  Fatal("PndStack::UpdateTrackIndex","Particle index not found in map");
318  }
319  track->SetMotherID( (*fIndexIter).second );
320  if(iMotherOld==-1){
321  iMotherOld = track->GetSecondMotherID();
322  fIndexIter = fIndexMap.find(iMotherOld);
323  if (fIndexIter == fIndexMap.end()) {
324  LOG(ERROR) << "PndStack: Particle index "<<iMotherOld<<" not found in dex map! (second mother id)" ;
325  Fatal("PndStack::UpdateTrackIndex","Particle index not found in map");
326  }
327  track->SetSecondMotherID( (*fIndexIter).second );
328  }
329  }
330 
331  // Now iterate through all active detectors
332  TIterator* detIter = detList->MakeIterator();
333  detIter->Reset();
334  FairDetector* det = NULL;
335  while ((det = (FairDetector*) detIter->Next())) {
336 
337  // --> Get hit collections from detector
338  Int_t iColl = 0;
339  TClonesArray* hitArray;
340  while ((hitArray = det->GetCollection(iColl++))) {
341  nColl++;
342  Int_t nPoints = hitArray->GetEntriesFast();
343 
344  // --> Update track index for all MCPoints in the collection
345  for (Int_t iPoint = 0; iPoint < nPoints; iPoint++) {
346  FairMCPoint* point = (FairMCPoint*) hitArray->At(iPoint);
347  Int_t iTrack = point->GetTrackID();
348 
349  fIndexIter = fIndexMap.find(iTrack);
350  if (fIndexIter == fIndexMap.end()) {
351  LOG(ERROR) << "PndStack: Particle index "<<iTrack<<" not found in index map! " ;
352  Fatal("PndStack::UpdateTrackIndex",
353  "Particle index not found in map");
354  }
355  point->SetTrackID((*fIndexIter).second);
356 // std::cout << "Header->GetEventID() " << header->GetEventID() << std::endl;
357  point->SetLink(FairLink(-1, (header->GetEventID()-1), "MCTrack", (*fIndexIter).second));
358  }
359 
360  } // Collections of this detector
361  } // List of active detectors
362 
363  LOG(DEBUG) << "...stack and "<<nColl<<" collections updated." ;
364  delete detIter;
365 }
366 // -------------------------------------------------------------------------
367 
368 
369 
370 // ----- Public method Reset -------------------------------------------
372  fIndex = 0;
373  fCurrentTrack = -1;
375  while (! fStack.empty() ) fStack.pop();
376  fParticles->Clear();
377  fTracks->Clear();
378  fPointsMap.clear();
379 }
380 // -------------------------------------------------------------------------
381 
382 
383 
384 // ----- Public method Register ----------------------------------------
386  FairRootManager::Instance()->Register("MCTrack", "Stack", fTracks, fPersistence);
387 }
388 // -------------------------------------------------------------------------
389 
390 
391 
392 // ----- Public method Print --------------------------------------------
393 void PndStack::Print(Int_t iVerbose) const {
394  LOG(DEBUG) << " PndStack: Number of primaries = " <<fNPrimaries
395  << "\n Total number of particles = " << fNParticles
396  << "\n Number of tracks in output = " << fNTracks ;
397 
398  if (iVerbose) {
399  for (Int_t iTrack=0; iTrack<fNTracks; iTrack++)
400  ((PndMCTrack*) fTracks->At(iTrack))->Print(iTrack);
401  }
402 }
403 // -------------------------------------------------------------------------
404 
405 
406 
407 // ----- Public method AddPoint (for current track) --------------------
409  Int_t iDet = detId;
410  pair<Int_t, Int_t> a(fCurrentTrack, iDet);
411  if ( fPointsMap.find(a) == fPointsMap.end() ) fPointsMap[a] = 1;
412  else fPointsMap[a]++;
413 }
414 // -------------------------------------------------------------------------
415 
416 
417 
418 // ----- Public method AddPoint (for arbitrary track) -------------------
419 void PndStack::AddPoint(DetectorId detId, Int_t iTrack) {
420  if ( iTrack < 0 ) return;
421  Int_t iDet = detId;
422  pair<Int_t, Int_t> a(iTrack, iDet);
423  if ( fPointsMap.find(a) == fPointsMap.end() ) fPointsMap[a] = 1;
424  else fPointsMap[a]++;
425 }
426 // -------------------------------------------------------------------------
427 
428 
429 
430 
431 // ----- Virtual method GetCurrentParentTrackNumber --------------------
433  TParticle* currentPart = GetCurrentTrack();
434  if ( currentPart ) return currentPart->GetFirstMother();
435  else return -1;
436 }
437 // -------------------------------------------------------------------------
438 
439 
440 
441 // ----- Public method GetParticle -------------------------------------
442 TParticle* PndStack::GetParticle(Int_t trackID) const {
443  if (trackID < 0 || trackID >= fNParticles) {
444  LOG(ERROR) << "PndStack: Particle index out of range." << trackID ;
445  Fatal("PndStack::GetParticle", "Index out of range");
446  }
447  return (TParticle*)fParticles->At(trackID);
448 }
449 // -------------------------------------------------------------------------
450 
451 
452 
453 // ----- Private method SelectTracks -----------------------------------
455 
456  // --> Clear storage map
457  fStoreMap.clear();
458 
459  // --> Check particles in the fParticle array
460  for (Int_t i=0; i<fNParticles; i++) {
461 
462  TParticle* thisPart = GetParticle(i);
463  Bool_t store = kTRUE;
464 
465  // --> Get track parameters
466  Int_t iMother = thisPart->GetMother(0);
467  TLorentzVector p;
468  thisPart->Momentum(p);
469  Double_t energy = p.E();
470  Double_t mass = p.M();
471 // Double_t mass = thisPart->GetMass();// Why?? Mass (given by generator) is inside by Lorentzvector!!! I dont care about PSG mass!
472  Double_t eKin = energy - mass;
473  if(eKin < 0.0) eKin=0.0; // sometimes due to different PDG masses between ROOT and G4!!!!!!
474  // --> Calculate number of points
475  Int_t nPoints = 0;
476  for (Int_t iDet=kRICH; iDet<=kHYP; iDet++) {
477  pair<Int_t, Int_t> a(i, iDet);
478  if ( fPointsMap.find(a) != fPointsMap.end() )
479  nPoints += fPointsMap[a];
480  }
481 
482  // --> Check for cuts (store primaries in any case)
483  if (iMother < 0) store = kTRUE;
484  else {
485  if (!fStoreSecondaries) store = kFALSE;
486  if (nPoints < fMinPoints) store = kFALSE;
487  if (eKin < fEnergyCut) store = kFALSE;
488  }
489 
490  // --> Set storage flag
491  fStoreMap[i] = store;
492 
493  }
494 
495  // --> If flag is set, flag recursively mothers of selected tracks
496  if (fStoreMothers) {
497  for (Int_t i=0; i<fNParticles; i++) {
498  if (fStoreMap[i]) {
499  Int_t iMother = GetParticle(i)->GetMother(0);
500  while(iMother >= 0) {
501  fStoreMap[iMother] = kTRUE;
502  iMother = GetParticle(iMother)->GetMother(0);
503  }
504  }
505  }
506  }
507 
508 }
509 // -------------------------------------------------------------------------
510 
511 
512 
TClonesArray * fTracks
Definition: PndStack.h:210
Double_t p
Definition: anasim.C:58
virtual TParticle * PopPrimaryForTracking(Int_t iPrim)
Definition: PndStack.cxx:144
virtual Int_t GetCurrentParentTrackNumber() const
Definition: PndStack.cxx:432
std::map< std::pair< Int_t, Int_t >, Int_t > fPointsMap
Definition: PndStack.h:224
Int_t i
Definition: run_full.C:25
__m128 m
Definition: P4_F32vec4.h:28
virtual TParticle * PopNextTrack(Int_t &iTrack)
Definition: PndStack.cxx:116
void SetNPoints(Int_t iDet, Int_t np)
Definition: PndMCTrack.cxx:143
int n
TParticle * GetParticle(Int_t trackId) const
Definition: PndStack.cxx:442
Bool_t fStoreMothers
Definition: PndStack.h:239
Bool_t fPersistence
Definition: PndStack.h:240
Int_t fIndex
Number of entries in fTracks.
Definition: PndStack.h:232
Bool_t fStoreSecondaries
Used for merging.
Definition: PndStack.h:236
void SetGeneratorDecayed(void)
Definition: PndMCTrack.h:88
void AddPoint(DetectorId iDet)
Definition: PndStack.cxx:408
virtual void Print(Int_t iVerbose=0) const
Definition: PndStack.cxx:393
std::map< Int_t, Int_t >::iterator fIndexIter
Definition: PndStack.h:220
Int_t fMinPoints
Definition: PndStack.h:237
const int particle
#define DEBUG
std::map< Int_t, Bool_t >::iterator fStoreIter
Definition: PndStack.h:215
Int_t fParticles
Definition: run_full.C:23
std::map< Int_t, Bool_t > fStoreMap
Definition: PndStack.h:214
Int_t a
Definition: anaLmdDigi.C:126
void SetGeneratorFlags(Int_t myid)
Definition: PndStack.cxx:248
Double_t
PndMCTrack * track
Definition: anaLmdCluster.C:89
std::stack< TParticle * > fStack
Definition: PndStack.h:200
Int_t fNTracks
Number of entries in fParticles.
Definition: PndStack.h:231
virtual TParticle * GetCurrentTrack() const
Definition: PndStack.cxx:173
std::map< Int_t, Int_t > fIndexMap
Definition: PndStack.h:219
vector< FitStore > store(3000)
static int is
Definition: ranlxd.cxx:374
Int_t fNParticles
Number of primary particles.
Definition: PndStack.h:230
virtual void UpdateTrackIndex(TRefArray *detArray)
Definition: PndStack.cxx:303
virtual void FillTrackArray()
Definition: PndStack.cxx:198
PndStack(Int_t size=100)
Definition: PndStack.cxx:27
Int_t GetSecondMotherID() const
Definition: PndMCTrack.h:75
Double32_t fEnergyCut
Definition: PndStack.h:238
ClassImp(PndAnaContFact)
Int_t iVerbose
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: PndStack.cxx:57
void SetSecondMotherID(Int_t id)
Definition: PndMCTrack.h:94
void SetGeneratorCreated(void)
Definition: PndMCTrack.h:87
void SetMotherID(Int_t id)
Definition: PndMCTrack.h:93
Int_t fNPrimaries
Index of current track.
Definition: PndStack.h:229
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
virtual void Reset()
Definition: PndStack.cxx:371
virtual void AddParticle(TParticle *part)
Definition: PndStack.cxx:186
TClonesArray * fParticles
Definition: PndStack.h:206
void SelectTracks()
Definition: PndStack.cxx:454
double pz[39]
Definition: pipisigmas.h:14
virtual void Register()
Definition: PndStack.cxx:385
virtual ~PndStack()
Definition: PndStack.cxx:42
Double_t energy
Definition: plot_dirc.C:15
PndSdsMCPoint * point
Definition: anaLmdCluster.C:72
Int_t fCurrentTrack
Definition: PndStack.h:228