Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 3428)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 3429)
@@ -26,4 +26,6 @@
   * mcalib/MHCalibrationChargeCam.[h,cc]
     - include MBadPixelsCam
+    - include one additional MBadPixelsPix for the average inner and 
+      outer pixel, respectively
 
   * mcalib/MHCalibrationChargePix.[h,cc]
Index: /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsPix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsPix.cc	(revision 3428)
+++ /trunk/MagicSoft/Mars/mbadpixels/MBadPixelsPix.cc	(revision 3429)
@@ -112,7 +112,4 @@
  MCalibrationCalc - valid for the result of a calibration run:
 
-  <-  1  Pedestals not available
-  <-  2  FADC samples not available
-
       3  Hi-Gain saturated, no LoGain available
 
@@ -122,22 +119,4 @@
       6  Cannot be fitted - calibrated using Histogram Mean and RMS
 
-      7  Mean Charge smaller than PedRMS
-      8  Mean Charge smaller than Mean charge in camera minus 5 sigma
-      9  Mean Charge bigger than Mean charge in camera plus 5 sigma
-
-     10  Sigma Charge smaller than PedRMS
-
-     11  Result of F-Factor    method invalid
-     12  Result of Blind Pixel method invalid
-     13  Result of PIN-Diode   method invalid
-     14  Methods inconsistency
-
-     15  Gains oscillate
-
-  <- 16  Arrival Time not calculated
-  <- 17  Arrival Time not valid
-
-     18  Sigma  Arrival Time  bigger than  FADC window
-     19  Mean   Arrival Time  at edge of   FADC window
      */
 
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3428)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3429)
@@ -330,6 +330,7 @@
       return kFALSE;
     }
-  
+
   fCam->SetGeomCam(fGeom);
+  fCam->SetBadPixelsCam(fBadPixels);
 
   fNumHiGainSamples  =  fSignals->GetNumUsedHiGainFADCSlices();
@@ -438,5 +439,5 @@
       pix.CheckTimeValidity(&bad);
       
-      if (!bad.IsCalibrationOK())
+      if (!bad.IsCalibrationResultOK())
         continue;
 
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 3428)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 3429)
@@ -167,4 +167,6 @@
     fAverageInnerPix = new MCalibrationChargePix("AverageInnerPix","Container of the fit results of the camera average inner pixels");
     fAverageOuterPix = new MCalibrationChargePix("AverageOuterPix","Container of the fit results of the camera average outer pixels");
+    fAverageInnerBadPix = new MBadPixelsPix("AverageInnerBadPix","Bad Pixel Container of the camera average inner pixels");
+    fAverageOuterBadPix = new MBadPixelsPix("AverageOuterBadPix","Bad Pixel Container of the camera average outer pixels");
 
     Clear();
@@ -188,4 +190,7 @@
   delete fAverageOuterPix;
   
+  delete fAverageInnerBadPix;
+  delete fAverageOuterBadPix;
+  
   if (fOffsets)
     delete fOffsets;
@@ -252,4 +257,7 @@
   fAverageInnerPix->Clear();
   fAverageOuterPix->Clear();
+
+  fAverageInnerBadPix->Clear();
+  fAverageOuterBadPix->Clear();
 
   fNumExcludedPixels                 = 0;
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h	(revision 3428)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h	(revision 3429)
@@ -16,4 +16,5 @@
 class MCalibrationChargePINDiode;
 class MCalibrationChargePix;
+class MBadPixelsPix;
 class MBadPixelsCam;
 class MCalibrationChargeCam : public MParContainer, public MCamEvent
@@ -25,4 +26,7 @@
   MCalibrationChargePix *fAverageInnerPix;       //-> Average Pixel of all events
   MCalibrationChargePix *fAverageOuterPix;       //-> Average Pixel of all events
+  
+  MBadPixelsPix         *fAverageInnerBadPix;    //-> Average Pixel of all events
+  MBadPixelsPix         *fAverageOuterBadPix;    //-> Average Pixel of all events
   
   const MCalibrationChargeBlindPix *fBlindPixel; //! Pointer to the Blind Pixel with fit results
