Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 3749)
+++ trunk/MagicSoft/Mars/Changelog	(revision 3750)
@@ -27,4 +27,9 @@
    * manalysis/MCerPhotPix.[h,cc]
      - added one variable fTime (with Getter and Setter)
+
+   * mcalib/MCalibrateRelTimes.[h,cc]
+   * mcalib/Makefile
+   * mcalib/CalibLinkDef.h
+     - new class to calibrate the relative times
 
 
Index: trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h	(revision 3749)
+++ trunk/MagicSoft/Mars/mcalib/CalibLinkDef.h	(revision 3750)
@@ -7,4 +7,5 @@
 #pragma link C++ class MCalibrate+;
 #pragma link C++ class MCalibrateData+;
+#pragma link C++ class MCalibrateRelTimes+;
 
 #pragma link C++ class MCalibrationCam+;
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.cc	(revision 3750)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.cc	(revision 3750)
@@ -0,0 +1,200 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Markus Gaug  04/2004 <mailto:markus@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//   MCalibrateRelTimes
+//
+//   This task takes the extracted arrival times from MArrivalTimeCam for each  
+//   pixel and applies the offset calibrated in MCalibrationRelTimeCam 
+//   The calibrated arrival time and its error gets stored in MCerPhotEvt. 
+//
+//   Input Containers:
+//    MArrivalTimeCam 
+//    MCalibrationRelTimeCam
+//
+//   Output Containers:
+//    MCerPhotEvt
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MCalibrateRelTimes.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+#include "MH.h"
+
+#include "MGeomCam.h"
+
+#include "MCalibrationRelTimeCam.h"
+#include "MCalibrationRelTimePix.h"
+
+#include "MArrivalTimeCam.h"
+#include "MArrivalTimePix.h"
+
+#include "MBadPixelsCam.h"
+#include "MBadPixelsPix.h"
+
+#include "MCerPhotEvt.h"
+
+ClassImp(MCalibrateRelTimes);
+
+using namespace std;
+// --------------------------------------------------------------------------
+//
+// Default constructor. 
+//
+MCalibrateRelTimes::MCalibrateRelTimes(const char *name, const char *title) 
+    : fGeomCam(NULL), fCalibrations(NULL), fBadPixels(NULL), fSignals(NULL), 
+      fCerPhotEvt(NULL)
+{
+    fName  = name  ? name  : "MCalibrateRelTimes";
+    fTitle = title ? title : "Task to calculate the calibrated arrival times of photons in one event";
+}
+
+// --------------------------------------------------------------------------
+//
+// The PreProcess searches for the following input containers:
+//  - MGeomCam
+//  - MCalibrationRelTimesCam
+//  - MArrivalTimeCam
+//  - MBadPixelsCam
+//
+// The following output containers are also searched and created if
+// they were not found:
+//
+//  - MCerPhotEvt
+//
+Int_t MCalibrateRelTimes::PreProcess(MParList *pList)
+{
+
+    fSignals = (MArrivalTimeCam*)pList->FindObject(AddSerialNumber("MArrivalTimeCam"));
+
+    if (!fSignals)
+    {
+      *fLog << err << AddSerialNumber("MArrivalTimeCam") << " not found ... aborting" << endl;
+        return kFALSE;
+    }
+
+    fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
+    if (!fBadPixels)
+      *fLog << warn << AddSerialNumber("MBadPixelsCam") << " not found ... no action" << endl;
+    
+    fCalibrations = (MCalibrationRelTimeCam*)pList->FindObject(AddSerialNumber("MCalibrationRelTimeCam"));
+    if (!fCalibrations)
+      {
+        *fLog << err << AddSerialNumber("MCalibrationRelTimeCam") << " not found ... aborting." << endl;
+        return kFALSE;
+      }
+
+
+    fCerPhotEvt = (MCerPhotEvt*)pList->FindObject(AddSerialNumber("MCerPhotEvt"));
+    if (!fCerPhotEvt)
+      {
+        *fLog << err << AddSerialNumber("MCerPhotEvt") << " not found ... aborting." << endl;
+        return kFALSE;
+      }
+    
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Check for validity of the selected calibration method, switch to a 
+// different one in case of need
+//
+Bool_t MCalibrateRelTimes::ReInit(MParList *pList)
+{
+  return kTRUE;
+}
+// --------------------------------------------------------------------------
+//
+// Apply the calibration factors to the extracted signal according to the 
+// selected calibration method
+//
+Int_t MCalibrateRelTimes::Process()
+{
+
+  /*
+    if (fCalibrations->GetNumPixels() != (UInt_t)fSignals->GetSize())
+    {
+        // FIXME: MArrivalTime must be of variable size -
+        //        like MCerPhotEvt - because we must be able
+        //        to reduce size by zero supression
+        //        For the moment this check could be done in ReInit...
+        *fLog << err << "MArrivalTime and MCalibrationCam have different sizes... abort." << endl;
+        return kFALSE;
+    }
+  */
+
+  UInt_t npix = fSignals->GetSize();
+
+  Float_t offset    = 0.;
+  Float_t precision = 0.;
+  
+  for (UInt_t pixidx=0; pixidx<npix; pixidx++)
+    {
+
+      MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*fCalibrations)[pixidx];
+      MBadPixelsPix          &bad = (*fBadPixels)[pixidx];
+      
+      if (fBadPixels)
+        if (!bad.IsCalibrationResultOK())
+          continue;
+      
+      offset     = pix.GetTimeOffset();
+      precision  = pix.GetTimePrecision();
+      
+      MArrivalTimePix &sig =  (*fSignals)[pixidx];
+      
+      Float_t signal;
+            
+      if (sig.IsLoGainUsed())
+        signal = sig.GetArrivalTimeLoGain();
+      else
+        signal = sig.GetArrivalTimeHiGain();
+      
+      const Float_t time = signal - offset;
+
+      //
+      // The following part is the outcommented first version of the error calculation
+      // Contact Markus Gaug for questions (or wait for the next documentation update...)
+      //
+      /*
+        nphotErr = signal    > 0 ? signalErr*signalErr / (signal * signal)  : 0.
+                 + calibConv > 0 ? calibConvVar  / (calibConv * calibConv ) : 0.
+                 + calibQE   > 0 ? calibQEVar    / (calibQE   * calibQE   ) : 0.;
+        nphotErr  = TMath::Sqrt(nphotErr) * nphot;
+      */
+
+      MCerPhotPix &cpix = (*fCerPhotEvt)[pixidx];
+      cpix.SetTime(time);
+
+    } /* for (UInt_t pixidx=0; pixidx<npix; pixidx++) */
+  
+  fCerPhotEvt->SetReadyToSave();
+  
+  return kTRUE;
+}
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.h	(revision 3750)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateRelTimes.h	(revision 3750)
@@ -0,0 +1,42 @@
+#ifndef MARS_MCalibrateRelTimes
+#define MARS_MCalibrateRelTimes
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MCalibrateRelTimes                                                      //
+//                                                                         //
+// Calculates the relative arrival time and applies the offset,            //
+// stored in MCalibrationRelTimePix                                        //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MGeomCam;
+class MBadPixelsCam;
+class MCalibrationRelTimeCam;
+class MArrivalTimeCam;
+class MCerPhotEvt;
+class MCalibrateRelTimes : public MTask
+{
+private:
+    MGeomCam               *fGeomCam;      // Camera geometry container
+    MCalibrationRelTimeCam *fCalibrations; // Calibration rel. time constants
+    MBadPixelsCam          *fBadPixels;    // Bad Pixels information
+    MArrivalTimeCam        *fSignals;      // Extracted Arrival Time
+    MCerPhotEvt            *fCerPhotEvt;   // Cerenkov Photon Event used for calculation
+
+    Int_t PreProcess(MParList *pList);
+    Bool_t ReInit(MParList *pList);
+    Int_t Process();
+
+public:
+
+    MCalibrateRelTimes(const char *name=NULL, const char *title=NULL);
+
+    ClassDef(MCalibrateRelTimes, 0)   // Task to calculate calibrated relative arrival times
+};
+ 
+
+#endif
Index: trunk/MagicSoft/Mars/mcalib/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mcalib/Makefile	(revision 3749)
+++ trunk/MagicSoft/Mars/mcalib/Makefile	(revision 3750)
@@ -39,4 +39,5 @@
 SRCFILES = MCalibrate.cc \
 	   MCalibrateData.cc \
+	   MCalibrateRelTimes.cc \
            MCalibrationCam.cc \
            MCalibrationPix.cc  \
