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.33 2000/05/16 13:10:40 fca
19 New method IsNewTrack and fix for a problem in Father-Daughter relations
21 Revision 1.32 2000/04/27 10:38:21 fca
22 Correct termination of Lego Run and introduce Lego getter in AliRun
24 Revision 1.31 2000/04/26 10:17:32 fca
25 Changes in Lego for G4 compatibility
27 Revision 1.30 2000/04/18 19:11:40 fca
28 Introduce variable Config.C function signature
30 Revision 1.29 2000/04/07 11:12:34 fca
31 G4 compatibility changes
33 Revision 1.28 2000/04/05 06:51:06 fca
34 Workaround for an HP compiler problem
36 Revision 1.27 2000/03/22 18:08:07 fca
37 Rationalisation of the virtual MC interfaces
39 Revision 1.26 2000/03/22 13:42:26 fca
40 SetGenerator does not replace an existing generator, ResetGenerator does
42 Revision 1.25 2000/02/23 16:25:22 fca
43 AliVMC and AliGeant3 classes introduced
44 ReadEuclid moved from AliRun to AliModule
46 Revision 1.24 2000/01/19 17:17:20 fca
47 Introducing a list of lists of hits -- more hits allowed for detector now
49 Revision 1.23 1999/12/03 11:14:31 fca
50 Fixing previous wrong checking
52 Revision 1.21 1999/11/25 10:40:08 fca
53 Fixing daughters information also in primary tracks
55 Revision 1.20 1999/10/04 18:08:49 fca
56 Adding protection against inconsistent Euclid files
58 Revision 1.19 1999/09/29 07:50:40 fca
59 Introduction of the Copyright and cvs Log
63 ///////////////////////////////////////////////////////////////////////////////
65 // Control class for Alice C++ //
66 // Only one single instance of this class exists. //
67 // The object is created in main program aliroot //
68 // and is pointed by the global gAlice. //
70 // -Supports the list of all Alice Detectors (fModules). //
71 // -Supports the list of particles (fParticles). //
72 // -Supports the Trees. //
73 // -Supports the geometry. //
74 // -Supports the event display. //
77 <img src="picts/AliRunClass.gif">
82 <img src="picts/alirun.gif">
86 ///////////////////////////////////////////////////////////////////////////////
94 #include <TObjectTable.h>
96 #include "TParticle.h"
98 #include "AliDisplay.h"
108 static AliHeader *header;
112 //_____________________________________________________________________________
116 // Default constructor for AliRun
140 fPDGDB = 0; //Particle factory object!
142 fConfigFunction = "\0";
145 //_____________________________________________________________________________
146 AliRun::AliRun(const char *name, const char *title)
150 // Constructor for the main processor.
151 // Creates the geometry
152 // Creates the list of Detectors.
153 // Creates the list of particles.
169 fConfigFunction = "Config();";
171 gROOT->GetListOfBrowsables()->Add(this,name);
173 // create the support list for the various Detectors
174 fModules = new TObjArray(77);
176 // Create the TNode geometry for the event display
178 BuildSimpleGeometry();
188 // Create the particle stack
189 fParticles = new TClonesArray("TParticle",100);
193 // Create default mag field
198 // Prepare the tracking medium lists
199 fImedia = new TArrayI(1000);
200 for(i=0;i<1000;i++) (*fImedia)[i]=-99;
203 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
205 // Create HitLists list
206 fHitLists = new TList();
209 //_____________________________________________________________________________
213 // Defaullt AliRun destructor
232 fParticles->Delete();
239 //_____________________________________________________________________________
240 void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
243 // Add a hit to detector id
245 TObjArray &dets = *fModules;
246 if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
249 //_____________________________________________________________________________
250 void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
253 // Add digit to detector id
255 TObjArray &dets = *fModules;
256 if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
259 //_____________________________________________________________________________
260 void AliRun::Browse(TBrowser *b)
263 // Called when the item "Run" is clicked on the left pane
264 // of the Root browser.
265 // It displays the Root Trees and all detectors.
267 if (fTreeK) b->Add(fTreeK,fTreeK->GetName());
268 if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
269 if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
270 if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
271 if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
273 TIter next(fModules);
275 while((detector = (AliModule*)next())) {
276 b->Add(detector,detector->GetName());
280 //_____________________________________________________________________________
284 // Initialize Alice geometry
289 //_____________________________________________________________________________
290 void AliRun::BuildSimpleGeometry()
293 // Create a simple TNode geometry used by Root display engine
295 // Initialise geometry
297 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
298 new TMaterial("void","Vacuum",0,0,0); //Everything is void
299 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
300 brik->SetVisibility(0);
301 new TNode("alice","alice","S_alice");
304 //_____________________________________________________________________________
305 void AliRun::CleanDetectors()
308 // Clean Detectors at the end of event
310 TIter next(fModules);
312 while((detector = (AliModule*)next())) {
313 detector->FinishEvent();
317 //_____________________________________________________________________________
318 void AliRun::CleanParents()
321 // Clean Particles stack.
322 // Set parent/daughter relations
324 TClonesArray &particles = *(gAlice->Particles());
327 for(i=0; i<fNtrack; i++) {
328 part = (TParticle *)particles.UncheckedAt(i);
329 if(!part->TestBit(Daughters_Bit)) {
330 part->SetFirstDaughter(-1);
331 part->SetLastDaughter(-1);
336 //_____________________________________________________________________________
337 Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
340 // Return the distance from the mouse to the AliRun object
346 //_____________________________________________________________________________
347 void AliRun::DumpPart (Int_t i)
350 // Dumps particle i in the stack
352 TClonesArray &particles = *fParticles;
353 ((TParticle*) particles[i])->Print();
356 //_____________________________________________________________________________
357 void AliRun::DumpPStack ()
360 // Dumps the particle stack
362 TClonesArray &particles = *fParticles;
364 "\n\n=======================================================================\n");
365 for (Int_t i=0;i<fNtrack;i++)
367 printf("-> %d ",i); ((TParticle*) particles[i])->Print();
368 printf("--------------------------------------------------------------\n");
371 "\n=======================================================================\n\n");
374 //_____________________________________________________________________________
375 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
376 Float_t maxField, char* filename)
379 // Set magnetic field parameters
380 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
381 // version Magnetic field map version (only 1 active now)
382 // scale Scale factor for the magnetic field
383 // maxField Maximum value for the magnetic field
386 // --- Sanity check on mag field flags
387 if(type<0 || type > 2) {
389 "Invalid magnetic field flag: %5d; Helix tracking chosen instead\n"
393 if(fField) delete fField;
395 fField = new AliMagFC("Map1"," ",type,version,scale,maxField);
396 } else if(version<=3) {
397 fField = new AliMagFCM("Map2-3",filename,type,version,scale,maxField);
400 Warning("SetField","Invalid map %d\n",version);
404 //_____________________________________________________________________________
405 void AliRun::FillTree()
408 // Fills all AliRun TTrees
410 if (fTreeK) fTreeK->Fill();
411 if (fTreeH) fTreeH->Fill();
412 if (fTreeD) fTreeD->Fill();
413 if (fTreeR) fTreeR->Fill();
416 //_____________________________________________________________________________
417 void AliRun::FinishPrimary()
420 // Called at the end of each primary track
423 // static Int_t count=0;
424 // const Int_t times=10;
425 // This primary is finished, purify stack
428 // Write out hits if any
429 if (gAlice->TreeH()) {
430 gAlice->TreeH()->Fill();
437 // if(++count%times==1) gObjectTable->Print();
440 //_____________________________________________________________________________
441 void AliRun::FinishEvent()
444 // Called at the end of the event.
448 if(fLego) fLego->FinishEvent();
450 //Update the energy deposit tables
452 for(i=0;i<fEventEnergy.GetSize();i++) {
453 fSummEnergy[i]+=fEventEnergy[i];
454 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
456 fEventEnergy.Reset();
458 // Clean detector information
461 // Write out the kinematics
467 // Write out the digits
473 // Write out reconstructed clusters
478 // Write out the event Header information
479 if (fTreeE) fTreeE->Fill();
484 // Write Tree headers
485 // Int_t ievent = fHeader.GetEvent();
487 // sprintf(hname,"TreeK%d",ievent);
488 if (fTreeK) fTreeK->Write();
489 // sprintf(hname,"TreeH%d",ievent);
490 if (fTreeH) fTreeH->Write();
491 // sprintf(hname,"TreeD%d",ievent);
492 if (fTreeD) fTreeD->Write();
493 // sprintf(hname,"TreeR%d",ievent);
494 if (fTreeR) fTreeR->Write();
499 //_____________________________________________________________________________
500 void AliRun::FinishRun()
503 // Called at the end of the run.
507 if(fLego) fLego->FinishRun();
509 // Clean detector information
510 TIter next(fModules);
512 while((detector = (AliModule*)next())) {
513 detector->FinishRun();
516 //Output energy summary tables
519 // file is retrieved from whatever tree
521 if (fTreeK) File = fTreeK->GetCurrentFile();
522 if ((!File) && (fTreeH)) File = fTreeH->GetCurrentFile();
523 if ((!File) && (fTreeD)) File = fTreeD->GetCurrentFile();
524 if ((!File) && (fTreeE)) File = fTreeE->GetCurrentFile();
526 Error("FinishRun","There isn't root file!");
532 // Clean tree information
533 delete fTreeK; fTreeK = 0;
534 delete fTreeH; fTreeH = 0;
535 delete fTreeD; fTreeD = 0;
536 delete fTreeR; fTreeR = 0;
537 delete fTreeE; fTreeE = 0;
539 // Write AliRun info and all detectors parameters
546 //_____________________________________________________________________________
547 void AliRun::FlagTrack(Int_t track)
550 // Flags a track and all its family tree to be kept
557 particle=(TParticle*)fParticles->UncheckedAt(curr);
559 // If the particle is flagged the three from here upward is saved already
560 if(particle->TestBit(Keep_Bit)) return;
562 // Save this particle
563 particle->SetBit(Keep_Bit);
565 // Move to father if any
566 if((curr=particle->GetFirstMother())==-1) return;
570 //_____________________________________________________________________________
571 void AliRun::EnergySummary()
574 // Print summary of deposited energy
580 Int_t kn, i, left, j, id;
581 const Float_t zero=0;
582 Int_t ievent=fHeader.GetEvent()+1;
584 // Energy loss information
586 printf("***************** Energy Loss Information per event (GEV) *****************\n");
587 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
590 fEventEnergy[ndep]=kn;
595 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,zero))/ed;
598 fSummEnergy[ndep]=ed;
599 fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,zero));
604 for(kn=0;kn<(ndep-1)/3+1;kn++) {
606 for(i=0;i<(3<left?3:left);i++) {
608 id=Int_t (fEventEnergy[j]+0.1);
609 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
614 // Relative energy loss in different detectors
615 printf("******************** Relative Energy Loss per event ********************\n");
616 printf("Total energy loss per event %10.3f GeV\n",edtot);
617 for(kn=0;kn<(ndep-1)/5+1;kn++) {
619 for(i=0;i<(5<left?5:left);i++) {
621 id=Int_t (fEventEnergy[j]+0.1);
622 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
626 for(kn=0;kn<75;kn++) printf("*");
630 // Reset the TArray's
631 // fEventEnergy.Set(0);
632 // fSummEnergy.Set(0);
633 // fSum2Energy.Set(0);
636 //_____________________________________________________________________________
637 AliModule *AliRun::GetModule(const char *name)
640 // Return pointer to detector from name
642 return (AliModule*)fModules->FindObject(name);
645 //_____________________________________________________________________________
646 AliDetector *AliRun::GetDetector(const char *name)
649 // Return pointer to detector from name
651 return (AliDetector*)fModules->FindObject(name);
654 //_____________________________________________________________________________
655 Int_t AliRun::GetModuleID(const char *name)
658 // Return galice internal detector identifier from name
661 TObject *mod=fModules->FindObject(name);
662 if(mod) i=fModules->IndexOf(mod);
666 //_____________________________________________________________________________
667 Int_t AliRun::GetEvent(Int_t event)
670 // Connect the Trees Kinematics and Hits for event # event
671 // Set branch addresses
674 // Reset existing structures
679 // Delete Trees already connected
680 if (fTreeK) delete fTreeK;
681 if (fTreeH) delete fTreeH;
682 if (fTreeD) delete fTreeD;
683 if (fTreeR) delete fTreeR;
685 // Get header from file
686 if(fTreeE) fTreeE->GetEntry(event);
687 else Error("GetEvent","Cannot file Header Tree\n");
689 // Get Kine Tree from file
691 sprintf(treeName,"TreeK%d",event);
692 fTreeK = (TTree*)gDirectory->Get(treeName);
693 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticles);
694 else Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
696 // Get Hits Tree header from file
697 sprintf(treeName,"TreeH%d",event);
698 fTreeH = (TTree*)gDirectory->Get(treeName);
700 Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
703 // Get Digits Tree header from file
704 sprintf(treeName,"TreeD%d",event);
705 fTreeD = (TTree*)gDirectory->Get(treeName);
707 Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
711 // Get Reconstruct Tree header from file
712 sprintf(treeName,"TreeR%d",event);
713 fTreeR = (TTree*)gDirectory->Get(treeName);
715 // printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
718 // Set Trees branch addresses
719 TIter next(fModules);
721 while((detector = (AliModule*)next())) {
722 detector->SetTreeAddress();
725 if (fTreeK) fTreeK->GetEvent(0);
726 fNtrack = Int_t (fParticles->GetEntries());
730 //_____________________________________________________________________________
731 TGeometry *AliRun::GetGeometry()
734 // Import Alice geometry from current file
735 // Return pointer to geometry object
737 if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
739 // Unlink and relink nodes in detectors
740 // This is bad and there must be a better way...
743 TIter next(fModules);
745 while((detector = (AliModule*)next())) {
746 detector->SetTreeAddress();
747 TList *dnodes=detector->Nodes();
750 for ( j=0; j<dnodes->GetSize(); j++) {
751 node = (TNode*) dnodes->At(j);
752 node1 = fGeometry->GetNode(node->GetName());
753 dnodes->Remove(node);
754 dnodes->AddAt(node1,j);
760 //_____________________________________________________________________________
761 void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
762 Float_t &e, Float_t *vpos, Float_t *polar,
766 // Return next track from stack of particles
771 for(Int_t i=fNtrack-1; i>=0; i--) {
772 track=(TParticle*) fParticles->UncheckedAt(i);
773 if(!track->TestBit(Done_Bit)) {
775 // The track has not yet been processed
777 ipart=track->GetPdgCode();
785 track->GetPolarisation(pol);
790 track->SetBit(Done_Bit);
796 // stop and start timer when we start a primary track
797 Int_t nprimaries = fHeader.GetNprimary();
798 if (fCurrent >= nprimaries) return;
799 if (fCurrent < nprimaries-1) {
801 track=(TParticle*) fParticles->UncheckedAt(fCurrent+1);
802 // track->SetProcessTime(fTimer.CpuTime());
807 //_____________________________________________________________________________
808 Int_t AliRun::GetPrimary(Int_t track)
811 // return number of primary that has generated track
819 part = (TParticle *)fParticles->UncheckedAt(current);
820 parent=part->GetFirstMother();
821 if(parent<0) return current;
825 //_____________________________________________________________________________
826 void AliRun::InitMC(const char *setup)
829 // Initialize the Alice setup
832 gROOT->LoadMacro(setup);
833 gInterpreter->ProcessLine(fConfigFunction.Data());
835 gMC->DefineParticles(); //Create standard MC particles
837 TObject *objfirst, *objlast;
839 fNdets = fModules->GetLast()+1;
842 //=================Create Materials and geometry
845 TIter next(fModules);
847 while((detector = (AliModule*)next())) {
848 detector->SetTreeAddress();
849 objlast = gDirectory->GetList()->Last();
851 // Add Detector histograms in Detector list of histograms
852 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
853 else objfirst = gDirectory->GetList()->First();
855 detector->Histograms()->Add(objfirst);
856 objfirst = gDirectory->GetList()->After(objfirst);
859 SetTransPar(); //Read the cuts for all materials
861 MediaTable(); //Build the special IMEDIA table
863 //Initialise geometry deposition table
864 fEventEnergy.Set(gMC->NofVolumes()+1);
865 fSummEnergy.Set(gMC->NofVolumes()+1);
866 fSum2Energy.Set(gMC->NofVolumes()+1);
868 //Compute cross-sections
871 //Write Geometry object to current file.
877 //_____________________________________________________________________________
878 void AliRun::MediaTable()
881 // Built media table to get from the media number to
884 Int_t kz, nz, idt, lz, i, k, ind;
886 TObjArray &dets = *gAlice->Detectors();
890 for (kz=0;kz<fNdets;kz++) {
891 // If detector is defined
892 if((det=(AliModule*) dets[kz])) {
893 TArrayI &idtmed = *(det->GetIdtmed());
894 for(nz=0;nz<100;nz++) {
895 // Find max and min material number
896 if((idt=idtmed[nz])) {
897 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
898 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
901 if(det->LoMedium() > det->HiMedium()) {
905 if(det->HiMedium() > fImedia->GetSize()) {
906 Error("MediaTable","Increase fImedia from %d to %d",
907 fImedia->GetSize(),det->HiMedium());
910 // Tag all materials in rage as belonging to detector kz
911 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
918 // Print summary table
919 printf(" Traking media ranges:\n");
920 for(i=0;i<(fNdets-1)/6+1;i++) {
921 for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
923 det=(AliModule*)dets[ind];
925 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
928 printf(" %6s: %3d -> %3d;","NULL",0,0);
934 //____________________________________________________________________________
935 void AliRun::SetGenerator(AliGenerator *generator)
938 // Load the event generator
940 if(!fGenerator) fGenerator = generator;
943 //____________________________________________________________________________
944 void AliRun::ResetGenerator(AliGenerator *generator)
947 // Load the event generator
951 Warning("ResetGenerator","Replacing generator %s with %s\n",
952 fGenerator->GetName(),generator->GetName());
954 Warning("ResetGenerator","Replacing generator %s with NULL\n",
955 fGenerator->GetName());
956 fGenerator = generator;
959 //____________________________________________________________________________
960 void AliRun::SetTransPar(char* filename)
963 // Read filename to set the transport parameters
967 const Int_t ncuts=10;
968 const Int_t nflags=11;
969 const Int_t npars=ncuts+nflags;
970 const char pars[npars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
971 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
972 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
973 "MULS","PAIR","PHOT","RAYL"};
979 Int_t i, itmed, iret, ktmed, kz;
982 // See whether the file is there
983 filtmp=gSystem->ExpandPathName(filename);
984 lun=fopen(filtmp,"r");
987 Warning("SetTransPar","File %s does not exist!\n",filename);
991 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
992 printf(" *%59s\n","*");
993 printf(" * Please check carefully what you are doing!%10s\n","*");
994 printf(" *%59s\n","*");
997 // Initialise cuts and flags
998 for(i=0;i<ncuts;i++) cut[i]=-99;
999 for(i=0;i<nflags;i++) flag[i]=-99;
1001 for(i=0;i<256;i++) line[i]='\0';
1002 // Read up to the end of line excluded
1003 iret=fscanf(lun,"%[^\n]",line);
1007 printf(" *%59s\n","*");
1008 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1011 // Read the end of line
1014 if(line[0]=='*') continue;
1016 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",
1017 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1018 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1019 &flag[8],&flag[9],&flag[10]);
1023 Warning("SetTransPar","Error reading file %s\n",filename);
1026 // Check that the module exist
1027 AliModule *mod = GetModule(detName);
1029 // Get the array of media numbers
1030 TArrayI &idtmed = *mod->GetIdtmed();
1031 // Check that the tracking medium code is valid
1032 if(0<=itmed && itmed < 100) {
1033 ktmed=idtmed[itmed];
1035 Warning("SetTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1038 // Set energy thresholds
1039 for(kz=0;kz<ncuts;kz++) {
1041 printf(" * %-6s set to %10.3E for tracking medium code %4d for %s\n",
1042 pars[kz],cut[kz],itmed,mod->GetName());
1043 gMC->Gstpar(ktmed,pars[kz],cut[kz]);
1046 // Set transport mechanisms
1047 for(kz=0;kz<nflags;kz++) {
1049 printf(" * %-6s set to %10d for tracking medium code %4d for %s\n",
1050 pars[ncuts+kz],flag[kz],itmed,mod->GetName());
1051 gMC->Gstpar(ktmed,pars[ncuts+kz],Float_t(flag[kz]));
1055 Warning("SetTransPar","Invalid medium code %d *\n",itmed);
1059 Warning("SetTransPar","Module %s not present\n",detName);
1065 //_____________________________________________________________________________
1066 void AliRun::MakeTree(Option_t *option)
1069 // Create the ROOT trees
1070 // Loop on all detectors to create the Root branch (if any)
1076 char *K = strstr(option,"K");
1077 char *H = strstr(option,"H");
1078 char *E = strstr(option,"E");
1079 char *D = strstr(option,"D");
1080 char *R = strstr(option,"R");
1083 sprintf(hname,"TreeK%d",fEvent);
1084 fTreeK = new TTree(hname,"Kinematics");
1085 // Create a branch for particles
1086 fTreeK->Branch("Particles",&fParticles,4000);
1089 sprintf(hname,"TreeH%d",fEvent);
1090 fTreeH = new TTree(hname,"Hits");
1091 fTreeH->SetAutoSave(1000000000); //no autosave
1094 sprintf(hname,"TreeD%d",fEvent);
1095 fTreeD = new TTree(hname,"Digits");
1098 sprintf(hname,"TreeR%d",fEvent);
1099 fTreeR = new TTree(hname,"Reconstruction");
1102 fTreeE = new TTree("TE","Header");
1103 // Create a branch for Header
1104 fTreeE->Branch("Header","AliHeader",&header,4000);
1107 // Create a branch for hits/digits for each detector
1108 // Each branch is a TClonesArray. Each data member of the Hits classes
1109 // will be in turn a subbranch of the detector master branch
1110 TIter next(fModules);
1111 AliModule *detector;
1112 while((detector = (AliModule*)next())) {
1113 if (H || D || R) detector->MakeBranch(option);
1117 //_____________________________________________________________________________
1118 Int_t AliRun::PurifyKine(Int_t lastSavedTrack, Int_t nofTracks)
1121 // PurifyKine with external parameters
1123 fHgwmk = lastSavedTrack;
1124 fNtrack = nofTracks;
1129 //_____________________________________________________________________________
1130 void AliRun::PurifyKine()
1133 // Compress kinematic tree keeping only flagged particles
1134 // and renaming the particle id's in all the hits
1136 TClonesArray &particles = *fParticles;
1137 int nkeep=fHgwmk+1, parent, i;
1138 TParticle *part, *partnew, *father;
1139 int *map = new int[particles.GetEntries()];
1141 // Save in Header total number of tracks before compression
1142 fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
1144 // First pass, invalid Daughter information
1145 for(i=0; i<fNtrack; i++) {
1146 // Preset map, to be removed later
1147 if(i<=fHgwmk) map[i]=i ; else map[i] = -99;
1148 ((TParticle *)particles.UncheckedAt(i))->ResetBit(Daughters_Bit);
1150 // Second pass, build map between old and new numbering
1151 for(i=fHgwmk+1; i<fNtrack; i++) {
1152 part = (TParticle *)particles.UncheckedAt(i);
1153 if(part->TestBit(Keep_Bit)) {
1155 // This particle has to be kept
1159 // Old and new are different, have to copy
1160 partnew = (TParticle *)particles.UncheckedAt(nkeep);
1161 // Change due to a bug in the HP compiler
1162 // *partnew = *part;
1163 memcpy(partnew,part,sizeof(TParticle));
1164 } else partnew = part;
1166 // as the parent is always *before*, it must be already
1167 // in place. This is what we are checking anyway!
1168 if((parent=partnew->GetFirstMother())>fHgwmk) {
1169 if(map[parent]==-99) printf("map[%d] = -99!\n",parent);
1170 partnew->SetFirstMother(map[parent]);
1177 // Fix daughters information
1178 for (i=0; i<fNtrack; i++) {
1179 part = (TParticle *)particles.UncheckedAt(i);
1180 parent = part->GetFirstMother();
1182 father = (TParticle *)particles.UncheckedAt(parent);
1183 if(father->TestBit(Daughters_Bit)) {
1185 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
1186 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
1188 // Iitialise daughters info for first pass
1189 father->SetFirstDaughter(i);
1190 father->SetLastDaughter(i);
1191 father->SetBit(Daughters_Bit);
1197 // Now loop on all detectors and reset the hits
1199 TIter next(fModules);
1200 AliModule *detector;
1201 while((detector = (AliModule*)next())) {
1202 if (!detector->Hits()) continue;
1203 TClonesArray &vHits=*(detector->Hits());
1204 if(vHits.GetEntries() != detector->GetNhits())
1205 printf("vHits.GetEntries()!=detector->GetNhits(): %d != %d\n",
1206 vHits.GetEntries(),detector->GetNhits());
1207 for (i=0; i<detector->GetNhits(); i++) {
1208 OneHit = (AliHit *)vHits.UncheckedAt(i);
1209 OneHit->SetTrack(map[OneHit->GetTrack()]);
1214 // Now loop on all registered hit lists
1215 TIter next(fHitLists);
1216 TCollection *hitList;
1217 while((hitList = (TCollection*)next())) {
1218 TIter nexthit(hitList);
1220 while((hit = (AliHit*)nexthit())) {
1221 hit->SetTrack(map[hit->GetTrack()]);
1227 particles.SetLast(fHgwmk);
1231 //_____________________________________________________________________________
1232 void AliRun::BeginEvent()
1235 // Reset all Detectors & kinematics & trees
1242 fLego->BeginEvent();
1251 // Initialise event header
1252 fHeader.Reset(fRun,fEvent);
1256 sprintf(hname,"TreeK%d",fEvent);
1257 fTreeK->SetName(hname);
1261 sprintf(hname,"TreeH%d",fEvent);
1262 fTreeH->SetName(hname);
1266 sprintf(hname,"TreeD%d",fEvent);
1267 fTreeD->SetName(hname);
1271 sprintf(hname,"TreeR%d",fEvent);
1272 fTreeR->SetName(hname);
1276 //_____________________________________________________________________________
1277 void AliRun::ResetDigits()
1280 // Reset all Detectors digits
1282 TIter next(fModules);
1283 AliModule *detector;
1284 while((detector = (AliModule*)next())) {
1285 detector->ResetDigits();
1289 //_____________________________________________________________________________
1290 void AliRun::ResetHits()
1293 // Reset all Detectors hits
1295 TIter next(fModules);
1296 AliModule *detector;
1297 while((detector = (AliModule*)next())) {
1298 detector->ResetHits();
1302 //_____________________________________________________________________________
1303 void AliRun::ResetPoints()
1306 // Reset all Detectors points
1308 TIter next(fModules);
1309 AliModule *detector;
1310 while((detector = (AliModule*)next())) {
1311 detector->ResetPoints();
1315 //_____________________________________________________________________________
1316 void AliRun::RunMC(Int_t nevent, const char *setup)
1319 // Main function to be called to process a galice run
1321 // Root > gAlice.Run();
1322 // a positive number of events will cause the finish routine
1326 // check if initialisation has been done
1327 if (!fInitDone) InitMC(setup);
1329 // Create the Root Tree with one branch per detector
1332 gMC->ProcessRun(nevent);
1334 // End of this run, close files
1335 if(nevent>0) FinishRun();
1338 //_____________________________________________________________________________
1339 void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
1340 Float_t themax,Int_t nphi,Float_t phimin,Float_t phimax,
1341 Float_t rmin,Float_t rmax,Float_t zmax)
1344 // Generates lego plots of:
1345 // - radiation length map phi vs theta
1346 // - radiation length map phi vs eta
1347 // - interaction length map
1348 // - g/cm2 length map
1350 // ntheta bins in theta, eta
1351 // themin minimum angle in theta (degrees)
1352 // themax maximum angle in theta (degrees)
1354 // phimin minimum angle in phi (degrees)
1355 // phimax maximum angle in phi (degrees)
1356 // rmin minimum radius
1357 // rmax maximum radius
1360 // The number of events generated = ntheta*nphi
1361 // run input parameters in macro setup (default="Config.C")
1363 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1366 <img src="picts/AliRunLego1.gif">
1371 <img src="picts/AliRunLego2.gif">
1376 <img src="picts/AliRunLego3.gif">
1381 // check if initialisation has been done
1382 if (!fInitDone) InitMC(setup);
1384 //Save current generator
1385 AliGenerator *gen=Generator();
1387 //Create Lego object
1388 fLego = new AliLego("lego",ntheta,themin,themax,nphi,phimin,phimax,rmin,rmax,zmax);
1390 //Prepare MC for Lego Run
1394 gMC->ProcessRun(ntheta*nphi+1);
1396 // Create only the Root event Tree
1399 // End of this run, close files
1402 // Delete Lego Object
1403 delete fLego; fLego=0;
1405 // Restore current generator
1409 //_____________________________________________________________________________
1410 void AliRun::SetCurrentTrack(Int_t track)
1413 // Set current track number
1418 //_____________________________________________________________________________
1419 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1420 Float_t *vpos, Float_t *polar, Float_t tof,
1421 const char *mecha, Int_t &ntr, Float_t weight)
1424 // Load a track on the stack
1426 // done 0 if the track has to be transported
1428 // parent identifier of the parent track. -1 for a primary
1429 // pdg particle code
1430 // pmom momentum GeV/c
1432 // polar polarisation
1433 // tof time of flight in seconds
1434 // mecha production mechanism
1435 // ntr on output the number of the track stored
1437 TClonesArray &particles = *fParticles;
1438 TParticle *particle;
1440 const Int_t firstdaughter=-1;
1441 const Int_t lastdaughter=-1;
1443 // const Float_t tlife=0;
1446 // Here we get the static mass
1447 // For MC is ok, but a more sophisticated method could be necessary
1448 // if the calculated mass is required
1449 // also, this method is potentially dangerous if the mass
1450 // used in the MC is not the same of the PDG database
1452 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
1453 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
1454 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
1456 //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",
1457 //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],KS,mecha);
1459 particle=new(particles[fNtrack]) TParticle(pdg,KS,parent,-1,firstdaughter,
1460 lastdaughter,pmom[0],pmom[1],pmom[2],
1461 e,vpos[0],vpos[1],vpos[2],tof);
1462 // polar[0],polar[1],polar[2],tof,
1464 ((TParticle*)particles[fNtrack])->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
1465 ((TParticle*)particles[fNtrack])->SetWeight(weight);
1466 if(!done) particle->SetBit(Done_Bit);
1467 //Declare that the daughter information is valid
1468 ((TParticle*)particles[fNtrack])->SetBit(Daughters_Bit);
1471 particle=(TParticle*) fParticles->UncheckedAt(parent);
1472 particle->SetLastDaughter(fNtrack);
1473 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
1476 // This is a primary track. Set high water mark for this event
1479 // Set also number if primary tracks
1480 fHeader.SetNprimary(fHgwmk+1);
1481 fHeader.SetNtrack(fHgwmk+1);
1486 //_____________________________________________________________________________
1487 void AliRun::KeepTrack(const Int_t track)
1490 // flags a track to be kept
1492 TClonesArray &particles = *fParticles;
1493 ((TParticle*)particles[track])->SetBit(Keep_Bit);
1496 //_____________________________________________________________________________
1497 void AliRun::StepManager(Int_t id)
1500 // Called at every step during transport
1504 // --- If lego option, do it and leave
1506 fLego->StepManager();
1509 //Update energy deposition tables
1510 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
1512 //Call the appropriate stepping routine;
1513 AliModule *det = (AliModule*)fModules->At(id);
1514 if(det) det->StepManager();
1518 //_____________________________________________________________________________
1519 void AliRun::Streamer(TBuffer &R__b)
1522 // Stream an object of class AliRun.
1524 if (R__b.IsReading()) {
1525 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1526 TNamed::Streamer(R__b);
1527 if (!gAlice) gAlice = this;
1528 gROOT->GetListOfBrowsables()->Add(this,"Run");
1529 fTreeE = (TTree*)gDirectory->Get("TE");
1530 if (fTreeE) fTreeE->SetBranchAddress("Header", &header);
1531 else Error("Streamer","cannot find Header Tree\n");
1535 fHeader.Streamer(R__b);
1545 R__b >> fPDGDB; //Particle factory object!
1546 fTreeE->GetEntry(0);
1548 fHeader.SetEvent(0);
1549 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
1552 fConfigFunction.Streamer(R__b);
1554 fConfigFunction="Config();";
1557 R__b.WriteVersion(AliRun::IsA());
1558 TNamed::Streamer(R__b);
1562 fHeader.Streamer(R__b);
1571 R__b << fPDGDB; //Particle factory object!
1572 fConfigFunction.Streamer(R__b);