Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 4773)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 4774)
@@ -20,4 +20,7 @@
                                                  -*-*- END OF LINE -*-*-
  2004/08/27: Markus Gaug
+
+   * mcalib/MCalibrationChargeCalc.cc
+     - change treatment of pixels with kSigmaNotValid
 
    * macros/bootcampstandardanalysis.C 
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4773)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4774)
@@ -741,6 +741,4 @@
   PrintUncalibrated(MBadPixelsPix::kChargeRelErrNotValid,
                     Form("%s%2.1f%s","Signal Error bigger than ",fChargeRelErrLimit," times Mean Signal:   "));
-  PrintUncalibrated(MBadPixelsPix::kChargeSigmaNotValid, 
-                    "Signal Sigma smaller than Pedestal RMS:           ");
   PrintUncalibrated(MBadPixelsPix::kLoGainSaturation,    
                     "Pixels with Low Gain Saturation:                  ");
@@ -757,4 +755,6 @@
   *fLog << GetDescriptor() << ": Unreliable errors statistics:" << endl;  
 
+  PrintUncalibrated(MBadPixelsPix::kChargeSigmaNotValid, 
+                    "Signal Sigma smaller than Pedestal RMS:           ");
   PrintUncalibrated(MBadPixelsPix::kHiGainOscillating,   
                     "Pixels with changing Hi Gain signal over time:    ");
@@ -824,8 +824,11 @@
 // 
 // Calls MCalibrationChargePix::CalcReducedSigma() and sets flag:  MBadPixelsPix::kChargeIsPedestal 
-//       if not succesful.
-// 
-// Calls MCalibrationChargePix::CalcFFactorMethod() and sets flag: MBadPixelsPix::kDeviatingNumPhes) 
-//       if not succesful.
+//       and returns kFALSE if not succesful. 
+// 
+// Calls MCalibrationChargePix::CalcFFactor() and sets flag: MBadPixelsPix::kDeviatingNumPhes) 
+//       and returns kFALSE if not succesful.
+//
+// Calls MCalibrationChargePix::CalcConvFFactor()and sets flag: MBadPixelsPix::kDeviatingNumPhes) 
+//       and returns kFALSE if not succesful.
 //
 Bool_t MCalibrationChargeCalc::FinalizeCharges(MCalibrationChargePix &cal, MBadPixelsPix &bad, const char* what)
@@ -857,4 +860,5 @@
 	    << Form(" Ped. RMS: %5.2f in %s%4i",cal.GetPedRms(),what,cal.GetPixId()) << endl;
       bad.SetUncalibrated( MBadPixelsPix::kChargeSigmaNotValid );
+      return kFALSE;
     }
 
@@ -865,9 +869,9 @@
             << Form("         %4i",cal.GetPixId()) 
             << endl;
-      bad.SetUncalibrated(MBadPixelsPix::kChargeIsPedestal);
+      bad.SetUncalibrated( MBadPixelsPix::kChargeSigmaNotValid );
       return kFALSE;
     }
 
-  if (!cal.CalcFFactorMethod())
+  if (!cal.CalcFFactor())
     {
       *fLog << warn << GetDescriptor() 
@@ -879,4 +883,14 @@
     }
 
+  if (!cal.CalcConvFFactor())
+    {
+      *fLog << warn << GetDescriptor() 
+            << Form(": Could not calculate the Conv. FADC counts to Phes in %s:  ",what)
+            << Form("         %4i",cal.GetPixId()) 
+            << endl;
+      bad.SetUncalibrated(MBadPixelsPix::kDeviatingNumPhes);
+      return kFALSE;
+    }
+
   return kTRUE;
 }
@@ -889,5 +903,4 @@
 // - MBadPixelsPix::kChargeIsPedestal
 // - MBadPixelsPix::kChargeRelErrNotValid 
-// - MBadPixelsPix::kChargeSigmaNotValid 
 // - MBadPixelsPix::kMeanTimeInFirstBin 
 // - MBadPixelsPix::kMeanTimeInLast2Bins 
@@ -895,4 +908,7 @@
 //
 // - Call MCalibrationPix::SetExcluded() for the bad pixels
+//
+// Sets pixel to MBadPixelsPix::kUnreliableRun, if one of the following flags is set:
+// - MBadPixelsPix::kChargeSigmaNotValid 
 //
 void MCalibrationChargeCalc::FinalizeBadPixels()
@@ -914,7 +930,4 @@
         bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
  
-      if (bad.IsUncalibrated( MBadPixelsPix::kChargeSigmaNotValid ))
-        bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
-
       if (bad.IsUncalibrated( MBadPixelsPix::kMeanTimeInFirstBin ))
         bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
