Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 5557)
+++ trunk/MagicSoft/Mars/Changelog	(revision 5558)
@@ -30,4 +30,79 @@
      - added possibility to set fPedestals directly by pointer
      - unfolded some Form statements
+
+   * callisto.cc:
+     - changed callisto to support second pedestal loop -- WORK IN PROGRESS
+
+   * mbase/MParContainer.[h,cc]:
+     - moved code from GetDescriptor to a static function GetDescriptor
+      
+   * mbase/MStatusDisplay.cc:
+     - removed a oboslete debug output
+
+   * mbase/MTaskEnv.h:
+     - made ReadEnv public
+
+   * mcalib/MCalibrationChargeCam.[h,cc]:
+     - changed some returned TArrayF* to TArrayF
+
+   * mimage/MImgCleanStd.cc:
+     - changed point of output of name of MPedPhotCam
+
+   * mjobs/MJCalibration.cc:
+     - don't use MParList to hand ExtractorCam over
+     - some changes to the structur for Writing (only consmetics)
+
+   * mjobs/MJPedestal.[h,cc]:
+     - ordered includes correctly
+     - for the moment removed fNameExtractorCam -- simplifies thing a lot
+     - make a clone of the extractor given by the user - for sanity
+     - correct handling of the allocated memory for fExtractor
+     - replaced counts/slice by cts/slice for space reasons
+     - changed name of new tabs for space reasons
+     - added possibility to CheckEnv to set extractor from resource-file
+     - outsourced some code to SetupExtractor
+     - given a name to MFillH
+     - changed handling of SetNoiseCalculation which is now set correctly all
+       the time
+     - Writing must still be checked!!!
+
+   * mjobs/MJob.[h,cc]:
+     - allow WriteContainer to use any TObject
+     - added Getter-functions for the TEnv stuff
+
+   * mpedestal/MExtractPedestal.[h,cc]:
+     - allow setting of fPedestalIn by pointer directly
+     - fixed handling of fPedestalIn accordingly
+     - changed some arguments from pointer to reference
+     - changed some accesses to TArrays from At to []-operator
+     - shortened and enhanced output
+     - fixed place and type of screen output
+
+   * mpedestal/MPedCalcFromLoGain.[h,cc]:
+     - removed GetSlice -> replaced by a array with a copy of the data
+     - changed some loops to pointer arithmetic for speed reasons
+       in this case
+
+   * mpedestal/MPedCalcPedRun.[h,cc]:
+     - changed handling of first pedestal run for simplicity
+     - replaced some Form calls
+     - changed direct handling of single bits to correct enums
+     - fixed wrong 'all' in output
+     - simplified output
+     - fixed some stuff in the output
+
+   * mpedestal/MPedestalCam.[h,cc]:
+     - fixed the Copy function - WITHOUT this fix the calibration could
+       not have worked properly at all.
+     - changed some function names to Mars standrad names
+     - removed some obsolete loops - replaced by ForEach
+     - changed some TArrayF* returnes to TArrayF
+
+   * msignal/MExtractTime.cc, msignal/MExtractTimeAndCharge.cc,
+     msignal/MExtractTimeAndChargeDigitalFilter.cc, 
+     msignal/MExtractor.cc
+     - fixed some wrong debug levels in output
+     - some simplification and shortening to output
+
 
 
Index: trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.cc	(revision 5558)
@@ -150,5 +150,4 @@
 /////////////////////////////////////////////////////////////////////////////
 #include "MExtractPedestal.h"
-#include "MExtractTimeAndCharge.h"
 
 #include "MParList.h"
@@ -168,4 +167,7 @@
 #include "MGeomCam.h"
 
+#include "MTaskEnv.h"
+#include "MExtractTimeAndCharge.h"
+
 ClassImp(MExtractPedestal);
 
@@ -173,4 +175,5 @@
 
 const TString MExtractPedestal::fgNamePedestalCam = "MPedestalCam";
+
 // --------------------------------------------------------------------------
 //
@@ -189,22 +192,21 @@
       fExtractWinFirst(0), fExtractWinSize(0)
 {
-
-  fName  = name  ? name  : "MExtractPedestal";
-  fTitle = title ? title : "Base class to calculate pedestals";
-  
-  AddToBranchList("fHiGainPixId");
-  AddToBranchList("fLoGainPixId");
-  AddToBranchList("fHiGainFadcSamples");
-  AddToBranchList("fLoGainFadcSamples");
-  
-  SetPedestalUpdate(kTRUE);
-
-  SetNamePedestalCamIn();
-  SetNamePedestalCamOut();
-  SetNumEventsDump();
-  SetNumAreasDump();
-  SetNumSectorsDump();
-  
-  Clear();
+    fName  = name  ? name  : "MExtractPedestal";
+    fTitle = title ? title : "Base class to calculate pedestals";
+
+    AddToBranchList("fHiGainPixId");
+    AddToBranchList("fLoGainPixId");
+    AddToBranchList("fHiGainFadcSamples");
+    AddToBranchList("fLoGainFadcSamples");
+
+    SetPedestalUpdate(kTRUE);
+
+    SetNamePedestalCamIn();
+    SetNamePedestalCamOut();
+    SetNumEventsDump();
+    SetNumAreasDump();
+    SetNumSectorsDump();
+
+    Clear();
 }
 
@@ -212,20 +214,20 @@
 {
     // Reset contents of arrays.
-  fSumx.Reset();        
-  fSumx2.Reset();       
-  fSumAB0.Reset();      
-  fSumAB1.Reset();      
-  fAreaSumx.Reset();    
-  fAreaSumx2.Reset();   
-  fAreaSumAB0.Reset();  
-  fAreaSumAB1.Reset();  
-  fAreaFilled.Reset();   
-  fAreaValid.Reset();   
-  fSectorSumx.Reset();  
-  fSectorSumx2.Reset(); 
-  fSectorSumAB0.Reset();
-  fSectorSumAB1.Reset();
-  fSectorFilled.Reset(); 
-  fSectorValid.Reset(); 
+    fSumx.Reset();
+    fSumx2.Reset();
+    fSumAB0.Reset();
+    fSumAB1.Reset();
+    fAreaSumx.Reset();
+    fAreaSumx2.Reset();
+    fAreaSumAB0.Reset();
+    fAreaSumAB1.Reset();
+    fAreaFilled.Reset();
+    fAreaValid.Reset();
+    fSectorSumx.Reset();
+    fSectorSumx2.Reset();
+    fSectorSumAB0.Reset();
+    fSectorSumAB1.Reset();
+    fSectorFilled.Reset();
+    fSectorValid.Reset();
 
 }
@@ -248,5 +250,4 @@
   fRunHeader    = NULL;
   fEvtHeader    = NULL;
-  fPedestalsIn  = NULL;
   fPedestalsOut = NULL;
 
@@ -270,20 +271,20 @@
   
   if (odd)
-    {
+  {
       *fLog << warn << GetDescriptor();
-      *fLog << " - WARNING: Window size in SetExtraxtWindow has to be even... " 
-        " raising from " << windows << " to " << flush;
+      *fLog << " - WARNING: Window size in SetExtraxtWindow has to be even... ";
+      *fLog << " raising from " << windows << " to ";
       windows += 1;
       *fLog << windows << "!" << endl;
       rc = kFALSE;
-    }
-
-  if (windows==0)  
-    {
+  }
+
+  if (windows==0)
+  {
       *fLog << warn << GetDescriptor();
       *fLog << " - WARNING: Window size in SetExtraxtWindow has to be > 0... adjusting to 2!" << endl;
       windows = 2;
       rc = kFALSE;
-    }
+  }
 
   fExtractWinSize  = windows;
@@ -315,46 +316,47 @@
   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;
-    }
+  }
   
   fEvtHeader = (MRawEvtHeader*)pList->FindObject(AddSerialNumber("MRawEvtHeader"));
   if (!fEvtHeader)
-    {
+  {
       *fLog << err << AddSerialNumber("MRawEvtHeader") << " not found... aborting." << endl;
       return kFALSE;
-    }
+  }
   
   fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
   if (!fGeom)
-    {
+  {
       *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
       return kFALSE;
-    }
-  
-  fPedestalsIn = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamIn.Data()));
-  if (!fPedestalsIn)
-    {
-      *fLog << err << fNamePedestalCamIn.Data() << " could not be found nor created... aborting" << endl;
+  }
+
+  if (fExtractor && !fPedestalsIn)
+  {
+      fPedestalsIn = (MPedestalCam*)pList->FindObject("MPedestalCam", AddSerialNumber(fNamePedestalCamIn));
+      if (!fPedestalsIn)
+      {
+          *fLog << err << AddSerialNumber(fNamePedestalCamIn) << " not found... aborting." << endl;
+          return kFALSE;
+      }
+  }
+
+  fPedestalsOut = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamOut));
+  if (!fPedestalsOut)
       return kFALSE;
