]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliRun.cxx
Lego generator for phi-z binning.
[u/mrichter/AliRoot.git] / STEER / AliRun.cxx
index 7d892b0b531599b32220462fe471bc596a28bbe9..00f0d7f8b92fb6e7888c361264ea5ce57f643905 100644 (file)
@@ -1,3 +1,95 @@
+/**************************************************************************
+ * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ *                                                                        *
+ * Author: The ALICE Off-line Project.                                    *
+ * Contributors are mentioned in the code where appropriate.              *
+ *                                                                        *
+ * Permission to use, copy, modify and distribute this software and its   *
+ * documentation strictly for non-commercial purposes is hereby granted   *
+ * without fee, provided that the above copyright notice appears in all   *
+ * copies and that both the copyright notice and this permission notice   *
+ * appear in the supporting documentation. The authors make no claims     *
+ * about the suitability of this software for any purpose. It is          *
+ * provided "as is" without express or implied warranty.                  *
+ **************************************************************************/
+
+/*
+$Log$
+Revision 1.43  2000/10/09 09:43:17  fca
+Special remapping of hits for TPC and TRD. End-of-primary action introduced
+
+Revision 1.42  2000/10/02 21:28:14  fca
+Removal of useless dependecies via forward declarations
+
+Revision 1.41  2000/07/13 16:19:09  fca
+Mainly coding conventions + some small bug fixes
+
+Revision 1.40  2000/07/12 08:56:25  fca
+Coding convention correction and warning removal
+
+Revision 1.39  2000/07/11 18:24:59  fca
+Coding convention corrections + few minor bug fixes
+
+Revision 1.38  2000/06/20 13:05:45  fca
+Writing down the TREE headers before job starts
+
+Revision 1.37  2000/06/09 20:05:11  morsch
+Introduce possibility to chose magnetic field version 3: AliMagFDM + field02.dat
+
+Revision 1.36  2000/06/08 14:03:58  hristov
+Only one initializer for a default argument
+
+Revision 1.35  2000/06/07 10:13:14  hristov
+Delete only existent objects.
+
+Revision 1.34  2000/05/18 10:45:38  fca
+Delete Particle Factory properly
+
+Revision 1.33  2000/05/16 13:10:40  fca
+New method IsNewTrack and fix for a problem in Father-Daughter relations
+
+Revision 1.32  2000/04/27 10:38:21  fca
+Correct termination of Lego Run and introduce Lego getter in AliRun
+
+Revision 1.31  2000/04/26 10:17:32  fca
+Changes in Lego for G4 compatibility
+
+Revision 1.30  2000/04/18 19:11:40  fca
+Introduce variable Config.C function signature
+
+Revision 1.29  2000/04/07 11:12:34  fca
+G4 compatibility changes
+
+Revision 1.28  2000/04/05 06:51:06  fca
+Workaround for an HP compiler problem
+
+Revision 1.27  2000/03/22 18:08:07  fca
+Rationalisation of the virtual MC interfaces
+
+Revision 1.26  2000/03/22 13:42:26  fca
+SetGenerator does not replace an existing generator, ResetGenerator does
+
+Revision 1.25  2000/02/23 16:25:22  fca
+AliVMC and AliGeant3 classes introduced
+ReadEuclid moved from AliRun to AliModule
+
+Revision 1.24  2000/01/19 17:17:20  fca
+Introducing a list of lists of hits -- more hits allowed for detector now
+
+Revision 1.23  1999/12/03 11:14:31  fca
+Fixing previous wrong checking
+
+Revision 1.21  1999/11/25 10:40:08  fca
+Fixing daughters information also in primary tracks
+
+Revision 1.20  1999/10/04 18:08:49  fca
+Adding protection against inconsistent Euclid files
+
+Revision 1.19  1999/09/29 07:50:40  fca
+Introduction of the Copyright and cvs Log
+
+*/
+
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
 //  Control class for Alice C++                                              //
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 #include <TFile.h>
 #include <TRandom.h>
 #include <TBRIK.h> 
 #include <TCint.h> 
 #include <TSystem.h>
 #include <TObjectTable.h>
+#include <TTree.h>
+#include <TGeometry.h>
+#include <TROOT.h>
+#include "TBrowser.h"
 
 #include "TParticle.h"
 #include "AliRun.h"
 #include "AliDisplay.h"
+#include "AliMC.h"
+#include "AliLego.h"
+#include "AliMagFC.h"
+#include "AliMagFCM.h"
+#include "AliMagFDM.h"
+#include "AliHit.h"
 
-#include "AliCallf77.h" 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-AliRun *gAlice;
-
-static AliHeader *header;
-
-#ifndef WIN32
+#include "AliDetector.h"
 
-# define rxgtrak rxgtrak_
-# define rxstrak rxstrak_
-# define rxkeep  rxkeep_ 
-# define rxouth  rxouth_
-#else
-
-# define rxgtrak RXGTRAK 
-# define rxstrak RXSTRAK 
-# define rxkeep  RXKEEP  
-# define rxouth  RXOUTH
-#endif
+AliRun *gAlice;
 
-static TArrayF sEventEnergy;
-static TArrayF sSummEnergy;
-static TArrayF sSum2Energy;
+static AliHeader *gAliHeader;
 
 ClassImp(AliRun)
 
@@ -71,7 +155,7 @@ AliRun::AliRun()
   //
   // Default constructor for AliRun
   //
-  header=&fHeader;
+  gAliHeader=&fHeader;
   fRun       = 0;
   fEvent     = 0;
   fCurrent   = -1;
@@ -86,7 +170,7 @@ AliRun::AliRun()
   fGeometry  = 0;
   fDisplay   = 0;
   fField     = 0;
-  fMC        = 0;
+  fMC       = 0;
   fNdets     = 0;
   fImedia    = 0;
   fTrRmax    = 1.e10;
@@ -94,6 +178,8 @@ AliRun::AliRun()
   fInitDone  = kFALSE;
   fLego      = 0;
   fPDGDB     = 0;        //Particle factory object!
+  fHitLists  = 0;
+  fConfigFunction    = "\0";
 }
 
 //_____________________________________________________________________________
@@ -120,6 +206,7 @@ AliRun::AliRun(const char *name, const char *title)
   fInitDone  = kFALSE;
   fLego      = 0;
   fField     = 0;
+  fConfigFunction    = "Config();";
   
   gROOT->GetListOfBrowsables()->Add(this,name);
   //
@@ -134,7 +221,7 @@ AliRun::AliRun(const char *name, const char *title)
   fNtrack=0;
   fHgwmk=0;
   fCurrent=-1;
