Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 3961)
+++ trunk/MagicSoft/Mars/Changelog	(revision 3962)
@@ -65,4 +65,12 @@
    * mcalib/MHGausEvents.cc
      - took out fEvents(0) and fHGausHist() from constructor
+
+   * msignal/MExtractor.cc
+   * msignal/MExtractPINDiode.cc
+     - fixed StreamPrimitive
+
+   * msignal/MExtractBlindPixel.[h,cc]
+     - deriving from MExtractor, possibility to filter NSB events
+
  
  2004/05/03: Thomas Bretz
Index: trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.cc	(revision 3961)
+++ trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.cc	(revision 3962)
@@ -27,4 +27,17 @@
 //   MExtractBlindPixel
 //
+//  Extracts the signal from a fixed window in a given range.
+//
+//  Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast) 
+//  to modify the ranges. The "lo-gain" ranges are used for the NSB rejection 
+//  whereas the high-gain ranges for blind pixel signal extraction. "High-gain" 
+//  ranges can extend to the slices stored as "low-gain" in MRawEvtPixelIter
+//  Defaults are: 
+// 
+//   fHiGainFirst =  fgHiGainFirst =  12 
+//   fHiGainLast  =  fgHiGainLast  =  16
+//   fLoGainFirst =  fgLoGainFirst =  0 
+//   fLoGainLast  =  fgLoGainLast  =  10
+//
 //////////////////////////////////////////////////////////////////////////////
 #include "MExtractBlindPixel.h"
@@ -40,24 +53,31 @@
 #include "MRawEvtPixelIter.h"
 
+#include "MExtractedSignalBlindPixel.h"
+
 #include "MPedestalCam.h"
 #include "MPedestalPix.h"
 
-#include "MExtractedSignalBlindPixel.h"
-
 ClassImp(MExtractBlindPixel);
 
 using namespace std;
 
-const Int_t  MExtractBlindPixel::fgBlindPixelIdx   = 559;
-const Byte_t MExtractBlindPixel::fgSaturationLimit = 254;
-const Byte_t MExtractBlindPixel::fgFirst =  3;
-const Byte_t MExtractBlindPixel::fgLast  = 16;
-
+const Int_t  MExtractBlindPixel::fgBlindPixelIdx  = 559;
+const Int_t  MExtractBlindPixel::fgNSBFilterLimit = 800;
+const Byte_t MExtractBlindPixel::fgHiGainFirst    =  12;
+const Byte_t MExtractBlindPixel::fgHiGainLast     =  29;
+const Byte_t MExtractBlindPixel::fgLoGainFirst    =  0;
+const Byte_t MExtractBlindPixel::fgLoGainLast     =  10;
 // --------------------------------------------------------------------------
 //
 // Default constructor. 
 //
+// Initializes:
+// - fBlindPixelIdx to fgBlindPixelIdx
+// - fNSBFilterLimit to fgNSBFilterLimit
+//
+// Calls:
+// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
+//
 MExtractBlindPixel::MExtractBlindPixel(const char *name, const char *title)
-    : fSaturationLimit(fgSaturationLimit)
 {
   
@@ -68,16 +88,20 @@
   
   SetBlindPixelIdx();
-  SetSaturationLimit();
-  SetRange();
-}
-
-void MExtractBlindPixel::SetRange(Byte_t first, Byte_t last)
-{
-
-    fNumSamples = last-first+1;
-    fFirst      = first;
-    fLast       = last;
-
-    fSqrtSamples = TMath::Sqrt((Float_t)fNumSamples);
+  SetNSBFilterLimit();
+  SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
+
+}
+
+void MExtractBlindPixel::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
+{
+
+  MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
+
+  fNumHiGainSamples = (Float_t)(fHiGainLast-fHiGainFirst+1);
+  fNumLoGainSamples = (Float_t)(fLoGainLast-fLoGainFirst+1);  
+
+  fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
+  fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);  
+  
 }
 
