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
90 fIsRootGeometry(kFALSE),
91 fGeometryFileName(""),
92 fTriggerDescriptor(""),
96 // Default constructor for AliRun
98 AliConfig::Instance();//skowron 29 Feb 2002
99 //ensures that the folder structure is build
102 //_______________________________________________________________________
103 AliRun::AliRun(const AliRun& arun):
117 fPDGDB(0), //Particle factory object
118 fConfigFunction("\0"),
120 fIsRootGeometry(kFALSE),
121 fGeometryFileName(""),
122 fTriggerDescriptor(""),
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
146 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
147 fConfigFunction("Config();"),
148 fRandom(new TRandom3()),
149 fIsRootGeometry(kFALSE),
150 fGeometryFileName(""),
151 fTriggerDescriptor(""),
155 // Constructor for the main processor.
156 // Creates the geometry
157 // Creates the list of Detectors.
158 // Creates the list of particles.
163 // Set random number generator
166 if (gSystem->Getenv("CONFIG_SEED")) {
167 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
170 // Add to list of browsable
171 gROOT->GetListOfBrowsables()->Add(this,name);
172 // Create the TNode geometry for the event display
173 BuildSimpleGeometry();
175 // Create default mag field
178 // Add particle list to configuration
179 AliConfig::Instance()->Add(fPDGDB);
184 //_______________________________________________________________________
188 // Default AliRun destructor
190 gROOT->GetListOfBrowsables()->Remove(this);
194 TFolder* evfold = fRunLoader->GetEventFolder();
195 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
196 TIter next(fModules);
198 while((mod = (AliModule*)next()))
200 modfold->Remove(mod);
219 //_______________________________________________________________________
220 void AliRun::Copy(TObject &) const
222 AliFatal("Not implemented!");
225 //_______________________________________________________________________
229 // Initialize Alice geometry
234 //_______________________________________________________________________
235 void AliRun::BuildSimpleGeometry()
238 // Create a simple TNode geometry used by Root display engine
240 // Initialise geometry
242 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
243 new TMaterial("void","Vacuum",0,0,0); //Everything is void
244 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
245 brik->SetVisibility(0);
246 new TNode("alice","alice","S_alice");
249 //_______________________________________________________________________
250 void AliRun::CleanDetectors()
253 // Clean Detectors at the end of event
255 fRunLoader->CleanDetectors();
258 //_______________________________________________________________________
259 void AliRun::ResetHits()
264 //_______________________________________________________________________
265 AliGenerator* AliRun::Generator() const
267 return fMCApp->Generator();
270 //_______________________________________________________________________
271 void AliRun::SetField(AliMagF* magField)
274 // Set Magnetic Field Map
280 //_______________________________________________________________________
281 void AliRun::SetRootGeometry(Bool_t flag)
283 // Instruct application that the geometry is to be retreived from a root file.
284 fIsRootGeometry = flag;
285 if (flag) gMC->SetRootGeometry();
287 //_______________________________________________________________________
288 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
289 Float_t maxField, const char* filename)
292 // Set magnetic field parameters
293 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
294 // version Magnetic field map version (only 1 active now)
295 // scale Scale factor for the magnetic field
296 // maxField Maximum value for the magnetic field
299 // --- Sanity check on mag field flags
300 if(fField) delete fField;
302 fField = new AliMagFC("Map1"," ",type,scale,maxField);
303 } else if(version<=2) {
304 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
306 } else if(version==3) {
307 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
310 AliWarning(Form("Invalid map %d",version));
314 //_____________________________________________________________________________
316 void AliRun::InitLoaders()
318 //creates list of getters
320 TIter next(fModules);
322 while((mod = (AliModule*)next()))
324 mod->SetRunLoader(fRunLoader);
325 AliDetector *det = dynamic_cast<AliDetector*>(mod);
328 AliDebug(2, Form("Adding %s", det->GetName()));
329 fRunLoader->AddLoader(det);
334 //_____________________________________________________________________________
336 void AliRun::FinishRun()
339 // Called at the end of the run.
344 AliDebug(1, "Finish Lego");
345 fRunLoader->CdGAFile();
349 // Clean detector information
350 TIter next(fModules);
352 while((detector = dynamic_cast<AliModule*>(next()))) {
353 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
354 detector->FinishRun();
357 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
358 fRunLoader->WriteHeader("OVERWRITE");
360 // Write AliRun info and all detectors parameters
361 fRunLoader->CdGAFile();
362 Write(0,TObject::kOverwrite);//write AliRun
363 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
365 // Clean tree information
366 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
367 fRunLoader->Stack()->FinishRun();
369 if(fMCApp) fMCApp->FinishRun();
371 fRunLoader->Synchronize();
374 //_______________________________________________________________________
375 void AliRun::Announce() const
378 // Announce the current version of AliRoot
381 "****************************************************************\n");
382 printf("%6s","*");printf("%64s","*\n");
385 printf(" You are running AliRoot version NewIO\n");
388 printf(" The cvs tag for the current program is $Name$\n");
390 printf("%6s","*");printf("%64s","*\n");
392 "****************************************************************\n");
395 //_______________________________________________________________________
396 AliModule *AliRun::GetModule(const char *name) const
399 // Return pointer to detector from name
401 return dynamic_cast<AliModule*>(fModules->FindObject(name));
404 //_______________________________________________________________________
405 AliDetector *AliRun::GetDetector(const char *name) const
408 // Return pointer to detector from name
410 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
413 //_______________________________________________________________________
414 Int_t AliRun::GetModuleID(const char *name) const
417 // Return galice internal detector identifier from name
420 TObject *mod=fModules->FindObject(name);
421 if(mod) i=fModules->IndexOf(mod);
425 //_______________________________________________________________________
426 Int_t AliRun::GetEvent(Int_t event)
429 // Reloads data containers in folders # event
430 // Set branch addresses
432 if (fRunLoader == 0x0)
434 AliError("RunLoader is not set. Can not load data.");
437 /*****************************************/
438 /**** P R E R E L O A D I N G ****/
439 /*****************************************/
440 // Reset existing structures
442 fMCApp->ResetTrackReferences();
446 /*****************************************/
447 /**** R E L O A D ****/
448 /*****************************************/
450 fRunLoader->GetEvent(event);
452 /*****************************************/
453 /**** P O S T R E L O A D I N G ****/
454 /*****************************************/
456 // Set Trees branch addresses
457 TIter next(fModules);
459 while((detector = dynamic_cast<AliModule*>(next())))
461 detector->SetTreeAddress();
464 return fRunLoader->GetHeader()->GetNtrack();
467 //_______________________________________________________________________
468 TGeometry *AliRun::GetGeometry()
471 // Import Alice geometry from current file
472 // Return pointer to geometry object
474 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
476 // Unlink and relink nodes in detectors
477 // This is bad and there must be a better way...
480 TIter next(fModules);
482 while((detector = dynamic_cast<AliModule*>(next()))) {
483 TList *dnodes=detector->Nodes();
486 for ( j=0; j<dnodes->GetSize(); j++) {
487 node = dynamic_cast<TNode*>(dnodes->At(j));
488 node1 = fGeometry->GetNode(node->GetName());
489 dnodes->Remove(node);
490 dnodes->AddAt(node1,j);
496 //_______________________________________________________________________
497 void AliRun::SetBaseFile(const char *filename)
499 fBaseFileName = filename;
502 //_______________________________________________________________________
503 void AliRun::ResetDigits()
506 // Reset all Detectors digits
508 TIter next(fModules);
510 while((detector = dynamic_cast<AliModule*>(next()))) {
511 detector->ResetDigits();
515 //_______________________________________________________________________
516 void AliRun::ResetSDigits()
519 // Reset all Detectors digits
521 TIter next(fModules);
523 while((detector = dynamic_cast<AliModule*>(next()))) {
524 detector->ResetSDigits();
529 //_______________________________________________________________________
531 void AliRun::ResetPoints()
534 // Reset all Detectors points
536 TIter next(fModules);
538 while((detector = dynamic_cast<AliModule*>(next()))) {
539 detector->ResetPoints();
542 //_______________________________________________________________________
544 void AliRun::InitMC(const char *setup)
547 // Initialize ALICE Simulation run
552 AliWarning("Cannot initialise AliRun twice!");
557 fMCApp=new AliMC(GetName(),GetTitle());
559 gROOT->LoadMacro(setup);
560 gInterpreter->ProcessLine(fConfigFunction.Data());
562 fRunLoader->CdGAFile();
564 AliPDG::AddParticlesToPdgDataBase();
566 fNdets = fModules->GetLast()+1;
568 TIter next(fModules);
569 for(Int_t i=0; i<fNdets; ++i)
571 TObject *objfirst, *objlast;
572 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
573 objlast = gDirectory->GetList()->Last();
575 // Add Detector histograms in Detector list of histograms
576 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
577 else objfirst = gDirectory->GetList()->First();
580 detector->Histograms()->Add(objfirst);
581 objfirst = gDirectory->GetList()->After(objfirst);
587 //Must be here because some MCs (G4) adds detectors here and not in Config.C
589 fRunLoader->MakeTree("E");
592 fRunLoader->LoadKinematics("RECREATE");
593 fRunLoader->LoadTrackRefs("RECREATE");
594 fRunLoader->LoadHits("all","RECREATE");
598 // Save stuff at the beginning of the file to avoid file corruption
599 fRunLoader->CdGAFile();
601 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
604 //_______________________________________________________________________
606 void AliRun::RunMC(Int_t nevent, const char *setup)
609 // Main function to be called to process a galice run
611 // Root > gAlice.Run();
612 // a positive number of events will cause the finish routine
615 fEventsPerRun = nevent;
616 // check if initialisation has been done
617 if (!fInitDone) InitMC(setup);
619 // Create the Root Tree with one branch per detector
620 //Hits moved to begin event -> now we are crating separate tree for each event
622 gMC->ProcessRun(nevent);
624 // End of this run, close files
625 if(nevent>0) FinishRun();
628 //_______________________________________________________________________
629 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
632 // Main function to be called to reconstruct Alice event
634 Int_t nev = fRunLoader->GetNumberOfEvents();
635 AliDebug(1, Form("Found %d events", nev));
636 Int_t nFirst = first;
637 Int_t nLast = (last < 0)? nev : last;
639 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
640 AliDebug(1, Form("Processing event %d", nevent));
642 Digits2Reco(selected);
646 //_______________________________________________________________________
648 void AliRun::Hits2Digits(const char *selected)
651 // Convert Hits to sumable digits
653 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
655 Hits2SDigits(selected);
656 SDigits2Digits(selected);
661 //_______________________________________________________________________
663 void AliRun::Tree2Tree(Option_t *option, const char *selected)
666 // Function to transform the content of
668 // - TreeH to TreeS (option "S")
669 // - TreeS to TreeD (option "D")
670 // - TreeD to TreeR (option "R")
672 // If multiple options are specified ("SDR"), transformation will be done in sequence for
673 // selected detector and for all detectors if none is selected (detector string
674 // can contain blank separated list of detector names).
677 const char *oS = strstr(option,"S");
678 const char *oD = strstr(option,"D");
679 const char *oR = strstr(option,"R");
681 TObjArray *detectors = Detectors();
683 TIter next(detectors);
685 AliDetector *detector = 0;
687 while((detector = dynamic_cast<AliDetector*>(next()))) {
689 if (strcmp(detector->GetName(),selected)) continue;
690 if (detector->IsActive())
693 AliLoader* loader = detector->GetLoader();
694 if (loader == 0x0) continue;
698 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
699 loader->LoadHits("read");
700 if (loader->TreeS() == 0x0) loader->MakeTree("S");
701 detector->MakeBranch(option);
702 detector->SetTreeAddress();
703 detector->Hits2SDigits();
704 loader->UnloadHits();
705 loader->UnloadSDigits();
709 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
710 loader->LoadSDigits("read");
711 if (loader->TreeD() == 0x0) loader->MakeTree("D");
712 detector->MakeBranch(option);
713 detector->SetTreeAddress();
714 detector->SDigits2Digits();
715 loader->UnloadSDigits();
716 loader->UnloadDigits();
720 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
721 loader->LoadDigits("read");
722 if (loader->TreeR() == 0x0) loader->MakeTree("R");
723 detector->MakeBranch(option);
724 detector->SetTreeAddress();
725 detector->Digits2Reco();
726 loader->UnloadDigits();
727 loader->UnloadRecPoints();
734 //_______________________________________________________________________
735 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
736 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
737 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
740 // Generates lego plots of:
741 // - radiation length map phi vs theta
742 // - radiation length map phi vs eta
743 // - interaction length map
744 // - g/cm2 length map
746 // ntheta bins in theta, eta
747 // themin minimum angle in theta (degrees)
748 // themax maximum angle in theta (degrees)
750 // phimin minimum angle in phi (degrees)
751 // phimax maximum angle in phi (degrees)
752 // rmin minimum radius
753 // rmax maximum radius
756 // The number of events generated = ntheta*nphi
757 // run input parameters in macro setup (default="Config.C")
759 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
762 <img src="picts/AliRunLego1.gif">
767 <img src="picts/AliRunLego2.gif">
772 <img src="picts/AliRunLego3.gif">
777 // check if initialisation has been done
778 // If runloader has been initialized, set the number of events per file to nc1 * nc2
781 if (!gener) gener = new AliLegoGenerator();
783 // Configure Generator
784 gener->SetRadiusRange(rmin, rmax);
785 gener->SetZMax(zmax);
786 gener->SetCoor1Range(nc1, c1min, c1max);
787 gener->SetCoor2Range(nc2, c2min, c2max);
791 fLego = new AliLego("lego",gener);
793 if (!fInitDone) InitMC(setup);
794 //Save current generator
796 AliGenerator *gen=fMCApp->Generator();
797 fMCApp->ResetGenerator(gener);
798 //Prepare MC for Lego Run
803 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
804 //gMC->ProcessRun(nc1*nc2+1);
805 gMC->ProcessRun(nc1*nc2);
807 // End of this run, close files
809 // Restore current generator
810 fMCApp->ResetGenerator(gen);
811 // Delete Lego Object
812 delete fLego; fLego=0;
815 //_______________________________________________________________________
816 void AliRun::SetConfigFunction(const char * config)
819 // Set the signature of the function contained in Config.C to configure
822 fConfigFunction=config;
829 //_______________________________________________________________________
830 void AliRun::Field(const Double_t* x, Double_t *b) const
833 // Return the value of the magnetic field
836 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
840 Field()->Field(xfloat,bfloat);
841 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
844 AliError("No mag field defined!");
850 // End of MC Application
853 //_______________________________________________________________________
854 void AliRun::Streamer(TBuffer &R__b)
856 // Stream an object of class AliRun.
858 if (R__b.IsReading()) {
859 if (!gAlice) gAlice = this;
860 AliRun::Class()->ReadBuffer(R__b, this);
861 gROOT->GetListOfBrowsables()->Add(this,"Run");
865 AliRun::Class()->WriteBuffer(R__b, this);
868 //_______________________________________________________________________
870 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
872 fRunLoader->GetHeader()->SetGenEventHeader(header);
874 //_______________________________________________________________________
876 Int_t AliRun::GetEvNumber() const
878 //Returns number of current event
879 if (fRunLoader == 0x0)
881 AliError("RunLoader is not set. Can not load data.");
885 return fRunLoader->GetEventNumber();
887 //_______________________________________________________________________
889 void AliRun::SetRunLoader(AliRunLoader* rloader)
892 // Set the loader of the run
894 fRunLoader = rloader;
895 if (fRunLoader == 0x0) return;
898 TFolder* evfold = fRunLoader->GetEventFolder();
899 if (evfold) evfoldname = evfold->GetName();
900 else AliWarning("Did not get Event Folder from Run Loader");
902 if ( fRunLoader->GetAliRun() )
903 {//if alrun already exists in folder
904 if (fRunLoader->GetAliRun() != this )
905 {//and is different than this - crash
906 AliFatal("AliRun is already in Folder and it is not this object");
912 evfold->Add(this);//Post this AliRun to Folder
915 TIter next(fModules);
917 while((module = (AliModule*)next()))
919 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
920 module->SetRunLoader(fRunLoader);
921 AliDetector* detector = dynamic_cast<AliDetector*>(module);
924 AliLoader* loader = fRunLoader->GetLoader(detector);
927 AliError(Form("Can not get loader for detector %s", detector->GetName()));
931 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
932 detector->SetLoader(loader);
938 void AliRun::AddModule(AliModule* mod)
941 // Add a module to the module list
943 if (mod == 0x0) return;
944 if (strlen(mod->GetName()) == 0) return;
945 if (GetModuleID(mod->GetName()) >= 0) return;
947 AliDebug(1, mod->GetName());
948 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
949 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
956 // added by Alberto Colla
957 //_____________________________________________________________________________
958 /*inline*/ Bool_t AliRun::IsFileAccessible(const char* fnam, EAccessMode mode)
959 { return !gSystem->AccessPathName(fnam,mode);}
961 //______________________________________________________
962 /*inline*/ Bool_t AliRun::IsFileAccessible(Char_t* name,EAccessMode mode)
964 TString str = name; gSystem->ExpandPathName(str);
965 return !gSystem->AccessPathName(str.Data(),mode);