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.26 2002/10/29 14:26:49 hristov
19 Code clean-up (F.Carminati)
21 Revision 1.25 2002/10/22 15:21:39 hristov
22 New methods for more effective access of particles from TreeK (J.Chudoba)
24 Revision 1.24 2002/10/22 15:02:15 alibrary
25 Introducing Riostream.h
27 Revision 1.23 2002/10/14 14:57:32 hristov
28 Merging the VirtualMC branch to the main development branch (HEAD)
30 Revision 1.19.4.2 2002/08/28 15:06:52 alibrary
33 Revision 1.19.4.1 2002/06/10 14:43:06 hristov
36 Revision 1.21 2002/05/28 14:24:57 hristov
37 Correct warning messages
39 Revision 1.20 2002/04/30 11:47:30 morsch
40 KeepPhysics method called by PurifyKine added (N. Carrer, A.M.)
42 Revision 1.19 2002/03/12 11:06:03 morsch
43 Add particle status code to argument list of SetTrack(..).
45 Revision 1.18 2002/02/20 16:14:41 hristov
46 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
48 Revision 1.17 2001/12/05 08:51:56 hristov
49 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
51 Revision 1.16 2001/11/20 09:27:55 hristov
52 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
54 Revision 1.15 2001/09/04 15:10:37 hristov
55 Additional protection is included to avoid some problems using Hijing
57 Revision 1.14 2001/08/30 09:44:06 hristov
58 VertexSource_t added to avoid the warnings
60 Revision 1.13 2001/08/29 13:31:42 morsch
61 Protection against (fTreeK == 0) in destructor.
63 Revision 1.12 2001/07/27 13:03:13 hristov
64 Default Branch split level set to 99
66 Revision 1.11 2001/07/27 12:34:20 jchudoba
67 remove the dummy argument in GetEvent method
69 Revision 1.10 2001/07/20 10:13:54 morsch
70 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
72 Revision 1.9 2001/07/03 08:10:57 hristov
73 J.Chudoba's changes merged correctly with the HEAD
75 Revision 1.6 2001/05/31 06:59:06 fca
76 Clean setting and deleting of fParticleBuffer
78 Revision 1.5 2001/05/30 12:18:46 hristov
79 Loop variables declared once
81 Revision 1.4 2001/05/25 07:25:20 hristov
82 AliStack destructor corrected (I.Hrivnacova)
84 Revision 1.3 2001/05/22 14:33:16 hristov
87 Revision 1.2 2001/05/17 05:49:39 fca
88 Reset pointers to daughters
90 Revision 1.1 2001/05/16 14:57:22 alibrary
91 New files for folders and Stack
95 ///////////////////////////////////////////////////////////////////////////////
97 // Particles stack class
99 ///////////////////////////////////////////////////////////////////////////////
101 #include <Riostream.h>
103 #include <TObjArray.h>
104 #include <TParticle.h>
110 #include "AliStack.h"
112 #include "AliModule.h"
117 //_______________________________________________________________________
118 AliStack::AliStack():
132 // Default constructor
136 //_______________________________________________________________________
137 AliStack::AliStack(Int_t size):
138 fParticles(new TClonesArray("TParticle",1000)),
139 fParticleMap(new TObjArray(size)),
155 //_______________________________________________________________________
156 AliStack::AliStack(const AliStack& st):
176 //_______________________________________________________________________
177 void AliStack::Copy(AliStack&) const
179 Fatal("Copy","Not implemented!\n");
182 //_______________________________________________________________________
183 AliStack::~AliStack()
190 fParticles->Delete();
201 //_____________________________________________________________________________
202 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
203 Float_t *vpos, Float_t *polar, Float_t tof,
204 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
207 // Load a track on the stack
209 // done 0 if the track has to be transported
211 // parent identifier of the parent track. -1 for a primary
213 // pmom momentum GeV/c
215 // polar polarisation
216 // tof time of flight in seconds
217 // mecha production mechanism
218 // ntr on output the number of the track stored
221 // const Float_t tlife=0;
224 // Here we get the static mass
225 // For MC is ok, but a more sophisticated method could be necessary
226 // if the calculated mass is required
227 // also, this method is potentially dangerous if the mass
228 // used in the MC is not the same of the PDG database
230 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
231 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
232 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
234 // 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",
235 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
238 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
239 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
240 mech, ntr, weight, is);
243 //_____________________________________________________________________________
244 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
245 Double_t px, Double_t py, Double_t pz, Double_t e,
246 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
247 Double_t polx, Double_t poly, Double_t polz,
248 TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
251 // Load a track on the stack
253 // done 0 if the track has to be transported
255 // parent identifier of the parent track. -1 for a primary
257 // kS generation status code
258 // px, py, pz momentum GeV/c
259 // vx, vy, vz position
260 // polar polarisation
261 // tof time of flight in seconds
262 // mech production mechanism
263 // ntr on output the number of the track stored
265 // New method interface:
266 // arguments were changed to be in correspondence with TParticle
268 // Note: the energy is not calculated from the static mass but
269 // it is passed by argument e.
272 const Int_t kFirstDaughter=-1;
273 const Int_t kLastDaughter=-1;
275 TClonesArray &particles = *fParticles;
277 = new(particles[fLoadPoint++])
278 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
279 px, py, pz, e, vx, vy, vz, tof);
281 particle->SetPolarisation(polx, poly, polz);
282 particle->SetWeight(weight);
283 particle->SetUniqueID(mech);
285 if(!done) particle->SetBit(kDoneBit);
287 // Declare that the daughter information is valid
288 particle->SetBit(kDaughtersBit);
289 // Add the particle to the stack
290 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
293 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
295 particle->SetLastDaughter(fNtrack);
296 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
299 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
304 // This is a primary track. Set high water mark for this event
307 // Set also number if primary tracks
308 fNprimary = fHgwmk+1;
314 //_____________________________________________________________________________
315 TParticle* AliStack::GetNextTrack(Int_t& itrack)
318 // Returns next track from stack of particles
322 TParticle* track = GetNextParticle();
326 track->SetBit(kDoneBit);
335 //_____________________________________________________________________________
336 void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
337 Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
338 Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
339 Double_t& polx, Double_t& poly, Double_t& polz)
342 // Return next track from stack of particles
346 TParticle* track = GetNextParticle();
347 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
351 pdg = track->GetPdgCode();
361 track->GetPolarisation(pol);
365 track->SetBit(kDoneBit);
366 // cout << "Filled params" << endl;
372 // stop and start timer when we start a primary track
373 Int_t nprimaries = fNprimary;
374 if (fCurrent >= nprimaries) return;
375 if (fCurrent < nprimaries-1) {
377 track=(TParticle*) fParticleMap->At(fCurrent+1);
378 // track->SetProcessTime(fTimer.CpuTime());
384 //_____________________________________________________________________________
385 TParticle* AliStack::GetPrimaryForTracking(Int_t i)
388 // Returns i-th primary particle if it is flagged to be tracked,
392 TParticle* particle = Particle(i);
394 if (!particle->TestBit(kDoneBit))
401 //_____________________________________________________________________________
402 void AliStack::PurifyKine()
405 // Compress kinematic tree keeping only flagged particles
406 // and renaming the particle id's in all the hits
409 TObjArray &particles = *fParticleMap;
410 int nkeep=fHgwmk+1, parent, i;
411 TParticle *part, *father;
412 TArrayI map(particles.GetLast()+1);
414 // Save in Header total number of tracks before compression
416 // If no tracks generated return now
417 if(fHgwmk+1 == fNtrack) return;
419 // First pass, invalid Daughter information
420 for(i=0; i<fNtrack; i++) {
421 // Preset map, to be removed later
422 if(i<=fHgwmk) map[i]=i ;
425 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
427 // Check of this track should be kept for physics reasons
428 if (KeepPhysics(part)) KeepTrack(i);
430 part->ResetBit(kDaughtersBit);
431 part->SetFirstDaughter(-1);
432 part->SetLastDaughter(-1);
436 // Invalid daughter information for the parent of the first particle
437 // generated. This may or may not be the current primary according to
438 // whether decays have been recorded among the primaries
439 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
440 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
441 // Second pass, build map between old and new numbering
442 for(i=fHgwmk+1; i<fNtrack; i++) {
443 if(particles.At(i)->TestBit(kKeepBit)) {
445 // This particle has to be kept
447 // If old and new are different, have to move the pointer
448 if(i!=nkeep) particles[nkeep]=particles.At(i);
449 part = dynamic_cast<TParticle*>(particles.At(nkeep));
451 // as the parent is always *before*, it must be already
452 // in place. This is what we are checking anyway!
453 if((parent=part->GetFirstMother())>fHgwmk)
454 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
455 else part->SetFirstMother(map[parent]);
461 // Fix daughters information
462 for (i=fHgwmk+1; i<nkeep; i++) {
463 part = dynamic_cast<TParticle*>(particles.At(i));
464 parent = part->GetFirstMother();
466 father = dynamic_cast<TParticle*>(particles.At(parent));
467 if(father->TestBit(kDaughtersBit)) {
469 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
470 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
472 // Initialise daughters info for first pass
473 father->SetFirstDaughter(i);
474 father->SetLastDaughter(i);
475 father->SetBit(kDaughtersBit);
480 // Now loop on all registered hit lists
481 TList* hitLists = gAlice->GetHitLists();
482 TIter next(hitLists);
483 TCollection *hitList;
484 while((hitList = dynamic_cast<TCollection*>(next()))) {
485 TIter nexthit(hitList);
487 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
488 hit->SetTrack(map[hit->GetTrack()]);
493 // This for detectors which have a special mapping mechanism
494 // for hits, such as TPC and TRD
497 TObjArray* modules = gAlice->Modules();
498 TIter nextmod(modules);
500 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
501 detector->RemapTrackHitIDs(map.GetArray());
502 detector->RemapTrackReferencesIDs(map.GetArray());
505 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
506 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
508 for (i=fHgwmk+1; i<nkeep; ++i) {
509 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
510 fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
512 particles[i]=fParticleBuffer=0;
515 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
517 Int_t toshrink = fNtrack-fHgwmk-1;
518 fLoadPoint-=toshrink;
519 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
526 Bool_t AliStack::KeepPhysics(TParticle* part)
529 // Some particles have to kept on the stack for reasons motivated
530 // by physics analysis. Decision is put here.
532 Bool_t keep = kFALSE;
534 // Keep first-generation daughter from primaries with heavy flavor
536 Int_t parent = part->GetFirstMother();
537 if (parent >= 0 && parent <= fHgwmk) {
538 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
539 Int_t kf = father->GetPdgCode();
543 if (kfl > 10) kfl/=100;
545 if (kfl > 10) kfl/=10;
546 if (kfl > 10) kfl/=10;
554 //_____________________________________________________________________________
555 void AliStack::FinishEvent()
558 // Write out the kinematics that was not yet filled
561 // Update event header
565 // Fatal("FinishEvent", "No kinematics tree is defined.");
566 // Don't panic this is a probably a lego run
572 if(fTreeK->GetEntries() ==0) {
573 // set the fParticleFileMap size for the first time
574 fParticleFileMap.Set(fHgwmk+1);
577 Bool_t allFilled = kFALSE;
579 for(Int_t i=0; i<fHgwmk+1; ++i)
580 if((part=fParticleMap->At(i))) {
581 fParticleBuffer = dynamic_cast<TParticle*>(part);
582 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
584 //PH (*fParticleMap)[i]=fParticleBuffer=0;
586 fParticleMap->AddAt(0,i);
588 // When all primaries were filled no particle!=0
589 // should be left => to be removed later.
590 if (allFilled) printf("Why != 0 part # %d?\n",i);
593 // // printf("Why = 0 part # %d?\n",i); => We know.
595 // we don't break now in order to be sure there is no
596 // particle !=0 left.
597 // To be removed later and replaced with break.
598 if(!allFilled) allFilled = kTRUE;
600 // cout << "Nof particles: " << fNtrack << endl;
604 //_____________________________________________________________________________
605 void AliStack::FlagTrack(Int_t track)
608 // Flags a track and all its family tree to be kept
615 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
617 // If the particle is flagged the three from here upward is saved already
618 if(particle->TestBit(kKeepBit)) return;
620 // Save this particle
621 particle->SetBit(kKeepBit);
623 // Move to father if any
624 if((curr=particle->GetFirstMother())==-1) return;
628 //_____________________________________________________________________________
629 void AliStack::KeepTrack(Int_t track)
632 // Flags a track to be kept
635 fParticleMap->At(track)->SetBit(kKeepBit);
638 //_____________________________________________________________________________
639 void AliStack::Reset(Int_t size)
653 //_____________________________________________________________________________
654 void AliStack::ResetArrays(Int_t size)
657 // Resets stack arrays
663 fParticles = new TClonesArray("TParticle",1000);
665 fParticleMap->Clear();
666 if (size>0) fParticleMap->Expand(size);}
668 fParticleMap = new TObjArray(size);
671 //_____________________________________________________________________________
672 void AliStack::SetHighWaterMark(Int_t)
675 // Set high water mark for last track in event
679 fCurrentPrimary=fHgwmk;
681 // Set also number of primary tracks
682 fNprimary = fHgwmk+1;
686 //_____________________________________________________________________________
687 TParticle* AliStack::Particle(Int_t i)
690 // Return particle with specified ID
692 //PH if(!(*fParticleMap)[i]) {
693 if(!fParticleMap->At(i)) {
694 Int_t nentries = fParticles->GetEntriesFast();
695 // algorithmic way of getting entry index
696 // (primary particles are filled after secondaries)
697 Int_t entry = TreeKEntry(i);
698 // check whether algorithmic way and
699 // and the fParticleFileMap[i] give the same;
700 // give the fatal error if not
701 if (entry != fParticleFileMap[i]) {
703 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
704 entry, fParticleFileMap[i]);
707 fTreeK->GetEntry(entry);
708 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
709 fParticleMap->AddAt((*fParticles)[nentries],i);
711 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
712 return dynamic_cast<TParticle*>(fParticleMap->At(i));
715 //_____________________________________________________________________________
716 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
719 // return pointer to TParticle with label id
722 if ((entry = TreeKEntry(id)) < 0) return 0;
723 if (fTreeK->GetEntry(entry)<=0) return 0;
724 return fParticleBuffer;
727 //_____________________________________________________________________________
728 Int_t AliStack::TreeKEntry(Int_t id) const
731 // return entry number in the TreeK for particle with label id
732 // return negative number if label>fNtrack
736 entry = id+fNtrack-fNprimary;
738 entry = id-fNprimary;
742 //_____________________________________________________________________________
743 Int_t AliStack::GetPrimary(Int_t id)
746 // Return number of primary that has generated track
754 parent=Particle(current)->GetFirstMother();
755 if(parent<0) return current;
759 //_____________________________________________________________________________
760 void AliStack::DumpPart (Int_t i) const
763 // Dumps particle i in the stack
766 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
767 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
770 //_____________________________________________________________________________
771 void AliStack::DumpPStack ()
774 // Dumps the particle stack
780 "\n\n=======================================================================\n");
781 for (i=0;i<fNtrack;i++)
783 TParticle* particle = Particle(i);
785 printf("-> %d ",i); particle->Print();
786 printf("--------------------------------------------------------------\n");
789 Warning("DumpPStack", "No particle with id %d.", i);
793 "\n=======================================================================\n\n");
795 // print particle file map
796 printf("\nParticle file map: \n");
797 for (i=0; i<fNtrack; i++)
798 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
802 //_____________________________________________________________________________
803 void AliStack::DumpLoadedStack() const
806 // Dumps the particle in the stack
807 // that are loaded in memory.
810 TObjArray &particles = *fParticleMap;
812 "\n\n=======================================================================\n");
813 for (Int_t i=0;i<fNtrack;i++)
815 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
817 printf("-> %d ",i); particle->Print();
818 printf("--------------------------------------------------------------\n");
821 printf("-> %d Particle not loaded.\n",i);
822 printf("--------------------------------------------------------------\n");
826 "\n=======================================================================\n\n");
833 //_____________________________________________________________________________
834 void AliStack::CleanParents()
837 // Clean particles stack
838 // Set parent/daughter relations
841 TObjArray &particles = *fParticleMap;
844 for(i=0; i<fHgwmk+1; i++) {
845 part = dynamic_cast<TParticle*>(particles.At(i));
846 if(part) if(!part->TestBit(kDaughtersBit)) {
847 part->SetFirstDaughter(-1);
848 part->SetLastDaughter(-1);
853 //_____________________________________________________________________________
854 TParticle* AliStack::GetNextParticle()
857 // Return next particle from stack of particles
860 TParticle* particle = 0;
862 // search secondaries
863 //for(Int_t i=fNtrack-1; i>=0; i--) {
864 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
865 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
866 if ((particle) && (!particle->TestBit(kDoneBit))) {
868 //cout << "GetNextParticle() - secondary "
869 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
874 // take next primary if all secondaries were done
875 while (fCurrentPrimary>=0) {
876 fCurrent = fCurrentPrimary;
877 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
878 if ((particle) && (!particle->TestBit(kDoneBit))) {
879 //cout << "GetNextParticle() - primary "
880 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
885 // nothing to be tracked
887 //cout << "GetNextParticle() - none "
888 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
892 //__________________________________________________________________________________________
893 void AliStack::MakeTree(Int_t event, const char * /*file*/)
896 // Make Kine tree and creates branch for writing particles
899 // Make Kinematics Tree
902 sprintf(hname,"TreeK%d",event);
903 fTreeK = new TTree(hname,"Kinematics");
904 // Create a branch for particles
905 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
906 fTreeK->Write(0,TObject::kOverwrite);
910 //_____________________________________________________________________________
911 void AliStack::BeginEvent(Int_t event)
922 sprintf(hname,"TreeK%d",event);
923 fTreeK->SetName(hname);
927 //_____________________________________________________________________________
928 void AliStack::FinishRun()
930 // Clean TreeK information
932 delete fTreeK; fTreeK = 0;
936 Bool_t AliStack::GetEvent(Int_t event)
939 // Get new event from TreeK
941 // Reset/Create the particle stack
942 if (fTreeK) delete fTreeK;
944 // Get Kine Tree from file
946 sprintf(treeName,"TreeK%d",event);
947 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
950 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
952 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
953 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
956 // printf("\n primaries %d", fNprimary);
957 // printf("\n tracks %d", fNtrack);
959 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());