]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALDigitizer.cxx
Add class to access simulation parameters, AliEMCALSimParam, to be set in configurati...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALDigitizer.cxx
index 85207b838d3e71fab799ef53c02911b90f3105fc..e3ab535fa03d7d5f46145ce13135d2cbf897c061 100644 (file)
@@ -18,7 +18,7 @@
 //_________________________________________________________________________
 // 
 //////////////////////////////////////////////////////////////////////////////
-// Class performs digitization of Summable digits 
+// Class performs digitization of Summable digits from simulated data
 //  
 // In addition it performs mixing of summable digits from different events.
 //
 #include <TTree.h>
 #include <TSystem.h>
 #include <TBenchmark.h>
-#include <TList.h>
-#include <TH1.h>
 #include <TBrowser.h>
 #include <TObjectTable.h>
 #include <TRandom.h>
+#include <cassert>
 
 // --- AliRoot header files ---
 #include "AliLog.h"
 #include "AliRun.h"
 #include "AliRunDigitizer.h"
 #include "AliRunLoader.h"
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
 #include "AliEMCALDigit.h"
 #include "AliEMCAL.h"
 #include "AliEMCALLoader.h"
@@ -81,7 +82,8 @@
 #include "AliEMCALSDigitizer.h"
 #include "AliEMCALGeometry.h"
 #include "AliEMCALTick.h"
-#include "AliEMCALHistoUtilities.h"
+#include "AliEMCALCalibData.h"
+#include "AliEMCALSimParam.h"
 
 ClassImp(AliEMCALDigitizer)
 
@@ -97,24 +99,23 @@ AliEMCALDigitizer::AliEMCALDigitizer()
     fEventNames(0x0),
     fDigitThreshold(0),
     fMeanPhotonElectron(0),
-    fPedestal(0),
-    fSlope(0),
+//    fPedestal(0), //Not used, remove?
+//    fSlope(0),    //Not used, remove?
     fPinNoise(0),
     fTimeResolution(0),
-    fTimeThreshold(0),    
-    fTimeSignalLength(0),
+//    fTimeThreshold(0),    //Not used, remove?
+//    fTimeSignalLength(0), //Not used, remove?
     fADCchannelEC(0),
     fADCpedestalEC(0),
     fNADCEC(0),
     fEventFolderName(""),
     fFirstEvent(0),
     fLastEvent(0),
-    fControlHists(0),
-    fHists(0)
+    fCalibData(0x0)
 {
   // ctor
   InitParameters() ; 
-  fManager = 0 ;                     // We work in the standalong mode
+  fManager = 0 ;                     // We work in the standalone mode
 }
 
 //____________________________________________________________________________ 
@@ -128,25 +129,24 @@ AliEMCALDigitizer::AliEMCALDigitizer(TString alirunFileName, TString eventFolder
     fEventNames(0), 
     fDigitThreshold(0),
     fMeanPhotonElectron(0),
-    fPedestal(0),
-    fSlope(0),
+//    fPedestal(0),//Not used, remove?
+//    fSlope(0),   //Not used, remove?
     fPinNoise(0),
     fTimeResolution(0),
-    fTimeThreshold(0),
-    fTimeSignalLength(0),
+//    fTimeThreshold(0),    //Not used, remove?
+//    fTimeSignalLength(0), //Not used, remove?
     fADCchannelEC(0),
     fADCpedestalEC(0),
     fNADCEC(0),
     fEventFolderName(eventFolderName),
     fFirstEvent(0),
     fLastEvent(0),
-    fControlHists(0),
-    fHists(0)
+    fCalibData(0x0)
 {
   // ctor
   InitParameters() ; 
   Init() ;
-  fManager = 0 ;                     // We work in the standalong mode
+  fManager = 0 ;                     // We work in the standalone mode
 }
 
 //____________________________________________________________________________ 
@@ -160,20 +160,19 @@ AliEMCALDigitizer::AliEMCALDigitizer(const AliEMCALDigitizer & d)
     fEventNames(d.fEventNames),
     fDigitThreshold(d.fDigitThreshold),
     fMeanPhotonElectron(d.fMeanPhotonElectron),
-    fPedestal(d.fPedestal),
-    fSlope(d.fSlope),
+//    fPedestal(d.fPedestal), //Not used, remove?
+//    fSlope(d.fSlope),       //Not used, remove?
     fPinNoise(d.fPinNoise),
     fTimeResolution(d.fTimeResolution),
-    fTimeThreshold(d.fTimeThreshold),
-    fTimeSignalLength(d.fTimeSignalLength),
+//    fTimeThreshold(d.fTimeThreshold),       //Not used, remove?
+//    fTimeSignalLength(d.fTimeSignalLength), //Not used, remove?
     fADCchannelEC(d.fADCchannelEC),
     fADCpedestalEC(d.fADCpedestalEC),
     fNADCEC(d.fNADCEC),
     fEventFolderName(d.fEventFolderName),
     fFirstEvent(d.fFirstEvent),
     fLastEvent(d.fLastEvent),
-    fControlHists(d.fControlHists),
-    fHists(d.fHists)
+    fCalibData(d.fCalibData)
 {
   // copyy ctor 
  }
@@ -189,20 +188,19 @@ AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd)
     fEventNames(0),
     fDigitThreshold(0.),
     fMeanPhotonElectron(0),