-    }
-  
-  fPedestalsOut = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamOut.Data()));
-  if (!fPedestalsOut)
-    {
-      *fLog << err << fNamePedestalCamOut.Data() << " could not be found nor created... aborting" << endl;
-      return kFALSE;
-    }
-  
+
   *fLog << inf;
-  
+  Print();
+
   return kTRUE;
 }
@@ -382,52 +384,57 @@
 Bool_t MExtractPedestal::ReInit(MParList *pList)
 {
-
   // If the size is not yet set, set the size
   if (fSumx.GetSize()==0)
   {
-    const Int_t npixels  = fPedestalsOut->GetSize();
-    const Int_t areas    = fPedestalsOut->GetAverageAreas();
-    const Int_t sectors  = fPedestalsOut->GetAverageSectors();
-    
-    fSumx.  Set(npixels);
-    fSumx2. Set(npixels);
-    fSumAB0.Set(npixels);
-    fSumAB1.Set(npixels);
-    
-    fAreaSumx.  Set(areas);
-    fAreaSumx2. Set(areas);
-    fAreaSumAB0.Set(areas);
-    fAreaSumAB1.Set(areas);
-    fAreaFilled.Set(areas);
-    fAreaValid .Set(areas);
-    
-    fSectorSumx.  Set(sectors);
-    fSectorSumx2. Set(sectors);
-    fSectorSumAB0.Set(sectors);
-    fSectorSumAB1.Set(sectors);
-    fSectorFilled.Set(sectors);
-    fSectorValid .Set(sectors);
-
-    for (Int_t i=0; i<npixels; i++)
+      const Int_t npixels  = fPedestalsOut->GetSize();
+      const Int_t areas    = fPedestalsOut->GetNumAverageArea();
+      const Int_t sectors  = fPedestalsOut->GetNumAverageSector();
+
+      fSumx.  Set(npixels);
+      fSumx2. Set(npixels);
+      fSumAB0.Set(npixels);
+      fSumAB1.Set(npixels);
+
+      fAreaSumx.  Set(areas);
+      fAreaSumx2. Set(areas);
+      fAreaSumAB0.Set(areas);
+      fAreaSumAB1.Set(areas);
+      fAreaFilled.Set(areas);
+      fAreaValid .Set(areas);
+
+      fSectorSumx.  Set(sectors);
+      fSectorSumx2. Set(sectors);
+      fSectorSumAB0.Set(sectors);
+      fSectorSumAB1.Set(sectors);
+      fSectorFilled.Set(sectors);
+      fSectorValid .Set(sectors);
+
+      for (Int_t i=0; i<npixels; i++)
       {
-        const UInt_t aidx   = (*fGeom)[i].GetAidx();
-        const UInt_t sector = (*fGeom)[i].GetSector();      
-        
-        fAreaValid  [aidx]  ++;
-        fSectorValid[sector]++;
+          const UInt_t aidx   = (*fGeom)[i].GetAidx();
+          const UInt_t sector = (*fGeom)[i].GetSector();
+
+          fAreaValid  [aidx]  ++;
+          fSectorValid[sector]++;
       }
   }
-  
+
   if (fExtractor)
-    {
+  {
       if (!fExtractor->InitArrays())
-        return kFALSE;
-      SetExtractWindow(fExtractor->GetHiGainFirst(),(Int_t)fExtractor->GetNumHiGainSamples());
-    }
-
-  Print();
+          return kFALSE;
+
+      SetExtractWindow(fExtractor->GetHiGainFirst(), (Int_t)TMath::Nint(fExtractor->GetNumHiGainSamples()));
+  }
 
   return kTRUE;
 }
+
+Int_t MExtractPedestal::PostProcess()
+{
+    fPedestalsIn = NULL;
+    return kTRUE;
+}
+
 
 // --------------------------------------------------------------------------
@@ -441,65 +448,64 @@
 Int_t MExtractPedestal::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
-
-  Bool_t rc=kFALSE;
-  
-  // find resource for numeventsdump
-  if (IsEnvDefined(env, prefix, "NumEventsDump", print))
-    {
-      SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
-      rc = kTRUE;
-    }
-
-  // find resource for numeventsdump
-  if (IsEnvDefined(env, prefix, "NumAreasDump", print))
-    {
-      SetNumAreasDump(GetEnvValue(env, prefix, "NumAreasDump", (Int_t)fNumAreasDump));
-      rc = kTRUE;
-    }
-
-  // find resource for numeventsdump
-  if (IsEnvDefined(env, prefix, "NumSectorsDump", print))
-    {
-      SetNumSectorsDump(GetEnvValue(env, prefix, "NumSectorsDump", (Int_t)fNumSectorsDump));
-      rc = kTRUE;
-    }
-
-  // find resource for pedestal update
-  if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
-    {
-      SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
-      rc = kTRUE;
-    }
-  
-  // Find resources for ExtractWindow
-  Int_t ef = fExtractWinFirst;
-  Int_t es = fExtractWinSize;
-  if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
-    {
-      ef = GetEnvValue(env, prefix, "ExtractWinFirst", ef);
-      rc = kTRUE;
-    }
-  if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
-    {
-      es = GetEnvValue(env, prefix, "ExtractWinSize", es);
-      rc = kTRUE;
-    }
-  
-  SetExtractWindow(ef,es);
-  
-  // find resource for MPedestalCam
-  if (IsEnvDefined(env, prefix, "NamePedestalCamIn", print))
-    {
-      SetNamePedestalCamIn(GetEnvValue(env, prefix, "NamePedestalCamIn", fNamePedestalCamIn));
-      rc = kTRUE;
-    }
-  
-  if (IsEnvDefined(env, prefix, "NamePedestalCamOut", print))
-    {
-      SetNamePedestalCamOut(GetEnvValue(env, prefix, "NamePedestalCamOut", fNamePedestalCamOut));
-      rc = kTRUE;
-    }
-  
-  return rc;
+    Bool_t rc=kFALSE;
+
+    // find resource for numeventsdump
+    if (IsEnvDefined(env, prefix, "NumEventsDump", print))
+    {
+        SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
+        rc = kTRUE;
+    }
+
+    // find resource for numeventsdump
+    if (IsEnvDefined(env, prefix, "NumAreasDump", print))
+    {
+        SetNumAreasDump(GetEnvValue(env, prefix, "NumAreasDump", (Int_t)fNumAreasDump));
+        rc = kTRUE;
+    }
+
+    // find resource for numeventsdump
+    if (IsEnvDefined(env, prefix, "NumSectorsDump", print))
+    {
+        SetNumSectorsDump(GetEnvValue(env, prefix, "NumSectorsDump", (Int_t)fNumSectorsDump));
+        rc = kTRUE;
+    }
+
+    // find resource for pedestal update
+    if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
+    {
+        SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
+        rc = kTRUE;
+    }
+
+    // Find resources for ExtractWindow
+    Int_t ef = fExtractWinFirst;
+    Int_t es = fExtractWinSize;
+    if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
+    {
+        ef = GetEnvValue(env, prefix, "ExtractWinFirst", ef);
+        rc = kTRUE;
+    }
+    if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
+    {
+        es = GetEnvValue(env, prefix, "ExtractWinSize", es);
+        rc = kTRUE;
+    }
+
+    SetExtractWindow(ef,es);
+
+    // find resource for MPedestalCam
+    if (IsEnvDefined(env, prefix, "NamePedestalCamIn", print))
+    {
+        SetNamePedestalCamIn(GetEnvValue(env, prefix, "NamePedestalCamIn", fNamePedestalCamIn));
+        rc = kTRUE;
+    }
+
+    if (IsEnvDefined(env, prefix, "NamePedestalCamOut", print))
+    {
+        SetNamePedestalCamOut(GetEnvValue(env, prefix, "NamePedestalCamOut", fNamePedestalCamOut));
+        rc = kTRUE;
+    }
+
+    return rc;
 }
 
@@ -516,26 +522,25 @@
 void MExtractPedestal::CalcPixResults(const UInt_t nevts, const UInt_t pixid)
 {
-  
-  const Float_t sum         = fSumx.At(pixid);
-  const Float_t sum2        = fSumx2.At(pixid);
-
-  // 1. Calculate the mean of the sums:
-  Float_t ped         = sum/nevts;
-  
-  // 2. Calculate the Variance of the sums:
-  Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
-
-  // 3. Calculate the amplitude of the 150MHz "AB" noise
-  Float_t abOffs = (fSumAB0[pixid] - fSumAB1[pixid]) / nevts;
-
-  // 4. Scale the mean, variance and AB-noise to the number of slices:
-  ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  
-  // 5. Calculate the RMS from the Variance:
-  const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
-  
-  (*fPedestalsOut)[pixid].Set(ped, rms, abOffs, nevts);
+    const Float_t sum  = fSumx[pixid];
+    const Float_t sum2 = fSumx2[pixid];
+
+    // 1. Calculate the mean of the sums:
+    Float_t ped        = sum/nevts;
+
+    // 2. Calculate the Variance of the sums:
+    Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
+
+    // 3. Calculate the amplitude of the 150MHz "AB" noise
+    Float_t abOffs = (fSumAB0[pixid] - fSumAB1[pixid]) / nevts;
+
+    // 4. Scale the mean, variance and AB-noise to the number of slices:
+    ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+
+    // 5. Calculate the RMS from the Variance:
+    const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
+
+    (*fPedestalsOut)[pixid].Set(ped, rms, abOffs, nevts);
 }
 
