]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALCalibTimeDep.cxx
coverity
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALCalibTimeDep.cxx
index cafac06e3f93da0219202b31327f100a9bf4f90d..556f31809a4ca2efd2b19416c755499051f0cb55 100644 (file)
 ///*-- Author: 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
-// class for EMCAL time-dep calibration                                      //
+// class for EMCAL time-dep calibration   
+// - supposed to run in preprocessor
+// we use input from the following sources:
+// AliEMCALCalibTempCoeff (APD temperature coefficients),
+// AliCaloCalibSignal (LED DA), AliEMCALSensorTempArray (ELMB DCS)
+// AliEMCALCalibReference: LED amplitude and temperature info at reference time
+//
+// output/result is in AliEMCALCalibTimeDepCorrection 
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
 #include <iostream>
 #include <TGraphSmooth.h>
+#include <TMath.h>
 #include "AliLog.h"
 #include "AliCDBEntry.h"
 #include "AliCDBManager.h"
 #include "AliEMCALSensorTempArray.h"
 #include "AliCaloCalibSignal.h"
-#include "AliEMCALBiasAPD.h"
-#include "AliEMCALCalibMapAPD.h"
+#include "AliEMCALCalibTempCoeff.h"
 #include "AliEMCALCalibReference.h"
 #include "AliEMCALCalibTimeDepCorrection.h" 
 #include "AliEMCALCalibTimeDep.h"
 
 /* first a bunch of constants.. */
-const double fkSecToHour = 1.0/3600.0; // conversion factor from seconds to hours
+const double kSecToHour = 1.0/3600.0; // conversion factor from seconds to hours
 
 // some global variables for APD handling; values from Catania studies, best fit
 // TempCoeff = p0+p1*M (M=gain), where p0 and and p1 are functions of the dark current
-const double fkTempCoeffP0Const = -0.903; // 
-const double fkTempCoeffP0Factor = -1.381e7; // 
-const double fkTempCoeffP1Const = -0.023; // 
-const double fkTempCoeffP1Factor = -4.966e5; //
+const double kTempCoeffP0Const = -0.903; // 
+const double kTempCoeffP0Factor = -1.381e7; // 
+const double kTempCoeffP1Const = -0.023; // 
+const double kTempCoeffP1Factor = -4.966e5; //
  
-const double fkErrorCode = -999; // to indicate that something went wrong
+const double kTempMaxDiffMedian = 2; // Temperature values should not be further away from median value within SM when considered in the average calc.
+
+const double kErrorCode = -999; // to indicate that something went wrong
 
 using namespace std;
 
@@ -59,17 +68,22 @@ AliEMCALCalibTimeDep::AliEMCALCalibTimeDep() :
   fEndTime(0),
   fMinTemp(0),
   fMaxTemp(0),
+  fMinTempVariation(0),
+  fMaxTempVariation(0),
+  fMinTempValid(15),
+  fMaxTempValid(35),
   fMinTime(0),
   fMaxTime(0),
   fTemperatureResolution(0.1), // 0.1 deg C is default
+  fMaxTemperatureDiff(5), // 5 deg C is default max diff relative to reference 
   fTimeBinsPerHour(2), // 2 30-min bins per hour is default
   fHighLowGainFactor(16), // factor ~16 between High gain and low gain
   fTempArray(NULL),
   fCalibSignal(NULL),
-  fBiasAPD(NULL),
-  fCalibMapAPD(NULL),
+  fCalibTempCoeff(NULL),
   fCalibReference(NULL),
-  fCalibTimeDepCorrection(NULL)
+  fCalibTimeDepCorrection(NULL),
+  fVerbosity(0)
 {
   // Constructor
 }
@@ -82,17 +96,22 @@ AliEMCALCalibTimeDep::AliEMCALCalibTimeDep(const AliEMCALCalibTimeDep& calibt) :
   fEndTime(calibt.GetEndTime()),
   fMinTemp(calibt.GetMinTemp()),
   fMaxTemp(calibt.GetMaxTemp()),
+  fMinTempVariation(calibt.GetMinTempVariation()),
+  fMaxTempVariation(calibt.GetMaxTempVariation()),
+  fMinTempValid(calibt.GetMinTempValid()),
+  fMaxTempValid(calibt.GetMaxTempValid()),
   fMinTime(calibt.GetMinTime()),
   fMaxTime(calibt.GetMaxTime()),
   fTemperatureResolution(calibt.GetTemperatureResolution()),
+  fMaxTemperatureDiff(calibt.GetMaxTemperatureDiff()),
   fTimeBinsPerHour(calibt.GetTimeBinsPerHour()),
   fHighLowGainFactor(calibt.GetHighLowGainFactor()),
   fTempArray(calibt.GetTempArray()),
   fCalibSignal(calibt.GetCalibSignal()),
-  fBiasAPD(calibt.GetBiasAPD()),
-  fCalibMapAPD(calibt.GetCalibMapAPD()),
+  fCalibTempCoeff(calibt.GetCalibTempCoeff()),
   fCalibReference(calibt.GetCalibReference()),
-  fCalibTimeDepCorrection(calibt.GetCalibTimeDepCorrection())
+  fCalibTimeDepCorrection(calibt.GetCalibTimeDepCorrection()),
+  fVerbosity(calibt.GetVerbosity())
 {
   // copy constructor
 }
@@ -123,16 +142,21 @@ void  AliEMCALCalibTimeDep::Reset()
   fEndTime = 0;
   fMinTemp = 0;
   fMaxTemp = 0;
+  fMinTempVariation = 0;
+  fMaxTempVariation = 0;
+  fMinTempValid = 15; 
+  fMaxTempValid = 35;
   fMinTime = 0;
   fMaxTime = 0;
   fTemperatureResolution = 0.1; // 0.1 deg C is default
