Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 5842)
+++ trunk/MagicSoft/Mars/Changelog	(revision 5843)
@@ -30,4 +30,7 @@
      - fixed one warning output which was not a real warning.
 
+   * mcalib/MCalibrationChargeCam.cc
+     - improved the function to calculate a mean conversion factor over 
+       the whole camera including a fit to the values.
 
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 5842)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 5843)
@@ -84,4 +84,6 @@
 
 #include <TOrdCollection.h>
+#include <TH1D.h>
+#include <TF1.h>
 
 #include "MLog.h"
@@ -96,4 +98,6 @@
 #include "MCalibrationQECam.h"
 #include "MCalibrationQEPix.h"
+
+#include "MHCamera.h"
 
 ClassImp(MCalibrationChargeCam);
@@ -650,7 +654,12 @@
   Int_t    nr    = 0;
 
+  MHCamera convcam(geom,"ConvFactors","Conversion Factors;Conv Factor [phot/FADC cnts];channels");
+
   for (int i=0; i<np; i++)
     {
       if (bad && (*bad)[i].IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
+        continue; 
+
+      if (bad && (*bad)[i].IsUncalibrated(MBadPixelsPix::kDeviatingNumPhes))
         continue; 
 
@@ -665,16 +674,56 @@
       
       const MCalibrationChargePix &pix = (MCalibrationChargePix&)(*this)[i];
-      const Float_t conv = pix.GetMeanConvFADC2Phe();
-      const Float_t qe   = qepix.GetQECascadesFFactor();
-
-      mean  += conv/qe;
-      mean2 += conv*conv/qe/qe;
+      const Float_t conv = pix.GetMeanConvFADC2Phe()/qepix.GetQECascadesFFactor();
+
+      mean  += conv;
+      mean2 += conv*conv;
       nr    ++;
-      
-    }
+
+      convcam.Fill(i,conv);
+      convcam.SetUsed(i);
+    }
+
+  Float_t mn  = nr   ? mean/nr : -1.;
+  Float_t sg  = nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0.;
+
+  const Int_t aidx = (Int_t)ai;
+
+  TH1D *h = convcam.ProjectionS(TArrayI(),TArrayI(1,&aidx),"_py",750);
+  h->SetDirectory(NULL);
+  h->Fit("gaus","Q");
+  TF1 *fit = h->GetFunction("gaus");
+  
+  Float_t ci2   = fit->GetChisquare();
+  Float_t sigma = fit->GetParameter(2);
+  
+  if (ci2 > 500. || sigma > sg)
+    {
+      h->Fit("gaus","QM");
+      fit = h->GetFunction("gaus");
+      
+      ci2   = fit->GetChisquare();
+      sigma = fit->GetParameter(2);
+    }
+  
+  const Int_t ndf = fit->GetNDF();
+
+  if (ci2 < 500. && sigma < sg && ndf > 2)
+    {
+      mn  = fit->GetParameter(1);
+      sg  = sigma;
+    }
+  
+  *fLog << inf << "Conversion Factors to photons area idx: " << aidx << ":" << endl;
+  *fLog << inf << "Mean: " << Form("%4.3f",mn) 
+        << "+-" << Form("%4.3f",fit->GetParError(1))
+        << "  Sigma: " << Form("%4.3f",sg) << "+-" << Form("%4.3f",fit->GetParError(2)) 
+        << "  Chisquare: " << Form("%4.3f",fit->GetChisquare()) << "  NDF  : " << ndf << endl;          
+  
+  delete h;
+  gROOT->GetListOfFunctions()->Remove(fit);
 
   TArrayF arr(2);
-  arr[0] = nr   ? mean/nr : -1.;
-  arr[1] = nr>1 ? TMath::Sqrt((mean2 - mean*mean/nr)/(nr-1)) : 0.;
+  arr[0] = mn;
+  arr[1] = sg;
 
   return arr;
