]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOSGetter.cxx
Raw Sdigits energy converted to GeV.
[u/mrichter/AliRoot.git] / PHOS / AliPHOSGetter.cxx
index b1b412721ff5fcad7f4eca53d500a9064b55337a..792cab4e52189960d4c13e0f6459820988eadec3 100644 (file)
 
 // --- ROOT system ---
 
-#include "TSystem.h"
-#include "TFile.h"
-#include "TROOT.h"
-
+#include <TFile.h>
+#include <TROOT.h>
+#include <TSystem.h>
+#include <TParticle.h>
+#include <TF1.h>
+#include <TGraph.h>
+#include <TCanvas.h>
+#include <TStyle.h>
+//#include <TFrame.h>
 
 // --- Standard library ---
 
 // --- AliRoot header files ---
-
+#include "AliESD.h"
+#include "AliHeader.h"  
+#include "AliMC.h"
+#include "AliPHOS.h"
+#include "AliPHOSBeamTestEvent.h"
 #include "AliPHOSGetter.h"
+#include "AliPHOSLoader.h"
+#include "AliPHOSPulseGenerator.h"
 #include "AliRunLoader.h"
 #include "AliStack.h"  
-#include "AliPHOSLoader.h"
-// #include "AliPHOSRaw2Digits.h"
-//#include "AliPHOSCalibrationDB.h"
-#include "AliPHOSBeamTestEvent.h"
+#include "AliPHOSRawDecoder.h"
+#include "AliRawReaderFile.h"
+#include "AliLog.h"
+#include "AliCDBLocal.h"
+#include "AliCDBStorage.h"
+#include "AliCDBManager.h"
+#include "AliPHOSRawDigiProducer.h"
 
 ClassImp(AliPHOSGetter)
   
-AliPHOSGetter * AliPHOSGetter::fgObjGetter = 0 ; 
-AliPHOSLoader * AliPHOSGetter::fgPhosLoader = 0;
+AliPHOSGetter   * AliPHOSGetter::fgObjGetter  = 0 ; 
+AliPHOSLoader   * AliPHOSGetter::fgPhosLoader = 0;
+AliPHOSCalibData* AliPHOSGetter::fgCalibData  = 0;
 Int_t AliPHOSGetter::fgDebug = 0;
 
 //  TFile * AliPHOSGetter::fgFile = 0 ; 
 
+AliPHOSGetter::AliPHOSGetter() :
+  fBTE(0),
+  fLoadingStatus(),
+  fNPrimaries(0),
+  fPrimaries(0),
+  fESDFile(0),
+  fESDFileName(),
+  fESD(0),
+  fESDTree(0),
+  fRawDigits(kFALSE),
+  fcdb(0)
+{
+  // ctor: this is a singleton, the ctor should never be called but cint needs it as public
+  Fatal("ctor", "AliPHOSGetter is a singleton default ctor not callable") ;
+} 
+
+
 //____________________________________________________________________________ 
-AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* version, Option_t * openingOption)
+AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* version, Option_t * openingOption) :
+  fBTE(0),
+  fLoadingStatus(),
+  fNPrimaries(0),
+  fPrimaries(0),
+  fESDFile(0),
+  fESDFileName(),
+  fESD(0),
+  fESDTree(0),
+  fRawDigits(kFALSE),
+  fcdb(0)
 {
   // ctor only called by Instance()
 
@@ -80,6 +122,7 @@ AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* version, Option
     if (rl->GetAliRun() == 0x0) {
       rl->LoadgAlice();
       gAlice = rl->GetAliRun(); // should be removed
+      rl->LoadHeader();
     }
   }
   fgPhosLoader = dynamic_cast<AliPHOSLoader*>(rl->GetLoader("PHOSLoader"));
@@ -88,29 +131,97 @@ AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* version, Option
   else 
     fgPhosLoader->SetTitle(version);
   
-  
   // initialize data members
   SetDebug(0) ; 
   fBTE = 0 ; 
   fPrimaries = 0 ; 
   fLoadingStatus = "" ; 
+  fESDFileName = rl->GetFileName()  ; // this should be the galice.root file
+  fESDFileName.ReplaceAll("galice.root", "AliESDs.root") ;  
+  fESDFile = 0 ; 
+  fESD = 0 ; 
+  fESDTree = 0 ; 
+  fRawDigits = kFALSE ;
+
 }
 
