Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5220)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5338)
@@ -19,7 +19,8 @@
 !   Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
 !   Author(s): Sebastian Commichau 12/2003 
-!   Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
+!   Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es> 
 !   Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
 !   Author(s): Florian Goebel 06/2004 <mailto:fgoebel@mppmu.mpg.de>
+!   Author(s): Nepomuk Otte 10/2004 <mailto:otte@mppmu.mpg.de>
 !
 !   Copyright: MAGIC Software Development, 2000-2004
@@ -29,9 +30,11 @@
 
 /////////////////////////////////////////////////////////////////////////////
+// 
+// 
 //
 //   MPedCalcLoGain
 //
 //
-//  This task is derived form MPedCalcPedRun, described below. However, It
+//  This task is devide form MPedCalcPedRun, described below. However, It 
 //  calculates the pedstals using the low gain slices, whenever the difference 
 //  between the highest and the lowest slice in the high gain
@@ -95,32 +98,29 @@
 //  Usage of this class: 
 //  ====================
-// 
-//  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  =  29
-//   fLoGainFirst =  fgLoGainFirst =  0 
-//   fLoGainLast  =  fgLoGainLast  =  14
-//
-//  Call: SetWindowSize(windowhigain, windowlogain) 
-//  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 = 14
-//   fLoGainWindowSize = fgLoGainWindowSize = 0
-//
-//  Variables:
-//   fgHiGainFirst;      First FADC slice Hi-Gain (currently set to: 3)
-//   fgHiGainLast:       Last FADC slice Hi-Gain (currently set to: 14)
-//   fgLoGainFirst:      First FADC slice Lo-Gain (currently set to: 3)
-//   fgLoGainLast:       Last FADC slice Lo-Gain (currently set to: 14)
-//   fgHiGainWindowSize: The extraction window Hi-Gain
-//   fgLoGainWindowSize: The extraction window Lo-Gain
-//   fgMaxHiGainVar:     The maximum difference between the highest and lowest slice
-//                       in the high gain window allowed in order to use low gain
+//  
+// 
+//   fCheckWinFirst =  fgCheckWinFirst =  0 
+//   fHiGainLast  =  fgCheckWinLast  =  29
+//   fExtractWinFirst =  fgExtractWinFirst =  0 
+//   fExtractWinSize  =  fgExtractWinSize  =  6
+//   fMaxSignalVar   =   fgMaxSignalVar  = 40;
+//
+//  Call: 
+//  SetCheckRange(fCheckWinFirst,fCheckWinLast);
+//  to set the Window in which a signal is searched
+//
+//  SetExtractWindow(fExtractWindFirst,fExtractWinSize);
+//  to set the Window from which a signal is extracted
+//
+//  SetMaxSignalVar(fMaxSignalVar);
+//  set the maximum allowed difference between maximum and minimal signal in CheckWindow  
+//
+//   Variables:
+//   fgCheckWinFirst;      First FADC slice to check for signal (currently set to: 0)
+//   fgCheckWinLast:       Last FADC slice to check for signal (currently set to: 29)
+//   fgExtractWinFirst:    First FADC slice to be used for pedestal extraction (currently set to: 15)
+//   fgExtractWinSize:     Window size in slices used for pedestal extraction (currently set to: 6)
+//   fgMaxSignalVar:       The maximum difference between the highest and lowest slice
+//                         in the check window allowed in order to use event
 //
 //  Input Containers:
@@ -157,11 +157,9 @@
 using namespace std;
 
-const Byte_t MPedCalcFromLoGain::fgHiGainFirst      = 0;
-const Byte_t MPedCalcFromLoGain::fgHiGainLast       = 11;
-const Byte_t MPedCalcFromLoGain::fgLoGainFirst      = 1;
-const Byte_t MPedCalcFromLoGain::fgLoGainLast       = 14;
-const Byte_t MPedCalcFromLoGain::fgHiGainWindowSize = 12;
-const Byte_t MPedCalcFromLoGain::fgLoGainWindowSize = 14;
-const Byte_t MPedCalcFromLoGain::fgMaxHiGainVar     = 40;
+const Int_t MPedCalcFromLoGain::fgCheckWinFirst      = 0;
+const Int_t MPedCalcFromLoGain::fgCheckWinLast       = 29;
+const Int_t MPedCalcFromLoGain::fgExtractWinFirst    = 15;
+const Int_t MPedCalcFromLoGain::fgExtractWinSize     = 6;
+const Int_t MPedCalcFromLoGain::fgMaxSignalVar       = 40;
 
 // --------------------------------------------------------------------------
