]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALRecoUtils.cxx
Coverity fixes
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRecoUtils.cxx
index 3ffd81e4f3f05a05a66d96642384b6ed6a31b632..526224ddf17bfc31ee94b6e484859b165d328e91 100644 (file)
@@ -22,8 +22,9 @@
 //
 //
 // Author:  Gustavo Conesa (LPSC- Grenoble) 
-///////////////////////////////////////////////////////////////////////////////
+//          Track matching part: Rongrong Ma (Yale)
 
+///////////////////////////////////////////////////////////////////////////////
 // --- standard c ---
 
 // standard C++ includes
 #include <TGeoManager.h>
 #include <TGeoMatrix.h>
 #include <TGeoBBox.h>
+#include <TH2F.h>
+#include <TArrayI.h>
+#include <TArrayF.h>
+#include <TObjArray.h>
 
 // STEER includes
-#include "AliEMCALRecoUtils.h"
-#include "AliEMCALGeometry.h"
 #include "AliVCluster.h"
 #include "AliVCaloCells.h"
-#include "AliVEvent.h"
 #include "AliLog.h"
-#include "AliEMCALPIDUtils.h"
 #include "AliPID.h"
 #include "AliESDEvent.h"
+#include "AliAODEvent.h"
 #include "AliESDtrack.h"
-#include "AliEMCALTrack.h"
+#include "AliAODTrack.h"
+#include "AliExternalTrackParam.h"
+#include "AliESDfriendTrack.h"
+#include "AliTrackerBase.h"
+
+// EMCAL includes
+#include "AliEMCALRecoUtils.h"
+#include "AliEMCALGeometry.h"
+#include "AliTrackerBase.h"
+#include "AliEMCALCalibTimeDepCorrection.h" // Run dependent
+#include "AliEMCALPIDUtils.h"
+
 
 ClassImp(AliEMCALRecoUtils)
   
-//______________________________________________
+//_____________________________________
 AliEMCALRecoUtils::AliEMCALRecoUtils():
-  fNonLinearityFunction (kNoCorrection), fParticleType(kPhoton),
-  fPosAlgo(kUnchanged),fW0(4.),
-  fRecalibration(kFALSE), fEMCALRecalibrationFactors(),
-  fRemoveBadChannels(kFALSE), fRecalDistToBadChannels(kFALSE), fEMCALBadChannelMap(),
-  fNCellsFromEMCALBorder(0), fNoEMCALBorderAtEta0(kTRUE),
-  fMatchedClusterIndex(0x0), fResidualZ(0x0), fResidualR(0x0), fCutR(20), fCutZ(20),
-  fCutMinNClusterTPC(0), fCutMinNClusterITS(0), fCutMaxChi2PerClusterTPC(0), fCutMaxChi2PerClusterITS(0),
-  fCutRequireTPCRefit(0), fCutRequireITSRefit(0), fCutAcceptKinkDaughters(0),
-  fCutMaxDCAToVertexXY(0), fCutMaxDCAToVertexZ(0),fCutDCAToVertex2D(0),
-  fPIDUtils()
+  fParticleType(0),                       fPosAlgo(0),                            fW0(0), 
+  fNonLinearityFunction(0),               fNonLinearThreshold(0),
+  fSmearClusterEnergy(kFALSE),            fRandom(),
+  fCellsRecalibrated(kFALSE),             fRecalibration(kFALSE),                 fEMCALRecalibrationFactors(),
+  fTimeRecalibration(kFALSE),             fEMCALTimeRecalibrationFactors(),
+  fUseRunCorrectionFactors(kFALSE),       fRunCorrectionFactorsSet(kFALSE),
+  fRemoveBadChannels(kFALSE),             fRecalDistToBadChannels(kFALSE),        fEMCALBadChannelMap(),
+  fNCellsFromEMCALBorder(0),              fNoEMCALBorderAtEta0(kTRUE),
+  fRejectExoticCluster(kFALSE),           fRejectExoticCells(kFALSE), 
+  fExoticCellFraction(0),                 fExoticCellDiffTime(0),                 fExoticCellMinAmplitude(0),
+  fPIDUtils(),                            fAODFilterMask(0),
+  fMatchedTrackIndex(0x0),                fMatchedClusterIndex(0x0), 
+  fResidualEta(0x0), fResidualPhi(0x0),   fCutEtaPhiSum(kFALSE),                  fCutEtaPhiSeparate(kFALSE), 
+  fCutR(0),                               fCutEta(0),                             fCutPhi(0),
+  fClusterWindow(0),                      fMass(0),                           
+  fStepSurface(0),                        fStepCluster(0),
+  fTrackCutsType(0),                      fCutMinTrackPt(0),                      fCutMinNClusterTPC(0), 
+  fCutMinNClusterITS(0),                  fCutMaxChi2PerClusterTPC(0),            fCutMaxChi2PerClusterITS(0),
+  fCutRequireTPCRefit(kFALSE),            fCutRequireITSRefit(kFALSE),            fCutAcceptKinkDaughters(kFALSE),
+  fCutMaxDCAToVertexXY(0),                fCutMaxDCAToVertexZ(0),                 fCutDCAToVertex2D(kFALSE)
 {
 //
   // Constructor.
@@ -68,53 +91,57 @@ AliEMCALRecoUtils::AliEMCALRecoUtils():
   // during Reco algorithm execution
   //
   
-  for(Int_t i = 0; i < 15 ; i++) {
-      fMisalTransShift[i] = 0.; 
-      fMisalRotShift[i] = 0.; 
-  }
-  for(Int_t i = 0; i < 6  ; i++) fNonLinearityParams[i] = 0.; 
-  //For kPi0GammaGamma case, but default is no correction
-  fNonLinearityParams[0] = 0.1457/0.1349766/1.038;
-  fNonLinearityParams[1] = -0.02024/0.1349766/1.038;
-  fNonLinearityParams[2] = 1.046;
-
-  fMatchedClusterIndex = new TArrayI();
-  fResidualZ = new TArrayF();
-  fResidualR = new TArrayF();
+  // Init parameters
+  InitParameters();
   
-  fPIDUtils = new AliEMCALPIDUtils();
+  //Track matching
+  fMatchedTrackIndex     = new TArrayI();
+  fMatchedClusterIndex   = new TArrayI();
+  fResidualPhi           = new TArrayF();
+  fResidualEta           = new TArrayF();
+  fPIDUtils              = new AliEMCALPIDUtils();
 
   InitTrackCuts();
-
 }
 
 //______________________________________________________________________
 AliEMCALRecoUtils::AliEMCALRecoUtils(const AliEMCALRecoUtils & reco) 
-: TNamed(reco), fNonLinearityFunction(reco.fNonLinearityFunction), 
-  fParticleType(reco.fParticleType), fPosAlgo(reco.fPosAlgo), fW0(reco.fW0), 
-  fRecalibration(reco.fRecalibration),fEMCALRecalibrationFactors(reco.fEMCALRecalibrationFactors),
-  fRemoveBadChannels(reco.fRemoveBadChannels),fRecalDistToBadChannels(reco.fRecalDistToBadChannels),
+: TNamed(reco), 
+  fParticleType(reco.fParticleType),                         fPosAlgo(reco.fPosAlgo),     fW0(reco.fW0),
+  fNonLinearityFunction(reco.fNonLinearityFunction),         fNonLinearThreshold(reco.fNonLinearThreshold),
+  fSmearClusterEnergy(reco.fSmearClusterEnergy),             fRandom(),
+  fCellsRecalibrated(reco.fCellsRecalibrated),
+  fRecalibration(reco.fRecalibration),                       fEMCALRecalibrationFactors(reco.fEMCALRecalibrationFactors),
+  fTimeRecalibration(reco.fTimeRecalibration),               fEMCALTimeRecalibrationFactors(reco.fEMCALTimeRecalibrationFactors),
+  fUseRunCorrectionFactors(reco.fUseRunCorrectionFactors),   fRunCorrectionFactorsSet(reco.fRunCorrectionFactorsSet),
+  fRemoveBadChannels(reco.fRemoveBadChannels),               fRecalDistToBadChannels(reco.fRecalDistToBadChannels),
   fEMCALBadChannelMap(reco.fEMCALBadChannelMap),
-  fNCellsFromEMCALBorder(reco.fNCellsFromEMCALBorder),fNoEMCALBorderAtEta0(reco.fNoEMCALBorderAtEta0),
+  fNCellsFromEMCALBorder(reco.fNCellsFromEMCALBorder),       fNoEMCALBorderAtEta0(reco.fNoEMCALBorderAtEta0),
+  fRejectExoticCluster(reco.fRejectExoticCluster),           fRejectExoticCells(reco.fRejectExoticCells), 
+  fExoticCellFraction(reco.fExoticCellFraction),             fExoticCellDiffTime(reco.fExoticCellDiffTime),               
+  fExoticCellMinAmplitude(reco.fExoticCellMinAmplitude),
+  fPIDUtils(reco.fPIDUtils),                                 fAODFilterMask(reco.fAODFilterMask),
+  fMatchedTrackIndex(  reco.fMatchedTrackIndex?  new TArrayI(*reco.fMatchedTrackIndex):0x0),
   fMatchedClusterIndex(reco.fMatchedClusterIndex?new TArrayI(*reco.fMatchedClusterIndex):0x0),
-  fResidualZ(reco.fResidualZ?new TArrayF(*reco.fResidualZ):0x0),
-  fResidualR(reco.fResidualR?new TArrayF(*reco.fResidualR):0x0),
-  fCutR(reco.fCutR),fCutZ(reco.fCutZ),
-  fCutMinNClusterTPC(reco.fCutMinNClusterTPC), fCutMinNClusterITS(reco.fCutMinNClusterITS), 
-  fCutMaxChi2PerClusterTPC(reco.fCutMaxChi2PerClusterTPC), fCutMaxChi2PerClusterITS(reco.fCutMaxChi2PerClusterITS),
-  fCutRequireTPCRefit(reco.fCutRequireTPCRefit), fCutRequireITSRefit(reco.fCutRequireITSRefit),
-  fCutAcceptKinkDaughters(reco.fCutAcceptKinkDaughters),
-  fCutMaxDCAToVertexXY(reco.fCutMaxDCAToVertexXY), fCutMaxDCAToVertexZ(reco.fCutMaxDCAToVertexZ),fCutDCAToVertex2D(reco.fCutDCAToVertex2D),
-  fPIDUtils(reco.fPIDUtils)
-
+  fResidualEta(        reco.fResidualEta?        new TArrayF(*reco.fResidualEta):0x0),
+  fResidualPhi(        reco.fResidualPhi?        new TArrayF(*reco.fResidualPhi):0x0),
+  fCutEtaPhiSum(reco.fCutEtaPhiSum),                         fCutEtaPhiSeparate(reco.fCutEtaPhiSeparate), 
+  fCutR(reco.fCutR),        fCutEta(reco.fCutEta),           fCutPhi(reco.fCutPhi),
+  fClusterWindow(reco.fClusterWindow),
+  fMass(reco.fMass),        fStepSurface(reco.fStepSurface), fStepCluster(reco.fStepCluster),
+  fTrackCutsType(reco.fTrackCutsType),                       fCutMinTrackPt(reco.fCutMinTrackPt), 
+  fCutMinNClusterTPC(reco.fCutMinNClusterTPC),               fCutMinNClusterITS(reco.fCutMinNClusterITS), 
+  fCutMaxChi2PerClusterTPC(reco.fCutMaxChi2PerClusterTPC),   fCutMaxChi2PerClusterITS(reco.fCutMaxChi2PerClusterITS),
+  fCutRequireTPCRefit(reco.fCutRequireTPCRefit),             fCutRequireITSRefit(reco.fCutRequireITSRefit),
+  fCutAcceptKinkDaughters(reco.fCutAcceptKinkDaughters),     fCutMaxDCAToVertexXY(reco.fCutMaxDCAToVertexXY),    
+  fCutMaxDCAToVertexZ(reco.fCutMaxDCAToVertexZ),             fCutDCAToVertex2D(reco.fCutDCAToVertex2D)
 {
   //Copy ctor
   
-  for(Int_t i = 0; i < 15 ; i++) {
-      fMisalRotShift[i] = reco.fMisalRotShift[i]; 
-      fMisalTransShift[i] = reco.fMisalTransShift[i]; 
-  } 
-  for(Int_t i = 0; i < 6  ; i++) fNonLinearityParams[i] = reco.fNonLinearityParams[i]; 
+  for(Int_t i = 0; i < 15 ; i++) { fMisalRotShift[i]      = reco.fMisalRotShift[i]      ; 
+                                   fMisalTransShift[i]    = reco.fMisalTransShift[i]    ; } 
+  for(Int_t i = 0; i < 7  ; i++) { fNonLinearityParams[i] = reco.fNonLinearityParams[i] ; }
+  for(Int_t i = 0; i < 3  ; i++) { fSmearClusterParam[i]  = reco.fSmearClusterParam[i]  ; }
 
 }
 
