Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 4602)
+++ trunk/MagicSoft/Mars/Changelog	(revision 4603)
@@ -28,4 +28,8 @@
      - new member function fFitFunc, can be set from outside and is 
        further passed onto the MHCalibrationChargeBlindPix's
+
+   * mcalib/MHCalibrationChargeCalc.[h,cc]
+     - improved readability of output
+     - set default fPheErrLimit to 4.5 sigma
 
    * mcalib/MCalibrateData.[h,cc]
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4603)
@@ -52,5 +52,5 @@
   MCerPhotEvt           *fCerPhotEvt;       // Cerenkov Photon Event used for calculation
   
-  UShort_t fCalibrationMode;
+  UShort_t fCalibrationMode;                // Flag defining the calibration mode (CalibrationMode_t)
   Byte_t   fPedestalFlag;                   // Flags defining to calibrate the pedestal each event or each run
   
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4603)
@@ -231,5 +231,5 @@
 const Float_t MCalibrationChargeCalc::fgLambdaErrLimit         = 0.2;
 const Float_t MCalibrationChargeCalc::fgLambdaCheckLimit       = 0.5;
-const Float_t MCalibrationChargeCalc::fgPheErrLimit            = 3.5;
+const Float_t MCalibrationChargeCalc::fgPheErrLimit            = 4.5;
 const Float_t MCalibrationChargeCalc::fgFFactorErrLimit        = 4.5;
 // --------------------------------------------------------------------------
@@ -582,4 +582,6 @@
     return kFALSE;
 
+  *fLog << endl;
+
   if (fPINDiode)
     if (!fPINDiode->IsValid())
@@ -606,4 +608,5 @@
       }
   
+  *fLog << endl;
   //
   // First loop over pixels, call FinalizePedestals and FinalizeCharges
@@ -627,8 +630,9 @@
       FinalizePedestals(ped,pix,aidx);
 
-      if (FinalizeCharges(pix,bad))
+      if (FinalizeCharges(pix,bad,"pixel  "))
         nvalid++;
     }
-  
+
+  *fLog << endl;  
   //
   // The Michele check ...
@@ -651,7 +655,9 @@
 
       FinalizePedestals(ped,pix,aidx);
-      FinalizeCharges(pix, fCam->GetAverageBadArea(aidx));
-    }
-  
+      FinalizeCharges(pix, fCam->GetAverageBadArea(aidx),"area id");
+    }
+  
+  *fLog << endl;
+
   for (UInt_t sector=0; sector<fGeom->GetNumSectors(); sector++)
     {
@@ -661,7 +667,8 @@
       MCalibrationChargePix &pix = (MCalibrationChargePix&)fCam->GetAverageSector(sector);
       FinalizePedestals(ped,pix, 0);
-      FinalizeCharges(pix, fCam->GetAverageBadSector(sector));
-    }
-  
+    }
+  
+  *fLog << endl;
+
   //
   // Finalize Bad Pixels
@@ -681,4 +688,5 @@
     fCam->SetFFactorMethodValid(kTRUE);
   
+  *fLog << endl;
   // 
   // Finalize Blind Pixel
@@ -821,5 +829,5 @@
 //       if not succesful.
 //
-Bool_t MCalibrationChargeCalc::FinalizeCharges(MCalibrationChargePix &cal, MBadPixelsPix &bad)
+Bool_t MCalibrationChargeCalc::FinalizeCharges(MCalibrationChargePix &cal, MBadPixelsPix &bad, const char* what)
 {
 
@@ -829,6 +837,7 @@
   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;
+      *fLog << warn << GetDescriptor() 
+            << Form(": Fitted Charge: %5.2f is smaller than %2.1f",cal.GetMean(),fChargeLimit)
+            << Form("   Pedestal RMS: %5.2f in %s%4i",cal.GetPedRms(),what,cal.GetPixId()) << endl;
       bad.SetUncalibrated( MBadPixelsPix::kChargeIsPedestal);
     }
@@ -836,7 +845,7 @@
    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;
+      *fLog << warn << GetDescriptor() 
+            << Form(": Fitted Charge: %4.2f is smaller than %2.1f",cal.GetMean(),fChargeRelErrLimit) 
+            << Form(" times its error: %4.2f in %s%4i",cal.GetMeanErr(),what,cal.GetPixId()) << endl;
       bad.SetUncalibrated( MBadPixelsPix::kChargeRelErrNotValid );
     }
@@ -844,6 +853,7 @@
   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;
+      *fLog << warn << GetDescriptor() 
+            << Form(": Sigma of Fitted Charge: %6.2f is smaller than",cal.GetSigma())
+	    << Form(" Ped. RMS: %5.2f in %s%4i",cal.GetPedRms(),what,cal.GetPixId()) << endl;
       bad.SetUncalibrated( MBadPixelsPix::kChargeSigmaNotValid );
     }
