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.18 2002/02/20 16:14:41 hristov
19 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
21 Revision 1.17 2001/12/05 08:51:56 hristov
22 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
24 Revision 1.16 2001/11/20 09:27:55 hristov
25 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
27 Revision 1.15 2001/09/04 15:10:37 hristov
28 Additional protection is included to avoid some problems using Hijing
30 Revision 1.14 2001/08/30 09:44:06 hristov
31 VertexSource_t added to avoid the warnings
33 Revision 1.13 2001/08/29 13:31:42 morsch
34 Protection against (fTreeK == 0) in destructor.
36 Revision 1.12 2001/07/27 13:03:13 hristov
37 Default Branch split level set to 99
39 Revision 1.11 2001/07/27 12:34:20 jchudoba
40 remove the dummy argument in GetEvent method
42 Revision 1.10 2001/07/20 10:13:54 morsch
43 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
45 Revision 1.9 2001/07/03 08:10:57 hristov
46 J.Chudoba's changes merged correctly with the HEAD
48 Revision 1.6 2001/05/31 06:59:06 fca
49 Clean setting and deleting of fParticleBuffer
51 Revision 1.5 2001/05/30 12:18:46 hristov
52 Loop variables declared once
54 Revision 1.4 2001/05/25 07:25:20 hristov
55 AliStack destructor corrected (I.Hrivnacova)
57 Revision 1.3 2001/05/22 14:33:16 hristov
60 Revision 1.2 2001/05/17 05:49:39 fca
61 Reset pointers to daughters
63 Revision 1.1 2001/05/16 14:57:22 alibrary
64 New files for folders and Stack
68 ///////////////////////////////////////////////////////////////////////////////
70 // Particles stack class
72 ///////////////////////////////////////////////////////////////////////////////
76 #include <TObjArray.h>
77 #include <TParticle.h>
85 #include "AliModule.h"
87 //#include "ETrackBits.h"
91 //_____________________________________________________________________________
92 AliStack::AliStack(Int_t size)
98 // Create the particles arrays
99 fParticles = new TClonesArray("TParticle",1000);
100 fParticleMap = new TObjArray(size);
105 fCurrentPrimary = -1;
110 //_____________________________________________________________________________
114 // Default constructor
117 // Create the particles arrays
124 fCurrentPrimary = -1;
129 //_____________________________________________________________________________
130 AliStack::~AliStack()
137 fParticles->Delete();
141 if (fTreeK) delete fTreeK;
148 //_____________________________________________________________________________
149 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
150 Float_t *vpos, Float_t *polar, Float_t tof,
151 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
154 // Load a track on the stack
156 // done 0 if the track has to be transported
158 // parent identifier of the parent track. -1 for a primary
160 // pmom momentum GeV/c
162 // polar polarisation
163 // tof time of flight in seconds
164 // mecha production mechanism
165 // ntr on output the number of the track stored
169 const Int_t kfirstdaughter=-1;
170 const Int_t klastdaughter=-1;
171 // const Float_t tlife=0;
174 // Here we get the static mass
175 // For MC is ok, but a more sophisticated method could be necessary
176 // if the calculated mass is required
177 // also, this method is potentially dangerous if the mass
178 // used in the MC is not the same of the PDG database
180 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
181 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
182 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
184 // 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",
185 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
187 TClonesArray &particles = *fParticles;
189 = new(particles[fLoadPoint++])
190 TParticle(pdg, is, parent, -1, kfirstdaughter, klastdaughter,
191 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
192 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
193 particle->SetWeight(weight);
194 particle->SetUniqueID(mech);
195 if(!done) particle->SetBit(kDoneBit);
198 // Declare that the daughter information is valid
199 particle->SetBit(kDaughtersBit);
200 // Add the particle to the stack
201 fParticleMap->AddAtAndExpand(particle, fNtrack);
204 particle = (TParticle*) fParticleMap->At(parent);
206 particle->SetLastDaughter(fNtrack);
207 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
210 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
215 // This is a primary track. Set high water mark for this event
218 // Set also number if primary tracks
219 fNprimary = fHgwmk+1;
225 //_____________________________________________________________________________
226 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
227 Double_t px, Double_t py, Double_t pz, Double_t e,
228 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
229 Double_t polx, Double_t poly, Double_t polz,
230 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
233 // Load a track on the stack
235 // done 0 if the track has to be transported
237 // parent identifier of the parent track. -1 for a primary
239 // kS generation status code
240 // px, py, pz momentum GeV/c
241 // vx, vy, vz position
242 // polar polarisation
243 // tof time of flight in seconds
244 // mech production mechanism
245 // ntr on output the number of the track stored
247 // New method interface:
248 // arguments were changed to be in correspondence with TParticle
250 // Note: the energy is not calculated from the static mass but
251 // it is passed by argument e.
254 const Int_t kFirstDaughter=-1;
255 const Int_t kLastDaughter=-1;
257 TClonesArray &particles = *fParticles;
259 = new(particles[fLoadPoint++])
260 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
261 px, py, pz, e, vx, vy, vz, tof);
263 particle->SetPolarisation(polx, poly, polz);
264 particle->SetWeight(weight);
265 particle->SetUniqueID(mech);
267 if(!done) particle->SetBit(kDoneBit);
269 // Declare that the daughter information is valid
270 particle->SetBit(kDaughtersBit);
271 // Add the particle to the stack
272 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
275 particle = (TParticle*) fParticleMap->At(parent);
277 particle->SetLastDaughter(fNtrack);
278 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
281 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
286 // This is a primary track. Set high water mark for this event
289 // Set also number if primary tracks
290 fNprimary = fHgwmk+1;
296 //_____________________________________________________________________________
297 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
298 Float_t &e, Float_t *vpos, Float_t *polar,
302 // Return next track from stack of particles
306 TParticle* track = GetNextParticle();
307 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
311 ipart=track->GetPdgCode();
320 track->GetPolarisation(pol);
325 track->SetBit(kDoneBit);
326 // cout << "Filled params" << endl;
332 // stop and start timer when we start a primary track
333 Int_t nprimaries = fNprimary;
334 if (fCurrent >= nprimaries) return;
335 if (fCurrent < nprimaries-1) {
337 track=(TParticle*) fParticleMap->At(fCurrent+1);
338 // track->SetProcessTime(fTimer.CpuTime());
344 //_____________________________________________________________________________
345 void AliStack::PurifyKine()
348 // Compress kinematic tree keeping only flagged particles
349 // and renaming the particle id's in all the hits
352 TObjArray &particles = *fParticleMap;
353 int nkeep=fHgwmk+1, parent, i;
354 TParticle *part, *father;
355 TArrayI map(particles.GetLast()+1);
357 // Save in Header total number of tracks before compression
359 // If no tracks generated return now
360 if(fHgwmk+1 == fNtrack) return;
362 // First pass, invalid Daughter information
363 for(i=0; i<fNtrack; i++) {
364 // Preset map, to be removed later
365 if(i<=fHgwmk) map[i]=i ;
368 if((part=(TParticle*) particles.At(i))) {
369 part->ResetBit(kDaughtersBit);
370 part->SetFirstDaughter(-1);
371 part->SetLastDaughter(-1);
375 // Invalid daughter information for the parent of the first particle
376 // generated. This may or may not be the current primary according to
377 // whether decays have been recorded among the primaries
378 part = (TParticle *)particles.At(fHgwmk+1);
379 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
380 // Second pass, build map between old and new numbering
381 for(i=fHgwmk+1; i<fNtrack; i++) {
382 if(particles.At(i)->TestBit(kKeepBit)) {
384 // This particle has to be kept
386 // If old and new are different, have to move the pointer
387 if(i!=nkeep) particles[nkeep]=particles.At(i);
388 part = (TParticle*) particles.At(nkeep);
390 // as the parent is always *before*, it must be already
391 // in place. This is what we are checking anyway!
392 if((parent=part->GetFirstMother())>fHgwmk)
393 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
394 else part->SetFirstMother(map[parent]);
400 // Fix daughters information
401 for (i=fHgwmk+1; i<nkeep; i++) {
402 part = (TParticle *)particles.At(i);
403 parent = part->GetFirstMother();
405 father = (TParticle *)particles.At(parent);
406 if(father->TestBit(kDaughtersBit)) {
408 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
409 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
411 // Initialise daughters info for first pass
412 father->SetFirstDaughter(i);
413 father->SetLastDaughter(i);
414 father->SetBit(kDaughtersBit);
419 // Now loop on all registered hit lists
420 TList* hitLists = gAlice->GetHitLists();
421 TIter next(hitLists);
422 TCollection *hitList;
423 while((hitList = (TCollection*)next())) {
424 TIter nexthit(hitList);
426 while((hit = (AliHit*)nexthit())) {
427 hit->SetTrack(map[hit->GetTrack()]);
432 // This for detectors which have a special mapping mechanism
433 // for hits, such as TPC and TRD
436 TObjArray* modules = gAlice->Modules();
437 TIter nextmod(modules);
439 while((detector = (AliModule*)nextmod())) {
440 detector->RemapTrackHitIDs(map.GetArray());
443 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
444 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
446 for (i=fHgwmk+1; i<nkeep; ++i) {
447 fParticleBuffer = (TParticle*) particles.At(i);
448 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
450 particles[i]=fParticleBuffer=0;
453 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
455 Int_t toshrink = fNtrack-fHgwmk-1;
456 fLoadPoint-=toshrink;
457 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
464 //_____________________________________________________________________________
465 void AliStack::FinishEvent()
468 // Write out the kinematics that was not yet filled
471 // Update event header
475 // Fatal("FinishEvent", "No kinematics tree is defined.");
476 // Don't panic this is a probably a lego run
482 if(fTreeK->GetEntries() ==0) {
483 // set the fParticleFileMap size for the first time
484 fParticleFileMap.Set(fHgwmk+1);
487 Bool_t allFilled = kFALSE;
489 for(Int_t i=0; i<fHgwmk+1; ++i)
490 if((part=fParticleMap->At(i))) {
491 fParticleBuffer = (TParticle*) part;
492 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
494 //PH (*fParticleMap)[i]=fParticleBuffer=0;
496 fParticleMap->AddAt(0,i);
498 // When all primaries were filled no particle!=0
499 // should be left => to be removed later.
500 if (allFilled) printf("Why != 0 part # %d?\n",i);
503 // // printf("Why = 0 part # %d?\n",i); => We know.
505 // we don't break now in order to be sure there is no
506 // particle !=0 left.
507 // To be removed later and replaced with break.
508 if(!allFilled) allFilled = kTRUE;
510 // cout << "Nof particles: " << fNtrack << endl;
514 //_____________________________________________________________________________
515 void AliStack::FlagTrack(Int_t track)
518 // Flags a track and all its family tree to be kept
525 particle=(TParticle*)fParticleMap->At(curr);
527 // If the particle is flagged the three from here upward is saved already
528 if(particle->TestBit(kKeepBit)) return;
530 // Save this particle
531 particle->SetBit(kKeepBit);
533 // Move to father if any
534 if((curr=particle->GetFirstMother())==-1) return;
538 //_____________________________________________________________________________
539 void AliStack::KeepTrack(Int_t track)
542 // Flags a track to be kept
545 fParticleMap->At(track)->SetBit(kKeepBit);
548 //_____________________________________________________________________________
549 void AliStack::Reset(Int_t size)
563 //_____________________________________________________________________________
564 void AliStack::ResetArrays(Int_t size)
567 // Resets stack arrays
573 fParticles = new TClonesArray("TParticle",1000);
575 fParticleMap->Clear();
576 if (size>0) fParticleMap->Expand(size);}
578 fParticleMap = new TObjArray(size);
581 //_____________________________________________________________________________
582 void AliStack::SetHighWaterMark(Int_t nt)
585 // Set high water mark for last track in event
589 fCurrentPrimary=fHgwmk;
591 // Set also number of primary tracks
592 fNprimary = fHgwmk+1;
596 //_____________________________________________________________________________
597 TParticle* AliStack::Particle(Int_t i)
600 // Return particle with specified ID
602 //PH if(!(*fParticleMap)[i]) {
603 if(!fParticleMap->At(i)) {
604 Int_t nentries = fParticles->GetEntriesFast();
605 // algorithmic way of getting entry index
606 // (primary particles are filled after secondaries)
609 entry = i+fNtrack-fNprimary;
612 // check whether algorithmic way and
613 // and the fParticleFileMap[i] give the same;
614 // give the fatal error if not
615 if (entry != fParticleFileMap[i]) {
617 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
618 entry, fParticleFileMap[i]);
621 fTreeK->GetEntry(entry);
622 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
623 fParticleMap->AddAt((*fParticles)[nentries],i);
625 //PH return (TParticle *) (*fParticleMap)[i];
626 return (TParticle *) fParticleMap->At(i);
629 //_____________________________________________________________________________
630 Int_t AliStack::GetPrimary(Int_t id)
633 // Return number of primary that has generated track
641 parent=Particle(current)->GetFirstMother();
642 if(parent<0) return current;
646 //_____________________________________________________________________________
647 void AliStack::DumpPart (Int_t i) const
650 // Dumps particle i in the stack
653 //PH ((TParticle*) (*fParticleMap)[i])->Print();
654 ((TParticle*) fParticleMap->At(i))->Print();
657 //_____________________________________________________________________________
658 void AliStack::DumpPStack ()
661 // Dumps the particle stack
667 "\n\n=======================================================================\n");
668 for (i=0;i<fNtrack;i++)
670 TParticle* particle = Particle(i);
672 printf("-> %d ",i); particle->Print();
673 printf("--------------------------------------------------------------\n");
676 Warning("DumpPStack", "No particle with id %d.", i);
680 "\n=======================================================================\n\n");
682 // print particle file map
683 printf("\nParticle file map: \n");
684 for (i=0; i<fNtrack; i++)
685 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
689 //_____________________________________________________________________________
690 void AliStack::DumpLoadedStack() const
693 // Dumps the particle in the stack
694 // that are loaded in memory.
697 TObjArray &particles = *fParticleMap;
699 "\n\n=======================================================================\n");
700 for (Int_t i=0;i<fNtrack;i++)
702 TParticle* particle = (TParticle*) particles[i];
704 printf("-> %d ",i); particle->Print();
705 printf("--------------------------------------------------------------\n");
708 printf("-> %d Particle not loaded.\n",i);
709 printf("--------------------------------------------------------------\n");
713 "\n=======================================================================\n\n");
720 //_____________________________________________________________________________
721 void AliStack::CleanParents()
724 // Clean particles stack
725 // Set parent/daughter relations
728 TObjArray &particles = *fParticleMap;
731 for(i=0; i<fHgwmk+1; i++) {
732 part = (TParticle *)particles.At(i);
733 if(part) if(!part->TestBit(kDaughtersBit)) {
734 part->SetFirstDaughter(-1);
735 part->SetLastDaughter(-1);
740 //_____________________________________________________________________________
741 TParticle* AliStack::GetNextParticle()
744 // Return next particle from stack of particles
747 TParticle* particle = 0;
749 // search secondaries
750 //for(Int_t i=fNtrack-1; i>=0; i--) {
751 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
752 particle = (TParticle*) fParticleMap->At(i);
753 if ((particle) && (!particle->TestBit(kDoneBit))) {
755 //cout << "GetNextParticle() - secondary "
756 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
761 // take next primary if all secondaries were done
762 while (fCurrentPrimary>=0) {
763 fCurrent = fCurrentPrimary;
764 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
765 if ((particle) && (!particle->TestBit(kDoneBit))) {
766 //cout << "GetNextParticle() - primary "
767 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
772 // nothing to be tracked
774 //cout << "GetNextParticle() - none "
775 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
779 //__________________________________________________________________________________________
780 void AliStack::MakeTree(Int_t event, const char *file)
783 // Make Kine tree and creates branch for writing particles
786 // Make Kinematics Tree
788 // printf("\n MakeTree called %d", event);
790 sprintf(hname,"TreeK%d",event);
791 fTreeK = new TTree(hname,"Kinematics");
792 // Create a branch for particles
793 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
794 fTreeK->Write(0,TObject::kOverwrite);
798 //_____________________________________________________________________________
799 void AliStack::BeginEvent(Int_t event)
810 sprintf(hname,"TreeK%d",event);
811 fTreeK->SetName(hname);
815 //_____________________________________________________________________________
816 void AliStack::FinishRun()
818 // Clean TreeK information
820 delete fTreeK; fTreeK = 0;
824 //_____________________________________________________________________________
825 Bool_t AliStack::GetEvent(Int_t event)
828 // Get new event from TreeK
830 // Reset/Create the particle stack
831 if (fTreeK) delete fTreeK;
833 // Get Kine Tree from file
835 sprintf(treeName,"TreeK%d",event);
836 fTreeK = (TTree*)gDirectory->Get(treeName);
839 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
841 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
844 // printf("\n primaries %d", fNprimary);
845 // printf("\n tracks %d", fNtrack);
847 Int_t size = (Int_t)fTreeK->GetEntries();
852 //----------------------------------------------------------------------