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>
38 #include "AliCDBEntry.h"
39 #include "AliCDBManager.h"
40 #include "AliCDBStorage.h"
41 #include "AliDetector.h"
42 #include "AliGenerator.h"
43 #include "AliGeomManager.h"
44 #include "AliHeader.h"
51 #include "AliSimulation.h"
53 #include "AliTrackReference.h"
57 //_______________________________________________________________________
81 //_______________________________________________________________________
82 AliMC::AliMC(const char *name, const char *title) :
83 TVirtualMCApplication(name, title),
93 fImedia(new TArrayI(1000)),
95 fHitLists(new TList()),
98 fTrackReferences("AliTrackReference", 100),
99 fTmpTrackReferences("AliTrackReference", 100)
102 // Set transport parameters
105 // Prepare the tracking medium lists
106 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
109 //_______________________________________________________________________
116 // Delete track references
119 //_______________________________________________________________________
120 void AliMC::ConstructGeometry()
123 // Either load geometry from file or create it through usual
124 // loop on detectors. In the first case the method
125 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
126 // at InitGeometry().
129 if(AliSimulation::Instance()->IsGeometryFromFile()){ //load geometry either from CDB or from file
130 if(IsGeometryFromCDB()){
131 AliInfo("Loading geometry from CDB default storage");
132 AliCDBPath path("GRP","Geometry","Data");
133 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
134 if(!entry) AliFatal("Unable to load geometry from CDB!");
136 gGeoManager = (TGeoManager*) entry->GetObject();
137 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
140 const char *geomfilename = AliSimulation::Instance()->GetGeometryFile();
141 if(gSystem->ExpandPathName(geomfilename)){
142 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
143 TGeoManager::Import(geomfilename);
145 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
149 gMC->SetRootGeometry();
151 // Create modules, materials, geometry
152 if (!gGeoManager) new TGeoManager("ALICE", "ALICE geometry");
154 TIter next(gAlice->Modules());
156 AliDebug(1, "Geometry creation:");
157 while((detector = dynamic_cast<AliModule*>(next()))) {
159 // Initialise detector materials and geometry
160 detector->CreateMaterials();
161 detector->CreateGeometry();
162 AliInfo(Form("%10s R:%.2fs C:%.2fs",
163 detector->GetName(),stw.RealTime(),stw.CpuTime()));
169 //_______________________________________________________________________
170 Bool_t AliMC::MisalignGeometry()
172 // Call misalignment code if AliSimulation object was defined.
174 if(!AliSimulation::Instance()->IsGeometryFromFile()){
175 //Set alignable volumes for the whole geometry
176 SetAllAlignableVolumes();
178 // Misalign geometry via AliSimulation instance
179 if (!AliSimulation::Instance()) return kFALSE;
180 AliGeomManager::SetGeometry(gGeoManager);
181 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
182 AliFatal("Current loaded geometry differs in the definition of symbolic names!");
184 return AliSimulation::Instance()->MisalignGeometry(AliRunLoader::Instance());
187 //_______________________________________________________________________
188 void AliMC::ConstructOpGeometry()
191 // Loop all detector modules and call DefineOpticalProperties() method
194 TIter next(gAlice->Modules());
196 AliInfo("Optical properties definition");
197 while((detector = dynamic_cast<AliModule*>(next()))) {
198 // Initialise detector geometry
199 if(AliSimulation::Instance()->IsGeometryFromFile()) detector->CreateMaterials();
200 // Initialise detector optical properties
201 detector->DefineOpticalProperties();
205 //_______________________________________________________________________
206 void AliMC::InitGeometry()
209 // Initialize detectors
212 AliInfo("Initialisation:");
214 TIter next(gAlice->Modules());
216 while((detector = dynamic_cast<AliModule*>(next()))) {
219 AliInfo(Form("%10s R:%.2fs C:%.2fs",
220 detector->GetName(),stw.RealTime(),stw.CpuTime()));
224 //_______________________________________________________________________
225 void AliMC::SetGeometryFromCDB()
227 // Set the loading of geometry from cdb instead of creating it
228 // A default CDB storage needs to be set before this method is called
229 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
230 AliCDBManager::Instance()->GetRun() >= 0)
231 AliSimulation::Instance()->SetGeometryFile("*OCDB*");
233 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
236 //_______________________________________________________________________
237 Bool_t AliMC::IsGeometryFromCDB() const
239 return (strcmp(AliSimulation::Instance()->GetGeometryFile(),"*OCDB*")==0);
242 //_______________________________________________________________________
243 void AliMC::SetAllAlignableVolumes()
246 // Add alignable volumes (TGeoPNEntries) looping on all
250 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
252 TIter next(gAlice->Modules());
253 while((detector = dynamic_cast<AliModule*>(next()))) {
254 detector->AddAlignableVolumes();
258 //_______________________________________________________________________
259 void AliMC::GeneratePrimaries()
262 // Generate primary particles and fill them in the stack.
265 Generator()->Generate();
268 //_______________________________________________________________________
269 void AliMC::SetGenerator(AliGenerator *generator)
272 // Load the event generator
274 if(!fGenerator) fGenerator = generator;
277 //_______________________________________________________________________
278 void AliMC::ResetGenerator(AliGenerator *generator)
281 // Load the event generator
285 AliWarning(Form("Replacing generator %s with %s",
286 fGenerator->GetName(),generator->GetName()));
289 AliWarning(Form("Replacing generator %s with NULL",
290 fGenerator->GetName()));
293 fGenerator = generator;
296 //_______________________________________________________________________
297 void AliMC::FinishRun()
299 // Clean generator information
300 AliDebug(1, "fGenerator->FinishRun()");
301 fGenerator->FinishRun();
303 //Output energy summary tables
304 AliDebug(1, "EnergySummary()");
305 ToAliDebug(1, EnergySummary());
308 //_______________________________________________________________________
309 void AliMC::BeginPrimary()
312 // Called at the beginning of each primary track
317 ResetTrackReferences();
320 //_______________________________________________________________________
321 void AliMC::PreTrack()
323 // Actions before the track's transport
325 TObjArray &dets = *gAlice->Modules();
328 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
329 if((module = dynamic_cast<AliModule*>(dets[i])))
333 //_______________________________________________________________________
334 void AliMC::Stepping()
337 // Called at every step during transport
339 Int_t id = DetFromMate(gMC->CurrentMedium());
343 if ( gMC->IsNewTrack() &&
344 gMC->TrackTime() == 0. &&
346 fRDecayMax > fRDecayMin &&
347 gMC->TrackPid() == fDecayPdg )
349 FixParticleDecaytime();
355 // --- If lego option, do it and leave
356 if (AliSimulation::Instance()->Lego())
357 AliSimulation::Instance()->Lego()->StepManager();
360 //Update energy deposition tables
361 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
363 // write tracke reference for track which is dissapearing - MI
365 if (gMC->IsTrackDisappeared() && !(gMC->IsTrackAlive())) {
366 if (gMC->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
367 AliTrackReference::kDisappeared);
372 //Call the appropriate stepping routine;
373 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
374 if(det && det->StepManagerIsEnabled()) {
380 //_______________________________________________________________________
381 void AliMC::EnergySummary()
384 // Print summary of deposited energy
390 Int_t kn, i, left, j, id;
391 const Float_t kzero=0;
392 Int_t ievent=AliRunLoader::Instance()->GetHeader()->GetEvent()+1;
394 // Energy loss information
396 printf("***************** Energy Loss Information per event (GEV) *****************\n");
397 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
400 fEventEnergy[ndep]=kn;
405 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
408 fSummEnergy[ndep]=ed;
409 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
414 for(kn=0;kn<(ndep-1)/3+1;kn++) {
416 for(i=0;i<(3<left?3:left);i++) {
418 id=Int_t (fEventEnergy[j]+0.1);
419 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
424 // Relative energy loss in different detectors
425 printf("******************** Relative Energy Loss per event ********************\n");
426 printf("Total energy loss per event %10.3f GeV\n",edtot);
427 for(kn=0;kn<(ndep-1)/5+1;kn++) {
429 for(i=0;i<(5<left?5:left);i++) {
431 id=Int_t (fEventEnergy[j]+0.1);
432 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
436 for(kn=0;kn<75;kn++) printf("*");
440 // Reset the TArray's
441 // fEventEnergy.Set(0);
442 // fSummEnergy.Set(0);
443 // fSum2Energy.Set(0);
446 //_____________________________________________________________________________
447 void AliMC::BeginEvent()
450 // Clean-up previous event
452 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
453 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
454 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
455 AliDebug(1, " BEGINNING EVENT ");
456 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
457 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
458 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
460 AliRunLoader *runloader=AliRunLoader::Instance();
462 /*******************************/
463 /* Clean after eventual */
465 /*******************************/
468 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
469 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
470 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
471 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
473 fEventEnergy.Reset();
474 // Clean detector information
476 if (runloader->Stack())
477 runloader->Stack()->Reset();//clean stack -> tree is unloaded
479 runloader->MakeStack();//or make a new one
482 if(AliSimulation::Instance()->Lego() == 0x0)
484 AliDebug(1, "fRunLoader->MakeTree(K)");
485 runloader->MakeTree("K");
488 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
489 gMC->SetStack(runloader->Stack());//Was in InitMC - but was moved here
490 //because we don't have guarantee that
491 //stack pointer is not going to change from event to event
492 //since it bellobgs to header and is obtained via RunLoader
494 // Reset all Detectors & kinematics & make/reset trees
497 runloader->GetHeader()->Reset(AliCDBManager::Instance()->GetRun(),gAlice->GetEvNumber(),
498 gAlice->GetEventNrInRun());
499 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
501 if(AliSimulation::Instance()->Lego())
503 AliSimulation::Instance()->Lego()->BeginEvent();
508 AliDebug(1, "ResetHits()");
511 AliDebug(1, "fRunLoader->MakeTree(H)");
512 runloader->MakeTree("H");
516 MakeTmpTrackRefsTree();
517 //create new branches and SetAdresses
518 TIter next(gAlice->Modules());
520 while((detector = (AliModule*)next()))
522 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
523 detector->MakeBranch("H");
527 //_______________________________________________________________________
528 void AliMC::ResetHits()
531 // Reset all Detectors hits
533 TIter next(gAlice->Modules());
535 while((detector = dynamic_cast<AliModule*>(next()))) {
536 detector->ResetHits();
540 //_______________________________________________________________________
541 void AliMC::ResetDigits()
544 // Reset all Detectors digits
546 TIter next(gAlice->Modules());
548 while((detector = dynamic_cast<AliModule*>(next()))) {
549 detector->ResetDigits();
553 //_______________________________________________________________________
554 void AliMC::ResetSDigits()
557 // Reset all Detectors digits
559 TIter next(gAlice->Modules());
561 while((detector = dynamic_cast<AliModule*>(next()))) {
562 detector->ResetSDigits();
566 //_______________________________________________________________________
567 void AliMC::PostTrack()
569 // Posts tracks for each module
571 TObjArray &dets = *gAlice->Modules();
574 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
575 if((module = dynamic_cast<AliModule*>(dets[i])))
579 //_______________________________________________________________________
580 void AliMC::FinishPrimary()
583 // Called at the end of each primary track
586 AliRunLoader *runloader=AliRunLoader::Instance();
587 // static Int_t count=0;
588 // const Int_t times=10;
589 // This primary is finished, purify stack
590 #if ROOT_VERSION_CODE > 262152
591 if (!(gMC->SecondariesAreOrdered())) {
592 if (runloader->Stack()->ReorderKine()) RemapHits();
595 if (runloader->Stack()->PurifyKine()) RemapHits();
597 TIter next(gAlice->Modules());
599 while((detector = dynamic_cast<AliModule*>(next()))) {
600 detector->FinishPrimary();
601 AliLoader* loader = detector->GetLoader();
604 TTree* treeH = loader->TreeH();
605 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
609 // Write out track references if any
610 if (fTmpTreeTR) fTmpTreeTR->Fill();
613 void AliMC::RemapHits()
616 // Remaps the track labels of the hits
617 AliRunLoader *runloader=AliRunLoader::Instance();
618 AliStack* stack = runloader->Stack();
619 TList* hitLists = GetHitLists();
620 TIter next(hitLists);
621 TCollection *hitList;
623 while((hitList = dynamic_cast<TCollection*>(next()))) {
624 TIter nexthit(hitList);
626 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
627 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
632 // This for detectors which have a special mapping mechanism
633 // for hits, such as TPC and TRD
637 TObjArray* modules = gAlice->Modules();
638 TIter nextmod(modules);
640 while((module = (AliModule*) nextmod())) {
641 AliDetector* det = dynamic_cast<AliDetector*> (module);
642 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
645 RemapTrackReferencesIDs(stack->TrackLabelMap());
648 //_______________________________________________________________________
649 void AliMC::FinishEvent()
652 // Called at the end of the event.
655 if(AliSimulation::Instance()->Lego()) AliSimulation::Instance()->Lego()->FinishEvent();
657 TIter next(gAlice->Modules());
659 while((detector = dynamic_cast<AliModule*>(next()))) {
660 detector->FinishEvent();
663 //Update the energy deposit tables
665 for(i=0;i<fEventEnergy.GetSize();i++)
667 fSummEnergy[i]+=fEventEnergy[i];
668 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
671 AliRunLoader *runloader=AliRunLoader::Instance();
673 AliHeader* header = runloader->GetHeader();
674 AliStack* stack = runloader->Stack();
675 if ( (header == 0x0) || (stack == 0x0) )
676 {//check if we got header and stack. If not cry and exit aliroot
677 AliFatal("Can not get the stack or header from LOADER");
678 return;//never reached
680 // Update Header information
681 header->SetNprimary(stack->GetNprimary());
682 header->SetNtrack(stack->GetNtrack());
684 // Write out the kinematics
685 if (!AliSimulation::Instance()->Lego()) stack->FinishEvent();
687 // Synchronize the TreeTR with TreeK
688 if (fTmpTreeTR) ReorderAndExpandTreeTR();
690 // Write out the event Header information
691 TTree* treeE = runloader->TreeE();
694 header->SetStack(stack);
699 AliError("Can not get TreeE from RL");
702 if(AliSimulation::Instance()->Lego() == 0x0)
704 runloader->WriteKinematics("OVERWRITE");
705 runloader->WriteTrackRefs("OVERWRITE");
706 runloader->WriteHits("OVERWRITE");
709 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
710 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
711 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
712 AliDebug(1, " FINISHING EVENT ");
713 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
714 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
715 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
718 //_______________________________________________________________________
723 //=================Create Materials and geometry
725 // Set alignable volumes for the whole geometry (with old root)
726 #if ROOT_VERSION_CODE < 331527
727 SetAllAlignableVolumes();
729 //Read the cuts for all materials
731 //Build the special IMEDIA table
734 //Compute cross-sections
737 //Initialise geometry deposition table
738 fEventEnergy.Set(gMC->NofVolumes()+1);
739 fSummEnergy.Set(gMC->NofVolumes()+1);
740 fSum2Energy.Set(gMC->NofVolumes()+1);
742 // Register MC in configuration
743 AliConfig::Instance()->Add(gMC);
747 //_______________________________________________________________________
748 void AliMC::MediaTable()
751 // Built media table to get from the media number to
755 Int_t kz, nz, idt, lz, i, k, ind;
757 TObjArray &dets = *gAlice->Detectors();
759 Int_t ndets=gAlice->GetNdets();
762 for (kz=0;kz<ndets;kz++) {
763 // If detector is defined
764 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
765 TArrayI &idtmed = *(det->GetIdtmed());
766 for(nz=0;nz<100;nz++) {
768 // Find max and min material number
769 if((idt=idtmed[nz])) {
770 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
771 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
774 if(det->LoMedium() > det->HiMedium()) {
778 if(det->HiMedium() > fImedia->GetSize()) {
779 AliError(Form("Increase fImedia from %d to %d",
780 fImedia->GetSize(),det->HiMedium()));
783 // Tag all materials in rage as belonging to detector kz
784 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
791 // Print summary table
792 AliInfo("Tracking media ranges:");
794 for(i=0;i<(ndets-1)/6+1;i++) {
795 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
797 det=dynamic_cast<AliModule*>(dets[ind]);
799 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
802 printf(" %6s: %3d -> %3d;","NULL",0,0);
809 //_______________________________________________________________________
810 void AliMC::ReadTransPar()
813 // Read filename to set the transport parameters
817 const Int_t kncuts=10;
818 const Int_t knflags=12;
819 const Int_t knpars=kncuts+knflags;
820 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
821 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
822 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
823 "MULS","PAIR","PHOT","RAYL","STRA"};
829 Int_t i, itmed, iret, ktmed, kz;
832 // See whether the file is there
833 filtmp=gSystem->ExpandPathName(fTransParName.Data());
834 lun=fopen(filtmp,"r");
837 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
842 // Initialise cuts and flags
843 for(i=0;i<kncuts;i++) cut[i]=-99;
844 for(i=0;i<knflags;i++) flag[i]=-99;
847 // Read up to the end of line excluded
848 iret=fscanf(lun,"%255[^\n]",line);
854 // Read the end of line
857 if(line[0]=='*') continue;
859 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 %d",
860 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
861 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
862 &flag[8],&flag[9],&flag[10],&flag[11]);
866 AliWarning(Form("Error reading file %s",fTransParName.Data()));
869 // Check that the module exist
870 AliModule *mod = gAlice->GetModule(detName);
872 // Get the array of media numbers
873 TArrayI &idtmed = *mod->GetIdtmed();
874 // Check that the tracking medium code is valid
875 if(0<=itmed && itmed < 100) {
878 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
881 // Set energy thresholds
882 for(kz=0;kz<kncuts;kz++) {
884 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
885 kpars[kz],cut[kz],itmed,mod->GetName()));
886 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
889 // Set transport mechanisms
890 for(kz=0;kz<knflags;kz++) {
892 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
893 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
894 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
898 AliWarning(Form("Invalid medium code %d",itmed));
902 AliDebug(1, Form("%s not present",detName));
908 //_______________________________________________________________________
909 void AliMC::SetTransPar(const char *filename)
912 // Sets the file name for transport parameters
914 fTransParName = filename;
917 //_______________________________________________________________________
918 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
921 // Add a hit to detector id
923 TObjArray &dets = *gAlice->Modules();
924 if(dets[id]) static_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
927 //_______________________________________________________________________
928 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
931 // Add digit to detector id
933 TObjArray &dets = *gAlice->Modules();
934 if(dets[id]) static_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
937 //_______________________________________________________________________
938 Int_t AliMC::GetCurrentTrackNumber() const {
940 // Returns current track
942 return AliRunLoader::Instance()->Stack()->GetCurrentTrackNumber();
945 //_______________________________________________________________________
946 void AliMC::DumpPart (Int_t i) const
949 // Dumps particle i in the stack
951 AliRunLoader * runloader = AliRunLoader::Instance();
952 if (runloader->Stack())
953 runloader->Stack()->DumpPart(i);
956 //_______________________________________________________________________
957 void AliMC::DumpPStack () const
960 // Dumps the particle stack
962 AliRunLoader * runloader = AliRunLoader::Instance();
963 if (runloader->Stack())
964 runloader->Stack()->DumpPStack();
967 //_______________________________________________________________________
968 Int_t AliMC::GetNtrack() const {
970 // Returns number of tracks in stack
973 AliRunLoader * runloader = AliRunLoader::Instance();
974 if (runloader->Stack())
975 ntracks = runloader->Stack()->GetNtrack();
979 //_______________________________________________________________________
980 Int_t AliMC::GetPrimary(Int_t track) const
983 // return number of primary that has generated track
985 Int_t nprimary = -999;
986 AliRunLoader * runloader = AliRunLoader::Instance();
987 if (runloader->Stack())
988 nprimary = runloader->Stack()->GetPrimary(track);
992 //_______________________________________________________________________
993 TParticle* AliMC::Particle(Int_t i) const
995 // Returns the i-th particle from the stack taking into account
996 // the remaping done by PurifyKine
997 AliRunLoader * runloader = AliRunLoader::Instance();
999 if (runloader->Stack())
1000 return runloader->Stack()->Particle(i);
1004 //_______________________________________________________________________
1005 const TObjArray* AliMC::Particles() const {
1007 // Returns pointer to Particles array
1009 AliRunLoader * runloader = AliRunLoader::Instance();
1011 if (runloader->Stack())
1012 return runloader->Stack()->Particles();
1016 //_______________________________________________________________________
1017 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, const Float_t *pmom,
1018 const Float_t *vpos, const Float_t *polar, Float_t tof,
1019 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1021 // Delegate to stack
1023 AliRunLoader * runloader = AliRunLoader::Instance();
1025 if (runloader->Stack())
1026 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1027 mech, ntr, weight, is);
1030 //_______________________________________________________________________
1031 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1032 Double_t px, Double_t py, Double_t pz, Double_t e,
1033 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1034 Double_t polx, Double_t poly, Double_t polz,
1035 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1037 // Delegate to stack
1039 AliRunLoader * runloader = AliRunLoader::Instance();
1041 if (runloader->Stack())
1042 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1043 polx, poly, polz, mech, ntr, weight, is);
1046 //_______________________________________________________________________
1047 void AliMC::SetHighWaterMark(Int_t nt) const
1050 // Set high water mark for last track in event
1051 AliRunLoader * runloader = AliRunLoader::Instance();
1053 if (runloader->Stack())
1054 runloader->Stack()->SetHighWaterMark(nt);
1057 //_______________________________________________________________________
1058 void AliMC::KeepTrack(Int_t track) const
1061 // Delegate to stack
1063 AliRunLoader * runloader = AliRunLoader::Instance();
1065 if (runloader->Stack())
1066 runloader->Stack()->KeepTrack(track);
1069 //_______________________________________________________________________
1070 void AliMC::FlagTrack(Int_t track) const
1072 // Delegate to stack
1074 AliRunLoader * runloader = AliRunLoader::Instance();
1076 if (runloader->Stack())
1077 runloader->Stack()->FlagTrack(track);
1080 //_______________________________________________________________________
1081 void AliMC::SetCurrentTrack(Int_t track) const
1084 // Set current track number
1086 AliRunLoader * runloader = AliRunLoader::Instance();
1088 if (runloader->Stack())
1089 runloader->Stack()->SetCurrentTrack(track);
1092 //_______________________________________________________________________
1093 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1096 // add a trackrefernce to the list
1097 Int_t primary = GetPrimary(label);
1098 Particle(primary)->SetBit(kKeepBit);
1100 Int_t nref = fTmpTrackReferences.GetEntriesFast();
1101 return new(fTmpTrackReferences[nref]) AliTrackReference(label, id);
1106 //_______________________________________________________________________
1107 void AliMC::ResetTrackReferences()
1110 // Reset all references
1112 fTmpTrackReferences.Clear();
1115 //_______________________________________________________________________
1116 void AliMC::RemapTrackReferencesIDs(const Int_t *map)
1119 // Remapping track reference
1120 // Called at finish primary
1123 Int_t nEntries = fTmpTrackReferences.GetEntries();
1124 for (Int_t i=0; i < nEntries; i++){
1125 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences.UncheckedAt(i));
1127 Int_t newID = map[ref->GetTrack()];
1128 if (newID>=0) ref->SetTrack(newID);
1130 ref->SetBit(kNotDeleted,kFALSE);
1131 fTmpTrackReferences.RemoveAt(i);
1135 fTmpTrackReferences.Compress();
1138 //_______________________________________________________________________
1139 void AliMC::FixParticleDecaytime()
1142 // Fix the particle decay time according to rmin and rmax for decays
1146 gMC->TrackMomentum(p);
1147 Double_t tmin, tmax;
1150 // Transverse velocity
1151 Double_t vt = p.Pt() / p.E();
1153 if ((b = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->SolenoidField()) > 0.) { // [kG]
1157 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1159 // Revolution frequency
1161 Double_t omega = vt / rho;
1163 // Maximum and minimum decay time
1165 // Check for curlers first
1166 const Double_t kOvRhoSqr2 = 1./(rho*TMath::Sqrt(2.));
1167 if (fRDecayMax * kOvRhoSqr2 > 1.) return;
1171 tmax = TMath::ACos((1.-fRDecayMax*kOvRhoSqr2)*(1.+fRDecayMax*kOvRhoSqr2)) / omega; // [ct]
1172 tmin = TMath::ACos((1.-fRDecayMin*kOvRhoSqr2)*(1.+fRDecayMin*kOvRhoSqr2)) / omega; // [ct]
1174 tmax = fRDecayMax / vt; // [ct]
1175 tmin = fRDecayMin / vt; // [ct]
1179 // Dial t using the two limits
1180 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1183 // Force decay time in transport code
1185 gMC->ForceDecayTime(t / 2.99792458e10);
1188 void AliMC::MakeTmpTrackRefsTree()
1190 // Make the temporary track reference tree
1191 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1192 fTmpTreeTR = new TTree("TreeTR", "Track References");
1193 TClonesArray* pRef = &fTmpTrackReferences;
1194 fTmpTreeTR->Branch("TrackReferences", &pRef, 4000);
1197 //_______________________________________________________________________
1198 void AliMC::ReorderAndExpandTreeTR()
1201 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1204 AliRunLoader *rl = AliRunLoader::Instance();
1207 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1208 rl->MakeTrackRefsContainer();
1209 TTree * treeTR = rl->TreeTR();
1210 // make branch for central track references
1211 TClonesArray* pRef = &fTrackReferences;
1212 treeTR->Branch("TrackReferences", &pRef);
1214 AliStack* stack = rl->Stack();
1215 Int_t np = stack->GetNprimary();
1216 Int_t nt = fTmpTreeTR->GetEntries();
1218 // Loop over tracks and find the secondaries with the help of the kine tree
1221 for (Int_t ip = np - 1; ip > -1; ip--) {
1222 TParticle *part = stack->Particle(ip);
1223 //printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1225 // Skip primaries that have not been transported
1226 Int_t dau1 = part->GetFirstDaughter();
1228 if (!part->TestBit(kTransportBit)) continue;
1230 fTmpTreeTR->GetEntry(it++);
1231 Int_t nh = fTmpTrackReferences.GetEntries();
1232 // Determine range of secondaries produced by this primary
1234 Int_t inext = ip - 1;
1237 part = stack->Particle(inext);
1238 dau2 = part->GetFirstDaughter();
1239 if (!(part->TestBit(kTransportBit)) || dau2 == -1 || dau2 < np) {
1240 // if (dau2 == -1 || dau2 < np) {
1246 dau2 = stack->GetNtrack() - 1;
1249 } // find upper bound
1251 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1253 // Loop over reference hits and find secondary label
1254 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1255 for (Int_t ih = 0; ih < nh; ih++) {
1256 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1257 Int_t label = tr->Label();
1259 if (label == ip) continue;
1260 if (label > dau2 || label < dau1)
1261 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1264 Int_t nref = fTrackReferences.GetEntriesFast();
1265 new(fTrackReferences[nref]) AliTrackReference(*tr);
1269 fTrackReferences.Clear();
1274 // Now loop again and write the primaries
1276 for (Int_t ip = 0; ip < np; ip++) {
1277 TParticle* part = stack->Particle(ip);
1278 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1279 if (part->TestBit(kTransportBit))
1281 // Skip particles that have not been transported
1282 fTmpTreeTR->GetEntry(it--);
1283 Int_t nh = fTmpTrackReferences.GetEntries();
1285 // Loop over reference hits and find primary labels
1286 for (Int_t ih = 0; ih < nh; ih++) {
1287 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1288 Int_t label = tr->Label();
1290 Int_t nref = fTrackReferences.GetEntriesFast();
1291 new(fTrackReferences[nref]) AliTrackReference(*tr);
1296 fTrackReferences.Clear();
1300 if (ifills != stack->GetNtrack())
1301 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1305 fTmpFileTR->Close();
1307 fTmpTrackReferences.Clear();
1308 gSystem->Exec("rm -rf TrackRefsTmp.root");