Index: trunk/MagicSoft/Mars/mcalib/MHGausEvents.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHGausEvents.cc	(revision 3765)
+++ trunk/MagicSoft/Mars/mcalib/MHGausEvents.cc	(revision 3766)
@@ -267,182 +267,71 @@
 }
 
-// --------------------------------------------------------------------------
-//
-// Default Reset(), can be overloaded.
-//
-// Executes:
-// - Clear()
-// - fHGausHist.Reset()
-// - fEvents.Set(0)
-//
-void MHGausEvents::Reset()
-{
-
-  Clear();
-  fHGausHist.Reset();
-  fEvents.Set(0);
-
-}
-
-// --------------------------------------------------------------------------
-//
-// Default InitBins, can be overloaded.
-//
-// Executes:
-// - fHGausHist.SetBins(fNbins,fFirst,fLast)
-//
-void MHGausEvents::InitBins()
-{
-  fHGausHist.SetBins(fNbins,fFirst,fLast);
-}
-
-// --------------------------------------------------------------------------
-//
-// Executes:
-// - FillArray()
-// - FillHist()
-//
-Bool_t MHGausEvents::FillHistAndArray(const Float_t f)
-{
-
-  FillArray(f);
-  return FillHist(f);
-}
-
-// --------------------------------------------------------------------------
-//
-// Fills fHGausHist with f
-// Returns kFALSE, if overflow or underflow occurred, else kTRUE
-//
-Bool_t MHGausEvents::FillHist(const Float_t f)
-{
-  return fHGausHist.Fill(f) > -1;
-}
-
-// --------------------------------------------------------------------------
-//
-// Fill fEvents with f
-// If size of fEvents is 0, initializes it to 512
-// If size of fEvents is smaller than fCurrentSize, double the size
-// Increase fCurrentSize by 1
-//
-void MHGausEvents::FillArray(const Float_t f)
-{
-  if (fEvents.GetSize() == 0)
-    fEvents.Set(512);
-
-  if (fCurrentSize >= fEvents.GetSize())
-    fEvents.Set(fEvents.GetSize()*2);
-  
-  fEvents.AddAt(f,fCurrentSize++);
-}
-
-// --------------------------------------------------------------------------
-//
-// Set Excluded bit from outside
-//
-void MHGausEvents::SetExcluded(const Bool_t b)
-{
-    b ? SETBIT(fFlags,kExcluded) : CLRBIT(fFlags,kExcluded);
-}
-
-
-const Double_t MHGausEvents::GetChiSquare()  const 
-{
-  return ( fFGausFit ? fFGausFit->GetChisquare() : 0.);
-}
-
-const Int_t MHGausEvents::GetNdf() const 
-{
-  return ( fFGausFit ? fFGausFit->GetNDF() : 0);
-}
-
-
-const Double_t MHGausEvents::GetExpChiSquare()  const 
-{
-  return ( fFExpFit ? fFExpFit->GetChisquare() : 0.);
-}
-
-
-const Int_t MHGausEvents::GetExpNdf()  const 
-{
-  return ( fFExpFit ? fFExpFit->GetNDF() : 0);
-}
-
-
-const Double_t MHGausEvents::GetExpProb()  const 
-{
-  return ( fFExpFit ? fFExpFit->GetProb() : 0.);
-}
-
-
-const Double_t MHGausEvents::GetOffset()  const 
-{
-  return ( fFExpFit ? fFExpFit->GetParameter(0) : 0.);
-}
-
-
-const Double_t MHGausEvents::GetSlope()  const 
-{
-  return ( fFExpFit ? fFExpFit->GetParameter(1) : 0.);
-}
-
-
-const Bool_t MHGausEvents::IsEmpty() const
-{
-    return !(fHGausHist.GetEntries());
-}
-
-
-const Bool_t MHGausEvents::IsFourierSpectrumOK() const 
-{
-  return TESTBIT(fFlags,kFourierSpectrumOK);
-}
-
-
-const Bool_t MHGausEvents::IsGausFitOK() const 
-{
-  return TESTBIT(fFlags,kGausFitOK);
-}
-
-
-const Bool_t MHGausEvents::IsExpFitOK() const 
-{
-  return TESTBIT(fFlags,kExpFitOK);
-}
-
-const Bool_t MHGausEvents::IsExcluded() const
-{
-  return TESTBIT(fFlags,kExcluded);
-}
-
-
-// -------------------------------------------------------------------
-//
-// The flag setters are to be used ONLY for Monte-Carlo!!
-//
-void  MHGausEvents::SetGausFitOK(const Bool_t b)
-{
-  b ? SETBIT(fFlags,kGausFitOK) : CLRBIT(fFlags,kGausFitOK);
-
-}
-// -------------------------------------------------------------------
-//
-// The flag setters are to be used ONLY for Monte-Carlo!!
-//
-void  MHGausEvents::SetExpFitOK(const Bool_t b)
-{
-  
-  b ? SETBIT(fFlags,kExpFitOK) : CLRBIT(fFlags,kExpFitOK);  
-}
-
-// -------------------------------------------------------------------
-//
-// The flag setters are to be used ONLY for Monte-Carlo!!
-//
-void  MHGausEvents::SetFourierSpectrumOK(const Bool_t b)
-{
-
-  b ? SETBIT(fFlags,kFourierSpectrumOK) : CLRBIT(fFlags,kFourierSpectrumOK);    
+
+// -----------------------------------------------------------------------------
+// 
+// Bypasses the Gauss fit by taking mean and RMS from the histogram
+//
+// Errors are determined in the following way:
+// MeanErr  = RMS / Sqrt(entries)
+// SigmaErr = RMS / (2.*Sqrt(entries) )
+//
+void MHGausEvents::BypassFit()
+{
+
+  const Stat_t entries = fHGausHist.GetEntries();
+  
+  if (entries <= 0.)
+    {
+      *fLog << warn << GetDescriptor() 
+            << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl;
+      return;
+    }
+  
+  fMean     = fHGausHist.GetMean();
+  fMeanErr  = fHGausHist.GetRMS() / TMath::Sqrt(entries);
+  fSigma    = fHGausHist.GetRMS() ;
+  fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.;
+}
+
+
+
+// --------------------------------------------------------------------------
+//
+// - Set fPixId to id
+//
+// Add id to names and titles of:
+// - fHGausHist
+//
+void MHGausEvents::ChangeHistId(const Int_t id)
+{
+
+  fPixId = id;
+
+  fHGausHist.SetName(  Form("%s%d", fHGausHist.GetName(),  id));
+  fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
+
+  fName  = Form("%s%d", fName.Data(),  id);
+  fTitle = Form("%s%d", fTitle.Data(), id);
+
+}
+
+// -----------------------------------------------------------------------------
+// 
+// Create the x-axis for the event graph
+//
+Float_t *MHGausEvents::CreateEventXaxis(Int_t n)
+{
+
+  Float_t *xaxis = new Float_t[n];
+
+  if (fEventFrequency)
+    for (Int_t i=0;i<n;i++)
+      xaxis[i] = (Float_t)i/fEventFrequency;
+  else
+    for (Int_t i=0;i<n;i++)
+      xaxis[i] = (Float_t)i;
+
+  return xaxis;
+                 
 }
 
@@ -527,225 +416,4 @@
 }
 
