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 if(fMCApp) fMCApp->FinishRun();
387 fRunLoader->Synchronize();
390 //_______________________________________________________________________
391 void AliRun::Announce() const
394 // Announce the current version of AliRoot
397 "****************************************************************\n");
398 printf("%6s","*");printf("%64s","*\n");
401 printf(" You are running AliRoot version NewIO\n");
404 printf(" The cvs tag for the current program is $Name$\n");
406 printf("%6s","*");printf("%64s","*\n");
408 "****************************************************************\n");
411 //_______________________________________________________________________
412 AliModule *AliRun::GetModule(const char *name) const
415 // Return pointer to detector from name
417 return dynamic_cast<AliModule*>(fModules->FindObject(name));
420 //_______________________________________________________________________
421 AliDetector *AliRun::GetDetector(const char *name) const
424 // Return pointer to detector from name
426 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
429 //_______________________________________________________________________
430 Int_t AliRun::GetModuleID(const char *name) const
433 // Return galice internal detector identifier from name
436 TObject *mod=fModules->FindObject(name);
437 if(mod) i=fModules->IndexOf(mod);
441 //_______________________________________________________________________
442 Int_t AliRun::GetEvent(Int_t event)
445 // Reloads data containers in folders # event
446 // Set branch addresses
448 if (fRunLoader == 0x0)
450 AliError("RunLoader is not set. Can not load data.");
453 /*****************************************/
454 /**** P R E R E L O A D I N G ****/
455 /*****************************************/
456 // Reset existing structures
458 fMCApp->ResetTrackReferences();
462 /*****************************************/
463 /**** R E L O A D ****/
464 /*****************************************/
466 fRunLoader->GetEvent(event);
468 /*****************************************/
469 /**** P O S T R E L O A D I N G ****/
470 /*****************************************/
472 // Set Trees branch addresses
473 TIter next(fModules);
474 AliDetector *detector;
475 while((detector = dynamic_cast<AliDetector*>(next())))
477 detector->SetTreeAddress();
480 return fRunLoader->GetHeader()->GetNtrack();
483 //_______________________________________________________________________
484 TGeometry *AliRun::GetGeometry()
487 // Create the TNode geometry for the event display
489 BuildSimpleGeometry();
491 // Unlink and relink nodes in detectors
492 // This is bad and there must be a better way...
495 TIter next(fModules);
497 while((detector = dynamic_cast<AliModule*>(next()))) {
498 detector->BuildGeometry();
499 TList *dnodes=detector->Nodes();
502 for ( j=0; j<dnodes->GetSize(); j++) {
503 node = dynamic_cast<TNode*>(dnodes->At(j));
504 node1 = fGeometry->GetNode(node->GetName());
505 dnodes->Remove(node);
506 dnodes->AddAt(node1,j);
513 //_______________________________________________________________________
514 void AliRun::SetBaseFile(const char *filename)
516 fBaseFileName = filename;
519 //_______________________________________________________________________
520 void AliRun::ResetDigits()
523 // Reset all Detectors digits
525 TIter next(fModules);
527 while((detector = dynamic_cast<AliModule*>(next()))) {
528 detector->ResetDigits();
532 //_______________________________________________________________________
533 void AliRun::ResetSDigits()
536 // Reset all Detectors digits
538 TIter next(fModules);
540 while((detector = dynamic_cast<AliModule*>(next()))) {
541 detector->ResetSDigits();
546 //_______________________________________________________________________
548 void AliRun::ResetPoints()
551 // Reset all Detectors points
553 TIter next(fModules);
555 while((detector = dynamic_cast<AliModule*>(next()))) {
556 detector->ResetPoints();
559 //_______________________________________________________________________
561 void AliRun::InitMC(const char *setup)
564 // Initialize ALICE Simulation run
569 AliWarning("Cannot initialise AliRun twice!");
574 fMCApp=new AliMC(GetName(),GetTitle());
576 gROOT->LoadMacro(setup);
577 gInterpreter->ProcessLine(fConfigFunction.Data());
579 // Set the run number in the CDB manager as assigned from
580 // constructor or from config file
581 AliCDBManager::Instance()->SetRun(GetRunNumber());
583 fRunLoader->CdGAFile();
585 AliPDG::AddParticlesToPdgDataBase();
587 fNdets = fModules->GetLast()+1;
588 TIter next(fModules);
589 for(Int_t i=0; i<fNdets; ++i)
591 TObject *objfirst, *objlast;
592 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
593 objlast = gDirectory->GetList()->Last();
595 // Add Detector histograms in Detector list of histograms
596 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
597 else objfirst = gDirectory->GetList()->First();
600 detector->Histograms()->Add(objfirst);
601 objfirst = gDirectory->GetList()->After(objfirst);
607 //Must be here because some MCs (G4) adds detectors here and not in Config.C
609 fRunLoader->MakeTree("E");
612 fRunLoader->LoadKinematics("RECREATE");
613 fRunLoader->LoadTrackRefs("RECREATE");
614 fRunLoader->LoadHits("all","RECREATE");
618 // Save stuff at the beginning of the file to avoid file corruption
619 fRunLoader->CdGAFile();
621 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
624 //_______________________________________________________________________
626 void AliRun::RunMC(Int_t nevent, const char *setup)
629 // Main function to be called to process a galice run
631 // Root > gAlice.Run();
632 // a positive number of events will cause the finish routine
635 fEventsPerRun = nevent;
636 // check if initialisation has been done
637 if (!fInitDone) InitMC(setup);
639 // Create the Root Tree with one branch per detector
640 //Hits moved to begin event -> now we are crating separate tree for each event
642 gMC->ProcessRun(nevent);
644 // End of this run, close files
645 if(nevent>0) FinishRun();
648 //_______________________________________________________________________
649 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
652 // Main function to be called to reconstruct Alice event
654 Int_t nev = fRunLoader->GetNumberOfEvents();
655 AliDebug(1, Form("Found %d events", nev));
656 Int_t nFirst = first;
657 Int_t nLast = (last < 0)? nev : last;
659 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
660 AliDebug(1, Form("Processing event %d", nevent));
662 Digits2Reco(selected);
666 //_______________________________________________________________________
668 void AliRun::Hits2Digits(const char *selected)
671 // Convert Hits to sumable digits
673 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
675 Hits2SDigits(selected);
676 SDigits2Digits(selected);
681 //_______________________________________________________________________
683 void AliRun::Tree2Tree(Option_t *option, const char *selected)
686 // Function to transform the content of
688 // - TreeH to TreeS (option "S")
689 // - TreeS to TreeD (option "D")
690 // - TreeD to TreeR (option "R")
692 // If multiple options are specified ("SDR"), transformation will be done in sequence for
693 // selected detector and for all detectors if none is selected (detector string
694 // can contain blank separated list of detector names).
697 const char *oS = strstr(option,"S");
698 const char *oD = strstr(option,"D");
699 const char *oR = strstr(option,"R");
701 TObjArray *detectors = Detectors();
703 TIter next(detectors);
705 AliDetector *detector = 0;
707 while((detector = dynamic_cast<AliDetector*>(next()))) {
709 if (strcmp(detector->GetName(),selected)) continue;
710 if (detector->IsActive())
713 AliLoader* loader = detector->GetLoader();
714 if (loader == 0x0) continue;
718 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
719 loader->LoadHits("read");
720 if (loader->TreeS() == 0x0) loader->MakeTree("S");
721 detector->MakeBranch(option);
722 detector->SetTreeAddress();
723 detector->Hits2SDigits();
724 loader->UnloadHits();
725 loader->UnloadSDigits();
729 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
730 loader->LoadSDigits("read");
731 if (loader->TreeD() == 0x0) loader->MakeTree("D");
732 detector->MakeBranch(option);
733 detector->SetTreeAddress();
734 detector->SDigits2Digits();
735 loader->UnloadSDigits();
736 loader->UnloadDigits();
740 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
741 loader->LoadDigits("read");
742 if (loader->TreeR() == 0x0) loader->MakeTree("R");
743 detector->MakeBranch(option);
744 detector->SetTreeAddress();
745 detector->Digits2Reco();
746 loader->UnloadDigits();
747 loader->UnloadRecPoints();
754 //_______________________________________________________________________
755 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
756 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
757 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
760 // Generates lego plots of:
761 // - radiation length map phi vs theta
762 // - radiation length map phi vs eta
763 // - interaction length map
764 // - g/cm2 length map
766 // ntheta bins in theta, eta
767 // themin minimum angle in theta (degrees)
768 // themax maximum angle in theta (degrees)
770 // phimin minimum angle in phi (degrees)
771 // phimax maximum angle in phi (degrees)
772 // rmin minimum radius
773 // rmax maximum radius
776 // The number of events generated = ntheta*nphi
777 // run input parameters in macro setup (default="Config.C")
779 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
782 <img src="picts/AliRunLego1.gif">
787 <img src="picts/AliRunLego2.gif">
792 <img src="picts/AliRunLego3.gif">
797 if (nev == -1) nev = nc1 * nc2;
799 // check if initialisation has been done
800 // If runloader has been initialized, set the number of events per file to nc1 * nc2
803 if (!gener) gener = new AliLegoGenerator();
805 // Configure Generator
807 gener->SetRadiusRange(rmin, rmax);
808 gener->SetZMax(zmax);
809 gener->SetCoor1Range(nc1, c1min, c1max);
810 gener->SetCoor2Range(nc2, c2min, c2max);
814 fLego = new AliLego("lego",gener);
816 if (!fInitDone) InitMC(setup);
817 //Save current generator
819 AliGenerator *gen=fMCApp->Generator();
820 fMCApp->ResetGenerator(gener);
821 //Prepare MC for Lego Run
827 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nev);
828 gMC->ProcessRun(nev);
830 // End of this run, close files
832 // Restore current generator
833 fMCApp->ResetGenerator(gen);
834 // Delete Lego Object
835 delete fLego; fLego=0;
838 //_______________________________________________________________________
839 void AliRun::SetConfigFunction(const char * config)
842 // Set the signature of the function contained in Config.C to configure
845 fConfigFunction=config;
852 //_______________________________________________________________________
853 void AliRun::Field(const Double_t* x, Double_t *b) const
856 // Return the value of the magnetic field
860 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
865 Field()->Field(xfloat,bfloat);
866 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
869 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);