]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliStack.cxx
AliStack dependences minimized.
[u/mrichter/AliRoot.git] / STEER / AliStack.cxx
CommitLineData
9e1a0ddb 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
acd84897 16/* $Id$ */
9e1a0ddb 17
18///////////////////////////////////////////////////////////////////////////////
19// //
af7ba10c 20// Particles stack class //
21// Implements the TMCVirtualStack of the Virtual Monte Carlo //
22// Holds the particles transported during simulation //
23// Is used to compare results of reconstruction with simulation //
24// Author A.Morsch //
9e1a0ddb 25// //
26///////////////////////////////////////////////////////////////////////////////
27
9e1a0ddb 28
7ca4655f 29#include <TClonesArray.h>
9e1a0ddb 30#include <TObjArray.h>
7ca4655f 31#include <TPDGCode.h>
9e1a0ddb 32#include <TParticle.h>
fe046ade 33#include <TParticlePDG.h>
9e1a0ddb 34#include <TTree.h>
942a9039 35#include <TDirectory.h>
9e1a0ddb 36
21bf7095 37#include "AliLog.h"
88cb7938 38#include "AliStack.h"
9e1a0ddb 39
40ClassImp(AliStack)
41
e2afb3b6 42//_______________________________________________________________________
43AliStack::AliStack():
44 fParticles(0),
45 fParticleMap(0),
46 fParticleFileMap(0),
47 fParticleBuffer(0),
90e48c0c 48 fCurrentTrack(0),
e2afb3b6 49 fTreeK(0),
50 fNtrack(0),
51 fNprimary(0),
52 fCurrent(-1),
53 fCurrentPrimary(-1),
54 fHgwmk(0),
88cb7938 55 fLoadPoint(0),
942a9039 56 fTrackLabelMap(0)
9e1a0ddb 57{
58 //
e2afb3b6 59 // Default constructor
9e1a0ddb 60 //
9e1a0ddb 61}
62
e2afb3b6 63//_______________________________________________________________________
942a9039 64AliStack::AliStack(Int_t size, const char* /*evfoldname*/):
e2afb3b6 65 fParticles(new TClonesArray("TParticle",1000)),
66 fParticleMap(new TObjArray(size)),
67 fParticleFileMap(0),
68 fParticleBuffer(0),
90e48c0c 69 fCurrentTrack(0),
e2afb3b6 70 fTreeK(0),
71 fNtrack(0),
72 fNprimary(0),
73 fCurrent(-1),
74 fCurrentPrimary(-1),
75 fHgwmk(0),
88cb7938 76 fLoadPoint(0),
942a9039 77 fTrackLabelMap(0)
e2afb3b6 78{
79 //
80 // Constructor
81 //
82}
9e1a0ddb 83
e2afb3b6 84//_______________________________________________________________________
85AliStack::AliStack(const AliStack& st):
9938d01c 86 TVirtualMCStack(st),
87 fParticles(new TClonesArray("TParticle",1000)),
88 fParticleMap(new TObjArray(*st.Particles())),
89 fParticleFileMap(st.fParticleFileMap),
90 fParticleBuffer(0),
91 fCurrentTrack(0),
92 fTreeK((TTree*)(st.fTreeK->Clone())),
93 fNtrack(st.GetNtrack()),
94 fNprimary(st.GetNprimary()),
95 fCurrent(-1),
96 fCurrentPrimary(-1),
97 fHgwmk(0),
98 fLoadPoint(0),
942a9039 99 fTrackLabelMap(0)
9e1a0ddb 100{
9938d01c 101 // Copy constructor
9e1a0ddb 102}
103
9938d01c 104
e2afb3b6 105//_______________________________________________________________________
6c4904c2 106void AliStack::Copy(TObject&) const
e2afb3b6 107{
21bf7095 108 AliFatal("Not implemented!");
e2afb3b6 109}
9e1a0ddb 110
e2afb3b6 111//_______________________________________________________________________
9e1a0ddb 112AliStack::~AliStack()
113{
114 //
115 // Destructor
116 //
117
118 if (fParticles) {
119 fParticles->Delete();
120 delete fParticles;
121 }
122 delete fParticleMap;
9e1a0ddb 123}
124
125//
126// public methods
127//
128
129//_____________________________________________________________________________
642f15cf 130void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
88cb7938 131 Float_t *vpos, Float_t *polar, Float_t tof,
132 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
9e1a0ddb 133{
134 //
135 // Load a track on the stack
136 //
137 // done 0 if the track has to be transported
138 // 1 if not
139 // parent identifier of the parent track. -1 for a primary
140 // pdg particle code
141 // pmom momentum GeV/c
142 // vpos position
143 // polar polarisation
144 // tof time of flight in seconds
145 // mecha production mechanism
146 // ntr on output the number of the track stored
147 //
148
9e1a0ddb 149 // const Float_t tlife=0;
150
151 //
152 // Here we get the static mass
153 // For MC is ok, but a more sophisticated method could be necessary
154 // if the calculated mass is required
155 // also, this method is potentially dangerous if the mass
156 // used in the MC is not the same of the PDG database
157 //
fe046ade 158 TParticlePDG* pmc = TDatabasePDG::Instance()->GetParticle(pdg);
159 if (pmc) {
160 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
161 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
162 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
163
ccf7a81f 164// 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",
165// mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
166
9e1a0ddb 167
642f15cf 168 PushTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
fe046ade 169 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
170 mech, ntr, weight, is);
171 } else {
21bf7095 172 AliWarning(Form("Particle type %d not defined in PDG Database !", pdg));
173 AliWarning("Particle skipped !");
fe046ade 174 }
9e1a0ddb 175}
176
177//_____________________________________________________________________________
642f15cf 178void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg,
9e1a0ddb 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,
98490ea9 182 TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
9e1a0ddb 183{
184 //
185 // Load a track on the stack
186 //
187 // done 0 if the track has to be transported
188 // 1 if not
189 // parent identifier of the parent track. -1 for a primary
190 // pdg particle code
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
198 //
199 // New method interface:
200 // arguments were changed to be in correspondence with TParticle
201 // constructor.
202 // Note: the energy is not calculated from the static mass but
203 // it is passed by argument e.
204
9e1a0ddb 205 const Int_t kFirstDaughter=-1;
206 const Int_t kLastDaughter=-1;
6308b375 207
208
9e1a0ddb 209 TClonesArray &particles = *fParticles;
210 TParticle* particle
211 = new(particles[fLoadPoint++])
47c8bcbe 212 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
9e1a0ddb 213 px, py, pz, e, vx, vy, vz, tof);
214
215 particle->SetPolarisation(polx, poly, polz);
216 particle->SetWeight(weight);
217 particle->SetUniqueID(mech);
218
219 if(!done) particle->SetBit(kDoneBit);
220
221 // Declare that the daughter information is valid
222 particle->SetBit(kDaughtersBit);
223 // Add the particle to the stack
6308b375 224
225
9e1a0ddb 226 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
227
228 if(parent>=0) {
942a9039 229 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
230 if (particle) {
231 particle->SetLastDaughter(fNtrack);
232 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
233 }
234 else {
235 AliError(Form("Parent %d does not exist",parent));
236 }
237 } else {
238 //
239 // This is a primary track. Set high water mark for this event
240 fHgwmk = fNtrack;
241 //
242 // Set also number if primary tracks
243 fNprimary = fHgwmk+1;
244 fCurrentPrimary++;
9e1a0ddb 245 }
246 ntr = fNtrack++;
247}
248
249//_____________________________________________________________________________
642f15cf 250TParticle* AliStack::PopNextTrack(Int_t& itrack)
b9d0a01d 251{
252 //
253 // Returns next track from stack of particles
254 //
255
256
257 TParticle* track = GetNextParticle();
258
259 if (track) {
260 itrack = fCurrent;
261 track->SetBit(kDoneBit);
262 }
fe046ade 263 else
b9d0a01d 264 itrack = -1;
fe046ade 265
266 fCurrentTrack = track;
b9d0a01d 267 return track;
268}
269
b9d0a01d 270//_____________________________________________________________________________
642f15cf 271TParticle* AliStack::PopPrimaryForTracking(Int_t i)
b9d0a01d 272{
273 //
274 // Returns i-th primary particle if it is flagged to be tracked,
275 // 0 otherwise
276 //
277
278 TParticle* particle = Particle(i);
279
280 if (!particle->TestBit(kDoneBit))
281 return particle;
282 else
283 return 0;
284}
285
9e1a0ddb 286//_____________________________________________________________________________
287void AliStack::PurifyKine()
288{
289 //
290 // Compress kinematic tree keeping only flagged particles
291 // and renaming the particle id's in all the hits
292 //
293
294 TObjArray &particles = *fParticleMap;
295 int nkeep=fHgwmk+1, parent, i;
296 TParticle *part, *father;
942a9039 297 fTrackLabelMap.Set(particles.GetLast()+1);
9e1a0ddb 298
299 // Save in Header total number of tracks before compression
9e1a0ddb 300 // If no tracks generated return now
301 if(fHgwmk+1 == fNtrack) return;
302
9e1a0ddb 303 // First pass, invalid Daughter information
942a9039 304 for(i=0; i<fNtrack; i++) {
305 // Preset map, to be removed later
306 if(i<=fHgwmk) fTrackLabelMap[i]=i ;
307 else {
308 fTrackLabelMap[i] = -99;
309 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
e44c5340 310//
311// Check of this track should be kept for physics reasons
942a9039 312 if (KeepPhysics(part)) KeepTrack(i);
e44c5340 313//
942a9039 314 part->ResetBit(kDaughtersBit);
315 part->SetFirstDaughter(-1);
316 part->SetLastDaughter(-1);
317 }
76969085 318 }
9e1a0ddb 319 }
320 // Invalid daughter information for the parent of the first particle
321 // generated. This may or may not be the current primary according to
322 // whether decays have been recorded among the primaries
e2afb3b6 323 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
9e1a0ddb 324 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
325 // Second pass, build map between old and new numbering
326 for(i=fHgwmk+1; i<fNtrack; i++) {
942a9039 327 if(particles.At(i)->TestBit(kKeepBit)) {
328
329 // This particle has to be kept
330 fTrackLabelMap[i]=nkeep;
331 // If old and new are different, have to move the pointer
332 if(i!=nkeep) particles[nkeep]=particles.At(i);
333 part = dynamic_cast<TParticle*>(particles.At(nkeep));
334
335 // as the parent is always *before*, it must be already
336 // in place. This is what we are checking anyway!
337 if((parent=part->GetFirstMother())>fHgwmk)
338 if(fTrackLabelMap[parent]==-99) Fatal("PurifyKine","fTrackLabelMap[%d] = -99!\n",parent);
339 else part->SetFirstMother(fTrackLabelMap[parent]);
340
341 nkeep++;
342 }
9e1a0ddb 343 }
344
345 // Fix daughters information
346 for (i=fHgwmk+1; i<nkeep; i++) {
942a9039 347 part = dynamic_cast<TParticle*>(particles.At(i));
348 parent = part->GetFirstMother();
349 if(parent>=0) {
350 father = dynamic_cast<TParticle*>(particles.At(parent));
351 if(father->TestBit(kDaughtersBit)) {
352
353 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
354 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
355 } else {
356 // Initialise daughters info for first pass
357 father->SetFirstDaughter(i);
358 father->SetLastDaughter(i);
359 father->SetBit(kDaughtersBit);
360 }
9e1a0ddb 361 }
9e1a0ddb 362 }
942a9039 363 //
364 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
365 if(nkeep > fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
366 for (i=fHgwmk+1; i<nkeep; ++i) {
367 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
368 fParticleFileMap[i]=static_cast<Int_t>(TreeK()->GetEntries());
369 TreeK()->Fill();
370 particles[i]=fParticleBuffer=0;
371 }
9e1a0ddb 372
942a9039 373 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
6308b375 374
942a9039 375 Int_t toshrink = fNtrack-fHgwmk-1;
376 fLoadPoint-=toshrink;
2b22f272 377
942a9039 378
379 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
380 fNtrack=nkeep;
381 fHgwmk=nkeep-1;
6308b375 382}
383
384void AliStack::ReorderKine()
385{
386//
387// In some transport code children might not come in a continuous sequence.
388// In this case the stack has to be reordered in order to establish the
389// mother daughter relation using index ranges.
390//
391 if(fHgwmk+1 == fNtrack) return;
392
393 //
394 // Howmany secondaries have been produced ?
395 Int_t nNew = fNtrack - fHgwmk - 1;
a1d92a3f 396
6308b375 397 if (nNew > 0) {
398 Int_t i, j;
399 TObjArray &particles = *fParticleMap;
400 TArrayI map1(nNew);
401 //
402 // Copy pointers to temporary array
403 TParticle** tmp = new TParticle*[nNew];
404
405 for (i = 0; i < nNew; i++) {
406 if (particles.At(fHgwmk + 1 + i)) {
6308b375 407 tmp[i] = (TParticle*) (particles.At(fHgwmk + 1 + i));
6308b375 408 } else {
409 tmp[i] = 0x0;
410 }
411 map1[i] = -99;
412 }
413
414
415 //
416 // Reset LoadPoint
417 //
418 fLoadPoint = fHgwmk + 1;
419 //
420 // Re-Push particles into stack
421 // The outer loop is over parents, the inner over children.
422 // -1 refers to the primary particle
423 //
a1d92a3f 424 for (i = -1; i < nNew-1; i++) {
6308b375 425 Int_t ipa;
426 TParticle* parP;
427 if (i == -1) {
428 ipa = tmp[0]->GetFirstMother();
429 parP =dynamic_cast<TParticle*>(particles.At(ipa));
430 } else {
431 ipa = (fHgwmk + 1 + i);
432 // Skip deleted particles
433 if (!tmp[i]) continue;
434 // Skip particles without children
435 if (tmp[i]->GetFirstDaughter() == -1) continue;
436 parP = tmp[i];
437 }
438 // Reset daughter information
a1d92a3f 439
440 Int_t idaumin = parP->GetFirstDaughter() - fHgwmk - 1;
441 Int_t idaumax = parP->GetLastDaughter() - fHgwmk - 1;
6308b375 442 parP->SetFirstDaughter(-1);
443 parP->SetLastDaughter(-1);
a1d92a3f 444 for (j = idaumin; j <= idaumax; j++) {
6308b375 445 // Skip deleted particles
446 if (!tmp[j]) continue;
447 // Skip particles already handled
448 if (map1[j] != -99) continue;
449 Int_t jpa = tmp[j]->GetFirstMother();
450 // Check if daughter of current parent
451 if (jpa == ipa) {
452 particles[fLoadPoint] = tmp[j];
453 // Re-establish daughter information
454 parP->SetLastDaughter(fLoadPoint);
455 if (parP->GetFirstDaughter() == -1) parP->SetFirstDaughter(fLoadPoint);
456 // Set Mother information
457 if (i != -1) {
458 tmp[j]->SetFirstMother(map1[i]);
459 }
460 // Build the map
461 map1[j] = fLoadPoint;
462 // Increase load point
463 fLoadPoint++;
464 }
465 } // children
466 } // parents
467
468 delete[] tmp;
469
470 //
471 // Build map for remapping of hits
472 //
942a9039 473 fTrackLabelMap.Set(fNtrack);
6308b375 474 for (i = 0; i < fNtrack; i ++) {
475 if (i <= fHgwmk) {
942a9039 476 fTrackLabelMap[i] = i;
6308b375 477 } else{
942a9039 478 fTrackLabelMap[i] = map1[i - fHgwmk -1];
6308b375 479 }
480 }
6308b375 481 } // new particles poduced
9e1a0ddb 482}
483
e44c5340 484Bool_t AliStack::KeepPhysics(TParticle* part)
485{
486 //
487 // Some particles have to kept on the stack for reasons motivated
488 // by physics analysis. Decision is put here.
489 //
490 Bool_t keep = kFALSE;
491 //
492 // Keep first-generation daughter from primaries with heavy flavor
493 //
494 Int_t parent = part->GetFirstMother();
495 if (parent >= 0 && parent <= fHgwmk) {
e2afb3b6 496 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
e44c5340 497 Int_t kf = father->GetPdgCode();
498 kf = TMath::Abs(kf);
499 Int_t kfl = kf;
500 // meson ?
501 if (kfl > 10) kfl/=100;
502 // baryon
503 if (kfl > 10) kfl/=10;
504 if (kfl > 10) kfl/=10;
505 if (kfl >= 4) {
506 keep = kTRUE;
507 }
508 }
509 return keep;
510}
511
9e1a0ddb 512//_____________________________________________________________________________
513void AliStack::FinishEvent()
514{
88cb7938 515//
516// Write out the kinematics that was not yet filled
517//
9e1a0ddb 518
88cb7938 519// Update event header
9e1a0ddb 520
88cb7938 521 if (!TreeK()) {
9e1a0ddb 522// Fatal("FinishEvent", "No kinematics tree is defined.");
523// Don't panic this is a probably a lego run
524 return;
9e1a0ddb 525 }
526
527 CleanParents();
942a9039 528 if(TreeK()->GetEntries() ==0) {
9e1a0ddb 529 // set the fParticleFileMap size for the first time
530 fParticleFileMap.Set(fHgwmk+1);
531 }
532
533 Bool_t allFilled = kFALSE;
534 TObject *part;
535 for(Int_t i=0; i<fHgwmk+1; ++i)
536 if((part=fParticleMap->At(i))) {
e2afb3b6 537 fParticleBuffer = dynamic_cast<TParticle*>(part);
88cb7938 538 fParticleFileMap[i]= static_cast<Int_t>(TreeK()->GetEntries());
539 TreeK()->Fill();
da2af24c 540 fParticleBuffer=0;
541 fParticleMap->AddAt(0,i);
9e1a0ddb 542
543 // When all primaries were filled no particle!=0
544 // should be left => to be removed later.
21bf7095 545 if (allFilled) AliWarning(Form("Why != 0 part # %d?\n",i));
9e1a0ddb 546 }
88cb7938 547 else
548 {
9e1a0ddb 549 // // printf("Why = 0 part # %d?\n",i); => We know.
550 // break;
88cb7938 551 // we don't break now in order to be sure there is no
552 // particle !=0 left.
553 // To be removed later and replaced with break.
554 if(!allFilled) allFilled = kTRUE;
555 }
9e1a0ddb 556}
9e1a0ddb 557//_____________________________________________________________________________
88cb7938 558
9e1a0ddb 559void AliStack::FlagTrack(Int_t track)
560{
561 //
562 // Flags a track and all its family tree to be kept
563 //
564
565 TParticle *particle;
566
567 Int_t curr=track;
568 while(1) {
e2afb3b6 569 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
9e1a0ddb 570
571 // If the particle is flagged the three from here upward is saved already
572 if(particle->TestBit(kKeepBit)) return;
573
574 // Save this particle
575 particle->SetBit(kKeepBit);
576
577 // Move to father if any
578 if((curr=particle->GetFirstMother())==-1) return;
579 }
580}
581
582//_____________________________________________________________________________
ed54bf31 583void AliStack::KeepTrack(Int_t track)
9e1a0ddb 584{
585 //
586 // Flags a track to be kept
587 //
588
589 fParticleMap->At(track)->SetBit(kKeepBit);
590}
591
592//_____________________________________________________________________________
593void AliStack::Reset(Int_t size)
594{
595 //
596 // Resets stack
597 //
88cb7938 598
9e1a0ddb 599 fNtrack=0;
600 fNprimary=0;
601 fHgwmk=0;
602 fLoadPoint=0;
603 fCurrent = -1;
88cb7938 604 fTreeK = 0x0;
9e1a0ddb 605 ResetArrays(size);
606}
607
608//_____________________________________________________________________________
609void AliStack::ResetArrays(Int_t size)
610{
611 //
612 // Resets stack arrays
613 //
614
be3380a3 615 if (fParticles)
616 fParticles->Clear();
617 else
618 fParticles = new TClonesArray("TParticle",1000);
619 if (fParticleMap) {
620 fParticleMap->Clear();
621 if (size>0) fParticleMap->Expand(size);}
622 else
623 fParticleMap = new TObjArray(size);
9e1a0ddb 624}
625
626//_____________________________________________________________________________
e2afb3b6 627void AliStack::SetHighWaterMark(Int_t)
9e1a0ddb 628{
629 //
630 // Set high water mark for last track in event
631 //
6308b375 632
942a9039 633 fHgwmk = fNtrack-1;
634 fCurrentPrimary=fHgwmk;
635 // Set also number of primary tracks
636 fNprimary = fHgwmk+1;
9e1a0ddb 637}
638
639//_____________________________________________________________________________
640TParticle* AliStack::Particle(Int_t i)
641{
642 //
643 // Return particle with specified ID
942a9039 644
da2af24c 645 if(!fParticleMap->At(i)) {
911a3148 646 Int_t nentries = fParticles->GetEntriesFast();
9e1a0ddb 647 // algorithmic way of getting entry index
648 // (primary particles are filled after secondaries)
e94530da 649 Int_t entry = TreeKEntry(i);
9e1a0ddb 650 // check whether algorithmic way and
651 // and the fParticleFileMap[i] give the same;
652 // give the fatal error if not
653 if (entry != fParticleFileMap[i]) {
21bf7095 654 AliFatal(Form(
9e1a0ddb 655 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
21bf7095 656 entry, fParticleFileMap[i]));
9e1a0ddb 657 }
942a9039 658 // Load particle at entry into fParticleBuffer
88cb7938 659 TreeK()->GetEntry(entry);
942a9039 660 // Add to the TClonesarray
9e1a0ddb 661 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
942a9039 662 // Store a pointer in the TObjArray
9e1a0ddb 663 fParticleMap->AddAt((*fParticles)[nentries],i);
664 }
e2afb3b6 665 return dynamic_cast<TParticle*>(fParticleMap->At(i));
9e1a0ddb 666}
667
e94530da 668//_____________________________________________________________________________
669TParticle* AliStack::ParticleFromTreeK(Int_t id) const
670{
671//
672// return pointer to TParticle with label id
673//
674 Int_t entry;
675 if ((entry = TreeKEntry(id)) < 0) return 0;
676 if (fTreeK->GetEntry(entry)<=0) return 0;
677 return fParticleBuffer;
678}
679
680//_____________________________________________________________________________
681Int_t AliStack::TreeKEntry(Int_t id) const
682{
683//
942a9039 684// Return entry number in the TreeK for particle with label id
685// Return negative number if label>fNtrack
686//
687// The order of particles in TreeK reflects the order of the transport of primaries and production of secondaries:
e94530da 688//
942a9039 689// Before transport there are fNprimary particles on the stack.
690// They are transported one by one and secondaries (fNtrack - fNprimary) are produced.
691// After the transport of each particles secondaries are written to the TreeK
692// They occupy the entries 0 ... fNtrack - fNprimary - 1
693// The primaries are written after they have been transported and occupy
694// fNtrack - fNprimary .. fNtrack - 1
695
e94530da 696 Int_t entry;
697 if (id<fNprimary)
698 entry = id+fNtrack-fNprimary;
699 else
700 entry = id-fNprimary;
701 return entry;
702}
703
5d8718b8 704//_____________________________________________________________________________
642f15cf 705Int_t AliStack::GetCurrentParentTrackNumber() const
5d8718b8 706{
707 //
708 // Return number of the parent of the current track
709 //
710
711 TParticle* current = (TParticle*)fParticleMap->At(fCurrent);
712
713 if (current)
714 return current->GetFirstMother();
715 else {
21bf7095 716 AliWarning("Current track not found in the stack");
5d8718b8 717 return -1;
718 }
719}
720
9e1a0ddb 721//_____________________________________________________________________________
f5f55563 722Int_t AliStack::GetPrimary(Int_t id)
9e1a0ddb 723{
724 //
725 // Return number of primary that has generated track
726 //
727
728 int current, parent;
9e1a0ddb 729 //
730 parent=id;
731 while (1) {
732 current=parent;
f5f55563 733 parent=Particle(current)->GetFirstMother();
9e1a0ddb 734 if(parent<0) return current;
735 }
736}
737
738//_____________________________________________________________________________
739void AliStack::DumpPart (Int_t i) const
740{
741 //
742 // Dumps particle i in the stack
743 //
e2afb3b6 744 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
9e1a0ddb 745}
746
747//_____________________________________________________________________________
748void AliStack::DumpPStack ()
749{
750 //
751 // Dumps the particle stack
752 //
753
3ab6f951 754 Int_t i;
755
88cb7938 756 printf("\n\n=======================================================================\n");
3ab6f951 757 for (i=0;i<fNtrack;i++)
9e1a0ddb 758 {
759 TParticle* particle = Particle(i);
760 if (particle) {
761 printf("-> %d ",i); particle->Print();
762 printf("--------------------------------------------------------------\n");
763 }
764 else
765 Warning("DumpPStack", "No particle with id %d.", i);
766 }
767
88cb7938 768 printf("\n=======================================================================\n\n");
9e1a0ddb 769
770 // print particle file map
771 printf("\nParticle file map: \n");
3ab6f951 772 for (i=0; i<fNtrack; i++)
9e1a0ddb 773 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
774}
775
776
777//_____________________________________________________________________________
778void AliStack::DumpLoadedStack() const
779{
780 //
781 // Dumps the particle in the stack
782 // that are loaded in memory.
783 //
784
785 TObjArray &particles = *fParticleMap;
786 printf(
787 "\n\n=======================================================================\n");
788 for (Int_t i=0;i<fNtrack;i++)
789 {
e2afb3b6 790 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
9e1a0ddb 791 if (particle) {
792 printf("-> %d ",i); particle->Print();
793 printf("--------------------------------------------------------------\n");
794 }
795 else {
796 printf("-> %d Particle not loaded.\n",i);
797 printf("--------------------------------------------------------------\n");
798 }
799 }
800 printf(
801 "\n=======================================================================\n\n");
802}
803
804//
805// protected methods
806//
807
808//_____________________________________________________________________________
809void AliStack::CleanParents()
810{
811 //
812 // Clean particles stack
813 // Set parent/daughter relations
814 //
815
816 TObjArray &particles = *fParticleMap;
817 TParticle *part;
818 int i;
819 for(i=0; i<fHgwmk+1; i++) {
e2afb3b6 820 part = dynamic_cast<TParticle*>(particles.At(i));
9e1a0ddb 821 if(part) if(!part->TestBit(kDaughtersBit)) {
822 part->SetFirstDaughter(-1);
823 part->SetLastDaughter(-1);
824 }
825 }
826}
827
828//_____________________________________________________________________________
829TParticle* AliStack::GetNextParticle()
830{
831 //
832 // Return next particle from stack of particles
833 //
834
835 TParticle* particle = 0;
836
837 // search secondaries
838 //for(Int_t i=fNtrack-1; i>=0; i--) {
839 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
e2afb3b6 840 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
9e1a0ddb 841 if ((particle) && (!particle->TestBit(kDoneBit))) {
842 fCurrent=i;
9e1a0ddb 843 return particle;
844 }
845 }
846
847 // take next primary if all secondaries were done
848 while (fCurrentPrimary>=0) {
849 fCurrent = fCurrentPrimary;
e2afb3b6 850 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
9e1a0ddb 851 if ((particle) && (!particle->TestBit(kDoneBit))) {
9e1a0ddb 852 return particle;
853 }
854 }
855
856 // nothing to be tracked
857 fCurrent = -1;
6308b375 858
859
9e1a0ddb 860 return particle;
861}
88cb7938 862//__________________________________________________________________________________________
9e1a0ddb 863
88cb7938 864TTree* AliStack::TreeK()
865{
866//returns TreeK
942a9039 867 return fTreeK;
88cb7938 868}
9e1a0ddb 869//__________________________________________________________________________________________
88cb7938 870
942a9039 871void AliStack::ConnectTree(TTree* tree)
9e1a0ddb 872{
873//
88cb7938 874// Creates branch for writing particles
875//
942a9039 876
877 fTreeK = tree;
878
21bf7095 879 AliDebug(1, "Connecting TreeK");
88cb7938 880 if (fTreeK == 0x0)
881 {
882 if (TreeK() == 0x0)
883 {
21bf7095 884 AliFatal("Parameter is NULL");//we don't like such a jokes
88cb7938 885 return;
886 }
887 return;//in this case TreeK() calls back this method (ConnectTree)
888 //tree after setting fTreeK, the rest was already executed
889 //it is safe to return now
890 }
891
892 // Create a branch for particles
893
21bf7095 894 AliDebug(2, Form("Tree name is %s",fTreeK->GetName()));
88cb7938 895
896 if (fTreeK->GetDirectory())
897 {
21bf7095 898 AliDebug(2, Form("and dir is %s",fTreeK->GetDirectory()->GetName()));
88cb7938 899 }
900 else
21bf7095 901 AliWarning("DIR IS NOT SET !!!");
88cb7938 902
942a9039 903 TBranch *branch=fTreeK->GetBranch("Particles");
88cb7938 904 if(branch == 0x0)
905 {
942a9039 906 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
21bf7095 907 AliDebug(2, "Creating Branch in Tree");
88cb7938 908 }
909 else
910 {
21bf7095 911 AliDebug(2, "Branch Found in Tree");
88cb7938 912 branch->SetAddress(&fParticleBuffer);
913 }
914 if (branch->GetDirectory())
915 {
21bf7095 916 AliDebug(1, Form("Branch Dir Name is %s",branch->GetDirectory()->GetName()));
88cb7938 917 }
918 else
21bf7095 919 AliWarning("Branch Dir is NOT SET");
9e1a0ddb 920}
88cb7938 921//__________________________________________________________________________________________
9e1a0ddb 922
88cb7938 923
924void AliStack::BeginEvent()
9e1a0ddb 925{
926// start a new event
88cb7938 927 Reset();
9e1a0ddb 928}
929
81694e6a 930//_____________________________________________________________________________
9e1a0ddb 931void AliStack::FinishRun()
932{
933// Clean TreeK information
9e1a0ddb 934}
5d8718b8 935//_____________________________________________________________________________
88cb7938 936
937Bool_t AliStack::GetEvent()
9e1a0ddb 938{
939//
940// Get new event from TreeK
ccf7a81f 941
9e1a0ddb 942 // Reset/Create the particle stack
88cb7938 943 Int_t size = (Int_t)TreeK()->GetEntries();
9e1a0ddb 944 ResetArrays(size);
ccf7a81f 945 return kTRUE;
9e1a0ddb 946}
88cb7938 947//_____________________________________________________________________________
948
1fed0e8b 949Bool_t AliStack::IsStable(Int_t pdg) const
950{
951//
952// Decide whether particle (pdg) is stable
953//
954
955 const Int_t kNstable = 14;
956 Int_t i;
957
958 Int_t pdgStable[kNstable] = {
959 kGamma, // Photon
960 kElectron, // Electron
961 kMuonPlus, // Muon
962 kPiPlus, // Pion
963 kKPlus, // Kaon
964 kProton, // Proton
965 kNeutron, // Neutron
966 kLambda0, // Lambda_0
967 kSigmaMinus, // Sigma Minus
968 kSigma0, // Sigma_0
969 kSigmaPlus, // Sigma Plus
970 3312, // Xsi Minus
971 3322, // Xsi
972 3334, // Omega
973 };
974
975 Bool_t isStable = kFALSE;
976 for (i = 0; i < kNstable; i++) {
977 if (pdg == TMath::Abs(pdgStable[i])) {
978 isStable = kTRUE;
979 break;
980 }
981 }
982
983 return isStable;
984}
985
986Bool_t AliStack::IsPhysicalPrimary(Int_t index)
987{
988 //
989 // Test if a particle is a physical primary according to the following definition:
990 // Particles produced in the collision including products of strong and
991 // electromagnetic decay and excluding feed-down from weak decays of strange
992 // particles.
993 //
994 TParticle* p = Particle(index);
995 Int_t ist = p->GetStatusCode();
996
997 //
998 // Initial state particle
999 if (ist > 20) return kFALSE;
1000
1001 Int_t pdg = TMath::Abs(p->GetPdgCode());
1002
1003 if (!IsStable(pdg)) return kFALSE;
1004
1005 if (index < GetNprimary()) {
1006//
1007// Particle produced by generator
1008 return kTRUE;
1009 } else {
1010//
1011// Particle produced during transport
1012//
1013// Check if this is a heavy flavor decay product
1014 Int_t imo = p->GetFirstMother();
1015 TParticle* pm = Particle(imo);
1016 Int_t mpdg = TMath::Abs(pm->GetPdgCode());
1017 Int_t mfl = Int_t (mpdg / TMath::Power(10, Int_t(TMath::Log10(mpdg))));
1018 //
1019 // Light hadron
1020 if (mfl < 4) return kFALSE;
1021
1022 //
1023 // Heavy flavor hadron produced by generator
1024 if (imo < GetNprimary()) {
1025 return kTRUE;
1026 }
1027
1028 // To be sure that heavy flavor has not been produced in a secondary interaction
1029 // Loop back to the generated mother
1030 while (imo >= GetNprimary()) {
1031 imo = p->GetFirstMother();
1032 pm = Particle(imo);
1033 }
1034 mpdg = TMath::Abs(pm->GetPdgCode());
1035 mfl = Int_t (mpdg / TMath::Power(10, Int_t(TMath::Log10(mpdg))));
1036
1037 if (mfl < 4) {
1038 return kFALSE;
1039 } else {
1040 return kTRUE;
1041 }
1042 } // produced by generator ?
1043}
1044