@@ -552,37 +557,30 @@
 void MExtractPedestal::CalcAreaResults(const UInt_t nevts, const UInt_t napix, const UInt_t aidx)
 {
-  
-  const Float_t sum         = fAreaSumx.At(aidx);
-  const Float_t sum2        = fAreaSumx2.At(aidx);
-
-  // 1. Calculate the mean of the sums:
-  Float_t ped         = sum/nevts;      
-  //
-  // 2. Calculate the Variance of the sums:
-  //
-  Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
-  //
-  // 3. Calculate the amplitude of the 150MHz "AB" noise
-  //
-  Float_t abOffs = (fAreaSumAB0[aidx] - fAreaSumAB1[aidx]) / nevts;
-  //
-  // 4. Scale the mean, variance and AB-noise to the number of slices:
-  //
-  ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  //
-  // 5. Scale the mean, variance and AB-noise to the number of pixels:
-  //
-  ped    /= napix;
-  var    /= napix;
-  abOffs /= napix;
-  //
-  // 6. Calculate the RMS from the Variance:
-  //
-  const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
-
-  fPedestalsOut->GetAverageArea(aidx).Set(ped, rms,abOffs,nevts);
-  
+    const Float_t sum  = fAreaSumx[aidx];
+    const Float_t sum2 = fAreaSumx2[aidx];
+
+    // 1. Calculate the mean of the sums:
+    Float_t ped        = sum/nevts;
+
+    // 2. Calculate the Variance of the sums:
+    Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
+
+    // 3. Calculate the amplitude of the 150MHz "AB" noise
+    Float_t abOffs = (fAreaSumAB0[aidx] - fAreaSumAB1[aidx]) / nevts;
+
+    // 4. Scale the mean, variance and AB-noise to the number of slices:
+    ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+
+    // 5. Scale the mean, variance and AB-noise to the number of pixels:
+    ped    /= napix;
+    var    /= napix;
+    abOffs /= napix;
+
+    // 6. Calculate the RMS from the Variance:
+    const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
+
+    fPedestalsOut->GetAverageArea(aidx).Set(ped, rms, abOffs, nevts);
 }
 
@@ -599,50 +597,44 @@
 void MExtractPedestal::CalcSectorResults(const UInt_t nevts, const UInt_t nspix, const UInt_t sector)
 {
-  
-  const Float_t sum         = fSectorSumx.At(sector);
-  const Float_t sum2        = fSectorSumx2.At(sector);
-
-  // 1. Calculate the mean of the sums:
-  Float_t ped         = sum/nevts;      
-  //
-  // 2. Calculate the Variance of the sums:
-  //
-  Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
-  //
-  // 3. Calculate the amplitude of the 150MHz "AB" noise
-  //
-  Float_t abOffs = (fSectorSumAB0[sector] - fSectorSumAB1[sector]) / nevts;
-  //
-  // 4. Scale the mean, variance and AB-noise to the number of slices:
-  //
-  ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
-  //
-  // 5. Scale the mean, variance and AB-noise to the number of pixels:
-  //
-  ped    /= nspix;
-  var    /= nspix;
-  abOffs /= nspix;
-  //
-  // 6. Calculate the RMS from the Variance:
-  //
-  const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
-
-  fPedestalsOut->GetAverageSector(sector).Set(ped, rms,abOffs,nevts);
-}
-
-
+    const Float_t sum  = fSectorSumx[sector];
+    const Float_t sum2 = fSectorSumx2[sector];
+
+    // 1. Calculate the mean of the sums:
+    Float_t ped        = sum/nevts;
+
+    // 2. Calculate the Variance of the sums:
+    Float_t var = (sum2-sum*sum/nevts)/(nevts-1.);
+
+    // 3. Calculate the amplitude of the 150MHz "AB" noise
+    Float_t abOffs = (fSectorSumAB0[sector] - fSectorSumAB1[sector]) / nevts;
+
+    // 4. Scale the mean, variance and AB-noise to the number of slices:
+    ped    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    var    /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+    abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
+
+    // 5. Scale the mean, variance and AB-noise to the number of pixels:
+    ped    /= nspix;
+    var    /= nspix;
+    abOffs /= nspix;
+
+    // 6. Calculate the RMS from the Variance:
+    const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
+
+    fPedestalsOut->GetAverageSector(sector).Set(ped, rms, abOffs, nevts);
+}
 
 void MExtractPedestal::Print(Option_t *o) const
 {
-
     *fLog << GetDescriptor() << ":" << endl;
-    *fLog << "Name of incoming MPedestalCam Container:  " << fNamePedestalCamIn.Data() << endl;
-    *fLog << "Name of outgoing MPedestalCam Container:  " << fNamePedestalCamOut.Data() << endl;
-    *fLog << "Number events for pedestal calculation:   " << fNumEventsDump << endl;
-    *fLog << "Number events for av. areas calculation:  " << fNumAreasDump << endl;
-    *fLog << "Number events for av. sector calculation: " << fNumSectorsDump << endl;
-    *fLog << "Pedestal Update is                        " << (fPedestalUpdate?"on":"off") << endl;
+    *fLog << "Name of input  MPedestalCam:  " << (fPedestalsIn?fPedestalsIn->GetName():fNamePedestalCamIn.Data()) << " (" << fPedestalsIn << ")" << endl;
+    *fLog << "Name of output MPedestalCam:  " << (fPedestalsOut?fPedestalsOut->GetName():fNamePedestalCamOut.Data()) << " (" << fPedestalsOut << ")" << endl;
+    *fLog << "Num evts for pedestal   calc: " << fNumEventsDump << endl;
+    *fLog << "Num evts for avg.areas  calc: " << fNumAreasDump << endl;
+    *fLog << "Num evts for avg.sector calc: " << fNumSectorsDump << endl;
+    *fLog << "Pedestal Update is            " << (fPedestalUpdate?"on":"off") << endl;
     *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
-}
+
+    if (fExtractor)
+        *fLog << "Extractor used:               " << fExtractor->ClassName() << endl;
+}
Index: trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.h	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.h	(revision 5558)
@@ -14,8 +14,4 @@
 #endif
 
-#ifndef ROOT_TString
-#include "TString.h"
-#endif
-
 class MGeomCam;
 class MPedestalCam;
@@ -24,10 +20,10 @@
 class MRawEvtHeader;
 class MExtractTimeAndCharge;
+
 class MExtractPedestal : public MTask
 {
 private:
+  static const TString  fgNamePedestalCam;  //! "MPedestalCam"
 
-  static const TString  fgNamePedestalCam;  //! "MPedestalCam"
-    
 protected:
   
@@ -49,5 +45,5 @@
   UInt_t  fNumEventsDump;       // Number of event after which MPedestalCam gets updated
   UInt_t  fNumAreasDump;        // Number of events after which averaged areas gets updated
-  UInt_t  fNumSectorsDump;      // Number of events after which averaged sectors gets updated  
+  UInt_t  fNumSectorsDump;      // Number of events after which averaged sectors gets updated
 
   Bool_t  fPedestalUpdate;     // Flag if the pedestal shall be updated after every fNumEventsDump
@@ -70,15 +66,16 @@
   MArrayI fSectorValid;        // number of valid pixels within sector idx
 
-  Int_t  PreProcess (MParList *pList);
-  Bool_t ReInit     (MParList *pList);
+  Int_t  PreProcess(MParList *pList);
+  Int_t  PostProcess();
+  Bool_t ReInit(MParList *pList);
   Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);
 
   virtual void ResetArrays();
-  void CalcPixResults   ( const UInt_t nevts, const UInt_t pixid );
-  void CalcAreaResults  ( const UInt_t nevts, const UInt_t napix, const UInt_t aidx );
-  void CalcSectorResults( const UInt_t nevts, const UInt_t nspix, const UInt_t sector );  
-  
+
+  void CalcPixResults   (const UInt_t nevts, const UInt_t pixid);
+  void CalcAreaResults  (const UInt_t nevts, const UInt_t napix, const UInt_t aidx);
+  void CalcSectorResults(const UInt_t nevts, const UInt_t nspix, const UInt_t sector);
+
 public:
-
   MExtractPedestal(const char *name=NULL, const char *title=NULL);
 
@@ -87,12 +84,17 @@
 
   Bool_t SetExtractWindow(UShort_t first, UShort_t size);
