Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 5497)
+++ trunk/MagicSoft/Mars/Changelog	(revision 5498)
@@ -39,4 +39,9 @@
        still extracted. This behaviour is corrected now, I hope it 
        does not affect any current analysis!
+
+   * mbase/MStatusDisplay.[h,cc]
+     - added GetterFunction for one dedicated pad in the status display.
+       The pad gets resized to normal size, then. 
+
 
  2004/11/26: Markus Gaug
Index: trunk/MagicSoft/Mars/mbase/MStatusDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MStatusDisplay.cc	(revision 5497)
+++ trunk/MagicSoft/Mars/mbase/MStatusDisplay.cc	(revision 5498)
@@ -761,4 +761,28 @@
 // --------------------------------------------------------------------------
 //
+// Returns j-th pad of the i-th Tab. 
+// Sets the pad to fill an entire window.
+//
+// This function can be used if single pad's out of an MStatusDisplay 
+// have to be stored to file. 
+//
+// ATTENTION: This function modifies the requested tab in MStatusDisplay itself!
+//
+TVirtualPad *MStatusDisplay::GetFullPad(const Int_t i, const Int_t j)
+{
+  
+  TVirtualPad *vpad = GetCanvas(i)->GetPad(j);
+  if (vpad)
+    vpad->SetPad(0.,0.,1.,1.);
+  else
+    *fLog << warn << "MStatusDisplay::GetFullPad: Pad is out of range." << endl;
+
+  return vpad;
+}
+
+
+
+// --------------------------------------------------------------------------
+//
 // Searches for a TRootEmbeddedCanvas in the TGCompositeFramme of the
 // Tab with the name 'name'. Returns the corresponding TCanvas or
Index: trunk/MagicSoft/Mars/mbase/MStatusDisplay.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MStatusDisplay.h	(revision 5497)
+++ trunk/MagicSoft/Mars/mbase/MStatusDisplay.h	(revision 5498)
@@ -159,5 +159,6 @@
      TCanvas *GetCanvas(int i) const;
      TCanvas *GetCanvas(const TString &name) const;
-
+     TVirtualPad *GetFullPad(const Int_t canvas, const Int_t pad);
+     
      Int_t Read(const char *name="MStatusDisplay");
      Int_t Write(const char *name="MStatusDisplay", Int_t option=0, Int_t bufsize=0)
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5497)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5498)
@@ -33,6 +33,6 @@
 //   MPedCalcLoGain
 //
-//  This task is devide form MPedCalcPedRun, described below. However, It 
-//  calculates the pedstals using the low gain slices, whenever the difference 
+//  This task derives from MExtractPedestal. 
+//  It calculates the pedestals using the low gain slices, whenever the difference 
 //  between the highest and the lowest slice in the high gain
 //  slices is below a given threshold (SetMaxHiGainVar). In this case the receiver
@@ -73,4 +73,5 @@
 <img src="images/PedestalStudyOuter.gif">
 */
+//End_Html
 //
 // The plots show the inner and outer pixels, respectivly and have the following meaning:
@@ -97,9 +98,9 @@
 //  
 // 
-//   fCheckWinFirst =  fgCheckWinFirst =  0 
-//   fHiGainLast  =  fgCheckWinLast  =  29
+//   fCheckWinFirst   =  fgCheckWinFirst   =  0 
+//   fCheckWinLast    =  fgCheckWinLast    =  29
 //   fExtractWinFirst =  fgExtractWinFirst =  0 
 //   fExtractWinSize  =  fgExtractWinSize  =  6
-//   fMaxSignalVar   =   fgMaxSignalVar  = 40;
+//   fMaxSignalVar    =  fgMaxSignalVar    = 40;
 //
 //  Call: 
@@ -133,5 +134,7 @@
 /////////////////////////////////////////////////////////////////////////////
 #include "MPedCalcFromLoGain.h"
-#include "MExtractor.h"
+#include "MExtractPedestal.h"
+
+#include "MExtractTimeAndCharge.h"
 
 #include "MParList.h"
