Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc	(revision 2980)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc	(revision 3007)
@@ -50,4 +50,14 @@
 using namespace std;
 
+const Int_t   MHCalibrationPixel::fChargeNbinsHiGain = 2100;
+const Int_t   MHCalibrationPixel::fChargeNbinsLoGain = 1010;
+const Int_t   MHCalibrationPixel::fAbsTimeNbins      = 32;
+const Int_t   MHCalibrationPixel::fRelTimeNbins      = 240;
+const Int_t   MHCalibrationPixel::fChargevsNbins     = 5000;
+const Axis_t  MHCalibrationPixel::fAbsTimeFirst      = - 0.25;
+const Axis_t  MHCalibrationPixel::fAbsTimeLast       =  15.75;
+const Axis_t  MHCalibrationPixel::fRelTimeFirst      = -11.;
+const Axis_t  MHCalibrationPixel::fRelTimeLast       =  12.;
+
 // --------------------------------------------------------------------------
 //
@@ -56,14 +66,8 @@
 MHCalibrationPixel::MHCalibrationPixel(const char *name, const char *title)
       : fPixId(-1),
-        fChargeNbinsHiGain(2100),
-        fChargeNbinsLoGain(1010),
-        fTimeNbins(32),
-        fChargevsNbins(5000),
-        fTimeFirst(-0.25),
-        fTimeLast(15.75),
 	fHivsLoGain(NULL),
         fHPSD(NULL),
         fChargeGausFit(NULL),