@@ -852,5 +862,7 @@
     {
       *fLog << warn << GetDescriptor() 
-            << ": Could not calculate reduced sigmas of pixel: " << cal.GetPixId() << endl;
+            << Form(": Could not calculate the reduced sigma in %s:               ",what)
+            << Form("         %4i",cal.GetPixId()) 
+            << endl;
       bad.SetUncalibrated(MBadPixelsPix::kChargeIsPedestal);
       return kFALSE;
@@ -860,5 +872,7 @@
     {
       *fLog << warn << GetDescriptor() 
-            << ": Could not calculate F-Factor of pixel: " << cal.GetPixId() << endl;
+            << Form(": Could not calculate the F-Factor in %s:                    ",what)
+            << Form("         %4i",cal.GetPixId()) 
+            << endl;
       bad.SetUncalibrated(MBadPixelsPix::kDeviatingNumPhes);
       return kFALSE;
@@ -1008,7 +1022,17 @@
         }
 
-      areavars[i] = (areavars[i] - areaphes[i]*areaphes[i]/numareavalid[i]) / (numareavalid[i]-1.);
-      areaphes[i] = areaphes[i] / numareavalid[i];
-
+      if (numareavalid[i] == 1)
+        areavars[i] = 0.;
+      else if (numareavalid[i] == 0)
+        {
+          areaphes[i]    = -1.;
+          areaweights[i] = -1.;
+        }
+      else
+        {
+          areavars[i] = (areavars[i] - areaphes[i]*areaphes[i]/numareavalid[i]) / (numareavalid[i]-1);
+          areaphes[i] = areaphes[i] / numareavalid[i];
+        }
+      
       if (areavars[i] < 0.)
         {
@@ -1029,4 +1053,7 @@
       const Float_t sigma = hist->GetFunction("gaus")->GetParameter(2);
       const Int_t   ndf   = hist->GetFunction("gaus")->GetNDF();
+
+      if (IsDebug())
+        camphes.DrawClone();
 
       if (ndf < 2)
@@ -1053,9 +1080,7 @@
         }
       
-      *fLog << inf << endl;
-      *fLog << inf << GetDescriptor() << ": Mean Number of photo-electrons " 
-            << "in the camera with area index: " << i << ": " 
-            << Form("%4.2f%s%4.2f",mean,"+-",sigma) << endl;
-      *fLog << inf << endl;
+      *fLog << inf << GetDescriptor() << ": Mean number of photo-electrons " 
+            << "with area idx " << i << ": " 
+            << Form("%7.2f+-%6.2f",mean,sigma) << endl;
 
       lowlim  [i] = mean  - fPheErrLimit*sigma;
@@ -1064,4 +1089,6 @@
       delete hist;
     }
+
+  *fLog << endl;
 
   memset(numareavalid,0,nareas*sizeof(Int_t));
@@ -1094,11 +1121,12 @@
       const Int_t   aidx   = (*fGeom)[i].GetAidx();
       const Int_t   sector = (*fGeom)[i].GetSector();
+      const Float_t area   = (*fGeom)[i].GetA();
       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;
+          *fLog << warn << GetDescriptor() << ": Number of phes: " 
+                << Form("%7.2f out of %3.1f sigma limit: ",nphe,fPheErrLimit)
+                << Form("[%7.2f,%7.2f] pixel%4i",lowlim[aidx],upplim[aidx],i) << endl;
           bad.SetUncalibrated( MBadPixelsPix::kDeviatingNumPhes );
           bad.SetUnsuitable  ( MBadPixelsPix::kUnsuitableRun    );
@@ -1107,7 +1135,4 @@
         }
       
-      //      const Float_t weight = 1./nvar;
-      //      areaweights   [aidx]   += weight;
-      //      areaphes      [aidx]   += weight*nphe;
       areaweights   [aidx]   += nphe*nphe;
       areaphes      [aidx]   += nphe;
@@ -1116,11 +1141,12 @@
       if (aidx == 0)
         fNumInnerFFactorMethodUsed++;
-      //       sectorweights [sector] += weight;
-      //      sectorphes    [sector] += weight*nphe;
-      sectorweights [sector] += nphe*nphe;
-      sectorphes    [sector] += nphe;
+
+      sectorweights [sector] += nphe*nphe/area/area;
+      sectorphes    [sector] += nphe/area;
       numsectorvalid[sector] ++;
     } 
 