@@ -171,32 +169,26 @@
 // Sets:
 // - all pointers to NULL
-// - fWindowSizeHiGain to fgHiGainWindowSize
-// - fWindowSizeLoGain to fgLoGainWindowSize
 //
 // Calls: 
 // - AddToBranchList("fHiGainPixId");
 // - AddToBranchList("fHiGainFadcSamples");
-// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
+// - SetCheckRange(fgCheckWinFirst, fgCheckWinLast, fgExtractWinFirst, fgExtractWinSize)
 // - Clear()
 //
 MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
-    : fWindowSizeHiGain(fgHiGainWindowSize), 
-      fWindowSizeLoGain(fgLoGainWindowSize), 
-      fGeom(NULL), fPedContainerName("MPedestalCam")
-{
-    fName  = name  ? name  : "MPedCalcFromLoGain";
-    fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
-
-    AddToBranchList("fHiGainPixId");
-    AddToBranchList("fLoGainPixId");
-    AddToBranchList("fHiGainFadcSamples");
-    AddToBranchList("fLoGainFadcSamples");
-
-    SetRange();
-    SetMaxHiGainVar();
-    SetPedestalUpdate(kTRUE);
-    SetNumEventsDump(500);
-
-    Clear();
+    : fGeom(NULL), fPedContainerName("MPedestalCam")
+{
+  fName  = name  ? name  : "MPedCalcFromLoGain";
+  fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
+  
+  AddToBranchList("fHiGainPixId");
+  AddToBranchList("fHiGainFadcSamples");
+  
+  SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
+  SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
+
+  SetMaxSignalVar(fgMaxSignalVar);
+  SetPedestalUpdate(kTRUE);
+  Clear();
 }
 
@@ -211,44 +203,56 @@
 void MPedCalcFromLoGain::Clear(const Option_t *o)
 {
-    fRawEvt    = NULL;
-    fRunHeader = NULL;
-    fPedestals = NULL;
-
-    // If the size is yet set, set the size
-    if (fSumx.GetSize()>0)
-    {
-        // Reset contents of arrays.
-        fSumx.Reset();
-        fSumx2.Reset();
-        fSumAB0.Reset();
-        fSumAB1.Reset();
-        fNumEventsUsed.Reset();
-        fTotalCounter.Reset();
-    }
-}
-
-// --------------------------------------------------------------------------
-//
-// SetRange: 
+  fRawEvt    = NULL;
+  fRunHeader = NULL;
+  fPedestals = NULL;
+
+  // If the size is yet set, set the size
+  if (fSumx.GetSize()>0)
+  {
+      // Reset contents of arrays.
+      fSumx.Reset();
+      fSumx2.Reset();
+      fSumAB0.Reset();
+      fSumAB1.Reset();
+      fNumEventsUsed.Reset();
+      fTotalCounter.Reset();
+  }
+}
+
+// --------------------------------------------------------------------------
+//
+// SetCheckRange: 
 //
 // Calls:
-// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
-// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
-//
-void MPedCalcFromLoGain::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
-    //
-    SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
-}
-
-// --------------------------------------------------------------------------
-//
-void MPedCalcFromLoGain::SetMaxHiGainVar(Byte_t maxvar)
-{
-    fMaxHiGainVar = maxvar;
+// - MExtractor::SetCheckRange(hifirst,hilast,lofirst,lolast);
+// - SetExtractWindow(fWindowSizeHiGain,fExtractWinSize);
+//
+void MPedCalcFromLoGain::SetCheckRange(Int_t chfirst, Int_t chlast)
+{
+
+
+  if(chfirst<0){
+    *fLog << warn << GetDescriptor() 
+           << Form(": First slice in window to check for Signal <0, adjust:")<< endl;
+    exit(-1);
+    }
+
+  if(chlast<=chfirst){
+    *fLog << warn << GetDescriptor() 
+    	  << Form(": Last slice in Check window smaller than first slice in window, adjust:")<< endl;
+    exit(-1);
+    }
+
+  fCheckWinFirst = chfirst;
+  fCheckWinLast = chlast;
+
+ 
+}
+
+// --------------------------------------------------------------------------
+//
+void MPedCalcFromLoGain::SetMaxSignalVar(Int_t maxvar)
+{
+  fMaxSignalVar = maxvar;
 }
 
@@ -256,77 +260,29 @@
 //
 // 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 MPedCalcFromLoGain::SetWindowSize(Byte_t windowh, Byte_t windowl)
-{
-
-    fWindowSizeHiGain = windowh & ~1;
-    fWindowSizeLoGain = windowl & ~1;
-  
-    if (fWindowSizeHiGain != windowh)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": HiGain window has to be even, set to: ";
-        *fLog << int(fWindowSizeHiGain) << " samples " << endl;
-    }
-    
-    if (fWindowSizeLoGain != windowl)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": Lo Gain window has to be even, set to: ";
-        *fLog << int(fWindowSizeLoGain) << " samples " << endl;
-    }
-
-    if (fWindowSizeHiGain == 0)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": HiGain window currently set to 0, will set it to 2 samples ";
-        fWindowSizeHiGain = 2;
-    }
-    
-    if (fWindowSizeLoGain == 0)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": LoGain window currently set to 0, will set it to 2 samples ";
-        fWindowSizeLoGain = 2;
-    }
-    
-    const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
-    const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
-  
-    if (fWindowSizeHiGain > availhirange)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": HiGain window " << (int)fWindowSizeHiGain;
-        *fLog << " out of range [" << (int)fHiGainFirst;
-        *fLog << "," << (int)fHiGainLast << "]" << endl;
-        *fLog << "Will set window size to " << (int)availhirange << endl;
-        fWindowSizeHiGain = availhirange;
-    }
-    
-    if (fWindowSizeLoGain > availlorange)
-    {
-        *fLog << warn;
-        *fLog << GetDescriptor() << ": LoGain window " << (int)fWindowSizeLoGain;
-        *fLog << " out of range [" << (int)fLoGainFirst;
-        *fLog << "," << (int)fLoGainLast << "]" << endl;
-        *fLog << "Will set window size to " << (int)availlorange << endl;
-        fWindowSizeLoGain = availlorange;
-    }
-    /*
-     fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
-     fNumLoGainSamples = (Float_t)fWindowSizeLoGain;
-
-     fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
-     fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
-    */
+// - if a window is odd
+// 
+ 
+void MPedCalcFromLoGain::SetExtractWindow(Int_t windowf, Int_t windows)
+{
+ 
+  if(windowf<0){
+    *fLog << warn << GetDescriptor() 
+           << Form(": First slice in Extract window has to be >0, adjust:")<< endl;
+    exit(-1);
+    }
+ 
+  Int_t odd  = windows & 0x1;
+ 
+
+  if (odd||(windows==0)){
+    *fLog << warn << GetDescriptor() << ": Extract window size has to be even and larger 0, adjust!"<< endl;
+    exit(-1);
+  }
+
+  fExtractWinSize = windows;
+  fExtractWinFirst = windowf;
+  fExtractWinLast = fExtractWinFirst+fExtractWinSize-1;
+
+ 
 }
 
@@ -369,19 +325,9 @@
     }
 
+
     fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fPedContainerName));
     if (!fPedestals)
         return kFALSE;
 
-    if (fNumEventsDump<=0 && fPedestalUpdate)
-    {
-        *fLog << warn << "Pedestal Update switched on and Number of Events to dump <= 0... fNumEventsDump=1000" << endl;
-        fNumEventsDump=1000;
-    }
-
-    *fLog << inf << "Continous update switched " << (fPedestalUpdate?"on":"off");
-    if (fPedestalUpdate)
-        *fLog << " (dump each " << fNumEventsDump << " events)" << endl;
-    *fLog << endl;
-
     return kTRUE;
 }
@@ -393,88 +339,59 @@
 // -  MRawRunHeader::GetNumSamplesLoGain()
 //
