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 ///////////////////////////////////////////////////////////////////////////////
27 #include <TObjArray.h>
28 #include <TParticle.h>
29 #include <TParticlePDG.h>
33 #include "AliModule.h"
35 #include "AliRunLoader.h"
40 //_______________________________________________________________________
53 fEventFolderName(AliConfig::fgkDefaultEventFolderName)
56 // Default constructor
60 //_______________________________________________________________________
61 AliStack::AliStack(Int_t size, const char* evfoldname):
62 fParticles(new TClonesArray("TParticle",1000)),
63 fParticleMap(new TObjArray(size)),
73 fEventFolderName(evfoldname)
80 //_______________________________________________________________________
81 AliStack::AliStack(const AliStack& st):
101 //_______________________________________________________________________
102 void AliStack::Copy(AliStack&) const
104 Fatal("Copy","Not implemented!\n");
107 //_______________________________________________________________________
108 AliStack::~AliStack()
115 fParticles->Delete();
119 //PH??? delete fTreeK;
126 //_____________________________________________________________________________
127 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
128 Float_t *vpos, Float_t *polar, Float_t tof,
129 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
132 // Load a track on the stack
134 // done 0 if the track has to be transported
136 // parent identifier of the parent track. -1 for a primary
138 // pmom momentum GeV/c
140 // polar polarisation
141 // tof time of flight in seconds
142 // mecha production mechanism
143 // ntr on output the number of the track stored
146 // const Float_t tlife=0;
149 // Here we get the static mass
150 // For MC is ok, but a more sophisticated method could be necessary
151 // if the calculated mass is required
152 // also, this method is potentially dangerous if the mass
153 // used in the MC is not the same of the PDG database
155 TParticlePDG* pmc = TDatabasePDG::Instance()->GetParticle(pdg);
157 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
158 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
159 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
161 // 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",
162 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
165 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
166 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
167 mech, ntr, weight, is);
169 Warning("SetTrack", "Particle type %d not defined in PDG Database !\n", pdg);
170 Warning("SetTrack", "Particle skipped !\n");
174 //_____________________________________________________________________________
175 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
176 Double_t px, Double_t py, Double_t pz, Double_t e,
177 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
178 Double_t polx, Double_t poly, Double_t polz,
179 TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
182 // Load a track on the stack
184 // done 0 if the track has to be transported
186 // parent identifier of the parent track. -1 for a primary
188 // kS generation status code
189 // px, py, pz momentum GeV/c
190 // vx, vy, vz position
191 // polar polarisation
192 // tof time of flight in seconds
193 // mech production mechanism
194 // ntr on output the number of the track stored
196 // New method interface:
197 // arguments were changed to be in correspondence with TParticle
199 // Note: the energy is not calculated from the static mass but
200 // it is passed by argument e.
203 const Int_t kFirstDaughter=-1;
204 const Int_t kLastDaughter=-1;
206 TClonesArray &particles = *fParticles;
208 = new(particles[fLoadPoint++])
209 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
210 px, py, pz, e, vx, vy, vz, tof);
212 particle->SetPolarisation(polx, poly, polz);
213 particle->SetWeight(weight);
214 particle->SetUniqueID(mech);
216 if(!done) particle->SetBit(kDoneBit);
218 // Declare that the daughter information is valid
219 particle->SetBit(kDaughtersBit);
220 // Add the particle to the stack
221 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
224 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
226 particle->SetLastDaughter(fNtrack);
227 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
230 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
235 // This is a primary track. Set high water mark for this event
238 // Set also number if primary tracks
239 fNprimary = fHgwmk+1;
245 //_____________________________________________________________________________
246 TParticle* AliStack::GetNextTrack(Int_t& itrack)
249 // Returns next track from stack of particles
253 TParticle* track = GetNextParticle();
257 track->SetBit(kDoneBit);
262 fCurrentTrack = track;
266 //_____________________________________________________________________________
267 TParticle* AliStack::GetPrimaryForTracking(Int_t i)
270 // Returns i-th primary particle if it is flagged to be tracked,
274 TParticle* particle = Particle(i);
276 if (!particle->TestBit(kDoneBit))
282 //_____________________________________________________________________________
283 void AliStack::PurifyKine()
286 // Compress kinematic tree keeping only flagged particles
287 // and renaming the particle id's in all the hits
290 TObjArray &particles = *fParticleMap;
291 int nkeep=fHgwmk+1, parent, i;
292 TParticle *part, *father;
293 TArrayI map(particles.GetLast()+1);
295 // Save in Header total number of tracks before compression
297 // If no tracks generated return now
298 if(fHgwmk+1 == fNtrack) return;
300 // First pass, invalid Daughter information
301 for(i=0; i<fNtrack; i++) {
302 // Preset map, to be removed later
303 if(i<=fHgwmk) map[i]=i ;
306 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
308 // Check of this track should be kept for physics reasons
309 if (KeepPhysics(part)) KeepTrack(i);
311 part->ResetBit(kDaughtersBit);
312 part->SetFirstDaughter(-1);
313 part->SetLastDaughter(-1);
317 // Invalid daughter information for the parent of the first particle
318 // generated. This may or may not be the current primary according to
319 // whether decays have been recorded among the primaries
320 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
321 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
322 // Second pass, build map between old and new numbering
323 for(i=fHgwmk+1; i<fNtrack; i++) {
324 if(particles.At(i)->TestBit(kKeepBit)) {
326 // This particle has to be kept
328 // If old and new are different, have to move the pointer
329 if(i!=nkeep) particles[nkeep]=particles.At(i);
330 part = dynamic_cast<TParticle*>(particles.At(nkeep));
332 // as the parent is always *before*, it must be already
333 // in place. This is what we are checking anyway!
334 if((parent=part->GetFirstMother())>fHgwmk)
335 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
336 else part->SetFirstMother(map[parent]);
342 // Fix daughters information
343 for (i=fHgwmk+1; i<nkeep; i++) {
344 part = dynamic_cast<TParticle*>(particles.At(i));
345 parent = part->GetFirstMother();
347 father = dynamic_cast<TParticle*>(particles.At(parent));
348 if(father->TestBit(kDaughtersBit)) {
350 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
351 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
353 // Initialise daughters info for first pass
354 father->SetFirstDaughter(i);
355 father->SetLastDaughter(i);
356 father->SetBit(kDaughtersBit);
361 // Now loop on all registered hit lists
362 TList* hitLists = gAlice->GetHitLists();
363 TIter next(hitLists);
364 TCollection *hitList;
365 while((hitList = dynamic_cast<TCollection*>(next()))) {
366 TIter nexthit(hitList);
368 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
369 hit->SetTrack(map[hit->GetTrack()]);
374 // This for detectors which have a special mapping mechanism
375 // for hits, such as TPC and TRD
378 TObjArray* modules = gAlice->Modules();
379 TIter nextmod(modules);
381 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
382 detector->RemapTrackHitIDs(map.GetArray());
383 detector->RemapTrackReferencesIDs(map.GetArray());
386 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
387 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
389 for (i=fHgwmk+1; i<nkeep; ++i) {
390 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
391 fParticleFileMap[i]=static_cast<Int_t>(TreeK()->GetEntries());
393 particles[i]=fParticleBuffer=0;
396 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
398 Int_t toshrink = fNtrack-fHgwmk-1;
399 fLoadPoint-=toshrink;
400 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
407 Bool_t AliStack::KeepPhysics(TParticle* part)
410 // Some particles have to kept on the stack for reasons motivated
411 // by physics analysis. Decision is put here.
413 Bool_t keep = kFALSE;
415 // Keep first-generation daughter from primaries with heavy flavor
417 Int_t parent = part->GetFirstMother();
418 if (parent >= 0 && parent <= fHgwmk) {
419 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
420 Int_t kf = father->GetPdgCode();
424 if (kfl > 10) kfl/=100;
426 if (kfl > 10) kfl/=10;
427 if (kfl > 10) kfl/=10;
435 //_____________________________________________________________________________
436 void AliStack::FinishEvent()
439 // Write out the kinematics that was not yet filled
442 // Update event header
446 // Fatal("FinishEvent", "No kinematics tree is defined.");
447 // Don't panic this is a probably a lego run
452 if(TreeK()->GetEntries() ==0) {
453 // set the fParticleFileMap size for the first time
454 fParticleFileMap.Set(fHgwmk+1);
457 Bool_t allFilled = kFALSE;
459 for(Int_t i=0; i<fHgwmk+1; ++i)
460 if((part=fParticleMap->At(i))) {
461 fParticleBuffer = dynamic_cast<TParticle*>(part);
462 fParticleFileMap[i]= static_cast<Int_t>(TreeK()->GetEntries());
464 //PH (*fParticleMap)[i]=fParticleBuffer=0;
466 fParticleMap->AddAt(0,i);
468 // When all primaries were filled no particle!=0
469 // should be left => to be removed later.
470 if (allFilled) printf("Why != 0 part # %d?\n",i);
474 // // printf("Why = 0 part # %d?\n",i); => We know.
476 // we don't break now in order to be sure there is no
477 // particle !=0 left.
478 // To be removed later and replaced with break.
479 if(!allFilled) allFilled = kTRUE;
482 //_____________________________________________________________________________
484 void AliStack::FlagTrack(Int_t track)
487 // Flags a track and all its family tree to be kept
494 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
496 // If the particle is flagged the three from here upward is saved already
497 if(particle->TestBit(kKeepBit)) return;
499 // Save this particle
500 particle->SetBit(kKeepBit);
502 // Move to father if any
503 if((curr=particle->GetFirstMother())==-1) return;
507 //_____________________________________________________________________________
508 void AliStack::KeepTrack(Int_t track)
511 // Flags a track to be kept
514 fParticleMap->At(track)->SetBit(kKeepBit);
517 //_____________________________________________________________________________
518 void AliStack::Reset(Int_t size)
533 //_____________________________________________________________________________
534 void AliStack::ResetArrays(Int_t size)
537 // Resets stack arrays
543 fParticles = new TClonesArray("TParticle",1000);
545 fParticleMap->Clear();
546 if (size>0) fParticleMap->Expand(size);}
548 fParticleMap = new TObjArray(size);
551 //_____________________________________________________________________________
552 void AliStack::SetHighWaterMark(Int_t)
555 // Set high water mark for last track in event
559 fCurrentPrimary=fHgwmk;
561 // Set also number of primary tracks
562 fNprimary = fHgwmk+1;
566 //_____________________________________________________________________________
567 TParticle* AliStack::Particle(Int_t i)
570 // Return particle with specified ID
572 //PH if(!(*fParticleMap)[i]) {
573 if(!fParticleMap->At(i)) {
574 Int_t nentries = fParticles->GetEntriesFast();
575 // algorithmic way of getting entry index
576 // (primary particles are filled after secondaries)
577 Int_t entry = TreeKEntry(i);
578 // check whether algorithmic way and
579 // and the fParticleFileMap[i] give the same;
580 // give the fatal error if not
581 if (entry != fParticleFileMap[i]) {
583 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
584 entry, fParticleFileMap[i]);
587 TreeK()->GetEntry(entry);
588 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
589 fParticleMap->AddAt((*fParticles)[nentries],i);
591 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
592 return dynamic_cast<TParticle*>(fParticleMap->At(i));
595 //_____________________________________________________________________________
596 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
599 // return pointer to TParticle with label id
602 if ((entry = TreeKEntry(id)) < 0) return 0;
603 if (fTreeK->GetEntry(entry)<=0) return 0;
604 return fParticleBuffer;
607 //_____________________________________________________________________________
608 Int_t AliStack::TreeKEntry(Int_t id) const
611 // return entry number in the TreeK for particle with label id
612 // return negative number if label>fNtrack
616 entry = id+fNtrack-fNprimary;
618 entry = id-fNprimary;
622 //_____________________________________________________________________________
623 Int_t AliStack::CurrentTrackParent() const
626 // Return number of the parent of the current track
629 TParticle* current = (TParticle*)fParticleMap->At(fCurrent);
632 return current->GetFirstMother();
634 Warning("CurrentTrackParent", "Current track not found in the stack");
639 //_____________________________________________________________________________
640 Int_t AliStack::GetPrimary(Int_t id)
643 // Return number of primary that has generated track
651 parent=Particle(current)->GetFirstMother();
652 if(parent<0) return current;
656 //_____________________________________________________________________________
657 void AliStack::DumpPart (Int_t i) const
660 // Dumps particle i in the stack
663 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
664 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
667 //_____________________________________________________________________________
668 void AliStack::DumpPStack ()
671 // Dumps the particle stack
676 printf("\n\n=======================================================================\n");
677 for (i=0;i<fNtrack;i++)
679 TParticle* particle = Particle(i);
681 printf("-> %d ",i); particle->Print();
682 printf("--------------------------------------------------------------\n");
685 Warning("DumpPStack", "No particle with id %d.", i);
688 printf("\n=======================================================================\n\n");
690 // print particle file map
691 printf("\nParticle file map: \n");
692 for (i=0; i<fNtrack; i++)
693 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
697 //_____________________________________________________________________________
698 void AliStack::DumpLoadedStack() const
701 // Dumps the particle in the stack
702 // that are loaded in memory.
705 TObjArray &particles = *fParticleMap;
707 "\n\n=======================================================================\n");
708 for (Int_t i=0;i<fNtrack;i++)
710 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
712 printf("-> %d ",i); particle->Print();
713 printf("--------------------------------------------------------------\n");
716 printf("-> %d Particle not loaded.\n",i);
717 printf("--------------------------------------------------------------\n");
721 "\n=======================================================================\n\n");
728 //_____________________________________________________________________________
729 void AliStack::CleanParents()
732 // Clean particles stack
733 // Set parent/daughter relations
736 TObjArray &particles = *fParticleMap;
739 for(i=0; i<fHgwmk+1; i++) {
740 part = dynamic_cast<TParticle*>(particles.At(i));
741 if(part) if(!part->TestBit(kDaughtersBit)) {
742 part->SetFirstDaughter(-1);
743 part->SetLastDaughter(-1);
748 //_____________________________________________________________________________
749 TParticle* AliStack::GetNextParticle()
752 // Return next particle from stack of particles
755 TParticle* particle = 0;
757 // search secondaries
758 //for(Int_t i=fNtrack-1; i>=0; i--) {
759 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
760 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
761 if ((particle) && (!particle->TestBit(kDoneBit))) {
767 // take next primary if all secondaries were done
768 while (fCurrentPrimary>=0) {
769 fCurrent = fCurrentPrimary;
770 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
771 if ((particle) && (!particle->TestBit(kDoneBit))) {
776 // nothing to be tracked
780 //__________________________________________________________________________________________
782 TTree* AliStack::TreeK()
791 AliRunLoader *rl = AliRunLoader::GetRunLoader(fEventFolderName);
794 Fatal("TreeK","Can not get RunLoader from event folder named %s",fEventFolderName.Data());
795 return 0x0;//pro forma
797 fTreeK = rl->TreeK();
800 Error("TreeK","Can not get TreeK from RL. Ev. Folder is %s",fEventFolderName.Data());
807 return fTreeK;//never reached
809 //__________________________________________________________________________________________
811 void AliStack::ConnectTree()
814 // Creates branch for writing particles
816 if (AliLoader::fgDebug) Info("ConnectTree","Connecting TreeK");
821 Fatal("ConnectTree","Parameter is NULL");//we don't like such a jokes
824 return;//in this case TreeK() calls back this method (ConnectTree)
825 //tree after setting fTreeK, the rest was already executed
826 //it is safe to return now
829 // Create a branch for particles
831 if (AliLoader::fgDebug)
832 Info("ConnectTree","Tree name is %s",fTreeK->GetName());
834 if (fTreeK->GetDirectory())
836 if (AliLoader::fgDebug)
837 Info("ConnectTree","and dir is %s",fTreeK->GetDirectory()->GetName());
840 Warning("ConnectTree","DIR IS NOT SET !!!");
842 TBranch *branch=fTreeK->GetBranch(AliRunLoader::fgkKineBranchName);
845 branch = fTreeK->Branch(AliRunLoader::fgkKineBranchName, "TParticle", &fParticleBuffer, 4000);
846 if (AliLoader::fgDebug) Info("ConnectTree","Creating Branch in Tree");
850 if (AliLoader::fgDebug) Info("ConnectTree","Branch Found in Tree");
851 branch->SetAddress(&fParticleBuffer);
853 if (branch->GetDirectory())
855 if (AliLoader::fgDebug)
856 Info("ConnectTree","Branch Dir Name is %s",branch->GetDirectory()->GetName());
859 Warning("ConnectTree","Branch Dir is NOT SET");
861 //__________________________________________________________________________________________
864 void AliStack::BeginEvent()
870 //_____________________________________________________________________________
871 void AliStack::FinishRun()
873 // Clean TreeK information
875 //_____________________________________________________________________________
877 Bool_t AliStack::GetEvent()
880 // Get new event from TreeK
882 // Reset/Create the particle stack
885 if (TreeK() == 0x0) //forces connecting
887 Error("GetEvent","cannot find Kine Tree for current event\n");
891 Int_t size = (Int_t)TreeK()->GetEntries();
895 //_____________________________________________________________________________
897 void AliStack::SetEventFolderName(const char* foldname)
899 //Sets event folder name
900 fEventFolderName = foldname;