+  *fLog << endl;
+
   for (UInt_t aidx=0; aidx<nareas; aidx++)
     {
@@ -1128,12 +1154,23 @@
       MCalibrationChargePix &apix = (MCalibrationChargePix&)fCam->GetAverageArea(aidx);
 
-      areaweights[aidx] -=  areaphes[aidx]*areaphes[aidx]/numareavalid[aidx];
-      areaphes[aidx]    /=  numareavalid[aidx];
-      areaweights[aidx] /=  numareavalid[aidx]-1.;
-      
-      if (areaweights[aidx] <= 0. || areaphes[aidx] <= 0.)
-        {
-          *fLog << warn << " Mean number of phe's from area index " << aidx << " cannot be calculated: "
-                << " Mean number of phes: "  << areaphes[aidx] 
+      if (numareavalid[aidx] == 1)
+        areaweights[aidx] = 0.;
+      else if (numareavalid[aidx] == 0)
+        {
+          areaphes[aidx]    = -1.;
+          areaweights[aidx] = -1.;
+        }
+      else
+        {
+          areaweights[aidx] = (areaweights[aidx] - areaphes[aidx]*areaphes[aidx]/numareavalid[aidx])
+                            / (numareavalid[aidx]-1);
+          areaphes[aidx]    /=  numareavalid[aidx];
+        }
+      
+      if (areaweights[aidx] < 0. || areaphes[aidx] <= 0.)
+        {
+          *fLog << warn << GetDescriptor() 
+                << ": Mean number phes from area index " << aidx << " could not be calculated: "
+                << " Mean: "  << areaphes[aidx] 
                 << " Variance: " << areaweights[aidx]    << endl;
           apix.SetFFactorMethodValid(kFALSE);
@@ -1141,11 +1178,7 @@
         }
       
-      *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]," +- ",TMath::Sqrt(areaweights[aidx])) << endl;
+      *fLog << inf << "Average total number phes in area idx " << aidx << ": " 
+            << Form("%7.2f%s%6.2f",areaphes[aidx]," +- ",TMath::Sqrt(areaweights[aidx])) << endl;
                     
-      //      apix.SetPheFFactorMethod   ( areaphes[aidx]/ areaweights[aidx] );
-      //      apix.SetPheFFactorMethodVar(    1.         / areaweights[aidx] );      
       apix.SetPheFFactorMethod   ( areaphes[aidx] );
       apix.SetPheFFactorMethodVar( areaweights[aidx] / numareavalid[aidx] );      
@@ -1154,17 +1187,32 @@
     }
 
+  *fLog << endl;
+
   for (UInt_t sector=0; sector<nsectors; sector++)
     {
 
-      sectorweights[sector] -=  sectorphes[sector]*sectorphes[sector]/numsectorvalid[sector];
-      sectorphes[sector]    /=  numsectorvalid[sector];
-      sectorweights[sector] /=  numsectorvalid[sector]-1.;
-
+      if (numsectorvalid[sector] == 1)
+        sectorweights[sector] = 0.;
+      else if (numsectorvalid[sector] == 0)
+        {
+          sectorphes[sector]    = -1.;
+          sectorweights[sector] = -1.;
+        }
+      else
+        {
+          sectorweights[sector] = (sectorweights[sector] 
+                                   - sectorphes[sector]*sectorphes[sector]/numsectorvalid[sector]
+                                  ) 
+                                / (numsectorvalid[sector]-1.);
+          sectorphes[sector]     /=  numsectorvalid[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: "
-                << " Mean number of phes: "  << sectorphes[sector] 
+      if (sectorweights[sector] < 0. || sectorphes[sector] <= 0.)
+        {
+          *fLog << warn << GetDescriptor() 
+                <<": Mean number phes per area for sector " << sector << " could not be calculated: "
+                << " Mean: "  << sectorphes[sector] 
                 << " Variance: "             << sectorweights[sector]    << endl;
           spix.SetFFactorMethodValid(kFALSE);
@@ -1172,11 +1220,8 @@
         }
 
-      *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]," +- ",TMath::Sqrt(sectorweights[sector])) << endl;
-
-      //      spix.SetPheFFactorMethod   ( sectorphes[sector]/ sectorweights[sector] );
-      //      spix.SetPheFFactorMethodVar(    1.        / sectorweights[sector] );      
+      *fLog << inf << "Average number phes per area in sector " << sector << ": " 
+            << Form("%5.2f+-%4.2f  [phe/mm^2]",sectorphes[sector],TMath::Sqrt(sectorweights[sector])) 
+            << endl;
+
       spix.SetPheFFactorMethod   ( sectorphes[sector] );
       spix.SetPheFFactorMethodVar( sectorweights[sector] / numsectorvalid[sector]);      
@@ -1453,4 +1498,7 @@
       const Int_t   ndf   = hist->GetFunction("gaus")->GetNDF();
 
+      if (IsDebug())
+        camffactor.DrawClone();
+
       if (ndf < 2)
         {
@@ -1476,15 +1524,15 @@
         }
 
-      *fLog << inf << endl;
-      *fLog << inf << GetDescriptor() << ": Mean F-Factor " 
-            << "in the camera with area index: " << i << ": " 
-            << Form("%4.2f%s%4.2f",mean,"+-",sigma) << endl;
-      *fLog << inf << endl;
-
-      lowlim  [i] = mean  - fFFactorErrLimit*sigma;
+      *fLog << inf << GetDescriptor() << ": Mean overall F-Factor (photons to FADC counts) " 
+            << "with area index: " << i << ": " 
+            << Form("%4.2f+-%4.2f",mean,sigma) << endl;
+
+      lowlim  [i] = 1.1;
       upplim  [i] = mean  + fFFactorErrLimit*sigma;
 
       delete hist;
     }
