]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFcalib.cxx
Added code in preprocessor to handle output from new laser drift velocity DA
[u/mrichter/AliRoot.git] / TOF / AliTOFcalib.cxx
index 422483352fe21c9971fdab7c645df69db57df10d..18bf558ead4e233b5c18fb20e02921e049afdde0 100644 (file)
@@ -116,7 +116,10 @@ author: Chiara Zampolli, zampolli@bo.infn.it
 #include "AliTOFDeltaBCOffset.h"
 #include "AliTOFCTPLatency.h"
 #include "AliTOFT0Fill.h"
-
+#include "AliTOFRunParams.h"
+#include "AliTOFResponseParams.h"
+#include "AliESDEvent.h"
+#include "AliESDtrack.h"
 
 class TROOT;
 class TStyle;
@@ -147,7 +150,13 @@ AliTOFcalib::AliTOFcalib():
   fConfigMap(new TMap),
   fDeltaBCOffset(NULL),
   fCTPLatency(NULL),
-  fT0Fill(NULL)
+  fT0Fill(NULL),
+  fRunParams(NULL),
+  fResponseParams(NULL),
+  fInitFlag(kFALSE),
+  fRemoveMeanT0(kTRUE),
+  fCalibrateTOFsignal(kTRUE),
+  fCorrectTExp(kFALSE)
 { 
   //TOF Calibration Class ctor
   fNChannels = AliTOFGeometry::NSectors()*(2*(AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB())+AliTOFGeometry::NStripA())*AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX();
@@ -174,8 +183,26 @@ AliTOFcalib::AliTOFcalib(const AliTOFcalib & calib):
   fConfigMap(calib.fConfigMap),
   fDeltaBCOffset(NULL),
   fCTPLatency(NULL),
-  fT0Fill(NULL)
+  fT0Fill(NULL),
+  fRunParams(NULL),
+  fResponseParams(NULL),
+  fInitFlag(calib.fInitFlag),
+  fRemoveMeanT0(calib.fRemoveMeanT0),
+  fCalibrateTOFsignal(calib.fCalibrateTOFsignal),
+  fCorrectTExp(calib.fCorrectTExp)
 {
+
+  fTOFCalOnline = new TObjArray(fNChannels);
+  fTOFCalOnlinePulser = new TObjArray(fNChannels);
+  fTOFCalOnlineNoise = new TObjArray(fNChannels);
+  fTOFCalOnlineHW = new TObjArray(fNChannels);
+  fTOFCalOffline = new TObjArray(fNChannels);
+  fTOFCalOnline->SetOwner();
+  fTOFCalOnlinePulser->SetOwner();
+  fTOFCalOnlineNoise->SetOwner();
+  fTOFCalOnlineHW->SetOwner();
+  fTOFCalOffline->SetOwner();
+
   //TOF Calibration Class copy ctor
   for (Int_t iarray = 0; iarray<fNChannels; iarray++){
     AliTOFChannelOnline * calChOnline = (AliTOFChannelOnline*)calib.fTOFCalOnline->At(iarray);
@@ -193,6 +220,8 @@ AliTOFcalib::AliTOFcalib(const AliTOFcalib & calib):
   if (calib.fDeltaBCOffset) fDeltaBCOffset = new AliTOFDeltaBCOffset(*calib.fDeltaBCOffset);
   if (calib.fCTPLatency) fCTPLatency = new AliTOFCTPLatency(*calib.fCTPLatency);
   if (calib.fT0Fill) fT0Fill = new AliTOFT0Fill(*calib.fT0Fill);
+  if (calib.fRunParams) fRunParams = new AliTOFRunParams(*calib.fRunParams);
+  if (calib.fResponseParams) fResponseParams = new AliTOFResponseParams(*calib.fResponseParams);
 }
 
 //____________________________________________________________________________ 
@@ -242,6 +271,18 @@ AliTOFcalib& AliTOFcalib::operator=(const AliTOFcalib &calib)
     if (fT0Fill) *fT0Fill = *calib.fT0Fill;
     else fT0Fill = new AliTOFT0Fill(*calib.fT0Fill);
   }
+  if (calib.fRunParams) {
+    if (fRunParams) *fRunParams = *calib.fRunParams;
+    else fRunParams = new AliTOFRunParams(*calib.fRunParams);
+  }
+  if (calib.fResponseParams) {
+    if (fResponseParams) *fResponseParams = *calib.fResponseParams;
+    else fResponseParams = new AliTOFResponseParams(*calib.fResponseParams);
+  }
+  fInitFlag = calib.fInitFlag;
+  fRemoveMeanT0 = calib.fRemoveMeanT0;
+  fCalibrateTOFsignal = calib.fCalibrateTOFsignal;
+  fCorrectTExp = calib.fCorrectTExp;
 
   return *this;
 }