@@ -85,4 +89,10 @@
   const MCalibrationChargePix *GetAverageOuterPix() const  { return fAverageOuterPix; }
 
+  MBadPixelsPix *GetAverageInnerBadPix()              { return fAverageInnerBadPix; }
+  const MBadPixelsPix *GetAverageInnerBadPix() const  { return fAverageInnerBadPix; }
+
+  MBadPixelsPix *GetAverageOuterBadPix()              { return fAverageOuterBadPix; }
+  const MBadPixelsPix *GetAverageOuterBadPix() const  { return fAverageOuterBadPix; }
+
   // Prints
   void Print(Option_t *o="") const;
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc	(revision 3428)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc	(revision 3429)
@@ -81,4 +81,7 @@
 #include "MGeomPix.h"
 
+#include "MBadPixelsCam.h"
+#include "MBadPixelsPix.h"
+
 #include "MRawEvtData.h"
 #include "MRawEvtPixelIter.h"
@@ -97,4 +100,5 @@
 //
 MHCalibrationChargeCam::MHCalibrationChargeCam(const char *name, const char *title)
+    : fRawEvt(NULL), fGeom(NULL), fBadPixels(NULL)
 {
     fName  = name  ? name  : "MHCalibrationChargeCam";
@@ -128,8 +132,11 @@
     fAverageLoGainOuterPix->GetHAbsTime()->SetTitle("Absolute Arrival Time average Outer pixels LoGain");
 
-    fAverageHiGainInnerPix->SetChargeLast(1000.);
-    fAverageLoGainInnerPix->SetChargeLast(100.);
-    fAverageHiGainOuterPix->SetChargeLast(1000.);
-    fAverageLoGainOuterPix->SetChargeLast(100.);
+    fAverageHiGainInnerPix->SetChargeFirst(-1000.);
+    fAverageHiGainOuterPix->SetChargeFirst(-1000.);
+
+    fAverageHiGainInnerPix->SetChargeLast(4000.);
+    fAverageLoGainInnerPix->SetChargeLast(4000.);
+    fAverageHiGainOuterPix->SetChargeLast(4000.);
+    fAverageLoGainOuterPix->SetChargeLast(4000.);
 
     fAverageHiGainInnerPix->SetChargeNbins(4000);
@@ -143,4 +150,5 @@
     fNumInnerPixels = 0;
     fNumOuterPixels = 0; 
+    fNumExcluded    = 0;
 }
 
@@ -295,4 +303,11 @@
 Bool_t MHCalibrationChargeCam::ReInit(MParList *pList)
 {
+
+  fBadPixels = (MBadPixelsCam*)pList->FindCreateObj("MBadPixelsCam");
+  if (!fBadPixels)
+  {
+      gLog << err << GetDescriptor() << ": ERROR: Could not find MBadPixelsCam ... aborting " << endl;
+      return kFALSE;
+  }
 
   fCam = (MCalibrationChargeCam*)pList->FindCreateObj("MCalibrationChargeCam");
@@ -318,8 +333,16 @@
       for (Int_t i=0; i<n; i++)
       {
+	  //
+	  // Oscillating pixels
+	  //
 	  (*fHiGainArray)[i] = new MHCalibrationChargeHiGainPix;
+
+          if ((*fBadPixels)[i].IsBad())
+	  {
+	      fNumExcluded++;
+	      (*this)[i].SetExcluded();
+	  }
 	  (*this)[i].Init();
 	  (*this)[i].ChangeHistId(i);
-	  
       }
   }
@@ -332,8 +355,19 @@
       {
 	  (*fLoGainArray)[i] = new MHCalibrationChargeLoGainPix;
+	  //
+	  // Pixels with non-valid behavior
+	  //
+          if ((*fBadPixels)[i].IsBad())
+            (*this)(i).SetExcluded();
+
 	  (*this)(i).Init();
 	  (*this)(i).ChangeHistId(i);
       }
   }
