Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 3197)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 3202)
@@ -34,5 +34,5 @@
 //   for every pixel. It is filled in the following way:
 //
-//   ProProcess: Search for MPedestalCam, MExtractedSignalCam
+//   ProProcess: Search for MPedestalCam, MExtractedSignalCam and MExtractedSignalBlindPixel
 //               Initialize MCalibrationCam
 //               Initialize pulser light wavelength
@@ -69,4 +69,5 @@
 //   MPedestalCam
 //   MExtractedSignalCam
+//   MExtractedSignalBlindPixel
 //
 //  Output Containers:
@@ -102,4 +103,5 @@
 #include "MExtractedSignalCam.h"
 #include "MExtractedSignalPix.h"
+#include "MExtractedSignalBlindPixel.h"
 
 #include "MCalibrationBlindPix.h"
@@ -109,10 +111,7 @@
 using namespace std;
 
-const UInt_t MCalibrationCalc::fgBlindPixelId    = 559;
-const UInt_t MCalibrationCalc::fgPINDiodeId      = 9999;
-const Byte_t MCalibrationCalc::fgSaturationLimit = 254;
-const Int_t  MCalibrationCalc::fgBlindPixelFirst = 3;
-const Int_t  MCalibrationCalc::fgBlindPixelLast  = 14;
-const Int_t  MCalibrationCalc::fgBlindPixelSinglePheCut = 400;
+const UInt_t MCalibrationCalc::fgBlindPixelIdx   = 559;
+const UInt_t MCalibrationCalc::fgPINDiodeIdx     = 9999;
+const UInt_t MCalibrationCalc::fgBlindPixelSinglePheCut = 400;
 
 // --------------------------------------------------------------------------
@@ -134,5 +133,6 @@
 
     Clear();
-    SetBlindPixelRange();
+    SetBlindPixelIdx();
+    SetPINDiodeIdx();
 }
 
@@ -146,7 +146,4 @@
     CLRBIT(fFlags, kHiGainOverFlow);
     CLRBIT(fFlags, kLoGainOverFlow);
-
-    fBlindPixelFirst   = 0;
-    fBlindPixelLast    = 0;
 
     fNumBlindPixelSinglePhe = 0;
@@ -159,12 +156,4 @@
 
     fColor = kECT1;
-}
-
-void MCalibrationCalc::SetBlindPixelRange(Int_t first, Int_t last)
-{
-  
-  fBlindPixelFirst = first;
-  fBlindPixelLast  = last;
-
 }
 
@@ -251,4 +240,11 @@
         return kFALSE;
       }
+
+    fBlindPixel = (MExtractedSignalBlindPixel*)pList->FindObject("MExtractedSignalBlindPixel");
+    if (!fBlindPixel)
+      {
+        *fLog << err << dbginf << "Cannot find MExtractedSignalBlindPixel ... aborting" << endl;
+        return kFALSE;
+      }
     
     return kTRUE;
@@ -303,8 +299,8 @@
 
         // Exclude the blind pixel and the PIN Diode from normal pixel calibration:
-        if (i == fgBlindPixelId)
+        if (i == fBlindPixelIdx)
           pix.SetExcluded();
 
-        if (i == fgPINDiodeId)
+        if (i == fPINDiodeIdx)
           pix.SetExcluded();
 
@@ -381,8 +377,4 @@
 {
 
-  //
-  // Initialize pointers to blind pixel, PIN Diode and individual pixels
-  //
-  MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
   
   MRawEvtPixelIter pixel(fRawEvt);
@@ -391,8 +383,52 @@
   // Create a loop to fill the calibration histograms
   // Search for: a signal in MExtractedSignalCam 
+  // Search for: a signal in MExtractedSignalBlindPixel
   // Fill histograms with:
   //             charge
   //             charge vs. event nr.
   //
+  //
+  // Initialize pointers to blind pixel and individual pixels
+  //
+  // FIXME: filling the bind pixel histograms in this class is preliminary 
+  //        and will be replaced soon to fill them with MFillH
+  //
+  MCalibrationBlindPix       &blindpixel = *(fCalibrations->GetBlindPixel());
+  MExtractedSignalBlindPixel &blindsig   = (*fBlindPixel);
+
+  const UInt_t signal = blindsig.GetExtractedSignal();
+  
+  if (!blindpixel.FillCharge(signal)) 
+    *fLog << warn << 
+      "Overflow or Underflow occurred filling Blind Pixel sum = " << signal << endl;
+
+  blindpixel.FillGraphs(signal,0);
+  
+  TH1I *hist;
+  
+  if (signal > fBlindPixelSinglePheCut)
+    {
+      hist = (blindpixel.GetHist())->GetHSinglePheFADCSlices();
+      fNumBlindPixelSinglePhe++;
+    }
+  else
+    {
+      hist = (blindpixel.GetHist())->GetHPedestalFADCSlices();
+      fNumBlindPixelPedestal++;
+    }
+
+  pixel.Jump(fBlindPixelIdx);
+  
+  const Byte_t *ptr = pixel.GetHiGainSamples();
+
+  for (Int_t i=1;i<16;i++)
+    hist->Fill(i,*ptr++);
+  
+  ptr = pixel.GetLoGainSamples();
+  for (Int_t i=16;i<31;i++)
+    hist->Fill(i,*ptr++);
+
+  pixel.Reset();
+  
   while (pixel.Next())
     {
@@ -400,7 +436,6 @@
       const UInt_t pixid = pixel.GetPixelId();
       
-      MCalibrationPix &pix = (*fCalibrations)[pixid];
-      
-      MExtractedSignalPix &sig =  (*fSignals)[pixid];
+      MCalibrationPix            &pix   =  (*fCalibrations)[pixid];
+      MExtractedSignalPix        &sig   =  (*fSignals)     [pixid];
       
       const Float_t sumhi  = sig.GetExtractedSignalHiGain();
@@ -408,4 +443,5 @@
 
       Float_t abstime = 0.;
+
       if (sig.IsLoGainUsed())
         abstime = (Float_t)pixel.GetIdxMaxLoGainSample();
@@ -413,147 +449,30 @@
         abstime = (Float_t)pixel.GetIdxMaxHiGainSample();
 
-      switch(pixid)
+      if (pix.IsExcluded())
+        continue;
+      
+      pix.FillGraphs(sumhi,sumlo);
+
+      if (sig.IsLoGainUsed())
         {
           
-        case fgBlindPixelId:
-
-          if (TESTBIT(fFlags,kUseBlindPixelFit))
-            {
+          if (!pix.FillChargeLoGain(sumlo))
+            *fLog << warn << "Could not fill Lo Gain Charge of pixel: " << pixid 
+                  << " signal = " << sumlo << endl;
           
-              Byte_t *ptr = pixel.GetHiGainSamples();
-
-              Int_t blindpixelsumhi = 0;
-              Int_t blindpixelsumlo = 0;
-              //
-              // We need a dedicated signal extractor for the blind pixel
-              //
-              Int_t diff  = 0;
-              Int_t last  = fBlindPixelLast;
-              Int_t first = fBlindPixelFirst;
-
-              if (last > 15)
-                {
-                  diff = last - 15;
-                  last = 15;
-                }
-
-              Byte_t *start   = ptr + first - 1;
-              Byte_t *end     = ptr + last  - 1;
-
-              ptr = start;
-
-              Int_t sum = 0;
-              
-              while (ptr<=end)
-                {
-                  sum += *ptr;
-                  ptr++;
-                }
-
-              if (diff > 0)
-                {
-                  ptr = pixel.GetLoGainSamples();
-                  end = ptr + diff - 1;
-                  
-                  while (ptr<=end)
-                    {
-                      sum += *ptr;
-                      ptr++;
-                    }
-                }
-              
-              blindpixelsumhi = sum;
-
-              ptr = pixel.GetLoGainSamples();
-
-              start   = ptr + fBlindPixelFirst - 1;
-              end     = ptr + fBlindPixelLast;
-
-              ptr = start;
-
-              sum = 0;
-              
-              while (++ptr<end)
-                sum += *ptr;
-
-              blindpixelsumlo = sum;
-
-              //              if (!CalcSignalBlindPixel(hiptr, blindpixelsumhi))
-              //                return kFALSE;
-
-              if (!blindpixel.FillCharge(blindpixelsumhi)) 
-                *fLog << warn << 
-                  "Overflow or Underflow occurred filling Blind Pixel sum = " << blindpixelsumhi << endl;
-              
-              //              Byte_t *loptr = pixel.GetLoGainSamples();
-              //              CalcSignalBlindPixel(loptr, blindpixelsumlo);
-              
-              blindpixel.FillGraphs(blindpixelsumhi,blindpixelsumlo);
+          if (!pix.FillAbsTimeLoGain(abstime)) 
+            *fLog << warn << "Could not fill Lo Gain Abs. Time of pixel: " 
+                  << pixid << " time = " << abstime << endl;
+        }
+      else
+        {
+          if (!pix.FillChargeHiGain(sumhi))
+            *fLog << warn << "Could not fill Hi Gain Charge of pixel: " << pixid 
+                  << " signal = " << sumhi << endl;
           
-              TH1I *hist;
-
-              if (blindpixelsumhi > fBlindPixelSinglePheCut)
-                {
-                  hist = (blindpixel.GetHist())->GetHSinglePheFADCSlices();
-                  fNumBlindPixelSinglePhe++;
-                }
-              else
-                {
-                  hist = (blindpixel.GetHist())->GetHPedestalFADCSlices();
-                  fNumBlindPixelPedestal++;
-                }
-              
-              ptr = pixel.GetHiGainSamples();
-              for (Int_t i=1;i<16;i++)
-                hist->Fill(i,*ptr++);
-
-              ptr = pixel.GetLoGainSamples();
-              for (Int_t i=16;i<31;i++)
-                hist->Fill(i,*ptr++);
-
-            } /* if use blind pixel */
-          
-          break;
-        default:
-              
-          if (pix.IsExcluded())
-            continue;
-
-          pix.FillGraphs(sumhi,sumlo);
-
-          if (sig.IsLoGainUsed())
-            {
-              
-              if (!pix.FillChargeLoGain(sumlo))
-                *fLog << warn << "Could not fill Lo Gain Charge of pixel: " << pixid 
-                      << " signal = " << sumlo << endl;
-
-              if (!pix.FillAbsTimeLoGain(abstime)) 
-                *fLog << warn << "Could not fill Lo Gain Abs. Time of pixel: " 
-                      << pixid << " time = " << abstime << endl;
-              /*
-                if (!pix.FillRelTimeLoGain(reltime)) 
-                *fLog << warn << "Could not fill Lo Gain Rel. Time of pixel: " 
-                    << pixid << " time = " << reltime << endl;
-              */
-            } /* if (sig.IsLoGainUsed()) */
-          else
-            {
-              if (!pix.FillChargeHiGain(sumhi))
-                *fLog << warn << "Could not fill Hi Gain Charge of pixel: " << pixid 
-                      << " signal = " << sumhi << endl;
-              
-              if (!pix.FillAbsTimeHiGain(abstime))
-                *fLog << warn << "Could not fill Hi Gain Abs. Time of pixel: " 
-                      << pixid << " time = " << abstime << endl;
-              /*
-              if (!pix.FillRelTimeHiGain(reltime))
-                    *fLog << warn << "Could not fill Hi Gain Rel. Time of pixel: " 
-                          << pixid << " time = " << reltime << endl;
-              */
-            } /* else (sig.IsLoGainUsed()) */
-          break;
-          
-        } /* switch(pixid) */
+          if (!pix.FillAbsTimeHiGain(abstime))
+            *fLog << warn << "Could not fill Hi Gain Abs. Time of pixel: " 
+                  << pixid << " time = " << abstime << endl;
+        }
       
     } /* while (pixel.Next()) */
