Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc	(revision 3653)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc	(revision 3654)
@@ -38,7 +38,7 @@
 //
 // The following variables are calculated inside this class:
-// -  fLoGainPedRmsSquare and LoGainPedRmsSquareVar (see CalcLoGainPedestal())
-// -  fRSigmaSquare and fRSigmaSquareVar            (see CalcReducedSigma() )
-// -  fPheFFactorMethod and fPheFFactorMethodVar    see CalcFFactorMethod() )
+// -  fLoGainPedRmsSquare and fLoGainPedRmsSquareVar (see CalcLoGainPedestal())
+// -  fRSigmaSquare and fRSigmaSquareVar             (see CalcReducedSigma()  )
+// -  fPheFFactorMethod and fPheFFactorMethodVar     (see CalcFFactorMethod() )
 //
 // The following variables are set by MHCalibrationChargeCam:
@@ -52,5 +52,5 @@
 // - fConversionFFactorMethodVar, fConversionBlindPixelMethodVar 
 //   fConversionPINDiodeMethodVar and fConversionCombinedMethodVar
-// - fSigmaConversionFFactorMethodm, fSigmaConversionBlindPixelMethod
+// - fSigmaConversionFFactorMethod, fSigmaConversionBlindPixelMethod
 //   fSigmaConversionPINDiodeMethod and fSigmaConversionCombinedMethod  
 // - fTotalFFactorFFactorMethod, fTotalFFactorBlindPixelMethod   
@@ -95,6 +95,6 @@
 // - fCalibFlags to 0
 // - fConversionHiLo to fgConversionHiLo
-// - fConversionHiLoErr to fgConversionHiLoErr
-// - PheFFactorMethodLimit to fgPheFFactorMethodLimit
+// - fConversionHiLoVar to square of fgConversionHiLoErr
+// - fPheFFactorMethodLimit to fgPheFFactorMethodLimit
 // 
 // Calls:
@@ -180,5 +180,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the conversion factors Blind Pixel Method from outside (only for MC)
+// Set conversion factors Blind Pixel Method from outside (only for MC)
 //
 void MCalibrationChargePix::SetConversionBlindPixelMethod(Float_t c, Float_t err, Float_t sig)
@@ -192,5 +192,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the conversion factors Combined Method from outside (only for MC)
+// Set conversion factors Combined Method from outside (only for MC)
 //
 void MCalibrationChargePix::SetConversionCombinedMethod(Float_t c, Float_t err, Float_t sig)
@@ -204,5 +204,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the conversion factors F-Factor Method from outside (only for MC)
+// Set conversion factors F-Factor Method from outside (only for MC)
 //
 void MCalibrationChargePix::SetConversionFFactorMethod(Float_t c, Float_t err, Float_t sig)
@@ -215,5 +215,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the conversion factors PIN Diode Method from outside (only for MC)
+// Set conversion factors PIN Diode Method from outside (only for MC)
 //
 void MCalibrationChargePix::SetConversionPINDiodeMethod(Float_t c, Float_t err, Float_t sig)
@@ -226,5 +226,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the BlindPixelMethod Validity Bit from outside 
+// Set Blind Pixel Method Validity Bit from outside 
 //
 void MCalibrationChargePix::SetBlindPixelMethodValid(const Bool_t b )
@@ -235,5 +235,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the CombinedMethod Validity Bit from outside 
+// Set Combined Method Validity Bit from outside 
 //
 void MCalibrationChargePix::SetCombinedMethodValid(const Bool_t b )  
@@ -244,5 +244,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the FFactorMethod Validity Bit from outside 
+// Set F-Factor Method Validity Bit from outside 
 //
 void MCalibrationChargePix::SetFFactorMethodValid(const Bool_t b )
@@ -253,5 +253,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the PINDiodeMethod Validity Bit from outside 
+// Set PIN Diode Method Validity Bit from outside 
 //
 void MCalibrationChargePix::SetPINDiodeMethodValid(const Bool_t b )  
@@ -262,5 +262,5 @@
 // --------------------------------------------------------------------------
 //
-// Set the pedestals from outside
+// Set pedestals from outside (done by MCalibrationChargeCalc)
 //
 void MCalibrationChargePix::SetPedestal(const Float_t ped, const Float_t pedrms, const Float_t pederr)
@@ -306,5 +306,6 @@
 // Get the Error of the pedestals RMS: 
 // - Test bit kHiGainSaturation: 
