Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 3910)
+++ trunk/MagicSoft/Mars/Changelog	(revision 3911)
@@ -44,4 +44,6 @@
        NaN's are obtained
 
+   * msignal/MExtractPINDiode.[h,cc]
+     - now deriving from MExtractor
 
 
Index: trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc	(revision 3910)
+++ trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc	(revision 3911)
@@ -27,6 +27,19 @@
 //   MExtractPINDiode
 //
+//  Extracts the signal from a fixed window in a given range.
+//
+//  Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast) 
+//  to modify the ranges. Ranges have to be an even number. In case of odd 
+//  ranges, the last slice will be reduced by one.
+//  Defaults are: 
+// 
+//   fHiGainFirst =  fgHiGainFirst =  3 
+//   fHiGainLast  =  fgHiGainLast  =  14
+//   fLoGainFirst =  fgLoGainFirst =  3 
+//   fLoGainLast  =  fgLoGainLast  =  14
+//
 //////////////////////////////////////////////////////////////////////////////
 #include "MExtractPINDiode.h"
+#include "MExtractor.h"
 
 #include <fstream>
@@ -49,15 +62,18 @@
 using namespace std;
 
-const UInt_t MExtractPINDiode::fgPINDiodeIdx     = 1;
-const Byte_t MExtractPINDiode::fgSaturationLimit = 254;
-const Byte_t MExtractPINDiode::fgFirst =  1;
-const Byte_t MExtractPINDiode::fgLast  = 30;
-
+const UInt_t MExtractPINDiode::fgPINDiodeIdx     = 100;
+const Byte_t MExtractPINDiode::fgHiGainFirst =  0;
+const Byte_t MExtractPINDiode::fgHiGainLast  =  14;
+const Byte_t MExtractPINDiode::fgLoGainFirst =  0;
+const Byte_t MExtractPINDiode::fgLoGainLast  =  14;
 // --------------------------------------------------------------------------
 //
 // Default constructor. 
 //
+// Calls: 
+// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
+// - SetPINDiodeIdx()
+//
 MExtractPINDiode::MExtractPINDiode(const char *name, const char *title)
-    : fSaturationLimit(fgSaturationLimit)
 {
   
@@ -65,26 +81,61 @@
   fTitle = title ? title : "Task to extract the signal from the FADC slices";
   
-  AddToBranchList("MRawEvtData.*");
-  
+  SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
   SetPINDiodeIdx();
-  SetSaturationLimit();
-  SetRange();
-}
-
-void MExtractPINDiode::SetRange(Byte_t first, Byte_t last)
-{
-
-    fNumSamples = last-first+1;
-    fFirst      = first;
-    fLast       = last;
-
-    fSqrtSamples = TMath::Sqrt((Float_t)fNumSamples);
-}
-
-// --------------------------------------------------------------------------
-//
-// The PreProcess searches for the following input containers:
-//  - MRawEvtData
-//  - MPedestalCam
+}
+
+// --------------------------------------------------------------------------
+//
+// SetRange: 
+//
+// Checks: 
+// - if the window defined by (fHiGainLast-fHiGainFirst-1) are odd, subtract one
+// - if the window defined by (fLoGainLast-fLoGainFirst-1) are odd, subtract one
+// - if the Hi Gain window is smaller than 2, set fHiGainLast to fHiGainFirst+1
+// - if the Lo Gain window is smaller than 2, set fLoGainLast to fLoGainFirst+1
+// 
+// Calls:
+// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
+// 
+// Sets:
+// - fNumHiGainSamples to: (Float_t)(fHiGainLast-fHiGainFirst+1)
+// - fNumLoGainSamples to: (Float_t)(fLoGainLast-fLoGainFirst+1)
+// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
+// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)  
+//  
+void MExtractPINDiode::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
+{
+
+  const Byte_t window = hilast-hifirst+1+lolast-lofirst+1;
+  const Byte_t weven  = window & ~1;
+
+  if (weven != window)
+    {
+      *fLog << warn << GetDescriptor() 
+            << Form("%s%2i%s%2i",": Total window size has to be even, set last slice from "
+                    ,(int)lolast," to ",(int)(lolast-1)) << endl;
+      lolast -= 1;
+    }
+  
+  if (window<2) 
+    {
+      *fLog << warn << GetDescriptor() 
+            << Form("%s%2i%s%2i",": Total window is smaller than 2 FADC sampes, set last slice from" 
+                    ,(int)lolast," to ",(int)(lofirst+1)) << endl;
+      hilast = hifirst+1;
+    }
+  
+
+  MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
+
+  fNumSamples = fHiGainLast-fHiGainFirst+1+fLoGainLast-fLoGainFirst+1;
+  fSqrtSamples = TMath::Sqrt((Float_t)fNumSamples);
+  
+}
+
+// --------------------------------------------------------------------------
+//
+// Calls: 
+// - MExtractor::PreProcess
 //
 // The following output containers are also searched and created if
