Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 2758)
+++ trunk/MagicSoft/Mars/Changelog	(revision 2759)
@@ -4,4 +4,10 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2004/01/09: Markus Gaug
+
+   * mcalib/MCalibrationPix.[h,cc]
+     - fixed a bug in calculation of fFitValid. 
+       before, a call to IsFitValid() always returned false
+
  2004/01/09: Javier López
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc	(revision 2758)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc	(revision 2759)
@@ -131,5 +131,5 @@
 // 8) Calculate the errors of the F-Factor method
 //
-// The fit is declared valid (fFitValid = kTRUE), if:
+// The fits are declared valid (fFitValid = kTRUE), if:
 //
 // 1) Pixel has a fitted charge greater than 3*PedRMS
@@ -139,5 +139,6 @@
 // 5) If FitTimes is used, 
 //    the mean arrival time is at least 1.0 slices from the used edge slices 
-// 
+// (this stage is only performed in the times fit)
+//
 // The conversion factor after the F-Factor method is declared valid, if:
 //
@@ -219,25 +220,7 @@
     fFitted = kTRUE;
 
-
-  if (fHiGainSaturation)
-    {
-      if ( (fCharge     > 0.3*GetPedRms()) &&
-          (fErrCharge  > 0.)              &&
-          (fHist->IsFitOK())              &&
-          (fSigmaCharge > fPedRms/fConversionHiLo)   &&
-          (fTime > fHist->GetTimeLowerFitRange()+1.) &&
-          (fTime < fHist->GetTimeUpperFitRange()-1.) )
-        fFitValid =  kTRUE;
-    }
-  else
-    {
-      if ( (fCharge > 3.*GetPedRms()) &&
-           (fErrCharge  > 0.)        &&
-           (fHist->IsFitOK())        &&
-           (fSigmaCharge > fPedRms)  &&
-           (fTime > fHist->GetTimeLowerFitRange()+1.) &&
-           (fTime < fHist->GetTimeUpperFitRange()-1.) )
-        fFitValid =  kTRUE;
-    }
+  if (CheckChargeFitValidity())
+    fFitValid = kTRUE;
+
   
   //
@@ -356,4 +339,74 @@
 }
 
+//
+// The check return kTRUE if:
+//
+// 1) Pixel has a fitted charge greater than 3*PedRMS
+// 2) Pixel has a fit error greater than 0.
+// 3) Pixel has a fit Probability greater than 0.0001 
+// 4) Pixel has a charge sigma bigger than its Pedestal RMS
+// 
+Bool_t MCalibrationPix::CheckChargeFitValidity()
+{
+
+  Float_t equivpedestal = GetPedRms();
+
+  if (fHiGainSaturation)
+    equivpedestal /= fConversionHiLo;
+      
+  if (fCharge < 3.*equivpedestal)
+    {
+      *fLog << warn << "WARNING: Fitted Charge is smaller than 3 Pedestal RMS in Pixel " << fPixId << endl;
+      return kFALSE;
+    }
+  
+  if (fErrCharge < 0.) 
+    {
+      *fLog << warn << "WARNING: Error of Fitted Charge is smaller than 0 in Pixel " << fPixId << endl;
+      return kFALSE;
+    }
+      
+  if (!fHist->IsFitOK()) 
+    {
+      *fLog << warn << "WARNING: Probability of Fitted Charge too low in Pixel " << fPixId << endl;
+      return kFALSE;
+    }
+
+  if (fSigmaCharge < equivpedestal)
+    {
+      *fLog << warn << "WARNING: Sigma of Fitted Charge smaller than Pedestal RMS in Pixel " << fPixId << endl;
+      return kFALSE;
+    }
+  return kTRUE;
+}
+
+//
+// The check returns kTRUE if:
+//
+// The mean arrival time is at least 1.0 slices from the used edge slices 
+//
+Bool_t MCalibrationPix::CheckTimeFitValidity()
+{
+
+  if (fTime < fHist->GetTimeLowerFitRange()+1.)
+    {
+      *fLog << warn 
+            << "WARNING: Mean Fitted Time inside or smaller than first used FADC slice in Pixel " 
+            << fPixId << endl;
+      return kFALSE;
+    }
+
+  if (fTime < fHist->GetTimeUpperFitRange()-1.)
+    {
+      *fLog << warn 
+            << "WARNING: Mean Fitted Time inside or greater than last used FADC slice in Pixel " 
+            << fPixId << endl;
+      return kFALSE;
+    }
+
+  return kTRUE;
+}
+
+
 
 void MCalibrationPix::ApplyLoGainConversion()
@@ -432,4 +485,7 @@
   fTimeChiSquare = fHist->GetTimeChiSquare();
 
+  if (!CheckTimeFitValidity())
+    fFitValid = kFALSE;
+
   return kTRUE;
 }
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationPix.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationPix.h	(revision 2758)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationPix.h	(revision 2759)
@@ -60,4 +60,7 @@
   
   MHCalibrationPixel *fHist;    //! Pointer to the histograms performing the fits, etc.  
+
+  Bool_t CheckChargeFitValidity();
+  Bool_t CheckTimeFitValidity();
   
 public:
