Index: trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc	(revision 6969)
+++ trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc	(revision 7005)
@@ -52,4 +52,13 @@
 // The flag kAverageing steers if the event-by-event averages are treated at all.
 //
+// Class Version 5:
+//  + Double_t fLowerFitLimitHiGain;          // Lower limit for the fit range for the hi-gain hist
+//  + Double_t fUpperFitLimitHiGain;          // Upper limit for the fit range for the hi-gain hist
+//  + Double_t fLowerFitLimitLoGain;          // Lower limit for the fit range for the lo-gain hist
+//  + Double_t fUpperFitLimitLoGain;          // Upper limit for the fit range for the lo-gain hist
+//  + Bool_t   fIsHiGainFitRanges;            // Are high-gain fit ranges defined?
+//  + Bool_t   fIsLoGainFitRanges;            // Are low-gain fit ranges defined?
+//
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MHCalibrationCam.h"
@@ -86,11 +95,18 @@
 using namespace std;
 
+const Double_t MHCalibrationCam::fgLowerFitLimitHiGain = 0;
+const Double_t MHCalibrationCam::fgUpperFitLimitHiGain = 0;
+const Double_t MHCalibrationCam::fgLowerFitLimitLoGain = 0;
+const Double_t MHCalibrationCam::fgUpperFitLimitLoGain = 0;
+
 const Int_t   MHCalibrationCam::fgPulserFrequency  = 500;
 const Float_t MHCalibrationCam::fgProbLimit        = 0.0001;
 const Float_t MHCalibrationCam::fgOverflowLimit    = 0.005;
+
 const TString MHCalibrationCam::gsHistName   = "Hist";
 const TString MHCalibrationCam::gsHistTitle  = "";
 const TString MHCalibrationCam::gsHistXTitle = "";
 const TString MHCalibrationCam::gsHistYTitle = "Nr. events";
+
 // --------------------------------------------------------------------------
 //
@@ -116,11 +132,16 @@
 //-  SetSizeCheck   (kTRUE);
 //-  SetInterlaced  (kFALSE);
+//-  SetLowerFitLimitHiGain();
+//-  SetUpperFitLimitHiGain();
+//-  SetLowerFitLimitLoGain();
+//-  SetUpperFitLimitLoGain();
 //
 MHCalibrationCam::MHCalibrationCam(const char *name, const char *title)
-    :  fHistName(gsHistName),fHistTitle(gsHistTitle),
-       fHistXTitle(gsHistXTitle),fHistYTitle(gsHistYTitle), 
-       fColor(MCalibrationCam::kNONE), fIntensBad(NULL),
-       fBadPixels(NULL), fIntensCam(NULL), fCam(NULL), fGeom(NULL), 
-       fRunHeader(NULL)
+    :  fIsHiGainFitRanges(kFALSE), fIsLoGainFitRanges(kFALSE),
+    fHistName(gsHistName),fHistTitle(gsHistTitle),
+    fHistXTitle(gsHistXTitle),fHistYTitle(gsHistYTitle),
+    fColor(MCalibrationCam::kNONE), fIntensBad(NULL),
+    fBadPixels(NULL), fIntensCam(NULL), fCam(NULL), fGeom(NULL),
+    fRunHeader(NULL)
 {
 
@@ -152,4 +173,9 @@
     SetOscillations(kTRUE);
     SetSizeCheck   (kTRUE);
+
+    SetLowerFitLimitHiGain();
+    SetUpperFitLimitHiGain();
+    SetLowerFitLimitLoGain();
+    SetUpperFitLimitLoGain();
 }
 
@@ -1048,5 +1074,6 @@
                                        MBadPixelsPix::UncalibratedType_t osctyp)
 {
-  
+  fIsHiGainFitRanges = TMath::Abs(fUpperFitLimitHiGain - fLowerFitLimitHiGain) > 1E-5;
+
   for (Int_t i=0; i<fHiGainArray->GetSize(); i++)
     {
@@ -1097,5 +1124,6 @@
                                             MBadPixelsPix::UncalibratedType_t osctyp)
 {
-  
+  fIsLoGainFitRanges = TMath::Abs(fUpperFitLimitLoGain - fLowerFitLimitLoGain) > 1E-5;
+
   if (!IsLoGain())
     return;
@@ -1207,13 +1235,20 @@
   // 2) Fit the Hi Gain histograms with a Gaussian
   //
-  if (!hist.FitGaus())
-    //
-    // 3) In case of failure set the bit Fitted to false and take histogram means and RMS
-    //
-    if (!hist.RepeatFit())
-      {
-        hist.BypassFit();
-        bad.SetUncalibrated( fittyp );
-      }
+  if (fIsHiGainFitRanges)
+  {
+      if (!hist.FitGaus("R",fLowerFitLimitHiGain,fUpperFitLimitHiGain))
+          bad.SetUncalibrated( fittyp );
+  }
+  else
+      if (!hist.FitGaus())
+          //
+          // 3) In case of failure set the bit Fitted to false and take histogram means and RMS
+          //
+          if (!hist.RepeatFit())
+          {
+              hist.BypassFit();
+              bad.SetUncalibrated( fittyp );
+          }
+
  
   //
@@ -1291,15 +1326,21 @@
   // 2) Fit the Hi Gain histograms with a Gaussian
   //
-  if (!hist.FitGaus())
-    //
-    // 3) In case of failure set the bit Fitted to false and take histogram means and RMS
-    //
-    if (!hist.RepeatFit())
-      {
-        hist.BypassFit();
-        if (pix.IsHiGainSaturation())
+  if (fIsLoGainFitRanges)
+  {
+      if (!hist.FitGaus("R",fLowerFitLimitLoGain,fUpperFitLimitLoGain))
           bad.SetUncalibrated( fittyp );
-      }
-  
+  }
+  else
+      if (!hist.FitGaus())
+          //
+          // 3) In case of failure set the bit Fitted to false and take histogram means and RMS
+          //
+          if (!hist.RepeatFit())
+          {
+              hist.BypassFit();
+              if (pix.IsHiGainSaturation())
+                  bad.SetUncalibrated( fittyp );
+          }
+
   //
   // 4) Check for oscillations
@@ -1429,4 +1470,23 @@
 }
 
+// -----------------------------------------------------------------------------
+// 
+// Available options
+//  Debug
+//  LoGain
+//  Oscillations
+//  SizeCheck
+//  Averageing
+//  Nbins
+//  First
+//  Last
+//  ProbLimit
+//  OverflowLimit
+//  PulserFrequency
+//  LowerFitLimitHiGain
+//  UpperFitLimitHiGain
+//  LowerFitLimitLoGain
+//  UpperFitLimitLoGain
+//
 Int_t MHCalibrationCam::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
@@ -1493,4 +1553,29 @@
       rc = kTRUE;
     }
