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.21 1999/11/25 10:40:08 fca
19 Fixing daughters information also in primary tracks
21 Revision 1.20 1999/10/04 18:08:49 fca
22 Adding protection against inconsistent Euclid files
24 Revision 1.19 1999/09/29 07:50:40 fca
25 Introduction of the Copyright and cvs Log
29 ///////////////////////////////////////////////////////////////////////////////
31 // Control class for Alice C++ //
32 // Only one single instance of this class exists. //
33 // The object is created in main program aliroot //
34 // and is pointed by the global gAlice. //
36 // -Supports the list of all Alice Detectors (fModules). //
37 // -Supports the list of particles (fParticles). //
38 // -Supports the Trees. //
39 // -Supports the geometry. //
40 // -Supports the event display. //
43 <img src="picts/AliRunClass.gif">
48 <img src="picts/alirun.gif">
52 ///////////////////////////////////////////////////////////////////////////////
60 #include <TObjectTable.h>
62 #include "TParticle.h"
64 #include "AliDisplay.h"
66 #include "AliCallf77.h"
74 static AliHeader *header;
78 # define rxgtrak rxgtrak_
79 # define rxstrak rxstrak_
80 # define rxkeep rxkeep_
81 # define rxouth rxouth_
84 # define rxgtrak RXGTRAK
85 # define rxstrak RXSTRAK
86 # define rxkeep RXKEEP
87 # define rxouth RXOUTH
90 static TArrayF sEventEnergy;
91 static TArrayF sSummEnergy;
92 static TArrayF sSum2Energy;
96 //_____________________________________________________________________________
100 // Default constructor for AliRun
124 fPDGDB = 0; //Particle factory object!
127 //_____________________________________________________________________________
128 AliRun::AliRun(const char *name, const char *title)
132 // Constructor for the main processor.
133 // Creates the geometry
134 // Creates the list of Detectors.
135 // Creates the list of particles.
152 gROOT->GetListOfBrowsables()->Add(this,name);
154 // create the support list for the various Detectors
155 fModules = new TObjArray(77);
157 // Create the TNode geometry for the event display
159 BuildSimpleGeometry();
169 // Create the particle stack
170 fParticles = new TClonesArray("TParticle",100);
174 // Create default mag field
179 // Prepare the tracking medium lists
180 fImedia = new TArrayI(1000);
181 for(i=0;i<1000;i++) (*fImedia)[i]=-99;
184 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
187 //_____________________________________________________________________________
191 // Defaullt AliRun destructor
210 fParticles->Delete();
215 //_____________________________________________________________________________
216 void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
219 // Add a hit to detector id
221 TObjArray &dets = *fModules;
222 if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
225 //_____________________________________________________________________________
226 void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
229 // Add digit to detector id
231 TObjArray &dets = *fModules;
232 if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
235 //_____________________________________________________________________________
236 void AliRun::Browse(TBrowser *b)
239 // Called when the item "Run" is clicked on the left pane
240 // of the Root browser.
241 // It displays the Root Trees and all detectors.
243 if (fTreeK) b->Add(fTreeK,fTreeK->GetName());
244 if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
245 if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
246 if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
247 if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
249 TIter next(fModules);
251 while((detector = (AliModule*)next())) {
252 b->Add(detector,detector->GetName());
256 //_____________________________________________________________________________
260 // Initialize Alice geometry
265 //_____________________________________________________________________________
266 void AliRun::BuildSimpleGeometry()
269 // Create a simple TNode geometry used by Root display engine
271 // Initialise geometry
273 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
274 new TMaterial("void","Vacuum",0,0,0); //Everything is void
275 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
276 brik->SetVisibility(0);
277 new TNode("alice","alice","S_alice");
280 //_____________________________________________________________________________
281 void AliRun::CleanDetectors()
284 // Clean Detectors at the end of event
286 TIter next(fModules);
288 while((detector = (AliModule*)next())) {
289 detector->FinishEvent();
293 //_____________________________________________________________________________
294 void AliRun::CleanParents()
297 // Clean Particles stack.
298 // Set parent/daughter relations
300 TClonesArray &particles = *(gAlice->Particles());
303 for(i=0; i<fNtrack; i++) {
304 part = (TParticle *)particles.UncheckedAt(i);
305 if(!part->TestBit(Daughters_Bit)) {
306 part->SetFirstDaughter(-1);
307 part->SetLastDaughter(-1);
312 //_____________________________________________________________________________
313 Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
316 // Return the distance from the mouse to the AliRun object
322 //_____________________________________________________________________________
323 void AliRun::DumpPart (Int_t i)
326 // Dumps particle i in the stack
328 TClonesArray &particles = *fParticles;
329 ((TParticle*) particles[i])->Print();
332 //_____________________________________________________________________________
333 void AliRun::DumpPStack ()
336 // Dumps the particle stack
338 TClonesArray &particles = *fParticles;
340 "\n\n=======================================================================\n");
341 for (Int_t i=0;i<fNtrack;i++)
343 printf("-> %d ",i); ((TParticle*) particles[i])->Print();
344 printf("--------------------------------------------------------------\n");
347 "\n=======================================================================\n\n");
350 //_____________________________________________________________________________
351 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
352 Float_t maxField, char* filename)
355 // Set magnetic field parameters
356 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
357 // version Magnetic field map version (only 1 active now)
358 // scale Scale factor for the magnetic field
359 // maxField Maximum value for the magnetic field
362 // --- Sanity check on mag field flags
363 if(type<0 || type > 2) {
365 "Invalid magnetic field flag: %5d; Helix tracking chosen instead\n"
369 if(fField) delete fField;
371 fField = new AliMagFC("Map1"," ",type,version,scale,maxField);
372 } else if(version<=3) {
373 fField = new AliMagFCM("Map2-3",filename,type,version,scale,maxField);
376 Warning("SetField","Invalid map %d\n",version);
380 //_____________________________________________________________________________
381 void AliRun::FillTree()
384 // Fills all AliRun TTrees
386 if (fTreeK) fTreeK->Fill();
387 if (fTreeH) fTreeH->Fill();
388 if (fTreeD) fTreeD->Fill();
389 if (fTreeR) fTreeR->Fill();
392 //_____________________________________________________________________________
393 void AliRun::FinishPrimary()
396 // Called at the end of each primary track
399 // static Int_t count=0;
400 // const Int_t times=10;
401 // This primary is finished, purify stack
402 gAlice->PurifyKine();
404 // Write out hits if any
405 if (gAlice->TreeH()) {
406 gAlice->TreeH()->Fill();
413 // if(++count%times==1) gObjectTable->Print();
416 //_____________________________________________________________________________
417 void AliRun::FinishEvent()
420 // Called at the end of the event.
423 //Update the energy deposit tables
425 for(i=0;i<sEventEnergy.GetSize();i++) {
426 sSummEnergy[i]+=sEventEnergy[i];
427 sSum2Energy[i]+=sEventEnergy[i]*sEventEnergy[i];
429 sEventEnergy.Reset();
431 // Clean detector information
434 // Write out the kinematics
440 // Write out the digits
446 // Write out reconstructed clusters
451 // Write out the event Header information
452 if (fTreeE) fTreeE->Fill();
457 // Write Tree headers
458 // Int_t ievent = fHeader.GetEvent();
460 // sprintf(hname,"TreeK%d",ievent);
461 if (fTreeK) fTreeK->Write();
462 // sprintf(hname,"TreeH%d",ievent);
463 if (fTreeH) fTreeH->Write();
464 // sprintf(hname,"TreeD%d",ievent);
465 if (fTreeD) fTreeD->Write();
466 // sprintf(hname,"TreeR%d",ievent);
467 if (fTreeR) fTreeR->Write();
470 //_____________________________________________________________________________
471 void AliRun::FinishRun()
474 // Called at the end of the run.
477 // Clean detector information
478 TIter next(fModules);
480 while((detector = (AliModule*)next())) {
481 detector->FinishRun();
484 //Output energy summary tables
487 // file is retrieved from whatever tree
489 if (fTreeK) File = fTreeK->GetCurrentFile();
490 if ((!File) && (fTreeH)) File = fTreeH->GetCurrentFile();
491 if ((!File) && (fTreeD)) File = fTreeD->GetCurrentFile();
492 if ((!File) && (fTreeE)) File = fTreeE->GetCurrentFile();
494 Error("FinishRun","There isn't root file!");
500 // Clean tree information
501 delete fTreeK; fTreeK = 0;
502 delete fTreeH; fTreeH = 0;
503 delete fTreeD; fTreeD = 0;
504 delete fTreeR; fTreeR = 0;
505 delete fTreeE; fTreeE = 0;
507 // Write AliRun info and all detectors parameters
515 //_____________________________________________________________________________
516 void AliRun::FlagTrack(Int_t track)
519 // Flags a track and all its family tree to be kept
526 particle=(TParticle*)fParticles->UncheckedAt(curr);
528 // If the particle is flagged the three from here upward is saved already
529 if(particle->TestBit(Keep_Bit)) return;
531 // Save this particle
532 particle->SetBit(Keep_Bit);
534 // Move to father if any
535 if((curr=particle->GetFirstMother())==-1) return;
539 //_____________________________________________________________________________
540 void AliRun::EnergySummary()
543 // Print summary of deposited energy
549 Int_t kn, i, left, j, id;
550 const Float_t zero=0;
551 Int_t ievent=fHeader.GetEvent()+1;
553 // Energy loss information
555 printf("***************** Energy Loss Information per event (GEV) *****************\n");
556 for(kn=1;kn<sEventEnergy.GetSize();kn++) {
559 sEventEnergy[ndep]=kn;
564 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,zero))/ed;
567 sSummEnergy[ndep]=ed;
568 sSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,zero));
573 for(kn=0;kn<(ndep-1)/3+1;kn++) {
575 for(i=0;i<(3<left?3:left);i++) {
577 id=Int_t (sEventEnergy[j]+0.1);
578 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),sSummEnergy[j],sSum2Energy[j]);
583 // Relative energy loss in different detectors
584 printf("******************** Relative Energy Loss per event ********************\n");
585 printf("Total energy loss per event %10.3f GeV\n",edtot);
586 for(kn=0;kn<(ndep-1)/5+1;kn++) {
588 for(i=0;i<(5<left?5:left);i++) {
590 id=Int_t (sEventEnergy[j]+0.1);
591 printf(" %s %10.3f%%;",gMC->VolName(id),100*sSummEnergy[j]/edtot);
595 for(kn=0;kn<75;kn++) printf("*");
599 // Reset the TArray's
605 //_____________________________________________________________________________
606 AliModule *AliRun::GetModule(const char *name)
609 // Return pointer to detector from name
611 return (AliModule*)fModules->FindObject(name);
614 //_____________________________________________________________________________
615 AliDetector *AliRun::GetDetector(const char *name)
618 // Return pointer to detector from name
620 return (AliDetector*)fModules->FindObject(name);
623 //_____________________________________________________________________________
624 Int_t AliRun::GetModuleID(const char *name)
627 // Return galice internal detector identifier from name
630 TObject *mod=fModules->FindObject(name);
631 if(mod) i=fModules->IndexOf(mod);
635 //_____________________________________________________________________________
636 Int_t AliRun::GetEvent(Int_t event)
639 // Connect the Trees Kinematics and Hits for event # event
640 // Set branch addresses
643 // Reset existing structures
648 // Delete Trees already connected
649 if (fTreeK) delete fTreeK;
650 if (fTreeH) delete fTreeH;
651 if (fTreeD) delete fTreeD;
652 if (fTreeR) delete fTreeR;
654 // Get header from file
655 if(fTreeE) fTreeE->GetEntry(event);
656 else Error("GetEvent","Cannot file Header Tree\n");
658 // Get Kine Tree from file
660 sprintf(treeName,"TreeK%d",event);
661 fTreeK = (TTree*)gDirectory->Get(treeName);
662 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticles);
663 else Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
665 // Get Hits Tree header from file
666 sprintf(treeName,"TreeH%d",event);
667 fTreeH = (TTree*)gDirectory->Get(treeName);
669 Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
672 // Get Digits Tree header from file
673 sprintf(treeName,"TreeD%d",event);
674 fTreeD = (TTree*)gDirectory->Get(treeName);
676 Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
680 // Get Reconstruct Tree header from file
681 sprintf(treeName,"TreeR%d",event);
682 fTreeR = (TTree*)gDirectory->Get(treeName);
684 // printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
687 // Set Trees branch addresses
688 TIter next(fModules);
690 while((detector = (AliModule*)next())) {
691 detector->SetTreeAddress();
694 if (fTreeK) fTreeK->GetEvent(0);
695 fNtrack = Int_t (fParticles->GetEntries());
699 //_____________________________________________________________________________
700 TGeometry *AliRun::GetGeometry()
703 // Import Alice geometry from current file
704 // Return pointer to geometry object
706 if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
708 // Unlink and relink nodes in detectors
709 // This is bad and there must be a better way...
712 TIter next(fModules);
714 while((detector = (AliModule*)next())) {
715 detector->SetTreeAddress();
716 TList *dnodes=detector->Nodes();
719 for ( j=0; j<dnodes->GetSize(); j++) {
720 node = (TNode*) dnodes->At(j);
721 node1 = fGeometry->GetNode(node->GetName());
722 dnodes->Remove(node);
723 dnodes->AddAt(node1,j);
729 //_____________________________________________________________________________
730 void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
731 Float_t &e, Float_t *vpos, Float_t *polar,
735 // Return next track from stack of particles
740 for(Int_t i=fNtrack-1; i>=0; i--) {
741 track=(TParticle*) fParticles->UncheckedAt(i);
742 if(!track->TestBit(Done_Bit)) {
744 // The track has not yet been processed
746 ipart=track->GetPdgCode();
754 track->GetPolarisation(pol);
759 track->SetBit(Done_Bit);
765 // stop and start timer when we start a primary track
766 Int_t nprimaries = fHeader.GetNprimary();
767 if (fCurrent >= nprimaries) return;
768 if (fCurrent < nprimaries-1) {
770 track=(TParticle*) fParticles->UncheckedAt(fCurrent+1);
771 // track->SetProcessTime(fTimer.CpuTime());
776 //_____________________________________________________________________________
777 Int_t AliRun::GetPrimary(Int_t track)
780 // return number of primary that has generated track
788 part = (TParticle *)fParticles->UncheckedAt(current);
789 parent=part->GetFirstMother();
790 if(parent<0) return current;
794 //_____________________________________________________________________________
795 void AliRun::Init(const char *setup)
798 // Initialize the Alice setup
801 gROOT->LoadMacro(setup);
802 gInterpreter->ProcessLine("Config();");
804 gMC->DefineParticles(); //Create standard MC particles
806 TObject *objfirst, *objlast;
808 fNdets = fModules->GetLast()+1;
811 //=================Create Materials, geometry, histograms, etc
812 TIter next(fModules);
814 while((detector = (AliModule*)next())) {
815 detector->SetTreeAddress();
816 objlast = gDirectory->GetList()->Last();
818 // Initialise detector materials, geometry, histograms,etc
819 detector->CreateMaterials();
820 detector->CreateGeometry();
821 detector->BuildGeometry();
824 // Add Detector histograms in Detector list of histograms
825 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
826 else objfirst = gDirectory->GetList()->First();
828 detector->Histograms()->Add(objfirst);
829 objfirst = gDirectory->GetList()->After(objfirst);
832 SetTransPar(); //Read the cuts for all materials
834 MediaTable(); //Build the special IMEDIA table
836 //Close the geometry structure
839 //Initialise geometry deposition table
840 sEventEnergy.Set(gMC->NofVolumes()+1);
841 sSummEnergy.Set(gMC->NofVolumes()+1);
842 sSum2Energy.Set(gMC->NofVolumes()+1);
844 //Create the color table
847 //Compute cross-sections
850 //Write Geometry object to current file.
856 //_____________________________________________________________________________
857 void AliRun::MediaTable()
860 // Built media table to get from the media number to
863 Int_t kz, nz, idt, lz, i, k, ind;
865 TObjArray &dets = *gAlice->Detectors();
869 for (kz=0;kz<fNdets;kz++) {
870 // If detector is defined
871 if((det=(AliModule*) dets[kz])) {
872 TArrayI &idtmed = *(det->GetIdtmed());
873 for(nz=0;nz<100;nz++) {
874 // Find max and min material number
875 if((idt=idtmed[nz])) {
876 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
877 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
880 if(det->LoMedium() > det->HiMedium()) {
884 if(det->HiMedium() > fImedia->GetSize()) {
885 Error("MediaTable","Increase fImedia from %d to %d",
886 fImedia->GetSize(),det->HiMedium());
889 // Tag all materials in rage as belonging to detector kz
890 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
897 // Print summary table
898 printf(" Traking media ranges:\n");
899 for(i=0;i<(fNdets-1)/6+1;i++) {
900 for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
902 det=(AliModule*)dets[ind];
904 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
907 printf(" %6s: %3d -> %3d;","NULL",0,0);
913 //____________________________________________________________________________
914 void AliRun::SetGenerator(AliGenerator *generator)
917 // Load the event generator
919 if(!fGenerator) fGenerator = generator;
922 //____________________________________________________________________________
923 void AliRun::SetTransPar(char* filename)
926 // Read filename to set the transport parameters
930 const Int_t ncuts=10;
931 const Int_t nflags=11;
932 const Int_t npars=ncuts+nflags;
933 const char pars[npars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
934 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
935 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
936 "MULS","PAIR","PHOT","RAYL"};
942 Int_t i, itmed, iret, ktmed, kz;
945 // See whether the file is there
946 filtmp=gSystem->ExpandPathName(filename);
947 lun=fopen(filtmp,"r");
950 Warning("SetTransPar","File %s does not exist!\n",filename);
954 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
955 printf(" *%59s\n","*");
956 printf(" * Please check carefully what you are doing!%10s\n","*");
957 printf(" *%59s\n","*");
960 // Initialise cuts and flags
961 for(i=0;i<ncuts;i++) cut[i]=-99;
962 for(i=0;i<nflags;i++) flag[i]=-99;
964 for(i=0;i<256;i++) line[i]='\0';
965 // Read up to the end of line excluded
966 iret=fscanf(lun,"%[^\n]",line);
970 printf(" *%59s\n","*");
971 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
974 // Read the end of line
977 if(line[0]=='*') continue;
979 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",
980 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
981 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
982 &flag[8],&flag[9],&flag[10]);
986 Warning("SetTransPar","Error reading file %s\n",filename);
989 // Check that the module exist
990 AliModule *mod = GetModule(detName);
992 // Get the array of media numbers
993 TArrayI &idtmed = *mod->GetIdtmed();
994 // Check that the tracking medium code is valid
995 if(0<=itmed && itmed < 100) {
998 Warning("SetTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1001 // Set energy thresholds
1002 for(kz=0;kz<ncuts;kz++) {
1004 printf(" * %-6s set to %10.3E for tracking medium code %4d for %s\n",
1005 pars[kz],cut[kz],itmed,mod->GetName());
1006 gMC->Gstpar(ktmed,pars[kz],cut[kz]);
1009 // Set transport mechanisms
1010 for(kz=0;kz<nflags;kz++) {
1012 printf(" * %-6s set to %10d for tracking medium code %4d for %s\n",
1013 pars[ncuts+kz],flag[kz],itmed,mod->GetName());
1014 gMC->Gstpar(ktmed,pars[ncuts+kz],Float_t(flag[kz]));
1018 Warning("SetTransPar","Invalid medium code %d *\n",itmed);
1022 Warning("SetTransPar","Module %s not present\n",detName);
1028 //_____________________________________________________________________________
1029 void AliRun::MakeTree(Option_t *option)
1032 // Create the ROOT trees
1033 // Loop on all detectors to create the Root branch (if any)
1038 char *K = strstr(option,"K");
1039 char *H = strstr(option,"H");
1040 char *E = strstr(option,"E");
1041 char *D = strstr(option,"D");
1042 char *R = strstr(option,"R");
1044 if (K && !fTreeK) fTreeK = new TTree("TreeK0","Kinematics");
1045 if (H && !fTreeH) fTreeH = new TTree("TreeH0","Hits");
1046 if (D && !fTreeD) fTreeD = new TTree("TreeD0","Digits");
1047 if (E && !fTreeE) fTreeE = new TTree("TE","Header");
1048 if (R && !fTreeR) fTreeR = new TTree("TreeR0","Reconstruction");
1049 if (fTreeH) fTreeH->SetAutoSave(1000000000); //no autosave
1051 // Create a branch for hits/digits for each detector
1052 // Each branch is a TClonesArray. Each data member of the Hits classes
1053 // will be in turn a subbranch of the detector master branch
1054 TIter next(fModules);
1055 AliModule *detector;
1056 while((detector = (AliModule*)next())) {
1057 if (H || D || R) detector->MakeBranch(option);
1059 // Create a branch for particles
1060 if (fTreeK && K) fTreeK->Branch("Particles",&fParticles,4000);
1062 // Create a branch for Header
1063 if (fTreeE && E) fTreeE->Branch("Header","AliHeader",&header,4000);
1066 //_____________________________________________________________________________
1067 Int_t AliRun::PurifyKine(Int_t lastSavedTrack, Int_t nofTracks)
1070 // PurifyKine with external parameters
1072 fHgwmk = lastSavedTrack;
1073 fNtrack = nofTracks;
1078 //_____________________________________________________________________________
1079 void AliRun::PurifyKine()
1082 // Compress kinematic tree keeping only flagged particles
1083 // and renaming the particle id's in all the hits
1085 TClonesArray &particles = *fParticles;
1086 int nkeep=fHgwmk+1, parent, i;
1087 TParticle *part, *partnew, *father;
1089 int *map = new int[particles.GetEntries()];
1091 // Save in Header total number of tracks before compression
1092 fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
1094 // Preset map, to be removed later
1095 for(i=0; i<fNtrack; i++) {
1096 if(i<=fHgwmk) map[i]=i ; else map[i] = -99 ;}
1097 // Second pass, build map between old and new numbering
1098 for(i=fHgwmk+1; i<fNtrack; i++) {
1099 part = (TParticle *)particles.UncheckedAt(i);
1100 if(part->TestBit(Keep_Bit)) {
1102 // This particle has to be kept
1106 // Old and new are different, have to copy
1107 partnew = (TParticle *)particles.UncheckedAt(nkeep);
1109 } else partnew = part;
1111 // as the parent is always *before*, it must be already
1112 // in place. This is what we are checking anyway!
1113 if((parent=partnew->GetFirstMother())>fHgwmk) {
1114 if(map[parent]==-99) printf("map[%d] = -99!\n",parent);
1115 partnew->SetFirstMother(map[parent]);
1122 // Fix daughters information
1123 for (i=0; i<fNtrack; i++) {
1124 part = (TParticle *)particles.UncheckedAt(i);
1125 parent = part->GetFirstMother();
1127 father = (TParticle *)particles.UncheckedAt(parent);
1128 if(father->TestBit(Daughters_Bit)) {
1130 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
1131 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
1133 // Iitialise daughters info for first pass
1134 father->SetFirstDaughter(i);
1135 father->SetLastDaughter(i);
1136 father->SetBit(Daughters_Bit);
1141 // Now loop on all detectors and reset the hits
1142 TIter next(fModules);
1143 AliModule *detector;
1144 while((detector = (AliModule*)next())) {
1145 if (!detector->Hits()) continue;
1146 TClonesArray &vHits=*(detector->Hits());
1147 if(vHits.GetEntries() != detector->GetNhits())
1148 printf("vHits.GetEntries()!=detector->GetNhits(): %d != %d\n",
1149 vHits.GetEntries(),detector->GetNhits());
1150 for (i=0; i<detector->GetNhits(); i++) {
1151 OneHit = (AliHit *)vHits.UncheckedAt(i);
1152 OneHit->SetTrack(map[OneHit->GetTrack()]);
1157 particles.SetLast(fHgwmk);
1161 //_____________________________________________________________________________
1162 void AliRun::Reset(Int_t run, Int_t idevent)
1165 // Reset all Detectors & kinematics & trees
1173 // Initialise event header
1174 fHeader.Reset(run,idevent);
1178 sprintf(hname,"TreeK%d",idevent);
1179 fTreeK->SetName(hname);
1183 sprintf(hname,"TreeH%d",idevent);
1184 fTreeH->SetName(hname);
1188 sprintf(hname,"TreeD%d",idevent);
1189 fTreeD->SetName(hname);
1193 sprintf(hname,"TreeR%d",idevent);
1194 fTreeR->SetName(hname);
1198 //_____________________________________________________________________________
1199 void AliRun::ResetDigits()
1202 // Reset all Detectors digits
1204 TIter next(fModules);
1205 AliModule *detector;
1206 while((detector = (AliModule*)next())) {
1207 detector->ResetDigits();
1211 //_____________________________________________________________________________
1212 void AliRun::ResetHits()
1215 // Reset all Detectors hits
1217 TIter next(fModules);
1218 AliModule *detector;
1219 while((detector = (AliModule*)next())) {
1220 detector->ResetHits();
1224 //_____________________________________________________________________________
1225 void AliRun::ResetPoints()
1228 // Reset all Detectors points
1230 TIter next(fModules);
1231 AliModule *detector;
1232 while((detector = (AliModule*)next())) {
1233 detector->ResetPoints();
1237 //_____________________________________________________________________________
1238 void AliRun::Run(Int_t nevent, const char *setup)
1241 // Main function to be called to process a galice run
1243 // Root > gAlice.Run();
1244 // a positive number of events will cause the finish routine
1249 // check if initialisation has been done
1250 if (!fInitDone) Init(setup);
1252 // Create the Root Tree with one branch per detector
1254 gAlice->MakeTree("KHDER");
1257 todo = TMath::Abs(nevent);
1258 for (i=0; i<todo; i++) {
1259 // Process one run (one run = one event)
1260 gAlice->Reset(fRun, fEvent);
1264 gAlice->FinishEvent();
1268 // End of this run, close files
1269 if(nevent>0) gAlice->FinishRun();
1272 //_____________________________________________________________________________
1273 void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
1274 Float_t themax,Int_t nphi,Float_t phimin,Float_t phimax,
1275 Float_t rmin,Float_t rmax,Float_t zmax)
1278 // Generates lego plots of:
1279 // - radiation length map phi vs theta
1280 // - radiation length map phi vs eta
1281 // - interaction length map
1282 // - g/cm2 length map
1284 // ntheta bins in theta, eta
1285 // themin minimum angle in theta (degrees)
1286 // themax maximum angle in theta (degrees)
1288 // phimin minimum angle in phi (degrees)
1289 // phimax maximum angle in phi (degrees)
1290 // rmin minimum radius
1291 // rmax maximum radius
1294 // The number of events generated = ntheta*nphi
1295 // run input parameters in macro setup (default="Config.C")
1297 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1300 <img src="picts/AliRunLego1.gif">
1305 <img src="picts/AliRunLego2.gif">
1310 <img src="picts/AliRunLego3.gif">
1315 // check if initialisation has been done
1316 if (!fInitDone) Init(setup);
1318 fLego = new AliLego("lego","lego");
1319 fLego->Init(ntheta,themin,themax,nphi,phimin,phimax,rmin,rmax,zmax);
1322 // Create only the Root event Tree
1323 gAlice->MakeTree("E");
1325 // End of this run, close files
1326 gAlice->FinishRun();
1329 //_____________________________________________________________________________
1330 void AliRun::SetCurrentTrack(Int_t track)
1333 // Set current track number
1338 //_____________________________________________________________________________
1339 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1340 Float_t *vpos, Float_t *polar, Float_t tof,
1341 const char *mecha, Int_t &ntr, Float_t weight)
1344 // Load a track on the stack
1346 // done 0 if the track has to be transported
1348 // parent identifier of the parent track. -1 for a primary
1349 // pdg particle code
1350 // pmom momentum GeV/c
1352 // polar polarisation
1353 // tof time of flight in seconds
1354 // mecha production mechanism
1355 // ntr on output the number of the track stored
1357 TClonesArray &particles = *fParticles;
1358 TParticle *particle;
1360 const Int_t firstdaughter=-1;
1361 const Int_t lastdaughter=-1;
1363 // const Float_t tlife=0;
1366 // Here we get the static mass
1367 // For MC is ok, but a more sophisticated method could be necessary
1368 // if the calculated mass is required
1369 // also, this method is potentially dangerous if the mass
1370 // used in the MC is not the same of the PDG database
1372 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
1373 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
1374 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
1376 //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",
1377 //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],KS,mecha);
1379 particle=new(particles[fNtrack]) TParticle(pdg,KS,parent,-1,firstdaughter,
1380 lastdaughter,pmom[0],pmom[1],pmom[2],
1381 e,vpos[0],vpos[1],vpos[2],tof);
1382 // polar[0],polar[1],polar[2],tof,
1384 ((TParticle*)particles[fNtrack])->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
1385 ((TParticle*)particles[fNtrack])->SetWeight(weight);
1386 if(!done) particle->SetBit(Done_Bit);
1389 particle=(TParticle*) fParticles->UncheckedAt(parent);
1390 particle->SetLastDaughter(fNtrack);
1391 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
1394 // This is a primary track. Set high water mark for this event
1397 // Set also number if primary tracks
1398 fHeader.SetNprimary(fHgwmk+1);
1399 fHeader.SetNtrack(fHgwmk+1);
1404 //_____________________________________________________________________________
1405 void AliRun::KeepTrack(const Int_t track)
1408 // flags a track to be kept
1410 TClonesArray &particles = *fParticles;
1411 ((TParticle*)particles[track])->SetBit(Keep_Bit);
1414 //_____________________________________________________________________________
1415 void AliRun::StepManager(Int_t id) const
1418 // Called at every step during transport
1423 // --- If lego option, do it and leave
1425 fLego->StepManager();
1428 //Update energy deposition tables
1429 sEventEnergy[gMC->CurrentVolID(copy)]+=gMC->Edep();
1431 //Call the appropriate stepping routine;
1432 AliModule *det = (AliModule*)fModules->At(id);
1433 if(det) det->StepManager();
1436 //_____________________________________________________________________________
1437 void AliRun::ReadEuclid(const char* filnam, const AliModule *det, char* topvol)
1440 // read in the geometry of the detector in euclid file format
1442 // id_det : the detector identification (2=its,...)
1443 // topvol : return parameter describing the name of the top
1444 // volume of geometry.
1446 // author : m. maire
1449 // several changes have been made by miroslav helbich
1450 // subroutine is rewrited to follow the new established way of memory
1451 // booking for tracking medias and rotation matrices.
1452 // all used tracking media have to be defined first, for this you can use
1453 // subroutine greutmed.
1454 // top volume is searched as only volume not positioned into another
1457 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
1458 Int_t ndvmx, nr, flag;
1459 char key[5], card[77], natmed[21];
1460 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
1463 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
1465 const Int_t maxrot=5000;
1466 Int_t idrot[maxrot],istop[7000];
1469 // *** The input filnam name will be with extension '.euc'
1470 filtmp=gSystem->ExpandPathName(filnam);
1471 lun=fopen(filtmp,"r");
1474 Error("ReadEuclid","Could not open file %s\n",filnam);
1477 //* --- definition of rotation matrix 0 ---
1478 TArrayI &idtmed = *(det->GetIdtmed());
1479 for(i=1; i<maxrot; ++i) idrot[i]=-99;
1483 for(i=0;i<77;i++) card[i]=0;
1484 iret=fscanf(lun,"%77[^\n]",card);
1485 if(iret<=0) goto L20;
1488 strncpy(key,card,4);
1490 if (!strcmp(key,"TMED")) {
1491 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
1492 if( itmed<0 || itmed>=100 ) {
1493 Error("ReadEuclid","TMED illegal medium number %d for %s\n",itmed,natmed);
1496 //Pad the string with blanks
1499 while(i<20) natmed[i++]=' ';
1502 if( idtmed[itmed]<=0 ) {
1503 Error("ReadEuclid","TMED undefined medium number %d for %s\n",itmed,natmed);
1506 gMC->Gckmat(idtmed[itmed],natmed);
1508 } else if (!strcmp(key,"ROTM")) {
1509 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
1510 if( irot<=0 || irot>=maxrot ) {
1511 Error("ReadEuclid","ROTM rotation matrix number %d illegal\n",irot);
1514 det->AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
1516 } else if (!strcmp(key,"VOLU")) {
1517 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
1519 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
1522 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
1523 //* save the defined volumes
1524 strcpy(volst[++nvol],name);
1527 } else if (!strcmp(key,"DIVN")) {
1528 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
1529 gMC->Gsdvn ( name, mother, ndiv, iaxe );
1531 } else if (!strcmp(key,"DVN2")) {
1532 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
1533 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
1535 } else if (!strcmp(key,"DIVT")) {
1536 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
1537 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
1539 } else if (!strcmp(key,"DVT2")) {
1540 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
1541 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
1543 } else if (!strcmp(key,"POSI")) {
1544 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
1545 if( irot<0 || irot>=maxrot ) {
1546 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
1549 if( idrot[irot] == -99) {
1550 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
1553 //*** volume name cannot be the top volume
1554 for(i=1;i<=nvol;i++) {
1555 if (!strcmp(volst[i],name)) istop[i]=0;
1558 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
1560 } else if (!strcmp(key,"POSP")) {
1561 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
1562 if( irot<0 || irot>=maxrot ) {
1563 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
1566 if( idrot[irot] == -99) {
1567 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
1571 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
1574 //*** volume name cannot be the top volume
1575 for(i=1;i<=nvol;i++) {
1576 if (!strcmp(volst[i],name)) istop[i]=0;
1579 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
1582 if (strcmp(key,"END")) goto L10;
1583 //* find top volume in the geometry
1585 for(i=1;i<=nvol;i++) {
1586 if (istop[i] && flag) {
1587 Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
1589 if (istop[i] && !flag) {
1590 strcpy(topvol,volst[i]);
1591 printf(" *** GREUCL *** volume %s taken as a top volume\n",topvol);
1596 Warning("ReadEuclid","top volume not found\n");
1600 //* commented out only for the not cernlib version
1601 printf(" *** GREUCL *** file: %s is now read in\n",filnam);
1606 Error("ReadEuclid","reading error or premature end of file\n");
1609 //_____________________________________________________________________________
1610 void AliRun::ReadEuclidMedia(const char* filnam, const AliModule *det)
1613 // read in the materials and tracking media for the detector
1614 // in euclid file format
1616 // filnam: name of the input file
1617 // id_det: id_det is the detector identification (2=its,...)
1619 // author : miroslav helbich
1621 Float_t sxmgmx = gAlice->Field()->Max();
1622 Int_t isxfld = gAlice->Field()->Integ();
1623 Int_t end, i, iret, itmed;
1624 char key[5], card[130], natmed[21], namate[21];
1629 Int_t nwbuf, isvol, ifield, nmat;
1630 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
1633 for(i=0;i<end;i++) if(filnam[i]=='.') {
1638 // *** The input filnam name will be with extension '.euc'
1639 printf("The file name is %s\n",filnam); //Debug
1640 filtmp=gSystem->ExpandPathName(filnam);
1641 lun=fopen(filtmp,"r");
1644 Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
1648 // Retrieve Mag Field parameters
1649 Int_t ISXFLD=gAlice->Field()->Integ();
1650 Float_t SXMGMX=gAlice->Field()->Max();
1651 // TArrayI &idtmed = *(det->GetIdtmed());
1654 for(i=0;i<130;i++) card[i]=0;
1655 iret=fscanf(lun,"%4s %[^\n]",key,card);
1656 if(iret<=0) goto L20;
1660 if (!strcmp(key,"MATE")) {
1661 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
1662 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
1663 //Pad the string with blanks
1666 while(i<20) namate[i++]=' ';
1669 det->AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
1670 //* read tracking medium
1671 } else if (!strcmp(key,"TMED")) {
1672 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
1673 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
1674 &stemax,&deemax,&epsil,&stmin,&nwbuf);
1675 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
1676 if (ifield<0) ifield=isxfld;
1677 if (fieldm<0) fieldm=sxmgmx;
1678 //Pad the string with blanks
1681 while(i<20) natmed[i++]=' ';
1684 det->AliMedium(itmed,natmed,nmat,isvol,ISXFLD,SXMGMX,tmaxfd,
1685 stemax,deemax,epsil,stmin,ubuf,nwbuf);
1686 // (*fImedia)[idtmed[itmed]-1]=id_det;
1690 if (strcmp(key,"END")) goto L10;
1693 //* commented out only for the not cernlib version
1694 Warning("ReadEuclidMedia","file: %s is now read in\n",filnam);
1699 Warning("ReadEuclidMedia","reading error or premature end of file\n");
1702 //_____________________________________________________________________________
1703 void AliRun::Streamer(TBuffer &R__b)
1706 // Stream an object of class AliRun.
1708 if (R__b.IsReading()) {
1709 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1710 TNamed::Streamer(R__b);
1711 if (!gAlice) gAlice = this;
1712 gROOT->GetListOfBrowsables()->Add(this,"Run");
1713 fTreeE = (TTree*)gDirectory->Get("TE");
1714 if (fTreeE) fTreeE->SetBranchAddress("Header", &header);
1715 else Error("Streamer","cannot find Header Tree\n");
1719 fHeader.Streamer(R__b);
1729 R__b >> fPDGDB; //Particle factory object!
1730 fTreeE->GetEntry(0);
1732 fHeader.SetEvent(0);
1733 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
1736 R__b.WriteVersion(AliRun::IsA());
1737 TNamed::Streamer(R__b);
1741 fHeader.Streamer(R__b);
1750 R__b << fPDGDB; //Particle factory object!
1755 //_____________________________________________________________________________
1757 // Interfaces to Fortran
1759 //_____________________________________________________________________________
1761 extern "C" void type_of_call rxgtrak (Int_t &mtrack, Int_t &ipart, Float_t *pmom,
1762 Float_t &e, Float_t *vpos, Float_t *polar,
1766 // Fetches next track from the ROOT stack for transport. Called by the
1767 // modified version of GTREVE.
1769 // Track number in the ROOT stack. If MTRACK=0 no
1770 // mtrack more tracks are left in the stack to be
1772 // ipart Particle code in the GEANT conventions.
1773 // pmom[3] Particle momentum in GeV/c
1774 // e Particle energy in GeV
1775 // vpos[3] Particle position
1776 // tof Particle time of flight in seconds
1779 gAlice->GetNextTrack(mtrack, pdg, pmom, e, vpos, polar, tof);
1780 ipart = gMC->IdFromPDG(pdg);
1784 //_____________________________________________________________________________
1785 extern "C" void type_of_call
1787 rxstrak (Int_t &keep, Int_t &parent, Int_t &ipart, Float_t *pmom,
1788 Float_t *vpos, Float_t &tof, const char* cmech, Int_t &ntr, const int cmlen)
1790 rxstrak (Int_t &keep, Int_t &parent, Int_t &ipart, Float_t *pmom,
1791 Float_t *vpos, Float_t &tof, const char* cmech, const int cmlen,
1796 // Fetches next track from the ROOT stack for transport. Called by GUKINE
1799 // Status of the track. If keep=0 the track is put
1800 // keep on the ROOT stack but it is not fetched for
1802 // parent Parent track. If parent=0 the track is a primary.
1803 // In GUSTEP the routine is normally called to store
1804 // secondaries generated by the current track whose
1805 // ROOT stack number is MTRACK (common SCKINE.
1806 // ipart Particle code in the GEANT conventions.
1807 // pmom[3] Particle momentum in GeV/c
1808 // vpos[3] Particle position
1809 // tof Particle time of flight in seconds
1811 // cmech (CHARACTER*10) Particle origin. This field is user
1812 // defined and it is not used inside the GALICE code.
1813 // ntr Number assigned to the particle in the ROOT stack.
1816 Float_t polar[3]={0.,0.,0.};
1817 for(int i=0; i<10 && i<cmlen; i++) mecha[i]=cmech[i];
1819 Int_t pdg=gMC->PDGFromId(ipart);
1820 gAlice->SetTrack(keep, parent-1, pdg, pmom, vpos, polar, tof, mecha, ntr);
1824 //_____________________________________________________________________________
1825 extern "C" void type_of_call rxkeep(const Int_t &n)
1827 if( NULL==gAlice ) exit(1);
1829 if( n<=0 || n>gAlice->Particles()->GetEntries() )
1831 printf(" Bad index n=%d must be 0<n<=%d\n",
1832 n,gAlice->Particles()->GetEntries());
1836 ((TParticle*)(gAlice->Particles()->UncheckedAt(n-1)))->SetBit(Keep_Bit);
1839 //_____________________________________________________________________________
1840 extern "C" void type_of_call rxouth ()
1843 // Called by Gtreve at the end of each primary track
1845 gAlice->FinishPrimary();