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.1 2001/05/16 14:57:22 alibrary
19 New files for folders and Stack
23 ///////////////////////////////////////////////////////////////////////////////
25 // Particles stack class
27 ///////////////////////////////////////////////////////////////////////////////
31 #include <TObjArray.h>
32 #include <TParticle.h>
40 #include "AliModule.h"
42 //#include "ETrackBits.h"
46 //_____________________________________________________________________________
47 AliStack::AliStack(Int_t size)
53 // Create the particles arrays
54 fParticles = new TClonesArray("TParticle",1000);
55 fParticleMap = new TObjArray(size);
56 fParticleBuffer = new TParticle();
65 //_____________________________________________________________________________
69 // Default constructor
72 // Create the particles arrays
73 fParticles = new TClonesArray("TParticle",1000);
74 fParticleMap = new TObjArray(10000);
75 fParticleBuffer = new TParticle();
84 //_____________________________________________________________________________
96 delete fParticleBuffer;
104 //_____________________________________________________________________________
105 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
106 Float_t *vpos, Float_t *polar, Float_t tof,
107 AliMCProcess mech, Int_t &ntr, Float_t weight)
110 // Load a track on the stack
112 // done 0 if the track has to be transported
114 // parent identifier of the parent track. -1 for a primary
116 // pmom momentum GeV/c
118 // polar polarisation
119 // tof time of flight in seconds
120 // mecha production mechanism
121 // ntr on output the number of the track stored
125 const Int_t kfirstdaughter=-1;
126 const Int_t klastdaughter=-1;
128 // const Float_t tlife=0;
131 // Here we get the static mass
132 // For MC is ok, but a more sophisticated method could be necessary
133 // if the calculated mass is required
134 // also, this method is potentially dangerous if the mass
135 // used in the MC is not the same of the PDG database
137 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
138 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
139 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
141 // 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",
142 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
144 TClonesArray &particles = *fParticles;
146 = new(particles[fLoadPoint++])
147 TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
148 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
149 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
150 particle->SetWeight(weight);
151 particle->SetUniqueID(mech);
152 if(!done) particle->SetBit(kDoneBit);
155 // Declare that the daughter information is valid
156 particle->SetBit(kDaughtersBit);
157 // Add the particle to the stack
158 fParticleMap->AddAtAndExpand(particle, fNtrack);
161 particle = (TParticle*) fParticleMap->At(parent);
162 particle->SetLastDaughter(fNtrack);
163 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
167 // This is a primary track. Set high water mark for this event
170 // Set also number if primary tracks
171 fNprimary = fHgwmk+1;
177 //_____________________________________________________________________________
178 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
179 Double_t px, Double_t py, Double_t pz, Double_t e,
180 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
181 Double_t polx, Double_t poly, Double_t polz,
182 AliMCProcess mech, Int_t &ntr, Float_t weight)
185 // Load a track on the stack
187 // done 0 if the track has to be transported
189 // parent identifier of the parent track. -1 for a primary
191 // kS generation status code
192 // px, py, pz momentum GeV/c
193 // vx, vy, vz position
194 // polar polarisation
195 // tof time of flight in seconds
196 // mech production mechanism
197 // ntr on output the number of the track stored
199 // New method interface:
200 // arguments were changed to be in correspondence with TParticle
202 // Note: the energy is not calculated from the static mass but
203 // it is passed by argument e.
207 const Int_t kFirstDaughter=-1;
208 const Int_t kLastDaughter=-1;
210 TClonesArray &particles = *fParticles;
212 = new(particles[fLoadPoint++])
213 TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
214 px, py, pz, e, vx, vy, vz, tof);
216 particle->SetPolarisation(polx, poly, polz);
217 particle->SetWeight(weight);
218 particle->SetUniqueID(mech);
220 if(!done) particle->SetBit(kDoneBit);
222 // Declare that the daughter information is valid
223 particle->SetBit(kDaughtersBit);
224 // Add the particle to the stack
225 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
228 particle = (TParticle*) fParticleMap->At(parent);
229 particle->SetLastDaughter(fNtrack);
230 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
234 // This is a primary track. Set high water mark for this event
237 // Set also number if primary tracks
238 fNprimary = fHgwmk+1;
244 //_____________________________________________________________________________
245 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
246 Float_t &e, Float_t *vpos, Float_t *polar,
250 // Return next track from stack of particles
254 TParticle* track = GetNextParticle();
255 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
259 ipart=track->GetPdgCode();
268 track->GetPolarisation(pol);
273 track->SetBit(kDoneBit);
274 //cout << "Filled params" << endl;
280 // stop and start timer when we start a primary track
281 Int_t nprimaries = fNprimary;
282 if (fCurrent >= nprimaries) return;
283 if (fCurrent < nprimaries-1) {
285 track=(TParticle*) fParticleMap->At(fCurrent+1);
286 // track->SetProcessTime(fTimer.CpuTime());
292 //_____________________________________________________________________________
293 void AliStack::PurifyKine()
296 // Compress kinematic tree keeping only flagged particles
297 // and renaming the particle id's in all the hits
300 TObjArray &particles = *fParticleMap;
301 int nkeep=fHgwmk+1, parent, i;
302 TParticle *part, *father;
303 TArrayI map(particles.GetLast()+1);
305 // Save in Header total number of tracks before compression
307 // If no tracks generated return now
308 if(fHgwmk+1 == fNtrack) return;
310 // First pass, invalid Daughter information
311 for(i=0; i<fNtrack; i++) {
312 // Preset map, to be removed later
313 if(i<=fHgwmk) map[i]=i ;
316 if((part=(TParticle*) particles.At(i))) {
317 part->ResetBit(kDaughtersBit);
318 part->SetFirstDaughter(-1);
319 part->SetLastDaughter(-1);
323 // Invalid daughter information for the parent of the first particle
324 // generated. This may or may not be the current primary according to
325 // whether decays have been recorded among the primaries
326 part = (TParticle *)particles.At(fHgwmk+1);
327 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
328 // Second pass, build map between old and new numbering
329 for(i=fHgwmk+1; i<fNtrack; i++) {
330 if(particles.At(i)->TestBit(kKeepBit)) {
332 // This particle has to be kept
334 // If old and new are different, have to move the pointer
335 if(i!=nkeep) particles[nkeep]=particles.At(i);
336 part = (TParticle*) particles.At(nkeep);
338 // as the parent is always *before*, it must be already
339 // in place. This is what we are checking anyway!
340 if((parent=part->GetFirstMother())>fHgwmk)
341 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
342 else part->SetFirstMother(map[parent]);
348 // Fix daughters information
349 for (i=fHgwmk+1; i<nkeep; i++) {
350 part = (TParticle *)particles.At(i);
351 parent = part->GetFirstMother();
353 father = (TParticle *)particles.At(parent);
354 if(father->TestBit(kDaughtersBit)) {
356 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
357 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
359 // Initialise daughters info for first pass
360 father->SetFirstDaughter(i);
361 father->SetLastDaughter(i);
362 father->SetBit(kDaughtersBit);
367 // Now loop on all registered hit lists
368 TList* hitLists = gAlice->GetHitLists();
369 TIter next(hitLists);
370 TCollection *hitList;
371 while((hitList = (TCollection*)next())) {
372 TIter nexthit(hitList);
374 while((hit = (AliHit*)nexthit())) {
375 hit->SetTrack(map[hit->GetTrack()]);
380 // This for detectors which have a special mapping mechanism
381 // for hits, such as TPC and TRD
384 TObjArray* modules = gAlice->Modules();
385 TIter nextmod(modules);
387 while((detector = (AliModule*)nextmod())) {
388 detector->RemapTrackHitIDs(map.GetArray());
391 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
392 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
394 for (i=fHgwmk+1; i<nkeep; ++i) {
395 fParticleBuffer = (TParticle*) particles.At(i);
396 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
401 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
403 Int_t toshrink = fNtrack-fHgwmk-1;
404 fLoadPoint-=toshrink;
405 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
412 //_____________________________________________________________________________
413 void AliStack::FinishEvent()
416 // Write out the kinematics that was not yet filled
419 // Update event header
423 // Fatal("FinishEvent", "No kinematics tree is defined.");
424 // Don't panic this is a probably a lego run
430 if(fTreeK->GetEntries() ==0) {
431 // set the fParticleFileMap size for the first time
432 fParticleFileMap.Set(fHgwmk+1);
435 Bool_t allFilled = kFALSE;
437 for(Int_t i=0; i<fHgwmk+1; ++i)
438 if((part=fParticleMap->At(i))) {
439 fParticleBuffer = (TParticle*) part;
440 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
442 (*fParticleMap)[i]=0;
444 // When all primaries were filled no particle!=0
445 // should be left => to be removed later.
446 if (allFilled) printf("Why != 0 part # %d?\n",i);
449 // // printf("Why = 0 part # %d?\n",i); => We know.
451 // we don't break now in order to be sure there is no
452 // particle !=0 left.
453 // To be removed later and replaced with break.
454 if(!allFilled) allFilled = kTRUE;
456 //cout << "Nof particles: " << fNtrack << endl;
460 //_____________________________________________________________________________
461 void AliStack::FlagTrack(Int_t track)
464 // Flags a track and all its family tree to be kept
471 particle=(TParticle*)fParticleMap->At(curr);
473 // If the particle is flagged the three from here upward is saved already
474 if(particle->TestBit(kKeepBit)) return;
476 // Save this particle
477 particle->SetBit(kKeepBit);
479 // Move to father if any
480 if((curr=particle->GetFirstMother())==-1) return;
484 //_____________________________________________________________________________
485 void AliStack::KeepTrack(const Int_t track)
488 // Flags a track to be kept
491 fParticleMap->At(track)->SetBit(kKeepBit);
494 //_____________________________________________________________________________
495 void AliStack::Reset(Int_t size)
509 //_____________________________________________________________________________
510 void AliStack::ResetArrays(Int_t size)
513 // Resets stack arrays
517 fParticleMap->Clear();
518 if (size>0) fParticleMap->Expand(size);
521 //_____________________________________________________________________________
522 void AliStack::SetHighWaterMark(const Int_t nt)
525 // Set high water mark for last track in event
529 fCurrentPrimary=fHgwmk;
531 // Set also number of primary tracks
532 fNprimary = fHgwmk+1;
536 //_____________________________________________________________________________
537 TParticle* AliStack::Particle(Int_t i)
540 // Return particle with specified ID
542 if(!(*fParticleMap)[i]) {
543 Int_t nentries = fParticles->GetEntries();
544 // algorithmic way of getting entry index
545 // (primary particles are filled after secondaries)
548 entry = i+fNtrack-fNprimary;
551 // check whether algorithmic way and
552 // and the fParticleFileMap[i] give the same;
553 // give the fatal error if not
554 if (entry != fParticleFileMap[i]) {
556 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
557 entry, fParticleFileMap[i]);
560 fTreeK->GetEntry(entry);
561 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
562 fParticleMap->AddAt((*fParticles)[nentries],i);
564 return (TParticle *) (*fParticleMap)[i];
567 //_____________________________________________________________________________
568 Int_t AliStack::GetPrimary(Int_t id) const
571 // Return number of primary that has generated track
580 part = (TParticle *)fParticleMap->At(current);
581 parent=part->GetFirstMother();
582 if(parent<0) return current;
586 //_____________________________________________________________________________
587 void AliStack::DumpPart (Int_t i) const
590 // Dumps particle i in the stack
593 ((TParticle*) (*fParticleMap)[i])->Print();
596 //_____________________________________________________________________________
597 void AliStack::DumpPStack ()
600 // Dumps the particle stack
604 "\n\n=======================================================================\n");
605 for (Int_t i=0;i<fNtrack;i++)
607 TParticle* particle = Particle(i);
609 printf("-> %d ",i); particle->Print();
610 printf("--------------------------------------------------------------\n");
613 Warning("DumpPStack", "No particle with id %d.", i);
617 "\n=======================================================================\n\n");
619 // print particle file map
620 printf("\nParticle file map: \n");
621 for (Int_t i=0; i<fNtrack; i++)
622 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
626 //_____________________________________________________________________________
627 void AliStack::DumpLoadedStack() const
630 // Dumps the particle in the stack
631 // that are loaded in memory.
634 TObjArray &particles = *fParticleMap;
636 "\n\n=======================================================================\n");
637 for (Int_t i=0;i<fNtrack;i++)
639 TParticle* particle = (TParticle*) particles[i];
641 printf("-> %d ",i); particle->Print();
642 printf("--------------------------------------------------------------\n");
645 printf("-> %d Particle not loaded.\n",i);
646 printf("--------------------------------------------------------------\n");
650 "\n=======================================================================\n\n");
657 //_____________________________________________________________________________
658 void AliStack::CleanParents()
661 // Clean particles stack
662 // Set parent/daughter relations
665 TObjArray &particles = *fParticleMap;
668 for(i=0; i<fHgwmk+1; i++) {
669 part = (TParticle *)particles.At(i);
670 if(part) if(!part->TestBit(kDaughtersBit)) {
671 part->SetFirstDaughter(-1);
672 part->SetLastDaughter(-1);
677 //_____________________________________________________________________________
678 TParticle* AliStack::GetNextParticle()
681 // Return next particle from stack of particles
684 TParticle* particle = 0;
686 // search secondaries
687 //for(Int_t i=fNtrack-1; i>=0; i--) {
688 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
689 particle = (TParticle*) fParticleMap->At(i);
690 if ((particle) && (!particle->TestBit(kDoneBit))) {
692 //cout << "GetNextParticle() - secondary "
693 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
698 // take next primary if all secondaries were done
699 while (fCurrentPrimary>=0) {
700 fCurrent = fCurrentPrimary;
701 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
702 if ((particle) && (!particle->TestBit(kDoneBit))) {
703 //cout << "GetNextParticle() - primary "
704 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
709 // nothing to be tracked
711 //cout << "GetNextParticle() - none "
712 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
716 //__________________________________________________________________________________________
717 void AliStack::MakeTree(Int_t event, const char *file)
720 // Make Kine tree and creates branch for writing particles
723 // Make Kinematics Tree
725 // printf("\n MakeTree called %d", event);
727 sprintf(hname,"TreeK%d",event);
728 fTreeK = new TTree(hname,"Kinematics");
729 // Create a branch for particles
730 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000, 1);
731 fTreeK->Write(0,TObject::kOverwrite);
735 void AliStack::BeginEvent(Int_t event)
746 sprintf(hname,"TreeK%d",event);
747 fTreeK->SetName(hname);
751 void AliStack::FinishRun()
753 // Clean TreeK information
755 delete fTreeK; fTreeK = 0;
759 void AliStack::GetEvent(Int_t event)
762 // Get new event from TreeK
763 // Reset/Create the particle stack
764 if (fTreeK) delete fTreeK;
766 // Get Kine Tree from file
768 sprintf(treeName,"TreeK%d",event);
769 fTreeK = (TTree*)gDirectory->Get(treeName);
771 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
774 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
776 // printf("\n primaries %d", fNprimary);
777 // printf("\n tracks %d", fNtrack);
779 Int_t size = (Int_t)fTreeK->GetEntries();