-  header=&fHeader;
+  gAliHeader=&fHeader;
   fRun       = 0;
   fEvent     = 0;
   //
@@ -154,8 +241,12 @@ AliRun::AliRun(const char *name, const char *title)
   //
   // Make particles
   fPDGDB     = TDatabasePDG::Instance();        //Particle factory object!
+  //
+  // Create HitLists list
+  fHitLists  = new TList();
 }
 
+
 //_____________________________________________________________________________
 AliRun::~AliRun()
 {
@@ -182,6 +273,8 @@ AliRun::~AliRun()
     fParticles->Delete();
     delete fParticles;
   }
+  delete fHitLists;
+  delete fPDGDB;
 }
 
 //_____________________________________________________________________________
@@ -274,7 +367,7 @@ void AliRun::CleanParents()
   int i;
   for(i=0; i<fNtrack; i++) {
     part = (TParticle *)particles.UncheckedAt(i);
-    if(!part->TestBit(Daughters_Bit)) {
+    if(!part->TestBit(kDaughtersBit)) {
       part->SetFirstDaughter(-1);
       part->SetLastDaughter(-1);
     }
@@ -292,7 +385,7 @@ Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
 }
 
 //_____________________________________________________________________________
-void AliRun::DumpPart (Int_t i)
+void AliRun::DumpPart (Int_t i) const
 {
   //
   // Dumps particle i in the stack
@@ -302,7 +395,7 @@ void AliRun::DumpPart (Int_t i)
 }
 
 //_____________________________________________________________________________
-void AliRun::DumpPStack ()
+void AliRun::DumpPStack () const
 {
   //
   // Dumps the particle stack
@@ -341,9 +434,12 @@ void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
   if(fField) delete fField;
   if(version==1) {
     fField = new AliMagFC("Map1"," ",type,version,scale,maxField);
-  } else if(version<=3) {
+  } else if(version<=2) {
     fField = new AliMagFCM("Map2-3",filename,type,version,scale,maxField);
     fField->ReadField();
+  } else if(version==3) {
+    fField = new AliMagFDM("Map4",filename,type,version,scale,maxField);
+    fField->ReadField();
   } else {
     Warning("SetField","Invalid map %d\n",version);
   }
@@ -371,7 +467,13 @@ void AliRun::FinishPrimary()
   //  static Int_t count=0;
   //  const Int_t times=10;
   // This primary is finished, purify stack
-  gAlice->PurifyKine();
+  PurifyKine();
+
+  TIter next(fModules);
+  AliModule *detector;
+  while((detector = (AliModule*)next())) {
+    detector->FinishPrimary();
+  }
 
   // Write out hits if any
   if (gAlice->TreeH()) {
@@ -392,13 +494,16 @@ void AliRun::FinishEvent()
   // Called at the end of the event.
   //
   
+  //
+  if(fLego) fLego->FinishEvent();
+
   //Update the energy deposit tables
   Int_t i;
-  for(i=0;i<sEventEnergy.GetSize();i++) {
-    sSummEnergy[i]+=sEventEnergy[i];
-    sSum2Energy[i]+=sEventEnergy[i]*sEventEnergy[i];
+  for(i=0;i<fEventEnergy.GetSize();i++) {
+    fSummEnergy[i]+=fEventEnergy[i];
+    fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
   }
-  sEventEnergy.Reset();
+  fEventEnergy.Reset();
   
   // Clean detector information
   CleanDetectors();
@@ -430,13 +535,15 @@ void AliRun::FinishEvent()
   //  Int_t ievent = fHeader.GetEvent();
   //  char hname[30];
   //  sprintf(hname,"TreeK%d",ievent);
-  if (fTreeK) fTreeK->Write();
+  if (fTreeK) fTreeK->Write(0,TObject::kOverwrite);
   //  sprintf(hname,"TreeH%d",ievent);
-  if (fTreeH) fTreeH->Write();
+  if (fTreeH) fTreeH->Write(0,TObject::kOverwrite);
   //  sprintf(hname,"TreeD%d",ievent);
-  if (fTreeD) fTreeD->Write();
+  if (fTreeD) fTreeD->Write(0,TObject::kOverwrite);
   //  sprintf(hname,"TreeR%d",ievent);
-  if (fTreeR) fTreeR->Write();
+  if (fTreeR) fTreeR->Write(0,TObject::kOverwrite);
+
+  ++fEvent;
 }
 
 //_____________________________________________________________________________
@@ -446,6 +553,9 @@ void AliRun::FinishRun()
   // Called at the end of the run.
   //
 
+  //
+  if(fLego) fLego->FinishRun();
+
   // Clean detector information
   TIter next(fModules);
   AliModule *detector;
@@ -457,31 +567,40 @@ void AliRun::FinishRun()
   EnergySummary();
   
   // file is retrieved from whatever tree
-  TFile *File = 0;
-  if (fTreeK) File = fTreeK->GetCurrentFile();
-  if ((!File) && (fTreeH)) File = fTreeH->GetCurrentFile();
-  if ((!File) && (fTreeD)) File = fTreeD->GetCurrentFile();
-  if ((!File) && (fTreeE)) File = fTreeE->GetCurrentFile();
-  if( NULL==File ) {
+  TFile *file = 0;
+  if (fTreeK) file = fTreeK->GetCurrentFile();
+  if ((!file) && (fTreeH)) file = fTreeH->GetCurrentFile();
+  if ((!file) && (fTreeD)) file = fTreeD->GetCurrentFile();
+  if ((!file) && (fTreeE)) file = fTreeE->GetCurrentFile();
+  if( NULL==file ) {
     Error("FinishRun","There isn't root file!");
     exit(1);
   }
-  File->cd();
-  fTreeE->Write();
+  file->cd();
+  fTreeE->Write(0,TObject::kOverwrite);
   
   // Clean tree information
-  delete fTreeK; fTreeK = 0;
-  delete fTreeH; fTreeH = 0;
-  delete fTreeD; fTreeD = 0;
-  delete fTreeR; fTreeR = 0;
-  delete fTreeE; fTreeE = 0;
+  if (fTreeK) {
+    delete fTreeK; fTreeK = 0;
+  }
+  if (fTreeH) {
+    delete fTreeH; fTreeH = 0;
+  }
+  if (fTreeD) {
+    delete fTreeD; fTreeD = 0;
+  }
+  if (fTreeR) {
+    delete fTreeR; fTreeR = 0;
+  }
+  if (fTreeE) {
+    delete fTreeE; fTreeE = 0;
+  }
   
   // Write AliRun info and all detectors parameters
   Write();
   
   // Close output file
-  File->Write();
-  File->Close();
+  file->Write();
 }
 
 //_____________________________________________________________________________
@@ -498,10 +617,10 @@ void AliRun::FlagTrack(Int_t track)
     particle=(TParticle*)fParticles->UncheckedAt(curr);
     
     // If the particle is flagged the three from here upward is saved already
-    if(particle->TestBit(Keep_Bit)) return;
+    if(particle->TestBit(kKeepBit)) return;
     
     // Save this particle
-    particle->SetBit(Keep_Bit);
+    particle->SetBit(kKeepBit);
     
     // Move to father if any
     if((curr=particle->GetFirstMother())==-1) return;
@@ -519,25 +638,25 @@ void AliRun::EnergySummary()
   Float_t edtot=0;
   Float_t ed, ed2;
   Int_t kn, i, left, j, id;
-  const Float_t zero=0;
+  const Float_t kzero=0;
   Int_t ievent=fHeader.GetEvent()+1;
   //
   // Energy loss information
   if(ievent) {
     printf("***************** Energy Loss Information per event (GEV) *****************\n");
-    for(kn=1;kn<sEventEnergy.GetSize();kn++) {
-      ed=sSummEnergy[kn];
+    for(kn=1;kn<fEventEnergy.GetSize();kn++) {
+      ed=fSummEnergy[kn];
       if(ed>0) {
-       sEventEnergy[ndep]=kn;
+       fEventEnergy[ndep]=kn;
        if(ievent>1) {
          ed=ed/ievent;
-         ed2=sSum2Energy[kn];
+         ed2=fSum2Energy[kn];
          ed2=ed2/ievent;
-         ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,zero))/ed;
+         ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
        } else 
          ed2=99;
-       sSummEnergy[ndep]=ed;
-       sSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,zero));
+       fSummEnergy[ndep]=ed;
+       fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,kzero));
        edtot+=ed;
        ndep++;
       }
