Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc	(revision 6165)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilterPeakSearch.cc	(revision 6166)
@@ -81,5 +81,5 @@
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgLoGainFirst             =  0;
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgLoGainLast              = 14;
-const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetLeftFromPeak      =  1;
+const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetLeftFromPeak      =  3;
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgOffsetRightFromPeak     =  3;
 const Byte_t MExtractTimeAndChargeDigitalFilterPeakSearch::fgPeakSearchWindowSize    =  2;
@@ -102,4 +102,6 @@
     fTitle = title ? title : "Digital Filter";
 
+    SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
+
     SetOffsetLeftFromPeak();
     SetOffsetRightFromPeak();
@@ -137,6 +139,6 @@
   fHiGainOutOfRangeLeft  = 0;
   fHiGainOutOfRangeRight = 0;
-  fLoGainOutOfRangeLeft   = 0;
-  fLoGainOutOfRangeRight  = 0;
+  fLoGainOutOfRangeLeft  = 0;
+  fLoGainOutOfRangeRight = 0;
   
   return kTRUE;
@@ -152,9 +154,9 @@
 // saturated slices.
 //
-void MExtractTimeAndChargeDigitalFilterPeakSearch::FindPeak(Byte_t *ptr, Byte_t &startslice, Int_t &max, 
+void MExtractTimeAndChargeDigitalFilterPeakSearch::FindPeak(Byte_t *ptr, Byte_t *logain, Byte_t &startslice, Int_t &max, 
 							    Int_t &sat, Byte_t &satpos) const
 {
 
-  const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
+  Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
 
   sat = 0;
@@ -208,4 +210,46 @@
     }
 
+  if (!fHiLoLast)
+    return;
+
+  Byte_t *l = logain;
+
+  while (++p < end && l < logain+fHiLoLast)
+    {
+      sum += *l - *p;
+      if (sum > max)
+	{
+	  max = sum;
+	  startslice = p-ptr+1;
+	}
+
+      if (*l++ >= fSaturationLimit)
+        {
+          if (sat == 0)
+            satpos = l-logain + fHiGainLast - fHiGainFirst;
+          sat++;
+        }
+    }
+  
+  if (fHiLoLast <= fPeakSearchWindowSize)
+    return;
+
+  while (l < logain+fHiLoLast)
+    {
+      if (*l++ >= fSaturationLimit)
+        {
+          if (sat == 0)
+            satpos = l-logain+fHiGainLast-fHiGainFirst;
+          sat++;
+        }
+
+      sum += *l - *(l-fPeakSearchWindowSize);
+      if (sum > max)
+	{
+	  max = sum;
+	  startslice = l-logain + fHiGainLast - fHiGainFirst - fPeakSearchWindowSize + 1;
+	}
+    }
+
   return;
 }
@@ -239,4 +283,5 @@
   Byte_t loGainFirstsave = fLoGainFirst;
   Byte_t loGainLastsave  = fLoGainLast; 
+  Byte_t hiLoLastsave    = fHiLoLast;
 
   Byte_t higainfirst     = fHiGainFirst;
@@ -248,5 +293,5 @@
       sat = 0;
 
-      FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, startslice, sumhi, sat, satpos);
+      FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), startslice, sumhi, sat, satpos);
 
       if (sumhi > maxsumhi && sat == 0)
@@ -279,12 +324,20 @@
   // Shift the last slice to the right:
   //
-  if (higainfirst + fOffsetRightFromPeak + fWindowSizeHiGain <= hiGainLastsave)
-    fHiGainLast  = higainfirst + fOffsetRightFromPeak + fWindowSizeHiGain;
+  const Byte_t rlim = higainfirst + fOffsetRightFromPeak + fWindowSizeHiGain;
+  if (rlim <= hiGainLastsave+fHiLoLast)
+    if (rlim > hiGainLastsave)
+      fHiLoLast   = rlim - fHiGainLast;
+    else
+      {
+        fHiLoLast   = 0;
+        fHiGainLast = rlim;
+      }
   else 
     fHiGainOutOfRangeRight++;
       
 
-  if ( fHiGainFirst+(Int_t)fOffsetLoGain >= fLoGainFirst ) 
-    fLoGainFirst = fHiGainFirst + (Int_t)fOffsetLoGain;
+  const Byte_t llim = fHiGainFirst + (Int_t)fOffsetLoGain;
+  if ( llim >= fLoGainFirst ) 
+    fLoGainFirst = llim;
   else
     fLoGainOutOfRangeLeft++;
@@ -302,4 +355,5 @@
   while (pixel.Next())
     {
+
       //
       // Find signal in hi- and lo-gain
@@ -384,4 +438,5 @@
   fLoGainFirst  = loGainFirstsave;
   fLoGainLast   = loGainLastsave ; 
+  fHiLoLast     = hiLoLastsave;
 
   return kTRUE;
