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