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"
67 #include "AliCDBManager.h"
68 #include "AliAlignObj.h"
74 //_______________________________________________________________________
88 fPDGDB(0), //Particle factory object
92 fIsRootGeometry(kFALSE),
93 fGeometryFromCDB(kFALSE),
94 fGeometryFileName(""),
95 fTriggerDescriptor(""),
99 // Default constructor for AliRun
101 AliConfig::Instance();//skowron 29 Feb 2002
102 //ensures that the folder structure is build
106 //_______________________________________________________________________
107 AliRun::AliRun(const AliRun& arun):
121 fPDGDB(0), //Particle factory object
122 fConfigFunction("\0"),
125 fIsRootGeometry(kFALSE),
126 fGeometryFromCDB(kFALSE),
127 fGeometryFileName(""),
128 fTriggerDescriptor(""),
132 // Copy constructor for AliRun
137 //_____________________________________________________________________________
138 AliRun::AliRun(const char *name, const char *title):
144 fModules(new TObjArray(77)), // Support list for the Detectors
152 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
153 fConfigFunction("Config();"),
154 fRandom(new TRandom3()),
156 fIsRootGeometry(kFALSE),
157 fGeometryFromCDB(kFALSE),
158 fGeometryFileName(""),
159 fTriggerDescriptor(""),
163 // Constructor for the main processor.
164 // Creates the geometry
165 // Creates the list of Detectors.
166 // Creates the list of particles.
171 // Set random number generator
174 if (gSystem->Getenv("CONFIG_SEED")) {
175 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
178 // Add to list of browsable
179 gROOT->GetListOfBrowsables()->Add(this,name);
181 // Create default mag field
184 // Add particle list to configuration
185 AliConfig::Instance()->Add(fPDGDB);
190 //_______________________________________________________________________
194 // Default AliRun destructor
196 gROOT->GetListOfBrowsables()->Remove(this);
200 TFolder* evfold = fRunLoader->GetEventFolder();
201 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
202 TIter next(fModules);
204 while((mod = (AliModule*)next()))
206 modfold->Remove(mod);
225 //_______________________________________________________________________
226 void AliRun::Copy(TObject &) const
228 AliFatal("Not implemented!");
231 //_______________________________________________________________________
235 // Initialize Alice geometry
240 //_______________________________________________________________________
241 void AliRun::BuildSimpleGeometry()
244 // Create a simple TNode geometry used by Root display engine
246 // Initialise geometry
248 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
249 new TMaterial("void","Vacuum",0,0,0); //Everything is void
250 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
251 brik->SetVisibility(0);
252 new TNode("alice","alice","S_alice");
255 //_______________________________________________________________________
256 void AliRun::CleanDetectors()
259 // Clean Detectors at the end of event
261 fRunLoader->CleanDetectors();
264 //_______________________________________________________________________
265 void AliRun::ResetHits()
270 //_______________________________________________________________________
271 AliGenerator* AliRun::Generator() const
273 return fMCApp->Generator();
276 //_______________________________________________________________________
277 void AliRun::SetField(AliMagF* magField)
280 // Set Magnetic Field Map
286 //_______________________________________________________________________
287 void AliRun::SetRootGeometry(Bool_t flag)
289 // Instruct application that the geometry is to be retreived from a root file.
290 fIsRootGeometry = flag;
291 if (flag && gMC) gMC->SetRootGeometry();
294 //_______________________________________________________________________
295 void AliRun::SetGeometryFromCDB()
297 // Set the loading of geometry from cdb instead of creating it
298 // A default CDB storage needs to be set before this method is called
299 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
300 AliCDBManager::Instance()->GetRun() >= 0){
302 fGeometryFromCDB = kTRUE;
304 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
305 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
306 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
310 //_______________________________________________________________________
311 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
312 Float_t maxField, const char* filename)
315 // Set magnetic field parameters
316 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
317 // version Magnetic field map version (only 1 active now)
318 // scale Scale factor for the magnetic field
319 // maxField Maximum value for the magnetic field
322 // --- Sanity check on mag field flags
323 if(fField) delete fField;
325 fField = new AliMagFC("Map1"," ",type,scale,maxField);
326 } else if(version<=2) {
327 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
329 } else if(version==3) {
330 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
333 AliWarning(Form("Invalid map %d",version));
337 //_____________________________________________________________________________
339 void AliRun::InitLoaders()
341 //creates list of getters
343 TIter next(fModules);
345 while((mod = (AliModule*)next()))
347 mod->SetRunLoader(fRunLoader);
348 AliDetector *det = dynamic_cast<AliDetector*>(mod);
351 AliDebug(2, Form("Adding %s", det->GetName()));
352 fRunLoader->AddLoader(det);
357 //_____________________________________________________________________________
359 void AliRun::FinishRun()
362 // Called at the end of the run.
367 AliDebug(1, "Finish Lego");
368 fRunLoader->CdGAFile();
372 // Clean detector information
373 TIter next(fModules);
375 while((detector = dynamic_cast<AliModule*>(next()))) {
376 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
377 detector->FinishRun();
380 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
381 fRunLoader->WriteHeader("OVERWRITE");
383 // Write AliRun info and all detectors parameters
384 fRunLoader->CdGAFile();
385 Write(0,TObject::kOverwrite);//write AliRun
386 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
388 if(fMCApp) fMCApp->FinishRun();
389 fRunLoader->Synchronize();
392 //_______________________________________________________________________
393 void AliRun::Announce() const
396 // Announce the current version of AliRoot
399 "****************************************************************\n");
400 printf("%6s","*");printf("%64s","*\n");
403 printf(" You are running AliRoot version NewIO\n");
406 printf(" The SVN version for the current program is $Id$\n");
408 printf("%6s","*");printf("%64s","*\n");
410 "****************************************************************\n");
413 //_______________________________________________________________________
414 AliModule *AliRun::GetModule(const char *name) const
417 // Return pointer to detector from name
419 return dynamic_cast<AliModule*>(fModules->FindObject(name));
422 //_______________________________________________________________________
423 AliDetector *AliRun::GetDetector(const char *name) const
426 // Return pointer to detector from name
428 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
431 //_______________________________________________________________________
432 Int_t AliRun::GetModuleID(const char *name) const
435 // Return galice internal detector identifier from name
438 TObject *mod=fModules->FindObject(name);
439 if(mod) i=fModules->IndexOf(mod);
443 //_______________________________________________________________________
444 Int_t AliRun::GetEvent(Int_t event)
447 // Reloads data containers in folders # event
448 // Set branch addresses
450 if (fRunLoader == 0x0)
452 AliError("RunLoader is not set. Can not load data.");
455 /*****************************************/
456 /**** P R E R E L O A D I N G ****/
457 /*****************************************/
458 // Reset existing structures
460 fMCApp->ResetTrackReferences();
464 /*****************************************/
465 /**** R E L O A D ****/
466 /*****************************************/
468 fRunLoader->GetEvent(event);
470 /*****************************************/
471 /**** P O S T R E L O A D I N G ****/
472 /*****************************************/
474 // Set Trees branch addresses
475 TIter next(fModules);
476 AliDetector *detector;
477 while((detector = dynamic_cast<AliDetector*>(next())))
479 detector->SetTreeAddress();
482 return fRunLoader->GetHeader()->GetNtrack();
485 //_______________________________________________________________________
486 TGeometry *AliRun::GetGeometry()
489 // Create the TNode geometry for the event display
491 BuildSimpleGeometry();
493 // Unlink and relink nodes in detectors
494 // This is bad and there must be a better way...
497 TIter next(fModules);
499 while((detector = dynamic_cast<AliModule*>(next()))) {
500 detector->BuildGeometry();
501 TList *dnodes=detector->Nodes();
504 for ( j=0; j<dnodes->GetSize(); j++) {
505 node = dynamic_cast<TNode*>(dnodes->At(j));
506 node1 = fGeometry->GetNode(node->GetName());
507 dnodes->Remove(node);
508 dnodes->AddAt(node1,j);
515 //_______________________________________________________________________
516 void AliRun::SetBaseFile(const char *filename)
518 fBaseFileName = filename;
521 //_______________________________________________________________________
522 void AliRun::ResetDigits()
525 // Reset all Detectors digits
527 TIter next(fModules);
529 while((detector = dynamic_cast<AliModule*>(next()))) {
530 detector->ResetDigits();
534 //_______________________________________________________________________
535 void AliRun::ResetSDigits()
538 // Reset all Detectors digits
540 TIter next(fModules);
542 while((detector = dynamic_cast<AliModule*>(next()))) {
543 detector->ResetSDigits();
548 //_______________________________________________________________________
550 void AliRun::ResetPoints()
553 // Reset all Detectors points
555 TIter next(fModules);
557 while((detector = dynamic_cast<AliModule*>(next()))) {
558 detector->ResetPoints();
561 //_______________________________________________________________________
563 void AliRun::InitMC(const char *setup)
566 // Initialize ALICE Simulation run
571 AliWarning("Cannot initialise AliRun twice!");
576 fMCApp=new AliMC(GetName(),GetTitle());
578 gROOT->LoadMacro(setup);
579 gInterpreter->ProcessLine(fConfigFunction.Data());
581 if(AliCDBManager::Instance()->GetRun() >= 0) {
582 SetRunNumber(AliCDBManager::Instance()->GetRun());
584 AliWarning("Run number not initialized!!");
587 fRunLoader->CdGAFile();
589 AliPDG::AddParticlesToPdgDataBase();
591 fNdets = fModules->GetLast()+1;
592 TIter next(fModules);
593 for(Int_t i=0; i<fNdets; ++i)
595 TObject *objfirst, *objlast;
596 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
597 objlast = gDirectory->GetList()->Last();
599 // Add Detector histograms in Detector list of histograms
600 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
601 else objfirst = gDirectory->GetList()->First();
604 detector->Histograms()->Add(objfirst);
605 objfirst = gDirectory->GetList()->After(objfirst);
611 //Must be here because some MCs (G4) adds detectors here and not in Config.C
613 fRunLoader->MakeTree("E");
616 fRunLoader->LoadKinematics("RECREATE");
617 fRunLoader->LoadTrackRefs("RECREATE");
618 fRunLoader->LoadHits("all","RECREATE");
622 // Save stuff at the beginning of the file to avoid file corruption
623 fRunLoader->CdGAFile();
625 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
628 //_______________________________________________________________________
630 void AliRun::RunMC(Int_t nevent, const char *setup)
633 // Main function to be called to process a galice run
635 // Root > gAlice.Run();
636 // a positive number of events will cause the finish routine
639 fEventsPerRun = nevent;
640 // check if initialisation has been done
641 if (!fInitDone) InitMC(setup);
643 // Create the Root Tree with one branch per detector
644 //Hits moved to begin event -> now we are crating separate tree for each event
646 gMC->ProcessRun(nevent);
648 // End of this run, close files
649 if(nevent>0) FinishRun();
652 //_______________________________________________________________________
653 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
656 // Main function to be called to reconstruct Alice event
658 Int_t nev = fRunLoader->GetNumberOfEvents();
659 AliDebug(1, Form("Found %d events", nev));
660 Int_t nFirst = first;
661 Int_t nLast = (last < 0)? nev : last;
663 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
664 AliDebug(1, Form("Processing event %d", nevent));
666 Digits2Reco(selected);
670 //_______________________________________________________________________
672 void AliRun::Hits2Digits(const char *selected)
675 // Convert Hits to sumable digits
677 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
679 Hits2SDigits(selected);
680 SDigits2Digits(selected);
685 //_______________________________________________________________________
687 void AliRun::Tree2Tree(Option_t *option, const char *selected)
690 // Function to transform the content of
692 // - TreeH to TreeS (option "S")
693 // - TreeS to TreeD (option "D")
694 // - TreeD to TreeR (option "R")
696 // If multiple options are specified ("SDR"), transformation will be done in sequence for
697 // selected detector and for all detectors if none is selected (detector string
698 // can contain blank separated list of detector names).
701 const char *oS = strstr(option,"S");
702 const char *oD = strstr(option,"D");
703 const char *oR = strstr(option,"R");
705 TObjArray *detectors = Detectors();
707 TIter next(detectors);
709 AliDetector *detector = 0;
711 while((detector = dynamic_cast<AliDetector*>(next()))) {
713 if (strcmp(detector->GetName(),selected)) continue;
714 if (detector->IsActive())
717 AliLoader* loader = detector->GetLoader();
718 if (loader == 0x0) continue;
722 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
723 loader->LoadHits("read");
724 if (loader->TreeS() == 0x0) loader->MakeTree("S");
725 detector->MakeBranch(option);
726 detector->SetTreeAddress();
727 detector->Hits2SDigits();
728 loader->UnloadHits();
729 loader->UnloadSDigits();
733 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
734 loader->LoadSDigits("read");
735 if (loader->TreeD() == 0x0) loader->MakeTree("D");
736 detector->MakeBranch(option);
737 detector->SetTreeAddress();
738 detector->SDigits2Digits();
739 loader->UnloadSDigits();
740 loader->UnloadDigits();
744 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
745 loader->LoadDigits("read");
746 if (loader->TreeR() == 0x0) loader->MakeTree("R");
747 detector->MakeBranch(option);
748 detector->SetTreeAddress();
749 detector->Digits2Reco();
750 loader->UnloadDigits();
751 loader->UnloadRecPoints();
758 //_______________________________________________________________________
759 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
760 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
761 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
764 // Generates lego plots of:
765 // - radiation length map phi vs theta
766 // - radiation length map phi vs eta
767 // - interaction length map
768 // - g/cm2 length map
770 // ntheta bins in theta, eta
771 // themin minimum angle in theta (degrees)
772 // themax maximum angle in theta (degrees)
774 // phimin minimum angle in phi (degrees)
775 // phimax maximum angle in phi (degrees)
776 // rmin minimum radius
777 // rmax maximum radius
780 // The number of events generated = ntheta*nphi
781 // run input parameters in macro setup (default="Config.C")
783 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
786 <img src="picts/AliRunLego1.gif">
791 <img src="picts/AliRunLego2.gif">
796 <img src="picts/AliRunLego3.gif">
801 if (nev == -1) nev = nc1 * nc2;
803 // check if initialisation has been done
804 // If runloader has been initialized, set the number of events per file to nc1 * nc2
807 if (!gener) gener = new AliLegoGenerator();
809 // Configure Generator
811 gener->SetRadiusRange(rmin, rmax);
812 gener->SetZMax(zmax);
813 gener->SetCoor1Range(nc1, c1min, c1max);
814 gener->SetCoor2Range(nc2, c2min, c2max);
818 fLego = new AliLego("lego",gener);
820 if (!fInitDone) InitMC(setup);
821 //Save current generator
823 AliGenerator *gen=fMCApp->Generator();
824 fMCApp->ResetGenerator(gener);
825 //Prepare MC for Lego Run
831 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nev);
832 gMC->ProcessRun(nev);
834 // End of this run, close files
836 // Restore current generator
837 fMCApp->ResetGenerator(gen);
838 // Delete Lego Object
839 delete fLego; fLego=0;
842 //_______________________________________________________________________
843 void AliRun::SetConfigFunction(const char * config)
846 // Set the signature of the function contained in Config.C to configure
849 fConfigFunction=config;
856 //_______________________________________________________________________
857 void AliRun::Field(const Double_t* x, Double_t *b) const
860 // Return the value of the magnetic field
864 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
869 Field()->Field(xfloat,bfloat);
870 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
873 AliError("No mag field defined!");
881 // End of MC Application
884 //_______________________________________________________________________
885 void AliRun::Streamer(TBuffer &R__b)
887 // Stream an object of class AliRun.
889 if (R__b.IsReading()) {
890 if (!gAlice) gAlice = this;
891 AliRun::Class()->ReadBuffer(R__b, this);
892 gROOT->GetListOfBrowsables()->Add(this,"Run");
896 AliRun::Class()->WriteBuffer(R__b, this);
899 //_______________________________________________________________________
901 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
903 fRunLoader->GetHeader()->SetGenEventHeader(header);
905 //_______________________________________________________________________
907 Int_t AliRun::GetEvNumber() const
909 //Returns number of current event
910 if (fRunLoader == 0x0)
912 AliError("RunLoader is not set. Can not load data.");
916 return fRunLoader->GetEventNumber();
918 //_______________________________________________________________________
920 void AliRun::SetRunLoader(AliRunLoader* rloader)
923 // Set the loader of the run
925 fRunLoader = rloader;
926 if (fRunLoader == 0x0) return;
929 TFolder* evfold = fRunLoader->GetEventFolder();
930 if (evfold) evfoldname = evfold->GetName();
931 else AliWarning("Did not get Event Folder from Run Loader");
933 if ( fRunLoader->GetAliRun() )
934 {//if alrun already exists in folder
935 if (fRunLoader->GetAliRun() != this )
936 {//and is different than this - crash
937 AliFatal("AliRun is already in Folder and it is not this object");
943 evfold->Add(this);//Post this AliRun to Folder
946 TIter next(fModules);
948 while((module = (AliModule*)next()))
950 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
951 module->SetRunLoader(fRunLoader);
952 AliDetector* detector = dynamic_cast<AliDetector*>(module);
955 AliLoader* loader = fRunLoader->GetLoader(detector);
958 AliError(Form("Can not get loader for detector %s", detector->GetName()));
962 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
963 detector->SetLoader(loader);
969 void AliRun::AddModule(AliModule* mod)
972 // Add a module to the module list
974 if (mod == 0x0) return;
975 if (strlen(mod->GetName()) == 0) return;
976 if (GetModuleID(mod->GetName()) >= 0) return;
978 AliDebug(1, mod->GetName());
979 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
980 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
987 //_____________________________________________________________________________
988 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
989 { return !gSystem->AccessPathName(fnam,mode);}
991 //______________________________________________________
992 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
994 TString str = name; gSystem->ExpandPathName(str);
995 return !gSystem->AccessPathName(str.Data(),mode);