@@ -584,31 +503,30 @@
 
       //
+      // retrieve the histogram containers
+      //
+      MHCalibrationBlindPixel *hist = blindpixel.GetHist();
+          
+      //
       // retrieve mean and sigma of the blind pixel pedestal, 
       // so that we can use it for the fit
       //
-      Float_t pedestal;
-      Float_t pederr;
-      Float_t pedsigma;
-      Float_t pedsigmaerr;
-      
-      const ULong_t nentries    = fPedestals->GetTotalEntries();
-      const Int_t   nslices     = fBlindPixelLast-fBlindPixelFirst+1;
-      const Float_t sqrslice    = TMath::Sqrt((Float_t)nslices);
-
-      MPedestalPix    &pedpix  = (*fPedestals)[fgBlindPixelId];
-      pedestal    = pedpix.GetPedestal()*nslices;
-      pederr      = pedpix.GetPedestalRms()*nslices/nentries;
-      pedsigma    = pedpix.GetPedestalRms()*sqrslice;
-      pedsigmaerr = pederr/2.;
-      //
-      // retrieve the histogram containers
-      //
-      MHCalibrationBlindPixel *hist = blindpixel.GetHist();
-      
-      hist->SetMeanPedestal(pedestal);
-      hist->SetMeanPedestalErr(pederr);
-      hist->SetSigmaPedestal(pedsigma);
-      hist->SetSigmaPedestalErr(pedsigmaerr);
-  
+      const UInt_t nentries    = fPedestals->GetTotalEntries();
+      const UInt_t nslices     = 12;
+      const Float_t sqrslice   = TMath::Sqrt((Float_t)nslices);
+
+      MPedestalPix &pedpix  = (*fPedestals)[fBlindPixelIdx];
+      if (&pedpix)
+        {
+          const Float_t pedestal    = pedpix.GetPedestal()*nslices;
+          const Float_t pederr      = pedpix.GetPedestalRms()*nslices/nentries;
+          const Float_t pedsigma    = pedpix.GetPedestalRms()*sqrslice;
+          const Float_t pedsigmaerr = pederr/2.;
+          
+          hist->SetMeanPedestal(pedestal);
+          hist->SetMeanPedestalErr(pederr);
+          hist->SetSigmaPedestal(pedsigma);
+          hist->SetSigmaPedestalErr(pedsigmaerr);
+        }
+      
       if (!blindpixel.FitCharge())
         {
@@ -708,34 +626,2 @@
 
 
-Bool_t MCalibrationCalc::CalcSignalBlindPixel(Byte_t *ptr, Float_t &signal) const
-{
-
-  Byte_t *newptr  = ptr;
-  Byte_t *start   = ptr + fBlindPixelFirst-1;
-  Byte_t *end     = ptr + fBlindPixelLast;
-
-  Byte_t sum = 0;
-  Int_t  sat = 0;
-  
-  newptr = start;
-
-  while (ptr<end)
-    {
-      sum += *ptr;
-      if (*ptr++ >= fgSaturationLimit)
-        sat++;
-    }
-
-  if (sat)
-  {
-      *fLog << err << "HI Gain Saturation occurred in the blind pixel! " 
-            << " Do not know yet how to treat this ... aborting " << endl;
-      *fLog << "If you need absolutely any other kind of calibration, "
-            << " use SkipBlindPixelFit() " << endl;
-      return kFALSE;
-  }
-
-  signal = (Float_t)sum;
-
-  return kTRUE;
-}
