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 <TGeometry.h>
49 #include <TVirtualMC.h>
50 #include <TGeoManager.h>
53 #include "AliDetector.h"
54 #include "AliHeader.h"
56 #include "AliLegoGenerator.h"
59 #include "AliMagFCM.h"
60 #include "AliMagFDM.h"
64 #include "AliCDBManager.h"
65 #include "AliAlignObj.h"
71 //_______________________________________________________________________
84 fIsRootGeometry(kFALSE),
85 fGeometryFromCDB(kFALSE),
86 fGeometryFileName(""),
87 fTriggerDescriptor(""),
91 // Default constructor for AliRun
93 AliConfig::Instance();//skowron 29 Feb 2002
94 //ensures that the folder structure is build
98 //_____________________________________________________________________________
99 AliRun::AliRun(const char *name, const char *title):
104 fModules(new TObjArray(77)), // Support list for the Detectors
109 fConfigFunction("Config();"),
110 fRandom(new TRandom3()),
112 fIsRootGeometry(kFALSE),
113 fGeometryFromCDB(kFALSE),
114 fGeometryFileName(""),
115 fTriggerDescriptor(""),
119 // Constructor for the main processor.
120 // Creates the geometry
121 // Creates the list of Detectors.
122 // Creates the list of particles.
127 // Set random number generator
130 if (gSystem->Getenv("CONFIG_SEED")) {
131 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
134 // Add to list of browsable
135 gROOT->GetListOfBrowsables()->Add(this,name);
137 // Create default mag field
143 //_______________________________________________________________________
147 // Default AliRun destructor
149 gROOT->GetListOfBrowsables()->Remove(this);
153 TFolder* evfold = fRunLoader->GetEventFolder();
154 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
155 TIter next(fModules);
157 while((mod = (AliModule*)next()))
159 modfold->Remove(mod);
175 //_______________________________________________________________________
176 void AliRun::ResetHits()
181 //_______________________________________________________________________
182 AliGenerator* AliRun::Generator() const
184 return fMCApp->Generator();
187 //_______________________________________________________________________
188 void AliRun::SetField(AliMagF* magField)
191 // Set Magnetic Field Map
197 //_______________________________________________________________________
198 void AliRun::SetRootGeometry(Bool_t flag)
200 // Instruct application that the geometry is to be retreived from a root file.
201 fIsRootGeometry = flag;
202 if (flag && gMC) gMC->SetRootGeometry();
205 //_______________________________________________________________________
206 void AliRun::SetGeometryFromCDB()
208 // Set the loading of geometry from cdb instead of creating it
209 // A default CDB storage needs to be set before this method is called
210 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
211 AliCDBManager::Instance()->GetRun() >= 0){
213 fGeometryFromCDB = kTRUE;
215 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
216 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
217 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
221 //_______________________________________________________________________
222 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
223 Float_t maxField, const char* filename)
226 // Set magnetic field parameters
227 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
228 // version Magnetic field map version (only 1 active now)
229 // scale Scale factor for the magnetic field
230 // maxField Maximum value for the magnetic field
233 // --- Sanity check on mag field flags
234 if(fField) delete fField;
236 fField = new AliMagFC("Map1"," ",type,scale,maxField);
237 } else if(version<=2) {
238 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
240 } else if(version==3) {
241 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
244 AliWarning(Form("Invalid map %d",version));
248 //_____________________________________________________________________________
250 void AliRun::InitLoaders()
252 //creates list of getters
254 TIter next(fModules);
256 while((mod = (AliModule*)next()))
258 mod->SetRunLoader(fRunLoader);
259 AliDetector *det = dynamic_cast<AliDetector*>(mod);
262 AliDebug(2, Form("Adding %s", det->GetName()));
263 fRunLoader->AddLoader(det);
268 //_____________________________________________________________________________
270 void AliRun::FinishRun()
273 // Called at the end of the run.
278 AliDebug(1, "Finish Lego");
279 AliRunLoader::GetRunLoader()->CdGAFile();
283 // Clean detector information
284 TIter next(fModules);
286 while((detector = dynamic_cast<AliModule*>(next()))) {
287 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
288 detector->FinishRun();
291 AliDebug(1, "AliRunLoader::GetRunLoader()->WriteHeader(OVERWRITE)");
292 AliRunLoader::GetRunLoader()->WriteHeader("OVERWRITE");
294 // Write AliRun info and all detectors parameters
295 AliRunLoader::GetRunLoader()->CdGAFile();
296 Write(0,TObject::kOverwrite);//write AliRun
297 AliRunLoader::GetRunLoader()->Write(0,TObject::kOverwrite);//write RunLoader itself
299 if(fMCApp) fMCApp->FinishRun();
300 AliRunLoader::GetRunLoader()->Synchronize();
303 //_______________________________________________________________________
304 void AliRun::Announce() const
307 // Announce the current version of AliRoot
310 "****************************************************************\n");
311 printf("%6s","*");printf("%64s","*\n");
314 printf(" You are running AliRoot version NewIO\n");
317 printf(" The SVN version for the current program is $Id$\n");
319 printf("%6s","*");printf("%64s","*\n");
321 "****************************************************************\n");
324 //_______________________________________________________________________
325 AliModule *AliRun::GetModule(const char *name) const
328 // Return pointer to detector from name
330 return dynamic_cast<AliModule*>(fModules->FindObject(name));
333 //_______________________________________________________________________
334 AliDetector *AliRun::GetDetector(const char *name) const
337 // Return pointer to detector from name
339 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
342 //_______________________________________________________________________
343 Int_t AliRun::GetModuleID(const char *name) const
346 // Return galice internal detector identifier from name
349 TObject *mod=fModules->FindObject(name);
350 if(mod) i=fModules->IndexOf(mod);
354 //_______________________________________________________________________
355 Int_t AliRun::GetEvent(Int_t event)
358 // Reloads data containers in folders # event
359 // Set branch addresses
361 if (fRunLoader == 0x0)
363 AliError("RunLoader is not set. Can not load data.");
366 /*****************************************/
367 /**** P R E R E L O A D I N G ****/
368 /*****************************************/
369 // Reset existing structures
371 fMCApp->ResetTrackReferences();
375 /*****************************************/
376 /**** R E L O A D ****/
377 /*****************************************/
379 AliRunLoader::GetRunLoader()->GetEvent(event);
381 /*****************************************/
382 /**** P O S T R E L O A D I N G ****/
383 /*****************************************/
385 // Set Trees branch addresses
386 TIter next(fModules);
387 AliDetector *detector;
388 while((detector = dynamic_cast<AliDetector*>(next())))
390 detector->SetTreeAddress();
393 return AliRunLoader::GetRunLoader()->GetHeader()->GetNtrack();
396 //_______________________________________________________________________
397 void AliRun::SetBaseFile(const char *filename)
399 fBaseFileName = filename;
402 //_______________________________________________________________________
403 void AliRun::ResetDigits()
406 // Reset all Detectors digits
408 TIter next(fModules);
410 while((detector = dynamic_cast<AliModule*>(next()))) {
411 detector->ResetDigits();
415 //_______________________________________________________________________
416 void AliRun::ResetSDigits()
419 // Reset all Detectors digits
421 TIter next(fModules);
423 while((detector = dynamic_cast<AliModule*>(next()))) {
424 detector->ResetSDigits();
429 //_______________________________________________________________________
430 void AliRun::InitMC(const char *setup)
433 // Initialize ALICE Simulation run
436 static Bool_t initDone=kFALSE;
441 AliError("AliRun already initialised! Check your logic!");
443 } else initDone=kTRUE;
446 fMCApp=new AliMC(GetName(),GetTitle());
448 gROOT->LoadMacro(setup);
449 gInterpreter->ProcessLine(fConfigFunction.Data());
451 if(AliCDBManager::Instance()->GetRun() >= 0) {
452 SetRunNumber(AliCDBManager::Instance()->GetRun());
454 AliWarning("Run number not initialized!!");
457 AliRunLoader::GetRunLoader()->CdGAFile();
459 AliPDG::AddParticlesToPdgDataBase();
463 //Must be here because some MCs (G4) adds detectors here and not in Config.C
465 AliRunLoader::GetRunLoader()->MakeTree("E");
468 AliRunLoader::GetRunLoader()->LoadKinematics("RECREATE");
469 AliRunLoader::GetRunLoader()->LoadTrackRefs("RECREATE");
470 AliRunLoader::GetRunLoader()->LoadHits("all","RECREATE");
473 // Save stuff at the beginning of the file to avoid file corruption
474 AliRunLoader::GetRunLoader()->CdGAFile();
476 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
479 //_______________________________________________________________________
480 void AliRun::Hits2Digits(const char *selected)
483 // Convert Hits to sumable digits
485 for (Int_t nevent=0; nevent<AliRunLoader::GetRunLoader()->TreeE()->GetEntries(); nevent++) {
487 Hits2SDigits(selected);
488 SDigits2Digits(selected);
493 //_______________________________________________________________________
495 void AliRun::Tree2Tree(Option_t *option, const char *selected)
498 // Function to transform the content of
500 // - TreeH to TreeS (option "S")
501 // - TreeS to TreeD (option "D")
502 // - TreeD to TreeR (option "R")
504 // If multiple options are specified ("SDR"), transformation will be done in sequence for
505 // selected detector and for all detectors if none is selected (detector string
506 // can contain blank separated list of detector names).
509 const char *oS = strstr(option,"S");
510 const char *oD = strstr(option,"D");
511 const char *oR = strstr(option,"R");
513 TObjArray *detectors = Detectors();
515 TIter next(detectors);
517 AliDetector *detector = 0;
519 while((detector = dynamic_cast<AliDetector*>(next()))) {
521 if (strcmp(detector->GetName(),selected)) continue;
522 if (detector->IsActive())
525 AliLoader* loader = detector->GetLoader();
526 if (loader == 0x0) continue;
530 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
531 loader->LoadHits("read");
532 if (loader->TreeS() == 0x0) loader->MakeTree("S");
533 detector->MakeBranch(option);
534 detector->SetTreeAddress();
535 detector->Hits2SDigits();
536 loader->UnloadHits();
537 loader->UnloadSDigits();
541 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
542 loader->LoadSDigits("read");
543 if (loader->TreeD() == 0x0) loader->MakeTree("D");
544 detector->MakeBranch(option);
545 detector->SetTreeAddress();
546 detector->SDigits2Digits();
547 loader->UnloadSDigits();
548 loader->UnloadDigits();
552 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
553 loader->LoadDigits("read");
554 if (loader->TreeR() == 0x0) loader->MakeTree("R");
555 detector->MakeBranch(option);
556 detector->SetTreeAddress();
557 detector->Digits2Reco();
558 loader->UnloadDigits();
559 loader->UnloadRecPoints();
566 //_______________________________________________________________________
567 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
568 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
569 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
572 // Generates lego plots of:
573 // - radiation length map phi vs theta
574 // - radiation length map phi vs eta
575 // - interaction length map
576 // - g/cm2 length map
578 // ntheta bins in theta, eta
579 // themin minimum angle in theta (degrees)
580 // themax maximum angle in theta (degrees)
582 // phimin minimum angle in phi (degrees)
583 // phimax maximum angle in phi (degrees)
584 // rmin minimum radius
585 // rmax maximum radius
588 // The number of events generated = ntheta*nphi
589 // run input parameters in macro setup (default="Config.C")
591 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
594 <img src="picts/AliRunLego1.gif">
599 <img src="picts/AliRunLego2.gif">
604 <img src="picts/AliRunLego3.gif">
609 if (nev == -1) nev = nc1 * nc2;
611 // check if initialisation has been done
612 // If runloader has been initialized, set the number of events per file to nc1 * nc2
615 if (!gener) gener = new AliLegoGenerator();
617 // Configure Generator
619 gener->SetRadiusRange(rmin, rmax);
620 gener->SetZMax(zmax);
621 gener->SetCoor1Range(nc1, c1min, c1max);
622 gener->SetCoor2Range(nc2, c2min, c2max);
626 fLego = new AliLego("lego",gener);
629 //Save current generator
631 AliGenerator *gen=fMCApp->Generator();
632 fMCApp->ResetGenerator(gener);
633 //Prepare MC for Lego Run
639 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nev);
640 gMC->ProcessRun(nev);
642 // End of this run, close files
644 // Restore current generator
645 fMCApp->ResetGenerator(gen);
646 // Delete Lego Object
647 delete fLego; fLego=0;
650 //_______________________________________________________________________
651 void AliRun::SetConfigFunction(const char * config)
654 // Set the signature of the function contained in Config.C to configure
657 fConfigFunction=config;
664 //_______________________________________________________________________
665 void AliRun::Field(const Double_t* x, Double_t *b) const
668 // Return the value of the magnetic field
672 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
677 Field()->Field(xfloat,bfloat);
678 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
681 AliError("No mag field defined!");
689 // End of MC Application
692 //_______________________________________________________________________
693 void AliRun::Streamer(TBuffer &R__b)
695 // Stream an object of class AliRun.
697 if (R__b.IsReading()) {
698 if (!gAlice) gAlice = this;
699 AliRun::Class()->ReadBuffer(R__b, this);
700 gROOT->GetListOfBrowsables()->Add(this,"Run");
704 AliRun::Class()->WriteBuffer(R__b, this);
707 //_______________________________________________________________________
709 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
711 AliRunLoader::GetRunLoader()->GetHeader()->SetGenEventHeader(header);
715 //_______________________________________________________________________
717 Int_t AliRun::GetEvNumber() const
719 //Returns number of current event
720 if (fRunLoader == 0x0)
722 AliError("RunLoader is not set. Can not load data.");
726 return fRunLoader->GetEventNumber();
728 //_______________________________________________________________________
730 void AliRun::SetRunLoader(AliRunLoader* rloader)
733 // Set the loader of the run
735 fRunLoader = rloader;
736 if (fRunLoader == 0x0) return;
739 TFolder* evfold = fRunLoader->GetEventFolder();
740 if (evfold) evfoldname = evfold->GetName();
741 else AliWarning("Did not get Event Folder from Run Loader");
743 if ( fRunLoader->GetAliRun() )
744 {//if alrun already exists in folder
745 if (fRunLoader->GetAliRun() != this )
746 {//and is different than this - crash
747 AliFatal("AliRun is already in Folder and it is not this object");
753 evfold->Add(this);//Post this AliRun to Folder
756 TIter next(fModules);
758 while((module = (AliModule*)next()))
760 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
761 module->SetRunLoader(fRunLoader);
762 AliDetector* detector = dynamic_cast<AliDetector*>(module);
765 AliLoader* loader = fRunLoader->GetLoader(detector);
768 AliError(Form("Can not get loader for detector %s", detector->GetName()));
772 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
773 detector->SetLoader(loader);
779 void AliRun::AddModule(AliModule* mod)
782 // Add a module to the module list
784 if (mod == 0x0) return;
785 if (strlen(mod->GetName()) == 0) return;
786 if (GetModuleID(mod->GetName()) >= 0) return;
788 AliDebug(1, mod->GetName());
789 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
790 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
797 //_____________________________________________________________________________
798 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
799 { return !gSystem->AccessPathName(fnam,mode);}
801 //______________________________________________________
802 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
804 TString str = name; gSystem->ExpandPathName(str);
805 return !gSystem->AccessPathName(str.Data(),mode);