@@ -279,6 +320,7 @@ AliTOFcalib::~AliTOFcalib()
     if (fDeltaBCOffset) delete fDeltaBCOffset;
     if (fCTPLatency) delete fCTPLatency;
     if (fT0Fill) delete fT0Fill;
+    if (fRunParams) delete fRunParams;
   }
   if (fTree!=0x0) delete fTree;
   if (fChain!=0x0) delete fChain;
@@ -336,8 +378,7 @@ void AliTOFcalib::WriteConfigMapOnCDB(const Char_t *sel, Int_t minrun, Int_t max
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Config" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF configuration map for online calib on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -355,8 +396,7 @@ void AliTOFcalib::WriteConfigMapOnCDB(const Char_t *sel)
   //Write calibration parameters to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Config" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF config map for online calib on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -376,8 +416,7 @@ void AliTOFcalib::WriteParOnlineDelayOnCDB(const Char_t *sel, Int_t minrun, Int_
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnlineDelay" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -396,8 +435,7 @@ void AliTOFcalib::WriteParOnlineStatusOnCDB(const Char_t *sel, Int_t minrun, Int
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Status" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online status calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -415,8 +453,7 @@ void AliTOFcalib::WriteParOnlineDelayOnCDB(const Char_t *sel)
   //Write calibration parameters to the CDB with infinite validity -------> new calib objs!!!!!
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnlineDelay" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -435,8 +472,7 @@ void AliTOFcalib::WriteParOnlineStatusOnCDB(const Char_t *sel)
   //Write calibration parameters to the CDB with infinite validity -------> new calib objs!!!!!
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Status" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online status calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -456,8 +492,7 @@ void AliTOFcalib::WriteParOnlineOnCDB(const Char_t *sel, Int_t minrun, Int_t max
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnline" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -476,8 +511,7 @@ void AliTOFcalib::WriteParOnlinePulserOnCDB(const Char_t *sel, Int_t minrun, Int
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Pulser" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online calib obj from pulser on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -496,8 +530,7 @@ void AliTOFcalib::WriteParOnlineNoiseOnCDB(const Char_t *sel, Int_t minrun, Int_
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Noise" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online calib obj from noise on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -516,8 +549,7 @@ void AliTOFcalib::WriteParOnlineHWOnCDB(const Char_t *sel, Int_t minrun, Int_t m
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "HW" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF online calib obj from hardware on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -535,8 +567,7 @@ void AliTOFcalib::WriteParOnlineOnCDB(const Char_t *sel)
   //Write calibration parameters to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnline" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -555,8 +586,7 @@ void AliTOFcalib::WriteParOnlinePulserOnCDB(const Char_t *sel)
   //Write calibration parameters from pulser to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Pulser" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online calib obj from pulser on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -575,8 +605,7 @@ void AliTOFcalib::WriteParOnlineNoiseOnCDB(const Char_t *sel)
   //Write calibration parameters from noise to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Noise" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online calib obj from noise on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -595,8 +624,7 @@ void AliTOFcalib::WriteParOnlineHWOnCDB(const Char_t *sel)
   //Write calibration parameters from hardware to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "HW" ;  // to be consistent with TOFPreprocessor
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF online calib obj from harware on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -617,8 +645,7 @@ void AliTOFcalib::WriteParOfflineOnCDB(const Char_t *sel, const Char_t *validity
   SetLastRun(maxrun);
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOffline" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliDebug(2,Form("Writing TOF offline calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
   AliCDBId id(out,fFirstRun,fLastRun);
   AliCDBMetaData *md = new AliCDBMetaData();
@@ -634,8 +661,7 @@ void AliTOFcalib::WriteParOfflineOnCDB(const Char_t *sel, const Char_t *validity
   //Write calibration parameters to the CDB with infinite validity
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOffline" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBRunRange runrange(fFirstRun,fLastRun);
   AliDebug(2,Form("Writing TOF offline calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
   AliCDBId id(out,runrange);
@@ -652,8 +678,7 @@ Bool_t AliTOFcalib::ReadConfigMapFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Config" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (ConfigMap) found!!!");
@@ -676,8 +701,7 @@ Bool_t AliTOFcalib::ReadParOnlineDelayFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from the CDB -------> new calib objs!!!!!
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnlineDelay" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (ParOnlineDelay) found!!!");
@@ -700,8 +724,7 @@ Bool_t AliTOFcalib::ReadParOnlineStatusFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from the CDB -------> new calib objs!!!!!
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Status" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (Status) found!!!");
@@ -724,8 +747,7 @@ Bool_t AliTOFcalib::ReadParOnlineFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOnline" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (ParOnline) found!!!");
@@ -748,8 +770,7 @@ Bool_t AliTOFcalib::ReadParOnlinePulserFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from pulser from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Pulser" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (Pulser) found!!!");
@@ -772,8 +793,7 @@ Bool_t AliTOFcalib::ReadParOnlineNoiseFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from noise from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "Noise" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (Noise) found!!!");
@@ -796,8 +816,7 @@ Bool_t AliTOFcalib::ReadParOnlineHWFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from hardware from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "HW" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (HW map) found!!!");
@@ -820,8 +839,7 @@ Bool_t AliTOFcalib::ReadParOfflineFromCDB(const Char_t *sel, Int_t nrun)
   //Read calibration parameters from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "ParOffline" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (ParOffline) found!!!");
