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>
54 #include "AliDetector.h"
55 #include "AliDisplay.h"
56 #include "AliHeader.h"
58 #include "AliLegoGenerator.h"
61 #include "AliMagFCM.h"
62 #include "AliMagFDM.h"
67 #include "AliTPCTrackHitsInterfaces.h"
74 //_______________________________________________________________________
89 fPDGDB(0), //Particle factory object
90 fConfigFunction("\0"),
95 // Default constructor for AliRun
97 AliConfig::Instance();//skowron 29 Feb 2002
98 //ensures that the folder structure is build
101 //_______________________________________________________________________
102 AliRun::AliRun(const AliRun& arun):
117 fPDGDB(0), //Particle factory object
118 fConfigFunction("\0"),
123 // Copy constructor for AliRun
128 //_____________________________________________________________________________
129 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);
177 // Terrible hack to avoid problem with the initialisation of
178 // static and globals on Mac OS X
179 AliClassAliTrackHitsInfo p1=galiclass____AliClassAliTrackHitsInfo;
180 AliClassAliTrackHitsParam p2=galiclass____AliTrackHitsParam;
181 AliClassAliHitInfo p3=galiclass____AliHitInfo;
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::SetField(Int_t type, Int_t version, Float_t scale,
285 Float_t maxField, const char* filename)
288 // Set magnetic field parameters
289 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
290 // version Magnetic field map version (only 1 active now)
291 // scale Scale factor for the magnetic field
292 // maxField Maximum value for the magnetic field
295 // --- Sanity check on mag field flags
296 if(fField) delete fField;
298 fField = new AliMagFC("Map1"," ",type,scale,maxField);
299 } else if(version<=2) {
300 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
302 } else if(version==3) {
303 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
306 AliWarning(Form("Invalid map %d",version));
310 //_____________________________________________________________________________
312 void AliRun::InitLoaders()
314 //creates list of getters
316 TIter next(fModules);
318 while((mod = (AliModule*)next()))
320 mod->SetRunLoader(fRunLoader);
321 AliDetector *det = dynamic_cast<AliDetector*>(mod);
324 AliDebug(2, Form("Adding %s", det->GetName()));
325 fRunLoader->AddLoader(det);
330 //_____________________________________________________________________________
332 void AliRun::FinishRun()
335 // Called at the end of the run.
340 AliDebug(1, "Finish Lego");
341 fRunLoader->CdGAFile();
345 // Clean detector information
346 TIter next(fModules);
348 while((detector = dynamic_cast<AliModule*>(next()))) {
349 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
350 detector->FinishRun();
353 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
354 fRunLoader->WriteHeader("OVERWRITE");
356 // Write AliRun info and all detectors parameters
357 fRunLoader->CdGAFile();
358 Write(0,TObject::kOverwrite);//write AliRun
359 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
361 // Clean tree information
362 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
363 fRunLoader->Stack()->FinishRun();
365 if(fMCApp) fMCApp->FinishRun();
367 fRunLoader->Synchronize();
370 //_______________________________________________________________________
371 void AliRun::Announce() const
374 // Announce the current version of AliRoot
377 "****************************************************************\n");
378 printf("%6s","*");printf("%64s","*\n");
381 printf(" You are running AliRoot version NewIO\n");
384 printf(" The cvs tag for the current program is $Name$\n");
386 printf("%6s","*");printf("%64s","*\n");
388 "****************************************************************\n");
391 //_______________________________________________________________________
392 AliModule *AliRun::GetModule(const char *name) const
395 // Return pointer to detector from name
397 return dynamic_cast<AliModule*>(fModules->FindObject(name));
400 //_______________________________________________________________________
401 AliDetector *AliRun::GetDetector(const char *name) const
404 // Return pointer to detector from name
406 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
409 //_______________________________________________________________________
410 Int_t AliRun::GetModuleID(const char *name) const
413 // Return galice internal detector identifier from name
416 TObject *mod=fModules->FindObject(name);
417 if(mod) i=fModules->IndexOf(mod);
421 //_______________________________________________________________________
422 Int_t AliRun::GetEvent(Int_t event)
425 // Reloads data containers in folders # event
426 // Set branch addresses
428 if (fRunLoader == 0x0)
430 AliError("RunLoader is not set. Can not load data.");
433 /*****************************************/
434 /**** P R E R E L O A D I N G ****/
435 /*****************************************/
436 // Reset existing structures
438 fMCApp->ResetTrackReferences();
442 /*****************************************/
443 /**** R E L O A D ****/
444 /*****************************************/
446 fRunLoader->GetEvent(event);
448 /*****************************************/
449 /**** P O S T R E L O A D I N G ****/
450 /*****************************************/
452 // Set Trees branch addresses
453 TIter next(fModules);
455 while((detector = dynamic_cast<AliModule*>(next())))
457 detector->SetTreeAddress();
460 return fRunLoader->GetHeader()->GetNtrack();
463 //_______________________________________________________________________
464 TGeometry *AliRun::GetGeometry()
467 // Import Alice geometry from current file
468 // Return pointer to geometry object
470 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
472 // Unlink and relink nodes in detectors
473 // This is bad and there must be a better way...
476 TIter next(fModules);
478 while((detector = dynamic_cast<AliModule*>(next()))) {
479 TList *dnodes=detector->Nodes();
482 for ( j=0; j<dnodes->GetSize(); j++) {
483 node = dynamic_cast<TNode*>(dnodes->At(j));
484 node1 = fGeometry->GetNode(node->GetName());
485 dnodes->Remove(node);
486 dnodes->AddAt(node1,j);
492 //_______________________________________________________________________
493 void AliRun::SetBaseFile(const char *filename)
495 fBaseFileName = filename;
498 //_______________________________________________________________________
499 void AliRun::ResetDigits()
502 // Reset all Detectors digits
504 TIter next(fModules);
506 while((detector = dynamic_cast<AliModule*>(next()))) {
507 detector->ResetDigits();
511 //_______________________________________________________________________
512 void AliRun::ResetSDigits()
515 // Reset all Detectors digits
517 TIter next(fModules);
519 while((detector = dynamic_cast<AliModule*>(next()))) {
520 detector->ResetSDigits();
525 //_______________________________________________________________________
527 void AliRun::ResetPoints()
530 // Reset all Detectors points
532 TIter next(fModules);
534 while((detector = dynamic_cast<AliModule*>(next()))) {
535 detector->ResetPoints();
538 //_______________________________________________________________________
540 void AliRun::InitMC(const char *setup)
543 // Initialize ALICE Simulation run
548 AliWarning("Cannot initialise AliRun twice!");
553 fMCApp=new AliMC(GetName(),GetTitle());
555 gROOT->LoadMacro(setup);
556 gInterpreter->ProcessLine(fConfigFunction.Data());
558 fRunLoader->CdGAFile();
560 AliPDG::AddParticlesToPdgDataBase();
562 fNdets = fModules->GetLast()+1;
564 TIter next(fModules);
565 for(Int_t i=0; i<fNdets; ++i)
567 TObject *objfirst, *objlast;
568 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
569 objlast = gDirectory->GetList()->Last();
571 // Add Detector histograms in Detector list of histograms
572 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
573 else objfirst = gDirectory->GetList()->First();
576 detector->Histograms()->Add(objfirst);
577 objfirst = gDirectory->GetList()->After(objfirst);
583 //Must be here because some MCs (G4) adds detectors here and not in Config.C
585 fRunLoader->MakeTree("E");
588 fRunLoader->LoadKinematics("RECREATE");
589 fRunLoader->LoadTrackRefs("RECREATE");
590 fRunLoader->LoadHits("all","RECREATE");
594 // Save stuff at the beginning of the file to avoid file corruption
595 fRunLoader->CdGAFile();
597 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
600 //_______________________________________________________________________
602 void AliRun::RunMC(Int_t nevent, const char *setup)
605 // Main function to be called to process a galice run
607 // Root > gAlice.Run();
608 // a positive number of events will cause the finish routine
611 fEventsPerRun = nevent;
612 // check if initialisation has been done
613 if (!fInitDone) InitMC(setup);
615 // Create the Root Tree with one branch per detector
616 //Hits moved to begin event -> now we are crating separate tree for each event
618 gMC->ProcessRun(nevent);
620 // End of this run, close files
621 if(nevent>0) FinishRun();
624 //_______________________________________________________________________
625 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
628 // Main function to be called to reconstruct Alice event
630 Int_t nev = fRunLoader->GetNumberOfEvents();
631 AliDebug(1, Form("Found %d events", nev));
632 Int_t nFirst = first;
633 Int_t nLast = (last < 0)? nev : last;
635 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
636 AliDebug(1, Form("Processing event %d", nevent));
638 Digits2Reco(selected);
642 //_______________________________________________________________________
644 void AliRun::Hits2Digits(const char *selected)
647 // Convert Hits to sumable digits
649 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
651 Hits2SDigits(selected);
652 SDigits2Digits(selected);
657 //_______________________________________________________________________
659 void AliRun::Tree2Tree(Option_t *option, const char *selected)
662 // Function to transform the content of
664 // - TreeH to TreeS (option "S")
665 // - TreeS to TreeD (option "D")
666 // - TreeD to TreeR (option "R")
668 // If multiple options are specified ("SDR"), transformation will be done in sequence for
669 // selected detector and for all detectors if none is selected (detector string
670 // can contain blank separated list of detector names).
673 const char *oS = strstr(option,"S");
674 const char *oD = strstr(option,"D");
675 const char *oR = strstr(option,"R");
677 TObjArray *detectors = Detectors();
679 TIter next(detectors);
681 AliDetector *detector = 0;
683 while((detector = dynamic_cast<AliDetector*>(next()))) {
685 if (strcmp(detector->GetName(),selected)) continue;
686 if (detector->IsActive())
689 AliLoader* loader = detector->GetLoader();
690 if (loader == 0x0) continue;
694 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
695 loader->LoadHits("read");
696 if (loader->TreeS() == 0x0) loader->MakeTree("S");
697 detector->MakeBranch(option);
698 detector->SetTreeAddress();
699 detector->Hits2SDigits();
700 loader->UnloadHits();
701 loader->UnloadSDigits();
705 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
706 loader->LoadSDigits("read");
707 if (loader->TreeD() == 0x0) loader->MakeTree("D");
708 detector->MakeBranch(option);
709 detector->SetTreeAddress();
710 detector->SDigits2Digits();
711 loader->UnloadSDigits();
712 loader->UnloadDigits();
716 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
717 loader->LoadDigits("read");
718 if (loader->TreeR() == 0x0) loader->MakeTree("R");
719 detector->MakeBranch(option);
720 detector->SetTreeAddress();
721 detector->Digits2Reco();
722 loader->UnloadDigits();
723 loader->UnloadRecPoints();
730 //_______________________________________________________________________
731 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
732 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
733 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
736 // Generates lego plots of:
737 // - radiation length map phi vs theta
738 // - radiation length map phi vs eta
739 // - interaction length map
740 // - g/cm2 length map
742 // ntheta bins in theta, eta
743 // themin minimum angle in theta (degrees)
744 // themax maximum angle in theta (degrees)
746 // phimin minimum angle in phi (degrees)
747 // phimax maximum angle in phi (degrees)
748 // rmin minimum radius
749 // rmax maximum radius
752 // The number of events generated = ntheta*nphi
753 // run input parameters in macro setup (default="Config.C")
755 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
758 <img src="picts/AliRunLego1.gif">
763 <img src="picts/AliRunLego2.gif">
768 <img src="picts/AliRunLego3.gif">
773 // check if initialisation has been done
774 // If runloader has been initialized, set the number of events per file to nc1 * nc2
777 if (!gener) gener = new AliLegoGenerator();
779 // Configure Generator
780 gener->SetRadiusRange(rmin, rmax);
781 gener->SetZMax(zmax);
782 gener->SetCoor1Range(nc1, c1min, c1max);
783 gener->SetCoor2Range(nc2, c2min, c2max);
787 fLego = new AliLego("lego",gener);
789 if (!fInitDone) InitMC(setup);
790 //Save current generator
792 AliGenerator *gen=fMCApp->Generator();
793 fMCApp->ResetGenerator(gener);
794 //Prepare MC for Lego Run
799 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
800 //gMC->ProcessRun(nc1*nc2+1);
801 gMC->ProcessRun(nc1*nc2);
803 // End of this run, close files
805 // Restore current generator
806 fMCApp->ResetGenerator(gen);
807 // Delete Lego Object
808 delete fLego; fLego=0;
811 //_______________________________________________________________________
812 void AliRun::SetConfigFunction(const char * config)
815 // Set the signature of the function contained in Config.C to configure
818 fConfigFunction=config;
825 //_______________________________________________________________________
826 void AliRun::Field(const Double_t* x, Double_t *b) const
829 // Return the value of the magnetic field
832 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
836 Field()->Field(xfloat,bfloat);
837 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
840 AliError("No mag field defined!");
846 // End of MC Application
849 //_______________________________________________________________________
850 void AliRun::Streamer(TBuffer &R__b)
852 // Stream an object of class AliRun.
854 if (R__b.IsReading()) {
855 if (!gAlice) gAlice = this;
856 AliRun::Class()->ReadBuffer(R__b, this);
857 gROOT->GetListOfBrowsables()->Add(this,"Run");
861 AliRun::Class()->WriteBuffer(R__b, this);
864 //_______________________________________________________________________
866 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
868 fRunLoader->GetHeader()->SetGenEventHeader(header);
870 //_______________________________________________________________________
872 Int_t AliRun::GetEvNumber() const
874 //Returns number of current event
875 if (fRunLoader == 0x0)
877 AliError("RunLoader is not set. Can not load data.");
881 return fRunLoader->GetEventNumber();
883 //_______________________________________________________________________
885 void AliRun::SetRunLoader(AliRunLoader* rloader)
888 // Set the loader of the run
890 fRunLoader = rloader;
891 if (fRunLoader == 0x0) return;
894 TFolder* evfold = fRunLoader->GetEventFolder();
895 if (evfold) evfoldname = evfold->GetName();
896 else AliWarning("Did not get Event Folder from Run Loader");
898 if ( fRunLoader->GetAliRun() )
899 {//if alrun already exists in folder
900 if (fRunLoader->GetAliRun() != this )
901 {//and is different than this - crash
902 AliFatal("AliRun is already in Folder and it is not this object");
908 evfold->Add(this);//Post this AliRun to Folder
911 TIter next(fModules);
913 while((module = (AliModule*)next()))
915 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
916 module->SetRunLoader(fRunLoader);
917 AliDetector* detector = dynamic_cast<AliDetector*>(module);
920 AliLoader* loader = fRunLoader->GetLoader(detector);
923 AliError(Form("Can not get loader for detector %s", detector->GetName()));
927 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
928 detector->SetLoader(loader);
934 void AliRun::AddModule(AliModule* mod)
937 // Add a module to the module list
939 if (mod == 0x0) return;
940 if (strlen(mod->GetName()) == 0) return;
941 if (GetModuleID(mod->GetName()) >= 0) return;
943 AliDebug(1, mod->GetName());
944 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
945 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
953 //_______________________________________________________________________
954 Int_t AliRun::GetDebug() const
956 AliWarning("Don't use this method any more, use AliDebug instead");
957 return AliDebugLevel();
960 //_______________________________________________________________________
961 void AliRun::SetDebug(Int_t level)
963 AliWarning("Don't use this method any more, use AliLog instead");
964 AliLog::SetClassDebugLevel("AliRun", level);