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 Revision 1.51 2001/02/02 15:16:20 morsch
19 SetHighWaterMark method added to mark last particle in event.
21 Revision 1.50 2001/01/27 10:32:00 hristov
22 Leave the loop when primaries are filled (I.Hrivnacova)
24 Revision 1.49 2001/01/26 19:58:48 hristov
25 Major upgrade of AliRoot code
27 Revision 1.48 2001/01/17 10:50:50 hristov
28 Corrections to destructors
30 Revision 1.47 2000/12/18 10:44:01 morsch
31 Possibility to set field map by passing pointer to objet of type AliMagF via
34 gAlice->SetField(new AliMagFCM("Map2", "$(ALICE_ROOT)/data/field01.dat",2,1.,10.));
36 Revision 1.46 2000/12/14 19:29:27 fca
37 galice.cuts was not read any more
39 Revision 1.45 2000/11/30 07:12:49 alibrary
40 Introducing new Rndm and QA classes
42 Revision 1.44 2000/10/26 13:58:59 morsch
43 Add possibility to choose the lego generator (of type AliGeneratorLego or derived) when running
44 RunLego(). Default is the base class AliGeneratorLego.
46 Revision 1.43 2000/10/09 09:43:17 fca
47 Special remapping of hits for TPC and TRD. End-of-primary action introduced
49 Revision 1.42 2000/10/02 21:28:14 fca
50 Removal of useless dependecies via forward declarations
52 Revision 1.41 2000/07/13 16:19:09 fca
53 Mainly coding conventions + some small bug fixes
55 Revision 1.40 2000/07/12 08:56:25 fca
56 Coding convention correction and warning removal
58 Revision 1.39 2000/07/11 18:24:59 fca
59 Coding convention corrections + few minor bug fixes
61 Revision 1.38 2000/06/20 13:05:45 fca
62 Writing down the TREE headers before job starts
64 Revision 1.37 2000/06/09 20:05:11 morsch
65 Introduce possibility to chose magnetic field version 3: AliMagFDM + field02.dat
67 Revision 1.36 2000/06/08 14:03:58 hristov
68 Only one initializer for a default argument
70 Revision 1.35 2000/06/07 10:13:14 hristov
71 Delete only existent objects.
73 Revision 1.34 2000/05/18 10:45:38 fca
74 Delete Particle Factory properly
76 Revision 1.33 2000/05/16 13:10:40 fca
77 New method IsNewTrack and fix for a problem in Father-Daughter relations
79 Revision 1.32 2000/04/27 10:38:21 fca
80 Correct termination of Lego Run and introduce Lego getter in AliRun
82 Revision 1.31 2000/04/26 10:17:32 fca
83 Changes in Lego for G4 compatibility
85 Revision 1.30 2000/04/18 19:11:40 fca
86 Introduce variable Config.C function signature
88 Revision 1.29 2000/04/07 11:12:34 fca
89 G4 compatibility changes
91 Revision 1.28 2000/04/05 06:51:06 fca
92 Workaround for an HP compiler problem
94 Revision 1.27 2000/03/22 18:08:07 fca
95 Rationalisation of the virtual MC interfaces
97 Revision 1.26 2000/03/22 13:42:26 fca
98 SetGenerator does not replace an existing generator, ResetGenerator does
100 Revision 1.25 2000/02/23 16:25:22 fca
101 AliVMC and AliGeant3 classes introduced
102 ReadEuclid moved from AliRun to AliModule
104 Revision 1.24 2000/01/19 17:17:20 fca
105 Introducing a list of lists of hits -- more hits allowed for detector now
107 Revision 1.23 1999/12/03 11:14:31 fca
108 Fixing previous wrong checking
110 Revision 1.21 1999/11/25 10:40:08 fca
111 Fixing daughters information also in primary tracks
113 Revision 1.20 1999/10/04 18:08:49 fca
114 Adding protection against inconsistent Euclid files
116 Revision 1.19 1999/09/29 07:50:40 fca
117 Introduction of the Copyright and cvs Log
121 ///////////////////////////////////////////////////////////////////////////////
123 // Control class for Alice C++ //
124 // Only one single instance of this class exists. //
125 // The object is created in main program aliroot //
126 // and is pointed by the global gAlice. //
128 // -Supports the list of all Alice Detectors (fModules). //
129 // -Supports the list of particles (fParticles). //
130 // -Supports the Trees. //
131 // -Supports the geometry. //
132 // -Supports the event display. //
135 <img src="picts/AliRunClass.gif">
140 <img src="picts/alirun.gif">
144 ///////////////////////////////////////////////////////////////////////////////
149 #include <iostream.h>
157 #include <TObjectTable.h>
159 #include <TGeometry.h>
161 #include "TBrowser.h"
163 #include "TParticle.h"
165 #include "AliDisplay.h"
168 #include "AliMagFC.h"
169 #include "AliMagFCM.h"
170 #include "AliMagFDM.h"
172 #include "TRandom3.h"
174 #include "AliGenerator.h"
175 #include "AliLegoGenerator.h"
177 #include "AliDetector.h"
181 static AliHeader *gAliHeader;
185 //_____________________________________________________________________________
189 // Default constructor for AliRun
214 fPDGDB = 0; //Particle factory object!
216 fConfigFunction = "\0";
219 fTransParName = "\0";
220 fBaseFileName = "\0";
222 fParticleMap = new TObjArray(10000);
225 //_____________________________________________________________________________
226 AliRun::AliRun(const char *name, const char *title)
230 // Constructor for the main processor.
231 // Creates the geometry
232 // Creates the list of Detectors.
233 // Creates the list of particles.
250 fConfigFunction = "Config();";
252 // Set random number generator
253 gRandom = fRandom = new TRandom3();
255 if (gSystem->Getenv("CONFIG_SEED")) {
256 gRandom->SetSeed((UInt_t)atoi(gSystem->Getenv("CONFIG_SEED")));
259 gROOT->GetListOfBrowsables()->Add(this,name);
261 // create the support list for the various Detectors
262 fModules = new TObjArray(77);
264 // Create the TNode geometry for the event display
266 BuildSimpleGeometry();
276 // Create the particle stack
277 fParticles = new TClonesArray("TParticle",1000);
281 // Create default mag field
286 // Prepare the tracking medium lists
287 fImedia = new TArrayI(1000);
288 for(i=0;i<1000;i++) (*fImedia)[i]=-99;
291 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
293 // Create HitLists list
294 fHitLists = new TList();
297 fBaseFileName = "\0";
299 fParticleMap = new TObjArray(10000);
303 //_____________________________________________________________________________
307 // Default AliRun destructor
327 fParticles->Delete();
335 //_____________________________________________________________________________
336 void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
339 // Add a hit to detector id
341 TObjArray &dets = *fModules;
342 if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
345 //_____________________________________________________________________________
346 void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
349 // Add digit to detector id
351 TObjArray &dets = *fModules;
352 if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
355 //_____________________________________________________________________________
356 void AliRun::Browse(TBrowser *b)
359 // Called when the item "Run" is clicked on the left pane
360 // of the Root browser.
361 // It displays the Root Trees and all detectors.
363 if (fTreeK) b->Add(fTreeK,fTreeK->GetName());
364 if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
365 if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
366 if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
367 if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
368 if (fTreeS) b->Add(fTreeS,fTreeS->GetName());
370 TIter next(fModules);
372 while((detector = (AliModule*)next())) {
373 b->Add(detector,detector->GetName());
375 b->Add(fMCQA,"AliMCQA");
378 //_____________________________________________________________________________
382 // Initialize Alice geometry
387 //_____________________________________________________________________________
388 void AliRun::BuildSimpleGeometry()
391 // Create a simple TNode geometry used by Root display engine
393 // Initialise geometry
395 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
396 new TMaterial("void","Vacuum",0,0,0); //Everything is void
397 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
398 brik->SetVisibility(0);
399 new TNode("alice","alice","S_alice");
402 //_____________________________________________________________________________
403 void AliRun::CleanDetectors()
406 // Clean Detectors at the end of event
408 TIter next(fModules);
410 while((detector = (AliModule*)next())) {
411 detector->FinishEvent();
415 //_____________________________________________________________________________
416 void AliRun::CleanParents()
419 // Clean Particles stack.
420 // Set parent/daughter relations
422 TObjArray &particles = *fParticleMap;
425 for(i=0; i<fHgwmk+1; i++) {
426 part = (TParticle *)particles.At(i);
427 if(part) if(!part->TestBit(kDaughtersBit)) {
428 part->SetFirstDaughter(-1);
429 part->SetLastDaughter(-1);
434 //_____________________________________________________________________________
435 Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
438 // Return the distance from the mouse to the AliRun object
444 //_____________________________________________________________________________
445 void AliRun::DumpPart (Int_t i) const
448 // Dumps particle i in the stack
450 ((TParticle*) (*fParticleMap)[i])->Print();
453 //_____________________________________________________________________________
454 void AliRun::DumpPStack () const
457 // Dumps the particle stack
459 TObjArray &particles = *fParticleMap;
461 "\n\n=======================================================================\n");
462 for (Int_t i=0;i<fNtrack;i++)
464 printf("-> %d ",i); ((TParticle*) particles[i])->Print();
465 printf("--------------------------------------------------------------\n");
468 "\n=======================================================================\n\n");
471 void AliRun::SetField(AliMagF* magField)
473 // Set Magnetic Field Map
478 //_____________________________________________________________________________
479 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
480 Float_t maxField, char* filename)
483 // Set magnetic field parameters
484 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
485 // version Magnetic field map version (only 1 active now)
486 // scale Scale factor for the magnetic field
487 // maxField Maximum value for the magnetic field
490 // --- Sanity check on mag field flags
491 if(fField) delete fField;
493 fField = new AliMagFC("Map1"," ",type,scale,maxField);
494 } else if(version<=2) {
495 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
497 } else if(version==3) {
498 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
501 Warning("SetField","Invalid map %d\n",version);
505 //_____________________________________________________________________________
506 void AliRun::PreTrack()
508 TObjArray &dets = *fModules;
511 for(Int_t i=0; i<=fNdets; i++)
512 if((module = (AliModule*)dets[i]))
518 //_____________________________________________________________________________
519 void AliRun::PostTrack()
521 TObjArray &dets = *fModules;
524 for(Int_t i=0; i<=fNdets; i++)
525 if((module = (AliModule*)dets[i]))
529 //_____________________________________________________________________________
530 void AliRun::FinishPrimary()
533 // Called at the end of each primary track
536 // static Int_t count=0;
537 // const Int_t times=10;
538 // This primary is finished, purify stack
541 TIter next(fModules);
543 while((detector = (AliModule*)next())) {
544 detector->FinishPrimary();
547 // Write out hits if any
548 if (gAlice->TreeH()) {
549 gAlice->TreeH()->Fill();
556 // if(++count%times==1) gObjectTable->Print();
559 //_____________________________________________________________________________
560 void AliRun::FinishEvent()
563 // Called at the end of the event.
567 if(fLego) fLego->FinishEvent();
569 //Update the energy deposit tables
571 for(i=0;i<fEventEnergy.GetSize();i++) {
572 fSummEnergy[i]+=fEventEnergy[i];
573 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
575 fEventEnergy.Reset();
577 // Clean detector information
580 // Write out the kinematics
585 for(i=0; i<fHgwmk+1; ++i) if((part=fParticleMap->At(i))) {
586 fParticleBuffer = (TParticle*) part;
587 fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
589 (*fParticleMap)[i]=0;
590 } else //printf("Why = 0 part # %d?\n",i);
594 // Write out the digits
605 // Write out reconstructed clusters
610 // Write out the event Header information
611 if (fTreeE) fTreeE->Fill();
616 // Write Tree headers
617 if (fTreeK) fTreeK->Write(0,TObject::kOverwrite);
618 if (fTreeH) fTreeH->Write(0,TObject::kOverwrite);
619 if (fTreeD) fTreeD->Write(0,TObject::kOverwrite);
620 if (fTreeR) fTreeR->Write(0,TObject::kOverwrite);
621 if (fTreeS) fTreeS->Write(0,TObject::kOverwrite);
626 //_____________________________________________________________________________
627 void AliRun::FinishRun()
630 // Called at the end of the run.
634 if(fLego) fLego->FinishRun();
636 // Clean detector information
637 TIter next(fModules);
639 while((detector = (AliModule*)next())) {
640 detector->FinishRun();
643 //Output energy summary tables
646 TFile *file = fTreeE->GetCurrentFile();
650 fTreeE->Write(0,TObject::kOverwrite);
652 // Write AliRun info and all detectors parameters
655 // Clean tree information
657 delete fTreeK; fTreeK = 0;
660 delete fTreeH; fTreeH = 0;
663 delete fTreeD; fTreeD = 0;
666 delete fTreeR; fTreeR = 0;
669 delete fTreeE; fTreeE = 0;
676 //_____________________________________________________________________________
677 void AliRun::FlagTrack(Int_t track)
680 // Flags a track and all its family tree to be kept
687 particle=(TParticle*)fParticleMap->At(curr);
689 // If the particle is flagged the three from here upward is saved already
690 if(particle->TestBit(kKeepBit)) return;
692 // Save this particle
693 particle->SetBit(kKeepBit);
695 // Move to father if any
696 if((curr=particle->GetFirstMother())==-1) return;
700 //_____________________________________________________________________________
701 void AliRun::EnergySummary()
704 // Print summary of deposited energy
710 Int_t kn, i, left, j, id;
711 const Float_t kzero=0;
712 Int_t ievent=fHeader.GetEvent()+1;
714 // Energy loss information
716 printf("***************** Energy Loss Information per event (GEV) *****************\n");
717 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
720 fEventEnergy[ndep]=kn;
725 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
728 fSummEnergy[ndep]=ed;
729 fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,kzero));
734 for(kn=0;kn<(ndep-1)/3+1;kn++) {
736 for(i=0;i<(3<left?3:left);i++) {
738 id=Int_t (fEventEnergy[j]+0.1);
739 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
744 // Relative energy loss in different detectors
745 printf("******************** Relative Energy Loss per event ********************\n");
746 printf("Total energy loss per event %10.3f GeV\n",edtot);
747 for(kn=0;kn<(ndep-1)/5+1;kn++) {
749 for(i=0;i<(5<left?5:left);i++) {
751 id=Int_t (fEventEnergy[j]+0.1);
752 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
756 for(kn=0;kn<75;kn++) printf("*");
760 // Reset the TArray's
761 // fEventEnergy.Set(0);
762 // fSummEnergy.Set(0);
763 // fSum2Energy.Set(0);
766 //_____________________________________________________________________________
767 AliModule *AliRun::GetModule(const char *name) const
770 // Return pointer to detector from name
772 return (AliModule*)fModules->FindObject(name);
775 //_____________________________________________________________________________
776 AliDetector *AliRun::GetDetector(const char *name) const
779 // Return pointer to detector from name
781 return (AliDetector*)fModules->FindObject(name);
784 //_____________________________________________________________________________
785 Int_t AliRun::GetModuleID(const char *name) const
788 // Return galice internal detector identifier from name
791 TObject *mod=fModules->FindObject(name);
792 if(mod) i=fModules->IndexOf(mod);
796 //_____________________________________________________________________________
797 Int_t AliRun::GetEvent(Int_t event)
800 // Connect the Trees Kinematics and Hits for event # event
801 // Set branch addresses
804 // Reset existing structures
810 // Delete Trees already connected
811 if (fTreeK) delete fTreeK;
812 if (fTreeH) delete fTreeH;
813 if (fTreeD) delete fTreeD;
814 if (fTreeR) delete fTreeR;
815 if (fTreeS) delete fTreeS;
817 // Get header from file
818 if(fTreeE) fTreeE->GetEntry(event);
819 else Error("GetEvent","Cannot file Header Tree\n");
820 TFile *file = fTreeE->GetCurrentFile();
824 // Get Kine Tree from file
826 sprintf(treeName,"TreeK%d",event);
827 fTreeK = (TTree*)gDirectory->Get(treeName);
828 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
829 else Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
830 // Create the particle stack
831 if(!fParticles) fParticles = new TClonesArray("TParticle",1000);
832 // Build the pointer list
834 fParticleMap->Clear();
835 fParticleMap->Expand(fTreeK->GetEntries());
837 fParticleMap = new TObjArray(fTreeK->GetEntries());
841 // Get Hits Tree header from file
842 sprintf(treeName,"TreeH%d",event);
843 fTreeH = (TTree*)gDirectory->Get(treeName);
845 Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
850 // Get Digits Tree header from file
851 sprintf(treeName,"TreeD%d",event);
852 fTreeD = (TTree*)gDirectory->Get(treeName);
854 // Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
859 // Get SDigits Tree header from file
860 sprintf(treeName,"TreeS%d",event);
861 fTreeS = (TTree*)gDirectory->Get(treeName);
863 // Warning("GetEvent","cannot find SDigits Tree for event:%d\n",event);
868 // Get Reconstruct Tree header from file
869 sprintf(treeName,"TreeR%d",event);
870 fTreeR = (TTree*)gDirectory->Get(treeName);
872 // printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
877 // Set Trees branch addresses
878 TIter next(fModules);
880 while((detector = (AliModule*)next())) {
881 detector->SetTreeAddress();
884 fNtrack = Int_t (fTreeK->GetEntries());
888 //_____________________________________________________________________________
889 TGeometry *AliRun::GetGeometry()
892 // Import Alice geometry from current file
893 // Return pointer to geometry object
895 if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
897 // Unlink and relink nodes in detectors
898 // This is bad and there must be a better way...
901 TIter next(fModules);
903 while((detector = (AliModule*)next())) {
904 TList *dnodes=detector->Nodes();
907 for ( j=0; j<dnodes->GetSize(); j++) {
908 node = (TNode*) dnodes->At(j);
909 node1 = fGeometry->GetNode(node->GetName());
910 dnodes->Remove(node);
911 dnodes->AddAt(node1,j);
917 //_____________________________________________________________________________
918 void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
919 Float_t &e, Float_t *vpos, Float_t *polar,
923 // Return next track from stack of particles
928 for(Int_t i=fNtrack-1; i>=0; i--) {
929 track=(TParticle*) fParticleMap->At(i);
930 if(track) if(!track->TestBit(kDoneBit)) {
932 // The track exists and has not yet been processed
934 ipart=track->GetPdgCode();
942 track->GetPolarisation(pol);
947 track->SetBit(kDoneBit);
953 // stop and start timer when we start a primary track
954 Int_t nprimaries = fHeader.GetNprimary();
955 if (fCurrent >= nprimaries) return;
956 if (fCurrent < nprimaries-1) {
958 track=(TParticle*) fParticleMap->At(fCurrent+1);
959 // track->SetProcessTime(fTimer.CpuTime());
964 //_____________________________________________________________________________
965 Int_t AliRun::GetPrimary(Int_t track) const
968 // return number of primary that has generated track
976 part = (TParticle *)fParticleMap->At(current);
977 parent=part->GetFirstMother();
978 if(parent<0) return current;
982 //_____________________________________________________________________________
983 void AliRun::InitMC(const char *setup)
986 // Initialize the Alice setup
990 Warning("Init","Cannot initialise AliRun twice!\n");
994 OpenBaseFile("recreate");
996 gROOT->LoadMacro(setup);
997 gInterpreter->ProcessLine(fConfigFunction.Data());
1000 gMC->DefineParticles(); //Create standard MC particles
1002 TObject *objfirst, *objlast;
1004 fNdets = fModules->GetLast()+1;
1007 //=================Create Materials and geometry
1010 // Added also after in case of interactive initialisation of modules
1011 fNdets = fModules->GetLast()+1;
1013 TIter next(fModules);
1014 AliModule *detector;
1015 while((detector = (AliModule*)next())) {
1016 detector->SetTreeAddress();
1017 objlast = gDirectory->GetList()->Last();
1019 // Add Detector histograms in Detector list of histograms
1020 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
1021 else objfirst = gDirectory->GetList()->First();
1023 detector->Histograms()->Add(objfirst);
1024 objfirst = gDirectory->GetList()->After(objfirst);
1027 ReadTransPar(); //Read the cuts for all materials
1029 MediaTable(); //Build the special IMEDIA table
1031 //Initialise geometry deposition table
1032 fEventEnergy.Set(gMC->NofVolumes()+1);
1033 fSummEnergy.Set(gMC->NofVolumes()+1);
1034 fSum2Energy.Set(gMC->NofVolumes()+1);
1036 //Compute cross-sections
1037 gMC->BuildPhysics();
1039 //Write Geometry object to current file.
1044 fMCQA = new AliMCQA(fNdets);
1047 // Save stuff at the beginning of the file to avoid file corruption
1051 //_____________________________________________________________________________
1052 void AliRun::MediaTable()
1055 // Built media table to get from the media number to
1058 Int_t kz, nz, idt, lz, i, k, ind;
1060 TObjArray &dets = *gAlice->Detectors();
1063 // For all detectors
1064 for (kz=0;kz<fNdets;kz++) {
1065 // If detector is defined
1066 if((det=(AliModule*) dets[kz])) {
1067 TArrayI &idtmed = *(det->GetIdtmed());
1068 for(nz=0;nz<100;nz++) {
1069 // Find max and min material number
1070 if((idt=idtmed[nz])) {
1071 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
1072 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
1075 if(det->LoMedium() > det->HiMedium()) {
1076 det->LoMedium() = 0;
1077 det->HiMedium() = 0;
1079 if(det->HiMedium() > fImedia->GetSize()) {
1080 Error("MediaTable","Increase fImedia from %d to %d",
1081 fImedia->GetSize(),det->HiMedium());
1084 // Tag all materials in rage as belonging to detector kz
1085 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
1092 // Print summary table
1093 printf(" Traking media ranges:\n");
1094 for(i=0;i<(fNdets-1)/6+1;i++) {
1095 for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
1097 det=(AliModule*)dets[ind];
1099 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
1102 printf(" %6s: %3d -> %3d;","NULL",0,0);
1108 //____________________________________________________________________________
1109 void AliRun::SetGenerator(AliGenerator *generator)
1112 // Load the event generator
1114 if(!fGenerator) fGenerator = generator;
1117 //____________________________________________________________________________
1118 void AliRun::ResetGenerator(AliGenerator *generator)
1121 // Load the event generator
1125 Warning("ResetGenerator","Replacing generator %s with %s\n",
1126 fGenerator->GetName(),generator->GetName());
1128 Warning("ResetGenerator","Replacing generator %s with NULL\n",
1129 fGenerator->GetName());
1130 fGenerator = generator;
1133 //____________________________________________________________________________
1134 void AliRun::SetTransPar(char *filename)
1136 fTransParName = filename;
1139 //____________________________________________________________________________
1140 void AliRun::SetBaseFile(char *filename)
1142 fBaseFileName = *filename;
1145 //____________________________________________________________________________
1146 void AliRun::OpenBaseFile(const char *option)
1148 if(!strlen(fBaseFileName.Data())) {
1149 const char *filename;
1150 if ((filename=gSystem->Getenv("CONFIG_FILE"))) {
1151 fBaseFileName=filename;
1153 fBaseFileName="galice.root";
1156 TFile *rootfile = new TFile(fBaseFileName.Data(),option);
1157 rootfile->SetCompressionLevel(2);
1160 //____________________________________________________________________________
1161 void AliRun::ReadTransPar()
1164 // Read filename to set the transport parameters
1168 const Int_t kncuts=10;
1169 const Int_t knflags=11;
1170 const Int_t knpars=kncuts+knflags;
1171 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
1172 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
1173 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
1174 "MULS","PAIR","PHOT","RAYL"};
1178 Float_t cut[kncuts];
1179 Int_t flag[knflags];
1180 Int_t i, itmed, iret, ktmed, kz;
1183 // See whether the file is there
1184 filtmp=gSystem->ExpandPathName(fTransParName.Data());
1185 lun=fopen(filtmp,"r");
1188 Warning("ReadTransPar","File %s does not exist!\n",fTransParName.Data());
1192 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1193 printf(" *%59s\n","*");
1194 printf(" * Please check carefully what you are doing!%10s\n","*");
1195 printf(" *%59s\n","*");
1198 // Initialise cuts and flags
1199 for(i=0;i<kncuts;i++) cut[i]=-99;
1200 for(i=0;i<knflags;i++) flag[i]=-99;
1202 for(i=0;i<256;i++) line[i]='\0';
1203 // Read up to the end of line excluded
1204 iret=fscanf(lun,"%[^\n]",line);
1208 printf(" *%59s\n","*");
1209 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1212 // Read the end of line
1215 if(line[0]=='*') continue;
1217 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",
1218 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1219 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1220 &flag[8],&flag[9],&flag[10]);
1224 Warning("ReadTransPar","Error reading file %s\n",fTransParName.Data());
1227 // Check that the module exist
1228 AliModule *mod = GetModule(detName);
1230 // Get the array of media numbers
1231 TArrayI &idtmed = *mod->GetIdtmed();
1232 // Check that the tracking medium code is valid
1233 if(0<=itmed && itmed < 100) {
1234 ktmed=idtmed[itmed];
1236 Warning("ReadTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1239 // Set energy thresholds
1240 for(kz=0;kz<kncuts;kz++) {
1242 printf(" * %-6s set to %10.3E for tracking medium code %4d for %s\n",
1243 kpars[kz],cut[kz],itmed,mod->GetName());
1244 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
1247 // Set transport mechanisms
1248 for(kz=0;kz<knflags;kz++) {
1250 printf(" * %-6s set to %10d for tracking medium code %4d for %s\n",
1251 kpars[kncuts+kz],flag[kz],itmed,mod->GetName());
1252 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
1256 Warning("ReadTransPar","Invalid medium code %d *\n",itmed);
1260 Warning("ReadTransPar","Module %s not present\n",detName);
1266 //_____________________________________________________________________________
1267 void AliRun::MakeBranchInTree(TTree *tree, const char* name, void* address, Int_t size, char *file)
1270 printf("* MakeBranch * Making Branch %s \n",name);
1272 TBranch *branch = tree->Branch(name,address,size);
1275 TDirectory *cwd = gDirectory;
1276 branch->SetFile(file);
1277 TIter next( branch->GetListOfBranches());
1278 while ((branch=(TBranch*)next())) {
1279 branch->SetFile(file);
1282 printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);
1287 //_____________________________________________________________________________
1288 void AliRun::MakeBranchInTree(TTree *tree, const char* name, const char *classname, void* address, Int_t size, Int_t splitlevel, char *file)
1290 TDirectory *cwd = gDirectory;
1291 TBranch *branch = tree->Branch(name,classname,address,size,splitlevel);
1293 printf("* MakeBranch * Making Branch %s \n",name);
1295 branch->SetFile(file);
1296 TIter next( branch->GetListOfBranches());
1297 while ((branch=(TBranch*)next())) {
1298 branch->SetFile(file);
1301 printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);
1305 //_____________________________________________________________________________
1306 void AliRun::MakeTree(Option_t *option, char *file)
1309 // Create the ROOT trees
1310 // Loop on all detectors to create the Root branch (if any)
1316 char *oK = strstr(option,"K");
1317 char *oH = strstr(option,"H");
1318 char *oE = strstr(option,"E");
1319 char *oD = strstr(option,"D");
1320 char *oR = strstr(option,"R");
1321 char *oS = strstr(option,"S");
1324 if (oK && !fTreeK) {
1325 sprintf(hname,"TreeK%d",fEvent);
1326 fTreeK = new TTree(hname,"Kinematics");
1327 // Create a branch for particles
1328 MakeBranchInTree(fTreeK,
1329 "Particles", "TParticle", &fParticleBuffer, 4000, 1, file) ;
1332 if (oH && !fTreeH) {
1333 sprintf(hname,"TreeH%d",fEvent);
1334 fTreeH = new TTree(hname,"Hits");
1335 fTreeH->SetAutoSave(1000000000); //no autosave
1338 if (oD && !fTreeD) {
1339 sprintf(hname,"TreeD%d",fEvent);
1340 fTreeD = new TTree(hname,"Digits");
1343 if (oS && !fTreeS) {
1344 sprintf(hname,"TreeS%d",fEvent);
1345 fTreeS = new TTree(hname,"SDigits");
1348 if (oR && !fTreeR) {
1349 sprintf(hname,"TreeR%d",fEvent);
1350 fTreeR = new TTree(hname,"Reconstruction");
1353 if (oE && !fTreeE) {
1354 fTreeE = new TTree("TE","Header");
1355 // Create a branch for Header
1356 MakeBranchInTree(fTreeE,
1357 "Header", "AliHeader", &gAliHeader, 4000, 1, file) ;
1362 // Create a branch for hits/digits for each detector
1363 // Each branch is a TClonesArray. Each data member of the Hits classes
1364 // will be in turn a subbranch of the detector master branch
1365 TIter next(fModules);
1366 AliModule *detector;
1367 while((detector = (AliModule*)next())) {
1368 if (oH || oR) detector->MakeBranch(option,file);
1372 //_____________________________________________________________________________
1373 Int_t AliRun::PurifyKine(Int_t lastSavedTrack, Int_t nofTracks)
1376 // PurifyKine with external parameters
1378 fHgwmk = lastSavedTrack;
1379 fNtrack = nofTracks;
1384 //_____________________________________________________________________________
1385 TParticle* AliRun::Particle(Int_t i)
1387 if(!(*fParticleMap)[i]) {
1388 Int_t nentries = fParticles->GetEntries();
1389 fTreeK->GetEntry(fParticleFileMap[i]);
1390 new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
1391 fParticleMap->AddAt((*fParticles)[nentries],i);
1393 return (TParticle *) (*fParticleMap)[i];
1396 //_____________________________________________________________________________
1397 void AliRun::PurifyKine()
1400 // Compress kinematic tree keeping only flagged particles
1401 // and renaming the particle id's in all the hits
1403 // TClonesArray &particles = *fParticles;
1404 TObjArray &particles = *fParticleMap;
1405 int nkeep=fHgwmk+1, parent, i;
1406 TParticle *part, *father;
1407 TArrayI map(particles.GetLast()+1);
1409 // Save in Header total number of tracks before compression
1410 fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
1412 // If no tracks generated return now
1413 if(fHgwmk+1 == fNtrack) return;
1415 Int_t toshrink = fNtrack-fHgwmk-1;
1417 // First pass, invalid Daughter information
1418 for(i=0; i<fNtrack; i++) {
1419 // Preset map, to be removed later
1420 if(i<=fHgwmk) map[i]=i ;
1423 // particles.UncheckedAt(i)->ResetBit(kDaughtersBit);
1424 if((part=(TParticle*) particles.At(i))) part->ResetBit(kDaughtersBit);
1427 // Invalid daughter information for the parent of the first particle
1428 // generated. This may or may not be the current primary according to
1429 // whether decays have been recorded among the primaries
1430 part = (TParticle *)particles.At(fHgwmk+1);
1431 particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
1432 // Second pass, build map between old and new numbering
1433 for(i=fHgwmk+1; i<fNtrack; i++) {
1434 if(particles.At(i)->TestBit(kKeepBit)) {
1436 // This particle has to be kept
1438 // If old and new are different, have to move the pointer
1439 if(i!=nkeep) particles[nkeep]=particles.At(i);
1440 part = (TParticle*) particles.At(nkeep);
1442 // as the parent is always *before*, it must be already
1443 // in place. This is what we are checking anyway!
1444 if((parent=part->GetFirstMother())>fHgwmk)
1445 if(map[parent]==-99) Fatal("PurifyKine","map[%d] = -99!\n",parent);
1446 else part->SetFirstMother(map[parent]);
1452 // Fix daughters information
1453 for (i=fHgwmk+1; i<nkeep; i++) {
1454 part = (TParticle *)particles.At(i);
1455 parent = part->GetFirstMother();
1457 father = (TParticle *)particles.At(parent);
1458 if(father->TestBit(kDaughtersBit)) {
1460 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
1461 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
1463 // Initialise daughters info for first pass
1464 father->SetFirstDaughter(i);
1465 father->SetLastDaughter(i);
1466 father->SetBit(kDaughtersBit);
1471 // Now loop on all registered hit lists
1472 TIter next(fHitLists);
1473 TCollection *hitList;
1474 while((hitList = (TCollection*)next())) {
1475 TIter nexthit(hitList);
1477 while((hit = (AliHit*)nexthit())) {
1478 hit->SetTrack(map[hit->GetTrack()]);
1483 // This for detectors which have a special mapping mechanism
1484 // for hits, such as TPC and TRD
1487 TIter nextmod(fModules);
1488 AliModule *detector;
1489 while((detector = (AliModule*)nextmod())) {
1490 detector->RemapTrackHitIDs(map.GetArray());
1493 // Now the output bit, from fHgwmk to nkeep we write everything and we erase
1494 if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
1497 for (i=fHgwmk+1; i<nkeep; ++i) {
1498 fParticleBuffer = (TParticle*) particles.At(i);
1499 fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
1504 for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
1506 fLoadPoint-=toshrink;
1507 for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
1514 //_____________________________________________________________________________
1515 void AliRun::BeginEvent()
1518 // Reset all Detectors & kinematics & trees
1525 fLego->BeginEvent();
1535 // Initialise event header
1536 fHeader.Reset(fRun,fEvent);
1540 sprintf(hname,"TreeK%d",fEvent);
1541 fTreeK->SetName(hname);
1545 sprintf(hname,"TreeH%d",fEvent);
1546 fTreeH->SetName(hname);
1550 sprintf(hname,"TreeD%d",fEvent);
1551 fTreeD->SetName(hname);
1555 sprintf(hname,"TreeS%d",fEvent);
1556 fTreeS->SetName(hname);
1560 sprintf(hname,"TreeR%d",fEvent);
1561 fTreeR->SetName(hname);
1564 //_____________________________________________________________________________
1565 void AliRun::ResetDigits()
1568 // Reset all Detectors digits
1570 TIter next(fModules);
1571 AliModule *detector;
1572 while((detector = (AliModule*)next())) {
1573 detector->ResetDigits();
1577 //_____________________________________________________________________________
1578 void AliRun::ResetSDigits()
1581 // Reset all Detectors digits
1583 TIter next(fModules);
1584 AliModule *detector;
1585 while((detector = (AliModule*)next())) {
1586 detector->ResetSDigits();
1590 //_____________________________________________________________________________
1591 void AliRun::ResetHits()
1594 // Reset all Detectors hits
1596 TIter next(fModules);
1597 AliModule *detector;
1598 while((detector = (AliModule*)next())) {
1599 detector->ResetHits();
1603 //_____________________________________________________________________________
1604 void AliRun::ResetPoints()
1607 // Reset all Detectors points
1609 TIter next(fModules);
1610 AliModule *detector;
1611 while((detector = (AliModule*)next())) {
1612 detector->ResetPoints();
1616 //_____________________________________________________________________________
1617 void AliRun::RunMC(Int_t nevent, const char *setup)
1620 // Main function to be called to process a galice run
1622 // Root > gAlice.Run();
1623 // a positive number of events will cause the finish routine
1627 // check if initialisation has been done
1628 if (!fInitDone) InitMC(setup);
1630 // Create the Root Tree with one branch per detector
1632 if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
1634 MakeTree("K","Kine.root");
1635 MakeTree("H","Hits.root");
1636 MakeTree("R","Reco.root");
1641 gMC->ProcessRun(nevent);
1643 // End of this run, close files
1644 if(nevent>0) FinishRun();
1647 //_____________________________________________________________________________
1649 void AliRun::Hits2Digits(const char *selected)
1651 Hits2SDigits(selected);
1652 SDigits2Digits(selected);
1655 //_____________________________________________________________________________
1657 void AliRun::Hits2SDigits(const char *selected)
1660 // Main function to be called to convert hits to digits.
1662 gAlice->GetEvent(0);
1664 TObjArray *detectors = gAlice->Detectors();
1666 TIter next(detectors);
1668 AliDetector *detector;
1670 TDirectory *cwd = gDirectory;
1674 while((detector = (AliDetector*)next())) {
1676 if (strcmp(detector->GetName(),selected)) continue;
1678 if (detector->IsActive()){
1679 if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
1681 cout << "Processing " << detector->GetName() << "..." << endl;
1682 char * outFile = new char[strlen (detector->GetName())+18];
1683 sprintf(outFile,"SDigits.%s.root",detector->GetName());
1684 detector->MakeBranch("S",outFile);
1687 detector->MakeBranch("S");
1690 detector->Hits2SDigits();
1695 //_____________________________________________________________________________
1697 void AliRun::SDigits2Digits(const char *selected)
1700 // Main function to be called to convert hits to digits.
1702 gAlice->GetEvent(0);
1704 TObjArray *detectors = gAlice->Detectors();
1706 TIter next(detectors);
1708 AliDetector *detector;
1710 TDirectory *cwd = gDirectory;
1714 while((detector = (AliDetector*)next())) {
1716 if (strcmp(detector->GetName(),selected)) continue;
1718 if (detector->IsActive()){
1719 if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
1721 cout << "Processing " << detector->GetName() << "..." << endl;
1722 char * outFile = new char[strlen (detector->GetName())+16];
1723 sprintf(outFile,"Digits.%s.root",detector->GetName());
1724 detector->MakeBranch("D",outFile);
1727 detector->MakeBranch("D");
1730 detector->SDigits2Digits();
1735 //_____________________________________________________________________________
1736 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
1737 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
1738 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
1741 // Generates lego plots of:
1742 // - radiation length map phi vs theta
1743 // - radiation length map phi vs eta
1744 // - interaction length map
1745 // - g/cm2 length map
1747 // ntheta bins in theta, eta
1748 // themin minimum angle in theta (degrees)
1749 // themax maximum angle in theta (degrees)
1751 // phimin minimum angle in phi (degrees)
1752 // phimax maximum angle in phi (degrees)
1753 // rmin minimum radius
1754 // rmax maximum radius
1757 // The number of events generated = ntheta*nphi
1758 // run input parameters in macro setup (default="Config.C")
1760 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1763 <img src="picts/AliRunLego1.gif">
1768 <img src="picts/AliRunLego2.gif">
1773 <img src="picts/AliRunLego3.gif">
1778 // check if initialisation has been done
1779 if (!fInitDone) InitMC(setup);
1780 //Save current generator
1781 AliGenerator *gen=Generator();
1783 // Set new generator
1784 if (!gener) gener = new AliLegoGenerator();
1785 ResetGenerator(gener);
1787 // Configure Generator
1788 gener->SetRadiusRange(rmin, rmax);
1789 gener->SetZMax(zmax);
1790 gener->SetCoor1Range(nc1, c1min, c1max);
1791 gener->SetCoor2Range(nc2, c2min, c2max);
1794 //Create Lego object
1795 fLego = new AliLego("lego",gener);
1797 //Prepare MC for Lego Run
1802 gMC->ProcessRun(nc1*nc2+1);
1804 // Create only the Root event Tree
1807 // End of this run, close files
1809 // Restore current generator
1810 ResetGenerator(gen);
1811 // Delete Lego Object
1812 delete fLego; fLego=0;
1815 //_____________________________________________________________________________
1816 void AliRun::SetConfigFunction(const char * config)
1819 // Set the signature of the function contained in Config.C to configure
1822 fConfigFunction=config;
1825 //_____________________________________________________________________________
1826 void AliRun::SetCurrentTrack(Int_t track)
1829 // Set current track number
1834 //_____________________________________________________________________________
1835 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1836 Float_t *vpos, Float_t *polar, Float_t tof,
1837 AliMCProcess mech, Int_t &ntr, Float_t weight)
1840 // Load a track on the stack
1842 // done 0 if the track has to be transported
1844 // parent identifier of the parent track. -1 for a primary
1845 // pdg particle code
1846 // pmom momentum GeV/c
1848 // polar polarisation
1849 // tof time of flight in seconds
1850 // mecha production mechanism
1851 // ntr on output the number of the track stored
1853 TClonesArray &particles = *fParticles;
1854 TParticle *particle;
1856 const Int_t kfirstdaughter=-1;
1857 const Int_t klastdaughter=-1;
1859 // const Float_t tlife=0;
1862 // Here we get the static mass
1863 // For MC is ok, but a more sophisticated method could be necessary
1864 // if the calculated mass is required
1865 // also, this method is potentially dangerous if the mass
1866 // used in the MC is not the same of the PDG database
1868 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
1869 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
1870 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
1872 //printf("Loading particle %s mass %f ene %f No %d ip %d pos %f %f %f mom %f %f %f kS %d m %s\n",
1873 //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS,mecha);
1875 particle=new(particles[fLoadPoint++]) TParticle(pdg,kS,parent,-1,kfirstdaughter,
1876 klastdaughter,pmom[0],pmom[1],pmom[2],
1877 e,vpos[0],vpos[1],vpos[2],tof);
1878 particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
1879 particle->SetWeight(weight);
1880 particle->SetUniqueID(mech);
1881 if(!done) particle->SetBit(kDoneBit);
1882 // Declare that the daughter information is valid
1883 particle->SetBit(kDaughtersBit);
1884 // Add the particle to the stack
1885 fParticleMap->AddAtAndExpand(particle,fNtrack);
1888 particle=(TParticle*) fParticleMap->At(parent);
1889 particle->SetLastDaughter(fNtrack);
1890 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
1893 // This is a primary track. Set high water mark for this event
1896 // Set also number if primary tracks
1897 fHeader.SetNprimary(fHgwmk+1);
1898 fHeader.SetNtrack(fHgwmk+1);
1903 void AliRun::SetHighWaterMark(const Int_t nt)
1906 // Set high water mark for last track in event
1909 // Set also number if primary tracks
1910 fHeader.SetNprimary(fHgwmk+1);
1911 fHeader.SetNtrack(fHgwmk+1);
1914 //_____________________________________________________________________________
1915 void AliRun::KeepTrack(const Int_t track)
1918 // flags a track to be kept
1920 fParticleMap->At(track)->SetBit(kKeepBit);
1923 //_____________________________________________________________________________
1924 void AliRun::StepManager(Int_t id)
1927 // Called at every step during transport
1931 // --- If lego option, do it and leave
1933 fLego->StepManager();
1936 //Update energy deposition tables
1937 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
1939 //Call the appropriate stepping routine;
1940 AliModule *det = (AliModule*)fModules->At(id);
1942 fMCQA->StepManager(id);
1948 //_____________________________________________________________________________
1949 void AliRun::Streamer(TBuffer &R__b)
1951 // Stream an object of class AliRun.
1953 if (R__b.IsReading()) {
1954 if (!gAlice) gAlice = this;
1956 AliRun::Class()->ReadBuffer(R__b, this);
1958 gROOT->GetListOfBrowsables()->Add(this,"Run");
1960 fTreeE = (TTree*)gDirectory->Get("TE");
1961 if (fTreeE) fTreeE->SetBranchAddress("Header", &gAliHeader);
1962 else Error("Streamer","cannot find Header Tree\n");
1963 fTreeE->GetEntry(0);
1967 AliRun::Class()->WriteBuffer(R__b, this);