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>
50 #include <TVirtualMC.h>
51 #include <TGeoManager.h>
54 #include "AliDetector.h"
55 #include "AliHeader.h"
57 #include "AliLegoGenerator.h"
60 #include "AliMagFCM.h"
61 #include "AliMagFDM.h"
65 #include "AliCDBManager.h"
66 #include "AliAlignObj.h"
72 //_______________________________________________________________________
84 fPDGDB(0), //Particle factory object
88 fIsRootGeometry(kFALSE),
89 fGeometryFromCDB(kFALSE),
90 fGeometryFileName(""),
91 fTriggerDescriptor(""),
95 // Default constructor for AliRun
97 AliConfig::Instance();//skowron 29 Feb 2002
98 //ensures that the folder structure is build
102 //_____________________________________________________________________________
103 AliRun::AliRun(const char *name, const char *title):
109 fModules(new TObjArray(77)), // Support list for the Detectors
115 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
116 fConfigFunction("Config();"),
117 fRandom(new TRandom3()),
119 fIsRootGeometry(kFALSE),
120 fGeometryFromCDB(kFALSE),
121 fGeometryFileName(""),
122 fTriggerDescriptor(""),
126 // Constructor for the main processor.
127 // Creates the geometry
128 // Creates the list of Detectors.
129 // Creates the list of particles.
134 // Set random number generator
137 if (gSystem->Getenv("CONFIG_SEED")) {
138 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
141 // Add to list of browsable
142 gROOT->GetListOfBrowsables()->Add(this,name);
144 // Create default mag field
147 // Add particle list to configuration
148 AliConfig::Instance()->Add(fPDGDB);
153 //_______________________________________________________________________
157 // Default AliRun destructor
159 gROOT->GetListOfBrowsables()->Remove(this);
163 TFolder* evfold = fRunLoader->GetEventFolder();
164 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
165 TIter next(fModules);
167 while((mod = (AliModule*)next()))
169 modfold->Remove(mod);
186 //_______________________________________________________________________
187 void AliRun::ResetHits()
192 //_______________________________________________________________________
193 AliGenerator* AliRun::Generator() const
195 return fMCApp->Generator();
198 //_______________________________________________________________________
199 void AliRun::SetField(AliMagF* magField)
202 // Set Magnetic Field Map
208 //_______________________________________________________________________
209 void AliRun::SetRootGeometry(Bool_t flag)
211 // Instruct application that the geometry is to be retreived from a root file.
212 fIsRootGeometry = flag;
213 if (flag && gMC) gMC->SetRootGeometry();
216 //_______________________________________________________________________
217 void AliRun::SetGeometryFromCDB()
219 // Set the loading of geometry from cdb instead of creating it
220 // A default CDB storage needs to be set before this method is called
221 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
222 AliCDBManager::Instance()->GetRun() >= 0){
224 fGeometryFromCDB = kTRUE;
226 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
227 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
228 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
232 //_______________________________________________________________________
233 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
234 Float_t maxField, const char* filename)
237 // Set magnetic field parameters
238 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
239 // version Magnetic field map version (only 1 active now)
240 // scale Scale factor for the magnetic field
241 // maxField Maximum value for the magnetic field
244 // --- Sanity check on mag field flags
245 if(fField) delete fField;
247 fField = new AliMagFC("Map1"," ",type,scale,maxField);
248 } else if(version<=2) {
249 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
251 } else if(version==3) {
252 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
255 AliWarning(Form("Invalid map %d",version));
259 //_____________________________________________________________________________
261 void AliRun::InitLoaders()
263 //creates list of getters
265 TIter next(fModules);
267 while((mod = (AliModule*)next()))
269 mod->SetRunLoader(fRunLoader);
270 AliDetector *det = dynamic_cast<AliDetector*>(mod);
273 AliDebug(2, Form("Adding %s", det->GetName()));
274 fRunLoader->AddLoader(det);
279 //_____________________________________________________________________________
281 void AliRun::FinishRun()
284 // Called at the end of the run.
289 AliDebug(1, "Finish Lego");
290 AliRunLoader::GetRunLoader()->CdGAFile();
294 // Clean detector information
295 TIter next(fModules);
297 while((detector = dynamic_cast<AliModule*>(next()))) {
298 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
299 detector->FinishRun();
302 AliDebug(1, "AliRunLoader::GetRunLoader()->WriteHeader(OVERWRITE)");
303 AliRunLoader::GetRunLoader()->WriteHeader("OVERWRITE");
305 // Write AliRun info and all detectors parameters
306 AliRunLoader::GetRunLoader()->CdGAFile();
307 Write(0,TObject::kOverwrite);//write AliRun
308 AliRunLoader::GetRunLoader()->Write(0,TObject::kOverwrite);//write RunLoader itself
310 if(fMCApp) fMCApp->FinishRun();
311 AliRunLoader::GetRunLoader()->Synchronize();
314 //_______________________________________________________________________
315 void AliRun::Announce() const
318 // Announce the current version of AliRoot
321 "****************************************************************\n");
322 printf("%6s","*");printf("%64s","*\n");
325 printf(" You are running AliRoot version NewIO\n");
328 printf(" The SVN version for the current program is $Id$\n");
330 printf("%6s","*");printf("%64s","*\n");
332 "****************************************************************\n");
335 //_______________________________________________________________________
336 AliModule *AliRun::GetModule(const char *name) const
339 // Return pointer to detector from name
341 return dynamic_cast<AliModule*>(fModules->FindObject(name));
344 //_______________________________________________________________________
345 AliDetector *AliRun::GetDetector(const char *name) const
348 // Return pointer to detector from name
350 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
353 //_______________________________________________________________________
354 Int_t AliRun::GetModuleID(const char *name) const
357 // Return galice internal detector identifier from name
360 TObject *mod=fModules->FindObject(name);
361 if(mod) i=fModules->IndexOf(mod);
365 //_______________________________________________________________________
366 Int_t AliRun::GetEvent(Int_t event)
369 // Reloads data containers in folders # event
370 // Set branch addresses
372 if (fRunLoader == 0x0)
374 AliError("RunLoader is not set. Can not load data.");
377 /*****************************************/
378 /**** P R E R E L O A D I N G ****/
379 /*****************************************/
380 // Reset existing structures
382 fMCApp->ResetTrackReferences();
386 /*****************************************/
387 /**** R E L O A D ****/
388 /*****************************************/
390 AliRunLoader::GetRunLoader()->GetEvent(event);
392 /*****************************************/
393 /**** P O S T R E L O A D I N G ****/
394 /*****************************************/
396 // Set Trees branch addresses
397 TIter next(fModules);
398 AliDetector *detector;
399 while((detector = dynamic_cast<AliDetector*>(next())))
401 detector->SetTreeAddress();
404 return AliRunLoader::GetRunLoader()->GetHeader()->GetNtrack();
407 //_______________________________________________________________________
408 void AliRun::SetBaseFile(const char *filename)
410 fBaseFileName = filename;
413 //_______________________________________________________________________
414 void AliRun::ResetDigits()
417 // Reset all Detectors digits
419 TIter next(fModules);
421 while((detector = dynamic_cast<AliModule*>(next()))) {
422 detector->ResetDigits();
426 //_______________________________________________________________________
427 void AliRun::ResetSDigits()
430 // Reset all Detectors digits
432 TIter next(fModules);
434 while((detector = dynamic_cast<AliModule*>(next()))) {
435 detector->ResetSDigits();
440 //_______________________________________________________________________
442 void AliRun::InitMC(const char *setup)
445 // Initialize ALICE Simulation run
450 AliWarning("Cannot initialise AliRun twice!");
455 fMCApp=new AliMC(GetName(),GetTitle());
457 gROOT->LoadMacro(setup);
458 gInterpreter->ProcessLine(fConfigFunction.Data());
460 if(AliCDBManager::Instance()->GetRun() >= 0) {
461 SetRunNumber(AliCDBManager::Instance()->GetRun());
463 AliWarning("Run number not initialized!!");
466 AliRunLoader::GetRunLoader()->CdGAFile();
468 AliPDG::AddParticlesToPdgDataBase();
472 //Must be here because some MCs (G4) adds detectors here and not in Config.C
474 AliRunLoader::GetRunLoader()->MakeTree("E");
477 AliRunLoader::GetRunLoader()->LoadKinematics("RECREATE");
478 AliRunLoader::GetRunLoader()->LoadTrackRefs("RECREATE");
479 AliRunLoader::GetRunLoader()->LoadHits("all","RECREATE");
483 // Save stuff at the beginning of the file to avoid file corruption
484 AliRunLoader::GetRunLoader()->CdGAFile();
486 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
489 //_______________________________________________________________________
491 void AliRun::RunMC(Int_t nevent, const char *setup)
494 // Main function to be called to process a galice run
496 // Root > gAlice.Run();
497 // a positive number of events will cause the finish routine
500 fEventsPerRun = nevent;
501 // check if initialisation has been done
502 if (!fInitDone) InitMC(setup);
504 // Create the Root Tree with one branch per detector
505 //Hits moved to begin event -> now we are crating separate tree for each event
507 gMC->ProcessRun(nevent);
509 // End of this run, close files
510 if(nevent>0) FinishRun();
513 //_______________________________________________________________________
515 void AliRun::Hits2Digits(const char *selected)
518 // Convert Hits to sumable digits
520 for (Int_t nevent=0; nevent<AliRunLoader::GetRunLoader()->TreeE()->GetEntries(); nevent++) {
522 Hits2SDigits(selected);
523 SDigits2Digits(selected);
528 //_______________________________________________________________________
530 void AliRun::Tree2Tree(Option_t *option, const char *selected)
533 // Function to transform the content of
535 // - TreeH to TreeS (option "S")
536 // - TreeS to TreeD (option "D")
537 // - TreeD to TreeR (option "R")
539 // If multiple options are specified ("SDR"), transformation will be done in sequence for
540 // selected detector and for all detectors if none is selected (detector string
541 // can contain blank separated list of detector names).
544 const char *oS = strstr(option,"S");
545 const char *oD = strstr(option,"D");
546 const char *oR = strstr(option,"R");
548 TObjArray *detectors = Detectors();
550 TIter next(detectors);
552 AliDetector *detector = 0;
554 while((detector = dynamic_cast<AliDetector*>(next()))) {
556 if (strcmp(detector->GetName(),selected)) continue;
557 if (detector->IsActive())
560 AliLoader* loader = detector->GetLoader();
561 if (loader == 0x0) continue;
565 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
566 loader->LoadHits("read");
567 if (loader->TreeS() == 0x0) loader->MakeTree("S");
568 detector->MakeBranch(option);
569 detector->SetTreeAddress();
570 detector->Hits2SDigits();
571 loader->UnloadHits();
572 loader->UnloadSDigits();
576 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
577 loader->LoadSDigits("read");
578 if (loader->TreeD() == 0x0) loader->MakeTree("D");
579 detector->MakeBranch(option);
580 detector->SetTreeAddress();
581 detector->SDigits2Digits();
582 loader->UnloadSDigits();
583 loader->UnloadDigits();
587 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
588 loader->LoadDigits("read");
589 if (loader->TreeR() == 0x0) loader->MakeTree("R");
590 detector->MakeBranch(option);
591 detector->SetTreeAddress();
592 detector->Digits2Reco();
593 loader->UnloadDigits();
594 loader->UnloadRecPoints();
601 //_______________________________________________________________________
602 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
603 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
604 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
607 // Generates lego plots of:
608 // - radiation length map phi vs theta
609 // - radiation length map phi vs eta
610 // - interaction length map
611 // - g/cm2 length map
613 // ntheta bins in theta, eta
614 // themin minimum angle in theta (degrees)
615 // themax maximum angle in theta (degrees)
617 // phimin minimum angle in phi (degrees)
618 // phimax maximum angle in phi (degrees)
619 // rmin minimum radius
620 // rmax maximum radius
623 // The number of events generated = ntheta*nphi
624 // run input parameters in macro setup (default="Config.C")
626 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
629 <img src="picts/AliRunLego1.gif">
634 <img src="picts/AliRunLego2.gif">
639 <img src="picts/AliRunLego3.gif">
644 if (nev == -1) nev = nc1 * nc2;
646 // check if initialisation has been done
647 // If runloader has been initialized, set the number of events per file to nc1 * nc2
650 if (!gener) gener = new AliLegoGenerator();
652 // Configure Generator
654 gener->SetRadiusRange(rmin, rmax);
655 gener->SetZMax(zmax);
656 gener->SetCoor1Range(nc1, c1min, c1max);
657 gener->SetCoor2Range(nc2, c2min, c2max);
661 fLego = new AliLego("lego",gener);
663 if (!fInitDone) InitMC(setup);
664 //Save current generator
666 AliGenerator *gen=fMCApp->Generator();
667 fMCApp->ResetGenerator(gener);
668 //Prepare MC for Lego Run
674 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nev);
675 gMC->ProcessRun(nev);
677 // End of this run, close files
679 // Restore current generator
680 fMCApp->ResetGenerator(gen);
681 // Delete Lego Object
682 delete fLego; fLego=0;
685 //_______________________________________________________________________
686 void AliRun::SetConfigFunction(const char * config)
689 // Set the signature of the function contained in Config.C to configure
692 fConfigFunction=config;
699 //_______________________________________________________________________
700 void AliRun::Field(const Double_t* x, Double_t *b) const
703 // Return the value of the magnetic field
707 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
712 Field()->Field(xfloat,bfloat);
713 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
716 AliError("No mag field defined!");
724 // End of MC Application
727 //_______________________________________________________________________
728 void AliRun::Streamer(TBuffer &R__b)
730 // Stream an object of class AliRun.
732 if (R__b.IsReading()) {
733 if (!gAlice) gAlice = this;
734 AliRun::Class()->ReadBuffer(R__b, this);
735 gROOT->GetListOfBrowsables()->Add(this,"Run");
739 AliRun::Class()->WriteBuffer(R__b, this);
742 //_______________________________________________________________________
744 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
746 AliRunLoader::GetRunLoader()->GetHeader()->SetGenEventHeader(header);
750 //_______________________________________________________________________
752 Int_t AliRun::GetEvNumber() const
754 //Returns number of current event
755 if (fRunLoader == 0x0)
757 AliError("RunLoader is not set. Can not load data.");
761 return fRunLoader->GetEventNumber();
763 //_______________________________________________________________________
765 void AliRun::SetRunLoader(AliRunLoader* rloader)
768 // Set the loader of the run
770 fRunLoader = rloader;
771 if (fRunLoader == 0x0) return;
774 TFolder* evfold = fRunLoader->GetEventFolder();
775 if (evfold) evfoldname = evfold->GetName();
776 else AliWarning("Did not get Event Folder from Run Loader");
778 if ( fRunLoader->GetAliRun() )
779 {//if alrun already exists in folder
780 if (fRunLoader->GetAliRun() != this )
781 {//and is different than this - crash
782 AliFatal("AliRun is already in Folder and it is not this object");
788 evfold->Add(this);//Post this AliRun to Folder
791 TIter next(fModules);
793 while((module = (AliModule*)next()))
795 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
796 module->SetRunLoader(fRunLoader);
797 AliDetector* detector = dynamic_cast<AliDetector*>(module);
800 AliLoader* loader = fRunLoader->GetLoader(detector);
803 AliError(Form("Can not get loader for detector %s", detector->GetName()));
807 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
808 detector->SetLoader(loader);
814 void AliRun::AddModule(AliModule* mod)
817 // Add a module to the module list
819 if (mod == 0x0) return;
820 if (strlen(mod->GetName()) == 0) return;
821 if (GetModuleID(mod->GetName()) >= 0) return;
823 AliDebug(1, mod->GetName());
824 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
825 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
832 //_____________________________________________________________________________
833 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
834 { return !gSystem->AccessPathName(fnam,mode);}
836 //______________________________________________________
837 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
839 TString str = name; gSystem->ExpandPathName(str);
840 return !gSystem->AccessPathName(str.Data(),mode);