Index: /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc
===================================================================
--- /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc	(revision 5725)
+++ /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc	(revision 5726)
@@ -51,5 +51,4 @@
 //  In case of odd numbers, the window will be modified.
 //
-//
 //  Call: SetOffsetFromWindow() to adjust the positionning of the extraction
 //  window w.r.t. the peak search window. An fOffsetFromWindow of 0 means that
@@ -251,5 +250,4 @@
 
   return kTRUE;
-  
 }
 
@@ -262,5 +260,6 @@
 // saturated slices.
 //
-void MExtractFixedWindowPeakSearch::FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice, Int_t &max, Int_t &sat) const
+void MExtractFixedWindowPeakSearch::FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice, Int_t &max, 
+                                             Int_t &sat, Byte_t &satpos) const
 {
 
@@ -268,4 +267,6 @@
 
   sat = 0;
+  satpos = 0;
+  
   startslice = 0;
   Int_t sum=0;
@@ -281,5 +282,9 @@
       sum += *p;
       if (*p++ >= fSaturationLimit)
-	sat++;
+        {
+          if (sat == 0)
+              satpos = p-ptr;
+          sat++;
+        }
     }
 
@@ -289,6 +294,10 @@
   while (p<end)
     if (*p++ >= fSaturationLimit)
-      sat++;
-
+      {
+        if (sat == 0)
+          satpos = p-ptr;
+        sat++;
+      }
+  
   //
   // Calculate the i-th sum of n as
@@ -395,5 +404,9 @@
 
   Int_t sat;
+  Byte_t  satpos;
+  ULong_t gsatpos = 0;
+
   Int_t maxsumhi = -1000000;
+  Int_t numsat   = 0;
   Byte_t startslice;
   Byte_t hiGainFirst = 0;
@@ -406,7 +419,7 @@
       sat = 0;
 
-      FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat);
-
-      if (sumhi > maxsumhi && sat == 0 )
+      FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat, satpos);
+
+      if (sumhi > maxsumhi && sat == 0)
 	{
 	  maxsumhi = sumhi;
@@ -416,9 +429,18 @@
 	    hiGainFirst = fHiGainFirst;
 	}
-    }
-
-
-  loGainFirst = ( hiGainFirst+fLoGainPeakShift > fLoGainFirst ) ?
-    hiGainFirst+fLoGainPeakShift : fLoGainFirst;
+      else if (sat)
+        {
+          numsat++;
+          gsatpos += satpos;
+        }
+    }
+
+  // Check necessary for calibration events
+  if (numsat > fSignals->GetSize()*0.9)
+    hiGainFirst = gsatpos/numsat - 1;
+
+  loGainFirst = ( hiGainFirst+fLoGainPeakShift > fLoGainFirst ) 
+    ? hiGainFirst+fLoGainPeakShift 
+    : fLoGainFirst;
 
   // Make sure we will not integrate beyond the hi gain limit:
@@ -434,5 +456,4 @@
   fSignals->Clear();
 
-  sat = 0;
   while (pixel.Next())
     {
@@ -448,9 +469,5 @@
       Byte_t satlo=0;
       if (pixel.HasLoGain())
-        {
-	  FindSignalLoGain(pixel.GetLoGainSamples()+loGainFirst, sumlo, satlo);
-	  if (satlo)
-	    sat++;
-        }
+        FindSignalLoGain(pixel.GetLoGainSamples()+loGainFirst, sumlo, satlo);
 
       //
Index: /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h
===================================================================
--- /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h	(revision 5725)
+++ /trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h	(revision 5726)
@@ -29,5 +29,5 @@
   void   FindSignalLoGain(Byte_t *ptr, Float_t &sum, Byte_t &sat) const;  
 
-  void   FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice, Int_t &signal, Int_t &sat) const;
+  void   FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice, Int_t &signal, Int_t &sat, Byte_t &satpos) const;
 
   Bool_t ReInit(MParList *pList);
@@ -37,19 +37,19 @@
 public:
 
-    MExtractFixedWindowPeakSearch(const char *name=NULL, const char *title=NULL);
+  MExtractFixedWindowPeakSearch(const char *name=NULL, const char *title=NULL);
+  
+  Byte_t GetHiGainWindowSize() const { return fHiGainWindowSize; }
+  Byte_t GetLoGainWindowSize() const { return fLoGainWindowSize; }
 
-    Byte_t GetHiGainWindowSize() const { return fHiGainWindowSize; }
-    Byte_t GetLoGainWindowSize() const { return fLoGainWindowSize; }
+  void SetRange(Byte_t hifirst=0, Byte_t hilast=0, Byte_t lofirst=0, Byte_t lolast=0);    
+  void SetWindows(Byte_t windowh=fgHiGainWindowSize, Byte_t windowl=fgLoGainWindowSize, 
+                  Byte_t peaksearchwindow=fgPeakSearchWindowSize);
+  void SetOffsetFromWindow(Byte_t offset=fgOffsetFromWindow)  {  fOffsetFromWindow = offset; }
+  
+  void SetLoGainPeakShift(Byte_t shift=fgLoGainPeakShift) { fLoGainPeakShift = shift; }
+  
+  void Print(Option_t *o="") const;
 
-    void SetRange(Byte_t hifirst=0, Byte_t hilast=0, Byte_t lofirst=0, Byte_t lolast=0);    
-    void SetWindows(Byte_t windowh=fgHiGainWindowSize, Byte_t windowl=fgLoGainWindowSize, 
-		    Byte_t peaksearchwindow=fgPeakSearchWindowSize);
-    void SetOffsetFromWindow(Byte_t offset=fgOffsetFromWindow)  {  fOffsetFromWindow = offset; }
-
-    void SetLoGainPeakShift(Byte_t shift=fgLoGainPeakShift) { fLoGainPeakShift = shift; }
-
-    void Print(Option_t *o="") const;
-
-    ClassDef(MExtractFixedWindowPeakSearch, 1) // Signal Extractor for fixed size trigger-corrected extraction window
+  ClassDef(MExtractFixedWindowPeakSearch, 1) // Signal Extractor for fixed size trigger-corrected extraction window
 };
 