-// In case that the variables fHiGainLast and fLoGainLast are smaller than 
-// the even part of the number of samples obtained from the run header, a
-// warning is given an the range is set back accordingly. A call to:  
-// - SetRange(fHiGainFirst, fHiGainLast-diff, fLoGainFirst, fLoGainLast) or 
-// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff) 
-// is performed in that case. The variable diff means here the difference 
-// between the requested range (fHiGainLast) and the available one. Note that 
-// the functions SetRange() are mostly overloaded and perform more checks, 
-// modifying the ranges again, if necessary.
-//
+
 Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
 {
-    if (fRunHeader->GetNumSamplesHiGain()<1)
-    {
-        *fLog << err << "ERROR - Number of available HiGainSamples<1... abort." << endl;
-        return kFALSE;
-    }
-    if (fRunHeader->GetNumSamplesLoGain()<1)
-    {
-        *fLog << err << "ERROR - Number of available LoGainSamples<1... abort." << endl;
-        return kFALSE;
-    }
-
-    fHiGainFirst = TMath::Max(fHiGainFirst, 0);
-    fHiGainLast  = TMath::Min(fHiGainLast,  fRunHeader->GetNumSamplesHiGain()-1);
-
-    fLoGainFirst = TMath::Max(fLoGainFirst, 0);
-    fLoGainLast  = TMath::Min(fLoGainLast,  fRunHeader->GetNumSamplesLoGain()-1);
-
-    const Double_t wh = fWindowSizeHiGain;
-    const Double_t wl = fWindowSizeLoGain;
-    const Double_t fh = fHiGainFirst;
-    const Double_t fl = fLoGainFirst;;
-
-    fWindowSizeHiGain = TMath::Min(fWindowSizeHiGain, fHiGainLast-fHiGainFirst+1);
-    fWindowSizeLoGain = TMath::Min(fWindowSizeLoGain, fLoGainLast-fLoGainFirst+1);
-
-    SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast);
-
-    if (wh!=fWindowSizeHiGain || fh!=fHiGainFirst || wl!=fWindowSizeLoGain || fl!=fLoGainFirst)
-    {
-        *fLog << inf << endl;
-        *fLog << "Taking " << Form("%2d", (int)fWindowSizeHiGain) << " slices of Hi-Gain starting at slice " << (int)fHiGainFirst << endl;
-        *fLog << "Taking " << Form("%2d", (int)fWindowSizeLoGain) << " slices of Lo-Gain starting at slice " << (int)fLoGainFirst << endl;
-    }
-
-    if (fWindowSizeHiGain==0)
-    {
-        *fLog << err << "ERROR - HiGain windows size == 0... abort." << endl;
-        return kFALSE;
-    }
-    if (fWindowSizeLoGain==0)
-    {
-        *fLog << err << "ERROR - LoGain windows size == 0... abort." << endl;
-        return kFALSE;
-    }
-
-    // If the size is not yet set, set the size
-    if (fSumx.GetSize()==0)
-    {
-        const Int_t npixels = fPedestals->GetSize();
-
-        fSumx. Set(npixels);
-        fSumx2.Set(npixels);
-        fSumAB0.Set(npixels);
-        fSumAB1.Set(npixels);
-        fNumEventsUsed.Set(npixels);
-        fTotalCounter.Set(npixels);
-
-        // Reset contents of arrays.
-        fSumx.Reset();
-        fSumx2.Reset();
-        fSumAB0.Reset();
-        fSumAB1.Reset();
-        fNumEventsUsed.Reset();
-        fTotalCounter.Reset();
-    }
-
-    return kTRUE;
+
+  Int_t lastavailableslice = (Int_t)fRunHeader->GetNumSamplesHiGain()+(Int_t)fRunHeader->GetNumSamplesLoGain()-1;
+
+  Int_t lastextractslice =  fExtractWinSize+ fExtractWinFirst - 1;
+
+   if ( lastextractslice  > lastavailableslice)//changed to override check
+    {
+      *fLog << endl; 
+      *fLog << warn << GetDescriptor()
+            << Form(": Selected Extract Window ranges out of the available limits adjust. Last available slice is %2i",
+		    lastavailableslice) << endl;
+      exit(-1);
+    }
+
+  if (  fCheckWinLast  > lastavailableslice)//changed to override check
+    {
+      *fLog << endl; 
+      *fLog << warn << GetDescriptor()
+            << Form(": Last Check Window slice is out of the available limits adjust. Last available slice is %2i",
+		    lastavailableslice) << endl;
+      exit(-1);
+    }
+
+
+ 
+  // If the size is not yet set, set the size
+  if (fSumx.GetSize()==0)
+  {
+      const Int_t npixels = fPedestals->GetSize();
+
+      fSumx. Set(npixels);
+      fSumx2.Set(npixels);
+      fSumAB0.Set(npixels);
+      fSumAB1.Set(npixels);
+      fNumEventsUsed.Set(npixels);
+      fTotalCounter.Set(npixels);
+
+      // Reset contents of arrays.
+      fSumx.Reset();
+      fSumx2.Reset();
+      fSumAB0.Reset();
+      fSumAB1.Reset();
+      fNumEventsUsed.Reset();
+      fTotalCounter.Reset();
+  }
+  
+  return kTRUE;
 }
 
 void MPedCalcFromLoGain::Calc(ULong_t n, UInt_t idx)
 {
-    const ULong_t nsamplestot = n*fWindowSizeLoGain;
+    const ULong_t nsamplestot = n*fExtractWinSize;
 
     const Float_t sum  = fSumx.At(idx);
@@ -486,5 +403,5 @@
 
     // 2. Scale the variance to the number of slices:
-    var /= (Float_t)(fWindowSizeLoGain);
+    var /= (Float_t)(fExtractWinSize);
 
     // 3. Calculate the RMS from the Variance:
@@ -497,4 +414,25 @@
 
     fTotalCounter[idx]++;
+}
+
+UInt_t MPedCalcFromLoGain::GetSlice(MRawEvtPixelIter *pixel, UInt_t slice)
+{
+
+ const UInt_t nh = (Int_t)fRunHeader->GetNumSamplesHiGain();
+
+ Byte_t *ptr;
+
+ if(slice>=nh)
+   {
+     ptr = pixel->GetLoGainSamples();
+     ptr += slice - nh;
+   }
+ else
+   {
+    ptr = pixel->GetHiGainSamples();
+    ptr += slice;
+   }
+
+   return *ptr;
 }
 