+
+  if (IsEnvDefined(env, prefix, "LowerFitLimitHiGain", print))
+    {
+      SetLowerFitLimitHiGain(GetEnvValue(env, prefix, "LowerFitLimitHiGain", fLowerFitLimitHiGain));
+      rc = kTRUE;
+    }
+
+  if (IsEnvDefined(env, prefix, "UpperFitLimitHiGain", print))
+    {
+      SetUpperFitLimitHiGain(GetEnvValue(env, prefix, "UpperFitLimitHiGain", fUpperFitLimitHiGain));
+      rc = kTRUE;
+    }
+
+  if (IsEnvDefined(env, prefix, "LowerFitLimitLoGain", print))
+    {
+      SetLowerFitLimitLoGain(GetEnvValue(env, prefix, "LowerFitLimitLoGain", fLowerFitLimitLoGain));
+      rc = kTRUE;
+    }
+
+  if (IsEnvDefined(env, prefix, "UpperFitLimitLoGain", print))
+    {
+      SetUpperFitLimitLoGain(GetEnvValue(env, prefix, "UpperFitLimitLoGain", fUpperFitLimitLoGain));
+      rc = kTRUE;
+    }
+
   
   return rc;
Index: trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.h
===================================================================
--- trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.h	(revision 6969)
+++ trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.h	(revision 7005)
@@ -40,4 +40,8 @@
   
 private:
+  static const Double_t fgLowerFitLimitHiGain; //! The default for fLowerFitLimitHiGain (now at: 0)
+  static const Double_t fgUpperFitLimitHiGain; //! The default for fUpperFitLimitHiGain (now at: 0)
+  static const Double_t fgLowerFitLimitLoGain; //! The default for fLowerFitLimitLoGain (now at: 0)
+  static const Double_t fgUpperFitLimitLoGain; //! The default for fUpperFitLimitLoGain (now at: 0)
 
   static const Int_t   fgPulserFrequency;  //! The default for fPulserFrequency (now set to: 500)
@@ -58,4 +62,12 @@
   Float_t fProbLimit;                     // Limit for acceptance of probability of Gauss-Fit
   Float_t fOverflowLimit;                 // Part of maximum allowed overflow events
+
+  Double_t fLowerFitLimitHiGain;          // Lower limit for the fit range for the hi-gain hist
+  Double_t fUpperFitLimitHiGain;          // Upper limit for the fit range for the hi-gain hist
+  Double_t fLowerFitLimitLoGain;          // Lower limit for the fit range for the lo-gain hist
+  Double_t fUpperFitLimitLoGain;          // Upper limit for the fit range for the lo-gain hist
+
+  Bool_t   fIsHiGainFitRanges;            // Are high-gain fit ranges defined?
+  Bool_t   fIsLoGainFitRanges;            // Are low-gain fit ranges defined?
   
   TString fHistName;                      //! Histogram names
@@ -211,4 +223,9 @@
   void SetHistXTitle( const char *name )  { fHistXTitle = name; }
   void SetHistYTitle( const char *name )  { fHistYTitle = name; }
+
+  void SetLowerFitLimitHiGain( const Double_t d=fgLowerFitLimitHiGain)  {  fLowerFitLimitHiGain = d;  }
+  void SetUpperFitLimitHiGain( const Double_t d=fgUpperFitLimitHiGain)  {  fUpperFitLimitHiGain = d;  }
+  void SetLowerFitLimitLoGain( const Double_t d=fgLowerFitLimitLoGain)  {  fLowerFitLimitLoGain = d;  }
+  void SetUpperFitLimitLoGain( const Double_t d=fgUpperFitLimitLoGain)  {  fUpperFitLimitLoGain = d;  }
    
   void SetNbins                   ( const Int_t  i )       { fNbins   = i; }
@@ -223,5 +240,5 @@
   void SetPulserFrequency      ( const Int_t   i=fgPulserFrequency )   { fPulserFrequency  = i; }
   
-  ClassDef(MHCalibrationCam, 4)	// Base Histogram class for Calibration Camera
+  ClassDef(MHCalibrationCam, 5)	// Base Histogram class for Calibration Camera
 };
 
