1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 // This class is extracted from the AliRun class
19 // and contains all the MC-related functionality
20 // The number of dependencies has to be reduced...
21 // Author: F.Carminati
22 // Federico.Carminati@cern.ch
28 #include <TClonesArray.h>
30 #include <TGeoGlobalMagField.h>
31 #include <TGeoManager.h>
32 #include <TParticle.h>
34 #include <TStopwatch.h>
36 #include <TVirtualMC.h>
37 #include <TMCVerbose.h>
40 #include "AliCDBEntry.h"
41 #include "AliCDBManager.h"
42 #include "AliCDBStorage.h"
43 #include "AliDetector.h"
44 #include "AliGenerator.h"
45 #include "AliGeomManager.h"
46 #include "AliHeader.h"
53 #include "AliSimulation.h"
55 #include "AliTrackReference.h"
56 #include "AliTransportMonitor.h"
62 //_______________________________________________________________________
65 fSaveRndmStatus(kFALSE),
66 fSaveRndmEventStatus(kFALSE),
67 fReadRndmStatus(kFALSE),
68 fUseMonitoring(kFALSE),
69 fRndmFileName("random.root"),
92 //_______________________________________________________________________
93 AliMC::AliMC(const char *name, const char *title) :
94 TVirtualMCApplication(name, title),
96 fSaveRndmStatus(kFALSE),
97 fSaveRndmEventStatus(kFALSE),
98 fReadRndmStatus(kFALSE),
99 fUseMonitoring(kFALSE),
100 fRndmFileName("random.root"),
109 fImedia(new TArrayI(1000)),
112 fHitLists(new TList()),
115 fTrackReferences("AliTrackReference", 100),
116 fTmpTrackReferences("AliTrackReference", 100)
119 // Set transport parameters
122 // Prepare the tracking medium lists
123 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
126 //_______________________________________________________________________
134 // Delete track references
137 //_______________________________________________________________________
138 void AliMC::ConstructGeometry()
141 // Either load geometry from file or create it through usual
142 // loop on detectors. In the first case the method
143 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
144 // at InitGeometry().
147 if(AliSimulation::Instance()->IsGeometryFromFile()){ //load geometry either from CDB or from file
148 if(IsGeometryFromCDB()){
149 AliInfo("Loading geometry from CDB default storage");
150 AliCDBPath path("GRP","Geometry","Data");
151 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
152 if(!entry) AliFatal("Unable to load geometry from CDB!");
154 gGeoManager = (TGeoManager*) entry->GetObject();
155 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
158 const char *geomfilename = AliSimulation::Instance()->GetGeometryFile();
159 if(gSystem->ExpandPathName(geomfilename)){
160 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
161 TGeoManager::Import(geomfilename);
163 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
167 TVirtualMC::GetMC()->SetRootGeometry();
169 // Create modules, materials, geometry
170 if (!gGeoManager) new TGeoManager("ALICE", "ALICE geometry");
172 TIter next(gAlice->Modules());
174 AliDebug(1, "Geometry creation:");
175 while((detector = dynamic_cast<AliModule*>(next()))) {
177 // Initialise detector materials and geometry
178 detector->CreateMaterials();
179 detector->CreateGeometry();
180 AliInfo(Form("%10s R:%.2fs C:%.2fs",
181 detector->GetName(),stw.RealTime(),stw.CpuTime()));
187 //_______________________________________________________________________
188 Bool_t AliMC::MisalignGeometry()
190 // Call misalignment code if AliSimulation object was defined.
192 if(!AliSimulation::Instance()->IsGeometryFromFile()){
193 //Set alignable volumes for the whole geometry
194 SetAllAlignableVolumes();
196 // Misalign geometry via AliSimulation instance
197 if (!AliSimulation::Instance()) return kFALSE;
198 AliGeomManager::SetGeometry(gGeoManager);
199 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
200 AliFatal("Current loaded geometry differs in the definition of symbolic names!");
202 return AliSimulation::Instance()->MisalignGeometry(AliRunLoader::Instance());
205 //_______________________________________________________________________
206 void AliMC::ConstructOpGeometry()
209 // Loop all detector modules and call DefineOpticalProperties() method
212 TIter next(gAlice->Modules());
214 AliInfo("Optical properties definition");
215 while((detector = dynamic_cast<AliModule*>(next()))) {
216 // Initialise detector geometry
217 if(AliSimulation::Instance()->IsGeometryFromFile()) detector->CreateMaterials();
218 // Initialise detector optical properties
219 detector->DefineOpticalProperties();
223 #include <TPDGCode.h>
224 //_______________________________________________________________________
225 void AliMC::AddParticles()
228 // Add particles (not present in Geant3 or Geant4)
231 cout << "########## AliMC::AddParticles" << endl;
234 TVirtualMC::GetMC()->DefineParticle(1010010030, "HyperTriton", kPTHadron, 2.99131 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 3, kFALSE);
236 TVirtualMC::GetMC()->DefineParticle(-1010010030, "AntiHyperTriton", kPTHadron, 2.99131 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 3, kFALSE);
239 TVirtualMC::GetMC()->DefineParticle(1010010040, "Hyperhydrog4", kPTHadron, 3.931 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 4, kFALSE);
240 //Anti-Hyper hydrogen 4
241 TVirtualMC::GetMC()->DefineParticle(-1010010040, "AntiHyperhydrog4", kPTHadron, 3.931 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 4, kFALSE);
244 TVirtualMC::GetMC()->DefineParticle(1010020040, "Hyperhelium4", kPTHadron, 3.929 , 2.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 4, kFALSE);
245 //Anti-Hyper helium 4
246 TVirtualMC::GetMC()->DefineParticle(-1010020040, "AntiHyperhelium4", kPTHadron, 3.929 , 2.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 4, kFALSE);
249 TVirtualMC::GetMC()->DefineParticle(1010000020, "LambdaNeutron", kPTNeutron, 2.054 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
251 //Anti-Lambda-Neutron
252 TVirtualMC::GetMC()->DefineParticle(-1010000020, "AntiLambdaNeutron", kPTNeutron, 2.054 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
255 TVirtualMC::GetMC()->DefineParticle(1020000020, "Hdibaryon", kPTNeutron, 2.23, 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
257 TVirtualMC::GetMC()->DefineParticle(-1020000020, "AntiHdibaryon", kPTNeutron, 2.23, 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
260 TVirtualMC::GetMC()->DefineParticle(1030000020, "Xi0Proton", kPTHadron, 2.248 , 1.0, 1.333e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
263 TVirtualMC::GetMC()->DefineParticle(-1030000020, "AntiXi0Proton", kPTHadron, 2.248 , 1.0, 1.333e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
265 //Lambda-Neutron-Neutron
266 TVirtualMC::GetMC()->DefineParticle(1010000030, "LambdaNeutronNeutron", kPTNeutron, 2.982 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
268 //Anti-Lambda-Neutron-Neutron
269 TVirtualMC::GetMC()->DefineParticle(-1010000030, "AntiLambdaNeutronNeutron", kPTNeutron, 2.982 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
271 //Resonances not in Generators
272 // f0(980) assume 70 MeV as width (PDG: 40 to 100 MeV)
273 TVirtualMC::GetMC()->DefineParticle(9010221, "f0_980", kPTNeutron, 0.98 , 0.0, 9.403e-24,"Hadron", 7e-2, 0, 0, 0, 0, 0, 0, 0, 0, kTRUE);
275 // f2(1270) (PDG: width = 185 MeV)
276 TVirtualMC::GetMC()->DefineParticle(225, "f2_1270", kPTNeutron, 1.275 , 0.0, 3.558e-24,"Hadron", 0.185, 0, 0, 0, 0, 0, 0, 0, 0, kTRUE);
278 // Define the 2- and 3-body phase space decay for the Hyper-Triton
282 for (Int_t kz = 0; kz < 6; kz++) {
289 mode[0][0] = 1000020030; // Helium3
290 mode[0][1] = -211; // negative pion
293 mode[1][0] = 1000010020; // deuteron
294 mode[1][1] = 2212; // proton
295 mode[1][2] = -211; // negative pion
297 TVirtualMC::GetMC()->SetDecayMode(1010010030,bratio,mode);
301 // Define the 2- and 3-body phase space decay for the Anti-Hyper-Triton
305 for (Int_t kz = 0; kz < 6; kz++) {
312 amode[0][0] = -1000020030; // anti- Helium3
313 amode[0][1] = 211; // positive pion
315 amode[1][0] = -1000010020; // anti-deuteron
316 amode[1][1] = -2212; // anti-proton
317 amode[1][2] = 211; // positive pion
319 TVirtualMC::GetMC()->SetDecayMode(-1010010030,abratio,amode);
321 ////// ----------Hypernuclei with Mass=4 ----------- //////////
323 // Define the 2- and 3-body phase space decay for the Hyper Hydrogen 4
328 for (Int_t kz = 0; kz < 6; kz++) {
335 mode3[0][0] = 1000020040; // Helium4
336 mode3[0][1] = -211; // negative pion
339 mode3[1][0] = 1000010030; // tritium
340 mode3[1][1] = 2212; // proton
341 mode3[1][2] = -211; // negative pion
343 TVirtualMC::GetMC()->SetDecayMode(1010010040,bratio3,mode3);
346 // Define the 2- and 3-body phase space decay for the Hyper Hydrogen 4
350 for (Int_t kz = 0; kz < 6; kz++) {
357 amode3[0][0] = -1000020040; // anti- Helium4
358 amode3[0][1] = 211; // positive pion
360 amode3[1][0] = -1000010030; // anti-tritium
361 amode3[1][1] = -2212; // anti-proton
362 amode3[1][2] = 211; // positive pion
364 TVirtualMC::GetMC()->SetDecayMode(-1010010040,abratio3,amode3);
367 // Define the 3-body phase space decay for the Hyper Helium 4
371 for (Int_t kz = 0; kz < 6; kz++) {
378 mode4[0][0] = 1000020030; // Helium3
379 mode4[0][1] = -211; // negative pion
380 mode4[0][2] = 2212; // proton
382 TVirtualMC::GetMC()->SetDecayMode(1010020040,bratio4,mode4);
385 // Define the 2-body phase space decay for the Anti-Hyper Helium 4
389 for (Int_t kz = 0; kz < 6; kz++) {
396 amode4[0][0] = -1000020030; // anti-Helium 3
397 amode4[0][1] = 211; // positive pion
398 amode4[0][2] = -2212; // anti proton
400 TVirtualMC::GetMC()->SetDecayMode(-1010020040,abratio4,amode4);
403 // Define the 2-body phase space decay for the Lambda-neutron boundstate
407 for (Int_t kz = 0; kz < 6; kz++) {
414 mode1[0][0] = 1000010020; // deuteron
415 mode1[0][1] = -211; // negative pion
417 TVirtualMC::GetMC()->SetDecayMode(1010000020,bratio1,mode1);
420 // Define the 2-body phase space decay for the Anti-Lambda-neutron boundstate
424 for (Int_t kz = 0; kz < 6; kz++) {
431 amode1[0][0] = -1000010020; // anti-deuteron
432 amode1[0][1] = 211; // positive pion
434 TVirtualMC::GetMC()->SetDecayMode(-1010000020,abratio1,amode1);
436 // Define the 2-body phase space decay for the H-Dibaryon
440 for (Int_t kz = 0; kz < 6; kz++) {
447 mode2[0][0] = 3122; // Lambda
448 mode2[0][1] = 2212; // proton
449 mode2[0][2] = -211; // negative pion
451 TVirtualMC::GetMC()->SetDecayMode(1020000020,bratio2,mode2);
453 // Define the 2-body phase space decay for the Anti-H-Dibaryon
457 for (Int_t kz = 0; kz < 6; kz++) {
464 amode2[0][0] = -3122; // anti-deuteron
465 amode2[0][1] = -2212; // anti-proton
466 amode2[0][2] = 211; // positive pion
468 TVirtualMC::GetMC()->SetDecayMode(-1020000020,abratio2,amode2);
470 // Define the 2-body phase space decay for the Xi0P
474 for (Int_t kz = 0; kz < 6; kz++) {
481 mode5[0][0] = 3122; // Lambda
482 mode5[0][1] = 2212; // proton
484 TVirtualMC::GetMC()->SetDecayMode(1030000020,bratio5,mode5);
486 // Define the 2-body phase space decay for the Anti-Xi0P
490 for (Int_t kz = 0; kz < 6; kz++) {
497 amode5[0][0] = -3122; // anti-Lambda
498 amode5[0][1] = -2212; // anti-proton
500 TVirtualMC::GetMC()->SetDecayMode(-1030000020,abratio5,amode5);
502 // Define the 2-body phase space decay for the Lambda-Neutron-Neutron
506 for (Int_t kz = 0; kz < 6; kz++) {
513 mode6[0][0] = 1000010030; // triton
514 mode6[0][1] = -211; // pion
516 TVirtualMC::GetMC()->SetDecayMode(1010000030,bratio6,mode6);
518 // Define the 2-body phase space decay for the Anti-Lambda-Neutron-Neutron
522 for (Int_t kz = 0; kz < 6; kz++) {
529 amode6[0][0] = -1000010030; // anti-triton
530 amode6[0][1] = 211; // pion
532 TVirtualMC::GetMC()->SetDecayMode(-1010000030,abratio6,amode6);
534 ///////////////////////////////////////////////////////////////////
536 // Define the 2-body phase space decay for the f0(980)
538 // Float_t bratio[6];
540 for (Int_t kz = 0; kz < 6; kz++) {
547 mode[0][0] = 211; // pion
548 mode[0][1] = -211; // pion
550 TVirtualMC::GetMC()->SetDecayMode(9010221,bratio,mode);
552 // Define the 2-body phase space decay for the f2(1270)
554 // Float_t bratio[6];
556 for (Int_t kz = 0; kz < 6; kz++) {
563 mode[0][0] = 211; // pion
564 mode[0][1] = -211; // pion
566 TVirtualMC::GetMC()->SetDecayMode(225,bratio,mode);
569 // --------------------------------------------------------------------
572 //_______________________________________________________________________
573 void AliMC::InitGeometry()
576 // Initialize detectors
579 AliInfo("Initialisation:");
581 TIter next(gAlice->Modules());
583 while((detector = dynamic_cast<AliModule*>(next()))) {
586 AliInfo(Form("%10s R:%.2fs C:%.2fs",
587 detector->GetName(),stw.RealTime(),stw.CpuTime()));
591 //_______________________________________________________________________
592 void AliMC::SetGeometryFromCDB()
594 // Set the loading of geometry from cdb instead of creating it
595 // A default CDB storage needs to be set before this method is called
596 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
597 AliCDBManager::Instance()->GetRun() >= 0)
598 AliSimulation::Instance()->SetGeometryFile("*OCDB*");
600 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
603 //_______________________________________________________________________
604 Bool_t AliMC::IsGeometryFromCDB() const
606 return (strcmp(AliSimulation::Instance()->GetGeometryFile(),"*OCDB*")==0);
609 //_______________________________________________________________________
610 void AliMC::SetAllAlignableVolumes()
613 // Add alignable volumes (TGeoPNEntries) looping on all
617 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
619 TIter next(gAlice->Modules());
620 while((detector = dynamic_cast<AliModule*>(next()))) {
621 detector->AddAlignableVolumes();
625 //_______________________________________________________________________
626 void AliMC::GeneratePrimaries()
629 // Generate primary particles and fill them in the stack.
632 Generator()->Generate();
635 //_______________________________________________________________________
636 void AliMC::SetGenerator(AliGenerator *generator)
639 // Load the event generator
641 if(!fGenerator) fGenerator = generator;
644 //_______________________________________________________________________
645 void AliMC::ResetGenerator(AliGenerator *generator)
648 // Load the event generator
652 AliWarning(Form("Replacing generator %s with %s",
653 fGenerator->GetName(),generator->GetName()));
656 AliWarning(Form("Replacing generator %s with NULL",
657 fGenerator->GetName()));
660 fGenerator = generator;
663 //_______________________________________________________________________
664 void AliMC::FinishRun()
666 // Clean generator information
667 AliDebug(1, "fGenerator->FinishRun()");
668 fGenerator->FinishRun();
670 // Monitoring information
673 fMonitor->Export("timing.root");
676 //Output energy summary tables
677 AliDebug(1, "EnergySummary()");
678 ToAliDebug(1, EnergySummary());
681 //_______________________________________________________________________
682 void AliMC::BeginPrimary()
685 // Called at the beginning of each primary track
690 ResetTrackReferences();
693 //_______________________________________________________________________
694 void AliMC::PreTrack()
696 // Actions before the track's transport
698 //verbose.PreTrack();
700 TObjArray &dets = *gAlice->Modules();
703 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
704 if((module = dynamic_cast<AliModule*>(dets[i])))
708 //_______________________________________________________________________
709 void AliMC::Stepping()
712 // Called at every step during transport
714 //verbose.Stepping();
716 Int_t id = DetFromMate(TVirtualMC::GetMC()->CurrentMedium());
720 if ( TVirtualMC::GetMC()->IsNewTrack() &&
721 TVirtualMC::GetMC()->TrackTime() == 0. &&
723 fRDecayMax > fRDecayMin &&
724 TVirtualMC::GetMC()->TrackPid() == fDecayPdg )
726 FixParticleDecaytime();
729 // --- If monitoring timing was requested, monitor the step
730 if (fUseMonitoring) {
732 fMonitor = new AliTransportMonitor(TVirtualMC::GetMC()->NofVolumes()+1);
735 if (TVirtualMC::GetMC()->IsNewTrack() || TVirtualMC::GetMC()->TrackTime() == 0. || TVirtualMC::GetMC()->TrackStep()<1.1E-10) {
736 fMonitor->DummyStep();
740 Int_t volId = TVirtualMC::GetMC()->CurrentVolID(copy);
741 Int_t pdg = TVirtualMC::GetMC()->TrackPid();
742 TLorentzVector xyz, pxpypz;
743 TVirtualMC::GetMC()->TrackPosition(xyz);
744 TVirtualMC::GetMC()->TrackMomentum(pxpypz);
745 fMonitor->StepInfo(volId, pdg, pxpypz.E(), xyz.X(), xyz.Y(), xyz.Z());
749 // --- If lego option, do it and leave
750 if (AliSimulation::Instance()->Lego())
751 AliSimulation::Instance()->Lego()->StepManager();
754 //Update energy deposition tables
755 AddEnergyDeposit(TVirtualMC::GetMC()->CurrentVolID(copy),TVirtualMC::GetMC()->Edep());
757 // write tracke reference for track which is dissapearing - MI
759 if (TVirtualMC::GetMC()->IsTrackDisappeared() && !(TVirtualMC::GetMC()->IsTrackAlive())) {
760 if (TVirtualMC::GetMC()->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
761 AliTrackReference::kDisappeared);
766 //Call the appropriate stepping routine;
767 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
768 if(det && det->StepManagerIsEnabled()) {
774 //_______________________________________________________________________
775 void AliMC::EnergySummary()
778 // Print summary of deposited energy
784 Int_t kn, i, left, j, id;
785 const Float_t kzero=0;
786 Int_t ievent=AliRunLoader::Instance()->GetHeader()->GetEvent()+1;
788 // Energy loss information
790 printf("***************** Energy Loss Information per event (GEV) *****************\n");
791 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
794 fEventEnergy[ndep]=kn;
799 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
802 fSummEnergy[ndep]=ed;
803 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
808 for(kn=0;kn<(ndep-1)/3+1;kn++) {
810 for(i=0;i<(3<left?3:left);i++) {
812 id=Int_t (fEventEnergy[j]+0.1);
813 printf(" %s %10.3f +- %10.3f%%;",TVirtualMC::GetMC()->VolName(id),fSummEnergy[j],fSum2Energy[j]);
818 // Relative energy loss in different detectors
819 printf("******************** Relative Energy Loss per event ********************\n");
820 printf("Total energy loss per event %10.3f GeV\n",edtot);
821 for(kn=0;kn<(ndep-1)/5+1;kn++) {
823 for(i=0;i<(5<left?5:left);i++) {
825 id=Int_t (fEventEnergy[j]+0.1);
826 printf(" %s %10.3f%%;",TVirtualMC::GetMC()->VolName(id),100*fSummEnergy[j]/edtot);
830 for(kn=0;kn<75;kn++) printf("*");
834 // Reset the TArray's
835 // fEventEnergy.Set(0);
836 // fSummEnergy.Set(0);
837 // fSum2Energy.Set(0);
840 //_____________________________________________________________________________
841 void AliMC::BeginEvent()
844 // Clean-up previous event
846 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
847 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
848 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
849 AliDebug(1, " BEGINNING EVENT ");
850 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
851 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
852 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
854 AliRunLoader *runloader=AliRunLoader::Instance();
856 /*******************************/
857 /* Clean after eventual */
859 /*******************************/
862 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
863 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
864 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
865 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
867 fEventEnergy.Reset();
868 // Clean detector information
870 if (runloader->Stack())
871 runloader->Stack()->Reset();//clean stack -> tree is unloaded
873 runloader->MakeStack();//or make a new one
875 // Random engine status
878 if ( fSaveRndmStatus || fSaveRndmEventStatus) {
879 TString fileName="random";
880 if ( fSaveRndmEventStatus ) {
882 fileName += gAlice->GetEventNrInRun();
886 // write ROOT random engine status
887 cout << "Saving random engine status in " << fileName.Data() << endl;
888 TFile f(fileName.Data(),"RECREATE");
889 gRandom->Write(fileName.Data());
892 if ( fReadRndmStatus ) {
893 //read ROOT random engine status
894 cout << "Reading random engine status from " << fRndmFileName.Data() << endl;
895 TFile f(fRndmFileName.Data());
896 gRandom->Read(fRndmFileName.Data());
899 if(AliSimulation::Instance()->Lego() == 0x0)
901 AliDebug(1, "fRunLoader->MakeTree(K)");
902 runloader->MakeTree("K");
905 AliDebug(1, "TVirtualMC::GetMC()->SetStack(fRunLoader->Stack())");
906 TVirtualMC::GetMC()->SetStack(runloader->Stack());//Was in InitMC - but was moved here
907 //because we don't have guarantee that
908 //stack pointer is not going to change from event to event
909 //since it bellobgs to header and is obtained via RunLoader
911 // Reset all Detectors & kinematics & make/reset trees
914 runloader->GetHeader()->Reset(AliCDBManager::Instance()->GetRun(),gAlice->GetEvNumber(),
915 gAlice->GetEventNrInRun());
916 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
918 if(AliSimulation::Instance()->Lego())
920 AliSimulation::Instance()->Lego()->BeginEvent();
925 AliDebug(1, "ResetHits()");
928 AliDebug(1, "fRunLoader->MakeTree(H)");
929 runloader->MakeTree("H");
933 MakeTmpTrackRefsTree();
934 //create new branches and SetAdresses
935 TIter next(gAlice->Modules());
937 while((detector = (AliModule*)next()))
939 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
940 detector->MakeBranch("H");
944 //_______________________________________________________________________
945 void AliMC::ResetHits()
948 // Reset all Detectors hits
950 TIter next(gAlice->Modules());
952 while((detector = dynamic_cast<AliModule*>(next()))) {
953 detector->ResetHits();
957 //_______________________________________________________________________
958 void AliMC::ResetDigits()
961 // Reset all Detectors digits
963 TIter next(gAlice->Modules());
965 while((detector = dynamic_cast<AliModule*>(next()))) {
966 detector->ResetDigits();
970 //_______________________________________________________________________
971 void AliMC::ResetSDigits()
974 // Reset all Detectors digits
976 TIter next(gAlice->Modules());
978 while((detector = dynamic_cast<AliModule*>(next()))) {
979 detector->ResetSDigits();
983 //_______________________________________________________________________
984 void AliMC::PostTrack()
986 // Posts tracks for each module
988 TObjArray &dets = *gAlice->Modules();
991 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
992 if((module = dynamic_cast<AliModule*>(dets[i])))
996 //_______________________________________________________________________
997 void AliMC::FinishPrimary()
1000 // Called at the end of each primary track
1003 AliRunLoader *runloader=AliRunLoader::Instance();
1004 // static Int_t count=0;
1005 // const Int_t times=10;
1006 // This primary is finished, purify stack
1007 #if ROOT_VERSION_CODE > 262152
1008 if (!(TVirtualMC::GetMC()->SecondariesAreOrdered())) {
1009 if (runloader->Stack()->ReorderKine()) RemapHits();
1012 if (runloader->Stack()->PurifyKine()) RemapHits();
1014 TIter next(gAlice->Modules());
1015 AliModule *detector;
1016 while((detector = dynamic_cast<AliModule*>(next()))) {
1017 detector->FinishPrimary();
1018 AliLoader* loader = detector->GetLoader();
1021 TTree* treeH = loader->TreeH();
1022 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
1026 // Write out track references if any
1027 if (fTmpTreeTR) fTmpTreeTR->Fill();
1030 void AliMC::RemapHits()
1033 // Remaps the track labels of the hits
1034 AliRunLoader *runloader=AliRunLoader::Instance();
1035 AliStack* stack = runloader->Stack();
1036 TList* hitLists = GetHitLists();
1037 TIter next(hitLists);
1038 TCollection *hitList;
1040 while((hitList = dynamic_cast<TCollection*>(next()))) {
1041 TIter nexthit(hitList);
1043 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
1044 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
1049 // This for detectors which have a special mapping mechanism
1050 // for hits, such as TPC and TRD
1054 TObjArray* modules = gAlice->Modules();
1055 TIter nextmod(modules);
1057 while((module = (AliModule*) nextmod())) {
1058 AliDetector* det = dynamic_cast<AliDetector*> (module);
1059 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
1062 RemapTrackReferencesIDs(stack->TrackLabelMap());
1065 //_______________________________________________________________________
1066 void AliMC::FinishEvent()
1069 // Called at the end of the event.
1072 if(AliSimulation::Instance()->Lego()) AliSimulation::Instance()->Lego()->FinishEvent();
1074 TIter next(gAlice->Modules());
1075 AliModule *detector;
1076 while((detector = dynamic_cast<AliModule*>(next()))) {
1077 detector->FinishEvent();
1080 //Update the energy deposit tables
1082 for(i=0;i<fEventEnergy.GetSize();i++)
1084 fSummEnergy[i]+=fEventEnergy[i];
1085 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
1088 AliRunLoader *runloader=AliRunLoader::Instance();
1090 AliHeader* header = runloader->GetHeader();
1091 AliStack* stack = runloader->Stack();
1092 if ( (header == 0x0) || (stack == 0x0) )
1093 {//check if we got header and stack. If not cry and exit aliroot
1094 AliFatal("Can not get the stack or header from LOADER");
1095 return;//never reached
1097 // Update Header information
1098 header->SetNprimary(stack->GetNprimary());
1099 header->SetNtrack(stack->GetNtrack());
1100 header->SetTimeStamp(AliSimulation::Instance()->GenerateTimeStamp());
1102 // Write out the kinematics
1103 if (!AliSimulation::Instance()->Lego()) stack->FinishEvent();
1105 // Synchronize the TreeTR with TreeK
1106 if (fTmpTreeTR) ReorderAndExpandTreeTR();
1108 // Write out the event Header information
1109 TTree* treeE = runloader->TreeE();
1112 header->SetStack(stack);
1117 AliError("Can not get TreeE from RL");
1120 if(AliSimulation::Instance()->Lego() == 0x0)
1122 runloader->WriteKinematics("OVERWRITE");
1123 runloader->WriteTrackRefs("OVERWRITE");
1124 runloader->WriteHits("OVERWRITE");
1127 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1128 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1129 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1130 AliDebug(1, " FINISHING EVENT ");
1131 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1132 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1133 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
1136 //_______________________________________________________________________
1139 // MC initialization
1142 //=================Create Materials and geometry
1143 TVirtualMC::GetMC()->Init();
1144 // Set alignable volumes for the whole geometry (with old root)
1145 #if ROOT_VERSION_CODE < 331527
1146 SetAllAlignableVolumes();
1148 //Read the cuts for all materials
1150 //Build the special IMEDIA table
1153 //Compute cross-sections
1154 TVirtualMC::GetMC()->BuildPhysics();
1156 //Initialise geometry deposition table
1157 fEventEnergy.Set(TVirtualMC::GetMC()->NofVolumes()+1);
1158 fSummEnergy.Set(TVirtualMC::GetMC()->NofVolumes()+1);
1159 fSum2Energy.Set(TVirtualMC::GetMC()->NofVolumes()+1);
1161 // Register MC in configuration
1162 AliConfig::Instance()->Add(TVirtualMC::GetMC());
1165 //_______________________________________________________________________
1166 void AliMC::MediaTable()
1169 // Built media table to get from the media number to
1173 Int_t kz, nz, idt, lz, i, k, ind;
1175 TObjArray &dets = *gAlice->Detectors();
1177 Int_t ndets=gAlice->GetNdets();
1179 // For all detectors
1180 for (kz=0;kz<ndets;kz++) {
1181 // If detector is defined
1182 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
1183 TArrayI &idtmed = *(det->GetIdtmed());
1184 for(nz=0;nz<100;nz++) {
1186 // Find max and min material number
1187 if((idt=idtmed[nz])) {
1188 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
1189 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
1192 if(det->LoMedium() > det->HiMedium()) {
1193 det->LoMedium() = 0;
1194 det->HiMedium() = 0;
1196 if(det->HiMedium() > fImedia->GetSize()) {
1197 AliError(Form("Increase fImedia from %d to %d",
1198 fImedia->GetSize(),det->HiMedium()));
1201 // Tag all materials in rage as belonging to detector kz
1202 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
1209 // Print summary table
1210 AliInfo("Tracking media ranges:");
1212 for(i=0;i<(ndets-1)/6+1;i++) {
1213 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
1215 det=dynamic_cast<AliModule*>(dets[ind]);
1217 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
1220 printf(" %6s: %3d -> %3d;","NULL",0,0);
1227 //_______________________________________________________________________
1228 void AliMC::ReadTransPar()
1231 // Read filename to set the transport parameters
1235 const Int_t kncuts=10;
1236 const Int_t knflags=12;
1237 const Int_t knpars=kncuts+knflags;
1238 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
1239 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
1240 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
1241 "MULS","PAIR","PHOT","RAYL","STRA"};
1245 Float_t cut[kncuts];
1246 Int_t flag[knflags];
1247 Int_t i, itmed, iret, jret, ktmed, kz;
1250 // See whether the file is there
1251 filtmp=gSystem->ExpandPathName(fTransParName.Data());
1252 lun=fopen(filtmp,"r");
1255 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
1260 // Initialise cuts and flags
1261 for(i=0;i<kncuts;i++) cut[i]=-99;
1262 for(i=0;i<knflags;i++) flag[i]=-99;
1265 // Read up to the end of line excluded
1266 iret=fscanf(lun,"%255[^\n]",line);
1272 // Read the end of line
1273 jret = fscanf(lun,"%*c");
1275 if(line[0]=='*') continue;
1277 iret=sscanf(line,"%6s %d %f %f %f %f %f %f %f %f %f %f %d %d %d %d %d %d %d %d %d %d %d %d",
1278 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1279 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1280 &flag[8],&flag[9],&flag[10],&flag[11]);
1284 AliWarning(Form("Error reading file %s",fTransParName.Data()));
1287 // Check that the module exist
1288 AliModule *mod = gAlice->GetModule(detName);
1290 // Get the array of media numbers
1291 TArrayI &idtmed = *mod->GetIdtmed();
1292 // Check that the tracking medium code is valid
1293 if(0<=itmed && itmed < 100) {
1294 ktmed=idtmed[itmed];
1296 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
1299 // Set energy thresholds
1300 for(kz=0;kz<kncuts;kz++) {
1302 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
1303 kpars[kz],cut[kz],itmed,mod->GetName()));
1304 TVirtualMC::GetMC()->Gstpar(ktmed,kpars[kz],cut[kz]);
1307 // Set transport mechanisms
1308 for(kz=0;kz<knflags;kz++) {
1310 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
1311 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
1312 TVirtualMC::GetMC()->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
1316 AliWarning(Form("Invalid medium code %d",itmed));
1320 AliDebug(1, Form("%s not present",detName));
1326 //_______________________________________________________________________
1327 void AliMC::SetTransPar(const char *filename)
1330 // Sets the file name for transport parameters
1332 fTransParName = filename;
1335 //_______________________________________________________________________
1336 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
1339 // Add a hit to detector id
1341 TObjArray &dets = *gAlice->Modules();
1342 if(dets[id]) static_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
1345 //_______________________________________________________________________
1346 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
1349 // Add digit to detector id
1351 TObjArray &dets = *gAlice->Modules();
1352 if(dets[id]) static_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
1355 //_______________________________________________________________________
1356 Int_t AliMC::GetCurrentTrackNumber() const {
1358 // Returns current track
1360 return AliRunLoader::Instance()->Stack()->GetCurrentTrackNumber();
1363 //_______________________________________________________________________
1364 void AliMC::DumpPart (Int_t i) const
1367 // Dumps particle i in the stack
1369 AliRunLoader * runloader = AliRunLoader::Instance();
1370 if (runloader->Stack())
1371 runloader->Stack()->DumpPart(i);
1374 //_______________________________________________________________________
1375 void AliMC::DumpPStack () const
1378 // Dumps the particle stack
1380 AliRunLoader * runloader = AliRunLoader::Instance();
1381 if (runloader->Stack())
1382 runloader->Stack()->DumpPStack();
1385 //_______________________________________________________________________
1386 Int_t AliMC::GetNtrack() const {
1388 // Returns number of tracks in stack
1391 AliRunLoader * runloader = AliRunLoader::Instance();
1392 if (runloader->Stack())
1393 ntracks = runloader->Stack()->GetNtrack();
1397 //_______________________________________________________________________
1398 Int_t AliMC::GetPrimary(Int_t track) const
1401 // return number of primary that has generated track
1403 Int_t nprimary = -999;
1404 AliRunLoader * runloader = AliRunLoader::Instance();
1405 if (runloader->Stack())
1406 nprimary = runloader->Stack()->GetPrimary(track);
1410 //_______________________________________________________________________
1411 TParticle* AliMC::Particle(Int_t i) const
1413 // Returns the i-th particle from the stack taking into account
1414 // the remaping done by PurifyKine
1415 AliRunLoader * runloader = AliRunLoader::Instance();
1417 if (runloader->Stack())
1418 return runloader->Stack()->Particle(i);
1422 //_______________________________________________________________________
1423 const TObjArray* AliMC::Particles() const {
1425 // Returns pointer to Particles array
1427 AliRunLoader * runloader = AliRunLoader::Instance();
1429 if (runloader->Stack())
1430 return runloader->Stack()->Particles();
1434 //_______________________________________________________________________
1435 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, const Float_t *pmom,
1436 const Float_t *vpos, const Float_t *polar, Float_t tof,
1437 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1439 // Delegate to stack
1441 AliRunLoader * runloader = AliRunLoader::Instance();
1443 if (runloader->Stack())
1444 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1445 mech, ntr, weight, is);
1448 //_______________________________________________________________________
1449 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1450 Double_t px, Double_t py, Double_t pz, Double_t e,
1451 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1452 Double_t polx, Double_t poly, Double_t polz,
1453 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1455 // Delegate to stack
1457 AliRunLoader * runloader = AliRunLoader::Instance();
1459 if (runloader->Stack())
1460 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1461 polx, poly, polz, mech, ntr, weight, is);
1464 //_______________________________________________________________________
1465 void AliMC::SetHighWaterMark(Int_t nt) const
1468 // Set high water mark for last track in event
1469 AliRunLoader * runloader = AliRunLoader::Instance();
1471 if (runloader->Stack())
1472 runloader->Stack()->SetHighWaterMark(nt);
1475 //_______________________________________________________________________
1476 void AliMC::KeepTrack(Int_t track) const
1479 // Delegate to stack
1481 AliRunLoader * runloader = AliRunLoader::Instance();
1483 if (runloader->Stack())
1484 runloader->Stack()->KeepTrack(track);
1487 //_______________________________________________________________________
1488 void AliMC::FlagTrack(Int_t track) const
1490 // Delegate to stack
1492 AliRunLoader * runloader = AliRunLoader::Instance();
1494 if (runloader->Stack())
1495 runloader->Stack()->FlagTrack(track);
1498 //_______________________________________________________________________
1499 void AliMC::SetCurrentTrack(Int_t track) const
1502 // Set current track number
1504 AliRunLoader * runloader = AliRunLoader::Instance();
1506 if (runloader->Stack())
1507 runloader->Stack()->SetCurrentTrack(track);
1510 //_______________________________________________________________________
1511 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1514 // add a trackrefernce to the list
1515 Int_t primary = GetPrimary(label);
1516 Particle(primary)->SetBit(kKeepBit);
1518 Int_t nref = fTmpTrackReferences.GetEntriesFast();
1519 return new(fTmpTrackReferences[nref]) AliTrackReference(label, id);
1524 //_______________________________________________________________________
1525 void AliMC::ResetTrackReferences()
1528 // Reset all references
1530 fTmpTrackReferences.Clear();
1533 //_______________________________________________________________________
1534 void AliMC::RemapTrackReferencesIDs(const Int_t *map)
1537 // Remapping track reference
1538 // Called at finish primary
1541 Int_t nEntries = fTmpTrackReferences.GetEntries();
1542 for (Int_t i=0; i < nEntries; i++){
1543 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences.UncheckedAt(i));
1545 Int_t newID = map[ref->GetTrack()];
1546 if (newID>=0) ref->SetTrack(newID);
1548 ref->SetBit(kNotDeleted,kFALSE);
1549 fTmpTrackReferences.RemoveAt(i);
1553 fTmpTrackReferences.Compress();
1556 //_______________________________________________________________________
1557 void AliMC::FixParticleDecaytime()
1560 // Fix the particle decay time according to rmin and rmax for decays
1564 TVirtualMC::GetMC()->TrackMomentum(p);
1565 Double_t tmin, tmax;
1568 // Transverse velocity
1569 Double_t vt = p.Pt() / p.E();
1571 if ((b = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->SolenoidField()) > 0.) { // [kG]
1575 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1577 // Revolution frequency
1579 Double_t omega = vt / rho;
1581 // Maximum and minimum decay time
1583 // Check for curlers first
1584 const Double_t kOvRhoSqr2 = 1./(rho*TMath::Sqrt(2.));
1585 if (fRDecayMax * kOvRhoSqr2 > 1.) return;
1589 tmax = TMath::ACos((1.-fRDecayMax*kOvRhoSqr2)*(1.+fRDecayMax*kOvRhoSqr2)) / omega; // [ct]
1590 tmin = TMath::ACos((1.-fRDecayMin*kOvRhoSqr2)*(1.+fRDecayMin*kOvRhoSqr2)) / omega; // [ct]
1592 tmax = fRDecayMax / vt; // [ct]
1593 tmin = fRDecayMin / vt; // [ct]
1597 // Dial t using the two limits
1598 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1601 // Force decay time in transport code
1603 TVirtualMC::GetMC()->ForceDecayTime(t / 2.99792458e10);
1606 void AliMC::MakeTmpTrackRefsTree()
1608 // Make the temporary track reference tree
1609 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1610 fTmpTreeTR = new TTree("TreeTR", "Track References");
1611 TClonesArray* pRef = &fTmpTrackReferences;
1612 fTmpTreeTR->Branch("TrackReferences", &pRef, 4000);
1615 //_______________________________________________________________________
1616 void AliMC::ReorderAndExpandTreeTR()
1619 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1622 AliRunLoader *rl = AliRunLoader::Instance();
1625 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1626 rl->MakeTrackRefsContainer();
1627 TTree * treeTR = rl->TreeTR();
1628 // make branch for central track references
1629 TClonesArray* pRef = &fTrackReferences;
1630 treeTR->Branch("TrackReferences", &pRef);
1632 AliStack* stack = rl->Stack();
1633 Int_t np = stack->GetNprimary();
1634 Int_t nt = fTmpTreeTR->GetEntries();
1636 // Loop over tracks and find the secondaries with the help of the kine tree
1639 for (Int_t ip = np - 1; ip > -1; ip--) {
1640 TParticle *part = stack->Particle(ip);
1641 //printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1643 // Skip primaries that have not been transported
1644 Int_t dau1 = part->GetFirstDaughter();
1646 if (!part->TestBit(kTransportBit)) continue;
1648 fTmpTreeTR->GetEntry(it++);
1649 Int_t nh = fTmpTrackReferences.GetEntries();
1650 // Determine range of secondaries produced by this primary
1652 Int_t inext = ip - 1;
1655 part = stack->Particle(inext);
1656 dau2 = part->GetFirstDaughter();
1657 if (!(part->TestBit(kTransportBit)) || dau2 == -1 || dau2 < np) {
1658 // if (dau2 == -1 || dau2 < np) {
1664 dau2 = stack->GetNtrack() - 1;
1667 } // find upper bound
1669 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1671 // Loop over reference hits and find secondary label
1672 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1673 for (Int_t ih = 0; ih < nh; ih++) {
1674 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1675 Int_t label = tr->Label();
1677 if (label == ip) continue;
1678 if (label > dau2 || label < dau1)
1679 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1682 Int_t nref = fTrackReferences.GetEntriesFast();
1683 new(fTrackReferences[nref]) AliTrackReference(*tr);
1687 fTrackReferences.Clear();
1692 // Now loop again and write the primaries
1694 for (Int_t ip = 0; ip < np; ip++) {
1695 TParticle* part = stack->Particle(ip);
1696 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1697 if (part->TestBit(kTransportBit))
1699 // Skip particles that have not been transported
1700 fTmpTreeTR->GetEntry(it--);
1701 Int_t nh = fTmpTrackReferences.GetEntries();
1703 // Loop over reference hits and find primary labels
1704 for (Int_t ih = 0; ih < nh; ih++) {
1705 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1706 Int_t label = tr->Label();
1708 Int_t nref = fTrackReferences.GetEntriesFast();
1709 new(fTrackReferences[nref]) AliTrackReference(*tr);
1714 fTrackReferences.Clear();
1718 if (ifills != stack->GetNtrack())
1719 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1723 fTmpFileTR->Close();
1725 fTmpTrackReferences.Clear();
1726 gSystem->Exec("rm -rf TrackRefsTmp.root");