Index: trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.cc	(revision 2931)
@@ -45,17 +45,4 @@
 //
 MCalibrationBlindPix::MCalibrationBlindPix(const char *name, const char *title)
-    : fHist(NULL),
-      fLambda(-1.),
-      fMu0   (-1.),
-      fMu1   (-1.),
-      fSigma0(-1.),
-      fSigma1(-1.),
-      fErrLambda(-1.),
-      fErrMu0   (-1.),
-      fErrMu1   (-1.),
-      fErrSigma0(-1.),
-      fErrSigma1(-1.),
-      fTime     (-1.),
-      fErrTime  (-1.)
 {
 
@@ -68,4 +55,5 @@
     *fLog << warn << dbginf << " Could not create MHCalibrationBlindPixel " << endl;
   
+  Clear();
 }
 
@@ -82,4 +70,18 @@
 {
   fHist->Reset();
+
+  fLambda = -1.;
+  fMu0    = -1.;
+  fMu1    = -1.;
+  fSigma0 = -1.;
+  fSigma1 = -1.;
+  fErrLambda = -1.;
+  fErrMu0    = -1.;
+  fErrMu1    = -1.;
+  fErrSigma0 = -1.;
+  fErrSigma1 = -1.;
+  fTime      = -1.;
+  fErrTime   = -1;
+
 }
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationBlindPix.h	(revision 2931)
@@ -52,20 +52,21 @@
   
   MHCalibrationBlindPixel *GetHist()     const  { return fHist;  }
+  MHCalibrationBlindPixel *GetHist()            { return fHist;  }
   
-  Bool_t IsFitOK()                              { return fHist->IsFitOK(); }
+  Bool_t IsFitOK()                        const  { return fHist->IsFitOK(); }
   
   // Fill histos
-  Bool_t FillCharge(Float_t q)                  { return fHist->FillBlindPixelCharge(q); }
-  Bool_t FillTime(Float_t t)                    { return fHist->FillBlindPixelTime(t); }  
-  Bool_t FillRChargevsTime(Float_t rq, Int_t t) { return fHist->FillBlindPixelChargevsN(rq,t); }    
+  Bool_t FillCharge(Float_t q)                  const { return fHist->FillBlindPixelCharge(q); }
+  Bool_t FillTime(Float_t t)                    const { return fHist->FillBlindPixelTime(t); }  
+  Bool_t FillRChargevsTime(Float_t rq, Int_t t) const { return fHist->FillBlindPixelChargevsN(rq,t); }    
   
   // Fits
   Bool_t FitCharge();
   Bool_t FitTime();
-  void ChangeFitFunc(MHCalibrationBlindPixel::FitFunc_t f)   { fHist->ChangeFitFunc(f); }
+  void ChangeFitFunc(MHCalibrationBlindPixel::FitFunc_t f) const  { fHist->ChangeFitFunc(f); }
   
   // Draws
-  void Draw(Option_t *opt="")                     { fHist->Draw(opt); }
-  TObject *DrawClone(Option_t *opt="") const    { return fHist->DrawClone(opt); }  
+  void Draw(Option_t *opt="")                  { fHist->Draw(opt); }
+  TObject *DrawClone(Option_t *opt="") const  {  return fHist->DrawClone(opt); }  
   
   ClassDef(MCalibrationBlindPix, 1)	// Storage Container for Calibration information of one pixel
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2931)
@@ -262,5 +262,4 @@
       return kFALSE;
     }
-
 
     fNumHiGainSamples =  fSignals->GetNumUsedHiGainFADCSlices();
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCam.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCam.cc	(revision 2931)
@@ -70,15 +70,7 @@
 //
 MCalibrationCam::MCalibrationCam(const char *name, const char *title)
-    : fNumPhotInsidePlexiglassAvailable(kFALSE),
-      fMeanPhotInsidePlexiglass(-1.),
-      fMeanPhotErrInsidePlexiglass(-1.),
-      fNumPhotOutsidePlexiglassAvailable(kFALSE),
-      fMeanPhotOutsidePlexiglass(-1.),
-      fMeanPhotErrOutsidePlexiglass(-1.),
-      fOffsets(NULL),
+    : fOffsets(NULL),
       fSlopes(NULL),
-      fOffvsSlope(NULL),
-      fNumExcludedPixels(0)
-
+      fOffvsSlope(NULL)
 {
     fName  = name  ? name  : "MCalibrationCam";
@@ -89,4 +81,5 @@
     fPINDiode   = new MCalibrationPINDiode();
 
+    Clear();
 }
 
@@ -125,5 +118,5 @@
 // fSize is the old size (in most cases: 1)
 //
-void MCalibrationCam::InitSize(const UInt_t size)
+void MCalibrationCam::InitSize(const UInt_t i)
 {
   
@@ -131,8 +124,8 @@
   // check if we have already initialized to size
   //
-  if (CheckBounds(size))
+  if (CheckBounds(i))
     return;
   
-  fPixels->ExpandCreate(size);
+  fPixels->ExpandCreate(i);
 
 }
@@ -156,5 +149,5 @@
 Bool_t MCalibrationCam::CheckBounds(Int_t i) const 
 {
-    return i < fPixels->GetEntriesFast();
+  return i < GetSize();
 } 
 
