Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 3105)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 3106)
@@ -26,4 +26,14 @@
        pixel numbers.
 
+   * mbase/MArray.[h,cc]
+     - introduced function to cut array edges with zeros. 
+       (Thomas, should we keep it there or move it to another class? )
+
+   * mcalib/MCalibrationCalc.[h,cc]
+     - new function SetBlindPixelRange to set start and end of FADC slices 
+       used for the blind pixel signal extraction. 
+     - remove fEvents which are not necessary any more after the shift 
+       to MFCosmics.
+
 
  2004/02/11: Abelardo Moralejo
@@ -59,5 +69,5 @@
       spectrum
 
-  * mfilter/MCosmics.[h,cc]
+  * mfilter/MFCosmics.[h,cc]
   * mfilter/Makefile
   * mfilter/FilterLinkDef.h
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 3105)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 3106)
@@ -116,5 +116,5 @@
 const Byte_t MCalibrationCalc::fgSaturationLimit = 254;
 const Byte_t MCalibrationCalc::fgBlindPixelFirst = 3;
-const Byte_t MCalibrationCalc::fgBlindPixelLast  = 16;
+const Byte_t MCalibrationCalc::fgBlindPixelLast  = 8;
 
 // --------------------------------------------------------------------------
@@ -136,4 +136,5 @@
 
     Clear();
+    SetBlindPixelRange();
 }
 
@@ -150,7 +151,6 @@
     CLRBIT(fFlags, kUsePinDiodeFit);
 
-
-
-    fEvents            = 0;
+    fBlindPixelFirst   = 0;
+    fBlindPixelLast    = 0;
 
     fNumHiGainSamples  = 0;
@@ -160,4 +160,11 @@
 
     fColor = kECT1;
+}
+
+void MCalibrationCalc::SetBlindPixelRange(Byte_t first, Byte_t last)
+{
+  
+  fBlindPixelFirst = first;
+  fBlindPixelLast  = last;
 }
 
@@ -387,6 +394,4 @@
   MRawEvtPixelIter pixel(fRawEvt);
   
-  fEvents++;
-
   //
   // Create a (second) loop to do fill the calibration histograms
@@ -431,5 +436,4 @@
               if (pixid == 1)
                 referencetime = (Float_t)pixel.GetIdxMaxHiGainSample();