-  void SetNumEventsDump  ( UInt_t dumpevents = 500  ) { fNumEventsDump  = dumpevents; }
-  void SetNumAreasDump   ( UInt_t dumpevents = 500  ) { fNumAreasDump   = dumpevents; }  
-  void SetNumSectorsDump ( UInt_t dumpevents = 500  ) { fNumSectorsDump = dumpevents; }
-  void SetPedestalUpdate ( Bool_t pedupdate         ) { fPedestalUpdate = pedupdate;  }
-  void SetNamePedestalCamIn( const char *name=fgNamePedestalCam.Data()) { fNamePedestalCamIn = name; }
-  void SetNamePedestalCamOut( const char *name=fgNamePedestalCam.Data()) { fNamePedestalCamOut = name; }  
-  void SetExtractor      ( MExtractTimeAndCharge *e ) { fExtractor      = e; }
-  
+
+  void SetNumEventsDump  (UInt_t dumpevents=500)  { fNumEventsDump  = dumpevents; }
+  void SetNumAreasDump   (UInt_t dumpevents=500)  { fNumAreasDump   = dumpevents; }
+  void SetNumSectorsDump (UInt_t dumpevents=500)  { fNumSectorsDump = dumpevents; }
+  void SetPedestalUpdate (Bool_t pedupdate=kTRUE) { fPedestalUpdate = pedupdate;  }
+
+  void SetNamePedestalCamIn(const char *name=fgNamePedestalCam.Data())  { fNamePedestalCamIn = name; }
+  void SetNamePedestalCamOut(const char *name=fgNamePedestalCam.Data()) { fNamePedestalCamOut = name; }
+
+  void SetPedestalsIn(MPedestalCam *pedcam) { fPedestalsIn = pedcam; }
+
+  void SetExtractor(MExtractTimeAndCharge *e) { fExtractor = e; }
+
   ClassDef(MExtractPedestal, 0)   // Base class for pedestal extractors
 };
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc	(revision 5558)
@@ -134,7 +134,4 @@
 /////////////////////////////////////////////////////////////////////////////
 #include "MPedCalcFromLoGain.h"
-#include "MExtractPedestal.h"
-
-#include "MExtractTimeAndCharge.h"
 
 #include "MParList.h"
@@ -152,4 +149,7 @@
 #include "MGeomPix.h"
 #include "MGeomCam.h"
+
+#include "MExtractPedestal.h"
+#include "MExtractTimeAndCharge.h"
 
 ClassImp(MPedCalcFromLoGain);
@@ -162,4 +162,5 @@
 const UShort_t MPedCalcFromLoGain::fgExtractWinSize  =  6;
 const UShort_t MPedCalcFromLoGain::fgMaxSignalVar    = 40;
+
 // --------------------------------------------------------------------------
 //
@@ -173,27 +174,22 @@
 // - AddToBranchList("fHiGainFadcSamples");
 // - SetCheckRange(fgCheckWinFirst, fgCheckWinLast, fgExtractWinFirst, fgExtractWinSize)
-// - Clear()
 //
 MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
 {
-
-  fName  = name  ? name  : "MPedCalcFromLoGain";
-  fTitle = title ? title : "Task to calculate pedestals from lo-gains";
-  
-  SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
-  SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
-  
-  SetMaxSignalVar(fgMaxSignalVar);
-  
-  Clear();
+    fName  = name  ? name  : "MPedCalcFromLoGain";
+    fTitle = title ? title : "Task to calculate pedestals from lo-gains";
+
+    SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
+    SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
+
+    SetMaxSignalVar(fgMaxSignalVar);
 }
 
 void MPedCalcFromLoGain::ResetArrays()
 {
-
-  MExtractPedestal::ResetArrays();
-  
-  fNumEventsUsed.Reset();
-  fTotalCounter.Reset();
+    MExtractPedestal::ResetArrays();
+
+    fNumEventsUsed.Reset();
+    fTotalCounter.Reset();
 }
 
@@ -235,73 +231,45 @@
 Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
 {
-
-  const UShort_t hisamples = fRunHeader->GetNumSamplesHiGain();
-  const UShort_t losamples = fRunHeader->GetNumSamplesLoGain();
-
-  UShort_t lastavailable   = hisamples+losamples-1;
-
-  if (fExtractor)
-    lastavailable = losamples-1;
-
-  // 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); 
-  }
-  
-  if (fExtractWinLast > lastavailable) //changed to override check
-   {
-     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;
-   }
-  
-   lastavailable = hisamples + losamples -1;
-   
-   if (fCheckWinLast > lastavailable) //changed to override check
-     {
-       *fLog << warn << GetDescriptor()
-             << " - WARNING: Last Check Window slice out of range...adjusting to last available slice "
-             << lastavailable << endl;
-       
-       fCheckWinLast = lastavailable;
-     }
-
-   if (fCheckWinLast < fExtractWinLast) 
-     {
-       *fLog << warn << GetDescriptor()
-             << " - WARNING: Last Check Window slice lower than extraction window "
-             << " - push it up to last number of low-gain samples: ";
-       fCheckWinLast = fExtractWinLast;
-       *fLog << fCheckWinLast << endl;
-     }
-
-  MExtractPedestal::ReInit(pList);
-  
-  return kTRUE;
-}
-
-// ---------------------------------------------------------------------------------
-//
-// Returns the pointer to slice "slice".
-//
-UShort_t MPedCalcFromLoGain::GetSlice(MRawEvtPixelIter *pixel, UInt_t slice)
-{
-    const UShort_t nh = (Int_t)fRunHeader->GetNumSamplesHiGain();
-
-    Byte_t *ptr;
-
-    if(slice<nh)
-        ptr = pixel->GetHiGainSamples() + slice;
-    else
-        ptr = pixel->GetLoGainSamples() + slice - nh;
-
-   return *ptr;
+    const UShort_t hisamples = fRunHeader->GetNumSamplesHiGain();
+    const UShort_t losamples = fRunHeader->GetNumSamplesLoGain();
+  
+    fSlices.Set(hisamples+losamples);
+  
+    UShort_t lastavailable   = hisamples+losamples-1;
+  
+    if (fExtractor)
+        lastavailable = losamples-1;
+  
+    // 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);
+    }
+  
+    if (fExtractWinLast > lastavailable) //changed to override check
+    {
+        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;
+    }
+  
+    lastavailable = hisamples + losamples -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 << lastavailable << endl;
+  
+        fCheckWinLast = lastavailable;
+    }
+  
+    return MExtractPedestal::ReInit(pList);
 }
 
@@ -314,189 +282,177 @@
 Int_t MPedCalcFromLoGain::Process()
 {
-
-  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++)
+    // This is the workaround to put hi- and lo-gains together
+    const Int_t nhigain = fRunHeader->GetNumSamplesHiGain();
+    const Int_t nlogain = fRunHeader->GetNumSamplesLoGain();
+
+    Byte_t *slices = fSlices.GetArray();
+
+    // Real Process
+    MRawEvtPixelIter pixel(fRawEvt);
+
+    while (pixel.Next())
+    {
+        // This is the fast workaround to put hi- and lo-gains together
+        memcpy(slices,         pixel.GetHiGainSamples(), nhigain);
+        memcpy(slices+nhigain, pixel.GetLoGainSamples(), nlogain);
+
+        // Start 'real' work
+        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 (Byte_t *slice=slices+fCheckWinFirst; slice<=slices+fCheckWinLast; slice++)
         {
-          const UShort_t svalue = GetSlice(&pixel,slice);
-          if (svalue > max)
-            max = svalue;
-          if (svalue < min)
-            min = svalue;
+            if (*slice > max)
+                max = *slice;
+            if (*slice < min)
+                min = *slice;
         }
       
-      // 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
+        // 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;
+            for(Byte_t *slice=slices+fExtractWinFirst; slice<=slices+fExtractWinLast; slice++)
+                sumi += *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)
+
+        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)
+            //
+            // Calculate the amplitude of the 150MHz "AB" noise
+            //
+            const UShort_t abFlag = (fExtractWinFirst + pixel.HasABFlag()) & 0x1;
+
+            for (Byte_t *slice=slices+fExtractWinFirst; slice<=slices+fExtractWinLast; slice+=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;      
+                const UShort_t ab0 = *(slice + abFlag);
+                const UShort_t ab1 = *(slice - abFlag + 1);
+
+                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++)
+
+        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))
+        CalcAreaResult();
+
+    if (!(GetNumExecutions() % fNumSectorsDump))
+        CalcSectorResult();
+
+    if (fPedestalUpdate)
+        fPedestalsOut->SetReadyToSave();
+
+  return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Loop over the sector indices to get the averaged pedestal per sector
+//
+void MPedCalcFromLoGain::CalcSectorResult()
+{
+    for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
+        if (fSectorValid[sector]>0)
+            CalcSectorResults(fSectorFilled[sector], fSectorValid[sector], sector);
+}
+
+// --------------------------------------------------------------------------
+//
+// Loop over the (two) area indices to get the averaged pedestal per aidx
+//
+void MPedCalcFromLoGain::CalcAreaResult()
+{
+    for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
+        if (fAreaValid[aidx]>0)
+            CalcAreaResults(fAreaFilled[aidx], fAreaValid[aidx], aidx);
+
+}
+
+void MPedCalcFromLoGain::CalcExtractor(const 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->FindTimeAndChargeHiGain(logain,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 Int_t   napix = fAreaValid.At(aidx);
-          if (napix == 0)
-            continue;
-          
-          CalcAreaResults(fSectorFilled[aidx],napix,aidx);
+            CalcPixResults(n, idx);
+            fTotalCounter[idx]++;
         }
     }
