]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliStack.cxx
Change fgkRpadW to 1.0 cm for new pad plane
[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
16/*
17$Log$
e2afb3b6 18Revision 1.25 2002/10/22 15:21:39 hristov
19New methods for more effective access of particles from TreeK (J.Chudoba)
20
e94530da 21Revision 1.24 2002/10/22 15:02:15 alibrary
22Introducing Riostream.h
23
b16a1b1e 24Revision 1.23 2002/10/14 14:57:32 hristov
25Merging the VirtualMC branch to the main development branch (HEAD)
26
b9d0a01d 27Revision 1.19.4.2 2002/08/28 15:06:52 alibrary
28Updating to v3-09-01
29
30Revision 1.19.4.1 2002/06/10 14:43:06 hristov
31Merged with v3-08-02
32
ae9676e4 33Revision 1.21 2002/05/28 14:24:57 hristov
34Correct warning messages
35
3d8c4988 36Revision 1.20 2002/04/30 11:47:30 morsch
37KeepPhysics method called by PurifyKine added (N. Carrer, A.M.)
38
e44c5340 39Revision 1.19 2002/03/12 11:06:03 morsch
40Add particle status code to argument list of SetTrack(..).
41
47c8bcbe 42Revision 1.18 2002/02/20 16:14:41 hristov
43fParticleBuffer points to object which doesn't belong to AliStack, do not delete it (J.Chudoba)
44
ba9938e6 45Revision 1.17 2001/12/05 08:51:56 hristov
46The default constructor now creates no objects (thanks to r.Brun). Some corrections required by the previous changes.
47
be3380a3 48Revision 1.16 2001/11/20 09:27:55 hristov
49Possibility to investigate a primary of not yet loaded particle (I.Hrivnacova)
50
f5f55563 51Revision 1.15 2001/09/04 15:10:37 hristov
52Additional protection is included to avoid some problems using Hijing
53
1305ca2f 54Revision 1.14 2001/08/30 09:44:06 hristov
55VertexSource_t added to avoid the warnings
56
da2af24c 57Revision 1.13 2001/08/29 13:31:42 morsch
58Protection against (fTreeK == 0) in destructor.
59
cfb2cc9c 60Revision 1.12 2001/07/27 13:03:13 hristov
61Default Branch split level set to 99
62
d0f40f23 63Revision 1.11 2001/07/27 12:34:20 jchudoba
64remove the dummy argument in GetEvent method
65
cec14bc5 66Revision 1.10 2001/07/20 10:13:54 morsch
67In Particle(Int_t) use GetEntriesFast to speed up the procedure.
68
911a3148 69Revision 1.9 2001/07/03 08:10:57 hristov
70J.Chudoba's changes merged correctly with the HEAD
71
d1750cff 72Revision 1.6 2001/05/31 06:59:06 fca
73Clean setting and deleting of fParticleBuffer
74
17f4a3d9 75Revision 1.5 2001/05/30 12:18:46 hristov
76Loop variables declared once
77
3ab6f951 78Revision 1.4 2001/05/25 07:25:20 hristov
79AliStack destructor corrected (I.Hrivnacova)
80
81694e6a 81Revision 1.3 2001/05/22 14:33:16 hristov
82Minor changes
83
ed54bf31 84Revision 1.2 2001/05/17 05:49:39 fca
85Reset pointers to daughters
86
76969085 87Revision 1.1 2001/05/16 14:57:22 alibrary
88New files for folders and Stack
89
9e1a0ddb 90*/
91
92///////////////////////////////////////////////////////////////////////////////
93// //
94// Particles stack class
95// //
96///////////////////////////////////////////////////////////////////////////////
97
b16a1b1e 98#include <Riostream.h>
9e1a0ddb 99
100#include <TObjArray.h>
101#include <TParticle.h>
102#include <TTree.h>
103#include <TFile.h>
104#include <TFolder.h>
105#include <TROOT.h>
106
107#include "AliStack.h"
108#include "AliRun.h"
109#include "AliModule.h"
110#include "AliHit.h"
9e1a0ddb 111
112ClassImp(AliStack)
113
e2afb3b6 114//_______________________________________________________________________
115AliStack::AliStack():
116 fParticles(0),
117 fParticleMap(0),
118 fParticleFileMap(0),
119 fParticleBuffer(0),
120 fTreeK(0),
121 fNtrack(0),
122 fNprimary(0),
123 fCurrent(-1),
124 fCurrentPrimary(-1),
125 fHgwmk(0),
126 fLoadPoint(0)
9e1a0ddb 127{
128 //
e2afb3b6 129 // Default constructor
9e1a0ddb 130 //
9e1a0ddb 131}
132
e2afb3b6 133//_______________________________________________________________________
134AliStack::AliStack(Int_t size):
135 fParticles(new TClonesArray("TParticle",1000)),
136 fParticleMap(new TObjArray(size)),
137 fParticleFileMap(0),
138 fParticleBuffer(0),
139 fTreeK(0),
140 fNtrack(0),
141 fNprimary(0),
142 fCurrent(-1),
143 fCurrentPrimary(-1),
144 fHgwmk(0),
145 fLoadPoint(0)
146{
147 //
148 // Constructor
149 //
150}
9e1a0ddb 151
e2afb3b6 152//_______________________________________________________________________
153AliStack::AliStack(const AliStack& st):
154 TVirtualMCStack(st),
155 fParticles(0),
156 fParticleMap(0),
157 fParticleFileMap(0),
158 fParticleBuffer(0),
159 fTreeK(0),
160 fNtrack(0),
161 fNprimary(0),
162 fCurrent(-1),
163 fCurrentPrimary(-1),
164 fHgwmk(0),
165 fLoadPoint(0)
9e1a0ddb 166{
167 //
e2afb3b6 168 // Copy constructor
9e1a0ddb 169 //
e2afb3b6 170 st.Copy(*this);
9e1a0ddb 171}
172
e2afb3b6 173//_______________________________________________________________________
174void AliStack::Copy(AliStack&) const
175{
176 Fatal("Copy","Not implemented!\n");
177}
9e1a0ddb 178
e2afb3b6 179//_______________________________________________________________________
9e1a0ddb 180AliStack::~AliStack()
181{
182 //
183 // Destructor
184 //
185
186 if (fParticles) {
187 fParticles->Delete();
188 delete fParticles;
189 }
190 delete fParticleMap;
e2afb3b6 191 delete fTreeK;
9e1a0ddb 192}
193
194//
195// public methods
196//
197
198//_____________________________________________________________________________
199void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
200 Float_t *vpos, Float_t *polar, Float_t tof,
47c8bcbe 201 AliMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
9e1a0ddb 202{
203 //
204 // Load a track on the stack
205 //
206 // done 0 if the track has to be transported
207 // 1 if not
208 // parent identifier of the parent track. -1 for a primary
209 // pdg particle code
210 // pmom momentum GeV/c
211 // vpos position
212 // polar polarisation
213 // tof time of flight in seconds
214 // mecha production mechanism
215 // ntr on output the number of the track stored
216 //
217
9e1a0ddb 218 // const Float_t tlife=0;
219
220 //
221 // Here we get the static mass
222 // For MC is ok, but a more sophisticated method could be necessary
223 // if the calculated mass is required
224 // also, this method is potentially dangerous if the mass
225 // used in the MC is not the same of the PDG database
226 //
b9d0a01d 227 Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
9e1a0ddb 228 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
229 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
230
ccf7a81f 231// 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",
232// mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
233
9e1a0ddb 234
b9d0a01d 235 SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
236 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
237 mech, ntr, weight, is);
9e1a0ddb 238}
239
240//_____________________________________________________________________________
241void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
242 Double_t px, Double_t py, Double_t pz, Double_t e,
243 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
244 Double_t polx, Double_t poly, Double_t polz,
b9d0a01d 245 AliMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
9e1a0ddb 246{
247 //
248 // Load a track on the stack
249 //
250 // done 0 if the track has to be transported
251 // 1 if not
252 // parent identifier of the parent track. -1 for a primary
253 // pdg particle code
254 // kS generation status code
255 // px, py, pz momentum GeV/c
256 // vx, vy, vz position
257 // polar polarisation
258 // tof time of flight in seconds
259 // mech production mechanism
260 // ntr on output the number of the track stored
261 //
262 // New method interface:
263 // arguments were changed to be in correspondence with TParticle
264 // constructor.
265 // Note: the energy is not calculated from the static mass but
266 // it is passed by argument e.
267
268
9e1a0ddb 269 const Int_t kFirstDaughter=-1;
270 const Int_t kLastDaughter=-1;
271
272 TClonesArray &particles = *fParticles;
273 TParticle* particle
274 = new(particles[fLoadPoint++])
47c8bcbe 275 TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
9e1a0ddb 276 px, py, pz, e, vx, vy, vz, tof);
277
278 particle->SetPolarisation(polx, poly, polz);
279 particle->SetWeight(weight);
280 particle->SetUniqueID(mech);
281
282 if(!done) particle->SetBit(kDoneBit);
283
284 // Declare that the daughter information is valid
285 particle->SetBit(kDaughtersBit);
286 // Add the particle to the stack
287 fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
288
289 if(parent>=0) {
e2afb3b6 290 particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
1305ca2f 291 if (particle) {
292 particle->SetLastDaughter(fNtrack);
293 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
294 }
295 else {
296 printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
297 }
9e1a0ddb 298 }
299 else {
300 //
301 // This is a primary track. Set high water mark for this event
302 fHgwmk = fNtrack;
303 //
304 // Set also number if primary tracks
305 fNprimary = fHgwmk+1;
306 fCurrentPrimary++;
307 }
308 ntr = fNtrack++;
309}
310
311//_____________________________________________________________________________
b9d0a01d 312TParticle* AliStack::GetNextTrack(Int_t& itrack)
313{
314 //
315 // Returns next track from stack of particles
316 //
317
318
319 TParticle* track = GetNextParticle();
320
321 if (track) {
322 itrack = fCurrent;
323 track->SetBit(kDoneBit);
324 }
325 else
326 itrack = -1;
327
328 return track;
329}
330
331/*
332//_____________________________________________________________________________
333void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
334 Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
335 Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
336 Double_t& polx, Double_t& poly, Double_t& polz)
9e1a0ddb 337{
338 //
339 // Return next track from stack of particles
340 //
341
342
343 TParticle* track = GetNextParticle();
ccf7a81f 344// cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
9e1a0ddb 345
b9d0a01d 346 if (track) {
347 itrack = fCurrent;
348 pdg = track->GetPdgCode();
349 px = track->Px();
350 py = track->Py();
351 pz = track->Pz();
352 e = track->Energy();
353 vx = track->Vx();
354 vy = track->Vy();
355 vz = track->Vz();
356 tof = track->T();
9e1a0ddb 357 TVector3 pol;
358 track->GetPolarisation(pol);
b9d0a01d 359 polx = pol.X();
360 poly = pol.Y();
361 polz = pol.Z();
9e1a0ddb 362 track->SetBit(kDoneBit);
ccf7a81f 363// cout << "Filled params" << endl;
9e1a0ddb 364 }
365 else
b9d0a01d 366 itrack = -1;
9e1a0ddb 367
368 //
369 // stop and start timer when we start a primary track
370 Int_t nprimaries = fNprimary;
371 if (fCurrent >= nprimaries) return;
372 if (fCurrent < nprimaries-1) {
373 fTimer.Stop();
374 track=(TParticle*) fParticleMap->At(fCurrent+1);
375 // track->SetProcessTime(fTimer.CpuTime());
376 }
377 fTimer.Start();
378}
379
b9d0a01d 380*/
381//_____________________________________________________________________________
382TParticle* AliStack::GetPrimaryForTracking(Int_t i)
383{
384 //
385 // Returns i-th primary particle if it is flagged to be tracked,
386 // 0 otherwise
387 //
388
389 TParticle* particle = Particle(i);
390
391 if (!particle->TestBit(kDoneBit))
392 return particle;
393 else
394 return 0;
395}
396
9e1a0ddb 397
398//_____________________________________________________________________________
399void AliStack::PurifyKine()
400{
401 //
402 // Compress kinematic tree keeping only flagged particles
403 // and renaming the particle id's in all the hits
404 //
405
406 TObjArray &particles = *fParticleMap;
407 int nkeep=fHgwmk+1, parent, i;
408 TParticle *part, *father;
409 TArrayI map(particles.GetLast()+1);
410
411 // Save in Header total number of tracks before compression
412
413 // If no tracks generated return now
414 if(fHgwmk+1 == fNtrack) return;
415
9e1a0ddb 416 // First pass, invalid Daughter information
417 for(i=0; i<fNtrack; i++) {
418 // Preset map, to be removed later
419 if(i<=fHgwmk) map[i]=i ;
420 else {
421 map[i] = -99;
e2afb3b6 422 if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
e44c5340 423//
424// Check of this track should be kept for physics reasons
425 if (KeepPhysics(part)) KeepTrack(i);
426//
76969085 427 part->ResetBit(kDaughtersBit);
428 part->SetFirstDaughter(-1);
429 part->SetLastDaughter(-1);
430 }
9e1a0ddb 431 }
432 }
433 // Invalid daughter information for the parent of the first particle
434 // generated. This may or may not be the current primary according to
435 // whether decays have been recorded among the primaries
e2afb3b6 436 part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
9e1a0ddb 437 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
438 // Second pass, build map between old and new numbering
439 for(i=fHgwmk+1; i<fNtrack; i++) {
440 if(particles.At(i)->TestBit(kKeepBit)) {
441
442 // This particle has to be kept
443 map[i]=nkeep;
444 // If old and new are different, have to move the pointer
445 if(i!=nkeep) particles[nkeep]=particles.At(i);
e2afb3b6 446 part = dynamic_cast<TParticle*>(particles.At(nkeep));
9e1a0ddb 447
448 // as the parent is always *before*, it must be already
449 // in place. This is what we are checking anyway!
450 if((parent=part->GetFirstMother())>fHgwmk)
451 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
452 else part->SetFirstMother(map[parent]);
453
454 nkeep++;
455 }
456 }
457
458 // Fix daughters information
459 for (i=fHgwmk+1; i<nkeep; i++) {
e2afb3b6 460 part = dynamic_cast<TParticle*>(particles.At(i));
9e1a0ddb 461 parent = part->GetFirstMother();
462 if(parent>=0) {
e2afb3b6 463 father = dynamic_cast<TParticle*>(particles.At(parent));
9e1a0ddb 464 if(father->TestBit(kDaughtersBit)) {
465
466 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
467 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
468 } else {
469 // Initialise daughters info for first pass
470 father->SetFirstDaughter(i);
471 father->SetLastDaughter(i);
472 father->SetBit(kDaughtersBit);
473 }
474 }
475 }
476
477 // Now loop on all registered hit lists
478 TList* hitLists = gAlice->GetHitLists();
479 TIter next(hitLists);
480 TCollection *hitList;
e2afb3b6 481 while((hitList = dynamic_cast<TCollection*>(next()))) {
9e1a0ddb 482 TIter nexthit(hitList);
483 AliHit *hit;
e2afb3b6 484 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
9e1a0ddb 485 hit->SetTrack(map[hit->GetTrack()]);
486 }
487 }
488
489 //
490 // This for detectors which have a special mapping mechanism
491 // for hits, such as TPC and TRD
492 //
493
494 TObjArray* modules = gAlice->Modules();
495 TIter nextmod(modules);
496 AliModule *detector;
e2afb3b6 497 while((detector = dynamic_cast<AliModule*>(nextmod()))) {
9e1a0ddb 498 detector->RemapTrackHitIDs(map.GetArray());
ae9676e4 499 detector->RemapTrackReferencesIDs(map.GetArray());
9e1a0ddb 500 }
501
502 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
503 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
504
505 for (i=fHgwmk+1; i<nkeep; ++i) {
e2afb3b6 506 fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
507 fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
9e1a0ddb 508 fTreeK->Fill();
17f4a3d9 509 particles[i]=fParticleBuffer=0;
9e1a0ddb 510 }
511
512 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
513
76969085 514 Int_t toshrink = fNtrack-fHgwmk-1;
9e1a0ddb 515 fLoadPoint-=toshrink;
516 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
517
518 fNtrack=nkeep;
519 fHgwmk=nkeep-1;
520 // delete [] map;
521}
522
e44c5340 523Bool_t AliStack::KeepPhysics(TParticle* part)
524{
525 //
526 // Some particles have to kept on the stack for reasons motivated
527 // by physics analysis. Decision is put here.
528 //
529 Bool_t keep = kFALSE;
530 //
531 // Keep first-generation daughter from primaries with heavy flavor
532 //
533 Int_t parent = part->GetFirstMother();
534 if (parent >= 0 && parent <= fHgwmk) {
e2afb3b6 535 TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
e44c5340 536 Int_t kf = father->GetPdgCode();
537 kf = TMath::Abs(kf);
538 Int_t kfl = kf;
539 // meson ?
540 if (kfl > 10) kfl/=100;
541 // baryon
542 if (kfl > 10) kfl/=10;
543 if (kfl > 10) kfl/=10;
544 if (kfl >= 4) {
545 keep = kTRUE;
546 }
547 }
548 return keep;
549}
550
9e1a0ddb 551//_____________________________________________________________________________
552void AliStack::FinishEvent()
553{
554 //
555 // Write out the kinematics that was not yet filled
556 //
557
558 // Update event header
559
560
561 if (!fTreeK) {
562// Fatal("FinishEvent", "No kinematics tree is defined.");
563// Don't panic this is a probably a lego run
564 return;
565
566 }
567
568 CleanParents();
569 if(fTreeK->GetEntries() ==0) {
570 // set the fParticleFileMap size for the first time
571 fParticleFileMap.Set(fHgwmk+1);
572 }
573
574 Bool_t allFilled = kFALSE;
575 TObject *part;
576 for(Int_t i=0; i<fHgwmk+1; ++i)
577 if((part=fParticleMap->At(i))) {
e2afb3b6 578 fParticleBuffer = dynamic_cast<TParticle*>(part);
579 fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
9e1a0ddb 580 fTreeK->Fill();
da2af24c 581 //PH (*fParticleMap)[i]=fParticleBuffer=0;
582 fParticleBuffer=0;
583 fParticleMap->AddAt(0,i);
9e1a0ddb 584
585 // When all primaries were filled no particle!=0
586 // should be left => to be removed later.
587 if (allFilled) printf("Why != 0 part # %d?\n",i);
588 }
589 else {
590 // // printf("Why = 0 part # %d?\n",i); => We know.
591 // break;
592 // we don't break now in order to be sure there is no
593 // particle !=0 left.
594 // To be removed later and replaced with break.
595 if(!allFilled) allFilled = kTRUE;
596 }
ccf7a81f 597// cout << "Nof particles: " << fNtrack << endl;
9e1a0ddb 598 //Reset();
599}
600
601//_____________________________________________________________________________
602void AliStack::FlagTrack(Int_t track)
603{
604 //
605 // Flags a track and all its family tree to be kept
606 //
607
608 TParticle *particle;
609
610 Int_t curr=track;
611 while(1) {
e2afb3b6 612 particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
9e1a0ddb 613
614 // If the particle is flagged the three from here upward is saved already
615 if(particle->TestBit(kKeepBit)) return;
616
617 // Save this particle
618 particle->SetBit(kKeepBit);
619
620 // Move to father if any
621 if((curr=particle->GetFirstMother())==-1) return;
622 }
623}
624
625//_____________________________________________________________________________
ed54bf31 626void AliStack::KeepTrack(Int_t track)
9e1a0ddb 627{
628 //
629 // Flags a track to be kept
630 //
631
632 fParticleMap->At(track)->SetBit(kKeepBit);
633}
634
635//_____________________________________________________________________________
636void AliStack::Reset(Int_t size)
637{
638 //
639 // Resets stack
640 //
641
642 fNtrack=0;
643 fNprimary=0;
644 fHgwmk=0;
645 fLoadPoint=0;
646 fCurrent = -1;
647 ResetArrays(size);
648}
649
650//_____________________________________________________________________________
651void AliStack::ResetArrays(Int_t size)
652{
653 //
654 // Resets stack arrays
655 //
656
be3380a3 657 if (fParticles)
658 fParticles->Clear();
659 else
660 fParticles = new TClonesArray("TParticle",1000);
661 if (fParticleMap) {
662 fParticleMap->Clear();
663 if (size>0) fParticleMap->Expand(size);}
664 else
665 fParticleMap = new TObjArray(size);
9e1a0ddb 666}
667
668//_____________________________________________________________________________
e2afb3b6 669void AliStack::SetHighWaterMark(Int_t)
9e1a0ddb 670{
671 //
672 // Set high water mark for last track in event
673 //
674
675 fHgwmk = fNtrack-1;
676 fCurrentPrimary=fHgwmk;
677
678 // Set also number of primary tracks
679 fNprimary = fHgwmk+1;
680 fNtrack = fHgwmk+1;
681}
682
683//_____________________________________________________________________________
684TParticle* AliStack::Particle(Int_t i)
685{
686 //
687 // Return particle with specified ID
688
da2af24c 689 //PH if(!(*fParticleMap)[i]) {
690 if(!fParticleMap->At(i)) {
911a3148 691 Int_t nentries = fParticles->GetEntriesFast();
9e1a0ddb 692 // algorithmic way of getting entry index
693 // (primary particles are filled after secondaries)
e94530da 694 Int_t entry = TreeKEntry(i);
9e1a0ddb 695 // check whether algorithmic way and
696 // and the fParticleFileMap[i] give the same;
697 // give the fatal error if not
698 if (entry != fParticleFileMap[i]) {
699 Fatal("Particle",
700 "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
701 entry, fParticleFileMap[i]);
702 }
703
704 fTreeK->GetEntry(entry);
705 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
706 fParticleMap->AddAt((*fParticles)[nentries],i);
707 }
e2afb3b6 708 //PH return dynamic_cast<TParticle *>((*fParticleMap)[i]);
709 return dynamic_cast<TParticle*>(fParticleMap->At(i));
9e1a0ddb 710}
711
e94530da 712//_____________________________________________________________________________
713TParticle* AliStack::ParticleFromTreeK(Int_t id) const
714{
715//
716// return pointer to TParticle with label id
717//
718 Int_t entry;
719 if ((entry = TreeKEntry(id)) < 0) return 0;
720 if (fTreeK->GetEntry(entry)<=0) return 0;
721 return fParticleBuffer;
722}
723
724//_____________________________________________________________________________
725Int_t AliStack::TreeKEntry(Int_t id) const
726{
727//
728// return entry number in the TreeK for particle with label id
729// return negative number if label>fNtrack
730//
731 Int_t entry;
732 if (id<fNprimary)
733 entry = id+fNtrack-fNprimary;
734 else
735 entry = id-fNprimary;
736 return entry;
737}
738
9e1a0ddb 739//_____________________________________________________________________________
f5f55563 740Int_t AliStack::GetPrimary(Int_t id)
9e1a0ddb 741{
742 //
743 // Return number of primary that has generated track
744 //
745
746 int current, parent;
9e1a0ddb 747 //
748 parent=id;
749 while (1) {
750 current=parent;
f5f55563 751 parent=Particle(current)->GetFirstMother();
9e1a0ddb 752 if(parent<0) return current;
753 }
754}
755
756//_____________________________________________________________________________
757void AliStack::DumpPart (Int_t i) const
758{
759 //
760 // Dumps particle i in the stack
761 //
762
e2afb3b6 763 //PH dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
764 dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
9e1a0ddb 765}
766
767//_____________________________________________________________________________
768void AliStack::DumpPStack ()
769{
770 //
771 // Dumps the particle stack
772 //
773
3ab6f951 774 Int_t i;
775
9e1a0ddb 776 printf(
777 "\n\n=======================================================================\n");
3ab6f951 778 for (i=0;i<fNtrack;i++)
9e1a0ddb 779 {
780 TParticle* particle = Particle(i);
781 if (particle) {
782 printf("-> %d ",i); particle->Print();
783 printf("--------------------------------------------------------------\n");
784 }
785 else
786 Warning("DumpPStack", "No particle with id %d.", i);
787 }
788
789 printf(
790 "\n=======================================================================\n\n");
791
792 // print particle file map
793 printf("\nParticle file map: \n");
3ab6f951 794 for (i=0; i<fNtrack; i++)
9e1a0ddb 795 printf(" %d th entry: %d \n",i,fParticleFileMap[i]);
796}
797
798
799//_____________________________________________________________________________
800void AliStack::DumpLoadedStack() const
801{
802 //
803 // Dumps the particle in the stack
804 // that are loaded in memory.
805 //
806
807 TObjArray &particles = *fParticleMap;
808 printf(
809 "\n\n=======================================================================\n");
810 for (Int_t i=0;i<fNtrack;i++)
811 {
e2afb3b6 812 TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
9e1a0ddb 813 if (particle) {
814 printf("-> %d ",i); particle->Print();
815 printf("--------------------------------------------------------------\n");
816 }
817 else {
818 printf("-> %d Particle not loaded.\n",i);
819 printf("--------------------------------------------------------------\n");
820 }
821 }
822 printf(
823 "\n=======================================================================\n\n");
824}
825
826//
827// protected methods
828//
829
830//_____________________________________________________________________________
831void AliStack::CleanParents()
832{
833 //
834 // Clean particles stack
835 // Set parent/daughter relations
836 //
837
838 TObjArray &particles = *fParticleMap;
839 TParticle *part;
840 int i;
841 for(i=0; i<fHgwmk+1; i++) {
e2afb3b6 842 part = dynamic_cast<TParticle*>(particles.At(i));
9e1a0ddb 843 if(part) if(!part->TestBit(kDaughtersBit)) {
844 part->SetFirstDaughter(-1);
845 part->SetLastDaughter(-1);
846 }
847 }
848}
849
850//_____________________________________________________________________________
851TParticle* AliStack::GetNextParticle()
852{
853 //
854 // Return next particle from stack of particles
855 //
856
857 TParticle* particle = 0;
858
859 // search secondaries
860 //for(Int_t i=fNtrack-1; i>=0; i--) {
861 for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
e2afb3b6 862 particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
9e1a0ddb 863 if ((particle) && (!particle->TestBit(kDoneBit))) {
864 fCurrent=i;
865 //cout << "GetNextParticle() - secondary "
866 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
867 return particle;
868 }
869 }
870
871 // take next primary if all secondaries were done
872 while (fCurrentPrimary>=0) {
873 fCurrent = fCurrentPrimary;
e2afb3b6 874 particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
9e1a0ddb 875 if ((particle) && (!particle->TestBit(kDoneBit))) {
876 //cout << "GetNextParticle() - primary "
877 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
878 return particle;
879 }
880 }
881
882 // nothing to be tracked
883 fCurrent = -1;
884 //cout << "GetNextParticle() - none "
885 // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
886 return particle;
887}
888
889//__________________________________________________________________________________________
e2afb3b6 890void AliStack::MakeTree(Int_t event, const char * /*file*/)
9e1a0ddb 891{
892//
893// Make Kine tree and creates branch for writing particles
894//
895 TBranch *branch=0;
896 // Make Kinematics Tree
897 char hname[30];
9e1a0ddb 898 if (!fTreeK) {
899 sprintf(hname,"TreeK%d",event);
900 fTreeK = new TTree(hname,"Kinematics");
901 // Create a branch for particles
d0f40f23 902 branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
9e1a0ddb 903 fTreeK->Write(0,TObject::kOverwrite);
904 }
905}
906
81694e6a 907//_____________________________________________________________________________
9e1a0ddb 908void AliStack::BeginEvent(Int_t event)
909{
910// start a new event
911//
912//
913 fNprimary = 0;
914 fNtrack = 0;
915
916 char hname[30];
917 if(fTreeK) {
918 fTreeK->Reset();
919 sprintf(hname,"TreeK%d",event);
920 fTreeK->SetName(hname);
921 }
922}
923
81694e6a 924//_____________________________________________________________________________
9e1a0ddb 925void AliStack::FinishRun()
926{
927// Clean TreeK information
928 if (fTreeK) {
929 delete fTreeK; fTreeK = 0;
930 }
931}
932
cec14bc5 933Bool_t AliStack::GetEvent(Int_t event)
9e1a0ddb 934{
935//
936// Get new event from TreeK
ccf7a81f 937
9e1a0ddb 938 // Reset/Create the particle stack
939 if (fTreeK) delete fTreeK;
940
941 // Get Kine Tree from file
942 char treeName[20];
943 sprintf(treeName,"TreeK%d",event);
e2afb3b6 944 fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
9e1a0ddb 945
ccf7a81f 946 if (fTreeK)
947 fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
948 else {
3d8c4988 949 // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
950 Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
ccf7a81f 951 return kFALSE;
952 }
953// printf("\n primaries %d", fNprimary);
954// printf("\n tracks %d", fNtrack);
955
e2afb3b6 956 Int_t size = static_cast<Int_t>(fTreeK->GetEntries());
9e1a0ddb 957 ResetArrays(size);
ccf7a81f 958 return kTRUE;
9e1a0ddb 959}