-// -------------------------------------------------------------------
-//
-// Fit fGausHist with a Gaussian after stripping zeros from both ends 
-// and rebinned to the number of bins specified in fBinsAfterStripping
-//
-// The fit results are retrieved and stored in class-own variables.  
-//
-// A flag IsGausFitOK() is set according to whether the fit probability 
-// is smaller or bigger than fProbLimit, whether the NDF is bigger than 
-// fNDFLimit and whether results are NaNs.
-//
-Bool_t MHGausEvents::FitGaus(Option_t *option, const Double_t xmin, const Double_t xmax)
-{
-
-  if (IsGausFitOK())
-    return kTRUE;
-
-  //
-  // First, strip the zeros from the edges which contain only zeros and rebin 
-  // to about fBinsAfterStripping bins. 
-  //
-  // (ATTENTION: The Chisquare method is more sensitive, 
-  // the _less_ bins, you have!)
-  //
-  StripZeros(&fHGausHist,fBinsAfterStripping);
-  
-  //
-  // Get the fitting ranges
-  //
-  Axis_t rmin = (xmin==0.) && (xmax==0.) ? fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst()) : xmin;
-  Axis_t rmax = (xmin==0.) && (xmax==0.) ? fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast())  : xmax;
-
-  //
-  // First guesses for the fit (should be as close to reality as possible, 
-  //
-  const Stat_t   entries     = fHGausHist.Integral("width");
-  const Double_t mu_guess    = fHGausHist.GetBinCenter(fHGausHist.GetMaximumBin());
-  const Double_t sigma_guess = fHGausHist.GetRMS();
-  const Double_t area_guess  = entries/TMath::Sqrt(TMath::TwoPi())/sigma_guess;
-
-  fFGausFit = new TF1("GausFit","gaus",rmin,rmax);
-
-  if (!fFGausFit) 
-    {
-    *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit " 
-          << "in pixel: " << fPixId << endl;
-    return kFALSE;
-    }
-  
-  fFGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
-  fFGausFit->SetParNames("Area","#mu","#sigma");
-  fFGausFit->SetParLimits(0,0.,area_guess*1.5);
-  fFGausFit->SetParLimits(1,rmin,rmax);
-  fFGausFit->SetParLimits(2,0.,rmax-rmin);
-  fFGausFit->SetRange(rmin,rmax);
-
-  fHGausHist.Fit(fFGausFit,option);
-
-
-  fMean     = fFGausFit->GetParameter(1);
-  fSigma    = fFGausFit->GetParameter(2);
-  fMeanErr  = fFGausFit->GetParError(1);
-  fSigmaErr = fFGausFit->GetParError(2);
-  fProb     = fFGausFit->GetProb();
-  //
-  // The fit result is accepted under condition:
-  // 1) The results are not nan's
-  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
-  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
-  //
-  if (   TMath::IsNaN(fMean) 
-      || TMath::IsNaN(fMeanErr)
-      || TMath::IsNaN(fProb)    
-      || TMath::IsNaN(fSigma)
-      || TMath::IsNaN(fSigmaErr) 
-      || fFGausFit->GetNDF() < fNDFLimit 
-      || fProb < fProbLimit )
-    return kFALSE;
-  
-  SetGausFitOK(kTRUE);
-  return kTRUE;
-}
-
-// -----------------------------------------------------------------------------
-//
-// If flag IsGausFitOK() is set (histogram already successfully fitted), 
-// returns kTRUE
-// 
-// If both fMean and fSigma are still zero, call FitGaus() 
-// 
-// Repeats the Gauss fit in a smaller range, defined by: 
-// 
-// min = GetMean() - fBlackoutLimit * GetSigma();
-// max = GetMean() + fPickupLimit   * GetSigma();
-//
-// The fit results are retrieved and stored in class-own variables.  
-//
-// A flag IsGausFitOK() is set according to whether the fit probability 
-// is smaller or bigger than fProbLimit, whether the NDF is bigger than 
-// fNDFLimit and whether results are NaNs.
-//
-Bool_t MHGausEvents::RepeatFit(const Option_t *option)
-{
-
-  if (IsGausFitOK())
-    return kTRUE;
-
-  if ((fMean == 0.) && (fSigma == 0.))
-    return FitGaus();
-
-  //
-  // Get new fitting ranges
-  //
-  Axis_t rmin = fMean - fBlackoutLimit * fSigma;
-  Axis_t rmax = fMean + fPickupLimit   * fSigma;
-
-  Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst());
-  Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ;
-
-  fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax);
-
-  fHGausHist.Fit(fFGausFit,option);
-
-  fMean     = fFGausFit->GetParameter(1);
-  fMeanErr  = fFGausFit->GetParameter(2);
-  fSigma    = fFGausFit->GetParError(1) ; 
-  fSigmaErr = fFGausFit->GetParError(2) ; 
-  fProb     = fFGausFit->GetProb()      ;      
-
-  //
-  // The fit result is accepted under condition:
-  // 1) The results are not nan's
-  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
-  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
-  //
-  if (   TMath::IsNaN ( fMean     ) 
-      || TMath::IsNaN ( fMeanErr  )
-      || TMath::IsNaN ( fProb     )    
-      || TMath::IsNaN ( fSigma    )
-      || TMath::IsNaN ( fSigmaErr ) 
-      || fFGausFit->GetNDF() < fNDFLimit 
-      || fProb < fProbLimit )
-    return kFALSE;
-  
-  SetGausFitOK(kTRUE);
-  return kTRUE;
-
-}
-
-// -----------------------------------------------------------------------------
-// 
-// Bypasses the Gauss fit by taking mean and RMS from the histogram
-//
-// Errors are determined in the following way:
-// MeanErr  = RMS / Sqrt(entries)
-// SigmaErr = RMS / (2.*Sqrt(entries) )
-//
-void MHGausEvents::BypassFit()
-{
-
-  const Stat_t entries = fHGausHist.GetEntries();
-  
-  if (entries <= 0.)
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl;
-      return;
-    }
-  
-  fMean     = fHGausHist.GetMean();
-  fMeanErr  = fHGausHist.GetRMS() / TMath::Sqrt(entries);
-  fSigma    = fHGausHist.GetRMS() ;
-  fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.;
-}
-
-// -----------------------------------------------------------------------------------
-// 
-// A default print
-//
-void MHGausEvents::Print(const Option_t *o) const 
-{
-  
-  *fLog << all                                                        << endl;
-  *fLog << all << "Results of the Gauss Fit in pixel: " << fPixId     << endl;
-  *fLog << all << "Mean: "             << GetMean()                   << endl;
-  *fLog << all << "Sigma: "            << GetSigma()                  << endl;
-  *fLog << all << "Chisquare: "        << GetChiSquare()              << endl;
-  *fLog << all << "DoF: "              << GetNdf()                    << endl;
-  *fLog << all << "Probability: "      << GetProb()                   << endl;
-  *fLog << all                                                        << endl;
-  
-}
-
-// --------------------------------------------------------------------------
-//
-// - Set fPixId to id
-//
-// Add id to names and titles of:
-// - fHGausHist
-//
-void MHGausEvents::ChangeHistId(const Int_t id)
-{
-
-  fPixId = id;
-
-  fHGausHist.SetName(  Form("%s%d", fHGausHist.GetName(),  id));
-  fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
-
-  fName  = Form("%s%d", fName.Data(),  id);
-  fTitle = Form("%s%d", fTitle.Data(), id);
-
-}
-
-// --------------------------------------------------------------------------
-//
-// Re-normalize the results, has to be overloaded
-//
-void  MHGausEvents::Renorm()
-{
-}
-
 // ----------------------------------------------------------------------------------
 //