-    fPedestal(0),
-    fSlope(0.),
+//    fPedestal(0), //Not used, remove?
+//    fSlope(0.),   //Not used, remove?
     fPinNoise(0),
     fTimeResolution(0.),
-    fTimeThreshold(0),
-    fTimeSignalLength(0),
+//    fTimeThreshold(0),    //Not used, remove?
+//    fTimeSignalLength(0), //Not used, remove?
     fADCchannelEC(0),
     fADCpedestalEC(0),
     fNADCEC(0),
     fEventFolderName(0),
     fFirstEvent(0),
     fLastEvent(0),
-    fControlHists(0),
-    fHists(0)
+    fCalibData(0x0)
 {
   // ctor Init() is called by RunDigitizer
   fManager = rd ; 
@@ -215,9 +213,9 @@ AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd)
   AliEMCALDigitizer::~AliEMCALDigitizer()
 {
   //dtor
-  if (AliRunLoader::GetRunLoader()) {
+  if (AliRunLoader::Instance()) {
     AliLoader *emcalLoader=0;
-    if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
+    if ((emcalLoader = AliRunLoader::Instance()->GetDetectorLoader("EMCAL")))
       emcalLoader->CleanDigitizer();
   }
   else
@@ -225,7 +223,6 @@ AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd)
   delete [] fInputFileNames ; 
   delete [] fEventNames ; 
 
-  if(fHists) delete fHists;
 }
 
 //____________________________________________________________________________
@@ -234,14 +231,18 @@ void AliEMCALDigitizer::Digitize(Int_t event)
 
   // Makes the digitization of the collected summable digits
   // for this it first creates the array of all EMCAL modules
-  // filled with noise (different for EMC, CPV and PPSD) and
-  // after that adds contributions from SDigits. This design 
-  // helps to avoid scanning over the list of digits to add 
-  // contribution of any new SDigit.
-  static int isTrd1Geom = -1; // -1 - mean undefined 
+  // filled with noise and after that adds contributions from 
+  // SDigits. This design helps to avoid scanning over the 
+  // list of digits to add  contribution of any new SDigit.
+  //
+  // JLK 26-Jun-2008
+  // Note that SDigit energy info is stored as an amplitude, so we
+  // must call the Calibrate() method of the SDigitizer to convert it
+  // back to an energy in GeV before adding it to the Digit
+  //
   static int nEMC=0; //max number of digits possible
 
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
+  AliRunLoader *rl = AliRunLoader::Instance();
   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
   Int_t readEvent = event ; 
   // fManager is data member from AliDigitizer
@@ -252,24 +253,21 @@ void AliEMCALDigitizer::Digitize(Int_t event)
   rl->GetEvent(readEvent);
 
   TClonesArray * digits = emcalLoader->Digits() ; 
-  digits->Clear() ;
+  digits->Delete() ;  //correct way to clear array when memory is
+                     //allocated by objects stored in array
 
   // Load Geometry