+  
+  *fLog << endl;
   
   for (UInt_t i=0; i<npixels; i++)
@@ -1502,7 +1550,7 @@
       if ( ffactor < lowlim[aidx] || ffactor > upplim[aidx] )
         {
-          *fLog << warn << GetDescriptor() << ": Deviating F-Factor: " 
-                << Form("%4.2f",ffactor) << " out of accepted limits: [" 
-                << Form("%4.2f%s%4.2f",lowlim[aidx],",",upplim[aidx]) << "] in pixel " << i << endl;
+          *fLog << warn << GetDescriptor() << ": Overall F-Factor: " 
+                << Form("%5.2f out of %3.1f sigma limit: ",ffactor,fFFactorErrLimit)
+                << Form("[%5.2f,%5.2f] pixel %4i",lowlim[aidx],upplim[aidx],i) << endl;
           bad.SetUncalibrated( MBadPixelsPix::kDeviatingFFactor );
           bad.SetUnsuitable  ( MBadPixelsPix::kUnsuitableRun    );
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.h	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.h	(revision 4603)
@@ -53,6 +53,6 @@
   static const Float_t fgLambdaCheckLimit;     //! Default for fLambdaCheckLimit   (now set to: 0.2)
   static const Float_t fgLambdaErrLimit;       //! Default for fLabmdaErrLimit     (now set to: 0.5)
-  static const Float_t fgPheErrLimit;          //! Default for fPheErrLimit        (now set to: 4.)
-  static const Float_t fgFFactorErrLimit;      //! Default for fFFactorErrLimit    (now set to: 4.)
+  static const Float_t fgPheErrLimit;          //! Default for fPheErrLimit        (now set to: 4.5)
+  static const Float_t fgFFactorErrLimit;      //! Default for fFFactorErrLimit    (now set to: 4.5)
  
   // Variables
@@ -95,5 +95,5 @@
   const char* GetOutputFile();
   void   FinalizePedestals       ( const MPedestalPix    &ped, MCalibrationChargePix &cal, const Int_t aidx );
-  Bool_t FinalizeCharges         ( MCalibrationChargePix &cal, MBadPixelsPix &bad                           );
+  Bool_t FinalizeCharges         ( MCalibrationChargePix &cal, MBadPixelsPix &bad, const char* what);
   Bool_t FinalizePINDiode        ();
   Bool_t FinalizeBlindPixel      ();
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 4603)
@@ -130,5 +130,5 @@
   fAverageAreas     = new TClonesArray("MCalibrationChargePix",1);
   fAverageSectors   = new TClonesArray("MCalibrationChargePix",1);
-  
+
   Clear();
 }
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.cc	(revision 4603)
@@ -723,7 +723,8 @@
   if (fRSigmaSquare <= 0.)
     {
-      *fLog << warn 
-            << "WARNING: Cannot calculate the reduced sigma: smaller than 0 in pixel " 
-            << fPixId << endl;
+      if (IsDebug())
+        *fLog << warn 
+              << "WARNING: Cannot calculate the reduced sigma: smaller than 0 in pixel " 
+              << fPixId << endl;
       return kFALSE;
     }
@@ -863,7 +864,6 @@
   if (convrelvar > limit || convrelvar < 0.)
     {
-      *fLog << warn << GetDescriptor() << ": Conversion F-Factor Method Rel. Variance: " 
-            << convrelvar << " above limits of: [0," << Form("%3.2f",limit)
-            << "] in pixel: " << fPixId << endl;
+      *fLog << warn << GetDescriptor() << ":  Conv. F-Factor Method Rel. Var.: " 
+            << Form("%4.3f out of limits: [0,%3.2f] in pixel:%4i",convrelvar,limit,fPixId) << endl;
       return kFALSE;
     }
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationCam.cc	(revision 4602)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationCam.cc	(revision 4603)
@@ -646,5 +646,5 @@
   if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
     hist.SetExcluded();
-  
+
   hist.InitBins();
   hist.ChangeHistId(i);
