Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2921)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2922)
@@ -45,4 +45,9 @@
        fit 
 
+   * mcalib/MHCalibrationPixel.[h,cc]:
+   * mcalib/MCalibrationPix.cc:
+   * mcalib/MCalibrationPINDiode.cc:
+     - combine FitChargeHiGain and FitChargeLoGain into one function
+
 
  2004/01/26: Thomas Bretz
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc	(revision 2921)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc	(revision 2922)
@@ -81,5 +81,5 @@
 Bool_t MCalibrationPINDiode::FitCharge() 
 {
-  if(!fHist->FitChargeHiGain())
+  if(!fHist->FitCharge())
     return kFALSE;
 
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc	(revision 2921)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc	(revision 2922)
@@ -356,50 +356,31 @@
   //    possible remaining cosmics to spoil the fit.
   //
-  if (fPed && fPedRms)
-    fHist->SetLowerFitRange(1.5*fPedRms);
+  //  if (fPed && fPedRms)
+  //    fHist->SetLowerFitRange(1.5*fPedRms);
+  //  else
+  //    *fLog << warn << "WARNING: Cannot set lower fit range: Pedestals not available" << endl;
+
+  //
+  // 3) Decide if the LoGain Histogram is fitted or the HiGain Histogram
+  //
+  if (fHist->UseLoGain())
+    SetHiGainSaturation();
+      
+  //
+  // 4) Fit the Lo Gain histograms with a Gaussian
+  //
+  if(fHist->FitCharge())
+    {
+      SETBIT(fFlags,kFitted);
+    }
   else
-    *fLog << warn << "WARNING: Cannot set lower fit range: Pedestals not available" << endl;
-
-  //
-  // 3) Decide if the LoGain Histogram is fitted or the HiGain Histogram
-  //
-  if (fHist->UseLoGain())
-    {
-
-      SetHiGainSaturation();
-      
-      //
-      // 4) Fit the Lo Gain histograms with a Gaussian
-      //
-      if(!fHist->FitChargeLoGain())
-	{
-	  *fLog << warn << "WARNING: Could not fit Lo Gain charges of pixel " << fPixId << endl;
-          //          
-          // 5) In case of failure print out the fit results
-          //
-          //	  fHist->PrintChargeFitResult();
-          CLRBIT(fFlags,kFitted);
-	}
-      else
-        SETBIT(fFlags,kFitted);
-    }
-  else 
-    {
-      //
-      // 4) Fit the Hi Gain histograms with a Gaussian
-      //
-      if(!fHist->FitChargeHiGain())
-	{
-	  *fLog << warn << "WARNING: Could not fit Hi Gain charges of pixel " << fPixId << endl;
-          //          
-          // 5) In case of failure print out the fit results
-          //
-          //	  fHist->PrintChargeFitResult();
-          CLRBIT(fFlags,kFitted);
-	}
-      else
-        SETBIT(fFlags,kFitted);
-    }
-  
+    {
+      *fLog << warn << "WARNING: Could not fit charges of pixel " << fPixId << endl;
+      //          
+      // 5) In case of failure print out the fit results
+      //
+      //	  fHist->PrintChargeFitResult();
+      CLRBIT(fFlags,kFitted);
+    }
 
   //
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc	(revision 2921)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc	(revision 2922)
@@ -541,5 +541,5 @@
   // The fit result is accepted under condition that: 
   // 1) the Probability is greater than gkProbLimit (default 0.001 == 99.7%)
-  // 2) at least 100 events are in the single Photo-electron peak
+  // 2) at least 50 events are in the single Photo-electron peak
   //
   if (fProb < gkProbLimit) 
@@ -551,10 +551,7 @@
     }
 
-  if (fProb < 0.01) 
-    *fLog << warn << "WARNING: Fit Probability " << fProb << " is smaller than 1% " << endl;
-
   Float_t contSinglePhe = TMath::Exp(-1.0*fLambda)*fLambda*entries;
   