@@ -929,4 +942,6 @@
         pix.SetExcluded();
 
+      if (bad.IsUncalibrated( MBadPixelsPix::kChargeSigmaNotValid ))
+        bad.SetUnsuitable(   MBadPixelsPix::kUnreliableRun    );
     }
 }
@@ -936,13 +951,17 @@
 //
 // 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 
+//             Include only pixels which are not MBadPixelsPix::kUnsuitableRun nor 
+//             MBadPixelsPix::kChargeSigmaNotValid (see FinalizeBadPixels()) and set 
 //             MCalibrationChargePix::SetFFactorMethodValid(kFALSE) in that case.
 //              
-// Second loop: Get weighted mean number of photo-electrons and its RMS including 
+// Second loop: Get 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.
+//
+//              For the suitable pixels with flag MBadPixelsPix::kChargeSigmaNotValid 
+//              set the number of photo-electrons as the mean number of photo-electrons
+//              calculated in that area index.
 // 
 //              Set weighted mean and variance of photo-electrons per area index in: 
@@ -953,4 +972,8 @@
 //              average sector pixels of MCalibrationChargeCam (obtained from: 
 //              MCalibrationChargeCam::GetAverageSector() )
+// 
+//
+// Third loop: Set mean number of photo-electrons and its RMS in the pixels
+//             only excluded as: MBadPixelsPix::kChargeSigmaNotValid 
 // 
 Bool_t MCalibrationChargeCalc::FinalizeFFactorMethod()
@@ -999,6 +1022,6 @@
         }
 
-      // if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
-      //        continue;
+      if (bad.IsUncalibrated(MBadPixelsPix::kChargeSigmaNotValid))
+        continue;
       
       const Float_t nphe  = pix.GetPheFFactorMethod();
@@ -1109,4 +1132,9 @@
         continue;
 
+      MBadPixelsPix         &bad = (*fBadPixels)[i];
+
+      if (bad.IsUncalibrated(MBadPixelsPix::kChargeSigmaNotValid))
+        continue;
+
       const Float_t nvar  = pix.GetPheFFactorMethodVar();
 
@@ -1117,6 +1145,4 @@
         }
       
-      MBadPixelsPix         &bad = (*fBadPixels)[i];
-
       const Int_t   aidx   = (*fGeom)[i].GetAidx();
       const Int_t   sector = (*fGeom)[i].GetSector();
@@ -1232,4 +1258,36 @@
     }
 
+  //
+  // Third loop: Set mean number of photo-electrons and its RMS in the pixels
+  //             only excluded as: MBadPixelsPix::kChargeSigmaNotValid 
+  // 
+  for (UInt_t i=0; i<npixels; i++)
+    {
+      
+      MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCam)[i];
+      MBadPixelsPix         &bad = (*fBadPixels)[i];
+
+      if (!pix.IsFFactorMethodValid())
+        continue;
+      
+      const Int_t   aidx   = (*fGeom)[i].GetAidx();
+
+      if (bad.IsUncalibrated(MBadPixelsPix::kChargeSigmaNotValid))
+        {
+          MCalibrationChargePix &apix = (MCalibrationChargePix&)fCam->GetAverageArea(aidx);
+          pix.SetPheFFactorMethod   ( apix.GetPheFFactorMethod()    );
+          pix.SetPheFFactorMethodVar( apix.GetPheFFactorMethodVar() );
+          if (!pix.CalcConvFFactor())
+            {
+              *fLog << warn << GetDescriptor() 
+                    << ": Could not calculate the Conv. FADC counts to Phes in pixel:  "
+                    << Form("         %4i",pix.GetPixId()) 
+                    << endl;
+              bad.SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
+              bad.SetUnsuitable  ( MBadPixelsPix::kUnsuitableRun    );
+            }
+        }
+    }
+  
   return kTRUE;
 }
@@ -1523,10 +1581,4 @@
       const Float_t qe      = pix.GetPheFFactorMethod() / photons ;
 
-      if (!pix.CalcMeanFFactor( photons , avphotrelvar ))
-        {
-          (*fBadPixels)[i].SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
-          continue;
-        }
-
       const Float_t qerelvar = avphotrelvar +  pix.GetPheFFactorMethodRelVar();
 
@@ -1538,4 +1590,15 @@
         *fLog << warn << GetDescriptor() 
               << ": Cannot update Quantum efficiencies with the F-Factor Method" << endl;
