Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc	(revision 6139)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc	(revision 6140)
@@ -78,10 +78,12 @@
 
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgHiGainFirst             =  0;
-const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgHiGainLast              = 18;
+const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgHiGainLast              = 20;
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgLoGainFirst             =  3;
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgLoGainLast              = 14;
-const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetLeftFromPeak      = 1;
-const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetRightFromPeak     = 2;
-const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgPeakSearchWindowSize    = 2;
+const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetLeftFromPeak      =  2;
+const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetRightFromPeak     =  3;
+const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgPeakSearchWindowSize    =  2;
+const Int_t  MExtractTimeAndChargeDigitalFilterPeakSearch::fgHiGainFailureLimit      =  5;
+const Int_t  MExtractTimeAndChargeDigitalFilterPeakSearch::fgLoGainFailureLimit      = 10;
 // --------------------------------------------------------------------------
 //
@@ -89,6 +91,9 @@
 //
 // Sets:
-// - fOffsetLeftFromPeak  to fgOffsetLeftFromPeak
-// - fOffsetRightFromPeak to fgOffsetRightFromPeak
+// - fOffsetLeftFromPeak    to fgOffsetLeftFromPeak
+// - fOffsetRightFromPeak   to fgOffsetRightFromPeak
+// - fPeakSearchWindowSize  to fgPeakSearchWindowSize
+// - fHiGainFailureLimit to fgHiGainFailureLimit
+// - fLoGainFailureLimit to fgLoGainFailureLimit
 //
 MExtractTimeAndChargeDigitalFilterPeakSearch::MExtractTimeAndChargeDigitalFilterPeakSearch(const char *name, const char *title) 
@@ -100,4 +105,40 @@
     SetOffsetRightFromPeak();
     SetPeakSearchWindowSize();
+    SetHiGainFailureLimit();
+    SetLoGainFailureLimit();
+}
+
+// --------------------------------------------------------------------------
+//
+// The PreProcess searches for the following input containers:
+//  - MRawEvtData
+//  - MRawRunHeader
+//  - MPedestalCam
+//
+// The following output containers are also searched and created if
+// they were not found:
+//
+//  - MExtractedSignalCam
+//  - MArrivalTimeCam    
+//
+// The following variables are set to 0:
+//
+//  - fHiGainOutOfRangeLeft
+//  - fHiGainOutOfRangeRight
+//  - fLoGainOutOfRangeLeft
+//  - fLoGainOutOfRangeRight
+//
+Int_t MExtractTimeAndChargeDigitalFilterPeakSearch::PreProcess(MParList *pList)
+{
+
+  if (!MExtractTimeAndCharge::PreProcess(pList))
+    return kFALSE;
+  
+  fHiGainOutOfRangeLeft  = 0;
+  fHiGainOutOfRangeRight = 0;
+  fLoGainOutOfRangeLeft   = 0;
+  fLoGainOutOfRangeRight  = 0;
+  
+  return kTRUE;
 }
 
@@ -233,5 +274,5 @@
     fHiGainFirst = higainfirst - fOffsetLeftFromPeak;
   else
-    *fLog << warn << " High Gain ranges out of limits to the left!!! " << (Int_t)higainfirst << endl;
+    fHiGainOutOfRangeLeft++;
 
   //
@@ -241,5 +282,5 @@
     fHiGainLast  = higainfirst + fOffsetRightFromPeak + fWindowSizeHiGain;
   else 
-    *fLog << warn << " High Gain ranges out of limits to the right!!! " << (Int_t)higainfirst << endl;
+    fHiGainOutOfRangeRight++;
       
 
@@ -247,5 +288,5 @@
     fLoGainFirst = fHiGainFirst + (Int_t)fOffsetLoGain;
   else
-    *fLog << inf << " High Gain ranges out of limits to the left!!! " << (Int_t)fHiGainFirst << endl;
+    fLoGainOutOfRangeLeft++;
   
   //
@@ -255,5 +296,5 @@
     fLoGainLast = fLoGainFirst+fWindowSizeLoGain+fOffsetRightFromPeak;
   else