@@ -94,26 +118,86 @@
 Int_t MExtractBlindPixel::PreProcess(MParList *pList)
 {
-    fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
-    if (!fRawEvt)
-    {
-        *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber("MPedestalCam"));
-    if (!fPedestals)
-    {
-        *fLog << err << AddSerialNumber("MPedestalCam") << " not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    fBlindPixel = (MExtractedSignalBlindPixel*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalBlindPixel"));
-    if (!fBlindPixel)
-        return kFALSE;
-
-    fBlindPixel->SetUsedFADCSlices(fFirst, fLast);
-    fBlindPixel->SetBlindPixelIdx(fBlindPixelIdx);
-
-    return kTRUE;
+
+  if (!MExtractor::PreProcess(pList))
+    return kFALSE;
+  
+  fBlindPixel = (MExtractedSignalBlindPixel*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalBlindPixel"));
+  if (!fBlindPixel)
+    return kFALSE;
+
+  fBlindPixel->SetBlindPixelIdx(fBlindPixelIdx);
+  fBlindPixel->SetUsedFADCSlices(fHiGainFirst, fHiGainLast);
+  
+  MPedestalPix &pedpix  = (*fPedestals)[fBlindPixelIdx];    
+  
+  if (&pedpix)
+    {
+      fBlindPixel->SetPed      ( pedpix.GetPedestal()   * fNumLoGainSamples );
+      fBlindPixel->SetPedErr   ( pedpix.GetPedestalRms()* fNumLoGainSamples 
+                                 / TMath::Sqrt((Float_t)fPedestals->GetTotalEntries()) );
+      fBlindPixel->SetPedRms   ( pedpix.GetPedestalRms()* TMath::Sqrt((Float_t)fNumLoGainSamples) );
+      fBlindPixel->SetPedRmsErr( fBlindPixel->GetPedErr()/2. );
+    }
+  
+  return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// FindSignalHiGain:
+//
+// - Loop from ptr to (ptr+fHiGainLast-fHiGainFirst)
+// - Sum up contents of *ptr
+// - If *ptr is greater than fSaturationLimit, raise sat by 1
+// - If fHiLoLast is set, loop from logain to (logain+fHiLoLast)
+// - Add contents of *logain to sum
+// 
+void MExtractBlindPixel::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Int_t &sum, Byte_t &sat) const
+{
+
+  Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
+  
+  while (ptr<end)
+    {
+      sum += *ptr;
+      
+      if (*ptr++ >= fSaturationLimit)
+        sat++;
+    }
+
+  if (fHiLoLast == 0)
+    return;
+  
+  end = logain + fHiLoLast;
+  while (logain<end)
+    {
+      sum += *logain;
+      
+      if (*logain++ >= fSaturationLimit)
+            sat++;
+    }
+
+}
+
+// --------------------------------------------------------------------------
+//
+// FindSignalFilter:
+//
+// - Loop from ptr to (ptr+fLoGainLast-fLoGainFirst)
+// - Sum up contents of *ptr
+// - If *ptr is greater than fSaturationLimit, raise sat by 1
+// 
+void MExtractBlindPixel::FindSignalFilter(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
+{
+
+  Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1;
+  
+  while (ptr<end)
+    {
+      sum += *ptr;
+      
+      if (*ptr++ >= fSaturationLimit)
+        sat++;
+    }
 }
 
@@ -126,111 +210,34 @@
 {
 
-    MRawEvtPixelIter pixel(fRawEvt);
-
-    fBlindPixel->Clear();
-
-    pixel.Jump(fBlindPixelIdx);
- 
-    const UInt_t nhigain = pixel.GetNumHiGainSamples();
-
-    Byte_t *ptr = pixel.GetHiGainSamples();
-    
-    //
-    // We need a dedicated signal extractor for the blind pixel
-    //
-    Int_t  diff  = 0;
-    UInt_t first = fFirst;
-    UInt_t last  = fLast;
-    UInt_t sat  = 0;
-
-    if (last > nhigain)
-      {
-        diff = last - nhigain;
-        last = nhigain;
-      }
-    
-    
-    Byte_t *start   = ptr + first - 1;
-    Byte_t *end     = ptr + last  - 1;
-    
-    ptr = start;
-    
-    Int_t sum = 0;
-    
-    while (ptr<=end)
-      {
-        sum += *ptr;
-
-        if (*ptr++ >= fSaturationLimit)
-          sat++;
-      }
-    
-    if (diff > 0)
-      {
-        ptr = pixel.GetLoGainSamples();
-        end = ptr + diff - 1;
-        
-        while (ptr<=end)
-          {
-
-            sum += *ptr;
-
-            if (*ptr++ >= fSaturationLimit)
-              sat++;
-
-          }
-      }
-    
-    fBlindPixel->SetExtractedSignal(sum);
-    fBlindPixel->SetNumSaturated(sat);
-    fBlindPixel->SetReadyToSave();
-
-    return kTRUE;
-}
-
-Int_t MExtractBlindPixel::PostProcess()
-{
-
-    MPedestalPix &pedpix  = (*fPedestals)[fBlindPixelIdx];    
-
-    if (&pedpix)
-    {
-	 fBlindPixel->SetPed      ( pedpix.GetPedestal()   * fNumSamples );
-	 fBlindPixel->SetPedErr   ( pedpix.GetPedestalRms()* fNumSamples / TMath::Sqrt((Float_t)fPedestals->GetTotalEntries()) );
-	 fBlindPixel->SetPedRms   ( pedpix.GetPedestalRms()* TMath::Sqrt((Float_t)fNumSamples) );
-	 fBlindPixel->SetPedRmsErr( fBlindPixel->GetPedErr()/2. );
-    }
-
-    return kTRUE;
-
-}
-
-// --------------------------------------------------------------------------
-//
-// Implementation of SavePrimitive. Used to write the call to a constructor
-// to a macro. In the original root implementation it is used to write
-// gui elements to a macro-file.
-//
-void MExtractBlindPixel::StreamPrimitive(ofstream &out) const
-{
-    out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
-    out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
-
-    if (fSaturationLimit!=fgSaturationLimit)
-    {
-        out << "   " << GetUniqueName() << ".SetSaturationLimit(";
-        out << (int)fSaturationLimit << ");" << endl;
-    }
-
-    const Bool_t arg2 = fNumSamples+fFirst-1 != fgLast;
-    const Bool_t arg1 = arg2 || fFirst != fgFirst;
-
-    if (!arg1)
-        return;
-
-    out << "   " << GetUniqueName() << ".SetRange(";
-    out << (int)fFirst;
-    if (arg2)
-      out << ", " << (int)(fNumSamples+fFirst-1);
-    out << ");" << endl;
-}
+  MRawEvtPixelIter pixel(fRawEvt);
+  
+  fBlindPixel->Clear();
+  
+  pixel.Jump(fBlindPixelIdx);
+  
+  Int_t sum   = 0;
+  Byte_t sat  = 0;
+
+  FindSignalFilter(pixel.GetHiGainSamples()+fLoGainFirst, sum, sat);
+
+  if (sum > fNSBFilterLimit)
+    {
+      sum = -1;
+      fBlindPixel->SetExtractedSignal(sum);
+      fBlindPixel->SetNumSaturated(sat);
+      fBlindPixel->SetReadyToSave();
+      return kTRUE;
+    }
+
+  sum = 0;
+  sat = 0;
+
+  FindSignalHiGain(pixel.GetHiGainSamples()+fLoGainFirst, pixel.GetLoGainSamples(), sum, sat);
+  
+  fBlindPixel->SetExtractedSignal(sum);
+  fBlindPixel->SetNumSaturated(sat);
+  fBlindPixel->SetReadyToSave();
+  
+  return kTRUE;
+}
+
Index: trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.h	(revision 3961)
+++ trunk/MagicSoft/Mars/msignal/MExtractBlindPixel.h	(revision 3962)
@@ -4,5 +4,5 @@
 /////////////////////////////////////////////////////////////////////////////
 //                                                                         //
