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(AliSimulation::Instance()->IsGeometryFromFile()){ //load geometry either from CDB or from file
128 if(IsGeometryFromCDB()){
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 = AliSimulation::Instance()->GetGeometryFile();
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));
147 gMC->SetRootGeometry();
149 // Create modules, materials, geometry
150 if (!gGeoManager) new TGeoManager("ALICE", "ALICE geometry");
152 TIter next(gAlice->Modules());
154 AliDebug(1, "Geometry creation:");
155 while((detector = dynamic_cast<AliModule*>(next()))) {
157 // Initialise detector materials and geometry
158 detector->CreateMaterials();
159 detector->CreateGeometry();
160 AliInfo(Form("%10s R:%.2fs C:%.2fs",
161 detector->GetName(),stw.RealTime(),stw.CpuTime()));
167 //_______________________________________________________________________
168 Bool_t AliMC::MisalignGeometry()
170 // Call misalignment code if AliSimulation object was defined.
172 if(!AliSimulation::Instance()->IsGeometryFromFile()){
173 //Set alignable volumes for the whole geometry
174 SetAllAlignableVolumes();
176 // Misalign geometry via AliSimulation instance
177 if (!AliSimulation::Instance()) return kFALSE;
178 AliGeomManager::SetGeometry(gGeoManager);
179 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
180 AliFatal("Current loaded geometry differs in the definition of symbolic names!");
182 return AliSimulation::Instance()->MisalignGeometry(AliRunLoader::Instance());
185 //_______________________________________________________________________
186 void AliMC::ConstructOpGeometry()
189 // Loop all detector modules and call DefineOpticalProperties() method
192 TIter next(gAlice->Modules());
194 AliInfo("Optical properties definition");
195 while((detector = dynamic_cast<AliModule*>(next()))) {
196 // Initialise detector geometry
197 if(AliSimulation::Instance()->IsGeometryFromFile()) detector->CreateMaterials();
198 // Initialise detector optical properties
199 detector->DefineOpticalProperties();
203 //_______________________________________________________________________
204 void AliMC::InitGeometry()
207 // Initialize detectors
210 AliInfo("Initialisation:");
212 TIter next(gAlice->Modules());
214 while((detector = dynamic_cast<AliModule*>(next()))) {
217 AliInfo(Form("%10s R:%.2fs C:%.2fs",
218 detector->GetName(),stw.RealTime(),stw.CpuTime()));
222 //_______________________________________________________________________
223 void AliMC::SetGeometryFromCDB()
225 // Set the loading of geometry from cdb instead of creating it
226 // A default CDB storage needs to be set before this method is called
227 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
228 AliCDBManager::Instance()->GetRun() >= 0)
229 AliSimulation::Instance()->SetGeometryFile("*OCDB*");
231 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
234 //_______________________________________________________________________
235 Bool_t AliMC::IsGeometryFromCDB() const
237 return (strcmp(AliSimulation::Instance()->GetGeometryFile(),"*OCDB*")==0);
240 //_______________________________________________________________________
241 void AliMC::SetAllAlignableVolumes()
244 // Add alignable volumes (TGeoPNEntries) looping on all
248 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
250 TIter next(gAlice->Modules());
251 while((detector = dynamic_cast<AliModule*>(next()))) {
252 detector->AddAlignableVolumes();
256 //_______________________________________________________________________
257 void AliMC::GeneratePrimaries()
260 // Generate primary particles and fill them in the stack.
263 Generator()->Generate();
266 //_______________________________________________________________________
267 void AliMC::SetGenerator(AliGenerator *generator)
270 // Load the event generator
272 if(!fGenerator) fGenerator = generator;
275 //_______________________________________________________________________
276 void AliMC::ResetGenerator(AliGenerator *generator)
279 // Load the event generator
283 AliWarning(Form("Replacing generator %s with %s",
284 fGenerator->GetName(),generator->GetName()));
287 AliWarning(Form("Replacing generator %s with NULL",
288 fGenerator->GetName()));
291 fGenerator = generator;
294 //_______________________________________________________________________
295 void AliMC::FinishRun()
297 // Clean generator information
298 AliDebug(1, "fGenerator->FinishRun()");
299 fGenerator->FinishRun();
301 //Output energy summary tables
302 AliDebug(1, "EnergySummary()");
303 ToAliDebug(1, EnergySummary());
306 //_______________________________________________________________________
307 void AliMC::BeginPrimary()
310 // Called at the beginning of each primary track
315 ResetTrackReferences();
318 //_______________________________________________________________________
319 void AliMC::PreTrack()
321 // Actions before the track's transport
323 TObjArray &dets = *gAlice->Modules();
326 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
327 if((module = dynamic_cast<AliModule*>(dets[i])))
331 //_______________________________________________________________________
332 void AliMC::Stepping()
335 // Called at every step during transport
337 Int_t id = DetFromMate(gMC->CurrentMedium());
341 if ( gMC->IsNewTrack() &&
342 gMC->TrackTime() == 0. &&
344 fRDecayMax > fRDecayMin &&
345 gMC->TrackPid() == fDecayPdg )
347 FixParticleDecaytime();
353 // --- If lego option, do it and leave
354 if (AliSimulation::Instance()->Lego())
355 AliSimulation::Instance()->Lego()->StepManager();
358 //Update energy deposition tables
359 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
361 // write tracke reference for track which is dissapearing - MI
363 if (gMC->IsTrackDisappeared() && !(gMC->IsTrackAlive())) {
364 if (gMC->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
365 AliTrackReference::kDisappeared);
370 //Call the appropriate stepping routine;
371 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
372 if(det && det->StepManagerIsEnabled()) {
378 //_______________________________________________________________________
379 void AliMC::EnergySummary()
382 // Print summary of deposited energy
388 Int_t kn, i, left, j, id;
389 const Float_t kzero=0;
390 Int_t ievent=AliRunLoader::Instance()->GetHeader()->GetEvent()+1;
392 // Energy loss information
394 printf("***************** Energy Loss Information per event (GEV) *****************\n");
395 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
398 fEventEnergy[ndep]=kn;
403 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
406 fSummEnergy[ndep]=ed;
407 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
412 for(kn=0;kn<(ndep-1)/3+1;kn++) {
414 for(i=0;i<(3<left?3:left);i++) {
416 id=Int_t (fEventEnergy[j]+0.1);
417 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
422 // Relative energy loss in different detectors
423 printf("******************** Relative Energy Loss per event ********************\n");
424 printf("Total energy loss per event %10.3f GeV\n",edtot);
425 for(kn=0;kn<(ndep-1)/5+1;kn++) {
427 for(i=0;i<(5<left?5:left);i++) {
429 id=Int_t (fEventEnergy[j]+0.1);
430 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
434 for(kn=0;kn<75;kn++) printf("*");
438 // Reset the TArray's
439 // fEventEnergy.Set(0);
440 // fSummEnergy.Set(0);
441 // fSum2Energy.Set(0);
444 //_____________________________________________________________________________
445 void AliMC::BeginEvent()
448 // Clean-up previous event
450 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
451 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
452 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
453 AliDebug(1, " BEGINNING EVENT ");
454 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
455 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
456 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
458 AliRunLoader *runloader=AliRunLoader::Instance();
460 /*******************************/
461 /* Clean after eventual */
463 /*******************************/
466 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
467 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
468 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
469 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
471 fEventEnergy.Reset();
472 // Clean detector information
474 if (runloader->Stack())
475 runloader->Stack()->Reset();//clean stack -> tree is unloaded
477 runloader->MakeStack();//or make a new one
480 if(AliSimulation::Instance()->Lego() == 0x0)
482 AliDebug(1, "fRunLoader->MakeTree(K)");
483 runloader->MakeTree("K");
486 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
487 gMC->SetStack(runloader->Stack());//Was in InitMC - but was moved here
488 //because we don't have guarantee that
489 //stack pointer is not going to change from event to event
490 //since it bellobgs to header and is obtained via RunLoader
492 // Reset all Detectors & kinematics & make/reset trees
495 runloader->GetHeader()->Reset(AliCDBManager::Instance()->GetRun(),gAlice->GetEvNumber(),
496 gAlice->GetEventNrInRun());
497 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
499 if(AliSimulation::Instance()->Lego())
501 AliSimulation::Instance()->Lego()->BeginEvent();
506 AliDebug(1, "ResetHits()");
509 AliDebug(1, "fRunLoader->MakeTree(H)");
510 runloader->MakeTree("H");
514 MakeTmpTrackRefsTree();
515 //create new branches and SetAdresses
516 TIter next(gAlice->Modules());
518 while((detector = (AliModule*)next()))
520 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
521 detector->MakeBranch("H");
525 //_______________________________________________________________________
526 void AliMC::ResetHits()
529 // Reset all Detectors hits
531 TIter next(gAlice->Modules());
533 while((detector = dynamic_cast<AliModule*>(next()))) {
534 detector->ResetHits();
538 //_______________________________________________________________________
539 void AliMC::ResetDigits()
542 // Reset all Detectors digits
544 TIter next(gAlice->Modules());
546 while((detector = dynamic_cast<AliModule*>(next()))) {
547 detector->ResetDigits();
551 //_______________________________________________________________________
552 void AliMC::ResetSDigits()
555 // Reset all Detectors digits
557 TIter next(gAlice->Modules());
559 while((detector = dynamic_cast<AliModule*>(next()))) {
560 detector->ResetSDigits();
564 //_______________________________________________________________________
565 void AliMC::PostTrack()
567 // Posts tracks for each module
569 TObjArray &dets = *gAlice->Modules();
572 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
573 if((module = dynamic_cast<AliModule*>(dets[i])))
577 //_______________________________________________________________________
578 void AliMC::FinishPrimary()
581 // Called at the end of each primary track
584 AliRunLoader *runloader=AliRunLoader::Instance();
585 // static Int_t count=0;
586 // const Int_t times=10;
587 // This primary is finished, purify stack
588 #if ROOT_VERSION_CODE > 262152
589 if (!(gMC->SecondariesAreOrdered())) {
590 if (runloader->Stack()->ReorderKine()) RemapHits();
593 if (runloader->Stack()->PurifyKine()) RemapHits();
595 TIter next(gAlice->Modules());
597 while((detector = dynamic_cast<AliModule*>(next()))) {
598 detector->FinishPrimary();
599 AliLoader* loader = detector->GetLoader();
602 TTree* treeH = loader->TreeH();
603 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
607 // Write out track references if any
608 if (fTmpTreeTR) fTmpTreeTR->Fill();
611 void AliMC::RemapHits()
614 // Remaps the track labels of the hits
615 AliRunLoader *runloader=AliRunLoader::Instance();
616 AliStack* stack = runloader->Stack();
617 TList* hitLists = GetHitLists();
618 TIter next(hitLists);
619 TCollection *hitList;
621 while((hitList = dynamic_cast<TCollection*>(next()))) {
622 TIter nexthit(hitList);
624 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
625 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
630 // This for detectors which have a special mapping mechanism
631 // for hits, such as TPC and TRD
635 TObjArray* modules = gAlice->Modules();
636 TIter nextmod(modules);
638 while((module = (AliModule*) nextmod())) {
639 AliDetector* det = dynamic_cast<AliDetector*> (module);
640 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
643 RemapTrackReferencesIDs(stack->TrackLabelMap());
646 //_______________________________________________________________________
647 void AliMC::FinishEvent()
650 // Called at the end of the event.
653 if(AliSimulation::Instance()->Lego()) AliSimulation::Instance()->Lego()->FinishEvent();
655 TIter next(gAlice->Modules());
657 while((detector = dynamic_cast<AliModule*>(next()))) {
658 detector->FinishEvent();
661 //Update the energy deposit tables
663 for(i=0;i<fEventEnergy.GetSize();i++)
665 fSummEnergy[i]+=fEventEnergy[i];
666 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
669 AliRunLoader *runloader=AliRunLoader::Instance();
671 AliHeader* header = runloader->GetHeader();
672 AliStack* stack = runloader->Stack();
673 if ( (header == 0x0) || (stack == 0x0) )
674 {//check if we got header and stack. If not cry and exit aliroot
675 AliFatal("Can not get the stack or header from LOADER");
676 return;//never reached
678 // Update Header information
679 header->SetNprimary(stack->GetNprimary());
680 header->SetNtrack(stack->GetNtrack());
682 // Write out the kinematics
683 if (!AliSimulation::Instance()->Lego()) stack->FinishEvent();
685 // Synchronize the TreeTR with TreeK
686 if (fTmpTreeTR) ReorderAndExpandTreeTR();
688 // Write out the event Header information
689 TTree* treeE = runloader->TreeE();
692 header->SetStack(stack);
697 AliError("Can not get TreeE from RL");
700 if(AliSimulation::Instance()->Lego() == 0x0)
702 runloader->WriteKinematics("OVERWRITE");
703 runloader->WriteTrackRefs("OVERWRITE");
704 runloader->WriteHits("OVERWRITE");
707 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
708 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
709 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
710 AliDebug(1, " FINISHING EVENT ");
711 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
712 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
713 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
716 //_______________________________________________________________________
721 //=================Create Materials and geometry
723 // Set alignable volumes for the whole geometry (with old root)
724 #if ROOT_VERSION_CODE < 331527
725 SetAllAlignableVolumes();
727 //Read the cuts for all materials
729 //Build the special IMEDIA table
732 //Compute cross-sections
735 //Initialise geometry deposition table
736 fEventEnergy.Set(gMC->NofVolumes()+1);
737 fSummEnergy.Set(gMC->NofVolumes()+1);
738 fSum2Energy.Set(gMC->NofVolumes()+1);
740 // Register MC in configuration
741 AliConfig::Instance()->Add(gMC);
745 //_______________________________________________________________________
746 void AliMC::MediaTable()
749 // Built media table to get from the media number to
753 Int_t kz, nz, idt, lz, i, k, ind;
755 TObjArray &dets = *gAlice->Detectors();
757 Int_t ndets=gAlice->GetNdets();
760 for (kz=0;kz<ndets;kz++) {
761 // If detector is defined
762 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
763 TArrayI &idtmed = *(det->GetIdtmed());
764 for(nz=0;nz<100;nz++) {
766 // Find max and min material number
767 if((idt=idtmed[nz])) {
768 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
769 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
772 if(det->LoMedium() > det->HiMedium()) {
776 if(det->HiMedium() > fImedia->GetSize()) {
777 AliError(Form("Increase fImedia from %d to %d",
778 fImedia->GetSize(),det->HiMedium()));
781 // Tag all materials in rage as belonging to detector kz
782 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
789 // Print summary table
790 AliInfo("Tracking media ranges:");
792 for(i=0;i<(ndets-1)/6+1;i++) {
793 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
795 det=dynamic_cast<AliModule*>(dets[ind]);
797 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
800 printf(" %6s: %3d -> %3d;","NULL",0,0);
807 //_______________________________________________________________________
808 void AliMC::ReadTransPar()
811 // Read filename to set the transport parameters
815 const Int_t kncuts=10;
816 const Int_t knflags=12;
817 const Int_t knpars=kncuts+knflags;
818 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
819 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
820 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
821 "MULS","PAIR","PHOT","RAYL","STRA"};
827 Int_t i, itmed, iret, ktmed, kz;
830 // See whether the file is there
831 filtmp=gSystem->ExpandPathName(fTransParName.Data());
832 lun=fopen(filtmp,"r");
835 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
840 // Initialise cuts and flags
841 for(i=0;i<kncuts;i++) cut[i]=-99;
842 for(i=0;i<knflags;i++) flag[i]=-99;
844 for(i=0;i<256;i++) line[i]='\0';
845 // Read up to the end of line excluded
846 iret=fscanf(lun,"%[^\n]",line);
852 // Read the end of line
855 if(line[0]=='*') continue;
857 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",
858 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
859 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
860 &flag[8],&flag[9],&flag[10],&flag[11]);
864 AliWarning(Form("Error reading file %s",fTransParName.Data()));
867 // Check that the module exist
868 AliModule *mod = gAlice->GetModule(detName);
870 // Get the array of media numbers
871 TArrayI &idtmed = *mod->GetIdtmed();
872 // Check that the tracking medium code is valid
873 if(0<=itmed && itmed < 100) {
876 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
879 // Set energy thresholds
880 for(kz=0;kz<kncuts;kz++) {
882 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
883 kpars[kz],cut[kz],itmed,mod->GetName()));
884 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
887 // Set transport mechanisms
888 for(kz=0;kz<knflags;kz++) {
890 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
891 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
892 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
896 AliWarning(Form("Invalid medium code %d",itmed));
900 AliDebug(1, Form("%s not present",detName));
906 //_______________________________________________________________________
907 void AliMC::SetTransPar(const char *filename)
910 // Sets the file name for transport parameters
912 fTransParName = filename;
915 //_______________________________________________________________________
916 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
919 // Add a hit to detector id
921 TObjArray &dets = *gAlice->Modules();
922 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
925 //_______________________________________________________________________
926 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
929 // Add digit to detector id
931 TObjArray &dets = *gAlice->Modules();
932 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
935 //_______________________________________________________________________
936 Int_t AliMC::GetCurrentTrackNumber() const {
938 // Returns current track
940 return AliRunLoader::Instance()->Stack()->GetCurrentTrackNumber();
943 //_______________________________________________________________________
944 void AliMC::DumpPart (Int_t i) const
947 // Dumps particle i in the stack
949 AliRunLoader * runloader = AliRunLoader::Instance();
950 if (runloader->Stack())
951 runloader->Stack()->DumpPart(i);
954 //_______________________________________________________________________
955 void AliMC::DumpPStack () const
958 // Dumps the particle stack
960 AliRunLoader * runloader = AliRunLoader::Instance();
961 if (runloader->Stack())
962 runloader->Stack()->DumpPStack();
965 //_______________________________________________________________________
966 Int_t AliMC::GetNtrack() const {
968 // Returns number of tracks in stack
971 AliRunLoader * runloader = AliRunLoader::Instance();
972 if (runloader->Stack())
973 ntracks = runloader->Stack()->GetNtrack();
977 //_______________________________________________________________________
978 Int_t AliMC::GetPrimary(Int_t track) const
981 // return number of primary that has generated track
983 Int_t nprimary = -999;
984 AliRunLoader * runloader = AliRunLoader::Instance();
985 if (runloader->Stack())
986 nprimary = runloader->Stack()->GetPrimary(track);
990 //_______________________________________________________________________
991 TParticle* AliMC::Particle(Int_t i) const
993 // Returns the i-th particle from the stack taking into account
994 // the remaping done by PurifyKine
995 AliRunLoader * runloader = AliRunLoader::Instance();
997 if (runloader->Stack())
998 return runloader->Stack()->Particle(i);
1002 //_______________________________________________________________________
1003 const TObjArray* AliMC::Particles() const {
1005 // Returns pointer to Particles array
1007 AliRunLoader * runloader = AliRunLoader::Instance();
1009 if (runloader->Stack())
1010 return runloader->Stack()->Particles();
1014 //_______________________________________________________________________
1015 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, const Float_t *pmom,
1016 const Float_t *vpos, const Float_t *polar, Float_t tof,
1017 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1019 // Delegate to stack
1021 AliRunLoader * runloader = AliRunLoader::Instance();
1023 if (runloader->Stack())
1024 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1025 mech, ntr, weight, is);
1028 //_______________________________________________________________________
1029 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1030 Double_t px, Double_t py, Double_t pz, Double_t e,
1031 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1032 Double_t polx, Double_t poly, Double_t polz,
1033 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1035 // Delegate to stack
1037 AliRunLoader * runloader = AliRunLoader::Instance();
1039 if (runloader->Stack())
1040 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1041 polx, poly, polz, mech, ntr, weight, is);
1044 //_______________________________________________________________________
1045 void AliMC::SetHighWaterMark(Int_t nt) const
1048 // Set high water mark for last track in event
1049 AliRunLoader * runloader = AliRunLoader::Instance();
1051 if (runloader->Stack())
1052 runloader->Stack()->SetHighWaterMark(nt);
1055 //_______________________________________________________________________
1056 void AliMC::KeepTrack(Int_t track) const
1059 // Delegate to stack
1061 AliRunLoader * runloader = AliRunLoader::Instance();
1063 if (runloader->Stack())
1064 runloader->Stack()->KeepTrack(track);
1067 //_______________________________________________________________________
1068 void AliMC::FlagTrack(Int_t track) const
1070 // Delegate to stack
1072 AliRunLoader * runloader = AliRunLoader::Instance();
1074 if (runloader->Stack())
1075 runloader->Stack()->FlagTrack(track);
1078 //_______________________________________________________________________
1079 void AliMC::SetCurrentTrack(Int_t track) const
1082 // Set current track number
1084 AliRunLoader * runloader = AliRunLoader::Instance();
1086 if (runloader->Stack())
1087 runloader->Stack()->SetCurrentTrack(track);
1090 //_______________________________________________________________________
1091 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1094 // add a trackrefernce to the list
1095 Int_t primary = GetPrimary(label);
1096 Particle(primary)->SetBit(kKeepBit);
1098 Int_t nref = fTmpTrackReferences.GetEntriesFast();
1099 return new(fTmpTrackReferences[nref]) AliTrackReference(label, id);
1104 //_______________________________________________________________________
1105 void AliMC::ResetTrackReferences()
1108 // Reset all references
1110 fTmpTrackReferences.Clear();
1113 //_______________________________________________________________________
1114 void AliMC::RemapTrackReferencesIDs(Int_t *map)
1117 // Remapping track reference
1118 // Called at finish primary
1121 Int_t nEntries = fTmpTrackReferences.GetEntries();
1122 for (Int_t i=0; i < nEntries; i++){
1123 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences.UncheckedAt(i));
1125 Int_t newID = map[ref->GetTrack()];
1126 if (newID>=0) ref->SetTrack(newID);
1128 ref->SetBit(kNotDeleted,kFALSE);
1129 fTmpTrackReferences.RemoveAt(i);
1133 fTmpTrackReferences.Compress();
1136 //_______________________________________________________________________
1137 void AliMC::FixParticleDecaytime()
1140 // Fix the particle decay time according to rmin and rmax for decays
1144 gMC->TrackMomentum(p);
1145 Double_t tmin, tmax;
1148 // Transverse velocity
1149 Double_t vt = p.Pt() / p.E();
1151 if ((b = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->SolenoidField()) > 0.) { // [kG]
1155 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1157 // Revolution frequency
1159 Double_t omega = vt / rho;
1161 // Maximum and minimum decay time
1163 // Check for curlers first
1164 const Double_t kOvRhoSqr2 = 1./(rho*TMath::Sqrt(2.));
1165 if (fRDecayMax * kOvRhoSqr2 > 1.) return;
1169 tmax = TMath::ACos((1.-fRDecayMax*kOvRhoSqr2)*(1.+fRDecayMax*kOvRhoSqr2)) / omega; // [ct]
1170 tmin = TMath::ACos((1.-fRDecayMin*kOvRhoSqr2)*(1.+fRDecayMin*kOvRhoSqr2)) / omega; // [ct]
1172 tmax = fRDecayMax / vt; // [ct]
1173 tmin = fRDecayMin / vt; // [ct]
1177 // Dial t using the two limits
1178 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1181 // Force decay time in transport code
1183 gMC->ForceDecayTime(t / 2.99792458e10);
1186 void AliMC::MakeTmpTrackRefsTree()
1188 // Make the temporary track reference tree
1189 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1190 fTmpTreeTR = new TTree("TreeTR", "Track References");
1191 TClonesArray* pRef = &fTmpTrackReferences;
1192 fTmpTreeTR->Branch("TrackReferences", &pRef, 4000);
1195 //_______________________________________________________________________
1196 void AliMC::ReorderAndExpandTreeTR()
1199 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1202 AliRunLoader *rl = AliRunLoader::Instance();
1205 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1206 rl->MakeTrackRefsContainer();
1207 TTree * treeTR = rl->TreeTR();
1209 // make branch for central track references
1211 TClonesArray* pRef = &fTrackReferences;
1212 branch = treeTR->Branch("TrackReferences", &pRef);
1215 AliStack* stack = rl->Stack();
1216 Int_t np = stack->GetNprimary();
1217 Int_t nt = fTmpTreeTR->GetEntries();
1219 // Loop over tracks and find the secondaries with the help of the kine tree
1222 for (Int_t ip = np - 1; ip > -1; ip--) {
1223 TParticle *part = stack->Particle(ip);
1224 //printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1226 // Skip primaries that have not been transported
1227 Int_t dau1 = part->GetFirstDaughter();
1229 if (!part->TestBit(kTransportBit)) continue;
1231 fTmpTreeTR->GetEntry(it++);
1232 Int_t nh = fTmpTrackReferences.GetEntries();
1233 // Determine range of secondaries produced by this primary
1235 Int_t inext = ip - 1;
1238 part = stack->Particle(inext);
1239 dau2 = part->GetFirstDaughter();
1240 if (!(part->TestBit(kTransportBit)) || dau2 == -1 || dau2 < np) {
1241 // if (dau2 == -1 || dau2 < np) {
1247 dau2 = stack->GetNtrack() - 1;
1250 } // find upper bound
1252 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1254 // Loop over reference hits and find secondary label
1255 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1256 for (Int_t ih = 0; ih < nh; ih++) {
1257 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1258 Int_t label = tr->Label();
1260 if (label == ip) continue;
1261 if (label > dau2 || label < dau1)
1262 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1265 Int_t nref = fTrackReferences.GetEntriesFast();
1266 new(fTrackReferences[nref]) AliTrackReference(*tr);
1270 fTrackReferences.Clear();
1275 // Now loop again and write the primaries
1277 for (Int_t ip = 0; ip < np; ip++) {
1278 TParticle* part = stack->Particle(ip);
1279 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1280 if (part->TestBit(kTransportBit))
1282 // Skip particles that have not been transported
1283 fTmpTreeTR->GetEntry(it--);
1284 Int_t nh = fTmpTrackReferences.GetEntries();
1286 // Loop over reference hits and find primary labels
1287 for (Int_t ih = 0; ih < nh; ih++) {
1288 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences.At(ih);
1289 Int_t label = tr->Label();
1291 Int_t nref = fTrackReferences.GetEntriesFast();
1292 new(fTrackReferences[nref]) AliTrackReference(*tr);
1297 fTrackReferences.Clear();
1301 if (ifills != stack->GetNtrack())
1302 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1306 fTmpFileTR->Close();
1308 fTmpTrackReferences.Clear();
1309 gSystem->Exec("rm -rf TrackRefsTmp.root");