+
+      //
+      // The following pixels are those with deviating sigma, but otherwise OK, 
+      // probably those with stars during the pedestal run, but not the cal. run.
+      //
+      if (!pix.CalcMeanFFactor( photons , avphotrelvar ))
+        {
+          (*fBadPixels)[i].SetUncalibrated( MBadPixelsPix::kDeviatingFFactor );
+          (*fBadPixels)[i].SetUnsuitable  ( MBadPixelsPix::kUnreliableRun    );
+          continue;
+        }
 
       const Int_t aidx = (*fGeom)[i].GetAidx();
@@ -1929,7 +1992,11 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Set the output file
+// 
 void MCalibrationChargeCalc::SetOutputFile(TString file)
 { 
-  fOutputFile        = file; 
+  fOutputFile = file; 
 }
 
@@ -1943,51 +2010,62 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Read the environment for the following data members:
+// - fChargeLimit
+// - fChargeErrLimit
+// - fChargeRelErrLimit
+// - fDebug
+// - fFFactorErrLimit
+// - fLambdaErrLimit
+// - fLambdaCheckErrLimit
+// - fPheErrLimit
+// 
 Int_t MCalibrationChargeCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
-    Bool_t rc = kFALSE;
-    if (IsEnvDefined(env, prefix, "ChargeLimit", print))
-    {
-        SetChargeLimit(GetEnvValue(env, prefix, "ChargeLimit", fChargeLimit));
-        rc = kTRUE;
-    }
-
-    if (IsEnvDefined(env, prefix, "ChargeErrLimit", print))
-    {
-        SetChargeErrLimit(GetEnvValue(env, prefix, "ChargeErrLimit", fChargeErrLimit));
-        rc = kTRUE;
-    }
-
-    if (IsEnvDefined(env, prefix, "ChargeRelErrLimit", print))
-    {
-        SetChargeRelErrLimit(GetEnvValue(env, prefix, "ChargeRelErrLimit", fChargeRelErrLimit));
-        rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "Debug", print))
-    {
-        SetDebug(GetEnvValue(env, prefix, "Debug", IsDebug()));
-        rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "FFactorErrLimit", print))
-    {
-        SetFFactorErrLimit(GetEnvValue(env, prefix, "FFactorErrLimit", fFFactorErrLimit));
-        rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "LambdaErrLimit", print))
-    {
-        SetLambdaErrLimit(GetEnvValue(env, prefix, "LambdaErrLimit", fLambdaErrLimit));
-        rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "LambdaCheckLimit", print))
-    {
-        SetLambdaCheckLimit(GetEnvValue(env, prefix, "LambdaCheckLimit", fLambdaCheckLimit));
-        rc = kTRUE;
-    }
-    if (IsEnvDefined(env, prefix, "PheErrLimit", print))
-    {
-        SetPheErrLimit(GetEnvValue(env, prefix, "PheErrLimit", fPheErrLimit));
-        rc = kTRUE;
-    }
-    // void SetPulserColor(const MCalibrationCam::PulserColor_t col) { fPulserColor = col; }
-
-    return rc;
-}
+
+  Bool_t rc = kFALSE;
+  if (IsEnvDefined(env, prefix, "ChargeLimit", print))
+    {
+      SetChargeLimit(GetEnvValue(env, prefix, "ChargeLimit", fChargeLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "ChargeErrLimit", print))
+    {
+      SetChargeErrLimit(GetEnvValue(env, prefix, "ChargeErrLimit", fChargeErrLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "ChargeRelErrLimit", print))
+    {
+      SetChargeRelErrLimit(GetEnvValue(env, prefix, "ChargeRelErrLimit", fChargeRelErrLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "Debug", print))
+    {
+      SetDebug(GetEnvValue(env, prefix, "Debug", IsDebug()));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "FFactorErrLimit", print))
+    {
+      SetFFactorErrLimit(GetEnvValue(env, prefix, "FFactorErrLimit", fFFactorErrLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "LambdaErrLimit", print))
+    {
+      SetLambdaErrLimit(GetEnvValue(env, prefix, "LambdaErrLimit", fLambdaErrLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "LambdaCheckLimit", print))
+    {
+      SetLambdaCheckLimit(GetEnvValue(env, prefix, "LambdaCheckLimit", fLambdaCheckLimit));
+      rc = kTRUE;
+    }
+  if (IsEnvDefined(env, prefix, "PheErrLimit", print))
+    {
+      SetPheErrLimit(GetEnvValue(env, prefix, "PheErrLimit", fPheErrLimit));
+      rc = kTRUE;
+    }
+  // void SetPulserColor(const MCalibrationCam::PulserColor_t col) { fPulserColor = col; }
+  
+  return rc;
+}
