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 ///////////////////////////////////////////////////////////////////////////////
20 // Particles stack class
22 ///////////////////////////////////////////////////////////////////////////////
24 #include <Riostream.h>
26 #include <TObjArray.h>
27 #include <TParticle.h>
28 #include <TParticlePDG.h>
36 #include "AliModule.h"
41 //_______________________________________________________________________
56 // Default constructor
60 //_______________________________________________________________________
61 AliStack::AliStack(Int_t size):
62 fParticles(new TClonesArray("TParticle",1000)),
63 fParticleMap(new TObjArray(size)),
79 //_______________________________________________________________________
80 AliStack::AliStack(const AliStack& st):
100 //_______________________________________________________________________
101 void AliStack::Copy(AliStack&) const
103 Fatal("Copy","Not implemented!\n");
106 //_______________________________________________________________________
107 AliStack::~AliStack()
114 fParticles->Delete();
125 //_____________________________________________________________________________
126 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
127 Float_t *vpos, Float_t *polar, Float_t tof,
128 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
131 // Load a track on the stack
133 // done 0 if the track has to be transported
135 // parent identifier of the parent track. -1 for a primary
137 // pmom momentum GeV/c
139 // polar polarisation
140 // tof time of flight in seconds
141 // mecha production mechanism
142 // ntr on output the number of the track stored
145 // const Float_t tlife=0;
148 // Here we get the static mass
149 // For MC is ok, but a more sophisticated method could be necessary
150 // if the calculated mass is required
151 // also, this method is potentially dangerous if the mass
152 // used in the MC is not the same of the PDG database
154 TParticlePDG* pmc = TDatabasePDG::Instance()->GetParticle(pdg);
156 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
157 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
158 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
160 // 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",
161 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
164 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
165 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
166 mech, ntr, weight, is);
168 Warning("SetTrack", "Particle type %d not defined in PDG Database !\n", pdg);
169 Warning("SetTrack", "Particle skipped !\n");
173 //_____________________________________________________________________________
174 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
175 Double_t px, Double_t py, Double_t pz, Double_t e,
176 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
177 Double_t polx, Double_t poly, Double_t polz,
178 TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
181 // Load a track on the stack
183 // done 0 if the track has to be transported
185 // parent identifier of the parent track. -1 for a primary
187 // kS generation status code
188 // px, py, pz momentum GeV/c
189 // vx, vy, vz position
190 // polar polarisation
191 // tof time of flight in seconds
192 // mech production mechanism
193 // ntr on output the number of the track stored
195 // New method interface:
196 // arguments were changed to be in correspondence with TParticle
198 // Note: the energy is not calculated from the static mass but
199 // it is passed by argument e.
202 const Int_t kFirstDaughter=-1;
203 const Int_t kLastDaughter=-1;
205 TClonesArray &particles = *fParticles;
207 = new(particles[fLoadPoint++])
208 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
209 px, py, pz, e, vx, vy, vz, tof);
211 particle->SetPolarisation(polx, poly, polz);
212 particle->SetWeight(weight);
213 particle->SetUniqueID(mech);
215 if(!done) particle->SetBit(kDoneBit);
217 // Declare that the daughter information is valid
218 particle->SetBit(kDaughtersBit);
219 // Add the particle to the stack
220 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
223 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
225 particle->SetLastDaughter(fNtrack);
226 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
229 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
234 // This is a primary track. Set high water mark for this event
237 // Set also number if primary tracks
238 fNprimary = fHgwmk+1;
244 //_____________________________________________________________________________
245 TParticle* AliStack::GetNextTrack(Int_t& itrack)
248 // Returns next track from stack of particles
252 TParticle* track = GetNextParticle();
256 track->SetBit(kDoneBit);
261 fCurrentTrack = track;
266 //_____________________________________________________________________________
267 void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
268 Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
269 Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
270 Double_t& polx, Double_t& poly, Double_t& polz)
273 // Return next track from stack of particles
277 TParticle* track = GetNextParticle();
278 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
282 pdg = track->GetPdgCode();
292 track->GetPolarisation(pol);
296 track->SetBit(kDoneBit);
297 // cout << "Filled params" << endl;
303 // stop and start timer when we start a primary track
304 Int_t nprimaries = fNprimary;
305 if (fCurrent >= nprimaries) return;
306 if (fCurrent < nprimaries-1) {
308 track=(TParticle*) fParticleMap->At(fCurrent+1);
309 // track->SetProcessTime(fTimer.CpuTime());
315 //_____________________________________________________________________________
316 TParticle* AliStack::GetPrimaryForTracking(Int_t i)
319 // Returns i-th primary particle if it is flagged to be tracked,
323 TParticle* particle = Particle(i);
325 if (!particle->TestBit(kDoneBit))
332 //_____________________________________________________________________________
333 void AliStack::PurifyKine()
336 // Compress kinematic tree keeping only flagged particles
337 // and renaming the particle id's in all the hits
340 TObjArray &particles = *fParticleMap;
341 int nkeep=fHgwmk+1, parent, i;
342 TParticle *part, *father;
343 TArrayI map(particles.GetLast()+1);
345 // Save in Header total number of tracks before compression
347 // If no tracks generated return now
348 if(fHgwmk+1 == fNtrack) return;
350 // First pass, invalid Daughter information
351 for(i=0; i<fNtrack; i++) {
352 // Preset map, to be removed later
353 if(i<=fHgwmk) map[i]=i ;
356 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
358 // Check of this track should be kept for physics reasons
359 if (KeepPhysics(part)) KeepTrack(i);
361 part->ResetBit(kDaughtersBit);
362 part->SetFirstDaughter(-1);
363 part->SetLastDaughter(-1);
367 // Invalid daughter information for the parent of the first particle
368 // generated. This may or may not be the current primary according to
369 // whether decays have been recorded among the primaries
370 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
371 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
372 // Second pass, build map between old and new numbering
373 for(i=fHgwmk+1; i<fNtrack; i++) {
374 if(particles.At(i)->TestBit(kKeepBit)) {
376 // This particle has to be kept
378 // If old and new are different, have to move the pointer
379 if(i!=nkeep) particles[nkeep]=particles.At(i);
380 part = dynamic_cast<TParticle*>(particles.At(nkeep));
382 // as the parent is always *before*, it must be already
383 // in place. This is what we are checking anyway!
384 if((parent=part->GetFirstMother())>fHgwmk)
385 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
386 else part->SetFirstMother(map[parent]);
392 // Fix daughters information
393 for (i=fHgwmk+1; i<nkeep; i++) {
394 part = dynamic_cast<TParticle*>(particles.At(i));
395 parent = part->GetFirstMother();
397 father = dynamic_cast<TParticle*>(particles.At(parent));
398 if(father->TestBit(kDaughtersBit)) {
400 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
401 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
403 // Initialise daughters info for first pass
404 father->SetFirstDaughter(i);
405 father->SetLastDaughter(i);
406 father->SetBit(kDaughtersBit);
411 // Now loop on all registered hit lists
412 TList* hitLists = gAlice->GetHitLists();
413 TIter next(hitLists);
414 TCollection *hitList;
415 while((hitList = dynamic_cast<TCollection*>(next()))) {
416 TIter nexthit(hitList);
418 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
419 hit->SetTrack(map[hit->GetTrack()]);
424 // This for detectors which have a special mapping mechanism
425 // for hits, such as TPC and TRD
428 TObjArray* modules = gAlice->Modules();
429 TIter nextmod(modules);
431 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
432 detector->RemapTrackHitIDs(map.GetArray());
433 detector->RemapTrackReferencesIDs(map.GetArray());
436 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
437 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
439 for (i=fHgwmk+1; i<nkeep; ++i) {
440 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
441 fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
443 particles[i]=fParticleBuffer=0;
446 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
448 Int_t toshrink = fNtrack-fHgwmk-1;
449 fLoadPoint-=toshrink;
450 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
457 //_____________________________________________________________________________
458 Bool_t AliStack::KeepPhysics(TParticle* part)
461 // Some particles have to kept on the stack for reasons motivated
462 // by physics analysis. Decision is put here.
464 Bool_t keep = kFALSE;
466 // Keep first-generation daughter from primaries with heavy flavor
468 Int_t parent = part->GetFirstMother();
469 if (parent >= 0 && parent <= fHgwmk) {
470 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
471 Int_t kf = father->GetPdgCode();
475 if (kfl > 10) kfl/=100;
477 if (kfl > 10) kfl/=10;
478 if (kfl > 10) kfl/=10;
486 //_____________________________________________________________________________
487 void AliStack::FinishEvent()
490 // Write out the kinematics that was not yet filled
493 // Update event header
497 // Fatal("FinishEvent", "No kinematics tree is defined.");
498 // Don't panic this is a probably a lego run
504 if(fTreeK->GetEntries() ==0) {
505 // set the fParticleFileMap size for the first time
506 fParticleFileMap.Set(fHgwmk+1);
509 Bool_t allFilled = kFALSE;
511 for(Int_t i=0; i<fHgwmk+1; ++i)
512 if((part=fParticleMap->At(i))) {
513 fParticleBuffer = dynamic_cast<TParticle*>(part);
514 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
516 //PH (*fParticleMap)[i]=fParticleBuffer=0;
518 fParticleMap->AddAt(0,i);
520 // When all primaries were filled no particle!=0
521 // should be left => to be removed later.
522 if (allFilled) printf("Why != 0 part # %d?\n",i);
525 // // printf("Why = 0 part # %d?\n",i); => We know.
527 // we don't break now in order to be sure there is no
528 // particle !=0 left.
529 // To be removed later and replaced with break.
530 if(!allFilled) allFilled = kTRUE;
532 // cout << "Nof particles: " << fNtrack << endl;
536 //_____________________________________________________________________________
537 void AliStack::FlagTrack(Int_t track)
540 // Flags a track and all its family tree to be kept
547 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
549 // If the particle is flagged the three from here upward is saved already
550 if(particle->TestBit(kKeepBit)) return;
552 // Save this particle
553 particle->SetBit(kKeepBit);
555 // Move to father if any
556 if((curr=particle->GetFirstMother())==-1) return;
560 //_____________________________________________________________________________
561 void AliStack::KeepTrack(Int_t track)
564 // Flags a track to be kept
567 fParticleMap->At(track)->SetBit(kKeepBit);
570 //_____________________________________________________________________________
571 void AliStack::Reset(Int_t size)
585 //_____________________________________________________________________________
586 void AliStack::ResetArrays(Int_t size)
589 // Resets stack arrays
595 fParticles = new TClonesArray("TParticle",1000);
597 fParticleMap->Clear();
598 if (size>0) fParticleMap->Expand(size);}
600 fParticleMap = new TObjArray(size);
603 //_____________________________________________________________________________
604 void AliStack::SetHighWaterMark(Int_t)
607 // Set high water mark for last track in event
611 fCurrentPrimary=fHgwmk;
613 // Set also number of primary tracks
614 fNprimary = fHgwmk+1;
618 //_____________________________________________________________________________
619 TParticle* AliStack::Particle(Int_t i)
622 // Return particle with specified ID
624 //PH if(!(*fParticleMap)[i]) {
625 if(!fParticleMap->At(i)) {
626 Int_t nentries = fParticles->GetEntriesFast();
627 // algorithmic way of getting entry index
628 // (primary particles are filled after secondaries)
629 Int_t entry = TreeKEntry(i);
630 // check whether algorithmic way and
631 // and the fParticleFileMap[i] give the same;
632 // give the fatal error if not
633 if (entry != fParticleFileMap[i]) {
635 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
636 entry, fParticleFileMap[i]);
639 fTreeK->GetEntry(entry);
640 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
641 fParticleMap->AddAt((*fParticles)[nentries],i);
643 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
644 return dynamic_cast<TParticle*>(fParticleMap->At(i));
647 //_____________________________________________________________________________
648 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
651 // return pointer to TParticle with label id
654 if ((entry = TreeKEntry(id)) < 0) return 0;
655 if (fTreeK->GetEntry(entry)<=0) return 0;
656 return fParticleBuffer;
659 //_____________________________________________________________________________
660 Int_t AliStack::TreeKEntry(Int_t id) const
663 // return entry number in the TreeK for particle with label id
664 // return negative number if label>fNtrack
668 entry = id+fNtrack-fNprimary;
670 entry = id-fNprimary;
674 //_____________________________________________________________________________
675 Int_t AliStack::CurrentTrackParent() const
678 // Return number of the parent of the current track
681 TParticle* current = (TParticle*)fParticleMap->At(fCurrent);
684 return current->GetFirstMother();
686 Warning("CurrentTrackParent", "Current track not found in the stack");
691 //_____________________________________________________________________________
692 Int_t AliStack::GetPrimary(Int_t id)
695 // Return number of primary that has generated track
703 parent=Particle(current)->GetFirstMother();
704 if(parent<0) return current;
708 //_____________________________________________________________________________
709 void AliStack::DumpPart (Int_t i) const
712 // Dumps particle i in the stack
715 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
716 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
719 //_____________________________________________________________________________
720 void AliStack::DumpPStack ()
723 // Dumps the particle stack
729 "\n\n=======================================================================\n");
730 for (i=0;i<fNtrack;i++)
732 TParticle* particle = Particle(i);
734 printf("-> %d ",i); particle->Print();
735 printf("--------------------------------------------------------------\n");
738 Warning("DumpPStack", "No particle with id %d.", i);
742 "\n=======================================================================\n\n");
744 // print particle file map
745 printf("\nParticle file map: \n");
746 for (i=0; i<fNtrack; i++)
747 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
751 //_____________________________________________________________________________
752 void AliStack::DumpLoadedStack() const
755 // Dumps the particle in the stack
756 // that are loaded in memory.
759 TObjArray &particles = *fParticleMap;
761 "\n\n=======================================================================\n");
762 for (Int_t i=0;i<fNtrack;i++)
764 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
766 printf("-> %d ",i); particle->Print();
767 printf("--------------------------------------------------------------\n");
770 printf("-> %d Particle not loaded.\n",i);
771 printf("--------------------------------------------------------------\n");
775 "\n=======================================================================\n\n");
782 //_____________________________________________________________________________
783 void AliStack::CleanParents()
786 // Clean particles stack
787 // Set parent/daughter relations
790 TObjArray &particles = *fParticleMap;
793 for(i=0; i<fHgwmk+1; i++) {
794 part = dynamic_cast<TParticle*>(particles.At(i));
795 if(part) if(!part->TestBit(kDaughtersBit)) {
796 part->SetFirstDaughter(-1);
797 part->SetLastDaughter(-1);
802 //_____________________________________________________________________________
803 TParticle* AliStack::GetNextParticle()
806 // Return next particle from stack of particles
809 TParticle* particle = 0;
811 // search secondaries
812 //for(Int_t i=fNtrack-1; i>=0; i--) {
813 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
814 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
815 if ((particle) && (!particle->TestBit(kDoneBit))) {
817 //cout << "GetNextParticle() - secondary "
818 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
823 // take next primary if all secondaries were done
824 while (fCurrentPrimary>=0) {
825 fCurrent = fCurrentPrimary;
826 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
827 if ((particle) && (!particle->TestBit(kDoneBit))) {
828 //cout << "GetNextParticle() - primary "
829 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
834 // nothing to be tracked
836 //cout << "GetNextParticle() - none "
837 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
841 //__________________________________________________________________________________________
842 void AliStack::MakeTree(Int_t event, const char * /*file*/)
845 // Make Kine tree and creates branch for writing particles
848 // Make Kinematics Tree
851 sprintf(hname,"TreeK%d",event);
852 fTreeK = new TTree(hname,"Kinematics");
853 // Create a branch for particles
854 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
855 fTreeK->Write(0,TObject::kOverwrite);
859 //_____________________________________________________________________________
860 void AliStack::BeginEvent(Int_t event)
871 sprintf(hname,"TreeK%d",event);
872 fTreeK->SetName(hname);
876 //_____________________________________________________________________________
877 void AliStack::FinishRun()
879 // Clean TreeK information
881 delete fTreeK; fTreeK = 0;
885 //_____________________________________________________________________________
886 Bool_t AliStack::GetEvent(Int_t event)
889 // Get new event from TreeK
891 // Reset/Create the particle stack
892 if (fTreeK) delete fTreeK;
894 // Get Kine Tree from file
896 sprintf(treeName,"TreeK%d",event);
897 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
900 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
902 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
903 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
906 // printf("\n primaries %d", fNprimary);
907 // printf("\n tracks %d", fNtrack);
909 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());