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 //_____________________________________________________________________________
450 Bool_t AliStack::KeepPhysics(TParticle* part)
453 // Some particles have to kept on the stack for reasons motivated
454 // by physics analysis. Decision is put here.
456 Bool_t keep = kFALSE;
458 // Keep first-generation daughter from primaries with heavy flavor
460 Int_t parent = part->GetFirstMother();
461 if (parent >= 0 && parent <= fHgwmk) {
462 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
463 Int_t kf = father->GetPdgCode();
467 if (kfl > 10) kfl/=100;
469 if (kfl > 10) kfl/=10;
470 if (kfl > 10) kfl/=10;
478 //_____________________________________________________________________________
479 void AliStack::FinishEvent()
482 // Write out the kinematics that was not yet filled
485 // Update event header
489 // Fatal("FinishEvent", "No kinematics tree is defined.");
490 // Don't panic this is a probably a lego run
496 if(fTreeK->GetEntries() ==0) {
497 // set the fParticleFileMap size for the first time
498 fParticleFileMap.Set(fHgwmk+1);
501 Bool_t allFilled = kFALSE;
503 for(Int_t i=0; i<fHgwmk+1; ++i)
504 if((part=fParticleMap->At(i))) {
505 fParticleBuffer = dynamic_cast<TParticle*>(part);
506 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
508 //PH (*fParticleMap)[i]=fParticleBuffer=0;
510 fParticleMap->AddAt(0,i);
512 // When all primaries were filled no particle!=0
513 // should be left => to be removed later.
514 if (allFilled) printf("Why != 0 part # %d?\n",i);
517 // // printf("Why = 0 part # %d?\n",i); => We know.
519 // we don't break now in order to be sure there is no
520 // particle !=0 left.
521 // To be removed later and replaced with break.
522 if(!allFilled) allFilled = kTRUE;
524 // cout << "Nof particles: " << fNtrack << endl;
528 //_____________________________________________________________________________
529 void AliStack::FlagTrack(Int_t track)
532 // Flags a track and all its family tree to be kept
539 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
541 // If the particle is flagged the three from here upward is saved already
542 if(particle->TestBit(kKeepBit)) return;
544 // Save this particle
545 particle->SetBit(kKeepBit);
547 // Move to father if any
548 if((curr=particle->GetFirstMother())==-1) return;
552 //_____________________________________________________________________________
553 void AliStack::KeepTrack(Int_t track)
556 // Flags a track to be kept
559 fParticleMap->At(track)->SetBit(kKeepBit);
562 //_____________________________________________________________________________
563 void AliStack::Reset(Int_t size)
577 //_____________________________________________________________________________
578 void AliStack::ResetArrays(Int_t size)
581 // Resets stack arrays
587 fParticles = new TClonesArray("TParticle",1000);
589 fParticleMap->Clear();
590 if (size>0) fParticleMap->Expand(size);}
592 fParticleMap = new TObjArray(size);
595 //_____________________________________________________________________________
596 void AliStack::SetHighWaterMark(Int_t)
599 // Set high water mark for last track in event
603 fCurrentPrimary=fHgwmk;
605 // Set also number of primary tracks
606 fNprimary = fHgwmk+1;
610 //_____________________________________________________________________________
611 TParticle* AliStack::Particle(Int_t i)
614 // Return particle with specified ID
616 //PH if(!(*fParticleMap)[i]) {
617 if(!fParticleMap->At(i)) {
618 Int_t nentries = fParticles->GetEntriesFast();
619 // algorithmic way of getting entry index
620 // (primary particles are filled after secondaries)
621 Int_t entry = TreeKEntry(i);
622 // check whether algorithmic way and
623 // and the fParticleFileMap[i] give the same;
624 // give the fatal error if not
625 if (entry != fParticleFileMap[i]) {
627 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
628 entry, fParticleFileMap[i]);
631 fTreeK->GetEntry(entry);
632 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
633 fParticleMap->AddAt((*fParticles)[nentries],i);
635 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
636 return dynamic_cast<TParticle*>(fParticleMap->At(i));
639 //_____________________________________________________________________________
640 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
643 // return pointer to TParticle with label id
646 if ((entry = TreeKEntry(id)) < 0) return 0;
647 if (fTreeK->GetEntry(entry)<=0) return 0;
648 return fParticleBuffer;
651 //_____________________________________________________________________________
652 Int_t AliStack::TreeKEntry(Int_t id) const
655 // return entry number in the TreeK for particle with label id
656 // return negative number if label>fNtrack
660 entry = id+fNtrack-fNprimary;
662 entry = id-fNprimary;
666 //_____________________________________________________________________________
667 Int_t AliStack::CurrentTrackParent() const
670 // Return number of the parent of the current track
673 TParticle* current = (TParticle*)fParticleMap->At(fCurrent);
676 return current->GetFirstMother();
678 Warning("CurrentTrackParent", "Current track not found in the stack");
683 //_____________________________________________________________________________
684 Int_t AliStack::GetPrimary(Int_t id)
687 // Return number of primary that has generated track
695 parent=Particle(current)->GetFirstMother();
696 if(parent<0) return current;
700 //_____________________________________________________________________________
701 void AliStack::DumpPart (Int_t i) const
704 // Dumps particle i in the stack
707 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
708 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
711 //_____________________________________________________________________________
712 void AliStack::DumpPStack ()
715 // Dumps the particle stack
721 "\n\n=======================================================================\n");
722 for (i=0;i<fNtrack;i++)
724 TParticle* particle = Particle(i);
726 printf("-> %d ",i); particle->Print();
727 printf("--------------------------------------------------------------\n");
730 Warning("DumpPStack", "No particle with id %d.", i);
734 "\n=======================================================================\n\n");
736 // print particle file map
737 printf("\nParticle file map: \n");
738 for (i=0; i<fNtrack; i++)
739 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
743 //_____________________________________________________________________________
744 void AliStack::DumpLoadedStack() const
747 // Dumps the particle in the stack
748 // that are loaded in memory.
751 TObjArray &particles = *fParticleMap;
753 "\n\n=======================================================================\n");
754 for (Int_t i=0;i<fNtrack;i++)
756 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
758 printf("-> %d ",i); particle->Print();
759 printf("--------------------------------------------------------------\n");
762 printf("-> %d Particle not loaded.\n",i);
763 printf("--------------------------------------------------------------\n");
767 "\n=======================================================================\n\n");
774 //_____________________________________________________________________________
775 void AliStack::CleanParents()
778 // Clean particles stack
779 // Set parent/daughter relations
782 TObjArray &particles = *fParticleMap;
785 for(i=0; i<fHgwmk+1; i++) {
786 part = dynamic_cast<TParticle*>(particles.At(i));
787 if(part) if(!part->TestBit(kDaughtersBit)) {
788 part->SetFirstDaughter(-1);
789 part->SetLastDaughter(-1);
794 //_____________________________________________________________________________
795 TParticle* AliStack::GetNextParticle()
798 // Return next particle from stack of particles
801 TParticle* particle = 0;
803 // search secondaries
804 //for(Int_t i=fNtrack-1; i>=0; i--) {
805 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
806 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
807 if ((particle) && (!particle->TestBit(kDoneBit))) {
809 //cout << "GetNextParticle() - secondary "
810 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
815 // take next primary if all secondaries were done
816 while (fCurrentPrimary>=0) {
817 fCurrent = fCurrentPrimary;
818 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
819 if ((particle) && (!particle->TestBit(kDoneBit))) {
820 //cout << "GetNextParticle() - primary "
821 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
826 // nothing to be tracked
828 //cout << "GetNextParticle() - none "
829 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
833 //__________________________________________________________________________________________
834 void AliStack::MakeTree(Int_t event, const char * /*file*/)
837 // Make Kine tree and creates branch for writing particles
840 // Make Kinematics Tree
843 sprintf(hname,"TreeK%d",event);
844 fTreeK = new TTree(hname,"Kinematics");
845 // Create a branch for particles
846 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
847 fTreeK->Write(0,TObject::kOverwrite);
851 //_____________________________________________________________________________
852 void AliStack::BeginEvent(Int_t event)
863 sprintf(hname,"TreeK%d",event);
864 fTreeK->SetName(hname);
868 //_____________________________________________________________________________
869 void AliStack::FinishRun()
871 // Clean TreeK information
873 delete fTreeK; fTreeK = 0;
877 //_____________________________________________________________________________
878 Bool_t AliStack::GetEvent(Int_t event)
881 // Get new event from TreeK
883 // Reset/Create the particle stack
884 if (fTreeK) delete fTreeK;
886 // Get Kine Tree from file
888 sprintf(treeName,"TreeK%d",event);
889 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
892 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
894 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
895 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
898 // printf("\n primaries %d", fNprimary);
899 // printf("\n tracks %d", fNtrack);
901 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());