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"
68 //#include "AliTPCTrackHitsInterfaces.h"
75 //_______________________________________________________________________
90 fPDGDB(0), //Particle factory object
91 fConfigFunction("\0"),
93 fIsRootGeometry(kFALSE),
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):
119 fPDGDB(0), //Particle factory object
120 fConfigFunction("\0"),
122 fIsRootGeometry(kFALSE),
126 // Copy constructor for AliRun
131 //_____________________________________________________________________________
132 AliRun::AliRun(const char *name, const char *title):
138 fModules(new TObjArray(77)), // Support list for the Detectors
147 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
148 fConfigFunction("Config();"),
149 fRandom(new TRandom3()),
150 fIsRootGeometry(kFALSE),
154 // Constructor for the main processor.
155 // Creates the geometry
156 // Creates the list of Detectors.
157 // Creates the list of particles.
162 // Set random number generator
165 if (gSystem->Getenv("CONFIG_SEED")) {
166 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
169 // Add to list of browsable
170 gROOT->GetListOfBrowsables()->Add(this,name);
171 // Create the TNode geometry for the event display
172 BuildSimpleGeometry();
174 // Create default mag field
177 // Add particle list to configuration
178 AliConfig::Instance()->Add(fPDGDB);
181 // Terrible hack to avoid problem with the initialisation of
182 // static and globals on Mac OS X
183 // AliClassAliTrackHitsInfo p1=galiclass____AliClassAliTrackHitsInfo;
184 // AliClassAliTrackHitsParam p2=galiclass____AliTrackHitsParam;
185 //AliClassAliHitInfo p3=galiclass____AliHitInfo;
191 //_______________________________________________________________________
195 // Default AliRun destructor
197 gROOT->GetListOfBrowsables()->Remove(this);
201 TFolder* evfold = fRunLoader->GetEventFolder();
202 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
203 TIter next(fModules);
205 while((mod = (AliModule*)next()))
207 modfold->Remove(mod);
226 //_______________________________________________________________________
227 void AliRun::Copy(TObject &) const
229 AliFatal("Not implemented!");
232 //_______________________________________________________________________
236 // Initialize Alice geometry
241 //_______________________________________________________________________
242 void AliRun::BuildSimpleGeometry()
245 // Create a simple TNode geometry used by Root display engine
247 // Initialise geometry
249 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
250 new TMaterial("void","Vacuum",0,0,0); //Everything is void
251 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
252 brik->SetVisibility(0);
253 new TNode("alice","alice","S_alice");
256 //_______________________________________________________________________
257 void AliRun::CleanDetectors()
260 // Clean Detectors at the end of event
262 fRunLoader->CleanDetectors();
265 //_______________________________________________________________________
266 void AliRun::ResetHits()
271 //_______________________________________________________________________
272 AliGenerator* AliRun::Generator() const
274 return fMCApp->Generator();
277 //_______________________________________________________________________
278 void AliRun::SetField(AliMagF* magField)
281 // Set Magnetic Field Map
287 //_______________________________________________________________________
288 void AliRun::SetRootGeometry(Bool_t flag)
290 // Instruct application that the geometry is to be retreived from a root file.
291 fIsRootGeometry = flag;
292 if (flag) gMC->SetRootGeometry();
294 //_______________________________________________________________________
295 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
296 Float_t maxField, const char* filename)
299 // Set magnetic field parameters
300 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
301 // version Magnetic field map version (only 1 active now)
302 // scale Scale factor for the magnetic field
303 // maxField Maximum value for the magnetic field
306 // --- Sanity check on mag field flags
307 if(fField) delete fField;
309 fField = new AliMagFC("Map1"," ",type,scale,maxField);
310 } else if(version<=2) {
311 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
313 } else if(version==3) {
314 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
317 AliWarning(Form("Invalid map %d",version));
321 //_____________________________________________________________________________
323 void AliRun::InitLoaders()
325 //creates list of getters
327 TIter next(fModules);
329 while((mod = (AliModule*)next()))
331 mod->SetRunLoader(fRunLoader);
332 AliDetector *det = dynamic_cast<AliDetector*>(mod);
335 AliDebug(2, Form("Adding %s", det->GetName()));
336 fRunLoader->AddLoader(det);
341 //_____________________________________________________________________________
343 void AliRun::FinishRun()
346 // Called at the end of the run.
351 AliDebug(1, "Finish Lego");
352 fRunLoader->CdGAFile();
356 // Clean detector information
357 TIter next(fModules);
359 while((detector = dynamic_cast<AliModule*>(next()))) {
360 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
361 detector->FinishRun();
364 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
365 fRunLoader->WriteHeader("OVERWRITE");
367 // Write AliRun info and all detectors parameters
368 fRunLoader->CdGAFile();
369 Write(0,TObject::kOverwrite);//write AliRun
370 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
372 // Clean tree information
373 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
374 fRunLoader->Stack()->FinishRun();
376 if(fMCApp) fMCApp->FinishRun();
378 fRunLoader->Synchronize();
381 //_______________________________________________________________________
382 void AliRun::Announce() const
385 // Announce the current version of AliRoot
388 "****************************************************************\n");
389 printf("%6s","*");printf("%64s","*\n");
392 printf(" You are running AliRoot version NewIO\n");
395 printf(" The cvs tag for the current program is $Name$\n");
397 printf("%6s","*");printf("%64s","*\n");
399 "****************************************************************\n");
402 //_______________________________________________________________________
403 AliModule *AliRun::GetModule(const char *name) const
406 // Return pointer to detector from name
408 return dynamic_cast<AliModule*>(fModules->FindObject(name));
411 //_______________________________________________________________________
412 AliDetector *AliRun::GetDetector(const char *name) const
415 // Return pointer to detector from name
417 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
420 //_______________________________________________________________________
421 Int_t AliRun::GetModuleID(const char *name) const
424 // Return galice internal detector identifier from name
427 TObject *mod=fModules->FindObject(name);
428 if(mod) i=fModules->IndexOf(mod);
432 //_______________________________________________________________________
433 Int_t AliRun::GetEvent(Int_t event)
436 // Reloads data containers in folders # event
437 // Set branch addresses
439 if (fRunLoader == 0x0)
441 AliError("RunLoader is not set. Can not load data.");
444 /*****************************************/
445 /**** P R E R E L O A D I N G ****/
446 /*****************************************/
447 // Reset existing structures
449 fMCApp->ResetTrackReferences();
453 /*****************************************/
454 /**** R E L O A D ****/
455 /*****************************************/
457 fRunLoader->GetEvent(event);
459 /*****************************************/
460 /**** P O S T R E L O A D I N G ****/
461 /*****************************************/
463 // Set Trees branch addresses
464 TIter next(fModules);
466 while((detector = dynamic_cast<AliModule*>(next())))
468 detector->SetTreeAddress();
471 return fRunLoader->GetHeader()->GetNtrack();
474 //_______________________________________________________________________
475 TGeometry *AliRun::GetGeometry()
478 // Import Alice geometry from current file
479 // Return pointer to geometry object
481 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
483 // Unlink and relink nodes in detectors
484 // This is bad and there must be a better way...
487 TIter next(fModules);
489 while((detector = dynamic_cast<AliModule*>(next()))) {
490 TList *dnodes=detector->Nodes();
493 for ( j=0; j<dnodes->GetSize(); j++) {
494 node = dynamic_cast<TNode*>(dnodes->At(j));
495 node1 = fGeometry->GetNode(node->GetName());
496 dnodes->Remove(node);
497 dnodes->AddAt(node1,j);
503 //_______________________________________________________________________
504 void AliRun::SetBaseFile(const char *filename)
506 fBaseFileName = filename;
509 //_______________________________________________________________________
510 void AliRun::ResetDigits()
513 // Reset all Detectors digits
515 TIter next(fModules);
517 while((detector = dynamic_cast<AliModule*>(next()))) {
518 detector->ResetDigits();
522 //_______________________________________________________________________
523 void AliRun::ResetSDigits()
526 // Reset all Detectors digits
528 TIter next(fModules);
530 while((detector = dynamic_cast<AliModule*>(next()))) {
531 detector->ResetSDigits();
536 //_______________________________________________________________________
538 void AliRun::ResetPoints()
541 // Reset all Detectors points
543 TIter next(fModules);
545 while((detector = dynamic_cast<AliModule*>(next()))) {
546 detector->ResetPoints();
549 //_______________________________________________________________________
551 void AliRun::InitMC(const char *setup)
554 // Initialize ALICE Simulation run
559 AliWarning("Cannot initialise AliRun twice!");
564 fMCApp=new AliMC(GetName(),GetTitle());
566 gROOT->LoadMacro(setup);
567 gInterpreter->ProcessLine(fConfigFunction.Data());
569 fRunLoader->CdGAFile();
571 AliPDG::AddParticlesToPdgDataBase();
573 fNdets = fModules->GetLast()+1;
575 TIter next(fModules);
576 for(Int_t i=0; i<fNdets; ++i)
578 TObject *objfirst, *objlast;
579 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
580 objlast = gDirectory->GetList()->Last();
582 // Add Detector histograms in Detector list of histograms
583 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
584 else objfirst = gDirectory->GetList()->First();
587 detector->Histograms()->Add(objfirst);
588 objfirst = gDirectory->GetList()->After(objfirst);
594 //Must be here because some MCs (G4) adds detectors here and not in Config.C
596 fRunLoader->MakeTree("E");
599 fRunLoader->LoadKinematics("RECREATE");
600 fRunLoader->LoadTrackRefs("RECREATE");
601 fRunLoader->LoadHits("all","RECREATE");
605 // Save stuff at the beginning of the file to avoid file corruption
606 fRunLoader->CdGAFile();
608 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
611 //_______________________________________________________________________
613 void AliRun::RunMC(Int_t nevent, const char *setup)
616 // Main function to be called to process a galice run
618 // Root > gAlice.Run();
619 // a positive number of events will cause the finish routine
622 fEventsPerRun = nevent;
623 // check if initialisation has been done
624 if (!fInitDone) InitMC(setup);
626 // Create the Root Tree with one branch per detector
627 //Hits moved to begin event -> now we are crating separate tree for each event
629 gMC->ProcessRun(nevent);
631 // End of this run, close files
632 if(nevent>0) FinishRun();
635 //_______________________________________________________________________
636 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
639 // Main function to be called to reconstruct Alice event
641 Int_t nev = fRunLoader->GetNumberOfEvents();
642 AliDebug(1, Form("Found %d events", nev));
643 Int_t nFirst = first;
644 Int_t nLast = (last < 0)? nev : last;
646 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
647 AliDebug(1, Form("Processing event %d", nevent));
649 Digits2Reco(selected);
653 //_______________________________________________________________________
655 void AliRun::Hits2Digits(const char *selected)
658 // Convert Hits to sumable digits
660 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
662 Hits2SDigits(selected);
663 SDigits2Digits(selected);
668 //_______________________________________________________________________
670 void AliRun::Tree2Tree(Option_t *option, const char *selected)
673 // Function to transform the content of
675 // - TreeH to TreeS (option "S")
676 // - TreeS to TreeD (option "D")
677 // - TreeD to TreeR (option "R")
679 // If multiple options are specified ("SDR"), transformation will be done in sequence for
680 // selected detector and for all detectors if none is selected (detector string
681 // can contain blank separated list of detector names).
684 const char *oS = strstr(option,"S");
685 const char *oD = strstr(option,"D");
686 const char *oR = strstr(option,"R");
688 TObjArray *detectors = Detectors();
690 TIter next(detectors);
692 AliDetector *detector = 0;
694 while((detector = dynamic_cast<AliDetector*>(next()))) {
696 if (strcmp(detector->GetName(),selected)) continue;
697 if (detector->IsActive())
700 AliLoader* loader = detector->GetLoader();
701 if (loader == 0x0) continue;
705 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
706 loader->LoadHits("read");
707 if (loader->TreeS() == 0x0) loader->MakeTree("S");
708 detector->MakeBranch(option);
709 detector->SetTreeAddress();
710 detector->Hits2SDigits();
711 loader->UnloadHits();
712 loader->UnloadSDigits();
716 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
717 loader->LoadSDigits("read");
718 if (loader->TreeD() == 0x0) loader->MakeTree("D");
719 detector->MakeBranch(option);
720 detector->SetTreeAddress();
721 detector->SDigits2Digits();
722 loader->UnloadSDigits();
723 loader->UnloadDigits();
727 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
728 loader->LoadDigits("read");
729 if (loader->TreeR() == 0x0) loader->MakeTree("R");
730 detector->MakeBranch(option);
731 detector->SetTreeAddress();
732 detector->Digits2Reco();
733 loader->UnloadDigits();
734 loader->UnloadRecPoints();
741 //_______________________________________________________________________
742 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
743 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
744 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
747 // Generates lego plots of:
748 // - radiation length map phi vs theta
749 // - radiation length map phi vs eta
750 // - interaction length map
751 // - g/cm2 length map
753 // ntheta bins in theta, eta
754 // themin minimum angle in theta (degrees)
755 // themax maximum angle in theta (degrees)
757 // phimin minimum angle in phi (degrees)
758 // phimax maximum angle in phi (degrees)
759 // rmin minimum radius
760 // rmax maximum radius
763 // The number of events generated = ntheta*nphi
764 // run input parameters in macro setup (default="Config.C")
766 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
769 <img src="picts/AliRunLego1.gif">
774 <img src="picts/AliRunLego2.gif">
779 <img src="picts/AliRunLego3.gif">
784 // check if initialisation has been done
785 // If runloader has been initialized, set the number of events per file to nc1 * nc2
788 if (!gener) gener = new AliLegoGenerator();
790 // Configure Generator
791 gener->SetRadiusRange(rmin, rmax);
792 gener->SetZMax(zmax);
793 gener->SetCoor1Range(nc1, c1min, c1max);
794 gener->SetCoor2Range(nc2, c2min, c2max);
798 fLego = new AliLego("lego",gener);
800 if (!fInitDone) InitMC(setup);
801 //Save current generator
803 AliGenerator *gen=fMCApp->Generator();
804 fMCApp->ResetGenerator(gener);
805 //Prepare MC for Lego Run
810 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
811 //gMC->ProcessRun(nc1*nc2+1);
812 gMC->ProcessRun(nc1*nc2);
814 // End of this run, close files
816 // Restore current generator
817 fMCApp->ResetGenerator(gen);
818 // Delete Lego Object
819 delete fLego; fLego=0;
822 //_______________________________________________________________________
823 void AliRun::SetConfigFunction(const char * config)
826 // Set the signature of the function contained in Config.C to configure
829 fConfigFunction=config;
836 //_______________________________________________________________________
837 void AliRun::Field(const Double_t* x, Double_t *b) const
840 // Return the value of the magnetic field
843 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
847 Field()->Field(xfloat,bfloat);
848 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
851 AliError("No mag field defined!");
857 // End of MC Application
860 //_______________________________________________________________________
861 void AliRun::Streamer(TBuffer &R__b)
863 // Stream an object of class AliRun.
865 if (R__b.IsReading()) {
866 if (!gAlice) gAlice = this;
867 AliRun::Class()->ReadBuffer(R__b, this);
868 gROOT->GetListOfBrowsables()->Add(this,"Run");
872 AliRun::Class()->WriteBuffer(R__b, this);
875 //_______________________________________________________________________
877 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
879 fRunLoader->GetHeader()->SetGenEventHeader(header);
881 //_______________________________________________________________________
883 Int_t AliRun::GetEvNumber() const
885 //Returns number of current event
886 if (fRunLoader == 0x0)
888 AliError("RunLoader is not set. Can not load data.");
892 return fRunLoader->GetEventNumber();
894 //_______________________________________________________________________
896 void AliRun::SetRunLoader(AliRunLoader* rloader)
899 // Set the loader of the run
901 fRunLoader = rloader;
902 if (fRunLoader == 0x0) return;
905 TFolder* evfold = fRunLoader->GetEventFolder();
906 if (evfold) evfoldname = evfold->GetName();
907 else AliWarning("Did not get Event Folder from Run Loader");
909 if ( fRunLoader->GetAliRun() )
910 {//if alrun already exists in folder
911 if (fRunLoader->GetAliRun() != this )
912 {//and is different than this - crash
913 AliFatal("AliRun is already in Folder and it is not this object");
919 evfold->Add(this);//Post this AliRun to Folder
922 TIter next(fModules);
924 while((module = (AliModule*)next()))
926 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
927 module->SetRunLoader(fRunLoader);
928 AliDetector* detector = dynamic_cast<AliDetector*>(module);
931 AliLoader* loader = fRunLoader->GetLoader(detector);
934 AliError(Form("Can not get loader for detector %s", detector->GetName()));
938 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
939 detector->SetLoader(loader);
945 void AliRun::AddModule(AliModule* mod)
948 // Add a module to the module list
950 if (mod == 0x0) return;
951 if (strlen(mod->GetName()) == 0) return;
952 if (GetModuleID(mod->GetName()) >= 0) return;
954 AliDebug(1, mod->GetName());
955 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
956 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
963 // added by Alberto Colla
964 //_____________________________________________________________________________
965 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
966 { return !gSystem->AccessPathName(fnam,mode);}
968 //______________________________________________________
969 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
971 TString str = name; gSystem->ExpandPathName(str);
972 return !gSystem->AccessPathName(str.Data(),mode);