Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3702)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3703)
@@ -45,5 +45,4 @@
 //
 //   PostProcess(): - FinalizePedestals()
-//                  - FinalizeAvPedestals()
 //                  - FinalizeCharges()
 //                  - FinalizeFFactorMethod()
@@ -60,7 +59,4 @@
 //   MCalibrationChargePINDiode
 //   MCalibrationQECam
-//   MExtractedSignalCam
-//   MExtractedSignalBlindPixel
-//   MExtractedSignalPINDiode
 //   MPedestalCam
 //   MBadPixelsCam
@@ -361,27 +357,24 @@
 //  - MCalibrationChargePINDiode
 //
-// Sets the pulser colour in:
+// It retrieves the following variables from MCalibrationChargeCam:
+//
+//  - fNumHiGainSamples 
+//  - fNumLoGainSamples 
+//
+// It defines the PixId of every pixel in:
+//
+// - MCalibrationChargeCam 
+// - MCalibrationQECam
+//
+// It sets all pixels in excluded which have the flag fBadBixelsPix::IsBad() set in:
+// 
+// - MCalibrationChargePix
+// - MCalibrationQEPix
+//
+// Sets the pulser colour and tests if it has not changed w.r.t. fPulserColor in:
 //
 // - MCalibrationChargeCam
 // - MCalibrationChargeBlindPix (if existing)
 // - MCalibrationChargePINDiode (if existing)
-//
-// It retrieves the following variables from MCalibrationChargeCam:
-//
-//  - fNumHiGainSamples 
-//  - fNumLoGainSamples 
-//
-// It defines the PixId of every pixel in:
-//
-// - MCalibrationChargeCam 
-// - MCalibrationQECam
-//
-// It sets all pixels in excluded which have the flag fBadBixelsPix::IsBad() set in:
-// 
-// - MCalibrationChargePix
-// - MCalibrationQEPix
-//
-// It initializes the pulser color in MCalibrationChargeCam, MCalibrationChargeBlindPix 
-// and MCalibrationChargePINDiode and tests, if it has not changed w.r.t. fPulserColor 
 //
 Bool_t MCalibrationChargeCalc::ReInit(MParList *pList )
@@ -495,603 +488,29 @@
 }
 
