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>
52 #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 //_______________________________________________________________________
89 fPDGDB(0), //Particle factory object
90 fConfigFunction("\0"),
95 // Default constructor for AliRun
97 AliConfig::Instance();//skowron 29 Feb 2002
98 //ensures that the folder structure is build
101 //_______________________________________________________________________
102 AliRun::AliRun(const AliRun& arun):
119 fPDGDB(0), //Particle factory object
120 fConfigFunction("\0"),
125 // Copy constructor for AliRun
130 //_____________________________________________________________________________
131 AliRun::AliRun(const char *name, const char *title):
138 fModules(new TObjArray(77)), // Support list for the Detectors
148 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
149 fConfigFunction("Config();"),
150 fRandom(new TRandom3()),
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);
183 //_______________________________________________________________________
187 // Default AliRun destructor
189 gROOT->GetListOfBrowsables()->Remove(this);
193 TFolder* evfold = fRunLoader->GetEventFolder();
194 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
195 TIter next(fModules);
197 while((mod = (AliModule*)next()))
199 modfold->Remove(mod);
218 //_______________________________________________________________________
219 void AliRun::Copy(TObject &) const
221 Fatal("Copy","Not implemented!\n");
224 //_______________________________________________________________________
228 // Initialize Alice geometry
233 //_______________________________________________________________________
234 void AliRun::BuildSimpleGeometry()
237 // Create a simple TNode geometry used by Root display engine
239 // Initialise geometry
241 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
242 new TMaterial("void","Vacuum",0,0,0); //Everything is void
243 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
244 brik->SetVisibility(0);
245 new TNode("alice","alice","S_alice");
248 //_______________________________________________________________________
249 void AliRun::CleanDetectors()
252 // Clean Detectors at the end of event
254 fRunLoader->CleanDetectors();
257 //_______________________________________________________________________
258 void AliRun::ResetHits()
263 //_______________________________________________________________________
264 AliGenerator* AliRun::Generator() const
266 return fMCApp->Generator();
269 //_______________________________________________________________________
270 void AliRun::SetField(AliMagF* magField)
272 // Set Magnetic Field Map
277 //_______________________________________________________________________
278 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
279 Float_t maxField, const char* filename)
282 // Set magnetic field parameters
283 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
284 // version Magnetic field map version (only 1 active now)
285 // scale Scale factor for the magnetic field
286 // maxField Maximum value for the magnetic field
289 // --- Sanity check on mag field flags
290 if(fField) delete fField;
292 fField = new AliMagFC("Map1"," ",type,scale,maxField);
293 } else if(version<=2) {
294 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
296 } else if(version==3) {
297 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
300 Warning("SetField","Invalid map %d\n",version);
304 //_____________________________________________________________________________
306 void AliRun::InitLoaders()
308 //creates list of getters
309 if (GetDebug()) Info("InitLoaders","");
310 TIter next(fModules);
312 while((mod = (AliModule*)next()))
314 mod->SetRunLoader(fRunLoader);
315 AliDetector *det = dynamic_cast<AliDetector*>(mod);
318 if (GetDebug()) Info("InitLoaders"," Adding %s ",det->GetName());
319 fRunLoader->AddLoader(det);
322 if (GetDebug()) Info("InitLoaders","Done");
324 //_____________________________________________________________________________
326 void AliRun::FinishRun()
329 // Called at the end of the run.
334 if (GetDebug()) Info("FinishRun"," Finish Lego");
335 fRunLoader->CdGAFile();
339 // Clean detector information
340 TIter next(fModules);
342 while((detector = dynamic_cast<AliModule*>(next()))) {
343 if (GetDebug()) Info("FinishRun"," %s->FinishRun()",detector->GetName());
344 detector->FinishRun();
347 if (GetDebug()) Info("FinishRun"," fRunLoader->WriteHeader(OVERWRITE)");
348 fRunLoader->WriteHeader("OVERWRITE");
350 // Write AliRun info and all detectors parameters
351 fRunLoader->CdGAFile();
352 Write(0,TObject::kOverwrite);//write AliRun
353 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
355 // Clean tree information
356 if (GetDebug()) Info("FinishRun"," fRunLoader->Stack()->FinishRun()");
357 fRunLoader->Stack()->FinishRun();
359 if(fMCApp) fMCApp->FinishRun();
361 fRunLoader->Synchronize();
364 //_______________________________________________________________________
365 void AliRun::Announce() const
368 // Announce the current version of AliRoot
371 "****************************************************************\n");
372 printf("%6s","*");printf("%64s","*\n");
375 printf(" You are running AliRoot version NewIO\n");
378 printf(" The cvs tag for the current program is $Name$\n");
380 printf("%6s","*");printf("%64s","*\n");
382 "****************************************************************\n");
385 //_______________________________________________________________________
386 AliModule *AliRun::GetModule(const char *name) const
389 // Return pointer to detector from name
391 return dynamic_cast<AliModule*>(fModules->FindObject(name));
394 //_______________________________________________________________________
395 AliDetector *AliRun::GetDetector(const char *name) const
398 // Return pointer to detector from name
400 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
403 //_______________________________________________________________________
404 Int_t AliRun::GetModuleID(const char *name) const
407 // Return galice internal detector identifier from name
410 TObject *mod=fModules->FindObject(name);
411 if(mod) i=fModules->IndexOf(mod);
415 //_______________________________________________________________________
416 Int_t AliRun::GetEvent(Int_t event)
419 // Reloads data containers in folders # event
420 // Set branch addresses
422 if (fRunLoader == 0x0)
424 Error("GetEvent","RunLoader is not set. Can not load data.");
427 /*****************************************/
428 /**** P R E R E L O A D I N G ****/
429 /*****************************************/
430 // Reset existing structures
432 fMCApp->ResetTrackReferences();
436 /*****************************************/
437 /**** R E L O A D ****/
438 /*****************************************/
440 fRunLoader->GetEvent(event);
442 /*****************************************/
443 /**** P O S T R E L O A D I N G ****/
444 /*****************************************/
446 // Set Trees branch addresses
447 TIter next(fModules);
449 while((detector = dynamic_cast<AliModule*>(next())))
451 detector->SetTreeAddress();
454 return fRunLoader->GetHeader()->GetNtrack();
457 //_______________________________________________________________________
458 TGeometry *AliRun::GetGeometry()
461 // Import Alice geometry from current file
462 // Return pointer to geometry object
464 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
466 // Unlink and relink nodes in detectors
467 // This is bad and there must be a better way...
470 TIter next(fModules);
472 while((detector = dynamic_cast<AliModule*>(next()))) {
473 TList *dnodes=detector->Nodes();
476 for ( j=0; j<dnodes->GetSize(); j++) {
477 node = dynamic_cast<TNode*>(dnodes->At(j));
478 node1 = fGeometry->GetNode(node->GetName());
479 dnodes->Remove(node);
480 dnodes->AddAt(node1,j);
486 //_______________________________________________________________________
487 void AliRun::SetBaseFile(const char *filename)
489 fBaseFileName = filename;
492 //_______________________________________________________________________
493 void AliRun::ResetDigits()
496 // Reset all Detectors digits
498 TIter next(fModules);
500 while((detector = dynamic_cast<AliModule*>(next()))) {
501 detector->ResetDigits();
505 //_______________________________________________________________________
506 void AliRun::ResetSDigits()
509 // Reset all Detectors digits
511 TIter next(fModules);
513 while((detector = dynamic_cast<AliModule*>(next()))) {
514 detector->ResetSDigits();
519 //_______________________________________________________________________
521 void AliRun::ResetPoints()
524 // Reset all Detectors points
526 TIter next(fModules);
528 while((detector = dynamic_cast<AliModule*>(next()))) {
529 detector->ResetPoints();
532 //_______________________________________________________________________
534 void AliRun::InitMC(const char *setup)
537 // Initialize ALICE Simulation run
542 Warning("Init","Cannot initialise AliRun twice!\n");
546 fMCApp=new AliMC(GetName(),GetTitle());
548 gROOT->LoadMacro(setup);
549 gInterpreter->ProcessLine(fConfigFunction.Data());
551 fRunLoader->CdGAFile();
553 AliPDG::AddParticlesToPdgDataBase();
555 fNdets = fModules->GetLast()+1;
557 // Added also after in case of interactive initialisation of modules
558 fNdets = fModules->GetLast()+1;
560 TIter next(fModules);
561 for(Int_t i=0; i<fNdets; ++i)
563 TObject *objfirst, *objlast;
564 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
565 objlast = gDirectory->GetList()->Last();
567 // Add Detector histograms in Detector list of histograms
568 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
569 else objfirst = gDirectory->GetList()->First();
572 detector->Histograms()->Add(objfirst);
573 objfirst = gDirectory->GetList()->After(objfirst);
579 //Must be here because some MCs (G4) adds detectors here and not in Config.C
581 fRunLoader->MakeTree("E");
584 fRunLoader->LoadKinematics("RECREATE");
585 fRunLoader->LoadTrackRefs("RECREATE");
586 fRunLoader->LoadHits("all","RECREATE");
590 // Save stuff at the beginning of the file to avoid file corruption
591 fRunLoader->CdGAFile();
593 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
596 //_______________________________________________________________________
598 void AliRun::RunMC(Int_t nevent, const char *setup)
601 // Main function to be called to process a galice run
603 // Root > gAlice.Run();
604 // a positive number of events will cause the finish routine
607 fEventsPerRun = nevent;
608 // check if initialisation has been done
609 if (!fInitDone) InitMC(setup);
611 // Create the Root Tree with one branch per detector
612 //Hits moved to begin event -> now we are crating separate tree for each event
614 gMC->ProcessRun(nevent);
616 // End of this run, close files
617 if(nevent>0) FinishRun();
620 //_______________________________________________________________________
621 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
624 // Main function to be called to reconstruct Alice event
626 Int_t nev = fRunLoader->GetNumberOfEvents();
627 if (GetDebug()) Info("RunReco","Found %d events",nev);
628 Int_t nFirst = first;
629 Int_t nLast = (last < 0)? nev : last;
631 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
632 if (GetDebug()) Info("RunReco","Processing event %d",nevent);
634 Digits2Reco(selected);
638 //_______________________________________________________________________
640 void AliRun::Hits2Digits(const char *selected)
643 // Convert Hits to sumable digits
645 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
647 Hits2SDigits(selected);
648 SDigits2Digits(selected);
653 //_______________________________________________________________________
655 void AliRun::Tree2Tree(Option_t *option, const char *selected)
658 // Function to transform the content of
660 // - TreeH to TreeS (option "S")
661 // - TreeS to TreeD (option "D")
662 // - TreeD to TreeR (option "R")
664 // If multiple options are specified ("SDR"), transformation will be done in sequence for
665 // selected detector and for all detectors if none is selected (detector string
666 // can contain blank separated list of detector names).
669 const char *oS = strstr(option,"S");
670 const char *oD = strstr(option,"D");
671 const char *oR = strstr(option,"R");
673 TObjArray *detectors = Detectors();
675 TIter next(detectors);
677 AliDetector *detector = 0;
679 while((detector = dynamic_cast<AliDetector*>(next()))) {
681 if (strcmp(detector->GetName(),selected)) continue;
682 if (detector->IsActive())
685 AliLoader* loader = detector->GetLoader();
686 if (loader == 0x0) continue;
690 if (GetDebug()) Info("Tree2Tree","Processing Hits2SDigits for %s ...",detector->GetName());
691 loader->LoadHits("read");
692 if (loader->TreeS() == 0x0) loader->MakeTree("S");
693 detector->MakeBranch(option);
694 detector->SetTreeAddress();
695 detector->Hits2SDigits();
696 loader->UnloadHits();
697 loader->UnloadSDigits();
701 if (GetDebug()) Info("Tree2Tree","Processing SDigits2Digits for %s ...",detector->GetName());
702 loader->LoadSDigits("read");
703 if (loader->TreeD() == 0x0) loader->MakeTree("D");
704 detector->MakeBranch(option);
705 detector->SetTreeAddress();
706 detector->SDigits2Digits();
707 loader->UnloadSDigits();
708 loader->UnloadDigits();
712 if (GetDebug()) Info("Tree2Tree","Processing Digits2Reco for %s ...",detector->GetName());
713 loader->LoadDigits("read");
714 if (loader->TreeR() == 0x0) loader->MakeTree("R");
715 detector->MakeBranch(option);
716 detector->SetTreeAddress();
717 detector->Digits2Reco();
718 loader->UnloadDigits();
719 loader->UnloadRecPoints();
726 //_______________________________________________________________________
727 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
728 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
729 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
732 // Generates lego plots of:
733 // - radiation length map phi vs theta
734 // - radiation length map phi vs eta
735 // - interaction length map
736 // - g/cm2 length map
738 // ntheta bins in theta, eta
739 // themin minimum angle in theta (degrees)
740 // themax maximum angle in theta (degrees)
742 // phimin minimum angle in phi (degrees)
743 // phimax maximum angle in phi (degrees)
744 // rmin minimum radius
745 // rmax maximum radius
748 // The number of events generated = ntheta*nphi
749 // run input parameters in macro setup (default="Config.C")
751 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
754 <img src="picts/AliRunLego1.gif">
759 <img src="picts/AliRunLego2.gif">
764 <img src="picts/AliRunLego3.gif">
769 // check if initialisation has been done
770 // If runloader has been initialized, set the number of events per file to nc1 * nc2
773 if (!gener) gener = new AliLegoGenerator();
775 // Configure Generator
776 gener->SetRadiusRange(rmin, rmax);
777 gener->SetZMax(zmax);
778 gener->SetCoor1Range(nc1, c1min, c1max);
779 gener->SetCoor2Range(nc2, c2min, c2max);
783 fLego = new AliLego("lego",gener);
785 if (!fInitDone) InitMC(setup);
786 //Save current generator
788 AliGenerator *gen=fMCApp->Generator();
789 fMCApp->ResetGenerator(gener);
790 //Prepare MC for Lego Run
795 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
796 //gMC->ProcessRun(nc1*nc2+1);
797 gMC->ProcessRun(nc1*nc2);
799 // End of this run, close files
801 // Restore current generator
802 fMCApp->ResetGenerator(gen);
803 // Delete Lego Object
804 delete fLego; fLego=0;
807 //_______________________________________________________________________
808 void AliRun::SetConfigFunction(const char * config)
811 // Set the signature of the function contained in Config.C to configure
814 fConfigFunction=config;
821 //_______________________________________________________________________
822 void AliRun::Field(const Double_t* x, Double_t *b) const
825 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
829 Field()->Field(xfloat,bfloat);
830 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
833 printf("No mag field defined!\n");
840 // End of MC Application
843 //_______________________________________________________________________
844 void AliRun::Streamer(TBuffer &R__b)
846 // Stream an object of class AliRun.
848 if (R__b.IsReading()) {
849 if (!gAlice) gAlice = this;
850 AliRun::Class()->ReadBuffer(R__b, this);
851 gROOT->GetListOfBrowsables()->Add(this,"Run");
855 AliRun::Class()->WriteBuffer(R__b, this);
858 //_______________________________________________________________________
860 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
862 fRunLoader->GetHeader()->SetGenEventHeader(header);
864 //_______________________________________________________________________
866 Int_t AliRun::GetEvNumber() const
868 //Returns number of current event
869 if (fRunLoader == 0x0)
871 Error("GetEvent","RunLoader is not set. Can not load data.");
875 return fRunLoader->GetEventNumber();
877 //_______________________________________________________________________
879 void AliRun::SetRunLoader(AliRunLoader* rloader)
881 fRunLoader = rloader;
882 if (fRunLoader == 0x0) return;
885 TFolder* evfold = fRunLoader->GetEventFolder();
886 if (evfold) evfoldname = evfold->GetName();
887 else Warning("SetRunLoader","Did not get Event Folder from Run Loader");
889 if ( fRunLoader->GetAliRun() )
890 {//if alrun already exists in folder
891 if (fRunLoader->GetAliRun() != this )
892 {//and is different than this - crash
893 Fatal("AliRun","AliRun is already in Folder and it is not this object");
899 evfold->Add(this);//Post this AliRun to Folder
902 TIter next(fModules);
904 while((module = (AliModule*)next()))
906 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
907 module->SetRunLoader(fRunLoader);
908 AliDetector* detector = dynamic_cast<AliDetector*>(module);
911 AliLoader* loader = fRunLoader->GetLoader(detector);
914 Error("SetRunLoader","Can not get loader for detector %s",detector->GetName());
918 if (GetDebug()) Info("SetRunLoader","Setting loader for detector %s",detector->GetName());
919 detector->SetLoader(loader);
925 void AliRun::AddModule(AliModule* mod)
927 if (mod == 0x0) return;
928 if (strlen(mod->GetName()) == 0) return;
929 if (GetModuleID(mod->GetName()) >= 0) return;
931 if (GetDebug()) Info("AddModule","%s",mod->GetName());
932 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
933 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());