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
26 #include <TClonesArray.h>
28 #include <TGeoGlobalMagField.h>
29 #include <TGeoManager.h>
30 #include <TParticle.h>
32 #include <TStopwatch.h>
34 #include <TVirtualMC.h>
36 #include "AliCDBEntry.h"
37 #include "AliCDBManager.h"
38 #include "AliCDBStorage.h"
39 #include "AliDetector.h"
40 #include "AliGenerator.h"
41 #include "AliGeomManager.h"
42 #include "AliHeader.h"
49 #include "AliSimulation.h"
51 #include "AliTrackReference.h"
55 //_______________________________________________________________________
79 //_______________________________________________________________________
80 AliMC::AliMC(const char *name, const char *title) :
81 TVirtualMCApplication(name, title),
91 fImedia(new TArrayI(1000)),
93 fHitLists(new TList()),
96 fTrackReferences("AliTrackReference", 100),
97 fTmpTrackReferences("AliTrackReference", 100)
100 // Set transport parameters
103 // Prepare the tracking medium lists
104 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
107 //_______________________________________________________________________
114 // Delete track references
117 //_______________________________________________________________________
118 void AliMC::ConstructGeometry()
121 // Either load geometry from file or create it through usual
122 // loop on detectors. In the first case the method
123 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
124 // at InitGeometry().
127 if(gAlice->IsRootGeometry()){ //load geometry either from CDB or from file
128 if(gAlice->IsGeomFromCDB()){
129 AliInfo("Loading geometry from CDB default storage");
130 AliCDBPath path("GRP","Geometry","Data");
131 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
132 if(!entry) AliFatal("Unable to load geometry from CDB!");
134 gGeoManager = (TGeoManager*) entry->GetObject();
135 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
138 const char *geomfilename = gAlice->GetGeometryFileName();
139 if(gSystem->ExpandPathName(geomfilename)){
140 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
141 TGeoManager::Import(geomfilename);
143 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
148 // Create modules, materials, geometry
150 TIter next(gAlice->Modules());
152 AliDebug(1, "Geometry creation:");
153 while((detector = dynamic_cast<AliModule*>(next()))) {
155 // Initialise detector materials and geometry
156 detector->CreateMaterials();
157 detector->CreateGeometry();
158 AliInfo(Form("%10s R:%.2fs C:%.2fs",
159 detector->GetName(),stw.RealTime(),stw.CpuTime()));
165 //_______________________________________________________________________
166 Bool_t AliMC::MisalignGeometry()
168 // Call misalignment code if AliSimulation object was defined.
170 if(!gAlice->IsRootGeometry()){
171 //Set alignable volumes for the whole geometry
172 SetAllAlignableVolumes();
174 // Misalign geometry via AliSimulation instance
175 if (!AliSimulation::Instance()) return kFALSE;
176 AliGeomManager::SetGeometry(gGeoManager);
177 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
178 AliFatal("Current loaded geometry differs in the definition of symbolic names!");
180 return AliSimulation::Instance()->MisalignGeometry(AliRunLoader::Instance());
183 //_______________________________________________________________________
184 void AliMC::ConstructOpGeometry()
187 // Loop all detector modules and call DefineOpticalProperties() method
190 TIter next(gAlice->Modules());
192 AliInfo("Optical properties definition");
193 while((detector = dynamic_cast<AliModule*>(next()))) {
194 // Initialise detector optical properties
195 detector->DefineOpticalProperties();
199 //_______________________________________________________________________
200 void AliMC::InitGeometry()
203 // Initialize detectors
206 AliInfo("Initialisation:");
208 TIter next(gAlice->Modules());
210 while((detector = dynamic_cast<AliModule*>(next()))) {
212 // Initialise detector geometry
213 if(gAlice->IsRootGeometry()) detector->CreateMaterials();
215 AliInfo(Form("%10s R:%.2fs C:%.2fs",
216 detector->GetName(),stw.RealTime(),stw.CpuTime()));
220 //_______________________________________________________________________
221 void AliMC::SetAllAlignableVolumes()
224 // Add alignable volumes (TGeoPNEntries) looping on all
228 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
230 TIter next(gAlice->Modules());
231 while((detector = dynamic_cast<AliModule*>(next()))) {
232 detector->AddAlignableVolumes();
236 //_______________________________________________________________________
237 void AliMC::GeneratePrimaries()
240 // Generate primary particles and fill them in the stack.
243 Generator()->Generate();
246 //_______________________________________________________________________
247 void AliMC::SetGenerator(AliGenerator *generator)
250 // Load the event generator
252 if(!fGenerator) fGenerator = generator;
255 //_______________________________________________________________________
256 void AliMC::ResetGenerator(AliGenerator *generator)
259 // Load the event generator
263 AliWarning(Form("Replacing generator %s with %s",
264 fGenerator->GetName(),generator->GetName()));
267 AliWarning(Form("Replacing generator %s with NULL",
268 fGenerator->GetName()));
271 fGenerator = generator;
274 //_______________________________________________________________________
275 void AliMC::FinishRun()
277 // Clean generator information
278 AliDebug(1, "fGenerator->FinishRun()");
279 fGenerator->FinishRun();
281 //Output energy summary tables
282 AliDebug(1, "EnergySummary()");
283 ToAliDebug(1, EnergySummary());
286 //_______________________________________________________________________
287 void AliMC::BeginPrimary()
290 // Called at the beginning of each primary track
295 ResetTrackReferences();
298 //_______________________________________________________________________
299 void AliMC::PreTrack()
301 // Actions before the track's transport
302 TObjArray &dets = *gAlice->Modules();
305 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
306 if((module = dynamic_cast<AliModule*>(dets[i])))
311 //_______________________________________________________________________
312 void AliMC::Stepping()
315 // Called at every step during transport
317 Int_t id = DetFromMate(gMC->CurrentMedium());
321 if ( gMC->IsNewTrack() &&
322 gMC->TrackTime() == 0. &&
324 fRDecayMax > fRDecayMin &&
325 gMC->TrackPid() == fDecayPdg )
327 FixParticleDecaytime();
333 // --- If lego option, do it and leave
334 if (AliSimulation::Instance()->Lego())
335 AliSimulation::Instance()->Lego()->StepManager();
338 //Update energy deposition tables
339 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
341 // write tracke reference for track which is dissapearing - MI
343 if (gMC->IsTrackDisappeared() && !(gMC->IsTrackAlive())) {
344 if (gMC->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
345 AliTrackReference::kDisappeared);
350 //Call the appropriate stepping routine;
351 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
352 if(det && det->StepManagerIsEnabled()) {
358 //_______________________________________________________________________
359 void AliMC::EnergySummary()
362 // Print summary of deposited energy
368 Int_t kn, i, left, j, id;
369 const Float_t kzero=0;
370 Int_t ievent=AliRunLoader::Instance()->GetHeader()->GetEvent()+1;
372 // Energy loss information
374 printf("***************** Energy Loss Information per event (GEV) *****************\n");
375 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
378 fEventEnergy[ndep]=kn;
383 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
386 fSummEnergy[ndep]=ed;
387 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
392 for(kn=0;kn<(ndep-1)/3+1;kn++) {
394 for(i=0;i<(3<left?3:left);i++) {
396 id=Int_t (fEventEnergy[j]+0.1);
397 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
402 // Relative energy loss in different detectors
403 printf("******************** Relative Energy Loss per event ********************\n");
404 printf("Total energy loss per event %10.3f GeV\n",edtot);
405 for(kn=0;kn<(ndep-1)/5+1;kn++) {
407 for(i=0;i<(5<left?5:left);i++) {
409 id=Int_t (fEventEnergy[j]+0.1);
410 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
414 for(kn=0;kn<75;kn++) printf("*");
418 // Reset the TArray's
419 // fEventEnergy.Set(0);
420 // fSummEnergy.Set(0);
421 // fSum2Energy.Set(0);
424 //_____________________________________________________________________________
425 void AliMC::BeginEvent()
428 // Clean-up previous event
430 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
431 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
432 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
433 AliDebug(1, " BEGINNING EVENT ");
434 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
435 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
436 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
438 AliRunLoader *runloader=AliRunLoader::Instance();
440 /*******************************/
441 /* Clean after eventual */
443 /*******************************/
446 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
447 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
448 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
449 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
451 fEventEnergy.Reset();
452 // Clean detector information
454 if (runloader->Stack())
455 runloader->Stack()->Reset();//clean stack -> tree is unloaded
457 runloader->MakeStack();//or make a new one
460 if(AliSimulation::Instance()->Lego() == 0x0)
462 AliDebug(1, "fRunLoader->MakeTree(K)");
463 runloader->MakeTree("K");
466 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
467 gMC->SetStack(runloader->Stack());//Was in InitMC - but was moved here
468 //because we don't have guarantee that
469 //stack pointer is not going to change from event to event
470 //since it bellobgs to header and is obtained via RunLoader
472 // Reset all Detectors & kinematics & make/reset trees
475 runloader->GetHeader()->Reset(AliCDBManager::Instance()->GetRun(),gAlice->GetEvNumber(),
476 gAlice->GetEventNrInRun());
477 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
479 if(AliSimulation::Instance()->Lego())
481 AliSimulation::Instance()->Lego()->BeginEvent();
486 AliDebug(1, "ResetHits()");
489 AliDebug(1, "fRunLoader->MakeTree(H)");
490 runloader->MakeTree("H");
494 MakeTmpTrackRefsTree();
495 //create new branches and SetAdresses
496 TIter next(gAlice->Modules());
498 while((detector = (AliModule*)next()))
500 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
501 detector->MakeBranch("H");
505 //_______________________________________________________________________
506 void AliMC::ResetHits()
509 // Reset all Detectors hits
511 TIter next(gAlice->Modules());
513 while((detector = dynamic_cast<AliModule*>(next()))) {
514 detector->ResetHits();
518 //_______________________________________________________________________
519 void AliMC::ResetDigits()
522 // Reset all Detectors digits
524 TIter next(gAlice->Modules());
526 while((detector = dynamic_cast<AliModule*>(next()))) {
527 detector->ResetDigits();
531 //_______________________________________________________________________
532 void AliMC::ResetSDigits()
535 // Reset all Detectors digits
537 TIter next(gAlice->Modules());
539 while((detector = dynamic_cast<AliModule*>(next()))) {
540 detector->ResetSDigits();
544 //_______________________________________________________________________
545 void AliMC::PostTrack()
547 // Posts tracks for each module
548 TObjArray &dets = *gAlice->Modules();
551 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
552 if((module = dynamic_cast<AliModule*>(dets[i])))
556 //_______________________________________________________________________
557 void AliMC::FinishPrimary()
560 // Called at the end of each primary track
562 AliRunLoader *runloader=AliRunLoader::Instance();
563 // static Int_t count=0;
564 // const Int_t times=10;
565 // This primary is finished, purify stack
566 #if ROOT_VERSION_CODE > 262152
567 if (!(gMC->SecondariesAreOrdered())) {
568 if (runloader->Stack()->ReorderKine()) RemapHits();
571 if (runloader->Stack()->PurifyKine()) RemapHits();
573 TIter next(gAlice->Modules());
575 while((detector = dynamic_cast<AliModule*>(next()))) {
576 detector->FinishPrimary();
577 AliLoader* loader = detector->GetLoader();
580 TTree* treeH = loader->TreeH();
581 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
585 // Write out track references if any
586 if (fTmpTreeTR) fTmpTreeTR->Fill();
589 void AliMC::RemapHits()
592 // Remaps the track labels of the hits
593 AliRunLoader *runloader=AliRunLoader::Instance();
594 AliStack* stack = runloader->Stack();
595 TList* hitLists = GetHitLists();
596 TIter next(hitLists);
597 TCollection *hitList;
599 while((hitList = dynamic_cast<TCollection*>(next()))) {
600 TIter nexthit(hitList);
602 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
603 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
608 // This for detectors which have a special mapping mechanism
609 // for hits, such as TPC and TRD
613 TObjArray* modules = gAlice->Modules();
614 TIter nextmod(modules);
616 while((module = (AliModule*) nextmod())) {
617 AliDetector* det = dynamic_cast<AliDetector*> (module);
618 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
621 RemapTrackReferencesIDs(stack->TrackLabelMap());
624 //_______________________________________________________________________
625 void AliMC::FinishEvent()
628 // Called at the end of the event.
633 if(AliSimulation::Instance()->Lego()) AliSimulation::Instance()->Lego()->FinishEvent();
635 TIter next(gAlice->Modules());
637 while((detector = dynamic_cast<AliModule*>(next()))) {
638 detector->FinishEvent();
641 //Update the energy deposit tables
643 for(i=0;i<fEventEnergy.GetSize();i++)
645 fSummEnergy[i]+=fEventEnergy[i];
646 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
649 AliRunLoader *runloader=AliRunLoader::Instance();
651 AliHeader* header = runloader->GetHeader();
652 AliStack* stack = runloader->Stack();
653 if ( (header == 0x0) || (stack == 0x0) )
654 {//check if we got header and stack. If not cry and exit aliroot
655 AliFatal("Can not get the stack or header from LOADER");
656 return;//never reached
658 // Update Header information
659 header->SetNprimary(stack->GetNprimary());
660 header->SetNtrack(stack->GetNtrack());
662 // Write out the kinematics
663 if (!AliSimulation::Instance()->Lego()) stack->FinishEvent();
665 // Synchronize the TreeTR with TreeK
666 if (fTmpTreeTR) ReorderAndExpandTreeTR();
668 // Write out the event Header information
669 TTree* treeE = runloader->TreeE();
672 header->SetStack(stack);
677 AliError("Can not get TreeE from RL");
680 if(AliSimulation::Instance()->Lego() == 0x0)
682 runloader->WriteKinematics("OVERWRITE");
683 runloader->WriteTrackRefs("OVERWRITE");
684 runloader->WriteHits("OVERWRITE");
687 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
688 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
689 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
690 AliDebug(1, " FINISHING EVENT ");
691 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
692 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
693 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
696 //_______________________________________________________________________
701 //=================Create Materials and geometry
703 // Set alignable volumes for the whole geometry (with old root)
704 #if ROOT_VERSION_CODE < 331527
705 SetAllAlignableVolumes();
707 //Read the cuts for all materials
709 //Build the special IMEDIA table
712 //Compute cross-sections
715 //Initialise geometry deposition table
716 fEventEnergy.Set(gMC->NofVolumes()+1);
717 fSummEnergy.Set(gMC->NofVolumes()+1);
718 fSum2Energy.Set(gMC->NofVolumes()+1);
720 // Register MC in configuration
721 AliConfig::Instance()->Add(gMC);
725 //_______________________________________________________________________
726 void AliMC::MediaTable()
729 // Built media table to get from the media number to
733 Int_t kz, nz, idt, lz, i, k, ind;
735 TObjArray &dets = *gAlice->Detectors();
737 Int_t ndets=gAlice->GetNdets();
740 for (kz=0;kz<ndets;kz++) {
741 // If detector is defined
742 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
743 TArrayI &idtmed = *(det->GetIdtmed());
744 for(nz=0;nz<100;nz++) {
746 // Find max and min material number
747 if((idt=idtmed[nz])) {
748 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
749 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
752 if(det->LoMedium() > det->HiMedium()) {
756 if(det->HiMedium() > fImedia->GetSize()) {
757 AliError(Form("Increase fImedia from %d to %d",
758 fImedia->GetSize(),det->HiMedium()));
761 // Tag all materials in rage as belonging to detector kz
762 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
769 // Print summary table
770 AliInfo("Tracking media ranges:");
772 for(i=0;i<(ndets-1)/6+1;i++) {
773 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
775 det=dynamic_cast<AliModule*>(dets[ind]);
777 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
780 printf(" %6s: %3d -> %3d;","NULL",0,0);
787 //_______________________________________________________________________
788 void AliMC::ReadTransPar()
791 // Read filename to set the transport parameters
795 const Int_t kncuts=10;
796 const Int_t knflags=11;
797 const Int_t knpars=kncuts+knflags;
798 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
799 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
800 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
801 "MULS","PAIR","PHOT","RAYL"};
807 Int_t i, itmed, iret, ktmed, kz;
810 // See whether the file is there
811 filtmp=gSystem->ExpandPathName(fTransParName.Data());
812 lun=fopen(filtmp,"r");
815 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
820 // Initialise cuts and flags
821 for(i=0;i<kncuts;i++) cut[i]=-99;
822 for(i=0;i<knflags;i++) flag[i]=-99;
824 for(i=0;i<256;i++) line[i]='\0';
825 // Read up to the end of line excluded
826 iret=fscanf(lun,"%[^\n]",line);
832 // Read the end of line
835 if(line[0]=='*') continue;
837 iret=sscanf(line,"%s %d %f %f %f %f %f %f %f %f %f %f %d %d %d %d %d %d %d %d %d %d %d",
838 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
839 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
840 &flag[8],&flag[9],&flag[10]);
844 AliWarning(Form("Error reading file %s",fTransParName.Data()));
847 // Check that the module exist
848 AliModule *mod = gAlice->GetModule(detName);
850 // Get the array of media numbers
851 TArrayI &idtmed = *mod->GetIdtmed();
852 // Check that the tracking medium code is valid
853 if(0<=itmed && itmed < 100) {
856 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
859 // Set energy thresholds
860 for(kz=0;kz<kncuts;kz++) {
862 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
863 kpars[kz],cut[kz],itmed,mod->GetName()));
864 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
867 // Set transport mechanisms
868 for(kz=0;kz<knflags;kz++) {
870 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
871 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
872 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
876 AliWarning(Form("Invalid medium code %d",itmed));
880 AliDebug(1, Form("%s not present",detName));
886 //_______________________________________________________________________
887 void AliMC::SetTransPar(const char *filename)
890 // Sets the file name for transport parameters
892 fTransParName = filename;
895 //_______________________________________________________________________
896 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
899 // Add a hit to detector id
901 TObjArray &dets = *gAlice->Modules();
902 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
905 //_______________________________________________________________________
906 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
909 // Add digit to detector id
911 TObjArray &dets = *gAlice->Modules();
912 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
915 //_______________________________________________________________________
916 Int_t AliMC::GetCurrentTrackNumber() const {
918 // Returns current track
920 return AliRunLoader::Instance()->Stack()->GetCurrentTrackNumber();
923 //_______________________________________________________________________
924 void AliMC::DumpPart (Int_t i) const
927 // Dumps particle i in the stack
929 AliRunLoader * runloader = AliRunLoader::Instance();
930 if (runloader->Stack())
931 runloader->Stack()->DumpPart(i);
934 //_______________________________________________________________________
935 void AliMC::DumpPStack () const
938 // Dumps the particle stack
940 AliRunLoader * runloader = AliRunLoader::Instance();
941 if (runloader->Stack())
942 runloader->Stack()->DumpPStack();
945 //_______________________________________________________________________
946 Int_t AliMC::GetNtrack() const {
948 // Returns number of tracks in stack
951 AliRunLoader * runloader = AliRunLoader::Instance();
952 if (runloader->Stack())
953 ntracks = runloader->Stack()->GetNtrack();
957 //_______________________________________________________________________
958 Int_t AliMC::GetPrimary(Int_t track) const
961 // return number of primary that has generated track
963 Int_t nprimary = -999;
964 AliRunLoader * runloader = AliRunLoader::Instance();
965 if (runloader->Stack())
966 nprimary = runloader->Stack()->GetPrimary(track);
970 //_______________________________________________________________________
971 TParticle* AliMC::Particle(Int_t i) const
973 // Returns the i-th particle from the stack taking into account
974 // the remaping done by PurifyKine
975 AliRunLoader * runloader = AliRunLoader::Instance();
977 if (runloader->Stack())
978 return runloader->Stack()->Particle(i);
982 //_______________________________________________________________________
983 const TObjArray* AliMC::Particles() const {
985 // Returns pointer to Particles array
987 AliRunLoader * runloader = AliRunLoader::Instance();
989 if (runloader->Stack())
990 return runloader->Stack()->Particles();
994 //_______________________________________________________________________
995 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, const Float_t *pmom,
996 const Float_t *vpos, const Float_t *polar, Float_t tof,
997 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1001 AliRunLoader * runloader = AliRunLoader::Instance();
1003 if (runloader->Stack())
1004 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1005 mech, ntr, weight, is);
1008 //_______________________________________________________________________
1009 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1010 Double_t px, Double_t py, Double_t pz, Double_t e,
1011 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1012 Double_t polx, Double_t poly, Double_t polz,
1013 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1015 // Delegate to stack
1017 AliRunLoader * runloader = AliRunLoader::Instance();
1019 if (runloader->Stack())
1020 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1021 polx, poly, polz, mech, ntr, weight, is);
1024 //_______________________________________________________________________
1025 void AliMC::SetHighWaterMark(Int_t nt) const
1028 // Set high water mark for last track in event
1029 AliRunLoader * runloader = AliRunLoader::Instance();
1031 if (runloader->Stack())
1032 runloader->Stack()->SetHighWaterMark(nt);
1035 //_______________________________________________________________________
1036 void AliMC::KeepTrack(Int_t track) const
1039 // Delegate to stack
1041 AliRunLoader * runloader = AliRunLoader::Instance();
1043 if (runloader->Stack())
1044 runloader->Stack()->KeepTrack(track);
1047 //_______________________________________________________________________
1048 void AliMC::FlagTrack(Int_t track) const
1050 // Delegate to stack
1052 AliRunLoader * runloader = AliRunLoader::Instance();
1054 if (runloader->Stack())
1055 runloader->Stack()->FlagTrack(track);
1058 //_______________________________________________________________________
1059 void AliMC::SetCurrentTrack(Int_t track) const
1062 // Set current track number
1064 AliRunLoader * runloader = AliRunLoader::Instance();
1066 if (runloader->Stack())
1067 runloader->Stack()->SetCurrentTrack(track);
1070 //_______________________________________________________________________
1071 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1074 // add a trackrefernce to the list
1075 Int_t primary = GetPrimary(label);
1076 Particle(primary)->SetBit(kKeepBit);
1078 Int_t nref = fTmpTrackReferences.GetEntriesFast();
1079 return new(fTmpTrackReferences[nref]) AliTrackReference(label, id);
1084 //_______________________________________________________________________
1085 void AliMC::ResetTrackReferences()
1088 // Reset all references
1090 fTmpTrackReferences.Clear();
1093 void AliMC::RemapTrackReferencesIDs(Int_t *map)
1096 // Remapping track reference
1097 // Called at finish primary
1100 Int_t nEntries = fTmpTrackReferences.GetEntries();
1101 for (Int_t i=0; i < nEntries; i++){
1102 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences.UncheckedAt(i));
1104 Int_t newID = map[ref->GetTrack()];
1105 if (newID>=0) ref->SetTrack(newID);
1107 ref->SetBit(kNotDeleted,kFALSE);
1108 fTmpTrackReferences.RemoveAt(i);
1112 fTmpTrackReferences.Compress();
1115 void AliMC::FixParticleDecaytime()
1118 // Fix the particle decay time according to rmin and rmax for decays
1122 gMC->TrackMomentum(p);
1123 Double_t tmin, tmax;
1126 // Transverse velocity
1127 Double_t vt = p.Pt() / p.E();
1129 if ((b = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->SolenoidField()) > 0.) { // [kG]
1133 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1135 // Revolution frequency
1137 Double_t omega = vt / rho;
1139 // Maximum and minimum decay time
1141 // Check for curlers first
1142 if (fRDecayMax * fRDecayMax / rho / rho / 2. > 1.) return;
1146 tmax = TMath::ACos(1. - fRDecayMax * fRDecayMax / rho / rho / 2.) / omega; // [ct]
1147 tmin = TMath::ACos(1. - fRDecayMin * fRDecayMin / rho / rho / 2.) / omega; // [ct]
1149 tmax = fRDecayMax / vt; // [ct]
1150 tmin = fRDecayMin / vt; // [ct]
1154 // Dial t using the two limits
1155 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1158 // Force decay time in transport code
1160 gMC->ForceDecayTime(t / 2.99792458e10);
1163 void AliMC::MakeTmpTrackRefsTree()
1165 // Make the temporary track reference tree
1166 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1167 fTmpTreeTR = new TTree("TreeTR", "Track References");
1168 TClonesArray* pRef = &fTmpTrackReferences;
1169 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &pRef, 4000);
1172 void AliMC::ReorderAndExpandTreeTR()
1175 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1178 AliRunLoader *rl = AliRunLoader::Instance();
1181 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1182 rl->MakeTrackRefsContainer();
1183 TTree * treeTR = rl->TreeTR();
1185 // make branch for central track references
1187 TClonesArray* pRef = &fTrackReferences;
1188 branch = treeTR->Branch("TrackReferences", &pRef);
1189 branch->SetAddress(&pRef);
1192 AliStack* stack = rl->Stack();
1193 Int_t np = stack->GetNprimary();
1194 Int_t nt = fTmpTreeTR->GetEntries();
1196 // Loop over tracks and find the secondaries with the help of the kine tree
1199 for (Int_t ip = np - 1; ip > -1; ip--) {
1200 TParticle *part = stack->Particle(ip);
1201 //printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1203 // Skip primaries that have not been transported
1204 Int_t dau1 = part->GetFirstDaughter();
1206 if (!part->TestBit(kTransportBit)) continue;
1208 fTmpTreeTR->GetEntry(it++);
1209 Int_t nh = fTmpTrackReferences.GetEntries();
1210 // Determine range of secondaries produced by this primary
1212 Int_t inext = ip - 1;
1215 part = stack->Particle(inext);
1216 dau2 = part->GetFirstDaughter();
1217 if (!(part->TestBit(kTransportBit)) || dau2 == -1 || dau2 < np) {
1218 // if (dau2 == -1 || dau2 < np) {
1224 dau2 = stack->GetNtrack() - 1;
1227 } // find upper bound
1229 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1231 // Loop over reference hits and find secondary label
1232 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1233 for (Int_t ih = 0; ih < nh; ih++) {
1234 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1235 Int_t label = tr->Label();
1237 if (label == ip) continue;
1238 if (label > dau2 || label < dau1)
1239 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1242 Int_t nref = fTrackReferences.GetEntriesFast();
1243 new(fTrackReferences[nref]) AliTrackReference(*tr);
1247 fTrackReferences.Clear();
1252 // Now loop again and write the primaries
1254 for (Int_t ip = 0; ip < np; ip++) {
1255 TParticle* part = stack->Particle(ip);
1256 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1257 if (part->TestBit(kTransportBit))
1259 // Skip particles that have not been transported
1260 fTmpTreeTR->GetEntry(it--);
1261 Int_t nh = fTmpTrackReferences.GetEntries();
1263 // Loop over reference hits and find primary labels
1264 for (Int_t ih = 0; ih < nh; ih++) {
1265 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1266 Int_t label = tr->Label();
1268 Int_t nref = fTrackReferences.GetEntriesFast();
1269 new(fTrackReferences[nref]) AliTrackReference(*tr);
1274 fTrackReferences.Clear();
1278 if (ifills != stack->GetNtrack())
1279 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1283 fTmpFileTR->Close();
1285 fTmpTrackReferences.Clear();
1286 gSystem->Exec("rm -rf TrackRefsTmp.root");