-  
-  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 Int_t   nspix = fSectorValid.At(sector);
-          if (nspix == 0)
-            continue;
-          
-          CalcSectorResults(fSectorFilled[sector],nspix,sector);
-        }
-    }
-  
-  if (fPedestalUpdate)
+
+    CalcAreaResult();
+    CalcSectorResult();
+
     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->FindTimeAndChargeHiGain(logain,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)
-        {
-          CalcPixResults(n, idx);
-          fTotalCounter[idx]++;
-        }
-    }
-
-  //
-  // 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;
+
+    return MExtractPedestal::PostProcess();
 }
 
@@ -511,40 +467,38 @@
 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 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;
+    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
 {
-
-  MExtractPedestal::Print(o);
-
-  *fLog << "CheckWindow   from slice  " << fCheckWinFirst   << " to " << fCheckWinLast << " incl." << endl;
-  *fLog << "Max. allowed signal variation:             " << fMaxSignalVar << endl;
-  *fLog << 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 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.h	(revision 5558)
@@ -6,55 +6,60 @@
 #endif
 
-#ifndef ROOT_TArrayD
-#include <TArrayD.h>
-#endif
-
 #ifndef ROOT_TArrayI
 #include <TArrayI.h>
 #endif
 
+#ifndef ROOT_MArrayB
+#include "MArrayB.h"
+#endif
+
 class MRawEvtPixelIter;
 class MPedestalPix;
+
 class MPedCalcFromLoGain : public MExtractPedestal
 {
+private:
+    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)
 
-  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;
+    UShort_t fMaxSignalVar;
+    UShort_t fCheckWinFirst;
+    UShort_t fCheckWinLast;
 
-  TArrayI fNumEventsUsed;      // Number of events used for pedestal calc for each pixel
-  TArrayI fTotalCounter;       // Counter for dumping values to Pedestal Container
+    TArrayI fNumEventsUsed;      //! Number of events used for pedestal calc for each pixel
+    TArrayI fTotalCounter;       //! Counter for dumping values to Pedestal Container
 
-  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();
+    MArrayB fSlices;             //! workaround to put hi- and lo-gain slices together
+
+    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
+    void CalcExtractor(const MRawEvtPixelIter &pixel, Float_t &sum, MPedestalPix &ped);
+    void ResetArrays();
+
+    void CalcSectorResult();
+    void CalcAreaResult();
 
 public:
 
-  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;    }
+    MPedCalcFromLoGain(const char *name=NULL, const char *title=NULL);
 
-  // Getters
-  TArrayI *GetNumEventsUsed() { return &fNumEventsUsed; }
-  
-  ClassDef(MPedCalcFromLoGain, 1)   // Task to calculate pedestals from data runs
+    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;    }
+
+    // Getters
+    TArrayI *GetNumEventsUsed() { return &fNumEventsUsed; }
+
+    ClassDef(MPedCalcFromLoGain, 1)   // Task to calculate pedestals from data runs
 };
 
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 5558)
@@ -119,7 +119,4 @@
 /////////////////////////////////////////////////////////////////////////////
 #include "MPedCalcPedRun.h"
-#include "MExtractPedestal.h"
-
-#include "MExtractTimeAndCharge.h"
 
 #include "MParList.h"
@@ -139,5 +136,6 @@
 #include "MGeomCam.h"
 
-#include "MStatusDisplay.h"
+#include "MExtractPedestal.h"
+#include "MExtractTimeAndCharge.h"
 
 ClassImp(MPedCalcPedRun);
@@ -148,4 +146,5 @@
 const UShort_t MPedCalcPedRun::fgExtractWinSize        = 6;
 const UInt_t   MPedCalcPedRun::gkFirstRunWithFinalBits = 45589;
+
 // --------------------------------------------------------------------------
 //
@@ -161,31 +160,12 @@
 // - AddToBranchList("fHiGainFadcSamples");
 // - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
-// - Clear()
 //
 MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
