]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALGetter.cxx
Neatening
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALGetter.cxx
index bb995e00ae81ee4315998c534e3b6eb83e9bcb31..18cc5775d975c4ce6ede428862e945d6b4d50158 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/* $Id:  */
-
-/* $Log:
-*/
+/* $Id$ */
 
 //_________________________________________________________________________
 //  A singleton. This class should be used in the analysis stage to get 
 //  ensures, that one reads Digits, made of these particular digits, RecPoints, 
 //  made of these particular RecPoints, TrackSegments and RecParticles. 
 //  This becomes non trivial if there are several identical branches, produced with
-//  different set of parameters. Currently This class only Retrieves Hits, Digits, and SDigits. 
+//  different set of parameters. 
 //
 //  An example of how to use (see also class AliEMCALAnalyser):
 //  AliEMCALGetter * gime = AliEMCALGetter::GetInstance("galice.root","test") ;
+//  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
+//     AliEMCALRecParticle * part = gime->RecParticle(1) ;
 //     ................
-//  please->GetEvent(event) ;    // reads new event from galice.root
+//  gime->Event(event) ;    // reads new event from galice.root
 //                  
-//*-- Author: Sahal Yacoob (LBL) 
-// based on : AliPHOSGetter
+//*-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC KI & SUBATECH)
+//*--         Completely redesigned by Dmitri Peressounko March 2001  
+//
+//*-- YS June 2001 : renamed the original AliEMCALIndexToObject and make
+//*--         systematic usage of TFolders without changing the interface        
 //////////////////////////////////////////////////////////////////////////////
 
-
 // --- ROOT system ---
 
+#include "TSystem.h"
 #include "TFile.h"
-#include "TTree.h"
 #include "TROOT.h"
-#include "TObjString.h"
-#include "TFolder.h"
+
 
 // --- Standard library ---
-#include <iostream.h>
 
 // --- AliRoot header files ---
 
-#include "AliRun.h"
-#include "AliConfig.h"
 #include "AliEMCALGetter.h"
-#include "AliEMCALv1.h"
-#include "AliEMCALDigitizer.h"
-#include "AliEMCALSDigitizer.h"
-#include "AliEMCALGeometry.h"
+#include "AliEMCAL.h"
+#include "AliRunLoader.h"
+#include "AliStack.h"  
+#include "AliEMCALLoader.h"
+#include "AliMC.h"
 
 ClassImp(AliEMCALGetter)
   
-  AliEMCALGetter * AliEMCALGetter::fgObjGetter = 0 ; 
+AliEMCALGetter * AliEMCALGetter::fgObjGetter = 0 ; 
+AliEMCALLoader * AliEMCALGetter::fgEmcalLoader = 0;
+Int_t AliEMCALGetter::fgDebug = 0;
+
+//  TFile * AliEMCALGetter::fgFile = 0 ; 
 
 //____________________________________________________________________________ 
