Index: trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc	(revision 3886)
+++ trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc	(revision 3887)
@@ -35,5 +35,24 @@
 //  determines the integration window position for all pixels of this event. 
 //  For the moment we neglect time delays between pixels (which are in most 
-//  cases small). The class is based on MExtractSignal2.
+//  cases small). The class is based on MExtractSlidingWindow.
+//
+//  Call: SetRange(higainfirst, higainlast, logainfirst, logainlast) 
+//  to modify the ranges in which the window is allowed to move. 
+//  Defaults are: 
+// 
+//   fHiGainFirst =  fgHiGainFirst =  0 
+//   fHiGainLast  =  fgHiGainLast  =  14
+//   fLoGainFirst =  fgLoGainFirst =  3 
+//   fLoGainLast  =  fgLoGainLast  =  14
+//
+//  Call: SetWindows(windowhigain, windowlogain,peaksearchwindow) 
+//  to modify the sliding window widths. Windows have to be an even number. 
+//  In case of odd numbers, the window will be modified.
+//
+//  Defaults are:
+//
+//   fHiGainWindowSize     = fgHiGainWindowSize     = 6
+//   fLoGainWindowSize     = fgLoGainWindowSize     = 6
+//   fPeakSearchWindowSize = fgPeakSearchWindowSize = 4
 //
 //////////////////////////////////////////////////////////////////////////////
@@ -61,5 +80,5 @@
 const Byte_t MExtractFixedWindowPeakSearch::fgHiGainFirst          = 0;
 const Byte_t MExtractFixedWindowPeakSearch::fgHiGainLast           = 14;
-const Byte_t MExtractFixedWindowPeakSearch::fgLoGainFirst          = 0;
+const Byte_t MExtractFixedWindowPeakSearch::fgLoGainFirst          = 3;
 const Byte_t MExtractFixedWindowPeakSearch::fgLoGainLast           = 14;
 // --------------------------------------------------------------------------
@@ -67,5 +86,16 @@
 // Default constructor. 
 //
+// Sets:
+// - fWindowSizeHiGain to fgWindowSizeHiGain
+// - fWindowSizeLoGain to fgWindowSizeLoGain
+// - fPeakSearchWindowSize to fgPeakSearchWindowSize
+//
+// Calls: 
+// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
+//
 MExtractFixedWindowPeakSearch::MExtractFixedWindowPeakSearch(const char *name, const char *title)
+    : fWindowSizeHiGain(fgHiGainWindowSize), 
+      fWindowSizeLoGain(fgLoGainWindowSize),
+      fPeakSearchWindowSize(fgPeakSearchWindowSize)
 {
 
@@ -74,45 +104,91 @@
 
   SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
-  SetWindows();
-}
-
+}
+
+// --------------------------------------------------------------------------
+//
+// SetRange: 
+//
+// Calls:
+// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
+// - SetWindows(fWindowSizeHiGain,fWindowSizeLoGain,fPeakSearchWindowSize);
+//
+void MExtractFixedWindowPeakSearch::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
+{
+
+  MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
+
+  //
+  // Redo the checks if the window is still inside the ranges
+  //
+  SetWindows(fWindowSizeHiGain,fWindowSizeLoGain,fPeakSearchWindowSize);
+  
+}
+
+// --------------------------------------------------------------------------
+//
+// Checks:
+// - if a window is odd, subtract one
+// - if a window is bigger than the one defined by the ranges, set it to the available range
+// - if a window is smaller than 2, set it to 2
+// 
+// Sets:
+// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
+// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
+// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
+// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)  
+//  
 void MExtractFixedWindowPeakSearch::SetWindows(Byte_t windowh, Byte_t windowl, Byte_t peaksearchwindow)
 {
-  //
-  // Set windows to even number of slices due to clock noise (odd/even slice effect).
-  //
-  fWindowSizeHiGain = windowh & ~1;
-  fWindowSizeLoGain = windowl & ~1;
+
+  fWindowSizeHiGain     = windowh & ~1;
+  fWindowSizeLoGain     = windowl & ~1;
   fPeakSearchWindowSize = peaksearchwindow & ~1;
 
-
   if (fWindowSizeHiGain != windowh)
-    *fLog << endl << warn << 
-      "MExtractFixedWindowPeakSearch::SetWindows - Hi Gain window size has to be even, set to: " 
-	  << int(fWindowSizeHiGain) << " samples " << endl;
+    *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: " 
+          << int(fWindowSizeHiGain) << " samples " << endl;
+  
+  if (fWindowSizeLoGain != windowl)
+    *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: " 
+          << int(fWindowSizeLoGain) << " samples " << endl;
     
-  if (fWindowSizeLoGain != windowl)
-    *fLog << endl << warn << 
-      "MExtractFixedWindowPeakSearch::SetWindows - Lo Gain window size has to be even, set to: " 
-	  << int(fWindowSizeLoGain) << " samples " << endl;
-
   if (fPeakSearchWindowSize != peaksearchwindow)
-    *fLog << endl << warn << 
-      "MExtractFixedWindowPeakSearch::SetWindows - Peak Search window size has to be even, set to: " 
-	  << int(fPeakSearchWindowSize) << " samples " << endl;
-
-
+    *fLog << warn << GetDescriptor() << ": Peak Search window size has to be even, set to: " 
+          << int(fPeakSearchWindowSize) << " samples " << endl;
+
+  const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
+  const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
+
+  if (fWindowSizeHiGain > availhirange)
+    {
+      *fLog << warn << GetDescriptor() 
+            << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
+                    " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
+      *fLog << warn << GetDescriptor() 
+            << ": Will set window size to: " << (int)availhirange << endl;
+      fWindowSizeHiGain = availhirange;
+    }
+  
+  if (fWindowSizeLoGain > availlorange)
+    {
+      *fLog << warn << GetDescriptor() 
+            << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain,
+                    " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
+      *fLog << warn << GetDescriptor() 
+            << ": Will set window size to: " << (int)availlorange << endl;
+      fWindowSizeLoGain = availlorange;
+    }
+  
   if (fWindowSizeHiGain<2) 
     {
       fWindowSizeHiGain = 2;
-      *fLog << warn 
-            << "MExtractFixedWindowPeakSearch::SetWindows - Hi Gain window size set to two samples" << endl;
-    }
-
+      *fLog << warn << GetDescriptor() << ": Hi Gain window size set to two samples" << endl;
+    }
+  
   if (fWindowSizeLoGain<2) 
     {
       fWindowSizeLoGain = 2;
-      *fLog << warn 
-            << "MExtractFixedWindowPeakSearch::SetWindows - Lo Gain window size set to two samples" << endl;
+      *fLog << warn << GetDescriptor() << ": Lo Gain window size set to two samples" << endl;
     }
 