@@ -511,36 +449,38 @@
     while (pixel.Next())
     {
-        const UInt_t idx = pixel.GetPixelId();
-
-        Byte_t *ptr = pixel.GetHiGainSamples() + fHiGainFirst;
-        Byte_t *end = ptr + fWindowSizeHiGain;
-
-        UInt_t sum = 0;
-        UInt_t sqr = 0;
-
-        UInt_t max = 0;
-        UInt_t min = 255;
+
+	 const UInt_t idx = pixel.GetPixelId();
+
+        Int_t max = 0;
+        Int_t min = 1025;
     
         // Find the maximum and minimum signal per slice in the high gain window
-        do {
-            if (*ptr > max)
-                max = *ptr;
-            if (*ptr < min)
-                min = *ptr;
-        } while (++ptr != end);
+     
+        for(Int_t slice=fCheckWinFirst; slice<=fCheckWinLast; slice++)
+	  {
+    
+	  Int_t svalue = GetSlice(&pixel,slice);
+            if (svalue > max) {
+                max = svalue;
+            }
+            if (svalue < min) {
+                min = svalue;
+            }
+        }
 
         // If the maximum in the high gain window is smaller than
-        if (max-min>=fMaxHiGainVar || max>=255)
+        if (max-min>=fMaxSignalVar || max>=250)
             continue;
 
-        ptr = pixel.GetLoGainSamples() + fLoGainFirst;
-        end = ptr + fWindowSizeLoGain;
-
-        Byte_t *firstSlice = ptr;
-
-        do {
-            sum += *ptr;
-            sqr += *ptr * *ptr;
-        } while (++ptr != end);
+	UInt_t sum = 0;
+        UInt_t sqr = 0;
+
+	//extract pedestal
+	for(Int_t slice=fExtractWinFirst; slice<=fExtractWinLast; slice++)
+	  {    
+	  UInt_t svalue = GetSlice(&pixel,slice);
+	  sum += svalue;
+	  sqr += svalue*svalue;
+        }
 
         const Float_t msum   = (Float_t)sum;
@@ -553,15 +493,14 @@
         // Calculate the amplitude of the 150MHz "AB" noise
 
-        if (pixel.IsABFlagValid())
+        Int_t abFlag = (fExtractWinFirst + pixel.HasABFlag()) & 0x1;
+
+	//cout << " MPedCalcFromLoGain: idx: " << idx << " abFlag: " << abFlag << endl;
+
+        for (Int_t islice=fExtractWinFirst; islice<=fExtractWinLast; islice+=2)
         {
-            const Int_t abFlag = (fRunHeader->GetNumSamplesHiGain()
-                                  + fLoGainFirst + pixel.HasABFlag()) & 0x1;
-            for (Int_t islice=0; islice<fWindowSizeLoGain; islice+=2)
-            {
-                const Int_t sliceAB0 = islice + abFlag;
-                const Int_t sliceAB1 = islice - abFlag + 1;
-                fSumAB0[idx] += firstSlice[sliceAB0];
-                fSumAB1[idx] += firstSlice[sliceAB1];
-            }
+            Int_t sliceAB0 = islice + abFlag;
+            Int_t sliceAB1 = islice - abFlag + 1;
+            fSumAB0[idx] += GetSlice(&pixel, sliceAB0);
+            fSumAB1[idx] += GetSlice(&pixel, sliceAB1);
         }
 
@@ -579,5 +518,5 @@
 
     if (fPedestalUpdate)
-      fPedestals->SetReadyToSave();
+        fPedestals->SetReadyToSave();
 
     return kTRUE;
@@ -591,10 +530,8 @@
 {
     // Compute pedestals and rms from the whole run
-    if (fPedestalUpdate || GetNumExecutions()<1)
+    if (fPedestalUpdate)
         return kTRUE;
 
-    *fLog << flush << inf << "Calculating pedestals..." << flush;
-
-    Double_t sum = 0;
+    *fLog << flush << inf << "Calculating Pedestals..." << flush;
 
     const Int_t npix = fGeom->GetNumPixels();
@@ -604,12 +541,7 @@
         if (n>1)
             Calc(n, idx);
-        sum += n;
-    }
-
-    *fLog << flush << inf << "Calculating means..." << flush;
-
-    fPedestals->SetTotalEntries((UInt_t)(sum/npix*(fWindowSizeLoGain+fWindowSizeHiGain)));
+    }
+
     fPedestals->SetReadyToSave();