-AliEMCALGetter::AliEMCALGetter(const char* headerFile, const char* branchTitle )
+AliEMCALGetter::AliEMCALGetter(const char* headerFile, const char* version, Option_t * openingOption)
 {
-  //Initialize  all lists
-
-  fHeaderFile         = headerFile ; 
-  fBranchTitle        = branchTitle ;
-  fSDigitsTitle       = branchTitle ; 
-  fDigitsTitle        = branchTitle ; 
+  // ctor only called by Instance()
 
-  fPrimaries = new TObjArray(1) ;
-  fModuleFolder  = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Configuration/Modules"));
-  fHitsFolder    = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/RunMC/Event/Data/Hits")); 
-  fSDigitsFolder = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/RunMC/Event/Data/SDigits")); 
-  fDigitsFolder  = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Event/Data")); 
-  fTasksFolder   = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Tasks")) ; 
-
-  if ( fHeaderFile != "aliroot"  ) { // to call the getter without a file
-
-    //open headers file
-    TFile * file = static_cast<TFile*>(gROOT->GetFile(fHeaderFile.Data() ) ) ;
-    
-    if(file == 0){    //if file was not opened yet, read gAlice
-      if(fHeaderFile.Contains("rfio")) // if we read file using HPSS
-       file =  TFile::Open(fHeaderFile.Data(),"update") ;
-      else
-       file = new TFile(fHeaderFile.Data(),"update") ;
-      
-      if (!file->IsOpen()) {
-       cerr << "ERROR : AliEMCALGetter::AliEMCALGetter -> Cannot open " << fHeaderFile.Data() << endl ; 
-       abort() ; 
-      }
-      
-      gAlice = static_cast<AliRun *>(file->Get("gAlice")) ;
-      
-      if (!gAlice) {
-       cerr << "ERROR : AliEMCALGetter::AliEMCALGetter -> Cannot find gAlice in " << fHeaderFile.Data() << endl ; 
-       abort() ; 
-      }
-      if (!EMCAL()) {
-         cout << "INFO: AliEMCALGetter -> Posting EMCAL to Folders" << endl ; 
-       AliConfig * conf = AliConfig::Instance() ; 
-       conf->Add(static_cast<AliDetector*>(gAlice->GetDetector("EMCAL"))) ; 
-       conf->Add(static_cast<AliModule*>(gAlice->GetDetector("EMCAL"))) ; 
-     }
+  AliRunLoader* rl = AliRunLoader::GetRunLoader(version) ; 
+  if (!rl) {
+    rl = AliRunLoader::Open(headerFile, version, openingOption);
+    if (!rl) {
+      Fatal("AliEMCALGetter", "Could not find the Run Loader for %s - %s",headerFile, version) ; 
+      return ;
+    } 
+    if (rl->GetAliRun() == 0x0) {
+      rl->LoadgAlice();
+      gAlice = rl->GetAliRun(); // should be removed
     }
-
   }
-}
-//____________________________________________________________________________ 
-AliEMCALGetter::~AliEMCALGetter(){
-
+  fgEmcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetLoader("EMCALLoader"));
+  if ( !fgEmcalLoader ) 
+    Error("AliEMCALGetter", "Could not find EMCALLoader") ; 
+  else 
+    fgEmcalLoader->SetTitle(version);
+  
+  
+  // initialize data members
+  SetDebug(0) ; 
+  //fBTE = 0 ; 
+  fPrimaries = 0 ; 
+  fLoadingStatus = "" ; 
 }
 
 //____________________________________________________________________________ 
-void AliEMCALGetter::CreateWhiteBoard() const
+AliEMCALGetter::~AliEMCALGetter()
 {
-
+  // dtor
+  delete fgEmcalLoader ;
+  fgEmcalLoader = 0 ;
+  //delete fBTE ; 
+  // fBTE = 0 ; 
+  fPrimaries->Delete() ; 
+  delete fPrimaries ; 
 }
 
 //____________________________________________________________________________ 
-AliEMCALGetter * AliEMCALGetter::GetInstance()
-{
-  // Returns the pointer of the unique instance already defined
-  
-  AliEMCALGetter * rv = 0 ;
-  if ( fgObjGetter )
-    rv = fgObjGetter ;
-  else
-    cout << "AliEMCALGetter::GetInstance ERROR: not yet initialized" << endl ;
-
-  return rv ;
+AliEMCALClusterizer * AliEMCALGetter::Clusterizer()
+{ 
+  // return pointer to Clusterizer Tree
+  AliEMCALClusterizer * rv ; 
+  rv =  dynamic_cast<AliEMCALClusterizer *>(EmcalLoader()->Reconstructioner()) ;
+  if (!rv) {
+    Event(0, "R") ; 
+    rv =  dynamic_cast<AliEMCALClusterizer*>(EmcalLoader()->Reconstructioner()) ;
+  }
+  return rv ; 
 }
 
+
 //____________________________________________________________________________ 
-AliEMCALGetter * AliEMCALGetter::GetInstance(const char* headerFile,
-                                          const char* branchTitle)
+TClonesArray * AliEMCALGetter::Digits() const  
 {
-  // Creates and returns the pointer of the unique instance
-  // Must be called only when the environment has changed 
+  // asks the Loader to return the Digits container 
 
-  if ( fgObjGetter )    
-    if((fgObjGetter->fBranchTitle.CompareTo(branchTitle) == 0) && 
-       (fgObjGetter->fHeaderFile.CompareTo(headerFile)==0))
-      return fgObjGetter ;
-    else
-      fgObjGetter->~AliEMCALGetter() ;  // delete it if already exists another version
-  
-  fgObjGetter = new AliEMCALGetter(headerFile,branchTitle) ; 
-  
-  // Posts a few item to the white board (folders)
-  // fgObjGetter->CreateWhiteBoard() ;
-    
-  return fgObjGetter ; 
-  
-}
+  TClonesArray * rv = 0 ; 
+  rv = EmcalLoader()->Digits() ; 
 
-//____________________________________________________________________________ 
-const AliEMCALv0 * AliEMCALGetter::EMCAL() 
-{
-  // returns the EMCAL object 
-  //AliEMCALv0 * emcal = dynamic_cast<AliEMCALv0 *>(gAlice->GetDetector("EMCAL")) ; 
-  AliEMCALv0 * emcal = dynamic_cast<AliEMCALv1 *>(fModuleFolder->FindObject("EMCAL")) ;  
-  if (!emcal) 
-      cout << "WARNING: AliEMCALGetter::EMCAL -> EMCAL module not found in Folders" << endl ; 
-  return emcal ; 
-}  
+  if( !rv ) {
+    EmcalLoader()->MakeDigitsArray() ; 
+    rv = EmcalLoader()->Digits() ;
+  }
+  return rv ; 
+}
 
 //____________________________________________________________________________ 
-const AliEMCALGeometry * AliEMCALGetter::EMCALGeometry() 
-{
-  AliEMCALGeometry * rv = 0 ; 
-  if (EMCAL() )
-   rv =  EMCAL()->GetGeometry() ;
+AliEMCALDigitizer * AliEMCALGetter::Digitizer() 
+{ 
+  // return pointer to Digitizer Tree
+  AliEMCALDigitizer * rv ; 
+  rv =  dynamic_cast<AliEMCALDigitizer *>(EmcalLoader()->Digitizer()) ;
+  if (!rv) {
+    Event(0, "D") ; 
+    rv =  dynamic_cast<AliEMCALDigitizer *>(EmcalLoader()->Digitizer()) ;
+  }
   return rv ; 
-} 
+}
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostHits(void) const 
-{  //------- Hits ----------------------
+TObjArray * AliEMCALGetter::ECARecPoints() const 
+{
+  // asks the Loader to return the EMC RecPoints container 
 
-  // the hierarchy is //Folders/RunMC/Event/Data/EMCAL/Hits
+  TObjArray * rv = 0 ; 
   
-  TFolder * emcalFolder = dynamic_cast<TFolder*>(fHitsFolder->FindObject("EMCAL")) ; 
-  if ( !emcalFolder ) {
-      cout << "WARNING: AliEMCALGetter::Post H -> Folder //" << fHitsFolder << "/EMCAL/ not found!" << endl;
-      cout << "INFO:    AliEMCALGetter::Post H -> Adding Folder //" << fHitsFolder << "/EMCAL/"  << endl;
-    emcalFolder = fHitsFolder->AddFolder("EMCAL", "Hits from EMCAL") ; 
-  }    
-  TClonesArray *hits=  new TClonesArray("AliEMCALHit",1000) ;
-  hits->SetName("Hits") ;
-  emcalFolder->Add(hits) ; 
-  
-  return kTRUE;
-} 
+  rv = EmcalLoader()->ECARecPoints() ; 
+  if (!rv) {
+    EmcalLoader()->MakeRecPointsArray() ;
+    rv = EmcalLoader()->ECARecPoints() ; 
+  }
+  return rv ; 
+}
 
 //____________________________________________________________________________ 
-void * AliEMCALGetter::HitsRef(void) const 
-{  //------- Hits ----------------------
+TClonesArray * AliEMCALGetter::TrackSegments() const 
+{
+  // asks the Loader to return the TrackSegments container 
 
+  TClonesArray * rv = 0 ; 
   
-  // the hierarchy is //Folders/RunMC/Event/Data/EMCAL/Hits
-  if ( !fHitsFolder ) {
-    cerr << "ERROR: AliEMCALGetter::Post H -> Folder //" << fHitsFolder << " not found!" << endl;
-    return 0;
-  }    
-  TFolder * emcalFolder = dynamic_cast<TFolder *>(fHitsFolder->FindObject("EMCAL")) ;
-  if ( !emcalFolder ) {
-    cerr << "ERROR: AliEMCALGetter::Post HRef -> Folder //" << fHitsFolder << "/EMCAL/ not found!" << endl;  
-    return 0;
+  rv = EmcalLoader()->TrackSegments() ; 
+  if (!rv) {
+    EmcalLoader()->MakeTrackSegmentsArray() ;
+    rv = EmcalLoader()->TrackSegments() ; 
   }
-  TObject * h = emcalFolder->FindObject("Hits") ;
-  if(!h) {
-    cerr << "ERROR: AliEMCALGetter::HRef -> " << emcalFolder->GetName() << "/Hits not found !" << endl ; 
-    return 0 ;
+  return rv ; 
+}
+
+//____________________________________________________________________________ 
+AliEMCALTrackSegmentMaker * AliEMCALGetter::TrackSegmentMaker() 
+{ 
+  // return pointer to TrackSegmentMaker Tree
+  AliEMCALTrackSegmentMaker * rv ; 
+  rv =  dynamic_cast<AliEMCALTrackSegmentMaker *>(EmcalLoader()->TrackSegmentMaker()) ;
+  if (!rv) {
+    Event(0, "T") ; 
+    rv =  dynamic_cast<AliEMCALTrackSegmentMaker *>(EmcalLoader()->TrackSegmentMaker()) ;
   }
-  else
-    return static_cast<void *>(emcalFolder->GetListOfFolders()->GetObjectRef(h)) ;
+  return rv ; 
 }
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostSDigits(const char * name, const char * headerFile) const 
-{  //---------- SDigits -------------------------
+TClonesArray * AliEMCALGetter::RecParticles() const  
+{
+  // asks the Loader to return the TrackSegments container 
 
+  TClonesArray * rv = 0 ; 
   
-  // the hierarchy is //Folders/RunMC/Event/Data/EMCAL/SDigits/headerFile/sdigitsname
-  // because you can have sdigits from several hit files for mixing
-  
-  TFolder * emcalFolder = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("EMCAL")) ;
-  if ( !emcalFolder ) {
-      cout << "WARNING: AliEMCALGetter::Post S -> Folder //" << fSDigitsFolder << "/EMCAL/ not found!" << endl;
-      cout << "INFO:    AliEMCALGetter::Post S -> Adding Folder //" << fHitsFolder << "/EMCAL/" << endl;
-    emcalFolder = fSDigitsFolder->AddFolder("EMCAL", "SDigits from EMCAL") ; 
-  }    
-  TString subdir(headerFile) ;
-  TFolder * emcalSubFolder = dynamic_cast<TFolder*>(emcalFolder->FindObject(subdir)) ; 
-  if ( !emcalSubFolder ) 
-    emcalSubFolder = emcalFolder->AddFolder(subdir, ""); 
-  
-  TObject * sd  = emcalSubFolder->FindObject(name); 
-  if ( sd ) {
-      cerr <<"INFO: AliEMCALGetter::Post S -> Folder " << subdir 
-          << " already exists!" << endl ;  
-  }else{
-    TClonesArray * sdigits = new TClonesArray("AliEMCALDigit",1000) ;
-    sdigits->SetName(name) ;
-    emcalSubFolder->Add(sdigits) ;
+  rv = EmcalLoader()->RecParticles() ; 
+  if (!rv) {
+    EmcalLoader()->MakeRecParticlesArray() ;
+    rv = EmcalLoader()->RecParticles() ; 
   }
-  
-  return kTRUE;
-} 
+  return rv ; 
+}
 //____________________________________________________________________________ 
-void * AliEMCALGetter::SDigitsRef(const char * name, const char * file) const 
-{  //------- SDigits ----------------------
-  
-  // the hierarchy is //Folders/RunMC/Event/Data/EMCAL/SDigits/filename/SDigits
-
-  if ( !fSDigitsFolder ) {
-    cerr << "ERROR: AliEMCALGetter::Post SRef -> Folder //" << fSDigitsFolder << " not found!" << endl;
-    return 0;
-  }    
-  TFolder * emcalFolder = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject("EMCAL")) ;
-  if ( !emcalFolder ) {
-    cerr << "ERROR: AliEMCALGetter::Post SRef -> Folder //" << fSDigitsFolder << "/EMCAL/ not found!" << endl;
-    return 0;
-  }
+void AliEMCALGetter::Event(const Int_t event, const char* opt) 
+{
+  // Reads the content of all Tree's S, D and R
 
-  TFolder * emcalSubFolder = 0 ;
-  if(file)
-    emcalSubFolder = dynamic_cast<TFolder *>(emcalFolder->FindObject(file)) ;
-  else
-    emcalSubFolder = dynamic_cast<TFolder *>(emcalFolder->FindObject(fHeaderFile)) ;
-  
-  if(!emcalSubFolder) {
-    cerr << "ERROR: AliEMCALGetter::Post SRef -> Folder //Folders/RunMC/Event/Data/EMCAL/" << file << "not found!" << endl;
-    return 0;
+  if ( event >= MaxEvent() ) {
+    Error("Event", "%d not found in TreeE !", event) ; 
+    return ; 
   }
 
-  TObject * dis = emcalSubFolder->FindObject(name) ;
-  if(!dis)
-    return 0 ;
-  else
-    return static_cast<void *>(emcalSubFolder->GetListOfFolders()->GetObjectRef(dis)) ;
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
 
-}
+  // checks if we are dealing with test-beam data
+//   TBranch * btb = rl->TreeE()->GetBranch("AliEMCALBeamTestEvent") ;
+//   if(btb){
+//     if(!fBTE)
+//       fBTE = new AliEMCALBeamTestEvent() ;
+//     btb->SetAddress(&fBTE) ;
+//     btb->GetEntry(event) ;
+//   }
+//   else{
+//     if(fBTE){
+//       delete fBTE ;
+//       fBTE = 0 ;
+//     }
+//   }
 
-//____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostSDigitizer(AliEMCALSDigitizer * sdigitizer) const 
-{  //---------- SDigitizer -------------------------
-    
-  // the hierarchy is //Folders/Tasks/SDigitizer/EMCAL/sdigitsname
+  // Loads the type of object(s) requested
+  
+  rl->GetEvent(event) ;
 
+  if( strstr(opt,"X") || (strcmp(opt,"")==0) )
+    ReadPrimaries() ;
 
-  TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
+  if(strstr(opt,"H") )
+    ReadTreeH();
 
-  if ( !sd ) {
-    cerr << "ERROR: AliEMCALGetter::Post Ser -> Task //" << fTasksFolder << "/SDigitizer not found!" << endl;
-    return kFALSE ;
-  }        
-  TTask * emcal = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-      cout <<"WARNING: AliEMCALGetter::Post Ser ->//" << fTasksFolder << "/SDigitizer/EMCAL/ not found!" << endl;  
-      cout <<"INFO: AliEMCALGetter::Post Ser -> Adding //" << fTasksFolder << "/SDigitizer/EMCAL/" << endl;
-    emcal = new TTask("EMCAL", "") ; 
-    sd->Add(emcal) ; 
-  } 
-  AliEMCALSDigitizer * emcalsd  = dynamic_cast<AliEMCALSDigitizer *>(emcal->GetListOfTasks()->FindObject( sdigitizer->GetName() )); 
-  if (emcalsd) { 
-      cout << "INFO: AliEMCALGetter::Post Ser -> Task " << sdigitizer->GetName() << " already exists" << endl ; 
-    emcal->GetListOfTasks()->Remove(emcalsd) ;
-  }
-  emcal->Add(sdigitizer) ;     
-  return kTRUE; 
-  
-}
+  if(strstr(opt,"S") )
+    ReadTreeS() ;
 
-//____________________________________________________________________________ 
-void * AliEMCALGetter::SDigitizerRef(const char * name) const 
-{  
+  if( strstr(opt,"D") )
+    ReadTreeD() ;
 
-  TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
-  if ( !sd ) {
-    cerr << "ERROR: AliEMCALGetter::Post SerRef -> Task //" << fTasksFolder << "/SDigitizer not found!" << endl;
-    abort();
-  }        
+  if( strstr(opt,"R") )
+    ReadTreeR() ;
 
-  TTask * emcal = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-    cerr <<"ERROR: AliEMCALGetter::Post SerRef ->  //" << fTasksFolder << "/SDigitizer/EMCAL not found!" << endl;
-    abort();
-  }        
+  if( strstr(opt,"T") )
+    ReadTreeT() ;
 
-  TTask * task = dynamic_cast<TTask*>(emcal->GetListOfTasks()->FindObject(name)) ; 
+  if( strstr(opt,"P") )
+    ReadTreeP() ;
+}
 
-  return static_cast<void *>(emcal->GetListOfTasks()->GetObjectRef(task)) ;
 
+//____________________________________________________________________________ 
+Int_t AliEMCALGetter::EventNumber() const
+  {
+  // return the current event number
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
+  return static_cast<Int_t>(rl->GetEventNumber()) ;   
 }
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostSDigitizer(const char * name, const char * file) const 
-{  //---------- SDigitizer -------------------------
+  TClonesArray * AliEMCALGetter::Hits() const   
+{
+  // asks the loader to return  the Hits container 
   
- // the hierarchy is //Folders/Tasks/SDigitizer/EMCAL/sdigitsname
-
-
-  TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
-  if ( !sd ) {
-    cerr << "ERROR: AliEMCALGetter::Post Ser -> Task //" << fTasksFolder << "/SDigitizer not found!" << endl;
-    return kFALSE ;
-  }        
-
-  TTask * emcal = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-      cout <<"WARNING: AliEMCALGetter::Post Ser ->  //" << fTasksFolder << "/SDigitizer/EMCAL/ not found!" << endl;
-      cout <<"INFO: AliEMCALGetter::Post Ser -> Adding  //" << fTasksFolder << "/SDigitizer/EMCAL" << endl;
-    emcal = new TTask("EMCAL", "") ; 
-    sd->Add(emcal) ; 
-  } 
-
-  TString sdname(name) ;
-  sdname.Append(":") ;
-  sdname.Append(file);
-  AliEMCALSDigitizer * emcalsd  = dynamic_cast<AliEMCALSDigitizer *>(emcal->GetListOfTasks()->FindObject( sdname )); 
-  if (!emcalsd) {
-    emcalsd = new AliEMCALSDigitizer() ;  
-    //Note, we can not call constructor with parameters: it will call Getter and scrud up everething
-    emcalsd->SetName(sdname) ;
-    emcalsd->SetTitle(file) ;
-    emcal->Add(emcalsd) ;      
-  }
-  return kTRUE; 
+  TClonesArray * rv = 0 ; 
   
+  rv = EmcalLoader()->Hits() ; 
+  if ( !rv ) {
+    EmcalLoader()->LoadHits("read"); 
+    rv = EmcalLoader()->Hits() ; 
+  }
+  return rv ; 
 }
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostDigits(const char * name) const 
-{  //---------- Digits -------------------------
-
-  // the hierarchy is //Folders/Run/Event/Data/EMCAL/SDigits/name
-
-  TFolder * emcalFolder  = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("EMCAL")) ;
-
-  if ( !emcalFolder ) {
-      cout << "WARNING: AliEMCALGetter::Post D -> Folder //" << fDigitsFolder << "/EMCAL/ not found!" << endl;
-      cout << "INFO:    AliEMCALGetter::Post D -> Adding Folder //" << fDigitsFolder << "/EMCAL/" << endl;
-    emcalFolder = fDigitsFolder->AddFolder("EMCAL", "Digits from EMCAL") ;  
-  }    
-  TObject*  dig = emcalFolder->FindObject( name ) ;
-  if ( !dig ) {
-    TClonesArray * digits = new TClonesArray("AliEMCALDigit",1000) ;
-    digits->SetName(name) ;
-    emcalFolder->Add(digits) ;  
+AliEMCALGetter * AliEMCALGetter::Instance(const char* alirunFileName, const char* version, Option_t * openingOption) 
+{
+  // Creates and returns the pointer of the unique instance
+  // Must be called only when the environment has changed
+  
+  if(!fgObjGetter){ // first time the getter is called 
+    fgObjGetter = new AliEMCALGetter(alirunFileName, version, openingOption) ;
+  }
+  else { // the getter has been called previously
+    AliRunLoader * rl = AliRunLoader::GetRunLoader(fgEmcalLoader->GetTitle());
+    if ( rl->GetFileName() == alirunFileName ) {// the alirunFile has the same name
+      // check if the file is already open
+      TFile * galiceFile = dynamic_cast<TFile *>(gROOT->FindObject(rl->GetFileName()) ) ; 
+      
+      if ( !galiceFile ) 
+       fgObjGetter = new AliEMCALGetter(alirunFileName, version, openingOption) ;
+      
+      else {  // the file is already open check the version name
+       TString currentVersionName = rl->GetEventFolder()->GetName() ; 
+       TString newVersionName(version) ; 
+       if (currentVersionName == newVersionName) 
+         if(fgDebug)
+           ::Warning( "Instance", "Files with version %s already open", currentVersionName.Data() ) ;  
+       else {
+         fgObjGetter = new AliEMCALGetter(alirunFileName, version, openingOption) ;      
+       }
+      }
+    }
+    else 
+      fgObjGetter = new AliEMCALGetter(alirunFileName, version, openingOption) ;      
   }
-  return kTRUE; 
+  if (!fgObjGetter) 
+    ::Error("Instance", "Failed to create the EMCAL Getter object") ;
+  else 
+    if (fgDebug)
+      Print() ;
+  
+  return fgObjGetter ;
 }
 
 //____________________________________________________________________________ 
-void * AliEMCALGetter::DigitsRef(const char * name) const 
-{ //------- Digits ----------------------
-  
-  // the hierarchy is //Folders/Run/Event/Data/EMCAL/Digits/name
-
-  if ( !fDigitsFolder ) {
-    cerr << "ERROR: AliEMCALGetter::Post DRef -> Folder //" << fDigitsFolder << " not found!" << endl;
-    return 0;
-  }    
+AliEMCALGetter *  AliEMCALGetter::Instance()
+{
+  // Returns the pointer of the unique instance already defined
   
-  TFolder * emcalFolder  = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("EMCAL")) ; 
-  if ( !emcalFolder ) {
-    cerr << "ERROR: AliEMCALGetter::DRef -> Folder //" << fDigitsFolder << "/EMCAL/ not found!" << endl;
-    return 0;
-  }    
-
-  TObject * d = emcalFolder->FindObject(name) ;
-  if(!d)
-    return 0 ;
-  else
-    return static_cast<void *>(emcalFolder->GetListOfFolders()->GetObjectRef(d)) ;
+  if(!fgObjGetter)
+     ::Error("Instance", "Getter not initialized") ;
 
+   return fgObjGetter ;
+           
 }
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostDigitizer(AliEMCALDigitizer * digitizer) const 
-{  //---------- Digitizer -------------------------
-  
-  TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
+Int_t AliEMCALGetter::MaxEvent() const 
+{
+  // returns the number of events in the run (from TE)
 
-  if ( !sd ) {
-    cerr << "ERROR: AliEMCALGetter::Post Der -> Task //" << fTasksFolder << "/Digitizer not found!" << endl;
-    return kFALSE ;
-  }        
-  TTask * emcal = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-      cout <<"WARNING: AliEMCALGetter::Post Der ->  //" << fTasksFolder << "/Digitizer/EMCAL not found!" << endl;
-      cout <<"INFO: AliEMCALGetter::Post Der -> Adding //" << fTasksFolder << "/Digitizer/EMCAL" << endl; 
-    emcal = new TTask("EMCAL", "") ; 
-    sd->Add(emcal) ; 
-  } 
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
+  return static_cast<Int_t>(rl->GetNumberOfEvents()) ; 
+}
 
-    AliEMCALDigitizer * emcald = dynamic_cast<AliEMCALDigitizer*>(emcal->GetListOfTasks()->FindObject(digitizer->GetName())) ; 
-    if (emcald) { 
-      emcald->Delete() ;
-      emcal->GetListOfTasks()->Remove(emcald) ;
-    }
-    emcal->Add(digitizer) ; 
-    return kTRUE; 
-}  
+//____________________________________________________________________________ 
+TParticle * AliEMCALGetter::Primary(Int_t index) const
+{
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
+  return rl->Stack()->Particle(index) ; 
+} 
 
 //____________________________________________________________________________ 
-Bool_t AliEMCALGetter::PostDigitizer(const char * name) const 
-{  //---------- Digitizer -------------------------
-  
- // the hierarchy is //Folders/Tasks/SDigitizer/EMCAL/sdigitsname
+AliEMCAL * AliEMCALGetter:: EMCAL() const  
+{
+  // returns the EMCAL object 
+  AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(EmcalLoader()->GetModulesFolder()->FindObject("EMCAL")) ;  
+  if (!emcal) 
+    if (fgDebug)
+      Warning("EMCAL", "EMCAL module not found in module folders: %s", EmcalLoader()->GetModulesFolder()->GetName() ) ; 
+  return emcal ; 
+}  
 
-  TTask * d  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
-  if ( !d ) {
-    cerr << "ERROR: AliEMCALGetter::Post Der -> Task //" << fTasksFolder << "/Digitizer not found!" << endl;
-    return kFALSE ;
-  }        
 
-  TTask * emcal = dynamic_cast<TTask*>(d->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-      cout <<"WARNING: AliEMCALGetter::Post Der -> //" << fTasksFolder << "/Digitizer/EMCAL not found!" << endl; 
-      cout <<"INFO: AliEMCALGetter::Post Der -> Adding //" << fTasksFolder << "/Digitizer/EMCAL" << endl;
-    emcal = new TTask("EMCAL", "") ; 
-    d->Add(emcal) ; 
-} 
 
-  AliEMCALDigitizer * emcald = dynamic_cast<AliEMCALDigitizer*>(emcal->GetListOfTasks()->FindObject(name)) ; 
-  if (!emcald) { 
-    emcald = new AliEMCALDigitizer() ;
-    emcald->SetName(fDigitsTitle) ;
-    emcald->SetTitle(fHeaderFile) ;
-    emcal->Add(emcald) ;
+//____________________________________________________________________________ 
+AliEMCALPID * AliEMCALGetter::PID() 
+{ 
+  // return pointer to PID Tree
+  AliEMCALPID * rv ; 
+  rv =  dynamic_cast<AliEMCALPID *>(EmcalLoader()->PIDTask()) ;
+  if (!rv) {
+    Event(0, "P") ; 
+    rv =  dynamic_cast<AliEMCALPID *>(EmcalLoader()->PIDTask()) ;
   }
-  return kTRUE;  
+  return rv ; 
 }
 
 //____________________________________________________________________________ 
-void * AliEMCALGetter::DigitizerRef(const char * name) const 
-{  
-  TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
-  if ( !sd ) {
-    cerr << "ERROR: AliEMCALGetter::Post DerRef -> Task //" << fTasksFolder << "/Digitizer not found!" << endl;
-    abort();
-  }        
-
-  TTask * emcal = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if ( !emcal )  {
-    cerr <<"ERROR: AliEMCALGetter::Post DerRef ->  //" << fTasksFolder << "/Digitizer/EMCAL" << endl;
-    abort();
-  }        
-
-  TTask * task = dynamic_cast<TTask*>(emcal->GetListOfTasks()->FindObject(name)) ; 
+AliEMCALGeometry * AliEMCALGetter::EMCALGeometry() const 
+{
+  // Returns EMCAL geometry
 
-  return static_cast<void *>(emcal->GetListOfTasks()->GetObjectRef(task)) ;
+  AliEMCALGeometry * rv = 0 ; 
+  if (EMCAL() )
+    rv =  EMCAL()->GetGeometry() ;
+  return rv ; 
+} 
 
+//____________________________________________________________________________ 
+TClonesArray * AliEMCALGetter::Primaries()  
+{
+  // creates the Primaries container if needed
+  if ( !fPrimaries ) {
+    if (fgDebug) 
+      printf("Primaries: Creating a new TClonesArray for primaries") ; 
+    fPrimaries = new TClonesArray("TParticle", 1000) ;
+  } 
+  return fPrimaries ; 
 }
+
 //____________________________________________________________________________ 
-const TParticle * AliEMCALGetter::Primary(Int_t index) const
+void  AliEMCALGetter::Print() 
 {
-  // Return primary particle numbered by <index>
+  // Print usefull information about the getter
+    
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(fgEmcalLoader->GetTitle());
+  printf("Print: gAlice file is %s -- version name is %s", (rl->GetFileName()).Data(), rl->GetEventFolder()->GetName() ) ; 
+}
 
-  if(index < 0) 
-    return 0 ;
+//____________________________________________________________________________ 
+void AliEMCALGetter::ReadPrimaries()  
+{
+  // Read Primaries from Kinematics.root
   
-  Int_t primaryIndex = index % 10000000 ; 
-  Int_t primaryList = (Int_t ) ((index-primaryIndex)/10000000.)  ;
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
   
-  if ( primaryList > 0  ) {
-      cout << " Getter does not support currently Mixing of primary " << endl ;
-      cout << "   can not return primary: " << index<< " (list "<< primaryList<< " primary # " << primaryIndex << " )"<<endl ;
-    return 0;
-  }
+  // gets kine tree from the root file (Kinematics.root)
+  if ( ! rl->TreeK() )  // load treeK the first time
+    rl->LoadKinematics() ;
   
-  return gAlice->Particle(primaryIndex) ;
+  fNPrimaries = rl->Stack()->GetNtrack() ; 
+
+  if (fgDebug) 
+    printf("ReadTreeK: Found %d particles in event # %d", fNPrimaries, EventNumber() ) ; 
+
+
+  // first time creates the container
+  if ( Primaries() ) 
+    fPrimaries->Clear() ; 
   
+  Int_t index = 0 ; 
+  for (index = 0 ; index < fNPrimaries; index++) { 
+    new ((*fPrimaries)[index]) TParticle(*(Primary(index)));
+  }
 }
 
 //____________________________________________________________________________ 
-void AliEMCALGetter::ReadTreeD()
+Int_t AliEMCALGetter::ReadTreeD()
 {
-  // Read the digit tree gAlice->TreeD()  
-  if(gAlice->TreeD()== 0){
-    cerr <<   "ERROR: AliEMCALGetter::ReadTreeD: can not read TreeD " << endl ;
-  return ;
-  }
- cout << "hello" << endl;  
-  TObjArray * lob = static_cast<TObjArray*>(gAlice->TreeD()->GetListOfBranches()) ;
-  TIter next(lob) ; 
-  TBranch * branch = 0 ; 
-  TBranch * digitsbranch = 0 ; 
-  TBranch * digitizerbranch = 0 ; 
-  Bool_t emcalfound = kFALSE, digitizerfound = kFALSE ; 
-  
-  while ( (branch = static_cast<TBranch*>(next())) && (!emcalfound || !digitizerfound) ) {
-    if ( (strcmp(branch->GetName(), "EMCAL")==0) && (strcmp(branch->GetTitle(), fDigitsTitle)==0) ) {
-      digitsbranch = branch ; 
-      emcalfound = kTRUE ;
-    }
-    else if ( (strcmp(branch->GetName(), "AliEMCALDigitizer")==0) && (strcmp(branch->GetTitle(), fDigitsTitle)==0) ) {
-      digitizerbranch = branch ; 
-      digitizerfound = kTRUE ; 
-    }
-  }
-
-  if ( !emcalfound || !digitizerfound ) {
-      cout << "WARNING: AliEMCALGetter::ReadTreeD -> Cannot find Digits and/or Digitizer with name " 
-          << fDigitsTitle << endl ;
-    return ; 
-  }   
-  //read digits
-  if(!Digits(fDigitsTitle) ) 
-    PostDigits(fDigitsTitle);
-  digitsbranch->SetAddress(DigitsRef(fDigitsTitle)) ;
-  digitsbranch->GetEntry(0) ;
-  
-  
-  // read  the Digitizer
-  if(!Digitizer(fDigitsTitle))
-    PostDigitizer(fDigitsTitle) ;
-  digitizerbranch->SetAddress(DigitizerRef(fDigitsTitle)) ;
-  digitizerbranch->GetEntry(0) ;
+  // Read the Digits
   
+  
+  // gets TreeD from the root file (EMCAL.SDigits.root)
+  if ( !IsLoaded("D") ) {
+    EmcalLoader()->LoadDigits("UPDATE") ;
+    EmcalLoader()->LoadDigitizer("UPDATE") ;
+    SetLoaded("D") ; 
+  } 
+  return Digits()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-void AliEMCALGetter::ReadTreeH()
+Int_t AliEMCALGetter::ReadTreeH()
 {
-  // Read the first entry of EMCAL branch in hit tree gAlice->TreeH()
+  // Read the Hits
+    
+  // gets TreeH from the root file (EMCAL.Hit.root)
+  if ( !IsLoaded("H") ) {
+    EmcalLoader()->LoadHits("UPDATE") ;
+    SetLoaded("H") ; 
+  }  
+  return Hits()->GetEntries() ; 
+}
 
-  if(gAlice->TreeH()== 0){
-    cerr <<   "ERROR: AliEMCALGetter::ReadTreeH: -> Cannot read TreeH " << endl ;
-    return ;
-  }
+//____________________________________________________________________________ 
+Int_t AliEMCALGetter::ReadTreeR()
+{
+  // Read the RecPoints
+  
   
-  TBranch * hitsbranch = static_cast<TBranch*>(gAlice->TreeH()->GetBranch("EMCAL")) ;
-  if ( !hitsbranch ) {
-      cout << "WARNING:  AliEMCALGetter::ReadTreeH -> Cannot find branch EMCAL" << endl ; 
-    return ;
+  // gets TreeR from the root file (EMCAL.RecPoints.root)
+  if ( !IsLoaded("R") ) {
+    EmcalLoader()->LoadRecPoints("UPDATE") ;
+    EmcalLoader()->LoadClusterizer("UPDATE") ;
+    SetLoaded("R") ; 
   }
-  if(!Hits())
-    PostHits() ;
-
-  hitsbranch->SetAddress(HitsRef()) ;
-
-  hitsbranch->GetEntry(0) ;
 
+  return ECARecPoints()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-void AliEMCALGetter::Track(Int_t itrack)
+Int_t AliEMCALGetter::ReadTreeT()
 {
-  // Read the first entry of EMCAL branch in hit tree gAlice->TreeH()
-
-  if(gAlice->TreeH()== 0){
-    cerr <<   "ERROR: AliEMCALGetter::ReadTreeH: -> Cannot read TreeH " << endl ;
-    return ;
-  }
+  // Read the TrackSegments
   
-  TBranch * hitsbranch = dynamic_cast<TBranch*>(gAlice->TreeH()->GetListOfBranches()->FindObject("EMCAL")) ;
-  if ( !hitsbranch ) {
-      cout << "WARNING:  AliEMCALGetter::ReadTreeH -> Cannot find branch EMCAL" << endl ; 
-    return ;
-  }  
-  if(!Hits())
-    PostHits() ;
-  hitsbranch->SetAddress(HitsRef()) ;
-  hitsbranch->GetEntry(itrack) ;
-
+  
+  // gets TreeT from the root file (EMCAL.TrackSegments.root)
+  if ( !IsLoaded("T") ) {
+    EmcalLoader()->LoadTracks("UPDATE") ;
+    EmcalLoader()->LoadTrackSegmentMaker("UPDATE") ;
+    SetLoaded("T") ; 
+  }
 
+  return TrackSegments()->GetEntries() ; 
 }
 //____________________________________________________________________________ 
-void AliEMCALGetter::ReadTreeS(Int_t event)
+Int_t AliEMCALGetter::ReadTreeP()
 {
-  // Read the summable digits tree gAlice->TreeS()  
+  // Read the TrackSegments
   
-  // loop over all opened files and read their SDigits to the White Board
-  TFolder * emcalF = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject("EMCAL")) ;
-  if (!emcalF) 
-    emcalF = fSDigitsFolder->AddFolder("EMCAL", "SDigits from EMCAL") ; 
-  TCollection * folderslist = emcalF->GetListOfFolders() ; 
   
-  //Add current file to list if it is not there yet
-  if ( (fHeaderFile != "aliroot") && ( !folderslist->Contains(fHeaderFile) ) ){
-    emcalF->AddFolder(fHeaderFile, ""); 
+  // gets TreeT from the root file (EMCAL.TrackSegments.root)
+  if ( !IsLoaded("P") ) {
+    EmcalLoader()->LoadRecParticles("UPDATE") ;
+    EmcalLoader()->LoadPID("UPDATE") ;
+    SetLoaded("P") ; 
   }
-    
-  TIter next(folderslist) ; 
-  TFolder * folder = 0 ; 
-  TFile * file; 
-  TTree * treeS = 0;
-  while ( (folder = static_cast<TFolder*>(next())) ) {
-    if(fHeaderFile.CompareTo(folder->GetName()) == 0 ) 
-      {treeS=gAlice->TreeS() ;
-        cout << "ReadTreeS  "<<  gAlice->TreeS()  <<endl ;}
-    else{
-     cout << " AliEMCALGetter::ReadTreeS 2 " <<  folder->GetName() << endl ; 
-     file = static_cast<TFile*>(gROOT->GetFile(folder->GetName())); 
-      file->cd() ;
-      
-      // Get SDigits Tree header from file
-      TString treeName("TreeS") ;
-      treeName += event ; 
-      treeS = dynamic_cast<TTree*>(gDirectory->Get(treeName.Data()));
-    }
-    if(treeS==0){
-      cerr << "ERROR: AliEMCALGetter::ReadTreeS There is no SDigit Tree" << endl;
-      return ;
-    }
-    
-    //set address of the SDigits and SDigitizer
-    TBranch   * sdigitsBranch    = 0;
-    TBranch   * sdigitizerBranch = 0;
-    TBranch   * branch           = 0 ;  
-    TObjArray * lob = static_cast<TObjArray*>(treeS->GetListOfBranches()) ;
-    TIter next(lob) ; 
-    Bool_t emcalfound = kFALSE, sdigitizerfound = kFALSE ; 
-    
-    while ( (branch = static_cast<TBranch*>(next())) && (!emcalfound || !sdigitizerfound) ) {
-     if ( (strcmp(branch->GetName(), "EMCAL")==0) && (strcmp(branch->GetTitle(), fSDigitsTitle)==0) ) {
-       emcalfound = kTRUE ;
-       sdigitsBranch = branch ; 
-       cout << "sdigitsbranch found = " << branch << endl ; 
-       }
-      
-      else if ( (strcmp(branch->GetName(), "AliEMCALSDigitizer")==0) && (strcmp(branch->GetTitle(), fSDigitsTitle)==0) ) {
-       sdigitizerfound = kTRUE ; 
-       sdigitizerBranch = branch ;
-       cout << "sdigitizerbranch found = " << branch << endl ; 
-      }
-    }
-    if ( !emcalfound || !sdigitizerfound ) {
-       cout << "WARNING: AliEMCALDigitizer::ReadSDigits -> Digits and/or Digitizer branch with name " << GetName() 
-            << " not found" << endl ;
-      return ; 
-    }   
-    
-    if ( !folder->FindObject(fSDigitsTitle) )  
-     { PostSDigits(fSDigitsTitle,folder->GetName()) ;
-       cout << "Posting SDigits " << endl << endl ;}  
-    sdigitsBranch->SetAddress(SDigitsRef(fSDigitsTitle,folder->GetName())) ;
-    
-    sdigitsBranch->GetEntry(0) ;
-    
-    TString sdname(fSDigitsTitle) ;
-    cout << sdname << endl ;
-    sdname+=":" ;
-    sdname+=folder->GetName() ;
-    if(!SDigitizer(sdname) ) 
-      PostSDigitizer(fSDigitsTitle,folder->GetName()) ;
-    sdigitizerBranch->SetAddress(SDigitizerRef(sdname)) ;
-    sdigitizerBranch->GetEntry(0) ;
-    
-  }    
+
+  return RecParticles()->GetEntries() ; 
+}
+//____________________________________________________________________________ 
+Int_t AliEMCALGetter::ReadTreeS()
+{
+  // Read the SDigits
   
-  // After SDigits have been read from all files, return to the first one
   
-  next.Reset();
-  folder = static_cast<TFolder*>(next());
-  if(folder){
-    file   = static_cast<TFile*>(gROOT->GetFile(folder->GetName())); 
-    file   ->cd() ;
+  // gets TreeS from the root file (EMCAL.SDigits.root)
+  if ( !IsLoaded("S") ) {
+    EmcalLoader()->LoadSDigits("UPDATE") ;
+    EmcalLoader()->LoadSDigitizer("UPDATE") ;
+    SetLoaded("S") ; 
   }
-  
+
+  return SDigits()->GetEntries() ; 
 }
-//____________________________________________________________________________ 
-void AliEMCALGetter::ReadTreeS(TTree * treeS, Int_t input)
-{  // Read the summable digits fron treeS()  
 
+//____________________________________________________________________________ 
+TClonesArray * AliEMCALGetter::SDigits() const  
+{
+  // asks the Loader to return the Digits container 
 
-  TString filename("mergefile") ;
-  filename+= input ;
+  TClonesArray * rv = 0 ; 
+  
+  rv = EmcalLoader()->SDigits() ; 
+  if (!rv) {
+    EmcalLoader()->MakeSDigitsArray() ;
+    rv = EmcalLoader()->SDigits() ; 
+  }
+  return rv ; 
+}
 
-  TFolder * emcalFolder = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("EMCAL")) ; 
-  if ( !emcalFolder ) { 
-   emcalFolder = fSDigitsFolder->AddFolder("EMCAL", "SDigits from EMCAL") ; 
-  } 
-  TFolder * folder=(TFolder*)emcalFolder->FindObject(filename) ;
-  //set address of the SDigits and SDigitizer
-  TBranch   * sdigitsBranch    = 0;
-  TBranch   * sdigitizerBranch = 0;
-  TBranch   * branch           = 0 ;  
-  TObjArray * lob = (TObjArray*)treeS->GetListOfBranches() ;
-  TIter next(lob) ; 
-  Bool_t emcalfound = kFALSE, sdigitizerfound = kFALSE ; 
-  
-  while ( (branch = (TBranch*)next()) && (!emcalfound || !sdigitizerfound) ) {
-    if ( strcmp(branch->GetName(), "EMCAL")==0) {
-      emcalfound = kTRUE ;
-      sdigitsBranch = branch ; 
-    }
-    
-    else if ( strcmp(branch->GetName(), "AliEMCALSDigitizer")==0) {
-      sdigitizerfound = kTRUE ; 
-      sdigitizerBranch = branch ;
-    }
+//____________________________________________________________________________ 
+AliEMCALSDigitizer * AliEMCALGetter::SDigitizer() 
+{ 
+  // return pointer to SDigitizer Tree
+  AliEMCALSDigitizer * rv ; 
+  rv =  dynamic_cast<AliEMCALSDigitizer *>(EmcalLoader()->SDigitizer()) ;
+  if (!rv) {
+    Event(0, "S") ; 
+    rv =  dynamic_cast<AliEMCALSDigitizer *>(EmcalLoader()->SDigitizer()) ;
   }
-  if ( !emcalfound || !sdigitizerfound ) {
-      cout << "WARNING: AliEMCALGetter::ReadTreeS -> Digits and/or Digitizer branch not found" << endl ;
-    return ; 
-  }   
-  
-  if (!folder || !(folder->FindObject(sdigitsBranch->GetTitle()) ) )
-    PostSDigits(sdigitsBranch->GetTitle(),filename) ;
+  return rv ; 
+}
 
-  sdigitsBranch->SetAddress(SDigitsRef(sdigitsBranch->GetTitle(),filename)) ;
-  
-  TString sdname(sdigitsBranch->GetTitle()) ;
-  sdname+=":" ;
-  sdname+=filename ;
-  if(!SDigitizer(sdigitsBranch->GetTitle()) )
-    PostSDigitizer(sdigitsBranch->GetTitle(),filename) ;
-  sdigitizerBranch->SetAddress(SDigitizerRef(sdname)) ;
-  
-  sdigitsBranch->GetEntry(0) ;
-  sdigitizerBranch->GetEntry(0) ;
-  
-}    
+//____________________________________________________________________________ 
+TParticle * AliEMCALGetter::Secondary(const TParticle* p, const Int_t index) const
+{
+  // Return first (index=1) or second (index=2) secondary particle of primary particle p 
+
+  if(index <= 0) 
+    return 0 ;
+  if(index > 2)
+    return 0 ;
 
+  if(p) {
+  Int_t daughterIndex = p->GetDaughter(index-1) ; 
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
+  return  rl->GetAliRun()->GetMCApp()->Particle(daughterIndex) ; 
+  }
+  else
+    return 0 ;
+}
 
 //____________________________________________________________________________ 
-void AliEMCALGetter::ReadPrimaries()
+void AliEMCALGetter::Track(const Int_t itrack) 
 {
-  // Reads specific branches of primaries
-  
-  fNPrimaries = gAlice->GetNtrack();
-  
-  //   //Check, is it necessary to open new files
-  //   TArrayI* events = fDigitizer->GetCurrentEvents() ; 
-  //   TClonesArray * filenames = fDigitizer->GetHeadersFiles() ;
-//   Int_t input ;
-//   for(input = 0; input < filenames->GetEntriesFast(); input++){
+  // Read the first entry of EMCAL branch in hit tree gAlice->TreeH()
+ AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
 
-//     TObjString * filename = (TObjString *) filenames->At(input) ;
+  if( !TreeH() ) // load treeH the first time
+    rl->LoadHits() ;
 
-//     //Test, if this file already open
-//     TFile *file = (TFile*) gROOT->GetFile( filename->GetString() ) ;
-//     if(file == 0)
-//       file = new TFile( filename->GetString()) ;
-//     file->cd() ;
-    
-//     // Get Kine Tree from file
-// //     char treeName[20];
-// //     sprintf(treeName,"TreeK%d",events->At(input));
-// //     TTree * treeK = (TTree*)gDirectory->Get(treeName);
-// //     if (treeK) 
-// //       treeK->SetBranchAddress("Particles", &fParticleBuffer);
-// //     else    
-// //       cout << "AliEMCALGetter: cannot find Kine Tree for event:" << events->At(input) << endl;
-
-// //     // Create the particle stack
-// //     if(!fParticles) fParticles = new TClonesArray("TParticle",1000);
-// //     // Build the pointer list
-// //     if(fParticleMap) {     <----
-// //       fParticleMap->Clear();
-// //       fParticleMap->Expand(treeK->GetEntries());
-// //     } else
-// //       fParticleMap = new TObjArray(treeK->GetEntries());
-    
-//     // From gAlice->Particle(i) 
+  // first time create the container
+  TClonesArray * hits = Hits() ; 
+  if ( hits ) 
+    hits->Clear() ; 
 
+  TBranch * emcalbranch = dynamic_cast<TBranch*>(TreeH()->GetBranch("EMCAL")) ; 
+  emcalbranch->SetAddress(&hits) ;
+  emcalbranch->GetEntry(itrack) ;
+}
 
-// //   if(!(*fParticleMap)[i]) {
-// //     Int_t nentries = fParticles->GetEntries();
-    
-// //     // algorithmic way of getting entry index
-// //     // (primary particles are filled after secondaries)
-// //     Int_t entry;
-// //     if (i<fHeader.GetNprimary())
-// //       entry = i+fHeader.GetNsecondary();
-// //     else 
-// //       entry = i-fHeader.GetNprimary();
-      
-// //     // only check the algorithmic way and give
-// //     // the fatal error if it is wrong
-// //     if (entry != fParticleFileMap[i]) {
-// //       Fatal("Particle",
-// //         "!!!! The algorithmic way is WRONG: !!!\n entry: %d map: %d",
-// //  entry, fParticleFileMap[i]); 
-// //     }  
-      
-// //     fTreeK->GetEntry(fParticleFileMap[i]);
-// //     new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
-// //     fParticleMap->AddAt((*fParticles)[nentries],i);
-// //   }
-// //   return (TParticle *) (*fParticleMap)[i];
+//____________________________________________________________________________ 
+TTree * AliEMCALGetter::TreeD() const 
+{
+  // return pointer to Digits Tree
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeD() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("D");
+    rv = EmcalLoader()->TreeD() ;
+  } 
+  
+  return rv ; 
+}
 
-   
-    
-//   }
+//____________________________________________________________________________ 
+TTree * AliEMCALGetter::TreeH() const 
+{
+  // return pointer to Hits Tree
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeH() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("H");
+    rv = EmcalLoader()->TreeH() ;
+  } 
+  
+  return rv ; 
+}
 
+//____________________________________________________________________________ 
+TTree * AliEMCALGetter::TreeR() const 
+{
+  // return pointer to RecPoints Tree
 
-//   //scan over opened files and read corresponding TreeK##
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeR() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("R");
+    rv = EmcalLoader()->TreeR() ;
+  } 
+  
+  return rv ; 
+}
 
-  return ;
+//____________________________________________________________________________ 
+TTree * AliEMCALGetter::TreeT() const 
+{  
+  // return pointer to TrackSegments Tree
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeT() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("T");
+    rv = EmcalLoader()->TreeT() ;
+  } 
+  
+  return rv ; 
 }
 //____________________________________________________________________________ 
-void AliEMCALGetter::Event(const Int_t event, const char* opt)
+TTree * AliEMCALGetter::TreeP() const 
 {
-  // Reads the content of all Tree's S, D and R
+  // return pointer to RecParticles Tree
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeP() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("P");
+    rv = EmcalLoader()->TreeP() ;
+  } 
   
-  if (event >= gAlice->TreeE()->GetEntries() ) {
-    cerr << "ERROR: AliEMCALGetter::Event -> " << event << " not found in TreeE!" << endl ; 
-    return ; 
-  }
-  gAlice->GetEvent(event) ;
+  return rv ; 
+}
 
-  if(strstr(opt,"H") )
-   {cout<<"Reading TreeH" << endl ; 
-   ReadTreeH() ;}
+//____________________________________________________________________________ 
+TTree * AliEMCALGetter::TreeS() const 
+{
+  // return pointer to SDigits Tree
+  TTree * rv = 0 ; 
+  rv = EmcalLoader()->TreeS() ; 
+  if ( !rv ) {
+    EmcalLoader()->MakeTree("S");
+    rv = EmcalLoader()->TreeS() ;
+  } 
   
-  if(strstr(opt,"S") )
-    { cout << "Reading TreeS" << endl ;
-    ReadTreeS(event) ;}
+  return rv ; 
+}
 
-  if( strstr(opt,"D") )
-    ReadTreeD() ;
+//____________________________________________________________________________ 
+Bool_t AliEMCALGetter::VersionExists(TString & opt) const
+{
+  // checks if the version with the present name already exists in the same directory
 
-  if( strstr(opt,"R") )
-//    ReadTreeR() ;
+  Bool_t rv = kFALSE ;
+  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
+  TString version( rl->GetEventFolder()->GetName() ) ; 
+
+  opt.ToLower() ; 
+  
+  if ( opt == "sdigits") {
+    // add the version name to the root file name
+    TString fileName( EmcalLoader()->GetSDigitsFileName() ) ; 
+    if (version != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
+      fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
+    if ( !(gSystem->AccessPathName(fileName)) ) { 
+      Warning("VersionExists", "The file %s already exists", fileName.Data()) ;
+      rv = kTRUE ; 
+    }
+    EmcalLoader()->SetSDigitsFileName(fileName) ;
+  }
 
-  if( strstr(opt,"Q") )
-//    ReadTreeQA() ;
+  if ( opt == "digits") {
+    // add the version name to the root file name
+    TString fileName( EmcalLoader()->GetDigitsFileName() ) ; 
+    if (version != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
+      fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
+    if ( !(gSystem->AccessPathName(fileName)) ) {
+      Warning("VersionExists", "The file %s already exists", fileName.Data()) ;  
+      rv = kTRUE ; 
+    }
+    EmcalLoader()->SetDigitsFileName(fileName) ;
+  }
 
-  if( strstr(opt,"P") )
-    ReadPrimaries() ;
+  return rv ;
 
 }
 
 //____________________________________________________________________________ 
-const TObject * AliEMCALGetter::ReturnO(TString what, TString name, TString file) const 
+UShort_t AliEMCALGetter::EventPattern(void) const
 {
-  // get the object named "what" from the folder
-  // folders are named like //Folders
-
-  if ( file.IsNull() ) 
-    file = fHeaderFile ; 
-
-  TFolder * folder = 0 ;
-  TObject * emcalO  = 0 ; 
-
-  //  if ( name.IsNull() ) {
-  if ( what.CompareTo("Hits") == 0 ) {
-    folder = dynamic_cast<TFolder *>(fHitsFolder->FindObject("EMCAL")) ; 
-    if (folder) 
-      emcalO  = dynamic_cast<TObject *>(folder->FindObject("Hits")) ;  
-  }
-  else if ( what.CompareTo("SDigits") == 0 ) { 
-    TString path = "EMCAL/" + file  ; 
-    folder = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject(path.Data())) ; 
-    if (folder) { 
-    cout << "folder found" << endl ;
-    if (name.IsNull())
-       name = fSDigitsTitle ; 
-      emcalO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
-    }
-  }
-  else if ( what.CompareTo("Digits") == 0 ){
-    folder = dynamic_cast<TFolder *>(fDigitsFolder->FindObject("EMCAL")) ; 
-    if (folder) { 
-      if (name.IsNull())
-       name = fDigitsTitle ; 
-      emcalO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
-    } 
-  }
-  if (!emcalO) {
-      cerr << "ERROR : AliEMCALGetter::ReturnO -> Object " << what << " not found in " << folder->GetName() << endl ; 
+  // Return the pattern (trigger bit register) of the beam-test event
+//   if(fBTE)
+//     return fBTE->GetPattern() ;
+//   else
     return 0 ;
-  }
-  return emcalO ;
 }
-  
 //____________________________________________________________________________ 
-const TTask * AliEMCALGetter::ReturnT(TString what, TString name) const 
+Float_t AliEMCALGetter::BeamEnergy(void) const
 {
-  // get the TTask named "what" from the folder
-  // folders are named like //Folders/Tasks/what/EMCAL/name
-
-  TString search(what) ; 
-
-  TTask * tasks = dynamic_cast<TTask*>(fTasksFolder->FindObject(search)) ; 
-
-  if (!tasks) {
-    cerr << "ERROR: AliEMCALGetter::ReturnT -> Task " << what << " not found!" << endl ;  
-    return 0 ; 
-  }
-
-  TTask * emcalT = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("EMCAL")) ; 
-  if (!emcalT) { 
-    cerr << "ERROR: AliEMCALGetter::ReturnT -> Task " << what << "/EMCAL not found!" << endl ;  
-    return 0 ; 
-  }
-  
-  TList * list = emcalT->GetListOfTasks() ; 
-  if (what.CompareTo("SDigitizer") == 0) {  
-    if ( name.IsNull() )
-      name =  fSDigitsTitle ; 
-  } else  if (what.CompareTo("Digitizer") == 0){ 
-    if ( name.IsNull() )
-      name =  fDigitsTitle ;
-  }
-  
-  TIter it(list) ;
-  TTask * task = 0 ; 
-  while((task = static_cast<TTask *>(it.Next()) )){
-    TString taskname(task->GetName()) ;
-    if(taskname.BeginsWith(name))
-      return task ;
-  }
-  
-    cout << "WARNING: AliEMCALGetter::ReturnT -> Task " << search << "/" << name << " not found!" << endl ; 
-  return 0 ;
+  // Return the beam energy of the beam-test event
+//   if(fBTE)
+//     return fBTE->GetBeamEnergy() ;
+//   else
+    return 0 ;
 }