Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4158)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 4159)
@@ -529,4 +529,6 @@
 // Call  FinalizeBlindPixelQECam() (fifth loop over pixels and areas)
 // Call  FinalizePINDiodeQECam() (sixth loop over pixels and areas)
+//
+// Call  FinalizeUnsuitablePixels()
 //  
 // Call MParContainer::SetReadyToSave() for fCam, fQECam, fBadPixels and 
@@ -609,4 +611,5 @@
 
       const MPedestalPix     &ped = fPedestals->GetAverageSector(sector);
+
       MCalibrationChargePix &pix = (MCalibrationChargePix&)fCam->GetAverageSector(sector);
       FinalizePedestals(ped,pix, 0);
@@ -928,5 +931,5 @@
         }
 
-      //      if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
+      // if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
       //        continue;
       
@@ -970,5 +973,5 @@
 
   //
-  // Second loop: Get weighted mean number of photo-electrons and its RMS excluding 
+  // Second loop: Get 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
@@ -1007,11 +1010,14 @@
         }
       
-      const Float_t weight = 1./nvar;
-      
-      areaweights   [aidx]   += weight;
-      areaphes      [aidx]   += weight*nphe;
+      //      const Float_t weight = 1./nvar;
+      //      areaweights   [aidx]   += weight;
+      //      areaphes      [aidx]   += weight*nphe;
+      areaweights   [aidx]   += nphe*nphe;
+      areaphes      [aidx]   += nphe;
       numareavalid  [aidx]   ++;
-      sectorweights [sector] += weight;
-      sectorphes    [sector] += weight*nphe;
+      //       sectorweights [sector] += weight;
+      //      sectorphes    [sector] += weight*nphe;
+      sectorweights [sector] += nphe*nphe;
+      sectorphes    [sector] += nphe;
       numsectorvalid[sector] ++;
     } 
@@ -1022,21 +1028,26 @@
       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: "
-                << " Sum of weights: "       << areaweights[aidx] 
-                << " Sum of weighted phes: " << areaphes[aidx]    << endl;
+                << " Mean number of phes: "  << areaphes[aidx] 
+                << " Variance: " << areaweights[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] );      
+            << Form("%5.3f%s%5.3f",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] );      
       apix.SetFFactorMethodValid ( kTRUE );
 
@@ -1046,4 +1057,8 @@
     {
 
+      sectorweights[sector] -=  sectorphes[sector]*sectorphes[sector]/numsectorvalid[sector];
+      sectorphes[sector]    /=  numsectorvalid[sector];
+      sectorweights[sector] /=  numsectorvalid[sector]-1.;
+
       MCalibrationChargePix &spix = (MCalibrationChargePix&)fCam->GetAverageSector(sector);
 
@@ -1051,6 +1066,6 @@
         {
           *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;
+                << " Mean number of phes: "  << sectorphes[sector] 
+                << " Variance: "             << sectorweights[sector]    << endl;
           spix.SetFFactorMethodValid(kFALSE);
           continue;
@@ -1060,9 +1075,10 @@
             << 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] );      
+            << Form("%5.3f%s%5.3f",sectorphes[sector]," +- ",TMath::Sqrt(sectorweights[sector])) << endl;
+
+      //      spix.SetPheFFactorMethod   ( sectorphes[sector]/ sectorweights[sector] );
+      //      spix.SetPheFFactorMethodVar(    1.        / sectorweights[sector] );      
+      spix.SetPheFFactorMethod   ( sectorphes[sector] );
+      spix.SetPheFFactorMethodVar( sectorweights[sector] / numsectorvalid[sector]);      
       spix.SetFFactorMethodValid ( kTRUE );
 
