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.3 2001/05/22 14:33:16 hristov
21 Revision 1.2 2001/05/17 05:49:39 fca
22 Reset pointers to daughters
24 Revision 1.1 2001/05/16 14:57:22 alibrary
25 New files for folders and Stack
29 ///////////////////////////////////////////////////////////////////////////////
31 // Particles stack class
33 ///////////////////////////////////////////////////////////////////////////////
37 #include <TObjArray.h>
38 #include <TParticle.h>
46 #include "AliModule.h"
48 //#include "ETrackBits.h"
52 //_____________________________________________________________________________
53 AliStack::AliStack(Int_t size)
59 // Create the particles arrays
60 fParticles = new TClonesArray("TParticle",1000);
61 fParticleMap = new TObjArray(size);
62 fParticleBuffer = new TParticle();
71 //_____________________________________________________________________________
75 // Default constructor
78 // Create the particles arrays
79 fParticles = new TClonesArray("TParticle",1000);
80 fParticleMap = new TObjArray(10000);
81 fParticleBuffer = new TParticle();
90 //_____________________________________________________________________________
97 delete fParticleBuffer;
110 //_____________________________________________________________________________
111 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
112 Float_t *vpos, Float_t *polar, Float_t tof,
113 AliMCProcess mech, Int_t &ntr, Float_t weight)
116 // Load a track on the stack
118 // done 0 if the track has to be transported
120 // parent identifier of the parent track. -1 for a primary
122 // pmom momentum GeV/c
124 // polar polarisation
125 // tof time of flight in seconds
126 // mecha production mechanism
127 // ntr on output the number of the track stored
131 const Int_t kfirstdaughter=-1;
132 const Int_t klastdaughter=-1;
134 // const Float_t tlife=0;
137 // Here we get the static mass
138 // For MC is ok, but a more sophisticated method could be necessary
139 // if the calculated mass is required
140 // also, this method is potentially dangerous if the mass
141 // used in the MC is not the same of the PDG database
143 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
144 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
145 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
147 // 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",
148 // mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
150 TClonesArray &particles = *fParticles;
152 = new(particles[fLoadPoint++])
153 TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
154 pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
155 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
156 particle->SetWeight(weight);
157 particle->SetUniqueID(mech);
158 if(!done) particle->SetBit(kDoneBit);
161 // Declare that the daughter information is valid
162 particle->SetBit(kDaughtersBit);
163 // Add the particle to the stack
164 fParticleMap->AddAtAndExpand(particle, fNtrack);
167 particle = (TParticle*) fParticleMap->At(parent);
168 particle->SetLastDaughter(fNtrack);
169 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
173 // This is a primary track. Set high water mark for this event
176 // Set also number if primary tracks
177 fNprimary = fHgwmk+1;
183 //_____________________________________________________________________________
184 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
185 Double_t px, Double_t py, Double_t pz, Double_t e,
186 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
187 Double_t polx, Double_t poly, Double_t polz,
188 AliMCProcess mech, Int_t &ntr, Float_t weight)
191 // Load a track on the stack
193 // done 0 if the track has to be transported
195 // parent identifier of the parent track. -1 for a primary
197 // kS generation status code
198 // px, py, pz momentum GeV/c
199 // vx, vy, vz position
200 // polar polarisation
201 // tof time of flight in seconds
202 // mech production mechanism
203 // ntr on output the number of the track stored
205 // New method interface:
206 // arguments were changed to be in correspondence with TParticle
208 // Note: the energy is not calculated from the static mass but
209 // it is passed by argument e.
213 const Int_t kFirstDaughter=-1;
214 const Int_t kLastDaughter=-1;
216 TClonesArray &particles = *fParticles;
218 = new(particles[fLoadPoint++])
219 TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
220 px, py, pz, e, vx, vy, vz, tof);
222 particle->SetPolarisation(polx, poly, polz);
223 particle->SetWeight(weight);
224 particle->SetUniqueID(mech);
226 if(!done) particle->SetBit(kDoneBit);
228 // Declare that the daughter information is valid
229 particle->SetBit(kDaughtersBit);
230 // Add the particle to the stack
231 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
234 particle = (TParticle*) fParticleMap->At(parent);
235 particle->SetLastDaughter(fNtrack);
236 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
240 // This is a primary track. Set high water mark for this event
243 // Set also number if primary tracks
244 fNprimary = fHgwmk+1;
250 //_____________________________________________________________________________
251 void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
252 Float_t &e, Float_t *vpos, Float_t *polar,
256 // Return next track from stack of particles
260 TParticle* track = GetNextParticle();
261 // cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
265 ipart=track->GetPdgCode();
274 track->GetPolarisation(pol);
279 track->SetBit(kDoneBit);
280 //cout << "Filled params" << endl;
286 // stop and start timer when we start a primary track
287 Int_t nprimaries = fNprimary;
288 if (fCurrent >= nprimaries) return;
289 if (fCurrent < nprimaries-1) {
291 track=(TParticle*) fParticleMap->At(fCurrent+1);
292 // track->SetProcessTime(fTimer.CpuTime());
298 //_____________________________________________________________________________
299 void AliStack::PurifyKine()
302 // Compress kinematic tree keeping only flagged particles
303 // and renaming the particle id's in all the hits
306 TObjArray &particles = *fParticleMap;
307 int nkeep=fHgwmk+1, parent, i;
308 TParticle *part, *father;
309 TArrayI map(particles.GetLast()+1);
311 // Save in Header total number of tracks before compression
313 // If no tracks generated return now
314 if(fHgwmk+1 == fNtrack) return;
316 // First pass, invalid Daughter information
317 for(i=0; i<fNtrack; i++) {
318 // Preset map, to be removed later
319 if(i<=fHgwmk) map[i]=i ;
322 if((part=(TParticle*) particles.At(i))) {
323 part->ResetBit(kDaughtersBit);
324 part->SetFirstDaughter(-1);
325 part->SetLastDaughter(-1);
329 // Invalid daughter information for the parent of the first particle
330 // generated. This may or may not be the current primary according to
331 // whether decays have been recorded among the primaries
332 part = (TParticle *)particles.At(fHgwmk+1);
333 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
334 // Second pass, build map between old and new numbering
335 for(i=fHgwmk+1; i<fNtrack; i++) {
336 if(particles.At(i)->TestBit(kKeepBit)) {
338 // This particle has to be kept
340 // If old and new are different, have to move the pointer
341 if(i!=nkeep) particles[nkeep]=particles.At(i);
342 part = (TParticle*) particles.At(nkeep);
344 // as the parent is always *before*, it must be already
345 // in place. This is what we are checking anyway!
346 if((parent=part->GetFirstMother())>fHgwmk)
347 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
348 else part->SetFirstMother(map[parent]);
354 // Fix daughters information
355 for (i=fHgwmk+1; i<nkeep; i++) {
356 part = (TParticle *)particles.At(i);
357 parent = part->GetFirstMother();
359 father = (TParticle *)particles.At(parent);
360 if(father->TestBit(kDaughtersBit)) {
362 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
363 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
365 // Initialise daughters info for first pass
366 father->SetFirstDaughter(i);
367 father->SetLastDaughter(i);
368 father->SetBit(kDaughtersBit);
373 // Now loop on all registered hit lists
374 TList* hitLists = gAlice->GetHitLists();
375 TIter next(hitLists);
376 TCollection *hitList;
377 while((hitList = (TCollection*)next())) {
378 TIter nexthit(hitList);
380 while((hit = (AliHit*)nexthit())) {
381 hit->SetTrack(map[hit->GetTrack()]);
386 // This for detectors which have a special mapping mechanism
387 // for hits, such as TPC and TRD
390 TObjArray* modules = gAlice->Modules();
391 TIter nextmod(modules);
393 while((detector = (AliModule*)nextmod())) {
394 detector->RemapTrackHitIDs(map.GetArray());
397 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
398 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
400 for (i=fHgwmk+1; i<nkeep; ++i) {
401 fParticleBuffer = (TParticle*) particles.At(i);
402 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
407 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
409 Int_t toshrink = fNtrack-fHgwmk-1;
410 fLoadPoint-=toshrink;
411 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
418 //_____________________________________________________________________________
419 void AliStack::FinishEvent()
422 // Write out the kinematics that was not yet filled
425 // Update event header
429 // Fatal("FinishEvent", "No kinematics tree is defined.");
430 // Don't panic this is a probably a lego run
436 if(fTreeK->GetEntries() ==0) {
437 // set the fParticleFileMap size for the first time
438 fParticleFileMap.Set(fHgwmk+1);
441 Bool_t allFilled = kFALSE;
443 for(Int_t i=0; i<fHgwmk+1; ++i)
444 if((part=fParticleMap->At(i))) {
445 fParticleBuffer = (TParticle*) part;
446 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
448 (*fParticleMap)[i]=0;
450 // When all primaries were filled no particle!=0
451 // should be left => to be removed later.
452 if (allFilled) printf("Why != 0 part # %d?\n",i);
455 // // printf("Why = 0 part # %d?\n",i); => We know.
457 // we don't break now in order to be sure there is no
458 // particle !=0 left.
459 // To be removed later and replaced with break.
460 if(!allFilled) allFilled = kTRUE;
462 //cout << "Nof particles: " << fNtrack << endl;
466 //_____________________________________________________________________________
467 void AliStack::FlagTrack(Int_t track)
470 // Flags a track and all its family tree to be kept
477 particle=(TParticle*)fParticleMap->At(curr);
479 // If the particle is flagged the three from here upward is saved already
480 if(particle->TestBit(kKeepBit)) return;
482 // Save this particle
483 particle->SetBit(kKeepBit);
485 // Move to father if any
486 if((curr=particle->GetFirstMother())==-1) return;
490 //_____________________________________________________________________________
491 void AliStack::KeepTrack(Int_t track)
494 // Flags a track to be kept
497 fParticleMap->At(track)->SetBit(kKeepBit);
500 //_____________________________________________________________________________
501 void AliStack::Reset(Int_t size)
515 //_____________________________________________________________________________
516 void AliStack::ResetArrays(Int_t size)
519 // Resets stack arrays
523 fParticleMap->Clear();
524 if (size>0) fParticleMap->Expand(size);
527 //_____________________________________________________________________________
528 void AliStack::SetHighWaterMark(Int_t nt)
531 // Set high water mark for last track in event
535 fCurrentPrimary=fHgwmk;
537 // Set also number of primary tracks
538 fNprimary = fHgwmk+1;
542 //_____________________________________________________________________________
543 TParticle* AliStack::Particle(Int_t i)
546 // Return particle with specified ID
548 if(!(*fParticleMap)[i]) {
549 Int_t nentries = fParticles->GetEntries();
550 // algorithmic way of getting entry index
551 // (primary particles are filled after secondaries)
554 entry = i+fNtrack-fNprimary;
557 // check whether algorithmic way and
558 // and the fParticleFileMap[i] give the same;
559 // give the fatal error if not
560 if (entry != fParticleFileMap[i]) {
562 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
563 entry, fParticleFileMap[i]);
566 fTreeK->GetEntry(entry);
567 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
568 fParticleMap->AddAt((*fParticles)[nentries],i);
570 return (TParticle *) (*fParticleMap)[i];
573 //_____________________________________________________________________________
574 Int_t AliStack::GetPrimary(Int_t id) const
577 // Return number of primary that has generated track
586 part = (TParticle *)fParticleMap->At(current);
587 parent=part->GetFirstMother();
588 if(parent<0) return current;
592 //_____________________________________________________________________________
593 void AliStack::DumpPart (Int_t i) const
596 // Dumps particle i in the stack
599 ((TParticle*) (*fParticleMap)[i])->Print();
602 //_____________________________________________________________________________
603 void AliStack::DumpPStack ()
606 // Dumps the particle stack
610 "\n\n=======================================================================\n");
611 for (Int_t i=0;i<fNtrack;i++)
613 TParticle* particle = Particle(i);
615 printf("-> %d ",i); particle->Print();
616 printf("--------------------------------------------------------------\n");
619 Warning("DumpPStack", "No particle with id %d.", i);
623 "\n=======================================================================\n\n");
625 // print particle file map
626 printf("\nParticle file map: \n");
627 for (Int_t i=0; i<fNtrack; i++)
628 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
632 //_____________________________________________________________________________
633 void AliStack::DumpLoadedStack() const
636 // Dumps the particle in the stack
637 // that are loaded in memory.
640 TObjArray &particles = *fParticleMap;
642 "\n\n=======================================================================\n");
643 for (Int_t i=0;i<fNtrack;i++)
645 TParticle* particle = (TParticle*) particles[i];
647 printf("-> %d ",i); particle->Print();
648 printf("--------------------------------------------------------------\n");
651 printf("-> %d Particle not loaded.\n",i);
652 printf("--------------------------------------------------------------\n");
656 "\n=======================================================================\n\n");
663 //_____________________________________________________________________________
664 void AliStack::CleanParents()
667 // Clean particles stack
668 // Set parent/daughter relations
671 TObjArray &particles = *fParticleMap;
674 for(i=0; i<fHgwmk+1; i++) {
675 part = (TParticle *)particles.At(i);
676 if(part) if(!part->TestBit(kDaughtersBit)) {
677 part->SetFirstDaughter(-1);
678 part->SetLastDaughter(-1);
683 //_____________________________________________________________________________
684 TParticle* AliStack::GetNextParticle()
687 // Return next particle from stack of particles
690 TParticle* particle = 0;
692 // search secondaries
693 //for(Int_t i=fNtrack-1; i>=0; i--) {
694 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
695 particle = (TParticle*) fParticleMap->At(i);
696 if ((particle) && (!particle->TestBit(kDoneBit))) {
698 //cout << "GetNextParticle() - secondary "
699 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
704 // take next primary if all secondaries were done
705 while (fCurrentPrimary>=0) {
706 fCurrent = fCurrentPrimary;
707 particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
708 if ((particle) && (!particle->TestBit(kDoneBit))) {
709 //cout << "GetNextParticle() - primary "
710 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
715 // nothing to be tracked
717 //cout << "GetNextParticle() - none "
718 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
722 //__________________________________________________________________________________________
723 void AliStack::MakeTree(Int_t event, const char *file)
726 // Make Kine tree and creates branch for writing particles
729 // Make Kinematics Tree
731 // printf("\n MakeTree called %d", event);
733 sprintf(hname,"TreeK%d",event);
734 fTreeK = new TTree(hname,"Kinematics");
735 // Create a branch for particles
736 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000, 1);
737 fTreeK->Write(0,TObject::kOverwrite);
741 //_____________________________________________________________________________
742 void AliStack::BeginEvent(Int_t event)
753 sprintf(hname,"TreeK%d",event);
754 fTreeK->SetName(hname);
758 //_____________________________________________________________________________
759 void AliStack::FinishRun()
761 // Clean TreeK information
763 delete fTreeK; fTreeK = 0;
767 //_____________________________________________________________________________
768 void AliStack::GetEvent(Int_t event)
771 // Get new event from TreeK
772 // Reset/Create the particle stack
773 if (fTreeK) delete fTreeK;
775 // Get Kine Tree from file
777 sprintf(treeName,"TreeK%d",event);
778 fTreeK = (TTree*)gDirectory->Get(treeName);
780 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
783 Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
785 // printf("\n primaries %d", fNprimary);
786 // printf("\n tracks %d", fNtrack);
788 Int_t size = (Int_t)fTreeK->GetEntries();