@@ -187,59 +180,25 @@
 
 
-// --------------------------------------------------------------------------
-//
-// Return true if pixel is inside bounds of the TClonesArray fPixels
-//
-Bool_t MCalibrationCam::IsPixelUsed(Int_t idx) const 
-{
-  if (!CheckBounds(idx))
-    return kFALSE;
-
-  return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-// Return true if pixel has already been fitted once (independent of the result)
-//
-Bool_t MCalibrationCam::IsPixelFitted(Int_t idx) const 
-{
-
-  if (!CheckBounds(idx))
-    return kFALSE;
-
-  return (*this)[idx].IsFitted();
-}
-
-
 // --------------------------------------
 //
 void MCalibrationCam::Clear(Option_t *o)
 {
-    fPixels->ForEach(TObject, Clear)();
-}
-
-// --------------------------------------------------------------------------
-//
-// Sets the user ranges of all histograms such that 
-// empty bins at the edges are not used. Additionally, it rebins the 
-// histograms such that in total, 50 bins are used.
-//
-void MCalibrationCam::CutEdges()
-{
-
-  fBlindPixel->GetHist()->CutAllEdges();
-  fPINDiode->GetHist()->CutAllEdges();
-
-  TIter Next(fPixels);
-  MCalibrationPix *pix;
-  while ((pix=(MCalibrationPix*)Next()))
-    {
-      pix->GetHist()->CutAllEdges();
-    }
+
+  fPixels->ForEach(TObject, Clear)();
+  fBlindPixel->Clear();
+  fPINDiode->Clear();
+
+  fNumPhotInsidePlexiglassAvailable  = kFALSE;
+  fMeanPhotInsidePlexiglass          = -1.;
+  fMeanPhotErrInsidePlexiglass       = -1.;
+  fNumPhotOutsidePlexiglassAvailable = kFALSE;
+  fMeanPhotOutsidePlexiglass         = -1.;
+  fMeanPhotErrOutsidePlexiglass      = -1.;
+
+  fNumExcludedPixels                 = 0;
 
   return;
 }
-  
+
 // --------------------------------------------------------------------------
 //
@@ -312,6 +271,53 @@
 
   *fLog << all << fNumExcludedPixels << " excluded pixels " << endl;
- 
-}
+}
+
+// --------------------------------------------------------------------------
+//
+// Return true if pixel is inside bounds of the TClonesArray fPixels
+//
+Bool_t MCalibrationCam::IsPixelUsed(Int_t idx) const 
+{
+  if (!CheckBounds(idx))
+    return kFALSE;
+
+  return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Return true if pixel has already been fitted once (independent of the result)
+//
+Bool_t MCalibrationCam::IsPixelFitted(Int_t idx) const 
+{
+
+  if (!CheckBounds(idx))
+    return kFALSE;
+
+  return (*this)[idx].IsFitted();
+}
+
+// --------------------------------------------------------------------------
+//
+// Sets the user ranges of all histograms such that 
+// empty bins at the edges are not used. Additionally, it rebins the 
+// histograms such that in total, 50 bins are used.
+//
+void MCalibrationCam::CutEdges()
+{
+
+  fBlindPixel->GetHist()->CutAllEdges();
+  fPINDiode->GetHist()->CutAllEdges();
+
+  TIter Next(fPixels);
+  MCalibrationPix *pix;
+  while ((pix=(MCalibrationPix*)Next()))
+    {
+      pix->GetHist()->CutAllEdges();
+    }
+
+  return;
+}
+  
 
 // The types are as follows:
@@ -500,9 +506,9 @@
 {
 
-  if (!fPINDiode->IsFitOK())
-    return kFALSE;
-  
-  const Float_t mean = fPINDiode->GetMean();
-  const Float_t merr = fPINDiode->GetMeanError();
+  if (!fPINDiode->IsFitValid())
+    return kFALSE;
+  
+  const Float_t mean = fPINDiode->GetCharge();
+  const Float_t merr = fPINDiode->GetErrCharge();
   
   switch (fColor)
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCam.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCam.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCam.h	(revision 2931)
@@ -37,5 +37,5 @@
   TH2D* fOffvsSlope;                          //! 
 
-  UInt_t  fNumExcludedPixels;
+  UInt_t fNumExcludedPixels;
 
 public:
@@ -60,9 +60,11 @@
     
   // Getters
-  Int_t GetSize()                    const;
-  UInt_t GetNumPixels()              const { return fNumPixels; }
+  Int_t GetSize()                        const;
+  UInt_t GetNumPixels()                  const { return fNumPixels; }
 
-  MCalibrationBlindPix *GetBlindPixel()              const { return fBlindPixel;  }
-  MCalibrationPINDiode *GetPINDiode()                const { return fPINDiode;    }
+  MCalibrationBlindPix *GetBlindPixel()  const { return fBlindPixel;  }
+  MCalibrationPINDiode *GetPINDiode()    const { return fPINDiode;    }
+  MCalibrationBlindPix *GetBlindPixel()       { return fBlindPixel;  }
+  MCalibrationPINDiode *GetPINDiode()         { return fPINDiode;    }
 
   Bool_t GetConversionFactorFFactor(Int_t ipx, Float_t &mean, Float_t &err, Float_t &sigma);
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.cc	(revision 2931)
@@ -44,13 +44,8 @@
 //
 MCalibrationPINDiode::MCalibrationPINDiode(const char *name, const char *title)
-  : fHist(NULL),
-    fCharge(-1.),
-    fErrCharge(-1.),
-    fPed(-1.),
-    fPedRms(-1.),
-    fSigmaCharge(-1.),
-    fErrSigmaCharge(-1.),
-    fTime(-1.),
-    fErrTime(-1.)
+    : fChargeLimit(3.),
+      fChargeErrLimit(0.),
+      fChargeRelErrLimit(1.),
+      fFlags(0)
 {
 
@@ -62,5 +57,7 @@
   if (!fHist)
     *fLog << warn << dbginf << " Could not create MHCalibrationPINDiode " << endl;
-  
+ 
+  Clear();
+
 }
 
@@ -77,15 +74,131 @@
 {
   fHist->Reset();
+
+  CLRBIT(fFlags, kExcluded);
+  CLRBIT(fFlags, kFitValid);
+  CLRBIT(fFlags, kFitted);
+
+  fCharge                           =  -1.;
+  fErrCharge                        =  -1.;
+  fSigmaCharge                      =  -1.;
+  fErrSigmaCharge                   =  -1.;
+  fRSigmaSquare                     =  -1.;
+  fChargeProb                       =  -1.;
+  fPed                              =  -1.;
+  fPedRms                           =  -1.;
+  fTime                             =  -1.;
+  fSigmaTime                        =  -1.;
+  fTimeChiSquare                    =  -1.;
+
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Set the pedestals from outside
+//
+void MCalibrationPINDiode::SetPedestal(Float_t ped, Float_t pedrms)
+{
+
+  fPed    = ped;    
+  fPedRms = pedrms;
+  
+}
+
+// --------------------------------------------------------------------------
+//
+// Set the Excluded Bit from outside 
+//
+void MCalibrationPINDiode::SetExcluded(Bool_t b )
+{ 
+  b ?  SETBIT(fFlags, kExcluded) : CLRBIT(fFlags, kExcluded); 
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Set the Excluded Bit from outside 
+//
+void MCalibrationPINDiode::SetExcludeQualityCheck(Bool_t b )
+{ 
+  b ?  SETBIT(fFlags, kExcludeQualityCheck) : CLRBIT(fFlags, kExcludeQualityCheck); 
+}
+
+// --------------------------------------------------------------------------
+//
+// Set the Excluded Bit from outside 
+//
+void MCalibrationPINDiode::SetFitValid(Bool_t b )    
+{ 
+  b ?  SETBIT(fFlags, kFitValid) : CLRBIT(fFlags, kFitValid); 
+}
+
+// --------------------------------------------------------------------------
+//
+// Set the Excluded Bit from outside 
+//
+void MCalibrationPINDiode::SetFitted(Bool_t b )
+{ 
+  b ?  SETBIT(fFlags, kFitted) : CLRBIT(fFlags, kFitted); 
+}
+
+Bool_t MCalibrationPINDiode::IsExcluded() const
+ { 
+   return TESTBIT(fFlags,kExcluded);  
+ }
+
+Bool_t MCalibrationPINDiode::IsFitValid() const 
+{
+  return TESTBIT(fFlags, kFitValid);  
+}
+
+Bool_t MCalibrationPINDiode::IsFitted() const 
+{ 
+  return TESTBIT(fFlags, kFitted);    
 }
 
 Bool_t MCalibrationPINDiode::FitCharge() 
 {
-  if(!fHist->FitCharge())
-    return kFALSE;
-
+
+  //
+  // 1) Return if the charge distribution is already succesfully fitted  
+  //    or if the histogram is empty
+  //
+  if (fHist->IsFitOK() || fHist->IsEmpty())
+    return kTRUE;
+
+  //
+  // 4) Fit the Lo Gain histograms with a Gaussian
+  //
+  if(fHist->FitCharge())
+    {
+      SETBIT(fFlags,kFitted);
+    }
+  else
+    {
+      *fLog << warn << "WARNING: Could not fit charges of PINDiode " << endl;
+      //          
+      // 5) In case of failure print out the fit results
+      //
+      //	  fHist->PrintChargeFitResult();
+      CLRBIT(fFlags,kFitted);
+    }
+
+  //
+  // 6) Retrieve the results and store them in this class
+  //
   fCharge         = fHist->GetChargeMean();
   fErrCharge      = fHist->GetChargeMeanErr(); 
   fSigmaCharge    = fHist->GetChargeSigma();
   fErrSigmaCharge = fHist->GetChargeSigmaErr();
+  fChargeProb     = fHist->GetChargeProb();
+
+  if (CheckChargeFitValidity())
+    SETBIT(fFlags,kFitValid);
+  else
+    {
+      CLRBIT(fFlags,kFitValid);
+      return kFALSE;
+    }
 
   return kTRUE;
@@ -93,14 +206,97 @@
 }
 
+//
+// The check return kTRUE if:
+//
+// 1) PINDiode has a fitted charge greater than 5*PedRMS
+// 2) PINDiode has a fit error greater than 0.
+// 3) PINDiode has a fitted charge greater its charge error
+// 4) PINDiode has a fit Probability greater than 0.0001 
+// 5) PINDiode has a charge sigma bigger than its Pedestal RMS
+// 
+Bool_t MCalibrationPINDiode::CheckChargeFitValidity()
+{
+
+  if (TESTBIT(fFlags,kExcludeQualityCheck))
+    return kTRUE;
+
+  if (fCharge < fChargeLimit*GetPedRms())
+    {
+      *fLog << warn << "WARNING: Fitted Charge is smaller than "
+            << fChargeLimit << " Pedestal RMS in PINDiode " << endl;
+      return kFALSE;
+    }
+  
+  if (fErrCharge < fChargeErrLimit) 
+    {
+      *fLog << warn << "WARNING: Error of Fitted Charge is smaller than "
+            << fChargeErrLimit << " in PINDiode " << endl;
+      return kFALSE;
+    }
+      
+  if (fCharge < fChargeRelErrLimit*fErrCharge) 
+    {
+      *fLog << warn << "WARNING: Fitted Charge is smaller than "
+            << fChargeRelErrLimit << "* its error in PINDiode " << endl;
+      return kFALSE;
+    }
+      
+  if (!fHist->IsFitOK()) 
+    {
+      *fLog << warn << "WARNING: Probability of Fitted Charge too low in PINDiode " << endl;
+      return kFALSE;
+    }
+
+  if (fSigmaCharge < GetPedRms())
+    {
+      *fLog << warn << "WARNING: Sigma of Fitted Charge smaller than Pedestal RMS in PINDiode " << endl;
+      return kFALSE;
+    }
+  return kTRUE;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// 1) Fit the arrival times
+// 2) Retrieve the results
+// 3) Note that because of the low number of bins, the NDf is sometimes 0, so 
+//    Root does not give a reasonable Probability, the Chisquare is more significant
+//
 Bool_t MCalibrationPINDiode::FitTime() 
 {
 
   if(!fHist->FitTimeHiGain())
-    return kFALSE;
-
-  fTime    = fHist->GetTime();
-  fErrTime = fHist->GetErrTime();
-  
+    {
+      *fLog << warn << "WARNING: Could not fit Hi Gain times of PIN Diode" << endl;
+      fHist->PrintTimeFitResult();
+      return kFALSE;
+    }
+  
+  fTime          = fHist->GetTimeMean();
+  fSigmaTime     = fHist->GetTimeSigma();
+  fTimeChiSquare = fHist->GetTimeChiSquare();
+  fTimeProb      = fHist->GetTimeProb();
+  
+  if (CheckTimeFitValidity())
+    SETBIT(fFlags,kFitValid);
+  else
+    CLRBIT(fFlags,kFitValid);
+
   return kTRUE;
-
-}
+}
+
+//
+// The check returns kTRUE if:
+//
+// The mean arrival time is at least 1.0 slices from the used edge slices 
+//
+Bool_t MCalibrationPINDiode::CheckTimeFitValidity()
+{
+
+  if (TESTBIT(fFlags,kExcludeQualityCheck))
+    return kTRUE;
+
+  return kTRUE;
+}
+
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationPINDiode.h	(revision 2931)
@@ -11,13 +11,32 @@
 
   MHCalibrationPINDiode *fHist; // Pointer to the histograms performing the fits, etc.  
+
+  const Float_t fChargeLimit;       // The limit (in units of PedRMS) for acceptance of the fitted mean charge
+  const Float_t fChargeErrLimit;    // The limit (in units of PedRMS) for acceptance of the fitted charge sigma
+  const Float_t fChargeRelErrLimit; // The limit (in units of Error of fitted charge) for acceptance of the fitted mean  
   
+  Float_t fPed;                   // The mean pedestal (from MPedestalPix)
+  Float_t fPedRms;                // The pedestal  RMS (from MPedestalPix)
+
   Float_t fCharge;                // The mean charge after the fit
   Float_t fErrCharge;             // The error of mean charge after the fit
-  Float_t fPed;              // The mean pedestal (from MPedestalPix)
-  Float_t fPedRms;           // The pedestal  RMS (from MPedestalPix)
   Float_t fSigmaCharge;           // The sigma of the mean charge after the fit
   Float_t fErrSigmaCharge;        // The error of the sigma of the mean charge after the fit
-  Float_t fTime;                // The mean arrival time after the fit  
-  Float_t fErrTime;             // The error of the mean arrival time after the fit
+  Float_t fRSigmaSquare;          // The reduced squares of sigmas after the fit
+  Float_t fChargeProb;            // The probability of the fit function 
+
+  Float_t fTime;                  // The mean arrival time after the fit  
+  Float_t fSigmaTime;             // The error of the mean arrival time after the fit
+  Float_t fTimeChiSquare;         // The Chi Square of the fit function 
+  Float_t fTimeProb;              // The probability of the fit function 
+  
+  Byte_t  fFlags;                 // Flag for the set Bits
+
+  enum  { kExcluded, kExcludeQualityCheck,
+          kFitValid, kFitted  };
+  
+  Bool_t CheckChargeFitValidity();
+  Bool_t CheckTimeFitValidity();
+  Bool_t CheckOscillations();  
   
 public:
@@ -28,22 +47,50 @@
   void Clear(Option_t *o="");
   
-  void SetPed(Float_t ped)          { fPed      = ped;      } 
-  void SetPedRms(Float_t pedrms)    { fPedRms   = pedrms; }
+  // Getter
+  MHCalibrationPINDiode *GetHist() const   { return fHist;     }
 
-  Float_t GetMean()           const { return fCharge; }
-  Float_t GetMeanError()      const { return fErrCharge; }
+  // Charges
+  Float_t GetCharge()              const { return fCharge;         }
+  Float_t GetErrCharge()           const { return fErrCharge;      }
+  Float_t GetChargeProb()          const { return fChargeProb;     }    
+  Float_t GetSigmaCharge()         const { return fSigmaCharge;    }
+  Float_t GetErrSigmaCharge()      const { return fErrSigmaCharge; }
+  Float_t GetRSigmaSquare()        const { return fRSigmaSquare;   }
 
-  Bool_t IsFitOK() const            { return fCharge > 0 && fErrCharge > 0; }
+  // Times  
+  Float_t GetTime()                const { return fTime;           }
+  Float_t GetSigmaTime()           const { return fSigmaTime;      }
+  Float_t GetTimeChiSquare()       const { return fTimeChiSquare;  }
+  Float_t GetTimeProb()            const { return fTimeProb;  }      
 
+  // Pedestals
+  Float_t GetPed()                 const { return fPed;    }
+  Float_t GetPedRms()              const { return fPedRms; }
+
+  Bool_t IsExcluded()              const;
+  Bool_t IsFitValid()              const;
+  Bool_t IsFitted()                const;
+
+  // Setters
+  void SetPedestal(Float_t ped, Float_t pedrms);
+  void SetExcluded(Bool_t b = kTRUE);
+  void SetExcludeQualityCheck(Bool_t b = kTRUE);
+  void SetFitValid(Bool_t b = kTRUE);
+  void SetFitted(Bool_t b = kTRUE);
+
+  // Fill histos
   Bool_t FillCharge(Float_t q)      { return fHist->FillChargeHiGain(q); }
   Bool_t FillTime(Float_t t)        { return fHist->FillTimeHiGain(t); }  
   Bool_t FillRChargevsTime(Float_t rq, Int_t t) { return fHist->FillChargevsNHiGain(rq,t); }    
-  
+
+  // Fits
   Bool_t FitCharge();
   Bool_t FitTime();
+
+  // Draws
+  void Draw(Option_t *opt="")                    { fHist->Draw(opt); }
+  TObject *DrawClone(Option_t *opt="") const    { return fHist->DrawClone(opt); }  
   
-  MHCalibrationPINDiode *GetHist()  const  { return fHist;  }
-  
-  ClassDef(MCalibrationPINDiode, 1)	// Storage Container for Calibration information of one pixel
+  ClassDef(MCalibrationPINDiode, 1)	// Storage Container for Calibration information of the PIN Diode
 };
 
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.cc	(revision 2931)
@@ -58,21 +58,14 @@
 //
 MHCalibrationBlindPixel::MHCalibrationBlindPixel(const char *name, const char *title)
-    :     fHBlindPixelCharge(NULL), 
-          fHBlindPixelTime(NULL), 
-          fHBlindPixelChargevsN(NULL),
-          fHBlindPixelPSD(NULL), 
-          fSinglePheFit(NULL),  
-          fTimeGausFit(NULL),
-          fSinglePhePedFit(NULL),
-          fBlindPixelChargefirst(0.),
-          fBlindPixelChargelast(0.), 
-          fBlindPixelChargenbins(0),
-          fFitLegend(NULL), fFitOK(kFALSE), 
-          fLambda(0.), fMu0(0.), fMu1(0.), fSigma0(0.), fSigma1(0.),
-          fLambdaErr(0.), fMu0Err(0.), fMu1Err(0.), fSigma0Err(0.), fSigma1Err(0.),    
-          fChisquare(0.), fProb(0.),  fNdf(0),
-          fMeanTime(0.),  fMeanTimeErr(0.), fSigmaTime(0.), fSigmaTimeErr(0.),
-          fLambdaCheck(0.), fLambdaCheckErr(0.),
-          fFitFunc(kEPoisson4)
+    :   fBlindPixelChargeNbins(500),
+        fBlindPixelTimeNbins(32),
+        fBlindPixelChargevsNbins(1000),
+        fBlindPixelTimeFirst(-0.25),
+        fBlindPixelTimeLast(15.75),
+        fHBlindPixelPSD(NULL), 
+        fSinglePheFit(NULL),  
+        fTimeGausFit(NULL),
+        fSinglePhePedFit(NULL),
+        fFitLegend(NULL)
 {
 
@@ -81,10 +74,9 @@
 
     // Create a large number of bins, later we will rebin
-    fBlindPixelChargefirst = -1000.;
-    fBlindPixelChargelast  = gkStartBlindPixelBinNr;
-    fBlindPixelChargenbins = gkStartBlindPixelBinNr+(int)fBlindPixelChargefirst;
+    fBlindPixelChargefirst = -100.;
+    fBlindPixelChargelast  = 400.;
 
     fHBlindPixelCharge = new TH1F("HBlindPixelCharge","Distribution of Summed FADC Slices",
-				  fBlindPixelChargenbins,fBlindPixelChargefirst,fBlindPixelChargelast);
+				  fBlindPixelChargeNbins,fBlindPixelChargefirst,fBlindPixelChargelast);
     fHBlindPixelCharge->SetXTitle("Sum FADC Slices");
     fHBlindPixelCharge->SetYTitle("Nr. of events");
@@ -92,9 +84,6 @@
     fHBlindPixelCharge->SetDirectory(NULL);
 
-    Axis_t tfirst = -0.5;
-    Axis_t tlast  = 15.5;
-    Int_t nbins   = 16;
-
-    fHBlindPixelTime = new TH1F("HBlindPixelTime","Distribution of Mean Arrival Times",nbins,tfirst,tlast);
+    fHBlindPixelTime = new TH1F("HBlindPixelTime","Distribution of Mean Arrival Times",
+                                fBlindPixelTimeNbins,fBlindPixelTimeFirst,fBlindPixelTimeLast);
     fHBlindPixelTime->SetXTitle("Mean Arrival Times [FADC slice nr]");
     fHBlindPixelTime->SetYTitle("Nr. of events");
@@ -102,14 +91,11 @@
     fHBlindPixelTime->SetDirectory(NULL);
 
-    // We define a reasonable number and later enlarge it if necessary
-    nbins = 20000;
-    Axis_t nfirst = -0.5;
-    Axis_t nlast  = (Axis_t)nbins - 0.5;
-
-    fHBlindPixelChargevsN = new TH1I("HBlindPixelChargevsN","Sum of Charges vs. Event Number",nbins,nfirst,nlast);
+    fHBlindPixelChargevsN = new TH1I("HBlindPixelChargevsN","Sum of Charges vs. Event Number",
+                                     fBlindPixelChargevsNbins,-0.5,(Axis_t)fBlindPixelChargevsNbins-0.5);
     fHBlindPixelChargevsN->SetXTitle("Event Nr.");
     fHBlindPixelChargevsN->SetYTitle("Sum of FADC slices");
     fHBlindPixelChargevsN->SetDirectory(NULL);
 
+    Clear();
 }
 
@@ -126,5 +112,4 @@
   if (fHBlindPixelPSD)
     delete fHBlindPixelPSD;
-
   if (fSinglePheFit)  
     delete fSinglePheFit;
@@ -136,4 +121,61 @@
 }
 
+void MHCalibrationBlindPixel::Clear(Option_t *o)
+{
+  
+  fBlindPixelChargefirst = -100.;
+  fBlindPixelChargelast  = 400.;
+
+  fLambda    = 0.;
+  fMu0       = 0.;
+  fMu1       = 0.;
+  fSigma0    = 0.;
+  fSigma1    = 0.;
+  
+  fLambdaErr = 0.;
+  fMu0Err    = 0.;
+  fMu1Err    = 0.;
+  fSigma0Err = 0.;
+  fSigma1Err = 0.;
+
+  fChisquare = -1.;
+  fProb      = -1.;
+  fNdf       = -1;
+
+  fMeanTime     = -1.;
+  fMeanTimeErr  = -1.;
+  fSigmaTime    = -1.;
+  fSigmaTimeErr = -1.;
+  
+  fLambdaCheck    = -1.;
+  fLambdaCheckErr = -1.;
+  
+  fFitFunc = kEPoisson4;
+
+  if (fFitLegend)
+    delete fFitLegend;
+  if (fHBlindPixelPSD)
+    delete fHBlindPixelPSD;
+  if (fSinglePheFit)  
+    delete fSinglePheFit;
+  if (fTimeGausFit)
+    delete fTimeGausFit;
+  if(fSinglePhePedFit)
+    delete fSinglePhePedFit;
+
+  return;
+}
+
+void MHCalibrationBlindPixel::Reset()
+{
+  
+  Clear();
+
+  fHBlindPixelCharge->Reset();
+  fHBlindPixelTime->Reset();
+  fHBlindPixelChargevsN->Reset();
+  
+}
+
 Bool_t MHCalibrationBlindPixel::FillBlindPixelCharge(Float_t q)
 {
@@ -149,11 +191,4 @@
 {
     return fHBlindPixelChargevsN->Fill(t,rq) > -1;
-}
-
-
-void MHCalibrationBlindPixel::ResetBin(Int_t i)
-{
-    fHBlindPixelCharge->SetBinContent (i, 1.e-20);
-    fHBlindPixelTime->SetBinContent(i, 1.e-20);
 }
 
@@ -578,5 +613,4 @@
   fBlindPixelChargefirst = fHBlindPixelCharge->GetBinLowEdge(fHBlindPixelCharge->GetXaxis()->GetFirst());
   fBlindPixelChargelast  = fHBlindPixelCharge->GetBinLowEdge(fHBlindPixelCharge->GetXaxis()->GetLast())+fHBlindPixelCharge->GetBinWidth(0);
-  fBlindPixelChargenbins = nbins;
 
   CutEdges(fHBlindPixelChargevsN,0);
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationBlindPixel.h	(revision 2931)
@@ -17,4 +17,10 @@
 private:
 
+  const Int_t fBlindPixelChargeNbins;
+  const Int_t fBlindPixelTimeNbins;
+  const Int_t fBlindPixelChargevsNbins;
+  const Axis_t fBlindPixelTimeFirst;
+  const Axis_t fBlindPixelTimeLast;
+  
   TH1F* fHBlindPixelCharge;        // Histogram with the single Phe spectrum
   TH1F* fHBlindPixelTime;          // Variance of summed FADC slices
@@ -28,7 +34,5 @@
   Axis_t  fBlindPixelChargefirst;
   Axis_t  fBlindPixelChargelast; 
-  Int_t   fBlindPixelChargenbins;
-  
-  void ResetBin(Int_t i);
+  
   void DrawLegend();
 
@@ -65,4 +69,7 @@
   ~MHCalibrationBlindPixel();
 
+  void Clear(Option_t *o="");  
+  void Reset();
+  
   Bool_t FillBlindPixelCharge(Float_t q);
   Bool_t FillBlindPixelTime(Float_t t);
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.cc	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.cc	(revision 2931)
@@ -36,4 +36,5 @@
 #include <TH1.h>
 #include <TF1.h>
+#include <TPaveText.h>
 
 ClassImp(MHCalibrationPINDiode);
@@ -46,5 +47,9 @@
 //
 MHCalibrationPINDiode::MHCalibrationPINDiode(const char *name, const char *title)
-    : fVarGausFit(NULL)
+    : fChargeNbins(1000),
+      fTimeNbins(64),
+      fChargevsNbins(10000),
+      fTimeFirst(-0.25),
+      fTimeLast(31.75)
 {
 
@@ -53,35 +58,28 @@
 
     // Create a large number of bins, later we will rebin
-    fChargeFirstHiGain = -1000.;
-    fChargeLastHiGain  = gkStartPINDiodeBinNr;
-    fChargeNbinsHiGain = gkStartPINDiodeBinNr;
+    fChargeFirstHiGain = -100.5;
+    fChargeLastHiGain  = 1999.5;
 
-    fHPCharge = new TH1I("HPCharge","Distribution of Summed FADC Slices",
-			 fChargeNbinsHiGain,fChargeFirstHiGain,fChargeLastHiGain);
-    fHPCharge->SetXTitle("Sum FADC Slices");
-    fHPCharge->SetYTitle("Nr. of events");
-    fHPCharge->Sumw2();
-    fHPCharge->SetDirectory(0);
+    fHPINDiodeCharge   = new TH1F("HPINDiodeCharge","Distribution of Summed FADC Slices PINDiode",
+			          fChargeNbins,fChargeFirstHiGain,fChargeLastHiGain);
+    fHPINDiodeCharge->SetXTitle("Sum FADC Slices");
+    fHPINDiodeCharge->SetYTitle("Nr. of events");
+    fHPINDiodeCharge->Sumw2();
+    fHPINDiodeCharge->SetDirectory(NULL);
 
-    fErrChargeFirst = 0.;
-    fErrChargeLast  = gkStartPINDiodeBinNr;
-    fErrChargeNbins = gkStartPINDiodeBinNr;
+    fHPINDiodeTime = new TH1F("HPINDiodeTime","Distribution of Mean Arrival Times PINDiode",
+                              fTimeNbins,fTimeFirst,fTimeLast);
+    fHPINDiodeTime->SetXTitle("Mean Arrival Times [FADC slice nr]");
+    fHPINDiodeTime->SetYTitle("Nr. of events");
+    fHPINDiodeTime->Sumw2();
+    fHPINDiodeTime->SetDirectory(NULL);
 
-    fHErrCharge = new TH1F("HErrCharge","Distribution of Variances of Summed FADC Slices",fErrChargeNbins,fErrChargeFirst,fErrChargeLast);
-    fHErrCharge->SetXTitle("Variance Summed FADC Slices");
-    fHErrCharge->SetYTitle("Nr. of events");
-    fHErrCharge->Sumw2();
-    fHErrCharge->SetDirectory(0);
+    fHPINDiodeChargevsN = new TH1I("HPINDiodeChargevsN","Sum of Hi Gain Charges vs. Event Number Pixel ",
+				 fChargevsNbins,-0.5,(Axis_t)fChargevsNbins - 0.5);
+    fHPINDiodeChargevsN->SetXTitle("Event Nr.");
+    fHPINDiodeChargevsN->SetYTitle("Sum of Hi Gain FADC slices");
+    fHPINDiodeChargevsN->SetDirectory(NULL);
 
-    Int_t tfirst = 0;
-    Int_t tlast  = 31;
-    Int_t nbins   = 32;
-
-    fHPTime = new TH1F("HPTime","Distribution of Mean Arrival Times",nbins,tfirst,tlast);
-    fHPTime->SetXTitle("Mean Arrival Times [FADC slice nr]");
-    fHPTime->SetYTitle("Nr. of events");
-    fHPTime->Sumw2();
-    fHPTime->SetDirectory(0);
-
+    Clear();
 }
 
@@ -89,21 +87,87 @@
 {
 
-  delete fHPCharge;
-  delete fHErrCharge;
+  delete fHPINDiodeCharge;
+  delete fHPINDiodeTime;
+  delete fHPINDiodeChargevsN;
+
+  if (fChargeGausFit)
+    delete fChargeGausFit;
+  if (fTimeGausFit)
+    delete fTimeGausFit;
+  if (fFitLegend)
+    delete fFitLegend;
+}
+
+void MHCalibrationPINDiode::Clear(Option_t *o)
+{
   
-  if (fVarGausFit)
-    delete fVarGausFit;
-  
-  delete fHPTime;
+  fTotalEntries            = 0;
+
+  fChargeFirstHiGain       = -100.5;
+  fChargeLastHiGain        = 1999.5;
+
+  fChargeChisquare         = -1.;
+  fChargeProb              = -1.;
+  fChargeNdf               = -1;
+  fTimeChisquare           = -1.;
+  fTimeProb                = -1.;
+  fTimeNdf                 = -1;
+  fTimeMean                = -1.;
+  fTimeSigma               = -1.;
+
+  fTimeLowerFitRangeHiGain = 0;
+  fTimeUpperFitRangeHiGain = 0;
+  fTimeLowerFitRangeLoGain = 0;
+  fTimeUpperFitRangeLoGain = 0;
+
+  if (fChargeGausFit)
+    delete fChargeGausFit;
+  if (fTimeGausFit)
+    delete fTimeGausFit;
+  if (fFitLegend)
+    delete fFitLegend;
+
+  return;
 
 }
 
-const Double_t MHCalibrationPINDiode::GetTime() const
+void MHCalibrationPINDiode::Reset()
 {
-    return fVarGausFit->GetParameter(2);
+
+  Clear();
+
+  fHPINDiodeCharge->Reset();
+  fHPINDiodeTime->Reset();
+  fHPINDiodeChargevsN->Reset();
 }
 
-const Double_t MHCalibrationPINDiode::GetErrTime() const
+
+Bool_t MHCalibrationPINDiode::FillCharge(Float_t q)
 {
-    return fVarGausFit->GetParameter(3);
+    return (fHPINDiodeCharge->Fill(q) > -1);
 }
+
+Bool_t MHCalibrationPINDiode::FillTime(Float_t t)
+{
+    return (fHPINDiodeTime->Fill(t)   > -1);
+}
+
+Bool_t MHCalibrationPINDiode::FillChargevsN(Float_t q, Int_t n)
+{
+    return (fHPINDiodeChargevsN->Fill(n,q) > -1);
+}
+
+void MHCalibrationPINDiode::CutAllEdges()
+{
+
+  Int_t nbins = 30;
+
+  CutEdges(fHPINDiodeCharge,nbins);
+
+  fChargeFirstHiGain = fHPINDiodeCharge->GetBinLowEdge(fHPINDiodeCharge->GetXaxis()->GetFirst());
+  fChargeLastHiGain  = fHPINDiodeCharge->GetBinLowEdge(fHPINDiodeCharge->GetXaxis()->GetLast())
+                      +fHPINDiodeCharge->GetBinWidth(0);
+  CutEdges(fHPINDiodeChargevsN,0);
+
+}
+
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationPINDiode.h	(revision 2931)
@@ -9,4 +9,5 @@
 class TH1F;
 class TF1;
+class TPaveText;
 
 class MHCalibrationPINDiode : public MHCalibrationPixel
@@ -14,13 +15,16 @@
 private:
 
-  TH1I* fHPCharge;           //-> Histogram containing the summed 32 PINDiode slices
-  TH1F* fHErrCharge;         //-> Variance of summed FADC slices
-  TH1F* fHPTime;             //-> Histogram with time evolution of summed charges
+  TH1F* fHPINDiodeCharge;    // Histogram containing the summed 32 PINDiode slices
+  TH1F* fHPINDiodeErrCharge; // Variance of summed FADC slices
+  TH1F* fHPINDiodeTime;      // Histogram with time evolution of summed charges
   
-  TF1 *fVarGausFit;
+  TH1I* fHPINDiodeChargevsN; // Summed Charge vs. Event Nr.
   
-  Float_t  fErrChargeFirst;
-  Float_t  fErrChargeLast;
-  UShort_t fErrChargeNbins;
+  const Int_t   fChargeNbins;
+  const Int_t   fTimeNbins;
+  const Int_t   fChargevsNbins;
+
+  const Axis_t  fTimeFirst;
+  const Axis_t  fTimeLast;
 
 public:
@@ -29,7 +33,19 @@
   ~MHCalibrationPINDiode();
 
-  const Double_t GetTime() const;
-  const Double_t GetErrTime() const;
+  void Clear(Option_t *o="");
+  void Reset();
 
+  // Fill histos
+  Bool_t FillCharge(Float_t q);
+  Bool_t FillTime(Float_t t);
+  Bool_t FillChargevsN(Float_t q, Int_t n);
+
+  // Fits -- not yet implemented
+  Bool_t FitCharge(Option_t *option="RQ0") { return kTRUE; }
+  Bool_t FitTime(Option_t *option="RQ0")   { return kTRUE; }
+
+  // Others
+  void CutAllEdges();
+  
   ClassDef(MHCalibrationPINDiode, 0)  // Histograms from the Calibration PIN Diode
 };
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h	(revision 2930)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationPixel.h	(revision 2931)
@@ -19,5 +19,4 @@
 
   Int_t fPixId;                  // Pixel Nr
-  Int_t fTotalEntries;           // Number of entries
 
   TArrayF *fHiGains;             //->
@@ -25,4 +24,6 @@
 
 protected:
+
+  Int_t fTotalEntries;           // Number of entries
 
   TH1F* fHChargeHiGain;          // Summed FADC slices High Gain
