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"
67 #include "AliTPCTrackHitsInterfaces.h"
74 //_______________________________________________________________________
89 fPDGDB(0), //Particle factory object
90 fConfigFunction("\0"),
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):
118 fPDGDB(0), //Particle factory object
119 fConfigFunction("\0"),
125 // Copy constructor for AliRun
130 //_____________________________________________________________________________
131 AliRun::AliRun(const char *name, const char *title):
137 fModules(new TObjArray(77)), // Support list for the Detectors
146 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
147 fConfigFunction("Config();"),
148 fRandom(new TRandom3()),
153 // Constructor for the main processor.
154 // Creates the geometry
155 // Creates the list of Detectors.
156 // Creates the list of particles.
161 // Set random number generator
164 if (gSystem->Getenv("CONFIG_SEED")) {
165 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
168 // Add to list of browsable
169 gROOT->GetListOfBrowsables()->Add(this,name);
170 // Create the TNode geometry for the event display
171 BuildSimpleGeometry();
173 // Create default mag field
176 // Add particle list to configuration
177 AliConfig::Instance()->Add(fPDGDB);
180 // Terrible hack to avoid problem with the initialisation of
181 // static and globals on Mac OS X
182 AliClassAliTrackHitsInfo p1=galiclass____AliClassAliTrackHitsInfo;
183 AliClassAliTrackHitsParam p2=galiclass____AliTrackHitsParam;
184 AliClassAliHitInfo p3=galiclass____AliHitInfo;
190 //_______________________________________________________________________
194 // Default AliRun destructor
196 gROOT->GetListOfBrowsables()->Remove(this);
200 TFolder* evfold = fRunLoader->GetEventFolder();
201 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
202 TIter next(fModules);
204 while((mod = (AliModule*)next()))
206 modfold->Remove(mod);
225 //_______________________________________________________________________
226 void AliRun::Copy(TObject &) const
228 AliFatal("Not implemented!");
231 //_______________________________________________________________________
235 // Initialize Alice geometry
240 //_______________________________________________________________________
241 void AliRun::BuildSimpleGeometry()
244 // Create a simple TNode geometry used by Root display engine
246 // Initialise geometry
248 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
249 new TMaterial("void","Vacuum",0,0,0); //Everything is void
250 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
251 brik->SetVisibility(0);
252 new TNode("alice","alice","S_alice");
255 //_______________________________________________________________________
256 void AliRun::CleanDetectors()
259 // Clean Detectors at the end of event
261 fRunLoader->CleanDetectors();
264 //_______________________________________________________________________
265 void AliRun::ResetHits()
270 //_______________________________________________________________________
271 AliGenerator* AliRun::Generator() const
273 return fMCApp->Generator();
276 //_______________________________________________________________________
277 void AliRun::SetField(AliMagF* magField)
280 // Set Magnetic Field Map
286 //_______________________________________________________________________
287 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
288 Float_t maxField, const char* filename)
291 // Set magnetic field parameters
292 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
293 // version Magnetic field map version (only 1 active now)
294 // scale Scale factor for the magnetic field
295 // maxField Maximum value for the magnetic field
298 // --- Sanity check on mag field flags
299 if(fField) delete fField;
301 fField = new AliMagFC("Map1"," ",type,scale,maxField);
302 } else if(version<=2) {
303 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
305 } else if(version==3) {
306 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
309 AliWarning(Form("Invalid map %d",version));
313 //_____________________________________________________________________________
315 void AliRun::InitLoaders()
317 //creates list of getters
319 TIter next(fModules);
321 while((mod = (AliModule*)next()))
323 mod->SetRunLoader(fRunLoader);
324 AliDetector *det = dynamic_cast<AliDetector*>(mod);
327 AliDebug(2, Form("Adding %s", det->GetName()));
328 fRunLoader->AddLoader(det);
333 //_____________________________________________________________________________
335 void AliRun::FinishRun()
338 // Called at the end of the run.
343 AliDebug(1, "Finish Lego");
344 fRunLoader->CdGAFile();
348 // Clean detector information
349 TIter next(fModules);
351 while((detector = dynamic_cast<AliModule*>(next()))) {
352 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
353 detector->FinishRun();
356 AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
357 fRunLoader->WriteHeader("OVERWRITE");
359 // Write AliRun info and all detectors parameters
360 fRunLoader->CdGAFile();
361 Write(0,TObject::kOverwrite);//write AliRun
362 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
364 // Clean tree information
365 AliDebug(1, "fRunLoader->Stack()->FinishRun()");
366 fRunLoader->Stack()->FinishRun();
368 if(fMCApp) fMCApp->FinishRun();
370 fRunLoader->Synchronize();
373 //_______________________________________________________________________
374 void AliRun::Announce() const
377 // Announce the current version of AliRoot
380 "****************************************************************\n");
381 printf("%6s","*");printf("%64s","*\n");
384 printf(" You are running AliRoot version NewIO\n");
387 printf(" The cvs tag for the current program is $Name$\n");
389 printf("%6s","*");printf("%64s","*\n");
391 "****************************************************************\n");
394 //_______________________________________________________________________
395 AliModule *AliRun::GetModule(const char *name) const
398 // Return pointer to detector from name
400 return dynamic_cast<AliModule*>(fModules->FindObject(name));
403 //_______________________________________________________________________
404 AliDetector *AliRun::GetDetector(const char *name) const
407 // Return pointer to detector from name
409 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
412 //_______________________________________________________________________
413 Int_t AliRun::GetModuleID(const char *name) const
416 // Return galice internal detector identifier from name
419 TObject *mod=fModules->FindObject(name);
420 if(mod) i=fModules->IndexOf(mod);
424 //_______________________________________________________________________
425 Int_t AliRun::GetEvent(Int_t event)
428 // Reloads data containers in folders # event
429 // Set branch addresses
431 if (fRunLoader == 0x0)
433 AliError("RunLoader is not set. Can not load data.");
436 /*****************************************/
437 /**** P R E R E L O A D I N G ****/
438 /*****************************************/
439 // Reset existing structures
441 fMCApp->ResetTrackReferences();
445 /*****************************************/
446 /**** R E L O A D ****/
447 /*****************************************/
449 fRunLoader->GetEvent(event);
451 /*****************************************/
452 /**** P O S T R E L O A D I N G ****/
453 /*****************************************/
455 // Set Trees branch addresses
456 TIter next(fModules);
458 while((detector = dynamic_cast<AliModule*>(next())))
460 detector->SetTreeAddress();
463 return fRunLoader->GetHeader()->GetNtrack();
466 //_______________________________________________________________________
467 TGeometry *AliRun::GetGeometry()
470 // Import Alice geometry from current file
471 // Return pointer to geometry object
473 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
475 // Unlink and relink nodes in detectors
476 // This is bad and there must be a better way...
479 TIter next(fModules);
481 while((detector = dynamic_cast<AliModule*>(next()))) {
482 TList *dnodes=detector->Nodes();
485 for ( j=0; j<dnodes->GetSize(); j++) {
486 node = dynamic_cast<TNode*>(dnodes->At(j));
487 node1 = fGeometry->GetNode(node->GetName());
488 dnodes->Remove(node);
489 dnodes->AddAt(node1,j);
495 //_______________________________________________________________________
496 void AliRun::SetBaseFile(const char *filename)
498 fBaseFileName = filename;
501 //_______________________________________________________________________
502 void AliRun::ResetDigits()
505 // Reset all Detectors digits
507 TIter next(fModules);
509 while((detector = dynamic_cast<AliModule*>(next()))) {
510 detector->ResetDigits();
514 //_______________________________________________________________________
515 void AliRun::ResetSDigits()
518 // Reset all Detectors digits
520 TIter next(fModules);
522 while((detector = dynamic_cast<AliModule*>(next()))) {
523 detector->ResetSDigits();
528 //_______________________________________________________________________
530 void AliRun::ResetPoints()
533 // Reset all Detectors points
535 TIter next(fModules);
537 while((detector = dynamic_cast<AliModule*>(next()))) {
538 detector->ResetPoints();
541 //_______________________________________________________________________
543 void AliRun::InitMC(const char *setup)
546 // Initialize ALICE Simulation run
551 AliWarning("Cannot initialise AliRun twice!");
556 fMCApp=new AliMC(GetName(),GetTitle());
558 gROOT->LoadMacro(setup);
559 gInterpreter->ProcessLine(fConfigFunction.Data());
561 fRunLoader->CdGAFile();
563 AliPDG::AddParticlesToPdgDataBase();
565 fNdets = fModules->GetLast()+1;
567 TIter next(fModules);
568 for(Int_t i=0; i<fNdets; ++i)
570 TObject *objfirst, *objlast;
571 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
572 objlast = gDirectory->GetList()->Last();
574 // Add Detector histograms in Detector list of histograms
575 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
576 else objfirst = gDirectory->GetList()->First();
579 detector->Histograms()->Add(objfirst);
580 objfirst = gDirectory->GetList()->After(objfirst);
586 //Must be here because some MCs (G4) adds detectors here and not in Config.C
588 fRunLoader->MakeTree("E");
591 fRunLoader->LoadKinematics("RECREATE");
592 fRunLoader->LoadTrackRefs("RECREATE");
593 fRunLoader->LoadHits("all","RECREATE");
597 // Save stuff at the beginning of the file to avoid file corruption
598 fRunLoader->CdGAFile();
600 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
603 //_______________________________________________________________________
605 void AliRun::RunMC(Int_t nevent, const char *setup)
608 // Main function to be called to process a galice run
610 // Root > gAlice.Run();
611 // a positive number of events will cause the finish routine
614 fEventsPerRun = nevent;
615 // check if initialisation has been done
616 if (!fInitDone) InitMC(setup);
618 // Create the Root Tree with one branch per detector
619 //Hits moved to begin event -> now we are crating separate tree for each event
621 gMC->ProcessRun(nevent);
623 // End of this run, close files
624 if(nevent>0) FinishRun();
627 //_______________________________________________________________________
628 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
631 // Main function to be called to reconstruct Alice event
633 Int_t nev = fRunLoader->GetNumberOfEvents();
634 AliDebug(1, Form("Found %d events", nev));
635 Int_t nFirst = first;
636 Int_t nLast = (last < 0)? nev : last;
638 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
639 AliDebug(1, Form("Processing event %d", nevent));
641 Digits2Reco(selected);
645 //_______________________________________________________________________
647 void AliRun::Hits2Digits(const char *selected)
650 // Convert Hits to sumable digits
652 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
654 Hits2SDigits(selected);
655 SDigits2Digits(selected);
660 //_______________________________________________________________________
662 void AliRun::Tree2Tree(Option_t *option, const char *selected)
665 // Function to transform the content of
667 // - TreeH to TreeS (option "S")
668 // - TreeS to TreeD (option "D")
669 // - TreeD to TreeR (option "R")
671 // If multiple options are specified ("SDR"), transformation will be done in sequence for
672 // selected detector and for all detectors if none is selected (detector string
673 // can contain blank separated list of detector names).
676 const char *oS = strstr(option,"S");
677 const char *oD = strstr(option,"D");
678 const char *oR = strstr(option,"R");
680 TObjArray *detectors = Detectors();
682 TIter next(detectors);
684 AliDetector *detector = 0;
686 while((detector = dynamic_cast<AliDetector*>(next()))) {
688 if (strcmp(detector->GetName(),selected)) continue;
689 if (detector->IsActive())
692 AliLoader* loader = detector->GetLoader();
693 if (loader == 0x0) continue;
697 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
698 loader->LoadHits("read");
699 if (loader->TreeS() == 0x0) loader->MakeTree("S");
700 detector->MakeBranch(option);
701 detector->SetTreeAddress();
702 detector->Hits2SDigits();
703 loader->UnloadHits();
704 loader->UnloadSDigits();
708 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
709 loader->LoadSDigits("read");
710 if (loader->TreeD() == 0x0) loader->MakeTree("D");
711 detector->MakeBranch(option);
712 detector->SetTreeAddress();
713 detector->SDigits2Digits();
714 loader->UnloadSDigits();
715 loader->UnloadDigits();
719 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
720 loader->LoadDigits("read");
721 if (loader->TreeR() == 0x0) loader->MakeTree("R");
722 detector->MakeBranch(option);
723 detector->SetTreeAddress();
724 detector->Digits2Reco();
725 loader->UnloadDigits();
726 loader->UnloadRecPoints();
733 //_______________________________________________________________________
734 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
735 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
736 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
739 // Generates lego plots of:
740 // - radiation length map phi vs theta
741 // - radiation length map phi vs eta
742 // - interaction length map
743 // - g/cm2 length map
745 // ntheta bins in theta, eta
746 // themin minimum angle in theta (degrees)
747 // themax maximum angle in theta (degrees)
749 // phimin minimum angle in phi (degrees)
750 // phimax maximum angle in phi (degrees)
751 // rmin minimum radius
752 // rmax maximum radius
755 // The number of events generated = ntheta*nphi
756 // run input parameters in macro setup (default="Config.C")
758 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
761 <img src="picts/AliRunLego1.gif">
766 <img src="picts/AliRunLego2.gif">
771 <img src="picts/AliRunLego3.gif">
776 // check if initialisation has been done
777 // If runloader has been initialized, set the number of events per file to nc1 * nc2
780 if (!gener) gener = new AliLegoGenerator();
782 // Configure Generator
783 gener->SetRadiusRange(rmin, rmax);
784 gener->SetZMax(zmax);
785 gener->SetCoor1Range(nc1, c1min, c1max);
786 gener->SetCoor2Range(nc2, c2min, c2max);
790 fLego = new AliLego("lego",gener);
792 if (!fInitDone) InitMC(setup);
793 //Save current generator
795 AliGenerator *gen=fMCApp->Generator();
796 fMCApp->ResetGenerator(gener);
797 //Prepare MC for Lego Run
802 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
803 //gMC->ProcessRun(nc1*nc2+1);
804 gMC->ProcessRun(nc1*nc2);
806 // End of this run, close files
808 // Restore current generator
809 fMCApp->ResetGenerator(gen);
810 // Delete Lego Object
811 delete fLego; fLego=0;
814 //_______________________________________________________________________
815 void AliRun::SetConfigFunction(const char * config)
818 // Set the signature of the function contained in Config.C to configure
821 fConfigFunction=config;
828 //_______________________________________________________________________
829 void AliRun::Field(const Double_t* x, Double_t *b) const
832 // Return the value of the magnetic field
835 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
839 Field()->Field(xfloat,bfloat);
840 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
843 AliError("No mag field defined!");
849 // End of MC Application
852 //_______________________________________________________________________
853 void AliRun::Streamer(TBuffer &R__b)
855 // Stream an object of class AliRun.
857 if (R__b.IsReading()) {
858 if (!gAlice) gAlice = this;
859 AliRun::Class()->ReadBuffer(R__b, this);
860 gROOT->GetListOfBrowsables()->Add(this,"Run");
864 AliRun::Class()->WriteBuffer(R__b, this);
867 //_______________________________________________________________________
869 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
871 fRunLoader->GetHeader()->SetGenEventHeader(header);
873 //_______________________________________________________________________
875 Int_t AliRun::GetEvNumber() const
877 //Returns number of current event
878 if (fRunLoader == 0x0)
880 AliError("RunLoader is not set. Can not load data.");
884 return fRunLoader->GetEventNumber();
886 //_______________________________________________________________________
888 void AliRun::SetRunLoader(AliRunLoader* rloader)
891 // Set the loader of the run
893 fRunLoader = rloader;
894 if (fRunLoader == 0x0) return;
897 TFolder* evfold = fRunLoader->GetEventFolder();
898 if (evfold) evfoldname = evfold->GetName();
899 else AliWarning("Did not get Event Folder from Run Loader");
901 if ( fRunLoader->GetAliRun() )
902 {//if alrun already exists in folder
903 if (fRunLoader->GetAliRun() != this )
904 {//and is different than this - crash
905 AliFatal("AliRun is already in Folder and it is not this object");
911 evfold->Add(this);//Post this AliRun to Folder
914 TIter next(fModules);
916 while((module = (AliModule*)next()))
918 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
919 module->SetRunLoader(fRunLoader);
920 AliDetector* detector = dynamic_cast<AliDetector*>(module);
923 AliLoader* loader = fRunLoader->GetLoader(detector);
926 AliError(Form("Can not get loader for detector %s", detector->GetName()));
930 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
931 detector->SetLoader(loader);
937 void AliRun::AddModule(AliModule* mod)
940 // Add a module to the module list
942 if (mod == 0x0) return;
943 if (strlen(mod->GetName()) == 0) return;
944 if (GetModuleID(mod->GetName()) >= 0) return;
946 AliDebug(1, mod->GetName());
947 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
948 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
956 //_______________________________________________________________________
957 Int_t AliRun::GetDebug() const
959 AliWarning("Don't use this method any more, use AliDebug instead");
960 return AliDebugLevel();
963 //_______________________________________________________________________
964 void AliRun::SetDebug(Int_t level)
966 AliWarning("Don't use this method any more, use AliLog instead");
967 AliLog::SetClassDebugLevel("AliRun", level);