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.5 2001/05/30 12:18:46 hristov
19 Loop variables declared once
21 Revision 1.4 2001/05/25 07:25:20 hristov
22 AliStack destructor corrected (I.Hrivnacova)
24 Revision 1.3 2001/05/22 14:33:16 hristov
27 Revision 1.2 2001/05/17 05:49:39 fca
28 Reset pointers to daughters
30 Revision 1.1 2001/05/16 14:57:22 alibrary
31 New files for folders and Stack
35 ///////////////////////////////////////////////////////////////////////////////
37 // Particles stack class
39 ///////////////////////////////////////////////////////////////////////////////
43 #include <TObjArray.h>
44 #include <TParticle.h>
52 #include "AliModule.h"
54 //#include "ETrackBits.h"
58 //_____________________________________________________________________________
59 AliStack::AliStack(Int_t size)
65 // Create the particles arrays
66 fParticles = new TClonesArray("TParticle",1000);
67 fParticleMap = new TObjArray(size);
77 //_____________________________________________________________________________
81 // Default constructor
84 // Create the particles arrays
85 fParticles = new TClonesArray("TParticle",1000);
86 fParticleMap = new TObjArray(10000);
96 //_____________________________________________________________________________
103 delete fParticleBuffer;
105 fParticles->Delete();
116 //_____________________________________________________________________________
117 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
118 Float_t *vpos, Float_t *polar, Float_t tof,
119 AliMCProcess mech, Int_t &ntr, Float_t weight)
122 // Load a track on the stack
124 // done 0 if the track has to be transported
126 // parent identifier of the parent track. -1 for a primary
128 // pmom momentum GeV/c
130 // polar polarisation
131 // tof time of flight in seconds
132 // mecha production mechanism
133 // ntr on output the number of the track stored
137 const Int_t kfirstdaughter=-1;
138 const Int_t klastdaughter=-1;
140 // const Float_t tlife=0;
143 // Here we get the static mass
144 // For MC is ok, but a more sophisticated method could be necessary
145 // if the calculated mass is required
146 // also, this method is potentially dangerous if the mass
147 // used in the MC is not the same of the PDG database
149 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
150 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
151 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
153 // 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",
154 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
156 TClonesArray &particles = *fParticles;
158 = new(particles[fLoadPoint++])
159 TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
160 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
161 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
162 particle->SetWeight(weight);
163 particle->SetUniqueID(mech);
164 if(!done) particle->SetBit(kDoneBit);
167 // Declare that the daughter information is valid
168 particle->SetBit(kDaughtersBit);
169 // Add the particle to the stack
170 fParticleMap->AddAtAndExpand(particle, fNtrack);
173 particle = (TParticle*) fParticleMap->At(parent);
174 particle->SetLastDaughter(fNtrack);
175 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
179 // This is a primary track. Set high water mark for this event
182 // Set also number if primary tracks
183 fNprimary = fHgwmk+1;
189 //_____________________________________________________________________________
190 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
191 Double_t px, Double_t py, Double_t pz, Double_t e,
192 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
193 Double_t polx, Double_t poly, Double_t polz,
194 AliMCProcess mech, Int_t &ntr, Float_t weight)
197 // Load a track on the stack
199 // done 0 if the track has to be transported
201 // parent identifier of the parent track. -1 for a primary
203 // kS generation status code
204 // px, py, pz momentum GeV/c
205 // vx, vy, vz position
206 // polar polarisation
207 // tof time of flight in seconds
208 // mech production mechanism
209 // ntr on output the number of the track stored
211 // New method interface:
212 // arguments were changed to be in correspondence with TParticle
214 // Note: the energy is not calculated from the static mass but
215 // it is passed by argument e.
219 const Int_t kFirstDaughter=-1;
220 const Int_t kLastDaughter=-1;
222 TClonesArray &particles = *fParticles;
224 = new(particles[fLoadPoint++])
225 TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
226 px, py, pz, e, vx, vy, vz, tof);
228 particle->SetPolarisation(polx, poly, polz);
229 particle->SetWeight(weight);
230 particle->SetUniqueID(mech);
232 if(!done) particle->SetBit(kDoneBit);
234 // Declare that the daughter information is valid
235 particle->SetBit(kDaughtersBit);
236 // Add the particle to the stack
237 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
240 particle = (TParticle*) fParticleMap->At(parent);
241 particle->SetLastDaughter(fNtrack);
242 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
246 // This is a primary track. Set high water mark for this event
249 // Set also number if primary tracks
250 fNprimary = fHgwmk+1;
256 //_____________________________________________________________________________
257 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
258 Float_t &e, Float_t *vpos, Float_t *polar,
262 // Return next track from stack of particles
266 TParticle* track = GetNextParticle();
267 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
271 ipart=track->GetPdgCode();
280 track->GetPolarisation(pol);
285 track->SetBit(kDoneBit);
286 //cout << "Filled params" << endl;
292 // stop and start timer when we start a primary track
293 Int_t nprimaries = fNprimary;
294 if (fCurrent >= nprimaries) return;
295 if (fCurrent < nprimaries-1) {
297 track=(TParticle*) fParticleMap->At(fCurrent+1);
298 // track->SetProcessTime(fTimer.CpuTime());
304 //_____________________________________________________________________________
305 void AliStack::PurifyKine()
308 // Compress kinematic tree keeping only flagged particles
309 // and renaming the particle id's in all the hits
312 TObjArray &particles = *fParticleMap;
313 int nkeep=fHgwmk+1, parent, i;
314 TParticle *part, *father;
315 TArrayI map(particles.GetLast()+1);
317 // Save in Header total number of tracks before compression
319 // If no tracks generated return now
320 if(fHgwmk+1 == fNtrack) return;
322 // First pass, invalid Daughter information
323 for(i=0; i<fNtrack; i++) {
324 // Preset map, to be removed later
325 if(i<=fHgwmk) map[i]=i ;
328 if((part=(TParticle*) particles.At(i))) {
329 part->ResetBit(kDaughtersBit);
330 part->SetFirstDaughter(-1);
331 part->SetLastDaughter(-1);
335 // Invalid daughter information for the parent of the first particle
336 // generated. This may or may not be the current primary according to
337 // whether decays have been recorded among the primaries
338 part = (TParticle *)particles.At(fHgwmk+1);
339 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
340 // Second pass, build map between old and new numbering
341 for(i=fHgwmk+1; i<fNtrack; i++) {
342 if(particles.At(i)->TestBit(kKeepBit)) {
344 // This particle has to be kept
346 // If old and new are different, have to move the pointer
347 if(i!=nkeep) particles[nkeep]=particles.At(i);
348 part = (TParticle*) particles.At(nkeep);
350 // as the parent is always *before*, it must be already
351 // in place. This is what we are checking anyway!
352 if((parent=part->GetFirstMother())>fHgwmk)
353 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
354 else part->SetFirstMother(map[parent]);
360 // Fix daughters information
361 for (i=fHgwmk+1; i<nkeep; i++) {
362 part = (TParticle *)particles.At(i);
363 parent = part->GetFirstMother();
365 father = (TParticle *)particles.At(parent);
366 if(father->TestBit(kDaughtersBit)) {
368 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
369 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
371 // Initialise daughters info for first pass
372 father->SetFirstDaughter(i);
373 father->SetLastDaughter(i);
374 father->SetBit(kDaughtersBit);
379 // Now loop on all registered hit lists
380 TList* hitLists = gAlice->GetHitLists();
381 TIter next(hitLists);
382 TCollection *hitList;
383 while((hitList = (TCollection*)next())) {
384 TIter nexthit(hitList);
386 while((hit = (AliHit*)nexthit())) {
387 hit->SetTrack(map[hit->GetTrack()]);
392 // This for detectors which have a special mapping mechanism
393 // for hits, such as TPC and TRD
396 TObjArray* modules = gAlice->Modules();
397 TIter nextmod(modules);
399 while((detector = (AliModule*)nextmod())) {
400 detector->RemapTrackHitIDs(map.GetArray());
403 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
404 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
406 for (i=fHgwmk+1; i<nkeep; ++i) {
407 fParticleBuffer = (TParticle*) particles.At(i);
408 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
410 particles[i]=fParticleBuffer=0;
413 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
415 Int_t toshrink = fNtrack-fHgwmk-1;
416 fLoadPoint-=toshrink;
417 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
424 //_____________________________________________________________________________
425 void AliStack::FinishEvent()
428 // Write out the kinematics that was not yet filled
431 // Update event header
435 // Fatal("FinishEvent", "No kinematics tree is defined.");
436 // Don't panic this is a probably a lego run
442 if(fTreeK->GetEntries() ==0) {
443 // set the fParticleFileMap size for the first time
444 fParticleFileMap.Set(fHgwmk+1);
447 Bool_t allFilled = kFALSE;
449 for(Int_t i=0; i<fHgwmk+1; ++i)
450 if((part=fParticleMap->At(i))) {
451 fParticleBuffer = (TParticle*) part;
452 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
454 (*fParticleMap)[i]=fParticleBuffer=0;
456 // When all primaries were filled no particle!=0
457 // should be left => to be removed later.
458 if (allFilled) printf("Why != 0 part # %d?\n",i);
461 // // printf("Why = 0 part # %d?\n",i); => We know.
463 // we don't break now in order to be sure there is no
464 // particle !=0 left.
465 // To be removed later and replaced with break.
466 if(!allFilled) allFilled = kTRUE;
468 //cout << "Nof particles: " << fNtrack << endl;
472 //_____________________________________________________________________________
473 void AliStack::FlagTrack(Int_t track)
476 // Flags a track and all its family tree to be kept
483 particle=(TParticle*)fParticleMap->At(curr);
485 // If the particle is flagged the three from here upward is saved already
486 if(particle->TestBit(kKeepBit)) return;
488 // Save this particle
489 particle->SetBit(kKeepBit);
491 // Move to father if any
492 if((curr=particle->GetFirstMother())==-1) return;
496 //_____________________________________________________________________________
497 void AliStack::KeepTrack(Int_t track)
500 // Flags a track to be kept
503 fParticleMap->At(track)->SetBit(kKeepBit);
506 //_____________________________________________________________________________
507 void AliStack::Reset(Int_t size)
521 //_____________________________________________________________________________
522 void AliStack::ResetArrays(Int_t size)
525 // Resets stack arrays
529 fParticleMap->Clear();
530 if (size>0) fParticleMap->Expand(size);
533 //_____________________________________________________________________________
534 void AliStack::SetHighWaterMark(Int_t nt)
537 // Set high water mark for last track in event
541 fCurrentPrimary=fHgwmk;
543 // Set also number of primary tracks
544 fNprimary = fHgwmk+1;
548 //_____________________________________________________________________________
549 TParticle* AliStack::Particle(Int_t i)
552 // Return particle with specified ID
554 if(!(*fParticleMap)[i]) {
555 Int_t nentries = fParticles->GetEntries();
556 // algorithmic way of getting entry index
557 // (primary particles are filled after secondaries)
560 entry = i+fNtrack-fNprimary;
563 // check whether algorithmic way and
564 // and the fParticleFileMap[i] give the same;
565 // give the fatal error if not
566 if (entry != fParticleFileMap[i]) {
568 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
569 entry, fParticleFileMap[i]);
572 fTreeK->GetEntry(entry);
573 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
574 fParticleMap->AddAt((*fParticles)[nentries],i);
576 return (TParticle *) (*fParticleMap)[i];
579 //_____________________________________________________________________________
580 Int_t AliStack::GetPrimary(Int_t id) const
583 // Return number of primary that has generated track
592 part = (TParticle *)fParticleMap->At(current);
593 parent=part->GetFirstMother();
594 if(parent<0) return current;
598 //_____________________________________________________________________________
599 void AliStack::DumpPart (Int_t i) const
602 // Dumps particle i in the stack
605 ((TParticle*) (*fParticleMap)[i])->Print();
608 //_____________________________________________________________________________
609 void AliStack::DumpPStack ()
612 // Dumps the particle stack
618 "\n\n=======================================================================\n");
619 for (i=0;i<fNtrack;i++)
621 TParticle* particle = Particle(i);
623 printf("-> %d ",i); particle->Print();
624 printf("--------------------------------------------------------------\n");
627 Warning("DumpPStack", "No particle with id %d.", i);
631 "\n=======================================================================\n\n");
633 // print particle file map
634 printf("\nParticle file map: \n");
635 for (i=0; i<fNtrack; i++)
636 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
640 //_____________________________________________________________________________
641 void AliStack::DumpLoadedStack() const
644 // Dumps the particle in the stack
645 // that are loaded in memory.
648 TObjArray &particles = *fParticleMap;
650 "\n\n=======================================================================\n");
651 for (Int_t i=0;i<fNtrack;i++)
653 TParticle* particle = (TParticle*) particles[i];
655 printf("-> %d ",i); particle->Print();
656 printf("--------------------------------------------------------------\n");
659 printf("-> %d Particle not loaded.\n",i);
660 printf("--------------------------------------------------------------\n");
664 "\n=======================================================================\n\n");
671 //_____________________________________________________________________________
672 void AliStack::CleanParents()
675 // Clean particles stack
676 // Set parent/daughter relations
679 TObjArray &particles = *fParticleMap;
682 for(i=0; i<fHgwmk+1; i++) {
683 part = (TParticle *)particles.At(i);
684 if(part) if(!part->TestBit(kDaughtersBit)) {
685 part->SetFirstDaughter(-1);
686 part->SetLastDaughter(-1);
691 //_____________________________________________________________________________
692 TParticle* AliStack::GetNextParticle()
695 // Return next particle from stack of particles
698 TParticle* particle = 0;
700 // search secondaries
701 //for(Int_t i=fNtrack-1; i>=0; i--) {
702 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
703 particle = (TParticle*) fParticleMap->At(i);
704 if ((particle) && (!particle->TestBit(kDoneBit))) {
706 //cout << "GetNextParticle() - secondary "
707 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
712 // take next primary if all secondaries were done
713 while (fCurrentPrimary>=0) {
714 fCurrent = fCurrentPrimary;
715 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
716 if ((particle) && (!particle->TestBit(kDoneBit))) {
717 //cout << "GetNextParticle() - primary "
718 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
723 // nothing to be tracked
725 //cout << "GetNextParticle() - none "
726 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
730 //__________________________________________________________________________________________
731 void AliStack::MakeTree(Int_t event, const char *file)
734 // Make Kine tree and creates branch for writing particles
737 // Make Kinematics Tree
739 // printf("\n MakeTree called %d", event);
741 sprintf(hname,"TreeK%d",event);
742 fTreeK = new TTree(hname,"Kinematics");
743 // Create a branch for particles
744 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000, 1);
745 fTreeK->Write(0,TObject::kOverwrite);
749 //_____________________________________________________________________________
750 void AliStack::BeginEvent(Int_t event)
761 sprintf(hname,"TreeK%d",event);
762 fTreeK->SetName(hname);
766 //_____________________________________________________________________________
767 void AliStack::FinishRun()
769 // Clean TreeK information
771 delete fTreeK; fTreeK = 0;
775 //_____________________________________________________________________________
776 void AliStack::GetEvent(Int_t event)
779 // Get new event from TreeK
780 // Reset/Create the particle stack
781 if (fTreeK) delete fTreeK;
783 // Get Kine Tree from file
785 sprintf(treeName,"TreeK%d",event);
786 fTreeK = (TTree*)gDirectory->Get(treeName);
788 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
791 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
793 // printf("\n primaries %d", fNprimary);
794 // printf("\n tracks %d", fNtrack);
796 Int_t size = (Int_t)fTreeK->GetEntries();