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>
31 #include <TParticle.h>
36 #include "AliDetector.h"
37 #include "AliGenerator.h"
38 #include "AliHeader.h"
46 #include "AliTrackReference.h"
47 #include "AliSimulation.h"
48 #include "AliGeomManager.h"
49 #include "AliCDBManager.h"
50 #include "AliCDBStorage.h"
51 #include "AliCDBEntry.h"
56 //_______________________________________________________________________
74 fTmpTrackReferences(0)
81 //_______________________________________________________________________
82 AliMC::AliMC(const char *name, const char *title) :
83 TVirtualMCApplication(name, title),
93 fImedia(new TArrayI(1000)),
96 fHitLists(new TList()),
99 fTrackReferences(new TClonesArray("AliTrackReference", 100)),
100 fTmpTrackReferences(new TClonesArray("AliTrackReference", 100))
103 // Set transport parameters
106 // Prepare the tracking medium lists
107 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
110 //_______________________________________________________________________
111 AliMC::AliMC(const AliMC &mc) :
112 TVirtualMCApplication(mc),
129 fTmpTrackReferences(0)
132 // Copy constructor for AliMC
137 //_______________________________________________________________________
145 // Delete track references
146 if (fTrackReferences) {
147 fTrackReferences->Delete();
148 delete fTrackReferences;
149 fTrackReferences = 0;
152 if (fTmpTrackReferences) {
153 fTmpTrackReferences->Delete();
154 delete fTmpTrackReferences;
155 fTmpTrackReferences = 0;
160 //_______________________________________________________________________
161 void AliMC::Copy(TObject &) const
163 //dummy Copy function
164 AliFatal("Not implemented!");
167 //_______________________________________________________________________
168 void AliMC::ConstructGeometry()
171 // Either load geometry from file or create it through usual
172 // loop on detectors. In the first case the method
173 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
174 // at InitGeometry().
177 if(gAlice->IsRootGeometry()){ //load geometry either from CDB or from file
178 if(gAlice->IsGeomFromCDB()){
179 AliInfo("Loading geometry from CDB default storage");
180 AliCDBPath path("GRP","Geometry","Data");
181 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
182 if(!entry) AliFatal("Unable to load geometry from CDB!");
184 gGeoManager = (TGeoManager*) entry->GetObject();
185 if (!gGeoManager) AliFatal("TGeoManager object not found in the specified CDB entry!");
188 const char *geomfilename = gAlice->GetGeometryFileName();
189 if(gSystem->ExpandPathName(geomfilename)){
190 AliInfo(Form("Loading geometry from file:\n %40s",geomfilename));
191 TGeoManager::Import(geomfilename);
193 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
198 // Create modules, materials, geometry
200 TIter next(gAlice->Modules());
202 AliDebug(1, "Geometry creation:");
203 while((detector = dynamic_cast<AliModule*>(next()))) {
205 // Initialise detector materials and geometry
206 detector->CreateMaterials();
207 detector->CreateGeometry();
208 AliInfo(Form("%10s R:%.2fs C:%.2fs",
209 detector->GetName(),stw.RealTime(),stw.CpuTime()));
215 //_______________________________________________________________________
216 Bool_t AliMC::MisalignGeometry()
218 // Call misalignment code if AliSimulation object was defined.
220 if(!gAlice->IsRootGeometry()){
221 //Set alignable volumes for the whole geometry
222 SetAllAlignableVolumes();
224 // Misalign geometry via AliSimulation instance
225 if (!AliSimulation::GetInstance()) return kFALSE;
226 AliGeomManager::SetGeometry(gGeoManager);
227 return AliSimulation::GetInstance()->MisalignGeometry(gAlice->GetRunLoader());
230 //_______________________________________________________________________
231 void AliMC::ConstructOpGeometry()
234 // Loop all detector modules and call DefineOpticalProperties() method
237 TIter next(gAlice->Modules());
239 AliInfo("Optical properties definition");
240 while((detector = dynamic_cast<AliModule*>(next()))) {
241 // Initialise detector optical properties
242 detector->DefineOpticalProperties();
246 //_______________________________________________________________________
247 void AliMC::InitGeometry()
250 // Initialize detectors
253 AliInfo("Initialisation:");
255 TIter next(gAlice->Modules());
257 while((detector = dynamic_cast<AliModule*>(next()))) {
259 // Initialise detector geometry
260 if(gAlice->IsRootGeometry()) detector->CreateMaterials();
262 AliInfo(Form("%10s R:%.2fs C:%.2fs",
263 detector->GetName(),stw.RealTime(),stw.CpuTime()));
267 //_______________________________________________________________________
268 void AliMC::SetAllAlignableVolumes()
271 // Add alignable volumes (TGeoPNEntries) looping on all
275 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
277 TIter next(gAlice->Modules());
278 while((detector = dynamic_cast<AliModule*>(next()))) {
279 detector->AddAlignableVolumes();
283 //_______________________________________________________________________
284 void AliMC::GeneratePrimaries()
287 // Generate primary particles and fill them in the stack.
290 Generator()->Generate();
293 //_______________________________________________________________________
294 void AliMC::SetGenerator(AliGenerator *generator)
297 // Load the event generator
299 if(!fGenerator) fGenerator = generator;
302 //_______________________________________________________________________
303 void AliMC::ResetGenerator(AliGenerator *generator)
306 // Load the event generator
310 AliWarning(Form("Replacing generator %s with %s",
311 fGenerator->GetName(),generator->GetName()));
314 AliWarning(Form("Replacing generator %s with NULL",
315 fGenerator->GetName()));
318 fGenerator = generator;
321 //_______________________________________________________________________
322 void AliMC::FinishRun()
324 // Clean generator information
325 AliDebug(1, "fGenerator->FinishRun()");
326 fGenerator->FinishRun();
328 //Output energy summary tables
329 AliDebug(1, "EnergySummary()");
330 ToAliDebug(1, EnergySummary());
333 //_______________________________________________________________________
334 void AliMC::BeginPrimary()
337 // Called at the beginning of each primary track
342 ResetTrackReferences();
345 //_______________________________________________________________________
346 void AliMC::PreTrack()
348 // Actions before the track's transport
349 TObjArray &dets = *gAlice->Modules();
352 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
353 if((module = dynamic_cast<AliModule*>(dets[i])))
359 //_______________________________________________________________________
360 void AliMC::Stepping()
363 // Called at every step during transport
366 Int_t id = DetFromMate(gMC->CurrentMedium());
370 if ( gMC->IsNewTrack() &&
371 gMC->TrackTime() == 0. &&
373 fRDecayMax > fRDecayMin &&
374 gMC->TrackPid() == fDecayPdg )
376 FixParticleDecaytime();
382 // --- If lego option, do it and leave
384 gAlice->Lego()->StepManager();
387 //Update energy deposition tables
388 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
390 // write tracke reference for track which is dissapearing - MI
392 if (gMC->IsTrackDisappeared() && !(gMC->IsTrackAlive())) {
393 if (gMC->Etot() > 0.05) AddTrackReference(GetCurrentTrackNumber(),
394 AliTrackReference::kDisappeared);
399 //Call the appropriate stepping routine;
400 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
401 if(det && det->StepManagerIsEnabled()) {
402 if(AliLog::GetGlobalDebugLevel()>0) fMCQA->StepManager(id);
408 //_______________________________________________________________________
409 void AliMC::EnergySummary()
412 // Print summary of deposited energy
418 Int_t kn, i, left, j, id;
419 const Float_t kzero=0;
420 Int_t ievent=gAlice->GetRunLoader()->GetHeader()->GetEvent()+1;
422 // Energy loss information
424 printf("***************** Energy Loss Information per event (GEV) *****************\n");
425 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
428 fEventEnergy[ndep]=kn;
433 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
436 fSummEnergy[ndep]=ed;
437 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
442 for(kn=0;kn<(ndep-1)/3+1;kn++) {
444 for(i=0;i<(3<left?3:left);i++) {
446 id=Int_t (fEventEnergy[j]+0.1);
447 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
452 // Relative energy loss in different detectors
453 printf("******************** Relative Energy Loss per event ********************\n");
454 printf("Total energy loss per event %10.3f GeV\n",edtot);
455 for(kn=0;kn<(ndep-1)/5+1;kn++) {
457 for(i=0;i<(5<left?5:left);i++) {
459 id=Int_t (fEventEnergy[j]+0.1);
460 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
464 for(kn=0;kn<75;kn++) printf("*");
468 // Reset the TArray's
469 // fEventEnergy.Set(0);
470 // fSummEnergy.Set(0);
471 // fSum2Energy.Set(0);
474 //_____________________________________________________________________________
475 void AliMC::BeginEvent()
478 // Clean-up previous event
480 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
481 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
482 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
483 AliDebug(1, " BEGINNING EVENT ");
484 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
485 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
486 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
488 AliRunLoader *runloader=gAlice->GetRunLoader();
490 /*******************************/
491 /* Clean after eventual */
493 /*******************************/
496 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
497 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
498 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
499 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
501 fEventEnergy.Reset();
502 // Clean detector information
504 if (runloader->Stack())
505 runloader->Stack()->Reset();//clean stack -> tree is unloaded
507 runloader->MakeStack();//or make a new one
510 if(gAlice->Lego() == 0x0)
512 AliDebug(1, "fRunLoader->MakeTree(K)");
513 runloader->MakeTree("K");
516 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
517 gMC->SetStack(gAlice->GetRunLoader()->Stack());//Was in InitMC - but was moved here
518 //because we don't have guarantee that
519 //stack pointer is not going to change from event to event
520 //since it bellobgs to header and is obtained via RunLoader
522 // Reset all Detectors & kinematics & make/reset trees
525 runloader->GetHeader()->Reset(gAlice->GetRunNumber(),gAlice->GetEvNumber(),
526 gAlice->GetEventNrInRun());
527 // fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
531 gAlice->Lego()->BeginEvent();
536 AliDebug(1, "ResetHits()");
539 AliDebug(1, "fRunLoader->MakeTree(H)");
540 runloader->MakeTree("H");
544 MakeTmpTrackRefsTree();
545 //create new branches and SetAdresses
546 TIter next(gAlice->Modules());
548 while((detector = (AliModule*)next()))
550 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
551 detector->MakeBranch("H");
555 //_______________________________________________________________________
556 void AliMC::ResetHits()
559 // Reset all Detectors hits
561 TIter next(gAlice->Modules());
563 while((detector = dynamic_cast<AliModule*>(next()))) {
564 detector->ResetHits();
568 //_______________________________________________________________________
569 void AliMC::PostTrack()
571 // Posts tracks for each module
572 TObjArray &dets = *gAlice->Modules();
575 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
576 if((module = dynamic_cast<AliModule*>(dets[i])))
580 //_______________________________________________________________________
581 void AliMC::FinishPrimary()
584 // Called at the end of each primary track
586 AliRunLoader *runloader=gAlice->GetRunLoader();
587 // static Int_t count=0;
588 // const Int_t times=10;
589 // This primary is finished, purify stack
590 #if ROOT_VERSION_CODE > 262152
591 if (!(gMC->SecondariesAreOrdered())) {
592 runloader->Stack()->ReorderKine();
596 runloader->Stack()->PurifyKine();
600 TIter next(gAlice->Modules());
602 while((detector = dynamic_cast<AliModule*>(next()))) {
603 detector->FinishPrimary();
604 AliLoader* loader = detector->GetLoader();
607 TTree* treeH = loader->TreeH();
608 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
612 // Write out track references if any
613 if (fTmpTreeTR) fTmpTreeTR->Fill();
616 void AliMC::RemapHits()
619 // Remaps the track labels of the hits
620 AliRunLoader *runloader=gAlice->GetRunLoader();
621 AliStack* stack = runloader->Stack();
622 TList* hitLists = GetHitLists();
623 TIter next(hitLists);
624 TCollection *hitList;
626 while((hitList = dynamic_cast<TCollection*>(next()))) {
627 TIter nexthit(hitList);
629 while((hit = dynamic_cast<AliHit*>(nexthit()))) {
630 hit->SetTrack(stack->TrackLabel(hit->GetTrack()));
635 // This for detectors which have a special mapping mechanism
636 // for hits, such as TPC and TRD
640 TObjArray* modules = gAlice->Modules();
641 TIter nextmod(modules);
643 while((module = (AliModule*) nextmod())) {
644 AliDetector* det = dynamic_cast<AliDetector*> (module);
645 if (det) det->RemapTrackHitIDs(stack->TrackLabelMap());
648 RemapTrackReferencesIDs(stack->TrackLabelMap());
651 //_______________________________________________________________________
652 void AliMC::FinishEvent()
655 // Called at the end of the event.
660 if(gAlice->Lego()) gAlice->Lego()->FinishEvent();
662 TIter next(gAlice->Modules());
664 while((detector = dynamic_cast<AliModule*>(next()))) {
665 detector->FinishEvent();
668 //Update the energy deposit tables
670 for(i=0;i<fEventEnergy.GetSize();i++)
672 fSummEnergy[i]+=fEventEnergy[i];
673 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
676 AliRunLoader *runloader=gAlice->GetRunLoader();
678 AliHeader* header = runloader->GetHeader();
679 AliStack* stack = runloader->Stack();
680 if ( (header == 0x0) || (stack == 0x0) )
681 {//check if we got header and stack. If not cry and exit aliroot
682 AliFatal("Can not get the stack or header from LOADER");
683 return;//never reached
685 // Update Header information
686 header->SetNprimary(stack->GetNprimary());
687 header->SetNtrack(stack->GetNtrack());
689 // Write out the kinematics
690 if (!gAlice->Lego()) stack->FinishEvent();
692 // Synchronize the TreeTR with TreeK
693 if (fTmpTreeTR) ReorderAndExpandTreeTR();
695 // Write out the event Header information
696 TTree* treeE = runloader->TreeE();
699 header->SetStack(stack);
704 AliError("Can not get TreeE from RL");
707 if(gAlice->Lego() == 0x0)
709 runloader->WriteKinematics("OVERWRITE");
710 runloader->WriteTrackRefs("OVERWRITE");
711 runloader->WriteHits("OVERWRITE");
714 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
715 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
716 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
717 AliDebug(1, " FINISHING EVENT ");
718 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
719 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
720 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
723 //_______________________________________________________________________
724 void AliMC::Field(const Double_t* x, Double_t* b) const
726 // Calculates field "b" at point "x"
730 //_______________________________________________________________________
735 //=================Create Materials and geometry
737 // Set alignable volumes for the whole geometry (with old root)
738 #if ROOT_VERSION_CODE < 331527
739 SetAllAlignableVolumes();
741 //Read the cuts for all materials
743 //Build the special IMEDIA table
746 //Compute cross-sections
749 //Initialise geometry deposition table
750 fEventEnergy.Set(gMC->NofVolumes()+1);
751 fSummEnergy.Set(gMC->NofVolumes()+1);
752 fSum2Energy.Set(gMC->NofVolumes()+1);
755 fMCQA = new AliMCQA(gAlice->GetNdets());
757 // Register MC in configuration
758 AliConfig::Instance()->Add(gMC);
762 //_______________________________________________________________________
763 void AliMC::MediaTable()
766 // Built media table to get from the media number to
770 Int_t kz, nz, idt, lz, i, k, ind;
772 TObjArray &dets = *gAlice->Detectors();
774 Int_t ndets=gAlice->GetNdets();
777 for (kz=0;kz<ndets;kz++) {
778 // If detector is defined
779 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
780 TArrayI &idtmed = *(det->GetIdtmed());
781 for(nz=0;nz<100;nz++) {
783 // Find max and min material number
784 if((idt=idtmed[nz])) {
785 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
786 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
789 if(det->LoMedium() > det->HiMedium()) {
793 if(det->HiMedium() > fImedia->GetSize()) {
794 AliError(Form("Increase fImedia from %d to %d",
795 fImedia->GetSize(),det->HiMedium()));
798 // Tag all materials in rage as belonging to detector kz
799 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
806 // Print summary table
807 AliInfo("Tracking media ranges:");
809 for(i=0;i<(ndets-1)/6+1;i++) {
810 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
812 det=dynamic_cast<AliModule*>(dets[ind]);
814 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
817 printf(" %6s: %3d -> %3d;","NULL",0,0);
824 //_______________________________________________________________________
825 void AliMC::ReadTransPar()
828 // Read filename to set the transport parameters
832 const Int_t kncuts=10;
833 const Int_t knflags=11;
834 const Int_t knpars=kncuts+knflags;
835 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
836 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
837 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
838 "MULS","PAIR","PHOT","RAYL"};
844 Int_t i, itmed, iret, ktmed, kz;
847 // See whether the file is there
848 filtmp=gSystem->ExpandPathName(fTransParName.Data());
849 lun=fopen(filtmp,"r");
852 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
857 // Initialise cuts and flags
858 for(i=0;i<kncuts;i++) cut[i]=-99;
859 for(i=0;i<knflags;i++) flag[i]=-99;
861 for(i=0;i<256;i++) line[i]='\0';
862 // Read up to the end of line excluded
863 iret=fscanf(lun,"%[^\n]",line);
869 // Read the end of line
872 if(line[0]=='*') continue;
874 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",
875 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
876 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
877 &flag[8],&flag[9],&flag[10]);
881 AliWarning(Form("Error reading file %s",fTransParName.Data()));
884 // Check that the module exist
885 AliModule *mod = gAlice->GetModule(detName);
887 // Get the array of media numbers
888 TArrayI &idtmed = *mod->GetIdtmed();
889 // Check that the tracking medium code is valid
890 if(0<=itmed && itmed < 100) {
893 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
896 // Set energy thresholds
897 for(kz=0;kz<kncuts;kz++) {
899 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
900 kpars[kz],cut[kz],itmed,mod->GetName()));
901 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
904 // Set transport mechanisms
905 for(kz=0;kz<knflags;kz++) {
907 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
908 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
909 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
913 AliWarning(Form("Invalid medium code %d",itmed));
917 AliDebug(1, Form("%s not present",detName));
923 //_______________________________________________________________________
924 void AliMC::SetTransPar(const char *filename)
927 // Sets the file name for transport parameters
929 fTransParName = filename;
932 //_______________________________________________________________________
933 void AliMC::Browse(TBrowser *b)
936 // Called when the item "Run" is clicked on the left pane
937 // of the Root browser.
938 // It displays the Root Trees and all detectors.
940 //detectors are in folders anyway
941 b->Add(fMCQA,"AliMCQA");
944 //_______________________________________________________________________
945 void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
948 // Add a hit to detector id
950 TObjArray &dets = *gAlice->Modules();
951 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
954 //_______________________________________________________________________
955 void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
958 // Add digit to detector id
960 TObjArray &dets = *gAlice->Modules();
961 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
964 //_______________________________________________________________________
965 Int_t AliMC::GetCurrentTrackNumber() const {
967 // Returns current track
969 return gAlice->GetRunLoader()->Stack()->GetCurrentTrackNumber();
972 //_______________________________________________________________________
973 void AliMC::DumpPart (Int_t i) const
976 // Dumps particle i in the stack
978 AliRunLoader * runloader = gAlice->GetRunLoader();
979 if (runloader->Stack())
980 runloader->Stack()->DumpPart(i);
983 //_______________________________________________________________________
984 void AliMC::DumpPStack () const
987 // Dumps the particle stack
989 AliRunLoader * runloader = gAlice->GetRunLoader();
990 if (runloader->Stack())
991 runloader->Stack()->DumpPStack();
994 //_______________________________________________________________________
995 Int_t AliMC::GetNtrack() const {
997 // Returns number of tracks in stack
1000 AliRunLoader * runloader = gAlice->GetRunLoader();
1001 if (runloader->Stack())
1002 ntracks = runloader->Stack()->GetNtrack();
1006 //_______________________________________________________________________
1007 Int_t AliMC::GetPrimary(Int_t track) const
1010 // return number of primary that has generated track
1012 Int_t nprimary = -999;
1013 AliRunLoader * runloader = gAlice->GetRunLoader();
1014 if (runloader->Stack())
1015 nprimary = runloader->Stack()->GetPrimary(track);
1019 //_______________________________________________________________________
1020 TParticle* AliMC::Particle(Int_t i) const
1022 // Returns the i-th particle from the stack taking into account
1023 // the remaping done by PurifyKine
1024 AliRunLoader * runloader = gAlice->GetRunLoader();
1026 if (runloader->Stack())
1027 return runloader->Stack()->Particle(i);
1031 //_______________________________________________________________________
1032 TObjArray* AliMC::Particles() const {
1034 // Returns pointer to Particles array
1036 AliRunLoader * runloader = gAlice->GetRunLoader();
1038 if (runloader->Stack())
1039 return runloader->Stack()->Particles();
1043 //_______________________________________________________________________
1044 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1045 Float_t *vpos, Float_t *polar, Float_t tof,
1046 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1048 // Delegate to stack
1050 AliRunLoader * runloader = gAlice->GetRunLoader();
1052 if (runloader->Stack())
1053 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
1054 mech, ntr, weight, is);
1057 //_______________________________________________________________________
1058 void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
1059 Double_t px, Double_t py, Double_t pz, Double_t e,
1060 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1061 Double_t polx, Double_t poly, Double_t polz,
1062 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
1064 // Delegate to stack
1066 AliRunLoader * runloader = gAlice->GetRunLoader();
1068 if (runloader->Stack())
1069 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1070 polx, poly, polz, mech, ntr, weight, is);
1073 //_______________________________________________________________________
1074 void AliMC::SetHighWaterMark(Int_t nt) const
1077 // Set high water mark for last track in event
1078 AliRunLoader * runloader = gAlice->GetRunLoader();
1080 if (runloader->Stack())
1081 runloader->Stack()->SetHighWaterMark(nt);
1084 //_______________________________________________________________________
1085 void AliMC::KeepTrack(Int_t track) const
1088 // Delegate to stack
1090 AliRunLoader * runloader = gAlice->GetRunLoader();
1092 if (runloader->Stack())
1093 runloader->Stack()->KeepTrack(track);
1096 //_______________________________________________________________________
1097 void AliMC::FlagTrack(Int_t track) const
1099 // Delegate to stack
1101 AliRunLoader * runloader = gAlice->GetRunLoader();
1103 if (runloader->Stack())
1104 runloader->Stack()->FlagTrack(track);
1107 //_______________________________________________________________________
1108 void AliMC::SetCurrentTrack(Int_t track) const
1111 // Set current track number
1113 AliRunLoader * runloader = gAlice->GetRunLoader();
1115 if (runloader->Stack())
1116 runloader->Stack()->SetCurrentTrack(track);
1119 //_______________________________________________________________________
1120 AliTrackReference* AliMC::AddTrackReference(Int_t label, Int_t id)
1123 // add a trackrefernce to the list
1124 if (!fTrackReferences) {
1125 AliError("Container trackrefernce not active");
1128 Int_t primary = GetPrimary(label);
1129 Particle(primary)->SetBit(kKeepBit);
1131 Int_t nref = fTmpTrackReferences->GetEntriesFast();
1132 TClonesArray &lref = *fTmpTrackReferences;
1133 return new(lref[nref]) AliTrackReference(label, id);
1138 //_______________________________________________________________________
1139 void AliMC::ResetTrackReferences()
1142 // Reset all references
1144 if (fTmpTrackReferences) fTmpTrackReferences->Clear();
1147 void AliMC::RemapTrackReferencesIDs(Int_t *map)
1150 // Remapping track reference
1151 // Called at finish primary
1153 if (!fTmpTrackReferences) return;
1154 Int_t nEntries = fTmpTrackReferences->GetEntries();
1155 for (Int_t i=0; i < nEntries; i++){
1156 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTmpTrackReferences->UncheckedAt(i));
1158 Int_t newID = map[ref->GetTrack()];
1159 if (newID>=0) ref->SetTrack(newID);
1161 ref->SetBit(kNotDeleted,kFALSE);
1162 fTmpTrackReferences->RemoveAt(i);
1166 fTmpTrackReferences->Compress();
1169 void AliMC::FixParticleDecaytime()
1172 // Fix the particle decay time according to rmin and rmax for decays
1176 gMC->TrackMomentum(p);
1177 Double_t tmin, tmax;
1180 // Transverse velocity
1181 Double_t vt = p.Pt() / p.E();
1183 if ((b = gAlice->Field()->SolenoidField()) > 0.) { // [kG]
1187 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1189 // Revolution frequency
1191 Double_t omega = vt / rho;
1193 // Maximum and minimum decay time
1195 // Check for curlers first
1196 if (fRDecayMax * fRDecayMax / rho / rho / 2. > 1.) return;
1200 tmax = TMath::ACos(1. - fRDecayMax * fRDecayMax / rho / rho / 2.) / omega; // [ct]
1201 tmin = TMath::ACos(1. - fRDecayMin * fRDecayMin / rho / rho / 2.) / omega; // [ct]
1203 tmax = fRDecayMax / vt; // [ct]
1204 tmin = fRDecayMin / vt; // [ct]
1208 // Dial t using the two limits
1209 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1212 // Force decay time in transport code
1214 gMC->ForceDecayTime(t / 2.99792458e10);
1217 void AliMC::MakeTmpTrackRefsTree()
1219 // Make the temporary track reference tree
1220 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
1221 fTmpTreeTR = new TTree("TreeTR", "Track References");
1222 if (!fTmpTrackReferences) fTmpTrackReferences = new TClonesArray("AliTrackReference", 100);
1223 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTmpTrackReferences, 4000);
1226 void AliMC::ReorderAndExpandTreeTR()
1229 // Reorder and expand the temporary track reference tree in order to match the kinematics tree
1232 AliRunLoader *rl = gAlice->GetRunLoader();
1235 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
1236 rl->MakeTrackRefsContainer();
1237 TTree * treeTR = rl->TreeTR();
1239 // make branch for central track references
1240 if (!fTrackReferences) fTrackReferences = new TClonesArray("AliTrackReference",0);
1242 branch = treeTR->Branch("TrackReferences",&fTrackReferences);
1243 branch->SetAddress(&fTrackReferences);
1246 AliStack* stack = rl->Stack();
1247 Int_t np = stack->GetNprimary();
1248 Int_t nt = fTmpTreeTR->GetEntries();
1250 // Loop over tracks and find the secondaries with the help of the kine tree
1253 for (Int_t ip = np - 1; ip > -1; ip--) {
1254 TParticle *part = stack->Particle(ip);
1255 // printf("Particle %5d %5d %5d %5d %5d \n", ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), part->GetLastDaughter());
1257 // Skip primaries that have not been transported
1258 Int_t dau1 = part->GetFirstDaughter();
1260 // if ((dau1 > -1 && dau1 < np) || part->GetStatusCode() > 1) continue;
1261 if (!part->TestBit(kTransportBit)) continue;
1263 fTmpTreeTR->GetEntry(it++);
1264 Int_t nh = fTmpTrackReferences->GetEntries();
1265 // Determine range of secondaries produced by this primary
1267 Int_t inext = ip - 1;
1270 part = stack->Particle(inext);
1271 dau2 = part->GetFirstDaughter();
1272 if (dau2 == -1 || dau2 < np) {
1278 dau2 = stack->GetNtrack() - 1;
1281 } // find upper bound
1283 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
1286 // Loop over reference hits and find secondary label
1287 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
1288 for (Int_t ih = 0; ih < nh; ih++) {
1289 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences->At(ih);
1290 Int_t label = tr->Label();
1292 if (label == ip) continue;
1293 if (label > dau2 || label < dau1)
1294 AliWarning(Form("Track Reference Label out of range !: %5d %5d %5d \n", label, dau1, dau2));
1297 Int_t nref = fTrackReferences->GetEntriesFast();
1298 TClonesArray &lref = *fTrackReferences;
1299 new(lref[nref]) AliTrackReference(*tr);
1303 fTrackReferences->Clear();
1308 // Now loop again and write the primaries
1310 for (Int_t ip = 0; ip < np; ip++) {
1311 TParticle* part = stack->Particle(ip);
1312 // if ((part->GetFirstDaughter() == -1 && part->GetStatusCode() <= 1) || part->GetFirstDaughter() >= np)
1313 if (part->TestBit(kTransportBit))
1315 // Skip particles that have not been transported
1316 fTmpTreeTR->GetEntry(it--);
1317 Int_t nh = fTmpTrackReferences->GetEntries();
1319 // Loop over reference hits and find primary labels
1320 for (Int_t ih = 0; ih < nh; ih++) {
1321 AliTrackReference* tr = (AliTrackReference*) fTmpTrackReferences->At(ih);
1322 Int_t label = tr->Label();
1324 Int_t nref = fTrackReferences->GetEntriesFast();
1325 TClonesArray &lref = *fTrackReferences;
1326 new(lref[nref]) AliTrackReference(*tr);
1331 fTrackReferences->Clear();
1335 if (ifills != stack->GetNtrack())
1336 AliWarning(Form("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, stack->GetNtrack()));
1340 fTmpFileTR->Close();
1342 delete fTmpTrackReferences;
1343 fTmpTrackReferences = 0;
1344 gSystem->Exec("rm -rf TrackRefsTmp.root");