@@ -95,27 +146,15 @@
 Int_t MExtractPINDiode::PreProcess(MParList *pList)
 {
-    fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
-    if (!fRawEvt)
-    {
-        *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-
-    fPINDiode = (MExtractedSignalPINDiode*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalPINDiode"));
-    if (!fPINDiode)
-        return kFALSE;
-
-    fPINDiode->SetUsedFADCSlices(fFirst, fLast);
-
-    fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber("MPedestalCam"));
-
-    if (!fPedestals)
-    {
-        *fLog << err << AddSerialNumber("MPedestalCam") << " not found... aborting" << endl;
-        return kFALSE;
-    }
-
-    const MPedestalPix &ped   = (*fPedestals)[fPINDiodeIdx]; 
+
+  if (!MExtractor::PreProcess(pList))
+    return kFALSE;
+  
+  fPINDiode = (MExtractedSignalPINDiode*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalPINDiode"));
+  if (!fPINDiode)
+    return kFALSE;
+
+  fPINDiode->SetUsedFADCSlices(fHiGainFirst, fLoGainLast);
+
+  const MPedestalPix &ped   = (*fPedestals)[fPINDiodeIdx]; 
 
     if (&ped)
@@ -134,15 +173,28 @@
 }
 
-void MExtractPINDiode::FindSignal(Byte_t *ptr, Int_t size, UInt_t &sum, UInt_t &sum2, UInt_t &sat, UInt_t &max) const
-{
-
-  Byte_t *end = ptr + size;
-  
+void MExtractPINDiode::FindSignalandVarianceHiGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const
+{
+
+  Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
+
   while (ptr<end)
     {
       sum  += *ptr;
       sum2 += *ptr * *ptr;
-      if (*ptr > max)
-        max = *ptr;
+
+      if (*ptr++ >= fSaturationLimit)
+        sat++;
+    }
+}
+
+void MExtractPINDiode::FindSignalandVarianceLoGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const
+{
+
+  Byte_t *end = ptr +  fLoGainLast - fLoGainFirst  + 1;
+
+  while (ptr<end)
+    {
+      sum  += *ptr;
+      sum2 += *ptr * *ptr;
       
       if (*ptr++ >= fSaturationLimit)
@@ -151,4 +203,6 @@
 }
 
+
+
 // --------------------------------------------------------------------------
 //
@@ -159,40 +213,43 @@
 {
 
-    MRawEvtPixelIter pixel(fRawEvt);
-
-    fPINDiode->Clear();
-
-    pixel.Jump(fPINDiodeIdx);
- 
-    UInt_t sum  = 0;
-    UInt_t sum2 = 0;
-    UInt_t sat  = 0;
-    UInt_t max  = 0;
-
-    FindSignal(pixel.GetHiGainSamples()+fFirst-1, 
-               fRawEvt->GetNumHiGainSamples(), 
-               sum, sum2, sat, max);
-    FindSignal(pixel.GetLoGainSamples(), 
-               fLast-fRawEvt->GetNumLoGainSamples(), 
-               sum, sum2, sat, max);
-    
-
-    const Float_t var = ((Float_t)sum2 - (Float_t)sum*sum/fNumSamples)/(fNumSamples-1);
-    const Float_t rms = TMath::Sqrt(var);
-    
-    // 
-    // FIXME: The following formulae have to be revised!!
-    //
-    fPINDiode->SetExtractedSignal(sum - fPedestal*fNumSamples, fPedRms*fSqrtSamples);
-    fPINDiode->SetExtractedRms   (rms, rms/2./fSqrtSamples);
-    fPINDiode->SetExtractedTime  (max, rms/fSqrtSamples);
-    fPINDiode->SetSaturation(sat);
-
-    if (sat)
-      *fLog << warn << "WARNING - saturation occurred in the PIN Diode " << endl;
-
-    fPINDiode->SetReadyToSave();
-
-    return kTRUE;
+
+  MRawEvtPixelIter pixel(fRawEvt);
+  
+  fPINDiode->Clear();
+  
+  pixel.Jump(fPINDiodeIdx);
+  
+  Int_t sum   = 0;
+  Int_t sum2  = 0;
+  Byte_t sat  = 0;
+  Int_t max   = 0;
+  
+  // 
+  // Calculate first the time:
+  //
+  const Int_t maxhi = pixel.GetIdxMaxHiGainSample();
+  const Int_t maxlo = pixel.GetIdxMaxLoGainSample();
+  
+  if (maxhi > maxlo)
+    max = maxhi;
+  else
+    max = maxlo + pixel.GetNumHiGainSamples();
+  
+  FindSignalandVarianceHiGain(pixel.GetHiGainSamples()+fHiGainFirst,sum,sum2,sat);
+  FindSignalandVarianceLoGain(pixel.GetLoGainSamples()+fLoGainFirst,sum,sum2,sat);
+
+  const Float_t var = ((Float_t)sum2 - (Float_t)sum*sum/fNumSamples)/(fNumSamples-1);
+  const Float_t rms = TMath::Sqrt(var);
+  
+  // 
+  // FIXME: The following formulae have to be revised!!
+  //
+  fPINDiode->SetExtractedSignal(sum - fPedestal*fNumSamples, fPedRms*fSqrtSamples);
+  fPINDiode->SetExtractedRms   (rms, rms/2./fSqrtSamples);
+  fPINDiode->SetExtractedTime  (max, rms/fSqrtSamples);
+  fPINDiode->SetSaturation(sat);
+  fPINDiode->SetReadyToSave();
+  
+  return kTRUE;
 }
 
@@ -214,6 +271,6 @@
     }
 
-    const Bool_t arg2 = fNumSamples+fFirst-1 != fgLast;
-    const Bool_t arg1 = arg2 || fFirst != fgFirst;
+    const Bool_t arg2 = fNumSamples+fHiGainFirst-1 != fgLoGainLast;
+    const Bool_t arg1 = arg2 || fHiGainFirst != fgHiGainFirst;
 
     if (!arg1)
@@ -221,7 +278,7 @@
 
     out << "   " << GetUniqueName() << ".SetRange(";
-    out << (int)fFirst;
+    out << (int)fHiGainFirst;
     if (arg2)
-      out << ", " << (int)(fNumSamples+fFirst-1);
+      out << ", " << (int)(fNumSamples+fHiGainFirst-1);
     out << ");" << endl;
 }
Index: trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h	(revision 3910)
+++ trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h	(revision 3911)
@@ -11,40 +11,30 @@
 /////////////////////////////////////////////////////////////////////////////
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MExtractor
+#include "MExtractor.h"
 #endif
 
-class MRawEvtData;
-class MRawRunHeader;
-
-class MPedestalCam;
 class MExtractedSignalPINDiode;
-class MExtractPINDiode : public MTask
+class MExtractPINDiode : public MExtractor
 {
 private:
 
   static const UInt_t fgPINDiodeIdx;  
-  static const Byte_t fgSaturationLimit;
-  static const Byte_t fgFirst;
-  static const Byte_t fgLast;
+  static const Byte_t fgHiGainFirst;     // First FADC slice Hi-Gain (currently set to: 3) 
+  static const Byte_t fgHiGainLast;      // Last FADC slice Hi-Gain (currently set to: 14) 
+  static const Byte_t fgLoGainFirst;     // First FADC slice Lo-Gain (currently set to: 3) 
+  static const Byte_t fgLoGainLast;      // Last FADC slice Lo-Gain (currently set to: 14) 
 
-  MPedestalCam              *fPedestals;    // Pedestals of all pixels in the camera
   MExtractedSignalPINDiode  *fPINDiode;     // Extracted signal of the PIN Diode
 
-  MRawEvtData         *fRawEvt;       // raw event data (time slices)
-  MRawRunHeader       *fRunHeader;    // RunHeader information
-  
-  Byte_t  fFirst;
-  Byte_t  fLast;
-  Byte_t  fNumSamples;
-  Float_t fSqrtSamples;
-  Byte_t  fSaturationLimit;
   UInt_t  fPINDiodeIdx;
-
   Float_t fPedestal;
   Float_t fPedRms;
+  Int_t   fNumSamples;
+  Float_t fSqrtSamples;
   
-  void   FindSignal(Byte_t *ptr, Int_t size, UInt_t &sum, UInt_t &sum2, UInt_t &sat, UInt_t &max) const;
-  
+  void   FindSignalandVarianceHiGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const;
+  void   FindSignalandVarianceLoGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const;
+
   Int_t  PreProcess(MParList *pList);
   Int_t  Process();
@@ -56,10 +46,8 @@
 
   // Setters
-  void SetRange(const Byte_t hifirst=fgFirst, 
-                const Byte_t hilast=fgLast);
-  void SetSaturationLimit(const Byte_t lim=fgSaturationLimit) { fSaturationLimit = lim; }
+  void SetRange(Byte_t hifirst=0, Byte_t hilast=0, Byte_t lofirst=0, Byte_t lolast=0);
   void SetPINDiodeIdx(    const UInt_t idx=fgPINDiodeIdx    ) { fPINDiodeIdx     = idx; }   
 
-  ClassDef(MExtractPINDiode, 0) // Task to fill the Extracted PINDiode Containers from raw data
+  ClassDef(MExtractPINDiode, 0) // Signal Extractor for the PIN Diode
 };
 