@@ -546,8 +665,8 @@ void AliRun::EnergySummary()
       left=ndep-kn*3;
       for(i=0;i<(3<left?3:left);i++) {
        j=kn*3+i;
-        id=Int_t (sEventEnergy[j]+0.1);
-       printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),sSummEnergy[j],sSum2Energy[j]);
+        id=Int_t (fEventEnergy[j]+0.1);
+       printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
       }
       printf("\n");
     }
@@ -559,8 +678,8 @@ void AliRun::EnergySummary()
       left=ndep-kn*5;
       for(i=0;i<(5<left?5:left);i++) {
        j=kn*5+i;
-        id=Int_t (sEventEnergy[j]+0.1);
-       printf(" %s %10.3f%%;",gMC->VolName(id),100*sSummEnergy[j]/edtot);
+        id=Int_t (fEventEnergy[j]+0.1);
+       printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
       }
       printf("\n");
     }
@@ -569,13 +688,13 @@ void AliRun::EnergySummary()
   }
   //
   // Reset the TArray's
-  sEventEnergy.Set(0);
-  sSummEnergy.Set(0);
-  sSum2Energy.Set(0);
+  //  fEventEnergy.Set(0);
+  //  fSummEnergy.Set(0);
+  //  fSum2Energy.Set(0);
 }
 
 //_____________________________________________________________________________
-AliModule *AliRun::GetModule(const char *name)
+AliModule *AliRun::GetModule(const char *name) const
 {
   //
   // Return pointer to detector from name
@@ -584,7 +703,7 @@ AliModule *AliRun::GetModule(const char *name)
 }
  
 //_____________________________________________________________________________
-AliDetector *AliRun::GetDetector(const char *name)
+AliDetector *AliRun::GetDetector(const char *name) const
 {
   //
   // Return pointer to detector from name
@@ -593,7 +712,7 @@ AliDetector *AliRun::GetDetector(const char *name)
 }
  
 //_____________________________________________________________________________