+
+
+  
+
+  *fLog << inf << GetDescriptor() << ": " << fNumExcluded << " excluded Pixels " << endl;
 
   return kTRUE;
@@ -382,4 +416,7 @@
   for (int i=0; i<n; i++)
     {
+
+      if ((*this)[i].IsExcluded())
+	continue;
 
       const MExtractedSignalPix &pix = (*signal)[i];
@@ -430,4 +467,6 @@
   //
   sumhiinnertot = sumloinnertot = sumhioutertot = sumlooutertot = 0.;
+  fNumInnerPixels = fNumOuterPixels = 0;
+
   MRawEvtPixelIter pixel(fRawEvt);
   while (pixel.Next())
@@ -435,6 +474,9 @@
       
       const UInt_t pixid = pixel.GetPixelId();
-      
-      const Float_t timehi = (Float_t)pixel.GetIdxMaxHiGainSample();
+
+      if ((*this)[pixid].IsExcluded())
+	continue;
+
+      const Int_t timehi   = (Int_t)pixel.GetIdxMaxHiGainSample();
       const Float_t timelo = (Float_t)pixel.GetIdxMaxLoGainSample() + 15.;
 
@@ -446,4 +488,5 @@
 	  sumhiinnertot += timehi;
 	  sumloinnertot += timelo;
+	  fNumInnerPixels++;
       }
       else
@@ -451,4 +494,5 @@
 	  sumhioutertot += timehi;
 	  sumlooutertot += timelo;
+	  fNumOuterPixels++;
       }
     }
@@ -478,8 +522,10 @@
     {
      
+	MCalibrationChargePix        &pix    = (*fCam)[i];
 	MHCalibrationChargeHiGainPix &histhi = (*this)[i];
-	MCalibrationChargePix        &pix    = (*fCam)[i];
-
-	FinalizeHiGainHists(histhi,pix);
+        MBadPixelsPix                &bad    = (*fBadPixels)[i];
+
+	FinalizeHiGainHists(histhi,pix,bad);
+
     }
 
@@ -487,14 +533,19 @@
     {
      
+        if ((*this)(i).IsExcluded())
+	   continue;
+
 	MHCalibrationChargeLoGainPix &histlo = (*this)(i);
 	MCalibrationChargePix        &pix    = (*fCam)[i];
-
-	FinalizeLoGainHists(histlo,pix);
-    }
-
-    FinalizeHiGainHists(*fAverageHiGainInnerPix,*fCam->GetAverageInnerPix());
-    FinalizeLoGainHists(*fAverageLoGainInnerPix,*fCam->GetAverageInnerPix());
-    FinalizeHiGainHists(*fAverageHiGainOuterPix,*fCam->GetAverageOuterPix());
-    FinalizeLoGainHists(*fAverageLoGainOuterPix,*fCam->GetAverageOuterPix());
+        MBadPixelsPix                &bad    = (*fBadPixels)[i];
+
+	FinalizeLoGainHists(histlo,pix,bad);
+
+    }
+
+    FinalizeHiGainHists(*fAverageHiGainInnerPix,*fCam->GetAverageInnerPix(),*fCam->GetAverageInnerBadPix());
+    FinalizeLoGainHists(*fAverageLoGainInnerPix,*fCam->GetAverageInnerPix(),*fCam->GetAverageInnerBadPix());
+    FinalizeHiGainHists(*fAverageHiGainOuterPix,*fCam->GetAverageOuterPix(),*fCam->GetAverageOuterBadPix());
+    FinalizeLoGainHists(*fAverageLoGainOuterPix,*fCam->GetAverageOuterPix(),*fCam->GetAverageOuterBadPix());
 
     fCam->GetAverageInnerPix()->SetSigmaCharge   (fCam->GetAverageInnerPix()->GetSigmaCharge()   *TMath::Sqrt((Float_t)fNumInnerPixels));
