Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 4627)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 4628)
@@ -21,4 +21,5 @@
 !   Author(s): Markus Gaug     04/2004 <mailto:markus@ifae.es>
 !   Author(s): Hendrik Bartko  08/2004 <mailto:hbartko@mppmu.mpg.de>
+!   Author(s): Thomas Bretz    08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
 !   Copyright: MAGIC Software Development, 2000-2004
@@ -47,25 +48,47 @@
 //   The calibration modes which exclude non-valid pixels are the following: 
 //
-//   kFfactor:    calibrates using the F-Factor method
-//   kBlindpixel: calibrates using the BlindPixel method 
-//   kBlindpixel: calibrates using the BlindPixel method 
-//   kFlatCharge: perform a charge flat-flatfielding. Outer pixels are area-corrected.
-//   kDummy:      calibrates with fixed conversion factors of 1 and errors of 0.
+//     kFfactor:    calibrates using the F-Factor method
+//     kBlindpixel: calibrates using the BlindPixel method
+//     kBlindpixel: calibrates using the BlindPixel method
+//     kFlatCharge: perform a charge flat-flatfielding. Outer pixels are area-corrected.
+//     kDummy:      calibrates with fixed conversion factors of 1 and errors of 0.
 //
 //   The calibration modes which include all pixels regardless of their validity is:
 //
-//   kNone:       calibrates with fixed conversion factors of 1 and errors of 0.
-//
-//   Use the kDummy and kNone methods ONLY FOR DEBUGGING!   
+//     kNone:       calibrates with fixed conversion factors of 1 and errors of 0.
+//
+//   Use the kDummy and kNone methods ONLY FOR DEBUGGING!
+//
+//
+//   This class can calibrate data and/or pedestals. To switch off calibration of data
+//   set the Calibration Mode to kSkip. To switch on pedestal calibration call either
+//     SetPedestalFlag(MCalibrateData::kRun)    (calibration is done once in ReInit)
+//     SetPedestalFlag(MCalibrateData::kEvent)  (calibration is done for each event)
+//
+//   By calling SetNamePedADCContainer() and/or SetNamePedPhotContainer() you
+//   can control the name of the MPedestalCam and/or MPedPhotCam container which is used.
+//
+//   Assume you want to calibrate "MPedestalCam" once and "MPedestalFromLoGain" [MPedestalCam]
+//   event-by-event, so:
+//     MCalibrateData cal1;
+//     cal1.SetCalibrationMode(MCalibrateData::kSkip);
+//     cal1.SetPedestalFlag(MCalibrateData::kRun);
+//     MCalibrateData cal2;
+//     cal2.SetCalibrationMode(MCalibrateData::kSkip);
+//     cal2.SetNamePedADCContainer("MPedestalFromLoGain");
+//     cal2.SetNamePedPhotContainer("MPedPhotFromLoGain")
+//     cal2.SetPedestalFlag(MCalibrateData::kEvent);
+//
 //
 //   Input Containers:
-//    MPedestalCam
-//    MExtractedSignalCam
-//    MCalibrationChargeCam
-//    MCalibrationQECam
+//    [MPedestalCam]
+//    [MExtractedSignalCam]
+//    [MCalibrationChargeCam]
+//    [MCalibrationQECam]
+//    MBadPixelsCam
 //
 //   Output Containers:
-//    MPedPhotCam
-//    MCerPhotEvt
+//    [MPedPhotCam]
+//    [MCerPhotEvt]
 //
 // See also: MJCalibration, MJPedestal, MJExtractSignal, MJExtractCalibTest
@@ -123,7 +146,7 @@
 //
 MCalibrateData::MCalibrateData(CalibrationMode_t calmode,const char *name, const char *title) 
-    : fGeomCam(NULL), fPedestal(NULL), 
+    : fGeomCam(NULL),   fPedestal(NULL),
       fBadPixels(NULL), fCalibrations(NULL), fQEs(NULL), fSignals(NULL), 
-      fPedPhot(NULL), fCerPhotEvt(NULL)
+      fPedPhot(NULL),   fCerPhotEvt(NULL), fPedestalFlag(kNo)
 {
 
@@ -132,9 +155,7 @@
   
   SetCalibrationMode();
-  SetPedestalFlag();
 
   SetNamePedADCContainer();
   SetNamePedPhotContainer();
-
 }
 
@@ -160,18 +181,4 @@
     // input containers
 