-Int_t AliRun::GetModuleID(const char *name)
+Int_t AliRun::GetModuleID(const char *name) const
 {
   //
   // Return galice internal detector identifier from name
@@ -711,7 +830,7 @@ void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
   TParticle *track;
   for(Int_t i=fNtrack-1; i>=0; i--) {
     track=(TParticle*) fParticles->UncheckedAt(i);
-    if(!track->TestBit(Done_Bit)) {
+    if(!track->TestBit(kDoneBit)) {
       //
       // The track has not yet been processed
       fCurrent=i;
@@ -728,7 +847,7 @@ void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
       polar[1]=pol.Y();
       polar[2]=pol.Z();
       tof=track->T();
-      track->SetBit(Done_Bit);
+      track->SetBit(kDoneBit);
       break;
     }
   }
@@ -746,7 +865,7 @@ void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
 }
 
 //_____________________________________________________________________________
-Int_t AliRun::GetPrimary(Int_t track)
+Int_t AliRun::GetPrimary(Int_t track) const
 {
   //
   // return number of primary that has generated track
@@ -764,14 +883,19 @@ Int_t AliRun::GetPrimary(Int_t track)
 }
  
 //_____________________________________________________________________________
-void AliRun::Init(const char *setup)
+void AliRun::InitMC(const char *setup)
 {
   //
   // Initialize the Alice setup
   //
 
+  if(fInitDone) {
+    Warning("Init","Cannot initialise AliRun twice!\n");
+    return;
+  }
+
   gROOT->LoadMacro(setup);
-  gInterpreter->ProcessLine("Config();");
+  gInterpreter->ProcessLine(fConfigFunction.Data());
 
   gMC->DefineParticles();  //Create standard MC particles
 
@@ -780,19 +904,15 @@ void AliRun::Init(const char *setup)
   fNdets = fModules->GetLast()+1;
 
   //
-  //=================Create Materials, geometry, histograms, etc
+  //=================Create Materials and geometry
+  gMC->Init();
+
    TIter next(fModules);
    AliModule *detector;
    while((detector = (AliModule*)next())) {
       detector->SetTreeAddress();
       objlast = gDirectory->GetList()->Last();
       
-      // Initialise detector materials, geometry, histograms,etc
-      detector->CreateMaterials();
-      detector->CreateGeometry();
-      detector->BuildGeometry();
-      detector->Init();
-      
       // Add Detector histograms in Detector list of histograms
       if (objlast) objfirst = gDirectory->GetList()->After(objlast);
       else         objfirst = gDirectory->GetList()->First();
@@ -805,24 +925,22 @@ void AliRun::Init(const char *setup)
    
    MediaTable(); //Build the special IMEDIA table
    
-   //Close the geometry structure
-   gMC->Ggclos();
-   
    //Initialise geometry deposition table
-   sEventEnergy.Set(gMC->NofVolumes()+1);
-   sSummEnergy.Set(gMC->NofVolumes()+1);
-   sSum2Energy.Set(gMC->NofVolumes()+1);
-   
-   //Create the color table
-   gMC->SetColors();
+   fEventEnergy.Set(gMC->NofVolumes()+1);
+   fSummEnergy.Set(gMC->NofVolumes()+1);
+   fSum2Energy.Set(gMC->NofVolumes()+1);
    
    //Compute cross-sections
-   gMC->Gphysi();
+   gMC->BuildPhysics();
    
    //Write Geometry object to current file.
    fGeometry->Write();
    
    fInitDone = kTRUE;
+
+   //
+   // Save stuff at the beginning of the file to avoid file corruption
+   Write();
 }
 
 //_____________________________________________________________________________
@@ -891,6 +1009,22 @@ void AliRun::SetGenerator(AliGenerator *generator)
   if(!fGenerator) fGenerator = generator;
 }
 
+//____________________________________________________________________________
+void AliRun::ResetGenerator(AliGenerator *generator)
+{
+  //
+  // Load the event generator
+  //
+  if(fGenerator)
+    if(generator)
+      Warning("ResetGenerator","Replacing generator %s with %s\n",
+             fGenerator->GetName(),generator->GetName());
+    else
+      Warning("ResetGenerator","Replacing generator %s with NULL\n",
+             fGenerator->GetName());
+  fGenerator = generator;
+}
+
 //____________________________________________________________________________
 void AliRun::SetTransPar(char* filename)
 {
@@ -899,18 +1033,18 @@ void AliRun::SetTransPar(char* filename)
   //
 
 
-  const Int_t ncuts=10;
-  const Int_t nflags=11;
-  const Int_t npars=ncuts+nflags;
-  const char pars[npars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
+  const Int_t kncuts=10;
+  const Int_t knflags=11;
+  const Int_t knpars=kncuts+knflags;
+  const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
                               "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
                               "BREM","COMP","DCAY","DRAY","HADR","LOSS",
                               "MULS","PAIR","PHOT","RAYL"};
   char line[256];
   char detName[7];
   char* filtmp;
-  Float_t cut[ncuts];
-  Int_t flag[nflags];
+  Float_t cut[kncuts];
+  Int_t flag[knflags];
   Int_t i, itmed, iret, ktmed, kz;
   FILE *lun;
   //
@@ -930,8 +1064,8 @@ void AliRun::SetTransPar(char* filename)
   //
   while(1) {
     // Initialise cuts and flags
-    for(i=0;i<ncuts;i++) cut[i]=-99;
-    for(i=0;i<nflags;i++) flag[i]=-99;
+    for(i=0;i<kncuts;i++) cut[i]=-99;
+    for(i=0;i<knflags;i++) flag[i]=-99;
     itmed=0;
     for(i=0;i<256;i++) line[i]='\0';
     // Read up to the end of line excluded
@@ -971,19 +1105,19 @@ void AliRun::SetTransPar(char* filename)
          continue;
        }
        // Set energy thresholds
-       for(kz=0;kz<ncuts;kz++) {
+       for(kz=0;kz<kncuts;kz++) {
          if(cut[kz]>=0) {
            printf(" *  %-6s set to %10.3E for tracking medium code %4d for %s\n",
-                  pars[kz],cut[kz],itmed,mod->GetName());
-           gMC->Gstpar(ktmed,pars[kz],cut[kz]);
+                  kpars[kz],cut[kz],itmed,mod->GetName());
+           gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
          }
        }
        // Set transport mechanisms
-       for(kz=0;kz<nflags;kz++) {
+       for(kz=0;kz<knflags;kz++) {
          if(flag[kz]>=0) {
            printf(" *  %-6s set to %10d for tracking medium code %4d for %s\n",
-                  pars[ncuts+kz],flag[kz],itmed,mod->GetName());
-           gMC->Gstpar(ktmed,pars[ncuts+kz],Float_t(flag[kz]));
+                  kpars[kncuts+kz],flag[kz],itmed,mod->GetName());
+           gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
          }
        }
       } else {
@@ -1005,20 +1139,44 @@ void AliRun::MakeTree(Option_t *option)
   //  Loop on all detectors to create the Root branch (if any)
   //
 
+  char hname[30];
   //
   // Analyse options
-  char *K = strstr(option,"K");
-  char *H = strstr(option,"H");
-  char *E = strstr(option,"E");
-  char *D = strstr(option,"D");
-  char *R = strstr(option,"R");
-  //
-  if (K && !fTreeK) fTreeK = new TTree("TreeK0","Kinematics");
-  if (H && !fTreeH) fTreeH = new TTree("TreeH0","Hits");
-  if (D && !fTreeD) fTreeD = new TTree("TreeD0","Digits");
-  if (E && !fTreeE) fTreeE = new TTree("TE","Header");
-  if (R && !fTreeR) fTreeR = new TTree("TreeR0","Reconstruction");
-  if (fTreeH) fTreeH->SetAutoSave(1000000000); //no autosave
+  char *oK = strstr(option,"K");
+  char *oH = strstr(option,"H");
+  char *oE = strstr(option,"E");
+  char *oD = strstr(option,"D");
+  char *oR = strstr(option,"R");
+  //
+  if (oK && !fTreeK) {
+    sprintf(hname,"TreeK%d",fEvent);
+    fTreeK = new TTree(hname,"Kinematics");
+    //  Create a branch for particles
+    fTreeK->Branch("Particles",&fParticles,4000);
+    fTreeK->Write();
+  }
+  if (oH && !fTreeH) {
+    sprintf(hname,"TreeH%d",fEvent);
+    fTreeH = new TTree(hname,"Hits");
+    fTreeH->SetAutoSave(1000000000); //no autosave
+    fTreeH->Write();
+  }
+  if (oD && !fTreeD) {
+    sprintf(hname,"TreeD%d",fEvent);
+    fTreeD = new TTree(hname,"Digits");
+    fTreeD->Write();
+  }
+  if (oR && !fTreeR) {
+    sprintf(hname,"TreeR%d",fEvent);
+    fTreeR = new TTree(hname,"Reconstruction");
+    fTreeR->Write();
+  }
+  if (oE && !fTreeE) {
+    fTreeE = new TTree("TE","Header");
+    //  Create a branch for Header
+    fTreeE->Branch("Header","AliHeader",&gAliHeader,4000);
+    fTreeE->Write();
+  }
   //
   // Create a branch for hits/digits for each detector
   // Each branch is a TClonesArray. Each data member of the Hits classes
@@ -1026,13 +1184,8 @@ void AliRun::MakeTree(Option_t *option)
   TIter next(fModules);
   AliModule *detector;
   while((detector = (AliModule*)next())) {
-     if (H || D || R) detector->MakeBranch(option);
+     if (oH || oD || oR) detector->MakeBranch(option);
   }
-  //  Create a branch for particles
-  if (fTreeK && K) fTreeK->Branch("Particles",&fParticles,4000);
-  
-  //  Create a branch for Header
-  if (fTreeE && E) fTreeE->Branch("Header","AliHeader",&header,4000);
 }
 
 //_____________________________________________________________________________
@@ -1057,19 +1210,21 @@ void AliRun::PurifyKine()
   TClonesArray &particles = *fParticles;
   int nkeep=fHgwmk+1, parent, i;
   TParticle *part, *partnew, *father;
-  AliHit *OneHit;
   int *map = new int[particles.GetEntries()];
 
   // Save in Header total number of tracks before compression
   fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
 
-  // Preset map, to be removed later
+  // First pass, invalid Daughter information
   for(i=0; i<fNtrack; i++) {
-    if(i<=fHgwmk) map[i]=i ; else map[i] = -99 ;}
+    // Preset map, to be removed later
+    if(i<=fHgwmk) map[i]=i ; else map[i] = -99;
+    ((TParticle *)particles.UncheckedAt(i))->ResetBit(kDaughtersBit);
+  }
   // Second pass, build map between old and new numbering
   for(i=fHgwmk+1; i<fNtrack; i++) {
     part = (TParticle *)particles.UncheckedAt(i);
-    if(part->TestBit(Keep_Bit)) {
+    if(part->TestBit(kKeepBit)) {
       
       // This particle has to be kept
       map[i]=nkeep;
@@ -1077,7 +1232,9 @@ void AliRun::PurifyKine()
        
        // Old and new are different, have to copy
        partnew = (TParticle *)particles.UncheckedAt(nkeep);
-       *partnew = *part;
+       // Change due to a bug in the HP compiler
+       //      *partnew = *part;
+       memcpy(partnew,part,sizeof(TParticle));
       } else partnew = part;
       
       // as the parent is always *before*, it must be already
@@ -1092,23 +1249,27 @@ void AliRun::PurifyKine()
   fNtrack=nkeep;
   
   // Fix daughters information
-  for (i=fHgwmk+1; i<fNtrack; i++) {
+  for (i=0; i<fNtrack; i++) {
     part = (TParticle *)particles.UncheckedAt(i);
     parent = part->GetFirstMother();
-    father = (TParticle *)particles.UncheckedAt(parent);
-    if(father->TestBit(Daughters_Bit)) {
+    if(parent>=0) {
+      father = (TParticle *)particles.UncheckedAt(parent);
+      if(father->TestBit(kDaughtersBit)) {
       
-      if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
-      if(i>father->GetLastDaughter())  father->SetLastDaughter(i);
-    } else {
-      // Iitialise daughters info for first pass
-      father->SetFirstDaughter(i);
-      father->SetLastDaughter(i);
-      father->SetBit(Daughters_Bit);
+       if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
+       if(i>father->GetLastDaughter())  father->SetLastDaughter(i);
+      } else {
+       // Iitialise daughters info for first pass
+       father->SetFirstDaughter(i);
+       father->SetLastDaughter(i);
+       father->SetBit(kDaughtersBit);
+      }
     }
   }
   
+#ifdef old
   // Now loop on all detectors and reset the hits
+  AliHit *OneHit;
   TIter next(fModules);
   AliModule *detector;
   while((detector = (AliModule*)next())) {
@@ -1122,6 +1283,31 @@ void AliRun::PurifyKine()
       OneHit->SetTrack(map[OneHit->GetTrack()]);
     }
   }
+#else
+
+  // Now loop on all registered hit lists
+  TIter next(fHitLists);
+  TCollection *hitList;
+  while((hitList = (TCollection*)next())) {
+    TIter nexthit(hitList);
+    AliHit *hit;
+    while((hit = (AliHit*)nexthit())) {
+      hit->SetTrack(map[hit->GetTrack()]);
+    }
+  }
+#endif
+
+  // 
+  // This for detectors which have a special mapping mechanism
+  // for hits, such as TPC and TRD
+  //
+
+   TIter nextmod(fModules);
+   AliModule *detector;
+   while((detector = (AliModule*)nextmod())) {
+     detector->RemapTrackHitIDs(map);
+   }
+  
 
   fHgwmk=nkeep-1;
   particles.SetLast(fHgwmk);
@@ -1129,38 +1315,46 @@ void AliRun::PurifyKine()
 }
 
 //_____________________________________________________________________________
-void AliRun::Reset(Int_t run, Int_t idevent)
+void AliRun::BeginEvent()
 {
   //
   //  Reset all Detectors & kinematics & trees
   //
   char hname[30];
+  //
+
+  //
+  if(fLego) {
+    fLego->BeginEvent();
+    return;
+  }
+
   //
   ResetStack();
   ResetHits();
   ResetDigits();
 
   // Initialise event header
-  fHeader.Reset(run,idevent);
+  fHeader.Reset(fRun,fEvent);
 
   if(fTreeK) {
     fTreeK->Reset();
-    sprintf(hname,"TreeK%d",idevent);
+    sprintf(hname,"TreeK%d",fEvent);
     fTreeK->SetName(hname);
   }
   if(fTreeH) {
     fTreeH->Reset();
-    sprintf(hname,"TreeH%d",idevent);
+    sprintf(hname,"TreeH%d",fEvent);
     fTreeH->SetName(hname);
   }
   if(fTreeD) {
     fTreeD->Reset();
-    sprintf(hname,"TreeD%d",idevent);
+    sprintf(hname,"TreeD%d",fEvent);
     fTreeD->SetName(hname);
   }
   if(fTreeR) {
     fTreeR->Reset();
-    sprintf(hname,"TreeR%d",idevent);
+    sprintf(hname,"TreeR%d",fEvent);
     fTreeR->SetName(hname);
   }
 }
@@ -1205,7 +1399,7 @@ void AliRun::ResetPoints()
 }
 
 //_____________________________________________________________________________
-void AliRun::Run(Int_t nevent, const char *setup)
+void AliRun::RunMC(Int_t nevent, const char *setup)
 {
   //
   // Main function to be called to process a galice run
@@ -1215,34 +1409,22 @@ void AliRun::Run(Int_t nevent, const char *setup)
   // to be called
   //
 
-  Int_t i, todo;
   // check if initialisation has been done
-  if (!fInitDone) Init(setup);
+  if (!fInitDone) InitMC(setup);
   
   // Create the Root Tree with one branch per detector
-  if(!fEvent) {
-    gAlice->MakeTree("KHDER");
-  }
+  MakeTree("KHDER");
+
+  gMC->ProcessRun(nevent);
 
-  todo = TMath::Abs(nevent);
-  for (i=0; i<todo; i++) {
-  // Process one run (one run = one event)
-     gAlice->Reset(fRun, fEvent);
-     gMC->Gtrigi();
-     gMC->Gtrigc();
-     gMC->Gtrig();
-     gAlice->FinishEvent();
-     fEvent++;
-  }
-  
   // End of this run, close files
-  if(nevent>0) gAlice->FinishRun();
+  if(nevent>0) FinishRun();
 }
 
 //_____________________________________________________________________________
-void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
-                    Float_t themax,Int_t nphi,Float_t phimin,Float_t phimax,
-                    Float_t rmin,Float_t rmax,Float_t zmax)
+void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
+                    Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
+                    Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
 {
   //
   // Generates lego plots of:
@@ -1283,17 +1465,50 @@ void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
   //
 
   // check if initialisation has been done
-  if (!fInitDone) Init(setup);
+  if (!fInitDone) InitMC(setup);
+  //Save current generator
+  AliGenerator *gen=Generator();
+
+  // Set new generator
+  if (!gener) gener  = new AliLegoGenerator();
+  ResetGenerator(gener);
+  //
+  // Configure Generator
+  gener->SetRadiusRange(rmin, rmax);
+  gener->SetZMax(zmax);
+  gener->SetCoor1Range(nc1, c1min, c1max);
+  gener->SetCoor2Range(nc2, c2min, c2max);
+  
+  
+  //Create Lego object  
+  fLego = new AliLego("lego",gener);
+
+  //Prepare MC for Lego Run
+  gMC->InitLego();
   
-  fLego = new AliLego("lego","lego");
-  fLego->Init(ntheta,themin,themax,nphi,phimin,phimax,rmin,rmax,zmax);
-  fLego->Run();
+  //Run Lego Object
+
+  gMC->ProcessRun(nc1*nc2+1);
   
   // Create only the Root event Tree
-  gAlice->MakeTree("E");
+  MakeTree("E");
   
   // End of this run, close files
-  gAlice->FinishRun();
+  FinishRun();
+  // Restore current generator
+  ResetGenerator(gen);
+  // Delete Lego Object
+  delete fLego; fLego=0;
+}
+
+//_____________________________________________________________________________
+void AliRun::SetConfigFunction(const char * config) 
+{
+  //
+  // Set the signature of the function contained in Config.C to configure
+  // the run
+  //
+  fConfigFunction=config;
 }
 
 //_____________________________________________________________________________
