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.19 2002/03/12 11:06:03 morsch
19 Add particle status code to argument list of SetTrack(..).
21 Revision 1.18 2002/02/20 16:14:41 hristov
22 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
24 Revision 1.17 2001/12/05 08:51:56 hristov
25 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
27 Revision 1.16 2001/11/20 09:27:55 hristov
28 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
30 Revision 1.15 2001/09/04 15:10:37 hristov
31 Additional protection is included to avoid some problems using Hijing
33 Revision 1.14 2001/08/30 09:44:06 hristov
34 VertexSource_t added to avoid the warnings
36 Revision 1.13 2001/08/29 13:31:42 morsch
37 Protection against (fTreeK == 0) in destructor.
39 Revision 1.12 2001/07/27 13:03:13 hristov
40 Default Branch split level set to 99
42 Revision 1.11 2001/07/27 12:34:20 jchudoba
43 remove the dummy argument in GetEvent method
45 Revision 1.10 2001/07/20 10:13:54 morsch
46 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
48 Revision 1.9 2001/07/03 08:10:57 hristov
49 J.Chudoba's changes merged correctly with the HEAD
51 Revision 1.6 2001/05/31 06:59:06 fca
52 Clean setting and deleting of fParticleBuffer
54 Revision 1.5 2001/05/30 12:18:46 hristov
55 Loop variables declared once
57 Revision 1.4 2001/05/25 07:25:20 hristov
58 AliStack destructor corrected (I.Hrivnacova)
60 Revision 1.3 2001/05/22 14:33:16 hristov
63 Revision 1.2 2001/05/17 05:49:39 fca
64 Reset pointers to daughters
66 Revision 1.1 2001/05/16 14:57:22 alibrary
67 New files for folders and Stack
71 ///////////////////////////////////////////////////////////////////////////////
73 // Particles stack class
75 ///////////////////////////////////////////////////////////////////////////////
79 #include <TObjArray.h>
80 #include <TParticle.h>
88 #include "AliModule.h"
93 //_____________________________________________________________________________
94 AliStack::AliStack(Int_t size)
100 // Create the particles arrays
101 fParticles = new TClonesArray("TParticle",1000);
102 fParticleMap = new TObjArray(size);
107 fCurrentPrimary = -1;
112 //_____________________________________________________________________________
116 // Default constructor
119 // Create the particles arrays
126 fCurrentPrimary = -1;
131 //_____________________________________________________________________________
132 AliStack::~AliStack()
139 fParticles->Delete();
143 if (fTreeK) delete fTreeK;
150 //_____________________________________________________________________________
151 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
152 Float_t *vpos, Float_t *polar, Float_t tof,
153 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
156 // Load a track on the stack
158 // done 0 if the track has to be transported
160 // parent identifier of the parent track. -1 for a primary
162 // pmom momentum GeV/c
164 // polar polarisation
165 // tof time of flight in seconds
166 // mecha production mechanism
167 // ntr on output the number of the track stored
171 const Int_t kfirstdaughter=-1;
172 const Int_t klastdaughter=-1;
173 // const Float_t tlife=0;
176 // Here we get the static mass
177 // For MC is ok, but a more sophisticated method could be necessary
178 // if the calculated mass is required
179 // also, this method is potentially dangerous if the mass
180 // used in the MC is not the same of the PDG database
182 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
183 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
184 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
186 // 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",
187 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
189 TClonesArray &particles = *fParticles;
191 = new(particles[fLoadPoint++])
192 TParticle(pdg, is, parent, -1, kfirstdaughter, klastdaughter,
193 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
194 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
195 particle->SetWeight(weight);
196 particle->SetUniqueID(mech);
197 if(!done) particle->SetBit(kDoneBit);
200 // Declare that the daughter information is valid
201 particle->SetBit(kDaughtersBit);
202 // Add the particle to the stack
203 fParticleMap->AddAtAndExpand(particle, fNtrack);
206 particle = (TParticle*) fParticleMap->At(parent);
208 particle->SetLastDaughter(fNtrack);
209 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
212 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
217 // This is a primary track. Set high water mark for this event
220 // Set also number if primary tracks
221 fNprimary = fHgwmk+1;
227 //_____________________________________________________________________________
228 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
229 Double_t px, Double_t py, Double_t pz, Double_t e,
230 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
231 Double_t polx, Double_t poly, Double_t polz,
232 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
235 // Load a track on the stack
237 // done 0 if the track has to be transported
239 // parent identifier of the parent track. -1 for a primary
241 // kS generation status code
242 // px, py, pz momentum GeV/c
243 // vx, vy, vz position
244 // polar polarisation
245 // tof time of flight in seconds
246 // mech production mechanism
247 // ntr on output the number of the track stored
249 // New method interface:
250 // arguments were changed to be in correspondence with TParticle
252 // Note: the energy is not calculated from the static mass but
253 // it is passed by argument e.
256 const Int_t kFirstDaughter=-1;
257 const Int_t kLastDaughter=-1;
259 TClonesArray &particles = *fParticles;
261 = new(particles[fLoadPoint++])
262 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
263 px, py, pz, e, vx, vy, vz, tof);
265 particle->SetPolarisation(polx, poly, polz);
266 particle->SetWeight(weight);
267 particle->SetUniqueID(mech);
269 if(!done) particle->SetBit(kDoneBit);
271 // Declare that the daughter information is valid
272 particle->SetBit(kDaughtersBit);
273 // Add the particle to the stack
274 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
277 particle = (TParticle*) fParticleMap->At(parent);
279 particle->SetLastDaughter(fNtrack);
280 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
283 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
288 // This is a primary track. Set high water mark for this event
291 // Set also number if primary tracks
292 fNprimary = fHgwmk+1;
298 //_____________________________________________________________________________
299 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
300 Float_t &e, Float_t *vpos, Float_t *polar,
304 // Return next track from stack of particles
308 TParticle* track = GetNextParticle();
309 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
313 ipart=track->GetPdgCode();
322 track->GetPolarisation(pol);
327 track->SetBit(kDoneBit);
328 // cout << "Filled params" << endl;
334 // stop and start timer when we start a primary track
335 Int_t nprimaries = fNprimary;
336 if (fCurrent >= nprimaries) return;
337 if (fCurrent < nprimaries-1) {
339 track=(TParticle*) fParticleMap->At(fCurrent+1);
340 // track->SetProcessTime(fTimer.CpuTime());
346 //_____________________________________________________________________________
347 void AliStack::PurifyKine()
350 // Compress kinematic tree keeping only flagged particles
351 // and renaming the particle id's in all the hits
354 TObjArray &particles = *fParticleMap;
355 int nkeep=fHgwmk+1, parent, i;
356 TParticle *part, *father;
357 TArrayI map(particles.GetLast()+1);
359 // Save in Header total number of tracks before compression
361 // If no tracks generated return now
362 if(fHgwmk+1 == fNtrack) return;
364 // First pass, invalid Daughter information
365 for(i=0; i<fNtrack; i++) {
366 // Preset map, to be removed later
367 if(i<=fHgwmk) map[i]=i ;
370 if((part=(TParticle*) particles.At(i))) {
372 // Check of this track should be kept for physics reasons
373 if (KeepPhysics(part)) KeepTrack(i);
375 part->ResetBit(kDaughtersBit);
376 part->SetFirstDaughter(-1);
377 part->SetLastDaughter(-1);
381 // Invalid daughter information for the parent of the first particle
382 // generated. This may or may not be the current primary according to
383 // whether decays have been recorded among the primaries
384 part = (TParticle *)particles.At(fHgwmk+1);
385 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
386 // Second pass, build map between old and new numbering
387 for(i=fHgwmk+1; i<fNtrack; i++) {
388 if(particles.At(i)->TestBit(kKeepBit)) {
390 // This particle has to be kept
392 // If old and new are different, have to move the pointer
393 if(i!=nkeep) particles[nkeep]=particles.At(i);
394 part = (TParticle*) particles.At(nkeep);
396 // as the parent is always *before*, it must be already
397 // in place. This is what we are checking anyway!
398 if((parent=part->GetFirstMother())>fHgwmk)
399 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
400 else part->SetFirstMother(map[parent]);
406 // Fix daughters information
407 for (i=fHgwmk+1; i<nkeep; i++) {
408 part = (TParticle *)particles.At(i);
409 parent = part->GetFirstMother();
411 father = (TParticle *)particles.At(parent);
412 if(father->TestBit(kDaughtersBit)) {
414 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
415 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
417 // Initialise daughters info for first pass
418 father->SetFirstDaughter(i);
419 father->SetLastDaughter(i);
420 father->SetBit(kDaughtersBit);
425 // Now loop on all registered hit lists
426 TList* hitLists = gAlice->GetHitLists();
427 TIter next(hitLists);
428 TCollection *hitList;
429 while((hitList = (TCollection*)next())) {
430 TIter nexthit(hitList);
432 while((hit = (AliHit*)nexthit())) {
433 hit->SetTrack(map[hit->GetTrack()]);
438 // This for detectors which have a special mapping mechanism
439 // for hits, such as TPC and TRD
442 TObjArray* modules = gAlice->Modules();
443 TIter nextmod(modules);
445 while((detector = (AliModule*)nextmod())) {
446 detector->RemapTrackHitIDs(map.GetArray());
449 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
450 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
452 for (i=fHgwmk+1; i<nkeep; ++i) {
453 fParticleBuffer = (TParticle*) particles.At(i);
454 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
456 particles[i]=fParticleBuffer=0;
459 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
461 Int_t toshrink = fNtrack-fHgwmk-1;
462 fLoadPoint-=toshrink;
463 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
470 Bool_t AliStack::KeepPhysics(TParticle* part)
473 // Some particles have to kept on the stack for reasons motivated
474 // by physics analysis. Decision is put here.
476 Bool_t keep = kFALSE;
478 // Keep first-generation daughter from primaries with heavy flavor
480 Int_t parent = part->GetFirstMother();
481 if (parent >= 0 && parent <= fHgwmk) {
482 TParticle* father = (TParticle*) Particles()->At(parent);
483 Int_t kf = father->GetPdgCode();
487 if (kfl > 10) kfl/=100;
489 if (kfl > 10) kfl/=10;
490 if (kfl > 10) kfl/=10;
498 //_____________________________________________________________________________
499 void AliStack::FinishEvent()
502 // Write out the kinematics that was not yet filled
505 // Update event header
509 // Fatal("FinishEvent", "No kinematics tree is defined.");
510 // Don't panic this is a probably a lego run
516 if(fTreeK->GetEntries() ==0) {
517 // set the fParticleFileMap size for the first time
518 fParticleFileMap.Set(fHgwmk+1);
521 Bool_t allFilled = kFALSE;
523 for(Int_t i=0; i<fHgwmk+1; ++i)
524 if((part=fParticleMap->At(i))) {
525 fParticleBuffer = (TParticle*) part;
526 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
528 //PH (*fParticleMap)[i]=fParticleBuffer=0;
530 fParticleMap->AddAt(0,i);
532 // When all primaries were filled no particle!=0
533 // should be left => to be removed later.
534 if (allFilled) printf("Why != 0 part # %d?\n",i);
537 // // printf("Why = 0 part # %d?\n",i); => We know.
539 // we don't break now in order to be sure there is no
540 // particle !=0 left.
541 // To be removed later and replaced with break.
542 if(!allFilled) allFilled = kTRUE;
544 // cout << "Nof particles: " << fNtrack << endl;
548 //_____________________________________________________________________________
549 void AliStack::FlagTrack(Int_t track)
552 // Flags a track and all its family tree to be kept
559 particle=(TParticle*)fParticleMap->At(curr);
561 // If the particle is flagged the three from here upward is saved already
562 if(particle->TestBit(kKeepBit)) return;
564 // Save this particle
565 particle->SetBit(kKeepBit);
567 // Move to father if any
568 if((curr=particle->GetFirstMother())==-1) return;
572 //_____________________________________________________________________________
573 void AliStack::KeepTrack(Int_t track)
576 // Flags a track to be kept
579 fParticleMap->At(track)->SetBit(kKeepBit);
582 //_____________________________________________________________________________
583 void AliStack::Reset(Int_t size)
597 //_____________________________________________________________________________
598 void AliStack::ResetArrays(Int_t size)
601 // Resets stack arrays
607 fParticles = new TClonesArray("TParticle",1000);
609 fParticleMap->Clear();
610 if (size>0) fParticleMap->Expand(size);}
612 fParticleMap = new TObjArray(size);
615 //_____________________________________________________________________________
616 void AliStack::SetHighWaterMark(Int_t nt)
619 // Set high water mark for last track in event
623 fCurrentPrimary=fHgwmk;
625 // Set also number of primary tracks
626 fNprimary = fHgwmk+1;
630 //_____________________________________________________________________________
631 TParticle* AliStack::Particle(Int_t i)
634 // Return particle with specified ID
636 //PH if(!(*fParticleMap)[i]) {
637 if(!fParticleMap->At(i)) {
638 Int_t nentries = fParticles->GetEntriesFast();
639 // algorithmic way of getting entry index
640 // (primary particles are filled after secondaries)
643 entry = i+fNtrack-fNprimary;
646 // check whether algorithmic way and
647 // and the fParticleFileMap[i] give the same;
648 // give the fatal error if not
649 if (entry != fParticleFileMap[i]) {
651 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
652 entry, fParticleFileMap[i]);
655 fTreeK->GetEntry(entry);
656 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
657 fParticleMap->AddAt((*fParticles)[nentries],i);
659 //PH return (TParticle *) (*fParticleMap)[i];
660 return (TParticle *) fParticleMap->At(i);
663 //_____________________________________________________________________________
664 Int_t AliStack::GetPrimary(Int_t id)
667 // Return number of primary that has generated track
675 parent=Particle(current)->GetFirstMother();
676 if(parent<0) return current;
680 //_____________________________________________________________________________
681 void AliStack::DumpPart (Int_t i) const
684 // Dumps particle i in the stack
687 //PH ((TParticle*) (*fParticleMap)[i])->Print();
688 ((TParticle*) fParticleMap->At(i))->Print();
691 //_____________________________________________________________________________
692 void AliStack::DumpPStack ()
695 // Dumps the particle stack
701 "\n\n=======================================================================\n");
702 for (i=0;i<fNtrack;i++)
704 TParticle* particle = Particle(i);
706 printf("-> %d ",i); particle->Print();
707 printf("--------------------------------------------------------------\n");
710 Warning("DumpPStack", "No particle with id %d.", i);
714 "\n=======================================================================\n\n");
716 // print particle file map
717 printf("\nParticle file map: \n");
718 for (i=0; i<fNtrack; i++)
719 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
723 //_____________________________________________________________________________
724 void AliStack::DumpLoadedStack() const
727 // Dumps the particle in the stack
728 // that are loaded in memory.
731 TObjArray &particles = *fParticleMap;
733 "\n\n=======================================================================\n");
734 for (Int_t i=0;i<fNtrack;i++)
736 TParticle* particle = (TParticle*) particles[i];
738 printf("-> %d ",i); particle->Print();
739 printf("--------------------------------------------------------------\n");
742 printf("-> %d Particle not loaded.\n",i);
743 printf("--------------------------------------------------------------\n");
747 "\n=======================================================================\n\n");
754 //_____________________________________________________________________________
755 void AliStack::CleanParents()
758 // Clean particles stack
759 // Set parent/daughter relations
762 TObjArray &particles = *fParticleMap;
765 for(i=0; i<fHgwmk+1; i++) {
766 part = (TParticle *)particles.At(i);
767 if(part) if(!part->TestBit(kDaughtersBit)) {
768 part->SetFirstDaughter(-1);
769 part->SetLastDaughter(-1);
774 //_____________________________________________________________________________
775 TParticle* AliStack::GetNextParticle()
778 // Return next particle from stack of particles
781 TParticle* particle = 0;
783 // search secondaries
784 //for(Int_t i=fNtrack-1; i>=0; i--) {
785 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
786 particle = (TParticle*) fParticleMap->At(i);
787 if ((particle) && (!particle->TestBit(kDoneBit))) {
789 //cout << "GetNextParticle() - secondary "
790 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
795 // take next primary if all secondaries were done
796 while (fCurrentPrimary>=0) {
797 fCurrent = fCurrentPrimary;
798 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
799 if ((particle) && (!particle->TestBit(kDoneBit))) {
800 //cout << "GetNextParticle() - primary "
801 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
806 // nothing to be tracked
808 //cout << "GetNextParticle() - none "
809 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
813 //__________________________________________________________________________________________
814 void AliStack::MakeTree(Int_t event, const char *file)
817 // Make Kine tree and creates branch for writing particles
820 // Make Kinematics Tree
823 sprintf(hname,"TreeK%d",event);
824 fTreeK = new TTree(hname,"Kinematics");
825 // Create a branch for particles
826 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
827 fTreeK->Write(0,TObject::kOverwrite);
831 //_____________________________________________________________________________
832 void AliStack::BeginEvent(Int_t event)
843 sprintf(hname,"TreeK%d",event);
844 fTreeK->SetName(hname);
848 //_____________________________________________________________________________
849 void AliStack::FinishRun()
851 // Clean TreeK information
853 delete fTreeK; fTreeK = 0;
857 Bool_t AliStack::GetEvent(Int_t event)
860 // Get new event from TreeK
862 // Reset/Create the particle stack
863 if (fTreeK) delete fTreeK;
865 // Get Kine Tree from file
867 sprintf(treeName,"TreeK%d",event);
868 fTreeK = (TTree*)gDirectory->Get(treeName);
871 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
873 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
876 // printf("\n primaries %d", fNprimary);
877 // printf("\n tracks %d", fNtrack);
879 Int_t size = (Int_t)fTreeK->GetEntries();