-{
-  fName  = name  ? name  : "MPedCalcPedRun";
-  fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
-
-  AddToBranchList("fHiGainPixId");
-  AddToBranchList("fHiGainFadcSamples");
-  
-  SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
-
-  Clear();
-  
-  fFirstRun   = kTRUE;
-  fSkip       = kFALSE;
-  fOverlap    = 0;
-  fUsedEvents = 0;
-}
-
-
-
-void MPedCalcPedRun::ResetArrays()
-{
-
-  MExtractPedestal::ResetArrays();
-  fUsedEvents    = 0;
+    : fOverlap(0)
+{
+    fName  = name  ? name  : "MPedCalcPedRun";
+    fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
+
+    SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
 }
 
@@ -197,54 +177,67 @@
 // same number of slices.
 //
-// The run type is checked for "Pedestal" (run type: 1) 
+void MPedCalcPedRun::CheckExtractionWindow()
+{
+    const UShort_t lastavailable = fRunHeader->GetNumSamplesHiGain()-1;
+
+    if (fExtractWinLast <= lastavailable)
+        return;
+
+    const UShort_t diff = fExtractWinLast - lastavailable;
+
+    *fLog << warn << endl;
+    *fLog << "Selected ExtractWindow [" << fExtractWinFirst << "," << fExtractWinLast;
+    *fLog << "] ranges out of range [0," << lastavailable << "]." << endl;
+    *fLog << "Using " << diff << " samples from the 'Lo-Gain' slices for the pedestal extraction" << endl;
+
+    fExtractWinLast -= diff;
+    fOverlap         = diff;
+}
+
+// --------------------------------------------------------------------------
+//
+// Set fIsFirstPedRun=kTRUE
+//
+Int_t MPedCalcPedRun::PreProcess(MParList *pList)
+{
+    fUsedEvents = 0;
+    fIsFirstPedRun = kTRUE;
+    return MExtractPedestal::PreProcess(pList);
+}
+
+// --------------------------------------------------------------------------
+//
+// The run type is checked for "kRTPedestal"
 // and the variable fSkip is set in that case
 //
 Bool_t MPedCalcPedRun::ReInit(MParList *pList)
 {
-
-  MExtractPedestal::ReInit(pList);
-
-  const UShort_t lastavailable = fRunHeader->GetNumSamplesHiGain()-1;
-  
-  if (fExtractWinLast > lastavailable)
+    if (!MExtractPedestal::ReInit(pList))
+        return kFALSE;
+
+    CheckExtractionWindow();
+
+    // If this is the first ped run, the next run (call to ReInit)
+    // is not the first anymore
+    if (fRunHeader->GetRunType()==MRawRunHeader::kRTPedestal)
     {
-      const UShort_t diff = fExtractWinLast - lastavailable;
-      *fLog << endl;
-      *fLog << warn << GetDescriptor()
-            << Form("%s%2i%s%2i%s%2i%s",": Selected ExtractWindow [",
-                    fExtractWinFirst,",",fExtractWinLast,
-                    "] ranges out of available limits: [0,",lastavailable,"].") << endl;
-      *fLog << warn << GetDescriptor() 
-            << Form("%s%2i%s",": Will use ",diff," samples from the 'Low-Gain' slices for the pedestal extraction")
-            << endl;
-      fExtractWinLast -= diff;
-      fOverlap         = diff;
+        fIsFirstPedRun = kFALSE;
+        return kTRUE;
     }
 
-  const UShort_t runtype = fRunHeader->GetRunType();
-
-  switch (runtype)
-    {
-    case 1: 
-      fFirstRun = kFALSE;
-      fSkip     = kFALSE;
-      return kTRUE;
-      break;
-    default:
-      fSkip     = kTRUE;
-      if (!fFirstRun)
-        {
-          *fLog << endl;
-          *fLog << inf << GetDescriptor() << " : Finalize pedestal calculations..." << flush;
-          Finalize();
-          Reset();
-        }
-      return kTRUE;        
-      break;
-    }
-  
-  Print();
-
-  return kTRUE;
+    // If this is the first call to ReInit (before reading the first file)
+    // nothing should be done
+    if (fIsFirstPedRun)
+        return kTRUE;
+
+    // In any other case some kind of finaliazation must be done
+    *fLog << inf << "Finalizing pedestal calculations..." << flush;
+
+    if (!Finalize())
+        return kFALSE;
+
+    Reset();
+
+    return kTRUE;
 }
 
@@ -260,4 +253,12 @@
 Int_t MPedCalcPedRun::Process()
 {
+    if (!IsPedBitSet())
+    {
+        *fLog << err << GetDescriptor() << " - ERROR: IsPedBitSet() returned kFALSE... abort." << endl;
+        return kERROR;
+    }
+
+    if (fRunHeader->GetRunType() != MRawRunHeader::kRTPedestal)
+        return kTRUE;
 
   fUsedEvents++;
@@ -266,5 +267,5 @@
   
   while (pixel.Next())
-    {
+  {
       const UInt_t idx    = pixel.GetPixelId();
       const UInt_t aidx   = (*fGeom)[idx].GetAidx();
@@ -276,26 +277,26 @@
       
       if (fExtractor)
-        CalcExtractor( &pixel, sum, (*fPedestalsIn)[idx]);
+          CalcExtractor(pixel, sum, (*fPedestalsIn)[idx]);
       else
-        CalcSums( &pixel, sum, ab0, ab1);
-
-      fSumx[idx]          += sum;
-      fAreaSumx[aidx]     += sum;
-      fSectorSumx[sector] += sum;      
-
-      const Float_t sqrsum  = sum*sum;
-      fSumx2[idx]          += sqrsum;
-      fAreaSumx2[aidx]     += sqrsum;
-      fSectorSumx2[sector] += sqrsum;      
-
-      fSumAB0[idx]        += ab0;
-      fSumAB1[idx]        += ab1;
-
-      fAreaSumAB0[aidx]   += ab0;
-      fAreaSumAB1[aidx]   += ab1;      
+          CalcSums(pixel, sum, ab0, ab1);
+
+      fSumx[idx]           += sum;
+      fAreaSumx[aidx]      += sum;
+      fSectorSumx[sector]  += sum;
+
+      const Float_t sqrsum   = sum*sum;
+      fSumx2[idx]           += sqrsum;
+      fAreaSumx2[aidx]      += sqrsum;
+      fSectorSumx2[sector]  += sqrsum;
+
+      fSumAB0[idx]         += ab0;
+      fSumAB1[idx]         += ab1;
+
+      fAreaSumAB0[aidx]    += ab0;
+      fAreaSumAB1[aidx]    += ab1;
       
-      fSectorSumAB0[aidx] += ab0;
-      fSectorSumAB1[aidx] += ab1;      
-    }
+      fSectorSumAB0[aidx]  += ab0;
+      fSectorSumAB1[aidx]  += ab1;
+  }
   
   fPedestalsOut->SetReadyToSave();
@@ -305,25 +306,25 @@
 
 
-void MPedCalcPedRun::CalcExtractor( MRawEvtPixelIter *pixel, Float_t &sum, MPedestalPix &ped)
-{
-  
-  Byte_t  sat = 0;
-  Byte_t *first  = pixel->GetHiGainSamples() + fExtractWinFirst;
-  Byte_t *logain = pixel->GetLoGainSamples();
-  const Bool_t abflag  = pixel->HasABFlag();
-  Float_t dummy;
-  fExtractor->FindTimeAndChargeHiGain(first,logain,sum,dummy,dummy,dummy,sat,ped,abflag);
-
-}
-
-
-void MPedCalcPedRun::CalcSums( MRawEvtPixelIter *pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1)
-{
-
-  Byte_t *higain = pixel->GetHiGainSamples() + fExtractWinFirst;
+void MPedCalcPedRun::CalcExtractor(const MRawEvtPixelIter &pixel, Float_t &sum, MPedestalPix &ped)
+{
+    const Bool_t abflag = pixel.HasABFlag();
+
+    Byte_t *first  = pixel.GetHiGainSamples() + fExtractWinFirst;
+    Byte_t *logain = pixel.GetLoGainSamples();
+
+    Byte_t  sat = 0;
+
+    Float_t dummy;
+    fExtractor->FindTimeAndChargeHiGain(first,logain,sum,dummy,dummy,dummy,sat,ped,abflag);
+}
+
+void MPedCalcPedRun::CalcSums(const MRawEvtPixelIter &pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1)
+{
+
+  Byte_t *higain = pixel.GetHiGainSamples() + fExtractWinFirst;
   Byte_t *ptr = higain;
   Byte_t *end = ptr + fExtractWinSize;
 
-  const Bool_t abflag = pixel->HasABFlag();
+  const Bool_t abflag = pixel.HasABFlag();
 
   Int_t sumi = 0;
@@ -331,43 +332,40 @@
   Int_t  cnt = 0;
   do
-    {
+  {
       sumi += *ptr;
-      if (pixel->IsABFlagValid())
-        {
+      if (!pixel.IsABFlagValid())
+          continue;
+
+      const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
+      if (abFlag)
+          ab1 += *ptr;
+      else
+          ab0 += *ptr;
+
+      cnt++;
+  } while (++ptr != end);
+
+  if (fOverlap != 0)
+  {
+      ptr = pixel.GetLoGainSamples();
+      end = ptr + fOverlap;
+
+      do
+      {
+          sumi += *ptr;
+          if (!pixel.IsABFlagValid())
+              continue;
+
           const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
           if (abFlag)
-            ab1 += *ptr;
+              ab1 += *ptr;
           else
-            ab0 += *ptr;
-          
+              ab0 += *ptr;
+
           cnt++;
-        }
-    }
-  while (++ptr != end);
-  
-  if (fOverlap != 0)
-    {
-      ptr = pixel->GetLoGainSamples();
-      end = ptr + fOverlap;
-      
-      do
-        {
-          sumi += *ptr;
-          if (pixel->IsABFlagValid())
-            {
-              const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
-              if (abFlag)
-                ab1 += *ptr;
-              else
-                ab0 += *ptr;
-              
-              cnt++;
-            }
-        }
-      while (++ptr != end);
-    }
+      } while (++ptr != end);
+  }
 
   sum = (Float_t)sumi;
-
 }
 
@@ -382,46 +380,39 @@
 // Compute signal mean and rms in the whole run and store it in MPedestalCam
 //
-void MPedCalcPedRun::Finalize()
-{
-
-  if (fUsedEvents == 0)
-    return;
-
-  MRawEvtPixelIter pixel(fRawEvt);
-  
-  while (pixel.Next())
-    {
-      const Int_t  pixid  = pixel.GetPixelId();
-      CalcPixResults(fUsedEvents,pixid);
-    }
-
-  //
-  // 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;
-
-      CalcAreaResults(fUsedEvents,napix,aidx);
-    }
-  
-  //
-  // Loop over the (six) sector indices to get the averaged pedestal per sector
-  //
-  for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
-    {
-      const Int_t   nspix = fSectorValid.At(sector);
-      if (nspix == 0)
-        continue;
-      
-      CalcSectorResults(fUsedEvents,nspix,sector);
-    }
-  
-  fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
-  fPedestalsOut->SetReadyToSave();
-
-  return;
+Int_t MPedCalcPedRun::Finalize()
+{
+    if (fUsedEvents == 0)
+        return kTRUE;
+
+    MRawEvtPixelIter pixel(fRawEvt);
+    while (pixel.Next())
+        CalcPixResults(fUsedEvents, pixel.GetPixelId());
+
+    //
+    // Loop over the (two) area indices to get the averaged pedestal per aidx
+    //
+    for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
+        if (fAreaValid[aidx]>0)
+            CalcAreaResults(fUsedEvents, fAreaValid[aidx], aidx);
+
+    //
+    // Loop over the (six) sector indices to get the averaged pedestal per sector
+    //
+    for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
+        if (fSectorValid[sector]>0)
+            CalcSectorResults(fUsedEvents, fSectorValid[sector], sector);
+
+    fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
+    fPedestalsOut->SetReadyToSave();
+
+    return kTRUE;
+}
+
+Int_t MPedCalcPedRun::PostProcess()
+{
+    if (!Finalize())
+        return kFALSE;
+
+    return MExtractPedestal::PostProcess();
 }
 
@@ -435,20 +426,26 @@
 Bool_t MPedCalcPedRun::IsPedBitSet()
 {
-
-  if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
-    return kFALSE;
-      
-  return (fEvtHeader->GetTriggerID() >> 3 & 1);
+    if (fRunHeader->GetRunNumber() == 38996)
+        return kTRUE;
+
+    if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
+        return kTRUE;
+
+    return (fEvtHeader->GetTriggerID() & BIT(3)) ? kTRUE : kFALSE;
 }
 
 void MPedCalcPedRun::Print(Option_t *o) const
 {
-
-  MExtractPedestal::Print(o);
-
-  *fLog << "Number overlap low-gain slices:           " << fOverlap << endl;
-  *fLog << "First run out of sequence:                " << (fFirstRun?"yes":"no") << endl;
-  *fLog << "Skip this run:                            " << (fSkip?"yes":"no") << endl;
-  *fLog << "Number of used events so far:             " << fUsedEvents << endl;
-  *fLog << endl;
-}
+    *fLog << GetDescriptor() << ":" << endl;
+    *fLog << "Name of input  MPedestalCam:   " << (fPedestalsIn?fPedestalsIn->GetName():fNamePedestalCamIn.Data()) << " (" << fPedestalsIn << ")" << endl;
+    *fLog << "Name of output MPedestalCam:   " << (fPedestalsOut?fPedestalsOut->GetName():fNamePedestalCamOut.Data()) << " (" << fPedestalsOut << ")" << endl;
+    *fLog << "ExtractWindow from slice       " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
+    *fLog << "Number overlap lo-gain slices: " << fOverlap << endl;
+    *fLog << "First ped run out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl;
+    *fLog << "Number of used events so far:  " << fUsedEvents << endl;
+
+    if (fExtractor)
+        *fLog << "Extractor used:                " << fExtractor->ClassName() << endl;
+
+    *fLog << endl;
+}
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h	(revision 5558)
@@ -6,46 +6,37 @@
 #endif
 
-#ifndef MARS_MArrayD
-#include <MArrayD.h>
-#endif
-
-#ifndef MARS_MArrayI
-#include <MArrayI.h>
-#endif
-
 class MRawEvtPixelIter;
 class MPedestalPix;
+
 class MPedCalcPedRun : public MExtractPedestal
 {
+private:
+    static const UShort_t fgExtractWinFirst;  // First FADC slice Hi-Gain (currently set to: 3)
+    static const UShort_t fgExtractWinSize;   // Extraction Size Hi-Gain (currently set to: 14)
+    static const UInt_t   gkFirstRunWithFinalBits; // First Run with pedestal trigger bit at place 3
 
-  static const UShort_t fgExtractWinFirst;  // First FADC slice Hi-Gain (currently set to: 3)
-  static const UShort_t fgExtractWinSize;   // Extraction Size Hi-Gain (currently set to: 14)
-  static const UInt_t   gkFirstRunWithFinalBits; // First Run with pedestal trigger bit at place 3 
-  
-  UShort_t fOverlap;         // Number of overlapping slices from High-Gain to Low-Gain
+    UShort_t fOverlap;         // Number of overlapping slices from High-Gain to Low-Gain
 
-  Bool_t  fFirstRun;         // Flag to tell if the first run out of many is used
-  Bool_t  fSkip;             // Flag to tell if the Process has to be skipped 
-  ULong_t fUsedEvents;       // Number of used (not skipped) events
-  
-  Bool_t IsPedBitSet();
-  
-  Bool_t ReInit     (MParList *pList);
-  Int_t  Process    ();
-  Int_t  PostProcess();
-  
-  void ResetArrays();
-  void CalcSums   ( MRawEvtPixelIter *pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1);
-  void CalcExtractor( MRawEvtPixelIter *pixel, Float_t &sum, MPedestalPix &ped);
-  
+    Bool_t  fIsFirstPedRun;    //! Flag to tell if the first run out of many is used
+    ULong_t fUsedEvents;       // Number of used (not skipped) events
+
+    Bool_t IsPedBitSet();
+
+    Bool_t ReInit(MParList *pList);
+    Int_t  PreProcess(MParList *pList);
+    Int_t  Process();
+    Int_t  PostProcess();
+
+    void CheckExtractionWindow();
+    void CalcSums(const MRawEvtPixelIter &pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1);
+    void CalcExtractor(const MRawEvtPixelIter &pixel, Float_t &sum, MPedestalPix &ped);
+
 public:
+    MPedCalcPedRun(const char *name=NULL, const char *title=NULL);
 
-  MPedCalcPedRun(const char *name=NULL, const char *title=NULL);
-  
-  void Finalize();  
+    void Print(Option_t *o="") const;
+    Int_t Finalize();
 
-  void Print(Option_t *o="") const;
-  
-  ClassDef(MPedCalcPedRun, 1)   // Task to calculate pedestals from pedestal runs
+    ClassDef(MPedCalcPedRun, 1)   // Task to calculate pedestals from pedestal runs
 };
 
Index: trunk/MagicSoft/Mars/mpedestal/MPedestalCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedestalCam.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedestalCam.cc	(revision 5558)
@@ -108,5 +108,5 @@
   MPedestalCam &cam = (MPedestalCam&)obj;
   
-  const Int_t n = GetSize();
+  Int_t n = GetSize();
   
   if (n==0)
@@ -116,4 +116,14 @@
   for (int i=0; i<n; i++)
     (*this)[i].Copy(cam[i]);
+
+  n = GetNumAverageArea();
+  cam.InitAverageAreas(n);
+  for (int i=0; i<n; i++)
+    GetAverageArea(i).Copy(cam.GetAverageArea(i));
+
+  n = GetNumAverageSector();
+  cam.InitAverageSectors(n);
+  for (int i=0; i<n; i++)
+    GetAverageSector(i).Copy(cam.GetAverageSector(i));
 }
 
@@ -178,5 +188,5 @@
 // independently if the MPedestalPix is filled with values or not.
 //
-const Int_t MPedestalCam::GetAverageAreas() const
+const Int_t MPedestalCam::GetNumAverageArea() const
 {
   return fAverageAreas->GetEntriesFast();
@@ -188,5 +198,5 @@
 // independently if the MPedestalPix is filled with values or not.
 //
-const Int_t MPedestalCam::GetAverageSectors() const
+const Int_t MPedestalCam::GetNumAverageSector() const
 {
   return fAverageSectors->GetEntriesFast();
@@ -259,20 +269,9 @@
 void MPedestalCam::Clear(Option_t *o)
 {
-  fArray->ForEach(TObject, Clear)();
+    { fArray->ForEach(TObject, Clear)(); }
+    { fAverageAreas->ForEach(TObject, Clear)(); }
+    { fAverageSectors->ForEach(TObject, Clear)(); }
   
-  //
-  // another ForEach does not compile, thus have to do the loop ourselves:
-  //
-  for (Int_t i=0;i<GetAverageAreas();i++)
-    fAverageAreas[i].Clear();
-
-
-  //
-  // another ForEach does not compile, thus have to do the loop ourselves:
-  //
-  for (Int_t i=0;i<GetAverageSectors();i++)
-    fAverageSectors[i].Clear();
-  
-  fTotalEntries = 0;
+    fTotalEntries = 0;
 }
 
@@ -352,7 +351,5 @@
 // arr[1]: Error (rms) of averaged pedestal (default: 0.)
 //
-// ATTENTION: THE USER HAS TO DELETE THE RETURNED TARRAYF ACCORDINGLY
-//
-TArrayF *MPedestalCam::GetAveragedPedPerArea(const MGeomCam &geom, const UInt_t ai, MBadPixelsCam *bad)
+TArrayF MPedestalCam::GetAveragedPedPerArea(const MGeomCam &geom, const UInt_t ai, MBadPixelsCam *bad)
 {
 
@@ -381,8 +378,7 @@
     }
 
-  TArrayF *arr = new TArrayF(2);
-  arr->AddAt(nr   ? mean/nr : -1.,0);
-  arr->AddAt(nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0. ,1);
-
+  TArrayF arr(2);
+  arr[0] = nr   ? mean/nr : -1.;
+  arr[1] = nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0;
   return arr;
 }
@@ -400,7 +396,5 @@
 // arr[1]: Error (rms) of averaged pedestal RMS (default: 0.)
 //
-// ATTENTION: THE USER HAS TO DELETE THE RETURNED TARRAYF ACCORDINGLY
-//
-TArrayF *MPedestalCam::GetAveragedRmsPerArea(const MGeomCam &geom, const UInt_t ai, MBadPixelsCam *bad)
+TArrayF MPedestalCam::GetAveragedRmsPerArea(const MGeomCam &geom, const UInt_t ai, MBadPixelsCam *bad)
 {
 
@@ -428,8 +422,7 @@
     }
 
-  TArrayF *arr = new TArrayF(2);
-  arr->AddAt(nr   ? rms/nr : -1.,0);
-  arr->AddAt(nr>1 ? TMath::Sqrt((rms2 - rms*rms/nr)/(nr-1)) : 0. ,1);
-
+  TArrayF arr(2);
+  arr[0] = nr   ? rms/nr : -1;
+  arr[1] = nr>1 ? TMath::Sqrt((rms2 - rms*rms/nr)/(nr-1)) : 0;
   return arr;
 }
@@ -447,7 +440,5 @@
 // arr[1]: Error (rms) of averaged pedestal (default: 0.)
 //
-// ATTENTION: THE USER HAS TO DELETE THE RETURNED TARRAYF ACCORDINGLY
-//
-TArrayF *MPedestalCam::GetAveragedPedPerSector(const MGeomCam &geom, const UInt_t sec, MBadPixelsCam *bad)
+TArrayF MPedestalCam::GetAveragedPedPerSector(const MGeomCam &geom, const UInt_t sec, MBadPixelsCam *bad)
 {
 
@@ -476,8 +467,7 @@
     }
 
-  TArrayF *arr = new TArrayF(2);
-  arr->AddAt(nr   ? mean/nr : -1.,0);
-  arr->AddAt(nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0. ,1);
-
+  TArrayF arr(2);
+  arr[0] = nr   ? mean/nr : -1;
+  arr[1] = nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0;
   return arr;
 }