-    fPedestals->ReCalc(*fGeom);
     return kTRUE;
 }
@@ -622,37 +554,39 @@
     Bool_t rc=kFALSE;
 
-    Byte_t hw = fWindowSizeHiGain;
-    Byte_t lw = fWindowSizeLoGain;
-    if (IsEnvDefined(env, prefix, "WindowSizeHiGain", print))
-    {
-        hw = GetEnvValue(env, prefix, "WindowSizeHiGain", hw);
+    Int_t lw = fExtractWinSize;
+    Int_t wf = fExtractWinFirst;
+
+    if (IsEnvDefined(env, prefix, "ExtractWindowSize", print))
+    {
+        lw = GetEnvValue(env, prefix, "ExtractWindowSize", lw);
+	wf = GetEnvValue(env, prefix, "ExtractWindowFirst", wf);
         rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "WindowSizeLoGain", print))
-    {
-        lw = GetEnvValue(env, prefix, "WindowSizeLoGain", lw);
+
+    }
+    SetExtractWindow(wf,lw);
+
+    Int_t num = fNumEventsDump;
+    if (IsEnvDefined(env, prefix, "NumEventsDump", print))
+    {
+        num = GetEnvValue(env, prefix, "NumEventsDump", num);
         rc = kTRUE;
     }
