]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDPreprocessorOffline.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / TRD / AliTRDPreprocessorOffline.cxx
index b33b5ccfeca976ba2fbbee9bf678d854243398cf..67a440b6e825a197bbae9615d426384af4654ccb 100644 (file)
@@ -47,6 +47,7 @@
 #include "TH2F.h"
 #include "TH1F.h"
 #include "TMath.h"
+#include "THnSparse.h"
 #include "TProfile2D.h"
 #include "AliTRDCalDet.h"
 #include "AliTRDCalPad.h"
 #include "AliTRDCalibraMode.h"
 #include "AliTRDCalibraFit.h"
 #include "AliTRDCalibraVdriftLinearFit.h"
+#include "AliTRDCalibraExbAltFit.h"
 #include "AliTRDPreprocessorOffline.h"
 #include "AliTRDCalChamberStatus.h"
+#include "AliTRDCalibChamberStatus.h"
 #include "AliTRDCommonParam.h"
-
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
+#include "AliTRDdEdxBaseUtils.h"
+#include "AliTRDdEdxCalibHistArray.h"
+#include "AliTRDdEdxCalibUtils.h"
 
 ClassImp(AliTRDPreprocessorOffline)
 
@@ -74,17 +81,25 @@ ClassImp(AliTRDPreprocessorOffline)
   fCH2d(0x0),
   fPH2d(0x0),
   fPRF2d(0x0),
+  fSparse(0x0),
   fAliTRDCalibraVdriftLinearFit(0x0),
+  fAliTRDCalibraExbAltFit(0x0),
   fNEvents(0x0),
   fAbsoluteGain(0x0),
-  fPlots(new TObjArray(9)),
-  fCalibObjects(new TObjArray(9)),
+  fPlots(new TObjArray(kNumCalibObjs)),
+  fCalibObjects(new TObjArray(kNumCalibObjs)),
+  fFirstRunGainUsed(0),
   fVersionGainUsed(0),
   fSubVersionGainUsed(0),
   fFirstRunVdriftUsed(0),
   fVersionVdriftUsed(0), 
   fSubVersionVdriftUsed(0),
+  fFirstRunExBUsed(0),
+  fVersionExBUsed(0), 
+  fSubVersionExBUsed(0),
+  fNoExBUsedInReco(kFALSE),
   fSwitchOnValidation(kTRUE),
+  fSwitchOnChamberStatus(kTRUE),
   fVdriftValidated(kFALSE),
   fExBValidated(kFALSE),
   fT0Validated(kFALSE),
@@ -92,16 +107,40 @@ ClassImp(AliTRDPreprocessorOffline)
   fMinStatsVdriftLinear(800),
   fMinStatsGain(800),
   fMinStatsPRF(600),
+  fMinStatsChamberStatus(20),
+  fMinSingleStatsChamberStatus(0.05),
   fBackCorrectGain(kFALSE),  
   fBackCorrectVdrift(kTRUE),
   fNotEnoughStatisticsForTheGain(kFALSE),
   fNotEnoughStatisticsForTheVdriftLinear(kFALSE),
   fStatusNeg(0),