-// ----------------------------------------------------------------------------------
-//  
-// Retrieves pedestal and pedestal RMS from MPedestalPix 
-// Retrieves total entries from MPedestalCam
-// Sets pedestal*fNumHiGainSamples and pedestal*fNumLoGainSamples in MCalibrationChargePix
-// Sets pedRMS *fSqrtHiGainSamples and pedRMS *fSqrtLoGainSamples in MCalibrationChargePix
-//
-void MCalibrationChargeCalc::FinalizePedestals(const MPedestalPix &ped, MCalibrationChargePix &cal)
-{
-  
-  //
-  // get the pedestals
-  //
-  const Float_t pedes  = ped.GetPedestal();
-  const Float_t prms   = ped.GetPedestalRms();
-  const Float_t num    = TMath::Sqrt((Float_t)fPedestals->GetTotalEntries());
-
-  //
-  // set them in the calibration camera
-  //
-  if (cal.IsHiGainSaturation())
-    {
-      cal.SetPedestal(pedes* fNumLoGainSamples,
-                      prms * fSqrtLoGainSamples,
-                      prms * fNumLoGainSamples / num);
-      cal.CalcLoGainPedestal((Float_t)fNumLoGainSamples);
-    }
-  else
-    {
-      cal.SetPedestal(pedes* fNumHiGainSamples,
-                      prms * fSqrtHiGainSamples,
-                      prms * fNumHiGainSamples / num);
-    }
-  
-}
-
-// ---------------------------------------------------------------------
-//
-// Finalize charges per pixel: 
-// - Check chage validity
-// - Calculate the reduced sigma
-// - Calculate the number of photo-electrons
-//
-Bool_t MCalibrationChargeCalc::FinalizeCharges(MCalibrationChargePix &cal, MBadPixelsPix &bad)
-{
-
-  //
-  // The check return kTRUE if:
-  //
-  // 1) Pixel has a fitted charge greater than fChargeLimit*PedRMS
-  // 2) Pixel has a fit error greater than fChargeVarLimit
-  // 3) Pixel has a fitted charge greater its fChargeRelVarLimit times its charge error
-  // 4) Pixel has a charge sigma bigger than its Pedestal RMS
-  // 
-  if (cal.GetMean() < fChargeLimit*cal.GetPedRms())
-    {
-      *fLog << warn << GetDescriptor() << ": Fitted Charge: " << cal.GetMean() << " is smaller than "
-            << fChargeLimit << " Pedestal RMS: " <<  cal.GetPedRms() << " in Pixel  " << cal.GetPixId() << endl;
-      bad.SetUncalibrated( MBadPixelsPix::kChargeIsPedestal);
-    }
-  
-  if (cal.GetMeanErr() < fChargeErrLimit) 
-    {
-      *fLog << warn << GetDescriptor() << ": Error of Fitted Charge: " << cal.GetMeanErr() 
-            << " is smaller than " << fChargeErrLimit << " in Pixel  " << cal.GetPixId() << endl;
-      bad.SetUncalibrated( MBadPixelsPix::kChargeErrNotValid );
-    }
-      
-   if (cal.GetMean() < fChargeRelErrLimit*cal.GetMeanErr()) 
-    {
-      *fLog << warn << GetDescriptor() << ": Fitted Charge: " << cal.GetMean() << " is smaller than "
-            << fChargeRelErrLimit << "* its error: " << cal.GetMeanErr() 
-            << " in Pixel  " << cal.GetPixId() << endl;
-      bad.SetUncalibrated( MBadPixelsPix::kChargeRelErrNotValid );
-    }
-
-  if (cal.GetSigma() < cal.GetPedRms())
-    {
-      *fLog << warn << GetDescriptor() << ": Sigma of Fitted Charge: " << cal.GetSigma() 
-	    << " smaller than Pedestal RMS: " << cal.GetPedRms() << " in Pixel  " << cal.GetPixId() << endl;
-      bad.SetUncalibrated( MBadPixelsPix::kChargeSigmaNotValid );
-    }
-
-  if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
-    return kFALSE;
-
-  if (!cal.CalcReducedSigma())
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": Could not calculate reduced sigmas of pixel: " << cal.GetPixId() << endl;
-      bad.SetUncalibrated(MBadPixelsPix::kChargeIsPedestal);
-      return kFALSE;
-    }
-  
-  if (!cal.CalcFFactorMethod())
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": Could not calculate F-Factor of pixel: " << cal.GetPixId() << endl;
-      bad.SetUncalibrated(MBadPixelsPix::kDeviatingNumPhes);
-      return kFALSE;
-    }
-
-  return kTRUE;
-}
-
-// ------------------------------------------------------------------------
-//
-// Returns kFALSE if pointer to MExtractedSignalPINDiode is NULL
-// Returns kFALSE if pointer to MCalibrationChargePINDiode is NULL
-//
-// The check returns kFALSE if:
-//
-// 1) PINDiode has a fitted charge smaller than fChargeLimit*PedRMS
-// 2) PINDiode has a fit error smaller than fChargeErrLimit
-// 3) PINDiode has a fitted charge smaller its fChargeRelErrLimit times its charge error
-// 4) PINDiode has a charge sigma smaller than its Pedestal RMS
-// 
-// Calls:
-// - MCalibrationChargePINDiode::CalcFluxOutsidePlexiglass()
-//
-Bool_t MCalibrationChargeCalc::FinalizePINDiode()
-{
-
-  if (!fPINDiode)
-    return kFALSE;  
-
-  if (fPINDiode->GetMean() < fChargeLimit*fPINDiode->GetPedRms())
-    {
-      *fLog << warn << GetDescriptor() << ": Fitted Charge is smaller than "
-            << fChargeLimit << " Pedestal RMS in PINDiode " << endl;
-      return kFALSE;
-    }
-  
-  if (fPINDiode->GetMeanErr() < fChargeErrLimit) 
-    {
-      *fLog << warn << GetDescriptor() << ": Error of Fitted Charge is smaller than "
-            << fChargeErrLimit << " in PINDiode " << endl;
-      return kFALSE;
-    }
-      
-  if (fPINDiode->GetMean() < fChargeRelErrLimit*fPINDiode->GetMeanErr()) 
-    {
-      *fLog << warn << GetDescriptor() << ": Fitted Charge is smaller than "
-            << fChargeRelErrLimit << "* its error in PINDiode " << endl;
-      return kFALSE;
-    }
-      
-  if (fPINDiode->GetSigma() < fPINDiode->GetPedRms())
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": Sigma of Fitted Charge smaller than Pedestal RMS in PINDiode " << endl;
-      return kFALSE;
-    }
-
-
-  if (!fPINDiode->CalcFluxOutsidePlexiglass())
-    {
-      *fLog << warn << "Could not calculate the flux of photons from the PIN Diode, "
-            << "will skip PIN Diode Calibration " << endl;
-      return kFALSE;
-    }
-  
-  return kTRUE;
-}
-
-// ------------------------------------------------------------------------
-//
-// Returns kFALSE if pointer to MExtractedSignalBlindPixel is NULL
-// Returns kFALSE if pointer to MCalibrationChargeBlindPix is NULL
-//
-// The check returns kFALSE if:
-//
-// 1) fLambda and fLambdaCheck are separated relatively to each other by more than fLambdaCheckLimit
-// 2) BlindPixel has an fLambdaErr greater than fLambdaErrLimit
-// 
-// Calls:
-// - MCalibrationChargeBlindPix::CalcFluxInsidePlexiglass()
-//
-Bool_t MCalibrationChargeCalc::FinalizeBlindPixel()
-{
-
-  if (!fBlindPixel)
-    return kFALSE;  
-
-  const Float_t lambda      = fBlindPixel->GetLambda();
-  const Float_t lambdaerr   = fBlindPixel->GetLambdaErr();
-  const Float_t lambdacheck = fBlindPixel->GetLambdaCheck();
-
-  if (2.*(lambdacheck-lambda)/(lambdacheck+lambda) < fLambdaCheckLimit)
-    {
-      *fLog << warn << GetDescriptor() << ": Lambda and Lambda-Check differ by more than "
-            << fLambdaCheckLimit << " in the Blind Pixel " << endl;
-      return kFALSE;
-    }
-  
-  if (lambdaerr < fLambdaErrLimit) 
-    {
-      *fLog << warn << GetDescriptor() << ": Error of Fitted Lambda is greater than "
-            << fLambdaErrLimit << " in Blind Pixel " << endl;
-      return kFALSE;
-    }
-      
-  if (!fBlindPixel->CalcFluxInsidePlexiglass())
-    {
-      *fLog << warn << "Could not calculate the flux of photons from the Blind Pixel, "
-            << "will skip Blind Pixel Calibration " << endl;
-      return kFALSE;
-    }
-  
-  return kTRUE;
-}
-
-// ------------------------------------------------------------------------
-//
-//
-Bool_t MCalibrationChargeCalc::FinalizeFFactorMethod()
-{
-
-  const UInt_t npixels  = fGeom->GetNumPixels();
-  const UInt_t nareas   = fGeom->GetNumAreas();
-  const UInt_t nsectors = fGeom->GetNumSectors();
-
-  Float_t lowlim      [nareas];
-  Float_t upplim      [nareas];
-  Float_t areavars    [nareas];
-  Float_t areaweights [nareas], sectorweights [nsectors];
-  Float_t areaphes    [nareas], sectorphes    [nsectors];
-  Int_t   numareavalid[nareas], numsectorvalid[nsectors];
-
-  memset(lowlim        ,0, nareas   * sizeof(Float_t));
-  memset(upplim        ,0, nareas   * sizeof(Float_t));
-  memset(areaphes      ,0, nareas   * sizeof(Float_t));
-  memset(areavars      ,0, nareas   * sizeof(Float_t));
-  memset(areaweights   ,0, nareas   * sizeof(Float_t));
-  memset(numareavalid  ,0, nareas   * sizeof(Int_t  ));
-  memset(sectorweights ,0, nsectors * sizeof(Float_t));
-  memset(sectorphes    ,0, nsectors * sizeof(Float_t));
-  memset(numsectorvalid,0, nsectors * sizeof(Int_t  ));
-  
-  //
-  // First loop: Get mean number of photo-electrons and the RMS
-  //             The loop is only to recognize later pixels with very deviating numbers
-  //
-  for (UInt_t i=0; i<npixels; i++)
-    {
-      
-      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)  [i];
-      MBadPixelsPix         &bad = (*fBadPixels)[i];
-      
-      if (!pix.IsFFactorMethodValid())
-        continue;
-
-      if (!bad.IsCalibrationResultOK())
-        {
-          pix.SetFFactorMethodValid(kFALSE);
-          continue;
-        }
-      
-      const Float_t nphe  = pix.GetPheFFactorMethod();
-      const Float_t nvar  = pix.GetPheFFactorMethodVar();
-      const Int_t   aidx  = (*fGeom)[i].GetAidx();
-
-      if (nvar > 0.)
-        {
-          areaphes    [aidx] += nphe;
-          areavars    [aidx] += nvar;
-          numareavalid[aidx] ++;
-        } 
-    } 
-
-  for (UInt_t i=0; i<nareas; i++)
-    {
-      if (numareavalid[i] == 0)
-        {
-          *fLog << warn << GetDescriptor() << ": No pixels with valid number of photo-electrons found "
-                << "in area index: " << i << endl;
-          continue;
-        }
-
-      areaphes[i] = areaphes[i] / numareavalid[i];
-      areavars[i] = areavars[i] / numareavalid[i];
-      lowlim  [i] = areaphes[i] - fPheErrLimit*TMath::Sqrt(areavars[i]);
-      upplim  [i] = areaphes[i] + fPheErrLimit*TMath::Sqrt(areavars[i]);
-    }
-
-  memset(numareavalid,0,nareas*sizeof(Int_t));
-  memset(areaphes    ,0,nareas*sizeof(Int_t));
-  memset(areavars    ,0,nareas*sizeof(Int_t));
-
-  //
-  // Second loop: Get weighted mean number of photo-electrons and its RMS excluding 
-  //              pixels deviating by more than fPheErrLimit sigma. 
-  //              Set the conversion factor FADC counts to photo-electrons
-  // 
-  for (UInt_t i=0; i<npixels; i++)
-    {
-      
-      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
-
-      if (!pix.IsFFactorMethodValid())
-        continue;
-
-      const Float_t nvar  = pix.GetPheFFactorMethodVar();
-
-      if (nvar <= 0.)
-        {
-          pix.SetFFactorMethodValid(kFALSE);
-          continue;
-        }
-      
-      MBadPixelsPix         &bad = (*fBadPixels)[i];
-
-      const Int_t   aidx   = (*fGeom)[i].GetAidx();
-      const Int_t   sector = (*fGeom)[i].GetSector();
-      const Float_t nphe   = pix.GetPheFFactorMethod();
-
-      if ( nphe < lowlim[aidx] || nphe > upplim[aidx] )
-        {
-          *fLog << warn << GetDescriptor() << ": Deviating number of photo-electrons: " 
-                << Form("%4.2f",nphe) << " out of accepted limits: [" 
-                << Form("%4.2f%s%4.2f",lowlim[aidx],",",upplim[aidx]) << "] in pixel " << i << endl;
-          bad.SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
-          bad.SetUnsuitable  ( MBadPixelsPix::kUnreliableRun    );
-          continue;
-        }
-      
-      const Float_t weight = 1./nvar;
-      
-      areaweights   [aidx]   += weight;
-      areaphes      [aidx]   += weight*nphe;
-      numareavalid  [aidx]   ++;
-      sectorweights [sector] += weight;
-      sectorphes    [sector] += weight*nphe;
-      numsectorvalid[sector] ++;
-    } 
-
-  for (UInt_t aidx=0; aidx<nareas; aidx++)
-    {
-
-      MCalibrationChargePix &apix = (MCalibrationChargePix&)fCam->GetAverageArea(aidx);
-
-      if (areaweights[aidx] <= 0. || areaphes[aidx] <= 0.)
-        {
-          *fLog << warn << " Mean number of phe's from area index " << aidx << " cannot be calculated: "
-                << " Sum of weights: "       << areaweights[aidx] 
-                << " Sum of weighted phes: " << areaphes[aidx]    << endl;
-          apix.SetFFactorMethodValid(kFALSE);
-          continue;
-        }
-
-      *fLog << inf << "Replacing number photo-electrons of average area idx " << aidx << ": " 
-            << Form("%5.3f%s%5.3f",apix.GetPheFFactorMethod()," +- ",apix.GetPheFFactorMethodErr()) << endl;
-      *fLog << inf << "  by average number of photo-electrons from area idx " << aidx <<  ": " 
-            << Form("%5.3f%s%5.3f",areaphes[aidx] / areaweights[aidx]," +- ",
-                    TMath::Sqrt(1./areaweights[aidx])) << endl;
-
-      apix.SetPheFFactorMethod   ( areaphes[aidx]/ areaweights[aidx] );
-      apix.SetPheFFactorMethodVar(    1.         / areaweights[aidx] );      
-      apix.SetFFactorMethodValid ( kTRUE );
-
-    }
-
-  for (UInt_t sector=0; sector<nsectors; sector++)
-    {
-
-      MCalibrationChargePix &spix = (MCalibrationChargePix&)fCam->GetAverageSector(sector);
-
-      if (sectorweights[sector] <= 0. || sectorphes[sector] <= 0.)
-        {
-          *fLog << warn << " Mean number of phe's from sector " << sector << " cannot be calculated: "
-                << " Sum of weights: "       << sectorweights[sector] 
-                << " Sum of weighted phes: " << sectorphes[sector]    << endl;
-          spix.SetFFactorMethodValid(kFALSE);
-          continue;
-        }
-
-      *fLog << inf << "Replacing number photo-electrons of average sector " << sector << ": " 
-            << Form("%5.3f%s%5.3f",spix.GetPheFFactorMethod()," +- ",spix.GetPheFFactorMethodErr()) << endl;
-      *fLog << inf << "   by average number photo-electrons from sector " << sector <<  ": " 
-            << Form("%5.3f%s%5.3f",sectorphes[sector]/ sectorweights[sector]," +- ",
-                    TMath::Sqrt(1./sectorweights[sector])) << endl;
-
-      spix.SetPheFFactorMethod   ( sectorphes[sector]/ sectorweights[sector] );
-      spix.SetPheFFactorMethodVar(    1.        / sectorweights[sector] );      
-      spix.SetFFactorMethodValid ( kTRUE );
-
-    }
-
-  return kTRUE;
-}
-
-
-// ----------------------------------------------------------------------
-//
-// Sets all pixels to MBadPixelsPix::kUnsuitableRun, if following flags are set:
-// - MBadPixelsPix::kChargeIsPedestal
-// - MBadPixelsPix::kChargeErrNotValid 
-// - MBadPixelsPix::kChargeRelErrNotValid 
-// - MBadPixelsPix::kChargeSigmaNotValid 
-// - MBadPixelsPix::kMeanTimeInFirstBin 
-// - MBadPixelsPix::kMeanTimeInLast2Bins 
-//
-// Sets all pixels to MBadPixelsPix::kUnreliableRun, if following flags are set:
-// - MBadPixelsPix::kDeviatingNumPhes
-//
-void MCalibrationChargeCalc::FinalizeBadPixels()
-{
-  
-  for (Int_t i=0; i<fBadPixels->GetSize(); i++)
-    {
-      
-      MBadPixelsPix    &bad    = (*fBadPixels)[i];
-
-      if (bad.IsUncalibrated( MBadPixelsPix::kChargeIsPedestal))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun   );
- 
-      if (bad.IsUncalibrated( MBadPixelsPix::kChargeErrNotValid ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
-
-      if (bad.IsUncalibrated( MBadPixelsPix::kChargeRelErrNotValid ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
- 
-      if (bad.IsUncalibrated( MBadPixelsPix::kChargeSigmaNotValid ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
-
-      if (bad.IsUncalibrated( MBadPixelsPix::kMeanTimeInFirstBin ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
-
-      if (bad.IsUncalibrated( MBadPixelsPix::kMeanTimeInLast2Bins ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
-
-      if (bad.IsUncalibrated( MBadPixelsPix::kDeviatingNumPhes ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnreliableRun    );
-    }
-}
-
-// ------------------------------------------------------------------------
-//
-//
-void MCalibrationChargeCalc::FinalizeFFactorQECam()
-{
-
-  MCalibrationChargePix &avpix = (MCalibrationChargePix&)fCam->GetAverageArea(0);
-  MCalibrationQEPix     &qepix = (MCalibrationQEPix&)  fQECam->GetAverageArea(0);  
-
-  const Float_t avphotons   = avpix.GetPheFFactorMethod()       
-                           / qepix.GetQEFFactor(fPulserColor)
-                           / fQECam->GetPlexiglassQE();
-
-  const Float_t avphotrelvar = avpix.GetPheFFactorMethodRelVar() 
-                            + qepix.GetQEFFactorRelVar(fPulserColor)
-                            + fQECam->GetPlexiglassQERelVar();
-
-  const UInt_t npixels  = fGeom->GetNumPixels();
-
-  for (UInt_t i=0; i<npixels; i++)
-    {
-      
-      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
-      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
-
-      if (!pix.IsFFactorMethodValid())
-        {
-          qepix.SetFFactorMethodValid(kFALSE,fPulserColor);
-          continue;
-        }
-      
-      const Float_t photons = avphotons / fGeom->GetPixRatio(i);
-      const Float_t qe      = pix.GetPheFFactorMethod() / photons ;
-
-      if (!pix.CalcMeanFFactor( photons , avphotrelvar ))
-        {
-          pix.SetFFactorMethodValid(kFALSE);
-          qepix.SetFFactorMethodValid(kFALSE, fPulserColor);
-          (*fBadPixels)[i].SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
-        }
-
-      const Float_t qerelvar = avphotrelvar +  pix.GetPheFFactorMethodRelVar();
-
-      qepix.SetQEFFactor    ( qe            , fPulserColor );
-      qepix.SetQEFFactorVar ( qerelvar*qe*qe, fPulserColor );      
-      qepix.SetFFactorMethodValid(  kTRUE   , fPulserColor );
-
-      if (!qepix.UpdateFFactorMethod())
-        *fLog << warn << GetDescriptor() << ": Cannot update Quantum efficiencies with the F-Factor Method" << endl;
-    }
-}
-
-// ------------------------------------------------------------------------
-//
-//
-void MCalibrationChargeCalc::FinalizeBlindPixelQECam()
-{
-
-  const UInt_t npixels  = fGeom->GetNumPixels();
-  
-  //
-  //  With the knowledge of the overall photon flux, calculate the 
-  //  quantum efficiencies after the Blind Pixel and PIN Diode method
-  //
-  for (UInt_t i=0; i<npixels; i++)
-    {
-      
-      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
-
-      if (!fBlindPixel)
-        {
-          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-      
-      if (!fBlindPixel->IsFluxInsidePlexiglassAvailable())
-        {
-          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-      
-      MBadPixelsPix       &bad   =                   (*fBadPixels)[i];
-
-      if (!bad.IsUnsuitable (MBadPixelsPix::kUnsuitableRun))
-        {
-          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-      
-      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
-      MGeomPix              &geo =                        (*fGeom)[i];
-      
-      const Float_t conv       = fBlindPixel->GetFluxInsidePlexiglass() 
-                              * geo.GetA() 
-                              / fQECam->GetPlexiglassQE()
-                              / pix.GetPheFFactorMethod();
-
-      const Float_t convrelvar = fBlindPixel->GetFluxInsidePlexiglassRelVar() 
-                               + fQECam->GetPlexiglassQERelVar()
-                               + pix.GetPheFFactorMethodRelVar();
-
-      qepix.SetQEBlindPixel    ( conv                    , fPulserColor );
-      qepix.SetQEBlindPixelVar ( convrelvar * conv * conv, fPulserColor );      
-      qepix.UpdateBlindPixelMethod();
-    }
-}
-
-// ------------------------------------------------------------------------
-//
-//
-void MCalibrationChargeCalc::FinalizePINDiodeQECam()
-{
-  
-  const UInt_t npixels  = fGeom->GetNumPixels();
-
-  //
-  //  With the knowledge of the overall photon flux, calculate the 
-  //  quantum efficiencies after the PIN Diode method
-  //
-  for (UInt_t i=0; i<npixels; i++)
-    {
-      
-      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
-
-      if (!fPINDiode)
-        {
-          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-      
-      if (!fPINDiode->IsFluxOutsidePlexiglassAvailable())
-        {
-          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-
-      MBadPixelsPix &bad  =  (*fBadPixels)[i];
-
-      if (!bad.IsUnsuitable (MBadPixelsPix::kUnsuitableRun))
-        {
-          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
-          continue;
-        }
-      
-      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
-      MGeomPix              &geo =                        (*fGeom)[i];
-      
-      const Float_t conv       = fPINDiode->GetFluxOutsidePlexiglass() * geo.GetA() / pix.GetPheFFactorMethod();
-      const Float_t convrelvar = fPINDiode->GetFluxOutsidePlexiglassRelVar() + pix.GetPheFFactorMethodRelVar();
-
-      qepix.SetQEPINDiode    ( conv                    , fPulserColor );
-      qepix.SetQEPINDiodeVar ( convrelvar * conv * conv, fPulserColor );      
-      qepix.UpdateBlindPixelMethod();
-    }
-}
-
-
 // -----------------------------------------------------------------------
 //
-// - Finalize the pedestals
-// - Do the quality checks 
-// - Calculate the reduced sigma 
-// - Calculate the F-Factor Method
+// Return if number of executions is null.
+//
+// First loop over pixels, average areas and sectors, call: 
+//  - FinalizePedestals() 
+//  - FinalizeCharges()
+// for every entry. Count number of valid pixels in loop and return kFALSE
+// if there are none (the "Michele check").
+//
+// Call FinalizeBadPixels()
+//
+// Call FinalizeFFactorMethod() (second and third loop over pixels and areas)
+//
+// Call FinalizeBlindPixel() 
+// Call FinalizePINDiode()
+//
+// Call  FinalizeFFactorQECam() (fourth loop over pixels and areas)
+// Call  FinalizeBlindPixelQECam() (fifth loop over pixels and areas)
+// Call  FinalizePINDiodeQECam() (sixth loop over pixels and areas)
+//  
+// Call MParContainer::SetReadyToSave() for fCam, fQECam, fBadPixels and 
+//                                          fBlindPixel and fPINDiode if they exist
+//
+// Print out some statistics
 //
 Int_t MCalibrationChargeCalc::PostProcess()
@@ -1102,5 +521,5 @@
 
   //
-  // loop over the pedestal events and check if we have calibration
+  // First loop over pixels, call FinalizePedestals and FinalizeCharges
   //
   Int_t   nvalid      = 0;
@@ -1201,4 +620,11 @@
   fQECam    ->SetReadyToSave();
   fBadPixels->SetReadyToSave();
+
+  if (fBlindPixel)
+    fBlindPixel->SetReadyToSave();
+  if (fPINDiode)
+    fPINDiode->SetReadyToSave();
+  
+
   
   *fLog << inf << endl;
@@ -1257,4 +683,691 @@
 }
 
+// ----------------------------------------------------------------------------------
+//  
+// Retrieves pedestal and pedestal RMS from MPedestalPix 
+// Retrieves total entries from MPedestalCam
+// Sets pedestal*fNumHiGainSamples and pedestal*fNumLoGainSamples in MCalibrationChargePix
+// Sets pedRMS *fSqrtHiGainSamples and pedRMS *fSqrtLoGainSamples in MCalibrationChargePix
+//
+// If the flag MCalibrationPix::IsHiGainSaturation() is set, call also: 
+// - MCalibrationChargePix::CalcLoGainPedestal()
+//
+void MCalibrationChargeCalc::FinalizePedestals(const MPedestalPix &ped, MCalibrationChargePix &cal)
+{
+  
+  //
+  // get the pedestals
+  //
+  const Float_t pedes  = ped.GetPedestal();
+  const Float_t prms   = ped.GetPedestalRms();
+  const Float_t num    = TMath::Sqrt((Float_t)fPedestals->GetTotalEntries());
+
+  //
+  // set them in the calibration camera
+  //
+  if (cal.IsHiGainSaturation())
+    {
+      cal.SetPedestal(pedes* fNumLoGainSamples,
+                      prms * fSqrtLoGainSamples,
+                      prms * fNumLoGainSamples / num);
+      cal.CalcLoGainPedestal((Float_t)fNumLoGainSamples);
+    }
+  else
+    {
+      cal.SetPedestal(pedes* fNumHiGainSamples,
+                      prms * fSqrtHiGainSamples,
+                      prms * fNumHiGainSamples / num);
+    }
+  
+}
+
+// ----------------------------------------------------------------------------------------------------
+//
+// Check fit results validity. Bad Pixels flags are set if:
+//
+// 1) Pixel has a mean smaller than fChargeLimit*PedRMS    ( Flag: MBadPixelsPix::kChargeIsPedestal)
+// 2) Pixel has a mean error smaller than fChargeErrLimit  ( Flag: MBadPixelsPix::kChargeErrNotValid)
+// 3) Pixel has mean smaller than fChargeRelVarLimit times its mean error 
+//                                                         ( Flag: MBadPixelsPix::kChargeRelErrNotValid)
+// 4) Pixel has a sigma bigger than its Pedestal RMS       ( Flag: MBadPixelsPix::kChargeSigmaNotValid )
+// 
+// Further returns if flags: MBadPixelsPix::kUnsuitableRun is set
+// 
+// Calls MCalibrationChargePix::CalcReducedSigma() and sets flag:  MBadPixelsPix::kChargeIsPedestal 
+//       if not succesful.
+// 
+// Calls MCalibrationChargePix::CalcFFactorMethod() and sets flag: MBadPixelsPix::kDeviatingNumPhes) 
+//       if not succesful.
+//
+Bool_t MCalibrationChargeCalc::FinalizeCharges(MCalibrationChargePix &cal, MBadPixelsPix &bad)
+{
+
+  if (cal.GetMean() < fChargeLimit*cal.GetPedRms())
+    {
+      *fLog << warn << GetDescriptor() << ": Fitted Charge: " << cal.GetMean() << " is smaller than "
+            << fChargeLimit << " Pedestal RMS: " <<  cal.GetPedRms() << " in Pixel  " << cal.GetPixId() << endl;
+      bad.SetUncalibrated( MBadPixelsPix::kChargeIsPedestal);
+    }
+  
+  if (cal.GetMeanErr() < fChargeErrLimit) 
+    {
+      *fLog << warn << GetDescriptor() << ": Error of Fitted Charge: " << cal.GetMeanErr() 
+            << " is smaller than " << fChargeErrLimit << " in Pixel  " << cal.GetPixId() << endl;
+      bad.SetUncalibrated( MBadPixelsPix::kChargeErrNotValid );
+    }
+      
+   if (cal.GetMean() < fChargeRelErrLimit*cal.GetMeanErr()) 
+    {
+      *fLog << warn << GetDescriptor() << ": Fitted Charge: " << cal.GetMean() << " is smaller than "
+            << fChargeRelErrLimit << "* its error: " << cal.GetMeanErr() 
+            << " in Pixel  " << cal.GetPixId() << endl;
+      bad.SetUncalibrated( MBadPixelsPix::kChargeRelErrNotValid );
+    }
+
+  if (cal.GetSigma() < cal.GetPedRms())
+    {
+      *fLog << warn << GetDescriptor() << ": Sigma of Fitted Charge: " << cal.GetSigma() 
+	    << " smaller than Pedestal RMS: " << cal.GetPedRms() << " in Pixel  " << cal.GetPixId() << endl;
+      bad.SetUncalibrated( MBadPixelsPix::kChargeSigmaNotValid );
+    }
+
+  if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
+    return kFALSE;
+
+  if (!cal.CalcReducedSigma())
+    {
+      *fLog << warn << GetDescriptor() 
+            << ": Could not calculate reduced sigmas of pixel: " << cal.GetPixId() << endl;
+      bad.SetUncalibrated(MBadPixelsPix::kChargeIsPedestal);
+      return kFALSE;
+    }
+  
+  if (!cal.CalcFFactorMethod())
+    {
+      *fLog << warn << GetDescriptor() 
+            << ": Could not calculate F-Factor of pixel: " << cal.GetPixId() << endl;
+      bad.SetUncalibrated(MBadPixelsPix::kDeviatingNumPhes);
+      return kFALSE;
+    }
+
+  return kTRUE;
+}
+
+
+
+// -----------------------------------------------------------------------------------
+//
+// Sets pixel to MBadPixelsPix::kUnsuitableRun, if one of the following flags is set:
+// - MBadPixelsPix::kChargeIsPedestal
+// - MBadPixelsPix::kChargeErrNotValid 
+// - MBadPixelsPix::kChargeRelErrNotValid 
+// - MBadPixelsPix::kChargeSigmaNotValid 
+// - MBadPixelsPix::kMeanTimeInFirstBin 
+// - MBadPixelsPix::kMeanTimeInLast2Bins 
+//
+// Sets pixel to MBadPixelsPix::kUnreliableRun, if one of the following flags is set:
+// - MBadPixelsPix::kDeviatingNumPhes
+//
+void MCalibrationChargeCalc::FinalizeBadPixels()
+{
+  
+  for (Int_t i=0; i<fBadPixels->GetSize(); i++)
+    {
+      
+      MBadPixelsPix    &bad    = (*fBadPixels)[i];
+
+      if (bad.IsUncalibrated( MBadPixelsPix::kChargeIsPedestal))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun   );
+ 
+      if (bad.IsUncalibrated( MBadPixelsPix::kChargeErrNotValid ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+
+      if (bad.IsUncalibrated( MBadPixelsPix::kChargeRelErrNotValid ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+ 
+      if (bad.IsUncalibrated( MBadPixelsPix::kChargeSigmaNotValid ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+
+      if (bad.IsUncalibrated( MBadPixelsPix::kMeanTimeInFirstBin ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+
+      if (bad.IsUncalibrated( MBadPixelsPix::kMeanTimeInLast2Bins ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+
+      if (bad.IsUncalibrated( MBadPixelsPix::kDeviatingNumPhes ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnreliableRun    );
+    }
+}
+
+// ------------------------------------------------------------------------
+//
+//
+// First loop: Calculate a mean and mean RMS of photo-electrons per area index 
+//             Include only pixels which are not MBadPixelsPix::kUnsuitableRun or 
+//             MBadPixelsPix::kUnreliableRun (see FinalizeBadPixels()) and set 
+//             MCalibrationChargePix::SetFFactorMethodValid(kFALSE) in that case.
+//              
+// Second loop: Get weighted mean number of photo-electrons and its RMS including 
+//              only pixels with flag MCalibrationChargePix::IsFFactorMethodValid() 
+//              and further exclude those deviating by more than fPheErrLimit mean 
+//              sigmas from the mean (obtained in first loop). Set 
+//              MBadPixelsPix::kDeviatingNumPhes if excluded.
+// 
+//              Set weighted mean and variance of photo-electrons per area index in: 
+//              average area pixels of MCalibrationChargeCam (obtained from: 
+//              MCalibrationChargeCam::GetAverageArea() )
+// 
+//              Set weighted mean  and variance of photo-electrons per sector in:
+//              average sector pixels of MCalibrationChargeCam (obtained from: 
+//              MCalibrationChargeCam::GetAverageSector() )
+// 
+Bool_t MCalibrationChargeCalc::FinalizeFFactorMethod()
+{
+
+  const UInt_t npixels  = fGeom->GetNumPixels();
+  const UInt_t nareas   = fGeom->GetNumAreas();
+  const UInt_t nsectors = fGeom->GetNumSectors();
+
+  Float_t lowlim      [nareas];
+  Float_t upplim      [nareas];
+  Float_t areavars    [nareas];
+  Float_t areaweights [nareas], sectorweights [nsectors];
+  Float_t areaphes    [nareas], sectorphes    [nsectors];
+  Int_t   numareavalid[nareas], numsectorvalid[nsectors];
+
+  memset(lowlim        ,0, nareas   * sizeof(Float_t));
+  memset(upplim        ,0, nareas   * sizeof(Float_t));
+  memset(areaphes      ,0, nareas   * sizeof(Float_t));
+  memset(areavars      ,0, nareas   * sizeof(Float_t));
+  memset(areaweights   ,0, nareas   * sizeof(Float_t));
+  memset(numareavalid  ,0, nareas   * sizeof(Int_t  ));
+  memset(sectorweights ,0, nsectors * sizeof(Float_t));
+  memset(sectorphes    ,0, nsectors * sizeof(Float_t));
+  memset(numsectorvalid,0, nsectors * sizeof(Int_t  ));
+  
+  //
+  // First loop: Get mean number of photo-electrons and the RMS
+  //             The loop is only to recognize later pixels with very deviating numbers
+  //
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)  [i];
+      MBadPixelsPix         &bad = (*fBadPixels)[i];
+      
+      if (!pix.IsFFactorMethodValid())
+        continue;
+
+      if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
+        {
+          pix.SetFFactorMethodValid(kFALSE);
+          continue;
+        }
+
+      if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
+        {
+          pix.SetFFactorMethodValid(kFALSE);
+          continue;
+        }
+      
+      const Float_t nphe  = pix.GetPheFFactorMethod();
+      const Float_t nvar  = pix.GetPheFFactorMethodVar();
+      const Int_t   aidx  = (*fGeom)[i].GetAidx();
+
+      if (nvar > 0.)
+        {
+          areaphes    [aidx] += nphe;
+          areavars    [aidx] += nvar;
+          numareavalid[aidx] ++;
+        } 
+    } 
+
+  for (UInt_t i=0; i<nareas; i++)
+    {
+      if (numareavalid[i] == 0)
+        {
+          *fLog << warn << GetDescriptor() << ": No pixels with valid number of photo-electrons found "
+                << "in area index: " << i << endl;
+          continue;
+        }
+
+      areaphes[i] = areaphes[i] / numareavalid[i];
+      areavars[i] = areavars[i] / numareavalid[i];
+      lowlim  [i] = areaphes[i] - fPheErrLimit*TMath::Sqrt(areavars[i]);
+      upplim  [i] = areaphes[i] + fPheErrLimit*TMath::Sqrt(areavars[i]);
+    }
+
+  memset(numareavalid,0,nareas*sizeof(Int_t));
+  memset(areaphes    ,0,nareas*sizeof(Int_t));
+  memset(areavars    ,0,nareas*sizeof(Int_t));
+
+  //
+  // Second loop: Get weighted mean number of photo-electrons and its RMS excluding 
+  //              pixels deviating by more than fPheErrLimit sigma. 
+  //              Set the conversion factor FADC counts to photo-electrons
+  // 
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
+
+      if (!pix.IsFFactorMethodValid())
+        continue;
+
+      const Float_t nvar  = pix.GetPheFFactorMethodVar();
+
+      if (nvar <= 0.)
+        {
+          pix.SetFFactorMethodValid(kFALSE);
+          continue;
+        }
+      
+      MBadPixelsPix         &bad = (*fBadPixels)[i];
+
+      const Int_t   aidx   = (*fGeom)[i].GetAidx();
+      const Int_t   sector = (*fGeom)[i].GetSector();
+      const Float_t nphe   = pix.GetPheFFactorMethod();
+
+      if ( nphe < lowlim[aidx] || nphe > upplim[aidx] )
+        {
+          *fLog << warn << GetDescriptor() << ": Deviating number of photo-electrons: " 
+                << Form("%4.2f",nphe) << " out of accepted limits: [" 
+                << Form("%4.2f%s%4.2f",lowlim[aidx],",",upplim[aidx]) << "] in pixel " << i << endl;
+          bad.SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
+          bad.SetUnsuitable  ( MBadPixelsPix::kUnreliableRun    );
+          continue;
+        }
+      
+      const Float_t weight = 1./nvar;
+      
+      areaweights   [aidx]   += weight;
+      areaphes      [aidx]   += weight*nphe;
+      numareavalid  [aidx]   ++;
+      sectorweights [sector] += weight;
+      sectorphes    [sector] += weight*nphe;
+      numsectorvalid[sector] ++;
+    } 
+
+  for (UInt_t aidx=0; aidx<nareas; aidx++)
+    {
+
+      MCalibrationChargePix &apix = (MCalibrationChargePix&)fCam->GetAverageArea(aidx);
+
+      if (areaweights[aidx] <= 0. || areaphes[aidx] <= 0.)
+        {
+          *fLog << warn << " Mean number of phe's from area index " << aidx << " cannot be calculated: "
+                << " Sum of weights: "       << areaweights[aidx] 
+                << " Sum of weighted phes: " << areaphes[aidx]    << endl;
+          apix.SetFFactorMethodValid(kFALSE);
+          continue;
+        }
+
+      *fLog << inf << "Replacing number photo-electrons of average area idx " << aidx << ": " 
+            << Form("%5.3f%s%5.3f",apix.GetPheFFactorMethod()," +- ",apix.GetPheFFactorMethodErr()) << endl;
+      *fLog << inf << "  by average number of photo-electrons from area idx " << aidx <<  ": " 
+            << Form("%5.3f%s%5.3f",areaphes[aidx] / areaweights[aidx]," +- ",
+                    TMath::Sqrt(1./areaweights[aidx])) << endl;
+
+      apix.SetPheFFactorMethod   ( areaphes[aidx]/ areaweights[aidx] );
+      apix.SetPheFFactorMethodVar(    1.         / areaweights[aidx] );      
+      apix.SetFFactorMethodValid ( kTRUE );
+
+    }
+
+  for (UInt_t sector=0; sector<nsectors; sector++)
+    {
+
+      MCalibrationChargePix &spix = (MCalibrationChargePix&)fCam->GetAverageSector(sector);
+
+      if (sectorweights[sector] <= 0. || sectorphes[sector] <= 0.)
+        {
+          *fLog << warn << " Mean number of phe's from sector " << sector << " cannot be calculated: "
+                << " Sum of weights: "       << sectorweights[sector] 
+                << " Sum of weighted phes: " << sectorphes[sector]    << endl;
+          spix.SetFFactorMethodValid(kFALSE);
+          continue;
+        }
+
+      *fLog << inf << "Replacing number photo-electrons of average sector " << sector << ": " 
+            << Form("%5.3f%s%5.3f",spix.GetPheFFactorMethod()," +- ",spix.GetPheFFactorMethodErr()) << endl;
+      *fLog << inf << "   by average number photo-electrons from sector " << sector <<  ": " 
+            << Form("%5.3f%s%5.3f",sectorphes[sector]/ sectorweights[sector]," +- ",
+                    TMath::Sqrt(1./sectorweights[sector])) << endl;
+
+      spix.SetPheFFactorMethod   ( sectorphes[sector]/ sectorweights[sector] );
+      spix.SetPheFFactorMethodVar(    1.        / sectorweights[sector] );      
+      spix.SetFFactorMethodValid ( kTRUE );
+
+    }
+
+  return kTRUE;
+}
+
+
+// ------------------------------------------------------------------------
+//
+// Returns kFALSE if pointer to MCalibrationChargeBlindPix is NULL
+//
+// The check returns kFALSE if:
+//
+// 1) fLambda and fLambdaCheck are separated relatively to each other by more than fLambdaCheckLimit
+// 2) BlindPixel has an fLambdaErr greater than fLambdaErrLimit
+// 
+// Calls:
+// - MCalibrationChargeBlindPix::CalcFluxInsidePlexiglass()
+//
+Bool_t MCalibrationChargeCalc::FinalizeBlindPixel()
+{
+
+  if (!fBlindPixel)
+    return kFALSE;  
+
+  const Float_t lambda      = fBlindPixel->GetLambda();
+  const Float_t lambdaerr   = fBlindPixel->GetLambdaErr();
+  const Float_t lambdacheck = fBlindPixel->GetLambdaCheck();
+
+  if (2.*(lambdacheck-lambda)/(lambdacheck+lambda) < fLambdaCheckLimit)
+    {
+      *fLog << warn << GetDescriptor() << ": Lambda and Lambda-Check differ by more than "
+            << fLambdaCheckLimit << " in the Blind Pixel " << endl;
+      return kFALSE;
+    }
+  
+  if (lambdaerr < fLambdaErrLimit) 
+    {
+      *fLog << warn << GetDescriptor() << ": Error of Fitted Lambda is greater than "
+            << fLambdaErrLimit << " in Blind Pixel " << endl;
+      return kFALSE;
+    }
+      
+  if (!fBlindPixel->CalcFluxInsidePlexiglass())
+    {
+      *fLog << warn << "Could not calculate the flux of photons from the Blind Pixel, "
+            << "will skip Blind Pixel Calibration " << endl;
+      return kFALSE;
+    }
+  
+  return kTRUE;
+}
+
+// ------------------------------------------------------------------------
+//
+// Returns kFALSE if pointer to MCalibrationChargePINDiode is NULL
+//
+// The check returns kFALSE if:
+//
+// 1) PINDiode has a fitted charge smaller than fChargeLimit*PedRMS
+// 2) PINDiode has a fit error smaller than fChargeErrLimit
+// 3) PINDiode has a fitted charge smaller its fChargeRelErrLimit times its charge error
+// 4) PINDiode has a charge sigma smaller than its Pedestal RMS
+// 
+// Calls:
+// - MCalibrationChargePINDiode::CalcFluxOutsidePlexiglass()
+//
+Bool_t MCalibrationChargeCalc::FinalizePINDiode()
+{
+
+  if (!fPINDiode)
+    return kFALSE;  
+
+  if (fPINDiode->GetMean() < fChargeLimit*fPINDiode->GetPedRms())
+    {
+      *fLog << warn << GetDescriptor() << ": Fitted Charge is smaller than "
+            << fChargeLimit << " Pedestal RMS in PINDiode " << endl;
+      return kFALSE;
+    }
+  
+  if (fPINDiode->GetMeanErr() < fChargeErrLimit) 
+    {
+      *fLog << warn << GetDescriptor() << ": Error of Fitted Charge is smaller than "
+            << fChargeErrLimit << " in PINDiode " << endl;
+      return kFALSE;
+    }
+      
+  if (fPINDiode->GetMean() < fChargeRelErrLimit*fPINDiode->GetMeanErr()) 
+    {
+      *fLog << warn << GetDescriptor() << ": Fitted Charge is smaller than "
+            << fChargeRelErrLimit << "* its error in PINDiode " << endl;
+      return kFALSE;
+    }
+      
+  if (fPINDiode->GetSigma() < fPINDiode->GetPedRms())
+    {
+      *fLog << warn << GetDescriptor() 
+            << ": Sigma of Fitted Charge smaller than Pedestal RMS in PINDiode " << endl;
+      return kFALSE;
+    }
+
+
+  if (!fPINDiode->CalcFluxOutsidePlexiglass())
+    {
+      *fLog << warn << "Could not calculate the flux of photons from the PIN Diode, "
+            << "will skip PIN Diode Calibration " << endl;
+      return kFALSE;
+    }
+  
+  return kTRUE;
+}
+
+// ------------------------------------------------------------------------
+//
+// Calculate the average number of photons outside the plexiglass with the 
+// formula: 
+// 
+// av.Num.photons(area index) = av.Num.Phes(area index) 
+//                            / MCalibrationQEPix::GetDefaultQE(fPulserColor) 
+//                            / MCalibrationQECam::GetPlexiglassQE()
+//
+// Calculate the variance on the average number of photons.
+//
+// Loop over pixels: 
+//
+// - Continue, if not MCalibrationChargePix::IsFFactorMethodValid() and set:
+//                    MCalibrationQEPix::SetFFactorMethodValid(kFALSE,fPulserColor)
+//
+// - Call MCalibrationChargePix::CalcMeanFFactor(av.Num.photons) and set: 
+//        MCalibrationQEPix::SetFFactorMethodValid(kFALSE,fPulserColor) if not succesful
+//
+// - Calculate the quantum efficiency with the formula:
+// 
+//   QE = ( Num.Phes / av.Num.photons ) * MGeomCam::GetPixRatio()
+//
+// - Set QE in MCalibrationQEPix::SetQEFFactor ( QE, fPulserColor );
+// - Set Variance of QE in  MCalibrationQEPix::SetQEFFactorVar ( Variance, fPulserColor );
+// - Set bit MCalibrationQEPix::SetFFactorMethodValid(kTRUE,fPulserColor) 
+//
+// - Call MCalibrationQEPix::UpdateFFactorMethod()
+//
+void MCalibrationChargeCalc::FinalizeFFactorQECam()
+{
+
+  MCalibrationChargePix &avpix = (MCalibrationChargePix&)fCam->GetAverageArea(0);
+  MCalibrationQEPix     &qepix = (MCalibrationQEPix&)  fQECam->GetAverageArea(0);  
+
+  const Float_t avphotons   = avpix.GetPheFFactorMethod()       
+                           / qepix.GetQEFFactor(fPulserColor)
+                           / fQECam->GetPlexiglassQE();
+
+  const Float_t avphotrelvar = avpix.GetPheFFactorMethodRelVar() 
+                            + qepix.GetQEFFactorRelVar(fPulserColor)
+                            + fQECam->GetPlexiglassQERelVar();
+
+  const UInt_t npixels  = fGeom->GetNumPixels();
+
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
+      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
+
+      if (!pix.IsFFactorMethodValid())
+        {
+          qepix.SetFFactorMethodValid(kFALSE,fPulserColor);
+          continue;
+        }
+      
+      const Float_t photons = avphotons / fGeom->GetPixRatio(i);
+      const Float_t qe      = pix.GetPheFFactorMethod() / photons ;
+
+      if (!pix.CalcMeanFFactor( photons , avphotrelvar ))
+        {
+          pix.SetFFactorMethodValid(kFALSE);
+          qepix.SetFFactorMethodValid(kFALSE, fPulserColor);
+          (*fBadPixels)[i].SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
+        }
+
+      const Float_t qerelvar = avphotrelvar +  pix.GetPheFFactorMethodRelVar();
+
+      qepix.SetQEFFactor    ( qe            , fPulserColor );
+      qepix.SetQEFFactorVar ( qerelvar*qe*qe, fPulserColor );      
+      qepix.SetFFactorMethodValid(  kTRUE   , fPulserColor );
+
+      if (!qepix.UpdateFFactorMethod())
+        *fLog << warn << GetDescriptor() 
+              << ": Cannot update Quantum efficiencies with the F-Factor Method" << endl;
+    }
+}
+
+
+// ------------------------------------------------------------------------
+//
+// Loop over pixels: 
+//
+// - Continue, if not MCalibrationChargeBlindPix::IsFluxInsidePlexiglassAvailable() and set:
+//                    MCalibrationQEPix::SetBlindPixelMethodValid(kFALSE,fPulserColor)
+//
+// - Calculate the quantum efficiency with the formula:
+// 
+//   QE =  Num.Phes / MCalibrationChargeBlindPix::GetFluxInsidePlexiglass() 
+//        / MGeomPix::GetA() * MCalibrationQECam::GetPlexiglassQE()
+//
+// - Set QE in MCalibrationQEPix::SetQEBlindPixel ( QE, fPulserColor );
+// - Set Variance of QE in  MCalibrationQEPix::SetQEBlindPixelVar ( Variance, fPulserColor );
+// - Set bit MCalibrationQEPix::SetBlindPixelMethodValid(kTRUE,fPulserColor) 
+//
+// - Call MCalibrationQEPix::UpdateBlindPixelMethod()
+//
+void MCalibrationChargeCalc::FinalizeBlindPixelQECam()
+{
+
+  const UInt_t npixels  = fGeom->GetNumPixels();
+  
+  //
+  //  With the knowledge of the overall photon flux, calculate the 
+  //  quantum efficiencies after the Blind Pixel and PIN Diode method
+  //
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
+
+      if (!fBlindPixel)
+        {
+          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+      
+      if (!fBlindPixel->IsFluxInsidePlexiglassAvailable())
+        {
+          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+      
+      MBadPixelsPix       &bad   =                   (*fBadPixels)[i];
+
+      if (!bad.IsUnsuitable (MBadPixelsPix::kUnsuitableRun))
+        {
+          qepix.SetBlindPixelMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
+      MGeomPix              &geo =                        (*fGeom)[i];
+      
+      const Float_t qe        = pix.GetPheFFactorMethod() 
+                             / fBlindPixel->GetFluxInsidePlexiglass() 
+                             / geo.GetA() 
+                            * fQECam->GetPlexiglassQE();
+
+      const Float_t qerelvar = fBlindPixel->GetFluxInsidePlexiglassRelVar() 
+                               + fQECam->GetPlexiglassQERelVar()
+                               + pix.GetPheFFactorMethodRelVar();
+
+      qepix.SetQEBlindPixel    ( qe            , fPulserColor );
+      qepix.SetQEBlindPixelVar ( qerelvar*qe*qe, fPulserColor );      
+      qepix.UpdateBlindPixelMethod();
+    }
+}
+
+// ------------------------------------------------------------------------
+//
+// Loop over pixels: 
+//
+// - Continue, if not MCalibrationChargePINDiode::IsFluxOutsidePlexiglassAvailable() and set:
+//                    MCalibrationQEPix::SetPINDiodeMethodValid(kFALSE,fPulserColor)
+//
+// - Calculate the quantum efficiency with the formula:
+// 
+//   QE =  Num.Phes / MCalibrationChargePINDiode::GetFluxOutsidePlexiglass() / MGeomPix::GetA()
+//
+// - Set QE in MCalibrationQEPix::SetQEPINDiode ( QE, fPulserColor );
+// - Set Variance of QE in  MCalibrationQEPix::SetQEPINDiodeVar ( Variance, fPulserColor );
+// - Set bit MCalibrationQEPix::SetPINDiodeMethodValid(kTRUE,fPulserColor) 
+//
+// - Call MCalibrationQEPix::UpdatePINDiodeMethod()
+//
+void MCalibrationChargeCalc::FinalizePINDiodeQECam()
+{
+  
+  const UInt_t npixels  = fGeom->GetNumPixels();
+
+  //
+  //  With the knowledge of the overall photon flux, calculate the 
+  //  quantum efficiencies after the PIN Diode method
+  //
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationQEPix   &qepix = (MCalibrationQEPix&)  (*fQECam)[i];
+
+      if (!fPINDiode)
+        {
+          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+      
+      if (!fPINDiode->IsFluxOutsidePlexiglassAvailable())
+        {
+          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+
+      MBadPixelsPix &bad  =  (*fBadPixels)[i];
+
+      if (!bad.IsUnsuitable (MBadPixelsPix::kUnsuitableRun))
+        {
+          qepix.SetPINDiodeMethodValid(kFALSE, fPulserColor);
+          continue;
+        }
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
+      MGeomPix              &geo =                        (*fGeom)[i];
+      
+      const Float_t qe       =  pix.GetPheFFactorMethod() 
+                               / fPINDiode->GetFluxOutsidePlexiglass() 
+                               / geo.GetA();
+
+      const Float_t qerelvar = fPINDiode->GetFluxOutsidePlexiglassRelVar() + pix.GetPheFFactorMethodRelVar();
+
+      qepix.SetQEPINDiode    ( qe            , fPulserColor );
+      qepix.SetQEPINDiodeVar ( qerelvar*qe*qe, fPulserColor );      
+      qepix.UpdateBlindPixelMethod();
+    }
+}
+
+// -----------------------------------------------------------------------------------------------
+//
+// Print out statistics about BadPixels of type UnsuitableType_t 
+// 
 void MCalibrationChargeCalc::PrintUnsuitable(MBadPixelsPix::UnsuitableType_t typ, const char *text) const 
 {
@@ -1278,4 +1391,8 @@
 }
 
+// -----------------------------------------------------------------------------------------------
+//
+// Print out statistics about BadPixels of type UncalibratedType_t 
+// 
 void MCalibrationChargeCalc::PrintUncalibrated(MBadPixelsPix::UncalibratedType_t typ, const char *text) const 
 {