-  // const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
-  rl->LoadgAlice(); 
-  AliRun * gAlice = rl->GetAliRun(); 
-  AliEMCAL * emcal  = (AliEMCAL*)gAlice->GetDetector("EMCAL");
-  AliEMCALGeometry * geom = emcal->GetGeometry();
-
-  if(isTrd1Geom < 0) { 
-    TString ng(geom->GetName());
-    isTrd1Geom = 0;
-    if(ng.Contains("SHISH") &&  ng.Contains("TRD1")) isTrd1Geom = 1;
-
-    if(isTrd1Geom == 0) nEMC = geom->GetNPhi()*geom->GetNZ();
-    else                nEMC = geom->GetNCells();
-    AliDebug(1,Form("nEMC %i (number cells in EMCAL) | %s | isTrd1Geom %i\n", nEMC, geom->GetName(), isTrd1Geom));
+  AliEMCALGeometry *geom = 0;
+  if (rl->GetAliRun()) {
+    AliEMCAL * emcal  = (AliEMCAL*)rl->GetAliRun()->GetDetector("EMCAL");
+    geom = emcal->GetGeometry();
   }
+  else 
+    AliFatal("Could not get AliRun from runLoader");
+
+  nEMC = geom->GetNCells();
+  AliDebug(1,Form("nEMC %i (number cells in EMCAL) | %s \n", nEMC, geom->GetName()));
+  
   Int_t absID ;
 
   digits->Expand(nEMC) ;
@@ -326,26 +324,34 @@ void AliEMCALDigitizer::Digitize(Int_t event)
   AliEMCALDigit * digit ;
   AliEMCALDigit * curSDigit ;
 
-  TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
+  //  TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
 
   //Put Noise contribution
-  for(absID = 1; absID <= nEMC; absID++){
-    Float_t amp = 0 ;
+  for(absID = 0; absID < nEMC; absID++){ // Nov 30, 2006 by PAI; was from 1 to nEMC
+    Float_t energy = 0 ;
     // amplitude set to zero, noise will be added later
-    new((*digits)[absID-1]) AliEMCALDigit( -1, -1, absID, 0, TimeOfNoise() ) ;
+    new((*digits)[absID]) AliEMCALDigit( -1, -1, absID, 0, TimeOfNoise() ); // absID-1->absID
     //look if we have to add signal?
-    digit = dynamic_cast<AliEMCALDigit *>(digits->At(absID-1)) ;
+    digit = dynamic_cast<AliEMCALDigit *>(digits->At(absID)); // absID-1->absID
     
     if(absID==nextSig){
       //Add SDigits from all inputs    
-      ticks->Clear() ;
-      Int_t contrib = 0 ;
-      Float_t a = digit->GetAmp() ;
-      Float_t b = TMath::Abs( a /fTimeSignalLength) ;
+      //      ticks->Clear() ;
+      //Int_t contrib = 0 ;
+
+      //Follow PHOS and comment out this timing model til a better one
+      //can be developed - JLK 28-Apr-2008
+
+      //Float_t a = digit->GetAmp() ;
+      //Float_t b = TMath::Abs( a /fTimeSignalLength) ;
       //Mark the beginning of the signal
-      new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);  
+      //new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);  
       //Mark the end of the signal     
-      new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
+      //new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
+
+      // Calculate time as time of the largest digit
+      Float_t time = digit->GetTime() ;
+      Float_t aTime= digit->GetAmp() ;
       
       // loop over input
       for(i = 0; i< fInput ; i++){  //loop over (possible) merge sources
@@ -362,13 +368,18 @@ void AliEMCALDigitizer::Digitize(Int_t event)
          else
            primaryoffset = i ;
          curSDigit->ShiftPrimary(primaryoffset) ;
-         
-         a = curSDigit->GetAmp() ;
-         b = a /fTimeSignalLength ;
-         new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);  
-         new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
 
-         *digit = *digit + *curSDigit ;  //add energies
+         //Remove old timing model - JLK 28-April-2008
+         //a = curSDigit->GetAmp() ;
+         //b = a /fTimeSignalLength ;
+         //new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);  
+         //new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
+         if(curSDigit->GetAmp()>aTime) {
+           aTime = curSDigit->GetAmp();
+           time = curSDigit->GetTime();
+         }
+
+         *digit = *digit + *curSDigit ;  //adds amplitudes of each digit
 
          index[i]++ ;
          if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
@@ -377,12 +388,14 @@ void AliEMCALDigitizer::Digitize(Int_t event)
            curSDigit = 0 ;
        }
       }
+      //Here we convert the summed amplitude to an energy in GeV
+      energy = sDigitizer->Calibrate(digit->GetAmp()) ; // GeV
       // add fluctuations for photo-electron creation
-      amp = sDigitizer->Calibrate(digit->GetAmp()) ; // GeV
-      amp *= static_cast<Float_t>(gRandom->Poisson(fMeanPhotonElectron)) / static_cast<Float_t>(fMeanPhotonElectron) ;
+      energy *= static_cast<Float_t>(gRandom->Poisson(fMeanPhotonElectron)) / static_cast<Float_t>(fMeanPhotonElectron) ;
   
       //calculate and set time
-      Float_t time = FrontEdgeTime(ticks) ;
+      //New timing model needed - JLK 28-April-2008
+      //Float_t time = FrontEdgeTime(ticks) ;
       digit->SetTime(time) ;
 
       //Find next signal module
@@ -397,21 +410,27 @@ void AliEMCALDigitizer::Digitize(Int_t event)
       }
     }
     // add the noise now
