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");
544 fMCApp=new AliMC(GetName(),GetTitle());
546 gROOT->LoadMacro(setup);
547 gInterpreter->ProcessLine(fConfigFunction.Data());
549 fRunLoader->CdGAFile();
551 AliPDG::AddParticlesToPdgDataBase();
553 fNdets = fModules->GetLast()+1;
555 // Added also after in case of interactive initialisation of modules
556 fNdets = fModules->GetLast()+1;
558 TIter next(fModules);
559 for(Int_t i=0; i<fNdets; ++i)
561 TObject *objfirst, *objlast;
562 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
563 objlast = gDirectory->GetList()->Last();
565 // Add Detector histograms in Detector list of histograms
566 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
567 else objfirst = gDirectory->GetList()->First();
570 detector->Histograms()->Add(objfirst);
571 objfirst = gDirectory->GetList()->After(objfirst);
577 //Must be here because some MCs (G4) adds detectors here and not in Config.C
579 fRunLoader->MakeTree("E");
582 fRunLoader->LoadKinematics("RECREATE");
583 fRunLoader->LoadTrackRefs("RECREATE");
584 fRunLoader->LoadHits("all","RECREATE");
588 // Save stuff at the beginning of the file to avoid file corruption
589 fRunLoader->CdGAFile();
591 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
594 //_______________________________________________________________________
596 void AliRun::RunMC(Int_t nevent, const char *setup)
599 // Main function to be called to process a galice run
601 // Root > gAlice.Run();
602 // a positive number of events will cause the finish routine
605 fEventsPerRun = nevent;
606 // check if initialisation has been done
607 if (!fInitDone) InitMC(setup);
609 // Create the Root Tree with one branch per detector
610 //Hits moved to begin event -> now we are crating separate tree for each event
612 gMC->ProcessRun(nevent);
614 // End of this run, close files
615 if(nevent>0) FinishRun();
618 //_______________________________________________________________________
619 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
622 // Main function to be called to reconstruct Alice event
624 Int_t nev = fRunLoader->GetNumberOfEvents();
625 if (GetDebug()) Info("RunReco","Found %d events",nev);
626 Int_t nFirst = first;
627 Int_t nLast = (last < 0)? nev : last;
629 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
630 if (GetDebug()) Info("RunReco","Processing event %d",nevent);
632 Digits2Reco(selected);
636 //_______________________________________________________________________
638 void AliRun::Hits2Digits(const char *selected)
641 // Convert Hits to sumable digits
643 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
645 Hits2SDigits(selected);
646 SDigits2Digits(selected);
651 //_______________________________________________________________________
653 void AliRun::Tree2Tree(Option_t *option, const char *selected)
656 // Function to transform the content of
658 // - TreeH to TreeS (option "S")
659 // - TreeS to TreeD (option "D")
660 // - TreeD to TreeR (option "R")
662 // If multiple options are specified ("SDR"), transformation will be done in sequence for
663 // selected detector and for all detectors if none is selected (detector string
664 // can contain blank separated list of detector names).
667 const char *oS = strstr(option,"S");
668 const char *oD = strstr(option,"D");
669 const char *oR = strstr(option,"R");
671 TObjArray *detectors = Detectors();
673 TIter next(detectors);
675 AliDetector *detector = 0;
677 while((detector = dynamic_cast<AliDetector*>(next()))) {
679 if (strcmp(detector->GetName(),selected)) continue;
680 if (detector->IsActive())
683 AliLoader* loader = detector->GetLoader();
684 if (loader == 0x0) continue;
688 if (GetDebug()) Info("Tree2Tree","Processing Hits2SDigits for %s ...",detector->GetName());
689 loader->LoadHits("read");
690 if (loader->TreeS() == 0x0) loader->MakeTree("S");
691 detector->MakeBranch(option);
692 detector->SetTreeAddress();
693 detector->Hits2SDigits();
694 loader->UnloadHits();
695 loader->UnloadSDigits();
699 if (GetDebug()) Info("Tree2Tree","Processing SDigits2Digits for %s ...",detector->GetName());
700 loader->LoadSDigits("read");
701 if (loader->TreeD() == 0x0) loader->MakeTree("D");
702 detector->MakeBranch(option);
703 detector->SetTreeAddress();
704 detector->SDigits2Digits();
705 loader->UnloadSDigits();
706 loader->UnloadDigits();
710 if (GetDebug()) Info("Tree2Tree","Processing Digits2Reco for %s ...",detector->GetName());
711 loader->LoadDigits("read");
712 if (loader->TreeR() == 0x0) loader->MakeTree("R");
713 detector->MakeBranch(option);
714 detector->SetTreeAddress();
715 detector->Digits2Reco();
716 loader->UnloadDigits();
717 loader->UnloadRecPoints();
724 //_______________________________________________________________________
725 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
726 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
727 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
730 // Generates lego plots of:
731 // - radiation length map phi vs theta
732 // - radiation length map phi vs eta
733 // - interaction length map
734 // - g/cm2 length map
736 // ntheta bins in theta, eta
737 // themin minimum angle in theta (degrees)
738 // themax maximum angle in theta (degrees)
740 // phimin minimum angle in phi (degrees)
741 // phimax maximum angle in phi (degrees)
742 // rmin minimum radius
743 // rmax maximum radius
746 // The number of events generated = ntheta*nphi
747 // run input parameters in macro setup (default="Config.C")
749 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
752 <img src="picts/AliRunLego1.gif">
757 <img src="picts/AliRunLego2.gif">
762 <img src="picts/AliRunLego3.gif">
767 // check if initialisation has been done
768 // If runloader has been initialized, set the number of events per file to nc1 * nc2
771 if (!gener) gener = new AliLegoGenerator();
773 // Configure Generator
774 gener->SetRadiusRange(rmin, rmax);
775 gener->SetZMax(zmax);
776 gener->SetCoor1Range(nc1, c1min, c1max);
777 gener->SetCoor2Range(nc2, c2min, c2max);
781 fLego = new AliLego("lego",gener);
783 if (!fInitDone) InitMC(setup);
784 //Save current generator
786 AliGenerator *gen=fMCApp->Generator();
787 fMCApp->ResetGenerator(gener);
788 //Prepare MC for Lego Run
793 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
794 //gMC->ProcessRun(nc1*nc2+1);
795 gMC->ProcessRun(nc1*nc2);
797 // End of this run, close files
799 // Restore current generator
800 fMCApp->ResetGenerator(gen);
801 // Delete Lego Object
802 delete fLego; fLego=0;
805 //_______________________________________________________________________
806 void AliRun::SetConfigFunction(const char * config)
809 // Set the signature of the function contained in Config.C to configure
812 fConfigFunction=config;
819 //_______________________________________________________________________
820 void AliRun::Field(const Double_t* x, Double_t *b) const
823 // Return the value of the magnetic field
826 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
830 Field()->Field(xfloat,bfloat);
831 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
834 printf("No mag field defined!\n");
840 // End of MC Application
843 //_______________________________________________________________________
844 void AliRun::Streamer(TBuffer &R__b)
846 // Stream an object of class AliRun.
848 if (R__b.IsReading()) {
849 if (!gAlice) gAlice = this;
850 AliRun::Class()->ReadBuffer(R__b, this);
851 gROOT->GetListOfBrowsables()->Add(this,"Run");
855 AliRun::Class()->WriteBuffer(R__b, this);
858 //_______________________________________________________________________
860 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
862 fRunLoader->GetHeader()->SetGenEventHeader(header);
864 //_______________________________________________________________________
866 Int_t AliRun::GetEvNumber() const
868 //Returns number of current event
869 if (fRunLoader == 0x0)
871 Error("GetEvent","RunLoader is not set. Can not load data.");
875 return fRunLoader->GetEventNumber();
877 //_______________________________________________________________________
879 void AliRun::SetRunLoader(AliRunLoader* rloader)
882 // Set the loader of the run
884 fRunLoader = rloader;
885 if (fRunLoader == 0x0) return;
888 TFolder* evfold = fRunLoader->GetEventFolder();
889 if (evfold) evfoldname = evfold->GetName();
890 else Warning("SetRunLoader","Did not get Event Folder from Run Loader");
892 if ( fRunLoader->GetAliRun() )
893 {//if alrun already exists in folder
894 if (fRunLoader->GetAliRun() != this )
895 {//and is different than this - crash
896 Fatal("AliRun","AliRun is already in Folder and it is not this object");
902 evfold->Add(this);//Post this AliRun to Folder
905 TIter next(fModules);
907 while((module = (AliModule*)next()))
909 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
910 module->SetRunLoader(fRunLoader);
911 AliDetector* detector = dynamic_cast<AliDetector*>(module);
914 AliLoader* loader = fRunLoader->GetLoader(detector);
917 Error("SetRunLoader","Can not get loader for detector %s",detector->GetName());
921 if (GetDebug()) Info("SetRunLoader","Setting loader for detector %s",detector->GetName());
922 detector->SetLoader(loader);
928 void AliRun::AddModule(AliModule* mod)
931 // Add a module to the module list
933 if (mod == 0x0) return;
934 if (strlen(mod->GetName()) == 0) return;
935 if (GetModuleID(mod->GetName()) >= 0) return;
937 if (GetDebug()) Info("AddModule","%s",mod->GetName());
938 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
939 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());