-	fTimeGausFit(NULL), 
+	fRelTimeGausFit(NULL), 
 	fFitLegend(NULL)
 { 
@@ -92,14 +96,27 @@
     fHChargeLoGain->Sumw2();
 
-    fHTimeHiGain = new TH1F("HTimeHiGain","Distribution of Mean Arrival Hi Gain Times Pixel ",
-			    fTimeNbins,fTimeFirst,fTimeLast);
-    fHTimeLoGain = new TH1F("HTimeLoGain","Distribution of Mean Arrival Lo Gain Times Pixel ",
-			    fTimeNbins,fTimeFirst,fTimeLast);
-
-    fHTimeHiGain->SetXTitle("Mean Arrival Times [Hi Gain FADC slice nr]");
-    fHTimeLoGain->SetXTitle("Mean Arrival Times [Lo Gain FADC slice nr]");
-
-    fHTimeHiGain->SetYTitle("Nr. of events");
-    fHTimeLoGain->SetYTitle("Nr. of events");
+    // Absolute Times
+    fHAbsTimeHiGain = new TH1F("HAbsTimeHiGain","Distribution of Absolute Arrival Hi Gain Times Pixel ",
+                               fAbsTimeNbins,fAbsTimeFirst,fAbsTimeLast);
+    fHAbsTimeLoGain = new TH1F("HAbsTimeLoGain","Distribution of Absolute Arrival Lo Gain Times Pixel ",
+                               fAbsTimeNbins,fAbsTimeFirst,fAbsTimeLast);
+
+    fHAbsTimeHiGain->SetXTitle("Absolute Arrival Time [Hi Gain FADC slice nr]");
+    fHAbsTimeLoGain->SetXTitle("Absolute Arrival Time [Lo Gain FADC slice nr]");
+
+    fHAbsTimeHiGain->SetYTitle("Nr. of events");
+    fHAbsTimeLoGain->SetYTitle("Nr. of events");
+
+    // Relative Times
+    fHRelTimeHiGain = new TH1F("HRelTimeHiGain","Distribution of Relative Arrival Times High Gain Pixel ",
+                               fRelTimeNbins,fRelTimeFirst,fRelTimeLast);
+    fHRelTimeLoGain = new TH1F("HRelTimeLoGain","Distribution of Relative Arrival Time Low Gain Pixel ",
+                               fRelTimeNbins,fRelTimeFirst,fRelTimeLast);
+
+    fHRelTimeHiGain->SetXTitle("Relative Arrival Times [Hi Gain FADC slice nr]");
+    fHRelTimeLoGain->SetXTitle("Relative Arrival Times [Lo Gain FADC slice nr]");
+
+    fHRelTimeHiGain->SetYTitle("Nr. of events");
+    fHRelTimeLoGain->SetYTitle("Nr. of events");
 
     // We define a reasonable number and later enlarge it if necessary
@@ -117,6 +134,8 @@
     fHChargeHiGain->SetDirectory(NULL);
     fHChargeLoGain->SetDirectory(NULL);
-    fHTimeHiGain->SetDirectory(NULL);
-    fHTimeLoGain->SetDirectory(NULL);
+    fHAbsTimeHiGain->SetDirectory(NULL);
+    fHAbsTimeLoGain->SetDirectory(NULL);
+    fHRelTimeHiGain->SetDirectory(NULL);
+    fHRelTimeLoGain->SetDirectory(NULL);
     fHChargevsNHiGain->SetDirectory(NULL);
     fHChargevsNLoGain->SetDirectory(NULL);
@@ -133,9 +152,11 @@
 
   delete fHChargeHiGain;
-  delete fHTimeHiGain;
+  delete fHAbsTimeHiGain;
+  delete fHRelTimeHiGain;
   delete fHChargevsNHiGain;
 
   delete fHChargeLoGain;
-  delete fHTimeLoGain;
+  delete fHAbsTimeLoGain;
+  delete fHRelTimeLoGain;
   delete fHChargevsNLoGain;
 
@@ -145,6 +166,6 @@
   if (fChargeGausFit)
     delete fChargeGausFit;
-  if (fTimeGausFit)
-    delete fTimeGausFit;
+  if (fRelTimeGausFit)
+    delete fRelTimeGausFit;
   if (fFitLegend)
     delete fFitLegend;
@@ -168,14 +189,20 @@
   fChargeNdf               = -1;
 
-  fTimeChisquare           = -1.;
-  fTimeProb                = -1.;
-  fTimeNdf                 = -1;
-  fTimeMean                = -1.;
-  fTimeSigma               = -1.;
-
-  fTimeLowerFitRangeHiGain = 0;
-  fTimeUpperFitRangeHiGain = 0;
-  fTimeLowerFitRangeLoGain = 0;
-  fTimeUpperFitRangeLoGain = 0;
+  fRelTimeChisquare        = -1.;
+  fRelTimeProb             = -1.;
+  fRelTimeNdf              = -1;
+  fRelTimeMean             = -1.;
+  fRelTimeSigma            = -1.;
+
+  fRelTimeLowerFitRangeHiGain = -99.;
+  fRelTimeUpperFitRangeHiGain = -99.;
+  fRelTimeLowerFitRangeLoGain = -99.;
+  fRelTimeUpperFitRangeLoGain = -99.;
+
+  fAbsTimeFirstHiGain     = -1.;
+  fAbsTimeFirstLoGain     = -1.;
+  fAbsTimeLastHiGain      = -1.;
+  fAbsTimeLastLoGain      = -1.;
+  
 
   fOffset = 0.;
@@ -184,6 +211,6 @@
   if (fChargeGausFit)
     delete fChargeGausFit;
-  if (fTimeGausFit)
-    delete fTimeGausFit;
+  if (fRelTimeGausFit)
+    delete fRelTimeGausFit;
   if (fFitLegend)
     delete fFitLegend;
@@ -193,4 +220,8 @@
     delete fHPSD;
 
+  CLRBIT(fFlags,kUseLoGain);
+  CLRBIT(fFlags,kChargeFitOK);
+  CLRBIT(fFlags,kTimeFitOK);
+
   return;
 }
@@ -204,6 +235,8 @@
   fHChargeHiGain->Reset();
   fHChargeLoGain->Reset();
-  fHTimeHiGain->Reset();
-  fHTimeLoGain->Reset();
+  fHAbsTimeHiGain->Reset();
+  fHAbsTimeLoGain->Reset();
+  fHRelTimeHiGain->Reset();
+  fHRelTimeLoGain->Reset();
   fHChargevsNHiGain->Reset();
   fHChargevsNLoGain->Reset();
@@ -214,4 +247,11 @@
 }
 
+void MHCalibrationPixel::SetUseLoGain(Bool_t b)
+{
+  if (b)
+    SETBIT(fFlags, kUseLoGain) ;
+  else
+    CLRBIT(fFlags, kUseLoGain);
+}
 
 
@@ -226,7 +266,12 @@
 }
 
