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"
61 //_______________________________________________________________________
64 fSaveRndmStatus(kFALSE),
65 fSaveRndmEventStatus(kFALSE),
66 fReadRndmStatus(kFALSE),
67 fRndmFileName("random.root"),
89 //_______________________________________________________________________
90 AliMC::AliMC(const char *name, const char *title) :
91 TVirtualMCApplication(name, title),
93 fSaveRndmStatus(kFALSE),
94 fSaveRndmEventStatus(kFALSE),
95 fReadRndmStatus(kFALSE),
96 fRndmFileName("random.root"),
105 fImedia(new TArrayI(1000)),
107 fHitLists(new TList()),
110 fTrackReferences("AliTrackReference", 100),
111 fTmpTrackReferences("AliTrackReference", 100)
114 // Set transport parameters
117 // Prepare the tracking medium lists
118 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
121 //_______________________________________________________________________
128 // Delete track references
131 //_______________________________________________________________________
132 void AliMC::ConstructGeometry()
135 // Either load geometry from file or create it through usual
136 // loop on detectors. In the first case the method
137 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
138 // at InitGeometry().
141 if(AliSimulation::Instance()->IsGeometryFromFile()){ //load geometry either from CDB or from file
142 if(IsGeometryFromCDB()){
143 AliInfo("Loading geometry from CDB default storage");
144 AliCDBPath path("GRP","Geometry","Data");
145 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
146 if(!entry) AliFatal("Unable to load geometry from CDB!");
148 gGeoManager = (TGeoManager*) entry->GetObject();
149 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
152 const char *geomfilename = AliSimulation::Instance()->GetGeometryFile();
153 if(gSystem->ExpandPathName(geomfilename)){
154 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
155 TGeoManager::Import(geomfilename);
157 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
161 gMC->SetRootGeometry();
163 // Create modules, materials, geometry
164 if (!gGeoManager) new TGeoManager("ALICE", "ALICE geometry");
166 TIter next(gAlice->Modules());
168 AliDebug(1, "Geometry creation:");
169 while((detector = dynamic_cast<AliModule*>(next()))) {
171 // Initialise detector materials and geometry
172 detector->CreateMaterials();
173 detector->CreateGeometry();
174 AliInfo(Form("%10s R:%.2fs C:%.2fs",
175 detector->GetName(),stw.RealTime(),stw.CpuTime()));
181 //_______________________________________________________________________
182 Bool_t AliMC::MisalignGeometry()
184 // Call misalignment code if AliSimulation object was defined.
186 if(!AliSimulation::Instance()->IsGeometryFromFile()){
187 //Set alignable volumes for the whole geometry
188 SetAllAlignableVolumes();
190 // Misalign geometry via AliSimulation instance
191 if (!AliSimulation::Instance()) return kFALSE;
192 AliGeomManager::SetGeometry(gGeoManager);
193 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
194 AliFatal("Current loaded geometry differs in the definition of symbolic names!");
196 return AliSimulation::Instance()->MisalignGeometry(AliRunLoader::Instance());
199 //_______________________________________________________________________
200 void AliMC::ConstructOpGeometry()
203 // Loop all detector modules and call DefineOpticalProperties() method
206 TIter next(gAlice->Modules());
208 AliInfo("Optical properties definition");
209 while((detector = dynamic_cast<AliModule*>(next()))) {
210 // Initialise detector geometry
211 if(AliSimulation::Instance()->IsGeometryFromFile()) detector->CreateMaterials();
212 // Initialise detector optical properties
213 detector->DefineOpticalProperties();
217 #include <TPDGCode.h>
218 //_______________________________________________________________________
219 void AliMC::AddParticles()
222 // Add particles (not present in Geant3 or Geant4)
225 // --------------------------------------------------------------------
226 // An example of adding a particle He5 with defined decay mode
227 // (TO BE REMOVED after a useful code is added)
229 cout << "########## AliMC::AddParticles" << endl;
232 gMC->DefineParticle(1010010030, "HyperTriton", kPTHadron, 2.99131 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 3, kFALSE);
234 gMC->DefineParticle(-1010010030, "AntiHyperTriton", kPTHadron, 2.99131 , 1.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 3, kFALSE);
238 gMC->DefineParticle(1010000020, "LambdaNeutron", kPTNeutron, 2.054 , 0.0, 2.632e-10,"Ion", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
240 //Anti-Lambda-Neutron
241 gMC->DefineParticle(-1010000020, "AntiLambdaNeutron", kPTNeutron, 2.054 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
244 gMC->DefineParticle(1020000020, "Hdibaryon", kPTNeutron, 2.21, 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
246 gMC->DefineParticle(-1020000020, "AntiHdibaryon", kPTNeutron, 2.21 , 0.0, 2.632e-10,"Hadron", 0.0, 0, 1, 0, 0, 0, 0, 0, 2, kFALSE);
249 // Define the 2- and 3-body phase space decay for the Hyper-Triton
253 for (Int_t kz = 0; kz < 6; kz++) {
260 mode[0][0] = 1000020030; // Helium3
261 mode[0][1] = -211; // negative pion
263 mode[1][0] = 1000010020; // deuteron
264 mode[1][1] = 2212; // proton
265 mode[1][2] = -211; // negative pion
267 gMC->SetDecayMode(1010010030,bratio,mode);
271 // Define the 2- and 3-body phase space decay for the Anti-Hyper-Triton
275 for (Int_t kz = 0; kz < 6; kz++) {
282 amode[0][0] = -1000020030; // anti- Helium3
283 amode[0][1] = 211; // positive pion
285 amode[1][0] = -1000010020; // anti-deuteron
286 amode[1][1] = -2212; // anti-proton
287 amode[1][2] = 211; // positive pion
289 gMC->SetDecayMode(-1010010030,abratio,amode);
291 // Define the 2-body phase space decay for the Lambda-neutron boundstate
295 for (Int_t kz = 0; kz < 6; kz++) {
302 mode1[0][0] = 1000010020; // deuteron
303 mode1[0][1] = -211; // negative pion
305 gMC->SetDecayMode(1010000020,bratio1,mode1);
308 // Define the 2-body phase space decay for the Anti-Lambda-neutron boundstate
312 for (Int_t kz = 0; kz < 6; kz++) {
319 amode1[0][0] = -1000010020; // anti-deuteron
320 amode1[0][1] = 211; // positive pion
322 gMC->SetDecayMode(-1010000020,abratio1,amode1);
324 // Define the 2-body phase space decay for the H-Dibaryon
328 for (Int_t kz = 0; kz < 6; kz++) {
335 mode2[0][0] = 3122; // Lambda
336 mode2[0][1] = 2212; // proton
337 mode2[0][2] = -211; // negative pion
339 gMC->SetDecayMode(1020000020,bratio2,mode2);
341 // Define the 2-body phase space decay for the Anti-H-Dibaryon
345 for (Int_t kz = 0; kz < 6; kz++) {
352 amode2[0][0] = -3122; // anti-deuteron
353 amode2[0][1] = -2212; // anti-proton
354 amode2[0][2] = 211; // positive pion
356 gMC->SetDecayMode(-1020000020,abratio2,amode2);
358 // end of the example
359 // --------------------------------------------------------------------
362 //_______________________________________________________________________
363 void AliMC::InitGeometry()
366 // Initialize detectors
369 AliInfo("Initialisation:");
371 TIter next(gAlice->Modules());
373 while((detector = dynamic_cast<AliModule*>(next()))) {
376 AliInfo(Form("%10s R:%.2fs C:%.2fs",
377 detector->GetName(),stw.RealTime(),stw.CpuTime()));
381 //_______________________________________________________________________
382 void AliMC::SetGeometryFromCDB()
384 // Set the loading of geometry from cdb instead of creating it
385 // A default CDB storage needs to be set before this method is called
386 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
387 AliCDBManager::Instance()->GetRun() >= 0)
388 AliSimulation::Instance()->SetGeometryFile("*OCDB*");
390 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
393 //_______________________________________________________________________
394 Bool_t AliMC::IsGeometryFromCDB() const
396 return (strcmp(AliSimulation::Instance()->GetGeometryFile(),"*OCDB*")==0);
399 //_______________________________________________________________________
400 void AliMC::SetAllAlignableVolumes()
403 // Add alignable volumes (TGeoPNEntries) looping on all
407 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
409 TIter next(gAlice->Modules());
410 while((detector = dynamic_cast<AliModule*>(next()))) {
411 detector->AddAlignableVolumes();
415 //_______________________________________________________________________
416 void AliMC::GeneratePrimaries()
419 // Generate primary particles and fill them in the stack.
422 Generator()->Generate();
425 //_______________________________________________________________________
426 void AliMC::SetGenerator(AliGenerator *generator)
429 // Load the event generator
431 if(!fGenerator) fGenerator = generator;
434 //_______________________________________________________________________
435 void AliMC::ResetGenerator(AliGenerator *generator)
438 // Load the event generator
442 AliWarning(Form("Replacing generator %s with %s",
443 fGenerator->GetName(),generator->GetName()));
446 AliWarning(Form("Replacing generator %s with NULL",
447 fGenerator->GetName()));
450 fGenerator = generator;
453 //_______________________________________________________________________
454 void AliMC::FinishRun()
456 // Clean generator information
457 AliDebug(1, "fGenerator->FinishRun()");
458 fGenerator->FinishRun();
460 //Output energy summary tables
461 AliDebug(1, "EnergySummary()");
462 ToAliDebug(1, EnergySummary());
465 //_______________________________________________________________________
466 void AliMC::BeginPrimary()
469 // Called at the beginning of each primary track
474 ResetTrackReferences();
477 //_______________________________________________________________________
478 void AliMC::PreTrack()
480 // Actions before the track's transport
482 //verbose.PreTrack();
484 TObjArray &dets = *gAlice->Modules();
487 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
488 if((module = dynamic_cast<AliModule*>(dets[i])))
492 //_______________________________________________________________________
493 void AliMC::Stepping()
496 // Called at every step during transport
498 //verbose.Stepping();
500 Int_t id = DetFromMate(gMC->CurrentMedium());
504 if ( gMC->IsNewTrack() &&
505 gMC->TrackTime() == 0. &&
507 fRDecayMax > fRDecayMin &&
508 gMC->TrackPid() == fDecayPdg )
510 FixParticleDecaytime();
516 // --- If lego option, do it and leave
517 if (AliSimulation::Instance()->Lego())
518 AliSimulation::Instance()->Lego()->StepManager();
521 //Update energy deposition tables
522 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
524 // write tracke reference for track which is dissapearing - MI
526 if (gMC->IsTrackDisappeared() && !(gMC->IsTrackAlive())) {
527 if (gMC->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
528 AliTrackReference::kDisappeared);
533 //Call the appropriate stepping routine;
534 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
535 if(det && det->StepManagerIsEnabled()) {
541 //_______________________________________________________________________
542 void AliMC::EnergySummary()
545 // Print summary of deposited energy
551 Int_t kn, i, left, j, id;
552 const Float_t kzero=0;
553 Int_t ievent=AliRunLoader::Instance()->GetHeader()->GetEvent()+1;
555 // Energy loss information
557 printf("***************** Energy Loss Information per event (GEV) *****************\n");
558 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
561 fEventEnergy[ndep]=kn;
566 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
569 fSummEnergy[ndep]=ed;
570 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
575 for(kn=0;kn<(ndep-1)/3+1;kn++) {
577 for(i=0;i<(3<left?3:left);i++) {
579 id=Int_t (fEventEnergy[j]+0.1);
580 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
585 // Relative energy loss in different detectors
586 printf("******************** Relative Energy Loss per event ********************\n");
587 printf("Total energy loss per event %10.3f GeV\n",edtot);
588 for(kn=0;kn<(ndep-1)/5+1;kn++) {
590 for(i=0;i<(5<left?5:left);i++) {
592 id=Int_t (fEventEnergy[j]+0.1);
593 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
597 for(kn=0;kn<75;kn++) printf("*");
601 // Reset the TArray's
602 // fEventEnergy.Set(0);
603 // fSummEnergy.Set(0);
604 // fSum2Energy.Set(0);
607 //_____________________________________________________________________________
608 void AliMC::BeginEvent()
611 // Clean-up previous event
613 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
614 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
615 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
616 AliDebug(1, " BEGINNING EVENT ");
617 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
618 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
619 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
621 AliRunLoader *runloader=AliRunLoader::Instance();
623 /*******************************/
624 /* Clean after eventual */
626 /*******************************/
629 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
630 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
631 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
632 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
634 fEventEnergy.Reset();
635 // Clean detector information
637 if (runloader->Stack())
638 runloader->Stack()->Reset();//clean stack -> tree is unloaded
640 runloader->MakeStack();//or make a new one
642 // Random engine status
645 if ( fSaveRndmStatus || fSaveRndmEventStatus) {
646 TString fileName="random";
647 if ( fSaveRndmEventStatus ) {
649 fileName += gAlice->GetEventNrInRun();
653 // write ROOT random engine status
654 cout << "Saving random engine status in " << fileName.Data() << endl;
655 TFile f(fileName.Data(),"RECREATE");
656 gRandom->Write(fileName.Data());
659 if ( fReadRndmStatus ) {
660 //read ROOT random engine status
661 cout << "Reading random engine status from " << fRndmFileName.Data() << endl;
662 TFile f(fRndmFileName.Data());
663 gRandom->Read(fRndmFileName.Data());
666 if(AliSimulation::Instance()->Lego() == 0x0)
668 AliDebug(1, "fRunLoader->MakeTree(K)");
669 runloader->MakeTree("K");
672 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
673 gMC->SetStack(runloader->Stack());//Was in InitMC - but was moved here
674 //because we don't have guarantee that
675 //stack pointer is not going to change from event to event
676 //since it bellobgs to header and is obtained via RunLoader
678 // Reset all Detectors & kinematics & make/reset trees
681 runloader->GetHeader()->Reset(AliCDBManager::Instance()->GetRun(),gAlice->GetEvNumber(),
682 gAlice->GetEventNrInRun());
683 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
685 if(AliSimulation::Instance()->Lego())
687 AliSimulation::Instance()->Lego()->BeginEvent();
692 AliDebug(1, "ResetHits()");
695 AliDebug(1, "fRunLoader->MakeTree(H)");
696 runloader->MakeTree("H");
700 MakeTmpTrackRefsTree();
701 //create new branches and SetAdresses
702 TIter next(gAlice->Modules());
704 while((detector = (AliModule*)next()))
706 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
707 detector->MakeBranch("H");
711 //_______________________________________________________________________
712 void AliMC::ResetHits()
715 // Reset all Detectors hits
717 TIter next(gAlice->Modules());
719 while((detector = dynamic_cast<AliModule*>(next()))) {
720 detector->ResetHits();
724 //_______________________________________________________________________
725 void AliMC::ResetDigits()
728 // Reset all Detectors digits
730 TIter next(gAlice->Modules());
732 while((detector = dynamic_cast<AliModule*>(next()))) {
733 detector->ResetDigits();
737 //_______________________________________________________________________
738 void AliMC::ResetSDigits()
741 // Reset all Detectors digits
743 TIter next(gAlice->Modules());
745 while((detector = dynamic_cast<AliModule*>(next()))) {
746 detector->ResetSDigits();
750 //_______________________________________________________________________
751 void AliMC::PostTrack()
753 // Posts tracks for each module
755 TObjArray &dets = *gAlice->Modules();
758 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
759 if((module = dynamic_cast<AliModule*>(dets[i])))
763 //_______________________________________________________________________
764 void AliMC::FinishPrimary()
767 // Called at the end of each primary track
770 AliRunLoader *runloader=AliRunLoader::Instance();
771 // static Int_t count=0;
772 // const Int_t times=10;
773 // This primary is finished, purify stack
774 #if ROOT_VERSION_CODE > 262152
775 if (!(gMC->SecondariesAreOrdered())) {
776 if (runloader->Stack()->ReorderKine()) RemapHits();
779 if (runloader->Stack()->PurifyKine()) RemapHits();
781 TIter next(gAlice->Modules());
783 while((detector = dynamic_cast<AliModule*>(next()))) {
784 detector->FinishPrimary();
785 AliLoader* loader = detector->GetLoader();
788 TTree* treeH = loader->TreeH();
789 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
793 // Write out track references if any
794 if (fTmpTreeTR) fTmpTreeTR->Fill();
797 void AliMC::RemapHits()
800 // Remaps the track labels of the hits
801 AliRunLoader *runloader=AliRunLoader::Instance();
802 AliStack* stack = runloader->Stack();
803 TList* hitLists = GetHitLists();
804 TIter next(hitLists);
805 TCollection *hitList;
807 while((hitList = dynamic_cast<TCollection*>(next()))) {
808 TIter nexthit(hitList);
810 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
811 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
816 // This for detectors which have a special mapping mechanism
817 // for hits, such as TPC and TRD
821 TObjArray* modules = gAlice->Modules();
822 TIter nextmod(modules);
824 while((module = (AliModule*) nextmod())) {
825 AliDetector* det = dynamic_cast<AliDetector*> (module);
826 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
829 RemapTrackReferencesIDs(stack->TrackLabelMap());
832 //_______________________________________________________________________
833 void AliMC::FinishEvent()
836 // Called at the end of the event.
839 if(AliSimulation::Instance()->Lego()) AliSimulation::Instance()->Lego()->FinishEvent();
841 TIter next(gAlice->Modules());
843 while((detector = dynamic_cast<AliModule*>(next()))) {
844 detector->FinishEvent();
847 //Update the energy deposit tables
849 for(i=0;i<fEventEnergy.GetSize();i++)
851 fSummEnergy[i]+=fEventEnergy[i];
852 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
855 AliRunLoader *runloader=AliRunLoader::Instance();
857 AliHeader* header = runloader->GetHeader();
858 AliStack* stack = runloader->Stack();
859 if ( (header == 0x0) || (stack == 0x0) )
860 {//check if we got header and stack. If not cry and exit aliroot
861 AliFatal("Can not get the stack or header from LOADER");
862 return;//never reached
864 // Update Header information
865 header->SetNprimary(stack->GetNprimary());
866 header->SetNtrack(stack->GetNtrack());
867 header->SetTimeStamp(AliSimulation::Instance()->GenerateTimeStamp());
869 // Write out the kinematics
870 if (!AliSimulation::Instance()->Lego()) stack->FinishEvent();
872 // Synchronize the TreeTR with TreeK
873 if (fTmpTreeTR) ReorderAndExpandTreeTR();
875 // Write out the event Header information
876 TTree* treeE = runloader->TreeE();
879 header->SetStack(stack);
884 AliError("Can not get TreeE from RL");
887 if(AliSimulation::Instance()->Lego() == 0x0)
889 runloader->WriteKinematics("OVERWRITE");
890 runloader->WriteTrackRefs("OVERWRITE");
891 runloader->WriteHits("OVERWRITE");
894 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
895 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
896 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
897 AliDebug(1, " FINISHING EVENT ");
898 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
899 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
900 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
903 //_______________________________________________________________________
909 //=================Create Materials and geometry
911 // Set alignable volumes for the whole geometry (with old root)
912 #if ROOT_VERSION_CODE < 331527
913 SetAllAlignableVolumes();
915 //Read the cuts for all materials
917 //Build the special IMEDIA table
920 //Compute cross-sections
923 //Initialise geometry deposition table
924 fEventEnergy.Set(gMC->NofVolumes()+1);
925 fSummEnergy.Set(gMC->NofVolumes()+1);
926 fSum2Energy.Set(gMC->NofVolumes()+1);
928 // Register MC in configuration
929 AliConfig::Instance()->Add(gMC);
932 //_______________________________________________________________________
933 void AliMC::MediaTable()
936 // Built media table to get from the media number to
940 Int_t kz, nz, idt, lz, i, k, ind;
942 TObjArray &dets = *gAlice->Detectors();
944 Int_t ndets=gAlice->GetNdets();
947 for (kz=0;kz<ndets;kz++) {
948 // If detector is defined
949 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
950 TArrayI &idtmed = *(det->GetIdtmed());
951 for(nz=0;nz<100;nz++) {
953 // Find max and min material number
954 if((idt=idtmed[nz])) {
955 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
956 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
959 if(det->LoMedium() > det->HiMedium()) {
963 if(det->HiMedium() > fImedia->GetSize()) {
964 AliError(Form("Increase fImedia from %d to %d",
965 fImedia->GetSize(),det->HiMedium()));
968 // Tag all materials in rage as belonging to detector kz
969 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
976 // Print summary table
977 AliInfo("Tracking media ranges:");
979 for(i=0;i<(ndets-1)/6+1;i++) {
980 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
982 det=dynamic_cast<AliModule*>(dets[ind]);
984 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
987 printf(" %6s: %3d -> %3d;","NULL",0,0);
994 //_______________________________________________________________________
995 void AliMC::ReadTransPar()
998 // Read filename to set the transport parameters
1002 const Int_t kncuts=10;
1003 const Int_t knflags=12;
1004 const Int_t knpars=kncuts+knflags;
1005 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
1006 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
1007 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
1008 "MULS","PAIR","PHOT","RAYL","STRA"};
1012 Float_t cut[kncuts];
1013 Int_t flag[knflags];
1014 Int_t i, itmed, iret, jret, ktmed, kz;
1017 // See whether the file is there
1018 filtmp=gSystem->ExpandPathName(fTransParName.Data());
1019 lun=fopen(filtmp,"r");
1022 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
1027 // Initialise cuts and flags
1028 for(i=0;i<kncuts;i++) cut[i]=-99;
1029 for(i=0;i<knflags;i++) flag[i]=-99;
1032 // Read up to the end of line excluded
1033 iret=fscanf(lun,"%255[^\n]",line);
1039 // Read the end of line
1040 jret = fscanf(lun,"%*c");
1042 if(line[0]=='*') continue;
1044 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",
1045 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1046 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1047 &flag[8],&flag[9],&flag[10],&flag[11]);
1051 AliWarning(Form("Error reading file %s",fTransParName.Data()));
1054 // Check that the module exist
1055 AliModule *mod = gAlice->GetModule(detName);
1057 // Get the array of media numbers
1058 TArrayI &idtmed = *mod->GetIdtmed();
1059 // Check that the tracking medium code is valid
1060 if(0<=itmed && itmed < 100) {
1061 ktmed=idtmed[itmed];
1063 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
1066 // Set energy thresholds
1067 for(kz=0;kz<kncuts;kz++) {
1069 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
1070 kpars[kz],cut[kz],itmed,mod->GetName()));
1071 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
1074 // Set transport mechanisms
1075 for(kz=0;kz<knflags;kz++) {
1077 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
1078 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
1079 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
1083 AliWarning(Form("Invalid medium code %d",itmed));
1087 AliDebug(1, Form("%s not present",detName));
1093 //_______________________________________________________________________
1094 void AliMC::SetTransPar(const char *filename)
1097 // Sets the file name for transport parameters
1099 fTransParName = filename;
1102 //_______________________________________________________________________
1103 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
1106 // Add a hit to detector id
1108 TObjArray &dets = *gAlice->Modules();
1109 if(dets[id]) static_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
1112 //_______________________________________________________________________
1113 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
1116 // Add digit to detector id
1118 TObjArray &dets = *gAlice->Modules();
1119 if(dets[id]) static_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
1122 //_______________________________________________________________________
1123 Int_t AliMC::GetCurrentTrackNumber() const {
1125 // Returns current track
1127 return AliRunLoader::Instance()->Stack()->GetCurrentTrackNumber();
1130 //_______________________________________________________________________
1131 void AliMC::DumpPart (Int_t i) const
1134 // Dumps particle i in the stack
1136 AliRunLoader * runloader = AliRunLoader::Instance();
1137 if (runloader->Stack())
1138 runloader->Stack()->DumpPart(i);
1141 //_______________________________________________________________________
1142 void AliMC::DumpPStack () const
1145 // Dumps the particle stack
1147 AliRunLoader * runloader = AliRunLoader::Instance();
1148 if (runloader->Stack())
1149 runloader->Stack()->DumpPStack();
1152 //_______________________________________________________________________
1153 Int_t AliMC::GetNtrack() const {
1155 // Returns number of tracks in stack
1158 AliRunLoader * runloader = AliRunLoader::Instance();
1159 if (runloader->Stack())
1160 ntracks = runloader->Stack()->GetNtrack();
1164 //_______________________________________________________________________
1165 Int_t AliMC::GetPrimary(Int_t track) const
1168 // return number of primary that has generated track
1170 Int_t nprimary = -999;
1171 AliRunLoader * runloader = AliRunLoader::Instance();
1172 if (runloader->Stack())
1173 nprimary = runloader->Stack()->GetPrimary(track);
1177 //_______________________________________________________________________
1178 TParticle* AliMC::Particle(Int_t i) const
1180 // Returns the i-th particle from the stack taking into account
1181 // the remaping done by PurifyKine
1182 AliRunLoader * runloader = AliRunLoader::Instance();
1184 if (runloader->Stack())
1185 return runloader->Stack()->Particle(i);
1189 //_______________________________________________________________________
1190 const TObjArray* AliMC::Particles() const {
1192 // Returns pointer to Particles array
1194 AliRunLoader * runloader = AliRunLoader::Instance();
1196 if (runloader->Stack())
1197 return runloader->Stack()->Particles();
1201 //_______________________________________________________________________
1202 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, const Float_t *pmom,
1203 const Float_t *vpos, const Float_t *polar, Float_t tof,
1204 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1206 // Delegate to stack
1208 AliRunLoader * runloader = AliRunLoader::Instance();
1210 if (runloader->Stack())
1211 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1212 mech, ntr, weight, is);
1215 //_______________________________________________________________________
1216 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1217 Double_t px, Double_t py, Double_t pz, Double_t e,
1218 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1219 Double_t polx, Double_t poly, Double_t polz,
1220 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1222 // Delegate to stack
1224 AliRunLoader * runloader = AliRunLoader::Instance();
1226 if (runloader->Stack())
1227 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1228 polx, poly, polz, mech, ntr, weight, is);
1231 //_______________________________________________________________________
1232 void AliMC::SetHighWaterMark(Int_t nt) const
1235 // Set high water mark for last track in event
1236 AliRunLoader * runloader = AliRunLoader::Instance();
1238 if (runloader->Stack())
1239 runloader->Stack()->SetHighWaterMark(nt);
1242 //_______________________________________________________________________
1243 void AliMC::KeepTrack(Int_t track) const
1246 // Delegate to stack
1248 AliRunLoader * runloader = AliRunLoader::Instance();
1250 if (runloader->Stack())
1251 runloader->Stack()->KeepTrack(track);
1254 //_______________________________________________________________________
1255 void AliMC::FlagTrack(Int_t track) const
1257 // Delegate to stack
1259 AliRunLoader * runloader = AliRunLoader::Instance();
1261 if (runloader->Stack())
1262 runloader->Stack()->FlagTrack(track);
1265 //_______________________________________________________________________
1266 void AliMC::SetCurrentTrack(Int_t track) const
1269 // Set current track number
1271 AliRunLoader * runloader = AliRunLoader::Instance();
1273 if (runloader->Stack())
1274 runloader->Stack()->SetCurrentTrack(track);
1277 //_______________________________________________________________________
1278 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1281 // add a trackrefernce to the list
1282 Int_t primary = GetPrimary(label);
1283 Particle(primary)->SetBit(kKeepBit);
1285 Int_t nref = fTmpTrackReferences.GetEntriesFast();
1286 return new(fTmpTrackReferences[nref]) AliTrackReference(label, id);
1291 //_______________________________________________________________________
1292 void AliMC::ResetTrackReferences()
1295 // Reset all references
1297 fTmpTrackReferences.Clear();
1300 //_______________________________________________________________________
1301 void AliMC::RemapTrackReferencesIDs(const Int_t *map)
1304 // Remapping track reference
1305 // Called at finish primary
1308 Int_t nEntries = fTmpTrackReferences.GetEntries();
1309 for (Int_t i=0; i < nEntries; i++){
1310 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences.UncheckedAt(i));
1312 Int_t newID = map[ref->GetTrack()];
1313 if (newID>=0) ref->SetTrack(newID);
1315 ref->SetBit(kNotDeleted,kFALSE);
1316 fTmpTrackReferences.RemoveAt(i);
1320 fTmpTrackReferences.Compress();
1323 //_______________________________________________________________________
1324 void AliMC::FixParticleDecaytime()
1327 // Fix the particle decay time according to rmin and rmax for decays
1331 gMC->TrackMomentum(p);
1332 Double_t tmin, tmax;
1335 // Transverse velocity
1336 Double_t vt = p.Pt() / p.E();
1338 if ((b = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->SolenoidField()) > 0.) { // [kG]
1342 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1344 // Revolution frequency
1346 Double_t omega = vt / rho;
1348 // Maximum and minimum decay time
1350 // Check for curlers first
1351 const Double_t kOvRhoSqr2 = 1./(rho*TMath::Sqrt(2.));
1352 if (fRDecayMax * kOvRhoSqr2 > 1.) return;
1356 tmax = TMath::ACos((1.-fRDecayMax*kOvRhoSqr2)*(1.+fRDecayMax*kOvRhoSqr2)) / omega; // [ct]
1357 tmin = TMath::ACos((1.-fRDecayMin*kOvRhoSqr2)*(1.+fRDecayMin*kOvRhoSqr2)) / omega; // [ct]
1359 tmax = fRDecayMax / vt; // [ct]
1360 tmin = fRDecayMin / vt; // [ct]
1364 // Dial t using the two limits
1365 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1368 // Force decay time in transport code
1370 gMC->ForceDecayTime(t / 2.99792458e10);
1373 void AliMC::MakeTmpTrackRefsTree()
1375 // Make the temporary track reference tree
1376 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1377 fTmpTreeTR = new TTree("TreeTR", "Track References");
1378 TClonesArray* pRef = &fTmpTrackReferences;
1379 fTmpTreeTR->Branch("TrackReferences", &pRef, 4000);
1382 //_______________________________________________________________________
1383 void AliMC::ReorderAndExpandTreeTR()
1386 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1389 AliRunLoader *rl = AliRunLoader::Instance();
1392 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1393 rl->MakeTrackRefsContainer();
1394 TTree * treeTR = rl->TreeTR();
1395 // make branch for central track references
1396 TClonesArray* pRef = &fTrackReferences;
1397 treeTR->Branch("TrackReferences", &pRef);
1399 AliStack* stack = rl->Stack();
1400 Int_t np = stack->GetNprimary();
1401 Int_t nt = fTmpTreeTR->GetEntries();
1403 // Loop over tracks and find the secondaries with the help of the kine tree
1406 for (Int_t ip = np - 1; ip > -1; ip--) {
1407 TParticle *part = stack->Particle(ip);
1408 //printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1410 // Skip primaries that have not been transported
1411 Int_t dau1 = part->GetFirstDaughter();
1413 if (!part->TestBit(kTransportBit)) continue;
1415 fTmpTreeTR->GetEntry(it++);
1416 Int_t nh = fTmpTrackReferences.GetEntries();
1417 // Determine range of secondaries produced by this primary
1419 Int_t inext = ip - 1;
1422 part = stack->Particle(inext);
1423 dau2 = part->GetFirstDaughter();
1424 if (!(part->TestBit(kTransportBit)) || dau2 == -1 || dau2 < np) {
1425 // if (dau2 == -1 || dau2 < np) {
1431 dau2 = stack->GetNtrack() - 1;
1434 } // find upper bound
1436 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1438 // Loop over reference hits and find secondary label
1439 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1440 for (Int_t ih = 0; ih < nh; ih++) {
1441 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1442 Int_t label = tr->Label();
1444 if (label == ip) continue;
1445 if (label > dau2 || label < dau1)
1446 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1449 Int_t nref = fTrackReferences.GetEntriesFast();
1450 new(fTrackReferences[nref]) AliTrackReference(*tr);
1454 fTrackReferences.Clear();
1459 // Now loop again and write the primaries
1461 for (Int_t ip = 0; ip < np; ip++) {
1462 TParticle* part = stack->Particle(ip);
1463 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1464 if (part->TestBit(kTransportBit))
1466 // Skip particles that have not been transported
1467 fTmpTreeTR->GetEntry(it--);
1468 Int_t nh = fTmpTrackReferences.GetEntries();
1470 // Loop over reference hits and find primary labels
1471 for (Int_t ih = 0; ih < nh; ih++) {
1472 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1473 Int_t label = tr->Label();
1475 Int_t nref = fTrackReferences.GetEntriesFast();
1476 new(fTrackReferences[nref]) AliTrackReference(*tr);
1481 fTrackReferences.Clear();
1485 if (ifills != stack->GetNtrack())
1486 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1490 fTmpFileTR->Close();
1492 fTmpTrackReferences.Clear();
1493 gSystem->Exec("rm -rf TrackRefsTmp.root");