Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 4580)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 4581)
@@ -230,4 +230,19 @@
   if (TestFlag(kRun))
   {
+    if (!CalibratePedestal(fPedestal,fPedPhot)) return kFALSE;
+  }
+
+  return kTRUE;
+}
+
+
+
+// --------------------------------------------------------------------------
+//
+// Calibrate the Pedestal values
+// 
+//
+Bool_t MCalibrateData::CalibratePedestal(MPedestalCam *pedADCCam, MPedPhotCam *pedPhotCam)
+{
   //---------------------------------------------
   // fill MPedPhot container using the informations from
@@ -237,5 +252,5 @@
 
   // is pixid equal to pixidx ?
-  if (    (Int_t)(fPedestal->GetSize()) != fSignals->GetSize())
+  if (    (Int_t)(pedADCCam->GetSize()) != fSignals->GetSize())
   {
     *fLog << err << "MCalibrateData::ReInit(); sizes of MPedestalCam and MCalibrationCam are different" 
@@ -243,13 +258,9 @@
   } 
 
-  *fLog << all << "MCalibrateData::ReInit(); fill MPedPhotCam container"
-        << endl;
-  *fLog << all << "     fNumUsedHiGainADCSlices = " 
-        <<  fNumUsedHiGainFADCSlices << endl;
-  *fLog << all << "     pixid, calibrationConversionFactor, ped, pedRMS, pedphot, pedphotRMS :"
-        << endl;
-  for (Int_t pixid=0; pixid<fPedestal->GetSize(); pixid++)
-  {
-    const MPedestalPix    &ped = (*fPedestal)[pixid];
+ 
+
+  for (Int_t pixid=0; pixid<pedADCCam->GetSize(); pixid++)
+  {
+    const MPedestalPix    &ped = (*pedADCCam)[pixid];
 
     // pedestals/(used FADC slices)   in [ADC] counts
@@ -263,8 +274,9 @@
     Float_t hiloconverr;
     Float_t calibConv;
+    Float_t calibConvVar;
     Float_t calibFFactor;
 
     if ( !GetConversionFactor(pixid, hiloconv, hiloconverr,
-			      calibConv, calibFFactor ))
+			      calibConv, calibConvVar, calibFFactor ))
       continue;
 
@@ -275,19 +287,21 @@
     Float_t pedphotrms = pedrms * calibConv;
 
-    (*fPedPhot)[pixid].Set(pedphot, pedphotrms);
-
-    *fLog << all << pixid << ",  " << calibConv << ",  "
-          << ped.GetPedestal() << ",  " << ped.GetPedestalRms() << ",  " 
-          << pedphot << ",  " << pedphotrms << endl;
+    (*pedPhotCam)[pixid].Set(pedphot, pedphotrms);
+
   }
 
   //---------------------------------------------
 
-  fPedPhot->SetReadyToSave();
-  }
+  pedPhotCam->SetReadyToSave();
 
   return kTRUE;
 }
 
+
+
+
+
+
+
 // --------------------------------------------------------------------------
 //
@@ -297,5 +311,5 @@
 Bool_t MCalibrateData::GetConversionFactor(UInt_t pixidx,
  Float_t &hiloconv, Float_t &hiloconverr,
- Float_t &calibConv, Float_t &calibFFactor)
+ Float_t &calibConv, Float_t &calibConvVar, Float_t &calibFFactor)
 {
 
@@ -308,39 +322,116 @@
   hiloconverr  = 0.;
   calibConv    = 1.;
+  calibConvVar  = 0.;
   calibFFactor = 0.;
+  Float_t calibQE       = 1.;
+  Float_t calibQEVar    = 0.;
+
+  Float_t avMean        = 1.;
+  Float_t avMeanRelVar  = 0.;
+
+
+
+  if (fCalibrationMode == kFlatCharge)
+  {
+    MCalibrationChargePix &avpix = (MCalibrationChargePix&)fCalibrations->GetAverageArea(0);
+    avMean       =  avpix.GetMean();
+    avMeanRelVar =  avpix.GetMeanRelVar();
+  }
+
 
   if(fCalibrationMode!=kNone)
   {
-    MCalibrationChargePix &pix   = (MCalibrationChargePix&)(*fCalibrations)[pixidx];       
-    MCalibrationQEPix     &qepix = (MCalibrationQEPix&)    (*fQEs)         [pixidx];       
-    MBadPixelsPix         &bad   = (*fBadPixels)[pixidx];
-    
-    if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
-      return kFALSE;
-    
-    hiloconv   = pix.GetConversionHiLo();
+	  
+    MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCalibrations)[pixidx];
+
+    hiloconv   = pix.GetConversionHiLo   ();
     hiloconverr= pix.GetConversionHiLoErr();
-
-    const Float_t pheConv = pix.GetMeanConvFADC2Phe();
-    calibFFactor          = pix.GetMeanFFactorFADC2Phot();
-    Float_t calibQE       = 1.;
-    
+	  
+    if (fBadPixels)
+    {
+      MBadPixelsPix         &bad = (*fBadPixels)[pixidx];
+      if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
+	return kFALSE;
+    }
+            
+    calibConv      = pix.GetMeanConvFADC2Phe();
+    calibConvVar   = pix.GetMeanConvFADC2PheVar();
+    calibFFactor   = pix.GetMeanFFactorFADC2Phot();
+
+    MCalibrationQEPix &qe  = (MCalibrationQEPix&) (*fQEs)[pixidx];
+          
     switch(fCalibrationMode)
     {
-      case kBlindPixel:
-        calibQE      = qepix.GetQECascadesBlindPixel   ( zenith );
-        //        calibQEVar   = qepix.GetQECascadesBlindPixelVar( zenith );
+    case kFlatCharge:
+      calibConv        = avMean / pix.GetMean() / fCam->GetPixRatio(pixidx) ;
+      calibConvVar     = (avMeanRelVar + pix.GetMeanRelVar()) * calibConv * calibConv;
+      if (pix.IsFFactorMethodValid())
+      {
+	const Float_t convmin1 = qe.GetQECascadesFFactor(zenith)/pix.GetMeanConvFADC2Phe();
+	if (convmin1 > 0)
+	  calibFFactor *= TMath::Sqrt(convmin1);
+	else
+	  calibFFactor = -1.;
+      }
       break;
-      case kFfactor:
-        calibQE      = qepix.GetQECascadesFFactor   ( zenith );
-        //        calibQEVar   = qepix.GetQECascadesFFactorVar( zenith );
+    case kBlindPixel:
+      if (qe.IsBlindPixelMethodValid())
+      {
+	calibQE      = qe.GetQECascadesBlindPixel   ( zenith );
+	calibQEVar   = qe.GetQECascadesBlindPixelVar( zenith );
+      }
+      else
+	return kFALSE;
       break;
-      default:
-      *fLog << warn << "MCalibrateData::GetConversionFactor; Warning: Calibration mode value ("<<fCalibrationMode<<") not known" << endl;
-	      break;
-    }
-    calibConv = pheConv / calibQE;
-
-  }
+    case kPinDiode:
+      if (qe.IsPINDiodeMethodValid())
+      {
+	calibQE      = qe.GetQECascadesPINDiode   ( zenith );
+	calibQEVar   = qe.GetQECascadesPINDiodeVar( zenith );
+      }
+      else
+	return kFALSE;
+      break;
+    case kFfactor:
+      if (pix.IsFFactorMethodValid())
+      {
+	calibQE      = qe.GetQECascadesFFactor   ( zenith );
+	calibQEVar   = qe.GetQECascadesFFactorVar( zenith );
+      }
+      else
+	return kFALSE;
+      break;
+    case kCombined:
+      if (qe.IsCombinedMethodValid())
+      {
+	calibQE      = qe.GetQECascadesCombined   ( zenith );
+	calibQEVar   = qe.GetQECascadesCombinedVar( zenith );
+      }
+      else
+	return kFALSE;
+      break;
+    case kDummy:
+      hiloconv     = 1.;
+      hiloconverr  = 0.;
+      calibQE      = 1.;
+      calibQEVar   = 0.;
+      break;
+              
+    } /* switch calibration mode */
+  } /* if(fCalibrationMode!=kNone) */
+  else
+  {
+    hiloconv       = 1.;
+    hiloconverr    = 0.;
+    calibConv      = 1./fCam->GetPixRatio(pixidx);
+    calibConvVar   = 0.;
+    calibFFactor   = 0.;
+    calibQE        = 1.;
+    calibQEVar     = 0.;
+  }     
+  
+  calibConv    /= calibQE;
+  calibConvVar /= calibQE;
+
 
   return kTRUE;
