Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 5847)
+++ trunk/MagicSoft/Mars/Changelog	(revision 5848)
@@ -42,4 +42,9 @@
        the whole camera including a fit to the values.
 
+   * mpedestal/MPedCalcPedRun.[h,cc]
+     - undo some changes introduced by TB on 2/12/04 which had made the 
+       use of interlaced pedestal events impossible again. 
+     - Use now the new class MTriggerPattern directly to recognize an 
+       interlaced pedestal event.     
 
 
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 5847)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc	(revision 5848)
@@ -139,4 +139,6 @@
 #include "MExtractTimeAndCharge.h"
 
+#include "MTriggerPattern.h"
+
 ClassImp(MPedCalcPedRun);
 
@@ -145,6 +147,5 @@
 const UShort_t MPedCalcPedRun::fgExtractWinFirst       = 0;
 const UShort_t MPedCalcPedRun::fgExtractWinSize        = 6;
-const UInt_t   MPedCalcPedRun::gkFirstRunWithFinalBits = 45589;
-
+const UInt_t   MPedCalcPedRun::gkFirstRunWithFinalBits = 45605;
 // --------------------------------------------------------------------------
 //
@@ -162,10 +163,14 @@
 //
 MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
-    : fOverlap(0)
-{
-    fName  = name  ? name  : "MPedCalcPedRun";
-    fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
-
-    SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
+    : 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);
 }
 
@@ -179,18 +184,25 @@
 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;
+  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;
+}
+
+void MPedCalcPedRun::Reset()
+{
+
+  MExtractPedestal::ResetArrays();
+  fUsedEvents    = 0;
 }
 
@@ -201,7 +213,15 @@
 Int_t MPedCalcPedRun::PreProcess(MParList *pList)
 {
-    fUsedEvents = 0;
-    fIsFirstPedRun = kTRUE;
-    return MExtractPedestal::PreProcess(pList);
+
+  fUsedEvents    = 0;
+  fIsFirstPedRun = kTRUE;
+  fIsNotPedRun   = kFALSE;
+  
+  fTrigPattern = (MTriggerPattern*)pList->FindObject("MTriggerPattern");
+  if (!fTrigPattern)
+    *fLog << warn << GetDescriptor() 
+          << ": MTriggerPattern not found... Cannot use interlaced pedestal events." << endl;
+
+  return MExtractPedestal::PreProcess(pList);
 }
 
@@ -209,35 +229,45 @@
 //
 // The run type is checked for "kRTPedestal"
-// and the variable fSkip is set in that case
+// and the variable fIsNotPedRun is set in that case
 //
 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
-    if (fRunHeader->GetRunType()==MRawRunHeader::kRTPedestal)
+
+  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)
     {
-        fIsFirstPedRun = kFALSE;
-        return kTRUE;
+      fIsFirstPedRun = kFALSE;
+      fIsNotPedRun   = kFALSE;
+      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();
-
+  
+  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;
 }
 
@@ -253,13 +283,16 @@
 Int_t MPedCalcPedRun::Calc()
 {
-    if (!IsPedBitSet())
-    {
-        *fLog << err << GetDescriptor() << " - ERROR: IsPedBitSet() returned kFALSE... abort." << endl;
-        return kERROR;
-    }
-
-    if (fRunHeader->GetRunType() != MRawRunHeader::kRTPedestal)
-        return kTRUE;
-
+
+  if (fIsNotPedRun && !IsPedBitSet())
+    return kTRUE;
+  
+  //  if (fUsedEvents == fNumEventsDump)
+  //    {
+  //      *fLog << endl;
+  //      *fLog << inf << GetDescriptor() << " : Finalize pedestal calculations..." << flush;
+  //      Finalize();
+  //      Reset();
+  //    }
+  
   fUsedEvents++;
 
@@ -277,7 +310,7 @@
       
       if (fExtractor)
-          CalcExtractor(pixel, sum, (*fPedestalsIn)[idx]);
+        CalcExtractor(pixel, sum, (*fPedestalsIn)[idx]);
       else
-          CalcSums(pixel, sum, ab0, ab1);
+        CalcSums(pixel, sum, ab0, ab1);
 
       fSumx[idx]           += sum;
@@ -370,4 +403,5 @@
 }
 
+
 // --------------------------------------------------------------------------
 //
@@ -376,37 +410,38 @@
 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();
-
+
+  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();
+  if (!Finalize())
+    return kFALSE;
+  
+  return MExtractPedestal::PostProcess();
 }
 
@@ -416,19 +451,21 @@
 // The last but one bit is used for the "pedestal-bit".
 //
-// This bit is set since run gkFinalizationTriggerBit
+// This bit is set since run gkFirstRunWithFinalBits
 //
 Bool_t MPedCalcPedRun::IsPedBitSet()
 {
-    if (fRunHeader->GetRunNumber() == 38996)
-        return kTRUE;
-
-    if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
-        return kTRUE;
-
-    return (fEvtHeader->GetTriggerID() & BIT(3)) ? kTRUE : kFALSE;
+
+  if (!fTrigPattern)
+    return kTRUE;
+
+  if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
+    return kTRUE;
+  
+  return (fTrigPattern->GetPrescaled() & MTriggerPattern::kPedestal) ? kTRUE : kFALSE;
 }
 
 void MPedCalcPedRun::Print(Option_t *o) const
 {
+
     MExtractPedestal::Print(o);
 
Index: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h
===================================================================
--- trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h	(revision 5847)
+++ trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.h	(revision 5848)
@@ -6,4 +6,5 @@
 #endif
 
+class MTriggerPattern;
 class MRawEvtPixelIter;
 class MPedestalPix;
@@ -19,5 +20,8 @@
 
     Bool_t  fIsFirstPedRun;    //! Flag to tell if the first run out of many is used
+    Bool_t  fIsNotPedRun;      //! Flag to tell if the current run is a pedestal run
     ULong_t fUsedEvents;       // Number of used (not skipped) events
+
+    MTriggerPattern *fTrigPattern;  //! Trigger pattern decoded
 
     Bool_t IsPedBitSet();
@@ -36,4 +40,6 @@
 
     void Print(Option_t *o="") const;
+    void Reset();
+    
     Int_t Finalize();
 
