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"
67 #include "AliAlignObj.h"
73 //_______________________________________________________________________
87 fPDGDB(0), //Particle factory object
88 fConfigFunction("\0"),
90 fIsRootGeometry(kFALSE),
94 // Default constructor for AliRun
96 AliConfig::Instance();//skowron 29 Feb 2002
97 //ensures that the folder structure is build
100 //_______________________________________________________________________
101 AliRun::AliRun(const AliRun& arun):
115 fPDGDB(0), //Particle factory object
116 fConfigFunction("\0"),
118 fIsRootGeometry(kFALSE),
122 // Copy constructor for AliRun
127 //_____________________________________________________________________________
128 AliRun::AliRun(const char *name, const char *title):
134 fModules(new TObjArray(77)), // Support list for the Detectors
142 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
143 fConfigFunction("Config();"),
144 fRandom(new TRandom3()),
145 fIsRootGeometry(kFALSE),
149 // Constructor for the main processor.
150 // Creates the geometry
151 // Creates the list of Detectors.
152 // Creates the list of particles.
157 // Set random number generator
160 if (gSystem->Getenv("CONFIG_SEED")) {
161 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
164 // Add to list of browsable
165 gROOT->GetListOfBrowsables()->Add(this,name);
166 // Create the TNode geometry for the event display
167 BuildSimpleGeometry();
169 // Create default mag field
172 // Add particle list to configuration
173 AliConfig::Instance()->Add(fPDGDB);
178 //_______________________________________________________________________
182 // Default AliRun destructor
184 gROOT->GetListOfBrowsables()->Remove(this);
188 TFolder* evfold = fRunLoader->GetEventFolder();
189 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
190 TIter next(fModules);
192 while((mod = (AliModule*)next()))
194 modfold->Remove(mod);
213 //_______________________________________________________________________
214 void AliRun::Copy(TObject &) const
216 AliFatal("Not implemented!");
219 //_______________________________________________________________________
223 // Initialize Alice geometry
228 //_______________________________________________________________________
229 void AliRun::BuildSimpleGeometry()
232 // Create a simple TNode geometry used by Root display engine
234 // Initialise geometry
236 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
237 new TMaterial("void","Vacuum",0,0,0); //Everything is void
238 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
239 brik->SetVisibility(0);
240 new TNode("alice","alice","S_alice");
243 //_______________________________________________________________________
244 void AliRun::CleanDetectors()
247 // Clean Detectors at the end of event
249 fRunLoader->CleanDetectors();
252 //_______________________________________________________________________
253 void AliRun::ResetHits()
258 //_______________________________________________________________________
259 AliGenerator* AliRun::Generator() const
261 return fMCApp->Generator();
264 //_______________________________________________________________________
265 void AliRun::SetField(AliMagF* magField)
268 // Set Magnetic Field Map
274 //_______________________________________________________________________
275 void AliRun::SetRootGeometry(Bool_t flag)
277 // Instruct application that the geometry is to be retreived from a root file.
278 fIsRootGeometry = flag;
279 if (flag) gMC->SetRootGeometry();
281 //_______________________________________________________________________
282 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
283 Float_t maxField, const char* filename)
286 // Set magnetic field parameters
287 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
288 // version Magnetic field map version (only 1 active now)
289 // scale Scale factor for the magnetic field
290 // maxField Maximum value for the magnetic field
293 // --- Sanity check on mag field flags
294 if(fField) delete fField;
296 fField = new AliMagFC("Map1"," ",type,scale,maxField);
297 } else if(version<=2) {
298 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
300 } else if(version==3) {
301 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
304 AliWarning(Form("Invalid map %d",version));
308 //_____________________________________________________________________________
310 void AliRun::InitLoaders()
312 //creates list of getters
314 TIter next(fModules);
316 while((mod = (AliModule*)next()))
318 mod->SetRunLoader(fRunLoader);
319 AliDetector *det = dynamic_cast<AliDetector*>(mod);
322 AliDebug(2, Form("Adding %s", det->GetName()));
323 fRunLoader->AddLoader(det);
328 //_____________________________________________________________________________
330 void AliRun::FinishRun()
333 // Called at the end of the run.
338 AliDebug(1, "Finish Lego");
339 fRunLoader->CdGAFile();
343 // Clean detector information
344 TIter next(fModules);
346 while((detector = dynamic_cast<AliModule*>(next()))) {
347 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
348 detector->FinishRun();
351 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
352 fRunLoader->WriteHeader("OVERWRITE");
354 // Write AliRun info and all detectors parameters
355 fRunLoader->CdGAFile();
356 Write(0,TObject::kOverwrite);//write AliRun
357 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
359 // Clean tree information
360 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
361 fRunLoader->Stack()->FinishRun();
363 if(fMCApp) fMCApp->FinishRun();
365 fRunLoader->Synchronize();
368 //_______________________________________________________________________
369 void AliRun::Announce() const
372 // Announce the current version of AliRoot
375 "****************************************************************\n");
376 printf("%6s","*");printf("%64s","*\n");
379 printf(" You are running AliRoot version NewIO\n");
382 printf(" The cvs tag for the current program is $Name$\n");
384 printf("%6s","*");printf("%64s","*\n");
386 "****************************************************************\n");
389 //_______________________________________________________________________
390 AliModule *AliRun::GetModule(const char *name) const
393 // Return pointer to detector from name
395 return dynamic_cast<AliModule*>(fModules->FindObject(name));
398 //_______________________________________________________________________
399 AliDetector *AliRun::GetDetector(const char *name) const
402 // Return pointer to detector from name
404 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
407 //_______________________________________________________________________
408 Int_t AliRun::GetModuleID(const char *name) const
411 // Return galice internal detector identifier from name
414 TObject *mod=fModules->FindObject(name);
415 if(mod) i=fModules->IndexOf(mod);
419 //_______________________________________________________________________
420 Int_t AliRun::GetEvent(Int_t event)
423 // Reloads data containers in folders # event
424 // Set branch addresses
426 if (fRunLoader == 0x0)
428 AliError("RunLoader is not set. Can not load data.");
431 /*****************************************/
432 /**** P R E R E L O A D I N G ****/
433 /*****************************************/
434 // Reset existing structures
436 fMCApp->ResetTrackReferences();
440 /*****************************************/
441 /**** R E L O A D ****/
442 /*****************************************/
444 fRunLoader->GetEvent(event);
446 /*****************************************/
447 /**** P O S T R E L O A D I N G ****/
448 /*****************************************/
450 // Set Trees branch addresses
451 TIter next(fModules);
453 while((detector = dynamic_cast<AliModule*>(next())))
455 detector->SetTreeAddress();
458 return fRunLoader->GetHeader()->GetNtrack();
461 //_______________________________________________________________________
462 TGeometry *AliRun::GetGeometry()
465 // Import Alice geometry from current file
466 // Return pointer to geometry object
468 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
470 // Unlink and relink nodes in detectors
471 // This is bad and there must be a better way...
474 TIter next(fModules);
476 while((detector = dynamic_cast<AliModule*>(next()))) {
477 TList *dnodes=detector->Nodes();
480 for ( j=0; j<dnodes->GetSize(); j++) {
481 node = dynamic_cast<TNode*>(dnodes->At(j));
482 node1 = fGeometry->GetNode(node->GetName());
483 dnodes->Remove(node);
484 dnodes->AddAt(node1,j);
490 //_______________________________________________________________________
491 void AliRun::SetBaseFile(const char *filename)
493 fBaseFileName = filename;
496 //_______________________________________________________________________
497 void AliRun::ResetDigits()
500 // Reset all Detectors digits
502 TIter next(fModules);
504 while((detector = dynamic_cast<AliModule*>(next()))) {
505 detector->ResetDigits();
509 //_______________________________________________________________________
510 void AliRun::ResetSDigits()
513 // Reset all Detectors digits
515 TIter next(fModules);
517 while((detector = dynamic_cast<AliModule*>(next()))) {
518 detector->ResetSDigits();
523 //_______________________________________________________________________
525 void AliRun::ResetPoints()
528 // Reset all Detectors points
530 TIter next(fModules);
532 while((detector = dynamic_cast<AliModule*>(next()))) {
533 detector->ResetPoints();
536 //_______________________________________________________________________
538 void AliRun::InitMC(const char *setup)
541 // Initialize ALICE Simulation run
546 AliWarning("Cannot initialise AliRun twice!");
551 fMCApp=new AliMC(GetName(),GetTitle());
553 gROOT->LoadMacro(setup);
554 gInterpreter->ProcessLine(fConfigFunction.Data());
556 fRunLoader->CdGAFile();
558 AliPDG::AddParticlesToPdgDataBase();
560 fNdets = fModules->GetLast()+1;
562 TIter next(fModules);
563 for(Int_t i=0; i<fNdets; ++i)
565 TObject *objfirst, *objlast;
566 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
567 objlast = gDirectory->GetList()->Last();
569 // Add Detector histograms in Detector list of histograms
570 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
571 else objfirst = gDirectory->GetList()->First();
574 detector->Histograms()->Add(objfirst);
575 objfirst = gDirectory->GetList()->After(objfirst);
581 //Must be here because some MCs (G4) adds detectors here and not in Config.C
583 fRunLoader->MakeTree("E");
586 fRunLoader->LoadKinematics("RECREATE");
587 fRunLoader->LoadTrackRefs("RECREATE");
588 fRunLoader->LoadHits("all","RECREATE");
592 // Save stuff at the beginning of the file to avoid file corruption
593 fRunLoader->CdGAFile();
595 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
598 //_______________________________________________________________________
600 void AliRun::RunMC(Int_t nevent, const char *setup)
603 // Main function to be called to process a galice run
605 // Root > gAlice.Run();
606 // a positive number of events will cause the finish routine
609 fEventsPerRun = nevent;
610 // check if initialisation has been done
611 if (!fInitDone) InitMC(setup);
613 // Create the Root Tree with one branch per detector
614 //Hits moved to begin event -> now we are crating separate tree for each event
616 gMC->ProcessRun(nevent);
618 // End of this run, close files
619 if(nevent>0) FinishRun();
622 //_______________________________________________________________________
623 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
626 // Main function to be called to reconstruct Alice event
628 Int_t nev = fRunLoader->GetNumberOfEvents();
629 AliDebug(1, Form("Found %d events", nev));
630 Int_t nFirst = first;
631 Int_t nLast = (last < 0)? nev : last;
633 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
634 AliDebug(1, Form("Processing event %d", nevent));
636 Digits2Reco(selected);
640 //_______________________________________________________________________
642 void AliRun::Hits2Digits(const char *selected)
645 // Convert Hits to sumable digits
647 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
649 Hits2SDigits(selected);
650 SDigits2Digits(selected);
655 //_______________________________________________________________________
657 void AliRun::Tree2Tree(Option_t *option, const char *selected)
660 // Function to transform the content of
662 // - TreeH to TreeS (option "S")
663 // - TreeS to TreeD (option "D")
664 // - TreeD to TreeR (option "R")
666 // If multiple options are specified ("SDR"), transformation will be done in sequence for
667 // selected detector and for all detectors if none is selected (detector string
668 // can contain blank separated list of detector names).
671 const char *oS = strstr(option,"S");
672 const char *oD = strstr(option,"D");
673 const char *oR = strstr(option,"R");
675 TObjArray *detectors = Detectors();
677 TIter next(detectors);
679 AliDetector *detector = 0;
681 while((detector = dynamic_cast<AliDetector*>(next()))) {
683 if (strcmp(detector->GetName(),selected)) continue;
684 if (detector->IsActive())
687 AliLoader* loader = detector->GetLoader();
688 if (loader == 0x0) continue;
692 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
693 loader->LoadHits("read");
694 if (loader->TreeS() == 0x0) loader->MakeTree("S");
695 detector->MakeBranch(option);
696 detector->SetTreeAddress();
697 detector->Hits2SDigits();
698 loader->UnloadHits();
699 loader->UnloadSDigits();
703 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
704 loader->LoadSDigits("read");
705 if (loader->TreeD() == 0x0) loader->MakeTree("D");
706 detector->MakeBranch(option);
707 detector->SetTreeAddress();
708 detector->SDigits2Digits();
709 loader->UnloadSDigits();
710 loader->UnloadDigits();
714 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
715 loader->LoadDigits("read");
716 if (loader->TreeR() == 0x0) loader->MakeTree("R");
717 detector->MakeBranch(option);
718 detector->SetTreeAddress();
719 detector->Digits2Reco();
720 loader->UnloadDigits();
721 loader->UnloadRecPoints();
728 //_______________________________________________________________________
729 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
730 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
731 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
734 // Generates lego plots of:
735 // - radiation length map phi vs theta
736 // - radiation length map phi vs eta
737 // - interaction length map
738 // - g/cm2 length map
740 // ntheta bins in theta, eta
741 // themin minimum angle in theta (degrees)
742 // themax maximum angle in theta (degrees)
744 // phimin minimum angle in phi (degrees)
745 // phimax maximum angle in phi (degrees)
746 // rmin minimum radius
747 // rmax maximum radius
750 // The number of events generated = ntheta*nphi
751 // run input parameters in macro setup (default="Config.C")
753 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
756 <img src="picts/AliRunLego1.gif">
761 <img src="picts/AliRunLego2.gif">
766 <img src="picts/AliRunLego3.gif">
771 // check if initialisation has been done
772 // If runloader has been initialized, set the number of events per file to nc1 * nc2
775 if (!gener) gener = new AliLegoGenerator();
777 // Configure Generator
778 gener->SetRadiusRange(rmin, rmax);
779 gener->SetZMax(zmax);
780 gener->SetCoor1Range(nc1, c1min, c1max);
781 gener->SetCoor2Range(nc2, c2min, c2max);
785 fLego = new AliLego("lego",gener);
787 if (!fInitDone) InitMC(setup);
788 //Save current generator
790 AliGenerator *gen=fMCApp->Generator();
791 fMCApp->ResetGenerator(gener);
792 //Prepare MC for Lego Run
797 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
798 //gMC->ProcessRun(nc1*nc2+1);
799 gMC->ProcessRun(nc1*nc2);
801 // End of this run, close files
803 // Restore current generator
804 fMCApp->ResetGenerator(gen);
805 // Delete Lego Object
806 delete fLego; fLego=0;
809 //_______________________________________________________________________
810 void AliRun::SetConfigFunction(const char * config)
813 // Set the signature of the function contained in Config.C to configure
816 fConfigFunction=config;
823 //_______________________________________________________________________
824 void AliRun::Field(const Double_t* x, Double_t *b) const
827 // Return the value of the magnetic field
830 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
834 Field()->Field(xfloat,bfloat);
835 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
838 AliError("No mag field defined!");
844 // End of MC Application
847 //_______________________________________________________________________
848 void AliRun::Streamer(TBuffer &R__b)
850 // Stream an object of class AliRun.
852 if (R__b.IsReading()) {
853 if (!gAlice) gAlice = this;
854 AliRun::Class()->ReadBuffer(R__b, this);
855 gROOT->GetListOfBrowsables()->Add(this,"Run");
859 AliRun::Class()->WriteBuffer(R__b, this);
862 //_______________________________________________________________________
864 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
866 fRunLoader->GetHeader()->SetGenEventHeader(header);
868 //_______________________________________________________________________
870 Int_t AliRun::GetEvNumber() const
872 //Returns number of current event
873 if (fRunLoader == 0x0)
875 AliError("RunLoader is not set. Can not load data.");
879 return fRunLoader->GetEventNumber();
881 //_______________________________________________________________________
883 void AliRun::SetRunLoader(AliRunLoader* rloader)
886 // Set the loader of the run
888 fRunLoader = rloader;
889 if (fRunLoader == 0x0) return;
892 TFolder* evfold = fRunLoader->GetEventFolder();
893 if (evfold) evfoldname = evfold->GetName();
894 else AliWarning("Did not get Event Folder from Run Loader");
896 if ( fRunLoader->GetAliRun() )
897 {//if alrun already exists in folder
898 if (fRunLoader->GetAliRun() != this )
899 {//and is different than this - crash
900 AliFatal("AliRun is already in Folder and it is not this object");
906 evfold->Add(this);//Post this AliRun to Folder
909 TIter next(fModules);
911 while((module = (AliModule*)next()))
913 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
914 module->SetRunLoader(fRunLoader);
915 AliDetector* detector = dynamic_cast<AliDetector*>(module);
918 AliLoader* loader = fRunLoader->GetLoader(detector);
921 AliError(Form("Can not get loader for detector %s", detector->GetName()));
925 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
926 detector->SetLoader(loader);
932 void AliRun::AddModule(AliModule* mod)
935 // Add a module to the module list
937 if (mod == 0x0) return;
938 if (strlen(mod->GetName()) == 0) return;
939 if (GetModuleID(mod->GetName()) >= 0) return;
941 AliDebug(1, mod->GetName());
942 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
943 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
950 // added by Alberto Colla
951 //_____________________________________________________________________________
952 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
953 { return !gSystem->AccessPathName(fnam,mode);}
955 //______________________________________________________
956 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
958 TString str = name; gSystem->ExpandPathName(str);
959 return !gSystem->AccessPathName(str.Data(),mode);
962 //_____________________________________________________________________________
963 Bool_t AliRun::ApplyAlignObjsToGeom(TObjArray* AlObjArray)
965 // Read collection of alignment objects (AliAlignObj derived) saved
966 // in the TClonesArray ClArrayName and apply them to the geometry
967 // manager singleton.
970 Int_t nvols = AlObjArray->GetEntriesFast();
972 for(Int_t j=0; j<nvols; j++)
974 AliAlignObj* alobj = (AliAlignObj*) AlObjArray->UncheckedAt(j);
975 if (alobj->ApplyToGeometry() == kFALSE)
979 gGeoManager->CheckOverlaps(20);
980 TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
981 if(ovexlist->GetEntriesFast()){
982 AliErrorClass("The application of alignment objects to the geometry caused huge overlaps/extrusions!");