@@ -373,9 +464,10 @@
   Float_t calibrationConversionFactor;
   Float_t calibrationConversionFactorErr;
-  
+  Float_t calibFFactor;
+
   for (UInt_t pixidx=0; pixidx<npix; pixidx++)
     {
       if ( !GetConversionFactor(pixidx, hiloconv, hiloconverr,
-	    calibrationConversionFactor, calibrationConversionFactorErr) )
+	    calibrationConversionFactor, calibrationConversionFactorErr, calibFFactor) )
         continue;
       
@@ -403,10 +495,10 @@
       
       nphot    = signal*calibrationConversionFactor;
-      nphotErr = signal*calibrationConversionFactorErr
-	*signal*calibrationConversionFactorErr
-	+signalErr*calibrationConversionFactor
-	*signalErr*calibrationConversionFactor;
-      
-      nphotErr  = TMath::Sqrt(nphotErr);
+      nphotErr = calibFFactor*TMath::Sqrt(TMath::Abs(nphot));
+      //   nphotErr = signal*calibrationConversionFactorErr
+      //	*signal*calibrationConversionFactorErr
+      //	+signalErr*calibrationConversionFactor
+      //	*signalErr*calibrationConversionFactor;
+      //     nphotErr  = TMath::Sqrt(nphotErr);
       
       MCerPhotPix *cpix = fCerPhotEvt->AddPixel(pixidx, nphot, nphotErr);
@@ -426,5 +518,8 @@
   if(TestFlag(kEvent)) 
   {
-
+    if (!CalibratePedestal(fPedestalFromData,fPedPhotFromData)) return kFALSE;
+  }
+
+  /*
   //---------------------------------------------
   // fill MPedPhot(FromData) container using the informations from
@@ -438,5 +533,5 @@
     *fLog << err << "MCalibrateData::ReInit(); sizes of MPedestalCam and MCalibrationCam are different" 
           << endl;
-  } 
+	  } */
 
   /*
@@ -447,5 +542,5 @@
   *fLog << all << "     pixid, calibrationConversionFactor, ped, pedRMS, pedphot, pedphotRMS :"
         << endl;
-  */
+  */ /*
   for (Int_t pixid=0; pixid<fPedestalFromData->GetSize(); pixid++)
   {
@@ -486,5 +581,5 @@
 
   }
-
+     */
 
   return kTRUE;
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4580)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4581)
@@ -61,6 +61,9 @@
 
 
+    Bool_t CalibratePedestal(MPedestalCam *, MPedPhotCam *);
+
     Bool_t GetConversionFactor(UInt_t, 
-                               Float_t &, Float_t &, Float_t &, Float_t &);    
+                               Float_t &, Float_t &, Float_t &, 
+                               Float_t &, Float_t &);    
 
     Int_t  PreProcess(MParList *pList);
@@ -70,5 +73,5 @@
 public:
 
-    enum CalibrationMode_t{kNone=0,kBlindPixel,kFfactor,kPinDiode,kCombined};
+    enum CalibrationMode_t{kNone=0,kFlatCharge=1,kBlindPixel,kFfactor,kPinDiode,kCombined, kDummy};
     static const CalibrationMode_t kDefault = kBlindPixel;
 
@@ -96,5 +99,5 @@
     void SetNamePedPhotRunContainer(const char *name)    { fNamePedPhotRunContainer = name; }
     void SetNamePedPhotEventContainer(const char *name)    { fNamePedPhotEventContainer = name; }
-    
+
     
     ClassDef(MCalibrateData, 0)   // Task to calculate cerenkov photons using calibration constants