@@ -159,5 +162,4 @@
 const UShort_t MPedCalcFromLoGain::fgExtractWinSize  =  6;
 const UShort_t MPedCalcFromLoGain::fgMaxSignalVar    = 40;
-
 // --------------------------------------------------------------------------
 //
@@ -174,59 +176,24 @@
 //
 MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
-    : fGeom(NULL), fNamePedestalCam("MPedestalCam")
-{
-    fName  = name  ? name  : "MPedCalcFromLoGain";
-    fTitle = title ? title : "Task to calculate pedestals from lo-gains";
-
-    AddToBranchList("fHiGainPixId");
-    AddToBranchList("fLoGainPixId");
-    AddToBranchList("fHiGainFadcSamples");
-    AddToBranchList("fLoGainFadcSamples");
-
-    SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
-    SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
-
-    SetMaxSignalVar(fgMaxSignalVar);
-    SetPedestalUpdate(kTRUE);
-
-    Clear();
+{
+
+  fName  = name  ? name  : "MPedCalcFromLoGain";
+  fTitle = title ? title : "Task to calculate pedestals from lo-gains";
+  
+  SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
+  SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
+  
+  SetMaxSignalVar(fgMaxSignalVar);
+  
+  Clear();
 }
 
 void MPedCalcFromLoGain::ResetArrays()
 {
-    // Reset contents of arrays.
-    fSumx.Reset();
-    fSumx2.Reset();
-    fSumAB0.Reset();
-    fSumAB1.Reset();
-    fNumEventsUsed.Reset();
-    fTotalCounter.Reset();
-}
-
-// --------------------------------------------------------------------------
-//
-// Sets:
-// - fRawEvt to NULL
-// - fRunHeader to NULL
-// - fPedestals to NULL
-//
-// Resets:
-// - fSumx
-// - fSumx2
-// - fSumAB0
-// - fSumAB1
-// - fNumEventsUsed
-// - fTotalCounter
-//
-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)
-      ResetArrays();
+
+  MExtractPedestal::ResetArrays();
+  
+  fNumEventsUsed.Reset();
+  fTotalCounter.Reset();
 }
 
