1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 Revision 1.2 2001/05/17 05:49:39 fca
19 Reset pointers to daughters
21 Revision 1.1 2001/05/16 14:57:22 alibrary
22 New files for folders and Stack
26 ///////////////////////////////////////////////////////////////////////////////
28 // Particles stack class
30 ///////////////////////////////////////////////////////////////////////////////
34 #include <TObjArray.h>
35 #include <TParticle.h>
43 #include "AliModule.h"
45 //#include "ETrackBits.h"
49 //_____________________________________________________________________________
50 AliStack::AliStack(Int_t size)
56 // Create the particles arrays
57 fParticles = new TClonesArray("TParticle",1000);
58 fParticleMap = new TObjArray(size);
59 fParticleBuffer = new TParticle();
68 //_____________________________________________________________________________
72 // Default constructor
75 // Create the particles arrays
76 fParticles = new TClonesArray("TParticle",1000);
77 fParticleMap = new TObjArray(10000);
78 fParticleBuffer = new TParticle();
87 //_____________________________________________________________________________
99 delete fParticleBuffer;
107 //_____________________________________________________________________________
108 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
109 Float_t *vpos, Float_t *polar, Float_t tof,
110 AliMCProcess mech, Int_t &ntr, Float_t weight)
113 // Load a track on the stack
115 // done 0 if the track has to be transported
117 // parent identifier of the parent track. -1 for a primary
119 // pmom momentum GeV/c
121 // polar polarisation
122 // tof time of flight in seconds
123 // mecha production mechanism
124 // ntr on output the number of the track stored
128 const Int_t kfirstdaughter=-1;
129 const Int_t klastdaughter=-1;
131 // const Float_t tlife=0;
134 // Here we get the static mass
135 // For MC is ok, but a more sophisticated method could be necessary
136 // if the calculated mass is required
137 // also, this method is potentially dangerous if the mass
138 // used in the MC is not the same of the PDG database
140 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
141 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
142 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
144 // printf("Loading mass %f ene %f No %d ip %d parent %d done %d pos %f %f %f mom %f %f %f kS %d m \n",
145 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
147 TClonesArray &particles = *fParticles;
149 = new(particles[fLoadPoint++])
150 TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
151 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
152 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
153 particle->SetWeight(weight);
154 particle->SetUniqueID(mech);
155 if(!done) particle->SetBit(kDoneBit);
158 // Declare that the daughter information is valid
159 particle->SetBit(kDaughtersBit);
160 // Add the particle to the stack
161 fParticleMap->AddAtAndExpand(particle, fNtrack);
164 particle = (TParticle*) fParticleMap->At(parent);
165 particle->SetLastDaughter(fNtrack);
166 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
170 // This is a primary track. Set high water mark for this event
173 // Set also number if primary tracks
174 fNprimary = fHgwmk+1;
180 //_____________________________________________________________________________
181 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
182 Double_t px, Double_t py, Double_t pz, Double_t e,
183 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
184 Double_t polx, Double_t poly, Double_t polz,
185 AliMCProcess mech, Int_t &ntr, Float_t weight)
188 // Load a track on the stack
190 // done 0 if the track has to be transported
192 // parent identifier of the parent track. -1 for a primary
194 // kS generation status code
195 // px, py, pz momentum GeV/c
196 // vx, vy, vz position
197 // polar polarisation
198 // tof time of flight in seconds
199 // mech production mechanism
200 // ntr on output the number of the track stored
202 // New method interface:
203 // arguments were changed to be in correspondence with TParticle
205 // Note: the energy is not calculated from the static mass but
206 // it is passed by argument e.
210 const Int_t kFirstDaughter=-1;
211 const Int_t kLastDaughter=-1;
213 TClonesArray &particles = *fParticles;
215 = new(particles[fLoadPoint++])
216 TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
217 px, py, pz, e, vx, vy, vz, tof);
219 particle->SetPolarisation(polx, poly, polz);
220 particle->SetWeight(weight);
221 particle->SetUniqueID(mech);
223 if(!done) particle->SetBit(kDoneBit);
225 // Declare that the daughter information is valid
226 particle->SetBit(kDaughtersBit);
227 // Add the particle to the stack
228 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
231 particle = (TParticle*) fParticleMap->At(parent);
232 particle->SetLastDaughter(fNtrack);
233 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
237 // This is a primary track. Set high water mark for this event
240 // Set also number if primary tracks
241 fNprimary = fHgwmk+1;
247 //_____________________________________________________________________________
248 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
249 Float_t &e, Float_t *vpos, Float_t *polar,
253 // Return next track from stack of particles
257 TParticle* track = GetNextParticle();
258 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
262 ipart=track->GetPdgCode();
271 track->GetPolarisation(pol);
276 track->SetBit(kDoneBit);
277 //cout << "Filled params" << endl;
283 // stop and start timer when we start a primary track
284 Int_t nprimaries = fNprimary;
285 if (fCurrent >= nprimaries) return;
286 if (fCurrent < nprimaries-1) {
288 track=(TParticle*) fParticleMap->At(fCurrent+1);
289 // track->SetProcessTime(fTimer.CpuTime());
295 //_____________________________________________________________________________
296 void AliStack::PurifyKine()
299 // Compress kinematic tree keeping only flagged particles
300 // and renaming the particle id's in all the hits
303 TObjArray &particles = *fParticleMap;
304 int nkeep=fHgwmk+1, parent, i;
305 TParticle *part, *father;
306 TArrayI map(particles.GetLast()+1);
308 // Save in Header total number of tracks before compression
310 // If no tracks generated return now
311 if(fHgwmk+1 == fNtrack) return;
313 // First pass, invalid Daughter information
314 for(i=0; i<fNtrack; i++) {
315 // Preset map, to be removed later
316 if(i<=fHgwmk) map[i]=i ;
319 if((part=(TParticle*) particles.At(i))) {
320 part->ResetBit(kDaughtersBit);
321 part->SetFirstDaughter(-1);
322 part->SetLastDaughter(-1);
326 // Invalid daughter information for the parent of the first particle
327 // generated. This may or may not be the current primary according to
328 // whether decays have been recorded among the primaries
329 part = (TParticle *)particles.At(fHgwmk+1);
330 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
331 // Second pass, build map between old and new numbering
332 for(i=fHgwmk+1; i<fNtrack; i++) {
333 if(particles.At(i)->TestBit(kKeepBit)) {
335 // This particle has to be kept
337 // If old and new are different, have to move the pointer
338 if(i!=nkeep) particles[nkeep]=particles.At(i);
339 part = (TParticle*) particles.At(nkeep);
341 // as the parent is always *before*, it must be already
342 // in place. This is what we are checking anyway!
343 if((parent=part->GetFirstMother())>fHgwmk)
344 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
345 else part->SetFirstMother(map[parent]);
351 // Fix daughters information
352 for (i=fHgwmk+1; i<nkeep; i++) {
353 part = (TParticle *)particles.At(i);
354 parent = part->GetFirstMother();
356 father = (TParticle *)particles.At(parent);
357 if(father->TestBit(kDaughtersBit)) {
359 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
360 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
362 // Initialise daughters info for first pass
363 father->SetFirstDaughter(i);
364 father->SetLastDaughter(i);
365 father->SetBit(kDaughtersBit);
370 // Now loop on all registered hit lists
371 TList* hitLists = gAlice->GetHitLists();
372 TIter next(hitLists);
373 TCollection *hitList;
374 while((hitList = (TCollection*)next())) {
375 TIter nexthit(hitList);
377 while((hit = (AliHit*)nexthit())) {
378 hit->SetTrack(map[hit->GetTrack()]);
383 // This for detectors which have a special mapping mechanism
384 // for hits, such as TPC and TRD
387 TObjArray* modules = gAlice->Modules();
388 TIter nextmod(modules);
390 while((detector = (AliModule*)nextmod())) {
391 detector->RemapTrackHitIDs(map.GetArray());
394 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
395 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
397 for (i=fHgwmk+1; i<nkeep; ++i) {
398 fParticleBuffer = (TParticle*) particles.At(i);
399 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
404 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
406 Int_t toshrink = fNtrack-fHgwmk-1;
407 fLoadPoint-=toshrink;
408 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
415 //_____________________________________________________________________________
416 void AliStack::FinishEvent()
419 // Write out the kinematics that was not yet filled
422 // Update event header
426 // Fatal("FinishEvent", "No kinematics tree is defined.");
427 // Don't panic this is a probably a lego run
433 if(fTreeK->GetEntries() ==0) {
434 // set the fParticleFileMap size for the first time
435 fParticleFileMap.Set(fHgwmk+1);
438 Bool_t allFilled = kFALSE;
440 for(Int_t i=0; i<fHgwmk+1; ++i)
441 if((part=fParticleMap->At(i))) {
442 fParticleBuffer = (TParticle*) part;
443 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
445 (*fParticleMap)[i]=0;
447 // When all primaries were filled no particle!=0
448 // should be left => to be removed later.
449 if (allFilled) printf("Why != 0 part # %d?\n",i);
452 // // printf("Why = 0 part # %d?\n",i); => We know.
454 // we don't break now in order to be sure there is no
455 // particle !=0 left.
456 // To be removed later and replaced with break.
457 if(!allFilled) allFilled = kTRUE;
459 //cout << "Nof particles: " << fNtrack << endl;
463 //_____________________________________________________________________________
464 void AliStack::FlagTrack(Int_t track)
467 // Flags a track and all its family tree to be kept
474 particle=(TParticle*)fParticleMap->At(curr);
476 // If the particle is flagged the three from here upward is saved already
477 if(particle->TestBit(kKeepBit)) return;
479 // Save this particle
480 particle->SetBit(kKeepBit);
482 // Move to father if any
483 if((curr=particle->GetFirstMother())==-1) return;
487 //_____________________________________________________________________________
488 void AliStack::KeepTrack(Int_t track)
491 // Flags a track to be kept
494 fParticleMap->At(track)->SetBit(kKeepBit);
497 //_____________________________________________________________________________
498 void AliStack::Reset(Int_t size)
512 //_____________________________________________________________________________
513 void AliStack::ResetArrays(Int_t size)
516 // Resets stack arrays
520 fParticleMap->Clear();
521 if (size>0) fParticleMap->Expand(size);
524 //_____________________________________________________________________________
525 void AliStack::SetHighWaterMark(Int_t nt)
528 // Set high water mark for last track in event
532 fCurrentPrimary=fHgwmk;
534 // Set also number of primary tracks
535 fNprimary = fHgwmk+1;
539 //_____________________________________________________________________________
540 TParticle* AliStack::Particle(Int_t i)
543 // Return particle with specified ID
545 if(!(*fParticleMap)[i]) {
546 Int_t nentries = fParticles->GetEntries();
547 // algorithmic way of getting entry index
548 // (primary particles are filled after secondaries)
551 entry = i+fNtrack-fNprimary;
554 // check whether algorithmic way and
555 // and the fParticleFileMap[i] give the same;
556 // give the fatal error if not
557 if (entry != fParticleFileMap[i]) {
559 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
560 entry, fParticleFileMap[i]);
563 fTreeK->GetEntry(entry);
564 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
565 fParticleMap->AddAt((*fParticles)[nentries],i);
567 return (TParticle *) (*fParticleMap)[i];
570 //_____________________________________________________________________________
571 Int_t AliStack::GetPrimary(Int_t id) const
574 // Return number of primary that has generated track
583 part = (TParticle *)fParticleMap->At(current);
584 parent=part->GetFirstMother();
585 if(parent<0) return current;
589 //_____________________________________________________________________________
590 void AliStack::DumpPart (Int_t i) const
593 // Dumps particle i in the stack
596 ((TParticle*) (*fParticleMap)[i])->Print();
599 //_____________________________________________________________________________
600 void AliStack::DumpPStack ()
603 // Dumps the particle stack
607 "\n\n=======================================================================\n");
608 for (Int_t i=0;i<fNtrack;i++)
610 TParticle* particle = Particle(i);
612 printf("-> %d ",i); particle->Print();
613 printf("--------------------------------------------------------------\n");
616 Warning("DumpPStack", "No particle with id %d.", i);
620 "\n=======================================================================\n\n");
622 // print particle file map
623 printf("\nParticle file map: \n");
624 for (Int_t i=0; i<fNtrack; i++)
625 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
629 //_____________________________________________________________________________
630 void AliStack::DumpLoadedStack() const
633 // Dumps the particle in the stack
634 // that are loaded in memory.
637 TObjArray &particles = *fParticleMap;
639 "\n\n=======================================================================\n");
640 for (Int_t i=0;i<fNtrack;i++)
642 TParticle* particle = (TParticle*) particles[i];
644 printf("-> %d ",i); particle->Print();
645 printf("--------------------------------------------------------------\n");
648 printf("-> %d Particle not loaded.\n",i);
649 printf("--------------------------------------------------------------\n");
653 "\n=======================================================================\n\n");
660 //_____________________________________________________________________________
661 void AliStack::CleanParents()
664 // Clean particles stack
665 // Set parent/daughter relations
668 TObjArray &particles = *fParticleMap;
671 for(i=0; i<fHgwmk+1; i++) {
672 part = (TParticle *)particles.At(i);
673 if(part) if(!part->TestBit(kDaughtersBit)) {
674 part->SetFirstDaughter(-1);
675 part->SetLastDaughter(-1);
680 //_____________________________________________________________________________
681 TParticle* AliStack::GetNextParticle()
684 // Return next particle from stack of particles
687 TParticle* particle = 0;
689 // search secondaries
690 //for(Int_t i=fNtrack-1; i>=0; i--) {
691 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
692 particle = (TParticle*) fParticleMap->At(i);
693 if ((particle) && (!particle->TestBit(kDoneBit))) {
695 //cout << "GetNextParticle() - secondary "
696 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
701 // take next primary if all secondaries were done
702 while (fCurrentPrimary>=0) {
703 fCurrent = fCurrentPrimary;
704 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
705 if ((particle) && (!particle->TestBit(kDoneBit))) {
706 //cout << "GetNextParticle() - primary "
707 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
712 // nothing to be tracked
714 //cout << "GetNextParticle() - none "
715 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
719 //__________________________________________________________________________________________
720 void AliStack::MakeTree(Int_t event, const char *file)
723 // Make Kine tree and creates branch for writing particles
726 // Make Kinematics Tree
728 // printf("\n MakeTree called %d", event);
730 sprintf(hname,"TreeK%d",event);
731 fTreeK = new TTree(hname,"Kinematics");
732 // Create a branch for particles
733 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000, 1);
734 fTreeK->Write(0,TObject::kOverwrite);
738 void AliStack::BeginEvent(Int_t event)
749 sprintf(hname,"TreeK%d",event);
750 fTreeK->SetName(hname);
754 void AliStack::FinishRun()
756 // Clean TreeK information
758 delete fTreeK; fTreeK = 0;
762 void AliStack::GetEvent(Int_t event)
765 // Get new event from TreeK
766 // Reset/Create the particle stack
767 if (fTreeK) delete fTreeK;
769 // Get Kine Tree from file
771 sprintf(treeName,"TreeK%d",event);
772 fTreeK = (TTree*)gDirectory->Get(treeName);
774 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
777 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
779 // printf("\n primaries %d", fNprimary);
780 // printf("\n tracks %d", fNtrack);
782 Int_t size = (Int_t)fTreeK->GetEntries();