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>
53 #include "AliDetector.h"
54 #include "AliDisplay.h"
55 #include "AliHeader.h"
57 #include "AliLegoGenerator.h"
60 #include "AliMagFCM.h"
61 #include "AliMagFDM.h"
71 //_______________________________________________________________________
87 fPDGDB(0), //Particle factory object
88 fConfigFunction("\0"),
93 // Default constructor for AliRun
95 AliConfig::Instance();//skowron 29 Feb 2002
96 //ensures that the folder structure is build
99 //_______________________________________________________________________
100 AliRun::AliRun(const AliRun& arun):
116 fPDGDB(0), //Particle factory object
117 fConfigFunction("\0"),
122 // Copy constructor for AliRun
127 //_____________________________________________________________________________
128 AliRun::AliRun(const char *name, const char *title):
135 fModules(new TObjArray(77)), // Support list for the Detectors
144 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
145 fConfigFunction("Config();"),
146 fRandom(new TRandom3()),
150 // Constructor for the main processor.
151 // Creates the geometry
152 // Creates the list of Detectors.
153 // Creates the list of particles.
158 // Set random number generator
161 if (gSystem->Getenv("CONFIG_SEED")) {
162 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
165 // Add to list of browsable
166 gROOT->GetListOfBrowsables()->Add(this,name);
167 // Create the TNode geometry for the event display
168 BuildSimpleGeometry();
170 // Create default mag field
173 // Add particle list to configuration
174 AliConfig::Instance()->Add(fPDGDB);
179 //_______________________________________________________________________
183 // Default AliRun destructor
185 gROOT->GetListOfBrowsables()->Remove(this);
189 TFolder* evfold = fRunLoader->GetEventFolder();
190 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
191 TIter next(fModules);
193 while((mod = (AliModule*)next()))
195 modfold->Remove(mod);
214 //_______________________________________________________________________
215 void AliRun::Copy(TObject &) const
217 Fatal("Copy","Not implemented!\n");
220 //_______________________________________________________________________
224 // Initialize Alice geometry
229 //_______________________________________________________________________
230 void AliRun::BuildSimpleGeometry()
233 // Create a simple TNode geometry used by Root display engine
235 // Initialise geometry
237 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
238 new TMaterial("void","Vacuum",0,0,0); //Everything is void
239 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
240 brik->SetVisibility(0);
241 new TNode("alice","alice","S_alice");
244 //_______________________________________________________________________
245 void AliRun::CleanDetectors()
248 // Clean Detectors at the end of event
250 fRunLoader->CleanDetectors();
253 //_______________________________________________________________________
254 void AliRun::ResetHits()
259 //_______________________________________________________________________
260 AliGenerator* AliRun::Generator() const
262 return fMCApp->Generator();
265 //_______________________________________________________________________
266 void AliRun::SetField(AliMagF* magField)
269 // Set Magnetic Field Map
275 //_______________________________________________________________________
276 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
277 Float_t maxField, const char* filename)
280 // Set magnetic field parameters
281 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
282 // version Magnetic field map version (only 1 active now)
283 // scale Scale factor for the magnetic field
284 // maxField Maximum value for the magnetic field
287 // --- Sanity check on mag field flags
288 if(fField) delete fField;
290 fField = new AliMagFC("Map1"," ",type,scale,maxField);
291 } else if(version<=2) {
292 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
294 } else if(version==3) {
295 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
298 Warning("SetField","Invalid map %d\n",version);
302 //_____________________________________________________________________________
304 void AliRun::InitLoaders()
306 //creates list of getters
307 if (GetDebug()) Info("InitLoaders","");
308 TIter next(fModules);
310 while((mod = (AliModule*)next()))
312 mod->SetRunLoader(fRunLoader);
313 AliDetector *det = dynamic_cast<AliDetector*>(mod);
316 if (GetDebug()) Info("InitLoaders"," Adding %s ",det->GetName());
317 fRunLoader->AddLoader(det);
320 if (GetDebug()) Info("InitLoaders","Done");
322 //_____________________________________________________________________________
324 void AliRun::FinishRun()
327 // Called at the end of the run.
332 if (GetDebug()) Info("FinishRun"," Finish Lego");
333 fRunLoader->CdGAFile();
337 // Clean detector information
338 TIter next(fModules);
340 while((detector = dynamic_cast<AliModule*>(next()))) {
341 if (GetDebug()) Info("FinishRun"," %s->FinishRun()",detector->GetName());
342 detector->FinishRun();
345 if (GetDebug()) Info("FinishRun"," fRunLoader->WriteHeader(OVERWRITE)");
346 fRunLoader->WriteHeader("OVERWRITE");
348 // Write AliRun info and all detectors parameters
349 fRunLoader->CdGAFile();
350 Write(0,TObject::kOverwrite);//write AliRun
351 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
353 // Clean tree information
354 if (GetDebug()) Info("FinishRun"," fRunLoader->Stack()->FinishRun()");
355 fRunLoader->Stack()->FinishRun();
357 if(fMCApp) fMCApp->FinishRun();
359 fRunLoader->Synchronize();
362 //_______________________________________________________________________
363 void AliRun::Announce() const
366 // Announce the current version of AliRoot
369 "****************************************************************\n");
370 printf("%6s","*");printf("%64s","*\n");
373 printf(" You are running AliRoot version NewIO\n");
376 printf(" The cvs tag for the current program is $Name$\n");
378 printf("%6s","*");printf("%64s","*\n");
380 "****************************************************************\n");
383 //_______________________________________________________________________
384 AliModule *AliRun::GetModule(const char *name) const
387 // Return pointer to detector from name
389 return dynamic_cast<AliModule*>(fModules->FindObject(name));
392 //_______________________________________________________________________
393 AliDetector *AliRun::GetDetector(const char *name) const
396 // Return pointer to detector from name
398 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
401 //_______________________________________________________________________
402 Int_t AliRun::GetModuleID(const char *name) const
405 // Return galice internal detector identifier from name
408 TObject *mod=fModules->FindObject(name);
409 if(mod) i=fModules->IndexOf(mod);
413 //_______________________________________________________________________
414 Int_t AliRun::GetEvent(Int_t event)
417 // Reloads data containers in folders # event
418 // Set branch addresses
420 if (fRunLoader == 0x0)
422 Error("GetEvent","RunLoader is not set. Can not load data.");
425 /*****************************************/
426 /**** P R E R E L O A D I N G ****/
427 /*****************************************/
428 // Reset existing structures
430 fMCApp->ResetTrackReferences();
434 /*****************************************/
435 /**** R E L O A D ****/
436 /*****************************************/
438 fRunLoader->GetEvent(event);
440 /*****************************************/
441 /**** P O S T R E L O A D I N G ****/
442 /*****************************************/
444 // Set Trees branch addresses
445 TIter next(fModules);
447 while((detector = dynamic_cast<AliModule*>(next())))
449 detector->SetTreeAddress();
452 return fRunLoader->GetHeader()->GetNtrack();
455 //_______________________________________________________________________
456 TGeometry *AliRun::GetGeometry()
459 // Import Alice geometry from current file
460 // Return pointer to geometry object
462 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
464 // Unlink and relink nodes in detectors
465 // This is bad and there must be a better way...
468 TIter next(fModules);
470 while((detector = dynamic_cast<AliModule*>(next()))) {
471 TList *dnodes=detector->Nodes();
474 for ( j=0; j<dnodes->GetSize(); j++) {
475 node = dynamic_cast<TNode*>(dnodes->At(j));
476 node1 = fGeometry->GetNode(node->GetName());
477 dnodes->Remove(node);
478 dnodes->AddAt(node1,j);
484 //_______________________________________________________________________
485 void AliRun::SetBaseFile(const char *filename)
487 fBaseFileName = filename;
490 //_______________________________________________________________________
491 void AliRun::ResetDigits()
494 // Reset all Detectors digits
496 TIter next(fModules);
498 while((detector = dynamic_cast<AliModule*>(next()))) {
499 detector->ResetDigits();
503 //_______________________________________________________________________
504 void AliRun::ResetSDigits()
507 // Reset all Detectors digits
509 TIter next(fModules);
511 while((detector = dynamic_cast<AliModule*>(next()))) {
512 detector->ResetSDigits();
517 //_______________________________________________________________________
519 void AliRun::ResetPoints()
522 // Reset all Detectors points
524 TIter next(fModules);
526 while((detector = dynamic_cast<AliModule*>(next()))) {
527 detector->ResetPoints();
530 //_______________________________________________________________________
532 void AliRun::InitMC(const char *setup)
535 // Initialize ALICE Simulation run
540 Warning("Init","Cannot initialise AliRun twice!\n");
545 fMCApp=new AliMC(GetName(),GetTitle());
547 gROOT->LoadMacro(setup);
548 gInterpreter->ProcessLine(fConfigFunction.Data());
550 fRunLoader->CdGAFile();
552 AliPDG::AddParticlesToPdgDataBase();
554 fNdets = fModules->GetLast()+1;
556 TIter next(fModules);
557 for(Int_t i=0; i<fNdets; ++i)
559 TObject *objfirst, *objlast;
560 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
561 objlast = gDirectory->GetList()->Last();
563 // Add Detector histograms in Detector list of histograms
564 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
565 else objfirst = gDirectory->GetList()->First();
568 detector->Histograms()->Add(objfirst);
569 objfirst = gDirectory->GetList()->After(objfirst);
575 //Must be here because some MCs (G4) adds detectors here and not in Config.C
577 fRunLoader->MakeTree("E");
580 fRunLoader->LoadKinematics("RECREATE");
581 fRunLoader->LoadTrackRefs("RECREATE");
582 fRunLoader->LoadHits("all","RECREATE");
586 // Save stuff at the beginning of the file to avoid file corruption
587 fRunLoader->CdGAFile();
589 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
592 //_______________________________________________________________________
594 void AliRun::RunMC(Int_t nevent, const char *setup)
597 // Main function to be called to process a galice run
599 // Root > gAlice.Run();
600 // a positive number of events will cause the finish routine
603 fEventsPerRun = nevent;
604 // check if initialisation has been done
605 if (!fInitDone) InitMC(setup);
607 // Create the Root Tree with one branch per detector
608 //Hits moved to begin event -> now we are crating separate tree for each event
610 gMC->ProcessRun(nevent);
612 // End of this run, close files
613 if(nevent>0) FinishRun();
616 //_______________________________________________________________________
617 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
620 // Main function to be called to reconstruct Alice event
622 Int_t nev = fRunLoader->GetNumberOfEvents();
623 if (GetDebug()) Info("RunReco","Found %d events",nev);
624 Int_t nFirst = first;
625 Int_t nLast = (last < 0)? nev : last;
627 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
628 if (GetDebug()) Info("RunReco","Processing event %d",nevent);
630 Digits2Reco(selected);
634 //_______________________________________________________________________
636 void AliRun::Hits2Digits(const char *selected)
639 // Convert Hits to sumable digits
641 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
643 Hits2SDigits(selected);
644 SDigits2Digits(selected);
649 //_______________________________________________________________________
651 void AliRun::Tree2Tree(Option_t *option, const char *selected)
654 // Function to transform the content of
656 // - TreeH to TreeS (option "S")
657 // - TreeS to TreeD (option "D")
658 // - TreeD to TreeR (option "R")
660 // If multiple options are specified ("SDR"), transformation will be done in sequence for
661 // selected detector and for all detectors if none is selected (detector string
662 // can contain blank separated list of detector names).
665 const char *oS = strstr(option,"S");
666 const char *oD = strstr(option,"D");
667 const char *oR = strstr(option,"R");
669 TObjArray *detectors = Detectors();
671 TIter next(detectors);
673 AliDetector *detector = 0;
675 while((detector = dynamic_cast<AliDetector*>(next()))) {
677 if (strcmp(detector->GetName(),selected)) continue;
678 if (detector->IsActive())
681 AliLoader* loader = detector->GetLoader();
682 if (loader == 0x0) continue;
686 if (GetDebug()) Info("Tree2Tree","Processing Hits2SDigits for %s ...",detector->GetName());
687 loader->LoadHits("read");
688 if (loader->TreeS() == 0x0) loader->MakeTree("S");
689 detector->MakeBranch(option);
690 detector->SetTreeAddress();
691 detector->Hits2SDigits();
692 loader->UnloadHits();
693 loader->UnloadSDigits();
697 if (GetDebug()) Info("Tree2Tree","Processing SDigits2Digits for %s ...",detector->GetName());
698 loader->LoadSDigits("read");
699 if (loader->TreeD() == 0x0) loader->MakeTree("D");
700 detector->MakeBranch(option);
701 detector->SetTreeAddress();
702 detector->SDigits2Digits();
703 loader->UnloadSDigits();
704 loader->UnloadDigits();
708 if (GetDebug()) Info("Tree2Tree","Processing Digits2Reco for %s ...",detector->GetName());
709 loader->LoadDigits("read");
710 if (loader->TreeR() == 0x0) loader->MakeTree("R");
711 detector->MakeBranch(option);
712 detector->SetTreeAddress();
713 detector->Digits2Reco();
714 loader->UnloadDigits();
715 loader->UnloadRecPoints();
722 //_______________________________________________________________________
723 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
724 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
725 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
728 // Generates lego plots of:
729 // - radiation length map phi vs theta
730 // - radiation length map phi vs eta
731 // - interaction length map
732 // - g/cm2 length map
734 // ntheta bins in theta, eta
735 // themin minimum angle in theta (degrees)
736 // themax maximum angle in theta (degrees)
738 // phimin minimum angle in phi (degrees)
739 // phimax maximum angle in phi (degrees)
740 // rmin minimum radius
741 // rmax maximum radius
744 // The number of events generated = ntheta*nphi
745 // run input parameters in macro setup (default="Config.C")
747 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
750 <img src="picts/AliRunLego1.gif">
755 <img src="picts/AliRunLego2.gif">
760 <img src="picts/AliRunLego3.gif">
765 // check if initialisation has been done
766 // If runloader has been initialized, set the number of events per file to nc1 * nc2
769 if (!gener) gener = new AliLegoGenerator();
771 // Configure Generator
772 gener->SetRadiusRange(rmin, rmax);
773 gener->SetZMax(zmax);
774 gener->SetCoor1Range(nc1, c1min, c1max);
775 gener->SetCoor2Range(nc2, c2min, c2max);
779 fLego = new AliLego("lego",gener);
781 if (!fInitDone) InitMC(setup);
782 //Save current generator
784 AliGenerator *gen=fMCApp->Generator();
785 fMCApp->ResetGenerator(gener);
786 //Prepare MC for Lego Run
791 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
792 //gMC->ProcessRun(nc1*nc2+1);
793 gMC->ProcessRun(nc1*nc2);
795 // End of this run, close files
797 // Restore current generator
798 fMCApp->ResetGenerator(gen);
799 // Delete Lego Object
800 delete fLego; fLego=0;
803 //_______________________________________________________________________
804 void AliRun::SetConfigFunction(const char * config)
807 // Set the signature of the function contained in Config.C to configure
810 fConfigFunction=config;
817 //_______________________________________________________________________
818 void AliRun::Field(const Double_t* x, Double_t *b) const
821 // Return the value of the magnetic field
824 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
828 Field()->Field(xfloat,bfloat);
829 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
832 printf("No mag field defined!\n");
838 // End of MC Application
841 //_______________________________________________________________________
842 void AliRun::Streamer(TBuffer &R__b)
844 // Stream an object of class AliRun.
846 if (R__b.IsReading()) {
847 if (!gAlice) gAlice = this;
848 AliRun::Class()->ReadBuffer(R__b, this);
849 gROOT->GetListOfBrowsables()->Add(this,"Run");
853 AliRun::Class()->WriteBuffer(R__b, this);
856 //_______________________________________________________________________
858 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
860 fRunLoader->GetHeader()->SetGenEventHeader(header);
862 //_______________________________________________________________________
864 Int_t AliRun::GetEvNumber() const
866 //Returns number of current event
867 if (fRunLoader == 0x0)
869 Error("GetEvent","RunLoader is not set. Can not load data.");
873 return fRunLoader->GetEventNumber();
875 //_______________________________________________________________________
877 void AliRun::SetRunLoader(AliRunLoader* rloader)
880 // Set the loader of the run
882 fRunLoader = rloader;
883 if (fRunLoader == 0x0) return;
886 TFolder* evfold = fRunLoader->GetEventFolder();
887 if (evfold) evfoldname = evfold->GetName();
888 else Warning("SetRunLoader","Did not get Event Folder from Run Loader");
890 if ( fRunLoader->GetAliRun() )
891 {//if alrun already exists in folder
892 if (fRunLoader->GetAliRun() != this )
893 {//and is different than this - crash
894 Fatal("AliRun","AliRun is already in Folder and it is not this object");
900 evfold->Add(this);//Post this AliRun to Folder
903 TIter next(fModules);
905 while((module = (AliModule*)next()))
907 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
908 module->SetRunLoader(fRunLoader);
909 AliDetector* detector = dynamic_cast<AliDetector*>(module);
912 AliLoader* loader = fRunLoader->GetLoader(detector);
915 Error("SetRunLoader","Can not get loader for detector %s",detector->GetName());
919 if (GetDebug()) Info("SetRunLoader","Setting loader for detector %s",detector->GetName());
920 detector->SetLoader(loader);
926 void AliRun::AddModule(AliModule* mod)
929 // Add a module to the module list
931 if (mod == 0x0) return;
932 if (strlen(mod->GetName()) == 0) return;
933 if (GetModuleID(mod->GetName()) >= 0) return;
935 if (GetDebug()) Info("AddModule","%s",mod->GetName());
936 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
937 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());