@@ -240,120 +207,19 @@
 Bool_t MPedCalcFromLoGain::SetCheckRange(UShort_t chfirst, UShort_t chlast)
 {
-    Bool_t rc = kTRUE;
-
-    if (chlast<=chfirst)
-    {
-        *fLog << warn << GetDescriptor();
-        *fLog << " - WARNING: Last slice in SetCheckRange smaller than first slice... set to first+2" << endl;
-        chlast = chfirst+1;
-        rc = kFALSE;
-    }
-
-    fCheckWinFirst = chfirst;
-    fCheckWinLast  = chlast;
-
-    return rc;
-}
-
-// --------------------------------------------------------------------------
-//
-// Checks:
-// - if a window is odd
-// 
-Bool_t MPedCalcFromLoGain::SetExtractWindow(UShort_t windowf, UShort_t windows)
-{
-    Bool_t rc = kTRUE;
-
-    const Int_t odd  = windows & 0x1;
-
-    if (odd || windows==0)
-    {
-        *fLog << warn << GetDescriptor();
-        *fLog << " - WARNING: Window size in SetExtraxtWindow has to be even and > 0... adjusting!" << endl;
-        windows += 1;
-        rc = kFALSE;
-    }
-
-    fExtractWinSize  = windows;
-    fExtractWinFirst = windowf;
-    fExtractWinLast  = fExtractWinFirst+fExtractWinSize-1;
-
-//
-//  NO RANGE CHECK IMPLEMENTED, YET
-//
-/*
-    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;
-    }
-    */
+
+  Bool_t rc = kTRUE;
+  
+  if (chlast<=chfirst)
+    {
+      *fLog << warn << GetDescriptor();
+      *fLog << " - WARNING: Last slice in SetCheckRange smaller than first slice... set to first+2" << endl;
+      chlast = chfirst+1;
+      rc = kFALSE;
+    }
+
+  fCheckWinFirst = chfirst;
+  fCheckWinLast  = chlast;
 
   return rc;
-}
-
-// --------------------------------------------------------------------------
-//
-// Look for the following input containers:
-//
-//  - MRawEvtData
-//  - MRawRunHeader
-//  - MGeomCam
-// 
-// The following output containers are also searched and created if
-// they were not found:
-//
-//  - MPedestalCam with the name fPedContainerName
-//
-Int_t MPedCalcFromLoGain::PreProcess(MParList *pList)
-{
-    Clear();
-
-    fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
-    if (!fRawEvt)
-    {
-        *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
-    if (!fRunHeader)
-    {
-        *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
-    if (!fGeom)
-    {
-        *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCam));
-    if (!fPedestals)
-        return kFALSE;
-
-    *fLog << inf;
-    Print();
-
-    return kTRUE;
 }
 
@@ -367,88 +233,48 @@
 //    fCheckWinLast
 //
-// Sets the size (from MPedestalCam::GetSize() ) and resets the following arrays:
-//  - fSumx
-//  - fSumx2
-//  - fSumAB0
-//  - fSumAB1
-//  - fNumEventsUsed
-//  - fTotalCounter
-//
 Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
 {
-  Int_t lastavailableslice = fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain()-1;
-  Int_t lastextractslice   = fExtractWinSize + fExtractWinFirst - 1;
-
-   if (lastextractslice>lastavailableslice)//changed to override check
+
+  const UShort_t hisamples = fRunHeader->GetNumSamplesHiGain();
+  const UShort_t losamples = fRunHeader->GetNumSamplesLoGain();
+
+  if (fExtractor)
+    SetExtractWindow(hisamples+fExtractor->GetHiGainFirst(),hisamples+fExtractor->GetHiGainLast());
+  
+  // If the size is not yet set, set the size
+  if (fSumx.GetSize()==0)
+  {
+    const Int_t npixels  = fPedestalsOut->GetSize();
+    fNumEventsUsed.Set(npixels);
+    fTotalCounter.Set(npixels); 
+  }
+  
+  MExtractPedestal::ReInit(pList);
+  
+  UShort_t lastavailable = hisamples+losamples-1;
+
+   if (fExtractWinLast > lastavailable) //changed to override check
    {
-       *fLog << warn << GetDescriptor();
-       *fLog << " - WARNING: Selected Extract Window ranges out of range...adjusting to last available slice ";
-       *fLog << lastavailableslice << endl;
-
-       lastextractslice=lastavailableslice;
+     const UShort_t diff = fExtractWinLast - lastavailable;
+     *fLog << warn << GetDescriptor();
+     *fLog << " - WARNING: Selected Extract Window ranges out of range...adjusting to last available slice ";
+     *fLog << lastavailable << endl;
+     
+     fExtractWinLast -= diff;
+     fExtractWinSize -= diff;
    }
 
-   if (fCheckWinLast>lastavailableslice)//changed to override check
+   lastavailable = fRunHeader->GetNumSamplesHiGain()-1;
+
+   if (fCheckWinLast > lastavailable) //changed to override check
    {
        *fLog << warn << GetDescriptor();
        *fLog << " - WARNING: Last Check Window slice out of range...adjusting to last available slice ";
-       *fLog << lastavailableslice << endl;
-
-       fCheckWinLast=lastavailableslice;
+       *fLog << lastavailable << endl;
+
+       fCheckWinLast = lastavailable;
    }
 
-
-  // 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);
-
-      ResetArrays();
-  }
-  
   return kTRUE;
-}
-
-// ---------------------------------------------------------------------------------
-//
-// Calculates for pixel "idx":
-//
-// Ped per slice      = sum / n / fExtractWinSize;
-// RMS per slice      = sqrt { (sum2 -  sum*sum/n) / (n-1) / fExtractWinSize }
-// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / n / fExtractWinSize;
-//
-// Sets fTotalCounter up by 1.
-//
-void MPedCalcFromLoGain::Calc(ULong_t n, UInt_t idx)
-{
-
-  const ULong_t nsamplestot = n*fExtractWinSize;
-  
-  const Float_t sum  = fSumx.At(idx);
-  const Float_t sum2 = fSumx2.At(idx);
-  const Float_t ped  = sum/nsamplestot;
-  
-  // 1. Calculate the Variance of the sums:
-  Float_t var = (sum2-sum*sum/n)/(n-1.);
-
-  // 2. Scale the variance to the number of slices:
-  var /= fExtractWinSize;
-  
-  // 3. Calculate the RMS from the Variance:
-  const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
-  
-  // 4. Calculate the amplitude of the 150MHz "AB" noise
-  const Float_t abOffs = (fSumAB0[idx] - fSumAB1[idx]) / nsamplestot;
-  
-  (*fPedestals)[idx].Set(ped, rms, abOffs, n);
-  
-  fTotalCounter[idx]++;
 }
 