@@ -1308,7 +1523,7 @@ void AliRun::SetCurrentTrack(Int_t track)
 //_____________________________________________________________________________
 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
                      Float_t *vpos, Float_t *polar, Float_t tof,
-                     const char *mecha, Int_t &ntr, Float_t weight)
+                     const char* /* mecha */, Int_t &ntr, Float_t weight)
 { 
   //
   // Load a track on the stack
@@ -1327,9 +1542,9 @@ void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
   TClonesArray &particles = *fParticles;
   TParticle *particle;
   Float_t mass;
-  const Int_t firstdaughter=-1;
-  const Int_t lastdaughter=-1;
-  const Int_t KS=0;
+  const Int_t kfirstdaughter=-1;
+  const Int_t klastdaughter=-1;
+  const Int_t kS=0;
   //  const Float_t tlife=0;
   
   //
@@ -1343,17 +1558,19 @@ void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
   Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
                        pmom[1]*pmom[1]+pmom[2]*pmom[2]);
   
-  //printf("Loading particle %s mass %f ene %f No %d ip %d pos %f %f %f mom %f %f %f KS %d m %s\n",
-  //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],KS,mecha);
+  //printf("Loading particle %s mass %f ene %f No %d ip %d pos %f %f %f mom %f %f %f kS %d m %s\n",
+  //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS,mecha);
   
