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.14 2001/08/30 09:44:06 hristov
19 VertexSource_t added to avoid the warnings
21 Revision 1.13 2001/08/29 13:31:42 morsch
22 Protection against (fTreeK == 0) in destructor.
24 Revision 1.12 2001/07/27 13:03:13 hristov
25 Default Branch split level set to 99
27 Revision 1.11 2001/07/27 12:34:20 jchudoba
28 remove the dummy argument in GetEvent method
30 Revision 1.10 2001/07/20 10:13:54 morsch
31 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
33 Revision 1.9 2001/07/03 08:10:57 hristov
34 J.Chudoba's changes merged correctly with the HEAD
36 Revision 1.6 2001/05/31 06:59:06 fca
37 Clean setting and deleting of fParticleBuffer
39 Revision 1.5 2001/05/30 12:18:46 hristov
40 Loop variables declared once
42 Revision 1.4 2001/05/25 07:25:20 hristov
43 AliStack destructor corrected (I.Hrivnacova)
45 Revision 1.3 2001/05/22 14:33:16 hristov
48 Revision 1.2 2001/05/17 05:49:39 fca
49 Reset pointers to daughters
51 Revision 1.1 2001/05/16 14:57:22 alibrary
52 New files for folders and Stack
56 ///////////////////////////////////////////////////////////////////////////////
58 // Particles stack class
60 ///////////////////////////////////////////////////////////////////////////////
64 #include <TObjArray.h>
65 #include <TParticle.h>
73 #include "AliModule.h"
75 //#include "ETrackBits.h"
79 //_____________________________________________________________________________
80 AliStack::AliStack(Int_t size)
86 // Create the particles arrays
87 fParticles = new TClonesArray("TParticle",1000);
88 fParticleMap = new TObjArray(size);
98 //_____________________________________________________________________________
102 // Default constructor
105 // Create the particles arrays
106 fParticles = new TClonesArray("TParticle",1000);
107 fParticleMap = new TObjArray(10000);
112 fCurrentPrimary = -1;
117 //_____________________________________________________________________________
118 AliStack::~AliStack()
124 delete fParticleBuffer;
126 fParticles->Delete();
130 if (fTreeK) delete fTreeK;
137 //_____________________________________________________________________________
138 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
139 Float_t *vpos, Float_t *polar, Float_t tof,
140 AliMCProcess mech, Int_t &ntr, Float_t weight)
143 // Load a track on the stack
145 // done 0 if the track has to be transported
147 // parent identifier of the parent track. -1 for a primary
149 // pmom momentum GeV/c
151 // polar polarisation
152 // tof time of flight in seconds
153 // mecha production mechanism
154 // ntr on output the number of the track stored
158 const Int_t kfirstdaughter=-1;
159 const Int_t klastdaughter=-1;
161 // const Float_t tlife=0;
164 // Here we get the static mass
165 // For MC is ok, but a more sophisticated method could be necessary
166 // if the calculated mass is required
167 // also, this method is potentially dangerous if the mass
168 // used in the MC is not the same of the PDG database
170 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
171 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
172 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
174 // 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",
175 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
177 TClonesArray &particles = *fParticles;
179 = new(particles[fLoadPoint++])
180 TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
181 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
182 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
183 particle->SetWeight(weight);
184 particle->SetUniqueID(mech);
185 if(!done) particle->SetBit(kDoneBit);
188 // Declare that the daughter information is valid
189 particle->SetBit(kDaughtersBit);
190 // Add the particle to the stack
191 fParticleMap->AddAtAndExpand(particle, fNtrack);
194 particle = (TParticle*) fParticleMap->At(parent);
196 particle->SetLastDaughter(fNtrack);
197 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
200 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
205 // This is a primary track. Set high water mark for this event
208 // Set also number if primary tracks
209 fNprimary = fHgwmk+1;
215 //_____________________________________________________________________________
216 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
217 Double_t px, Double_t py, Double_t pz, Double_t e,
218 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
219 Double_t polx, Double_t poly, Double_t polz,
220 AliMCProcess mech, Int_t &ntr, Float_t weight)
223 // Load a track on the stack
225 // done 0 if the track has to be transported
227 // parent identifier of the parent track. -1 for a primary
229 // kS generation status code
230 // px, py, pz momentum GeV/c
231 // vx, vy, vz position
232 // polar polarisation
233 // tof time of flight in seconds
234 // mech production mechanism
235 // ntr on output the number of the track stored
237 // New method interface:
238 // arguments were changed to be in correspondence with TParticle
240 // Note: the energy is not calculated from the static mass but
241 // it is passed by argument e.
245 const Int_t kFirstDaughter=-1;
246 const Int_t kLastDaughter=-1;
248 TClonesArray &particles = *fParticles;
250 = new(particles[fLoadPoint++])
251 TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
252 px, py, pz, e, vx, vy, vz, tof);
254 particle->SetPolarisation(polx, poly, polz);
255 particle->SetWeight(weight);
256 particle->SetUniqueID(mech);
258 if(!done) particle->SetBit(kDoneBit);
260 // Declare that the daughter information is valid
261 particle->SetBit(kDaughtersBit);
262 // Add the particle to the stack
263 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
266 particle = (TParticle*) fParticleMap->At(parent);
268 particle->SetLastDaughter(fNtrack);
269 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
272 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
277 // This is a primary track. Set high water mark for this event
280 // Set also number if primary tracks
281 fNprimary = fHgwmk+1;
287 //_____________________________________________________________________________
288 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
289 Float_t &e, Float_t *vpos, Float_t *polar,
293 // Return next track from stack of particles
297 TParticle* track = GetNextParticle();
298 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
302 ipart=track->GetPdgCode();
311 track->GetPolarisation(pol);
316 track->SetBit(kDoneBit);
317 // cout << "Filled params" << endl;
323 // stop and start timer when we start a primary track
324 Int_t nprimaries = fNprimary;
325 if (fCurrent >= nprimaries) return;
326 if (fCurrent < nprimaries-1) {
328 track=(TParticle*) fParticleMap->At(fCurrent+1);
329 // track->SetProcessTime(fTimer.CpuTime());
335 //_____________________________________________________________________________
336 void AliStack::PurifyKine()
339 // Compress kinematic tree keeping only flagged particles
340 // and renaming the particle id's in all the hits
343 TObjArray &particles = *fParticleMap;
344 int nkeep=fHgwmk+1, parent, i;
345 TParticle *part, *father;
346 TArrayI map(particles.GetLast()+1);
348 // Save in Header total number of tracks before compression
350 // If no tracks generated return now
351 if(fHgwmk+1 == fNtrack) return;
353 // First pass, invalid Daughter information
354 for(i=0; i<fNtrack; i++) {
355 // Preset map, to be removed later
356 if(i<=fHgwmk) map[i]=i ;
359 if((part=(TParticle*) particles.At(i))) {
360 part->ResetBit(kDaughtersBit);
361 part->SetFirstDaughter(-1);
362 part->SetLastDaughter(-1);
366 // Invalid daughter information for the parent of the first particle
367 // generated. This may or may not be the current primary according to
368 // whether decays have been recorded among the primaries
369 part = (TParticle *)particles.At(fHgwmk+1);
370 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
371 // Second pass, build map between old and new numbering
372 for(i=fHgwmk+1; i<fNtrack; i++) {
373 if(particles.At(i)->TestBit(kKeepBit)) {
375 // This particle has to be kept
377 // If old and new are different, have to move the pointer
378 if(i!=nkeep) particles[nkeep]=particles.At(i);
379 part = (TParticle*) particles.At(nkeep);
381 // as the parent is always *before*, it must be already
382 // in place. This is what we are checking anyway!
383 if((parent=part->GetFirstMother())>fHgwmk)
384 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
385 else part->SetFirstMother(map[parent]);
391 // Fix daughters information
392 for (i=fHgwmk+1; i<nkeep; i++) {
393 part = (TParticle *)particles.At(i);
394 parent = part->GetFirstMother();
396 father = (TParticle *)particles.At(parent);
397 if(father->TestBit(kDaughtersBit)) {
399 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
400 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
402 // Initialise daughters info for first pass
403 father->SetFirstDaughter(i);
404 father->SetLastDaughter(i);
405 father->SetBit(kDaughtersBit);
410 // Now loop on all registered hit lists
411 TList* hitLists = gAlice->GetHitLists();
412 TIter next(hitLists);
413 TCollection *hitList;
414 while((hitList = (TCollection*)next())) {
415 TIter nexthit(hitList);
417 while((hit = (AliHit*)nexthit())) {
418 hit->SetTrack(map[hit->GetTrack()]);
423 // This for detectors which have a special mapping mechanism
424 // for hits, such as TPC and TRD
427 TObjArray* modules = gAlice->Modules();
428 TIter nextmod(modules);
430 while((detector = (AliModule*)nextmod())) {
431 detector->RemapTrackHitIDs(map.GetArray());
434 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
435 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
437 for (i=fHgwmk+1; i<nkeep; ++i) {
438 fParticleBuffer = (TParticle*) particles.At(i);
439 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
441 particles[i]=fParticleBuffer=0;
444 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
446 Int_t toshrink = fNtrack-fHgwmk-1;
447 fLoadPoint-=toshrink;
448 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
455 //_____________________________________________________________________________
456 void AliStack::FinishEvent()
459 // Write out the kinematics that was not yet filled
462 // Update event header
466 // Fatal("FinishEvent", "No kinematics tree is defined.");
467 // Don't panic this is a probably a lego run
473 if(fTreeK->GetEntries() ==0) {
474 // set the fParticleFileMap size for the first time
475 fParticleFileMap.Set(fHgwmk+1);
478 Bool_t allFilled = kFALSE;
480 for(Int_t i=0; i<fHgwmk+1; ++i)
481 if((part=fParticleMap->At(i))) {
482 fParticleBuffer = (TParticle*) part;
483 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
485 //PH (*fParticleMap)[i]=fParticleBuffer=0;
487 fParticleMap->AddAt(0,i);
489 // When all primaries were filled no particle!=0
490 // should be left => to be removed later.
491 if (allFilled) printf("Why != 0 part # %d?\n",i);
494 // // printf("Why = 0 part # %d?\n",i); => We know.
496 // we don't break now in order to be sure there is no
497 // particle !=0 left.
498 // To be removed later and replaced with break.
499 if(!allFilled) allFilled = kTRUE;
501 // cout << "Nof particles: " << fNtrack << endl;
505 //_____________________________________________________________________________
506 void AliStack::FlagTrack(Int_t track)
509 // Flags a track and all its family tree to be kept
516 particle=(TParticle*)fParticleMap->At(curr);
518 // If the particle is flagged the three from here upward is saved already
519 if(particle->TestBit(kKeepBit)) return;
521 // Save this particle
522 particle->SetBit(kKeepBit);
524 // Move to father if any
525 if((curr=particle->GetFirstMother())==-1) return;
529 //_____________________________________________________________________________
530 void AliStack::KeepTrack(Int_t track)
533 // Flags a track to be kept
536 fParticleMap->At(track)->SetBit(kKeepBit);
539 //_____________________________________________________________________________
540 void AliStack::Reset(Int_t size)
554 //_____________________________________________________________________________
555 void AliStack::ResetArrays(Int_t size)
558 // Resets stack arrays
562 fParticleMap->Clear();
563 if (size>0) fParticleMap->Expand(size);
566 //_____________________________________________________________________________
567 void AliStack::SetHighWaterMark(Int_t nt)
570 // Set high water mark for last track in event
574 fCurrentPrimary=fHgwmk;
576 // Set also number of primary tracks
577 fNprimary = fHgwmk+1;
581 //_____________________________________________________________________________
582 TParticle* AliStack::Particle(Int_t i)
585 // Return particle with specified ID
587 //PH if(!(*fParticleMap)[i]) {
588 if(!fParticleMap->At(i)) {
589 Int_t nentries = fParticles->GetEntriesFast();
590 // algorithmic way of getting entry index
591 // (primary particles are filled after secondaries)
594 entry = i+fNtrack-fNprimary;
597 // check whether algorithmic way and
598 // and the fParticleFileMap[i] give the same;
599 // give the fatal error if not
600 if (entry != fParticleFileMap[i]) {
602 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
603 entry, fParticleFileMap[i]);
606 fTreeK->GetEntry(entry);
607 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
608 fParticleMap->AddAt((*fParticles)[nentries],i);
610 //PH return (TParticle *) (*fParticleMap)[i];
611 return (TParticle *) fParticleMap->At(i);
614 //_____________________________________________________________________________
615 Int_t AliStack::GetPrimary(Int_t id) const
618 // Return number of primary that has generated track
627 part = (TParticle *)fParticleMap->At(current);
628 parent=part->GetFirstMother();
629 if(parent<0) return current;
633 //_____________________________________________________________________________
634 void AliStack::DumpPart (Int_t i) const
637 // Dumps particle i in the stack
640 //PH ((TParticle*) (*fParticleMap)[i])->Print();
641 ((TParticle*) fParticleMap->At(i))->Print();
644 //_____________________________________________________________________________
645 void AliStack::DumpPStack ()
648 // Dumps the particle stack
654 "\n\n=======================================================================\n");
655 for (i=0;i<fNtrack;i++)
657 TParticle* particle = Particle(i);
659 printf("-> %d ",i); particle->Print();
660 printf("--------------------------------------------------------------\n");
663 Warning("DumpPStack", "No particle with id %d.", i);
667 "\n=======================================================================\n\n");
669 // print particle file map
670 printf("\nParticle file map: \n");
671 for (i=0; i<fNtrack; i++)
672 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
676 //_____________________________________________________________________________
677 void AliStack::DumpLoadedStack() const
680 // Dumps the particle in the stack
681 // that are loaded in memory.
684 TObjArray &particles = *fParticleMap;
686 "\n\n=======================================================================\n");
687 for (Int_t i=0;i<fNtrack;i++)
689 TParticle* particle = (TParticle*) particles[i];
691 printf("-> %d ",i); particle->Print();
692 printf("--------------------------------------------------------------\n");
695 printf("-> %d Particle not loaded.\n",i);
696 printf("--------------------------------------------------------------\n");
700 "\n=======================================================================\n\n");
707 //_____________________________________________________________________________
708 void AliStack::CleanParents()
711 // Clean particles stack
712 // Set parent/daughter relations
715 TObjArray &particles = *fParticleMap;
718 for(i=0; i<fHgwmk+1; i++) {
719 part = (TParticle *)particles.At(i);
720 if(part) if(!part->TestBit(kDaughtersBit)) {
721 part->SetFirstDaughter(-1);
722 part->SetLastDaughter(-1);
727 //_____________________________________________________________________________
728 TParticle* AliStack::GetNextParticle()
731 // Return next particle from stack of particles
734 TParticle* particle = 0;
736 // search secondaries
737 //for(Int_t i=fNtrack-1; i>=0; i--) {
738 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
739 particle = (TParticle*) fParticleMap->At(i);
740 if ((particle) && (!particle->TestBit(kDoneBit))) {
742 //cout << "GetNextParticle() - secondary "
743 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
748 // take next primary if all secondaries were done
749 while (fCurrentPrimary>=0) {
750 fCurrent = fCurrentPrimary;
751 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
752 if ((particle) && (!particle->TestBit(kDoneBit))) {
753 //cout << "GetNextParticle() - primary "
754 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
759 // nothing to be tracked
761 //cout << "GetNextParticle() - none "
762 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
766 //__________________________________________________________________________________________
767 void AliStack::MakeTree(Int_t event, const char *file)
770 // Make Kine tree and creates branch for writing particles
773 // Make Kinematics Tree
775 // printf("\n MakeTree called %d", event);
777 sprintf(hname,"TreeK%d",event);
778 fTreeK = new TTree(hname,"Kinematics");
779 // Create a branch for particles
780 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
781 fTreeK->Write(0,TObject::kOverwrite);
785 //_____________________________________________________________________________
786 void AliStack::BeginEvent(Int_t event)
797 sprintf(hname,"TreeK%d",event);
798 fTreeK->SetName(hname);
802 //_____________________________________________________________________________
803 void AliStack::FinishRun()
805 // Clean TreeK information
807 delete fTreeK; fTreeK = 0;
811 //_____________________________________________________________________________
812 Bool_t AliStack::GetEvent(Int_t event)
815 // Get new event from TreeK
817 // Reset/Create the particle stack
818 if (fTreeK) delete fTreeK;
820 // Get Kine Tree from file
822 sprintf(treeName,"TreeK%d",event);
823 fTreeK = (TTree*)gDirectory->Get(treeName);
826 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
828 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
831 // printf("\n primaries %d", fNprimary);
832 // printf("\n tracks %d", fNtrack);
834 Int_t size = (Int_t)fTreeK->GetEntries();
839 //----------------------------------------------------------------------