]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliRun.cxx
use AliLog message scheme
[u/mrichter/AliRoot.git] / STEER / AliRun.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
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.                                     //
24 //                                                                           //
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.                                            //
30 //Begin_Html
31 /*
32 <img src="picts/AliRunClass.gif">
33 */
34 //End_Html
35 //Begin_Html
36 /*
37 <img src="picts/alirun.gif">
38 */
39 //End_Html
40 //                                                                           //
41 ///////////////////////////////////////////////////////////////////////////////
42
43 #include <TBRIK.h> 
44 #include <TCint.h> 
45 #include <TDatabasePDG.h>
46 #include <TGeometry.h>
47 #include <TNode.h>
48 #include <TROOT.h>
49 #include <TRandom3.h>
50 #include <TSystem.h>
51 #include <TVirtualMC.h>
52 // 
53 #include "AliLog.h"
54 #include "AliDetector.h"
55 #include "AliDisplay.h"
56 #include "AliHeader.h"
57 #include "AliLego.h"
58 #include "AliLegoGenerator.h"
59 #include "AliMC.h"
60 #include "AliMagFC.h"
61 #include "AliMagFCM.h"
62 #include "AliMagFDM.h"
63 #include "AliPDG.h"
64 #include "AliRun.h"
65 #include "AliStack.h"
66
67 AliRun *gAlice;
68
69
70 ClassImp(AliRun)
71
72 //_______________________________________________________________________
73 AliRun::AliRun():
74   fRun(0),
75   fEvent(0),
76   fEventNrInRun(0),
77   fEventsPerRun(0),
78   fModules(0),
79   fGeometry(0),
80   fMCApp(0),
81   fDisplay(0),
82   fField(0),
83   fMC(0),
84   fNdets(0),
85   fInitDone(kFALSE),
86   fLego(0),
87   fPDGDB(0),  //Particle factory object
88   fConfigFunction("\0"),
89   fRandom(0),
90   fRunLoader(0x0)
91 {
92   //
93   // Default constructor for AliRun
94   //
95   AliConfig::Instance();//skowron 29 Feb 2002
96                         //ensures that the folder structure is build
97 }
98
99 //_______________________________________________________________________
100 AliRun::AliRun(const AliRun& arun):
101   TNamed(arun),
102   fRun(0),
103   fEvent(0),
104   fEventNrInRun(0),
105   fEventsPerRun(0),
106   fModules(0),
107   fGeometry(0),
108   fMCApp(0),
109   fDisplay(0),
110   fField(0),
111   fMC(0),
112   fNdets(0),
113   fInitDone(kFALSE),
114   fLego(0),
115   fPDGDB(0),  //Particle factory object
116   fConfigFunction("\0"),
117   fRandom(0),
118   fRunLoader(0x0)
119 {
120   //
121   // Copy constructor for AliRun
122   //
123   arun.Copy(*this);
124 }
125
126 //_____________________________________________________________________________
127 AliRun::AliRun(const char *name, const char *title):
128   TNamed(name,title),
129   fRun(0),
130   fEvent(0),
131   fEventNrInRun(0),
132   fEventsPerRun(0),
133   fModules(new TObjArray(77)), // Support list for the Detectors
134   fGeometry(0),
135   fMCApp(0),
136   fDisplay(0),
137   fField(0),
138   fMC(gMC),
139   fNdets(0),
140   fInitDone(kFALSE),
141   fLego(0),
142   fPDGDB(TDatabasePDG::Instance()),        //Particle factory object!
143   fConfigFunction("Config();"),
144   fRandom(new TRandom3()),
145   fRunLoader(0x0)
146 {
147   //
148   //  Constructor for the main processor.
149   //  Creates the geometry
150   //  Creates the list of Detectors.
151   //  Creates the list of particles.
152   //
153
154   gAlice     = this;
155
156   // Set random number generator
157   gRandom = fRandom;
158
159   if (gSystem->Getenv("CONFIG_SEED")) {
160      gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
161   }
162
163   // Add to list of browsable  
164   gROOT->GetListOfBrowsables()->Add(this,name);
165   // Create the TNode geometry for the event display
166   BuildSimpleGeometry();
167   
168   // Create default mag field
169   SetField();
170
171   // Add particle list to configuration
172   AliConfig::Instance()->Add(fPDGDB); 
173
174 }
175
176
177 //_______________________________________________________________________
178 AliRun::~AliRun()
179 {
180   //
181   // Default AliRun destructor
182   //
183   gROOT->GetListOfBrowsables()->Remove(this);
184
185   if (fRunLoader)
186    {
187     TFolder* evfold = fRunLoader->GetEventFolder();
188     TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
189     TIter next(fModules);
190     AliModule *mod;
191     while((mod = (AliModule*)next()))
192      { 
193        modfold->Remove(mod);
194      }
195    }
196   
197   
198   delete fField;
199   delete fMCApp;
200   delete gMC; gMC=0;
201   delete fGeometry;
202   delete fDisplay;
203   delete fLego;
204   if (fModules) {
205     fModules->Delete();
206     delete fModules;
207   }
208   
209   delete fPDGDB;
210 }
211
212 //_______________________________________________________________________
213 void AliRun::Copy(TObject &) const
214 {
215   AliFatal("Not implemented!");
216 }
217
218 //_______________________________________________________________________
219 void AliRun::Build()
220 {
221   //
222   // Initialize Alice geometry
223   // Dummy routine
224   //
225 }
226  
227 //_______________________________________________________________________
228 void AliRun::BuildSimpleGeometry()
229 {
230   //
231   // Create a simple TNode geometry used by Root display engine
232   //
233   // Initialise geometry
234   //
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");
240 }
241
242 //_______________________________________________________________________
243 void AliRun::CleanDetectors()
244 {
245   //
246   // Clean Detectors at the end of event
247   //
248    fRunLoader->CleanDetectors();
249 }
250
251 //_______________________________________________________________________
252 void AliRun::ResetHits() 
253 {
254   fMCApp->ResetHits();
255 }
256
257 //_______________________________________________________________________
258 AliGenerator* AliRun::Generator() const 
259 {
260   return fMCApp->Generator();
261 }
262
263 //_______________________________________________________________________
264 void  AliRun::SetField(AliMagF* magField)
265 {
266   //
267   // Set Magnetic Field Map
268   //
269   fField = magField;
270   fField->ReadField();
271 }
272
273 //_______________________________________________________________________
274 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
275                       Float_t maxField, const char* filename)
276 {
277   //
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
283
284   //
285   // --- Sanity check on mag field flags
286   if(fField) delete fField;
287   if(version==1) {
288     fField = new AliMagFC("Map1"," ",type,scale,maxField);
289   } else if(version<=2) {
290     fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
291     fField->ReadField();
292   } else if(version==3) {
293     fField = new AliMagFDM("Map4",filename,type,scale,maxField);
294     fField->ReadField();
295   } else {
296     AliWarning(Form("Invalid map %d",version));
297   }
298 }
299
300 //_____________________________________________________________________________
301
302 void AliRun::InitLoaders()
303 {
304   //creates list of getters
305   AliDebug(1, "");
306   TIter next(fModules);
307   AliModule *mod;
308   while((mod = (AliModule*)next()))
309    { 
310      mod->SetRunLoader(fRunLoader);
311      AliDetector *det = dynamic_cast<AliDetector*>(mod);
312      if (det) 
313       {
314         AliDebug(2, Form("Adding %s", det->GetName()));
315         fRunLoader->AddLoader(det);
316       }
317    }
318   AliDebug(1, "Done");
319 }
320 //_____________________________________________________________________________
321
322 void AliRun::FinishRun()
323 {
324   //
325   // Called at the end of the run.
326   //
327   
328   if(fLego) 
329    {
330     AliDebug(1, "Finish Lego");
331     fRunLoader->CdGAFile();
332     fLego->FinishRun();
333    }
334   
335   // Clean detector information
336   TIter next(fModules);
337   AliModule *detector;
338   while((detector = dynamic_cast<AliModule*>(next()))) {
339     AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
340     detector->FinishRun();
341   }
342   
343   AliDebug(1, "fRunLoader->WriteHeader(OVERWRITE)");
344   fRunLoader->WriteHeader("OVERWRITE");
345
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
350   
351   // Clean tree information
352   AliDebug(1, "fRunLoader->Stack()->FinishRun()");
353   fRunLoader->Stack()->FinishRun();
354
355   if(fMCApp) fMCApp->FinishRun();
356
357   fRunLoader->Synchronize();
358 }
359
360 //_______________________________________________________________________
361 void AliRun::Announce() const
362 {
363   //
364   // Announce the current version of AliRoot
365   //
366   printf("%70s",
367          "****************************************************************\n");
368   printf("%6s","*");printf("%64s","*\n");
369
370   printf("%6s","*");
371   printf("    You are running AliRoot version NewIO\n");
372
373   printf("%6s","*");
374   printf("    The cvs tag for the current program is $Name$\n");
375
376   printf("%6s","*");printf("%64s","*\n");
377   printf("%70s",
378          "****************************************************************\n");
379 }
380
381 //_______________________________________________________________________
382 AliModule *AliRun::GetModule(const char *name) const
383 {
384   //
385   // Return pointer to detector from name
386   //
387   return dynamic_cast<AliModule*>(fModules->FindObject(name));
388 }
389  
390 //_______________________________________________________________________
391 AliDetector *AliRun::GetDetector(const char *name) const
392 {
393   //
394   // Return pointer to detector from name
395   //
396   return dynamic_cast<AliDetector*>(fModules->FindObject(name));
397 }
398  
399 //_______________________________________________________________________
400 Int_t AliRun::GetModuleID(const char *name) const
401 {
402   //
403   // Return galice internal detector identifier from name
404   //
405   Int_t i=-1;
406   TObject *mod=fModules->FindObject(name);
407   if(mod) i=fModules->IndexOf(mod);
408   return i;
409 }
410  
411 //_______________________________________________________________________
412 Int_t AliRun::GetEvent(Int_t event)
413 {
414 //
415 // Reloads data containers in folders # event
416 // Set branch addresses
417 //
418   if (fRunLoader == 0x0)
419    {
420      AliError("RunLoader is not set. Can not load data.");
421      return -1;
422    }
423 /*****************************************/ 
424 /****   P R E    R E L O A D I N G    ****/
425 /*****************************************/ 
426 // Reset existing structures
427   fMCApp->ResetHits();
428   fMCApp->ResetTrackReferences();
429   ResetDigits();
430   ResetSDigits();
431
432 /*****************************************/ 
433 /****       R  E  L  O  A  D          ****/
434 /*****************************************/
435
436   fRunLoader->GetEvent(event);
437
438 /*****************************************/ 
439 /****  P O S T    R E L O A D I N G   ****/
440 /*****************************************/ 
441
442   // Set Trees branch addresses
443   TIter next(fModules);
444   AliModule *detector;
445   while((detector = dynamic_cast<AliModule*>(next()))) 
446    {
447      detector->SetTreeAddress();
448    }
449  
450   return fRunLoader->GetHeader()->GetNtrack();
451 }
452
453 //_______________________________________________________________________
454 TGeometry *AliRun::GetGeometry()
455 {
456   //
457   // Import Alice geometry from current file
458   // Return pointer to geometry object
459   //
460   if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
461   //
462   // Unlink and relink nodes in detectors
463   // This is bad and there must be a better way...
464   //
465   
466   TIter next(fModules);
467   AliModule *detector;
468   while((detector = dynamic_cast<AliModule*>(next()))) {
469     TList *dnodes=detector->Nodes();
470     Int_t j;
471     TNode *node, *node1;
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);
477     }
478   }
479   return fGeometry;
480 }
481
482 //_______________________________________________________________________
483 void AliRun::SetBaseFile(const char *filename)
484 {
485   fBaseFileName = filename;
486 }
487
488 //_______________________________________________________________________
489 void AliRun::ResetDigits()
490 {
491   //
492   //  Reset all Detectors digits
493   //
494   TIter next(fModules);
495   AliModule *detector;
496   while((detector = dynamic_cast<AliModule*>(next()))) {
497      detector->ResetDigits();
498   }
499 }
500
501 //_______________________________________________________________________
502 void AliRun::ResetSDigits()
503 {
504   //
505   //  Reset all Detectors digits
506   //
507   TIter next(fModules);
508   AliModule *detector;
509   while((detector = dynamic_cast<AliModule*>(next()))) {
510      detector->ResetSDigits();
511   }
512 }
513
514
515 //_______________________________________________________________________
516
517 void AliRun::ResetPoints()
518 {
519   //
520   // Reset all Detectors points
521   //
522   TIter next(fModules);
523   AliModule *detector;
524   while((detector = dynamic_cast<AliModule*>(next()))) {
525      detector->ResetPoints();
526   }
527 }
528 //_______________________________________________________________________
529
530 void AliRun::InitMC(const char *setup)
531 {
532   //
533   // Initialize ALICE Simulation run
534   //
535   Announce();
536
537   if(fInitDone) {
538     AliWarning("Cannot initialise AliRun twice!");
539     return;
540   }
541     
542   if (!fMCApp)  
543     fMCApp=new AliMC(GetName(),GetTitle());
544     
545   gROOT->LoadMacro(setup);
546   gInterpreter->ProcessLine(fConfigFunction.Data());
547
548   fRunLoader->CdGAFile();
549
550   AliPDG::AddParticlesToPdgDataBase();  
551
552   fNdets = fModules->GetLast()+1;
553
554   TIter next(fModules);
555   for(Int_t i=0; i<fNdets; ++i)
556    {
557      TObject *objfirst, *objlast;
558      AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
559      objlast = gDirectory->GetList()->Last();
560       
561      // Add Detector histograms in Detector list of histograms
562      if (objlast) objfirst = gDirectory->GetList()->After(objlast);
563      else         objfirst = gDirectory->GetList()->First();
564      while (objfirst) 
565       {
566         detector->Histograms()->Add(objfirst);
567         objfirst = gDirectory->GetList()->After(objfirst);
568       }
569    }
570    
571    fMCApp->Init();
572    
573    //Must be here because some MCs (G4) adds detectors here and not in Config.C
574    InitLoaders();
575    fRunLoader->MakeTree("E");
576    if (fLego == 0x0)
577     {
578       fRunLoader->LoadKinematics("RECREATE");
579       fRunLoader->LoadTrackRefs("RECREATE");
580       fRunLoader->LoadHits("all","RECREATE");
581     }
582    fInitDone = kTRUE;
583    //
584    // Save stuff at the beginning of the file to avoid file corruption
585    fRunLoader->CdGAFile();
586    Write();
587    fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
588 }
589
590 //_______________________________________________________________________
591
592 void AliRun::RunMC(Int_t nevent, const char *setup)
593 {
594   //
595   // Main function to be called to process a galice run
596   // example
597   //    Root > gAlice.Run(); 
598   // a positive number of events will cause the finish routine
599   // to be called
600   //
601   fEventsPerRun = nevent;
602   // check if initialisation has been done
603   if (!fInitDone) InitMC(setup);
604   
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
607
608   gMC->ProcessRun(nevent);
609
610   // End of this run, close files
611   if(nevent>0) FinishRun();
612 }
613
614 //_______________________________________________________________________
615 void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
616 {
617   //
618   // Main function to be called to reconstruct Alice event
619   // 
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;
624    
625    for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
626      AliDebug(1, Form("Processing event %d", nevent));
627      GetEvent(nevent);
628      Digits2Reco(selected);
629    }
630 }
631
632 //_______________________________________________________________________
633
634 void AliRun::Hits2Digits(const char *selected)
635 {
636
637    // Convert Hits to sumable digits
638    // 
639    for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
640      GetEvent(nevent);
641      Hits2SDigits(selected);
642      SDigits2Digits(selected);
643    }  
644 }
645
646
647 //_______________________________________________________________________
648
649 void AliRun::Tree2Tree(Option_t *option, const char *selected)
650 {
651   //
652   // Function to transform the content of
653   //  
654   // - TreeH to TreeS (option "S")
655   // - TreeS to TreeD (option "D")
656   // - TreeD to TreeR (option "R")
657   // 
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). 
661
662
663    const char *oS = strstr(option,"S");
664    const char *oD = strstr(option,"D");
665    const char *oR = strstr(option,"R");
666    
667    TObjArray *detectors = Detectors();
668
669    TIter next(detectors);
670
671    AliDetector *detector = 0;
672
673    while((detector = dynamic_cast<AliDetector*>(next()))) {
674      if (selected) 
675        if (strcmp(detector->GetName(),selected)) continue;
676      if (detector->IsActive())
677       { 
678        
679        AliLoader* loader = detector->GetLoader();
680        if (loader == 0x0) continue;
681        
682        if (oS) 
683         {
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();
692         }  
693        if (oD) 
694         {
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();
703         } 
704        if (oR) 
705         {
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();
714
715         }
716      }   
717    }
718 }
719
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)
724 {
725   //
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
731   //
732   //  ntheta    bins in theta, eta
733   //  themin    minimum angle in theta (degrees)
734   //  themax    maximum angle in theta (degrees)
735   //  nphi      bins in phi
736   //  phimin    minimum angle in phi (degrees)
737   //  phimax    maximum angle in phi (degrees)
738   //  rmin      minimum radius
739   //  rmax      maximum radius
740   //  
741   //
742   //  The number of events generated = ntheta*nphi
743   //  run input parameters in macro setup (default="Config.C")
744   //
745   //  Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
746   //Begin_Html
747   /*
748     <img src="picts/AliRunLego1.gif">
749   */
750   //End_Html
751   //Begin_Html
752   /*
753     <img src="picts/AliRunLego2.gif">
754   */
755   //End_Html
756   //Begin_Html
757   /*
758     <img src="picts/AliRunLego3.gif">
759   */
760   //End_Html
761   //
762
763   // check if initialisation has been done
764   // If runloader has been initialized, set the number of events per file to nc1 * nc2
765
766   // Set new generator
767   if (!gener) gener  = new AliLegoGenerator();
768   //
769   // Configure Generator
770   gener->SetRadiusRange(rmin, rmax);
771   gener->SetZMax(zmax);
772   gener->SetCoor1Range(nc1, c1min, c1max);
773   gener->SetCoor2Range(nc2, c2min, c2max);
774   
775   
776   //Create Lego object  
777   fLego = new AliLego("lego",gener);
778
779   if (!fInitDone) InitMC(setup);
780   //Save current generator
781   
782   AliGenerator *gen=fMCApp->Generator();
783   fMCApp->ResetGenerator(gener);
784   //Prepare MC for Lego Run
785   gMC->InitLego();
786   
787   //Run Lego Object
788
789   if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
790   //gMC->ProcessRun(nc1*nc2+1);
791   gMC->ProcessRun(nc1*nc2);
792   
793   // End of this run, close files
794   FinishRun();
795   // Restore current generator
796   fMCApp->ResetGenerator(gen);
797   // Delete Lego Object
798   delete fLego; fLego=0;
799 }
800
801 //_______________________________________________________________________
802 void AliRun::SetConfigFunction(const char * config) 
803 {
804   //
805   // Set the signature of the function contained in Config.C to configure
806   // the run
807   //
808   fConfigFunction=config;
809 }
810
811 // 
812 // MC Application
813 // 
814
815 //_______________________________________________________________________
816 void AliRun::Field(const Double_t* x, Double_t *b) const
817 {
818   //
819   // Return the value of the magnetic field
820   //
821   Float_t xfloat[3];
822   for (Int_t i=0; i<3; i++) xfloat[i] = x[i]; 
823   
824   if (Field()) {
825     Float_t bfloat[3];
826     Field()->Field(xfloat,bfloat);
827     for (Int_t j=0; j<3; j++) b[j] = bfloat[j]; 
828   } 
829   else {
830     AliError("No mag field defined!");
831     b[0]=b[1]=b[2]=0.;
832   }
833 }      
834
835 // 
836 // End of MC Application
837 // 
838
839 //_______________________________________________________________________
840 void AliRun::Streamer(TBuffer &R__b)
841 {
842   // Stream an object of class AliRun.
843
844   if (R__b.IsReading()) {
845     if (!gAlice) gAlice = this;
846     AliRun::Class()->ReadBuffer(R__b, this);
847     gROOT->GetListOfBrowsables()->Add(this,"Run");
848
849     gRandom = fRandom;
850   } else {
851     AliRun::Class()->WriteBuffer(R__b, this);
852   }
853 }
854 //_______________________________________________________________________
855
856 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
857 {
858   fRunLoader->GetHeader()->SetGenEventHeader(header);
859 }
860 //_______________________________________________________________________
861
862 Int_t AliRun::GetEvNumber() const
863
864 //Returns number of current event  
865   if (fRunLoader == 0x0)
866    {
867      AliError("RunLoader is not set. Can not load data.");
868      return -1;
869    }
870
871   return fRunLoader->GetEventNumber();
872 }
873 //_______________________________________________________________________
874
875 void AliRun::SetRunLoader(AliRunLoader* rloader)
876 {
877   //
878   // Set the loader of the run
879   //
880   fRunLoader = rloader;
881   if (fRunLoader == 0x0) return;
882   
883   TString evfoldname;
884   TFolder* evfold = fRunLoader->GetEventFolder();
885   if (evfold) evfoldname = evfold->GetName();
886   else AliWarning("Did not get Event Folder from Run Loader");
887   
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");
893        return;//pro forma
894      }//else do nothing
895    }
896   else
897    {
898      evfold->Add(this);//Post this AliRun to Folder
899    }
900   
901   TIter next(fModules);
902   AliModule *module;
903   while((module = (AliModule*)next())) 
904    {
905      if (evfold) AliConfig::Instance()->Add(module,evfoldname);
906      module->SetRunLoader(fRunLoader);
907      AliDetector* detector = dynamic_cast<AliDetector*>(module);
908      if (detector)
909       {
910         AliLoader* loader = fRunLoader->GetLoader(detector);
911         if (loader == 0x0)
912          {
913            AliError(Form("Can not get loader for detector %s", detector->GetName()));
914          }
915         else
916          {
917            AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
918            detector->SetLoader(loader);
919          }
920       }
921    }
922 }
923
924 void AliRun::AddModule(AliModule* mod)
925 {
926   //
927   // Add a module to the module list
928   //
929   if (mod == 0x0) return;
930   if (strlen(mod->GetName()) == 0) return;
931   if (GetModuleID(mod->GetName()) >= 0) return;
932   
933   AliDebug(1, mod->GetName());
934   if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
935   else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
936
937   Modules()->Add(mod);
938   
939   fNdets++;
940 }
941
942
943 //_______________________________________________________________________
944 Int_t AliRun::GetDebug() const
945 {
946   AliWarning("Don't use this method any more, use AliDebug instead");
947   return AliDebugLevel();
948 }
949
950 //_______________________________________________________________________
951 void AliRun::SetDebug(Int_t level)
952 {
953   AliWarning("Don't use this method any more, use AliLog instead");
954   AliLog::SetClassDebugLevel("AliRun", level);
955 }