@@ -127,63 +154,104 @@ AliEMCALRecoUtils & AliEMCALRecoUtils::operator = (const AliEMCALRecoUtils & rec
   if(this == &reco)return *this;
   ((TNamed *)this)->operator=(reco);
 
-  fNonLinearityFunction  = reco.fNonLinearityFunction;
-  fParticleType          = reco.fParticleType;
-  fPosAlgo               = reco.fPosAlgo; 
-  fW0                    = reco.fW0;
-  fRecalibration         = reco.fRecalibration;
-  fEMCALRecalibrationFactors = reco.fEMCALRecalibrationFactors;
-  fRemoveBadChannels     = reco.fRemoveBadChannels;
-  fRecalDistToBadChannels= reco.fRecalDistToBadChannels;
-  fEMCALBadChannelMap    = reco.fEMCALBadChannelMap;
-  fNCellsFromEMCALBorder = reco.fNCellsFromEMCALBorder;
-  fNoEMCALBorderAtEta0   = reco.fNoEMCALBorderAtEta0;
-
-
-  for(Int_t i = 0; i < 15 ; i++) {fMisalTransShift[i] = reco.fMisalTransShift[i]; fMisalRotShift[i] = reco.fMisalRotShift[i];}
-  for(Int_t i = 0; i < 6  ; i++) fNonLinearityParams[i] = reco.fNonLinearityParams[i]; 
+  for(Int_t i = 0; i < 15 ; i++) { fMisalTransShift[i]    = reco.fMisalTransShift[i]    ; 
+                                   fMisalRotShift[i]      = reco.fMisalRotShift[i]      ; }
+  for(Int_t i = 0; i < 7  ; i++) { fNonLinearityParams[i] = reco.fNonLinearityParams[i] ; }
+  for(Int_t i = 0; i < 3  ; i++) { fSmearClusterParam[i]  = reco.fSmearClusterParam[i]  ; }   
+  
+  fParticleType              = reco.fParticleType;
+  fPosAlgo                   = reco.fPosAlgo; 
+  fW0                        = reco.fW0;
   
-  fCutR                  = reco.fCutR;
-  fCutZ                  = reco.fCutZ;
+  fNonLinearityFunction      = reco.fNonLinearityFunction;
+  fNonLinearThreshold        = reco.fNonLinearThreshold;
+  fSmearClusterEnergy        = reco.fSmearClusterEnergy;
 
-  fCutMinNClusterTPC        = reco.fCutMinNClusterTPC;
-  fCutMinNClusterITS        = reco.fCutMinNClusterITS; 
-  fCutMaxChi2PerClusterTPC  = reco.fCutMaxChi2PerClusterTPC;
-  fCutMaxChi2PerClusterITS  = reco.fCutMaxChi2PerClusterITS;
-  fCutRequireTPCRefit       = reco.fCutRequireTPCRefit;
-  fCutRequireITSRefit       = reco.fCutRequireITSRefit;
-  fCutAcceptKinkDaughters   = reco.fCutAcceptKinkDaughters;
-  fCutMaxDCAToVertexXY      = reco.fCutMaxDCAToVertexXY;
-  fCutMaxDCAToVertexZ       = reco.fCutMaxDCAToVertexZ;
-  fCutDCAToVertex2D         = reco.fCutDCAToVertex2D;
+  fCellsRecalibrated         = reco.fCellsRecalibrated;
+  fRecalibration             = reco.fRecalibration;
+  fEMCALRecalibrationFactors = reco.fEMCALRecalibrationFactors;
 
-  fPIDUtils              = reco.fPIDUtils;
-  
-  
-  if(reco.fResidualR){
+  fTimeRecalibration             = reco.fTimeRecalibration;
+  fEMCALTimeRecalibrationFactors = reco.fEMCALTimeRecalibrationFactors;
+
+  fUseRunCorrectionFactors   = reco.fUseRunCorrectionFactors;
+  fRunCorrectionFactorsSet   = reco.fRunCorrectionFactorsSet;
+  
+  fRemoveBadChannels         = reco.fRemoveBadChannels;
+  fRecalDistToBadChannels    = reco.fRecalDistToBadChannels;
+  fEMCALBadChannelMap        = reco.fEMCALBadChannelMap;
+  
+  fNCellsFromEMCALBorder     = reco.fNCellsFromEMCALBorder;
+  fNoEMCALBorderAtEta0       = reco.fNoEMCALBorderAtEta0;
+  
+  fRejectExoticCluster       = reco.fRejectExoticCluster;           
+  fRejectExoticCells         = reco.fRejectExoticCells; 
+  fExoticCellFraction        = reco.fExoticCellFraction;
+  fExoticCellDiffTime        = reco.fExoticCellDiffTime;              
+  fExoticCellMinAmplitude    = reco.fExoticCellMinAmplitude;
+  
+  fPIDUtils                  = reco.fPIDUtils;
+
+  fAODFilterMask             = reco.fAODFilterMask;
+  
+  fCutEtaPhiSum              = reco.fCutEtaPhiSum;
+  fCutEtaPhiSeparate         = reco.fCutEtaPhiSeparate;
+  fCutR                      = reco.fCutR;
+  fCutEta                    = reco.fCutEta;
+  fCutPhi                    = reco.fCutPhi;
+  fClusterWindow             = reco.fClusterWindow;
+  fMass                      = reco.fMass;
+  fStepSurface               = reco.fStepSurface;
+  fStepCluster               = reco.fStepCluster;
+
+  fTrackCutsType             = reco.fTrackCutsType;
+  fCutMinTrackPt             = reco.fCutMinTrackPt;
+  fCutMinNClusterTPC         = reco.fCutMinNClusterTPC;
+  fCutMinNClusterITS         = reco.fCutMinNClusterITS; 
+  fCutMaxChi2PerClusterTPC   = reco.fCutMaxChi2PerClusterTPC;
+  fCutMaxChi2PerClusterITS   = reco.fCutMaxChi2PerClusterITS;
+  fCutRequireTPCRefit        = reco.fCutRequireTPCRefit;
+  fCutRequireITSRefit        = reco.fCutRequireITSRefit;
+  fCutAcceptKinkDaughters    = reco.fCutAcceptKinkDaughters;
+  fCutMaxDCAToVertexXY       = reco.fCutMaxDCAToVertexXY;
+  fCutMaxDCAToVertexZ        = reco.fCutMaxDCAToVertexZ;
+  fCutDCAToVertex2D          = reco.fCutDCAToVertex2D;
+  
+  if(reco.fResidualEta){
     // assign or copy construct
-    if(fResidualR){ 
-      *fResidualR = *reco.fResidualR;
+    if(fResidualEta){ 
+      *fResidualEta = *reco.fResidualEta;
     }
-    else fResidualR = new TArrayF(*reco.fResidualR);
+    else fResidualEta = new TArrayF(*reco.fResidualEta);
   }
   else{
-    if(fResidualR)delete fResidualR;
-    fResidualR = 0;
+    if(fResidualEta)delete fResidualEta;
+    fResidualEta = 0;
   }
   
-  if(reco.fResidualZ){
+  if(reco.fResidualPhi){
     // assign or copy construct
-    if(fResidualZ){ 
-      *fResidualZ = *reco.fResidualZ;
+    if(fResidualPhi){ 
+      *fResidualPhi = *reco.fResidualPhi;
     }
-    else fResidualZ = new TArrayF(*reco.fResidualZ);
+    else fResidualPhi = new TArrayF(*reco.fResidualPhi);
   }
   else{
-    if(fResidualZ)delete fResidualZ;
-    fResidualZ = 0;
+    if(fResidualPhi)delete fResidualPhi;
+    fResidualPhi = 0;
   }
   
+  if(reco.fMatchedTrackIndex){
+    // assign or copy construct
+    if(fMatchedTrackIndex){ 
+      *fMatchedTrackIndex = *reco.fMatchedTrackIndex;
+    }
+    else fMatchedTrackIndex = new TArrayI(*reco.fMatchedTrackIndex);
+  }
+  else{
+    if(fMatchedTrackIndex)delete fMatchedTrackIndex;
+    fMatchedTrackIndex = 0;
+  }  
   
   if(reco.fMatchedClusterIndex){
     // assign or copy construct
@@ -196,31 +264,72 @@ AliEMCALRecoUtils & AliEMCALRecoUtils::operator = (const AliEMCALRecoUtils & rec
     if(fMatchedClusterIndex)delete fMatchedClusterIndex;
     fMatchedClusterIndex = 0;
   }
-  
-  
+   
   return *this;
 }
 
 
-//__________________________________________________
+//_____________________________________
 AliEMCALRecoUtils::~AliEMCALRecoUtils()
 {
   //Destructor.
        
-       if(fEMCALRecalibrationFactors) { 
-               fEMCALRecalibrationFactors->Clear();
-               delete  fEMCALRecalibrationFactors;
+  if(fEMCALRecalibrationFactors) { 
+    fEMCALRecalibrationFactors->Clear();
+    delete  fEMCALRecalibrationFactors;
+  }    
+  
+  if(fEMCALTimeRecalibrationFactors) { 
+               fEMCALTimeRecalibrationFactors->Clear();
+               delete  fEMCALTimeRecalibrationFactors;
        }       
   
   if(fEMCALBadChannelMap) { 
-               fEMCALBadChannelMap->Clear();
-               delete  fEMCALBadChannelMap;
-       }
+    fEMCALBadChannelMap->Clear();
+    delete  fEMCALBadChannelMap;
+  }
  
-  if(fMatchedClusterIndex) {delete fMatchedClusterIndex; fMatchedClusterIndex=0;}
-  if(fResidualR)           {delete fResidualR;           fResidualR=0;}
-  if(fResidualZ)           {delete fResidualZ;           fResidualZ=0;}
+  delete fMatchedTrackIndex   ; 
+  delete fMatchedClusterIndex ; 
+  delete fResidualEta         ; 
+  delete fResidualPhi         ; 
+  delete fPIDUtils            ;
 
+  InitTrackCuts();
+}
+
+//_______________________________________________________________________________
+Bool_t AliEMCALRecoUtils::AcceptCalibrateCell(const Int_t absID, const Int_t bc,
+                                              Float_t  & amp,    Double_t & time, 
+                                              AliVCaloCells* cells) 
+{
+  // Reject cell if criteria not passed and calibrate it
+  
+  AliEMCALGeometry* geom = AliEMCALGeometry::GetInstance();
+  
+  if(absID < 0 || absID >= 24*48*geom->GetNumberOfSuperModules()) return kFALSE;
+  
+  Int_t imod = -1, iphi =-1, ieta=-1,iTower = -1, iIphi = -1, iIeta = -1; 
+  geom->GetCellIndex(absID,imod,iTower,iIphi,iIeta); 
+  geom->GetCellPhiEtaIndexInSModule(imod,iTower,iIphi, iIeta,iphi,ieta);       
+  
+  // Do not include bad channels found in analysis,
+  if( IsBadChannelsRemovalSwitchedOn() && GetEMCALChannelStatus(imod, ieta, iphi)) {
+    return kFALSE;
+  }
+  
+  //Recalibrate energy
+  amp  = cells->GetCellAmplitude(absID);
+  if(IsRecalibrationOn())
+    amp *= GetEMCALChannelRecalibrationFactor(imod,ieta,iphi);
+  
+  
+  // Recalibrate time
+  time = cells->GetCellTime(absID);
+  
+  RecalibrateCellTime(absID,bc,time);
+  
+  return kTRUE;
 }
 
 //_______________________________________________________________
@@ -229,6 +338,11 @@ Bool_t AliEMCALRecoUtils::CheckCellFiducialRegion(AliEMCALGeometry* geom, AliVCl
        // Given the list of AbsId of the cluster, get the maximum cell and 
        // check if there are fNCellsFromBorder from the calorimeter border
        
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return kFALSE;
+  }
+  
   //If the distance to the border is 0 or negative just exit accept all clusters
        if(cells->GetType()==AliVCaloCells::kEMCALCell && fNCellsFromEMCALBorder <= 0 ) return kTRUE;
   
@@ -288,7 +402,7 @@ Bool_t AliEMCALRecoUtils::CheckCellFiducialRegion(AliEMCALGeometry* geom, AliVCl
 
 
 //_________________________________________________________________________________________________________
-Bool_t AliEMCALRecoUtils::ClusterContainsBadChannel(AliEMCALGeometry* geom, UShort_t* cellList, Int_t nCells){
+Bool_t AliEMCALRecoUtils::ClusterContainsBadChannel(const AliEMCALGeometry* geom, const UShort_t* cellList, const Int_t nCells){
        // Check that in the cluster cells, there is no bad channel of those stored 
        // in fEMCALBadChannelMap or fPHOSBadChannelMap
        
@@ -316,53 +430,261 @@ Bool_t AliEMCALRecoUtils::ClusterContainsBadChannel(AliEMCALGeometry* geom, USho
        
 }
 
+//_______________________________________________________________________
+Bool_t AliEMCALRecoUtils::IsExoticCell(const Int_t absID, AliVCaloCells* cells, const Int_t bc)
+{
+  // Look to cell neighbourhood and reject if it seems exotic
+  // Do before recalibrating the cells
+  if(!fRejectExoticCells) return kFALSE;
+    
+  AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
+  
+  Int_t imod = -1, iphi =-1, ieta=-1,iTower = -1, iIphi = -1, iIeta = -1; 
+  geom->GetCellIndex(absID,imod,iTower,iIphi,iIeta); 
+  geom->GetCellPhiEtaIndexInSModule(imod,iTower,iIphi, iIeta,iphi,ieta);       
+  
+  //Get close cells index, energy and time, not in corners
+  Int_t absID1 = geom-> GetAbsCellIdFromCellIndexes(imod, iphi+1, ieta);
+  Int_t absID2 = geom-> GetAbsCellIdFromCellIndexes(imod, iphi-1, ieta);
+  Int_t absID3 = geom-> GetAbsCellIdFromCellIndexes(imod, iphi, ieta+1);
+  Int_t absID4 = geom-> GetAbsCellIdFromCellIndexes(imod, iphi, ieta-1);
+  
+  Float_t  ecell  = 0, ecell1  = 0, ecell2  = 0, ecell3  = 0, ecell4  = 0;
+  Double_t tcell  = 0, tcell1  = 0, tcell2  = 0, tcell3  = 0, tcell4  = 0;
+  Bool_t   accept = 0, accept1 = 0, accept2 = 0, accept3 = 0, accept4 = 0;
+  
+  accept  = AcceptCalibrateCell(absID, bc, ecell ,tcell ,cells); 
+    
+  if(!accept) return kTRUE; // reject this cell
+  
+  if(ecell < fExoticCellMinAmplitude) return kFALSE; // do not reject low energy cells
+  
+  accept1 = AcceptCalibrateCell(absID1,bc, ecell1,tcell1,cells); 
+  accept2 = AcceptCalibrateCell(absID2,bc, ecell2,tcell2,cells); 
+  accept3 = AcceptCalibrateCell(absID3,bc, ecell3,tcell3,cells); 
+  accept4 = AcceptCalibrateCell(absID4,bc, ecell4,tcell4,cells); 
+  
+  /*
+    printf("Cell absID %d \n",absID);
+    printf("\t  accept1 %d, accept2 %d, accept3 %d, accept4 %d\n",
+           accept1,accept2,accept3,accept4);
+    printf("\t id %d: id1 %d, id2 %d, id3 %d, id4 %d\n",
+           absID,absID1,absID2,absID3,absID4);
+    printf("\t e %f: e1 %f, e2 %f, e3 %f, e4 %f\n",
+           ecell,ecell1,ecell2,ecell3,ecell4);
+    printf("\t t %f: t1 %f, t2 %f, t3 %f, t4 %f;\n dt1 %f, dt2 %f, dt3 %f, dt4 %f\n",
+           tcell*1.e9,tcell1*1.e9,tcell2*1.e9,tcell3*1.e9,tcell4*1.e9,
+           TMath::Abs(tcell-tcell1)*1.e9, TMath::Abs(tcell-tcell2)*1.e9, TMath::Abs(tcell-tcell3)*1.e9, TMath::Abs(tcell-tcell4)*1.e9);
+  */
+  
+  if(TMath::Abs(tcell-tcell1)*1.e9 > fExoticCellDiffTime) ecell1 = 0 ;
+  if(TMath::Abs(tcell-tcell2)*1.e9 > fExoticCellDiffTime) ecell2 = 0 ;
+  if(TMath::Abs(tcell-tcell3)*1.e9 > fExoticCellDiffTime) ecell3 = 0 ;
+  if(TMath::Abs(tcell-tcell4)*1.e9 > fExoticCellDiffTime) ecell4 = 0 ;
+
+  Float_t eCross = ecell1+ecell2+ecell3+ecell4;
+
+  //printf("\t eCell %f, eCross %f, 1-eCross/eCell %f\n",ecell,eCross,1-eCross/ecell);
+  
+  if(1-eCross/ecell > fExoticCellFraction) {
+    AliDebug(2,Form("AliEMCALRecoUtils::IsExoticCell() - EXOTIC CELL id %d, eCell %f, eCross %f, 1-eCross/eCell %f\n",absID,ecell,eCross,1-eCross/ecell));
+    return kTRUE;
+  }
+  
+  return kFALSE;
+  
+}
+
+//_________________________________________________
+Bool_t AliEMCALRecoUtils::IsExoticCluster(AliVCluster *cluster, AliVCaloCells *cells, const Int_t bc) {
+  // Check if the cluster highest energy tower is exotic
+  
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return kFALSE;
+  }
+  
+  if(!fRejectExoticCluster) return kFALSE;
+  
+  // Get highest energy tower
+  AliEMCALGeometry* geom = AliEMCALGeometry::GetInstance();
+  Int_t iSupMod = -1, absId = -1, ieta = -1, iphi = -1;
+  Bool_t shared = kFALSE;
+  GetMaxEnergyCell(geom, cells, cluster, absId, iSupMod, ieta, iphi, shared);
+
+  return IsExoticCell(absId,cells,bc);
+  
+}
+
+//__________________________________________________
+Float_t AliEMCALRecoUtils::SmearClusterEnergy(const AliVCluster* cluster) {
+
+  //In case of MC analysis, smear energy to match resolution/calibration in real data
+  
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return 0;
+  }
+  
+  Float_t energy    = cluster->E() ;
+  Float_t rdmEnergy = energy ;
+  if(fSmearClusterEnergy){
+    rdmEnergy = fRandom.Gaus(energy,fSmearClusterParam[0] * TMath::Sqrt(energy) +
+                                    fSmearClusterParam[1] * energy +
+                                    fSmearClusterParam[2] );
+    AliDebug(2, Form("Energy: original %f, smeared %f\n", energy, rdmEnergy));
+  }
+  
+  return rdmEnergy ;
+
+}
+
 //__________________________________________________
 Float_t AliEMCALRecoUtils::CorrectClusterEnergyLinearity(AliVCluster* cluster){
 // Correct cluster energy from non linearity functions
-  Float_t energy = cluster->E();
   
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return 0;
+  }
+  
+  Float_t energy = cluster->E();
+
   switch (fNonLinearityFunction) {
       
     case kPi0MC:
+    {
       //Non-Linearity correction (from MC with function ([0]*exp(-[1]/E))+(([2]/([3]*2.*TMath::Pi())*exp(-(E-[4])^2/(2.*[3]^2)))))
-      //Double_t par0 = 1.001;
-      //Double_t par1 = -0.01264;
-      //Double_t par2 = -0.03632;
-      //Double_t par3 = 0.1798;
-      //Double_t par4 = -0.522;
-       energy /= (fNonLinearityParams[0]*exp(-fNonLinearityParams[1]/energy))+
+      //Double_t fNonLinearityParams[0] = 1.014;
+      //Double_t fNonLinearityParams[1] = -0.03329;
+      //Double_t fNonLinearityParams[2] = -0.3853;
+      //Double_t fNonLinearityParams[3] = 0.5423;
+      //Double_t fNonLinearityParams[4] = -0.4335;
+       energy *= (fNonLinearityParams[0]*exp(-fNonLinearityParams[1]/energy))+
                   ((fNonLinearityParams[2]/(fNonLinearityParams[3]*2.*TMath::Pi())*
                     exp(-(energy-fNonLinearityParams[4])*(energy-fNonLinearityParams[4])/(2.*fNonLinearityParams[3]*fNonLinearityParams[3]))));
       break;
-      
+    }
+     
     case kPi0GammaGamma:
-
+    {
       //Non-Linearity correction (from Olga Data with function p0+p1*exp(-p2*E))
-      //Double_t par0 = 0.1457;
-      //Double_t par1 = -0.02024;
-      //Double_t par2 = 1.046;
+      //Double_t fNonLinearityParams[0] = 1.04;
+      //Double_t fNonLinearityParams[1] = -0.1445;
+      //Double_t fNonLinearityParams[2] = 1.046;
       energy /= (fNonLinearityParams[0]+fNonLinearityParams[1]*exp(-fNonLinearityParams[2]*energy)); //Olga function
       break;
+    }
       
     case kPi0GammaConversion:
-      
+    {
       //Non-Linearity correction (Nicolas from Dimitri Data with function C*[1-a*exp(-b*E)])
-      //Double_t C = 0.139393/0.1349766;
-      //Double_t a = 0.0566186;
-      //Double_t b = 0.982133;
+      //fNonLinearityParams[0] = 0.139393/0.1349766;
+      //fNonLinearityParams[1] = 0.0566186;
+      //fNonLinearityParams[2] = 0.982133;
       energy /= fNonLinearityParams[0]*(1-fNonLinearityParams[1]*exp(-fNonLinearityParams[2]*energy));
       
       break;
+    }
+      
+    case kBeamTest:
+    {
+      //From beam test, Alexei's results, for different ZS thresholds
+      //                        th=30 MeV; th = 45 MeV; th = 75 MeV
+      //fNonLinearityParams[0] = 1.007;      1.003;      1.002 
+      //fNonLinearityParams[1] = 0.894;      0.719;      0.797 
+      //fNonLinearityParams[2] = 0.246;      0.334;      0.358 
+      //Rescale the param[0] with 1.03
+      energy /= fNonLinearityParams[0]/(1+fNonLinearityParams[1]*exp(-energy/fNonLinearityParams[2]));
+      
+      break;
+    }
+      
+    case kBeamTestCorrected:
+    {
+      //From beam test, corrected for material between beam and EMCAL
+      //fNonLinearityParams[0] =  0.99078
+      //fNonLinearityParams[1] =  0.161499;
+      //fNonLinearityParams[2] =  0.655166; 
+      //fNonLinearityParams[3] =  0.134101;
+      //fNonLinearityParams[4] =  163.282;
+      //fNonLinearityParams[5] =  23.6904;
+      //fNonLinearityParams[6] =  0.978;
+        energy *= fNonLinearityParams[6]/(fNonLinearityParams[0]*(1./(1.+fNonLinearityParams[1]*exp(-energy/fNonLinearityParams[2]))*1./(1.+fNonLinearityParams[3]*exp((energy-fNonLinearityParams[4])/fNonLinearityParams[5]))));
+
+      break;
+    }
       
     case kNoCorrection:
       AliDebug(2,"No correction on the energy\n");
       break;
       
   }
-  
+
   return energy;
 
 }
+//__________________________________________________
+void AliEMCALRecoUtils::InitNonLinearityParam()
+{
+       //Initialising Non Linearity Parameters
+       
+       if(fNonLinearityFunction == kPi0MC)
+               {
+                       fNonLinearityParams[0] = 1.014;
+      fNonLinearityParams[1] = -0.03329;
+      fNonLinearityParams[2] = -0.3853;
+      fNonLinearityParams[3] = 0.5423;
+      fNonLinearityParams[4] = -0.4335;
+               }
+
+       if(fNonLinearityFunction == kPi0GammaGamma)
+               {
+                       fNonLinearityParams[0] = 1.04;
+                       fNonLinearityParams[1] = -0.1445;
+                       fNonLinearityParams[2] = 1.046;
+               }       
+
+       if(fNonLinearityFunction == kPi0GammaConversion)
+               {
+      fNonLinearityParams[0] = 0.139393;
+      fNonLinearityParams[1] = 0.0566186;
+      fNonLinearityParams[2] = 0.982133;
+               }       
+
+       if(fNonLinearityFunction == kBeamTest)
+               {
+                       if(fNonLinearThreshold == 30)
+                       {
+                               fNonLinearityParams[0] = 1.007; 
+                               fNonLinearityParams[1] = 0.894; 
+                               fNonLinearityParams[2] = 0.246; 
+                       }
+                       if(fNonLinearThreshold == 45)
+                       {
+                               fNonLinearityParams[0] = 1.003; 
+                               fNonLinearityParams[1] = 0.719; 
+                               fNonLinearityParams[2] = 0.334; 
+                       }
+                       if(fNonLinearThreshold == 75)
+                       {
+                               fNonLinearityParams[0] = 1.002; 
+                               fNonLinearityParams[1] = 0.797; 
+                               fNonLinearityParams[2] = 0.358; 
+                       }
+               }
+
+       if(fNonLinearityFunction == kBeamTestCorrected)
+               {
+                       fNonLinearityParams[0] =  0.99078;
+                       fNonLinearityParams[1] =  0.161499;
+                       fNonLinearityParams[2] =  0.655166; 
+                       fNonLinearityParams[3] =  0.134101;
+                       fNonLinearityParams[4] =  163.282;
+                       fNonLinearityParams[5] =  23.6904;
+                       fNonLinearityParams[6] =  0.978;
+               }
+}
 
 //__________________________________________________
 Float_t  AliEMCALRecoUtils::GetDepth(const Float_t energy, const Int_t iParticle, const Int_t iSM) const 
@@ -413,9 +735,15 @@ Float_t  AliEMCALRecoUtils::GetDepth(const Float_t energy, const Int_t iParticle
   
 }
 
-//__________________________________________________
-void AliEMCALRecoUtils::GetMaxEnergyCell(AliEMCALGeometry *geom, AliVCaloCells* cells, AliVCluster* clu, 
-                                         Int_t & absId,  Int_t& iSupMod, Int_t& ieta, Int_t& iphi, Bool_t &shared)
+//____________________________________________________________________
+void AliEMCALRecoUtils::GetMaxEnergyCell(const AliEMCALGeometry *geom, 
+                                         AliVCaloCells* cells, 
+                                         const AliVCluster* clu, 
+                                         Int_t & absId,  
+                                         Int_t& iSupMod, 
+                                         Int_t& ieta, 
+                                         Int_t& iphi, 
+                                         Bool_t &shared)
 {
   //For a given CaloCluster gets the absId of the cell 
   //with maximum energy deposit.
@@ -430,7 +758,13 @@ void AliEMCALRecoUtils::GetMaxEnergyCell(AliEMCALGeometry *geom, AliVCaloCells*
   Int_t iIphi   = -1;
   Int_t iIeta   = -1;
   Int_t iSupMod0= -1;
-       //printf("---Max?\n");
+
+  if(!clu){
+    AliInfo("Cluster pointer null!");
+    absId=-1; iSupMod0=-1, ieta = -1; iphi = -1; shared = -1;
+    return;
+  }
+  
   for (Int_t iDig=0; iDig< clu->GetNCells(); iDig++) {
     cellAbsId = clu->GetCellAbsId(iDig);
     fraction  = clu->GetCellAmplitudeFraction(iDig);
@@ -465,8 +799,88 @@ void AliEMCALRecoUtils::GetMaxEnergyCell(AliEMCALGeometry *geom, AliVCaloCells*
   
 }
 
-//________________________________________________________________
-void AliEMCALRecoUtils::InitEMCALRecalibrationFactors(){
+//______________________________________
+void AliEMCALRecoUtils::InitParameters()
+{
+  // Initialize data members with default values
+  
+  fParticleType = kPhoton;
+  fPosAlgo      = kUnchanged;
+  fW0           = 4.5;
+  
+  fNonLinearityFunction = kNoCorrection;
+  fNonLinearThreshold   = 30;
+  
+  fExoticCellFraction     = 0.97;
+  fExoticCellDiffTime     = 1e6;
+  fExoticCellMinAmplitude = 0.5;
+  
+  fAODFilterMask = 32;
+  
+  fCutEtaPhiSum      = kTRUE;
+  fCutEtaPhiSeparate = kFALSE;
+  
+  fCutR   = 0.05; 
+  fCutEta = 0.025; 
+  fCutPhi = 0.05;
+  
+  fClusterWindow = 100;
+  fMass          = 0.139;
+  
+  fStepSurface   = 20.;                      
+  fStepCluster   = 5.;
+  fTrackCutsType = kLooseCut;
+  
+  fCutMinTrackPt     = 0;
+  fCutMinNClusterTPC = -1;
+  fCutMinNClusterITS = -1;
+  
+  fCutMaxChi2PerClusterTPC  = 1e10;
+  fCutMaxChi2PerClusterITS  = 1e10;
+  
+  fCutRequireTPCRefit     = kFALSE;
+  fCutRequireITSRefit     = kFALSE;
+  fCutAcceptKinkDaughters = kFALSE;
+  
+  fCutMaxDCAToVertexXY = 1e10;             
+  fCutMaxDCAToVertexZ  = 1e10;              
+  fCutDCAToVertex2D    = kFALSE;
+  
+  
+  //Misalignment matrices
+  for(Int_t i = 0; i < 15 ; i++) {
+    fMisalTransShift[i] = 0.; 
+    fMisalRotShift[i]   = 0.; 
+  }
+  
+  //Non linearity
+  for(Int_t i = 0; i < 7  ; i++) fNonLinearityParams[i] = 0.; 
+  
+  //For kBeamTestCorrected case, but default is no correction
+  fNonLinearityParams[0] =  0.99078;
+  fNonLinearityParams[1] =  0.161499;
+  fNonLinearityParams[2] =  0.655166; 
+  fNonLinearityParams[3] =  0.134101;
+  fNonLinearityParams[4] =  163.282;
+  fNonLinearityParams[5] =  23.6904;
+  fNonLinearityParams[6] =  0.978;
+  
+  //For kPi0GammaGamma case
+  //fNonLinearityParams[0] = 0.1457/0.1349766/1.038;
+  //fNonLinearityParams[1] = -0.02024/0.1349766/1.038;
+  //fNonLinearityParams[2] = 1.046;
+  
+  //Cluster energy smearing
+  fSmearClusterEnergy   = kFALSE;
+  fSmearClusterParam[0] = 0.07; // * sqrt E term
+  fSmearClusterParam[1] = 0.00; // * E term
+  fSmearClusterParam[2] = 0.00; // constant
+  
+}
+
+//_____________________________________________________
+void AliEMCALRecoUtils::InitEMCALRecalibrationFactors()
+{
        //Init EMCAL recalibration factors
        AliDebug(2,"AliCalorimeterUtils::InitEMCALRecalibrationFactors()");
        //In order to avoid rewriting the same histograms
@@ -474,9 +888,9 @@ void AliEMCALRecoUtils::InitEMCALRecalibrationFactors(){
        TH1::AddDirectory(kFALSE);
   
        fEMCALRecalibrationFactors = new TObjArray(10);
-       for (int i = 0; i < 12; i++) fEMCALRecalibrationFactors->Add(new TH2F(Form("EMCALRecalFactors_SM%d",i),Form("EMCALRecalFactors_SM%d",i),  48, 0, 48, 24, 0, 24));
+       for (int i = 0; i < 10; i++) fEMCALRecalibrationFactors->Add(new TH2F(Form("EMCALRecalFactors_SM%d",i),Form("EMCALRecalFactors_SM%d",i),  48, 0, 48, 24, 0, 24));
        //Init the histograms with 1
-       for (Int_t sm = 0; sm < 12; sm++) {
+       for (Int_t sm = 0; sm < 10; sm++) {
                for (Int_t i = 0; i < 48; i++) {
                        for (Int_t j = 0; j < 24; j++) {
                                SetEMCALChannelRecalibrationFactor(sm,i,j,1.);
@@ -490,6 +904,31 @@ void AliEMCALRecoUtils::InitEMCALRecalibrationFactors(){
        TH1::AddDirectory(oldStatus);           
 }
 
+//________________________________________________________________
+void AliEMCALRecoUtils::InitEMCALTimeRecalibrationFactors(){
+       //Init EMCAL recalibration factors
+       AliDebug(2,"AliCalorimeterUtils::InitEMCALRecalibrationFactors()");
+       //In order to avoid rewriting the same histograms
+       Bool_t oldStatus = TH1::AddDirectoryStatus();
+       TH1::AddDirectory(kFALSE);
+  
+       fEMCALTimeRecalibrationFactors = new TObjArray(4);
+       for (int i = 0; i < 4; i++) 
+    fEMCALTimeRecalibrationFactors->Add(new TH1F(Form("hAllTimeAvBC%d",i),
+                                                 Form("hAllTimeAvBC%d",i),  
+                                                 48*24*10,0.,48*24*10)          );
+       //Init the histograms with 1
+       for (Int_t bc = 0; bc < 4; bc++) {
+                       for (Int_t i = 0; i < 48*24*10; i++) 
+        SetEMCALChannelTimeRecalibrationFactor(bc,i,0.);
+  }
+
+       fEMCALTimeRecalibrationFactors->SetOwner(kTRUE);
+       fEMCALTimeRecalibrationFactors->Compress();
+       
+       //In order to avoid rewriting the same histograms
+       TH1::AddDirectory(oldStatus);           
+}
 
 //________________________________________________________________
 void AliEMCALRecoUtils::InitEMCALBadChannelStatusMap(){
@@ -504,9 +943,7 @@ void AliEMCALRecoUtils::InitEMCALBadChannelStatusMap(){
        for (int i = 0; i < 10; i++) {
                fEMCALBadChannelMap->Add(new TH2I(Form("EMCALBadChannelMap_Mod%d",i),Form("EMCALBadChannelMap_Mod%d",i), 48, 0, 48, 24, 0, 24));
        }
-       
-       //delete hTemp;
-       
+               
        fEMCALBadChannelMap->SetOwner(kTRUE);
        fEMCALBadChannelMap->Compress();
        
@@ -514,10 +951,21 @@ void AliEMCALRecoUtils::InitEMCALBadChannelStatusMap(){
        TH1::AddDirectory(oldStatus);           
 }
 
-//________________________________________________________________
-void AliEMCALRecoUtils::RecalibrateClusterEnergy(AliEMCALGeometry* geom, AliVCluster * cluster, AliVCaloCells * cells){
-       // Recalibrate the cluster energy, considering the recalibration map and the energy of the cells that compose the cluster.
+//____________________________________________________________________________
+void AliEMCALRecoUtils::RecalibrateClusterEnergy(const AliEMCALGeometry* geom, 
+                                                 AliVCluster * cluster, 
+                                                 AliVCaloCells * cells, 
+                                                 const Int_t bc)
+{
+       // Recalibrate the cluster energy and Time, considering the recalibration map 
+  // and the energy of the cells and time that compose the cluster.
+  // bc= bunch crossing number returned by esdevent->GetBunchCrossNumber();
        
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return;
+  }  
+  
        //Get the cluster number of cells and list of absId, check what kind of cluster do we have.
        UShort_t * index    = cluster->GetCellsAbsId() ;
        Double_t * fraction = cluster->GetCellsAmplitudeFraction() ;
@@ -525,39 +973,151 @@ void AliEMCALRecoUtils::RecalibrateClusterEnergy(AliEMCALGeometry* geom, AliVClu
        
        //Initialize some used variables
        Float_t energy = 0;
-       Int_t absId    = -1;
-  Int_t icol = -1, irow = -1, imod=1;
+       Int_t   absId  =-1;
+  Int_t   icol   =-1, irow =-1, imod=1;
        Float_t factor = 1, frac = 0;
-       
+  Int_t   absIdMax = -1;
+  Float_t emax     = 0;
+  
        //Loop on the cells, get the cell amplitude and recalibration factor, multiply and and to the new energy
        for(Int_t icell = 0; icell < ncells; icell++){
                absId = index[icell];
                frac =  fraction[icell];
                if(frac < 1e-5) frac = 1; //in case of EMCAL, this is set as 0 since unfolding is off
-               Int_t iTower = -1, iIphi = -1, iIeta = -1; 
-               geom->GetCellIndex(absId,imod,iTower,iIphi,iIeta); 
-               if(fEMCALRecalibrationFactors->GetEntries() <= imod) continue;
-               geom->GetCellPhiEtaIndexInSModule(imod,iTower,iIphi, iIeta,irow,icol);                  
-               factor = GetEMCALChannelRecalibrationFactor(imod,icol,irow);
-    AliDebug(2,Form("AliEMCALRecoUtils::RecalibrateClusterEnergy - recalibrate cell: module %d, col %d, row %d, cell fraction %f,recalibration factor %f, cell energy %f\n",
-             imod,icol,irow,frac,factor,cells->GetCellAmplitude(absId)));
-               
+    
+    if(!fCellsRecalibrated && IsRecalibrationOn()){
+      
+      // Energy  
+      Int_t iTower = -1, iIphi = -1, iIeta = -1; 
+      geom->GetCellIndex(absId,imod,iTower,iIphi,iIeta); 
+      if(fEMCALRecalibrationFactors->GetEntries() <= imod) continue;
+      geom->GetCellPhiEtaIndexInSModule(imod,iTower,iIphi, iIeta,irow,icol);                   
+      factor = GetEMCALChannelRecalibrationFactor(imod,icol,irow);
+      
+      AliDebug(2,Form("AliEMCALRecoUtils::RecalibrateClusterEnergy - recalibrate cell: module %d, col %d, row %d, cell fraction %f,recalibration factor %f, cell energy %f\n",
+                      imod,icol,irow,frac,factor,cells->GetCellAmplitude(absId)));
+      
+    } 
+    
                energy += cells->GetCellAmplitude(absId)*factor*frac;
+    
+    if(emax < cells->GetCellAmplitude(absId)*factor*frac){
+      emax     = cells->GetCellAmplitude(absId)*factor*frac;
+      absIdMax = absId;
+    }
+
        }
        
-       
-               AliDebug(2,Form("AliEMCALRecoUtils::RecalibrateClusterEnergy - Energy before %f, after %f\n",cluster->E(),energy));
-       
-       cluster->SetE(energy);
-       
+  cluster->SetE(energy);
+
+  AliDebug(2,Form("AliEMCALRecoUtils::RecalibrateClusterEnergy - Energy before %f, after %f\n",cluster->E(),energy));
+
+       // Recalculate time of cluster only for ESDs
+  if(!strcmp("AliESDCaloCluster",Form("%s",cluster->ClassName()))){
+    
+    // Time
+    Double_t weightedTime = 0;
+    Double_t weight       = 0;
+    Double_t weightTot    = 0;
+    Double_t maxcellTime  = 0;
+    for(Int_t icell = 0; icell < ncells; icell++){
+      absId = index[icell];
+      frac =  fraction[icell];
+      if(frac < 1e-5) frac = 1; //in case of EMCAL, this is set as 0 since unfolding is off
+
+      Double_t celltime = cells->GetCellTime(absId);
+      RecalibrateCellTime(absId, bc, celltime);
+      if(absId == absIdMax) maxcellTime = celltime;
+
+      if(!fCellsRecalibrated){
+      
+        Int_t iTower = -1, iIphi = -1, iIeta = -1; 
+        geom->GetCellIndex(absId,imod,iTower,iIphi,iIeta); 
+        if(fEMCALRecalibrationFactors->GetEntries() <= imod) continue;
+        geom->GetCellPhiEtaIndexInSModule(imod,iTower,iIphi, iIeta,irow,icol);                 
+        factor = GetEMCALChannelRecalibrationFactor(imod,icol,irow);
+        
+        AliDebug(2,Form("AliEMCALRecoUtils::RecalibrateClusterEnergy - recalibrate cell: module %d, col %d, row %d, cell fraction %f,recalibration factor %f, cell energy %f\n",
+                        imod,icol,irow,frac,factor,cells->GetCellTime(absId)));
+        
+      } 
+      
+      weight        = GetCellWeight(cells->GetCellAmplitude(absId)*factor*frac , energy );
+      weightTot    += weight;
+      weightedTime += celltime * weight;
+      
+    }
+    
+    if(weightTot > 0)
+      cluster->SetTOF(weightedTime/weightTot);
+    else 
+      cluster->SetTOF(maxcellTime);
+    
+  }
 }
 
+//________________________________________________________________
+void AliEMCALRecoUtils::RecalibrateCells(AliVCaloCells * cells, Int_t bc){
+       // Recalibrate the cells time and energy, considering the recalibration map and the energy 
+  // of the cells that compose the cluster.
+  // bc= bunch crossing number returned by esdevent->GetBunchCrossNumber();
+
+  if(!IsRecalibrationOn() && !IsTimeRecalibrationOn()) return;
+  
+  if(!cells){
+    AliInfo("Cells pointer null!");
+    return;
+  }  
+  
+  fCellsRecalibrated = kTRUE;
+  
+  Int_t    absId  =-1;
+  Bool_t   accept = kFALSE;
+  Float_t  ecell  = 0;
+  Double_t tcell  = 0;
+  
+  Int_t    nEMcell  = cells->GetNumberOfCells() ;  
+  for (Int_t iCell = 0; iCell < nEMcell; iCell++) { 
+    
+    absId  = cells->GetCellNumber(iCell);
+    
+    accept = AcceptCalibrateCell(absId, bc, ecell ,tcell ,cells); 
+    if(!accept) {
+      ecell = 0;
+      tcell = 0;
+    }
+    
+    //Set new values
+    cells->SetCell(iCell,absId,ecell, tcell);
+    
+  }
+  
+}
 
+//_______________________________________________________________________________________________________
+void AliEMCALRecoUtils::RecalibrateCellTime(const Int_t absId, const Int_t bc, Double_t & celltime) const
+{
+       // Recalibrate time of cell with absID  considering the recalibration map 
+  // bc= bunch crossing number returned by esdevent->GetBunchCrossNumber();
+  
+  if(!fCellsRecalibrated && IsTimeRecalibrationOn() && bc >= 0){
+    
+    celltime -= GetEMCALChannelTimeRecalibrationFactor(bc%4,absId)*1.e-9;    ;  
+    
+  }
+  
+}
+  
 //__________________________________________________
 void AliEMCALRecoUtils::RecalculateClusterPosition(AliEMCALGeometry *geom, AliVCaloCells* cells, AliVCluster* clu)
 {
   //For a given CaloCluster recalculates the position for a given set of misalignment shifts and puts it again in the CaloCluster.
   
+  if(!clu){
+    AliInfo("Cluster pointer null!");
+    return;
+  }
+    
   if     (fPosAlgo==kPosTowerGlobal) RecalculateClusterPositionFromTowerGlobal( geom, cells, clu);
   else if(fPosAlgo==kPosTowerIndex)  RecalculateClusterPositionFromTowerIndex ( geom, cells, clu);
   else   AliDebug(2,"Algorithm to recalculate position not selected, do nothing.");
@@ -589,20 +1149,26 @@ void AliEMCALRecoUtils::RecalculateClusterPositionFromTowerGlobal(AliEMCALGeomet
   //printf("** Cluster energy %f, ncells %d, depth %f\n",clEnergy,clu->GetNCells(),depth);
   
   for (Int_t iDig=0; iDig< clu->GetNCells(); iDig++) {
-    absId = clu->GetCellAbsId(iDig);
-    fraction  = clu->GetCellAmplitudeFraction(iDig);
-    if(fraction < 1e-4) fraction = 1.; // in case unfolding is off
-    geom->GetCellIndex(absId,iSM,iTower,iIphi,iIeta); 
-    geom->GetCellPhiEtaIndexInSModule(iSM,iTower,iIphi, iIeta,iphi,ieta);                      
     
-    if(IsRecalibrationOn()) {
-      recalFactor = GetEMCALChannelRecalibrationFactor(iSM,ieta,iphi);
+      absId = clu->GetCellAbsId(iDig);
+      fraction  = clu->GetCellAmplitudeFraction(iDig);
+      if(fraction < 1e-4) fraction = 1.; // in case unfolding is off
+    
+    if(!fCellsRecalibrated){
+
+      geom->GetCellIndex(absId,iSM,iTower,iIphi,iIeta); 
+      geom->GetCellPhiEtaIndexInSModule(iSM,iTower,iIphi, iIeta,iphi,ieta);                    
+      
+      if(IsRecalibrationOn()) {
+        recalFactor = GetEMCALChannelRecalibrationFactor(iSM,ieta,iphi);
+      }
     }
+    
     eCell  = cells->GetCellAmplitude(absId)*fraction*recalFactor;
     
     weight = GetCellWeight(eCell,clEnergy);
-    //printf("cell energy %f, weight %f\n",eCell,weight);
     totalWeight += weight;
+    
     geom->RelPosCellInSModule(absId,depth,pLocal[0],pLocal[1],pLocal[2]);
     //printf("pLocal (%f,%f,%f), SM %d, absId %d\n",pLocal[0],pLocal[1],pLocal[2],iSupModMax,absId);
     geom->GetGlobal(pLocal,pGlobal,iSupModMax);
@@ -670,17 +1236,24 @@ void AliEMCALRecoUtils::RecalculateClusterPositionFromTowerIndex(AliEMCALGeometr
     absId = clu->GetCellAbsId(iDig);
     fraction  = clu->GetCellAmplitudeFraction(iDig);
     if(fraction < 1e-4) fraction = 1.; // in case unfolding is off
-    geom->GetCellIndex(absId,iSupMod,iTower,iIphi,iIeta); 
-    geom->GetCellPhiEtaIndexInSModule(iSupMod,iTower,iIphi, iIeta,iphi,ieta);                  
-    
+
     if     (iDig==0)  startingSM = iSupMod;
     else if(iSupMod != startingSM) areInSameSM = kFALSE;
 
     eCell  = cells->GetCellAmplitude(absId);
     
-    if(IsRecalibrationOn()) {
-      recalFactor = GetEMCALChannelRecalibrationFactor(iSupMod,ieta,iphi);
+    geom->GetCellIndex(absId,iSupMod,iTower,iIphi,iIeta); 
+    geom->GetCellPhiEtaIndexInSModule(iSupMod,iTower,iIphi, iIeta,iphi,ieta);          
+    
+    if(!fCellsRecalibrated){
+      
+      if(IsRecalibrationOn()) {
+                
+        recalFactor = GetEMCALChannelRecalibrationFactor(iSupMod,ieta,iphi);
+        
+      }
     }
+    
     eCell  = cells->GetCellAmplitude(absId)*fraction*recalFactor;
     
     weight = GetCellWeight(eCell,clEnergy);
@@ -716,6 +1289,11 @@ void AliEMCALRecoUtils::RecalculateClusterDistanceToBadChannel(AliEMCALGeometry
   
   if(!fRecalDistToBadChannels) return;
   
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return;
+  }  
+  
        //Get channels map of the supermodule where the cluster is.
   Int_t absIdMax       = -1, iSupMod =-1, icolM = -1, irowM = -1;
   Bool_t shared = kFALSE;
@@ -789,7 +1367,12 @@ void AliEMCALRecoUtils::RecalculateClusterDistanceToBadChannel(AliEMCALGeometry
 void AliEMCALRecoUtils::RecalculateClusterPID(AliVCluster * cluster){           
        
   //re-evaluate identification parameters with bayesian
-
+  
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return;
+  }
+  
        if ( cluster->GetM02() != 0)
     fPIDUtils->ComputePID(cluster->E(),cluster->GetM02());
   
@@ -807,6 +1390,11 @@ void AliEMCALRecoUtils::RecalculateClusterShowerShapeParameters(AliEMCALGeometry
   // and tranfers into global ALICE coordinates
   // Calculates Dispersion and main axis
   
+  if(!cluster){
+    AliInfo("Cluster pointer null!");
+    return;
+  }
+    
   Int_t nstat  = 0;
   Float_t wtot = 0. ;
   Double_t eCell       = 0.;
@@ -840,9 +1428,15 @@ void AliEMCALRecoUtils::RecalculateClusterShowerShapeParameters(AliEMCALGeometry
     //Get the cell energy, if recalibration is on, apply factors
     fraction  = cluster->GetCellAmplitudeFraction(iDigit);
     if(fraction < 1e-4) fraction = 1.; // in case unfolding is off
-    if(IsRecalibrationOn()) {
-      recalFactor = GetEMCALChannelRecalibrationFactor(iSupMod,ieta,iphi);
+    
+    if(!fCellsRecalibrated){
+      
+      if(IsRecalibrationOn()) {
+        recalFactor = GetEMCALChannelRecalibrationFactor(iSupMod,ieta,iphi);
+      }
+      
     }
+    
     eCell  = cells->GetCellAmplitude(cluster->GetCellAbsId(iDigit))*fraction*recalFactor;
     
     if(cluster->E() > 0 && eCell > 0){
@@ -923,120 +1517,503 @@ void AliEMCALRecoUtils::RecalculateClusterShowerShapeParameters(AliEMCALGeometry
     cluster->SetDispersion(TMath::Sqrt(d)) ;
   else    
     cluster->SetDispersion(0) ;
-  
 }
 
-//__________________________________________________
-void AliEMCALRecoUtils::FindMatches(AliVEvent *event)
+//____________________________________________________________________________
+void AliEMCALRecoUtils::FindMatches(AliVEvent *event,TObjArray * clusterArr,  AliEMCALGeometry *geom)
 {
   //This function should be called before the cluster loop
   //Before call this function, please recalculate the cluster positions
   //Given the input event, loop over all the tracks, select the closest cluster as matched with fCutR
   //Store matched cluster indexes and residuals
-  //It only works with ESDs, not AODs
   
+  fMatchedTrackIndex  ->Reset();
   fMatchedClusterIndex->Reset();
-  fResidualZ->Reset();
-  fResidualR->Reset();
+  fResidualPhi->Reset();
+  fResidualEta->Reset();
+  
+  fMatchedTrackIndex  ->Set(500);
+  fMatchedClusterIndex->Set(500);
+  fResidualPhi->Set(500);
+  fResidualEta->Set(500);
   
-  fMatchedClusterIndex->Set(100);
-  fResidualZ->Set(100);
-  fResidualR->Set(100);
+  AliESDEvent* esdevent = dynamic_cast<AliESDEvent*> (event);
+  AliAODEvent* aodevent = dynamic_cast<AliAODEvent*> (event);
+  
+  // init the magnetic field if not already on
+  if(!TGeoGlobalMagField::Instance()->GetField()){
+    AliInfo("Init the magnetic field\n");
+    if     (esdevent) 
+    {
+      esdevent->InitMagneticField();
+    }
+    else if(aodevent)
+    {
+      Double_t curSol = 30000*aodevent->GetMagneticField()/5.00668;
+      Double_t curDip = 6000 *aodevent->GetMuonMagFieldScale();
+      AliMagF *field  = AliMagF::CreateFieldMap(curSol,curDip);
+      TGeoGlobalMagField::Instance()->SetField(field);
+    }
+    else
+    {
+      AliInfo("Mag Field not initialized, null esd/aod evetn pointers");
+    }
+    
+  } // Init mag field
+  
+  TObjArray *clusterArray = 0x0;
+  if(!clusterArr)
+    {
+      clusterArray = new TObjArray(event->GetNumberOfCaloClusters());
+      for(Int_t icl=0; icl<event->GetNumberOfCaloClusters(); icl++)
+       {
+         AliVCluster *cluster = (AliVCluster*) event->GetCaloCluster(icl);
+         if(geom && !IsGoodCluster(cluster,geom,(AliVCaloCells*)event->GetEMCALCells())) continue;
+         clusterArray->AddAt(cluster,icl);
+       }
+    }
   
   Int_t    matched=0;
-  Float_t  clsPos[3];
-  Double_t trkPos[3];
+  Double_t cv[21];
+  for (Int_t i=0; i<21;i++) cv[i]=0;
   for(Int_t itr=0; itr<event->GetNumberOfTracks(); itr++)
   {
-    AliESDtrack *track = ((AliESDEvent*)event)->GetTrack(itr);
-    if(!track || !IsAccepted(track)) continue;
+    AliExternalTrackParam *trackParam = 0;
     
-    Float_t dRMax = fCutR, dZMax = fCutZ;
-    Int_t index = -1;
-    AliEMCALTrack *emctrack = new AliEMCALTrack(*track);
-    for(Int_t icl=0; icl<event->GetNumberOfCaloClusters(); icl++)
+    //If the input event is ESD, the starting point for extrapolation is TPCOut, if available, or TPCInner 
+    AliESDtrack *esdTrack = 0;
+    AliAODTrack *aodTrack = 0;
+    if(esdevent)
     {
-      AliVCluster *cluster = (AliVCluster*) event->GetCaloCluster(icl);
-      if(!cluster->IsEMCAL()) continue;
-      cluster->GetPosition(clsPos); //Has been recalculated
-      if(!emctrack->PropagateToGlobal(clsPos[0],clsPos[1],clsPos[2],0.,0.) )  continue;
-      emctrack->GetXYZ(trkPos);
-      Float_t tmpR = TMath::Sqrt( TMath::Power(clsPos[0]-trkPos[0],2)+TMath::Power(clsPos[1]-trkPos[1],2)+TMath::Power(clsPos[2]-trkPos[2],2) );
-      Float_t tmpZ = TMath::Abs(clsPos[2]-trkPos[2]);
-      
-      if(tmpR<dRMax)
-           {
-             dRMax=tmpR;
-             dZMax=tmpZ;
-             index=icl;
-           }
-      
-    }//cluser loop
+      esdTrack = esdevent->GetTrack(itr);
+      if(!esdTrack) continue;
+      if(!IsAccepted(esdTrack)) continue;
+      if(esdTrack->Pt()<fCutMinTrackPt) continue;
+      Double_t phi = esdTrack->Phi()*TMath::RadToDeg();
+      if(TMath::Abs(esdTrack->Eta())>0.8 || phi <= 20 || phi >= 240 ) continue;
+      trackParam =  const_cast<AliExternalTrackParam*>(esdTrack->GetInnerParam());
+    }
+    
+    //If the input event is AOD, the starting point for extrapolation is at vertex
+    //AOD tracks are selected according to its filterbit.
+    else if(aodevent)
+    {
+      aodTrack = aodevent->GetTrack(itr);
+      if(!aodTrack) continue;
+      if(!aodTrack->TestFilterMask(fAODFilterMask)) continue; //Select AOD tracks that fulfill GetStandardITSTPCTrackCuts2010()
+      if(aodTrack->Pt()<fCutMinTrackPt) continue;
+      Double_t phi = aodTrack->Phi()*TMath::RadToDeg();
+      if(TMath::Abs(aodTrack->Eta())>0.8 || phi <= 20 || phi >= 240 ) continue;
+      Double_t pos[3],mom[3];
+      aodTrack->GetXYZ(pos);
+      aodTrack->GetPxPyPz(mom);
+      AliDebug(5,Form("aod track: i=%d | pos=(%5.4f,%5.4f,%5.4f) | mom=(%5.4f,%5.4f,%5.4f) | charge=%d\n",itr,pos[0],pos[1],pos[2],mom[0],mom[1],mom[2],aodTrack->Charge()));
+      trackParam= new AliExternalTrackParam(pos,mom,cv,aodTrack->Charge());
+    }
+    
+    //Return if the input data is not "AOD" or "ESD"
+    else
+    {
+      printf("Wrong input data type! Should be \"AOD\" or \"ESD\"\n");
+      if(clusterArray)
+       {
+         clusterArray->Clear();
+         delete clusterArray;
+       }
+      return;
+    }
+    
+    if(!trackParam) continue;
+
+    //Extrapolate the track to EMCal surface
+    AliExternalTrackParam emcalParam(*trackParam);
+    Float_t eta, phi;
+    if(!ExtrapolateTrackToEMCalSurface(&emcalParam, 430., fMass, fStepSurface, eta, phi)) 
+      {
+       if(aodevent && trackParam) delete trackParam;
+       continue;
+      }
+
+    if(esdevent)
+      {
+       esdTrack->SetOuterParam(&emcalParam,AliExternalTrackParam::kMultSec);
+      }
+
+    if(TMath::Abs(eta)>0.75 || (phi) < 70*TMath::DegToRad() || (phi) > 190*TMath::DegToRad())
+      {
+       if(aodevent && trackParam) delete trackParam;
+       continue;
+      }
+
+
+    //Find matched clusters
+    Int_t index = -1;
+    Float_t dEta = -999, dPhi = -999;
+    if(!clusterArr)
+      {
+       index = FindMatchedClusterInClusterArr(&emcalParam, &emcalParam, clusterArray, dEta, dPhi);  
+      }
+    else
+      {
+       index = FindMatchedClusterInClusterArr(&emcalParam, &emcalParam, clusterArr, dEta, dPhi);  
+      }  
     
     if(index>-1)
     {
-      fMatchedClusterIndex->AddAt(index,matched);
-      fResidualZ->AddAt(dZMax,matched);
-      fResidualR->AddAt(dRMax,matched);
+      fMatchedTrackIndex   ->AddAt(itr,matched);
+      fMatchedClusterIndex ->AddAt(index,matched);
+      fResidualEta         ->AddAt(dEta,matched);
+      fResidualPhi         ->AddAt(dPhi,matched);
       matched++;
     }
-    delete emctrack;
+    if(aodevent && trackParam) delete trackParam;
   }//track loop
-  fMatchedClusterIndex->Set(matched);
-  fResidualZ->Set(matched);
-  fResidualR->Set(matched);
+
+  if(clusterArray)
+    {
+      clusterArray->Clear();
+      delete clusterArray;
+    }
+  
+  AliDebug(2,Form("Number of matched pairs = %d !\n",matched));
   
-  //printf("Number of matched pairs: %d\n",matched);
+  fMatchedTrackIndex   ->Set(matched);
+  fMatchedClusterIndex ->Set(matched);
+  fResidualPhi         ->Set(matched);
+  fResidualEta         ->Set(matched);
 }
 
-//__________________________________________________
-void AliEMCALRecoUtils::GetMatchedResiduals(Int_t index, Float_t &dR, Float_t &dZ)
+//________________________________________________________________________________
+Int_t AliEMCALRecoUtils::FindMatchedClusterInEvent(AliESDtrack *track, AliVEvent *event, AliEMCALGeometry *geom, Float_t &dEta, Float_t &dPhi)
+{
+  //
+  // This function returns the index of matched cluster to input track
+  // Returns -1 if no match is found
+  Int_t index = -1;
+  Double_t phiV = track->Phi()*TMath::RadToDeg();
+  if(TMath::Abs(track->Eta())>0.8 || phiV <= 20 || phiV >= 240 ) return index;
+  AliExternalTrackParam *trackParam = const_cast<AliExternalTrackParam*>(track->GetInnerParam());
+  if(!trackParam) return index;
+  AliExternalTrackParam emcalParam(*trackParam);
+  Float_t eta, phi;
+  if(!ExtrapolateTrackToEMCalSurface(&emcalParam, 430., fMass, fStepSurface, eta, phi)) return index;
+  if(TMath::Abs(eta)>0.75 || (phi) < 70*TMath::DegToRad() || (phi) > 190*TMath::DegToRad()) return index;
+
+  TObjArray *clusterArr = new TObjArray(event->GetNumberOfCaloClusters());
+
+  for(Int_t icl=0; icl<event->GetNumberOfCaloClusters(); icl++)
+  {
+    AliVCluster *cluster = (AliVCluster*) event->GetCaloCluster(icl);
+    if(geom && !IsGoodCluster(cluster,geom,(AliVCaloCells*)event->GetEMCALCells())) continue;
+    clusterArr->AddAt(cluster,icl);
+  }
+
+  index = FindMatchedClusterInClusterArr(&emcalParam, &emcalParam, clusterArr, dEta, dPhi);  
+  clusterArr->Clear();
+  delete clusterArr;
+  
+  return index;
+}
+
+//________________________________________________________________________________
+Int_t  AliEMCALRecoUtils::FindMatchedClusterInClusterArr(AliExternalTrackParam *emcalParam, AliExternalTrackParam *trkParam, TObjArray * clusterArr, Float_t &dEta, Float_t &dPhi)
+{
+  dEta=-999, dPhi=-999;
+  Float_t dRMax = fCutR, dEtaMax=fCutEta, dPhiMax=fCutPhi;
+  Int_t index = -1;
+  Float_t tmpEta=-999, tmpPhi=-999;
+
+  Double_t exPos[3] = {0.,0.,0.};
+  if(!emcalParam->GetXYZ(exPos)) return index;
+
+  Float_t clsPos[3] = {0.,0.,0.};
+  for(Int_t icl=0; icl<clusterArr->GetEntriesFast(); icl++)
+    {
+      AliVCluster *cluster = dynamic_cast<AliVCluster*> (clusterArr->At(icl)) ;
+      if(!cluster || !cluster->IsEMCAL()) continue;
+      cluster->GetPosition(clsPos);
+      Double_t dR = TMath::Sqrt(TMath::Power(exPos[0]-clsPos[0],2)+TMath::Power(exPos[1]-clsPos[1],2)+TMath::Power(exPos[2]-clsPos[2],2));
+      if(dR > fClusterWindow) continue;
+
+      AliExternalTrackParam trkPamTmp (*trkParam);//Retrieve the starting point every time before the extrapolation
+      if(!ExtrapolateTrackToCluster(&trkPamTmp, cluster, fMass, fStepCluster, tmpEta, tmpPhi)) continue;
+      if(fCutEtaPhiSum)
+        {
+          Float_t tmpR=TMath::Sqrt(tmpEta*tmpEta + tmpPhi*tmpPhi);
+          if(tmpR<dRMax)
+           {
+             dRMax=tmpR;
+             dEtaMax=tmpEta;
+             dPhiMax=tmpPhi;
+             index=icl;
+           }
+        }
+      else if(fCutEtaPhiSeparate)
+        {
+          if(TMath::Abs(tmpEta)<TMath::Abs(dEtaMax) && TMath::Abs(tmpPhi)<TMath::Abs(dPhiMax))
+           {
+             dEtaMax = tmpEta;
+             dPhiMax = tmpPhi;
+             index=icl;
+           }
+        }
+      else
+        {
+          printf("Error: please specify your cut criteria\n");
+          printf("To cut on sqrt(dEta^2+dPhi^2), use: SwitchOnCutEtaPhiSum()\n");
+          printf("To cut on dEta and dPhi separately, use: SwitchOnCutEtaPhiSeparate()\n");
+          return index;
+        }
+    }
+
+  dEta=dEtaMax;
+  dPhi=dPhiMax;
+
+  return index;
+}
+
+//------------------------------------------------------------------------------------
+Bool_t AliEMCALRecoUtils::ExtrapolateTrackToEMCalSurface(AliExternalTrackParam *trkParam, 
+                                                         Double_t emcalR,
+                                                         Double_t mass, 
+                                                         Double_t step, 
+                                                         Float_t &eta, 
+                                                         Float_t &phi)
+{
+  //Extrapolate track to EMCAL surface
+  
+  eta = -999, phi = -999;
+  if(!trkParam) return kFALSE;
+  if(!AliTrackerBase::PropagateTrackToBxByBz(trkParam, emcalR, mass, step, kTRUE, 0.8, -1)) return kFALSE;
+  Double_t trkPos[3] = {0.,0.,0.};
+  if(!trkParam->GetXYZ(trkPos)) return kFALSE;
+  TVector3 trkPosVec(trkPos[0],trkPos[1],trkPos[2]);
+  eta = trkPosVec.Eta();
+  phi = trkPosVec.Phi();
+  if(phi<0)
+    phi += 2*TMath::Pi();
+
+  return kTRUE;
+}
+
+//-----------------------------------------------------------------------------------
+Bool_t AliEMCALRecoUtils::ExtrapolateTrackToPosition(AliExternalTrackParam *trkParam, 
+                                                     const Float_t *clsPos, 
+                                                     Double_t mass, 
+                                                     Double_t step, 
+                                                     Float_t &tmpEta, 
+                                                     Float_t &tmpPhi)
+{
+  //
+  //Return the residual by extrapolating a track param to a global position
+  //
+  tmpEta = -999;
+  tmpPhi = -999;
+  if(!trkParam) return kFALSE;
+  Double_t trkPos[3] = {0.,0.,0.};
+  TVector3 vec(clsPos[0],clsPos[1],clsPos[2]);
+  Double_t alpha =  ((int)(vec.Phi()*TMath::RadToDeg()/20)+0.5)*20*TMath::DegToRad();
+  vec.RotateZ(-alpha); //Rotate the cluster to the local extrapolation coordinate system
+  if(!AliTrackerBase::PropagateTrackToBxByBz(trkParam, vec.X(), mass, step,kTRUE, 0.8, -1)) return kFALSE;
+  if(!trkParam->GetXYZ(trkPos)) return kFALSE; //Get the extrapolated global position
+
+  TVector3 clsPosVec(clsPos[0],clsPos[1],clsPos[2]);
+  TVector3 trkPosVec(trkPos[0],trkPos[1],trkPos[2]);
+
+  // track cluster matching
+  tmpPhi = clsPosVec.DeltaPhi(trkPosVec);    // tmpPhi is between -pi and pi
+  tmpEta = clsPosVec.Eta()-trkPosVec.Eta();
+
+  return kTRUE;
+}
+
+//----------------------------------------------------------------------------------
+Bool_t AliEMCALRecoUtils::ExtrapolateTrackToCluster(AliExternalTrackParam *trkParam, 
+                                                    AliVCluster *cluster, 
+                                                    Double_t mass, 
+                                                    Double_t step, 
+                                                    Float_t &tmpEta, 
+                                                    Float_t &tmpPhi)
 {
-  //Given a cluster index as in AliESDEvent::GetCaloCluster(index)
-  //Get the residuals dR and dZ for this cluster
-  //It only works with ESDs, not AODs
+  //
+  //Return the residual by extrapolating a track param to a cluster
+  //
+  tmpEta = -999;
+  tmpPhi = -999;
+  if(!cluster || !trkParam) return kFALSE;
+
+  Float_t clsPos[3] = {0.,0.,0.};
+  cluster->GetPosition(clsPos);
+
+  return ExtrapolateTrackToPosition(trkParam, clsPos, mass, step, tmpEta, tmpPhi);
+}
+
+//---------------------------------------------------------------------------------
+Bool_t AliEMCALRecoUtils::ExtrapolateTrackToCluster(AliExternalTrackParam *trkParam, 
+                                                    AliVCluster *cluster, 
+                                                    Float_t &tmpEta, 
+                                                    Float_t &tmpPhi)
+{
+  //
+  //Return the residual by extrapolating a track param to a clusterfStepCluster
+  //
+
+  return ExtrapolateTrackToCluster(trkParam, cluster, fMass, fStepCluster, tmpEta, tmpPhi);
+}
+
 
-  if( FindMatchedPos(index) >= 999 )
+//_______________________________________________________________________________________
+void AliEMCALRecoUtils::GetMatchedResiduals(Int_t clsIndex, Float_t &dEta, Float_t &dPhi)
+{
+  //Given a cluster index as in AliESDEvent::GetCaloCluster(clsIndex)
+  //Get the residuals dEta and dPhi for this cluster to the closest track
+  //Works with ESDs and AODs
+
+  if( FindMatchedPosForCluster(clsIndex) >= 999 )
   {
     AliDebug(2,"No matched tracks found!\n");
-    dR=999.;
-    dZ=999.;
+    dEta=999.;
+    dPhi=999.;
+    return;
+  }
+  dEta = fResidualEta->At(FindMatchedPosForCluster(clsIndex));
+  dPhi = fResidualPhi->At(FindMatchedPosForCluster(clsIndex));
+}
+//______________________________________________________________________________________________
+void AliEMCALRecoUtils::GetMatchedClusterResiduals(Int_t trkIndex, Float_t &dEta, Float_t &dPhi)
+{
+  //Given a track index as in AliESDEvent::GetTrack(trkIndex)
+  //Get the residuals dEta and dPhi for this track to the closest cluster
+  //Works with ESDs and AODs
+
+  if( FindMatchedPosForTrack(trkIndex) >= 999 )
+  {
+    AliDebug(2,"No matched cluster found!\n");
+    dEta=999.;
+    dPhi=999.;
     return;
   }
-  dR = fResidualR->At(FindMatchedPos(index));
-  dZ = fResidualZ->At(FindMatchedPos(index));
+  dEta = fResidualEta->At(FindMatchedPosForTrack(trkIndex));
+  dPhi = fResidualPhi->At(FindMatchedPosForTrack(trkIndex));
+}
+
+//__________________________________________________________
+Int_t AliEMCALRecoUtils::GetMatchedTrackIndex(Int_t clsIndex)
+{
+  //Given a cluster index as in AliESDEvent::GetCaloCluster(clsIndex)
+  //Get the index of matched track to this cluster
+  //Works with ESDs and AODs
+  
+  if(IsClusterMatched(clsIndex))
+    return fMatchedTrackIndex->At(FindMatchedPosForCluster(clsIndex));
+  else 
+    return -1; 
+}
+
+//__________________________________________________________
+Int_t AliEMCALRecoUtils::GetMatchedClusterIndex(Int_t trkIndex)
+{
+  //Given a track index as in AliESDEvent::GetTrack(trkIndex)
+  //Get the index of matched cluster to this track
+  //Works with ESDs and AODs
+  
+  if(IsTrackMatched(trkIndex))
+    return fMatchedClusterIndex->At(FindMatchedPosForTrack(trkIndex));
+  else 
+    return -1; 
 }
 
 //__________________________________________________
-Bool_t AliEMCALRecoUtils::IsMatched(Int_t index)
+Bool_t AliEMCALRecoUtils::IsClusterMatched(Int_t clsIndex) const
 {
-  //Given a cluster index as in AliESDEvent::GetCaloCluster(index)
-  //Returns if cluster has a match
-  if(FindMatchedPos(index) < 999) 
+  //Given a cluster index as in AliESDEvent::GetCaloCluster(clsIndex)
+  //Returns if the cluster has a match
+  if(FindMatchedPosForCluster(clsIndex) < 999) 
     return kTRUE;
   else
     return kFALSE;
 }
+
 //__________________________________________________
-UInt_t AliEMCALRecoUtils::FindMatchedPos(Int_t index) const
+Bool_t AliEMCALRecoUtils::IsTrackMatched(Int_t trkIndex) const 
 {
-  //Given a cluster index as in AliESDEvent::GetCaloCluster(index)
+  //Given a track index as in AliESDEvent::GetTrack(trkIndex)
+  //Returns if the track has a match
+  if(FindMatchedPosForTrack(trkIndex) < 999) 
+    return kTRUE;
+  else
+    return kFALSE;
+}
+
+//__________________________________________________________
+UInt_t AliEMCALRecoUtils::FindMatchedPosForCluster(Int_t clsIndex) const
+{
+  //Given a cluster index as in AliESDEvent::GetCaloCluster(clsIndex)
   //Returns the position of the match in the fMatchedClusterIndex array
   Float_t tmpR = fCutR;
   UInt_t pos = 999;
-
+  
   for(Int_t i=0; i<fMatchedClusterIndex->GetSize(); i++)
-    {
-      if(fMatchedClusterIndex->At(i)==index && fResidualR->At(i)<tmpR)
-       {
-         pos=i;
-         tmpR=fResidualR->At(i);
-       }
-      AliDebug(3,Form("Matched cluster pos: %d, index: %d, dR: %2.4f, dZ: %2.4f.\n",i,fMatchedClusterIndex->At(i),fResidualR->At(i),fResidualZ->At(i)));
-    }
+  {
+    if(fMatchedClusterIndex->At(i)==clsIndex)
+      {
+       Float_t r = TMath::Sqrt(fResidualEta->At(i)*fResidualEta->At(i) + fResidualPhi->At(i)*fResidualPhi->At(i));
+       if(r<tmpR)
+         {
+           pos=i;
+           tmpR=r;
+           AliDebug(3,Form("Matched cluster index: index: %d, dEta: %2.4f, dPhi: %2.4f.\n",fMatchedClusterIndex->At(i),fResidualEta->At(i),fResidualPhi->At(i)));
+         }
+      }
+  }
   return pos;
 }
 
+//__________________________________________________________
+UInt_t AliEMCALRecoUtils::FindMatchedPosForTrack(Int_t trkIndex) const
+{
+  //Given a track index as in AliESDEvent::GetTrack(trkIndex)
+  //Returns the position of the match in the fMatchedTrackIndex array
+  Float_t tmpR = fCutR;
+  UInt_t pos = 999;
+  
+  for(Int_t i=0; i<fMatchedTrackIndex->GetSize(); i++)
+  {
+    if(fMatchedTrackIndex->At(i)==trkIndex)
+      {
+       Float_t r = TMath::Sqrt(fResidualEta->At(i)*fResidualEta->At(i) + fResidualPhi->At(i)*fResidualPhi->At(i));
+       if(r<tmpR)
+         {
+           pos=i;
+           tmpR=r;
+           AliDebug(3,Form("Matched track index: index: %d, dEta: %2.4f, dPhi: %2.4f.\n",fMatchedTrackIndex->At(i),fResidualEta->At(i),fResidualPhi->At(i)));
+         }
+      }
+  }
+  return pos;
+}
+
+//___________________________________________________________________________________
+Bool_t AliEMCALRecoUtils::IsGoodCluster(AliVCluster *cluster, AliEMCALGeometry *geom, 
+                                        AliVCaloCells* cells,const Int_t bc)
+{
+  // check if the cluster survives some quality cut
+  //
+  //
+  Bool_t isGood=kTRUE;
+  
+  if(!cluster || !cluster->IsEMCAL())              return kFALSE;
+  
+  if(ClusterContainsBadChannel(geom,cluster->GetCellsAbsId(),cluster->GetNCells())) return kFALSE;
+  
+  if(!CheckCellFiducialRegion(geom,cluster,cells)) return kFALSE;
+  
+  if(IsExoticCluster(cluster, cells,bc))           return kFALSE;
+
+  return isGood;
+}
+
+//__________________________________________________________
 Bool_t AliEMCALRecoUtils::IsAccepted(AliESDtrack *esdTrack)
 {
   // Given a esd track, return whether the track survive all the cuts
@@ -1059,9 +2036,12 @@ Bool_t AliEMCALRecoUtils::IsAccepted(AliESDtrack *esdTrack)
 
 
   //DCA cuts
-  Float_t MaxDCAToVertexXYPtDep = 0.0182 + 0.0350/TMath::Power(esdTrack->Pt(),1.01); //This expression comes from AliESDtrackCuts::GetStandardITSTPCTrackCuts2010()
-  //AliDebug(3,Form("Track pT = %f, DCAtoVertexXY = %f",esdTrack->Pt(),MaxDCAToVertexXYPtDep));
-  SetMaxDCAToVertexXY(MaxDCAToVertexXYPtDep); //Set pT dependent DCA cut to vertex in x-y plane
+  if(fTrackCutsType==kGlobalCut)
+    {
+      Float_t maxDCAToVertexXYPtDep = 0.0182 + 0.0350/TMath::Power(esdTrack->Pt(),1.01); //This expression comes from AliESDtrackCuts::GetStandardITSTPCTrackCuts2010()
+      //AliDebug(3,Form("Track pT = %f, DCAtoVertexXY = %f",esdTrack->Pt(),MaxDCAToVertexXYPtDep));
+      SetMaxDCAToVertexXY(maxDCAToVertexXYPtDep); //Set pT dependent DCA cut to vertex in x-y plane
+    }
 
 
   Float_t b[2];
@@ -1108,12 +2088,15 @@ Bool_t AliEMCALRecoUtils::IsAccepted(AliESDtrack *esdTrack)
   if (!fCutDCAToVertex2D && TMath::Abs(dcaToVertexZ) > fCutMaxDCAToVertexZ)
     cuts[9] = kTRUE;
 
-  //Require at least one SPD point + anything else in ITS
-  if( (esdTrack->HasPointOnITSLayer(0) || esdTrack->HasPointOnITSLayer(1)) == kFALSE)
-    cuts[10] = kTRUE;
+  if(fTrackCutsType==kGlobalCut)
+    {
+      //Require at least one SPD point + anything else in ITS
+      if( (esdTrack->HasPointOnITSLayer(0) || esdTrack->HasPointOnITSLayer(1)) == kFALSE)
+       cuts[10] = kTRUE;
+    }
 
   Bool_t cut=kFALSE;
-  for (Int_t i=0; i<kNCuts; i++) 
+  for (Int_t i=0; i<kNCuts; i++)
     if (cuts[i]) {cut = kTRUE;}
 
     // cut the track
@@ -1122,26 +2105,135 @@ Bool_t AliEMCALRecoUtils::IsAccepted(AliESDtrack *esdTrack)
   else 
     return kTRUE;
 }
-//__________________________________________________
+
+
+//_____________________________________
 void AliEMCALRecoUtils::InitTrackCuts()
 {
   //Intilize the track cut criteria
-  //By default these cuts are set according to AliESDtrackCuts::GetStandardITSTPCTrackCuts2010()
+  //By default these cuts are set according to AliESDtrackCuts::GetStandardTPCOnlyTrackCuts()
   //Also you can customize the cuts using the setters
   
-  //TPC
-  SetMinNClustersTPC(70);
-  SetMaxChi2PerClusterTPC(4);
-  SetAcceptKinkDaughters(kFALSE);
-  SetRequireTPCRefit(kTRUE);
+  switch (fTrackCutsType)
+  {
+    case kTPCOnlyCut:
+    {
+      AliInfo(Form("Track cuts for matching: GetStandardTPCOnlyTrackCuts()"));
+      //TPC
+      SetMinNClustersTPC(70);
+      SetMaxChi2PerClusterTPC(4);
+      SetAcceptKinkDaughters(kFALSE);
+      SetRequireTPCRefit(kFALSE);
+      
+      //ITS
+      SetRequireITSRefit(kFALSE);
+      SetMaxDCAToVertexZ(3.2);
+      SetMaxDCAToVertexXY(2.4);
+      SetDCAToVertex2D(kTRUE);
+      
+      break;
+    }
+      
+    case kGlobalCut:
+    {
+      AliInfo(Form("Track cuts for matching: GetStandardITSTPCTrackCuts2010(kTURE)"));
+      //TPC
+      SetMinNClustersTPC(70);
+      SetMaxChi2PerClusterTPC(4);
+      SetAcceptKinkDaughters(kFALSE);
+      SetRequireTPCRefit(kTRUE);
+      
+      //ITS
+      SetRequireITSRefit(kTRUE);
+      SetMaxDCAToVertexZ(2);
+      SetMaxDCAToVertexXY();
+      SetDCAToVertex2D(kFALSE);
+      
+      break;
+    }
+      
+    case kLooseCut:
+    {
+      AliInfo(Form("Track cuts for matching: Loose cut w/o DCA cut"));
+      SetMinNClustersTPC(50);
+      SetAcceptKinkDaughters(kTRUE);
+      
+      break;
+    }
+  }
+}
+
+
+//________________________________________________________________________
+void AliEMCALRecoUtils::SetClusterMatchedToTrack(const AliESDEvent *event)
+{
+  // Checks if tracks are matched to EMC clusters and set the matched EMCAL cluster index to ESD track. 
+  
+  Int_t nTracks = event->GetNumberOfTracks();
+  for (Int_t iTrack = 0; iTrack < nTracks; ++iTrack) {
+    AliESDtrack* track = event->GetTrack(iTrack);
+    if (!track) {
+      AliWarning(Form("Could not receive track %d", iTrack));
+      continue;
+    }
+    Int_t matchClusIndex = GetMatchedClusterIndex(iTrack);                
+    track->SetEMCALcluster(matchClusIndex); //sets -1 if track not matched within residual
+    if(matchClusIndex != -1) 
+      track->SetStatus(AliESDtrack::kEMCALmatch);
+    else
+      track->ResetStatus(AliESDtrack::kEMCALmatch);
+  }
+    AliDebug(2,"Track matched to closest cluster");    
+}
+
+//_________________________________________________________________________
+void AliEMCALRecoUtils::SetTracksMatchedToCluster(const AliESDEvent *event)
+{
+  // Checks if EMC clusters are matched to ESD track.
+  // Adds track indexes of all the tracks matched to a cluster withing residuals in ESDCalocluster.
+  
+  for (Int_t iClus=0; iClus < event->GetNumberOfCaloClusters(); ++iClus) {
+    AliESDCaloCluster *cluster = event->GetCaloCluster(iClus);
+    if (!cluster->IsEMCAL()) 
+      continue;
+    
+    Int_t nTracks = event->GetNumberOfTracks();
+    TArrayI arrayTrackMatched(nTracks);
+    
+    // Get the closest track matched to the cluster
+    Int_t nMatched = 0;
+    Int_t matchTrackIndex = GetMatchedTrackIndex(iClus);
+    if (matchTrackIndex != -1) {
+      arrayTrackMatched[nMatched] = matchTrackIndex;
+      nMatched++;
+    }
+    
+    // Get all other tracks matched to the cluster
+    for(Int_t iTrk=0; iTrk<nTracks; ++iTrk) {
+      AliESDtrack* track = event->GetTrack(iTrk);
+      if(iTrk == matchTrackIndex) continue;
+      if(track->GetEMCALcluster() == iClus){
+        arrayTrackMatched[nMatched] = iTrk;
+        ++nMatched;
+      }
+    }
+    
+    //printf("Tender::SetTracksMatchedToCluster - cluster E %f, N matches %d, first match %d\n",cluster->E(),nMatched,arrayTrackMatched[0]);
+    
+    arrayTrackMatched.Set(nMatched);
+    cluster->AddTracksMatched(arrayTrackMatched);
+    
+    Float_t eta= -999, phi = -999;
+    if (matchTrackIndex != -1) 
+      GetMatchedResiduals(iClus, eta, phi);
+    cluster->SetTrackDistance(phi, eta);
+  }
   
-  //ITS
-  SetRequireITSRefit(kTRUE);
-  SetMaxDCAToVertexZ(2);
-  SetDCAToVertex2D(kFALSE);
+    AliDebug(2,"Cluster matched to tracks");   
 }
 
-//__________________________________________________
+
+//___________________________________________________
 void AliEMCALRecoUtils::Print(const Option_t *) const 
 {
   // Print Parameters
@@ -1156,14 +2248,63 @@ void AliEMCALRecoUtils::Print(const Option_t *) const
   
   printf("Position Recalculation option %d, Particle Type %d, fW0 %2.2f, Recalibrate Data %d \n",fPosAlgo,fParticleType,fW0, fRecalibration);
 
-  printf("Matching criteria: dR < %2.2f[cm], dZ < %2.2f[cm]\n",fCutR,fCutZ);
+  printf("Matching criteria: ");
+  if(fCutEtaPhiSum)
+    {
+      printf("sqrt(dEta^2+dPhi^2)<%4.3f\n",fCutR);
+    }
+  else if(fCutEtaPhiSeparate)
+    {
+      printf("dEta<%4.3f, dPhi<%4.3f\n",fCutEta,fCutPhi);
+    }
+  else
+    {
+      printf("Error\n");
+      printf("please specify your cut criteria\n");
+      printf("To cut on sqrt(dEta^2+dPhi^2), use: SwitchOnCutEtaPhiSum()\n");
+      printf("To cut on dEta and dPhi separately, use: SwitchOnCutEtaPhiSeparate()\n");
+    }
+
+  printf("Mass hypothesis = %2.3f [GeV/c^2], extrapolation step to surface = %2.2f[cm], step to cluster = %2.2f[cm]\n",fMass,fStepSurface, fStepCluster);
+  printf("Cluster selection window: dR < %2.0f\n",fClusterWindow);
 
   printf("Track cuts: \n");
+  printf("Minimum track pT: %1.2f\n",fCutMinTrackPt);
+  printf("AOD track selection mask: %d\n",fAODFilterMask);
   printf("TPCRefit = %d, ITSRefit = %d\n",fCutRequireTPCRefit,fCutRequireITSRefit);
   printf("AcceptKinks = %d\n",fCutAcceptKinkDaughters);
   printf("MinNCulsterTPC = %d, MinNClusterITS = %d\n",fCutMinNClusterTPC,fCutMinNClusterITS);
   printf("MaxChi2TPC = %2.2f, MaxChi2ITS = %2.2f\n",fCutMaxChi2PerClusterTPC,fCutMaxChi2PerClusterITS);
   printf("DCSToVertex2D = %d, MaxDCAToVertexXY = %2.2f, MaxDCAToVertexZ = %2.2f\n",fCutDCAToVertex2D,fCutMaxDCAToVertexXY,fCutMaxDCAToVertexZ);
 
-    
 }
+
+//_____________________________________________________________________
+void AliEMCALRecoUtils::SetRunDependentCorrections(Int_t runnumber){
+  //Get EMCAL time dependent corrections from file and put them in the recalibration histograms
+  //Do it only once and only if it is requested
+  
+  if(!fUseRunCorrectionFactors) return;
+  if(fRunCorrectionFactorsSet)  return;
+  
+  AliInfo(Form("AliEMCALRecoUtils::GetRunDependentCorrections() - Get Correction Factors for Run number %d\n",runnumber));
+  AliEMCALCalibTimeDepCorrection  *corr =  new AliEMCALCalibTimeDepCorrection();
+  corr->ReadRootInfo(Form("CorrectionFiles/Run%d_Correction.root",runnumber));
+  
+  SwitchOnRecalibration();
+  for(Int_t ism = 0; ism < 4; ism++){
+    for(Int_t icol = 0; icol < 48; icol++){
+      for(Int_t irow = 0; irow < 24; irow++){
+        Float_t orgRecalFactor = GetEMCALChannelRecalibrationFactors(ism)->GetBinContent(icol,irow);
+        Float_t newRecalFactor = orgRecalFactor*corr->GetCorrection(ism, icol,irow,0);
+        GetEMCALChannelRecalibrationFactors(ism)->SetBinContent(icol,irow,newRecalFactor);
+        //printf("ism %d, icol %d, irow %d, corrections : org %f, time dep %f, final %f (org*time %f)\n",ism, icol, irow, 
+        //        orgRecalFactor, corr->GetCorrection(ism, icol,irow,0),
+        //       (GetEMCALChannelRecalibrationFactors(ism))->GetBinContent(icol,irow),newRecalFactor);
+      }
+    }
+  }
+   fRunCorrectionFactorsSet = kTRUE;
+}
+