-// MExtractBlindPixel                                                        //
+// MExtractBlindPixel                                                      //
 //                                                                         //
 // Integrates the time slices of the all pixels of a calibration event     //
@@ -11,40 +11,31 @@
 /////////////////////////////////////////////////////////////////////////////
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MExtractor
+#include "MExtractor.h"
 #endif
 
-class MRawEvtData;
-class MRawRunHeader;
-class MRawEvtPixelIter;
-class MPedestalCam;
 class MExtractedSignalBlindPixel;
-class MExtractBlindPixel : public MTask
+class MExtractBlindPixel : public MExtractor
 {
 private:
 
-  static const Int_t  fgBlindPixelIdx;  
-  static const Byte_t fgSaturationLimit;
-  static const Byte_t fgFirst;
-  static const Byte_t fgLast;
+  static const Int_t  fgBlindPixelIdx;
+  static const Int_t  fgNSBFilterLimit;  
+  static const Byte_t fgHiGainFirst;     // First FADC slice Hi-Gain (currently set to: 0) 
+  static const Byte_t fgHiGainLast;      // Last FADC slice Hi-Gain (currently set to: 11) 
+  static const Byte_t fgLoGainFirst;     // First FADC slice Lo-Gain (currently set to: 0) 
+  static const Byte_t fgLoGainLast;      // Last FADC slice Lo-Gain (currently set to:  2) 
 
   MExtractedSignalBlindPixel  *fBlindPixel;   // Extracted signal of the Blind Pixel
 
-  MRawEvtData         *fRawEvt;       // raw event data (time slices)
-  MRawRunHeader       *fRunHeader;    // RunHeader information
-  MPedestalCam        *fPedestals;    // pointer to the pedestal information
+  Int_t   fBlindPixelIdx;
+  Int_t   fNSBFilterLimit;  
+
+  void FindSignalHiGain(Byte_t *firstused, Byte_t *lowgain, Int_t &sum, Byte_t &sat) const;
+  void FindSignalFilter(Byte_t *ptr, Int_t &sum, Byte_t &sat) const;
   
-  Byte_t  fFirst;
-  Byte_t  fLast;
-  Byte_t  fNumSamples;
-  Float_t fSqrtSamples;
-  Byte_t  fSaturationLimit;
-
-  Int_t  fBlindPixelIdx;
   Int_t  PreProcess(MParList *pList);
   Int_t  Process();
-  Int_t  PostProcess();
-  void   StreamPrimitive(ofstream &out) const;
-  
+
 public:
 
@@ -52,9 +43,9 @@
 
   // Setters
-  void SetRange(const Byte_t first=fgFirst, const Byte_t last=fgLast);
-  void SetSaturationLimit(const Byte_t lim=fgSaturationLimit) { fSaturationLimit = lim; }
-  void SetBlindPixelIdx(  const  Int_t idx=fgBlindPixelIdx  ) { fBlindPixelIdx   = idx; }  
+  void SetRange(Byte_t hifirst=0, Byte_t hilast=0, Byte_t lofirst=0, Byte_t lolast=0);
+  void SetBlindPixelIdx(  const  Int_t idx=fgBlindPixelIdx  ) { fBlindPixelIdx   = idx; }
+  void SetNSBFilterLimit(  const  Int_t lim=fgNSBFilterLimit ) { fNSBFilterLimit     = lim; }    
 
-  ClassDef(MExtractBlindPixel, 0) // Task to fill the Extracted BlindPixel Containers from raw data
+  ClassDef(MExtractBlindPixel, 0) // Signal Extractor for the Blind Pixel
 };
 