-    amp += TMath::Abs(gRandom->Gaus(0., fPinNoise)) ;
-    digit->SetAmp(sDigitizer->Digitize(amp)) ;  
-    AliDebug(10,Form(" absID %5i amp %f nextSig %5i\n",
-                    absID, amp, nextSig));
-  } // for(absID = 1; absID <= nEMC; absID++)
+    energy += TMath::Abs(gRandom->Gaus(0., fPinNoise)) ;
+    // JLK 26-June-2008
+    //Now digitize the energy according to the sDigitizer method,
+    //which merely converts the energy to an integer.  Later we will
+    //check that the stored value matches our allowed dynamic ranges
+    digit->SetAmp(sDigitizer->Digitize(energy)) ;  
+    AliDebug(10,Form(" absID %5i energy %f nextSig %5i\n",
+                    absID, energy, nextSig));
+  } // for(absID = 0; absID < nEMC; absID++)
   
-  ticks->Delete() ;
-  delete ticks ;
+  //ticks->Delete() ;
+  //delete ticks ;
 
+  //JLK is it better to call Clear() here?
   delete sdigArray ; //We should not delete its contents
 
   //remove digits below thresholds
   for(i = 0 ; i < nEMC ; i++){
     digit = dynamic_cast<AliEMCALDigit*>( digits->At(i) ) ;
-    Float_t threshold = fDigitThreshold ; 
+    Float_t threshold = fDigitThreshold ; //this is in GeV
+    //need to calibrate digit amplitude to energy in GeV for comparison
     if(sDigitizer->Calibrate( digit->GetAmp() ) < threshold)
       digits->RemoveAt(i) ;
     else 
@@ -421,60 +440,53 @@ void AliEMCALDigitizer::Digitize(Int_t event)
   digits->Compress() ;  
   
   Int_t ndigits = digits->GetEntriesFast() ; 
-  digits->Expand(ndigits) ;
-  
-  //Set indexes in list of digits and fill hists.
-  AliEMCALHistoUtilities::FillH1(fHists, 0, Double_t(ndigits));
-  Float_t energy=0., esum=0.;
+
+  //JLK 26-June-2008
+  //After we have done the summing and digitizing to create the
+  //digits, now we want to calibrate the resulting amplitude to match
+  //the dynamic range of our real data.  
+  Float_t energy=0;
   for (i = 0 ; i < ndigits ; i++) { 
     digit = dynamic_cast<AliEMCALDigit *>( digits->At(i) ) ; 
     digit->SetIndexInList(i) ; 
     energy = sDigitizer->Calibrate(digit->GetAmp()) ;
-    esum += energy;
-    digit->SetAmp(DigitizeEnergy(energy, digit->GetId()) ) ; // for what ??
-    AliEMCALHistoUtilities::FillH1(fHists, 2, double(digit->GetAmp()));
-    AliEMCALHistoUtilities::FillH1(fHists, 3, double(energy));
-    AliEMCALHistoUtilities::FillH1(fHists, 4, double(digit->GetId()));
+    digit->SetAmp(DigitizeEnergy(energy, digit->GetId()) ) ;
   }
-  AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
+
 }
 
 // //_____________________________________________________________________
 Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy, Int_t AbsId)
 { 
+  // JLK 26-June-2008
   // Returns digitized value of the energy in a cell absId
-  // Loader
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>
-    (rl->GetDetectorLoader("EMCAL"));
-  
-  // Load EMCAL Geometry
-  rl->LoadgAlice(); 
-  AliRun * gAlice = rl->GetAliRun(); 
-  AliEMCAL * emcal  = (AliEMCAL*)gAlice->GetDetector("EMCAL");
-  AliEMCALGeometry * geom = emcal->GetGeometry();
+  // using the calibration constants stored in the OCDB
+  // or default values if no CalibData object is found.
+  // This effectively converts everything to match the dynamic range
+  // of the real data we will collect
+  //
+  // Load Geometry
+  const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
 
   if (geom==0)
-    AliFatal("Did not get geometry from EMCALLoader") ;
+    AliFatal("Did not get geometry from EMCALLoader");
 
   Int_t iSupMod = -1;
-  Int_t nTower  = -1;
+  Int_t nModule  = -1;
   Int_t nIphi   = -1;
   Int_t nIeta   = -1;
   Int_t iphi    = -1;
   Int_t ieta    = -1;
   Int_t channel = -999; 
 
-  Bool_t bCell = geom->GetCellIndex(AbsId, iSupMod, nTower, nIphi, nIeta) ;
+  Bool_t bCell = geom->GetCellIndex(AbsId, iSupMod, nModule, nIphi, nIeta) ;
   if(!bCell)
-    Error("DigitizeEnergy","Wrong cell id number") ;
-  geom->GetCellPhiEtaIndexInSModule(iSupMod,nTower,nIphi, nIeta,iphi,ieta);
+    Error("DigitizeEnergy","Wrong cell id number : AbsId %i ", AbsId) ;
+  geom->GetCellPhiEtaIndexInSModule(iSupMod,nModule,nIphi, nIeta,iphi,ieta);
   
-  if(emcalLoader->CalibData()) {
-    fADCpedestalEC = emcalLoader->CalibData()
-      ->GetADCpedestal(iSupMod,ieta,iphi);
-    fADCchannelEC = emcalLoader->CalibData()
-      ->GetADCchannel(iSupMod,ieta,iphi);
+  if(fCalibData) {
+    fADCpedestalEC = fCalibData->GetADCpedestal(iSupMod,ieta,iphi);
+    fADCchannelEC = fCalibData->GetADCchannel(iSupMod,ieta,iphi);
   }
   
   channel = static_cast<Int_t> (TMath::Ceil( (energy + fADCpedestalEC)/fADCchannelEC ))  ;
@@ -508,14 +520,15 @@ void AliEMCALDigitizer::Exec(Option_t *option)
   if(strstr(option,"tim"))
     gBenchmark->Start("EMCALDigitizer");
 
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
+  AliRunLoader *rl = AliRunLoader::Instance();
   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
 
   // Post Digitizer to the white board
   emcalLoader->PostDigitizer(this) ;
   
-  if (fLastEvent == -1) 
+  if (fLastEvent == -1)  {
     fLastEvent = rl->GetNumberOfEvents() - 1 ;
+  }
   else if (fManager) 
     fLastEvent = fFirstEvent ; // what is this ??
 
@@ -549,33 +562,34 @@ void AliEMCALDigitizer::Exec(Option_t *option)
 }
 
 //____________________________________________________________________________ 
-Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks) 
-{ 
-  //  Returns the shortest time among all time ticks
-
-  ticks->Sort() ; //Sort in accordance with times of ticks
-  TIter it(ticks) ;
-  AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
-  Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
-  
-  AliEMCALTick * t ;  
-  while((t=(AliEMCALTick*) it.Next())){
-    if(t->GetTime() < time)  //This tick starts before crossing
-      *ctick+=*t ;
-    else
-      return time ;
-    
-    time = ctick->CrossingTime(fTimeThreshold) ;    
-  }
-  return time ;
-}
+//Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks) 
+//{ 
+//  //  Returns the shortest time among all time ticks
+//
+//  ticks->Sort() ; //Sort in accordance with times of ticks
+//  TIter it(ticks) ;
+//  AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
+//  Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
+//  
+//  AliEMCALTick * t ;  
+//  while((t=(AliEMCALTick*) it.Next())){
+//    if(t->GetTime() < time)  //This tick starts before crossing
+//      *ctick+=*t ;
+//    else
+//      return time ;
+//    
+//    time = ctick->CrossingTime(fTimeThreshold) ;    
+//  }
+//  return time ;
+//}
+//
 
 //____________________________________________________________________________ 
 Bool_t AliEMCALDigitizer::Init()
 {
   // Makes all memory allocations
   fInit = kTRUE ; 
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
 
   if ( emcalLoader == 0 ) {
     Fatal("Init", "Could not obtain the AliEMCALLoader");  
@@ -604,33 +618,37 @@ Bool_t AliEMCALDigitizer::Init()
   //to prevent cleaning of this object while GetEvent is called
   emcalLoader->GetDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
 
-  //PH  Print();
-  
+  //Calibration instance
+  fCalibData = emcalLoader->CalibData();
   return fInit ;    
 }
 
 //____________________________________________________________________________ 
 void AliEMCALDigitizer::InitParameters()
 { 
-  //parameter initialization for digitizer
-  // Tune parameters - 24-nov-04
-
-  fMeanPhotonElectron = 3300 ; // electrons per GeV 
-  fPinNoise           = 0.004; 
-  if (fPinNoise == 0. ) 
+  // Parameter initialization for digitizer
+  fMeanPhotonElectron = AliEMCALSimParam::GetInstance()->GetMeanPhotonElectron();//4400;  // electrons per GeV 
+  fPinNoise           = AliEMCALSimParam::GetInstance()->GetPinNoise();//0.012; // pin noise in GeV from analysis test beam data 
+  if (fPinNoise < 0.0001 ) 
     Warning("InitParameters", "No noise added\n") ; 
-  fDigitThreshold     = fPinNoise * 3; // 3 * sigma
-  fTimeResolution     = 0.3e-9 ; // 300 psc
-  fTimeSignalLength   = 1.0e-9 ;
-
-  fADCchannelEC    = 0.00305; // 200./65536 - width of one ADC channel in GeV
-  fADCpedestalEC   = 0.009 ;  // GeV
-  fNADCEC          = (Int_t) TMath::Power(2,16) ;  // number of channels in Tower ADC - 65536
-
-  fTimeThreshold      = 0.001*10000000 ; // Means 1 MeV in terms of SDigits amplitude ??
-  // hists. for control; no hists on default
-  fControlHists = 0;
-  fHists        = 0;
+  fDigitThreshold     = AliEMCALSimParam::GetInstance()->GetDigitThreshold(); //fPinNoise * 3; // 3 * sigma
+  fTimeResolution     = AliEMCALSimParam::GetInstance()->GetTimeResolution(); //0.6e-9 ; // 600 psc
+
+  // These defaults are normally not used. 
+  // Values are read from calibration database instead
+  fADCchannelEC       = 0.0153; // Update 24 Apr 2007: 250./16/1024 - width of one ADC channel in GeV
+  fADCpedestalEC      = 0.0 ;  // GeV
+
+  fNADCEC          = AliEMCALSimParam::GetInstance()->GetNADCEC();//(Int_t) TMath::Power(2,16) ;  // number of channels in Tower ADC - 65536
+
+  AliDebug(2,Form("Mean Photon Electron %d, Noise %f, E Threshold %f,Time Resolution %g,NADCEC %d",
+               fMeanPhotonElectron,fPinNoise,fDigitThreshold,fTimeResolution,fNADCEC));
+
+  // Not used anymore, remove?
+  // fTimeSignalLength   = 1.0e-9 ;
+  // fTimeThreshold      = 0.001*10000000 ; // Means 1 MeV in terms of SDigits amplitude ??
+
 }
 
 //__________________________________________________________________
@@ -661,7 +679,7 @@ void AliEMCALDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
     return ; 
   }
   // looking for the file which contains SDigits
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
   TString fileName( emcalLoader->GetSDigitsFileName() ) ; 
     if ( eventFolderName != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name 
       fileName = fileName.ReplaceAll(".root", "") + "_" + eventFolderName + ".root" ;
@@ -670,6 +688,7 @@ void AliEMCALDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
       return ;
     }
     // need to increase the arrays