-    fPedestal = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedADCContainer), "MPedestalCam");
-    if (!fPedestal)
-    {
-        *fLog << err << AddSerialNumber(fNamePedADCContainer) << " [MPedestalCam] not found ... aborting" << endl;
-        return kFALSE;
-    }
-
-    fSignals = (MExtractedSignalCam*)pList->FindObject(AddSerialNumber("MExtractedSignalCam"));
-    if (!fSignals)
-    {
-        *fLog << err << AddSerialNumber("MExtractedSignalCam") << " not found ... aborting" << endl;
-        return kFALSE;
-    }
-
     fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
     if (!fBadPixels)
@@ -181,4 +188,22 @@
     }
 
+    fSignals    = 0;
+    fCerPhotEvt = 0;
+    if (fCalibrationMode>kSkip)
+    {
+        fSignals = (MExtractedSignalCam*)pList->FindObject(AddSerialNumber("MExtractedSignalCam"));
+        if (!fSignals)
+        {
+            *fLog << err << AddSerialNumber("MExtractedSignalCam") << " not found ... aborting" << endl;
+            return kFALSE;
+        }
+
+        fCerPhotEvt = (MCerPhotEvt*)pList->FindCreateObj(AddSerialNumber("MCerPhotEvt"));
+        if (!fCerPhotEvt)
+            return kFALSE;
+    }
+
+    fCalibrations = 0;
+    fQEs          = 0;
     if (fCalibrationMode>kNone)
     {
@@ -198,13 +223,19 @@
     }
 
-    // output containers
-
-    fPedPhot = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam", AddSerialNumber(fNamePedPhotContainer));
-    if (!fPedPhot)
-        return kFALSE;
-
-    fCerPhotEvt = (MCerPhotEvt*)pList->FindCreateObj(AddSerialNumber("MCerPhotEvt"));
-    if (!fCerPhotEvt)
-        return kFALSE;
+    fPedestal = 0;
+    fPedPhot  = 0;
+    if (fPedestalFlag)
+    {
+        fPedestal = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedADCContainer), "MPedestalCam");
+        if (!fPedestal)
+        {
+            *fLog << err << AddSerialNumber(fNamePedADCContainer) << " [MPedestalCam] not found ... aborting" << endl;
+            return kFALSE;
+        }
+
+        fPedPhot = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam", AddSerialNumber(fNamePedPhotContainer));
+        if (!fPedPhot)
+            return kFALSE;
+    }
 
     return kTRUE;
@@ -225,79 +256,66 @@
 Bool_t MCalibrateData::ReInit(MParList *pList)
 {
-
-  fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
-  if (!fGeomCam)
-    {
-      *fLog << err << "No MGeomCam found... aborting." << endl;
-      return kFALSE;
-    }
-
-  if(fCalibrationMode == kBlindPixel && !fQEs->IsBlindPixelMethodValid())
-    {
-      *fLog << warn << GetDescriptor() 
-            << ": Blind pixel calibration method not valid, switching to default F-factor method" << endl;
-      fCalibrationMode = kFfactor;
-    }
-
-  if(fCalibrationMode == kPinDiode && !fQEs->IsPINDiodeMethodValid())
-    { 
-      *fLog << warn << GetDescriptor()
-            << ": PIN diode calibration method not valid, switching to default F-factor method" << endl;
-      fCalibrationMode = kFfactor;
-    }
-
-  if(fCalibrationMode == kCombined && !fQEs->IsCombinedMethodValid())
-    { 
-      *fLog << warn << GetDescriptor() 
-            << ": Combined calibration method not valid, switching to default F-factor method" << endl;
-      fCalibrationMode = kFfactor;
-    }
-
-  //
-  // output information or warnings:
-  //
-  switch(fCalibrationMode)
+    fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
+    if (!fGeomCam)
+    {
+        *fLog << err << "No MGeomCam found... aborting." << endl;
+        return kFALSE;
+    }
+
+    if(fCalibrationMode == kBlindPixel && !fQEs->IsBlindPixelMethodValid())
+    {
+        *fLog << warn << "Blind pixel calibration method not valid, switching to F-factor method" << endl;
+        fCalibrationMode = kFfactor;
+    }
+
+    if(fCalibrationMode == kPinDiode && !fQEs->IsPINDiodeMethodValid())
+    {
+        *fLog << warn << "PIN diode calibration method not valid, switching to F-factor method" << endl;
+        fCalibrationMode = kFfactor;
+    }
+
+    if(fCalibrationMode == kCombined && !fQEs->IsCombinedMethodValid())
+    {
+        *fLog << warn << "Combined calibration method not valid, switching to F-factor method" << endl;
+        fCalibrationMode = kFfactor;
+    }
+
+    //
+    // output information or warnings:
+    //
+    switch(fCalibrationMode)
     {
     case kBlindPixel:
-      break;
+        break;
     case kFfactor:
-      break;
+        break;
     case kPinDiode:
-      *fLog << err << GetDescriptor() 
-                    << ": PIN Diode Calibration mode not yet available " << endl;
-      return kFALSE;
-      break;
+        *fLog << err << "PIN Diode Calibration mode not yet available!" << endl;
+        return kFALSE;
+        break;
     case kCombined:
-      *fLog << err << GetDescriptor() 
-                    << ": Combined Calibration mode not yet available " << endl;
-      return kFALSE;
-      break;
+        *fLog << err << "Combined Calibration mode not yet available!" << endl;
+        return kFALSE;
+        break;
     case kFlatCharge:
-      *fLog << warn << GetDescriptor() 
-            << ": WARNING: Flat-fielding charges - only for muon calibration! " << endl;
-      break;
+        *fLog << warn << "WARNING - Flat-fielding charges - only for muon calibration!" << endl;
+        break;
     case kDummy:
-      *fLog << warn << GetDescriptor() 
-            << ": WARNING: Dummy calibration, no calibration applied!!" << endl;
-      break;
+        *fLog << warn << "WARNING - Dummy calibration, no calibration applied!" << endl;
+        break;
     case kNone:
-      *fLog << warn << GetDescriptor() 
-            << ": WARNING: No calibration applied!!" << endl;
-      break;
+        *fLog << warn << "WARNING - No calibration applied!" << endl;
+        break;
     default:
-      *fLog << warn << GetDescriptor() 
-            << ": WARNING: Calibration mode value ("
-            << fCalibrationMode << ") not known" << endl;
-      return kFALSE;
-    }
-
-  if (TestPedestalFlag(kRun))
-      if (!CalibratePedestal())
+        *fLog << warn << "WARNING - Calibration mode value (" << fCalibrationMode << ") not known" << endl;
+        return kFALSE;
+    }
+
+    if (TestPedestalFlag(kRun))
+        if (!CalibratePedestal())
           return kFALSE;
 