-    *fLog << warn << " Low Gain ranges out of limits to the right!!! " << endl;
+    fLoGainOutOfRangeRight++;
 
   pixel.Reset();
@@ -347,5 +388,40 @@
 }
 
-
+Int_t MExtractTimeAndChargeDigitalFilterPeakSearch::PostProcess()
+{
+
+  if (GetNumExecutions() == 0)
+    return kTRUE;
+
+  const Int_t higainfailure = (fHiGainOutOfRangeLeft+fHiGainOutOfRangeRight)/GetNumExecutions()*100;
+  const Int_t logainfailure = (fLoGainOutOfRangeLeft+fLoGainOutOfRangeRight)/GetNumExecutions()*100;
+
+  if (fHiGainOutOfRangeLeft > 0)
+    *fLog << warn << GetDescriptor() << ": " << fHiGainOutOfRangeLeft/GetNumExecutions()*100 << "% ranging out of high-gain window to the left!" << endl;
+  if (fHiGainOutOfRangeRight > 0)
+    *fLog << warn << GetDescriptor() << ": " << fHiGainOutOfRangeRight/GetNumExecutions()*100 << "% ranging out of high-gain window to the right!" << endl;
+  if (fLoGainOutOfRangeLeft > 0)
+    *fLog << warn << GetDescriptor() << ": " << fLoGainOutOfRangeLeft/GetNumExecutions()*100 << "% ranging out of low-gain window to the left!" << endl;
+  if (fHiGainOutOfRangeRight > 0)
+    *fLog << warn << GetDescriptor() << ": " << fHiGainOutOfRangeRight/GetNumExecutions()*100 << "% ranging out of high-gain window to the right!" << endl;
+
+  
+  if (higainfailure > fHiGainFailureLimit)
+    {
+      *fLog << err << GetDescriptor() << ": " << higainfailure << "% range failures in high gain above limit of: " << fHiGainFailureLimit << "%." << endl;
+      return kFALSE;
+    }
+  
+  if (logainfailure > fLoGainFailureLimit)
+    {
+      *fLog << err << GetDescriptor() << ": " << logainfailure << "% range failures in low gain above limit of: " << fLoGainFailureLimit << "%." << endl;
+      return kFALSE;
+    }
+  
+
+
+  return kTRUE;
+
+}
 
 // --------------------------------------------------------------------------
@@ -381,4 +457,16 @@
     }
   
+  if (IsEnvDefined(env, prefix, "HiGainFailureLimit", print))
+    {
+      fHiGainFailureLimit = GetEnvValue(env, prefix, fHiGainFailureLimit);
+      rc = kTRUE;
+    }
+  
+  if (IsEnvDefined(env, prefix, "LoGainFailureLimit", print))
+    {
+      fLoGainFailureLimit = GetEnvValue(env, prefix, fLoGainFailureLimit);
+      rc = kTRUE;
+    }
+  
   return MExtractTimeAndChargeDigitalFilter::ReadEnv(env, prefix, print) ? kTRUE : rc;
 }
@@ -394,3 +482,5 @@
     *fLog << " Offset from Peak right:  " << fOffsetRightFromPeak  << endl;
     *fLog << " Peak search window size: " << fPeakSearchWindowSize << endl;
-}
+    *fLog << " High Gain Failure limit: " << fHiGainFailureLimit << endl;
+    *fLog << " Low Gain Failure limit:  " << fLoGainFailureLimit << endl;
+}
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.h	(revision 6139)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.h	(revision 6140)
@@ -21,20 +21,32 @@
 private:
 