@@ -845,8 +863,7 @@ void AliTOFcalib::WriteSimHistoOnCDB(const Char_t *sel, Int_t minrun, Int_t maxr
   fTOFSimToT=histo;
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "SimHisto" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBMetaData *mdhisto = new AliCDBMetaData();
   mdhisto->SetResponsible("Chiara Zampolli");
   AliCDBId id(out,minrun,maxrun);
@@ -862,8 +879,7 @@ Bool_t AliTOFcalib::ReadSimHistoFromCDB(const Char_t *sel, Int_t nrun)
   // The Tot Histo
 
   const Char_t *sel1 = "SimHisto" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (SimHisto) found!!!");
@@ -885,8 +901,7 @@ void AliTOFcalib::WriteRecParOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun
   AliCDBMetaData *md = new AliCDBMetaData();
   md->SetResponsible("Silvia Arcelli");
   const Char_t *sel1 = "RecoParam" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBId id(out,minrun,maxrun);
   man->Put(param,id,md);
   delete md;
@@ -897,8 +912,7 @@ AliTOFRecoParam * AliTOFcalib::ReadRecParFromCDB(const Char_t *sel, Int_t nrun)
   //Read reconstruction parameters from the CDB
   AliCDBManager *man = AliCDBManager::Instance();
   const Char_t *sel1 = "RecoParam" ;
-  Char_t  out[100];
-  sprintf(out,"%s/%s",sel,sel1); 
+  TString out(Form("%s/%s",sel,sel1));
   AliCDBEntry *entry = man->Get(out,nrun);
   if (!entry) { 
     AliFatal("Exiting, no CDB object (RecoParam) found!!!");
@@ -934,12 +948,10 @@ void AliTOFcalib::CreateTreeFromCDB(Int_t minrun, Int_t maxrun){
       AliInfo(Form("No entry found for run %i",irun));
     }
     else{
-      TTree *tree = new TTree();
-      tree = (TTree*)entry->GetObject();
+      TTree *tree = (TTree*)entry->GetObject();
       tree->SetBranchAddress("nentries",&nentries);
       tree->SetBranchAddress("TOFentries",p);      
       fTree->CopyEntries(tree);
-      delete tree;
       fNruns++;
     }
   }
@@ -961,10 +973,10 @@ void AliTOFcalib::CreateTreeFromGrid(Int_t minrun, Int_t maxrun){
   AliInfo("connected to alien");
   TGrid::Connect("alien://");
   
-  Char_t filename[100];
+  TString filename;
   for (Int_t irun = minrun;irun<=maxrun;irun++){
-    sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
-    TFile *filegrid = TFile::Open(filename,"READ");
+    filename = Form("alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
+    TFile *filegrid = TFile::Open(filename.Data(),"READ");
     TTree *tree = (TTree*)filegrid->Get("T");
     tree->SetBranchAddress("nentries",&nentries);
     tree->SetBranchAddress("TOFentries",p);      
@@ -988,10 +1000,10 @@ void AliTOFcalib::CreateTreeFromFile(Int_t minrun, Int_t maxrun){
   fTree->SetDirectory(0);
   fTree->Branch("nentries",&nentries,"nentries/I");
   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
-  Char_t filename[100];
+  TString filename;
   for (Int_t irun = minrun;irun<=maxrun;irun++){
-    sprintf(filename,"$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
-    TFile *file = new TFile(filename,"READ");
+    filename = Form("$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
+    TFile *file = new TFile(filename.Data(),"READ");
     TTree *tree = (TTree*)file->Get("T");
     tree->SetBranchAddress("nentries",&nentries);
     tree->SetBranchAddress("TOFentries",p);      
@@ -1015,10 +1027,10 @@ void AliTOFcalib::CreateChainFromGrid(Int_t minrun, Int_t maxrun){
   AliInfo("connected to alien");
   TGrid::Connect("alien://");
   
-  Char_t filename[100];
+  TString filename;
   for (Int_t irun = minrun;irun<=maxrun;irun++){
-    sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
-    fChain->Add(filename);
+    filename = Form("alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
+    fChain->Add(filename.Data());
     fNruns++;    
   }
   
@@ -1376,7 +1388,7 @@ Int_t AliTOFcalib::Calibrate(Int_t nch, Int_t *ch, Option_t *optionSave, Option_
       ntracksTotal+=nentries/3;
     }
     if (ntracksTotal < MEANENTRIES) {
-      AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %f), not calibrating channel and continuing.....",i,ntracksTotal));
+      AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",i,ntracksTotal));
       continue;
     }
   
@@ -1492,12 +1504,11 @@ Int_t AliTOFcalib::CalibrateFromProfile(Int_t ich, Option_t *optionSave, Option_
   }
 
   if (ntracksTotal < MEANENTRIES) {  
-    AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
+    AliInfo(Form(" Too small mean number of entires per channel (mean number = %d) not calibrating and exiting.....",ntracksTotal));
     return 2;
   }
 
-  TH1F * hProf = new TH1F();
-  hProf = Profile(ich);
+  TH1F * hProf = Profile(ich);
   hProf->Fit("pol5",optionFit,"",0,4);
   TF1 * calibfunc = (TF1*)hProf->GetFunction("pol5");
   Float_t par[6];    
@@ -1591,7 +1602,7 @@ Int_t AliTOFcalib::Calibrate(Option_t *optionSave, Option_t *optionFit){
       ntracksTotal+=nentries/3;
     }
     if (ntracksTotal < MEANENTRIES) {
-      AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %f), not calibrating channel and continuing.....",ii,ntracksTotal));
+      AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",ii,ntracksTotal));
       continue;
     }
     Float_t meantime=0;
@@ -1879,6 +1890,22 @@ AliTOFcalib::CreateT0Fill()
   
 //----------------------------------------------------------------------------
 
+void
+AliTOFcalib::CreateRunParams()
+{
+  /*
+   * create run params
+   */
+
+  if (fRunParams) {
+    AliWarning("RunParams object already defined, cannot create a new one");
+    return;
+  }
+  fRunParams = new AliTOFRunParams();
+}
+  
+//----------------------------------------------------------------------------
+
 void
 AliTOFcalib::WriteDeltaBCOffsetOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
 {
@@ -1936,6 +1963,25 @@ AliTOFcalib::WriteT0FillOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
 
 //----------------------------------------------------------------------------
 
+void
+AliTOFcalib::WriteRunParamsOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+  /*
+   * write run params on CDB 
+   */
+  
+  if (!fRunParams) return;
+  AliCDBId id(Form("%s/RunParams", sel), minrun, maxrun);
+  AliCDBMetaData *md = new AliCDBMetaData();
+  md->SetResponsible("Roberto Preghenella");
+  AliCDBManager *man = AliCDBManager::Instance();
+  man->Put(fRunParams, id, md);
+  AliDebug(2,Form("RunParams written on CDB with run range [%i, %i] ",minrun ,maxrun));
+  delete md;
+}
+
+//----------------------------------------------------------------------------
+
 Bool_t
 AliTOFcalib::ReadDeltaBCOffsetFromCDB(const Char_t *sel , Int_t nrun)
 {
@@ -2003,3 +2049,214 @@ AliTOFcalib::ReadT0FillFromCDB(const Char_t *sel , Int_t nrun)
   return kTRUE; 
 }
 
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadRunParamsFromCDB(const Char_t *sel , Int_t nrun)
+{
+  /*
+   * read run params from CDB
+   */
+  
+  AliCDBManager *man = AliCDBManager::Instance();
+  AliCDBEntry *entry = man->Get(Form("%s/RunParams", sel),nrun);
+  if (!entry) { 
+    AliFatal("No RunParams entry found in CDB");
+    exit(0);  
+  }
+  fRunParams =(AliTOFRunParams *)entry->GetObject();
+  if(!fRunParams){
+    AliFatal("No RunParams object found in CDB entry");
+    exit(0);  
+  }  
+  return kTRUE; 
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t 
+AliTOFcalib::Init(Int_t run)
+{
+  /*
+   * init
+   */
+
+  if (fInitFlag) {
+    AliWarning("the class was already initialized, re-initialize it");
+    fInitFlag = kFALSE;
+  }
+  
+  /* read channel status array */
+  if (!ReadParOnlineStatusFromCDB("TOF/Calib", run)) {
+    AliError("cannot get \"Status\" object from OCDB");
+    return kFALSE;
+  }
+  /* get par offline array */
+  if (!ReadParOfflineFromCDB("TOF/Calib", run)) {
+    AliError("cannot get \"ParOffline\" object from OCDB");
+    return kFALSE;
+  }
+  /* get deltaBC offset obj */
+  if (!ReadDeltaBCOffsetFromCDB("TOF/Calib", run)) {
+    AliError("cannot get \"DeltaBCOffset\" object from OCDB");
+    return kFALSE;
+  }
+  /* get CTP latency obj */
+  if (!ReadCTPLatencyFromCDB("TOF/Calib", run)) {
+    AliError("cannot get \"CTPLatency\" object from OCDB");
+    return kFALSE;
+  }
+  /* get run params obj */
+  if (!ReadRunParamsFromCDB("TOF/Calib", run)) {
+    AliError("cannot get \"RunParams\" object from OCDB");
+    return kFALSE;
+  }
+  /* get response params */
+  if (fCorrectTExp) {
+    TFile *responseFile = TFile::Open("$ALICE_ROOT/TOF/data/AliTOFresponsePar.root");
+    if (!responseFile || !responseFile->IsOpen()) {
+      AliError("cannot open \"ResponseParams\" local file");
+      return kFALSE;
+    }
+    fResponseParams = (AliTOFResponseParams *)responseFile->Get("ResponseParams");
+    if (!fResponseParams) {
+      AliError("cannot get \"ResponseParams\" object from local file");
+      return kFALSE;
+    }
+    responseFile->Close();
+  }
+
+  /* all done */
+  fInitFlag = kTRUE;
+  return kTRUE;
+
+}
+
+//----------------------------------------------------------------------------
+
+Double_t
+AliTOFcalib::GetTimeCorrection(Int_t index, Double_t tot, Int_t deltaBC, Int_t l0l1, UInt_t timestamp)
+{
+  /*
+   * get time correction
+   */
+
+  if (!fInitFlag) {
+    AliError("class not yet initialized. Initialize it before.");
+    return 0.;
+  }
+
+  /* get calibration params */
+  AliTOFChannelOffline *parOffline = (AliTOFChannelOffline *)fTOFCalOffline->At(index);
+  Int_t deltaBCOffset = fDeltaBCOffset->GetDeltaBCOffset();
+  Float_t ctpLatency = fCTPLatency->GetCTPLatency();
+  Float_t tdcLatencyWindow = fStatus->GetLatencyWindow(index) * 1.e3;
+  Float_t timezero = fRunParams->EvalT0(timestamp);
+  /* check whether to remove mean T0.
+   * useful when one wants to compute mean T0 */
+  if (!fRemoveMeanT0) timezero = 0.;
+
+  /* compute correction */
+  Double_t corr = 0.;
+  /* deltaBC correction */
+  deltaBC = deltaBCOffset; /* inhibit deltaBC correction for the time being */
+  corr += (deltaBC - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
+  /* L0-L1 latency correction */
+  corr -= l0l1 * AliTOFGeometry::BunchCrossingBinWidth();
+  /* CTP latency correction */
+  corr -= ctpLatency;
+  /* TDC latency window correction */
+  corr += tdcLatencyWindow;
+  /* time-zero correction */
+  corr += timezero;
+  /* time calibration correction */
+  if (tot < AliTOFGeometry::SlewTOTMin()) 
+    tot = AliTOFGeometry::SlewTOTMin();
+  if (tot > AliTOFGeometry::SlewTOTMax()) 
+    tot = AliTOFGeometry::SlewTOTMax();
+  for (Int_t islew = 0; islew < 6; islew++)
+    corr += parOffline->GetSlewPar(islew) * TMath::Power(tot, islew) * 1.e3;
+
+  /* return correction */
+  return corr;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CalibrateESD(AliESDEvent *event)
+{
+  /*
+   * calibrate ESD
+   */
+
+  if (!fInitFlag) {
+    AliError("class not yet initialized. Initialize it before.");
+    return;
+  }
+
+  /* loop over tracks */
+  AliESDtrack *track = NULL;
+  Int_t index, l0l1, deltaBC;
+  Double_t time, tot, corr, texp[AliPID::kSPECIES];
+  UInt_t timestamp = event->GetTimeStamp();
+  for (Int_t itrk = 0; itrk < event->GetNumberOfTracks(); itrk++) {
+
+    /* get track */
+    track = event->GetTrack(itrk);
+    if (!track || !(track->GetStatus() & AliESDtrack::kTOFout)) continue;
+
+    /* calibrate TOF signal */
+    if (fCalibrateTOFsignal) {
+      /* get info */
+      index = track->GetTOFCalChannel();
+      time = track->GetTOFsignalRaw();
+      tot = track->GetTOFsignalToT();
+      l0l1 = track->GetTOFL0L1();
+      deltaBC = track->GetTOFDeltaBC();
+      /* get correction */
+      corr = GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp);
+      /* apply correction */
+      time -= corr;
+      /* set new TOF signal */
+      track->SetTOFsignal(time);
+    }
+
+    /* correct expected time */
+    if (fCorrectTExp) {
+      /* get integrated times */
+      track->GetIntegratedTimes(texp);
+      /* loop over particle types and correct expected time */
+      for (Int_t ipart = 0; ipart < AliPID::kSPECIES; ipart++)
+       texp[ipart] += fResponseParams->EvalTExpCorr(ipart, track->P());
+      /* set integrated times */
+      track->SetIntegratedTimes(texp);
+    }
+
+  }
+
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::IsChannelEnabled(Int_t index)
+{
+  /*
+   * is channel enabled
+   */
+
+  if (!fInitFlag) {
+    AliError("class not yet initialized. Initialize it before.");
+    return kTRUE;
+  }
+
+  /* check bad status */
+  if (fStatus->GetPulserStatus(index) == AliTOFChannelOnlineStatusArray::kTOFPulserBad) return kFALSE;
+  if (fStatus->GetNoiseStatus(index) == AliTOFChannelOnlineStatusArray::kTOFNoiseBad) return kFALSE;
+  if (fStatus->GetHWStatus(index) == AliTOFChannelOnlineStatusArray::kTOFHWBad) return kFALSE;
+  
+  /* good status */
+  return kTRUE;
+
+}