-  particle=new(particles[fNtrack]) TParticle(pdg,KS,parent,-1,firstdaughter,
-                                            lastdaughter,pmom[0],pmom[1],pmom[2],
+  particle=new(particles[fNtrack]) TParticle(pdg,kS,parent,-1,kfirstdaughter,
+                                            klastdaughter,pmom[0],pmom[1],pmom[2],
                                             e,vpos[0],vpos[1],vpos[2],tof);
   //                                        polar[0],polar[1],polar[2],tof,
   //                                        mecha,weight);
   ((TParticle*)particles[fNtrack])->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
   ((TParticle*)particles[fNtrack])->SetWeight(weight);
-  if(!done) particle->SetBit(Done_Bit);
+  if(!done) particle->SetBit(kDoneBit);
+  //Declare that the daughter information is valid
+  ((TParticle*)particles[fNtrack])->SetBit(kDaughtersBit);
   
   if(parent>=0) {
     particle=(TParticle*) fParticles->UncheckedAt(parent);
@@ -1378,267 +1595,31 @@ void AliRun::KeepTrack(const Int_t track)
   // flags a track to be kept
   //
   TClonesArray &particles = *fParticles;
-  ((TParticle*)particles[track])->SetBit(Keep_Bit);
+  ((TParticle*)particles[track])->SetBit(kKeepBit);
 }
  
 //_____________________________________________________________________________
-void AliRun::StepManager(Int_t id) const
+void AliRun::StepManager(Int_t id) 
 {
   //
   // Called at every step during transport
   //
 
-  Int_t copy;
   //
   // --- If lego option, do it and leave 
-  if (fLego) {
+  if (fLego)
     fLego->StepManager();
-    return;
-  }
-  //Update energy deposition tables
-  sEventEnergy[gMC->CurrentVolID(copy)]+=gMC->Edep();
+  else {
+    Int_t copy;
+    //Update energy deposition tables
+    AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
   
-  //Call the appropriate stepping routine;
-  AliModule *det = (AliModule*)fModules->At(id);
-  if(det) det->StepManager();
-}
-
-//_____________________________________________________________________________
-void AliRun::ReadEuclid(const char* filnam, const AliModule *det, char* topvol)
-{
-  //                                                                     
-  //       read in the geometry of the detector in euclid file format    
-  //                                                                     
-  //        id_det : the detector identification (2=its,...)            
-  //        topvol : return parameter describing the name of the top    
-  //        volume of geometry.                                          
-  //                                                                     
-  //            author : m. maire                                        
-  //                                                                     
-  //     28.07.98
-  //     several changes have been made by miroslav helbich
-  //     subroutine is rewrited to follow the new established way of memory
-  //     booking for tracking medias and rotation matrices.
-  //     all used tracking media have to be defined first, for this you can use
-  //     subroutine  greutmed.
-  //     top volume is searched as only volume not positioned into another 
-  //
-
-  Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
-  Int_t ndvmx, nr, flag;
-  char key[5], card[77], natmed[21];
-  char name[5], mother[5], shape[5], konly[5], volst[7000][5];
-  char *filtmp;
-  Float_t par[50];
-  Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
-  Float_t xo, yo, zo;
-  Int_t idrot[5000],istop[7000];
-  FILE *lun;
-  //
-  // *** The input filnam name will be with extension '.euc'
-  filtmp=gSystem->ExpandPathName(filnam);
-  lun=fopen(filtmp,"r");
-  delete [] filtmp;
-  if(!lun) {
-    Error("ReadEuclid","Could not open file %s\n",filnam);
-    return;
-  }
-  //* --- definition of rotation matrix 0 ---  
-  TArrayI &idtmed = *(det->GetIdtmed());
-  idrot[0]=0;
-  nvol=0;
- L10:
-  for(i=0;i<77;i++) card[i]=0;
-  iret=fscanf(lun,"%77[^\n]",card);
-  if(iret<=0) goto L20;
-  fscanf(lun,"%*c");
-  //*
-  strncpy(key,card,4);
-  key[4]='\0';
-  if (!strcmp(key,"TMED")) {
-    sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
-    //Pad the string with blanks
-    i=-1;
-    while(natmed[++i]);
-    while(i<20) natmed[i++]=' ';
-    natmed[i]='\0';
-    //
-    gMC->Gckmat(idtmed[itmed],natmed);
-    //*
-  } else if (!strcmp(key,"ROTM")) {
-    sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
-    det->AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
-    //*
-  } else if (!strcmp(key,"VOLU")) {
-    sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
-    if (npar>0) {
-      for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
-      fscanf(lun,"%*c");
-    }
-    gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
-    //*     save the defined volumes
-    strcpy(volst[++nvol],name);
-    istop[nvol]=1;
-    //*
-  } else if (!strcmp(key,"DIVN")) {
-    sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
-    gMC->Gsdvn  ( name, mother, ndiv, iaxe );
-    //*
-  } else if (!strcmp(key,"DVN2")) {
-    sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
-    gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
-    //*
-  } else if (!strcmp(key,"DIVT")) {
-    sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
-    gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
-    //*
-  } else if (!strcmp(key,"DVT2")) {
-    sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
-    gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
-    //*
-  } else if (!strcmp(key,"POSI")) {
-    sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
-    //*** volume name cannot be the top volume
-    for(i=1;i<=nvol;i++) {
-      if (!strcmp(volst[i],name)) istop[i]=0;
-    }
-    //*
-    gMC->Gspos  ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
-    //*
-  } else if (!strcmp(key,"POSP")) {
-    sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
-    if (npar > 0) {
-      for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
-      fscanf(lun,"%*c");
-    }
-    //*** volume name cannot be the top volume
-    for(i=1;i<=nvol;i++) {
-      if (!strcmp(volst[i],name)) istop[i]=0;
-    }
-    //*
-    gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
-  }
-  //*
-  if (strcmp(key,"END")) goto L10;
-  //* find top volume in the geometry
-  flag=0;
-  for(i=1;i<=nvol;i++) {
-    if (istop[i] && flag) {
-      Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
-    }
-    if (istop[i] && !flag) {
-      strcpy(topvol,volst[i]);
-      printf(" *** GREUCL *** volume %s taken as a top volume\n",topvol);
-      flag=1;
-    }
-  }
-  if (!flag) {
-    Warning("ReadEuclid","top volume not found\n");
+    //Call the appropriate stepping routine;
+    AliModule *det = (AliModule*)fModules->At(id);
+    if(det) det->StepManager();
   }
-  fclose (lun);
-  //*
-  //*     commented out only for the not cernlib version
-  printf(" *** GREUCL *** file: %s is now read in\n",filnam);
-  //
-  return;
-  //*
-  L20:
-  Error("ReadEuclid","reading error or premature end of file\n");
 }
 
-//_____________________________________________________________________________
-void AliRun::ReadEuclidMedia(const char* filnam, const AliModule *det)
-{
-  //                                                                     
-  //       read in the materials and tracking media for the detector     
-  //                   in euclid file format                             
-  //                                                                     
-  //       filnam: name of the input file                                
-  //       id_det: id_det is the detector identification (2=its,...)     
-  //                                                                     
-  //            author : miroslav helbich                                
-  //
-  Float_t sxmgmx = gAlice->Field()->Max();
-  Int_t   isxfld = gAlice->Field()->Integ();
-  Int_t end, i, iret, itmed;
-  char key[5], card[130], natmed[21], namate[21];
-  Float_t ubuf[50];
-  char* filtmp;
-  FILE *lun;
-  Int_t imate;
-  Int_t nwbuf, isvol, ifield, nmat;
-  Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
-  //
-  end=strlen(filnam);
-  for(i=0;i<end;i++) if(filnam[i]=='.') {
-    end=i;
-    break;
-  }
-  //
-  // *** The input filnam name will be with extension '.euc'
-  printf("The file name is %s\n",filnam); //Debug
-  filtmp=gSystem->ExpandPathName(filnam);
-  lun=fopen(filtmp,"r");
-  delete [] filtmp;
-  if(!lun) {
-    Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
-    return;
-  }
-  //
-  // Retrieve Mag Field parameters
-  Int_t ISXFLD=gAlice->Field()->Integ();
-  Float_t SXMGMX=gAlice->Field()->Max();
-  //  TArrayI &idtmed = *(det->GetIdtmed());
-  //
- L10:
-  for(i=0;i<130;i++) card[i]=0;
-  iret=fscanf(lun,"%4s %[^\n]",key,card);
-  if(iret<=0) goto L20;
-  fscanf(lun,"%*c");
-  //*
-  //* read material
-  if (!strcmp(key,"MATE")) {
-    sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
-    if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
-    //Pad the string with blanks
-    i=-1;
-    while(namate[++i]);
-    while(i<20) namate[i++]=' ';
-    namate[i]='\0';
-    //
-    det->AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
-    //* read tracking medium
-  } else if (!strcmp(key,"TMED")) {
-    sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
-          &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
-          &stemax,&deemax,&epsil,&stmin,&nwbuf);
-    if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
-    if (ifield<0) ifield=isxfld;
-    if (fieldm<0) fieldm=sxmgmx;
-    //Pad the string with blanks
-    i=-1;
-    while(natmed[++i]);
-    while(i<20) natmed[i++]=' ';
-    natmed[i]='\0';
-    //
-    det->AliMedium(itmed,natmed,nmat,isvol,ISXFLD,SXMGMX,tmaxfd,
-                  stemax,deemax,epsil,stmin,ubuf,nwbuf);
-    //    (*fImedia)[idtmed[itmed]-1]=id_det;
-    //*
-  }
-  //*
-  if (strcmp(key,"END")) goto L10;
-  fclose (lun);
-  //*
-  //*     commented out only for the not cernlib version
-  Warning("ReadEuclidMedia","file: %s is now read in\n",filnam);
-  //*
-  return;
-  //*
- L20:
-  Warning("ReadEuclidMedia","reading error or premature end of file\n");
-} 
 //_____________________________________________________________________________
 void AliRun::Streamer(TBuffer &R__b)
 {
@@ -1651,7 +1632,7 @@ void AliRun::Streamer(TBuffer &R__b)
     if (!gAlice) gAlice = this;
     gROOT->GetListOfBrowsables()->Add(this,"Run");
     fTreeE = (TTree*)gDirectory->Get("TE");
-    if (fTreeE) fTreeE->SetBranchAddress("Header", &header);
+    if (fTreeE) fTreeE->SetBranchAddress("Header", &gAliHeader);
     else    Error("Streamer","cannot find Header Tree\n");
     R__b >> fNtrack;
     R__b >> fHgwmk;
@@ -1672,6 +1653,11 @@ void AliRun::Streamer(TBuffer &R__b)
       fHeader.SetEvent(0);
       fPDGDB     = TDatabasePDG::Instance();        //Particle factory object!
     }
+    if(R__v>2) {
+      fConfigFunction.Streamer(R__b);
+    } else {
+      fConfigFunction="Config();";
+    }
   } else {
     R__b.WriteVersion(AliRun::IsA());
     TNamed::Streamer(R__b);
@@ -1688,100 +1674,6 @@ void AliRun::Streamer(TBuffer &R__b)
     R__b << fTrZmax;
     R__b << fGenerator;
     R__b << fPDGDB;        //Particle factory object!
+    fConfigFunction.Streamer(R__b);
   }
 } 