Index: trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc	(revision 3961)
+++ trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc	(revision 3962)
@@ -41,5 +41,4 @@
 //////////////////////////////////////////////////////////////////////////////
 #include "MExtractPINDiode.h"
-#include "MExtractor.h"
 
 #include <fstream>
@@ -278,31 +277,2 @@
 }
 
-// --------------------------------------------------------------------------
-//
-// Implementation of SavePrimitive. Used to write the call to a constructor
-// to a macro. In the original root implementation it is used to write
-// gui elements to a macro-file.
-//
-void MExtractPINDiode::StreamPrimitive(ofstream &out) const
-{
-    out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
-    out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
-
-    if (fSaturationLimit!=fgSaturationLimit)
-    {
-        out << "   " << GetUniqueName() << ".SetSaturationLimit(";
-        out << (int)fSaturationLimit << ");" << endl;
-    }
-
-    const Bool_t arg2 = fNumSamples+fHiGainFirst-1 != fgLoGainLast;
-    const Bool_t arg1 = arg2 || fHiGainFirst != fgHiGainFirst;
-
-    if (!arg1)
-        return;
-
-    out << "   " << GetUniqueName() << ".SetRange(";
-    out << (int)fHiGainFirst;
-    if (arg2)
-      out << ", " << (int)(fNumSamples+fHiGainFirst-1);
-    out << ");" << endl;
-}
Index: trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h	(revision 3961)
+++ trunk/MagicSoft/Mars/msignal/MExtractPINDiode.h	(revision 3962)
@@ -40,5 +40,4 @@
   Bool_t ReInit(MParList *pList);  
   Int_t  Process();
-  void   StreamPrimitive(ofstream &out) const;
   
 public:
Index: trunk/MagicSoft/Mars/msignal/MExtractor.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 3961)
+++ trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 3962)
@@ -279,4 +279,20 @@
 void MExtractor::StreamPrimitive(ofstream &out) const
 {
-}
-
+
+  out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
+  out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
+  
+  if (fSaturationLimit!=fgSaturationLimit)
+    {
+      out << "   " << GetUniqueName() << ".SetSaturationLimit(";
+      out << (int)fSaturationLimit << ");" << endl;
+    }
+  
+  out << "   " << GetUniqueName() << ".SetRange(";
+  out << (int)fHiGainFirst;
+  out << ", " << (int)fHiGainLast;
+  out << ", " << (int)fLoGainFirst;
+  out << ", " << (int)fLoGainLast;
+  out << ");" << endl;
+}
+
