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 "AliAlignObj.h"
73 //_______________________________________________________________________
87 fPDGDB(0), //Particle factory object
91 fIsRootGeometry(kFALSE),
92 fGeometryFileName(""),
93 fTriggerDescriptor(""),
97 // Default constructor for AliRun
99 AliConfig::Instance();//skowron 29 Feb 2002
100 //ensures that the folder structure is build
103 //_______________________________________________________________________
104 AliRun::AliRun(const AliRun& arun):
118 fPDGDB(0), //Particle factory object
119 fConfigFunction("\0"),
122 fIsRootGeometry(kFALSE),
123 fGeometryFileName(""),
124 fTriggerDescriptor(""),
128 // Copy constructor for AliRun
133 //_____________________________________________________________________________
134 AliRun::AliRun(const char *name, const char *title):
140 fModules(new TObjArray(77)), // Support list for the Detectors
148 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
149 fConfigFunction("Config();"),
150 fRandom(new TRandom3()),
152 fIsRootGeometry(kFALSE),
153 fGeometryFileName(""),
154 fTriggerDescriptor(""),
158 // Constructor for the main processor.
159 // Creates the geometry
160 // Creates the list of Detectors.
161 // Creates the list of particles.
166 // Set random number generator
169 if (gSystem->Getenv("CONFIG_SEED")) {
170 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
173 // Add to list of browsable
174 gROOT->GetListOfBrowsables()->Add(this,name);
175 // Create the TNode geometry for the event display
176 BuildSimpleGeometry();
178 // Create default mag field
181 // Add particle list to configuration
182 AliConfig::Instance()->Add(fPDGDB);
187 //_______________________________________________________________________
191 // Default AliRun destructor
193 gROOT->GetListOfBrowsables()->Remove(this);
197 TFolder* evfold = fRunLoader->GetEventFolder();
198 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
199 TIter next(fModules);
201 while((mod = (AliModule*)next()))
203 modfold->Remove(mod);
222 //_______________________________________________________________________
223 void AliRun::Copy(TObject &) const
225 AliFatal("Not implemented!");
228 //_______________________________________________________________________
232 // Initialize Alice geometry
237 //_______________________________________________________________________
238 void AliRun::BuildSimpleGeometry()
241 // Create a simple TNode geometry used by Root display engine
243 // Initialise geometry
245 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
246 new TMaterial("void","Vacuum",0,0,0); //Everything is void
247 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
248 brik->SetVisibility(0);
249 new TNode("alice","alice","S_alice");
252 //_______________________________________________________________________
253 void AliRun::CleanDetectors()
256 // Clean Detectors at the end of event
258 fRunLoader->CleanDetectors();
261 //_______________________________________________________________________
262 void AliRun::ResetHits()
267 //_______________________________________________________________________
268 AliGenerator* AliRun::Generator() const
270 return fMCApp->Generator();
273 //_______________________________________________________________________
274 void AliRun::SetField(AliMagF* magField)
277 // Set Magnetic Field Map
283 //_______________________________________________________________________
284 void AliRun::SetRootGeometry(Bool_t flag)
286 // Instruct application that the geometry is to be retreived from a root file.
287 fIsRootGeometry = flag;
288 if (flag) gMC->SetRootGeometry();
290 //_______________________________________________________________________
291 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
292 Float_t maxField, const char* filename)
295 // Set magnetic field parameters
296 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
297 // version Magnetic field map version (only 1 active now)
298 // scale Scale factor for the magnetic field
299 // maxField Maximum value for the magnetic field
302 // --- Sanity check on mag field flags
303 if(fField) delete fField;
305 fField = new AliMagFC("Map1"," ",type,scale,maxField);
306 } else if(version<=2) {
307 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
309 } else if(version==3) {
310 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
313 AliWarning(Form("Invalid map %d",version));
317 //_____________________________________________________________________________
319 void AliRun::InitLoaders()
321 //creates list of getters
323 TIter next(fModules);
325 while((mod = (AliModule*)next()))
327 mod->SetRunLoader(fRunLoader);
328 AliDetector *det = dynamic_cast<AliDetector*>(mod);
331 AliDebug(2, Form("Adding %s", det->GetName()));
332 fRunLoader->AddLoader(det);
337 //_____________________________________________________________________________
339 void AliRun::FinishRun()
342 // Called at the end of the run.
347 AliDebug(1, "Finish Lego");
348 fRunLoader->CdGAFile();
352 // Clean detector information
353 TIter next(fModules);
355 while((detector = dynamic_cast<AliModule*>(next()))) {
356 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
357 detector->FinishRun();
360 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
361 fRunLoader->WriteHeader("OVERWRITE");
363 // Write AliRun info and all detectors parameters
364 fRunLoader->CdGAFile();
365 Write(0,TObject::kOverwrite);//write AliRun
366 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
368 // Clean tree information
369 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
370 fRunLoader->Stack()->FinishRun();
372 if(fMCApp) fMCApp->FinishRun();
374 fRunLoader->Synchronize();
377 //_______________________________________________________________________
378 void AliRun::Announce() const
381 // Announce the current version of AliRoot
384 "****************************************************************\n");
385 printf("%6s","*");printf("%64s","*\n");
388 printf(" You are running AliRoot version NewIO\n");
391 printf(" The cvs tag for the current program is $Name$\n");
393 printf("%6s","*");printf("%64s","*\n");
395 "****************************************************************\n");
398 //_______________________________________________________________________
399 AliModule *AliRun::GetModule(const char *name) const
402 // Return pointer to detector from name
404 return dynamic_cast<AliModule*>(fModules->FindObject(name));
407 //_______________________________________________________________________
408 AliDetector *AliRun::GetDetector(const char *name) const
411 // Return pointer to detector from name
413 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
416 //_______________________________________________________________________
417 Int_t AliRun::GetModuleID(const char *name) const
420 // Return galice internal detector identifier from name
423 TObject *mod=fModules->FindObject(name);
424 if(mod) i=fModules->IndexOf(mod);
428 //_______________________________________________________________________
429 Int_t AliRun::GetEvent(Int_t event)
432 // Reloads data containers in folders # event
433 // Set branch addresses
435 if (fRunLoader == 0x0)
437 AliError("RunLoader is not set. Can not load data.");
440 /*****************************************/
441 /**** P R E R E L O A D I N G ****/
442 /*****************************************/
443 // Reset existing structures
445 fMCApp->ResetTrackReferences();
449 /*****************************************/
450 /**** R E L O A D ****/
451 /*****************************************/
453 fRunLoader->GetEvent(event);
455 /*****************************************/
456 /**** P O S T R E L O A D I N G ****/
457 /*****************************************/
459 // Set Trees branch addresses
460 TIter next(fModules);
462 while((detector = dynamic_cast<AliModule*>(next())))
464 detector->SetTreeAddress();
467 return fRunLoader->GetHeader()->GetNtrack();
470 //_______________________________________________________________________
471 TGeometry *AliRun::GetGeometry()
474 // Import Alice geometry from current file
475 // Return pointer to geometry object
477 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
479 // Unlink and relink nodes in detectors
480 // This is bad and there must be a better way...
483 TIter next(fModules);
485 while((detector = dynamic_cast<AliModule*>(next()))) {
486 TList *dnodes=detector->Nodes();
489 for ( j=0; j<dnodes->GetSize(); j++) {
490 node = dynamic_cast<TNode*>(dnodes->At(j));
491 node1 = fGeometry->GetNode(node->GetName());
492 dnodes->Remove(node);
493 dnodes->AddAt(node1,j);
499 //_______________________________________________________________________
500 void AliRun::SetBaseFile(const char *filename)
502 fBaseFileName = filename;
505 //_______________________________________________________________________
506 void AliRun::ResetDigits()
509 // Reset all Detectors digits
511 TIter next(fModules);
513 while((detector = dynamic_cast<AliModule*>(next()))) {
514 detector->ResetDigits();
518 //_______________________________________________________________________
519 void AliRun::ResetSDigits()
522 // Reset all Detectors digits
524 TIter next(fModules);
526 while((detector = dynamic_cast<AliModule*>(next()))) {
527 detector->ResetSDigits();
532 //_______________________________________________________________________
534 void AliRun::ResetPoints()
537 // Reset all Detectors points
539 TIter next(fModules);
541 while((detector = dynamic_cast<AliModule*>(next()))) {
542 detector->ResetPoints();
545 //_______________________________________________________________________
547 void AliRun::InitMC(const char *setup)
550 // Initialize ALICE Simulation run
555 AliWarning("Cannot initialise AliRun twice!");
560 fMCApp=new AliMC(GetName(),GetTitle());
562 gROOT->LoadMacro(setup);
563 gInterpreter->ProcessLine(fConfigFunction.Data());
565 fRunLoader->CdGAFile();
567 AliPDG::AddParticlesToPdgDataBase();
569 fNdets = fModules->GetLast()+1;
571 TIter next(fModules);
572 for(Int_t i=0; i<fNdets; ++i)
574 TObject *objfirst, *objlast;
575 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
576 objlast = gDirectory->GetList()->Last();
578 // Add Detector histograms in Detector list of histograms
579 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
580 else objfirst = gDirectory->GetList()->First();
583 detector->Histograms()->Add(objfirst);
584 objfirst = gDirectory->GetList()->After(objfirst);
590 //Must be here because some MCs (G4) adds detectors here and not in Config.C
592 fRunLoader->MakeTree("E");
595 fRunLoader->LoadKinematics("RECREATE");
596 fRunLoader->LoadTrackRefs("RECREATE");
597 fRunLoader->LoadHits("all","RECREATE");
601 // Save stuff at the beginning of the file to avoid file corruption
602 fRunLoader->CdGAFile();
604 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
607 //_______________________________________________________________________
609 void AliRun::RunMC(Int_t nevent, const char *setup)
612 // Main function to be called to process a galice run
614 // Root > gAlice.Run();
615 // a positive number of events will cause the finish routine
618 fEventsPerRun = nevent;
619 // check if initialisation has been done
620 if (!fInitDone) InitMC(setup);
622 // Create the Root Tree with one branch per detector
623 //Hits moved to begin event -> now we are crating separate tree for each event
625 gMC->ProcessRun(nevent);
627 // End of this run, close files
628 if(nevent>0) FinishRun();
631 //_______________________________________________________________________
632 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
635 // Main function to be called to reconstruct Alice event
637 Int_t nev = fRunLoader->GetNumberOfEvents();
638 AliDebug(1, Form("Found %d events", nev));
639 Int_t nFirst = first;
640 Int_t nLast = (last < 0)? nev : last;
642 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
643 AliDebug(1, Form("Processing event %d", nevent));
645 Digits2Reco(selected);
649 //_______________________________________________________________________
651 void AliRun::Hits2Digits(const char *selected)
654 // Convert Hits to sumable digits
656 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
658 Hits2SDigits(selected);
659 SDigits2Digits(selected);
664 //_______________________________________________________________________
666 void AliRun::Tree2Tree(Option_t *option, const char *selected)
669 // Function to transform the content of
671 // - TreeH to TreeS (option "S")
672 // - TreeS to TreeD (option "D")
673 // - TreeD to TreeR (option "R")
675 // If multiple options are specified ("SDR"), transformation will be done in sequence for
676 // selected detector and for all detectors if none is selected (detector string
677 // can contain blank separated list of detector names).
680 const char *oS = strstr(option,"S");
681 const char *oD = strstr(option,"D");
682 const char *oR = strstr(option,"R");
684 TObjArray *detectors = Detectors();
686 TIter next(detectors);
688 AliDetector *detector = 0;
690 while((detector = dynamic_cast<AliDetector*>(next()))) {
692 if (strcmp(detector->GetName(),selected)) continue;
693 if (detector->IsActive())
696 AliLoader* loader = detector->GetLoader();
697 if (loader == 0x0) continue;
701 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
702 loader->LoadHits("read");
703 if (loader->TreeS() == 0x0) loader->MakeTree("S");
704 detector->MakeBranch(option);
705 detector->SetTreeAddress();
706 detector->Hits2SDigits();
707 loader->UnloadHits();
708 loader->UnloadSDigits();
712 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
713 loader->LoadSDigits("read");
714 if (loader->TreeD() == 0x0) loader->MakeTree("D");
715 detector->MakeBranch(option);
716 detector->SetTreeAddress();
717 detector->SDigits2Digits();
718 loader->UnloadSDigits();
719 loader->UnloadDigits();
723 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
724 loader->LoadDigits("read");
725 if (loader->TreeR() == 0x0) loader->MakeTree("R");
726 detector->MakeBranch(option);
727 detector->SetTreeAddress();
728 detector->Digits2Reco();
729 loader->UnloadDigits();
730 loader->UnloadRecPoints();
737 //_______________________________________________________________________
738 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
739 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
740 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
743 // Generates lego plots of:
744 // - radiation length map phi vs theta
745 // - radiation length map phi vs eta
746 // - interaction length map
747 // - g/cm2 length map
749 // ntheta bins in theta, eta
750 // themin minimum angle in theta (degrees)
751 // themax maximum angle in theta (degrees)
753 // phimin minimum angle in phi (degrees)
754 // phimax maximum angle in phi (degrees)
755 // rmin minimum radius
756 // rmax maximum radius
759 // The number of events generated = ntheta*nphi
760 // run input parameters in macro setup (default="Config.C")
762 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
765 <img src="picts/AliRunLego1.gif">
770 <img src="picts/AliRunLego2.gif">
775 <img src="picts/AliRunLego3.gif">
780 // check if initialisation has been done
781 // If runloader has been initialized, set the number of events per file to nc1 * nc2
784 if (!gener) gener = new AliLegoGenerator();
786 // Configure Generator
787 gener->SetRadiusRange(rmin, rmax);
788 gener->SetZMax(zmax);
789 gener->SetCoor1Range(nc1, c1min, c1max);
790 gener->SetCoor2Range(nc2, c2min, c2max);
794 fLego = new AliLego("lego",gener);
796 if (!fInitDone) InitMC(setup);
797 //Save current generator
799 AliGenerator *gen=fMCApp->Generator();
800 fMCApp->ResetGenerator(gener);
801 //Prepare MC for Lego Run
806 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
807 //gMC->ProcessRun(nc1*nc2+1);
808 gMC->ProcessRun(nc1*nc2);
810 // End of this run, close files
812 // Restore current generator
813 fMCApp->ResetGenerator(gen);
814 // Delete Lego Object
815 delete fLego; fLego=0;
818 //_______________________________________________________________________
819 void AliRun::SetConfigFunction(const char * config)
822 // Set the signature of the function contained in Config.C to configure
825 fConfigFunction=config;
832 //_______________________________________________________________________
833 void AliRun::Field(const Double_t* x, Double_t *b) const
836 // Return the value of the magnetic field
839 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
843 Field()->Field(xfloat,bfloat);
844 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
847 AliError("No mag field defined!");
853 // End of MC Application
856 //_______________________________________________________________________
857 void AliRun::Streamer(TBuffer &R__b)
859 // Stream an object of class AliRun.
861 if (R__b.IsReading()) {
862 if (!gAlice) gAlice = this;
863 AliRun::Class()->ReadBuffer(R__b, this);
864 gROOT->GetListOfBrowsables()->Add(this,"Run");
868 AliRun::Class()->WriteBuffer(R__b, this);
871 //_______________________________________________________________________
873 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
875 fRunLoader->GetHeader()->SetGenEventHeader(header);
877 //_______________________________________________________________________
879 Int_t AliRun::GetEvNumber() const
881 //Returns number of current event
882 if (fRunLoader == 0x0)
884 AliError("RunLoader is not set. Can not load data.");
888 return fRunLoader->GetEventNumber();
890 //_______________________________________________________________________
892 void AliRun::SetRunLoader(AliRunLoader* rloader)
895 // Set the loader of the run
897 fRunLoader = rloader;
898 if (fRunLoader == 0x0) return;
901 TFolder* evfold = fRunLoader->GetEventFolder();
902 if (evfold) evfoldname = evfold->GetName();
903 else AliWarning("Did not get Event Folder from Run Loader");
905 if ( fRunLoader->GetAliRun() )
906 {//if alrun already exists in folder
907 if (fRunLoader->GetAliRun() != this )
908 {//and is different than this - crash
909 AliFatal("AliRun is already in Folder and it is not this object");
915 evfold->Add(this);//Post this AliRun to Folder
918 TIter next(fModules);
920 while((module = (AliModule*)next()))
922 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
923 module->SetRunLoader(fRunLoader);
924 AliDetector* detector = dynamic_cast<AliDetector*>(module);
927 AliLoader* loader = fRunLoader->GetLoader(detector);
930 AliError(Form("Can not get loader for detector %s", detector->GetName()));
934 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
935 detector->SetLoader(loader);
941 void AliRun::AddModule(AliModule* mod)
944 // Add a module to the module list
946 if (mod == 0x0) return;
947 if (strlen(mod->GetName()) == 0) return;
948 if (GetModuleID(mod->GetName()) >= 0) return;
950 AliDebug(1, mod->GetName());
951 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
952 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
959 // added by Alberto Colla
960 //_____________________________________________________________________________
961 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
962 { return !gSystem->AccessPathName(fnam,mode);}
964 //______________________________________________________
965 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
967 TString str = name; gSystem->ExpandPathName(str);
968 return !gSystem->AccessPathName(str.Data(),mode);