-  static const Byte_t fgHiGainFirst;             //! Default for fHiGainFirst          (now set to: 0)
-  static const Byte_t fgHiGainLast;              //! Default for fHiGainLast           (now set to:18)
-  static const Byte_t fgLoGainFirst;             //! Default for fLoGainFirst          (now set to: 2)
-  static const Byte_t fgLoGainLast;              //! Default for fLoGainLast           (now set to:14)
-  static const Byte_t fgOffsetLeftFromPeak;      //! Default for fOffsetLeftFromPeak   (now set to: 1)  
-  static const Byte_t fgOffsetRightFromPeak;     //! Default for fOffsetRightFromPeak  (now set to: 2)  
-  static const Byte_t fgPeakSearchWindowSize;    //! Default for fPeakSearchWindowSize (now set to: 2)
+  static const Byte_t fgHiGainFirst;             //! Default for fHiGainFirst           (now set to: 0)
+  static const Byte_t fgHiGainLast;              //! Default for fHiGainLast            (now set to:18)
+  static const Byte_t fgLoGainFirst;             //! Default for fLoGainFirst           (now set to: 2)
+  static const Byte_t fgLoGainLast;              //! Default for fLoGainLast            (now set to:14)
+  static const Byte_t fgOffsetLeftFromPeak;      //! Default for fOffsetLeftFromPeak    (now set to: 1)  
+  static const Byte_t fgOffsetRightFromPeak;     //! Default for fOffsetRightFromPeak   (now set to: 2)  
+  static const Byte_t fgPeakSearchWindowSize;    //! Default for fPeakSearchWindowSize  (now set to: 2)
+  static const Int_t  fgHiGainFailureLimit;      //! Default for fHiGainMaxFailureLimit (now set to: 5)
+  static const Int_t  fgLoGainFailureLimit;      //! Default for fHiGainMaxFailureLimit (now set to: 10)
 
   Byte_t  fOffsetLeftFromPeak;                   // Number of slices to start extraction before peak slice
   Byte_t  fOffsetRightFromPeak;                  // Number of slices to stop  extraction after  peak slice
   Byte_t  fPeakSearchWindowSize;                 // Size of FADC window in the search for the highest peak of all pixels.
+  Int_t   fHiGainFailureLimit;                   // Limit for percentage of events ranging out of limits in high gain
+  Int_t   fLoGainFailureLimit;                   // Limit for percentage of events ranging out of limits in low gain
+
+  ULong_t fHiGainOutOfRangeLeft;                // Number of events out of range to the left side in high gain
+  ULong_t fHiGainOutOfRangeRight;               // Number of events out of range to the right side in high gain
+  ULong_t fLoGainOutOfRangeLeft;                // Number of events out of range to the left side in low gain
+  ULong_t fLoGainOutOfRangeRight;               // Number of events out of range to the right side in low gain
 
   void    FindPeak(Byte_t *ptr, Byte_t &startslice, Int_t &signal, Int_t &sat, Byte_t &satpos) const;
 
+  Int_t   PreProcess(MParList *pList);
+  Int_t   Process();
+  Int_t   PostProcess();
+
   Int_t   ReadEnv(const TEnv &env, TString prefix, Bool_t print);
-  Int_t   Process();
 
 public:
@@ -43,7 +55,9 @@
   ~MExtractTimeAndChargeDigitalFilterPeakSearch() { }
   
-  void SetOffsetLeftFromPeak ( Byte_t offset=fgOffsetLeftFromPeak  )  {  fOffsetLeftFromPeak   = offset; }
-  void SetOffsetRightFromPeak( Byte_t offset=fgOffsetRightFromPeak )  {  fOffsetRightFromPeak  = offset; }
-  void SetPeakSearchWindowSize(Byte_t size =fgPeakSearchWindowSize )  {  fPeakSearchWindowSize = size;   }
+  void SetOffsetLeftFromPeak ( Byte_t offset=fgOffsetLeftFromPeak  )  { fOffsetLeftFromPeak   = offset; }
+  void SetOffsetRightFromPeak( Byte_t offset=fgOffsetRightFromPeak )  { fOffsetRightFromPeak  = offset; }
+  void SetPeakSearchWindowSize(Byte_t size =fgPeakSearchWindowSize )  { fPeakSearchWindowSize = size;   }
+  void SetHiGainFailureLimit ( Int_t  lim=fgHiGainFailureLimit     )  { fHiGainFailureLimit   = lim;    }     
+  void SetLoGainFailureLimit ( Int_t  lim=fgLoGainFailureLimit     )  { fLoGainFailureLimit   = lim;    }
 
   void Print(Option_t *o="") const;