+  fMaxTemperatureDiff = 5; // 5 deg C is default max diff relative to reference 
   fTimeBinsPerHour = 2; // 2 30-min bins per hour is default
   fTempArray = NULL;
   fCalibSignal = NULL;
-  fBiasAPD = NULL;
-  fCalibMapAPD = NULL;
+  fCalibTempCoeff = NULL;
   fCalibReference = NULL;
   fCalibTimeDepCorrection = NULL;
+  fVerbosity = 0;
   return;
 }
 
@@ -145,11 +169,18 @@ void  AliEMCALCalibTimeDep::PrintInfo() const
   cout << " VARIABLE DUMP: " << endl
        << " GetStartTime() " << GetStartTime() << endl
        << " GetEndTime() " << GetEndTime() << endl
+       << " GetMinTime() " << GetMinTime() << endl
+       << " GetMaxTime() " << GetMaxTime() << endl
        << " GetMinTemp() " << GetMinTemp() << endl
-       << " GetMaxTemp() " << GetMaxTemp() << endl;
+       << " GetMaxTemp() " << GetMaxTemp() << endl
+       << " GetMinTempVariation() " << GetMinTempVariation() << endl
+       << " GetMaxTempVariation() " << GetMaxTempVariation() << endl
+       << " GetTemperatureResolution() " << GetTemperatureResolution() << endl;
   // run ranges
   cout << " RUN INFO: " << endl
+       << " runnumber " << GetRunNumber() << endl
        << " length (in hours) " << GetLengthOfRunInHours() << endl
+       << " length (in bins) " << GetLengthOfRunInBins() << endl
        << " range of temperature measurements (in hours) " << GetRangeOfTempMeasureInHours()
        << " (in deg. C) " << GetRangeOfTempMeasureInDegrees()
        << endl;
@@ -160,19 +191,19 @@ void  AliEMCALCalibTimeDep::PrintInfo() const
 //________________________________________________________________ 
 Double_t AliEMCALCalibTimeDep::GetLengthOfRunInHours() const
 {
-  return (fEndTime - fStartTime)*fkSecToHour;
+  return (fEndTime - fStartTime)*kSecToHour;
 }
 
 //________________________________________________________________ 
 Double_t AliEMCALCalibTimeDep::GetLengthOfRunInBins() const
 {
-  return (fEndTime - fStartTime)*fkSecToHour*fTimeBinsPerHour;
+  return (fEndTime - fStartTime)*kSecToHour*fTimeBinsPerHour;
 }
 
 //________________________________________________________________ 
 Double_t AliEMCALCalibTimeDep::GetRangeOfTempMeasureInHours() const
 {
-  return (fMaxTime - fMinTime)*fkSecToHour;
+  return (fMaxTime - fMinTime)*kSecToHour;
 }
 
 //________________________________________________________________ 
@@ -184,7 +215,7 @@ Double_t AliEMCALCalibTimeDep::GetRangeOfTempMeasureInDegrees() const
 //________________________________________________________________
 void AliEMCALCalibTimeDep::Initialize(Int_t run, 
                                      UInt_t startTime, UInt_t endTime)
-{
+{ // setup, and get temperature info
   Reset(); // start fresh
 
   fRun = run;
@@ -198,51 +229,15 @@ void AliEMCALCalibTimeDep::Initialize(Int_t run,
   return;
 }
 
-//________________________________________________________________
-Double_t AliEMCALCalibTimeDep::GetTemperature(UInt_t timeStamp) const
-{// return estimate for all SuperModules and sensors, that had data 
-
-  // first convert from seconds to hours..
-  Double_t timeHour = (timeStamp - fStartTime) * fkSecToHour;
-
-  Double_t average = 0;
-  int n = 0;
-
-  for (int i=0; i<fTempArray->NumSensors(); i++) {
-    
-    AliEMCALSensorTemp *st = fTempArray->GetSensor(i);
-
-    // check if we had valid data for the time that is being asked for
-    if ( timeStamp>=st->GetStartTime() && timeStamp<=st->GetEndTime() ) {
-      AliSplineFit *f = st->GetFit();
-      if (f) { // ok, looks like we have valid data/info
-       // let's check what the expected value at the time appears to be
-       Double_t val = f->Eval(timeHour);
-       average += val;
-       n++;
-      }
-    } // time
-  } // loop over fTempArray
-  
-  if (n>0) { // some valid data was found
-    average /= n;
-    return average;
-  }
-  else { // no good data
-    return fkErrorCode;
-  }
-
-}
-
 //________________________________________________________________
 Double_t AliEMCALCalibTimeDep::GetTemperatureSM(int imod, UInt_t timeStamp) const
 {// return estimate for this one SuperModule, if it had data 
 
   // first convert from seconds to hours..
-  Double_t timeHour = (timeStamp - fStartTime) * fkSecToHour;
+  Double_t timeHour = (timeStamp - fStartTime) * kSecToHour;
 
-  Double_t average = 0;
   int n = 0;
+  Double_t valArr[8]={0}; // 8 sensors per SM
 
   for (int i=0; i<fTempArray->NumSensors(); i++) {
     
@@ -255,9 +250,13 @@ Double_t AliEMCALCalibTimeDep::GetTemperatureSM(int imod, UInt_t timeStamp) cons
        if (f) { // ok, looks like we have valid data/info
          // let's check what the expected value at the time appears to be
          Double_t val = f->Eval(timeHour);
-         cout << " i " << i << " val " << val << endl;
-         average += val;
-         n++;
+         if ( fVerbosity > 0 ) {
+           cout << " sensor i " << i << " val " << val << endl;
+         }
+         if (val>fMinTempValid && val<fMaxTempValid && n<8) { 
+           valArr[n] = val;
+           n++;
+         }
        }
       } // time
     }
@@ -265,76 +264,67 @@ Double_t AliEMCALCalibTimeDep::GetTemperatureSM(int imod, UInt_t timeStamp) cons
   } // loop over fTempArray
   
   if (n>0) { // some valid data was found
-    average /= n;
-    return average;
+    Double_t median = TMath::Median(n, valArr);
+    Double_t average = 0;
+    Int_t nval = 0;
+    for (int is=0; is<n; is++) {
+      if (TMath::Abs(valArr[is] - median) < kTempMaxDiffMedian) {
+       average += valArr[is];
+       nval++;
+      }
+    }
+    if (nval >  0) {
+      average /= nval;
+      return average;
+    }
+    else { // this case should not happen, but kept for completeness (coverity etc)
+      return median;
+    }
   }
   else { // no good data
-    return fkErrorCode;
+    return kErrorCode;
   }
 
 }
 
-//________________________________________________________________
-Double_t AliEMCALCalibTimeDep::GetTemperatureSMSensor(int imod, int isens, UInt_t timeStamp) const
-{// return estimate for this one SuperModule and sensor, if it had data 
-
-  // first convert from seconds to hours..
-  Double_t timeHour = (timeStamp - fStartTime) * fkSecToHour;
-
-  for (int i=0; i<fTempArray->NumSensors(); i++) {
-    
-    AliEMCALSensorTemp *st = fTempArray->GetSensor(i);
-    int module = st->GetSector()*2 + st->GetSide();
-    if ( module == imod && st->GetNum()==isens ) { // right module, and sensor
-      // check if we had valid data for the time that is being asked for
-      if ( timeStamp>=st->GetStartTime() && timeStamp<=st->GetEndTime() ) {
-       AliSplineFit *f = st->GetFit();
-       if (f) { // ok, looks like we have valid data/info
-         // let's check what the expected value at the time appears to be
-         Double_t val = f->Eval(timeHour);
-
-         return val; // no point to move further in for loop, we have found the sensor we were looking for
-       }
-      } // time
-    }
-    
-  } // loop over fTempArray
-  
-  // if we made it all here, it means that we didn't find the sensor we were looking for
-  // i.e. no good data
-  return fkErrorCode;
-
-}
-
 //________________________________________________________________
 Int_t AliEMCALCalibTimeDep::CalcCorrection()
 { // OK, this is where the real action takes place - the heart of this class..
   /* The philosophy is as follows:
-     0. Init corrections to 1.0 values
-     1: if we have LED info for the tower, use it
-     2. if not 1, we rely on LED info averaged over strip
-     3. if not 2 either, we try to use temperature info + APD bias and calibration info
+     0. Init corrections to 1.0 values, and see how many correction bins we need
+     1. Check how large temperature variations we have through the run - do we really need all the correction bias (otherwise adjust to single bin)
+     2. try to use temperature info + APD temperature coefficient info, to estimate correction.
+     For now (from Dec 2009), we do not use LED info.
    */
 
   // 0: Init
   // how many SuperModules do we have?
   Int_t nSM = fCalibReference->GetNSuperModule();
   // how many time-bins should we have for this run?
-  Int_t nBins = (Int_t) GetLengthOfRunInBins(); // round-down (from double to int)
+  Int_t nBins = (Int_t) (GetLengthOfRunInBins() + 1); // round-up (from double to int; always at least 1)
   Int_t binSize = (Int_t) (3600/fTimeBinsPerHour); // in seconds
+
+  // 1: get info on how much individual sensors might have changed during
+  // the run (compare max-min for each sensor separately)
+  if (fMaxTempVariation < fTemperatureResolution) {
+    nBins = 1; // just one bin needed..
+  }
+  if (nBins == 1) {    
+    binSize = fEndTime - fStartTime;
+  }
+  if (fVerbosity > 0) {
+    cout << " nBins " << nBins << " binSize " << binSize << endl;
+  }
+
   // set up a reasonable default (correction = 1.0)
+  fCalibTimeDepCorrection = new AliEMCALCalibTimeDepCorrection(nSM);
   fCalibTimeDepCorrection->InitCorrection(nSM, nBins, 1.0);
   fCalibTimeDepCorrection->SetStartTime(fStartTime);
   fCalibTimeDepCorrection->SetNTimeBins(nBins);
   fCalibTimeDepCorrection->SetTimeBinSize(binSize);
 
-  // 1+2: try with LED corrections
-  Int_t nRemaining = CalcLEDCorrection(nSM, nBins);
-
-  // 3: try with Temperature, if needed
-  if (nRemaining>0) {
-    nRemaining = CalcTemperatureCorrection(nSM, nBins);
-  }
+  // 2: try with Temperature correction 
+  Int_t nRemaining = CalcTemperatureCorrection(nSM, nBins, binSize);
 
   return nRemaining;
 }
@@ -345,12 +335,14 @@ Double_t AliEMCALCalibTimeDep::GetTempCoeff(Double_t IDark, Double_t M) const
 { // estimate the Temperature Coefficient, based on the dark current (IDark)
   // and the gain (M), based on Catania parameterizations
 
-  Double_t P0 = fkTempCoeffP0Const + fkTempCoeffP0Factor * IDark;
-  Double_t P1 = fkTempCoeffP1Const + fkTempCoeffP1Factor * IDark;
+  Double_t dP0 = kTempCoeffP0Const + kTempCoeffP0Factor * IDark;
+  Double_t dP1 = kTempCoeffP1Const + kTempCoeffP1Factor * IDark;
  
-  Double_t TC = P0 + P1*M;
+  Double_t dTC = dP0 + dP1*M;
+  // from % numbers to regular ones..:
+  dTC *= 0.01;
 
-  return TC;
+  return TMath::Abs(dTC); // return the absolute value, to avoid any sign confusion
 }
 
 /* Next come the methods that do the work in picking up all the needed info..*/
@@ -381,6 +373,8 @@ Int_t AliEMCALCalibTimeDep::ScanTemperatureInfo()
 
   fMinTemp = 999; // init to some large value (999 deg C)
   fMaxTemp = 0;
+  fMinTempVariation = 999; // init to some large value (999 deg C)
+  fMaxTempVariation = 0;
   fMinTime = 2147483647; // init to a large value in the far future (0x7fffffff), year 2038 times..
   fMaxTime = 0;
 
@@ -389,18 +383,40 @@ Int_t AliEMCALCalibTimeDep::ScanTemperatureInfo()
   for (int i=0; i<fTempArray->NumSensors(); i++) {
     
     AliEMCALSensorTemp *st = fTempArray->GetSensor(i);
+    if ( st->GetStartTime() == 0 ) { // no valid data
+      continue;
+    }
 
     // check time ranges
     if (fMinTime > st->GetStartTime()) { fMinTime = st->GetStartTime(); }
     if (fMaxTime < st->GetEndTime()) { fMaxTime = st->GetEndTime(); }
-
     // check temperature ranges
-    TGraph *g = st->GetGraph();
-    if (g) { // ok, looks like we have valid data/info
-      // let's check what the expected value at the time appears to be
-      if (fMinTemp > g->GetMinimum()) { fMinTemp = g->GetMinimum(); }
-      if (fMaxTemp < g->GetMaximum()) { fMaxTemp = g->GetMaximum(); }
-      n++;
+    AliSplineFit *f = st->GetFit();
+
+    if (f) { // ok, looks like we have valid data/info
+      int np = f->GetKnots();
+      Double_t *y0 = f->GetY0();
+      // min and max values within the single sensor
+      Double_t min = 999;
+      Double_t max = 0;
+      int nval = 0;
+      for (int ip=0; ip<np; ip++) { 
+       if (y0[ip]>fMinTempValid && y0[ip]<fMaxTempValid) {
+         if (min > y0[ip]) { min = y0[ip]; }
+         if (max < y0[ip]) { max = y0[ip]; }
+         nval++;
+       }
+      }
+      if (nval>0) {
+       if (fMinTemp > min) { fMinTemp = min; }
+       if (fMaxTemp < max) { fMaxTemp = max; }
+       Double_t variation = max - min;
+       if (fMinTempVariation > variation) { fMinTempVariation = variation; }
+       if (fMaxTempVariation < variation) { fMaxTempVariation = variation; }
+       
+       n++;
+      }
     }
   } // loop over fTempArray
   
@@ -408,7 +424,7 @@ Int_t AliEMCALCalibTimeDep::ScanTemperatureInfo()
     return n;
   }
   else { // no good data
-    return (Int_t) fkErrorCode;
+    return (Int_t) kErrorCode;
   }
 
 }