@@ -120,6 +196,6 @@
     {
       fPeakSearchWindowSize = 2;
-      *fLog << warn 
-            << "MExtractFixedWindowPeakSearch::SetWindows - Peak Search window size set to two samples" << endl;
+      *fLog << warn << GetDescriptor() 
+            << ": Peak Search window size set to two samples" << endl;
     }
 
@@ -143,4 +219,5 @@
 			       Int_t &max, Int_t &sat) const
 {
+
   const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
 
@@ -188,8 +265,10 @@
 // --------------------------------------------------------------------------
 //
-// FindSignal
-// Adds up "window" slices starting in slice to which "ptr" points. The result 
-// is stored in "sum", and the number of saturated  slices in "sat".
-//
+// FindSignalHiGain:
+//
+// - Loop from ptr to (ptr+fWindowSizeHiGain)
+// - Sum up contents of *ptr
+// - If *ptr is greater than fSaturationLimit, raise sat by 1
+// 
 void MExtractFixedWindowPeakSearch::FindSignalHiGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
 {
@@ -213,8 +292,10 @@
 // --------------------------------------------------------------------------
 //
-// FindSignal
-// Adds up "window" slices starting in slice to which "ptr" points. The result 
-// is stored in "sum", and the number of saturated  slices in "sat".
-//
+// FindSignalLoGain:
+//
+// - Loop from ptr to (ptr+fWindowSizeLoGain)
+// - Sum up contents of *ptr
+// - If *ptr is greater than fSaturationLimit, raise sat by 1
+// 
 void MExtractFixedWindowPeakSearch::FindSignalLoGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
 {
@@ -262,17 +343,18 @@
       sat = 0;
 
-      FindPeak(pixel.GetHiGainSamples(), fPeakSearchWindowSize, startslice, sumhi, sat);
+      FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat);
+
       if (sumhi > maxsumhi && sat == 0 )
 	{
 	  maxsumhi = sumhi;
 	  if (startslice > 0)
-	    hiGainFirst = startslice-1;
+	    hiGainFirst = fHiGainFirst + startslice - 1;
 	  else
-	    hiGainFirst = 0;
+	    hiGainFirst = fHiGainFirst;
 	}
     }
 
 
-  loGainFirst = hiGainFirst;
+  loGainFirst = ( hiGainFirst > fLoGainFirst ) ? hiGainFirst : fLoGainFirst;
 
   // Make sure we will not integrate beyond the hi gain limit:
@@ -327,4 +409,5 @@
     } /* while (pixel.Next()) */
 
+
   fSignals->SetReadyToSave();
 
Index: trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h	(revision 3886)
+++ trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.h	(revision 3887)
@@ -18,8 +18,8 @@
   static const Byte_t fgPeakSearchWindowSize; // The window in which the global peak is searched for
 
+  Byte_t  fWindowSizeHiGain;     // Number of Hi Gain slices in window
+  Byte_t  fWindowSizeLoGain;     // Number of Lo Gain slices in window
   Byte_t  fPeakSearchWindowSize; // Size of FADC window in the search for the highest peak of all pixels.
 
-  Byte_t  fWindowSizeHiGain;     // Number of Hi Gain slices in window
-  Byte_t  fWindowSizeLoGain;     // Number of Lo Gain slices in window
 
   void   FindSignalHiGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const;
@@ -34,4 +34,5 @@
     MExtractFixedWindowPeakSearch(const char *name=NULL, const char *title=NULL);
 
+    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);
Index: trunk/MagicSoft/Mars/msignal/MExtractor.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 3886)
+++ trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 3887)
@@ -132,7 +132,4 @@
         return kFALSE;
 
-    fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
-                                fLoGainFirst, fLoGainLast, fNumLoGainSamples);
-
     fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber("MPedestalCam"));
 
@@ -162,4 +159,8 @@
 // modifying the ranges again, if necessary.
 //
+// Call: 
+// - MExtractedSignalCam::SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
+//                                          fLoGainFirst, fLoGainLast, fNumLoGainSamples);
+//
 Bool_t MExtractor::ReInit(MParList *pList)
 {
@@ -195,4 +196,7 @@
     }
 
+  fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
+                              fLoGainFirst, fLoGainLast, fNumLoGainSamples);
+
   return kTRUE;
 }
