Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 6748)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 6749)
@@ -29,4 +29,6 @@
      - set version number to 3
 
+   * mcalib/MCalibrationChargeCam.[h,cc]
+     - added new function GetAvergedConvFADC2PhePerArea for the datacheck
 
  2005/03/04 Abelardo Moralejo
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 6748)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.cc	(revision 6749)
@@ -761,4 +761,132 @@
 // --------------------------------------------------------------------------
 //
+// Calculates the average conversion factor FADC counts to equiv. photo-electrons for pixel sizes. 
+// The geometry container is used to get the necessary
+// geometry information (area index). The MCalibrationQECam container is necessary for 
+// the quantum efficiency information. 
+// If the bad pixel container is given all pixels which have the flag 'kUnsuitableRun' are ignored
+// in the calculation of the size average.
+//
+// Returns a TArrayF of dimension 2: 
+// arr[0]: averaged conversion factors (default: -1.)
+// arr[1]: Error (rms) of averaged conversion factors (default: 0.)
+//
+TArrayF MCalibrationChargeCam::GetAveragedConvFADC2PhePerArea  ( const MGeomCam &geom, const MCalibrationQECam &qecam,
+                                           const UInt_t ai,  MBadPixelsCam *bad)
+{
+
+  const Int_t np = GetSize();
+
+  Double_t mean  = 0.;
+  Double_t mean2 = 0.;
+  Int_t    nr    = 0;
+
+  MHCamera convcam(geom,"ConvFactors","Conversion Factors;Conv Factor [phe/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; 
+
+      const UInt_t aidx = geom[i].GetAidx();
+      
+      if (ai != aidx)
+        continue;
+      
+      const MCalibrationQEPix &qepix = (MCalibrationQEPix&)qecam[i];
+      if (!qepix.IsAverageQEFFactorAvailable())
+        continue;
+      
+      const MCalibrationChargePix &pix = (MCalibrationChargePix&)(*this)[i];
+      const Float_t conv = pix.GetMeanConvFADC2Phe()/qepix.GetQECascadesFFactor()*MCalibrationQEPix::gkDefaultAverageQE;
+
+      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);
+
+  TF1 *fit = NULL;
+
+  if (geom.InheritsFrom("MGeomCamMagic"))
+    {
+
+      fit = new TF1("fit","gaus",0.01,1.);
+      
+      // Fix the ranges, as found by Nadia
+      if(aidx == 0)
+        {h->Fit("fit","REQ", "",0.07,0.3);}
+      else
+        {h->Fit("fit","REQ", "",0.15,1.0);}
+    }
+  else
+    {
+      h->Fit("gaus","Q");
+      fit = h->GetFunction("gaus");
+    }
+
+  Float_t ci2   = fit->GetChisquare();
+  Float_t sigma = fit->GetParameter(2);
+  
+  if (ci2 > 500. || sigma > sg)
+    {
+      if (geom.InheritsFrom("MGeomCamMagic"))
+        {
+          // Fix the ranges, as found by Nadia
+          if(aidx == 0)
+            {h->Fit("fit","REQ", "",0.07,0.3);}
+          else
+            {h->Fit("fit","REQ", "",0.15,1.0);}
+        }
+      else
+        {
+          h->Fit("gaus","MREQ");
+          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 equiv. photo-electrons 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 fit;
+  delete h;
+  gROOT->GetListOfFunctions()->Remove(fit);
+
+  TArrayF arr(2);
+  arr[0] = mn;
+  arr[1] = sg;
+
+  return arr;
+}
+
+// --------------------------------------------------------------------------
+//
 // Calculates the average conversion factor FADC counts to photons for camera sectors. 
 // The geometry container is used to get the necessary
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h	(revision 6748)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCam.h	(revision 6749)
@@ -46,4 +46,6 @@
   TArrayF GetAveragedConvFADC2PhotPerArea    (const MGeomCam &geom, const MCalibrationQECam &qecam,
                                               const UInt_t ai=0,  MBadPixelsCam *bad=NULL);
+  TArrayF GetAveragedConvFADC2PhePerArea     (const MGeomCam &geom, const MCalibrationQECam &qecam,
+                                              const UInt_t ai=0,  MBadPixelsCam *bad=NULL);
   TArrayF GetAveragedConvFADC2PhotPerSector  (const MGeomCam &geom, const MCalibrationQECam &qecam,
                                               const UInt_t sec=0, MBadPixelsCam *bad=NULL);