@@ -502,17 +553,23 @@
     fCam->GetAverageInnerPix()->SetSigmaChargeErr(fCam->GetAverageInnerPix()->GetSigmaChargeErr()*TMath::Sqrt((Float_t)fNumInnerPixels));
     fCam->GetAverageOuterPix()->SetSigmaChargeErr(fCam->GetAverageOuterPix()->GetSigmaChargeErr()*TMath::Sqrt((Float_t)fNumOuterPixels));
+    
+    *fLog << err << TMath::Sqrt((Float_t)fNumInnerPixels) << endl;
+    *fLog << err << TMath::Sqrt((Float_t)fNumOuterPixels) << endl;
 
     return kTRUE;
 }
 
-void MHCalibrationChargeCam::FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, MCalibrationChargePix &pix)
+void MHCalibrationChargeCam::FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, 
+                                                 MCalibrationChargePix &pix, 
+                                                 MBadPixelsPix &bad)
 {
 
     if (hist.IsEmpty())
 	return;
-    
+
     if (hist.GetSaturated() > fNumHiGainSaturationLimit*hist.GetHGausHist()->GetEntries())
     {
 	pix.SetHiGainSaturation();
+        bad.SetHiGainSaturation();
 	return;
     }
@@ -522,13 +579,16 @@
     //
     if (hist.FitGaus())
-	pix.SetHiGainFitted();
-    //
-    // 3) In case of failure set the bit kFitted to false and take histogram means and RMS
+	bad.SetHiGainFitted();
+    //
+    // 3) In case of failure set the bit Fitted to false and take histogram means and RMS
     //
     else if (hist.RepeatFit())
-	pix.SetHiGainFitted();
+	bad.SetHiGainFitted();
     else
+      {
 	hist.BypassFit();
-    
+        bad.SetNoHiGainFitted();
+      }
+
     //
     // 4) Check for pickup
@@ -553,11 +613,14 @@
     pix.SetAbsTimeRms          ( hist.GetAbsTimeRms() );
     
-    pix.SetHiGainOscillating   ( !hist.IsFourierSpectrumOK() );
     pix.SetHiGainNumPickup     (  hist.GetPickup()           );
     
-}
-
-
-void MHCalibrationChargeCam::FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, MCalibrationChargePix &pix)
+    if (!hist.IsFourierSpectrumOK())
+      bad.SetHiGainOscillating();
+}
+
+
+void MHCalibrationChargeCam::FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, 
+                                                 MCalibrationChargePix &pix, 
+                                                 MBadPixelsPix &bad)
 {
 
@@ -569,4 +632,5 @@
     {
 	pix.SetLoGainSaturation();
+        bad.SetLoGainSaturation();
 	return;
     }
@@ -575,13 +639,16 @@
     //
     if (hist.FitGaus())
-	pix.SetLoGainFitted();
+	bad.SetLoGainFitted();
     //
     // 3) In case of failure set the bit kFitted to false and take histogram means and RMS
     //
     else if (hist.RepeatFit())
-	pix.SetLoGainFitted();
+	bad.SetLoGainFitted();
     else
+      {
 	hist.BypassFit();
-    
+        bad.SetNoLoGainFitted();
+      }
+
     //
     // 4) Check for pickup
@@ -609,7 +676,8 @@
     }	    
     
-    pix.SetLoGainOscillating   ( !hist.IsFourierSpectrumOK() );
     pix.SetLoGainNumPickup     (  hist.GetPickup()           );
-    
+
+    if (!hist.IsFourierSpectrumOK())
+      bad.SetLoGainOscillating();
 }    
 
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h	(revision 3428)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h	(revision 3429)
@@ -16,4 +16,5 @@
 class MGeomCam;
 class MBadPixelsCam;
+class MBadPixelsPix;
 class MCalibrationChargeCam;
 class MCalibrationChargePix;
@@ -46,6 +47,6 @@
   Int_t fNumExcluded;
 
-  void FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, MCalibrationChargePix &pix);
-  void FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, MCalibrationChargePix &pix);
+  void FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, MCalibrationChargePix &pix, MBadPixelsPix &bad);
+  void FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, MCalibrationChargePix &pix, MBadPixelsPix &bad);
 
 public:
