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 ///////////////////////////////////////////////////////////////////////////////
20 // Control class for Alice C++ //
21 // Only one single instance of this class exists. //
22 // The object is created in main program aliroot //
23 // and is pointed by the global gAlice. //
25 // -Supports the list of all Alice Detectors (fModules). //
26 // -Supports the list of particles (fParticles). //
27 // -Supports the Trees. //
28 // -Supports the geometry. //
29 // -Supports the event display. //
32 <img src="picts/AliRunClass.gif">
37 <img src="picts/alirun.gif">
41 ///////////////////////////////////////////////////////////////////////////////
45 #include <TDatabasePDG.h>
46 #include <TGeometry.h>
51 #include <TVirtualMC.h>
52 #include <TGeoManager.h>
55 #include "AliDetector.h"
56 #include "AliDisplay.h"
57 #include "AliHeader.h"
59 #include "AliLegoGenerator.h"
62 #include "AliMagFCM.h"
63 #include "AliMagFDM.h"
68 //#include "AliTPCTrackHitsInterfaces.h"
75 //_______________________________________________________________________
90 fPDGDB(0), //Particle factory object
91 fConfigFunction("\0"),
93 fIsRootGeometry(kFALSE),
97 // Default constructor for AliRun
99 AliConfig::Instance();//skowron 29 Feb 2002
100 //ensures that the folder structure is build
103 //_______________________________________________________________________
104 AliRun::AliRun(const AliRun& arun):
119 fPDGDB(0), //Particle factory object
120 fConfigFunction("\0"),
122 fIsRootGeometry(kFALSE),
126 // Copy constructor for AliRun
131 //_____________________________________________________________________________
132 AliRun::AliRun(const char *name, const char *title):
138 fModules(new TObjArray(77)), // Support list for the Detectors
147 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
148 fConfigFunction("Config();"),
149 fRandom(new TRandom3()),
150 fIsRootGeometry(kFALSE),
154 // Constructor for the main processor.
155 // Creates the geometry
156 // Creates the list of Detectors.
157 // Creates the list of particles.
162 // Set random number generator
165 if (gSystem->Getenv("CONFIG_SEED")) {
166 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
169 // Add to list of browsable
170 gROOT->GetListOfBrowsables()->Add(this,name);
171 // Create the TNode geometry for the event display
172 BuildSimpleGeometry();
174 // Create default mag field
177 // Add particle list to configuration
178 AliConfig::Instance()->Add(fPDGDB);
181 // Terrible hack to avoid problem with the initialisation of
182 // static and globals on Mac OS X
183 // AliClassAliTrackHitsInfo p1=galiclass____AliClassAliTrackHitsInfo;
184 // AliClassAliTrackHitsParam p2=galiclass____AliTrackHitsParam;
185 //AliClassAliHitInfo p3=galiclass____AliHitInfo;
191 //_______________________________________________________________________
195 // Default AliRun destructor
197 gROOT->GetListOfBrowsables()->Remove(this);
201 TFolder* evfold = fRunLoader->GetEventFolder();
202 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
203 TIter next(fModules);
205 while((mod = (AliModule*)next()))
207 modfold->Remove(mod);
226 //_______________________________________________________________________
227 void AliRun::Copy(TObject &) const
229 AliFatal("Not implemented!");
232 //_______________________________________________________________________
236 // Initialize Alice geometry
241 //_______________________________________________________________________
242 void AliRun::BuildSimpleGeometry()
245 // Create a simple TNode geometry used by Root display engine
247 // Initialise geometry
249 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
250 new TMaterial("void","Vacuum",0,0,0); //Everything is void
251 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
252 brik->SetVisibility(0);
253 new TNode("alice","alice","S_alice");
256 //_______________________________________________________________________
257 void AliRun::CleanDetectors()
260 // Clean Detectors at the end of event
262 fRunLoader->CleanDetectors();
265 //_______________________________________________________________________
266 void AliRun::ResetHits()
271 //_______________________________________________________________________
272 AliGenerator* AliRun::Generator() const
274 return fMCApp->Generator();
277 //_______________________________________________________________________
278 void AliRun::SetField(AliMagF* magField)
281 // Set Magnetic Field Map
287 //_______________________________________________________________________
288 void AliRun::SetRootGeometry(Bool_t flag)
290 // Instruct application that the geometry is to be retreived from a root file.
291 fIsRootGeometry = flag;
292 if (flag) gMC->SetRootGeometry();
295 //_______________________________________________________________________
296 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
297 Float_t maxField, const char* filename)
300 // Set magnetic field parameters
301 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
302 // version Magnetic field map version (only 1 active now)
303 // scale Scale factor for the magnetic field
304 // maxField Maximum value for the magnetic field
307 // --- Sanity check on mag field flags
308 if(fField) delete fField;
310 fField = new AliMagFC("Map1"," ",type,scale,maxField);
311 } else if(version<=2) {
312 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
314 } else if(version==3) {
315 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
318 AliWarning(Form("Invalid map %d",version));
322 //_____________________________________________________________________________
324 void AliRun::InitLoaders()
326 //creates list of getters
328 TIter next(fModules);
330 while((mod = (AliModule*)next()))
332 mod->SetRunLoader(fRunLoader);
333 AliDetector *det = dynamic_cast<AliDetector*>(mod);
336 AliDebug(2, Form("Adding %s", det->GetName()));
337 fRunLoader->AddLoader(det);
342 //_____________________________________________________________________________
344 void AliRun::FinishRun()
347 // Called at the end of the run.
352 AliDebug(1, "Finish Lego");
353 fRunLoader->CdGAFile();
357 // Clean detector information
358 TIter next(fModules);
360 while((detector = dynamic_cast<AliModule*>(next()))) {
361 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
362 detector->FinishRun();
365 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
366 fRunLoader->WriteHeader("OVERWRITE");
368 // Write AliRun info and all detectors parameters
369 fRunLoader->CdGAFile();
370 Write(0,TObject::kOverwrite);//write AliRun
371 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
373 // Clean tree information
374 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
375 fRunLoader->Stack()->FinishRun();
377 if(fMCApp) fMCApp->FinishRun();
379 fRunLoader->Synchronize();
382 //_______________________________________________________________________
383 void AliRun::Announce() const
386 // Announce the current version of AliRoot
389 "****************************************************************\n");
390 printf("%6s","*");printf("%64s","*\n");
393 printf(" You are running AliRoot version NewIO\n");
396 printf(" The cvs tag for the current program is $Name$\n");
398 printf("%6s","*");printf("%64s","*\n");
400 "****************************************************************\n");
403 //_______________________________________________________________________
404 AliModule *AliRun::GetModule(const char *name) const
407 // Return pointer to detector from name
409 return dynamic_cast<AliModule*>(fModules->FindObject(name));
412 //_______________________________________________________________________
413 AliDetector *AliRun::GetDetector(const char *name) const
416 // Return pointer to detector from name
418 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
421 //_______________________________________________________________________
422 Int_t AliRun::GetModuleID(const char *name) const
425 // Return galice internal detector identifier from name
428 TObject *mod=fModules->FindObject(name);
429 if(mod) i=fModules->IndexOf(mod);
433 //_______________________________________________________________________
434 Int_t AliRun::GetEvent(Int_t event)
437 // Reloads data containers in folders # event
438 // Set branch addresses
440 if (fRunLoader == 0x0)
442 AliError("RunLoader is not set. Can not load data.");
445 /*****************************************/
446 /**** P R E R E L O A D I N G ****/
447 /*****************************************/
448 // Reset existing structures
450 fMCApp->ResetTrackReferences();
454 /*****************************************/
455 /**** R E L O A D ****/
456 /*****************************************/
458 fRunLoader->GetEvent(event);
460 /*****************************************/
461 /**** P O S T R E L O A D I N G ****/
462 /*****************************************/
464 // Set Trees branch addresses
465 TIter next(fModules);
467 while((detector = dynamic_cast<AliModule*>(next())))
469 detector->SetTreeAddress();
472 return fRunLoader->GetHeader()->GetNtrack();
475 //_______________________________________________________________________
476 TGeometry *AliRun::GetGeometry()
479 // Import Alice geometry from current file
480 // Return pointer to geometry object
482 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
484 // Unlink and relink nodes in detectors
485 // This is bad and there must be a better way...
488 TIter next(fModules);
490 while((detector = dynamic_cast<AliModule*>(next()))) {
491 TList *dnodes=detector->Nodes();
494 for ( j=0; j<dnodes->GetSize(); j++) {
495 node = dynamic_cast<TNode*>(dnodes->At(j));
496 node1 = fGeometry->GetNode(node->GetName());
497 dnodes->Remove(node);
498 dnodes->AddAt(node1,j);
504 //_______________________________________________________________________
505 void AliRun::SetBaseFile(const char *filename)
507 fBaseFileName = filename;
510 //_______________________________________________________________________
511 void AliRun::ResetDigits()
514 // Reset all Detectors digits
516 TIter next(fModules);
518 while((detector = dynamic_cast<AliModule*>(next()))) {
519 detector->ResetDigits();
523 //_______________________________________________________________________
524 void AliRun::ResetSDigits()
527 // Reset all Detectors digits
529 TIter next(fModules);
531 while((detector = dynamic_cast<AliModule*>(next()))) {
532 detector->ResetSDigits();
537 //_______________________________________________________________________
539 void AliRun::ResetPoints()
542 // Reset all Detectors points
544 TIter next(fModules);
546 while((detector = dynamic_cast<AliModule*>(next()))) {
547 detector->ResetPoints();
550 //_______________________________________________________________________
552 void AliRun::InitMC(const char *setup)
555 // Initialize ALICE Simulation run
560 AliWarning("Cannot initialise AliRun twice!");
565 fMCApp=new AliMC(GetName(),GetTitle());
567 gROOT->LoadMacro(setup);
568 gInterpreter->ProcessLine(fConfigFunction.Data());
570 fRunLoader->CdGAFile();
572 AliPDG::AddParticlesToPdgDataBase();
574 fNdets = fModules->GetLast()+1;
576 TIter next(fModules);
577 for(Int_t i=0; i<fNdets; ++i)
579 TObject *objfirst, *objlast;
580 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
581 objlast = gDirectory->GetList()->Last();
583 // Add Detector histograms in Detector list of histograms
584 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
585 else objfirst = gDirectory->GetList()->First();
588 detector->Histograms()->Add(objfirst);
589 objfirst = gDirectory->GetList()->After(objfirst);
595 //Must be here because some MCs (G4) adds detectors here and not in Config.C
597 fRunLoader->MakeTree("E");
600 fRunLoader->LoadKinematics("RECREATE");
601 fRunLoader->LoadTrackRefs("RECREATE");
602 fRunLoader->LoadHits("all","RECREATE");
606 // Save stuff at the beginning of the file to avoid file corruption
607 fRunLoader->CdGAFile();
609 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
612 //_______________________________________________________________________
614 void AliRun::RunMC(Int_t nevent, const char *setup)
617 // Main function to be called to process a galice run
619 // Root > gAlice.Run();
620 // a positive number of events will cause the finish routine
623 fEventsPerRun = nevent;
624 // check if initialisation has been done
625 if (!fInitDone) InitMC(setup);
627 // Create the Root Tree with one branch per detector
628 //Hits moved to begin event -> now we are crating separate tree for each event
630 gMC->ProcessRun(nevent);
631 // cout<<"\n***** "<<gGeoManager->GetMaterial("TPCEpoxy")->GetA()<<" ****\n";
633 // End of this run, close files
634 if(nevent>0) FinishRun();
637 //_______________________________________________________________________
638 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
641 // Main function to be called to reconstruct Alice event
643 Int_t nev = fRunLoader->GetNumberOfEvents();
644 AliDebug(1, Form("Found %d events", nev));
645 Int_t nFirst = first;
646 Int_t nLast = (last < 0)? nev : last;
648 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
649 AliDebug(1, Form("Processing event %d", nevent));
651 Digits2Reco(selected);
655 //_______________________________________________________________________
657 void AliRun::Hits2Digits(const char *selected)
660 // Convert Hits to sumable digits
662 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
664 Hits2SDigits(selected);
665 SDigits2Digits(selected);
670 //_______________________________________________________________________
672 void AliRun::Tree2Tree(Option_t *option, const char *selected)
675 // Function to transform the content of
677 // - TreeH to TreeS (option "S")
678 // - TreeS to TreeD (option "D")
679 // - TreeD to TreeR (option "R")
681 // If multiple options are specified ("SDR"), transformation will be done in sequence for
682 // selected detector and for all detectors if none is selected (detector string
683 // can contain blank separated list of detector names).
686 const char *oS = strstr(option,"S");
687 const char *oD = strstr(option,"D");
688 const char *oR = strstr(option,"R");
690 TObjArray *detectors = Detectors();
692 TIter next(detectors);
694 AliDetector *detector = 0;
696 while((detector = dynamic_cast<AliDetector*>(next()))) {
698 if (strcmp(detector->GetName(),selected)) continue;
699 if (detector->IsActive())
702 AliLoader* loader = detector->GetLoader();
703 if (loader == 0x0) continue;
707 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
708 loader->LoadHits("read");
709 if (loader->TreeS() == 0x0) loader->MakeTree("S");
710 detector->MakeBranch(option);
711 detector->SetTreeAddress();
712 detector->Hits2SDigits();
713 loader->UnloadHits();
714 loader->UnloadSDigits();
718 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
719 loader->LoadSDigits("read");
720 if (loader->TreeD() == 0x0) loader->MakeTree("D");
721 detector->MakeBranch(option);
722 detector->SetTreeAddress();
723 detector->SDigits2Digits();
724 loader->UnloadSDigits();
725 loader->UnloadDigits();
729 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
730 loader->LoadDigits("read");
731 if (loader->TreeR() == 0x0) loader->MakeTree("R");
732 detector->MakeBranch(option);
733 detector->SetTreeAddress();
734 detector->Digits2Reco();
735 loader->UnloadDigits();
736 loader->UnloadRecPoints();
743 //_______________________________________________________________________
744 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
745 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
746 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
749 // Generates lego plots of:
750 // - radiation length map phi vs theta
751 // - radiation length map phi vs eta
752 // - interaction length map
753 // - g/cm2 length map
755 // ntheta bins in theta, eta
756 // themin minimum angle in theta (degrees)
757 // themax maximum angle in theta (degrees)
759 // phimin minimum angle in phi (degrees)
760 // phimax maximum angle in phi (degrees)
761 // rmin minimum radius
762 // rmax maximum radius
765 // The number of events generated = ntheta*nphi
766 // run input parameters in macro setup (default="Config.C")
768 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
771 <img src="picts/AliRunLego1.gif">
776 <img src="picts/AliRunLego2.gif">
781 <img src="picts/AliRunLego3.gif">
786 // check if initialisation has been done
787 // If runloader has been initialized, set the number of events per file to nc1 * nc2
790 if (!gener) gener = new AliLegoGenerator();
792 // Configure Generator
793 gener->SetRadiusRange(rmin, rmax);
794 gener->SetZMax(zmax);
795 gener->SetCoor1Range(nc1, c1min, c1max);
796 gener->SetCoor2Range(nc2, c2min, c2max);
800 fLego = new AliLego("lego",gener);
802 if (!fInitDone) InitMC(setup);
803 //Save current generator
805 AliGenerator *gen=fMCApp->Generator();
806 fMCApp->ResetGenerator(gener);
807 //Prepare MC for Lego Run
812 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
813 //gMC->ProcessRun(nc1*nc2+1);
814 gMC->ProcessRun(nc1*nc2);
816 // End of this run, close files
818 // Restore current generator
819 fMCApp->ResetGenerator(gen);
820 // Delete Lego Object
821 delete fLego; fLego=0;
824 //_______________________________________________________________________
825 void AliRun::SetConfigFunction(const char * config)
828 // Set the signature of the function contained in Config.C to configure
831 fConfigFunction=config;
838 //_______________________________________________________________________
839 void AliRun::Field(const Double_t* x, Double_t *b) const
842 // Return the value of the magnetic field
845 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
849 Field()->Field(xfloat,bfloat);
850 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
853 AliError("No mag field defined!");
859 // End of MC Application
862 //_______________________________________________________________________
863 void AliRun::Streamer(TBuffer &R__b)
865 // Stream an object of class AliRun.
867 if (R__b.IsReading()) {
868 if (!gAlice) gAlice = this;
869 AliRun::Class()->ReadBuffer(R__b, this);
870 gROOT->GetListOfBrowsables()->Add(this,"Run");
874 AliRun::Class()->WriteBuffer(R__b, this);
877 //_______________________________________________________________________
879 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
881 fRunLoader->GetHeader()->SetGenEventHeader(header);
883 //_______________________________________________________________________
885 Int_t AliRun::GetEvNumber() const
887 //Returns number of current event
888 if (fRunLoader == 0x0)
890 AliError("RunLoader is not set. Can not load data.");
894 return fRunLoader->GetEventNumber();
896 //_______________________________________________________________________
898 void AliRun::SetRunLoader(AliRunLoader* rloader)
901 // Set the loader of the run
903 fRunLoader = rloader;
904 if (fRunLoader == 0x0) return;
907 TFolder* evfold = fRunLoader->GetEventFolder();
908 if (evfold) evfoldname = evfold->GetName();
909 else AliWarning("Did not get Event Folder from Run Loader");
911 if ( fRunLoader->GetAliRun() )
912 {//if alrun already exists in folder
913 if (fRunLoader->GetAliRun() != this )
914 {//and is different than this - crash
915 AliFatal("AliRun is already in Folder and it is not this object");
921 evfold->Add(this);//Post this AliRun to Folder
924 TIter next(fModules);
926 while((module = (AliModule*)next()))
928 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
929 module->SetRunLoader(fRunLoader);
930 AliDetector* detector = dynamic_cast<AliDetector*>(module);
933 AliLoader* loader = fRunLoader->GetLoader(detector);
936 AliError(Form("Can not get loader for detector %s", detector->GetName()));
940 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
941 detector->SetLoader(loader);
947 void AliRun::AddModule(AliModule* mod)
950 // Add a module to the module list
952 if (mod == 0x0) return;
953 if (strlen(mod->GetName()) == 0) return;
954 if (GetModuleID(mod->GetName()) >= 0) return;
956 AliDebug(1, mod->GetName());
957 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
958 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
966 //_______________________________________________________________________
967 Int_t AliRun::GetDebug() const
969 AliWarning("Don't use this method any more, use AliDebug instead");
970 return AliDebugLevel();
973 //_______________________________________________________________________
974 void AliRun::SetDebug(Int_t level)
976 AliWarning("Don't use this method any more, use AliLog instead");
977 AliLog::SetClassDebugLevel("AliRun", level);
981 // added by Alberto Colla
982 //_____________________________________________________________________________
983 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
984 { return !gSystem->AccessPathName(fnam,mode);}
986 //______________________________________________________
987 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
989 TString str = name; gSystem->ExpandPathName(str);
990 return !gSystem->AccessPathName(str.Data(),mode);