Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 3176)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 3177)
@@ -4,4 +4,16 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2004/02/16: Markus Gaug
+
+   * mcalib/Makefile
+   * mcalib/CalibLinkDef.h
+
+   * mcalib/MHCalibrationRelTimePix.[h,cc]
+   * mcalib/MHCalibrationRelTimeCam.[h,cc]
+     - new classes to fit and display the relative arrival times. 
+     - filled from MArrivalTime	
+     - need: MFillH("MHCalibrationRelTimeCam","MArrivalTime")
+
 
  2004/02/14: Markus Gaug
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.cc	(revision 3177)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.cc	(revision 3177)
@@ -0,0 +1,290 @@
+/* ======================================================================== *\
+!
+! *
+! * 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   02/2004 <mailto:markus@ifae.es>
+!              
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MHCalibrationRelTimeCam                                                            //
+//                                                                         //
+// Hold the CalibrationRelTime information for all pixels in the camera              //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+#include "MHCalibrationRelTimeCam.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+
+#include "MHCalibrationRelTimePix.h"
+
+#include "MArrivalTime.h"
+
+ClassImp(MHCalibrationRelTimeCam);
+
+using namespace std;
+const Float_t MHCalibrationRelTimeCam::fgTimeSliceWidth = 3.3;
+// --------------------------------------------------------------------------
+//
+// Default constructor. Creates a MHCalibrationRelTimePix object for each pixel
+//
+MHCalibrationRelTimeCam::MHCalibrationRelTimeCam(const char *name, const char *title) 
+{
+
+    fName  = name  ? name  : "MHCalibrationRelTimeCam";
+    fTitle = title ? title : "Histogram container for the relative time calibration of the camera";
+
+    //
+    // loop over all Pixels and create two histograms
+    // one for the Low and one for the High gain
+    // connect all the histogram with the container fHist
+    //
+    fArray = new TObjArray;
+    fArray->SetOwner();
+
+}
+
+// --------------------------------------------------------------------------
+//
+// Delete the array conatining the pixel pedest information
+//
+MHCalibrationRelTimeCam::~MHCalibrationRelTimeCam()
+{
+  delete fArray;
+}
+
+// --------------------------------------
+//
+void MHCalibrationRelTimeCam::Clear(Option_t *o)
+{
+
+  fArray->ForEach(TObject, Clear)();
+  return;
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th pixel (pixel number)
+//
+MHCalibrationRelTimePix &MHCalibrationRelTimeCam::operator[](UInt_t i)
+{
+  return *(MHCalibrationRelTimePix*)(fArray->At(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th pixel (pixel number)
+//
+const MHCalibrationRelTimePix &MHCalibrationRelTimeCam::operator[](UInt_t i) const
+{
+  return *(MHCalibrationRelTimePix*)(fArray->At(i));
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Our own clone function is necessary since root 3.01/06 or Mars 0.4
+// I don't know the reason
+//
+TObject *MHCalibrationRelTimeCam::Clone(const char *) const
+{
+
+  const Int_t n = fArray->GetSize();
+  
+  //
+  // FIXME, this might be done faster and more elegant, by direct copy.
+  //
+  MHCalibrationRelTimeCam *cam = new MHCalibrationRelTimeCam;
+  
+  cam->fArray->Expand(n);
+  
+  for (int i=0; i<n; i++)
+    {
+      delete (*cam->fArray)[i];
+      (*cam->fArray)[i] = (*fArray)[i]->Clone();
+    }
+
+  return cam;
+}
+
+
+
+// --------------------------------------------------------------------------
+//
+//
+Bool_t MHCalibrationRelTimeCam::SetupFill(const MParList *pList)
+{
+
+  return kTRUE;
+
+}
+
+// --------------------------------------------------------------------------
+//
+Bool_t MHCalibrationRelTimeCam::Fill(const MParContainer *par, const Stat_t w)
+{
+
+  MArrivalTime *arrtime = (MArrivalTime*)par;
+  if (!arrtime)
+    {
+      gLog << err << "No argument in MArrivalTime::Fill... abort." << endl;
+      return kFALSE;
+    }
+  
+  const Int_t n = arrtime->GetSize();
+  
+  if (fArray->GetEntries()==0)
+    {
+      fArray->Expand(n);
+      
+      for (Int_t i=0; i<n; i++)
+        {
+          (*fArray)[i] = new MHCalibrationRelTimePix;
+          MHCalibrationRelTimePix &hist = (*this)[i];
+          hist.ChangeHistId(i);
+          hist.InitBins();
+        }
+    }
+  
+  if (fArray->GetEntries() != n)
+    {
+      gLog << err << "ERROR - Size mismatch... abort." << endl;
+      return kFALSE;
+    }
+  
+  for (Int_t i=0; i<n; i++)
+    {
+
+      const Float_t reltime = (*arrtime)[i] - (*arrtime)[1];
+
+      MHCalibrationRelTimePix  &hist = (*this)[i];
+      hist.FillHistAndArray(reltime);
+    }
+  
+  return kTRUE;
+}
+
+Bool_t MHCalibrationRelTimeCam::Finalize()
+{
+    for (Int_t i=0; i<fArray->GetSize(); i++)
+    {
+
+        MHCalibrationRelTimePix &hist = (*this)[i];
+
+        //
+        // 1) Return if the charge distribution is already succesfully fitted
+        //    or if the histogram is empty
+        //
+        if (hist.IsGausFitOK() || hist.IsEmpty())
+          continue;
+
+        //
+        // 2) Fit the Hi Gain histograms with a Gaussian
+        //
+        hist.FitGaus();
+        
+        //
+        // 3) If fit does not succeed , bypass the fit and take the histogram means and sigmas
+        //
+        if (!hist.IsGausFitOK())
+          hist.BypassFit();
+
+        //
+        // 4) Create the fourier transform of the arrays 
+        //
+        hist.CreateFourierSpectrum();
+
+        //
+        // 5) Renormalize to the real time in ns.
+        //
+        hist.Renorm(fTimeSliceWidth);
+
+    }
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// The types are as follows:
+// 
+// Fitted values:
+// ============== 
+//
+// 0: Fitted Mean Relative Arrival Time
+// 1: Error of fitted Mean Relative Arrival Time
+// 2: Sigma of fitted Relative Arrival Time
+// 3: Error of Sigma of fitted Relative Arrival Time
+//
+//
+// Useful variables derived from the fit results:
+// =============================================
+//
+// 4: Returned probability of Gauss fit to Charge distribution
+//
+// Localized defects:
+// ==================
+//
+// 5: Gaus fit not OK
+// 6: Fourier spectrum not OK
+//
+Bool_t MHCalibrationRelTimeCam::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
+{
+
+  if (fArray->GetSize() <= idx)
+    return kFALSE;
+
+  switch (type)
+    {
+    case 0:
+      val = (*this)[idx].GetMean();
+      break;
+    case 1:
+      val = (*this)[idx].GetMeanErr();
+      break;
+    case 2:
+      val = (*this)[idx].GetSigma();
+      break;
+    case 3:
+      val = (*this)[idx].GetSigmaErr();
+      break;
+    case 4:
+      val = (*this)[idx].GetProb();
+      break;
+    case 5:
+      if (!(*this)[idx].IsGausFitOK())
+        val = 1.;
+      break;
+    case 6:
+      if (!(*this)[idx].IsFourierSpectrumOK())
+        val = 1.;
+      break;
+    default:
+      return kFALSE;
+    }
+  return kTRUE;
+}
+
+void MHCalibrationRelTimeCam::DrawPixelContent(Int_t idx) const
+{
+  (*this)[idx].DrawClone();
+}
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.h	(revision 3177)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimeCam.h	(revision 3177)
@@ -0,0 +1,52 @@
+#ifndef MARS_MHCalibrationRelTimeCam
+#define MARS_MHCalibrationRelTimeCam
+
+#ifndef ROOT_TObjArray
+#include <TObjArray.h>
+#endif
+
+#ifndef MARS_MH
+#include "MH.h"
+#endif
+#ifndef MARS_MCamEvent
+#include "MCamEvent.h"
+#endif
+
+class MHCalibrationRelTimePix;
+class MHCalibrationRelTimeCam : public MH, public MCamEvent
+{
+
+private:
+
+  static const Float_t fgTimeSliceWidth;          // Default FADC slice time width for MAGIC
+  Float_t              fTimeSliceWidth;          //  FADC slice time width
+  
+  TObjArray  *fArray;       //-> List of MHCalibrationRelTimePix's
+
+public:
+
+  MHCalibrationRelTimeCam(const char *name=NULL, const char *title=NULL);
+  ~MHCalibrationRelTimeCam();
+
+  void Clear(Option_t *o="");
+  
+  MHCalibrationRelTimePix &operator[](UInt_t i);
+  const MHCalibrationRelTimePix &operator[](UInt_t i) const;
+  
+  Bool_t SetupFill(const MParList *pList);
+  Bool_t Fill(const MParContainer *par, const Stat_t w=1);
+  Bool_t Finalize();
+
+  // Setters
+  void SetTimeSliceWidth(const Float_t width=fgTimeSliceWidth)  {  fTimeSliceWidth = width; }
+  
+  TObject *Clone(const char *) const;
+  
+  Bool_t GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type=0) const;
+  void DrawPixelContent(Int_t idx) const;
+  
+  ClassDef(MHCalibrationRelTimeCam, 1)	// Storage Container for all pedestal information of the camera
+};
+
+#endif
+
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.cc	(revision 3177)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.cc	(revision 3177)
@@ -0,0 +1,138 @@
+/* ======================================================================== *\
+!
+! *
+! * 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 02/2004 <mailto:markus@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//  MHCalibrationRelTimePix
+//
+//  Histogram class for the relative arrival time analysis. 
+//  Holds the histogrammed arrival times,
+//  derives from MHGausEvents, perform Fourier analysis
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MHCalibrationRelTimePix.h"
+
+#include <TH1.h>
+
+ClassImp(MHCalibrationRelTimePix);
+
+using namespace std;
+//
+const Int_t   MHCalibrationRelTimePix::fgChargeNbins    = 900;
+const Axis_t  MHCalibrationRelTimePix::fgChargeFirst    = -13.;
+const Axis_t  MHCalibrationRelTimePix::fgChargeLast     =  13.;
+
+const Int_t   MHCalibrationRelTimePix::fgPulserFrequency = 200;
+// --------------------------------------------------------------------------
+//
+// Default Constructor. 
+//
+MHCalibrationRelTimePix::MHCalibrationRelTimePix(const char *name, const char *title) 
+    : fPixId(-1)
+{ 
+
+  fName  = name  ? name  : "MHCalibrationRelTimePix";
+  fTitle = title ? title : "Histogrammed Calibration Relative Arrival Time events";
+
+  SetChargeNbins();
+  SetChargeFirst();
+  SetChargeLast();
+
+  // Create a large number of bins, later we will rebin
+  fHGausHist.SetName("HCalibrationRelTimeCharge");
+  fHGausHist.SetTitle("Distribution of Relative Arrival Times Pixel ");
+  fHGausHist.SetXTitle("FADC Slice");
+  fHGausHist.SetYTitle("Nr. of events");
+  fHGausHist.Sumw2();
+
+  SetPulserFrequency();
+}
+
+MHCalibrationRelTimePix::~MHCalibrationRelTimePix()
+{
+}
+
+void MHCalibrationRelTimePix::Clear(Option_t *o)
+{
+
+  fPixId = -1;
+  MHGausEvents::Clear();
+  return;
+}
+
+
+
+void MHCalibrationRelTimePix::InitBins()
+{
+
+  fHGausHist.SetBins(fChargeNbins,fChargeFirst,fChargeLast);
+
+}
+
+
+void MHCalibrationRelTimePix::ChangeHistId(Int_t id)
+{
+
+  fPixId = id;
+
+  fHGausHist.SetName(  Form("%s%d", fHGausHist.GetName(),  id));
+  fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
+
+}
+
+
+void  MHCalibrationRelTimePix::SetPulserFrequency(Float_t f)
+{
+  SetEventFrequency(f);
+}
+
+
+void  MHCalibrationRelTimePix::BypassFit()
+{
+
+  //
+  // In case, we do not obtain reasonable values 
+  // with the fit, we take the histogram values
+  //
+  SetMean(      fHGausHist.GetMean()                              );
+  SetMeanErr(   fHGausHist.GetRMS() / fHGausHist.GetEntries()     );
+  SetSigma(     fHGausHist.GetRMS()                               );
+  SetSigmaErr(  fHGausHist.GetRMS() / fHGausHist.GetEntries() /2. );
+
+}
+
+// ----------------------------------------------------------------------
+//
+// Renorm the results from FADC slices to times in ns.
+//
+void MHCalibrationRelTimePix::Renorm(const Float_t slicewidth)
+{
+
+  SetMean(     GetMean()    * slicewidth  );
+  SetMeanErr(  GetMeanErr() * slicewidth  );
+  SetSigma(    GetSigma()   * slicewidth  );
+  SetSigmaErr( GetSigmaErr()* slicewidth  );
+  
+}
+
Index: /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.h	(revision 3177)
+++ /trunk/MagicSoft/Mars/mcalib/MHCalibrationRelTimePix.h	(revision 3177)
@@ -0,0 +1,52 @@
+#ifndef MARS_MHCalibrationRelTimePix
+#define MARS_MHCalibrationRelTimePix
+
+#ifndef MARS_MHGausEvents
+#include "MHGausEvents.h"
+#endif
+
+class MHCalibrationRelTimePix : public MHGausEvents
+{
+
+private:
+
+  static const Int_t   fgPulserFrequency;
+
+  static const Int_t   fgChargeNbins;
+  static const Axis_t  fgChargeFirst;
+  static const Axis_t  fgChargeLast;
+
+  Int_t   fChargeNbins;
+  Axis_t  fChargeFirst;
+  Axis_t  fChargeLast;
+
+  Int_t fPixId;                  // Pixel Nr
+
+public:
+
+  MHCalibrationRelTimePix(const char *name=NULL, const char *title=NULL);
+  ~MHCalibrationRelTimePix();
+
+  void Clear(Option_t *o="");
+  void InitBins();
+  
+  // Setters
+  void SetChargeNbins(const Int_t  bins =fgChargeNbins)    { fChargeNbins = bins; }
+  void SetChargeFirst(const Axis_t first=fgChargeFirst)    { fChargeFirst = first; }
+  void SetChargeLast( const Axis_t last =fgChargeLast)     { fChargeLast  = last; }
+
+  void SetPulserFrequency(Float_t f=fgPulserFrequency);
+
+  // Fits
+  void BypassFit();
+  
+  // Others
+  void ChangeHistId(Int_t i);
+  void Renorm(const Float_t slicewidth);
+  
+  //  TObject *DrawClone(Option_t *opt="") const;
+  
+  ClassDef(MHCalibrationRelTimePix, 1)     // Histograms for each calibrated pixel
+};
+
+#endif
Index: /trunk/MagicSoft/Mars/mcalib/Makefile
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/Makefile	(revision 3176)
+++ /trunk/MagicSoft/Mars/mcalib/Makefile	(revision 3177)
@@ -49,4 +49,6 @@
            MHCalibrationChargePix.cc \
            MHCalibrationChargePINDiode.cc \
+           MHCalibrationRelTimeCam.cc \
+           MHCalibrationRelTimePix.cc \
 	   MMcCalibrationCalc.cc \
 	   MExtractPINDiode.cc \