-  return kTRUE;
-}
-
-
+    return kTRUE;
+}
 
 // --------------------------------------------------------------------------
@@ -316,6 +334,6 @@
     if ((Int_t)fPedestal->GetSize() != fSignals->GetSize())
     {
-        *fLog << err << "MCalibrateData::ReInit(); sizes of MPedestalCam and MCalibrationCam are different"
-            << endl;
+        *fLog << err << "Sizes of MPedestalCam and MCalibrationCam are different" << endl;
+        return kFALSE;
     }
 
@@ -326,6 +344,6 @@
 
         // pedestals/(used FADC slices)   in [ADC] counts
-        Float_t pedes  = ped.GetPedestal()    * slices;
-        Float_t pedrms = ped.GetPedestalRms() * TMath::Sqrt(slices);
+        const Float_t pedes  = ped.GetPedestal()    * slices;
+        const Float_t pedrms = ped.GetPedestalRms() * TMath::Sqrt(slices);
 
         //
@@ -337,7 +355,6 @@
         Float_t calibConvVar;
         Float_t calibFFactor;
-
-        if ( !GetConversionFactor(pixid, hiloconv, hiloconverr,
-                                  calibConv, calibConvVar, calibFFactor ))
+        if (!GetConversionFactor(pixid, hiloconv, hiloconverr,
+                                 calibConv, calibConvVar, calibFFactor))
             continue;
 
@@ -345,6 +362,6 @@
         // pedestals/(used FADC slices)   in [number of photons]
         //
-        Float_t pedphot    = pedes  * calibConv;
-        Float_t pedphotrms = pedrms * calibConv;
+        const Float_t pedphot    = pedes  * calibConv;
+        const Float_t pedphotrms = pedrms * calibConv;
 
         (*fPedPhot)[pixid].Set(pedphot, pedphotrms);
