Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 7188)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 8151)
@@ -1,3 +1,5 @@
 /* ======================================================================== *\
+! $Name: not supported by cvs2svn $:$Id: MPedCalcPedRun.cc,v 1.49 2006-10-24 07:58:13 tbretz Exp $
+! --------------------------------------------------------------------------
 !
 ! *
@@ -22,5 +24,5 @@
 !   Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
 !
-!   Copyright: MAGIC Software Development, 2000-2004
+!   Copyright: MAGIC Software Development, 2000-2006
 !
 !
@@ -137,5 +139,5 @@
 
 #include "MExtractPedestal.h"
-#include "MExtractTimeAndCharge.h"
+#include "MPedestalSubtractedEvt.h"
 
 #include "MTriggerPattern.h"
@@ -147,62 +149,31 @@
 const UShort_t MPedCalcPedRun::fgExtractWinFirst       = 0;
 const UShort_t MPedCalcPedRun::fgExtractWinSize        = 6;
+
 const UInt_t   MPedCalcPedRun::gkFirstRunWithFinalBits = 45605;
+
 // --------------------------------------------------------------------------
 //
 // Default constructor: 
 //
-// Sets:
-// - all pointers to NULL
-// - fWindowSizeHiGain to fgHiGainWindowSize
-// - fWindowSizeLoGain to fgLoGainWindowSize
-//
 // Calls: 
-// - AddToBranchList("fHiGainPixId");
-// - AddToBranchList("fHiGainFadcSamples");
-// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
+// - SetExtractWindow(fgExtractWinFirst, fgExtractWinSize)
 //
 MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
-    : fOverlap(0), fIsFirstPedRun(kFALSE), fUsedEvents(0), fTrigPattern(NULL)
-{
-  fName  = name  ? name  : "MPedCalcPedRun";
-  fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
-  
-  SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
-  
-  //  SetNumEventsDump(1001);
-  //  SetNumAreasDump(1001);
-  //  SetNumSectorsDump(1001);
-}
-
-// --------------------------------------------------------------------------
-//
-// In case that the variables fExtractLast is greater than the number of 
-// High-Gain FADC samples obtained from the run header, the flag 
-// fOverlap is set to the difference and fExtractLast is set back by the
-// same number of slices.
-//
-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;
-}
-
+    : fIsFirstPedRun(kFALSE), fUsedEvents(0), fTrigPattern(NULL)
+{
+    fName  = name  ? name  : "MPedCalcPedRun";
+    fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
+
+    SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
+}
+
+// --------------------------------------------------------------------------
+//
+// Call MExtractPedestal::reset and set fUsedEvents to 0.
+//
 void MPedCalcPedRun::Reset()
 {
-
-  MExtractPedestal::ResetArrays();
-  fUsedEvents    = 0;
+    MExtractPedestal::ResetArrays();
+    fUsedEvents = 0;
 }
 
@@ -213,14 +184,13 @@
 Int_t MPedCalcPedRun::PreProcess(MParList *pList)
 {
-
-  fUsedEvents    = 0;
-  fIsFirstPedRun = kTRUE;
-  fIsNotPedRun   = kFALSE;
-  
-  fTrigPattern = (MTriggerPattern*)pList->FindObject("MTriggerPattern");
-  if (!fTrigPattern)
-    *fLog << inf << "MTriggerPattern not found... Cannot use interlaced pedestal events." << endl;
-
-  return MExtractPedestal::PreProcess(pList);
+    fUsedEvents    = 0;
+    fIsFirstPedRun = kTRUE;
+    fIsNotPedRun   = kFALSE;
+
+    fTrigPattern = (MTriggerPattern*)pList->FindObject("MTriggerPattern");
+    if (!fTrigPattern)
+        *fLog << inf << "MTriggerPattern not found... Cannot use interlaced pedestal events." << endl;
+
+    return MExtractPedestal::PreProcess(pList);
 }
 
@@ -232,59 +202,56 @@
 Bool_t MPedCalcPedRun::ReInit(MParList *pList)
 {
-
-  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
-  //
-  switch (fRunHeader->GetRunType())
-  {
-  case MRawRunHeader::kRTPedestal:
-  case MRawRunHeader::kRTMonteCarlo:
-      fIsFirstPedRun = kFALSE;
-      fIsNotPedRun   = kFALSE;
-      return kTRUE;
-  
-  case MRawRunHeader::kRTCalibration:
-      {
-          TString proj(fRunHeader->GetProjectName());
-          proj.ToLower();
-
-          // test if a continuous light run has been declared as calibration...
-          if (proj.Contains("cl"))
-          {
-              fIsFirstPedRun = kFALSE;
-              fIsNotPedRun   = kFALSE;
-              return kTRUE;
-          }
-      }
-  }
-
-  fIsNotPedRun = kTRUE;
-
-  //
-  // If this is the first call to ReInit (before reading the first file)
-  // nothing should be done. It occurs for the case that the first treated 
-  // file is not of pedestal type.
-  //
-  if (fIsFirstPedRun)
+    if (!MExtractPedestal::ReInit(pList))
+        return kFALSE;
+
+    //
+    // If this is the first ped run, the next run (call to ReInit)
+    // is not the first anymore
+    //
+    switch (fRunHeader->GetRunType())
+    {
+    case MRawRunHeader::kRTPedestal:
+    case MRawRunHeader::kRTMonteCarlo:
+        fIsFirstPedRun = kFALSE;
+        fIsNotPedRun   = kFALSE;
+        return kTRUE;
+
+    case MRawRunHeader::kRTCalibration:
+        {
+            TString proj(fRunHeader->GetProjectName());
+            proj.ToLower();
+
+            // test if a continuous light run has been declared as calibration...
+            if (proj.Contains("cl"))
+            {
+                fIsFirstPedRun = kFALSE;
+                fIsNotPedRun   = kFALSE;
+                return kTRUE;
+            }
+        }
+    }
+
+    fIsNotPedRun = kTRUE;
+
+    //
+    // If this is the first call to ReInit (before reading the first file)
+    // nothing should be done. It occurs for the case that the first treated
+    // file is not of pedestal type.
+    //
+    if (fIsFirstPedRun)
+        return kTRUE;
+
+    //
+    // In the other case, produce the MPedestalCam to be use the subsequent
+    // (non-pedestal) events
+    //
+    *fLog << inf << "Finalizing pedestal calculations..." << flush;
+
+    if (!Finalize())
+        return kFALSE;
+
+    Reset();
+
     return kTRUE;
-  
-  //
-  // In the other case, produce the MPedestalCam to be use the subsequent
-  // (non-pedestal) events
-  //
-  *fLog << inf << "Finalizing pedestal calculations..." << flush;
-  
-  if (!Finalize())
-    return kFALSE;
-  
-  Reset();
-
-  return kTRUE;
 }
 
@@ -300,130 +267,54 @@
 Int_t MPedCalcPedRun::Calc()
 {
-  if (fIsNotPedRun && !IsPedBitSet())
+    if (fIsNotPedRun && !IsPedBitSet())
+        return kTRUE;
+
+    fUsedEvents++;
+
+    MRawEvtPixelIter pixel(fRawEvt);
+    while (pixel.Next())
+    {
+        const UInt_t idx = pixel.GetPixelId();
+
+        if (!CheckVariation(idx))
+            continue;
+
+        //extract pedestal
+        UInt_t ab[2];
+        const Float_t sum = fExtractor ?
+            CalcExtractor(pixel, 0) :
+            CalcSums(pixel, 0, ab[0], ab[1]);
+
+        if (fIntermediateStorage)
+            (*fPedestalsInter)[idx].Set(sum, 0, 0, fUsedEvents);
+
+        const Float_t sqrsum = sum*sum;
+
+        fSumx[idx]   += sum;
+        fSumx2[idx]  += sqrsum;
+
+        fSumAB0[idx] += ab[0];
+        fSumAB1[idx] += ab[1];
+
+        if (fUseSpecialPixels)
+            continue;
+
+        const UInt_t aidx   = (*fGeom)[idx].GetAidx();
+        const UInt_t sector = (*fGeom)[idx].GetSector();
+
+        fAreaSumx[aidx]      += sum;
+        fAreaSumx2[aidx]     += sqrsum;
+        fSectorSumx[sector]  += sum;
+        fSectorSumx2[sector] += sqrsum;
+
+        fAreaSumAB0[aidx]    += ab[0];
+        fAreaSumAB1[aidx]    += ab[1];
+        fSectorSumAB0[aidx]  += ab[0];
+        fSectorSumAB1[aidx]  += ab[1];
+    }
+
+    fPedestalsOut->SetReadyToSave();
+
     return kTRUE;
-  
-  //  if (fUsedEvents == fNumEventsDump)
-  //    {
-  //      *fLog << endl;
-  //      *fLog << inf << GetDescriptor() << " : Finalize pedestal calculations..." << flush;
-  //      Finalize();
-  //      Reset();
-  //    }
-  
-  fUsedEvents++;
-
-  MRawEvtPixelIter pixel(fRawEvt);
-  
-  while (pixel.Next())
-  {
-      const UInt_t idx = pixel.GetPixelId();
-
-      Float_t sum = 0.;
-      UInt_t  ab0 = 0;
-      UInt_t  ab1 = 0;
-      
-      if (fExtractor)
-        CalcExtractor(pixel, sum, (*fPedestalsIn)[idx]);
-      else
-        CalcSums(pixel, sum, ab0, ab1);
-
-      fSumx[idx] += sum;
-
-      if (fIntermediateStorage)
-        (*fPedestalsInter)[idx].Set(sum,0.,0.,fUsedEvents);
-
-      const Float_t sqrsum = sum*sum;
-
-      fSumx2[idx]  += sqrsum;
-      fSumAB0[idx] += ab0;
-      fSumAB1[idx] += ab1;
-
-      if (fUseSpecialPixels)
-	continue;
-
-      const UInt_t aidx   = (*fGeom)[idx].GetAidx();
-      const UInt_t sector = (*fGeom)[idx].GetSector();      
-
-      fAreaSumx[aidx]      += sum;
-      fSectorSumx[sector]  += sum;
-
-      fAreaSumx2[aidx]     += sqrsum;
-      fSectorSumx2[sector] += sqrsum;
-
-      fAreaSumAB0[aidx]    += ab0;
-      fAreaSumAB1[aidx]    += ab1;
-      
-      fSectorSumAB0[aidx]  += ab0;
-      fSectorSumAB1[aidx]  += ab1;
-  }
-  
-  fPedestalsOut->SetReadyToSave();
-
-  return kTRUE;
-}
-
-
-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();
-
-  Int_t sumi = 0;
-      
-  Int_t  cnt = 0;
-  do
-  {
-      sumi += *ptr;
-      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;
-          else
-              ab0 += *ptr;
-
-          cnt++;
-      } while (++ptr != end);
-  }
-
-  sum = (Float_t)sumi;
 }
 
@@ -472,4 +363,8 @@
 }
 
+//-----------------------------------------------------------------------
+//
+// PostProcess MExtractPedestal and call Finalize
+//
 Int_t MPedCalcPedRun::PostProcess()
 {
@@ -501,14 +396,10 @@
 }
 
+//-----------------------------------------------------------------------
+//
 void MPedCalcPedRun::Print(Option_t *o) const
 {
     MExtractPedestal::Print(o);
 
-    const Int_t last = fExtractor 
-      ? fExtractWinFirst + fExtractor->GetWindowSizeHiGain() -1
-      : fExtractWinLast;
-
-    *fLog << "ExtractWindow from slice      " << fExtractWinFirst << " to " << last << " incl." << endl;
-    *fLog << "Num overlap lo-gain slices:   " << fOverlap << endl;
     *fLog << "First pedrun out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl;
     *fLog << "Number of used events so far: " << fUsedEvents << endl;