-#endif
               if (sig.IsLoGainUsed())
                 {
@@ -445,4 +449,5 @@
               //        }  /* if Use Times */
 
+#endif
       switch(pixid)
         {
@@ -453,4 +458,6 @@
             {
           
+              Byte_t *ptr = pixel.GetHiGainSamples();
+
               Float_t blindpixelsumhi = 0.;
               Float_t blindpixelsumlo = 0.;
@@ -458,33 +465,84 @@
               // We need a dedicated signal extractor for the blind pixel
               //
-              MPedestalPix &ped  = (*fPedestals)[pixid];
-              if (!CalcSignalBlindPixel(pixel.GetHiGainSamples(), blindpixelsumhi, ped.GetPedestal()))
-                return kFALSE;
-
-              CalcSignalBlindPixel(pixel.GetLoGainSamples(), blindpixelsumlo, ped.GetPedestal());
-              
-              blindpixel.FillGraphs(blindpixelsumhi,blindpixelsumlo);
-          
+              MPedestalPix &ped  = (*fPedestals)[fBlindPixelId];
+
+              Int_t diff = 0;
+
+              if (fBlindPixelLast > 15)
+                {
+                  diff = fBlindPixelLast - 15;
+                  fBlindPixelLast = 15;
+                }
+
+              Byte_t *start   = ptr   + fBlindPixelFirst - 1;
+              Byte_t *end     = start + fBlindPixelLast - fBlindPixelFirst + 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 = (Float_t)sum;
+
+              ptr = pixel.GetLoGainSamples();
+
+              start   = ptr + fBlindPixelFirst - 1;
+              end     = ptr + fBlindPixelLast;
+
+              ptr = start;
+
+              sum = 0;
+              
+              while (++ptr<end)
+                sum += *ptr;
+
+              blindpixelsumlo = (Float_t)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 *hiptr = pixel.GetHiGainSamples();
-              Byte_t *loptr = pixel.GetHiGainSamples();
-              
+              //              Byte_t *loptr = pixel.GetLoGainSamples();
+              //              CalcSignalBlindPixel(loptr, blindpixelsumlo);
+              
+              blindpixel.FillGraphs(blindpixelsumhi,blindpixelsumlo);
+          
+#if 0              
               TH1I *hist = blindpixel.GetSinglePheFADCSlices();
 
-              if (blindpixelsumhi > 100.)
+              if (blindpixelsumhi > 300.)
                 {
+                  ptr = pixel.GetHiGainSamples();
                   for (Int_t i=0;i<15;i++)
-                    hist->Fill(i,*hiptr++);
+                    hist->Fill(i,*ptr++);
+                  ptr = pixel.GetLoGainSamples();
                   for (Int_t i=15;i<30;i++)
-                    hist->Fill(i,*loptr++);
+                    hist->Fill(i,*ptr++);
                 }
-              
+#endif              
             } /* if use blind pixel */
           
-          break;
-              
+          //          break;
         case fPINDiodeId:
 
@@ -504,10 +562,9 @@
               if (!pindiode.FillGraphs(sumhi,sumlo))
                 *fLog << warn 
-                      << "Overflow or Underflow occurred filling PINDiode: eventnr = " 
-                      << fEvents << endl;
+                      << "Overflow or Underflow occurred filling PINDiode: eventnr = " << endl;
               
             } /* if use PIN Diode */
           
-          break;
+          //          break;
               
         default:
@@ -563,11 +620,4 @@
   *fLog << inf << endl;
 
-  if (fEvents == 0)
-    {
-      *fLog << err << GetDescriptor() 
-            << ": This run contains no calibration data! " << endl;
-      return kFALSE;
-    }
-  
   *fLog << inf << GetDescriptor() << ": Cut Histogram Edges" << endl;
 
@@ -595,36 +645,44 @@
       if (fPedestals->GetHistSize() > fBlindPixelId)
         {
+
+          Float_t pedestal;
+          Float_t pederr;
+          Float_t pedsigma;
+          Float_t pedsigmaerr;
+
+          const ULong_t nentries    = fPedestals->GetTotalEntries();
+          const Float_t nslices     = (Float_t)(fgBlindPixelLast-fgBlindPixelFirst+1);
+          const Float_t sqrslice    = TMath::Sqrt(nslices);
           //
           // retrieve the pedestal pix of the blind pixel
           //
-          MHPedestalPixel &pedhist = (*fPedestals)(fBlindPixelId);
-          MPedestalPix    &pedpix  = (*fPedestals)[fBlindPixelId];
+          if (fPedestals->GetHistSize() != 0)
+            {
+              MHPedestalPixel &pedhist = (*fPedestals)(fBlindPixelId);
+              pedestal    = pedhist.GetChargeMean()*nslices;
+              pederr      = pedhist.GetChargeMeanErr()*nslices;
+              //
+              // Fitted sigma: 1. one sqrt(Nr. slices) for the division which is not 
+              //                  not appropriate: sigma(real)/slice = GetSigma*sqrt(nslices)
+              //               2. another sqrt(Nr. slices) to calculate back to number 
+              //                  of slices
+              // 
+              pedsigma    = pedhist.GetChargeSigma()*nslices;
+              pedsigmaerr = pedhist.GetChargeSigmaErr()*nslices;
+            }
+          else
+            {
+              MPedestalPix    &pedpix  = (*fPedestals)[fBlindPixelId];
+              pedestal    = pedpix.GetPedestal()*nslices;
+              pederr      = pedpix.GetPedestalRms()*nslices/nentries;
+              pedsigma    = pedpix.GetPedestalRms()*sqrslice;
+              pedsigmaerr = pederr/2.;
+            }
           //
           // retrieve the histogram containers
           //
           MHCalibrationBlindPixel *hist = blindpixel.GetHist();
-          //
-          // Set the corresponding values
-          //
-          const Float_t nslices     = (Float_t)(fgBlindPixelLast-fgBlindPixelFirst+1);
-          const Float_t sqrslice    = TMath::Sqrt(nslices);
-          const ULong_t nentries    = fPedestals->GetTotalEntries();
-          
-          const Float_t peddiff     = (pedhist.GetChargeMean()-pedpix.GetPedestal())*nslices;
-
-          Float_t pederr  = pedhist.GetChargeMeanErr()*pedhist.GetChargeMeanErr();
-          pederr         += pedpix.GetPedestalRms()*pedpix.GetPedestalRms()/nentries;
-          pederr          = TMath::Sqrt(pederr)*sqrslice;
-          
-          //
-          // Fitted sigma: 1. one sqrt(Nr. slices) for the division which is not 
-          //                  not appropriate: sigma(real)/slice = GetSigma*sqrt(nslices)
-          //               2. another sqrt(Nr. slices) to calculate back to number 
-          //                  of slices
-          // 
-          const Float_t pedsigma    = pedhist.GetChargeSigma()*nslices;
-          const Float_t pedsigmaerr = pedhist.GetChargeSigmaErr()*nslices;
-          
-          hist->SetMeanPedestal(peddiff);
+
+          hist->SetMeanPedestal(pedestal);
           hist->SetMeanPedestalErr(pederr);
           hist->SetSigmaPedestal(pedsigma);
@@ -724,14 +782,16 @@
 
 
-Bool_t MCalibrationCalc::CalcSignalBlindPixel(Byte_t *ptr, Float_t &signal, const Float_t ped) const
-{
-
-  Byte_t *start = ptr + fgBlindPixelFirst-1;
-  Byte_t *end   = ptr + fgBlindPixelLast;
+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 nsamples = fBlindPixelLast-fgBlindPixelFirst+1;
 
   Byte_t sum = 0;
   Int_t  sat = 0;
   
-  ptr = start;
+  newptr = start;
 
   while (ptr<end)
@@ -739,5 +799,5 @@
       sum += *ptr;
       if (*ptr++ >= fgSaturationLimit)
-            sat++;
+        sat++;
     }
 
@@ -746,8 +806,10 @@
       *fLog << err << "HI Gain Saturation occurred in the blind pixel! " 
             << " Do not know yet how to treat this ... aborting " << endl;
+      *fLog << err << "If you need absolutely any other kind of calibration, " 
+            << " use SkipBlindPixelFit() " << endl;
       return kFALSE;
     }
 
-  signal = (Float_t)sum - ped*(Float_t)(fgBlindPixelLast-fgBlindPixelFirst+1);
+  signal = (Float_t)sum;
 
   return kTRUE;
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h	(revision 3105)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h	(revision 3106)
@@ -53,9 +53,10 @@
   MTime                    *fEvtTime;      // Time of the event
 
-  Int_t fEvents;                           // Number of events  
-  
   Byte_t fNumHiGainSamples; 
   Byte_t fNumLoGainSamples; 
   Float_t fSqrtHiGainSamples; 
+  
+  Byte_t fBlindPixelFirst;
+  Byte_t fBlindPixelLast;
   
   Float_t fConversionHiLo;
@@ -69,5 +70,5 @@
           kHiLoGainCalibration,
           kHiGainOverFlow, kLoGainOverFlow  };
-
+  
 public:
   
@@ -83,5 +84,5 @@
   Int_t PostProcess();
 
-  Bool_t CalcSignalBlindPixel(Byte_t *ptr, Float_t &signal, const Float_t ped) const;  
+  Bool_t CalcSignalBlindPixel(Byte_t *ptr, Float_t &signal) const;  
   
 public:
@@ -105,4 +106,6 @@
   void SetConversionHiLo(Float_t conv)         { fConversionHiLo = conv; }
 
+  void SetBlindPixelRange(Byte_t first=fgBlindPixelFirst, Byte_t last=fgBlindPixelLast);
+
   // Getters
   MCalibrationBlindPix *GetBlindPixel() const;
