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.25 2002/10/22 15:21:39 hristov
19 New methods for more effective access of particles from TreeK (J.Chudoba)
21 Revision 1.24 2002/10/22 15:02:15 alibrary
22 Introducing Riostream.h
24 Revision 1.23 2002/10/14 14:57:32 hristov
25 Merging the VirtualMC branch to the main development branch (HEAD)
27 Revision 1.19.4.2 2002/08/28 15:06:52 alibrary
30 Revision 1.19.4.1 2002/06/10 14:43:06 hristov
33 Revision 1.21 2002/05/28 14:24:57 hristov
34 Correct warning messages
36 Revision 1.20 2002/04/30 11:47:30 morsch
37 KeepPhysics method called by PurifyKine added (N. Carrer, A.M.)
39 Revision 1.19 2002/03/12 11:06:03 morsch
40 Add particle status code to argument list of SetTrack(..).
42 Revision 1.18 2002/02/20 16:14:41 hristov
43 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
45 Revision 1.17 2001/12/05 08:51:56 hristov
46 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
48 Revision 1.16 2001/11/20 09:27:55 hristov
49 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
51 Revision 1.15 2001/09/04 15:10:37 hristov
52 Additional protection is included to avoid some problems using Hijing
54 Revision 1.14 2001/08/30 09:44:06 hristov
55 VertexSource_t added to avoid the warnings
57 Revision 1.13 2001/08/29 13:31:42 morsch
58 Protection against (fTreeK == 0) in destructor.
60 Revision 1.12 2001/07/27 13:03:13 hristov
61 Default Branch split level set to 99
63 Revision 1.11 2001/07/27 12:34:20 jchudoba
64 remove the dummy argument in GetEvent method
66 Revision 1.10 2001/07/20 10:13:54 morsch
67 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
69 Revision 1.9 2001/07/03 08:10:57 hristov
70 J.Chudoba's changes merged correctly with the HEAD
72 Revision 1.6 2001/05/31 06:59:06 fca
73 Clean setting and deleting of fParticleBuffer
75 Revision 1.5 2001/05/30 12:18:46 hristov
76 Loop variables declared once
78 Revision 1.4 2001/05/25 07:25:20 hristov
79 AliStack destructor corrected (I.Hrivnacova)
81 Revision 1.3 2001/05/22 14:33:16 hristov
84 Revision 1.2 2001/05/17 05:49:39 fca
85 Reset pointers to daughters
87 Revision 1.1 2001/05/16 14:57:22 alibrary
88 New files for folders and Stack
92 ///////////////////////////////////////////////////////////////////////////////
94 // Particles stack class
96 ///////////////////////////////////////////////////////////////////////////////
98 #include <Riostream.h>
100 #include <TObjArray.h>
101 #include <TParticle.h>
107 #include "AliStack.h"
109 #include "AliModule.h"
114 //_______________________________________________________________________
115 AliStack::AliStack():
129 // Default constructor
133 //_______________________________________________________________________
134 AliStack::AliStack(Int_t size):
135 fParticles(new TClonesArray("TParticle",1000)),
136 fParticleMap(new TObjArray(size)),
152 //_______________________________________________________________________
153 AliStack::AliStack(const AliStack& st):
173 //_______________________________________________________________________
174 void AliStack::Copy(AliStack&) const
176 Fatal("Copy","Not implemented!\n");
179 //_______________________________________________________________________
180 AliStack::~AliStack()
187 fParticles->Delete();
198 //_____________________________________________________________________________
199 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
200 Float_t *vpos, Float_t *polar, Float_t tof,
201 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
204 // Load a track on the stack
206 // done 0 if the track has to be transported
208 // parent identifier of the parent track. -1 for a primary
210 // pmom momentum GeV/c
212 // polar polarisation
213 // tof time of flight in seconds
214 // mecha production mechanism
215 // ntr on output the number of the track stored
218 // const Float_t tlife=0;
221 // Here we get the static mass
222 // For MC is ok, but a more sophisticated method could be necessary
223 // if the calculated mass is required
224 // also, this method is potentially dangerous if the mass
225 // used in the MC is not the same of the PDG database
227 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
228 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
229 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
231 // 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",
232 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
235 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
236 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
237 mech, ntr, weight, is);
240 //_____________________________________________________________________________
241 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
242 Double_t px, Double_t py, Double_t pz, Double_t e,
243 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
244 Double_t polx, Double_t poly, Double_t polz,
245 AliMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
248 // Load a track on the stack
250 // done 0 if the track has to be transported
252 // parent identifier of the parent track. -1 for a primary
254 // kS generation status code
255 // px, py, pz momentum GeV/c
256 // vx, vy, vz position
257 // polar polarisation
258 // tof time of flight in seconds
259 // mech production mechanism
260 // ntr on output the number of the track stored
262 // New method interface:
263 // arguments were changed to be in correspondence with TParticle
265 // Note: the energy is not calculated from the static mass but
266 // it is passed by argument e.
269 const Int_t kFirstDaughter=-1;
270 const Int_t kLastDaughter=-1;
272 TClonesArray &particles = *fParticles;
274 = new(particles[fLoadPoint++])
275 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
276 px, py, pz, e, vx, vy, vz, tof);
278 particle->SetPolarisation(polx, poly, polz);
279 particle->SetWeight(weight);
280 particle->SetUniqueID(mech);
282 if(!done) particle->SetBit(kDoneBit);
284 // Declare that the daughter information is valid
285 particle->SetBit(kDaughtersBit);
286 // Add the particle to the stack
287 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
290 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
292 particle->SetLastDaughter(fNtrack);
293 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
296 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
301 // This is a primary track. Set high water mark for this event
304 // Set also number if primary tracks
305 fNprimary = fHgwmk+1;
311 //_____________________________________________________________________________
312 TParticle* AliStack::GetNextTrack(Int_t& itrack)
315 // Returns next track from stack of particles
319 TParticle* track = GetNextParticle();
323 track->SetBit(kDoneBit);
332 //_____________________________________________________________________________
333 void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
334 Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
335 Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
336 Double_t& polx, Double_t& poly, Double_t& polz)
339 // Return next track from stack of particles
343 TParticle* track = GetNextParticle();
344 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
348 pdg = track->GetPdgCode();
358 track->GetPolarisation(pol);
362 track->SetBit(kDoneBit);
363 // cout << "Filled params" << endl;
369 // stop and start timer when we start a primary track
370 Int_t nprimaries = fNprimary;
371 if (fCurrent >= nprimaries) return;
372 if (fCurrent < nprimaries-1) {
374 track=(TParticle*) fParticleMap->At(fCurrent+1);
375 // track->SetProcessTime(fTimer.CpuTime());
381 //_____________________________________________________________________________
382 TParticle* AliStack::GetPrimaryForTracking(Int_t i)
385 // Returns i-th primary particle if it is flagged to be tracked,
389 TParticle* particle = Particle(i);
391 if (!particle->TestBit(kDoneBit))
398 //_____________________________________________________________________________
399 void AliStack::PurifyKine()
402 // Compress kinematic tree keeping only flagged particles
403 // and renaming the particle id's in all the hits
406 TObjArray &particles = *fParticleMap;
407 int nkeep=fHgwmk+1, parent, i;
408 TParticle *part, *father;
409 TArrayI map(particles.GetLast()+1);
411 // Save in Header total number of tracks before compression
413 // If no tracks generated return now
414 if(fHgwmk+1 == fNtrack) return;
416 // First pass, invalid Daughter information
417 for(i=0; i<fNtrack; i++) {
418 // Preset map, to be removed later
419 if(i<=fHgwmk) map[i]=i ;
422 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
424 // Check of this track should be kept for physics reasons
425 if (KeepPhysics(part)) KeepTrack(i);
427 part->ResetBit(kDaughtersBit);
428 part->SetFirstDaughter(-1);
429 part->SetLastDaughter(-1);
433 // Invalid daughter information for the parent of the first particle
434 // generated. This may or may not be the current primary according to
435 // whether decays have been recorded among the primaries
436 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
437 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
438 // Second pass, build map between old and new numbering
439 for(i=fHgwmk+1; i<fNtrack; i++) {
440 if(particles.At(i)->TestBit(kKeepBit)) {
442 // This particle has to be kept
444 // If old and new are different, have to move the pointer
445 if(i!=nkeep) particles[nkeep]=particles.At(i);
446 part = dynamic_cast<TParticle*>(particles.At(nkeep));
448 // as the parent is always *before*, it must be already
449 // in place. This is what we are checking anyway!
450 if((parent=part->GetFirstMother())>fHgwmk)
451 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
452 else part->SetFirstMother(map[parent]);
458 // Fix daughters information
459 for (i=fHgwmk+1; i<nkeep; i++) {
460 part = dynamic_cast<TParticle*>(particles.At(i));
461 parent = part->GetFirstMother();
463 father = dynamic_cast<TParticle*>(particles.At(parent));
464 if(father->TestBit(kDaughtersBit)) {
466 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
467 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
469 // Initialise daughters info for first pass
470 father->SetFirstDaughter(i);
471 father->SetLastDaughter(i);
472 father->SetBit(kDaughtersBit);
477 // Now loop on all registered hit lists
478 TList* hitLists = gAlice->GetHitLists();
479 TIter next(hitLists);
480 TCollection *hitList;
481 while((hitList = dynamic_cast<TCollection*>(next()))) {
482 TIter nexthit(hitList);
484 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
485 hit->SetTrack(map[hit->GetTrack()]);
490 // This for detectors which have a special mapping mechanism
491 // for hits, such as TPC and TRD
494 TObjArray* modules = gAlice->Modules();
495 TIter nextmod(modules);
497 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
498 detector->RemapTrackHitIDs(map.GetArray());
499 detector->RemapTrackReferencesIDs(map.GetArray());
502 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
503 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
505 for (i=fHgwmk+1; i<nkeep; ++i) {
506 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
507 fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
509 particles[i]=fParticleBuffer=0;
512 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
514 Int_t toshrink = fNtrack-fHgwmk-1;
515 fLoadPoint-=toshrink;
516 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
523 Bool_t AliStack::KeepPhysics(TParticle* part)
526 // Some particles have to kept on the stack for reasons motivated
527 // by physics analysis. Decision is put here.
529 Bool_t keep = kFALSE;
531 // Keep first-generation daughter from primaries with heavy flavor
533 Int_t parent = part->GetFirstMother();
534 if (parent >= 0 && parent <= fHgwmk) {
535 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
536 Int_t kf = father->GetPdgCode();
540 if (kfl > 10) kfl/=100;
542 if (kfl > 10) kfl/=10;
543 if (kfl > 10) kfl/=10;
551 //_____________________________________________________________________________
552 void AliStack::FinishEvent()
555 // Write out the kinematics that was not yet filled
558 // Update event header
562 // Fatal("FinishEvent", "No kinematics tree is defined.");
563 // Don't panic this is a probably a lego run
569 if(fTreeK->GetEntries() ==0) {
570 // set the fParticleFileMap size for the first time
571 fParticleFileMap.Set(fHgwmk+1);
574 Bool_t allFilled = kFALSE;
576 for(Int_t i=0; i<fHgwmk+1; ++i)
577 if((part=fParticleMap->At(i))) {
578 fParticleBuffer = dynamic_cast<TParticle*>(part);
579 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
581 //PH (*fParticleMap)[i]=fParticleBuffer=0;
583 fParticleMap->AddAt(0,i);
585 // When all primaries were filled no particle!=0
586 // should be left => to be removed later.
587 if (allFilled) printf("Why != 0 part # %d?\n",i);
590 // // printf("Why = 0 part # %d?\n",i); => We know.
592 // we don't break now in order to be sure there is no
593 // particle !=0 left.
594 // To be removed later and replaced with break.
595 if(!allFilled) allFilled = kTRUE;
597 // cout << "Nof particles: " << fNtrack << endl;
601 //_____________________________________________________________________________
602 void AliStack::FlagTrack(Int_t track)
605 // Flags a track and all its family tree to be kept
612 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
614 // If the particle is flagged the three from here upward is saved already
615 if(particle->TestBit(kKeepBit)) return;
617 // Save this particle
618 particle->SetBit(kKeepBit);
620 // Move to father if any
621 if((curr=particle->GetFirstMother())==-1) return;
625 //_____________________________________________________________________________
626 void AliStack::KeepTrack(Int_t track)
629 // Flags a track to be kept
632 fParticleMap->At(track)->SetBit(kKeepBit);
635 //_____________________________________________________________________________
636 void AliStack::Reset(Int_t size)
650 //_____________________________________________________________________________
651 void AliStack::ResetArrays(Int_t size)
654 // Resets stack arrays
660 fParticles = new TClonesArray("TParticle",1000);
662 fParticleMap->Clear();
663 if (size>0) fParticleMap->Expand(size);}
665 fParticleMap = new TObjArray(size);
668 //_____________________________________________________________________________
669 void AliStack::SetHighWaterMark(Int_t)
672 // Set high water mark for last track in event
676 fCurrentPrimary=fHgwmk;
678 // Set also number of primary tracks
679 fNprimary = fHgwmk+1;
683 //_____________________________________________________________________________
684 TParticle* AliStack::Particle(Int_t i)
687 // Return particle with specified ID
689 //PH if(!(*fParticleMap)[i]) {
690 if(!fParticleMap->At(i)) {
691 Int_t nentries = fParticles->GetEntriesFast();
692 // algorithmic way of getting entry index
693 // (primary particles are filled after secondaries)
694 Int_t entry = TreeKEntry(i);
695 // check whether algorithmic way and
696 // and the fParticleFileMap[i] give the same;
697 // give the fatal error if not
698 if (entry != fParticleFileMap[i]) {
700 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
701 entry, fParticleFileMap[i]);
704 fTreeK->GetEntry(entry);
705 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
706 fParticleMap->AddAt((*fParticles)[nentries],i);
708 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
709 return dynamic_cast<TParticle*>(fParticleMap->At(i));
712 //_____________________________________________________________________________
713 TParticle* AliStack::ParticleFromTreeK(Int_t id) const
716 // return pointer to TParticle with label id
719 if ((entry = TreeKEntry(id)) < 0) return 0;
720 if (fTreeK->GetEntry(entry)<=0) return 0;
721 return fParticleBuffer;
724 //_____________________________________________________________________________
725 Int_t AliStack::TreeKEntry(Int_t id) const
728 // return entry number in the TreeK for particle with label id
729 // return negative number if label>fNtrack
733 entry = id+fNtrack-fNprimary;
735 entry = id-fNprimary;
739 //_____________________________________________________________________________
740 Int_t AliStack::GetPrimary(Int_t id)
743 // Return number of primary that has generated track
751 parent=Particle(current)->GetFirstMother();
752 if(parent<0) return current;
756 //_____________________________________________________________________________
757 void AliStack::DumpPart (Int_t i) const
760 // Dumps particle i in the stack
763 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
764 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
767 //_____________________________________________________________________________
768 void AliStack::DumpPStack ()
771 // Dumps the particle stack
777 "\n\n=======================================================================\n");
778 for (i=0;i<fNtrack;i++)
780 TParticle* particle = Particle(i);
782 printf("-> %d ",i); particle->Print();
783 printf("--------------------------------------------------------------\n");
786 Warning("DumpPStack", "No particle with id %d.", i);
790 "\n=======================================================================\n\n");
792 // print particle file map
793 printf("\nParticle file map: \n");
794 for (i=0; i<fNtrack; i++)
795 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
799 //_____________________________________________________________________________
800 void AliStack::DumpLoadedStack() const
803 // Dumps the particle in the stack
804 // that are loaded in memory.
807 TObjArray &particles = *fParticleMap;
809 "\n\n=======================================================================\n");
810 for (Int_t i=0;i<fNtrack;i++)
812 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
814 printf("-> %d ",i); particle->Print();
815 printf("--------------------------------------------------------------\n");
818 printf("-> %d Particle not loaded.\n",i);
819 printf("--------------------------------------------------------------\n");
823 "\n=======================================================================\n\n");
830 //_____________________________________________________________________________
831 void AliStack::CleanParents()
834 // Clean particles stack
835 // Set parent/daughter relations
838 TObjArray &particles = *fParticleMap;
841 for(i=0; i<fHgwmk+1; i++) {
842 part = dynamic_cast<TParticle*>(particles.At(i));
843 if(part) if(!part->TestBit(kDaughtersBit)) {
844 part->SetFirstDaughter(-1);
845 part->SetLastDaughter(-1);
850 //_____________________________________________________________________________
851 TParticle* AliStack::GetNextParticle()
854 // Return next particle from stack of particles
857 TParticle* particle = 0;
859 // search secondaries
860 //for(Int_t i=fNtrack-1; i>=0; i--) {
861 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
862 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
863 if ((particle) && (!particle->TestBit(kDoneBit))) {
865 //cout << "GetNextParticle() - secondary "
866 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
871 // take next primary if all secondaries were done
872 while (fCurrentPrimary>=0) {
873 fCurrent = fCurrentPrimary;
874 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
875 if ((particle) && (!particle->TestBit(kDoneBit))) {
876 //cout << "GetNextParticle() - primary "
877 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
882 // nothing to be tracked
884 //cout << "GetNextParticle() - none "
885 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
889 //__________________________________________________________________________________________
890 void AliStack::MakeTree(Int_t event, const char * /*file*/)
893 // Make Kine tree and creates branch for writing particles
896 // Make Kinematics Tree
899 sprintf(hname,"TreeK%d",event);
900 fTreeK = new TTree(hname,"Kinematics");
901 // Create a branch for particles
902 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
903 fTreeK->Write(0,TObject::kOverwrite);
907 //_____________________________________________________________________________
908 void AliStack::BeginEvent(Int_t event)
919 sprintf(hname,"TreeK%d",event);
920 fTreeK->SetName(hname);
924 //_____________________________________________________________________________
925 void AliStack::FinishRun()
927 // Clean TreeK information
929 delete fTreeK; fTreeK = 0;
933 Bool_t AliStack::GetEvent(Int_t event)
936 // Get new event from TreeK
938 // Reset/Create the particle stack
939 if (fTreeK) delete fTreeK;
941 // Get Kine Tree from file
943 sprintf(treeName,"TreeK%d",event);
944 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
947 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
949 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
950 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
953 // printf("\n primaries %d", fNprimary);
954 // printf("\n tracks %d", fNtrack);
956 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());