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"
66 #include "AliAlignObj.h"
72 //_______________________________________________________________________
86 fPDGDB(0), //Particle factory object
87 fConfigFunction("\0"),
89 fIsRootGeometry(kFALSE),
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):
114 fPDGDB(0), //Particle factory object
115 fConfigFunction("\0"),
117 fIsRootGeometry(kFALSE),
121 // Copy constructor for AliRun
126 //_____________________________________________________________________________
127 AliRun::AliRun(const char *name, const char *title):
133 fModules(new TObjArray(77)), // Support list for the Detectors
141 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
142 fConfigFunction("Config();"),
143 fRandom(new TRandom3()),
144 fIsRootGeometry(kFALSE),
148 // Constructor for the main processor.
149 // Creates the geometry
150 // Creates the list of Detectors.
151 // Creates the list of particles.
156 // Set random number generator
159 if (gSystem->Getenv("CONFIG_SEED")) {
160 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
163 // Add to list of browsable
164 gROOT->GetListOfBrowsables()->Add(this,name);
165 // Create the TNode geometry for the event display
166 BuildSimpleGeometry();
168 // Create default mag field
171 // Add particle list to configuration
172 AliConfig::Instance()->Add(fPDGDB);
177 //_______________________________________________________________________
181 // Default AliRun destructor
183 gROOT->GetListOfBrowsables()->Remove(this);
187 TFolder* evfold = fRunLoader->GetEventFolder();
188 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
189 TIter next(fModules);
191 while((mod = (AliModule*)next()))
193 modfold->Remove(mod);
212 //_______________________________________________________________________
213 void AliRun::Copy(TObject &) const
215 AliFatal("Not implemented!");
218 //_______________________________________________________________________
222 // Initialize Alice geometry
227 //_______________________________________________________________________
228 void AliRun::BuildSimpleGeometry()
231 // Create a simple TNode geometry used by Root display engine
233 // Initialise geometry
235 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
236 new TMaterial("void","Vacuum",0,0,0); //Everything is void
237 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
238 brik->SetVisibility(0);
239 new TNode("alice","alice","S_alice");
242 //_______________________________________________________________________
243 void AliRun::CleanDetectors()
246 // Clean Detectors at the end of event
248 fRunLoader->CleanDetectors();
251 //_______________________________________________________________________
252 void AliRun::ResetHits()
257 //_______________________________________________________________________
258 AliGenerator* AliRun::Generator() const
260 return fMCApp->Generator();
263 //_______________________________________________________________________
264 void AliRun::SetField(AliMagF* magField)
267 // Set Magnetic Field Map
273 //_______________________________________________________________________
274 void AliRun::SetRootGeometry(Bool_t flag)
276 // Instruct application that the geometry is to be retreived from a root file.
277 fIsRootGeometry = flag;
278 if (flag) gMC->SetRootGeometry();
280 //_______________________________________________________________________
281 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
282 Float_t maxField, const char* filename)
285 // Set magnetic field parameters
286 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
287 // version Magnetic field map version (only 1 active now)
288 // scale Scale factor for the magnetic field
289 // maxField Maximum value for the magnetic field
292 // --- Sanity check on mag field flags
293 if(fField) delete fField;
295 fField = new AliMagFC("Map1"," ",type,scale,maxField);
296 } else if(version<=2) {
297 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
299 } else if(version==3) {
300 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
303 AliWarning(Form("Invalid map %d",version));
307 //_____________________________________________________________________________
309 void AliRun::InitLoaders()
311 //creates list of getters
313 TIter next(fModules);
315 while((mod = (AliModule*)next()))
317 mod->SetRunLoader(fRunLoader);
318 AliDetector *det = dynamic_cast<AliDetector*>(mod);
321 AliDebug(2, Form("Adding %s", det->GetName()));
322 fRunLoader->AddLoader(det);
327 //_____________________________________________________________________________
329 void AliRun::FinishRun()
332 // Called at the end of the run.
337 AliDebug(1, "Finish Lego");
338 fRunLoader->CdGAFile();
342 // Clean detector information
343 TIter next(fModules);
345 while((detector = dynamic_cast<AliModule*>(next()))) {
346 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
347 detector->FinishRun();
350 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
351 fRunLoader->WriteHeader("OVERWRITE");
353 // Write AliRun info and all detectors parameters
354 fRunLoader->CdGAFile();
355 Write(0,TObject::kOverwrite);//write AliRun
356 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
358 // Clean tree information
359 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
360 fRunLoader->Stack()->FinishRun();
362 if(fMCApp) fMCApp->FinishRun();
364 fRunLoader->Synchronize();
367 //_______________________________________________________________________
368 void AliRun::Announce() const
371 // Announce the current version of AliRoot
374 "****************************************************************\n");
375 printf("%6s","*");printf("%64s","*\n");
378 printf(" You are running AliRoot version NewIO\n");
381 printf(" The cvs tag for the current program is $Name$\n");
383 printf("%6s","*");printf("%64s","*\n");
385 "****************************************************************\n");
388 //_______________________________________________________________________
389 AliModule *AliRun::GetModule(const char *name) const
392 // Return pointer to detector from name
394 return dynamic_cast<AliModule*>(fModules->FindObject(name));
397 //_______________________________________________________________________
398 AliDetector *AliRun::GetDetector(const char *name) const
401 // Return pointer to detector from name
403 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
406 //_______________________________________________________________________
407 Int_t AliRun::GetModuleID(const char *name) const
410 // Return galice internal detector identifier from name
413 TObject *mod=fModules->FindObject(name);
414 if(mod) i=fModules->IndexOf(mod);
418 //_______________________________________________________________________
419 Int_t AliRun::GetEvent(Int_t event)
422 // Reloads data containers in folders # event
423 // Set branch addresses
425 if (fRunLoader == 0x0)
427 AliError("RunLoader is not set. Can not load data.");
430 /*****************************************/
431 /**** P R E R E L O A D I N G ****/
432 /*****************************************/
433 // Reset existing structures
435 fMCApp->ResetTrackReferences();
439 /*****************************************/
440 /**** R E L O A D ****/
441 /*****************************************/
443 fRunLoader->GetEvent(event);
445 /*****************************************/
446 /**** P O S T R E L O A D I N G ****/
447 /*****************************************/
449 // Set Trees branch addresses
450 TIter next(fModules);
452 while((detector = dynamic_cast<AliModule*>(next())))
454 detector->SetTreeAddress();
457 return fRunLoader->GetHeader()->GetNtrack();
460 //_______________________________________________________________________
461 TGeometry *AliRun::GetGeometry()
464 // Import Alice geometry from current file
465 // Return pointer to geometry object
467 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
469 // Unlink and relink nodes in detectors
470 // This is bad and there must be a better way...
473 TIter next(fModules);
475 while((detector = dynamic_cast<AliModule*>(next()))) {
476 TList *dnodes=detector->Nodes();
479 for ( j=0; j<dnodes->GetSize(); j++) {
480 node = dynamic_cast<TNode*>(dnodes->At(j));
481 node1 = fGeometry->GetNode(node->GetName());
482 dnodes->Remove(node);
483 dnodes->AddAt(node1,j);
489 //_______________________________________________________________________
490 void AliRun::SetBaseFile(const char *filename)
492 fBaseFileName = filename;
495 //_______________________________________________________________________
496 void AliRun::ResetDigits()
499 // Reset all Detectors digits
501 TIter next(fModules);
503 while((detector = dynamic_cast<AliModule*>(next()))) {
504 detector->ResetDigits();
508 //_______________________________________________________________________
509 void AliRun::ResetSDigits()
512 // Reset all Detectors digits
514 TIter next(fModules);
516 while((detector = dynamic_cast<AliModule*>(next()))) {
517 detector->ResetSDigits();
522 //_______________________________________________________________________
524 void AliRun::ResetPoints()
527 // Reset all Detectors points
529 TIter next(fModules);
531 while((detector = dynamic_cast<AliModule*>(next()))) {
532 detector->ResetPoints();
535 //_______________________________________________________________________
537 void AliRun::InitMC(const char *setup)
540 // Initialize ALICE Simulation run
545 AliWarning("Cannot initialise AliRun twice!");
550 fMCApp=new AliMC(GetName(),GetTitle());
552 gROOT->LoadMacro(setup);
553 gInterpreter->ProcessLine(fConfigFunction.Data());
555 fRunLoader->CdGAFile();
557 AliPDG::AddParticlesToPdgDataBase();
559 fNdets = fModules->GetLast()+1;
561 TIter next(fModules);
562 for(Int_t i=0; i<fNdets; ++i)
564 TObject *objfirst, *objlast;
565 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
566 objlast = gDirectory->GetList()->Last();
568 // Add Detector histograms in Detector list of histograms
569 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
570 else objfirst = gDirectory->GetList()->First();
573 detector->Histograms()->Add(objfirst);
574 objfirst = gDirectory->GetList()->After(objfirst);
580 //Must be here because some MCs (G4) adds detectors here and not in Config.C
582 fRunLoader->MakeTree("E");
585 fRunLoader->LoadKinematics("RECREATE");
586 fRunLoader->LoadTrackRefs("RECREATE");
587 fRunLoader->LoadHits("all","RECREATE");
591 // Save stuff at the beginning of the file to avoid file corruption
592 fRunLoader->CdGAFile();
594 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
597 //_______________________________________________________________________
599 void AliRun::RunMC(Int_t nevent, const char *setup)
602 // Main function to be called to process a galice run
604 // Root > gAlice.Run();
605 // a positive number of events will cause the finish routine
608 fEventsPerRun = nevent;
609 // check if initialisation has been done
610 if (!fInitDone) InitMC(setup);
612 // Create the Root Tree with one branch per detector
613 //Hits moved to begin event -> now we are crating separate tree for each event
615 gMC->ProcessRun(nevent);
617 // End of this run, close files
618 if(nevent>0) FinishRun();
621 //_______________________________________________________________________
622 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
625 // Main function to be called to reconstruct Alice event
627 Int_t nev = fRunLoader->GetNumberOfEvents();
628 AliDebug(1, Form("Found %d events", nev));
629 Int_t nFirst = first;
630 Int_t nLast = (last < 0)? nev : last;
632 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
633 AliDebug(1, Form("Processing event %d", nevent));
635 Digits2Reco(selected);
639 //_______________________________________________________________________
641 void AliRun::Hits2Digits(const char *selected)
644 // Convert Hits to sumable digits
646 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
648 Hits2SDigits(selected);
649 SDigits2Digits(selected);
654 //_______________________________________________________________________
656 void AliRun::Tree2Tree(Option_t *option, const char *selected)
659 // Function to transform the content of
661 // - TreeH to TreeS (option "S")
662 // - TreeS to TreeD (option "D")
663 // - TreeD to TreeR (option "R")
665 // If multiple options are specified ("SDR"), transformation will be done in sequence for
666 // selected detector and for all detectors if none is selected (detector string
667 // can contain blank separated list of detector names).
670 const char *oS = strstr(option,"S");
671 const char *oD = strstr(option,"D");
672 const char *oR = strstr(option,"R");
674 TObjArray *detectors = Detectors();
676 TIter next(detectors);
678 AliDetector *detector = 0;
680 while((detector = dynamic_cast<AliDetector*>(next()))) {
682 if (strcmp(detector->GetName(),selected)) continue;
683 if (detector->IsActive())
686 AliLoader* loader = detector->GetLoader();
687 if (loader == 0x0) continue;
691 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
692 loader->LoadHits("read");
693 if (loader->TreeS() == 0x0) loader->MakeTree("S");
694 detector->MakeBranch(option);
695 detector->SetTreeAddress();
696 detector->Hits2SDigits();
697 loader->UnloadHits();
698 loader->UnloadSDigits();
702 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
703 loader->LoadSDigits("read");
704 if (loader->TreeD() == 0x0) loader->MakeTree("D");
705 detector->MakeBranch(option);
706 detector->SetTreeAddress();
707 detector->SDigits2Digits();
708 loader->UnloadSDigits();
709 loader->UnloadDigits();
713 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
714 loader->LoadDigits("read");
715 if (loader->TreeR() == 0x0) loader->MakeTree("R");
716 detector->MakeBranch(option);
717 detector->SetTreeAddress();
718 detector->Digits2Reco();
719 loader->UnloadDigits();
720 loader->UnloadRecPoints();
727 //_______________________________________________________________________
728 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
729 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
730 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
733 // Generates lego plots of:
734 // - radiation length map phi vs theta
735 // - radiation length map phi vs eta
736 // - interaction length map
737 // - g/cm2 length map
739 // ntheta bins in theta, eta
740 // themin minimum angle in theta (degrees)
741 // themax maximum angle in theta (degrees)
743 // phimin minimum angle in phi (degrees)
744 // phimax maximum angle in phi (degrees)
745 // rmin minimum radius
746 // rmax maximum radius
749 // The number of events generated = ntheta*nphi
750 // run input parameters in macro setup (default="Config.C")
752 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
755 <img src="picts/AliRunLego1.gif">
760 <img src="picts/AliRunLego2.gif">
765 <img src="picts/AliRunLego3.gif">
770 // check if initialisation has been done
771 // If runloader has been initialized, set the number of events per file to nc1 * nc2
774 if (!gener) gener = new AliLegoGenerator();
776 // Configure Generator
777 gener->SetRadiusRange(rmin, rmax);
778 gener->SetZMax(zmax);
779 gener->SetCoor1Range(nc1, c1min, c1max);
780 gener->SetCoor2Range(nc2, c2min, c2max);
784 fLego = new AliLego("lego",gener);
786 if (!fInitDone) InitMC(setup);
787 //Save current generator
789 AliGenerator *gen=fMCApp->Generator();
790 fMCApp->ResetGenerator(gener);
791 //Prepare MC for Lego Run
796 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
797 //gMC->ProcessRun(nc1*nc2+1);
798 gMC->ProcessRun(nc1*nc2);
800 // End of this run, close files
802 // Restore current generator
803 fMCApp->ResetGenerator(gen);
804 // Delete Lego Object
805 delete fLego; fLego=0;
808 //_______________________________________________________________________
809 void AliRun::SetConfigFunction(const char * config)
812 // Set the signature of the function contained in Config.C to configure
815 fConfigFunction=config;
822 //_______________________________________________________________________
823 void AliRun::Field(const Double_t* x, Double_t *b) const
826 // Return the value of the magnetic field
829 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
833 Field()->Field(xfloat,bfloat);
834 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
837 AliError("No mag field defined!");
843 // End of MC Application
846 //_______________________________________________________________________
847 void AliRun::Streamer(TBuffer &R__b)
849 // Stream an object of class AliRun.
851 if (R__b.IsReading()) {
852 if (!gAlice) gAlice = this;
853 AliRun::Class()->ReadBuffer(R__b, this);
854 gROOT->GetListOfBrowsables()->Add(this,"Run");
858 AliRun::Class()->WriteBuffer(R__b, this);
861 //_______________________________________________________________________
863 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
865 fRunLoader->GetHeader()->SetGenEventHeader(header);
867 //_______________________________________________________________________
869 Int_t AliRun::GetEvNumber() const
871 //Returns number of current event
872 if (fRunLoader == 0x0)
874 AliError("RunLoader is not set. Can not load data.");
878 return fRunLoader->GetEventNumber();
880 //_______________________________________________________________________
882 void AliRun::SetRunLoader(AliRunLoader* rloader)
885 // Set the loader of the run
887 fRunLoader = rloader;
888 if (fRunLoader == 0x0) return;
891 TFolder* evfold = fRunLoader->GetEventFolder();
892 if (evfold) evfoldname = evfold->GetName();
893 else AliWarning("Did not get Event Folder from Run Loader");
895 if ( fRunLoader->GetAliRun() )
896 {//if alrun already exists in folder
897 if (fRunLoader->GetAliRun() != this )
898 {//and is different than this - crash
899 AliFatal("AliRun is already in Folder and it is not this object");
905 evfold->Add(this);//Post this AliRun to Folder
908 TIter next(fModules);
910 while((module = (AliModule*)next()))
912 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
913 module->SetRunLoader(fRunLoader);
914 AliDetector* detector = dynamic_cast<AliDetector*>(module);
917 AliLoader* loader = fRunLoader->GetLoader(detector);
920 AliError(Form("Can not get loader for detector %s", detector->GetName()));
924 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
925 detector->SetLoader(loader);
931 void AliRun::AddModule(AliModule* mod)
934 // Add a module to the module list
936 if (mod == 0x0) return;
937 if (strlen(mod->GetName()) == 0) return;
938 if (GetModuleID(mod->GetName()) >= 0) return;
940 AliDebug(1, mod->GetName());
941 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
942 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
949 // added by Alberto Colla
950 //_____________________________________________________________________________
951 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
952 { return !gSystem->AccessPathName(fnam,mode);}
954 //______________________________________________________
955 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
957 TString str = name; gSystem->ExpandPathName(str);
958 return !gSystem->AccessPathName(str.Data(),mode);
961 //_____________________________________________________________________________
962 Bool_t AliRun::ApplyDisplacements(TClonesArray* AlObjArray)
964 // Read collection of alignment objects (AliAlignObj derived) saved
965 // in the TClonesArray ClArrayName and apply them to the geometry
966 // manager singleton.
968 Int_t nvols = AlObjArray->GetEntriesFast();
970 for(Int_t j=0; j<nvols; j++)
972 AliAlignObj* alobj = (AliAlignObj*) AlObjArray->UncheckedAt(j);
973 if (alobj->ApplyToGeometry() == kFALSE)