Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 2875)
+++ trunk/MagicSoft/Mars/Changelog	(revision 2876)
@@ -4,4 +4,22 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2004/01/21: Abelardo Moralejo
+
+   * mcalib/MMcCalibrationCalc.[h,cc]
+     - Added. First version of the task intended to calculate the
+       conversion factors from ADC counts to photons in the MC. It has
+       a different aim than the recently renamed class 
+       "MMcCalibrationUpdate" (which takes care that the correct 
+       pedestals and conversion factors are supplied to each event, but 
+       does not really calculate anything).
+
+   * mcalib/Makefile, CalibLinkDef.h
+     - Added new class MMcCalibrationCalc
+
+   * manalysis/MMcCalibrationUpdate.[h,cc]
+     - Now the container MCalibrationCam is not created or modified if
+       it is found that one such container already exists in the 
+       Parameter list (from a previous calibration).
+
 
  2004/01/21: Thomas Bretz
@@ -16,5 +34,5 @@
 
 
- 2004/01/21: Abelardo moralejo
+ 2004/01/21: Abelardo Moralejo
 
    * mmc/MMcEvt.hxx
Index: trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc	(revision 2875)
+++ trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.cc	(revision 2876)
@@ -73,4 +73,6 @@
     fADC2PhOuter = 1.;
 
+    fFillCalibrationCam = kTRUE;
+
 }
 
@@ -117,9 +119,21 @@
     return kTRUE;
 
-  fCalCam = (MCalibrationCam*) pList->FindCreateObj(AddSerialNumber("MCalibrationCam"));
+
+  fCalCam = (MCalibrationCam*) pList->FindObject(AddSerialNumber("MCalibrationCam"));
   if ( !fCalCam )
     {
-      *fLog << err << dbginf << "Cannot create MCalibrationCam... aborting." << endl;
-      return kFALSE;
+      *fLog << warn << dbginf << AddSerialNumber("MCalibrationCam") << " does not exist... Creating." << endl;
+
+      fCalCam = (MCalibrationCam*) pList->FindCreateObj(AddSerialNumber("MCalibrationCam"));
+      if ( !fCalCam )
+	{
+	  *fLog << err << dbginf << "Cannot create " << AddSerialNumber("MCalibrationCam") << "... aborting." << endl;
+	  return kFALSE;
+	}
+    }
+  else
+    {
+      fFillCalibrationCam = kFALSE;
+      *fLog << warn << dbginf << AddSerialNumber("MCalibrationCam") << " already exists... " << endl;
     }
 
@@ -185,4 +199,13 @@
     fConversionHiLo = fHeaderFadc->GetLow2HighGain();
 
+    //
+    // If MCalibrationCam already existed in the parameter list before
+    // MMcCalibrationUpdate::PreProcess was executed (from a 
+    // previous calibration loop) we must not fill it, hence nothing
+    // else has to be done in ReInit:
+    //
+    if ( !fFillCalibrationCam )
+      return kTRUE;
+
 
     const int num = fCalCam->GetSize();
Index: trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h	(revision 2875)
+++ trunk/MagicSoft/Mars/manalysis/MMcCalibrationUpdate.h	(revision 2876)
@@ -30,4 +30,6 @@
     Float_t fConversionHiLo;
 
+    Bool_t  fFillCalibrationCam;
+
 public:
     MMcCalibrationUpdate(const char *name=NULL, const char *title=NULL);
Index: trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h	(revision 2875)
+++ trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h	(revision 2876)
@@ -17,3 +17,5 @@
 #pragma link C++ class MHCalibrationPixel+;
 
+#pragma link C++ class MMcCalibrationCalc++;
+
 #endif