-
-    if (rc)
-        SetWindowSize(hw, lw);
-
-    if (IsEnvDefined(env, prefix, "NumEventsDump", print))
-    {
-        SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", fNumEventsDump));
+    SetNumEventsDump(num);
+
+    Byte_t max = fMaxSignalVar;
+    if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
+    {
+        max = GetEnvValue(env, prefix, "MaxSignalVar", max);
         rc = kTRUE;
     }
-
-    if (IsEnvDefined(env, prefix, "MaxHiGainVar", print))
-    {
-        SetMaxHiGainVar(GetEnvValue(env, prefix, "MaxHiGainVar", fMaxHiGainVar));
+    SetMaxSignalVar(max);
+
+    Bool_t upd = fPedestalUpdate;
+    if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
+    {
+        upd = GetEnvValue(env, prefix, "PedestalUpdate", upd);
         rc = kTRUE;
     }
-
-    if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
-    {
-        SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
-        rc = kTRUE;
-    }
+    SetPedestalUpdate(upd);
 
     return rc;
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h	(revision 5220)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h	(revision 5338)
@@ -16,20 +16,22 @@
 class MGeomCam;
 class MBadPixelsCam;
+class MRawEvtPixelIter;
 
 class MPedCalcFromLoGain : public MExtractor
 {
-    static const Byte_t fgHiGainFirst;      // First FADC slice Hi-Gain (currently set to: 3)
-    static const Byte_t fgHiGainLast;       // Last FADC slice Hi-Gain (currently set to: 14)
-    static const Byte_t fgLoGainFirst;      // First FADC slice Lo-Gain (currently set to: 3)
-    static const Byte_t fgLoGainLast;       // Last FADC slice Lo-Gain (currently set to: 14)
-    static const Byte_t fgHiGainWindowSize; // The extraction window Hi-Gain
-    static const Byte_t fgLoGainWindowSize; // The extraction window Lo-Gain
-    static const Byte_t fgMaxHiGainVar;     // The maximum difference between the highest and lowest slice
+    static const Int_t fgCheckWinFirst;      // First FADC slice to check for signal (currently set to: 0)
+    static const Int_t fgCheckWinLast;       // Last FADC slice to check for signal  (currently set to: 29)
+    static const Int_t fgExtractWinFirst;      // First FADC slice to use for pedestal calculation (currently set to: 15)
+    static const Int_t fgExtractWinSize;       // number of successive slices used to calculate pedestal (currently set to: 6)
+    static const Int_t fgMaxSignalVar;     // The maximum difference between the highest and lowest slice
 
     Int_t   fNumEventsDump;      // Number of event after which MPedestalCam gets updated
 
-    Byte_t  fMaxHiGainVar;
-    Byte_t  fWindowSizeHiGain;   // Number of Hi Gain slices in window
-    Byte_t  fWindowSizeLoGain;   // Number of Lo Gain slices in window
+    Int_t  fMaxSignalVar;
+    Int_t  fCheckWinFirst;
+    Int_t  fCheckWinLast;
+    Int_t  fExtractWinSize;              // Number of slices to calculate the pedestal from
+    Int_t  fExtractWinFirst;
+    Int_t  fExtractWinLast;
 
     Bool_t  fPedestalUpdate;
@@ -55,4 +57,7 @@
     void Calc(ULong_t n, UInt_t idx);
 
+    //Helper function to extract slice values by slice number
+    UInt_t GetSlice(MRawEvtPixelIter *pixel, UInt_t slice);
+
 public:
     MPedCalcFromLoGain(const char *name=NULL, const char *title=NULL);
@@ -62,8 +67,8 @@
 
     // Setter
-    void SetRange(Byte_t hifirst=fgHiGainFirst, Byte_t hilast=fgHiGainLast, Byte_t lofirst=fgLoGainFirst, Byte_t lolast=fgLoGainLast);
-    void SetWindowSize(Byte_t windowh=fgHiGainWindowSize, Byte_t windowl=fgLoGainWindowSize);
-    void SetMaxHiGainVar(Byte_t maxvar=fgMaxHiGainVar);
-    void SetNumEventsDump(UInt_t dumpevents = 0) { fNumEventsDump = dumpevents; }
+    void SetCheckRange(Int_t checkfirst=0, Int_t checklast=29);
+    void SetExtractWindow(Int_t extractfirst=15, Int_t windowsize=6);
+    void SetMaxSignalVar(Int_t maxvar=0);
+    void SetNumEventsDump(UInt_t dumpevents = 0) {fNumEventsDump = dumpevents;}
     void SetPedestalUpdate(Bool_t pedupdate)  {fPedestalUpdate = pedupdate;}
 