@@ -495,7 +485,5 @@
 // arr[1]: Error (rms) of averaged pedestal RMS (default: 0.)
 //
-// ATTENTION: THE USER HAS TO DELETE THE RETURNED TARRAYF ACCORDINGLY
-//
-TArrayF *MPedestalCam::GetAveragedRmsPerSector(const MGeomCam &geom, const UInt_t sec, MBadPixelsCam *bad)
+TArrayF MPedestalCam::GetAveragedRmsPerSector(const MGeomCam &geom, const UInt_t sec, MBadPixelsCam *bad)
 {
 
@@ -524,8 +512,7 @@
     }
 
-  TArrayF *arr = new TArrayF(2);
-  arr->AddAt(nr   ? rms/nr : -1.,0);
-  arr->AddAt(nr>1 ? TMath::Sqrt((rms2 - rms*rms/nr)/(nr-1)) : 0. ,1);
-
+  TArrayF arr(2);
+  arr[0] = nr   ? rms/nr : -1;
+  arr[1] = nr>1 ? TMath::Sqrt((rms2 - rms*rms/nr)/(nr-1)) : 0;
   return arr;
   
Index: trunk/MagicSoft/Mars/mpedestal/MPedestalCam.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedestalCam.h	(revision 5557)
+++ trunk/MagicSoft/Mars/mpedestal/MPedestalCam.h	(revision 5558)
@@ -36,8 +36,8 @@
         MPedestalPix &GetAverageArea   ( UInt_t i );
   const MPedestalPix &GetAverageArea   ( UInt_t i )            const;