-  fStatusPos(0)
+  fStatusPos(0),
+  fBadCalibValidate(40),
+  fNoDataValidate(40),
+  fRMSBadCalibratedGain(20.0),
+  fRMSBadCalibratedVdrift(20.0),
+  fRMSBadCalibratedExB(20.0),
+  fMinTimeOffsetValidate(-1.6),
+  fRobustFitDriftVelocity(kTRUE),
+  fRobustFitExbAlt(kFALSE),
+  fAlternativeVdrfitFit(kFALSE),
+  fAlternativeExbAltFit(kFALSE),
+  fMinNbOfPointVdriftFit(11),
+  fMethodeGain(0),
+  fOutliersFitChargeLow(0.03),
+  fOutliersFitChargeHigh(0.7),
+  fBeginFitCharge(3.5),
+  fT0Shift0(0.124797),
+  fT0Shift1(0.267451),
+  fPHQon(kTRUE),
+  fDebugPHQon(kFALSE)
 {
   //
   // default constructor
   //
+
+  memset(fBadCalib, 0, sizeof(Int_t) * 18);
+  memset(fNoData, 0, sizeof(Int_t) * 18);
 }
 //_________________________________________________________________________________________________________________
 AliTRDPreprocessorOffline::~AliTRDPreprocessorOffline() {
@@ -115,24 +154,63 @@ AliTRDPreprocessorOffline::~AliTRDPreprocessorOffline() {
   if(fCH2d) delete fCH2d;
   if(fPH2d) delete fPH2d;
   if(fPRF2d) delete fPRF2d;
+  if(fSparse) delete fSparse;
+
+  if(IsPHQon()){
+    AliTRDdEdxCalibUtils::DeleteHistArray();
+    AliTRDdEdxCalibUtils::DeleteObjArray();
+  }
+
   if(fAliTRDCalibraVdriftLinearFit) delete fAliTRDCalibraVdriftLinearFit;
+  if(fAliTRDCalibraExbAltFit) delete fAliTRDCalibraExbAltFit;
   if(fNEvents) delete fNEvents;
   if(fAbsoluteGain) delete fAbsoluteGain;
   if(fPlots) delete fPlots;
   if(fCalibObjects) delete fCalibObjects;
   
+}
+//___________________________________________________________________________________
+void AliTRDPreprocessorOffline::Process(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage) 
+{
+  //
+  // Process to the gain, vdrift, timeoffset, exb and chamber status calibration
+  //
+  
+  if(SetCalDetGain(startRunNumber,fVersionGainUsed,fSubVersionGainUsed) && SetCalDetVdriftExB(startRunNumber,fVersionVdriftUsed,fSubVersionVdriftUsed,fVersionExBUsed,fSubVersionExBUsed)) {
+    
+    CalibVdriftT0(file,startRunNumber,endRunNumber,ocdbStorage);
+    CalibGain(file,startRunNumber,endRunNumber,ocdbStorage);
+    if(fSwitchOnChamberStatus) CalibChamberStatus(file,startRunNumber,endRunNumber,ocdbStorage);
+    CalibExbAlt(file,startRunNumber,endRunNumber,ocdbStorage);
+
+  }
+
+  if(IsPHQon()){
+    printf("\n                  AliTRDPreprocessorOffline PHQ on!!\n\n");
+    AliTRDdEdxBaseUtils::PrintControl();
+    CalibPHQ(file, startRunNumber, endRunNumber, ocdbStorage);
+  }
+  else{
+    printf("\n                  AliTRDPreprocessorOffline PHQ off!!\n\n");
+  }
+
+  PrintStatus();
+  
 }
 //___________________________________________________________________________________________________________________
 
-void AliTRDPreprocessorOffline::CalibVdriftT0(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
+void AliTRDPreprocessorOffline::CalibVdriftT0(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage){
   //
   // make calibration of the drift velocity
   // Input parameters:
   //      file                             - the location of input file
   //      startRunNumber, endRunNumber     - run validity period 
-  //      ocdbStorage                      - path to the OCDB storage
+  //      ocdbStorage                      - OCDB storage
   //                                       - if empty - local storage 'pwd' uesed
-  if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
   //
   // 1. Initialization 
   //
@@ -174,17 +252,58 @@ void AliTRDPreprocessorOffline::CalibVdriftT0(const Char_t* file, Int_t startRun
   if(fExBValidated) UpdateOCDBExB(startRunNumber,endRunNumber,ocdbStorage);
   
 }
+//___________________________________________________________________________________________________________________
+
+void AliTRDPreprocessorOffline::CalibExbAlt(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage){
+  //
+  // make calibration of the drift velocity
+  // Input parameters:
+  //      file                             - the location of input file
+  //      startRunNumber, endRunNumber     - run validity period 
+  //      ocdbStorage                      - OCDB storage
+  //                                       - if empty - local storage 'pwd' uesed
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
+  //
+  // 1. Initialization 
+  //
+
+  //
+  // 2. extraction of the information
+  //
+  if(ReadExbAltFitGlobal(file)) AnalyzeExbAltFit();
+  //
+  // 3. Append QA plots
+  //
+  //MakeDefaultPlots(fVdriftArray,fVdriftArray);
+  //
+  //
+  // 4. validate OCDB entries
+  //
+  //
+  // 5. update of OCDB
+  //
+  //
+  UpdateOCDBExBAlt(startRunNumber,endRunNumber,ocdbStorage);
+  
+}
+
 //_________________________________________________________________________________________________________________
 
-void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
+void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage){
   //
   // make calibration of the drift velocity
   // Input parameters:
   //      file                             - the location of input file
   //      startRunNumber, endRunNumber     - run validity period 
-  //      ocdbStorage                      - path to the OCDB storage
+  //      ocdbStorage                      - OCDB storage
   //                                       - if empty - local storage 'pwd' uesed
-  if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
   //
   fNotEnoughStatisticsForTheGain = kFALSE;
   //
@@ -196,7 +315,7 @@ void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumb
   //
   AnalyzeGain();
   if(fBackCorrectGain) CorrectFromDetGainUsed();
-  if(fBackCorrectVdrift) CorrectFromDetVdriftUsed();
+  //if(fBackCorrectVdrift) CorrectFromDetVdriftUsed();
   //
   // 3. Append QA plots
   //
@@ -219,15 +338,18 @@ void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumb
 }
 //________________________________________________________________________________________________________________
 
-void AliTRDPreprocessorOffline::CalibPRF(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
+void AliTRDPreprocessorOffline::CalibPRF(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage){
   //
   // make calibration of the drift velocity
   // Input parameters:
   //      file                             - the location of input file
   //      startRunNumber, endRunNumber     - run validity period 
-  //      ocdbStorage                      - path to the OCDB storage
+  //      ocdbStorage                      - OCDB storage
   //                                       - if empty - local storage 'pwd' uesed
-  if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
   //
   // 1. Initialization 
   if(!ReadPRFGlobal(file)) return;
@@ -257,15 +379,47 @@ void AliTRDPreprocessorOffline::CalibPRF(const Char_t* file, Int_t startRunNumbe
   
 }
 //________________________________________________________________________________________________________________
+void AliTRDPreprocessorOffline::CalibPHQ(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage)
+{
+  //
+  // make calibration of puls height Q
+  // Input parameters:
+  //      startRunNumber, endRunNumber     - run validity period 
+  //      ocdbStorage                      - OCDB storage
+  //                                       - if empty - local storage 'pwd' uesed
+  //
+
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
+  //printf("test %s\n", ocdbStorage.Data());
 
-void AliTRDPreprocessorOffline::CalibChamberStatus(Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
+  if(!ReadPHQGlobal(file)) return;
+
+  if(!AnalyzePHQ(startRunNumber)) return;
+
+  UpdateOCDBPHQ(startRunNumber,endRunNumber,ocdbStorage);
+}
+
+//________________________________________________________________________________________________________________
+
+void AliTRDPreprocessorOffline::CalibChamberStatus(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* ocdbStorage){
   //
   // make calibration of the chamber status
   // Input parameters:
   //      startRunNumber, endRunNumber     - run validity period 
-  //      ocdbStorage                      - path to the OCDB storage
+  //      ocdbStorage                      - OCDB storage
   //                                       - if empty - local storage 'pwd' uesed
-  if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+  if (ocdbStorage==0x0) {
+    TString localStorage = "local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+    ocdbStorage=AliCDBManager::Instance()->GetStorage(localStorage.Data());
+  }
+  //
+  //
+  // 1. Initialization  
+  if(!ReadStatusGlobal(file)) return;
+  //
   //
   //
   // 2. extraction of the information
@@ -280,15 +434,19 @@ void AliTRDPreprocessorOffline::CalibChamberStatus(Int_t startRunNumber, Int_t e
   //
   // 4. validate OCDB entries
   //
-  if(fSwitchOnValidation==kTRUE && ValidateChamberStatus()==kFALSE) { 
-    //AliError("TRD Chamber status OCDB parameters not ok!");
-    return;
+  //printf("Enough stats for vdrift? %d\n",(Int_t)fNotEnoughStatisticsForTheVdriftLinear);
+  //printf("Enough stats for gain? %d\n",(Int_t)fNotEnoughStatisticsForTheGain); 
+  if((!fNotEnoughStatisticsForTheVdriftLinear) && (!fNotEnoughStatisticsForTheGain)) {
+    if(fSwitchOnValidation==kTRUE && ValidateChamberStatus()==kFALSE) { 
+      //AliError("TRD Chamber status OCDB parameters not ok!");
+      return;
+    }
+    //
+    // 5. update of OCDB
+    //
+    //
+    UpdateOCDBChamberStatus(startRunNumber,endRunNumber,ocdbStorage);
   }
-  //
-  // 5. update of OCDB
-  //
-  //
-  if((!fNotEnoughStatisticsForTheGain) && (!fNotEnoughStatisticsForTheGain)) UpdateOCDBChamberStatus(startRunNumber,endRunNumber,ocdbStorage);
   
 }
 //______________________________________________________________________________________________________
@@ -311,32 +469,78 @@ Bool_t AliTRDPreprocessorOffline::Init(const Char_t* fileName){
   if(ReadGainGlobal(fileName)) {
 
     TString namech = fCH2d->GetTitle();
+    fFirstRunGainUsed = GetFirstRun(namech); 
     fVersionGainUsed = GetVersion(namech);  
     fSubVersionGainUsed = GetSubVersion(namech);    
 
     //printf("Found Version %d, Subversion %d for gain\n",fVersionGainUsed,fSubVersionGainUsed);
 
   }
+  
+  if(ReadVdriftLinearFitGlobal(fileName)) {
+
+    TString namelinear = fAliTRDCalibraVdriftLinearFit->GetNameCalibUsed();
+    fFirstRunExBUsed = GetFirstRun(namelinear); 
+    fVersionExBUsed = GetVersion(namelinear);  
+    fSubVersionExBUsed = GetSubVersion(namelinear);   
+
+    //printf("Found Version %d, Subversion %d, run %d for ExB\n",fVersionExBUsed,fSubVersionExBUsed,fFirstRunExBUsed);
+    
+  }
    
   if(fVersionVdriftUsed == 0) fStatusPos = fStatusPos |kVdriftErrorOld;
   if(fVersionGainUsed == 0) fStatusPos = fStatusPos | kGainErrorOld;
+
   return kTRUE;
   
 }
 //___________________________________________________________________________________________________________________
 
+Bool_t AliTRDPreprocessorOffline::ReadStatusGlobal(const Char_t* fileName){
+  //
+  // read calibration entries from file
+  // 
+  if(fSparse) return kTRUE;
+  TFile fcalib(fileName);
+  TList * array = (TList*)fcalib.Get(fNameList);
+  if (array){
+    fSparse = (THnSparseI *) array->FindObject("NumberOfEntries");
+    if(!fSparse) return kFALSE;
+  }
+  else 
+    return kFALSE;
+  
+  return kTRUE;
+  
+}
+//___________________________________________________________________________________________________________________
+
+Bool_t AliTRDPreprocessorOffline::ReadPHQGlobal(const Char_t* fileName)
+{
+  //
+  // read calibration entries from file
+  //
+
+  return AliTRDdEdxCalibUtils::ReadHistArray(fileName, fNameList);
+}
+
+//___________________________________________________________________________________________________________________
+
 Bool_t AliTRDPreprocessorOffline::ReadGainGlobal(const Char_t* fileName){
   //
   // read calibration entries from file
   // 
   if(fCH2d) return kTRUE;
   TFile fcalib(fileName);
-  TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
+  TList * array = (TList*)fcalib.Get(fNameList);
   if (array){
     TH2I *ch2d = (TH2I *) array->FindObject("CH2d");
-    if(!ch2d) return kFALSE;
+    if(!ch2d) {
+      delete array;
+      return kFALSE;
+    }
     fCH2d = (TH2I*)ch2d->Clone();
+    delete array;
     //fNEvents = (TH1I *) array->FindObject("NEvents");
     //fAbsoluteGain = (TH2F *) array->FindObject("AbsoluteGain");
   }else{
@@ -360,12 +564,16 @@ Bool_t AliTRDPreprocessorOffline::ReadVdriftT0Global(const Char_t* fileName){
   // 
   if(fPH2d) return kTRUE;
   TFile fcalib(fileName);
-  TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
+  TList * array = (TList*)fcalib.Get(fNameList);
   if (array){
     TProfile2D *ph2d = (TProfile2D *) array->FindObject("PH2d");
-    if(!ph2d) return kFALSE;
+    if(!ph2d) {
+      delete array;
+      return kFALSE;
+    }
     fPH2d = (TProfile2D*)ph2d->Clone();
     //fNEvents = (TH1I *) array->FindObject("NEvents");
+    delete array;
   }else{
     TProfile2D *ph2d = (TProfile2D *) fcalib.Get("PH2d");
     if(!ph2d) return kFALSE;
@@ -386,10 +594,12 @@ Bool_t AliTRDPreprocessorOffline::ReadVdriftLinearFitGlobal(const Char_t* fileNa
   // 
   if(fAliTRDCalibraVdriftLinearFit) return kTRUE;
   TFile fcalib(fileName);
-  TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
+  TList * array = (TList*)fcalib.Get(fNameList);
   if (array){
-    fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) array->FindObject("AliTRDCalibraVdriftLinearFit");
+    AliTRDCalibraVdriftLinearFit * dummy = (AliTRDCalibraVdriftLinearFit *) array->FindObject("AliTRDCalibraVdriftLinearFit");
+    fAliTRDCalibraVdriftLinearFit = dummy ? (AliTRDCalibraVdriftLinearFit *) dummy->Clone() : 0x0;
     //fNEvents = (TH1I *) array->FindObject("NEvents");
+    delete array;
   }else{
     fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) fcalib.Get("AliTRDCalibraVdriftLinearFit");
     //fNEvents = (TH1I *) fcalib.Get("NEvents");
@@ -400,6 +610,30 @@ Bool_t AliTRDPreprocessorOffline::ReadVdriftLinearFitGlobal(const Char_t* fileNa
   }
   return kTRUE;
   
+}
+//_____________________________________________________________________________________________________________
+Bool_t AliTRDPreprocessorOffline::ReadExbAltFitGlobal(const Char_t* fileName){
+  //
+  // read calibration entries from file
+  // 
+  if(fAliTRDCalibraExbAltFit) return kTRUE;
+  TFile fcalib(fileName);
+  TList * array = (TList*)fcalib.Get(fNameList);
+  if (array){
+     AliTRDCalibraExbAltFit * dummy = (AliTRDCalibraExbAltFit *) array->FindObject("AliTRDCalibraExbAltFit");
+     fAliTRDCalibraExbAltFit = dummy ? (AliTRDCalibraExbAltFit *)dummy->Clone() : 0x0;
+    //fNEvents = (TH1I *) array->FindObject("NEvents");
+    delete array;
+  }else{
+    fAliTRDCalibraExbAltFit = (AliTRDCalibraExbAltFit *) fcalib.Get("AliTRDCalibraExbAltFit");
+    //fNEvents = (TH1I *) fcalib.Get("NEvents");
+  }
+  if(!fAliTRDCalibraExbAltFit) {
+    //printf("No AliTRDCalibraExbAltFit\n");
+    return kFALSE;
+  }
+  return kTRUE;
+  
 }
 //_____________________________________________________________________________________________________________
 
@@ -409,11 +643,15 @@ Bool_t AliTRDPreprocessorOffline::ReadPRFGlobal(const Char_t* fileName){
   // 
   if(fPRF2d) return kTRUE;
   TFile fcalib(fileName);
-  TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
+  TList * array = (TList*)fcalib.Get(fNameList);
   if (array){
     TProfile2D *prf2d = (TProfile2D *) array->FindObject("PRF2d");
-    if(!prf2d) return kFALSE;
+    if(!prf2d) {
+      delete array;
+      return kFALSE;
+    }
     fPRF2d = (TProfile2D*)prf2d->Clone();
+    delete array;
     //fNEvents = (TH1I *) array->FindObject("NEvents");
   }else{
     TProfile2D *prf2d = (TProfile2D *) fcalib.Get("PRF2d");
@@ -435,6 +673,10 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeGain(){
   //
 
   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
+  calibra->ChooseMethod(fMethodeGain);
+  calibra->SetBeginFitCharge(fBeginFitCharge);
+  calibra->SetFitOutliersChargeLow(fOutliersFitChargeLow);
+  calibra->SetFitOutliersChargeHigh(fOutliersFitChargeHigh);
   calibra->SetMinEntries(fMinStatsGain); // If there is less than 1000 entries in the histo: no fit
   calibra->AnalyseCH(fCH2d);
 
@@ -506,8 +748,11 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftT0(){
   //
 
   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
+  calibra->SetT0Shift0(fT0Shift0);
+  calibra->SetT0Shift1(fT0Shift1);
   calibra->SetMinEntries(fMinStatsVdriftT0PH); // If there is less than 1000 entries in the histo: no fit
-  calibra->AnalysePH(fPH2d);
+  calibra->AnalysePH(fPH2d); 
+  //calibra->SetDebugLevel(2);
 
   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
@@ -520,6 +765,7 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftT0(){
   Bool_t ok = kFALSE;
   if ((nbtg >                  0) && 
       (nbfit        >= 0.5*nbE) && (nbE > 30) && (nbfitSuccess > 30)) {
+    //printf("nbtg %d, nbfit %d, nbE %d, nbfitSucess %d\n",nbtg,nbfit,nbE,nbfitSuccess);
     //printf("Pass the cut for VdriftT0\n");
     // create the cal objects
     calibra->RemoveOutliers(1,kFALSE);
@@ -570,8 +816,16 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftLinearFit(){
   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
   calibra->SetCalDetVdriftExB(fCalDetVdriftUsed,fCalDetExBUsed);
   calibra->SetMinEntries(fMinStatsVdriftLinear); // If there is less than 1000 entries in the histo: no fit
+  //printf("The mean stat is by %d for VdriftLinear\n",fMinStatsVdriftLinear);
+  //fAliTRDCalibraVdriftLinearFit->SetSeeDetector(0);
+  //fAliTRDCalibraVdriftLinearFit->SetDebugLevel(1);
   //printf("Fill PE Array\n");
-  fAliTRDCalibraVdriftLinearFit->FillPEArray();
+  fAliTRDCalibraVdriftLinearFit->SetRobustFit(fRobustFitDriftVelocity);
+  fAliTRDCalibraVdriftLinearFit->SetMinNumberOfPointsForFit(fMinNbOfPointVdriftFit);
+  if(!fAlternativeVdrfitFit)
+    fAliTRDCalibraVdriftLinearFit->FillPEArray();
+  else
+    fAliTRDCalibraVdriftLinearFit->FillPEArray2();
   //printf("AliTRDCalibraFit\n");
   calibra->AnalyseLinearFitters(fAliTRDCalibraVdriftLinearFit);
   //printf("After\n");
@@ -622,7 +876,7 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftLinearFit(){
       if((oldmeanvdrift > 0.0) && (oldmeanexb < 70.0))  {
        //printf("Correction factor %f\n",vdriftoverall);
        calDetVdrift->Multiply(vdriftoverall/oldmeanvdrift);
-       calDetLorentz->Multiply(exboverall/oldmeanexb);
+       if(TMath::Abs(oldmeanexb) > 0.0001) calDetLorentz->Multiply(exboverall/oldmeanexb);
        //printf("newmean %f\n",calDetVdrift->CalcMean(kFALSE));
        TH1F *coefDriftLinear  = calDetVdrift->MakeHisto1DAsFunctionOfDet();
        TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
@@ -654,6 +908,56 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftLinearFit(){
 }
 //________________________________________________________________________________________________________________
 
+Bool_t AliTRDPreprocessorOffline::AnalyzeExbAltFit(){
+  //
+  // Analyze vdrift linear fit - produce the calibration objects
+  //
+
+  //printf("Analyse linear fit\n");
+
+  
+  AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
+  calibra->SetMinEntries(fMinStatsVdriftLinear); // If there is less than 1000 entries in the histo: no fit
+  //printf("Fill PE Array\n");
+  fAliTRDCalibraExbAltFit->SetRobustFit(fRobustFitExbAlt);
+  if(!fAlternativeExbAltFit)
+    fAliTRDCalibraExbAltFit->FillPEArray();
+  else
+    fAliTRDCalibraExbAltFit->FillPEArray2();
+  //printf("AliTRDCalibraFit\n");
+  calibra->AnalyseExbAltFit(fAliTRDCalibraExbAltFit);
+  //printf("After\n");
+
+  //Int_t nbtg        = 540;
+  Int_t nbfit       = calibra->GetNumberFit();
+  Int_t nbE         = calibra->GetNumberEnt();
+
+  
+  Bool_t ok = kFALSE;
+  // enough statistics
+  if ((nbfit        >= 0.5*nbE) && (nbE > 30)) {
+    // create the cal objects
+    //calibra->RemoveOutliers(1,kTRUE);
+    calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
+    //
+    TObjArray object  = calibra->GetVectorFit2();
+    AliTRDCalDet *calDetLorentz = calibra->CreateDetObjectExbAlt(&object);
+    TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
+    //if(!calDetLorentz) printf("No lorentz created\n");
+    // Put them in the array
+    fCalibObjects->AddAt(calDetLorentz,kExbAlt);
+    fPlots->AddAt(coefLorentzAngle,kExbAlt);
+    //
+    ok = kTRUE;
+  }
+  
+  calibra->ResetVectorFit();
+  
+  return ok;
+  
+}
+//________________________________________________________________________________________________________________
+
 Bool_t AliTRDPreprocessorOffline::AnalyzePRF(){
   //
   // Analyze PRF - produce the calibration objects
@@ -690,6 +994,44 @@ Bool_t AliTRDPreprocessorOffline::AnalyzePRF(){
   
 }
 
+//_____________________________________________________________________________
+Bool_t AliTRDPreprocessorOffline::AnalyzePHQ(Int_t startRunNumber)
+{
+  //
+  //Produce PHQ calibration results
+  //
+  TList *lout = 0x0;
+  TTreeSRedirector *calibStream = 0x0;
+  if(IsDebugPHQon()){
+    lout = new TList;
+    lout->SetOwner();
+
+    calibStream = new TTreeSRedirector(Form("TRDCalibStream_%010d.root", startRunNumber));
+  }
+
+  for(Int_t iter=0; iter<AliTRDdEdxCalibUtils::GetHistArray()->GetSize(); iter++){
+    THnBase *hi = (THnBase*) AliTRDdEdxCalibUtils::GetHistAt(iter);
+    TObjArray *obji = AliTRDdEdxCalibUtils::HistToObj(hi, startRunNumber, lout, calibStream);
+    //printf("test analyze %s\n", obji->GetName());
+    AliTRDdEdxCalibUtils::GetObjArray()->AddAt(obji, iter);
+  }
+
+  fCalibObjects->AddAt(AliTRDdEdxCalibUtils::GetObjArray(), kPHQ);
+
+  if(lout){
+    TFile *fout=new TFile(Form("TRDCalibList_%010d.root", startRunNumber),"recreate");
+    fout->cd();
+    lout->Write();
+    fout->Save();
+    fout->Close();
+    delete fout;
+  }
+  delete calibStream;
+  delete lout;
+
+  return kTRUE;
+}
+
 //_____________________________________________________________________________
 Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
 {
@@ -697,9 +1039,12 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
   // Produce AliTRDCalChamberStatus out of calibration results
   //
   
-  // set up AliTRDCalChamberStatus
-  AliTRDCalChamberStatus *CalChamberStatus = new AliTRDCalChamberStatus();
-  for(Int_t det = 0; det < 540; det++) CalChamberStatus->SetStatus(det,1);
+  // set up AliTRDCalibChamberStatus
+  AliTRDCalibChamberStatus *chamberStatus = new AliTRDCalibChamberStatus();
+  chamberStatus->SetSparseI(fSparse);
+  chamberStatus->AnalyseHisto(fMinStatsChamberStatus, fMinSingleStatsChamberStatus);
+  // get AliTRDCalChamberStatus
+  AliTRDCalChamberStatus *calChamberStatus = chamberStatus->GetCalChamberStatus();
 
   // get calibration objects
   AliTRDCalDet *calDetGain   = (AliTRDCalDet *) fCalibObjects->At(kGain);
@@ -707,16 +1052,16 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
   AliTRDCalDet *calDetExB    = (AliTRDCalDet *) fCalibObjects->At(kLorentzLinear);
 
   // Check
-  if((!calDetGain) || (!calDetVDrift) || (!fCH2d) || (!calDetExB)) return kFALSE;
+  if((!calDetGain) || (!calDetVDrift) || (!fCH2d) || (!calDetExB) || (!calChamberStatus)) return kFALSE;
 
   // Gain
-  Double_t gainmean = calDetGain->GetMean();
+  Double_t gainmean   = calDetGain->GetMean();
   Double_t vdriftmean = calDetVDrift->GetMean();
-  Double_t exbmean = calDetExB->GetMean();
+  Double_t exbmean    = calDetExB->GetMean();
 
-  Double_t gainrms = calDetGain->GetRMSRobust();
-  Double_t vdriftrms = calDetVDrift->GetRMSRobust();
-  Double_t exbrms = calDetExB->GetRMSRobust();
+  Double_t gainrms    = calDetGain->GetRMSRobust();
+  Double_t vdriftrms  = calDetVDrift->GetRMSRobust();
+  Double_t exbrms     = calDetExB->GetRMSRobust();
 
   //printf("Gain mean: %f, rms: %f\n",gainmean,gainrms);
   //printf("Vdrift mean: %f, rms: %f\n",vdriftmean,vdriftrms);
@@ -725,6 +1070,19 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
   // Check
   if((TMath::Abs(gainrms) < 0.001) || (TMath::Abs(vdriftrms) < 0.001) || (TMath::Abs(exbrms) < 0.0000001)) return kFALSE;
 
+  // Take mean each SM
+  Double_t *gainmeanSM = new Double_t[18];
+  Double_t *vdriftmeanSM = new Double_t[18];
+  Double_t *exbmeanSM = new Double_t[18];
+  //Double_t *t0meanSM = new Double_t[18];
+  for(Int_t sm=0; sm< 18; sm++) {
+    gainmeanSM[sm] = calDetGain->GetMeanSM(kFALSE,sm);
+    vdriftmeanSM[sm] = calDetVDrift->GetMeanSM(kFALSE,sm);
+    exbmeanSM[sm] = calDetExB->GetMeanSM(kFALSE,sm);
+    //t0meanSM[sm] = calDetGain->GetMeanSM(kFALSE);
+  }
+
+
   // mask chambers with empty gain entries
   //Int_t counter = 0;
   for (Int_t idet = 0; idet < 540; idet++) {
@@ -732,6 +1090,10 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
     // ch2d
     TH1I *projch =  (TH1I *) fCH2d->ProjectionX("projch",idet+1,idet+1,(Option_t *)"e");
     Double_t entries = projch->GetEntries();
+    //printf("Number of entries %f for det %d\n",entries,idet);
+
+    // sm number
+    Int_t smnumber = (Int_t) idet/30;
 
     // gain
     Double_t gain = calDetGain->GetValue(idet);
@@ -743,49 +1105,74 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
     Double_t exb = calDetExB->GetValue(idet);
 
 
-    if(entries<=0.5 ||
-       TMath::Abs(gainmean-gain) > (15.0*gainrms) ||
-       TMath::Abs(vdriftmean-vdrift) > (15.0*vdriftrms) ||
-       TMath::Abs(exbmean-exb) > (50.0*exbrms)) {
+    if( (entries<50 && !calChamberStatus->IsNoData(idet))  ||
+        TMath::Abs(gainmean-gain) > (fRMSBadCalibratedGain*gainrms)          ||
+        TMath::Abs(vdriftmean-vdrift) > (fRMSBadCalibratedVdrift*vdriftrms)    ||
+        TMath::Abs(exbmean-exb) > (fRMSBadCalibratedExB*exbrms) ) {
      
       //printf(" chamber det %03d masked \n",idet);
       //printf(" gainmean %f and gain %f, gainrms %f \n",gainmean,gain,gainrms);
       //printf(" vdriftmean %f and vdrift %f, vdriftrms %f \n",vdriftmean,vdrift,vdriftrms);
       //printf(" exbmean %f and exb %f, exbrms %f \n",exbmean,exb,exbrms);
       
-      CalChamberStatus->SetStatus(idet,AliTRDCalChamberStatus::kMasked);
+      calChamberStatus->SetStatus(idet,AliTRDCalChamberStatus::kBadCalibrated);
       //counter++;
     }
 
-     /*
-     // installed supermodules+1 -> abort
-     if(counter > (7+1)*30) {
-       printf("ERROR: more than one SM to be masked!! \n Abort...\n");
-       if(projch) delete projch;
-       return 0x0;
-     }
-     */
+    if(TMath::Abs(gainmeanSM[smnumber]-gain) < 0.000001  ||
+       TMath::Abs(vdriftmeanSM[smnumber]-vdrift) < 0.000001 ||
+       TMath::Abs(exbmeanSM[smnumber]-exb) < 0.000001) {
+      
+      //printf(" chamber det %03d notcalibrated sm %d \n",idet,smnumber);
+      //printf(" gainmeanSM %f and gain %f\n",gainmeanSM[smnumber],gain);
+      //printf(" vdriftmeanSM %f and vdrift %f \n",vdriftmeanSM[smnumber],vdrift);
+      //printf(" exbmeanSM %f and exb %f \n",exbmeanSM[smnumber],exb);
+
+      calChamberStatus->SetStatus(idet,AliTRDCalChamberStatus::kNotCalibrated);
+    }
+
 
     delete projch;
     
    }
 
-   // Security
-   for(Int_t sm=0; sm < 18; sm++) {
-     Int_t counter = 0;
-     for(Int_t det = 0; det < 30; det++){
-       Int_t detector = sm*30+det;
-       if(CalChamberStatus->IsMasked(detector)) counter++;
-     }
-     if(counter >= 10) {
-       for(Int_t det = 0; det < 30; det++){
-        Int_t detector = sm*30+det;
-        CalChamberStatus->SetStatus(detector,AliTRDCalChamberStatus::kInstalled);
-       }
-     }
-   }
+  // Security
+  for(Int_t sm=0; sm < 18; sm++) {
+    Int_t smnodata   = 0;
+    Int_t smbadcalib = 0;
+    for(Int_t det = 0; det < 30; det++){
+      Int_t detector = sm*30+det;
+      if(calChamberStatus->IsNoData(detector)) smnodata++;
+      else {
+       if(calChamberStatus->IsBadCalibrated(detector)) smbadcalib++;
+      }
+    }
+    fNoData[sm]  = smnodata;
+    fBadCalib[sm]= smbadcalib;
+    //printf("No Data %d, bad calibrated %d for %d\n",fNoData[sm],fBadCalib[sm],sm);
+  }
 
-   fCalibObjects->AddAt(CalChamberStatus,kChamberStatus);
+  // delete
+  delete []gainmeanSM;
+  delete []vdriftmeanSM;
+  delete []exbmeanSM;
+  
+  // Security
+  //   for(Int_t sm=0; sm < 18; sm++) {
+  //     Int_t counter = 0;
+  //     for(Int_t det = 0; det < 30; det++){
+  //       Int_t detector = sm*30+det;
+  //       if(calChamberStatus->IsBadCalibrated(detector)) counter++;
+  //     }
+  //     if(counter >= 20) {
+  //       for(Int_t det = 0; det < 30; det++){
+  //   Int_t detector = sm*30+det;
+  //   calChamberStatus->SetStatus(detector,AliTRDCalChamberStatus::kGood);
+  //       }
+  //     }
+  //  }
+
+   fCalibObjects->AddAt(calChamberStatus,kChamberStatus);
    return kTRUE;
 
  }
@@ -885,7 +1272,7 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    
  }
 //_________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBGain(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBGain(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage *storage){
    //
    // Update OCDB entry
    //
@@ -893,17 +1280,17 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalDet");
    metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
    AliCDBId id1("TRD/Calib/ChamberGainFactor", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kGain);
-   if(calDet) gStorage->Put(calDet, id1, metaData);
+   if(calDet) storage->Put(calDet, id1, metaData);
 
 
  }
  //___________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBExB(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBExB(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage *storage){
    //
    // Update OCDB entry
    //
@@ -916,17 +1303,40 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalDet");
    metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
    AliCDBId id1("TRD/Calib/ChamberExB", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detExB);
-   if(calDet) gStorage->Put(calDet, id1, metaData);
+   if(calDet) storage->Put(calDet, id1, metaData);
    //if(!calDet) printf("No caldet\n");
 
  }
+//___________________________________________________________________________________________________________________
+void AliTRDPreprocessorOffline::UpdateOCDBExBAlt(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage *storage){
+  //
+  // Update OCDB entry
+  //
+
+  Int_t detExB = kExbAlt;
+  if(!fMethodSecond) return;
+
+  //printf("Pass\n");
+
+  AliCDBMetaData *metaData= new AliCDBMetaData();
+  metaData->SetObjectClassName("AliTRDCalDet");
+  metaData->SetResponsible("Theo Rascanu");
+  metaData->AddDateToComment();
+  metaData->SetBeamPeriod(1);
+
+  AliCDBId id1("TRD/Calib/ChamberExBAlt", startRunNumber, endRunNumber);
+  AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detExB);
+  if(calDet) storage->Put(calDet, id1, metaData);
+  //if(!calDet) printf("No caldet\n");
+
+}
  //___________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBVdrift(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBVdrift(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* storage){
    //
    // Update OCDB entry
    //
@@ -938,12 +1348,12 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalDet");
    metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
    AliCDBId id1("TRD/Calib/ChamberVdrift", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
-   if(calDet) gStorage->Put(calDet, id1, metaData);
+   if(calDet) storage->Put(calDet, id1, metaData);
 
    //
 
@@ -952,17 +1362,18 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
      AliCDBMetaData *metaDataPad= new AliCDBMetaData();
      metaDataPad->SetObjectClassName("AliTRDCalPad");
      metaDataPad->SetResponsible("Raphaelle Bailhache");
+     metaDataPad->AddDateToComment();
      metaDataPad->SetBeamPeriod(1);
 
      AliCDBId id1Pad("TRD/Calib/LocalVdrift", startRunNumber, endRunNumber);
      AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kVdriftPHPad);
-     if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
+     if(calPad) storage->Put(calPad, id1Pad, metaDataPad);
 
    }
 
  }
  //________________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBT0(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBT0(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* storage){
    //
    // Update OCDB entry
    //
@@ -970,29 +1381,30 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalDet");
    metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
    AliCDBId id1("TRD/Calib/ChamberT0", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kT0PHDet);
-   if(calDet) gStorage->Put(calDet, id1, metaData);
+   if(calDet) storage->Put(calDet, id1, metaData);
 
    //
 
    AliCDBMetaData *metaDataPad= new AliCDBMetaData();
    metaDataPad->SetObjectClassName("AliTRDCalPad");
    metaDataPad->SetResponsible("Raphaelle Bailhache");
+   metaDataPad->AddDateToComment();
    metaDataPad->SetBeamPeriod(1);
 
    AliCDBId id1Pad("TRD/Calib/LocalT0", startRunNumber, endRunNumber);
    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kT0PHPad);
-   if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
+   if(calPad) storage->Put(calPad, id1Pad, metaDataPad);
 
 
 
  }
  //_________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBPRF(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBPRF(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* storage){
    //
    // Update OCDB entry
    //
@@ -1000,30 +1412,51 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalPad");
    metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
+
    AliCDBId id1("TRD/Calib/PRFWidth", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kPRF);
-   if(calPad) gStorage->Put(calPad, id1, metaData);
+   if(calPad) storage->Put(calPad, id1, metaData);
 
 
  }
+//_________________________________________________________________________________________________________________
+void AliTRDPreprocessorOffline::UpdateOCDBPHQ(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* storage)
+{
+  //
+  // Update OCDB entry
+  //
+  AliCDBMetaData *metaData= new AliCDBMetaData();
+  metaData->SetObjectClassName("TObjArray");
+  metaData->SetResponsible("Raphaelle Bailhache and Xianguo Lu");
+  metaData->AddDateToComment();
+  metaData->SetBeamPeriod(1);
+
+  AliCDBId id1("TRD/Calib/PHQ", startRunNumber, endRunNumber);
+  TObjArray *cobj = (TObjArray *) fCalibObjects->At(kPHQ);
+  if(cobj){
+    //cobj->Print();
+    storage->Put(cobj, id1, metaData);
+  }
+}
+
  //_________________________________________________________________________________________________________________
- void AliTRDPreprocessorOffline::UpdateOCDBChamberStatus(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
+ void AliTRDPreprocessorOffline::UpdateOCDBChamberStatus(Int_t startRunNumber, Int_t endRunNumber, AliCDBStorage* storage){
    //
    // Update OCDB entry
    //
 
    AliCDBMetaData *metaData= new AliCDBMetaData();
    metaData->SetObjectClassName("AliTRDCalChamberStatus");
-   metaData->SetResponsible("Raphaelle Bailhache");
+   metaData->SetResponsible("Raphaelle Bailhache and Julian Book");
+   metaData->AddDateToComment();
    metaData->SetBeamPeriod(1);
 
    AliCDBId id1("TRD/Calib/ChamberStatus", startRunNumber, endRunNumber);
-   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
    AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
-   if(calChamberStatus) gStorage->Put(calChamberStatus, id1, metaData);
+   if(calChamberStatus) storage->Put(calChamberStatus, id1, metaData);
 
 
  }
@@ -1122,8 +1555,8 @@ Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
      Double_t rmsdet = calDet->GetRMSRobust();
      Double_t meanpad = calPad->GetMean();
      //Double_t rmspad = calPad->GetRMS();
-     //printf("T0::minimum %f, rmsdet %f,meanpad %f, rmspad %f\n",meandet,rmsdet,meanpad,rmspad);
-     if((meandet > -1.5) && (meandet < 5.0) && (rmsdet < 4.0) && (meanpad < 5.0) && (meanpad > -0.5)) return kTRUE;
+     printf("T0::meandet %f, rmsdet %f,meanpad %f\n",meandet,rmsdet,meanpad);
+     if((meandet >   fMinTimeOffsetValidate) && (meandet < 5.0) && (rmsdet < 4.0) && (meanpad < 5.0) && (meanpad > -0.5)) return kTRUE;
      else {
        fStatusPos = fStatusPos | kTimeOffsetErrorRange;
        return kFALSE;
@@ -1158,19 +1591,28 @@ Bool_t AliTRDPreprocessorOffline::ValidateChamberStatus(){
   
   AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
   if(calChamberStatus) {
-    Int_t detectormasked = 0;
-    for(Int_t det = 0; det < 540; det++) {
-      if(calChamberStatus->IsMasked(det)) detectormasked++;
+
+    Int_t detectornodata   = 0;
+    Int_t detectorbadcalib = 0;
+    
+    for(Int_t sm=0; sm < 18; sm++) {
+      //printf("%d chambers w/o data in sm %d\n",fNoData[sm],sm);
+      //printf("%d bad calibrated chambers in sm %d\n",fBadCalib[sm],sm);
+      if(fNoData[sm] != 30) detectornodata += fNoData[sm];
+      detectorbadcalib+=fBadCalib[sm];
     }
-    //printf("Number of chambers masked %d\n",detectormasked);
-    if(detectormasked > 40) {
+    //printf("Number of chambers w/o data %d\n",detectornodata);
+    //printf("Number of chambers bad calibrated %d\n",detectorbadcalib);
+
+    if((detectornodata > fNoDataValidate) ||
+       (detectorbadcalib > fBadCalibValidate)){
       fStatusPos = fStatusPos | kChamberStatusErrorRange;
       return kFALSE;
     }
-    else return kTRUE;
+    return kTRUE;
   }
   else return kFALSE;
+  
 }
 //_____________________________________________________________________________
 Int_t AliTRDPreprocessorOffline::GetVersion(TString name) const
@@ -1326,5 +1768,99 @@ void AliTRDPreprocessorOffline::SetCalDetVdrift(AliTRDCalDet *calDetVdriftUsed)
   }
   
 };
+//___________________________________________________________________________________
+Bool_t AliTRDPreprocessorOffline::SetCalDetGain(Int_t runNumber, Int_t version, Int_t subversion) 
+{
+  //
+  // Set the fCalDetGainUsed
+  //
 
+  if((version == 0) && (subversion == 0)) return kFALSE;
+
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberGainFactor",runNumber, version, subversion);
+  if(!entry) {
+    AliError("Found no entry\n");
+    fStatusPos = fStatusPos | kGainErrorOld;
+    return kFALSE;
+  }
+  //const AliCDBId id = entry->GetId();
+  //version = id.GetVersion();
+  //subversion = id.GetSubVersion();
+  //printf("Found version %d and subversion %d for vdrift\n",version,subversion);
+  AliTRDCalDet* calDet = (AliTRDCalDet *)entry->GetObject();
+  if(calDet) fCalDetGainUsed = calDet;
+  else {
+    fStatusPos = fStatusPos | kGainErrorOld;
+    return kFALSE;
+  }
+  
+  return kTRUE;
+
+}
+//___________________________________________________________________________________
+Bool_t AliTRDPreprocessorOffline::SetCalDetVdriftExB(Int_t runNumber, Int_t versionv, Int_t subversionv, Int_t versionexb, Int_t subversionexb) 
+{
+  //
+  // Set the fCalDetVdriftUsed and fCalDetExBUsed
+  //
+
+  if((versionv == 0) && (subversionv == 0)) return kFALSE;
+
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberVdrift",runNumber, versionv, subversionv);
+  if(!entry) {
+    AliError("Found no entry\n");
+    fStatusPos = fStatusPos | kVdriftErrorOld;
+    return kFALSE;
+  }
+  AliTRDCalDet* calDet = (AliTRDCalDet *)entry->GetObject();
+  if(calDet) fCalDetVdriftUsed = calDet;
+  else {
+    fStatusPos = fStatusPos | kVdriftErrorOld;
+    return kFALSE;
+  }
+
+  // ExB object
+
+  if((versionexb == 0) && (subversionexb == 0)) {
+    
+    fCalDetExBUsed = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
+    for(Int_t k = 0; k < 540; k++){
+      fCalDetExBUsed->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(fCalDetVdriftUsed->GetValue(k)));
+      //printf("Nothing found: set the exb object for detector %d, vdrift %f and exb %f\n",k,fCalDetVdriftUsed->GetValue(k),fCalDetExBUsed->GetValue(k));
+    }
+  }
+  else {
+
+    entry = 0x0;
+    entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberExB",runNumber, versionexb, subversionexb);
+    if(!entry) {
+      //printf("Found no entry\n");
+      fStatusPos = fStatusPos | kExBErrorOld;   
+      return kFALSE;
+    }
+    AliTRDCalDet* calDetexb = (AliTRDCalDet *)entry->GetObject();
+    if(!calDetexb) {
+      fStatusPos = fStatusPos | kExBErrorOld;   
+      return kFALSE;
+    }
+    
+    Double_t meanexb = calDetexb->GetMean();
+    //printf("Mean value %f\n",meanexb);
+    if((meanexb > 70) || (fNoExBUsedInReco)) {
+      fCalDetExBUsed = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
+      for(Int_t k = 0; k < 540; k++){
+       fCalDetExBUsed->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(fCalDetVdriftUsed->GetValue(k)));
+       //printf("Found but: set the exb object for detector %d, vdrift %f and exb %f\n",k,fCalDetVdriftUsed->GetValue(k),fCalDetExBUsed->GetValue(k));
+      }
+    }
+    else {
+      fCalDetExBUsed = calDetexb;
+    }
+    
+  }
+
+  
+  return kTRUE;
+
+}