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"
72 //_______________________________________________________________________
87 fPDGDB(0), //Particle factory object
88 fConfigFunction("\0"),
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):
115 fPDGDB(0), //Particle factory object
116 fConfigFunction("\0"),
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
142 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
143 fConfigFunction("Config();"),
144 fRandom(new TRandom3()),
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::SetField(Int_t type, Int_t version, Float_t scale,
275 Float_t maxField, const char* filename)
278 // Set magnetic field parameters
279 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
280 // version Magnetic field map version (only 1 active now)
281 // scale Scale factor for the magnetic field
282 // maxField Maximum value for the magnetic field
285 // --- Sanity check on mag field flags
286 if(fField) delete fField;
288 fField = new AliMagFC("Map1"," ",type,scale,maxField);
289 } else if(version<=2) {
290 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
292 } else if(version==3) {
293 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
296 AliWarning(Form("Invalid map %d",version));
300 //_____________________________________________________________________________
302 void AliRun::InitLoaders()
304 //creates list of getters
306 TIter next(fModules);
308 while((mod = (AliModule*)next()))
310 mod->SetRunLoader(fRunLoader);
311 AliDetector *det = dynamic_cast<AliDetector*>(mod);
314 AliDebug(2, Form("Adding %s", det->GetName()));
315 fRunLoader->AddLoader(det);
320 //_____________________________________________________________________________
322 void AliRun::FinishRun()
325 // Called at the end of the run.
330 AliDebug(1, "Finish Lego");
331 fRunLoader->CdGAFile();
335 // Clean detector information
336 TIter next(fModules);
338 while((detector = dynamic_cast<AliModule*>(next()))) {
339 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
340 detector->FinishRun();
343 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
344 fRunLoader->WriteHeader("OVERWRITE");
346 // Write AliRun info and all detectors parameters
347 fRunLoader->CdGAFile();
348 Write(0,TObject::kOverwrite);//write AliRun
349 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
351 // Clean tree information
352 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
353 fRunLoader->Stack()->FinishRun();
355 if(fMCApp) fMCApp->FinishRun();
357 fRunLoader->Synchronize();
360 //_______________________________________________________________________
361 void AliRun::Announce() const
364 // Announce the current version of AliRoot
367 "****************************************************************\n");
368 printf("%6s","*");printf("%64s","*\n");
371 printf(" You are running AliRoot version NewIO\n");
374 printf(" The cvs tag for the current program is $Name$\n");
376 printf("%6s","*");printf("%64s","*\n");
378 "****************************************************************\n");
381 //_______________________________________________________________________
382 AliModule *AliRun::GetModule(const char *name) const
385 // Return pointer to detector from name
387 return dynamic_cast<AliModule*>(fModules->FindObject(name));
390 //_______________________________________________________________________
391 AliDetector *AliRun::GetDetector(const char *name) const
394 // Return pointer to detector from name
396 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
399 //_______________________________________________________________________
400 Int_t AliRun::GetModuleID(const char *name) const
403 // Return galice internal detector identifier from name
406 TObject *mod=fModules->FindObject(name);
407 if(mod) i=fModules->IndexOf(mod);
411 //_______________________________________________________________________
412 Int_t AliRun::GetEvent(Int_t event)
415 // Reloads data containers in folders # event
416 // Set branch addresses
418 if (fRunLoader == 0x0)
420 AliError("RunLoader is not set. Can not load data.");
423 /*****************************************/
424 /**** P R E R E L O A D I N G ****/
425 /*****************************************/
426 // Reset existing structures
428 fMCApp->ResetTrackReferences();
432 /*****************************************/
433 /**** R E L O A D ****/
434 /*****************************************/
436 fRunLoader->GetEvent(event);
438 /*****************************************/
439 /**** P O S T R E L O A D I N G ****/
440 /*****************************************/
442 // Set Trees branch addresses
443 TIter next(fModules);
445 while((detector = dynamic_cast<AliModule*>(next())))
447 detector->SetTreeAddress();
450 return fRunLoader->GetHeader()->GetNtrack();
453 //_______________________________________________________________________
454 TGeometry *AliRun::GetGeometry()
457 // Import Alice geometry from current file
458 // Return pointer to geometry object
460 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
462 // Unlink and relink nodes in detectors
463 // This is bad and there must be a better way...
466 TIter next(fModules);
468 while((detector = dynamic_cast<AliModule*>(next()))) {
469 TList *dnodes=detector->Nodes();
472 for ( j=0; j<dnodes->GetSize(); j++) {
473 node = dynamic_cast<TNode*>(dnodes->At(j));
474 node1 = fGeometry->GetNode(node->GetName());
475 dnodes->Remove(node);
476 dnodes->AddAt(node1,j);
482 //_______________________________________________________________________
483 void AliRun::SetBaseFile(const char *filename)
485 fBaseFileName = filename;
488 //_______________________________________________________________________
489 void AliRun::ResetDigits()
492 // Reset all Detectors digits
494 TIter next(fModules);
496 while((detector = dynamic_cast<AliModule*>(next()))) {
497 detector->ResetDigits();
501 //_______________________________________________________________________
502 void AliRun::ResetSDigits()
505 // Reset all Detectors digits
507 TIter next(fModules);
509 while((detector = dynamic_cast<AliModule*>(next()))) {
510 detector->ResetSDigits();
515 //_______________________________________________________________________
517 void AliRun::ResetPoints()
520 // Reset all Detectors points
522 TIter next(fModules);
524 while((detector = dynamic_cast<AliModule*>(next()))) {
525 detector->ResetPoints();
528 //_______________________________________________________________________
530 void AliRun::InitMC(const char *setup)
533 // Initialize ALICE Simulation run
538 AliWarning("Cannot initialise AliRun twice!");
543 fMCApp=new AliMC(GetName(),GetTitle());
545 gROOT->LoadMacro(setup);
546 gInterpreter->ProcessLine(fConfigFunction.Data());
548 fRunLoader->CdGAFile();
550 AliPDG::AddParticlesToPdgDataBase();
552 fNdets = fModules->GetLast()+1;
554 TIter next(fModules);
555 for(Int_t i=0; i<fNdets; ++i)
557 TObject *objfirst, *objlast;
558 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
559 objlast = gDirectory->GetList()->Last();
561 // Add Detector histograms in Detector list of histograms
562 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
563 else objfirst = gDirectory->GetList()->First();
566 detector->Histograms()->Add(objfirst);
567 objfirst = gDirectory->GetList()->After(objfirst);
573 //Must be here because some MCs (G4) adds detectors here and not in Config.C
575 fRunLoader->MakeTree("E");
578 fRunLoader->LoadKinematics("RECREATE");
579 fRunLoader->LoadTrackRefs("RECREATE");
580 fRunLoader->LoadHits("all","RECREATE");
584 // Save stuff at the beginning of the file to avoid file corruption
585 fRunLoader->CdGAFile();
587 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
590 //_______________________________________________________________________
592 void AliRun::RunMC(Int_t nevent, const char *setup)
595 // Main function to be called to process a galice run
597 // Root > gAlice.Run();
598 // a positive number of events will cause the finish routine
601 fEventsPerRun = nevent;
602 // check if initialisation has been done
603 if (!fInitDone) InitMC(setup);
605 // Create the Root Tree with one branch per detector
606 //Hits moved to begin event -> now we are crating separate tree for each event
608 gMC->ProcessRun(nevent);
610 // End of this run, close files
611 if(nevent>0) FinishRun();
614 //_______________________________________________________________________
615 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
618 // Main function to be called to reconstruct Alice event
620 Int_t nev = fRunLoader->GetNumberOfEvents();
621 AliDebug(1, Form("Found %d events", nev));
622 Int_t nFirst = first;
623 Int_t nLast = (last < 0)? nev : last;
625 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
626 AliDebug(1, Form("Processing event %d", nevent));
628 Digits2Reco(selected);
632 //_______________________________________________________________________
634 void AliRun::Hits2Digits(const char *selected)
637 // Convert Hits to sumable digits
639 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
641 Hits2SDigits(selected);
642 SDigits2Digits(selected);
647 //_______________________________________________________________________
649 void AliRun::Tree2Tree(Option_t *option, const char *selected)
652 // Function to transform the content of
654 // - TreeH to TreeS (option "S")
655 // - TreeS to TreeD (option "D")
656 // - TreeD to TreeR (option "R")
658 // If multiple options are specified ("SDR"), transformation will be done in sequence for
659 // selected detector and for all detectors if none is selected (detector string
660 // can contain blank separated list of detector names).
663 const char *oS = strstr(option,"S");
664 const char *oD = strstr(option,"D");
665 const char *oR = strstr(option,"R");
667 TObjArray *detectors = Detectors();
669 TIter next(detectors);
671 AliDetector *detector = 0;
673 while((detector = dynamic_cast<AliDetector*>(next()))) {
675 if (strcmp(detector->GetName(),selected)) continue;
676 if (detector->IsActive())
679 AliLoader* loader = detector->GetLoader();
680 if (loader == 0x0) continue;
684 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
685 loader->LoadHits("read");
686 if (loader->TreeS() == 0x0) loader->MakeTree("S");
687 detector->MakeBranch(option);
688 detector->SetTreeAddress();
689 detector->Hits2SDigits();
690 loader->UnloadHits();
691 loader->UnloadSDigits();
695 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
696 loader->LoadSDigits("read");
697 if (loader->TreeD() == 0x0) loader->MakeTree("D");
698 detector->MakeBranch(option);
699 detector->SetTreeAddress();
700 detector->SDigits2Digits();
701 loader->UnloadSDigits();
702 loader->UnloadDigits();
706 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
707 loader->LoadDigits("read");
708 if (loader->TreeR() == 0x0) loader->MakeTree("R");
709 detector->MakeBranch(option);
710 detector->SetTreeAddress();
711 detector->Digits2Reco();
712 loader->UnloadDigits();
713 loader->UnloadRecPoints();
720 //_______________________________________________________________________
721 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
722 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
723 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
726 // Generates lego plots of:
727 // - radiation length map phi vs theta
728 // - radiation length map phi vs eta
729 // - interaction length map
730 // - g/cm2 length map
732 // ntheta bins in theta, eta
733 // themin minimum angle in theta (degrees)
734 // themax maximum angle in theta (degrees)
736 // phimin minimum angle in phi (degrees)
737 // phimax maximum angle in phi (degrees)
738 // rmin minimum radius
739 // rmax maximum radius
742 // The number of events generated = ntheta*nphi
743 // run input parameters in macro setup (default="Config.C")
745 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
748 <img src="picts/AliRunLego1.gif">
753 <img src="picts/AliRunLego2.gif">
758 <img src="picts/AliRunLego3.gif">
763 // check if initialisation has been done
764 // If runloader has been initialized, set the number of events per file to nc1 * nc2
767 if (!gener) gener = new AliLegoGenerator();
769 // Configure Generator
770 gener->SetRadiusRange(rmin, rmax);
771 gener->SetZMax(zmax);
772 gener->SetCoor1Range(nc1, c1min, c1max);
773 gener->SetCoor2Range(nc2, c2min, c2max);
777 fLego = new AliLego("lego",gener);
779 if (!fInitDone) InitMC(setup);
780 //Save current generator
782 AliGenerator *gen=fMCApp->Generator();
783 fMCApp->ResetGenerator(gener);
784 //Prepare MC for Lego Run
789 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
790 //gMC->ProcessRun(nc1*nc2+1);
791 gMC->ProcessRun(nc1*nc2);
793 // End of this run, close files
795 // Restore current generator
796 fMCApp->ResetGenerator(gen);
797 // Delete Lego Object
798 delete fLego; fLego=0;
801 //_______________________________________________________________________
802 void AliRun::SetConfigFunction(const char * config)
805 // Set the signature of the function contained in Config.C to configure
808 fConfigFunction=config;
815 //_______________________________________________________________________
816 void AliRun::Field(const Double_t* x, Double_t *b) const
819 // Return the value of the magnetic field
822 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
826 Field()->Field(xfloat,bfloat);
827 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
830 AliError("No mag field defined!");
836 // End of MC Application
839 //_______________________________________________________________________
840 void AliRun::Streamer(TBuffer &R__b)
842 // Stream an object of class AliRun.
844 if (R__b.IsReading()) {
845 if (!gAlice) gAlice = this;
846 AliRun::Class()->ReadBuffer(R__b, this);
847 gROOT->GetListOfBrowsables()->Add(this,"Run");
851 AliRun::Class()->WriteBuffer(R__b, this);
854 //_______________________________________________________________________
856 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
858 fRunLoader->GetHeader()->SetGenEventHeader(header);
860 //_______________________________________________________________________
862 Int_t AliRun::GetEvNumber() const
864 //Returns number of current event
865 if (fRunLoader == 0x0)
867 AliError("RunLoader is not set. Can not load data.");
871 return fRunLoader->GetEventNumber();
873 //_______________________________________________________________________
875 void AliRun::SetRunLoader(AliRunLoader* rloader)
878 // Set the loader of the run
880 fRunLoader = rloader;
881 if (fRunLoader == 0x0) return;
884 TFolder* evfold = fRunLoader->GetEventFolder();
885 if (evfold) evfoldname = evfold->GetName();
886 else AliWarning("Did not get Event Folder from Run Loader");
888 if ( fRunLoader->GetAliRun() )
889 {//if alrun already exists in folder
890 if (fRunLoader->GetAliRun() != this )
891 {//and is different than this - crash
892 AliFatal("AliRun is already in Folder and it is not this object");
898 evfold->Add(this);//Post this AliRun to Folder
901 TIter next(fModules);
903 while((module = (AliModule*)next()))
905 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
906 module->SetRunLoader(fRunLoader);
907 AliDetector* detector = dynamic_cast<AliDetector*>(module);
910 AliLoader* loader = fRunLoader->GetLoader(detector);
913 AliError(Form("Can not get loader for detector %s", detector->GetName()));
917 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
918 detector->SetLoader(loader);
924 void AliRun::AddModule(AliModule* mod)
927 // Add a module to the module list
929 if (mod == 0x0) return;
930 if (strlen(mod->GetName()) == 0) return;
931 if (GetModuleID(mod->GetName()) >= 0) return;
933 AliDebug(1, mod->GetName());
934 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
935 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
943 //_______________________________________________________________________
944 Int_t AliRun::GetDebug() const
946 AliWarning("Don't use this method any more, use AliDebug instead");
947 return AliDebugLevel();
950 //_______________________________________________________________________
951 void AliRun::SetDebug(Int_t level)
953 AliWarning("Don't use this method any more, use AliLog instead");
954 AliLog::SetClassDebugLevel("AliRun", level);