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.21 2002/05/28 14:24:57 hristov
19 Correct warning messages
21 Revision 1.20 2002/04/30 11:47:30 morsch
22 KeepPhysics method called by PurifyKine added (N. Carrer, A.M.)
24 Revision 1.19 2002/03/12 11:06:03 morsch
25 Add particle status code to argument list of SetTrack(..).
27 Revision 1.18 2002/02/20 16:14:41 hristov
28 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
30 Revision 1.17 2001/12/05 08:51:56 hristov
31 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
33 Revision 1.16 2001/11/20 09:27:55 hristov
34 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
36 Revision 1.15 2001/09/04 15:10:37 hristov
37 Additional protection is included to avoid some problems using Hijing
39 Revision 1.14 2001/08/30 09:44:06 hristov
40 VertexSource_t added to avoid the warnings
42 Revision 1.13 2001/08/29 13:31:42 morsch
43 Protection against (fTreeK == 0) in destructor.
45 Revision 1.12 2001/07/27 13:03:13 hristov
46 Default Branch split level set to 99
48 Revision 1.11 2001/07/27 12:34:20 jchudoba
49 remove the dummy argument in GetEvent method
51 Revision 1.10 2001/07/20 10:13:54 morsch
52 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
54 Revision 1.9 2001/07/03 08:10:57 hristov
55 J.Chudoba's changes merged correctly with the HEAD
57 Revision 1.6 2001/05/31 06:59:06 fca
58 Clean setting and deleting of fParticleBuffer
60 Revision 1.5 2001/05/30 12:18:46 hristov
61 Loop variables declared once
63 Revision 1.4 2001/05/25 07:25:20 hristov
64 AliStack destructor corrected (I.Hrivnacova)
66 Revision 1.3 2001/05/22 14:33:16 hristov
69 Revision 1.2 2001/05/17 05:49:39 fca
70 Reset pointers to daughters
72 Revision 1.1 2001/05/16 14:57:22 alibrary
73 New files for folders and Stack
77 ///////////////////////////////////////////////////////////////////////////////
79 // Particles stack class
81 ///////////////////////////////////////////////////////////////////////////////
85 #include <TObjArray.h>
86 #include <TParticle.h>
94 #include "AliModule.h"
99 //_____________________________________________________________________________
100 AliStack::AliStack(Int_t size)
106 // Create the particles arrays
107 fParticles = new TClonesArray("TParticle",1000);
108 fParticleMap = new TObjArray(size);
113 fCurrentPrimary = -1;
118 //_____________________________________________________________________________
122 // Default constructor
125 // Create the particles arrays
132 fCurrentPrimary = -1;
137 //_____________________________________________________________________________
138 AliStack::~AliStack()
145 fParticles->Delete();
149 if (fTreeK) delete fTreeK;
156 //_____________________________________________________________________________
157 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
158 Float_t *vpos, Float_t *polar, Float_t tof,
159 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
162 // Load a track on the stack
164 // done 0 if the track has to be transported
166 // parent identifier of the parent track. -1 for a primary
168 // pmom momentum GeV/c
170 // polar polarisation
171 // tof time of flight in seconds
172 // mecha production mechanism
173 // ntr on output the number of the track stored
177 const Int_t kfirstdaughter=-1;
178 const Int_t klastdaughter=-1;
179 // const Float_t tlife=0;
182 // Here we get the static mass
183 // For MC is ok, but a more sophisticated method could be necessary
184 // if the calculated mass is required
185 // also, this method is potentially dangerous if the mass
186 // used in the MC is not the same of the PDG database
188 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
189 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
190 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
192 // 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",
193 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
195 TClonesArray &particles = *fParticles;
197 = new(particles[fLoadPoint++])
198 TParticle(pdg, is, parent, -1, kfirstdaughter, klastdaughter,
199 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
200 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
201 particle->SetWeight(weight);
202 particle->SetUniqueID(mech);
203 if(!done) particle->SetBit(kDoneBit);
206 // Declare that the daughter information is valid
207 particle->SetBit(kDaughtersBit);
208 // Add the particle to the stack
209 fParticleMap->AddAtAndExpand(particle, fNtrack);
212 particle = (TParticle*) fParticleMap->At(parent);
214 particle->SetLastDaughter(fNtrack);
215 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
218 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
223 // This is a primary track. Set high water mark for this event
226 // Set also number if primary tracks
227 fNprimary = fHgwmk+1;
233 //_____________________________________________________________________________
234 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
235 Double_t px, Double_t py, Double_t pz, Double_t e,
236 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
237 Double_t polx, Double_t poly, Double_t polz,
238 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
241 // Load a track on the stack
243 // done 0 if the track has to be transported
245 // parent identifier of the parent track. -1 for a primary
247 // kS generation status code
248 // px, py, pz momentum GeV/c
249 // vx, vy, vz position
250 // polar polarisation
251 // tof time of flight in seconds
252 // mech production mechanism
253 // ntr on output the number of the track stored
255 // New method interface:
256 // arguments were changed to be in correspondence with TParticle
258 // Note: the energy is not calculated from the static mass but
259 // it is passed by argument e.
262 const Int_t kFirstDaughter=-1;
263 const Int_t kLastDaughter=-1;
265 TClonesArray &particles = *fParticles;
267 = new(particles[fLoadPoint++])
268 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
269 px, py, pz, e, vx, vy, vz, tof);
271 particle->SetPolarisation(polx, poly, polz);
272 particle->SetWeight(weight);
273 particle->SetUniqueID(mech);
275 if(!done) particle->SetBit(kDoneBit);
277 // Declare that the daughter information is valid
278 particle->SetBit(kDaughtersBit);
279 // Add the particle to the stack
280 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
283 particle = (TParticle*) fParticleMap->At(parent);
285 particle->SetLastDaughter(fNtrack);
286 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
289 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
294 // This is a primary track. Set high water mark for this event
297 // Set also number if primary tracks
298 fNprimary = fHgwmk+1;
304 //_____________________________________________________________________________
305 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
306 Float_t &e, Float_t *vpos, Float_t *polar,
310 // Return next track from stack of particles
314 TParticle* track = GetNextParticle();
315 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
319 ipart=track->GetPdgCode();
328 track->GetPolarisation(pol);
333 track->SetBit(kDoneBit);
334 // cout << "Filled params" << endl;
340 // stop and start timer when we start a primary track
341 Int_t nprimaries = fNprimary;
342 if (fCurrent >= nprimaries) return;
343 if (fCurrent < nprimaries-1) {
345 track=(TParticle*) fParticleMap->At(fCurrent+1);
346 // track->SetProcessTime(fTimer.CpuTime());
352 //_____________________________________________________________________________
353 void AliStack::PurifyKine()
356 // Compress kinematic tree keeping only flagged particles
357 // and renaming the particle id's in all the hits
360 TObjArray &particles = *fParticleMap;
361 int nkeep=fHgwmk+1, parent, i;
362 TParticle *part, *father;
363 TArrayI map(particles.GetLast()+1);
365 // Save in Header total number of tracks before compression
367 // If no tracks generated return now
368 if(fHgwmk+1 == fNtrack) return;
370 // First pass, invalid Daughter information
371 for(i=0; i<fNtrack; i++) {
372 // Preset map, to be removed later
373 if(i<=fHgwmk) map[i]=i ;
376 if((part=(TParticle*) particles.At(i))) {
378 // Check of this track should be kept for physics reasons
379 if (KeepPhysics(part)) KeepTrack(i);
381 part->ResetBit(kDaughtersBit);
382 part->SetFirstDaughter(-1);
383 part->SetLastDaughter(-1);
387 // Invalid daughter information for the parent of the first particle
388 // generated. This may or may not be the current primary according to
389 // whether decays have been recorded among the primaries
390 part = (TParticle *)particles.At(fHgwmk+1);
391 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
392 // Second pass, build map between old and new numbering
393 for(i=fHgwmk+1; i<fNtrack; i++) {
394 if(particles.At(i)->TestBit(kKeepBit)) {
396 // This particle has to be kept
398 // If old and new are different, have to move the pointer
399 if(i!=nkeep) particles[nkeep]=particles.At(i);
400 part = (TParticle*) particles.At(nkeep);
402 // as the parent is always *before*, it must be already
403 // in place. This is what we are checking anyway!
404 if((parent=part->GetFirstMother())>fHgwmk)
405 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
406 else part->SetFirstMother(map[parent]);
412 // Fix daughters information
413 for (i=fHgwmk+1; i<nkeep; i++) {
414 part = (TParticle *)particles.At(i);
415 parent = part->GetFirstMother();
417 father = (TParticle *)particles.At(parent);
418 if(father->TestBit(kDaughtersBit)) {
420 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
421 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
423 // Initialise daughters info for first pass
424 father->SetFirstDaughter(i);
425 father->SetLastDaughter(i);
426 father->SetBit(kDaughtersBit);
431 // Now loop on all registered hit lists
432 TList* hitLists = gAlice->GetHitLists();
433 TIter next(hitLists);
434 TCollection *hitList;
435 while((hitList = (TCollection*)next())) {
436 TIter nexthit(hitList);
438 while((hit = (AliHit*)nexthit())) {
439 hit->SetTrack(map[hit->GetTrack()]);
444 // This for detectors which have a special mapping mechanism
445 // for hits, such as TPC and TRD
448 TObjArray* modules = gAlice->Modules();
449 TIter nextmod(modules);
451 while((detector = (AliModule*)nextmod())) {
452 detector->RemapTrackHitIDs(map.GetArray());
453 detector->RemapTrackReferencesIDs(map.GetArray());
456 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
457 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
459 for (i=fHgwmk+1; i<nkeep; ++i) {
460 fParticleBuffer = (TParticle*) particles.At(i);
461 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
463 particles[i]=fParticleBuffer=0;
466 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
468 Int_t toshrink = fNtrack-fHgwmk-1;
469 fLoadPoint-=toshrink;
470 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
477 Bool_t AliStack::KeepPhysics(TParticle* part)
480 // Some particles have to kept on the stack for reasons motivated
481 // by physics analysis. Decision is put here.
483 Bool_t keep = kFALSE;
485 // Keep first-generation daughter from primaries with heavy flavor
487 Int_t parent = part->GetFirstMother();
488 if (parent >= 0 && parent <= fHgwmk) {
489 TParticle* father = (TParticle*) Particles()->At(parent);
490 Int_t kf = father->GetPdgCode();
494 if (kfl > 10) kfl/=100;
496 if (kfl > 10) kfl/=10;
497 if (kfl > 10) kfl/=10;
505 //_____________________________________________________________________________
506 void AliStack::FinishEvent()
509 // Write out the kinematics that was not yet filled
512 // Update event header
516 // Fatal("FinishEvent", "No kinematics tree is defined.");
517 // Don't panic this is a probably a lego run
523 if(fTreeK->GetEntries() ==0) {
524 // set the fParticleFileMap size for the first time
525 fParticleFileMap.Set(fHgwmk+1);
528 Bool_t allFilled = kFALSE;
530 for(Int_t i=0; i<fHgwmk+1; ++i)
531 if((part=fParticleMap->At(i))) {
532 fParticleBuffer = (TParticle*) part;
533 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
535 //PH (*fParticleMap)[i]=fParticleBuffer=0;
537 fParticleMap->AddAt(0,i);
539 // When all primaries were filled no particle!=0
540 // should be left => to be removed later.
541 if (allFilled) printf("Why != 0 part # %d?\n",i);
544 // // printf("Why = 0 part # %d?\n",i); => We know.
546 // we don't break now in order to be sure there is no
547 // particle !=0 left.
548 // To be removed later and replaced with break.
549 if(!allFilled) allFilled = kTRUE;
551 // cout << "Nof particles: " << fNtrack << endl;
555 //_____________________________________________________________________________
556 void AliStack::FlagTrack(Int_t track)
559 // Flags a track and all its family tree to be kept
566 particle=(TParticle*)fParticleMap->At(curr);
568 // If the particle is flagged the three from here upward is saved already
569 if(particle->TestBit(kKeepBit)) return;
571 // Save this particle
572 particle->SetBit(kKeepBit);
574 // Move to father if any
575 if((curr=particle->GetFirstMother())==-1) return;
579 //_____________________________________________________________________________
580 void AliStack::KeepTrack(Int_t track)
583 // Flags a track to be kept
586 fParticleMap->At(track)->SetBit(kKeepBit);
589 //_____________________________________________________________________________
590 void AliStack::Reset(Int_t size)
604 //_____________________________________________________________________________
605 void AliStack::ResetArrays(Int_t size)
608 // Resets stack arrays
614 fParticles = new TClonesArray("TParticle",1000);
616 fParticleMap->Clear();
617 if (size>0) fParticleMap->Expand(size);}
619 fParticleMap = new TObjArray(size);
622 //_____________________________________________________________________________
623 void AliStack::SetHighWaterMark(Int_t nt)
626 // Set high water mark for last track in event
630 fCurrentPrimary=fHgwmk;
632 // Set also number of primary tracks
633 fNprimary = fHgwmk+1;
637 //_____________________________________________________________________________
638 TParticle* AliStack::Particle(Int_t i)
641 // Return particle with specified ID
643 //PH if(!(*fParticleMap)[i]) {
644 if(!fParticleMap->At(i)) {
645 Int_t nentries = fParticles->GetEntriesFast();
646 // algorithmic way of getting entry index
647 // (primary particles are filled after secondaries)
650 entry = i+fNtrack-fNprimary;
653 // check whether algorithmic way and
654 // and the fParticleFileMap[i] give the same;
655 // give the fatal error if not
656 if (entry != fParticleFileMap[i]) {
658 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
659 entry, fParticleFileMap[i]);
662 fTreeK->GetEntry(entry);
663 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
664 fParticleMap->AddAt((*fParticles)[nentries],i);
666 //PH return (TParticle *) (*fParticleMap)[i];
667 return (TParticle *) fParticleMap->At(i);
670 //_____________________________________________________________________________
671 Int_t AliStack::GetPrimary(Int_t id)
674 // Return number of primary that has generated track
682 parent=Particle(current)->GetFirstMother();
683 if(parent<0) return current;
687 //_____________________________________________________________________________
688 void AliStack::DumpPart (Int_t i) const
691 // Dumps particle i in the stack
694 //PH ((TParticle*) (*fParticleMap)[i])->Print();
695 ((TParticle*) fParticleMap->At(i))->Print();
698 //_____________________________________________________________________________
699 void AliStack::DumpPStack ()
702 // Dumps the particle stack
708 "\n\n=======================================================================\n");
709 for (i=0;i<fNtrack;i++)
711 TParticle* particle = Particle(i);
713 printf("-> %d ",i); particle->Print();
714 printf("--------------------------------------------------------------\n");
717 Warning("DumpPStack", "No particle with id %d.", i);
721 "\n=======================================================================\n\n");
723 // print particle file map
724 printf("\nParticle file map: \n");
725 for (i=0; i<fNtrack; i++)
726 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
730 //_____________________________________________________________________________
731 void AliStack::DumpLoadedStack() const
734 // Dumps the particle in the stack
735 // that are loaded in memory.
738 TObjArray &particles = *fParticleMap;
740 "\n\n=======================================================================\n");
741 for (Int_t i=0;i<fNtrack;i++)
743 TParticle* particle = (TParticle*) particles[i];
745 printf("-> %d ",i); particle->Print();
746 printf("--------------------------------------------------------------\n");
749 printf("-> %d Particle not loaded.\n",i);
750 printf("--------------------------------------------------------------\n");
754 "\n=======================================================================\n\n");
761 //_____________________________________________________________________________
762 void AliStack::CleanParents()
765 // Clean particles stack
766 // Set parent/daughter relations
769 TObjArray &particles = *fParticleMap;
772 for(i=0; i<fHgwmk+1; i++) {
773 part = (TParticle *)particles.At(i);
774 if(part) if(!part->TestBit(kDaughtersBit)) {
775 part->SetFirstDaughter(-1);
776 part->SetLastDaughter(-1);
781 //_____________________________________________________________________________
782 TParticle* AliStack::GetNextParticle()
785 // Return next particle from stack of particles
788 TParticle* particle = 0;
790 // search secondaries
791 //for(Int_t i=fNtrack-1; i>=0; i--) {
792 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
793 particle = (TParticle*) fParticleMap->At(i);
794 if ((particle) && (!particle->TestBit(kDoneBit))) {
796 //cout << "GetNextParticle() - secondary "
797 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
802 // take next primary if all secondaries were done
803 while (fCurrentPrimary>=0) {
804 fCurrent = fCurrentPrimary;
805 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
806 if ((particle) && (!particle->TestBit(kDoneBit))) {
807 //cout << "GetNextParticle() - primary "
808 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
813 // nothing to be tracked
815 //cout << "GetNextParticle() - none "
816 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
820 //__________________________________________________________________________________________
821 void AliStack::MakeTree(Int_t event, const char *file)
824 // Make Kine tree and creates branch for writing particles
827 // Make Kinematics Tree
830 sprintf(hname,"TreeK%d",event);
831 fTreeK = new TTree(hname,"Kinematics");
832 // Create a branch for particles
833 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
834 fTreeK->Write(0,TObject::kOverwrite);
838 //_____________________________________________________________________________
839 void AliStack::BeginEvent(Int_t event)
850 sprintf(hname,"TreeK%d",event);
851 fTreeK->SetName(hname);
855 //_____________________________________________________________________________
856 void AliStack::FinishRun()
858 // Clean TreeK information
860 delete fTreeK; fTreeK = 0;
864 Bool_t AliStack::GetEvent(Int_t event)
867 // Get new event from TreeK
869 // Reset/Create the particle stack
870 if (fTreeK) delete fTreeK;
872 // Get Kine Tree from file
874 sprintf(treeName,"TreeK%d",event);
875 fTreeK = (TTree*)gDirectory->Get(treeName);
878 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
880 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
881 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
884 // printf("\n primaries %d", fNprimary);
885 // printf("\n tracks %d", fNtrack);
887 Int_t size = (Int_t)fTreeK->GetEntries();