Index: trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc	(revision 2876)
+++ trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.cc	(revision 2876)
@@ -0,0 +1,218 @@
+/* ======================================================================== *\
+   !
+   ! *
+   ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+   ! * Software. It is distributed to you in the hope that it can be a useful
+   ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+   ! * It is distributed WITHOUT ANY WARRANTY.
+   ! *
+   ! * Permission to use, copy, modify and distribute this software and its
+   ! * documentation for any purpose is hereby granted without fee,
+   ! * provided that the above copyright notice appear in all copies and
+   ! * that both that copyright notice and this permission notice appear
+   ! * in supporting documentation. It is provided "as is" without express
+   ! * or implied warranty.
+   ! *
+   !
+   !
+   !   Author(s): Abelardo Moralejo, 12/2003 <mailto:moralejo@pd.infn.it>
+   !
+   !   Copyright: MAGIC Software Development, 2000-2003
+   !
+   !
+   \* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+//  MMcCalibrationCalc
+//
+//  Input Containers:
+//   MRawRunHeader
+//   MMcFadcHeader
+//   MHillas
+//   MNewImagePar
+//   MMcEvt
+//
+//  Output Containers:
+//   MCalibrationCam
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MMcCalibrationCalc.h"
+
+#include "MParList.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MCalibrationPix.h"
+#include "MCalibrationCam.h"
+#include "MGeomCam.h"
+#include "MRawRunHeader.h"
+#include "MMcFadcHeader.hxx"
+#include "MHillas.h"
+#include "MNewImagePar.h"
+#include "MMcEvt.hxx"
+
+ClassImp(MMcCalibrationCalc);
+
+using namespace std;
+
+MMcCalibrationCalc::MMcCalibrationCalc(const char *name, const char *title)
+{
+  fName  = name  ? name  : "MMcCalibrationCalc";
+  fTitle = title ? title : "Calculate and write conversion factors into MCalibrationCam Container";
+
+  fADC2Phot = 0.;
+  fEvents = 0;
+
+  fHistRatio = new TH1F("HistRatio", "Ratio fPassPhotCone/fSize", 1000., 0., 10.);
+}
+
+// --------------------------------------------------------------------------
+//
+// Check for the run type. Return kTRUE if it is a MC run or if there
+// is no MC run header (old camera files) kFALSE in case of a different
+// run type
+//
+Bool_t MMcCalibrationCalc::CheckRunType(MParList *pList) const
+{
+  const MRawRunHeader *run = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
+  if (!run)
+    {
+      *fLog << warn << dbginf << "Warning - cannot check file type, MRawRunHeader not found." << endl;
+      return kTRUE;
+    }
+
+  return  run->GetRunType() == kRTMonteCarlo;
+}
+
+// --------------------------------------------------------------------------
+//
+// Make sure, that there is an MCalibrationCam Object in the Parameter List.
+//
+Int_t MMcCalibrationCalc::PreProcess(MParList *pList)
+{
+  //
+  // If it is no MC file display error and exit
+  //
+  if (!CheckRunType(pList))
+    {
+      *fLog << err << dbginf << "This is no MC file... aborting." << endl;
+      return kFALSE;
+    }
+
+  fCalCam = (MCalibrationCam*) pList->FindObject(AddSerialNumber("MCalibrationCam"));
+
+  if ( !fCalCam )
+    {
+      *fLog << err << dbginf << "Cannot find MCalibrationCam... aborting." << endl;
+      return kFALSE;
+    }
+
+  fHillas = (MHillas*) pList->FindCreateObj(AddSerialNumber("MHillas"));
+  if ( !fHillas)
+    {
+      *fLog << err << dbginf << "Cannot find " << AddSerialNumber("MHillas") << "... aborting." << endl;
+      return kFALSE;
+    }
+
+  fNew = (MNewImagePar*) pList->FindCreateObj(AddSerialNumber("MNewImagePar"));
+  if ( !fNew)
+    {
+      *fLog << err << dbginf << "Cannot find " << AddSerialNumber("MNewImagePar") << "... aborting." << endl;
+      return kFALSE;
+    }
+
+  fMcEvt = (MMcEvt*) pList->FindCreateObj(AddSerialNumber("MMcEvt"));
+  if ( !fMcEvt)
+    {
+      *fLog << err << dbginf << "Cannot find " << AddSerialNumber("MMcEvt") << "... aborting." << endl;
+      return kFALSE;
+    }
+
+  return kTRUE;
+
+}
+
+// --------------------------------------------------------------------------
+//
+// Check for the runtype.
+// Search for MGeomCam and MMcFadcHeader.
+//
+Bool_t MMcCalibrationCalc::ReInit(MParList *pList)
+{
+  //
+  // Now check the existence of all necessary containers.
+  //
+
+  fGeom = (MGeomCam*) pList->FindObject(AddSerialNumber("MGeomCam"));
+  if ( ! fGeom )
+    {
+      *fLog << err << dbginf << "Cannot find MGeomCam... aborting." << endl;
+      return kFALSE;
+    }
+
+  fHeaderFadc = (MMcFadcHeader*)pList->FindObject(AddSerialNumber("MMcFadcHeader"));
+  if (!fHeaderFadc)
+    {
+      *fLog << err << dbginf << "MMcFadcHeader not found... aborting." << endl;
+      return kFALSE;
+    }
+
+  //
+  // FIXME: Check that the relevant parameters in the FADC header are the
+  // same for all the analyzed data!
+  //
+
+  return kTRUE;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Obtain average ratio of photons in camera to image Size.
+//
+Int_t MMcCalibrationCalc::Process()
+{
+
+  if ( fNew->GetNumSaturatedPixels() > 0 )
+    return kTRUE;
+
+  fADC2Phot += fMcEvt->GetPassPhotCone() / fHillas->GetSize();
+  fEvents ++;
+
+  fHistRatio->Fill(fMcEvt->GetPassPhotCone()/fHillas->GetSize());
+
+  return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Fill the MCalibrationCam object
+//
+Int_t MMcCalibrationCalc::PostProcess()
+{
+
+  if (fEvents > 0)
+    fADC2Phot /= fEvents;
+  else
+    {
+      *fLog << err << dbginf << "No events were read! Aborting." << endl;
+      return kFALSE;
+    }
+
+  const int num = fCalCam->GetSize();
+  
+  for (int i=0; i<num; i++)
+    {
+      MCalibrationPix &calpix = (*fCalCam)[i];
+
+      Float_t factor =  fADC2Phot*calpix.GetMeanConversionBlindPixelMethod();
+
+      calpix.SetConversionBlindPixelMethod(factor, 0., 0.);
+      
+    }
+
+  return kTRUE;
+
+}
Index: trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h	(revision 2876)
+++ trunk/MagicSoft/Mars/mcalib/MMcCalibrationCalc.h	(revision 2876)
@@ -0,0 +1,44 @@
+#ifndef MARS_MMcCalibrationCalc
+#define MARS_MMcCalibrationCalc
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MCalibrationCam;
+class MGeomCam;
+class MHillas;
+class MNewImagePar;
+class MMcEvt;
+class MMcFadcHeader;
+
+#include <TH1.h>
+
+class MMcCalibrationCalc : public MTask
+{
+private:
+    Bool_t CheckRunType(MParList *pList) const;
+    Int_t  PreProcess(MParList *pList);
+    Int_t  Process();
+    Int_t  PostProcess();
+    Bool_t ReInit(MParList *pList);
+
+    MCalibrationCam     *fCalCam;
+    MGeomCam            *fGeom;
+    MHillas             *fHillas;
+    MNewImagePar        *fNew;
+    MMcEvt              *fMcEvt;
+    MMcFadcHeader       *fHeaderFadc;
+
+    Float_t fADC2Phot;
+    Long_t  fEvents;
+
+    TH1F*   fHistRatio;
+
+public:
+    MMcCalibrationCalc(const char *name=NULL, const char *title=NULL);
+
+    ClassDef(MMcCalibrationCalc, 0)   // Task which obtains, for MC files, the calibration factor from ADC counts to photons. 
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mcalib/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mcalib/Makefile	(revision 2875)
+++ trunk/MagicSoft/Mars/mcalib/Makefile	(revision 2876)
@@ -23,11 +23,12 @@
 #
 INCLUDES = -I. -I../mbase -I../mhbase -I../mgui -I../mgeom -I../manalysis \
-	   -I../mraw -I../mtools
+	   -I../mraw -I../mtools -I../mmc -I../mimage
+
 # mhbase:    MBinning MH 
 # mgui:      MCamEvent (McalibrationCam)
 # mgeom:     MGeomCam (McalibrationCam - necessary?)
 # manalysis: MExtractedSignal, MCerPhotEvt (move to mcalib?)
-# mraw:      MRawRunHeader, MRawEvtHeader, MRawEvtPixelIter (3xMCalibrationCalc)
-
+# mraw:      MRawRunHeader, MRawEvtHeader, MRawEvtPixelIter (3xMCalibrationCalc)# mmc:       MMcFadcHeader, MMcEvt
+# mimage     MHillas
 
 #------------------------------------------------------------------------------
@@ -43,5 +44,6 @@
            MHCalibrationBlindPixel.cc \
            MHCalibrationPINDiode.cc \
-           MHCalibrationPixel.cc 
+           MHCalibrationPixel.cc \
+	   MMcCalibrationCalc.cc
 
 SRCS    = $(SRCFILES)