+    // MvL: This code only works when fInput == 1, I think.
     TString tempo = fInputFileNames[fInput-1] ; 
     delete [] fInputFileNames ; 
     fInputFileNames = new TString[fInput+1] ; 
@@ -685,6 +704,7 @@ void AliEMCALDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
     fInput++ ;
 }  
 
+//__________________________________________________________________
 void AliEMCALDigitizer::Print1(Option_t * option)
 { // 19-nov-04 - just for convinience
   Print(); 
@@ -721,7 +741,7 @@ void AliEMCALDigitizer::Print(Option_t*)const
       printf ("Adding SDigits from %s %s\n", fInputFileNames[index].Data(), fileName.Data()) ; 
     }
 
-    AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+    AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
 
     printf("\nWriting digits to %s", emcalLoader->GetDigitsFileName().Data()) ;
     
@@ -740,7 +760,7 @@ void AliEMCALDigitizer::PrintDigits(Option_t * option)
 {
   //utility method for printing digit information
 
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
   TClonesArray * digits  = emcalLoader->Digits() ;
   TClonesArray * sdigits = emcalLoader->SDigits() ;
   
@@ -787,7 +807,7 @@ void AliEMCALDigitizer::Unload()
     if ((rl = AliRunLoader::GetRunLoader(tempo))) 
       rl->GetDetectorLoader("EMCAL")->UnloadSDigits() ; 
   }
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
   emcalLoader->UnloadDigits() ; 
 }
 
