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.20 2002/04/30 11:47:30 morsch
19 KeepPhysics method called by PurifyKine added (N. Carrer, A.M.)
21 Revision 1.19 2002/03/12 11:06:03 morsch
22 Add particle status code to argument list of SetTrack(..).
24 Revision 1.18 2002/02/20 16:14:41 hristov
25 fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
27 Revision 1.17 2001/12/05 08:51:56 hristov
28 The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
30 Revision 1.16 2001/11/20 09:27:55 hristov
31 Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
33 Revision 1.15 2001/09/04 15:10:37 hristov
34 Additional protection is included to avoid some problems using Hijing
36 Revision 1.14 2001/08/30 09:44:06 hristov
37 VertexSource_t added to avoid the warnings
39 Revision 1.13 2001/08/29 13:31:42 morsch
40 Protection against (fTreeK == 0) in destructor.
42 Revision 1.12 2001/07/27 13:03:13 hristov
43 Default Branch split level set to 99
45 Revision 1.11 2001/07/27 12:34:20 jchudoba
46 remove the dummy argument in GetEvent method
48 Revision 1.10 2001/07/20 10:13:54 morsch
49 In Particle(Int_t) use GetEntriesFast to speed up the procedure.
51 Revision 1.9 2001/07/03 08:10:57 hristov
52 J.Chudoba's changes merged correctly with the HEAD
54 Revision 1.6 2001/05/31 06:59:06 fca
55 Clean setting and deleting of fParticleBuffer
57 Revision 1.5 2001/05/30 12:18:46 hristov
58 Loop variables declared once
60 Revision 1.4 2001/05/25 07:25:20 hristov
61 AliStack destructor corrected (I.Hrivnacova)
63 Revision 1.3 2001/05/22 14:33:16 hristov
66 Revision 1.2 2001/05/17 05:49:39 fca
67 Reset pointers to daughters
69 Revision 1.1 2001/05/16 14:57:22 alibrary
70 New files for folders and Stack
74 ///////////////////////////////////////////////////////////////////////////////
76 // Particles stack class
78 ///////////////////////////////////////////////////////////////////////////////
82 #include <TObjArray.h>
83 #include <TParticle.h>
91 #include "AliModule.h"
96 //_____________________________________________________________________________
97 AliStack::AliStack(Int_t size)
103 // Create the particles arrays
104 fParticles = new TClonesArray("TParticle",1000);
105 fParticleMap = new TObjArray(size);
110 fCurrentPrimary = -1;
115 //_____________________________________________________________________________
119 // Default constructor
122 // Create the particles arrays
129 fCurrentPrimary = -1;
134 //_____________________________________________________________________________
135 AliStack::~AliStack()
142 fParticles->Delete();
146 if (fTreeK) delete fTreeK;
153 //_____________________________________________________________________________
154 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
155 Float_t *vpos, Float_t *polar, Float_t tof,
156 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
159 // Load a track on the stack
161 // done 0 if the track has to be transported
163 // parent identifier of the parent track. -1 for a primary
165 // pmom momentum GeV/c
167 // polar polarisation
168 // tof time of flight in seconds
169 // mecha production mechanism
170 // ntr on output the number of the track stored
174 const Int_t kfirstdaughter=-1;
175 const Int_t klastdaughter=-1;
176 // const Float_t tlife=0;
179 // Here we get the static mass
180 // For MC is ok, but a more sophisticated method could be necessary
181 // if the calculated mass is required
182 // also, this method is potentially dangerous if the mass
183 // used in the MC is not the same of the PDG database
185 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
186 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
187 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
189 // 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",
190 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
192 TClonesArray &particles = *fParticles;
194 = new(particles[fLoadPoint++])
195 TParticle(pdg, is, parent, -1, kfirstdaughter, klastdaughter,
196 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
197 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
198 particle->SetWeight(weight);
199 particle->SetUniqueID(mech);
200 if(!done) particle->SetBit(kDoneBit);
203 // Declare that the daughter information is valid
204 particle->SetBit(kDaughtersBit);
205 // Add the particle to the stack
206 fParticleMap->AddAtAndExpand(particle, fNtrack);
209 particle = (TParticle*) fParticleMap->At(parent);
211 particle->SetLastDaughter(fNtrack);
212 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
215 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
220 // This is a primary track. Set high water mark for this event
223 // Set also number if primary tracks
224 fNprimary = fHgwmk+1;
230 //_____________________________________________________________________________
231 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
232 Double_t px, Double_t py, Double_t pz, Double_t e,
233 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
234 Double_t polx, Double_t poly, Double_t polz,
235 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
238 // Load a track on the stack
240 // done 0 if the track has to be transported
242 // parent identifier of the parent track. -1 for a primary
244 // kS generation status code
245 // px, py, pz momentum GeV/c
246 // vx, vy, vz position
247 // polar polarisation
248 // tof time of flight in seconds
249 // mech production mechanism
250 // ntr on output the number of the track stored
252 // New method interface:
253 // arguments were changed to be in correspondence with TParticle
255 // Note: the energy is not calculated from the static mass but
256 // it is passed by argument e.
259 const Int_t kFirstDaughter=-1;
260 const Int_t kLastDaughter=-1;
262 TClonesArray &particles = *fParticles;
264 = new(particles[fLoadPoint++])
265 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
266 px, py, pz, e, vx, vy, vz, tof);
268 particle->SetPolarisation(polx, poly, polz);
269 particle->SetWeight(weight);
270 particle->SetUniqueID(mech);
272 if(!done) particle->SetBit(kDoneBit);
274 // Declare that the daughter information is valid
275 particle->SetBit(kDaughtersBit);
276 // Add the particle to the stack
277 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
280 particle = (TParticle*) fParticleMap->At(parent);
282 particle->SetLastDaughter(fNtrack);
283 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
286 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
291 // This is a primary track. Set high water mark for this event
294 // Set also number if primary tracks
295 fNprimary = fHgwmk+1;
301 //_____________________________________________________________________________
302 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
303 Float_t &e, Float_t *vpos, Float_t *polar,
307 // Return next track from stack of particles
311 TParticle* track = GetNextParticle();
312 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
316 ipart=track->GetPdgCode();
325 track->GetPolarisation(pol);
330 track->SetBit(kDoneBit);
331 // cout << "Filled params" << endl;
337 // stop and start timer when we start a primary track
338 Int_t nprimaries = fNprimary;
339 if (fCurrent >= nprimaries) return;
340 if (fCurrent < nprimaries-1) {
342 track=(TParticle*) fParticleMap->At(fCurrent+1);
343 // track->SetProcessTime(fTimer.CpuTime());
349 //_____________________________________________________________________________
350 void AliStack::PurifyKine()
353 // Compress kinematic tree keeping only flagged particles
354 // and renaming the particle id's in all the hits
357 TObjArray &particles = *fParticleMap;
358 int nkeep=fHgwmk+1, parent, i;
359 TParticle *part, *father;
360 TArrayI map(particles.GetLast()+1);
362 // Save in Header total number of tracks before compression
364 // If no tracks generated return now
365 if(fHgwmk+1 == fNtrack) return;
367 // First pass, invalid Daughter information
368 for(i=0; i<fNtrack; i++) {
369 // Preset map, to be removed later
370 if(i<=fHgwmk) map[i]=i ;
373 if((part=(TParticle*) particles.At(i))) {
375 // Check of this track should be kept for physics reasons
376 if (KeepPhysics(part)) KeepTrack(i);
378 part->ResetBit(kDaughtersBit);
379 part->SetFirstDaughter(-1);
380 part->SetLastDaughter(-1);
384 // Invalid daughter information for the parent of the first particle
385 // generated. This may or may not be the current primary according to
386 // whether decays have been recorded among the primaries
387 part = (TParticle *)particles.At(fHgwmk+1);
388 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
389 // Second pass, build map between old and new numbering
390 for(i=fHgwmk+1; i<fNtrack; i++) {
391 if(particles.At(i)->TestBit(kKeepBit)) {
393 // This particle has to be kept
395 // If old and new are different, have to move the pointer
396 if(i!=nkeep) particles[nkeep]=particles.At(i);
397 part = (TParticle*) particles.At(nkeep);
399 // as the parent is always *before*, it must be already
400 // in place. This is what we are checking anyway!
401 if((parent=part->GetFirstMother())>fHgwmk)
402 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
403 else part->SetFirstMother(map[parent]);
409 // Fix daughters information
410 for (i=fHgwmk+1; i<nkeep; i++) {
411 part = (TParticle *)particles.At(i);
412 parent = part->GetFirstMother();
414 father = (TParticle *)particles.At(parent);
415 if(father->TestBit(kDaughtersBit)) {
417 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
418 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
420 // Initialise daughters info for first pass
421 father->SetFirstDaughter(i);
422 father->SetLastDaughter(i);
423 father->SetBit(kDaughtersBit);
428 // Now loop on all registered hit lists
429 TList* hitLists = gAlice->GetHitLists();
430 TIter next(hitLists);
431 TCollection *hitList;
432 while((hitList = (TCollection*)next())) {
433 TIter nexthit(hitList);
435 while((hit = (AliHit*)nexthit())) {
436 hit->SetTrack(map[hit->GetTrack()]);
441 // This for detectors which have a special mapping mechanism
442 // for hits, such as TPC and TRD
445 TObjArray* modules = gAlice->Modules();
446 TIter nextmod(modules);
448 while((detector = (AliModule*)nextmod())) {
449 detector->RemapTrackHitIDs(map.GetArray());
452 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
453 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
455 for (i=fHgwmk+1; i<nkeep; ++i) {
456 fParticleBuffer = (TParticle*) particles.At(i);
457 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
459 particles[i]=fParticleBuffer=0;
462 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
464 Int_t toshrink = fNtrack-fHgwmk-1;
465 fLoadPoint-=toshrink;
466 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
473 Bool_t AliStack::KeepPhysics(TParticle* part)
476 // Some particles have to kept on the stack for reasons motivated
477 // by physics analysis. Decision is put here.
479 Bool_t keep = kFALSE;
481 // Keep first-generation daughter from primaries with heavy flavor
483 Int_t parent = part->GetFirstMother();
484 if (parent >= 0 && parent <= fHgwmk) {
485 TParticle* father = (TParticle*) Particles()->At(parent);
486 Int_t kf = father->GetPdgCode();
490 if (kfl > 10) kfl/=100;
492 if (kfl > 10) kfl/=10;
493 if (kfl > 10) kfl/=10;
501 //_____________________________________________________________________________
502 void AliStack::FinishEvent()
505 // Write out the kinematics that was not yet filled
508 // Update event header
512 // Fatal("FinishEvent", "No kinematics tree is defined.");
513 // Don't panic this is a probably a lego run
519 if(fTreeK->GetEntries() ==0) {
520 // set the fParticleFileMap size for the first time
521 fParticleFileMap.Set(fHgwmk+1);
524 Bool_t allFilled = kFALSE;
526 for(Int_t i=0; i<fHgwmk+1; ++i)
527 if((part=fParticleMap->At(i))) {
528 fParticleBuffer = (TParticle*) part;
529 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
531 //PH (*fParticleMap)[i]=fParticleBuffer=0;
533 fParticleMap->AddAt(0,i);
535 // When all primaries were filled no particle!=0
536 // should be left => to be removed later.
537 if (allFilled) printf("Why != 0 part # %d?\n",i);
540 // // printf("Why = 0 part # %d?\n",i); => We know.
542 // we don't break now in order to be sure there is no
543 // particle !=0 left.
544 // To be removed later and replaced with break.
545 if(!allFilled) allFilled = kTRUE;
547 // cout << "Nof particles: " << fNtrack << endl;
551 //_____________________________________________________________________________
552 void AliStack::FlagTrack(Int_t track)
555 // Flags a track and all its family tree to be kept
562 particle=(TParticle*)fParticleMap->At(curr);
564 // If the particle is flagged the three from here upward is saved already
565 if(particle->TestBit(kKeepBit)) return;
567 // Save this particle
568 particle->SetBit(kKeepBit);
570 // Move to father if any
571 if((curr=particle->GetFirstMother())==-1) return;
575 //_____________________________________________________________________________
576 void AliStack::KeepTrack(Int_t track)
579 // Flags a track to be kept
582 fParticleMap->At(track)->SetBit(kKeepBit);
585 //_____________________________________________________________________________
586 void AliStack::Reset(Int_t size)
600 //_____________________________________________________________________________
601 void AliStack::ResetArrays(Int_t size)
604 // Resets stack arrays
610 fParticles = new TClonesArray("TParticle",1000);
612 fParticleMap->Clear();
613 if (size>0) fParticleMap->Expand(size);}
615 fParticleMap = new TObjArray(size);
618 //_____________________________________________________________________________
619 void AliStack::SetHighWaterMark(Int_t nt)
622 // Set high water mark for last track in event
626 fCurrentPrimary=fHgwmk;
628 // Set also number of primary tracks
629 fNprimary = fHgwmk+1;
633 //_____________________________________________________________________________
634 TParticle* AliStack::Particle(Int_t i)
637 // Return particle with specified ID
639 //PH if(!(*fParticleMap)[i]) {
640 if(!fParticleMap->At(i)) {
641 Int_t nentries = fParticles->GetEntriesFast();
642 // algorithmic way of getting entry index
643 // (primary particles are filled after secondaries)
646 entry = i+fNtrack-fNprimary;
649 // check whether algorithmic way and
650 // and the fParticleFileMap[i] give the same;
651 // give the fatal error if not
652 if (entry != fParticleFileMap[i]) {
654 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
655 entry, fParticleFileMap[i]);
658 fTreeK->GetEntry(entry);
659 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
660 fParticleMap->AddAt((*fParticles)[nentries],i);
662 //PH return (TParticle *) (*fParticleMap)[i];
663 return (TParticle *) fParticleMap->At(i);
666 //_____________________________________________________________________________
667 Int_t AliStack::GetPrimary(Int_t id)
670 // Return number of primary that has generated track
678 parent=Particle(current)->GetFirstMother();
679 if(parent<0) return current;
683 //_____________________________________________________________________________
684 void AliStack::DumpPart (Int_t i) const
687 // Dumps particle i in the stack
690 //PH ((TParticle*) (*fParticleMap)[i])->Print();
691 ((TParticle*) fParticleMap->At(i))->Print();
694 //_____________________________________________________________________________
695 void AliStack::DumpPStack ()
698 // Dumps the particle stack
704 "\n\n=======================================================================\n");
705 for (i=0;i<fNtrack;i++)
707 TParticle* particle = Particle(i);
709 printf("-> %d ",i); particle->Print();
710 printf("--------------------------------------------------------------\n");
713 Warning("DumpPStack", "No particle with id %d.", i);
717 "\n=======================================================================\n\n");
719 // print particle file map
720 printf("\nParticle file map: \n");
721 for (i=0; i<fNtrack; i++)
722 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
726 //_____________________________________________________________________________
727 void AliStack::DumpLoadedStack() const
730 // Dumps the particle in the stack
731 // that are loaded in memory.
734 TObjArray &particles = *fParticleMap;
736 "\n\n=======================================================================\n");
737 for (Int_t i=0;i<fNtrack;i++)
739 TParticle* particle = (TParticle*) particles[i];
741 printf("-> %d ",i); particle->Print();
742 printf("--------------------------------------------------------------\n");
745 printf("-> %d Particle not loaded.\n",i);
746 printf("--------------------------------------------------------------\n");
750 "\n=======================================================================\n\n");
757 //_____________________________________________________________________________
758 void AliStack::CleanParents()
761 // Clean particles stack
762 // Set parent/daughter relations
765 TObjArray &particles = *fParticleMap;
768 for(i=0; i<fHgwmk+1; i++) {
769 part = (TParticle *)particles.At(i);
770 if(part) if(!part->TestBit(kDaughtersBit)) {
771 part->SetFirstDaughter(-1);
772 part->SetLastDaughter(-1);
777 //_____________________________________________________________________________
778 TParticle* AliStack::GetNextParticle()
781 // Return next particle from stack of particles
784 TParticle* particle = 0;
786 // search secondaries
787 //for(Int_t i=fNtrack-1; i>=0; i--) {
788 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
789 particle = (TParticle*) fParticleMap->At(i);
790 if ((particle) && (!particle->TestBit(kDoneBit))) {
792 //cout << "GetNextParticle() - secondary "
793 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
798 // take next primary if all secondaries were done
799 while (fCurrentPrimary>=0) {
800 fCurrent = fCurrentPrimary;
801 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
802 if ((particle) && (!particle->TestBit(kDoneBit))) {
803 //cout << "GetNextParticle() - primary "
804 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
809 // nothing to be tracked
811 //cout << "GetNextParticle() - none "
812 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
816 //__________________________________________________________________________________________
817 void AliStack::MakeTree(Int_t event, const char *file)
820 // Make Kine tree and creates branch for writing particles
823 // Make Kinematics Tree
826 sprintf(hname,"TreeK%d",event);
827 fTreeK = new TTree(hname,"Kinematics");
828 // Create a branch for particles
829 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
830 fTreeK->Write(0,TObject::kOverwrite);
834 //_____________________________________________________________________________
835 void AliStack::BeginEvent(Int_t event)
846 sprintf(hname,"TreeK%d",event);
847 fTreeK->SetName(hname);
851 //_____________________________________________________________________________
852 void AliStack::FinishRun()
854 // Clean TreeK information
856 delete fTreeK; fTreeK = 0;
860 Bool_t AliStack::GetEvent(Int_t event)
863 // Get new event from TreeK
865 // Reset/Create the particle stack
866 if (fTreeK) delete fTreeK;
868 // Get Kine Tree from file
870 sprintf(treeName,"TreeK%d",event);
871 fTreeK = (TTree*)gDirectory->Get(treeName);
874 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
876 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
877 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
880 // printf("\n primaries %d", fNprimary);
881 // printf("\n tracks %d", fNtrack);
883 Int_t size = (Int_t)fTreeK->GetEntries();