-  const Int_t         GetAverageAreas  ()                      const;
+  const Int_t         GetNumAverageArea()                      const;
         MPedestalPix &GetAverageSector ( UInt_t i );
   const MPedestalPix &GetAverageSector ( UInt_t i )            const;
-  const Int_t         GetAverageSectors()                      const;
+  const Int_t         GetNumAverageSector()                    const;
   Float_t             GetPedestalMin   ( const MGeomCam *cam ) const;
   Float_t             GetPedestalMax   ( const MGeomCam *cam ) const;
@@ -45,8 +45,8 @@
   ULong_t             GetTotalEntries  ()                      const { return fTotalEntries; }
 
-  TArrayF *GetAveragedPedPerArea  ( const MGeomCam &geom, const UInt_t ai=0,  MBadPixelsCam *bad=NULL );
-  TArrayF *GetAveragedPedPerSector( const MGeomCam &geom, const UInt_t sec=0, MBadPixelsCam *bad=NULL );  
-  TArrayF *GetAveragedRmsPerArea  ( const MGeomCam &geom, const UInt_t ai=0,  MBadPixelsCam *bad=NULL );
-  TArrayF *GetAveragedRmsPerSector( const MGeomCam &geom, const UInt_t sec=0, MBadPixelsCam *bad=NULL );  
+  TArrayF GetAveragedPedPerArea  ( const MGeomCam &geom, const UInt_t ai=0,  MBadPixelsCam *bad=NULL );
+  TArrayF GetAveragedPedPerSector( const MGeomCam &geom, const UInt_t sec=0, MBadPixelsCam *bad=NULL );
+  TArrayF GetAveragedRmsPerArea  ( const MGeomCam &geom, const UInt_t ai=0,  MBadPixelsCam *bad=NULL );
+  TArrayF GetAveragedRmsPerSector( const MGeomCam &geom, const UInt_t sec=0, MBadPixelsCam *bad=NULL );
   
         MPedestalPix &operator[]       ( Int_t i             );
Index: trunk/MagicSoft/Mars/msignal/MExtractTime.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTime.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/msignal/MExtractTime.cc	(revision 5558)
@@ -215,5 +215,4 @@
 void MExtractTime::Print(Option_t *o) const
 {
-    *fLog << all;
     if (IsA()==MExtractTime::Class())
         *fLog << GetDescriptor() << ":" << endl;
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 5558)
@@ -115,4 +115,7 @@
   if (!fSignals)
       return kFALSE;
+
+  *fLog << flush << inf;
+  Print();
   
   return kTRUE;
@@ -141,6 +144,4 @@
     fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
                                 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
-  
-  Print();
 
   return kTRUE;
@@ -244,11 +245,9 @@
 void MExtractTimeAndCharge::Print(Option_t *o) const
 {
-
-  *fLog << all;
   if (IsA()==MExtractTimeAndCharge::Class())
     *fLog << GetDescriptor() << ":" << endl;
   
   *fLog << dec << endl;
-  *fLog << inf << "Taking " << fNumHiGainSamples
+  *fLog << "Taking " << fNumHiGainSamples
         << " HiGain samples from slice " << (Int_t)fHiGainFirst
         << " to " << (Int_t)(fHiGainLast+fHiLoLast) << " incl" << endl;
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc	(revision 5558)
@@ -1012,30 +1012,22 @@
 void MExtractTimeAndChargeDigitalFilter::Print(Option_t *o) const
 {
-
-  *fLog << all;
-  *fLog << GetDescriptor() << ":" << endl;
-
-  MExtractTimeAndCharge::Print(o);
-  *fLog << " Time Shift HiGain:  " << fTimeShiftHiGain << endl;
-  *fLog << " Time Shift LoGain:  " << fTimeShiftLoGain << endl;
-  *fLog << " Window Size HiGain: " << fWindowSizeHiGain << endl;
-  *fLog << " Window Size LoGain: " << fWindowSizeLoGain << endl;
-  *fLog << " Binning Res HiGain: " << fBinningResolutionHiGain << endl;
-  *fLog << " Binning Res LoGain: " << fBinningResolutionHiGain << endl;
-  *fLog << " Weights File:       " << fNameWeightsFile.Data() << endl;
-  
-  TString opt(o);
-  if (!opt.Contains("weights"))
-    return;
-  
-  *fLog << endl;
-  *fLog << inf << "Using the following weights: " << endl;
-  *fLog << "Hi-Gain:" << endl;
-  for (Int_t i=0; i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
-    *fLog << " " << fAmpWeightsHiGain[i] << " \t " << fTimeWeightsHiGain[i] << endl;
-  
-  *fLog << "Lo-Gain:" << endl;
-  for (Int_t i=0; i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
-    *fLog << " " << fAmpWeightsLoGain[i] << " \t " << fTimeWeightsLoGain[i] << endl;
-  
+    MExtractTimeAndCharge::Print(o);
+    *fLog << " Time Shift  HiGain: " << fTimeShiftHiGain         << "  LoGain: " << fTimeShiftLoGain << endl;
+    *fLog << " Window Size HiGain: " << fWindowSizeHiGain        << "  LoGain: " << fWindowSizeLoGain << endl;
+    *fLog << " Binning Res HiGain: " << fBinningResolutionHiGain << "  LoGain: " << fBinningResolutionHiGain << endl;
+    *fLog << " Weights File:       " << fNameWeightsFile.Data() << endl;
+
+    TString opt(o);
+    if (!opt.Contains("weights"))
+        return;
+
+    *fLog << endl;
+    *fLog << inf << "Using the following weights: " << endl;
+    *fLog << "Hi-Gain:" << endl;
+    for (Int_t i=0; i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
+        *fLog << " " << fAmpWeightsHiGain[i] << " \t " << fTimeWeightsHiGain[i] << endl;
+
+    *fLog << "Lo-Gain:" << endl;
+    for (Int_t i=0; i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
+        *fLog << " " << fAmpWeightsLoGain[i] << " \t " << fTimeWeightsLoGain[i] << endl;
 }
Index: trunk/MagicSoft/Mars/msignal/MExtractor.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 5557)
+++ trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 5558)
@@ -412,6 +412,4 @@
 void MExtractor::Print(Option_t *o) const
 {
-    *fLog << all;
-
     if (IsA()==MExtractor::Class())
         *fLog << GetDescriptor() << ":" << endl;
@@ -419,6 +417,5 @@
     *fLog << " Hi Gain Range:  " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
     *fLog << " Lo Gain Range:  " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
-    *fLog << " Num Samples Hi: " << fNumHiGainSamples << endl;
-    *fLog << " Num Samples Lo: " << fNumHiGainSamples << endl;
     *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
-}
+    *fLog << " Num Samples HiGain: " << fNumHiGainSamples << "  LoGain: " << fNumLoGainSamples << endl;
+}