-Bool_t MHCalibrationPixel::IsFitOK() const 
-{
-    return TESTBIT(fFlags,kFitOK);
+Bool_t MHCalibrationPixel::IsChargeFitOK() const 
+{
+    return TESTBIT(fFlags,kChargeFitOK);
+}
+
+Bool_t MHCalibrationPixel::IsTimeFitOK() const 
+{
+    return TESTBIT(fFlags,kTimeFitOK);
 }
 
@@ -236,7 +281,12 @@
 }
 
-Bool_t MHCalibrationPixel::FillTimeLoGain(Float_t t)
-{
-    return (fHTimeLoGain->Fill(t)   > -1);
+Bool_t MHCalibrationPixel::FillAbsTimeLoGain(Float_t t)
+{
+    return (fHAbsTimeLoGain->Fill(t)   > -1);
+}
+
+Bool_t MHCalibrationPixel::FillRelTimeLoGain(Float_t t)
+{
+    return (fHRelTimeLoGain->Fill(t)   > -1);
 }
 
@@ -251,7 +301,12 @@
 }
 
-Bool_t MHCalibrationPixel::FillTimeHiGain(Float_t t)
-{
-    return (fHTimeHiGain->Fill(t) > -1);
+Bool_t MHCalibrationPixel::FillAbsTimeHiGain(Float_t t)
+{
+    return (fHAbsTimeHiGain->Fill(t) > -1);
+}
+
+Bool_t MHCalibrationPixel::FillRelTimeHiGain(Float_t t)
+{
+    return (fHRelTimeHiGain->Fill(t) > -1);
 }
 
@@ -273,7 +328,11 @@
   fHChargeHiGain->SetName(nameQHiGain.Data());
 
-  TString nameTHiGain = TString(fHTimeHiGain->GetName());
-  nameTHiGain += id;
-  fHTimeHiGain->SetName(nameTHiGain.Data());
+  TString nameTAHiGain = TString(fHAbsTimeHiGain->GetName());
+  nameTAHiGain += id;
+  fHAbsTimeHiGain->SetName(nameTAHiGain.Data());
+
+  TString nameTRHiGain = TString(fHRelTimeHiGain->GetName());
+  nameTRHiGain += id;
+  fHRelTimeHiGain->SetName(nameTRHiGain.Data());
 
   TString nameQvsNHiGain  = TString(fHChargevsNHiGain->GetName());
@@ -288,7 +347,11 @@
   fHChargeHiGain->SetTitle(titleQHiGain.Data());
 
-  TString titleTHiGain = TString(fHTimeHiGain->GetTitle());
-  titleTHiGain += id;
-  fHTimeHiGain->SetTitle(titleTHiGain.Data());
+  TString titleTAHiGain = TString(fHAbsTimeHiGain->GetTitle());
+  titleTAHiGain += id;
+  fHAbsTimeHiGain->SetTitle(titleTAHiGain.Data());
+
+  TString titleTRHiGain = TString(fHRelTimeHiGain->GetTitle());
+  titleTRHiGain += id;
+  fHRelTimeHiGain->SetTitle(titleTRHiGain.Data());
 
   TString titleQvsNHiGain  = TString(fHChargevsNHiGain->GetTitle());
@@ -303,7 +366,11 @@
   fHChargeLoGain->SetName(nameQLoGain.Data());
 
-  TString nameTLoGain = TString(fHTimeLoGain->GetName());
-  nameTLoGain += id;
-  fHTimeLoGain->SetName(nameTLoGain.Data());
+  TString nameTALoGain = TString(fHAbsTimeLoGain->GetName());
+  nameTALoGain += id;
+  fHAbsTimeLoGain->SetName(nameTALoGain.Data());
+
+  TString nameTRLoGain = TString(fHRelTimeLoGain->GetName());
+  nameTRLoGain += id;
+  fHRelTimeLoGain->SetName(nameTRLoGain.Data());
 
   TString nameQvsNLoGain  = TString(fHChargevsNLoGain->GetName());
@@ -318,7 +385,11 @@
   fHChargeLoGain->SetTitle(titleQLoGain.Data());
 
-  TString titleTLoGain = TString(fHTimeLoGain->GetTitle());
-  titleTLoGain += id;
-  fHTimeLoGain->SetTitle(titleTLoGain.Data());
+  TString titleTALoGain = TString(fHAbsTimeLoGain->GetTitle());
+  titleTALoGain += id;
+  fHAbsTimeLoGain->SetTitle(titleTALoGain.Data());
+
+  TString titleTRLoGain = TString(fHRelTimeLoGain->GetTitle());
+  titleTRLoGain += id;
+  fHRelTimeLoGain->SetTitle(titleTRLoGain.Data());
 
   TString titleQvsNLoGain  = TString(fHChargevsNLoGain->GetTitle());
@@ -375,5 +446,5 @@
   fFitLegend = new TPaveText(0.05,0.05,0.95,0.95);
 
-  if (IsFitOK()) 
+  if (IsChargeFitOK()) 
       fFitLegend->SetFillColor(80);
   else
@@ -403,5 +474,5 @@
   t4->SetBit(kCanDelete);
 
-  if (IsFitOK())
+  if (IsChargeFitOK())
     {
       TText *t5 = fFitLegend->AddText("Result of the Fit: OK");
@@ -459,5 +530,5 @@
   TCanvas *c = MakeDefCanvas(this,600,900); 
   
-  c->Divide(2,4);
+  c->Divide(2,5);
   
   c->cd(1);
@@ -490,5 +561,5 @@
       if (fChargeGausFit)
         {
-          if (IsFitOK())
+          if (IsChargeFitOK())
             fChargeGausFit->SetLineColor(kGreen);          
           else
@@ -516,5 +587,5 @@
       if (fChargeGausFit)
         {
-          if (IsFitOK())
+          if (IsChargeFitOK())
             fChargeGausFit->SetLineColor(kGreen);          
           else
@@ -557,5 +628,5 @@
   gPad->SetTicks();  
   gPad->SetLogy(0);
-  fHTimeHiGain->Draw(opt);
+  fHRelTimeHiGain->Draw(opt);
   c->Modified();
   c->Update();
@@ -567,16 +638,16 @@
         gPad->SetTicks();
 	gPad->SetLogy(0);
-	fHTimeLoGain->Draw(opt);
+	fHRelTimeLoGain->Draw(opt);
 	c->Modified();
 	c->Update();
 	
-	if (fTimeGausFit)
+	if (fRelTimeGausFit)
 	  {
-	    if (fTimeChisquare > 20.) 
-	      fTimeGausFit->SetLineColor(kRed);
+	    if (fRelTimeChisquare > 20.) 
+	      fRelTimeGausFit->SetLineColor(kRed);
 	    else
-	      fTimeGausFit->SetLineColor(kGreen);
+	      fRelTimeGausFit->SetLineColor(kGreen);
 	    
-	    fTimeGausFit->Draw("same");
+	    fRelTimeGausFit->Draw("same");
 	    c->Modified();
 	    c->Update();
@@ -585,12 +656,12 @@
     else
       {  
-	if (fTimeGausFit)
+	if (fRelTimeGausFit)
 	  {
-	    if (fTimeChisquare > 20.) 
-	      fTimeGausFit->SetLineColor(kRed);
+	    if (fRelTimeChisquare > 20.) 
+	      fRelTimeGausFit->SetLineColor(kRed);
 	    else
-	      fTimeGausFit->SetLineColor(kGreen);
+	      fRelTimeGausFit->SetLineColor(kGreen);
 	    
-	    fTimeGausFit->Draw("same");
+	    fRelTimeGausFit->Draw("same");
 	    c->Modified();
 	    c->Update();
@@ -600,5 +671,5 @@
         gPad->SetTicks();
 	gPad->SetLogy(0);
-	fHTimeLoGain->Draw(opt);	
+	fHRelTimeLoGain->Draw(opt);	
 	c->Modified();
 	c->Update();
@@ -610,9 +681,22 @@
     c->cd(7);
     gPad->SetTicks();
+    fHAbsTimeHiGain->Draw(opt);
+    c->Modified();
+    c->Update();
+
+    c->cd(8);
+    gPad->SetTicks();
+    fHAbsTimeLoGain->Draw(opt);
+    c->Modified();
+    c->Update();
+
+
+    c->cd(9);
+    gPad->SetTicks();
     fHChargevsNHiGain->Draw(opt);
     c->Modified();
     c->Update();
 
-    c->cd(8);
+    c->cd(10);
     gPad->SetTicks();
     fHChargevsNLoGain->Draw(opt);
@@ -656,55 +740,82 @@
 
 
-Bool_t MHCalibrationPixel::FitTimeHiGain(Axis_t rmin, Axis_t rmax, Option_t *option)
-{
-
-  if (fTimeGausFit)
+Bool_t MHCalibrationPixel::FitTime(Option_t *option)
+{
+
+  if (fRelTimeGausFit)
     return kFALSE;
 
-  rmin = (rmin != 0.) ? rmin : (Axis_t)fTimeLowerFitRangeHiGain;
-  rmax = (rmax != 0.) ? rmax : (Axis_t)fTimeUpperFitRangeHiGain;
-
-  const Stat_t entries       = fHTimeHiGain->Integral();
-  const Double_t mu_guess    = fHTimeHiGain->GetBinCenter(fHTimeHiGain->GetMaximumBin());
+  //
+  // Get the fitting ranges
+  //
+  Axis_t rmin = fRelTimeLowerFitRangeHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    rmin = fRelTimeLowerFitRangeLoGain;
+
+  Axis_t rmax = fRelTimeUpperFitRangeHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    rmin = fRelTimeUpperFitRangeLoGain;
+
+  TH1F *hist = fHRelTimeHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    hist = fHRelTimeLoGain;
+
+  const Stat_t   entries     = hist->Integral("width");
+  const Double_t mu_guess    = hist->GetBinCenter(hist->GetMaximumBin());
   const Double_t sigma_guess = (rmax - rmin)/2.;
-  const Double_t area_guess  = entries/gkSq2Pi;
-
-  TString name = TString("GausTime");
+  const Double_t area_guess  = 2.*entries/gkSq2Pi/sigma_guess;
+
+  TString name = TString("GausRelTime");
   name += fPixId;
-  fTimeGausFit = new TF1(name.Data(),"gaus",rmin,rmax);  
-
-  if (!fTimeGausFit) 
-    {
-    *fLog << warn << dbginf << "WARNING: Could not create fit function for Time fit" << endl;
+  fRelTimeGausFit = new TF1(name.Data(),"gaus",rmin,rmax);  
+
+  if (!fRelTimeGausFit) 
+    {
+    *fLog << warn << dbginf << "WARNING: Could not create fit function for RelTime fit" << endl;
     return kFALSE;
     }
 
-  fTimeGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
-  fTimeGausFit->SetParNames("Area","#mu","#sigma");
-  fTimeGausFit->SetParLimits(0,0.,entries);
-  fTimeGausFit->SetParLimits(1,rmin,rmax);
-  fTimeGausFit->SetParLimits(2,0.,(rmax-rmin));
-  fTimeGausFit->SetRange(rmin,rmax);
-
-  fHTimeHiGain->Fit(fTimeGausFit,option);
-
-  rmin = fTimeGausFit->GetParameter(1) - 3.*fTimeGausFit->GetParameter(2);
-  rmax = fTimeGausFit->GetParameter(1) + 3.*fTimeGausFit->GetParameter(2);
-  fTimeGausFit->SetRange(rmin,rmax);  
-
-  fHTimeHiGain->Fit(fTimeGausFit,option);
-
-  fTimeChisquare = fTimeGausFit->GetChisquare();
-  fTimeNdf       = fTimeGausFit->GetNDF();
-  fTimeProb      = fTimeGausFit->GetProb();
-
-  fTimeMean      = fTimeGausFit->GetParameter(1);
-  fTimeSigma     = fTimeGausFit->GetParameter(2);
-
-  if (TMath::IsNaN(fTimeMean) || TMath::IsNaN(fTimeSigma))
-    return kFALSE;
-
-  if (TMath::IsNaN(fTimeChisquare) || fTimeChisquare > 20.)  // Cannot use Probability because Ndf is sometimes < 1
-    return kFALSE;
+  fRelTimeGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
+  fRelTimeGausFit->SetParNames("Area","#mu","#sigma");
+  fRelTimeGausFit->SetParLimits(0,0.,5.*area_guess);
+  fRelTimeGausFit->SetParLimits(1,rmin,rmax);
+  fRelTimeGausFit->SetParLimits(2,0.,(rmax-rmin));
+  fRelTimeGausFit->SetRange(rmin,rmax);
+
+  hist->Fit(fRelTimeGausFit,option);
+
+  //
+  // If the fit does not converge, try another one with smaller bounderies
+  //
+  if (fRelTimeGausFit->GetProb() < 0.001)
+    {
+      rmin = fRelTimeGausFit->GetParameter(1) - 2.*fRelTimeGausFit->GetParameter(2);
+      rmax = fRelTimeGausFit->GetParameter(1) + 2.*fRelTimeGausFit->GetParameter(2);
+      fRelTimeGausFit->SetRange(rmin,rmax);  
+      hist->Fit(fRelTimeGausFit,option);
+    }
+  
+  fRelTimeChisquare = fRelTimeGausFit->GetChisquare();
+  fRelTimeNdf       = fRelTimeGausFit->GetNDF();
+  fRelTimeProb      = fRelTimeGausFit->GetProb();
+
+  fRelTimeMean      = fRelTimeGausFit->GetParameter(1);
+  fRelTimeSigma     = fRelTimeGausFit->GetParameter(2);
+
+  fRelTimeMeanErr   = fRelTimeGausFit->GetParError(1);
+
+  if (TMath::IsNaN(fRelTimeMean) || TMath::IsNaN(fRelTimeSigma))
+    {
+      CLRBIT(fFlags,kTimeFitOK);
+      return kFALSE;
+    }
+  
+  if (TMath::IsNaN(fRelTimeChisquare) || (fRelTimeProb < gkProbLimit))
+    {
+      CLRBIT(fFlags,kTimeFitOK);
+      return kFALSE;
+    }
+  
+  SETBIT(fFlags,kTimeFitOK);
 
   return kTRUE;
@@ -712,59 +823,4 @@
 }
 
-Bool_t MHCalibrationPixel::FitTimeLoGain(Axis_t rmin, Axis_t rmax, Option_t *option)
-{
-
-  if (fTimeGausFit)
-    return kFALSE;
-
-  rmin = (rmin != 0.) ? rmin : (Axis_t)fTimeLowerFitRangeLoGain;
-  rmax = (rmax != 0.) ? rmax : (Axis_t)fTimeUpperFitRangeLoGain;
-
-  const Stat_t   entries     = fHTimeLoGain->Integral();
-  const Double_t mu_guess    = fHTimeLoGain->GetBinCenter(fHTimeLoGain->GetMaximumBin());
-  const Double_t sigma_guess = (rmax - rmin)/2.;
-  const Double_t area_guess  = entries/gkSq2Pi;
-
-  TString name = TString("GausTime");
-  name += fPixId;
-  fTimeGausFit = new TF1(name.Data(),"gaus",rmin,rmax);  
-
-  if (!fTimeGausFit) 
-    {
-    *fLog << warn << dbginf << "WARNING: Could not create fit function for Time fit" << endl;
-    return kFALSE;
-    }
-
-  fTimeGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
-  fTimeGausFit->SetParNames("Area","#mu","#sigma");
-  fTimeGausFit->SetParLimits(0,0.,entries);
-  fTimeGausFit->SetParLimits(1,rmin,rmax);
-  fTimeGausFit->SetParLimits(2,0.,(rmax-rmin));
-  fTimeGausFit->SetRange(rmin,rmax);
-
-  fHTimeLoGain->Fit(fTimeGausFit,option);
-
-  rmin = fTimeGausFit->GetParameter(1) - 3.*fTimeGausFit->GetParameter(2);
-  rmax = fTimeGausFit->GetParameter(1) + 3.*fTimeGausFit->GetParameter(2);
-  fTimeGausFit->SetRange(rmin,rmax);  
-
-  fHTimeLoGain->Fit(fTimeGausFit,option);
-
-  fTimeChisquare = fTimeGausFit->GetChisquare();
-  fTimeNdf       = fTimeGausFit->GetNDF();
-  fTimeProb      = fTimeGausFit->GetProb();
-
-  fTimeMean      = fTimeGausFit->GetParameter(1);
-  fTimeSigma     = fTimeGausFit->GetParameter(2);
-
-  if (fTimeChisquare > 20.)  // Cannot use Probability because Ndf is sometimes < 1
-    {
-      *fLog << warn << "WARNING: Fit of the Arrival times failed ! " << endl;
-      return kFALSE;
-    }
-  
-  return kTRUE;
-
-}
 
 Bool_t MHCalibrationPixel::FitCharge(Option_t *option)
@@ -783,5 +839,5 @@
   Axis_t rmax = fChargeLastHiGain;
   if (TESTBIT(fFlags,kUseLoGain))
-    rmin = fChargeFirstLoGain;
+    rmax = fChargeLastLoGain;
 
   TH1F *hist = fHChargeHiGain;
@@ -847,5 +903,5 @@
   if (TMath::IsNaN(fChargeMean) || TMath::IsNaN(fChargeMeanErr))
     {
-      CLRBIT(fFlags,kFitOK);
+      CLRBIT(fFlags,kChargeFitOK);
       return kFALSE;
     }
@@ -853,9 +909,9 @@
   if ((fChargeProb < gkProbLimit) || (TMath::IsNaN(fChargeProb)))
     {
-      CLRBIT(fFlags,kFitOK);
+      CLRBIT(fFlags,kChargeFitOK);
       return kFALSE;
     }
   
-  SETBIT(fFlags,kFitOK);
+  SETBIT(fFlags,kChargeFitOK);
   return kTRUE;
 }
@@ -880,4 +936,28 @@
   fChargeLastLoGain  = fHChargeLoGain->GetBinLowEdge(fHChargeLoGain->GetXaxis()->GetLast())
                       +fHChargeLoGain->GetBinWidth(0);
+
+  CutEdges(fHRelTimeHiGain,nbins);
+
+  fRelTimeLowerFitRangeHiGain = fHRelTimeHiGain->GetBinLowEdge(fHRelTimeHiGain->GetXaxis()->GetFirst());
+  fRelTimeUpperFitRangeHiGain = fHRelTimeHiGain->GetBinLowEdge(fHRelTimeHiGain->GetXaxis()->GetLast())
+                               +fHRelTimeHiGain->GetBinWidth(0);
+
+  CutEdges(fHRelTimeLoGain,nbins);
+
+  fRelTimeLowerFitRangeLoGain = fHRelTimeLoGain->GetBinLowEdge(fHRelTimeLoGain->GetXaxis()->GetFirst());
+  fRelTimeUpperFitRangeLoGain = fHRelTimeLoGain->GetBinLowEdge(fHRelTimeLoGain->GetXaxis()->GetLast())
+                               +fHRelTimeLoGain->GetBinWidth(0);
+
+  CutEdges(fHAbsTimeHiGain,nbins);
+
+  fAbsTimeFirstHiGain = fHAbsTimeHiGain->GetBinLowEdge(fHAbsTimeHiGain->GetXaxis()->GetFirst());
+  fAbsTimeLastHiGain  = fHAbsTimeHiGain->GetBinLowEdge(fHAbsTimeHiGain->GetXaxis()->GetLast())
+                       +fHAbsTimeHiGain->GetBinWidth(0);
+
+  CutEdges(fHAbsTimeLoGain,nbins);
+
+  fAbsTimeFirstLoGain = fHAbsTimeLoGain->GetBinLowEdge(fHAbsTimeLoGain->GetXaxis()->GetFirst());
+  fAbsTimeLastLoGain  = fHAbsTimeLoGain->GetBinLowEdge(fHAbsTimeLoGain->GetXaxis()->GetLast())
+                       +fHAbsTimeLoGain->GetBinWidth(0);
 
   CutEdges(fHChargevsNHiGain,0);
@@ -901,7 +981,7 @@
 
   *fLog << all << "Results of the Time Slices Fit: "                        << endl;
-  *fLog << all << "Chisquare: "   << fTimeChisquare                         << endl;
-  *fLog << all << "Ndf: "         << fTimeNdf                               << endl;
-  *fLog << all << "Probability: " << fTimeProb                              << endl;
+  *fLog << all << "Chisquare: "   << fRelTimeChisquare                      << endl;
+  *fLog << all << "Ndf: "         << fRelTimeNdf                            << endl;
+  *fLog << all << "Probability: " << fRelTimeProb                           << endl;
   *fLog << all                                                              << endl;
 