@@ -783,23 +451,4 @@
 }
 
-// -----------------------------------------------------------------------------
-// 
-// Create the x-axis for the event graph
-//
-Float_t *MHGausEvents::CreateEventXaxis(Int_t n)
-{
-
-  Float_t *xaxis = new Float_t[n];
-
-  if (fEventFrequency)
-    for (Int_t i=0;i<n;i++)
-      xaxis[i] = (Float_t)i/fEventFrequency;
-  else
-    for (Int_t i=0;i<n;i++)
-      xaxis[i] = (Float_t)i;
-
-  return xaxis;
-                 
-}
 
 // -----------------------------------------------------------------------------
@@ -942,5 +591,137 @@
 }
 
-const Double_t MHGausEvents::GetPickup() const 
+
+// --------------------------------------------------------------------------
+//
+// Fill fEvents with f
+// If size of fEvents is 0, initializes it to 512
+// If size of fEvents is smaller than fCurrentSize, double the size
+// Increase fCurrentSize by 1
+//
+void MHGausEvents::FillArray(const Float_t f)
+{
+  if (fEvents.GetSize() == 0)
+    fEvents.Set(512);
+
+  if (fCurrentSize >= fEvents.GetSize())
+    fEvents.Set(fEvents.GetSize()*2);
+  
+  fEvents.AddAt(f,fCurrentSize++);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Fills fHGausHist with f
+// Returns kFALSE, if overflow or underflow occurred, else kTRUE
+//
+Bool_t MHGausEvents::FillHist(const Float_t f)
+{
+  return fHGausHist.Fill(f) > -1;
+}
+
+// --------------------------------------------------------------------------
+//
+// Executes:
+// - FillArray()
+// - FillHist()
+//
+Bool_t MHGausEvents::FillHistAndArray(const Float_t f)
+{
+
+  FillArray(f);
+  return FillHist(f);
+}
+
+// -------------------------------------------------------------------
+//
+// Fit fGausHist with a Gaussian after stripping zeros from both ends 
+// and rebinned to the number of bins specified in fBinsAfterStripping
+//
+// The fit results are retrieved and stored in class-own variables.  
+//
+// A flag IsGausFitOK() is set according to whether the fit probability 
+// is smaller or bigger than fProbLimit, whether the NDF is bigger than 
+// fNDFLimit and whether results are NaNs.
+//
+Bool_t MHGausEvents::FitGaus(Option_t *option, const Double_t xmin, const Double_t xmax)
+{
+
+  if (IsGausFitOK())
+    return kTRUE;
+
+  //
+  // First, strip the zeros from the edges which contain only zeros and rebin 
+  // to about fBinsAfterStripping bins. 
+  //
+  // (ATTENTION: The Chisquare method is more sensitive, 
+  // the _less_ bins, you have!)
+  //
+  StripZeros(&fHGausHist,fBinsAfterStripping);
+  
+  //
+  // Get the fitting ranges
+  //
+  Axis_t rmin = (xmin==0.) && (xmax==0.) ? fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst()) : xmin;
+  Axis_t rmax = (xmin==0.) && (xmax==0.) ? fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast())  : xmax;
+
+  //
+  // First guesses for the fit (should be as close to reality as possible, 
+  //
+  const Stat_t   entries     = fHGausHist.Integral("width");
+  const Double_t mu_guess    = fHGausHist.GetBinCenter(fHGausHist.GetMaximumBin());
+  const Double_t sigma_guess = fHGausHist.GetRMS();
+  const Double_t area_guess  = entries/TMath::Sqrt(TMath::TwoPi())/sigma_guess;
+
+  fFGausFit = new TF1("GausFit","gaus",rmin,rmax);
+
+  if (!fFGausFit) 
+    {
+    *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit " 
+          << "in pixel: " << fPixId << endl;
+    return kFALSE;
+    }
+  
+  fFGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
+  fFGausFit->SetParNames("Area","#mu","#sigma");
+  fFGausFit->SetParLimits(0,0.,area_guess*1.5);
+  fFGausFit->SetParLimits(1,rmin,rmax);
+  fFGausFit->SetParLimits(2,0.,rmax-rmin);
+  fFGausFit->SetRange(rmin,rmax);
+
+  fHGausHist.Fit(fFGausFit,option);
+
+
+  fMean     = fFGausFit->GetParameter(1);
+  fSigma    = fFGausFit->GetParameter(2);
+  fMeanErr  = fFGausFit->GetParError(1);
+  fSigmaErr = fFGausFit->GetParError(2);
+  fProb     = fFGausFit->GetProb();
+  //
+  // The fit result is accepted under condition:
+  // 1) The results are not nan's
+  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
+  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
+  //
+  if (   TMath::IsNaN(fMean) 
+      || TMath::IsNaN(fMeanErr)
+      || TMath::IsNaN(fProb)    
+      || TMath::IsNaN(fSigma)
+      || TMath::IsNaN(fSigmaErr) 
+      || fFGausFit->GetNDF() < fNDFLimit 
+      || fProb < fProbLimit )
+    return kFALSE;
+  
+  SetGausFitOK(kTRUE);
+  return kTRUE;
+}
+
+// -------------------------------------------------------------------------------
+//
+// Return the number of "blackout" events, which are events with values higher 
+// than fBlackoutLimit sigmas from the mean
+//
+//
+const Double_t MHGausEvents::GetBlackout() const 
 {
   
@@ -948,4 +729,60 @@
     return -1.;
 
+  const Int_t first = fHGausHist.GetXaxis()->GetFirst();
+  const Int_t last  = fHGausHist.GetXaxis()->FindBin(fMean-fBlackoutLimit*fSigma);
+
+  if (first >= last)
+    return 0.;
+  
+  return fHGausHist.Integral(first, last, "width");
+}
+
+const Double_t MHGausEvents::GetChiSquare()  const 
+{
+  return ( fFGausFit ? fFGausFit->GetChisquare() : 0.);
+}
+
+
+const Double_t MHGausEvents::GetExpChiSquare()  const 
+{
+  return ( fFExpFit ? fFExpFit->GetChisquare() : 0.);
+}
+
+
+const Int_t MHGausEvents::GetExpNdf()  const 
+{
+  return ( fFExpFit ? fFExpFit->GetNDF() : 0);
+}
+
+
+const Double_t MHGausEvents::GetExpProb()  const 
+{
+  return ( fFExpFit ? fFExpFit->GetProb() : 0.);
+}
+
+
+const Int_t MHGausEvents::GetNdf() const 
+{
+  return ( fFGausFit ? fFGausFit->GetNDF() : 0);
+}
+
+const Double_t MHGausEvents::GetOffset()  const 
+{
+  return ( fFExpFit ? fFExpFit->GetParameter(0) : 0.);
+}
+
+
+// -------------------------------------------------------------------------------
+//
+// Return the number of "pickup" events, which are events with values higher 
+// than fPickupLimit sigmas from the mean
+//
+//
+const Double_t MHGausEvents::GetPickup() const 
+{
+  
+  if ((fMean == 0.) && (fSigma == 0.))
+    return -1.;
+
   const Int_t first = fHGausHist.GetXaxis()->FindBin(fMean+fPickupLimit*fSigma);
   const Int_t last  = fHGausHist.GetXaxis()->GetLast();
@@ -957,18 +794,199 @@
 }
 