@@ -479,94 +305,188 @@
 Int_t MPedCalcFromLoGain::Process()
 {
-    MRawEvtPixelIter pixel(fRawEvt);
-
-    while (pixel.Next())
-    {
-        const UInt_t idx = pixel.GetPixelId();
-
-        UShort_t max = 0;
-        UShort_t min = (UShort_t)-1;
-
-        // Find the maximum and minimum signal per slice in the high gain window
-        for (Int_t slice=fCheckWinFirst; slice<=fCheckWinLast; slice++)
+
+  MRawEvtPixelIter pixel(fRawEvt);
+  
+  while (pixel.Next())
+    {
+      const UInt_t idx = pixel.GetPixelId();
+      const UInt_t aidx   = (*fGeom)[idx].GetAidx();
+      const UInt_t sector = (*fGeom)[idx].GetSector();      
+      
+      UShort_t max = 0;
+      UShort_t min = (UShort_t)-1;
+      
+      // Find the maximum and minimum signal per slice in the high gain window
+      for (Int_t slice=fCheckWinFirst; slice<=fCheckWinLast; slice++)
         {
-            const UShort_t svalue = GetSlice(&pixel,slice);
-            if (svalue > max)
-                max = svalue;
-            if (svalue < min)
-                min = svalue;
+          const UShort_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>=fMaxSignalVar || max>=250)
+      
+      // If the maximum in the high gain window is smaller than
+      if (max-min>=fMaxSignalVar || max>=250)
+        continue;
+      
+      Float_t sum  = 0.;
+      UInt_t  sumi = 0;
+      
+      //extract pedestal
+      if (fExtractor)
+        CalcExtractor( &pixel, sum, (*fPedestalsIn)[idx] );
+      else
+        {
+          for(Int_t slice=fExtractWinFirst; slice<=fExtractWinLast; slice++)
+            sumi += GetSlice(&pixel,slice);
+          sum = (Float_t)sumi;
+        }
+      
+      const Float_t sqrsum = sum*sum;
+      
+      fSumx[idx]           += sum;
+      fSumx2[idx]          += sqrsum;
+      fAreaSumx[aidx]      += sum;
+      fAreaSumx2[aidx]     += sqrsum;
+      fSectorSumx[sector]  += sum;      
+      fSectorSumx2[sector] += sqrsum;      
+      
+      fNumEventsUsed[idx]   ++;
+      fAreaFilled   [aidx]  ++;
+      fSectorFilled [sector]++;
+      
+      if (!fExtractor)
+        {
+          //
+          // Calculate the amplitude of the 150MHz "AB" noise
+          //
+          const UShort_t abFlag = (fExtractWinFirst + pixel.HasABFlag()) & 0x1;
+          
+          for (UShort_t islice=fExtractWinFirst; islice<=fExtractWinLast; islice+=2)
+            {
+              const UShort_t sliceAB0 = islice + abFlag;
+              const UShort_t sliceAB1 = islice - abFlag + 1;
+              const UShort_t ab0 = GetSlice(&pixel, sliceAB0);
+              const UShort_t ab1 = GetSlice(&pixel, sliceAB1);
+              fSumAB0[idx]        += ab0;
+              fSumAB1[idx]        += ab1;
+              fAreaSumAB0[aidx]   += ab0;
+              fAreaSumAB1[aidx]   += ab1;      
+              fSectorSumAB0[aidx] += ab0;
+              fSectorSumAB1[aidx] += ab1;      
+            }
+        }
+      
+      if (!fPedestalUpdate || (UInt_t)fNumEventsUsed[idx]<fNumEventsDump)
+        continue;
+      
+      CalcPixResults(fNumEventsDump, idx);
+      fTotalCounter[idx]++;
+      
+      fNumEventsUsed[idx]=0;
+      fSumx[idx]=0;
+      fSumx2[idx]=0;
+      fSumAB0[idx]=0;
+      fSumAB1[idx]=0;
+    }
+  
+  if (!(GetNumExecutions() % fNumAreasDump))
+    {
+      //
+      // Loop over the (two) area indices to get the averaged pedestal per aidx
+      //
+      for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
+        {
+          const Int_t   napix = fAreaValid.At(aidx);
+          if (napix == 0)
             continue;
-
-	UInt_t sum = 0;
-        UInt_t sqr = 0;
-
-	//extract pedestal
-	for(Int_t slice=fExtractWinFirst; slice<=fExtractWinLast; slice++)
+          
+          CalcAreaResults(fSectorFilled[aidx],napix,aidx);
+        }
+    }
+  
+  if (!(GetNumExecutions() % fNumSectorsDump))
+    {
+      //
+      // Loop over the (two) sector indices to get the averaged pedestal per sector
+      //
+      for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
         {
-            const UInt_t svalue = GetSlice(&pixel,slice);
-            sum += svalue;
-            sqr += svalue*svalue;
+          const Int_t   nspix = fSectorValid.At(sector);
+          if (nspix == 0)
+            continue;
+          
+          CalcSectorResults(fSectorFilled[sector],nspix,sector);
         }
-
-        fSumx[idx]  += sum;
-        fSumx2[idx] += sum*sum;
-
-        fNumEventsUsed[idx]++;
-
-        // Calculate the amplitude of the 150MHz "AB" noise
-        UShort_t abFlag = (fExtractWinFirst + pixel.HasABFlag()) & 0x1;
-
-        for (UShort_t islice=fExtractWinFirst; islice<=fExtractWinLast; islice+=2)
+    }
+  
+  if (fPedestalUpdate)
+    fPedestalsOut->SetReadyToSave();
+  
+  return kTRUE;
+}
+
+void MPedCalcFromLoGain::CalcExtractor( MRawEvtPixelIter *pixel, Float_t &sum, MPedestalPix &ped)
+{
+  
+  Byte_t  sat  = 0;
+  Byte_t *logain = pixel->GetLoGainSamples() + fExtractWinFirst;
+  const Bool_t logainabflag = (pixel->HasABFlag() + pixel->GetNumHiGainSamples()) & 0x1;
+  Float_t dummy;
+  fExtractor->FindTimeAndChargeLoGain(logain,sum,dummy,dummy,dummy,sat,ped,logainabflag);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Compute signal mean and rms in the whole run and store it in MPedestalCam
+//
+Int_t MPedCalcFromLoGain::PostProcess()
+{
+
+  // Compute pedestals and rms from the whole run
+  if (fPedestalUpdate)
+    return kTRUE;
+  
+  *fLog << flush << inf << "Calculating Pedestals..." << flush;
+  
+  const Int_t npix = fGeom->GetNumPixels();
+  for (Int_t idx=0; idx<npix; idx++)
+    {
+      const ULong_t n = fNumEventsUsed[idx];
+      if (n>1)
         {
-            const UShort_t sliceAB0 = islice + abFlag;
-            const UShort_t sliceAB1 = islice - abFlag + 1;
-            fSumAB0[idx] += GetSlice(&pixel, sliceAB0);
-            fSumAB1[idx] += GetSlice(&pixel, sliceAB1);
+          CalcPixResults(n, idx);
+          fTotalCounter[idx]++;
         }
-
-        if (!fPedestalUpdate || (UInt_t)fNumEventsUsed[idx]<fNumEventsDump)
-          continue;
-
-        Calc(fNumEventsDump, idx);
-
-        fNumEventsUsed[idx]=0;
-        fSumx[idx]=0;
-        fSumx2[idx]=0;
-        fSumAB0[idx]=0;
-        fSumAB1[idx]=0;
-    }
-
-    if (fPedestalUpdate)
-      fPedestals->SetReadyToSave();
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-// Compute signal mean and rms in the whole run and store it in MPedestalCam
-//
-Int_t MPedCalcFromLoGain::PostProcess()
-{
-    // Compute pedestals and rms from the whole run
-    if (fPedestalUpdate)
-      return kTRUE;
-
-    *fLog << flush << inf << "Calculating Pedestals..." << flush;
-
-    const Int_t npix = fGeom->GetNumPixels();
-    for (Int_t idx=0; idx<npix; idx++)
-      {
-        const ULong_t n = fNumEventsUsed[idx];
-        if (n>1)
-          Calc(n, idx);
-      }
-
-    fPedestals->SetReadyToSave();
-    return kTRUE;
+    }
+
+  //
+  // Loop over the (two) area indices to get the averaged pedestal per aidx
+  //
+  for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
+    {
+      const Int_t   napix = fAreaValid.At(aidx);
+      if (napix == 0)
+        continue;
+
+      CalcAreaResults(fAreaFilled[aidx],napix,aidx);
+    }
+  
+  //
+  // Loop over the (six) sector indices to get the averaged pedestal per sector
+  //
+  for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
+    {
+      const Int_t   nspix = fSectorValid.At(sector);
+      if (nspix == 0)
+        continue;
+      
+      CalcSectorResults(fSectorFilled[sector],nspix,sector);
+    }
+
+  fPedestalsOut->SetReadyToSave();
+  return kTRUE;
 }
 
@@ -577,78 +497,44 @@
 //    FirstCheckWindowSlice:  0
 //    LastCheckWindowSlice:  29
-//    ExtractWindowFirst:    15
-//    ExtractWindowSize:      6
-//    NumEventsDump:        500
 //    MaxSignalVar:          40
-//    PedestalUpdate:       yes
 //
 Int_t MPedCalcFromLoGain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
-    Bool_t rc=kFALSE;
-
-    // Find resources for CheckWindow
-    Int_t fs = fCheckWinFirst;
-    Int_t ls = fCheckWinLast;
-    if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
-    {
-        fs = GetEnvValue(env, prefix, "CheckWinFirst", fs);
-	rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "CheckWinLast", print))
-    {
-	ls = GetEnvValue(env, prefix, "CheckWinLast", ls);
-        rc = kTRUE;
-    }
-
-    SetCheckRange(fs,ls);
-
-    // Find resources for ExtractWindow
-    Int_t lw = fExtractWinSize;
-    Int_t wf = fExtractWinFirst;
-    if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
-    {
-        lw = GetEnvValue(env, prefix, "ExtractWinSize", lw);
-        rc = kTRUE;
-    }
-
-    if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
-    {
-        wf = GetEnvValue(env, prefix, "ExtractWinFirst", wf);
-        rc = kTRUE;
-    }
-    SetExtractWindow(wf,lw);
-
-    // find resource for numeventsdump
-    if (IsEnvDefined(env, prefix, "NumEventsDump", print))
-    {
-        SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
-        rc = kTRUE;
-    }
-
-    // find resource for maximum signal variation
-    if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
-    {
-        SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
-        rc = kTRUE;
-    }
-
-    // find resource for pedestal update
-    if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
-    {
-        SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
-        rc = kTRUE;
-    }
-
-    return rc;
+
+  Bool_t rc=kFALSE;
+  
+  // Find resources for CheckWindow
+  Int_t fs = fCheckWinFirst;
+  Int_t ls = fCheckWinLast;
+  if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
+    {
+      fs = GetEnvValue(env, prefix, "CheckWinFirst", fs);
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "CheckWinLast", print))
+    {
+      ls = GetEnvValue(env, prefix, "CheckWinLast", ls);
+      rc = kTRUE;
+    }
+  
+  SetCheckRange(fs,ls);
+  
+  // find resource for maximum signal variation
+  if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
+    {
+      SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
+      rc = kTRUE;
+    }
+  
+  return MExtractPedestal::ReadEnv(env,prefix,print) ? kTRUE : rc;
 }
 
 void MPedCalcFromLoGain::Print(Option_t *o) const
 {
-    *fLog << GetDescriptor() << ":" << endl;
-    *fLog << "Parameters used for pedestal calculation from " << fNamePedestalCam << ":"<<endl;
-    *fLog << "CheckWindow from slice " << fCheckWinFirst   << " to " << fCheckWinLast << " included." << endl;
-    *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " included." << endl;
-    *fLog << "Max allowed signal variation: " << fMaxSignalVar << endl;
-    *fLog << "Number of events to calculate pedestal from: " << fNumEventsDump << endl;
-    *fLog << "Pedestal Update is " << (fPedestalUpdate?"on":"off") << endl;
-}
+
+  MExtractPedestal::Print(o);
+
+  *fLog << "CheckWindow   from slice  " << fCheckWinFirst   << " to " << fCheckWinLast << " incl." << endl;
+  *fLog << "Max. allowed signal variation:             " << fMaxSignalVar << endl;
+  *fLog << endl;
+}
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h	(revision 5497)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h	(revision 5498)
@@ -2,6 +2,6 @@
 #define MARS_MPedCalcFromLoGain
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MExtractPedestal
+#include "MExtractPedestal.h"
 #endif
 
