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>
27 #include <TGeoManager.h>
28 #include <TStopwatch.h>
30 #include <TVirtualMC.h>
34 #include "AliDetector.h"
35 #include "AliGenerator.h"
36 #include "AliHeader.h"
43 #include "AliTrackReference.h"
44 #include "AliSimulation.h"
45 #include "AliGeomManager.h"
46 #include "AliCDBManager.h"
47 #include "AliCDBStorage.h"
48 #include "AliCDBEntry.h"
53 //_______________________________________________________________________
75 //_______________________________________________________________________
76 AliMC::AliMC(const char *name, const char *title) :
77 TVirtualMCApplication(name, title),
87 fImedia(new TArrayI(1000)),
90 fHitLists(new TList()),
91 fTrackReferences(new TClonesArray("AliTrackReference", 100))
94 // Set transport parameters
97 // Prepare the tracking medium lists
98 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
101 //_______________________________________________________________________
102 AliMC::AliMC(const AliMC &mc) :
103 TVirtualMCApplication(mc),
120 // Copy constructor for AliMC
125 //_______________________________________________________________________
133 // Delete track references
134 if (fTrackReferences) {
135 fTrackReferences->Delete();
136 delete fTrackReferences;
137 fTrackReferences = 0;
142 //_______________________________________________________________________
143 void AliMC::Copy(TObject &) const
145 //dummy Copy function
146 AliFatal("Not implemented!");
149 //_______________________________________________________________________
150 void AliMC::ConstructGeometry()
153 // Either load geometry from file or create it through usual
154 // loop on detectors. In the first case the method
155 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
156 // at InitGeometry().
159 if(gAlice->IsRootGeometry()){ //load geometry either from CDB or from file
160 if(gAlice->IsGeomFromCDB()){
161 AliInfo("Loading geometry from CDB default storage");
162 AliCDBPath path("GRP","Geometry","Data");
163 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
164 if(!entry) AliFatal("Unable to load geometry from CDB!");
166 gGeoManager = (TGeoManager*) entry->GetObject();
167 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
170 const char *geomfilename = gAlice->GetGeometryFileName();
171 if(gSystem->ExpandPathName(geomfilename)){
172 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
173 TGeoManager::Import(geomfilename);
175 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
180 // Create modules, materials, geometry
182 TIter next(gAlice->Modules());
184 AliDebug(1, "Geometry creation:");
185 while((detector = dynamic_cast<AliModule*>(next()))) {
187 // Initialise detector materials and geometry
188 detector->CreateMaterials();
189 detector->CreateGeometry();
190 AliInfo(Form("%10s R:%.2fs C:%.2fs",
191 detector->GetName(),stw.RealTime(),stw.CpuTime()));
197 //_______________________________________________________________________
198 Bool_t AliMC::MisalignGeometry()
200 // Call misalignment code if AliSimulation object was defined.
202 if(!gAlice->IsRootGeometry()){
203 //Set alignable volumes for the whole geometry
204 SetAllAlignableVolumes();
206 // Misalign geometry via AliSimulation instance
207 if (!AliSimulation::GetInstance()) return kFALSE;
208 AliGeomManager::SetGeometry(gGeoManager);
209 return AliSimulation::GetInstance()->MisalignGeometry(gAlice->GetRunLoader());
212 //_______________________________________________________________________
213 void AliMC::ConstructOpGeometry()
216 // Loop all detector modules and call DefineOpticalProperties() method
219 TIter next(gAlice->Modules());
221 AliInfo("Optical properties definition");
222 while((detector = dynamic_cast<AliModule*>(next()))) {
223 // Initialise detector optical properties
224 detector->DefineOpticalProperties();
228 //_______________________________________________________________________
229 void AliMC::InitGeometry()
232 // Initialize detectors
235 AliInfo("Initialisation:");
237 TIter next(gAlice->Modules());
239 while((detector = dynamic_cast<AliModule*>(next()))) {
241 // Initialise detector geometry
242 if(gAlice->IsRootGeometry()) detector->CreateMaterials();
244 AliInfo(Form("%10s R:%.2fs C:%.2fs",
245 detector->GetName(),stw.RealTime(),stw.CpuTime()));
249 //_______________________________________________________________________
250 void AliMC::SetAllAlignableVolumes()
253 // Add alignable volumes (TGeoPNEntries) looping on all
257 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
259 TIter next(gAlice->Modules());
260 while((detector = dynamic_cast<AliModule*>(next()))) {
261 detector->AddAlignableVolumes();
265 //_______________________________________________________________________
266 void AliMC::GeneratePrimaries()
269 // Generate primary particles and fill them in the stack.
272 Generator()->Generate();
275 //_______________________________________________________________________
276 void AliMC::SetGenerator(AliGenerator *generator)
279 // Load the event generator
281 if(!fGenerator) fGenerator = generator;
284 //_______________________________________________________________________
285 void AliMC::ResetGenerator(AliGenerator *generator)
288 // Load the event generator
292 AliWarning(Form("Replacing generator %s with %s",
293 fGenerator->GetName(),generator->GetName()));
296 AliWarning(Form("Replacing generator %s with NULL",
297 fGenerator->GetName()));
300 fGenerator = generator;
303 //_______________________________________________________________________
304 void AliMC::FinishRun()
306 // Clean generator information
307 AliDebug(1, "fGenerator->FinishRun()");
308 fGenerator->FinishRun();
310 //Output energy summary tables
311 AliDebug(1, "EnergySummary()");
312 ToAliDebug(1, EnergySummary());
315 //_______________________________________________________________________
316 void AliMC::BeginPrimary()
319 // Called at the beginning of each primary track
324 ResetTrackReferences();
328 //_______________________________________________________________________
329 void AliMC::PreTrack()
331 // Actions before the track's transport
332 TObjArray &dets = *gAlice->Modules();
335 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
336 if((module = dynamic_cast<AliModule*>(dets[i])))
342 //_______________________________________________________________________
343 void AliMC::Stepping()
346 // Called at every step during transport
349 Int_t id = DetFromMate(gMC->CurrentMedium());
353 if ( gMC->IsNewTrack() &&
354 gMC->TrackTime() == 0. &&
356 fRDecayMax > fRDecayMin &&
357 gMC->TrackPid() == fDecayPdg )
359 FixParticleDecaytime();
365 // --- If lego option, do it and leave
367 gAlice->Lego()->StepManager();
370 //Update energy deposition tables
371 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
373 // write tracke reference for track which is dissapearing - MI
374 if (gMC->IsTrackDisappeared()) {
375 if (gMC->Etot()>0.05) AddTrackReference(GetCurrentTrackNumber());
378 //Call the appropriate stepping routine;
379 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
380 if(det && det->StepManagerIsEnabled()) {
381 if(AliLog::GetGlobalDebugLevel()>0) fMCQA->StepManager(id);
387 //_______________________________________________________________________
388 void AliMC::EnergySummary()
391 // Print summary of deposited energy
397 Int_t kn, i, left, j, id;
398 const Float_t kzero=0;
399 Int_t ievent=gAlice->GetRunLoader()->GetHeader()->GetEvent()+1;
401 // Energy loss information
403 printf("***************** Energy Loss Information per event (GEV) *****************\n");
404 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
407 fEventEnergy[ndep]=kn;
412 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
415 fSummEnergy[ndep]=ed;
416 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
421 for(kn=0;kn<(ndep-1)/3+1;kn++) {
423 for(i=0;i<(3<left?3:left);i++) {
425 id=Int_t (fEventEnergy[j]+0.1);
426 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
431 // Relative energy loss in different detectors
432 printf("******************** Relative Energy Loss per event ********************\n");
433 printf("Total energy loss per event %10.3f GeV\n",edtot);
434 for(kn=0;kn<(ndep-1)/5+1;kn++) {
436 for(i=0;i<(5<left?5:left);i++) {
438 id=Int_t (fEventEnergy[j]+0.1);
439 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
443 for(kn=0;kn<75;kn++) printf("*");
447 // Reset the TArray's
448 // fEventEnergy.Set(0);
449 // fSummEnergy.Set(0);
450 // fSum2Energy.Set(0);
453 //_____________________________________________________________________________
454 void AliMC::BeginEvent()
457 // Clean-up previous event
459 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
460 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
461 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
462 AliDebug(1, " BEGINNING EVENT ");
463 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
464 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
465 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
467 AliRunLoader *runloader=gAlice->GetRunLoader();
469 /*******************************/
470 /* Clean after eventual */
472 /*******************************/
475 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
476 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
477 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
478 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
480 fEventEnergy.Reset();
481 // Clean detector information
483 if (runloader->Stack())
484 runloader->Stack()->Reset();//clean stack -> tree is unloaded
486 runloader->MakeStack();//or make a new one
488 if(gAlice->Lego() == 0x0)
490 AliDebug(1, "fRunLoader->MakeTree(K)");
491 runloader->MakeTree("K");
494 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
495 gMC->SetStack(gAlice->GetRunLoader()->Stack());//Was in InitMC - but was moved here
496 //because we don't have guarantee that
497 //stack pointer is not going to change from event to event
498 //since it bellobgs to header and is obtained via RunLoader
500 // Reset all Detectors & kinematics & make/reset trees
503 runloader->GetHeader()->Reset(gAlice->GetRunNumber(),gAlice->GetEvNumber(),
504 gAlice->GetEventNrInRun());
505 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
509 gAlice->Lego()->BeginEvent();
514 AliDebug(1, "ResetHits()");
517 AliDebug(1, "fRunLoader->MakeTree(H)");
518 runloader->MakeTree("H");
520 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
521 runloader->MakeTrackRefsContainer();//for insurance
524 //create new branches and SetAdresses
525 TIter next(gAlice->Modules());
527 while((detector = (AliModule*)next()))
529 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
530 detector->MakeBranch("H");
531 AliDebug(2, Form("%s->MakeBranchTR()",detector->GetName()));
532 detector->MakeBranchTR();
533 AliDebug(2, Form("%s->SetTreeAddress()",detector->GetName()));
534 detector->SetTreeAddress();
536 // make branch for AliRun track References
537 TTree * treeTR = runloader->TreeTR();
539 // make branch for central track references
540 if (!fTrackReferences) fTrackReferences = new TClonesArray("AliTrackReference",0);
542 branch = treeTR->Branch("AliRun",&fTrackReferences);
543 branch->SetAddress(&fTrackReferences);
548 //_______________________________________________________________________
549 void AliMC::ResetHits()
552 // Reset all Detectors hits
554 TIter next(gAlice->Modules());
556 while((detector = dynamic_cast<AliModule*>(next()))) {
557 detector->ResetHits();
561 //_______________________________________________________________________
562 void AliMC::PostTrack()
564 // Posts tracks for each module
565 TObjArray &dets = *gAlice->Modules();
568 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
569 if((module = dynamic_cast<AliModule*>(dets[i])))
573 //_______________________________________________________________________
574 void AliMC::FinishPrimary()
577 // Called at the end of each primary track
579 AliRunLoader *runloader=gAlice->GetRunLoader();
580 // static Int_t count=0;
581 // const Int_t times=10;
582 // This primary is finished, purify stack
583 #if ROOT_VERSION_CODE > 262152
584 if (!(gMC->SecondariesAreOrdered()))
585 runloader->Stack()->ReorderKine();
587 runloader->Stack()->PurifyKine();
589 TIter next(gAlice->Modules());
591 while((detector = dynamic_cast<AliModule*>(next()))) {
592 detector->FinishPrimary();
593 AliLoader* loader = detector->GetLoader();
596 TTree* treeH = loader->TreeH();
597 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
601 // Write out track references if any
602 if (runloader->TreeTR()) runloader->TreeTR()->Fill();
605 //_______________________________________________________________________
606 void AliMC::FinishEvent()
609 // Called at the end of the event.
614 if(gAlice->Lego()) gAlice->Lego()->FinishEvent();
616 TIter next(gAlice->Modules());
618 while((detector = dynamic_cast<AliModule*>(next()))) {
619 detector->FinishEvent();
622 //Update the energy deposit tables
624 for(i=0;i<fEventEnergy.GetSize();i++)
626 fSummEnergy[i]+=fEventEnergy[i];
627 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
630 AliRunLoader *runloader=gAlice->GetRunLoader();
632 AliHeader* header = runloader->GetHeader();
633 AliStack* stack = runloader->Stack();
634 if ( (header == 0x0) || (stack == 0x0) )
635 {//check if we got header and stack. If not cry and exit aliroot
636 AliFatal("Can not get the stack or header from LOADER");
637 return;//never reached
639 // Update Header information
640 header->SetNprimary(stack->GetNprimary());
641 header->SetNtrack(stack->GetNtrack());
644 // Write out the kinematics
645 if (!gAlice->Lego()) stack->FinishEvent();
647 // Write out the event Header information
648 TTree* treeE = runloader->TreeE();
651 header->SetStack(stack);
656 AliError("Can not get TreeE from RL");
659 if(gAlice->Lego() == 0x0)
661 runloader->WriteKinematics("OVERWRITE");
662 runloader->WriteTrackRefs("OVERWRITE");
663 runloader->WriteHits("OVERWRITE");
666 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
667 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
668 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
669 AliDebug(1, " FINISHING EVENT ");
670 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
671 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
672 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
675 //_______________________________________________________________________
676 void AliMC::Field(const Double_t* x, Double_t* b) const
678 // Calculates field "b" at point "x"
682 //_______________________________________________________________________
687 //=================Create Materials and geometry
689 // Set alignable volumes for the whole geometry (with old root)
690 #if ROOT_VERSION_CODE < 331527
691 SetAllAlignableVolumes();
693 //Read the cuts for all materials
695 //Build the special IMEDIA table
698 //Compute cross-sections
701 //Initialise geometry deposition table
702 fEventEnergy.Set(gMC->NofVolumes()+1);
703 fSummEnergy.Set(gMC->NofVolumes()+1);
704 fSum2Energy.Set(gMC->NofVolumes()+1);
707 fMCQA = new AliMCQA(gAlice->GetNdets());
709 // Register MC in configuration
710 AliConfig::Instance()->Add(gMC);
714 //_______________________________________________________________________
715 void AliMC::MediaTable()
718 // Built media table to get from the media number to
722 Int_t kz, nz, idt, lz, i, k, ind;
724 TObjArray &dets = *gAlice->Detectors();
726 Int_t ndets=gAlice->GetNdets();
729 for (kz=0;kz<ndets;kz++) {
730 // If detector is defined
731 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
732 TArrayI &idtmed = *(det->GetIdtmed());
733 for(nz=0;nz<100;nz++) {
735 // Find max and min material number
736 if((idt=idtmed[nz])) {
737 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
738 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
741 if(det->LoMedium() > det->HiMedium()) {
745 if(det->HiMedium() > fImedia->GetSize()) {
746 AliError(Form("Increase fImedia from %d to %d",
747 fImedia->GetSize(),det->HiMedium()));
750 // Tag all materials in rage as belonging to detector kz
751 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
758 // Print summary table
759 AliInfo("Tracking media ranges:");
761 for(i=0;i<(ndets-1)/6+1;i++) {
762 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
764 det=dynamic_cast<AliModule*>(dets[ind]);
766 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
769 printf(" %6s: %3d -> %3d;","NULL",0,0);
776 //_______________________________________________________________________
777 void AliMC::ReadTransPar()
780 // Read filename to set the transport parameters
784 const Int_t kncuts=10;
785 const Int_t knflags=11;
786 const Int_t knpars=kncuts+knflags;
787 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
788 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
789 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
790 "MULS","PAIR","PHOT","RAYL"};
796 Int_t i, itmed, iret, ktmed, kz;
799 // See whether the file is there
800 filtmp=gSystem->ExpandPathName(fTransParName.Data());
801 lun=fopen(filtmp,"r");
804 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
809 // Initialise cuts and flags
810 for(i=0;i<kncuts;i++) cut[i]=-99;
811 for(i=0;i<knflags;i++) flag[i]=-99;
813 for(i=0;i<256;i++) line[i]='\0';
814 // Read up to the end of line excluded
815 iret=fscanf(lun,"%[^\n]",line);
821 // Read the end of line
824 if(line[0]=='*') continue;
826 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",
827 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
828 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
829 &flag[8],&flag[9],&flag[10]);
833 AliWarning(Form("Error reading file %s",fTransParName.Data()));
836 // Check that the module exist
837 AliModule *mod = gAlice->GetModule(detName);
839 // Get the array of media numbers
840 TArrayI &idtmed = *mod->GetIdtmed();
841 // Check that the tracking medium code is valid
842 if(0<=itmed && itmed < 100) {
845 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
848 // Set energy thresholds
849 for(kz=0;kz<kncuts;kz++) {
851 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
852 kpars[kz],cut[kz],itmed,mod->GetName()));
853 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
856 // Set transport mechanisms
857 for(kz=0;kz<knflags;kz++) {
859 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
860 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
861 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
865 AliWarning(Form("Invalid medium code %d",itmed));
869 AliDebug(1, Form("%s not present",detName));
875 //_______________________________________________________________________
876 void AliMC::SetTransPar(const char *filename)
879 // Sets the file name for transport parameters
881 fTransParName = filename;
884 //_______________________________________________________________________
885 void AliMC::Browse(TBrowser *b)
888 // Called when the item "Run" is clicked on the left pane
889 // of the Root browser.
890 // It displays the Root Trees and all detectors.
892 //detectors are in folders anyway
893 b->Add(fMCQA,"AliMCQA");
896 //_______________________________________________________________________
897 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
900 // Add a hit to detector id
902 TObjArray &dets = *gAlice->Modules();
903 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
906 //_______________________________________________________________________
907 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
910 // Add digit to detector id
912 TObjArray &dets = *gAlice->Modules();
913 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
916 //_______________________________________________________________________
917 Int_t AliMC::GetCurrentTrackNumber() const {
919 // Returns current track
921 return gAlice->GetRunLoader()->Stack()->GetCurrentTrackNumber();
924 //_______________________________________________________________________
925 void AliMC::DumpPart (Int_t i) const
928 // Dumps particle i in the stack
930 AliRunLoader * runloader = gAlice->GetRunLoader();
931 if (runloader->Stack())
932 runloader->Stack()->DumpPart(i);
935 //_______________________________________________________________________
936 void AliMC::DumpPStack () const
939 // Dumps the particle stack
941 AliRunLoader * runloader = gAlice->GetRunLoader();
942 if (runloader->Stack())
943 runloader->Stack()->DumpPStack();
946 //_______________________________________________________________________
947 Int_t AliMC::GetNtrack() const {
949 // Returns number of tracks in stack
952 AliRunLoader * runloader = gAlice->GetRunLoader();
953 if (runloader->Stack())
954 ntracks = runloader->Stack()->GetNtrack();
958 //_______________________________________________________________________
959 Int_t AliMC::GetPrimary(Int_t track) const
962 // return number of primary that has generated track
964 Int_t nprimary = -999;
965 AliRunLoader * runloader = gAlice->GetRunLoader();
966 if (runloader->Stack())
967 nprimary = runloader->Stack()->GetPrimary(track);
971 //_______________________________________________________________________
972 TParticle* AliMC::Particle(Int_t i) const
974 // Returns the i-th particle from the stack taking into account
975 // the remaping done by PurifyKine
976 AliRunLoader * runloader = gAlice->GetRunLoader();
978 if (runloader->Stack())
979 return runloader->Stack()->Particle(i);
983 //_______________________________________________________________________
984 TObjArray* AliMC::Particles() const {
986 // Returns pointer to Particles array
988 AliRunLoader * runloader = gAlice->GetRunLoader();
990 if (runloader->Stack())
991 return runloader->Stack()->Particles();
995 //_______________________________________________________________________
996 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
997 Float_t *vpos, Float_t *polar, Float_t tof,
998 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1000 // Delegate to stack
1002 AliRunLoader * runloader = gAlice->GetRunLoader();
1004 if (runloader->Stack())
1005 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1006 mech, ntr, weight, is);
1009 //_______________________________________________________________________
1010 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1011 Double_t px, Double_t py, Double_t pz, Double_t e,
1012 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1013 Double_t polx, Double_t poly, Double_t polz,
1014 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1016 // Delegate to stack
1018 AliRunLoader * runloader = gAlice->GetRunLoader();
1020 if (runloader->Stack())
1021 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1022 polx, poly, polz, mech, ntr, weight, is);
1025 //_______________________________________________________________________
1026 void AliMC::SetHighWaterMark(Int_t nt) const
1029 // Set high water mark for last track in event
1030 AliRunLoader * runloader = gAlice->GetRunLoader();
1032 if (runloader->Stack())
1033 runloader->Stack()->SetHighWaterMark(nt);
1036 //_______________________________________________________________________
1037 void AliMC::KeepTrack(Int_t track) const
1040 // Delegate to stack
1042 AliRunLoader * runloader = gAlice->GetRunLoader();
1044 if (runloader->Stack())
1045 runloader->Stack()->KeepTrack(track);
1048 //_______________________________________________________________________
1049 void AliMC::FlagTrack(Int_t track) const
1051 // Delegate to stack
1053 AliRunLoader * runloader = gAlice->GetRunLoader();
1055 if (runloader->Stack())
1056 runloader->Stack()->FlagTrack(track);
1059 //_______________________________________________________________________
1060 void AliMC::SetCurrentTrack(Int_t track) const
1063 // Set current track number
1065 AliRunLoader * runloader = gAlice->GetRunLoader();
1067 if (runloader->Stack())
1068 runloader->Stack()->SetCurrentTrack(track);
1071 //_______________________________________________________________________
1072 void AliMC::AddTrackReference(Int_t label){
1074 // add a trackrefernce to the list
1075 if (!fTrackReferences) {
1076 AliError("Container trackrefernce not active");
1079 Int_t nref = fTrackReferences->GetEntriesFast();
1080 TClonesArray &lref = *fTrackReferences;
1081 new(lref[nref]) AliTrackReference(label);
1086 //_______________________________________________________________________
1087 void AliMC::ResetTrackReferences()
1090 // Reset all references
1092 if (fTrackReferences) fTrackReferences->Clear();
1094 TIter next(gAlice->Modules());
1095 AliModule *detector;
1096 while((detector = dynamic_cast<AliModule*>(next()))) {
1097 detector->ResetTrackReferences();
1101 void AliMC::RemapTrackReferencesIDs(Int_t *map)
1104 // Remapping track reference
1105 // Called at finish primary
1107 if (!fTrackReferences) return;
1108 Int_t nEntries = fTrackReferences->GetEntries();
1109 for (Int_t i=0; i < nEntries; i++){
1110 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
1112 Int_t newID = map[ref->GetTrack()];
1113 if (newID>=0) ref->SetTrack(newID);
1115 ref->SetBit(kNotDeleted,kFALSE);
1116 fTrackReferences->RemoveAt(i);
1120 fTrackReferences->Compress();
1123 void AliMC::FixParticleDecaytime()
1126 // Fix the particle decay time according to rmin and rmax for decays
1130 gMC->TrackMomentum(p);
1131 Double_t tmin, tmax;
1134 // Transverse velocity
1135 Double_t vt = p.Pt() / p.E();
1137 if ((b = gAlice->Field()->SolenoidField()) > 0.) { // [kG]
1141 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1143 // Revolution frequency
1145 Double_t omega = vt / rho;
1147 // Maximum and minimum decay time
1149 // Check for curlers first
1150 if (fRDecayMax * fRDecayMax / rho / rho / 2. > 1.) return;
1154 tmax = TMath::ACos(1. - fRDecayMax * fRDecayMax / rho / rho / 2.) / omega; // [ct]
1155 tmin = TMath::ACos(1. - fRDecayMin * fRDecayMin / rho / rho / 2.) / omega; // [ct]
1157 tmax = fRDecayMax / vt; // [ct]
1158 tmin = fRDecayMin / vt; // [ct]
1162 // Dial t using the two limits
1163 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1166 // Force decay time in transport code
1168 gMC->ForceDecayTime(t / 2.99792458e10);