@@ -492,9 +509,9 @@
     for (UInt_t pixidx=0; pixidx<npix; pixidx++)
     {
-        if ( !GetConversionFactor(pixidx, hiloconv, hiloconverr,
-                                  calibrationConversionFactor, calibrationConversionFactorErr, calibFFactor) )
+        if (!GetConversionFactor(pixidx, hiloconv, hiloconverr,
+                                 calibrationConversionFactor, calibrationConversionFactorErr, calibFFactor))
             continue;
 
-        MExtractedSignalPix &sig =  (*fSignals)[pixidx];
+        MExtractedSignalPix &sig = (*fSignals)[pixidx];
 
         Float_t signal;
@@ -520,4 +537,5 @@
         nphot    = signal*calibrationConversionFactor;
         nphotErr = calibFFactor*TMath::Sqrt(TMath::Abs(nphot));
+
         //
         // The following part is the commented first version of the error calculation
@@ -580,26 +598,40 @@
 void MCalibrateData::StreamPrimitive(ofstream &out) const
 {
-
-  out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
-  out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
-
-  if (TestPedestalFlag(kEvent))
-    out << "   " << GetUniqueName() << ".EnablePedestalType(kEvent)" << endl;
-  if (TestPedestalFlag(kRun))
-    out << "   " << GetUniqueName() << ".EnablePedestalType(kRun)" << endl;
-
-  if (fCalibrationMode != kDefault)
-    out << "   " << GetUniqueName() << ".SetCalibrationMode(" << fCalibrationMode << ")" << endl;
-
-  if (fNamePedADCContainer != fgNamePedADCContainer)
-    {
-      out << "   " << GetUniqueName() << ".SetNamePedADCContainer(";
-      out << fNamePedADCContainer.Data() << ");" << endl;
-    }
-  
-  if (fNamePedPhotContainer  != fgNamePedPhotContainer)
-    {
-      out << "   " << GetUniqueName() << ".SetNamePedPhotContainer(";
-      out << fNamePedPhotContainer.Data() << ");" << endl;
-    }
-}
+    out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
+    out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
+
+    if (TestPedestalFlag(kEvent))
+        out << "   " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kEvent)" << endl;
+    if (TestPedestalFlag(kRun))
+        out << "   " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kRun)" << endl;
+
+    if (fCalibrationMode != kDefault)
+    {
+        out << "   " << GetUniqueName() << ".SetCalibrationMode(MCalibrateData::";
+        switch (fCalibrationMode)
+        {
+        case kSkip:       out << "kSkip";               break;
+        case kNone:       out << "kNone";               break;
+        case kFlatCharge: out << "kFlatCharge";         break;
+        case kBlindPixel: out << "kBlindPixel";         break;
+        case kFfactor:    out << "kFfactor";            break;
+        case kPinDiode:   out << "kPinDiode";           break;
+        case kCombined:   out << "kCombined";           break;
+        case kDummy:      out << "kDummy";              break;
+        default:          out << (int)fCalibrationMode; break;
+        }
+        out << ")" << endl;
+    }
+
+    if (fNamePedADCContainer != fgNamePedADCContainer)
+    {
+        out << "   " << GetUniqueName() << ".SetNamePedADCContainer(";
+        out << fNamePedADCContainer.Data() << ");" << endl;
+    }
+
+    if (fNamePedPhotContainer  != fgNamePedPhotContainer)
+    {
+        out << "   " << GetUniqueName() << ".SetNamePedPhotContainer(";
+        out << fNamePedPhotContainer.Data() << ");" << endl;
+    }
+}
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4627)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.h	(revision 4628)
@@ -34,5 +34,4 @@
 {
 private:
-
   static const TString fgNamePedADCContainer;    //! "MPedestalCam"
   static const TString fgNamePedPhotContainer;   //! "MPedPhotCam"
@@ -45,12 +44,12 @@
   MExtractedSignalCam   *fSignals;          //! Integrated charge in FADCs counts
   
-  MPedPhotCam           *fPedPhot;          // Pedestals/(used slices) [photons]
-  MCerPhotEvt           *fCerPhotEvt;       // Cerenkov Photon Event used for calculation
+  MPedPhotCam           *fPedPhot;          //! Pedestals/(used slices) [photons]
+  MCerPhotEvt           *fCerPhotEvt;       //! Cerenkov Photon Event used for calculation
   
   UShort_t fCalibrationMode;                // Flag defining the calibration mode (CalibrationMode_t)
   Byte_t   fPedestalFlag;                   // Flags defining to calibrate the pedestal each event or each run
   
-  TString  fNamePedADCContainer;         // name of fPedestal
-  TString  fNamePedPhotContainer;        // name of fPedPhot
+  TString  fNamePedADCContainer;            // name of fPedestal
+  TString  fNamePedPhotContainer;           // name of fPedPhot
 
   Bool_t CalibratePedestal();
@@ -69,12 +68,12 @@
     enum CalibrationMode_t
     {
-        kNone       =  0,
-        kFlatCharge =  1,
-        kBlindPixel =  2,
-        kFfactor    =  3,
-        kPinDiode   =  4,
-        kCombined   =  5,
-        kDummy      =  6,
-        kSkip       =  7
+        kSkip       =  0,
+        kNone       =  1,
+        kFlatCharge =  2,
+        kBlindPixel =  3,
+        kFfactor    =  4,
+        kPinDiode   =  5,
+        kCombined   =  6,
+        kDummy      =  7
     };
 