@@ -803,7 +823,7 @@ void AliEMCALDigitizer::WriteDigits()
   //      and branch "AliEMCALDigitizer", with the same title to keep all the parameters
   //      and names of files, from which digits are made.
 
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
+  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
 
   const TClonesArray * digits = emcalLoader->Digits() ; 
   TTree * treeD = emcalLoader->TreeD(); 
@@ -815,8 +835,10 @@ void AliEMCALDigitizer::WriteDigits()
   // -- create Digits branch
   Int_t bufferSize = 32000 ;    
   TBranch * digitsBranch = 0;
-  if ((digitsBranch = treeD->GetBranch("EMCAL")))
+  if ((digitsBranch = treeD->GetBranch("EMCAL"))) {
     digitsBranch->SetAddress(&digits);
+    AliWarning("Digits Branch already exists. Not all digits will be visible");
+  }
   else
     treeD->Branch("EMCAL","TClonesArray",&digits,bufferSize);
   //digitsBranch->SetTitle(fEventFolderName);
@@ -829,36 +851,8 @@ void AliEMCALDigitizer::WriteDigits()
 
 }
 
+//__________________________________________________________________
 void AliEMCALDigitizer::Browse(TBrowser* b)
 {
-  if(fHists) b->Add(fHists);
   TTask::Browse(b);
 }
-
-TList *AliEMCALDigitizer::BookControlHists(int var)
-{ 
-  // 22-nov-04
-  // histograms for monitoring digitizer performance
-
-  Info("BookControlHists"," started ");
-  gROOT->cd();
-  const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
-  if(var>=1){
-    new TH1F("hDigiN",  "#EMCAL digits with fAmp > fDigitThreshold", 
-    fNADCEC+1, -0.5, Double_t(fNADCEC));
-    new TH1F("HDigiSumEnergy","Sum.EMCAL energy from digi", 1000, 0.0, 200.);
-    new TH1F("hDigiAmp",  "EMCAL digital amplitude", fNADCEC+1, -0.5, Double_t(fNADCEC));
-    new TH1F("hDigiEnergy","EMCAL cell energy", 2000, 0.0, 200.);
-    new TH1F("hDigiAbsId","EMCAL absId cells with fAmp > fDigitThreshold ",
-    geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
-  }
-
-  fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalDigiControlHists", kFALSE);
-  fHists = 0; //huh? JLK 03-Mar-2006
-  return fHists;
-}
-
-void AliEMCALDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
-{
-  AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt); 
-}