-//   If yes, return kLoGainPedRms, else fPedRms
+//   If yes, return square root of (0.25*fLoGainPedRmsSquareVar/ fLoGainPedRmsSquare) (if greater than 0, otherwise -1.)
+//   If no , return square root of (fPedVar) (if greater than 0, otherwise -1.), divided by 2. 
 //
 Float_t  MCalibrationChargePix::GetPedRmsErr()  const
@@ -336,9 +337,18 @@
 //
 // Get the Error of the Low Gain Mean: 
-// Returns the quadratic sum of the relative low Gain Mean error and the 
-// the relative conversion High-to-Low error, mulitplied with GetLoGainMean()
+//
+// Returns -1 if the variable fLoGainMean or fLoGainMeanVar are smaller than 0.
+//
+// Returns the square root of the quadratic sum of the relative variances of 
+// the fLoGainMean and fConversionHiLo, mulitplied with GetLoGainMean()
 //
 Float_t MCalibrationChargePix::GetLoGainMeanErr()  const
 {
+  
+  if (fLoGainMeanVar < 0.)
+    return -1.;
+
+  if (fLoGainMean < 0.)
+    return -1.;
 
   const Float_t chargeRelVar     =  fLoGainMeanVar
@@ -361,9 +371,18 @@
 //
 // Get the Error of the Low Gain Sigma: 
-// Returns the quadratic sum of the relative low Gain Sigma error and the 
-// the relative conversion High-to-Low error, mulitplied with GetLoGainSigma()
+//
+// Returns -1 if the variable fLoGainSigma or fLoGainSigmaVar are smaller than 0.
+//
+// Returns the square root of the quadratic sum of the relative variances of 
+// the fLoGainSigma and fConversionHiLo, mulitplied with GetLoGainSigma()
 //
 Float_t MCalibrationChargePix::GetLoGainSigmaErr()  const
 {
+
+  if (fLoGainSigmaVar < 0.)
+    return -1.;
+
+  if (fLoGainSigma < 0.)
+    return -1.;
 
   const Float_t sigmaRelVar     =  fLoGainSigmaVar
@@ -396,8 +415,8 @@
 // - If fRSigmaSquareVar is smaller than 0 (i.e. has not yet been set), return -1.
 // - Calculate the absolute variance of the reduced sigma with the formula:
-//   sigma variance = 0.25 * fRSigmaSquareVar / fRSigmaSquare
+//   reduced sigma variance = 0.25 * fRSigmaSquareVar / fRSigmaSquare
 // - Test bit kHiGainSaturation: 
-//   If yes, returns the quadratic sum of the relative reduced Sigma error and the 
-//    the relative conversion High-to-Low error, mulitplied with GetRSigma()
+//   If yes, returns the square root of the quadratic sum of the relative variances of the 
+//           reduced sigma and fConversionHiLo, mulitplied with GetRSigma()
 //   Else returns the square root of rel. (0.25*fRSigmaSquareVar / fRSigmaSquare)
 //
@@ -593,5 +612,5 @@
 // - If fPedRms is smaller than 0 (i.e. has not yet been set), return kFALSE
 //
-// Calculate the reduced sigma:
+// Calculate the reduced sigma of the low-Gain FADC slices:
 // - Test bit IsHiGainSaturation() for the Sigma: 
 //   If yes, take fLoGainSigma and fLoGainSigmaVar 
@@ -600,12 +619,17 @@
 // - Test bit IsHiGainSaturation() for the pedRMS: 
 //   If yes, take fLoGainPedRmsSquare and fLoGainPedRmsSquareVar
-//   If no , take fPedRms and fPedRmsVar
+//   If no , take fPedRms and fPedVar
 //
 // - Calculate the reduced sigma with the formula:
 //   fRSigmaSquare = Sigma*Sigma - pedRMS*pedRMS
 // 
+// - If fRSigmaSquare is smaller than 0, give a warning and return kFALSE
+//
 // - Calculate the variance of the reduced sigma with the formula:
 //   fRSigmaSquareVar = 4.* (sigmaVar*Sigma*Sigma + pedRmsVar*pedRMS*pedRMS)
 //
+// A back-transformation to the corr. amplification factor of the High-Gain is done 
+// in GetRSigma() and GetRSigmaErr()
+//
 Bool_t MCalibrationChargePix::CalcReducedSigma()
 {
@@ -617,6 +641,6 @@
     return kFALSE;
 
-  const Float_t sigma    = IsHiGainSaturation() ? fLoGainSigma    : fHiGainSigma   ;
-  const Float_t sigmavar = IsHiGainSaturation() ? fLoGainSigmaVar : fHiGainSigmaVar;
+  const Float_t sigma           = IsHiGainSaturation() ? fLoGainSigma           : fHiGainSigma   ;
+  const Float_t sigmavar        = IsHiGainSaturation() ? fLoGainSigmaVar        : fHiGainSigmaVar;
   const Float_t pedRmsSquare    = IsHiGainSaturation() ? fLoGainPedRmsSquare    : fPedRms*fPedRms;
   const Float_t pedRmsSquareVar = IsHiGainSaturation() ? fLoGainPedRmsSquareVar : 0.25*fPedVar*pedRmsSquare;
@@ -654,16 +678,17 @@
 // - Test bit IsHiGainSaturation() for the pedRMS: 
 //   If yes, take fLoGainPedRmsSquare and fLoGainPedRmsSquareVar
-//   If no , take fPedRms and fPedRmsVar
+//   If no , take fPedRms and fPedVar
 //
 // - Calculate the number of photo-electrons with the formula:
-//   fPheFFactorMethod   = gkFFactor*gkFFactor * mean * mean  / fRSigmaSquare
+//   fPheFFactorMethod   = gkFFactor*gkFFactor * Mean * Mean  / fRSigmaSquare
 //
 // - Calculate the Variance on the photo-electrons with the formula:
 //   fPheFFactorMethodVar =  (  4. * gkFFactorErr * gkFFactorErr / ( gkFFactor * gkFFactor ) 
-//                            + 4. * fMeanVar                    / ( mean      * mean      )
-//                            + 4. * fRSigmaVar                  / ( fRSigma   * fRSigma   )
-//                            ) * fPheFFactor * fPheFFactor
-// - if fPheFFactorMethod is less than fPheFFactorMethodLimit, 
-//   Set kFFactorMethodValid to kFALSE and return kFALSE
+//                            + 4. * Mean Var.   / ( Mean * Mean )
+//                            + fRSigmaSquareVar / fRSigmaSquare
+//                            ) * fPheFFactorMethod * fPheFFactorMethod
+//
+// - If fPheFFactorMethod is less than fPheFFactorMethodLimit, 
+//   set kFFactorMethodValid to kFALSE and return kFALSE
 //   else: Set kFFactorMethodValid to kTRUE and return kTRUE
 //
@@ -722,16 +747,16 @@
 //  - elec. pedestal = gkElectronicPedRms * sqrt(logainsamples)
 // 
-// Calculate the LONS ped. RMS contribution in the high-gain 
+// Calculate the night sky background ped. RMS contribution ("NSB") in the high-gain 
 // from the high gain Pedestal RMS with the formula:
 // - HiGain NSB square      = fPedRms * fPedRms - elec.ped.* elec.ped.
 // - Var(HiGain NSB square) = fPedVar * fPedRms * fPedRms + 4.*elecPedRmsVar * elec.ped.* elec.ped.
 //
-// If PedRMS (LONS,lowgain) square is smaller than 0., set it to zero. (but not the error!)
-//
-// Convert the LONS ped. RMS contribution to the low-gain with the formula:
+// If HiGain NSB square is smaller than 0., set it to zero. (but not the error!)
+//
+// Convert the NSB ped. RMS contribution to the low-gain with the formula:
 // - LoGain NSB square      = - HiGain NSB square / (fConversionHiLo*fConversionHiLo)
 // - Var(LoGain NSB square) = ( Var(HiGain NSB square) / (HiGain NSB square * HiGain NSB square)
-//                              + GetConversionHiLoRelVar()   )
-//                            * LoGain NSB square * LoGain NSB square
+//                              + GetConversionHiLoRelVar()   
+//                            ) * LoGain NSB square * LoGain NSB square
 //
 // - Low Gain Ped RMS Square       = LoGain NSB square      + elec.ped. square
@@ -784,4 +809,10 @@
 }
  
+// --------------------------------------------------------------------------
+//
+// Get the relative variance of the conversion factor between higain and logain:
+// - If fConversionHiLo is smaller than 0, return -1.
+// - Else returns: fConversionHiLoVar / (fConversionHiLo * fConversionHiLo)
+//
 const Float_t MCalibrationChargePix::GetConversionHiLoRelVar() const 
 {
