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>
35 #include "AliModule.h"
40 //_______________________________________________________________________
55 // Default constructor
59 //_______________________________________________________________________
60 AliStack::AliStack(Int_t size):
61 fParticles(new TClonesArray("TParticle",1000)),
62 fParticleMap(new TObjArray(size)),
78 //_______________________________________________________________________
79 AliStack::AliStack(const AliStack& st):
99 //_______________________________________________________________________
100 void AliStack::Copy(AliStack&) const
102 Fatal("Copy","Not implemented!\n");
105 //_______________________________________________________________________
106 AliStack::~AliStack()
113 fParticles->Delete();
124 //_____________________________________________________________________________
125 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
126 Float_t *vpos, Float_t *polar, Float_t tof,
127 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
130 // Load a track on the stack
132 // done 0 if the track has to be transported
134 // parent identifier of the parent track. -1 for a primary
136 // pmom momentum GeV/c
138 // polar polarisation
139 // tof time of flight in seconds
140 // mecha production mechanism
141 // ntr on output the number of the track stored
144 // const Float_t tlife=0;
147 // Here we get the static mass
148 // For MC is ok, but a more sophisticated method could be necessary
149 // if the calculated mass is required
150 // also, this method is potentially dangerous if the mass
151 // used in the MC is not the same of the PDG database
153 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
154 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
155 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
157 // 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",
158 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
161 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
162 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
163 mech, ntr, weight, is);
166 //_____________________________________________________________________________
167 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
168 Double_t px, Double_t py, Double_t pz, Double_t e,
169 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
170 Double_t polx, Double_t poly, Double_t polz,
171 TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
174 // Load a track on the stack
176 // done 0 if the track has to be transported
178 // parent identifier of the parent track. -1 for a primary
180 // kS generation status code
181 // px, py, pz momentum GeV/c
182 // vx, vy, vz position
183 // polar polarisation
184 // tof time of flight in seconds
185 // mech production mechanism
186 // ntr on output the number of the track stored
188 // New method interface:
189 // arguments were changed to be in correspondence with TParticle
191 // Note: the energy is not calculated from the static mass but
192 // it is passed by argument e.
195 const Int_t kFirstDaughter=-1;
196 const Int_t kLastDaughter=-1;
198 TClonesArray &particles = *fParticles;
200 = new(particles[fLoadPoint++])
201 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
202 px, py, pz, e, vx, vy, vz, tof);
204 particle->SetPolarisation(polx, poly, polz);
205 particle->SetWeight(weight);
206 particle->SetUniqueID(mech);
208 if(!done) particle->SetBit(kDoneBit);
210 // Declare that the daughter information is valid
211 particle->SetBit(kDaughtersBit);
212 // Add the particle to the stack
213 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
216 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
218 particle->SetLastDaughter(fNtrack);
219 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
222 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
227 // This is a primary track. Set high water mark for this event
230 // Set also number if primary tracks
231 fNprimary = fHgwmk+1;
237 //_____________________________________________________________________________
238 TParticle* AliStack::GetNextTrack(Int_t& itrack)
241 // Returns next track from stack of particles
245 TParticle* track = GetNextParticle();
249 track->SetBit(kDoneBit);
258 //_____________________________________________________________________________
259 void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
260 Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
261 Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
262 Double_t& polx, Double_t& poly, Double_t& polz)
265 // Return next track from stack of particles
269 TParticle* track = GetNextParticle();
270 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
274 pdg = track->GetPdgCode();
284 track->GetPolarisation(pol);
288 track->SetBit(kDoneBit);
289 // cout << "Filled params" << endl;
295 // stop and start timer when we start a primary track
296 Int_t nprimaries = fNprimary;
297 if (fCurrent >= nprimaries) return;
298 if (fCurrent < nprimaries-1) {
300 track=(TParticle*) fParticleMap->At(fCurrent+1);
301 // track->SetProcessTime(fTimer.CpuTime());
307 //_____________________________________________________________________________
308 TParticle* AliStack::GetPrimaryForTracking(Int_t i)
311 // Returns i-th primary particle if it is flagged to be tracked,
315 TParticle* particle = Particle(i);
317 if (!particle->TestBit(kDoneBit))
324 //_____________________________________________________________________________
325 void AliStack::PurifyKine()
328 // Compress kinematic tree keeping only flagged particles
329 // and renaming the particle id's in all the hits
332 TObjArray &particles = *fParticleMap;
333 int nkeep=fHgwmk+1, parent, i;
334 TParticle *part, *father;
335 TArrayI map(particles.GetLast()+1);
337 // Save in Header total number of tracks before compression
339 // If no tracks generated return now
340 if(fHgwmk+1 == fNtrack) return;
342 // First pass, invalid Daughter information
343 for(i=0; i<fNtrack; i++) {
344 // Preset map, to be removed later
345 if(i<=fHgwmk) map[i]=i ;
348 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
350 // Check of this track should be kept for physics reasons
351 if (KeepPhysics(part)) KeepTrack(i);
353 part->ResetBit(kDaughtersBit);
354 part->SetFirstDaughter(-1);
355 part->SetLastDaughter(-1);
359 // Invalid daughter information for the parent of the first particle
360 // generated. This may or may not be the current primary according to
361 // whether decays have been recorded among the primaries
362 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
363 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
364 // Second pass, build map between old and new numbering
365 for(i=fHgwmk+1; i<fNtrack; i++) {
366 if(particles.At(i)->TestBit(kKeepBit)) {
368 // This particle has to be kept
370 // If old and new are different, have to move the pointer
371 if(i!=nkeep) particles[nkeep]=particles.At(i);
372 part = dynamic_cast<TParticle*>(particles.At(nkeep));
374 // as the parent is always *before*, it must be already
375 // in place. This is what we are checking anyway!
376 if((parent=part->GetFirstMother())>fHgwmk)
377 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
378 else part->SetFirstMother(map[parent]);
384 // Fix daughters information
385 for (i=fHgwmk+1; i<nkeep; i++) {
386 part = dynamic_cast<TParticle*>(particles.At(i));
387 parent = part->GetFirstMother();
389 father = dynamic_cast<TParticle*>(particles.At(parent));
390 if(father->TestBit(kDaughtersBit)) {
392 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
393 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
395 // Initialise daughters info for first pass
396 father->SetFirstDaughter(i);
397 father->SetLastDaughter(i);
398 father->SetBit(kDaughtersBit);
403 // Now loop on all registered hit lists
404 TList* hitLists = gAlice->GetHitLists();
405 TIter next(hitLists);
406 TCollection *hitList;
407 while((hitList = dynamic_cast<TCollection*>(next()))) {
408 TIter nexthit(hitList);
410 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
411 hit->SetTrack(map[hit->GetTrack()]);
416 // This for detectors which have a special mapping mechanism
417 // for hits, such as TPC and TRD
420 TObjArray* modules = gAlice->Modules();
421 TIter nextmod(modules);
423 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
424 detector->RemapTrackHitIDs(map.GetArray());
425 detector->RemapTrackReferencesIDs(map.GetArray());
428 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
429 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
431 for (i=fHgwmk+1; i<nkeep; ++i) {
432 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
433 fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
435 particles[i]=fParticleBuffer=0;
438 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
440 Int_t toshrink = fNtrack-fHgwmk-1;
441 fLoadPoint-=toshrink;
442 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
449 Bool_t AliStack::KeepPhysics(TParticle* part)
452 // Some particles have to kept on the stack for reasons motivated
453 // by physics analysis. Decision is put here.
455 Bool_t keep = kFALSE;
457 // Keep first-generation daughter from primaries with heavy flavor
459 Int_t parent = part->GetFirstMother();
460 if (parent >= 0 && parent <= fHgwmk) {
461 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
462 Int_t kf = father->GetPdgCode();
466 if (kfl > 10) kfl/=100;
468 if (kfl > 10) kfl/=10;
469 if (kfl > 10) kfl/=10;
477 //_____________________________________________________________________________
478 void AliStack::FinishEvent()
481 // Write out the kinematics that was not yet filled
484 // Update event header
488 // Fatal("FinishEvent", "No kinematics tree is defined.");
489 // Don't panic this is a probably a lego run
495 if(fTreeK->GetEntries() ==0) {
496 // set the fParticleFileMap size for the first time
497 fParticleFileMap.Set(fHgwmk+1);
500 Bool_t allFilled = kFALSE;
502 for(Int_t i=0; i<fHgwmk+1; ++i)
503 if((part=fParticleMap->At(i))) {
504 fParticleBuffer = dynamic_cast<TParticle*>(part);
505 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
507 //PH (*fParticleMap)[i]=fParticleBuffer=0;
509 fParticleMap->AddAt(0,i);
511 // When all primaries were filled no particle!=0
512 // should be left => to be removed later.
513 if (allFilled) printf("Why != 0 part # %d?\n",i);
516 // // printf("Why = 0 part # %d?\n",i); => We know.
518 // we don't break now in order to be sure there is no
519 // particle !=0 left.
520 // To be removed later and replaced with break.
521 if(!allFilled) allFilled = kTRUE;
523 // cout << "Nof particles: " << fNtrack << endl;
527 //_____________________________________________________________________________
528 void AliStack::FlagTrack(Int_t track)
531 // Flags a track and all its family tree to be kept
538 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
540 // If the particle is flagged the three from here upward is saved already
541 if(particle->TestBit(kKeepBit)) return;
543 // Save this particle
544 particle->SetBit(kKeepBit);
546 // Move to father if any
547 if((curr=particle->GetFirstMother())==-1) return;
551 //_____________________________________________________________________________
552 void AliStack::KeepTrack(Int_t track)
555 // Flags a track to be kept
558 fParticleMap->At(track)->SetBit(kKeepBit);
561 //_____________________________________________________________________________
562 void AliStack::Reset(Int_t size)
576 //_____________________________________________________________________________
577 void AliStack::ResetArrays(Int_t size)
580 // Resets stack arrays
586 fParticles = new TClonesArray("TParticle",1000);
588 fParticleMap->Clear();
589 if (size>0) fParticleMap->Expand(size);}
591 fParticleMap = new TObjArray(size);
594 //_____________________________________________________________________________
595 void AliStack::SetHighWaterMark(Int_t)
598 // Set high water mark for last track in event
602 fCurrentPrimary=fHgwmk;
604 // Set also number of primary tracks
605 fNprimary = fHgwmk+1;
609 //_____________________________________________________________________________
610 TParticle* AliStack::Particle(Int_t i)
613 // Return particle with specified ID
615 //PH if(!(*fParticleMap)[i]) {
616 if(!fParticleMap->At(i)) {
617 Int_t nentries = fParticles->GetEntriesFast();
618 // algorithmic way of getting entry index
619 // (primary particles are filled after secondaries)
620 Int_t entry = TreeKEntry(i);
621 // check whether algorithmic way and
622 // and the fParticleFileMap[i] give the same;
623 // give the fatal error if not
624 if (entry != fParticleFileMap[i]) {
626 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
627 entry, fParticleFileMap[i]);
630 fTreeK->GetEntry(entry);
631 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
632 fParticleMap->AddAt((*fParticles)[nentries],i);
634 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
635 return dynamic_cast<TParticle*>(fParticleMap->At(i));
638 //_____________________________________________________________________________
639 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
642 // return pointer to TParticle with label id
645 if ((entry = TreeKEntry(id)) < 0) return 0;
646 if (fTreeK->GetEntry(entry)<=0) return 0;
647 return fParticleBuffer;
650 //_____________________________________________________________________________
651 Int_t AliStack::TreeKEntry(Int_t id) const
654 // return entry number in the TreeK for particle with label id
655 // return negative number if label>fNtrack
659 entry = id+fNtrack-fNprimary;
661 entry = id-fNprimary;
665 //_____________________________________________________________________________
666 Int_t AliStack::GetPrimary(Int_t id)
669 // Return number of primary that has generated track
677 parent=Particle(current)->GetFirstMother();
678 if(parent<0) return current;
682 //_____________________________________________________________________________
683 void AliStack::DumpPart (Int_t i) const
686 // Dumps particle i in the stack
689 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
690 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
693 //_____________________________________________________________________________
694 void AliStack::DumpPStack ()
697 // Dumps the particle stack
703 "\n\n=======================================================================\n");
704 for (i=0;i<fNtrack;i++)
706 TParticle* particle = Particle(i);
708 printf("-> %d ",i); particle->Print();
709 printf("--------------------------------------------------------------\n");
712 Warning("DumpPStack", "No particle with id %d.", i);
716 "\n=======================================================================\n\n");
718 // print particle file map
719 printf("\nParticle file map: \n");
720 for (i=0; i<fNtrack; i++)
721 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
725 //_____________________________________________________________________________
726 void AliStack::DumpLoadedStack() const
729 // Dumps the particle in the stack
730 // that are loaded in memory.
733 TObjArray &particles = *fParticleMap;
735 "\n\n=======================================================================\n");
736 for (Int_t i=0;i<fNtrack;i++)
738 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
740 printf("-> %d ",i); particle->Print();
741 printf("--------------------------------------------------------------\n");
744 printf("-> %d Particle not loaded.\n",i);
745 printf("--------------------------------------------------------------\n");
749 "\n=======================================================================\n\n");
756 //_____________________________________________________________________________
757 void AliStack::CleanParents()
760 // Clean particles stack
761 // Set parent/daughter relations
764 TObjArray &particles = *fParticleMap;
767 for(i=0; i<fHgwmk+1; i++) {
768 part = dynamic_cast<TParticle*>(particles.At(i));
769 if(part) if(!part->TestBit(kDaughtersBit)) {
770 part->SetFirstDaughter(-1);
771 part->SetLastDaughter(-1);
776 //_____________________________________________________________________________
777 TParticle* AliStack::GetNextParticle()
780 // Return next particle from stack of particles
783 TParticle* particle = 0;
785 // search secondaries
786 //for(Int_t i=fNtrack-1; i>=0; i--) {
787 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
788 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
789 if ((particle) && (!particle->TestBit(kDoneBit))) {
791 //cout << "GetNextParticle() - secondary "
792 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
797 // take next primary if all secondaries were done
798 while (fCurrentPrimary>=0) {
799 fCurrent = fCurrentPrimary;
800 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
801 if ((particle) && (!particle->TestBit(kDoneBit))) {
802 //cout << "GetNextParticle() - primary "
803 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
808 // nothing to be tracked
810 //cout << "GetNextParticle() - none "
811 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
815 //__________________________________________________________________________________________
816 void AliStack::MakeTree(Int_t event, const char * /*file*/)
819 // Make Kine tree and creates branch for writing particles
822 // Make Kinematics Tree
825 sprintf(hname,"TreeK%d",event);
826 fTreeK = new TTree(hname,"Kinematics");
827 // Create a branch for particles
828 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
829 fTreeK->Write(0,TObject::kOverwrite);
833 //_____________________________________________________________________________
834 void AliStack::BeginEvent(Int_t event)
845 sprintf(hname,"TreeK%d",event);
846 fTreeK->SetName(hname);
850 //_____________________________________________________________________________
851 void AliStack::FinishRun()
853 // Clean TreeK information
855 delete fTreeK; fTreeK = 0;
859 Bool_t AliStack::GetEvent(Int_t event)
862 // Get new event from TreeK
864 // Reset/Create the particle stack
865 if (fTreeK) delete fTreeK;
867 // Get Kine Tree from file
869 sprintf(treeName,"TreeK%d",event);
870 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
873 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
875 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
876 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
879 // printf("\n primaries %d", fNprimary);
880 // printf("\n tracks %d", fNtrack);
882 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());