-  if (contSinglePhe < 100.) 
+  if (contSinglePhe < 50.) 
     {
       *fLog << err << "ERROR: Statistics is too low: Only " << contSinglePhe 
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc	(revision 2921)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.cc	(revision 2922)
@@ -67,5 +67,4 @@
 	fHivsLoGain(NULL),
 	fFitLegend(NULL),
-	fLowerFitRange(-100.),
 	fChargeFirstHiGain(-100.5),
 	fChargeLastHiGain(1999.5),
@@ -112,5 +111,5 @@
     Axis_t tfirst = -0.5;
     Axis_t tlast  = 15.5;
-    Int_t  ntbins = 16;
+    Int_t  ntbins = 32;
 
     fHTimeHiGain = new TH1F("HTimeHiGain","Distribution of Mean Arrival Hi Gain Times Pixel ",
@@ -695,5 +694,8 @@
   fTimeSigma     = fTimeGausFit->GetParameter(2);
 
-  if (fTimeChisquare > 20.)  // Cannot use Probability because Ndf is sometimes < 1
+  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;
 
@@ -758,5 +760,5 @@
 }
 
-Bool_t MHCalibrationPixel::FitChargeHiGain(Option_t *option)
+Bool_t MHCalibrationPixel::FitCharge(Option_t *option)
 {
 
@@ -767,6 +769,15 @@
   // Get the fitting ranges
   //
-  Axis_t rmin = (fLowerFitRange != 0.) ? fLowerFitRange : fChargeFirstHiGain;
-  Axis_t rmax = 0.;
+  Axis_t rmin = fChargeFirstHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    rmin = fChargeFirstLoGain;
+
+  Axis_t rmax = fChargeLastHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    rmin = fChargeFirstLoGain;
+
+  TH1F *hist = fHChargeHiGain;
+  if (TESTBIT(fFlags,kUseLoGain))
+    hist = fHChargeLoGain;
 
   //
@@ -774,7 +785,7 @@
   // otherwise the fit goes gaga because of high number of dimensions ...
   //
-  const Stat_t   entries    = fHChargeHiGain->Integral();
-  const Double_t area_guess = entries/gkSq2Pi;
-  const Double_t mu_guess   = fHChargeHiGain->GetBinCenter(fHChargeHiGain->GetMaximumBin());
+  const Stat_t   entries     = hist->Integral();
+  const Double_t area_guess  = entries/gkSq2Pi;
+  const Double_t mu_guess    = hist->GetBinCenter(hist->GetMaximumBin());
   const Double_t sigma_guess = mu_guess/15.;
 
@@ -782,5 +793,5 @@
   name += fPixId;
 
-  fChargeGausFit = new TF1(name.Data(),"gaus",rmin,fChargeLastHiGain);
+  fChargeGausFit = new TF1(name.Data(),"gaus",rmin,rmax);
 
   if (!fChargeGausFit) 
@@ -793,18 +804,24 @@
   fChargeGausFit->SetParNames("Area","#mu","#sigma");
   fChargeGausFit->SetParLimits(0,0.,entries);
-  fChargeGausFit->SetParLimits(1,rmin,fChargeLastHiGain);
-  fChargeGausFit->SetParLimits(2,0.,fChargeLastHiGain-rmin);
-  fChargeGausFit->SetRange(rmin,fChargeLastHiGain);
-
-  fHChargeHiGain->Fit(fChargeGausFit,option);
-  
-  Axis_t rtry = fChargeGausFit->GetParameter(1) - 2.0*fChargeGausFit->GetParameter(2);
-  
-  rmin = (rtry < rmin ? rmin : rtry);
-  rmax = fChargeGausFit->GetParameter(1) + 3.5*fChargeGausFit->GetParameter(2);
-  fChargeGausFit->SetRange(rmin,rmax);  
-
-  fHChargeHiGain->Fit(fChargeGausFit,option);
-
+  fChargeGausFit->SetParLimits(1,rmin,rmax);
+  fChargeGausFit->SetParLimits(2,0.,rmax-rmin);
+  fChargeGausFit->SetRange(rmin,rmax);
+
+  hist->Fit(fChargeGausFit,option);
+  
+  // 
+  // If we are not able to fit, try once again
+  //
+  if (fChargeGausFit->GetProb() < gkProbLimit)
+    {
+
+      Axis_t rtry = fChargeGausFit->GetParameter(1) - 3.0*fChargeGausFit->GetParameter(2);
+      rmin        = (rtry < rmin ? rmin : rtry);
+      rmax        = fChargeGausFit->GetParameter(1) + 3.0*fChargeGausFit->GetParameter(2);
+      fChargeGausFit->SetRange(rmin,rmax);  
+
+      fHChargeHiGain->Fit(fChargeGausFit,option);
+    }
+  
   fChargeChisquare = fChargeGausFit->GetChisquare();
   fChargeNdf       = fChargeGausFit->GetNDF();
@@ -816,15 +833,20 @@
 
   //
-  // The fit result is accepted under condition
+  // The fit result is accepted under condition:
+  // The Results are not nan's
   // The Probability is greater than gkProbLimit (default 0.001 == 99.9%)
   //
-  if (fChargeProb < gkProbLimit) 
-    {
-      //      *fLog << warn << "WARNING: Fit Probability " << fChargeProb 
-      //            << " is smaller than the allowed value: " << gkProbLimit << endl;
+  if (TMath::IsNaN(fChargeMean) || TMath::IsNaN(fChargeMeanErr))
+    {
       CLRBIT(fFlags,kFitOK);
       return kFALSE;
     }
   
+  if ((fChargeProb < gkProbLimit) || (TMath::IsNaN(fChargeProb)))
+    {
+      CLRBIT(fFlags,kFitOK);
+      return kFALSE;
+    }
+  
   SETBIT(fFlags,kFitOK);
   return kTRUE;
@@ -832,83 +854,4 @@
 
 
-Bool_t MHCalibrationPixel::FitChargeLoGain(Option_t *option)
-{
-
-  if (fChargeGausFit)
-    return kFALSE;
-
-  //
-  // Get the fitting ranges
-  //
-  Axis_t rmin = (fLowerFitRange != 0.) ? fLowerFitRange : fChargeFirstLoGain;
-  Axis_t rmax = 0.;
-
-  //
-  // First guesses for the fit (should be as close to reality as possible, 
-  // otherwise the fit goes gaga because of high number of dimensions ...
-  //
-  const Stat_t   entries    = fHChargeLoGain->Integral();
-  const Double_t area_guess = entries/gkSq2Pi;
-  const Double_t mu_guess   = fHChargeLoGain->GetBinCenter(fHChargeLoGain->GetMaximumBin());
-  const Double_t sigma_guess = mu_guess/15.;
-
-  TString name = TString("ChargeGausFit");
-  name += fPixId;
-
-  fChargeGausFit = new TF1(name.Data(),"gaus",rmin,fChargeLastLoGain);
-
-  if (!fChargeGausFit) 
-    {
-    *fLog << warn << dbginf << "WARNING: Could not create fit function for Charges fit" << endl;
-    return kFALSE;
-    }
-  
-  fChargeGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
-  fChargeGausFit->SetParNames("Area","#mu","#sigma");
-  fChargeGausFit->SetParLimits(0,0.,entries);
-  fChargeGausFit->SetParLimits(1,rmin,fChargeLastLoGain);
-  fChargeGausFit->SetParLimits(2,0.,fChargeLastLoGain-rmin);
-  fChargeGausFit->SetRange(rmin,fChargeLastLoGain);
-
-  fHChargeLoGain->Fit(fChargeGausFit,option);
-  
-  Axis_t rtry = fChargeGausFit->GetParameter(1) - 2.*fChargeGausFit->GetParameter(2);
-  
-  rmin = (rtry < rmin ? rmin : rtry);
-  rmax = fChargeGausFit->GetParameter(1) + 3.5*fChargeGausFit->GetParameter(2);
-  fChargeGausFit->SetRange(rmin,rmax);  
-
-  fHChargeLoGain->Fit(fChargeGausFit,option);
-
-  //  rmin = fChargeGausFit->GetParameter(1) - 2.5*fChargeGausFit->GetParameter(2);
-  //  rmax = fChargeGausFit->GetParameter(1) + 2.5*fChargeGausFit->GetParameter(2);
-  //  fChargeGausFit->SetRange(rmin,rmax);  
-
-  // fHChargeLoGain->Fit(fChargeGausFit,option);
-
-  fChargeChisquare = fChargeGausFit->GetChisquare();
-  fChargeNdf       = fChargeGausFit->GetNDF();
-  fChargeProb      = fChargeGausFit->GetProb();
-  fChargeMean      = fChargeGausFit->GetParameter(1);
-  fChargeMeanErr   = fChargeGausFit->GetParError(1);
-  fChargeSigma     = fChargeGausFit->GetParameter(2);
-  fChargeSigmaErr  = fChargeGausFit->GetParError(2);
-
-  //
-  // The fit result is accepted under condition
-  // The Probability is greater than gkProbLimit (default 0.01 == 99%)
-  //
-  if (fChargeProb < gkProbLimit) 
-    {
-      //      *fLog << warn << "WARNING: Fit Probability " << fChargeProb 
-      //            << " is smaller than the allowed value: " << gkProbLimit << endl;
-      CLRBIT(fFlags,kFitOK);
-      return kFALSE;
-    }
-  
-  SETBIT(fFlags,kFitOK);
-    
-  return kTRUE;
-}
 
  
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h	(revision 2921)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h	(revision 2922)
@@ -41,5 +41,4 @@
   TPaveText *fFitLegend;  
   
-  Axis_t  fLowerFitRange;
   Axis_t  fChargeFirstHiGain;
   Axis_t  fChargeLastHiGain;
@@ -95,6 +94,4 @@
   void SetTimeFitRangesLoGain(Byte_t low, Byte_t up) { fTimeLowerFitRangeLoGain = low,
                                                fTimeUpperFitRangeLoGain = up ;  }
-
-  void SetLowerFitRange(Axis_t min)                {  fLowerFitRange = min; }
 
   // Getters
@@ -152,8 +149,7 @@
   
   // Fits
-  Bool_t FitChargeHiGain(Option_t *option="RQ0");  
+  Bool_t FitCharge(Option_t *option="RQ0");  
+
   Bool_t FitTimeHiGain(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0");    
-
-  Bool_t FitChargeLoGain(Option_t *option="RQ0");  
   Bool_t FitTimeLoGain(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0");    
 
