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
105 //_______________________________________________________________________
106 AliRun::AliRun(const AliRun& arun):
120 fPDGDB(0), //Particle factory object
121 fConfigFunction("\0"),
124 fIsRootGeometry(kFALSE),
125 fGeometryFromCDB(kFALSE),
126 fGeometryFileName(""),
127 fTriggerDescriptor(""),
131 // Copy constructor for AliRun
136 //_____________________________________________________________________________
137 AliRun::AliRun(const char *name, const char *title):
143 fModules(new TObjArray(77)), // Support list for the Detectors
151 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
152 fConfigFunction("Config();"),
153 fRandom(new TRandom3()),
155 fIsRootGeometry(kFALSE),
156 fGeometryFromCDB(kFALSE),
157 fGeometryFileName(""),
158 fTriggerDescriptor(""),
162 // Constructor for the main processor.
163 // Creates the geometry
164 // Creates the list of Detectors.
165 // Creates the list of particles.
170 // Set random number generator
173 if (gSystem->Getenv("CONFIG_SEED")) {
174 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
177 // Add to list of browsable
178 gROOT->GetListOfBrowsables()->Add(this,name);
180 // Create default mag field
183 // Add particle list to configuration
184 AliConfig::Instance()->Add(fPDGDB);
189 //_______________________________________________________________________
193 // Default AliRun destructor
195 gROOT->GetListOfBrowsables()->Remove(this);
199 TFolder* evfold = fRunLoader->GetEventFolder();
200 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
201 TIter next(fModules);
203 while((mod = (AliModule*)next()))
205 modfold->Remove(mod);
224 //_______________________________________________________________________
225 void AliRun::Copy(TObject &) const
227 AliFatal("Not implemented!");
230 //_______________________________________________________________________
234 // Initialize Alice geometry
239 //_______________________________________________________________________
240 void AliRun::BuildSimpleGeometry()
243 // Create a simple TNode geometry used by Root display engine
245 // Initialise geometry
247 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
248 new TMaterial("void","Vacuum",0,0,0); //Everything is void
249 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
250 brik->SetVisibility(0);
251 new TNode("alice","alice","S_alice");
254 //_______________________________________________________________________
255 void AliRun::CleanDetectors()
258 // Clean Detectors at the end of event
260 fRunLoader->CleanDetectors();
263 //_______________________________________________________________________
264 void AliRun::ResetHits()
269 //_______________________________________________________________________
270 AliGenerator* AliRun::Generator() const
272 return fMCApp->Generator();
275 //_______________________________________________________________________
276 void AliRun::SetField(AliMagF* magField)
279 // Set Magnetic Field Map
285 //_______________________________________________________________________
286 void AliRun::SetRootGeometry(Bool_t flag)
288 // Instruct application that the geometry is to be retreived from a root file.
289 fIsRootGeometry = flag;
290 if (flag && gMC) gMC->SetRootGeometry();
293 //_______________________________________________________________________
294 void AliRun::SetGeometryFromCDB()
296 // Set the loading of geometry from cdb instead of creating it
297 // A default CDB storage needs to be set before this method is called
298 if(AliCDBManager::Instance()->IsDefaultStorageSet()){
300 fGeometryFromCDB = kTRUE;
302 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
303 AliError("Loading of geometry from CDB ignored. First set a default CDB storage!");
304 AliError("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
308 //_______________________________________________________________________
309 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
310 Float_t maxField, const char* filename)
313 // Set magnetic field parameters
314 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
315 // version Magnetic field map version (only 1 active now)
316 // scale Scale factor for the magnetic field
317 // maxField Maximum value for the magnetic field
320 // --- Sanity check on mag field flags
321 if(fField) delete fField;
323 fField = new AliMagFC("Map1"," ",type,scale,maxField);
324 } else if(version<=2) {
325 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
327 } else if(version==3) {
328 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
331 AliWarning(Form("Invalid map %d",version));
335 //_____________________________________________________________________________
337 void AliRun::InitLoaders()
339 //creates list of getters
341 TIter next(fModules);
343 while((mod = (AliModule*)next()))
345 mod->SetRunLoader(fRunLoader);
346 AliDetector *det = dynamic_cast<AliDetector*>(mod);
349 AliDebug(2, Form("Adding %s", det->GetName()));
350 fRunLoader->AddLoader(det);
355 //_____________________________________________________________________________
357 void AliRun::FinishRun()
360 // Called at the end of the run.
365 AliDebug(1, "Finish Lego");
366 fRunLoader->CdGAFile();
370 // Clean detector information
371 TIter next(fModules);
373 while((detector = dynamic_cast<AliModule*>(next()))) {
374 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
375 detector->FinishRun();
378 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
379 fRunLoader->WriteHeader("OVERWRITE");
381 // Write AliRun info and all detectors parameters
382 fRunLoader->CdGAFile();
383 Write(0,TObject::kOverwrite);//write AliRun
384 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
386 // Clean tree information
387 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
388 fRunLoader->Stack()->FinishRun();
390 if(fMCApp) fMCApp->FinishRun();
392 fRunLoader->Synchronize();
395 //_______________________________________________________________________
396 void AliRun::Announce() const
399 // Announce the current version of AliRoot
402 "****************************************************************\n");
403 printf("%6s","*");printf("%64s","*\n");
406 printf(" You are running AliRoot version NewIO\n");
409 printf(" The cvs tag for the current program is $Name$\n");
411 printf("%6s","*");printf("%64s","*\n");
413 "****************************************************************\n");
416 //_______________________________________________________________________
417 AliModule *AliRun::GetModule(const char *name) const
420 // Return pointer to detector from name
422 return dynamic_cast<AliModule*>(fModules->FindObject(name));
425 //_______________________________________________________________________
426 AliDetector *AliRun::GetDetector(const char *name) const
429 // Return pointer to detector from name
431 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
434 //_______________________________________________________________________
435 Int_t AliRun::GetModuleID(const char *name) const
438 // Return galice internal detector identifier from name
441 TObject *mod=fModules->FindObject(name);
442 if(mod) i=fModules->IndexOf(mod);
446 //_______________________________________________________________________
447 Int_t AliRun::GetEvent(Int_t event)
450 // Reloads data containers in folders # event
451 // Set branch addresses
453 if (fRunLoader == 0x0)
455 AliError("RunLoader is not set. Can not load data.");
458 /*****************************************/
459 /**** P R E R E L O A D I N G ****/
460 /*****************************************/
461 // Reset existing structures
463 fMCApp->ResetTrackReferences();
467 /*****************************************/
468 /**** R E L O A D ****/
469 /*****************************************/
471 fRunLoader->GetEvent(event);
473 /*****************************************/
474 /**** P O S T R E L O A D I N G ****/
475 /*****************************************/
477 // Set Trees branch addresses
478 TIter next(fModules);
480 while((detector = dynamic_cast<AliModule*>(next())))
482 detector->SetTreeAddress();
485 return fRunLoader->GetHeader()->GetNtrack();
488 //_______________________________________________________________________
489 TGeometry *AliRun::GetGeometry()
491 // Create the TNode geometry for the event display
493 BuildSimpleGeometry();
495 // Unlink and relink nodes in detectors
496 // This is bad and there must be a better way...
499 TIter next(fModules);
501 while((detector = dynamic_cast<AliModule*>(next()))) {
502 detector->BuildGeometry();
503 TList *dnodes=detector->Nodes();
506 for ( j=0; j<dnodes->GetSize(); j++) {
507 node = dynamic_cast<TNode*>(dnodes->At(j));
508 node1 = fGeometry->GetNode(node->GetName());
509 dnodes->Remove(node);
510 dnodes->AddAt(node1,j);
517 //_______________________________________________________________________
518 void AliRun::SetBaseFile(const char *filename)
520 fBaseFileName = filename;
523 //_______________________________________________________________________
524 void AliRun::ResetDigits()
527 // Reset all Detectors digits
529 TIter next(fModules);
531 while((detector = dynamic_cast<AliModule*>(next()))) {
532 detector->ResetDigits();
536 //_______________________________________________________________________
537 void AliRun::ResetSDigits()
540 // Reset all Detectors digits
542 TIter next(fModules);
544 while((detector = dynamic_cast<AliModule*>(next()))) {
545 detector->ResetSDigits();
550 //_______________________________________________________________________
552 void AliRun::ResetPoints()
555 // Reset all Detectors points
557 TIter next(fModules);
559 while((detector = dynamic_cast<AliModule*>(next()))) {
560 detector->ResetPoints();
563 //_______________________________________________________________________
565 void AliRun::InitMC(const char *setup)
568 // Initialize ALICE Simulation run
573 AliWarning("Cannot initialise AliRun twice!");
578 fMCApp=new AliMC(GetName(),GetTitle());
580 gROOT->LoadMacro(setup);
581 gInterpreter->ProcessLine(fConfigFunction.Data());
583 // Set the run number in the CDB manager as assigned from
584 // constructor or from config file
585 AliCDBManager::Instance()->SetRun(GetRunNumber());
587 fRunLoader->CdGAFile();
589 AliPDG::AddParticlesToPdgDataBase();
591 fNdets = fModules->GetLast()+1;
593 TIter next(fModules);
594 for(Int_t i=0; i<fNdets; ++i)
596 TObject *objfirst, *objlast;
597 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
598 objlast = gDirectory->GetList()->Last();
600 // Add Detector histograms in Detector list of histograms
601 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
602 else objfirst = gDirectory->GetList()->First();
605 detector->Histograms()->Add(objfirst);
606 objfirst = gDirectory->GetList()->After(objfirst);
612 //Must be here because some MCs (G4) adds detectors here and not in Config.C
614 fRunLoader->MakeTree("E");
617 fRunLoader->LoadKinematics("RECREATE");
618 fRunLoader->LoadTrackRefs("RECREATE");
619 fRunLoader->LoadHits("all","RECREATE");
623 // Save stuff at the beginning of the file to avoid file corruption
624 fRunLoader->CdGAFile();
626 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
629 //_______________________________________________________________________
631 void AliRun::RunMC(Int_t nevent, const char *setup)
634 // Main function to be called to process a galice run
636 // Root > gAlice.Run();
637 // a positive number of events will cause the finish routine
640 fEventsPerRun = nevent;
641 // check if initialisation has been done
642 if (!fInitDone) InitMC(setup);
644 // Create the Root Tree with one branch per detector
645 //Hits moved to begin event -> now we are crating separate tree for each event
647 gMC->ProcessRun(nevent);
649 // End of this run, close files
650 if(nevent>0) FinishRun();
653 //_______________________________________________________________________
654 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
657 // Main function to be called to reconstruct Alice event
659 Int_t nev = fRunLoader->GetNumberOfEvents();
660 AliDebug(1, Form("Found %d events", nev));
661 Int_t nFirst = first;
662 Int_t nLast = (last < 0)? nev : last;
664 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
665 AliDebug(1, Form("Processing event %d", nevent));
667 Digits2Reco(selected);
671 //_______________________________________________________________________
673 void AliRun::Hits2Digits(const char *selected)
676 // Convert Hits to sumable digits
678 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
680 Hits2SDigits(selected);
681 SDigits2Digits(selected);
686 //_______________________________________________________________________
688 void AliRun::Tree2Tree(Option_t *option, const char *selected)
691 // Function to transform the content of
693 // - TreeH to TreeS (option "S")
694 // - TreeS to TreeD (option "D")
695 // - TreeD to TreeR (option "R")
697 // If multiple options are specified ("SDR"), transformation will be done in sequence for
698 // selected detector and for all detectors if none is selected (detector string
699 // can contain blank separated list of detector names).
702 const char *oS = strstr(option,"S");
703 const char *oD = strstr(option,"D");
704 const char *oR = strstr(option,"R");
706 TObjArray *detectors = Detectors();
708 TIter next(detectors);
710 AliDetector *detector = 0;
712 while((detector = dynamic_cast<AliDetector*>(next()))) {
714 if (strcmp(detector->GetName(),selected)) continue;
715 if (detector->IsActive())
718 AliLoader* loader = detector->GetLoader();
719 if (loader == 0x0) continue;
723 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
724 loader->LoadHits("read");
725 if (loader->TreeS() == 0x0) loader->MakeTree("S");
726 detector->MakeBranch(option);
727 detector->SetTreeAddress();
728 detector->Hits2SDigits();
729 loader->UnloadHits();
730 loader->UnloadSDigits();
734 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
735 loader->LoadSDigits("read");
736 if (loader->TreeD() == 0x0) loader->MakeTree("D");
737 detector->MakeBranch(option);
738 detector->SetTreeAddress();
739 detector->SDigits2Digits();
740 loader->UnloadSDigits();
741 loader->UnloadDigits();
745 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
746 loader->LoadDigits("read");
747 if (loader->TreeR() == 0x0) loader->MakeTree("R");
748 detector->MakeBranch(option);
749 detector->SetTreeAddress();
750 detector->Digits2Reco();
751 loader->UnloadDigits();
752 loader->UnloadRecPoints();
759 //_______________________________________________________________________
760 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
761 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
762 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
765 // Generates lego plots of:
766 // - radiation length map phi vs theta
767 // - radiation length map phi vs eta
768 // - interaction length map
769 // - g/cm2 length map
771 // ntheta bins in theta, eta
772 // themin minimum angle in theta (degrees)
773 // themax maximum angle in theta (degrees)
775 // phimin minimum angle in phi (degrees)
776 // phimax maximum angle in phi (degrees)
777 // rmin minimum radius
778 // rmax maximum radius
781 // The number of events generated = ntheta*nphi
782 // run input parameters in macro setup (default="Config.C")
784 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
787 <img src="picts/AliRunLego1.gif">
792 <img src="picts/AliRunLego2.gif">
797 <img src="picts/AliRunLego3.gif">
802 if (nev == -1) nev = nc1 * nc2;
804 // check if initialisation has been done
805 // If runloader has been initialized, set the number of events per file to nc1 * nc2
808 if (!gener) gener = new AliLegoGenerator();
810 // 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
830 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nev);
831 //gMC->ProcessRun(nc1*nc2+1);
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
863 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
867 Field()->Field(xfloat,bfloat);
868 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
871 AliError("No mag field defined!");
877 // End of MC Application
880 //_______________________________________________________________________
881 void AliRun::Streamer(TBuffer &R__b)
883 // Stream an object of class AliRun.
885 if (R__b.IsReading()) {
886 if (!gAlice) gAlice = this;
887 AliRun::Class()->ReadBuffer(R__b, this);
888 gROOT->GetListOfBrowsables()->Add(this,"Run");
892 AliRun::Class()->WriteBuffer(R__b, this);
895 //_______________________________________________________________________
897 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
899 fRunLoader->GetHeader()->SetGenEventHeader(header);
901 //_______________________________________________________________________
903 Int_t AliRun::GetEvNumber() const
905 //Returns number of current event
906 if (fRunLoader == 0x0)
908 AliError("RunLoader is not set. Can not load data.");
912 return fRunLoader->GetEventNumber();
914 //_______________________________________________________________________
916 void AliRun::SetRunLoader(AliRunLoader* rloader)
919 // Set the loader of the run
921 fRunLoader = rloader;
922 if (fRunLoader == 0x0) return;
925 TFolder* evfold = fRunLoader->GetEventFolder();
926 if (evfold) evfoldname = evfold->GetName();
927 else AliWarning("Did not get Event Folder from Run Loader");
929 if ( fRunLoader->GetAliRun() )
930 {//if alrun already exists in folder
931 if (fRunLoader->GetAliRun() != this )
932 {//and is different than this - crash
933 AliFatal("AliRun is already in Folder and it is not this object");
939 evfold->Add(this);//Post this AliRun to Folder
942 TIter next(fModules);
944 while((module = (AliModule*)next()))
946 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
947 module->SetRunLoader(fRunLoader);
948 AliDetector* detector = dynamic_cast<AliDetector*>(module);
951 AliLoader* loader = fRunLoader->GetLoader(detector);
954 AliError(Form("Can not get loader for detector %s", detector->GetName()));
958 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
959 detector->SetLoader(loader);
965 void AliRun::AddModule(AliModule* mod)
968 // Add a module to the module list
970 if (mod == 0x0) return;
971 if (strlen(mod->GetName()) == 0) return;
972 if (GetModuleID(mod->GetName()) >= 0) return;
974 AliDebug(1, mod->GetName());
975 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
976 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
983 // added by Alberto Colla
984 //_____________________________________________________________________________
985 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
986 { return !gSystem->AccessPathName(fnam,mode);}
988 //______________________________________________________
989 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
991 TString str = name; gSystem->ExpandPathName(str);
992 return !gSystem->AccessPathName(str.Data(),mode);