-
-
-//_____________________________________________________________________________
-//
-//                 Interfaces to Fortran
-//
-//_____________________________________________________________________________
-
-extern "C" void type_of_call  rxgtrak (Int_t &mtrack, Int_t &ipart, Float_t *pmom, 
-                                      Float_t &e, Float_t *vpos, Float_t *polar,
-                                      Float_t &tof)
-{
-  //
-  //     Fetches next track from the ROOT stack for transport. Called by the
-  //     modified version of GTREVE.
-  //
-  //              Track number in the ROOT stack. If MTRACK=0 no
-  //      mtrack  more tracks are left in the stack to be
-  //              transported.
-  //      ipart   Particle code in the GEANT conventions.
-  //      pmom[3] Particle momentum in GeV/c
-  //      e       Particle energy in GeV
-  //      vpos[3] Particle position
-  //      tof     Particle time of flight in seconds
-  //
-  Int_t pdg;
-  gAlice->GetNextTrack(mtrack, pdg, pmom, e, vpos, polar, tof);
-  ipart = gMC->IdFromPDG(pdg);
-  mtrack++;
-}
-
-//_____________________________________________________________________________
-extern "C" void type_of_call 
-#ifndef WIN32
-rxstrak (Int_t &keep, Int_t &parent, Int_t &ipart, Float_t *pmom, 
-              Float_t *vpos, Float_t &tof, const char* cmech, Int_t &ntr, const int cmlen)
-#else
-rxstrak (Int_t &keep, Int_t &parent, Int_t &ipart, Float_t *pmom,
-        Float_t *vpos, Float_t &tof, const char* cmech, const int cmlen,
-        Int_t &ntr)
-#endif
-{
-  //
-  //     Fetches next track from the ROOT stack for transport. Called by GUKINE
-  //     and GUSTEP.
-  //
-  //              Status of the track. If keep=0 the track is put
-  //      keep    on the ROOT stack but it is not fetched for
-  //              transport.
-  //      parent  Parent track. If parent=0 the track is a primary.
-  //              In GUSTEP the routine is normally called to store
-  //              secondaries generated by the current track whose
-  //              ROOT stack number is MTRACK (common SCKINE.
-  //      ipart   Particle code in the GEANT conventions.
-  //      pmom[3] Particle momentum in GeV/c
-  //      vpos[3] Particle position
-  //      tof     Particle time of flight in seconds
-  //
-  //      cmech   (CHARACTER*10) Particle origin. This field is user
-  //              defined and it is not used inside the GALICE code.
-  //      ntr     Number assigned to the particle in the ROOT stack.
-  //
-  char mecha[11];
-  Float_t polar[3]={0.,0.,0.};
-  for(int i=0; i<10 && i<cmlen; i++) mecha[i]=cmech[i];
-  mecha[10]=0;
-  Int_t pdg=gMC->PDGFromId(ipart);
-  gAlice->SetTrack(keep, parent-1, pdg, pmom, vpos, polar, tof, mecha, ntr);
-  ntr++;
-}
-
-//_____________________________________________________________________________
-extern "C" void type_of_call  rxkeep(const Int_t &n)
-{
-  if( NULL==gAlice ) exit(1);
-  
-  if( n<=0 || n>gAlice->Particles()->GetEntries() )
-    {
-      printf("  Bad index n=%d must be 0<n<=%d\n",
-            n,gAlice->Particles()->GetEntries());
-      exit(1);
-    }
-  
-  ((TParticle*)(gAlice->Particles()->UncheckedAt(n-1)))->SetBit(Keep_Bit);
-}
-
-//_____________________________________________________________________________
-extern "C" void type_of_call  rxouth ()
-{
-  //
-  // Called by Gtreve at the end of each primary track
-  //
-  gAlice->FinishPrimary();
-}
-