@@ -423,11 +439,11 @@ void AliEMCALCalibTimeDep::GetCalibSignalInfo()
   }
 
   if (fCalibSignal) { 
-    AliInfo( Form("CalibSignal: NEvents %d NAcceptedEvents %d Entries %d AvgEntries LEDRefEntries %d LEDRefAvgEntries %d",
+    AliInfo( Form("CalibSignal: NEvents %d NAcceptedEvents %d Entries %lld AvgEntries LEDRefEntries %lld LEDRefEntries %lld, LEDRefAvgEntries %lld",
                  fCalibSignal->GetNEvents(), fCalibSignal->GetNAcceptedEvents(),
                  fCalibSignal->GetTreeAmpVsTime()->GetEntries(),
                  fCalibSignal->GetTreeAvgAmpVsTime()->GetEntries(),
-                  fCalibSignal->GetTreeLEDAmpVsTime()->GetEntries(),
+                 fCalibSignal->GetTreeLEDAmpVsTime()->GetEntries(),
                  fCalibSignal->GetTreeLEDAvgAmpVsTime()->GetEntries() ) );               
   }
   else {
@@ -438,39 +454,20 @@ void AliEMCALCalibTimeDep::GetCalibSignalInfo()
 }
 
 //________________________________________________________________
-void AliEMCALCalibTimeDep::GetBiasAPDInfo() 
-{
-  // pick up Preprocessor output, based on fRun (most recent version)
-  AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/BiasAPD", fRun);
-  if (entry) {
-    fBiasAPD = (AliEMCALBiasAPD *) entry->GetObject();
-  }
-
-  if (fBiasAPD) { 
-    AliInfo( Form("BiasAPD: NSuperModule %d ", fBiasAPD->GetNSuperModule() ) );
-  }
-  else {
-    AliWarning( Form("AliEMCALBiasAPD not found!") );
-  }
-  
-  return;
-}
-
-//________________________________________________________________
-void AliEMCALCalibTimeDep::GetCalibMapAPDInfo() 
+void AliEMCALCalibTimeDep::GetCalibTempCoeffInfo() 
 {
   // pick up Preprocessor output, based on fRun (most recent version)
-  AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/MapAPD", fRun);
+  AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/TempCoeff", fRun);
   // stored object should be a TTree; read the info
   if (entry) {
-    fCalibMapAPD = (AliEMCALCalibMapAPD *) entry->GetObject();
+    fCalibTempCoeff = (AliEMCALCalibTempCoeff *) entry->GetObject();
   }
 
-  if (fCalibMapAPD) { 
-    AliInfo( Form("CalibMapAPD: NSuperModule %d ", fCalibMapAPD->GetNSuperModule() ) );
+  if (fCalibTempCoeff) { 
+    AliInfo( Form("CalibTempCoeff: NSuperModule %d ", fCalibTempCoeff->GetNSuperModule() ) );
   }
   else {
-    AliWarning( Form("AliEMCALCalibMapAPD not found!") );
+    AliWarning( Form("AliEMCALCalibTempCoeff not found!") );
   }
   
   return;
@@ -480,7 +477,7 @@ void AliEMCALCalibTimeDep::GetCalibMapAPDInfo()
 void AliEMCALCalibTimeDep::GetCalibReferenceInfo() 
 {
   // pick up Preprocessor output, based on fRun (most recent version)
-  AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/MapAPD", fRun);
+  AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/Reference", fRun);
   if (entry) {
     fCalibReference = (AliEMCALCalibReference *) entry->GetObject();
   }
@@ -507,11 +504,11 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
 
   // sanity check; same SuperModule indices for corrections as for regular calibrations
   for (int i = 0; i < nSM; i++) {
-    AliEMCALSuperModuleCalibReference * CalibReferenceData = fCalibReference->GetSuperModuleCalibReferenceNum(i);
-    AliEMCALSuperModuleCalibTimeDepCorrection * CalibTimeDepCorrectionData = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
+    AliEMCALSuperModuleCalibReference * dataCalibReference = fCalibReference->GetSuperModuleCalibReferenceNum(i);
+    AliEMCALSuperModuleCalibTimeDepCorrection * dataCalibTimeDepCorrection = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
 
-    int iSMRef = CalibReferenceData->GetSuperModuleNum();
-    int iSMCorr = CalibTimeDepCorrectionData->GetSuperModuleNum();
+    int iSMRef = dataCalibReference->GetSuperModuleNum();
+    int iSMCorr = dataCalibTimeDepCorrection->GetSuperModuleNum();
     if (iSMRef != iSMCorr) {
       AliWarning( Form("AliEMCALCalibTimeDep - SuperModule index mismatch: %d != %d", iSMRef, iSMCorr) );
       nRemaining = nSM * AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows * nBins;
@@ -528,9 +525,9 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
   // The fCalibSignal info is stored in TTrees
   // Note that the time-bins for the TTree's may not exactly match with our correction time bins 
   int timeDiff = fCalibSignal->GetStartTime() - fStartTime; // in seconds
-  // fCalibSignal time info in seconds: Hour/fkSecToHour
-  // corrected for startTime difference: Hour/fkSecToHour + timeDiff
-  // converted into a time-bin we use: (Hour + timeDiff*fkSecToHour) * fTimeBinsPerHour
+  // fCalibSignal time info in seconds: Hour/kSecToHour
+  // corrected for startTime difference: Hour/kSecToHour + timeDiff
+  // converted into a time-bin we use: (Hour + timeDiff*kSecToHour) * fTimeBinsPerHour
 
   // values for R(T), size of TArray = nBins
   // the [2] dimension below is for the low or high gain 
@@ -570,45 +567,45 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
   }
 
   // OK, now loop over the TTrees and fill the arrays needed for R(T)
-  TTree *TAvg = fCalibSignal->GetTreeAvgAmpVsTime();
-  TTree *TLEDRefAvg = fCalibSignal->GetTreeAvgAmpVsTime();
+  TTree *treeAvg = fCalibSignal->GetTreeAvgAmpVsTime();
+  TTree *treeLEDRefAvg = fCalibSignal->GetTreeAvgAmpVsTime();
 
-  int ChannelNum; // for regular towers
-  int RefNum; // for LED
-  double Hour;
-  double AvgAmp;
+  int iChannelNum = 0; // for regular towers
+  int iRefNum = 0; // for LED
+  double dHour = 0;
+  double dAvgAmp = 0;
 
-  TAvg->SetBranchAddress("fChannelNum", &ChannelNum);
-  TAvg->SetBranchAddress("fHour", &Hour);
-  TAvg->SetBranchAddress("fAvgAmp",&AvgAmp);
+  treeAvg->SetBranchAddress("fChannelNum", &iChannelNum);
+  treeAvg->SetBranchAddress("fHour", &dHour);
+  treeAvg->SetBranchAddress("fAvgAmp",&dAvgAmp);
 
   int iBin = 0;
   // counters for how many values were seen per SuperModule
   int nCount[AliEMCALGeoParams::fgkEMCALModules] = {0};
   int nCountLEDRef[AliEMCALGeoParams::fgkEMCALModules] = {0};
 
-  for (int ient=0; ient<TAvg->GetEntries(); ient++) {
-    TAvg->GetEntry(ient);
+  for (int ient=0; ient<treeAvg->GetEntries(); ient++) {
+    treeAvg->GetEntry(ient);
     // figure out where this info comes from
-    fCalibSignal->DecodeChannelNum(ChannelNum, &iSM, &iCol, &iRow, &iGain);
-    iBin = (int) ( (Hour + timeDiff*fkSecToHour) * fTimeBinsPerHour  ); // CHECK!!!
+    fCalibSignal->DecodeChannelNum(iChannelNum, &iSM, &iCol, &iRow, &iGain);
+    iBin = (int) ( (dHour + timeDiff*kSecToHour) * fTimeBinsPerHour  ); // CHECK!!!
     // add value in the arrays
-    ampT[iSM][iCol][iRow][iGain].AddAt( ampT[iSM][iCol][iRow][iGain].At(iBin)+AvgAmp, iBin );
+    ampT[iSM][iCol][iRow][iGain].AddAt( ampT[iSM][iCol][iRow][iGain].At(iBin)+dAvgAmp, iBin );
     nT[iSM][iCol][iRow][iGain].AddAt( nT[iSM][iCol][iRow][iGain].At(iBin)+1, iBin );
     nCount[iSM]++;
   }
 
-  TLEDRefAvg->SetBranchAddress("fRefNum", &RefNum);
-  TLEDRefAvg->SetBranchAddress("fHour", &Hour);
-  TLEDRefAvg->SetBranchAddress("fAvgAmp",&AvgAmp);
+  treeLEDRefAvg->SetBranchAddress("fRefNum", &iRefNum);
+  treeLEDRefAvg->SetBranchAddress("fHour", &dHour);
+  treeLEDRefAvg->SetBranchAddress("fAvgAmp",&dAvgAmp);
 
-  for (int ient=0; ient<TLEDRefAvg->GetEntries(); ient++) {
-    TLEDRefAvg->GetEntry(ient);
+  for (int ient=0; ient<treeLEDRefAvg->GetEntries(); ient++) {
+    treeLEDRefAvg->GetEntry(ient);
     // figure out where this info comes from
-    fCalibSignal->DecodeRefNum(RefNum, &iSM, &iStrip, &iGain);
-    iBin = (int) ( (Hour + timeDiff*fkSecToHour) * fTimeBinsPerHour  ); // CHECK!!!
+    fCalibSignal->DecodeRefNum(iRefNum, &iSM, &iStrip, &iGain);
+    iBin = (int) ( (dHour + timeDiff*kSecToHour) * fTimeBinsPerHour  ); // CHECK!!!
     // add value in the arrays
-    ampLEDRefT[iSM][iStrip][iGain].AddAt( ampLEDRefT[iSM][iStrip][iGain].At(iBin)+AvgAmp, iBin );
+    ampLEDRefT[iSM][iStrip][iGain].AddAt( ampLEDRefT[iSM][iStrip][iGain].At(iBin)+dAvgAmp, iBin );
     nLEDRefT[iSM][iStrip][iGain].AddAt( nLEDRefT[iSM][iStrip][iGain].At(iBin)+1, iBin );
     nCountLEDRef[iSM]++;
   }
@@ -648,34 +645,34 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
 
 
   // Calculate correction values, and store them
-  // set fkErrorCode values for those that could not be set
+  // set kErrorCode values for those that could not be set
 
-  Float_t Rt0 = 0;
-  Float_t RT = 0;
+  Float_t ratiot0 = 0;
+  Float_t ratioT = 0;
   Float_t correction = 0; // c(T) = R(t0)/R(T)
   Int_t refGain = 0; // typically use low gain for LED reference amplitude (high gain typically well beyond saturation)
 
   for (int i = 0; i < nSM; i++) {
-    AliEMCALSuperModuleCalibReference * CalibReferenceData = fCalibReference->GetSuperModuleCalibReferenceNum(i);
-    AliEMCALSuperModuleCalibTimeDepCorrection * CalibTimeDepCorrectionData = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
-    iSM = CalibReferenceData->GetSuperModuleNum();
+    AliEMCALSuperModuleCalibReference * dataCalibReference = fCalibReference->GetSuperModuleCalibReferenceNum(i);
+    AliEMCALSuperModuleCalibTimeDepCorrection * dataCalibTimeDepCorrection = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
+    iSM = dataCalibReference->GetSuperModuleNum();
 
     for (iCol = 0; iCol < AliEMCALGeoParams::fgkEMCALCols; iCol++) {
       //      iStrip = AliEMCALGeoParams::GetStripModule(iSM, iCol);
       iStrip = (iSM%2==0) ? iCol/2 : AliEMCALGeoParams::fgkEMCALLEDRefs - 1 - iCol/2; //TMP, FIXME
-       refGain = CalibReferenceData->GetLEDRefHighLow(iStrip); // LED reference gain value used for reference calibration      
+       refGain = dataCalibReference->GetLEDRefHighLow(iStrip); // LED reference gain value used for reference calibration      
 
       for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
 
        // Calc. R(t0):
-       AliEMCALCalibReferenceVal * refVal = CalibReferenceData->GetAPDVal(iCol, iRow);
+       AliEMCALCalibReferenceVal * refVal = dataCalibReference->GetAPDVal(iCol, iRow);
        iGain = refVal->GetHighLow(); // gain value used for reference calibration      
        // valid amplitude values should be larger than 0
-       if (refVal->GetLEDAmp()>0 && CalibReferenceData->GetLEDRefAmp(iStrip)>0) {
-         Rt0 =  refVal->GetLEDAmp() / CalibReferenceData->GetLEDRefAmp(iStrip);
+       if (refVal->GetLEDAmp()>0 && dataCalibReference->GetLEDRefAmp(iStrip)>0) {
+         ratiot0 =  refVal->GetLEDAmp() / dataCalibReference->GetLEDRefAmp(iStrip);
        }
        else {
-         Rt0 = fkErrorCode;
+         ratiot0 = kErrorCode;
        }
 
        // Calc. R(T)
@@ -685,7 +682,7 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
          // same gain as for reference calibration is the default
          if (ampT[iSM][iCol][iRow][iGain].At(j)>0 && ampLEDRefT[iSM][iStrip][refGain].At(j)>0) {
            // looks like valid data with the right gain combination
-           RT = ampT[iSM][iCol][iRow][iGain].At(j) / ampLEDRefT[iSM][iStrip][refGain].At(j); 
+           ratioT = ampT[iSM][iCol][iRow][iGain].At(j) / ampLEDRefT[iSM][iStrip][refGain].At(j); 
 
            // if data appears to be saturated, and we are in high gain, then try with low gain instead
            int newGain = iGain;
@@ -701,31 +698,31 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrection(Int_t nSM, Int_t nBins)
              // compensate for using different gain than in the reference calibration
              // we may need to have a custom H/L ratio value for each tower
              // later, but for now just use a common value, as the rest of the code does.. 
-             RT = ampT[iSM][iCol][iRow][newGain].At(j) / ampLEDRefT[iSM][iStrip][newRefGain].At(j); 
+             ratioT = ampT[iSM][iCol][iRow][newGain].At(j) / ampLEDRefT[iSM][iStrip][newRefGain].At(j); 
 
              if (newGain<iGain) {
-               RT *= fHighLowGainFactor;
+               ratioT *= fHighLowGainFactor;
              }
              else if (newRefGain<refGain) {
-               RT /= fHighLowGainFactor;
+               ratioT /= fHighLowGainFactor;
              }
            }
          }
          else {
-           RT = fkErrorCode;
+           ratioT = kErrorCode;
          }
 
          // Calc. correction factor
-         if (Rt0>0 && RT>0) { 
-           correction = Rt0/RT;
+         if (ratiot0>0 && ratioT>0) { 
+           correction = ratiot0/ratioT;
          }
          else {
-           correction = fkErrorCode;
+           correction = kErrorCode;
            nRemaining++;
          }
 
          // Store the value
-         CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->AddAt(correction, j);
+         dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->AddAt(correction, j);
          /* Check that
          fTimeDepCorrection->SetCorrection(i, iCol, iRow, j, correction);
          also works OK */
@@ -747,7 +744,7 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrectionStripBasis(Int_t nSM, Int_t nBins)
 
   // for calculating StripAverage info
   int nValidTower = 0;
-  Float_t StripAverage = 0;
+  Float_t stripAverage = 0;
   Float_t val = 0;
 
   int iSM = 0;
@@ -758,13 +755,13 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrectionStripBasis(Int_t nSM, Int_t nBins)
   int lastCol = 0;
 
   for (int i = 0; i < nSM; i++) {
-    AliEMCALSuperModuleCalibTimeDepCorrection * CalibTimeDepCorrectionData = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
-    iSM = CalibTimeDepCorrectionData->GetSuperModuleNum();
+    AliEMCALSuperModuleCalibTimeDepCorrection * dataCalibTimeDepCorrection = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
+    iSM = dataCalibTimeDepCorrection->GetSuperModuleNum();
 
     for (int j = 0; j < nBins; j++) {
 
       nValidTower = 0;
-      StripAverage = 0;
+      stripAverage = 0;
 
       for (iStrip = 0; iStrip < AliEMCALGeoParams::fgkEMCALLEDRefs; iStrip++) {
        firstCol = iStrip*2;
@@ -775,9 +772,9 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrectionStripBasis(Int_t nSM, Int_t nBins)
 
        for (iCol = firstCol; iCol <= lastCol; iCol++) {
          for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
-           val = CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->At(j);
+           val = dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->At(j);
            if (val>0) { // valid value; error code is negative
-             StripAverage += val;
+             stripAverage += val;
              nValidTower++;
            } 
          }
@@ -785,12 +782,12 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrectionStripBasis(Int_t nSM, Int_t nBins)
 
        // calc average over strip
        if (nValidTower>0) {
-         StripAverage /= nValidTower;
+         stripAverage /= nValidTower;
          for (iCol = firstCol; iCol <= lastCol; iCol++) {
            for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
-             val = CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->At(j);
+             val = dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->At(j);
              if (val<0) { // invalid value; error code is negative
-               CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->AddAt(val, j);
+               dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->AddAt(val, j);
              }
            }
          }
@@ -807,7 +804,7 @@ Int_t AliEMCALCalibTimeDep::CalcLEDCorrectionStripBasis(Int_t nSM, Int_t nBins)
 }
 
 //________________________________________________________________
-Int_t AliEMCALCalibTimeDep::CalcTemperatureCorrection(Int_t nSM, Int_t nBins) 
+Int_t AliEMCALCalibTimeDep::CalcTemperatureCorrection(Int_t nSM, Int_t nBins, Int_t binSize
 { // OK, so we didn't have valid LED data that allowed us to do the correction only 
   // with that info.
   // So, instead we'll rely on the temperature info and try to do the correction
@@ -819,61 +816,84 @@ Int_t AliEMCALCalibTimeDep::CalcTemperatureCorrection(Int_t nSM, Int_t nBins)
   int iCol = 0;
   int iRow = 0;
 
-  Double_t TempCoeff[AliEMCALGeoParams::fgkEMCALCols][AliEMCALGeoParams::fgkEMCALRows];
-  memset(TempCoeff, 0, sizeof(TempCoeff));
-  Float_t MGain = 0; 
+  Double_t dTempCoeff[AliEMCALGeoParams::fgkEMCALCols][AliEMCALGeoParams::fgkEMCALRows];
+  memset(dTempCoeff, 0, sizeof(dTempCoeff));
   Double_t correction = 0;
-  Double_t secondsPerBin = (3600/fTimeBinsPerHour);
+  Double_t secondsPerBin = (Double_t) binSize;
 
   for (int i = 0; i < nSM; i++) {
-    AliEMCALSuperModuleCalibTimeDepCorrection * CalibTimeDepCorrectionData = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(iSM);
-    iSM = CalibTimeDepCorrectionData->GetSuperModuleNum();
+    AliEMCALSuperModuleCalibTimeDepCorrection * dataCalibTimeDepCorrection = fCalibTimeDepCorrection->GetSuperModuleCalibTimeDepCorrectionNum(i);
+    iSM = dataCalibTimeDepCorrection->GetSuperModuleNum();
 
-    AliEMCALSuperModuleCalibReference * CalibReferenceData = fCalibReference->GetSuperModuleCalibReferenceNum(iSM);
-    AliEMCALSuperModuleCalibMapAPD * CalibMapAPDData = fCalibMapAPD->GetSuperModuleCalibMapAPDNum(iSM);
-    AliEMCALSuperModuleBiasAPD * BiasAPDData = fBiasAPD->GetSuperModuleBiasAPDNum(iSM);
-    
-    // first calculate the M=Gain values, and TemperatureCoeff, for all towers in this SuperModule, from BiasAPD and CalibMapAPD info
+    AliEMCALSuperModuleCalibReference * dataCalibReference = fCalibReference->GetSuperModuleCalibReferenceNum(iSM);
+    AliEMCALSuperModuleCalibTempCoeff * dataCalibTempCoeff = fCalibTempCoeff->GetSuperModuleCalibTempCoeffNum(iSM);
+
+    // first get CalibTempCoeff info
     for (iCol = 0; iCol < AliEMCALGeoParams::fgkEMCALCols; iCol++) {
       for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
-       AliEMCALCalibMapAPDVal * mapAPD = CalibMapAPDData->GetAPDVal(iCol, iRow);
-       MGain =  fCalibMapAPD->GetGain(mapAPD->GetPar(0), mapAPD->GetPar(1), mapAPD->GetPar(2), 
-                                      BiasAPDData->GetVoltage(iCol, iRow));
-       TempCoeff[iCol][iRow] = GetTempCoeff(mapAPD->GetDarkCurrent(), MGain);
+
+       dTempCoeff[iCol][iRow] = dataCalibTempCoeff->GetTC(iCol, iRow);
+       if (fVerbosity > 1) {
+         cout << " iSM " << iSM << " iCol " << iCol << " iRow " << iRow 
+              << " dTempCoeff " << dTempCoeff[iCol][iRow] << endl; 
+       }
       }
     }
     
     // figure out what the reference temperature is, from fCalibReference
-    Double_t ReferenceTemperature = 0;
+    Double_t referenceTemperature = 0;
     int nVal = 0;
     for (int iSensor = 0; iSensor<AliEMCALGeoParams::fgkEMCALTempSensors ; iSensor++) {
-      if (CalibReferenceData->GetTemperature(iSensor)>0) { // hopefully OK value
-       ReferenceTemperature += CalibReferenceData->GetTemperature(iSensor);
+      if (dataCalibReference->GetTemperature(iSensor)>0) { // hopefully OK value
+       referenceTemperature += dataCalibReference->GetTemperature(iSensor);
        nVal++;
       }
     }
     
     if (nVal>0) {
-      ReferenceTemperature /= nVal; // valid values exist, we can look into corrections
+      referenceTemperature /= nVal; // valid values exist, we can look into corrections
       
       for (int j = 0; j < nBins; j++) {
-
        // what is the timestamp in the middle of this bin? (0.5 is for middle of bin)
        UInt_t timeStamp = fStartTime + (UInt_t)((j+0.5)*secondsPerBin);
       // get the temperature at this time; use average over whole SM for now (TO BE CHECKED LATER - if we can do better with finer grained info)
-       Double_t SMTemperature = GetTemperatureSM(iSM, timeStamp); 
-
-       Double_t TemperatureDiff = ReferenceTemperature - SMTemperature; // old vs new
-       // if the new temperature is higher than the old/reference one, then the gain has gone down 
-       if (fabs(TemperatureDiff)>fTemperatureResolution) { 
-         // significant enough difference that we need to consider it
+       Double_t dSMTemperature = GetTemperatureSM(iSM, timeStamp); 
+
+       Double_t temperatureDiff = referenceTemperature - dSMTemperature; // ref - new
+       if (fVerbosity > 0) {
+         cout << " referenceTemperature " << referenceTemperature
+              << " dSMTemperature " << dSMTemperature
+              << " temperatureDiff " << temperatureDiff
+              << endl;
+       }
+       // if the new temperature is higher than the old/reference one (diff<0), then the gain has gone down 
+       // if the new temperature is lower than the old/reference one (diff>0), then the gain has gone up
+       // dTempCoeff is a (unsigned) factor describing how many % the gain 
+       // changes with a degree change.  
+       // i.e. the product temperatureDiff * dTempCoeff increase when the gain goes up
+       // The correction we want to keep is what we should multiply our ADC value with as a function
+       // of time, i.e. the inverse of the gain change..
+       if ( (TMath::Abs(temperatureDiff)>fTemperatureResolution)
+            && (TMath::Abs(temperatureDiff)<fMaxTemperatureDiff) ) {
+         // significant enough difference that we need to consider it, and also not unreasonably large
 
          // loop over all towers; effect of temperature change will depend on gain for this tower
          for (iCol = 0; iCol < AliEMCALGeoParams::fgkEMCALCols; iCol++) {
            for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
 
-             correction = TemperatureDiff * TempCoeff[iCol][iRow];
-             CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->AddAt(correction, j);
+             // the correction should be inverse of modification in gain: (see discussion above)
+             // modification in gain: 1.0 + (temperatureDiff * dTempCoeff[iCol][iRow])*0.01; 
+             // 1/(1+x) ~= 1 - x for small x, i.e. we arrive at:
+             correction = 1.0 - (temperatureDiff * dTempCoeff[iCol][iRow]); 
+             dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->AddAt(correction, j);
+             if (fVerbosity > 1) {
+               cout << " iSM " << iSM
+                    << " iCol  " << iCol 
+                    << " iRow  " << iRow 
+                    << " j  " << j 
+                    << " correction  " << correction 
+                    << endl;
+             }
            }
          }
 
@@ -882,7 +902,7 @@ Int_t AliEMCALCalibTimeDep::CalcTemperatureCorrection(Int_t nSM, Int_t nBins)
          correction = 1;
          for (iCol = 0; iCol < AliEMCALGeoParams::fgkEMCALCols; iCol++) {
            for (iRow = 0; iRow < AliEMCALGeoParams::fgkEMCALRows; iRow++) {
-             CalibTimeDepCorrectionData->GetCorrection(iCol,iRow)->AddAt(correction, j);
+             dataCalibTimeDepCorrection->GetCorrection(iCol,iRow)->AddAt(correction, j);
            }
          }
        } // else