-const Double_t MHGausEvents::GetBlackout() const 
-{
-  
+
+const Double_t MHGausEvents::GetSlope()  const 
+{
+  return ( fFExpFit ? fFExpFit->GetParameter(1) : 0.);
+}
+
+// --------------------------------------------------------------------------
+//
+// Default InitBins, can be overloaded.
+//
+// Executes:
+// - fHGausHist.SetBins(fNbins,fFirst,fLast)
+//
+void MHGausEvents::InitBins()
+{
+  fHGausHist.SetBins(fNbins,fFirst,fLast);
+}
+
+const Bool_t MHGausEvents::IsEmpty() const
+{
+    return !(fHGausHist.GetEntries());
+}
+
+
+const Bool_t MHGausEvents::IsExcluded() const
+{
+  return TESTBIT(fFlags,kExcluded);
+}
+
+
+const Bool_t MHGausEvents::IsExpFitOK() const 
+{
+  return TESTBIT(fFlags,kExpFitOK);
+}
+
+const Bool_t MHGausEvents::IsFourierSpectrumOK() const 
+{
+  return TESTBIT(fFlags,kFourierSpectrumOK);
+}
+
+
+const Bool_t MHGausEvents::IsGausFitOK() const 
+{
+  return TESTBIT(fFlags,kGausFitOK);
+}
+
+
+// -----------------------------------------------------------------------------------
+// 
+// A default print
+//
+void MHGausEvents::Print(const Option_t *o) const 
+{
+  
+  *fLog << all                                                        << endl;
+  *fLog << all << "Results of the Gauss Fit in pixel: " << fPixId     << endl;
+  *fLog << all << "Mean: "             << GetMean()                   << endl;
+  *fLog << all << "Sigma: "            << GetSigma()                  << endl;
+  *fLog << all << "Chisquare: "        << GetChiSquare()              << endl;
+  *fLog << all << "DoF: "              << GetNdf()                    << endl;
+  *fLog << all << "Probability: "      << GetProb()                   << endl;
+  *fLog << all                                                        << endl;
+  
+}
+
+// --------------------------------------------------------------------------
+//
+// Re-normalize the results, has to be overloaded
+//
+void  MHGausEvents::Renorm()
+{
+}
+
+// -----------------------------------------------------------------------------
+//
+// If flag IsGausFitOK() is set (histogram already successfully fitted), 
+// returns kTRUE
+// 
+// If both fMean and fSigma are still zero, call FitGaus() 
+// 
+// Repeats the Gauss fit in a smaller range, defined by: 
+// 
+// min = GetMean() - fBlackoutLimit * GetSigma();
+// max = GetMean() + fPickupLimit   * GetSigma();
+//
+// The fit results are retrieved and stored in class-own variables.  
+//
+// A flag IsGausFitOK() is set according to whether the fit probability 
+// is smaller or bigger than fProbLimit, whether the NDF is bigger than 
+// fNDFLimit and whether results are NaNs.
+//
+Bool_t MHGausEvents::RepeatFit(const Option_t *option)
+{
+
+  if (IsGausFitOK())
+    return kTRUE;
+
   if ((fMean == 0.) && (fSigma == 0.))
-    return -1.;
-
-  const Int_t first = fHGausHist.GetXaxis()->GetFirst();
-  const Int_t last  = fHGausHist.GetXaxis()->FindBin(fMean-fBlackoutLimit*fSigma);
-
-  if (first >= last)
-    return 0.;
-  
-  return fHGausHist.Integral(first, last, "width");
-}
-
-
+    return FitGaus();
+
+  //
+  // Get new fitting ranges
+  //
+  Axis_t rmin = fMean - fBlackoutLimit * fSigma;
+  Axis_t rmax = fMean + fPickupLimit   * fSigma;
+
+  Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst());
+  Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ;
+
+  fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax);
+
+  fHGausHist.Fit(fFGausFit,option);
+
+  fMean     = fFGausFit->GetParameter(1);
+  fMeanErr  = fFGausFit->GetParameter(2);
+  fSigma    = fFGausFit->GetParError(1) ; 
+  fSigmaErr = fFGausFit->GetParError(2) ; 
+  fProb     = fFGausFit->GetProb()      ;      
+
+  //
+  // The fit result is accepted under condition:
+  // 1) The results are not nan's
+  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
+  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
+  //
+  if (   TMath::IsNaN ( fMean     ) 
+      || TMath::IsNaN ( fMeanErr  )
+      || TMath::IsNaN ( fProb     )    
+      || TMath::IsNaN ( fSigma    )
+      || TMath::IsNaN ( fSigmaErr ) 
+      || fFGausFit->GetNDF() < fNDFLimit 
+      || fProb < fProbLimit )
+    return kFALSE;
+  
+  SetGausFitOK(kTRUE);
+  return kTRUE;
+
+}
+
+// --------------------------------------------------------------------------
+//
+// Default Reset(), can be overloaded.
+//
+// Executes:
+// - Clear()
+// - fHGausHist.Reset()
+// - fEvents.Set(0)
+//
+void MHGausEvents::Reset()
+{
+
+  Clear();
+  fHGausHist.Reset();
+  fEvents.Set(0);
+
+}
+
+// --------------------------------------------------------------------------
+//
+// Set Excluded bit from outside
+//
+void MHGausEvents::SetExcluded(const Bool_t b)
+{
+    b ? SETBIT(fFlags,kExcluded) : CLRBIT(fFlags,kExcluded);
+}
+
+
+// -------------------------------------------------------------------
+//
+// The flag setters are to be used ONLY for Monte-Carlo!!
+//
+void  MHGausEvents::SetExpFitOK(const Bool_t b)
+{
+  
+  b ? SETBIT(fFlags,kExpFitOK) : CLRBIT(fFlags,kExpFitOK);  
+}
+
+// -------------------------------------------------------------------
+//
+// The flag setters are to be used ONLY for Monte-Carlo!!
+//
+void  MHGausEvents::SetFourierSpectrumOK(const Bool_t b)
+{
+
+  b ? SETBIT(fFlags,kFourierSpectrumOK) : CLRBIT(fFlags,kFourierSpectrumOK);    
+}
+
+
+// -------------------------------------------------------------------
+//
+// The flag setters are to be used ONLY for Monte-Carlo!!
+//
+void  MHGausEvents::SetGausFitOK(const Bool_t b)
+{
+  b ? SETBIT(fFlags,kGausFitOK) : CLRBIT(fFlags,kGausFitOK);
+
+}