@@ -14,78 +14,47 @@
 #endif
 
-class MGeomCam;
-class MPedestalCam;
-class MRawEvtData;
 class MRawEvtPixelIter;
-class MRawRunHeader;
+class MPedestalPix;
+class MPedCalcFromLoGain : public MExtractPedestal
+{
 
-class MPedCalcFromLoGain : public MTask
-{
-    static const UShort_t fgCheckWinFirst;    // First FADC slice to check for signal (currently set to: 0)
-    static const UShort_t fgCheckWinLast;     // Last FADC slice to check for signal  (currently set to: 29)
-    static const UShort_t fgExtractWinFirst;  // First FADC slice to use for pedestal calculation (currently set to: 15)
-    static const UShort_t fgExtractWinSize;   // number of successive slices used to calculate pedestal (currently set to: 6)
-    static const UShort_t fgMaxSignalVar;     // The maximum difference between the highest and lowest slice
+  static const UShort_t fgCheckWinFirst;    // First FADC slice to check for signal (currently set to: 0)
+  static const UShort_t fgCheckWinLast;     // Last FADC slice to check for signal  (currently set to: 29)
+  static const UShort_t fgMaxSignalVar;     // The maximum difference between the highest and lowest slice
+  static const UShort_t fgExtractWinFirst;  // First FADC slice to use for pedestal calculation (currently set to: 15)
+  static const UShort_t fgExtractWinSize;   // number of successive slices used to calculate pedestal (currently set to: 6)
+    
+  UShort_t fMaxSignalVar;
+  UShort_t fCheckWinFirst;
+  UShort_t fCheckWinLast;
 
-    MGeomCam      *fGeom;        //! Camera geometry
-    MPedestalCam  *fPedestals;   //! Pedestals of all pixels in the camera
-    MRawEvtData   *fRawEvt;      //! Raw event data (time slices)
-    MRawRunHeader *fRunHeader;   //! RunHeader information
+  TArrayI fNumEventsUsed;      // Number of events used for pedestal calc for each pixel
+  TArrayI fTotalCounter;       // Counter for dumping values to Pedestal Container
 
-    UInt_t   fNumEventsDump;     // Number of event after which MPedestalCam gets updated
-
-    UShort_t fMaxSignalVar;
-    UShort_t fCheckWinFirst;
-    UShort_t fCheckWinLast;
-    UShort_t fExtractWinSize;    // Number of slices to calculate the pedestal from
-    UShort_t fExtractWinFirst;
-    UShort_t fExtractWinLast;
-
-    Bool_t  fPedestalUpdate;
-
-    TString fNamePedestalCam;    // name of the 'MPedestalCam' container
-
-    TArrayI fNumEventsUsed;      // Number of events used for pedestal calc for each pixel
-    TArrayI fTotalCounter;       // Counter for dumping values to Pedestal Container
-    TArrayD fSumx;               // sum of values
-    TArrayD fSumx2;              // sum of squared values
-    TArrayD fSumAB0;             // sum of ABFlag=0 slices
-    TArrayD fSumAB1;             // sum of ABFlag=1 slices
-
-    // MParContainer
-    Int_t  PreProcess (MParList *pList);
-    Bool_t ReInit     (MParList *pList);
-    Int_t  Process    ();
-    Int_t  PostProcess();
-    Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);
-
-    // Calculation
-    void Calc(ULong_t n, UInt_t idx);
-
-    //Helper function to extract slice values by slice number
-    UShort_t GetSlice(MRawEvtPixelIter *pixel, UInt_t slice);
-    void ResetArrays();
+  Bool_t ReInit     (MParList *pList);
+  Int_t  Process    ();
+  Int_t  PostProcess();
+  
+  Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);
+  
+  //Helper function to extract slice values by slice number
+  UShort_t GetSlice(MRawEvtPixelIter *pixel, UInt_t slice);
+  void CalcExtractor   ( MRawEvtPixelIter *pixel, Float_t &sum, MPedestalPix &ped);
+  void ResetArrays();
 
 public:
-    MPedCalcFromLoGain(const char *name=NULL, const char *title=NULL);
 
-    // TObject
-    void Clear(const Option_t *o="");
-    void Print(Option_t *o="") const;
+  MPedCalcFromLoGain(const char *name=NULL, const char *title=NULL);
+  
+  void Print(Option_t *o="") const;
+  
+  // Setters
+  Bool_t SetCheckRange(UShort_t checkfirst=fgCheckWinFirst, UShort_t checklast=fgCheckWinLast);
+  void SetMaxSignalVar(UShort_t maxvar=40)       { fMaxSignalVar = maxvar;    }
 
-    // Setter
-    Bool_t SetCheckRange(UShort_t checkfirst=fgCheckWinFirst, UShort_t checklast=fgCheckWinLast);
-    Bool_t SetExtractWindow(UShort_t extractfirst=fgExtractWinFirst, UShort_t windowsize=fgExtractWinSize);
-
-    void SetMaxSignalVar(UShort_t maxvar=40)       { fMaxSignalVar = maxvar;    }
-    void SetNumEventsDump(UInt_t dumpevents = 500) { fNumEventsDump = dumpevents;}
-    void SetPedestalUpdate(Bool_t pedupdate)  {fPedestalUpdate = pedupdate;}
-
-    void SetNamePedestalCam(const char *name) { fNamePedestalCam = name; }
-
-    // Getter
-    TArrayI *GetNumEventsUsed() { return &fNumEventsUsed; }
-
-    ClassDef(MPedCalcFromLoGain, 0)   // Task to calculate pedestals from pedestal runs raw data
+  // Getters
+  TArrayI *GetNumEventsUsed() { return &fNumEventsUsed; }
+  
+  ClassDef(MPedCalcFromLoGain, 1)   // Task to calculate pedestals from data runs
 };
 
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 5497)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 5498)
@@ -130,19 +130,20 @@
 Bool_t MExtractTimeAndCharge::ReInit(MParList *pList)
 {
-    if (!MExtractTime::ReInit(pList))
-        return kFALSE;
-
-    fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
+
+  if (!MExtractTime::ReInit(pList))
+    return kFALSE;
+
+  fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
                                 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
-
-    *fLog << dec << endl;
-    *fLog << inf << "Taking " << fNumHiGainSamples
+  
+  *fLog << dec << endl;
+  *fLog << inf << "Taking " << fNumHiGainSamples
         << " HiGain samples from slice " << (Int_t)fHiGainFirst
         << " to " << (Int_t)(fHiGainLast+fHiLoLast) << " incl" << endl;
-    *fLog << inf << "Taking " << fNumLoGainSamples
+  *fLog << inf << "Taking " << fNumLoGainSamples
         << " LoGain samples from slice " << (Int_t)fLoGainFirst
         << " to " << (Int_t)fLoGainLast << " incl" << endl;
-
-    return kTRUE;
+  
+  return kTRUE;
 }
 
