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);
176 // Create default mag field
179 // Add particle list to configuration
180 AliConfig::Instance()->Add(fPDGDB);
185 //_______________________________________________________________________
189 // Default AliRun destructor
191 gROOT->GetListOfBrowsables()->Remove(this);
195 TFolder* evfold = fRunLoader->GetEventFolder();
196 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
197 TIter next(fModules);
199 while((mod = (AliModule*)next()))
201 modfold->Remove(mod);
220 //_______________________________________________________________________
221 void AliRun::Copy(TObject &) const
223 AliFatal("Not implemented!");
226 //_______________________________________________________________________
230 // Initialize Alice geometry
235 //_______________________________________________________________________
236 void AliRun::BuildSimpleGeometry()
239 // Create a simple TNode geometry used by Root display engine
241 // Initialise geometry
243 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
244 new TMaterial("void","Vacuum",0,0,0); //Everything is void
245 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
246 brik->SetVisibility(0);
247 new TNode("alice","alice","S_alice");
250 //_______________________________________________________________________
251 void AliRun::CleanDetectors()
254 // Clean Detectors at the end of event
256 fRunLoader->CleanDetectors();
259 //_______________________________________________________________________
260 void AliRun::ResetHits()
265 //_______________________________________________________________________
266 AliGenerator* AliRun::Generator() const
268 return fMCApp->Generator();
271 //_______________________________________________________________________
272 void AliRun::SetField(AliMagF* magField)
275 // Set Magnetic Field Map
281 //_______________________________________________________________________
282 void AliRun::SetRootGeometry(Bool_t flag)
284 // Instruct application that the geometry is to be retreived from a root file.
285 fIsRootGeometry = flag;
286 if (flag) gMC->SetRootGeometry();
288 //_______________________________________________________________________
289 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
290 Float_t maxField, const char* filename)
293 // Set magnetic field parameters
294 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
295 // version Magnetic field map version (only 1 active now)
296 // scale Scale factor for the magnetic field
297 // maxField Maximum value for the magnetic field
300 // --- Sanity check on mag field flags
301 if(fField) delete fField;
303 fField = new AliMagFC("Map1"," ",type,scale,maxField);
304 } else if(version<=2) {
305 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
307 } else if(version==3) {
308 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
311 AliWarning(Form("Invalid map %d",version));
315 //_____________________________________________________________________________
317 void AliRun::InitLoaders()
319 //creates list of getters
321 TIter next(fModules);
323 while((mod = (AliModule*)next()))
325 mod->SetRunLoader(fRunLoader);
326 AliDetector *det = dynamic_cast<AliDetector*>(mod);
329 AliDebug(2, Form("Adding %s", det->GetName()));
330 fRunLoader->AddLoader(det);
335 //_____________________________________________________________________________
337 void AliRun::FinishRun()
340 // Called at the end of the run.
345 AliDebug(1, "Finish Lego");
346 fRunLoader->CdGAFile();
350 // Clean detector information
351 TIter next(fModules);
353 while((detector = dynamic_cast<AliModule*>(next()))) {
354 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
355 detector->FinishRun();
358 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
359 fRunLoader->WriteHeader("OVERWRITE");
361 // Write AliRun info and all detectors parameters
362 fRunLoader->CdGAFile();
363 Write(0,TObject::kOverwrite);//write AliRun
364 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
366 // Clean tree information
367 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
368 fRunLoader->Stack()->FinishRun();
370 if(fMCApp) fMCApp->FinishRun();
372 fRunLoader->Synchronize();
375 //_______________________________________________________________________
376 void AliRun::Announce() const
379 // Announce the current version of AliRoot
382 "****************************************************************\n");
383 printf("%6s","*");printf("%64s","*\n");
386 printf(" You are running AliRoot version NewIO\n");
389 printf(" The cvs tag for the current program is $Name$\n");
391 printf("%6s","*");printf("%64s","*\n");
393 "****************************************************************\n");
396 //_______________________________________________________________________
397 AliModule *AliRun::GetModule(const char *name) const
400 // Return pointer to detector from name
402 return dynamic_cast<AliModule*>(fModules->FindObject(name));
405 //_______________________________________________________________________
406 AliDetector *AliRun::GetDetector(const char *name) const
409 // Return pointer to detector from name
411 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
414 //_______________________________________________________________________
415 Int_t AliRun::GetModuleID(const char *name) const
418 // Return galice internal detector identifier from name
421 TObject *mod=fModules->FindObject(name);
422 if(mod) i=fModules->IndexOf(mod);
426 //_______________________________________________________________________
427 Int_t AliRun::GetEvent(Int_t event)
430 // Reloads data containers in folders # event
431 // Set branch addresses
433 if (fRunLoader == 0x0)
435 AliError("RunLoader is not set. Can not load data.");
438 /*****************************************/
439 /**** P R E R E L O A D I N G ****/
440 /*****************************************/
441 // Reset existing structures
443 fMCApp->ResetTrackReferences();
447 /*****************************************/
448 /**** R E L O A D ****/
449 /*****************************************/
451 fRunLoader->GetEvent(event);
453 /*****************************************/
454 /**** P O S T R E L O A D I N G ****/
455 /*****************************************/
457 // Set Trees branch addresses
458 TIter next(fModules);
460 while((detector = dynamic_cast<AliModule*>(next())))
462 detector->SetTreeAddress();
465 return fRunLoader->GetHeader()->GetNtrack();
468 //_______________________________________________________________________
469 TGeometry *AliRun::GetGeometry()
471 // Create the TNode geometry for the event display
473 BuildSimpleGeometry();
475 // Unlink and relink nodes in detectors
476 // This is bad and there must be a better way...
479 TIter next(fModules);
481 while((detector = dynamic_cast<AliModule*>(next()))) {
482 detector->BuildGeometry();
483 TList *dnodes=detector->Nodes();
486 for ( j=0; j<dnodes->GetSize(); j++) {
487 node = dynamic_cast<TNode*>(dnodes->At(j));
488 node1 = fGeometry->GetNode(node->GetName());
489 dnodes->Remove(node);
490 dnodes->AddAt(node1,j);
497 //_______________________________________________________________________
498 void AliRun::SetBaseFile(const char *filename)
500 fBaseFileName = filename;
503 //_______________________________________________________________________
504 void AliRun::ResetDigits()
507 // Reset all Detectors digits
509 TIter next(fModules);
511 while((detector = dynamic_cast<AliModule*>(next()))) {
512 detector->ResetDigits();
516 //_______________________________________________________________________
517 void AliRun::ResetSDigits()
520 // Reset all Detectors digits
522 TIter next(fModules);
524 while((detector = dynamic_cast<AliModule*>(next()))) {
525 detector->ResetSDigits();
530 //_______________________________________________________________________
532 void AliRun::ResetPoints()
535 // Reset all Detectors points
537 TIter next(fModules);
539 while((detector = dynamic_cast<AliModule*>(next()))) {
540 detector->ResetPoints();
543 //_______________________________________________________________________
545 void AliRun::InitMC(const char *setup)
548 // Initialize ALICE Simulation run
553 AliWarning("Cannot initialise AliRun twice!");
558 fMCApp=new AliMC(GetName(),GetTitle());
560 gROOT->LoadMacro(setup);
561 gInterpreter->ProcessLine(fConfigFunction.Data());
563 fRunLoader->CdGAFile();
565 AliPDG::AddParticlesToPdgDataBase();
567 fNdets = fModules->GetLast()+1;
569 TIter next(fModules);
570 for(Int_t i=0; i<fNdets; ++i)
572 TObject *objfirst, *objlast;
573 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
574 objlast = gDirectory->GetList()->Last();
576 // Add Detector histograms in Detector list of histograms
577 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
578 else objfirst = gDirectory->GetList()->First();
581 detector->Histograms()->Add(objfirst);
582 objfirst = gDirectory->GetList()->After(objfirst);
588 //Must be here because some MCs (G4) adds detectors here and not in Config.C
590 fRunLoader->MakeTree("E");
593 fRunLoader->LoadKinematics("RECREATE");
594 fRunLoader->LoadTrackRefs("RECREATE");
595 fRunLoader->LoadHits("all","RECREATE");
599 // Save stuff at the beginning of the file to avoid file corruption
600 fRunLoader->CdGAFile();
602 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
605 //_______________________________________________________________________
607 void AliRun::RunMC(Int_t nevent, const char *setup)
610 // Main function to be called to process a galice run
612 // Root > gAlice.Run();
613 // a positive number of events will cause the finish routine
616 fEventsPerRun = nevent;
617 // check if initialisation has been done
618 if (!fInitDone) InitMC(setup);
620 // Create the Root Tree with one branch per detector
621 //Hits moved to begin event -> now we are crating separate tree for each event
623 gMC->ProcessRun(nevent);
625 // End of this run, close files
626 if(nevent>0) FinishRun();
629 //_______________________________________________________________________
630 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
633 // Main function to be called to reconstruct Alice event
635 Int_t nev = fRunLoader->GetNumberOfEvents();
636 AliDebug(1, Form("Found %d events", nev));
637 Int_t nFirst = first;
638 Int_t nLast = (last < 0)? nev : last;
640 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
641 AliDebug(1, Form("Processing event %d", nevent));
643 Digits2Reco(selected);
647 //_______________________________________________________________________
649 void AliRun::Hits2Digits(const char *selected)
652 // Convert Hits to sumable digits
654 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
656 Hits2SDigits(selected);
657 SDigits2Digits(selected);
662 //_______________________________________________________________________
664 void AliRun::Tree2Tree(Option_t *option, const char *selected)
667 // Function to transform the content of
669 // - TreeH to TreeS (option "S")
670 // - TreeS to TreeD (option "D")
671 // - TreeD to TreeR (option "R")
673 // If multiple options are specified ("SDR"), transformation will be done in sequence for
674 // selected detector and for all detectors if none is selected (detector string
675 // can contain blank separated list of detector names).
678 const char *oS = strstr(option,"S");
679 const char *oD = strstr(option,"D");
680 const char *oR = strstr(option,"R");
682 TObjArray *detectors = Detectors();
684 TIter next(detectors);
686 AliDetector *detector = 0;
688 while((detector = dynamic_cast<AliDetector*>(next()))) {
690 if (strcmp(detector->GetName(),selected)) continue;
691 if (detector->IsActive())
694 AliLoader* loader = detector->GetLoader();
695 if (loader == 0x0) continue;
699 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
700 loader->LoadHits("read");
701 if (loader->TreeS() == 0x0) loader->MakeTree("S");
702 detector->MakeBranch(option);
703 detector->SetTreeAddress();
704 detector->Hits2SDigits();
705 loader->UnloadHits();
706 loader->UnloadSDigits();
710 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
711 loader->LoadSDigits("read");
712 if (loader->TreeD() == 0x0) loader->MakeTree("D");
713 detector->MakeBranch(option);
714 detector->SetTreeAddress();
715 detector->SDigits2Digits();
716 loader->UnloadSDigits();
717 loader->UnloadDigits();
721 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
722 loader->LoadDigits("read");
723 if (loader->TreeR() == 0x0) loader->MakeTree("R");
724 detector->MakeBranch(option);
725 detector->SetTreeAddress();
726 detector->Digits2Reco();
727 loader->UnloadDigits();
728 loader->UnloadRecPoints();
735 //_______________________________________________________________________
736 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
737 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
738 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
741 // Generates lego plots of:
742 // - radiation length map phi vs theta
743 // - radiation length map phi vs eta
744 // - interaction length map
745 // - g/cm2 length map
747 // ntheta bins in theta, eta
748 // themin minimum angle in theta (degrees)
749 // themax maximum angle in theta (degrees)
751 // phimin minimum angle in phi (degrees)
752 // phimax maximum angle in phi (degrees)
753 // rmin minimum radius
754 // rmax maximum radius
757 // The number of events generated = ntheta*nphi
758 // run input parameters in macro setup (default="Config.C")
760 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
763 <img src="picts/AliRunLego1.gif">
768 <img src="picts/AliRunLego2.gif">
773 <img src="picts/AliRunLego3.gif">
778 // check if initialisation has been done
779 // If runloader has been initialized, set the number of events per file to nc1 * nc2
782 if (!gener) gener = new AliLegoGenerator();
784 // Configure Generator
785 gener->SetRadiusRange(rmin, rmax);
786 gener->SetZMax(zmax);
787 gener->SetCoor1Range(nc1, c1min, c1max);
788 gener->SetCoor2Range(nc2, c2min, c2max);
792 fLego = new AliLego("lego",gener);
794 if (!fInitDone) InitMC(setup);
795 //Save current generator
797 AliGenerator *gen=fMCApp->Generator();
798 fMCApp->ResetGenerator(gener);
799 //Prepare MC for Lego Run
804 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
805 //gMC->ProcessRun(nc1*nc2+1);
806 gMC->ProcessRun(nc1*nc2);
808 // End of this run, close files
810 // Restore current generator
811 fMCApp->ResetGenerator(gen);
812 // Delete Lego Object
813 delete fLego; fLego=0;
816 //_______________________________________________________________________
817 void AliRun::SetConfigFunction(const char * config)
820 // Set the signature of the function contained in Config.C to configure
823 fConfigFunction=config;
830 //_______________________________________________________________________
831 void AliRun::Field(const Double_t* x, Double_t *b) const
834 // Return the value of the magnetic field
837 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
841 Field()->Field(xfloat,bfloat);
842 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
845 AliError("No mag field defined!");
851 // End of MC Application
854 //_______________________________________________________________________
855 void AliRun::Streamer(TBuffer &R__b)
857 // Stream an object of class AliRun.
859 if (R__b.IsReading()) {
860 if (!gAlice) gAlice = this;
861 AliRun::Class()->ReadBuffer(R__b, this);
862 gROOT->GetListOfBrowsables()->Add(this,"Run");
866 AliRun::Class()->WriteBuffer(R__b, this);
869 //_______________________________________________________________________
871 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
873 fRunLoader->GetHeader()->SetGenEventHeader(header);
875 //_______________________________________________________________________
877 Int_t AliRun::GetEvNumber() const
879 //Returns number of current event
880 if (fRunLoader == 0x0)
882 AliError("RunLoader is not set. Can not load data.");
886 return fRunLoader->GetEventNumber();
888 //_______________________________________________________________________
890 void AliRun::SetRunLoader(AliRunLoader* rloader)
893 // Set the loader of the run
895 fRunLoader = rloader;
896 if (fRunLoader == 0x0) return;
899 TFolder* evfold = fRunLoader->GetEventFolder();
900 if (evfold) evfoldname = evfold->GetName();
901 else AliWarning("Did not get Event Folder from Run Loader");
903 if ( fRunLoader->GetAliRun() )
904 {//if alrun already exists in folder
905 if (fRunLoader->GetAliRun() != this )
906 {//and is different than this - crash
907 AliFatal("AliRun is already in Folder and it is not this object");
913 evfold->Add(this);//Post this AliRun to Folder
916 TIter next(fModules);
918 while((module = (AliModule*)next()))
920 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
921 module->SetRunLoader(fRunLoader);
922 AliDetector* detector = dynamic_cast<AliDetector*>(module);
925 AliLoader* loader = fRunLoader->GetLoader(detector);
928 AliError(Form("Can not get loader for detector %s", detector->GetName()));
932 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
933 detector->SetLoader(loader);
939 void AliRun::AddModule(AliModule* mod)
942 // Add a module to the module list
944 if (mod == 0x0) return;
945 if (strlen(mod->GetName()) == 0) return;
946 if (GetModuleID(mod->GetName()) >= 0) return;
948 AliDebug(1, mod->GetName());
949 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
950 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
957 // added by Alberto Colla
958 //_____________________________________________________________________________
959 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
960 { return !gSystem->AccessPathName(fnam,mode);}
962 //______________________________________________________
963 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
965 TString str = name; gSystem->ExpandPathName(str);
966 return !gSystem->AccessPathName(str.Data(),mode);