+AliPHOSGetter::AliPHOSGetter(const AliPHOSGetter & obj) :
+  TObject(obj),
+  fBTE(0),
+  fLoadingStatus(),
+  fNPrimaries(0),
+  fPrimaries(0),
+  fESDFile(0),
+  fESDFileName(),
+  fESD(0),
+  fESDTree(0),
+  fRawDigits(kFALSE),
+  fcdb(0)
+{
+  // cpy ctor requested by Coding Convention 
+  Fatal("cpy ctor", "not implemented") ;
+} 
+
+//____________________________________________________________________________ 
+AliPHOSGetter::AliPHOSGetter(Int_t /*i*/) :
+  fBTE(0),
+  fLoadingStatus(),
+  fNPrimaries(0),
+  fPrimaries(0),
+  fESDFile(0),
+  fESDFileName(),
+  fESD(0),
+  fESDTree(0),
+  fRawDigits(kFALSE),
+  fcdb(0)
+{
+  // special constructor for onflight 
+} 
+
+
 //____________________________________________________________________________ 
 AliPHOSGetter::~AliPHOSGetter()
 {
   // dtor
-  delete fgPhosLoader ;
-  fgPhosLoader = 0 ;
-  delete fBTE ; 
-  fBTE = 0 ; 
-  fPrimaries->Delete() ; 
-  delete fPrimaries ; 
+  if(fgPhosLoader){
+    delete fgPhosLoader ;
+    fgPhosLoader = 0 ;
+  }
+  if(fBTE){
+    delete fBTE ; 
+    fBTE = 0 ;
+  } 
+  if(fPrimaries){
+    fPrimaries->Delete() ; 
+    delete fPrimaries ;
+  } 
+  if (fESD) 
+    delete fESD ; 
+  if (fESDTree) 
+    delete fESDTree ;
+  fgObjGetter = 0;
+}
+
+//____________________________________________________________________________ 
+void AliPHOSGetter::Reset()
+{
+  // resets things in case the getter is called consecutively with different files
+  // the PHOS Loader is already deleted by the Run Loader
+
+  if (fPrimaries) { 
+    fPrimaries->Delete() ; 
+    delete fPrimaries ;
+  } 
+  fgPhosLoader = 0; 
+  fgObjGetter = 0; 
 }
 
 //____________________________________________________________________________ 
 AliPHOSClusterizer * AliPHOSGetter::Clusterizer()
 { 
+  // Returns pointer to the Clusterizer task 
   AliPHOSClusterizer * rv ; 
   rv =  dynamic_cast<AliPHOSClusterizer *>(PhosLoader()->Reconstructioner()) ;
   if (!rv) {
@@ -121,7 +232,7 @@ AliPHOSClusterizer * AliPHOSGetter::Clusterizer()
 }
 
 //____________________________________________________________________________ 
-TObjArray * AliPHOSGetter::CpvRecPoints() 
+TObjArray * AliPHOSGetter::CpvRecPoints() const
 {
   // asks the Loader to return the CPV RecPoints container 
 
@@ -136,7 +247,7 @@ TObjArray * AliPHOSGetter::CpvRecPoints()
 }
 
 //____________________________________________________________________________ 
-TClonesArray * AliPHOSGetter::Digits() 
+TClonesArray * AliPHOSGetter::Digits() const
 {
   // asks the Loader to return the Digits container 
 
@@ -153,6 +264,7 @@ TClonesArray * AliPHOSGetter::Digits()
 //____________________________________________________________________________ 
 AliPHOSDigitizer * AliPHOSGetter::Digitizer() 
 { 
+  // Returns pointer to the Digitizer task 
   AliPHOSDigitizer * rv ; 
   rv =  dynamic_cast<AliPHOSDigitizer *>(PhosLoader()->Digitizer()) ;
   if (!rv) {
@@ -164,7 +276,7 @@ AliPHOSDigitizer * AliPHOSGetter::Digitizer()
 
 
 //____________________________________________________________________________ 
-TObjArray * AliPHOSGetter::EmcRecPoints() 
+TObjArray * AliPHOSGetter::EmcRecPoints() const
 {
   // asks the Loader to return the EMC RecPoints container 
 
@@ -179,7 +291,7 @@ TObjArray * AliPHOSGetter::EmcRecPoints()
 }
 
 //____________________________________________________________________________ 
-TClonesArray * AliPHOSGetter::TrackSegments() 
+TClonesArray * AliPHOSGetter::TrackSegments() const
 {
   // asks the Loader to return the TrackSegments container 
 
@@ -194,8 +306,9 @@ TClonesArray * AliPHOSGetter::TrackSegments()
 }
 
 //____________________________________________________________________________ 
-AliPHOSTrackSegmentMaker * AliPHOSGetter::TrackSegmentMaker() 
+AliPHOSTrackSegmentMaker * AliPHOSGetter::TrackSegmentMaker()
 { 
+  // Returns pointer to the TrackSegmentMaker task 
   AliPHOSTrackSegmentMaker * rv ; 
   rv =  dynamic_cast<AliPHOSTrackSegmentMaker *>(PhosLoader()->TrackSegmentMaker()) ;
   if (!rv) {
@@ -206,7 +319,7 @@ AliPHOSTrackSegmentMaker * AliPHOSGetter::TrackSegmentMaker()
 }
 
 //____________________________________________________________________________ 
-TClonesArray * AliPHOSGetter::RecParticles() 
+TClonesArray * AliPHOSGetter::RecParticles() const
 {
   // asks the Loader to return the TrackSegments container 
 
@@ -220,59 +333,82 @@ TClonesArray * AliPHOSGetter::RecParticles()
   return rv ; 
 }
 //____________________________________________________________________________ 
-void AliPHOSGetter::Event(const Int_t event, const char* opt) 
+void AliPHOSGetter::Event(Int_t event, const char* opt) 
 {
   // Reads the content of all Tree's S, D and R
 
-  if ( event >= MaxEvent() ) {
-    Error("Event", "%d not found in TreeE !", event) ; 
-    return ; 
-  }
+//   if ( event >= MaxEvent() ) {
+//     Error("Event", "%d not found in TreeE !", event) ; 
+//     return ; 
+//   }
 
   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
 
-  // checks if we are dealing with test-beam data
-  TBranch * btb = rl->TreeE()->GetBranch("AliPHOSBeamTestEvent") ;
-  if(btb){
-    if(!fBTE)
-      fBTE = new AliPHOSBeamTestEvent() ;
-    btb->SetAddress(&fBTE) ;
-    btb->GetEntry(event) ;
-  }
-  else{
-    if(fBTE){
-      delete fBTE ;
-      fBTE = 0 ;
-    }
-  }
+//   // checks if we are dealing with test-beam data
+//   TBranch * btb = rl->TreeE()->GetBranch("AliPHOSBeamTestEvent") ;
+//   if(btb){
+//     if(!fBTE)
+//       fBTE = new AliPHOSBeamTestEvent() ;
+//     btb->SetAddress(&fBTE) ;
+//     btb->GetEntry(event) ;
+//   }
+//   else{
+//     if(fBTE){
+//       delete fBTE ;
+//       fBTE = 0 ;
+//     }
+//   }
 
   // Loads the type of object(s) requested
   
   rl->GetEvent(event) ;
 
-  if( strstr(opt,"X") || (strcmp(opt,"")==0) )
+  if(strstr(opt,"X") || (strcmp(opt,"")==0)){
     ReadPrimaries() ;
-
-  if(strstr(opt,"H") )
+  }
+  
+  if(strstr(opt,"H")  ){
     ReadTreeH();
-
-  if(strstr(opt,"S") )
+  }
+  
+  if(strstr(opt,"S")  ){
     ReadTreeS() ;
-
-  if( strstr(opt,"D") )
+  }
+  
+  if(strstr(opt,"D") ){
     ReadTreeD() ;
-
-  if( strstr(opt,"R") )
+  }
+  
+  if(strstr(opt,"R") ){
     ReadTreeR() ;
+  }
 
-  if( strstr(opt,"T") )
+  if( strstr(opt,"T") ){
     ReadTreeT() ;
+  }    
 
-  if( strstr(opt,"P") )
+  if( strstr(opt,"P") ){
     ReadTreeP() ;
+  }    
 
-//   if( strstr(opt,"Q") )
-//     ReadTreeQA() ;
+  if( strstr(opt,"E") ){
+    ReadTreeE(event) ;
+  }
+
+}
+
+
+//____________________________________________________________________________ 
+void AliPHOSGetter::Event(AliRawReader *rawReader, const char* opt, Bool_t isOldRCUFormat) 
+{
+  // Reads the raw event from rawReader
+  // isOldRCUFormat defines whenever to assume
+  // the old RCU format or not
+  
+  if( strstr(opt,"W")  ){
+    rawReader->NextEvent();
+    ReadRaw(rawReader,isOldRCUFormat) ;
+  }    
  
 }
 
@@ -286,7 +422,7 @@ Int_t AliPHOSGetter::EventNumber() const
 }
 
 //____________________________________________________________________________ 
-  TClonesArray * AliPHOSGetter::Hits()  
+  TClonesArray * AliPHOSGetter::Hits() const
 {
   // asks the loader to return  the Hits container 
   
@@ -306,8 +442,6 @@ AliPHOSGetter * AliPHOSGetter::Instance(const char* alirunFileName, const char*
   // Creates and returns the pointer of the unique instance
   // Must be called only when the environment has changed
   
-  //::Info("Instance","alirunFileName=%s version=%s openingOption=%s",alirunFileName,version,openingOption);
-  
   if(!fgObjGetter){ // first time the getter is called 
     fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;
   }
@@ -331,11 +465,15 @@ AliPHOSGetter * AliPHOSGetter::Instance(const char* alirunFileName, const char*
        }
       }
     }
-    else 
+    else {
+      rl = AliRunLoader::GetRunLoader(fgPhosLoader->GetTitle()) ; 
+      if ( strstr(version, AliConfig::GetDefaultEventFolderName()) ) // false in case of merging
+       delete rl ; 
       fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;      
+    }
   }
   if (!fgObjGetter) 
-    ::Error("Instance", "Failed to create the PHOS Getter object") ;
+    ::Error("AliPHOSGetter::Instance", "Failed to create the PHOS Getter object") ;
   else 
     if (fgDebug)
       Print() ;
@@ -348,8 +486,8 @@ AliPHOSGetter *  AliPHOSGetter::Instance()
 {
   // Returns the pointer of the unique instance already defined
   
-  if(!fgObjGetter)
-     ::Error("Instance", "Getter not initialized") ;
+  if(!fgObjGetter && fgDebug)
+     ::Warning("AliPHOSGetter::Instance", "Getter not initialized") ;
 
    return fgObjGetter ;
            
@@ -375,7 +513,16 @@ TParticle * AliPHOSGetter::Primary(Int_t index) const
 AliPHOS * AliPHOSGetter:: PHOS() const  
 {
   // returns the PHOS object 
-  AliPHOS * phos = dynamic_cast<AliPHOS*>(PhosLoader()->GetModulesFolder()->FindObject("PHOS")) ;  
+  AliPHOSLoader *    loader = 0;
+  static AliPHOSLoader * oldloader = 0;
+  static AliPHOS * phos = 0;
+
+  loader = PhosLoader();
+
+  if ( loader != oldloader) {
+    phos = dynamic_cast<AliPHOS*>(loader->GetModulesFolder()->FindObject("PHOS")) ;
+    oldloader = loader;
+  }
   if (!phos) 
     if (fgDebug)
       Warning("PHOS", "PHOS module not found in module folders: %s", PhosLoader()->GetModulesFolder()->GetName() ) ; 
@@ -385,8 +532,9 @@ AliPHOS * AliPHOSGetter:: PHOS() const
 
 
 //____________________________________________________________________________ 
-AliPHOSPID * AliPHOSGetter::PID() 
+AliPHOSPID * AliPHOSGetter::PID()
 { 
+  // Returns pointer to the PID task 
   AliPHOSPID * rv ; 
   rv =  dynamic_cast<AliPHOSPID *>(PhosLoader()->PIDTask()) ;
   if (!rv) {
@@ -404,6 +552,13 @@ AliPHOSGeometry * AliPHOSGetter::PHOSGeometry() const
   AliPHOSGeometry * rv = 0 ; 
   if (PHOS() )
     rv =  PHOS()->GetGeometry() ;
+  else {
+    rv = AliPHOSGeometry::GetInstance();
+    if (!rv) {
+      AliError("Could not find PHOS geometry! Loading the default one !");
+      rv = AliPHOSGeometry::GetInstance("IHEP","");
+    }
+  }
   return rv ; 
 } 
 
@@ -436,11 +591,11 @@ void AliPHOSGetter::ReadPrimaries()
   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
   
   // gets kine tree from the root file (Kinematics.root)
-  if ( ! rl->TreeK() )  // load treeK the first time
+  if ( ! rl->TreeK() ) { // load treeK the first time
     rl->LoadKinematics() ;
+  }
   
-  fNPrimaries = rl->Stack()->GetNtrack() ; 
-
+  fNPrimaries = (rl->GetHeader())->GetNtrack(); 
   if (fgDebug) 
     Info( "ReadTreeK", "Found %d particles in event # %d", fNPrimaries, EventNumber() ) ; 
 
@@ -456,97 +611,287 @@ void AliPHOSGetter::ReadPrimaries()
 }
 
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeD()
+Bool_t AliPHOSGetter::OpenESDFile() 
 {
-  // Read the Digits
+  //Open the ESD file    
+  Bool_t rv = kTRUE ; 
+  if (!fESDFile) {
+    fESDFile = TFile::Open(fESDFileName) ;
+    if (!fESDFile ) 
+      return kFALSE ; 
+  }
+  else if (fESDFile->IsOpen()) {
+    fESDFile->Close() ; 
+    fESDFile = TFile::Open(fESDFileName) ;
+  }
+  if (!fESDFile->IsOpen())
+    rv = kFALSE ; 
+  return rv ; 
+}
+
+//____________________________________________________________________________ 
+void AliPHOSGetter::FitRaw(Bool_t lowGainFlag, TGraph * gLowGain, TGraph * gHighGain, TF1* signalF, Double_t & energy, Double_t & time) const
+{
+  // Fits the raw signal time distribution 
+
+  const Int_t kNoiseThreshold = 0 ;
+  Double_t timezero1 = 0., timezero2 = 0., timemax = 0. ;
+  Double_t signal = 0., signalmax = 0. ;       
+  time   = 0. ; 
+  energy = 0. ; 
+
+  // Create a shaper pulse object which contains all the shaper parameters
+  AliPHOSPulseGenerator pulse;
+
+  if (lowGainFlag) {
+    timezero1 = timezero2 = signalmax = timemax = 0. ;
+    signalF->FixParameter(0, pulse.GetRawFormatLowCharge()) ; 
+    signalF->FixParameter(1, pulse.GetRawFormatLowGain()) ; 
+    Int_t index ; 
+    for (index = 0; index < pulse.GetRawFormatTimeBins(); index++) {
+      gLowGain->GetPoint(index, time, signal) ; 
+      if (signal > kNoiseThreshold && timezero1 == 0.) 
+       timezero1 = time ;
+      if (signal <= kNoiseThreshold && timezero1 > 0. && timezero2 == 0.)
+       timezero2 = time ; 
+      if (signal > signalmax) {
+       signalmax = signal ; 
+       timemax   = time ; 
+      }
+    }
+    signalmax /= 
+      pulse.RawResponseFunctionMax(pulse.GetRawFormatLowCharge(), 
+                                  pulse.GetRawFormatLowGain()) ;
+    if ( timezero1 + pulse.GetRawFormatTimePeak() < pulse.GetRawFormatTimeMax() * 0.4 ) { // else its noise 
+      signalF->SetParameter(2, signalmax) ; 
+      signalF->SetParameter(3, timezero1) ;                
+      gLowGain->Fit(signalF, "QRO", "", 0., timezero2); //, "QRON") ; 
+      energy = signalF->GetParameter(2) ; 
+      time   = signalF->GetMaximumX() - pulse.GetRawFormatTimePeak() - pulse.GetRawFormatTimeTrigger() ;
+    }
+  } else {
+    timezero1 = timezero2 = signalmax = timemax = 0. ;
+    signalF->FixParameter(0, pulse.GetRawFormatHighCharge()) ; 
+    signalF->FixParameter(1, pulse.GetRawFormatHighGain()) ; 
+    Int_t index ; 
+    for (index = 0; index < pulse.GetRawFormatTimeBins(); index++) {
+      gHighGain->GetPoint(index, time, signal) ;               
+      if (signal > kNoiseThreshold && timezero1 == 0.) 
+       timezero1 = time ;
+      if (signal <= kNoiseThreshold && timezero1 > 0. && timezero2 == 0.)
+       timezero2 = time ; 
+      if (signal > signalmax) {
+       signalmax = signal ;   
+       timemax   = time ; 
+      }
+    }
+    signalmax /= pulse.RawResponseFunctionMax(pulse.GetRawFormatHighCharge(), 
+                                             pulse.GetRawFormatHighGain()) ;;
+    if ( timezero1 + pulse.GetRawFormatTimePeak() < pulse.GetRawFormatTimeMax() * 0.4 ) { // else its noise  
+      signalF->SetParameter(2, signalmax) ; 
+      signalF->SetParameter(3, timezero1) ;               
+      gHighGain->Fit(signalF, "QRO", "", 0., timezero2) ; 
+      energy = signalF->GetParameter(2) ; 
+      time   = signalF->GetMaximumX() - pulse.GetRawFormatTimePeak() - pulse.GetRawFormatTimeTrigger() ;
+    }
+  }
+  if (time == 0) energy = 0 ; 
+}
+
+//____________________________________________________________________________ 
+Int_t AliPHOSGetter::CalibrateRaw(Double_t energy, Int_t *relId)
+{
+  // Convert energy into digitized amplitude for a cell relId
+  // It is a user responsilibity to open CDB and set
+  // AliPHOSCalibData object by the following operators:
+  // 
+  // AliCDBLocal *loc = new AliCDBLocal("deCalibDB");
+  // AliPHOSCalibData* clb = (AliPHOSCalibData*)AliCDBStorage::Instance()
+  //    ->Get(path_to_calibdata,run_number);
+  // AliPHOSGetter* gime = AliPHOSGetter::Instance("galice.root");
+  // gime->SetCalibData(clb);
+
+  if (CalibData() == 0)
+    Warning("CalibrateRaw","Calibration DB is not initiated!");
+
+  Int_t   module = relId[0];
+  Int_t   column = relId[3];
+  Int_t   row    = relId[2];
+
+  Float_t gainFactor = 0.0015; // width of one Emc ADC channel in GeV
+  Float_t pedestal   = 0.005;  // Emc pedestals
+
+  if(CalibData()) {
+    gainFactor = CalibData()->GetADCchannelEmc (module,column,row);
+    pedestal   = CalibData()->GetADCpedestalEmc(module,column,row);
+  }
+  
+  Int_t   amp = static_cast<Int_t>( (energy - pedestal) / gainFactor + 0.5 ) ; 
+  return amp;
+}
+//____________________________________________________________________________ 
+Int_t AliPHOSGetter::ReadRaw(AliRawReader *rawReader,Bool_t isOldRCUFormat)
+{
+  // reads the raw format data, converts it into digits format and store digits in Digits()
+  // container.
+  // isOldRCUFormat = kTRUE in case of the old RCU
+  // format used in the raw data readout.
+  // Reimplemented by Boris Polichtchouk (Jul 2006)
+  // to make it working with the Jul-Aug 2006 beam test data.
+  //Create raw decoder.
+
+  AliPHOSRawDecoder dc(rawReader);
+  dc.SetOldRCUFormat(isOldRCUFormat);
+  dc.SubtractPedestals(kTRUE);
+
+  TClonesArray * digits = Digits() ;
+  AliPHOSRawDigiProducer pr;
+  pr.MakeDigits(digits,&dc);
   
+  //ADC counts -> GeV
+  for(Int_t i=0; i<digits->GetEntries(); i++) {
+    AliPHOSDigit* digit = (AliPHOSDigit*)digits->At(i);
+    digit->SetEnergy(digit->GetEnergy()/AliPHOSPulseGenerator::GeV2ADC());
+  }
   
-  // gets TreeD from the root file (PHOS.SDigits.root)
-  if ( !IsLoaded("D") ) {
-    PhosLoader()->LoadDigits("UPDATE") ;
-    PhosLoader()->LoadDigitizer("UPDATE") ;
-    SetLoaded("D") ; 
-  } 
+  //!!!!for debug!!!
+  Int_t modMax=-111;
+  Int_t colMax=-111;
+  Int_t rowMax=-111;
+  Float_t eMax=-333;
+  //!!!for debug!!!
+
+  Int_t relId[4];
+  for(Int_t iDigit=0; iDigit<digits->GetEntries(); iDigit++) {
+    AliPHOSDigit* digit = (AliPHOSDigit*)digits->At(iDigit);
+    if(digit->GetEnergy()>eMax) {
+      PHOSGeometry()->AbsToRelNumbering(digit->GetId(),relId);
+      eMax=digit->GetEnergy();
+      modMax=relId[0];
+      rowMax=relId[2];
+      colMax=relId[3];
+    }
+  }
+
+  AliDebug(1,Form("Digit with max. energy:  modMax %d colMax %d rowMax %d  eMax %f\n\n",
+                 modMax,colMax,rowMax,eMax));
+
+  return digits->GetEntriesFast() ; 
+}
+
+//____________________________________________________________________________ 
+Int_t AliPHOSGetter::ReadTreeD() const
+{
+  // Read the Digits
+  
+  PhosLoader()->CleanDigits() ;    
+  PhosLoader()->LoadDigits("UPDATE") ;
+  PhosLoader()->LoadDigitizer("UPDATE") ;
   return Digits()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeH()
+Int_t AliPHOSGetter::ReadTreeH() const
 {
   // Read the Hits
-    
+  PhosLoader()->CleanHits() ;
   // gets TreeH from the root file (PHOS.Hit.root)
-  if ( !IsLoaded("H") ) {
+  //if ( !IsLoaded("H") ) {
     PhosLoader()->LoadHits("UPDATE") ;
-    SetLoaded("H") ; 
-  }  
+  //  SetLoaded("H") ; 
+  //}  
   return Hits()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeR()
+Int_t AliPHOSGetter::ReadTreeR() const
 {
   // Read the RecPoints
   
-  
+  PhosLoader()->CleanRecPoints() ;
   // gets TreeR from the root file (PHOS.RecPoints.root)
-  if ( !IsLoaded("R") ) {
+  //if ( !IsLoaded("R") ) {
     PhosLoader()->LoadRecPoints("UPDATE") ;
     PhosLoader()->LoadClusterizer("UPDATE") ;
-    SetLoaded("R") ; 
-  }
+    //  SetLoaded("R") ; 
+    //}
 
   return EmcRecPoints()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeT()
+Int_t AliPHOSGetter::ReadTreeT() const
 {
   // Read the TrackSegments
   
-  
+  PhosLoader()->CleanTracks() ; 
   // gets TreeT from the root file (PHOS.TrackSegments.root)
-  if ( !IsLoaded("T") ) {
+  //if ( !IsLoaded("T") ) {
     PhosLoader()->LoadTracks("UPDATE") ;
     PhosLoader()->LoadTrackSegmentMaker("UPDATE") ;
-    SetLoaded("T") ; 
-  }
+    //    SetLoaded("T") ; 
+    //}
 
   return TrackSegments()->GetEntries() ; 
 }
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeP()
+Int_t AliPHOSGetter::ReadTreeP() const
 {
-  // Read the TrackSegments
-  
+  // Read the RecParticles
   
+  PhosLoader()->CleanRecParticles() ; 
+
   // gets TreeT from the root file (PHOS.TrackSegments.root)
-  if ( !IsLoaded("P") ) {
+  //  if ( !IsLoaded("P") ) {
     PhosLoader()->LoadRecParticles("UPDATE") ;
     PhosLoader()->LoadPID("UPDATE") ;
-    SetLoaded("P") ; 
-  }
+    //  SetLoaded("P") ; 
+    //}
 
   return RecParticles()->GetEntries() ; 
 }
 //____________________________________________________________________________ 
-Int_t AliPHOSGetter::ReadTreeS()
+Int_t AliPHOSGetter::ReadTreeS() const
 {
   // Read the SDigits
   
-  
+  PhosLoader()->CleanSDigits() ; 
   // gets TreeS from the root file (PHOS.SDigits.root)
-  if ( !IsLoaded("S") ) {
-    PhosLoader()->LoadSDigits("UPDATE") ;
-    PhosLoader()->LoadSDigitizer("UPDATE") ;
-    SetLoaded("S") ; 
-  }
+  //if ( !IsLoaded("S") ) {
+    PhosLoader()->LoadSDigits("READ") ;
+    PhosLoader()->LoadSDigitizer("READ") ;
+    //  SetLoaded("S") ; 
+    //}
 
   return SDigits()->GetEntries() ; 
 }
 
 //____________________________________________________________________________ 
-TClonesArray * AliPHOSGetter::SDigits() 
+Int_t AliPHOSGetter::ReadTreeE(Int_t event)
+{
+  // Read the ESD
+  
+  // gets esdTree from the root file (AliESDs.root)
+  if (!fESDFile)
+    if ( !OpenESDFile() ) 
+      return -1 ; 
+
+  fESDTree = static_cast<TTree*>(fESDFile->Get("esdTree")) ; 
+  fESD = new AliESD;
+   if (!fESDTree) {
+
+     Error("ReadTreeE", "no ESD tree found");
+     return -1;
+   }
+   fESDTree->SetBranchAddress("ESD", &fESD);
+   fESDTree->GetEvent(event);
+
+   return event ; 
+}
+
+//____________________________________________________________________________ 
+TClonesArray * AliPHOSGetter::SDigits() const
 {
   // asks the Loader to return the Digits container 
 
@@ -561,8 +906,9 @@ TClonesArray * AliPHOSGetter::SDigits()
 }
 
 //____________________________________________________________________________ 
-AliPHOSSDigitizer * AliPHOSGetter::SDigitizer() 
+AliPHOSSDigitizer * AliPHOSGetter::SDigitizer()
 { 
+  // Returns pointer to the SDigitizer task 
   AliPHOSSDigitizer * rv ; 
   rv =  dynamic_cast<AliPHOSSDigitizer *>(PhosLoader()->SDigitizer()) ;
   if (!rv) {
@@ -573,7 +919,7 @@ AliPHOSSDigitizer * AliPHOSGetter::SDigitizer()
 }
 
 //____________________________________________________________________________ 
-TParticle * AliPHOSGetter::Secondary(const TParticle* p, const Int_t index) const
+TParticle * AliPHOSGetter::Secondary(const TParticle* p, Int_t index) const
 {
   // Return first (index=1) or second (index=2) secondary particle of primary particle p 
 
@@ -585,14 +931,14 @@ TParticle * AliPHOSGetter::Secondary(const TParticle* p, const Int_t index) cons
   if(p) {
   Int_t daughterIndex = p->GetDaughter(index-1) ; 
   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
-  return  rl->GetAliRun()->Particle(daughterIndex) ; 
+  return  rl->GetAliRun()->GetMCApp()->Particle(daughterIndex) ; 
   }
   else
     return 0 ;
 }
 
 //____________________________________________________________________________ 
-void AliPHOSGetter::Track(const Int_t itrack) 
+void AliPHOSGetter::Track(Int_t itrack) 
 {
   // Read the first entry of PHOS branch in hit tree gAlice->TreeH()
  
@@ -614,6 +960,7 @@ void AliPHOSGetter::Track(const Int_t itrack)
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeD() const 
 {
+  // Returns pointer to the Digits Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeD() ; 
   if ( !rv ) {
@@ -627,6 +974,7 @@ TTree * AliPHOSGetter::TreeD() const
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeH() const 
 {
+  // Returns pointer to the Hits Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeH() ; 
   if ( !rv ) {
@@ -640,6 +988,7 @@ TTree * AliPHOSGetter::TreeH() const
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeR() const 
 {
+  // Returns pointer to the RecPoints Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeR() ; 
   if ( !rv ) {
@@ -653,6 +1002,7 @@ TTree * AliPHOSGetter::TreeR() const
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeT() const 
 {
+  // Returns pointer to the TrackSegments Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeT() ; 
   if ( !rv ) {
@@ -665,6 +1015,7 @@ TTree * AliPHOSGetter::TreeT() const
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeP() const 
 {
+  // Returns pointer to the RecParticles  Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeP() ; 
   if ( !rv ) {
@@ -677,7 +1028,8 @@ TTree * AliPHOSGetter::TreeP() const
 
 //____________________________________________________________________________ 
 TTree * AliPHOSGetter::TreeS() const 
-{
+{ 
+ // Returns pointer to the SDigits Tree
   TTree * rv = 0 ; 
   rv = PhosLoader()->TreeS() ; 
   if ( !rv ) {
@@ -703,7 +1055,7 @@ Bool_t AliPHOSGetter::VersionExists(TString & opt) const
   if ( opt == "sdigits") {
     // add the version name to the root file name
     TString fileName( PhosLoader()->GetSDigitsFileName() ) ; 
-    if (version != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
+    if (version != AliConfig::GetDefaultEventFolderName()) // 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()) ;
@@ -715,13 +1067,12 @@ Bool_t AliPHOSGetter::VersionExists(TString & opt) const
   if ( opt == "digits") {
     // add the version name to the root file name
     TString fileName( PhosLoader()->GetDigitsFileName() ) ; 
-    if (version != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
+    if (version != AliConfig::GetDefaultEventFolderName()) // 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 ; 
     }
-    PhosLoader()->SetDigitsFileName(fileName) ;
   }
 
   return rv ;
@@ -746,3 +1097,11 @@ Float_t AliPHOSGetter::BeamEnergy(void) const
   else
     return 0 ;
 }
+//____________________________________________________________________________ 
+
+AliPHOSCalibData* AliPHOSGetter::CalibData()
+{ 
+  // Check if the instance of AliPHOSCalibData exists, and return it
+
+  return fgCalibData;
+}