#ifndef MARS_MJCalibration
#define MARS_MJCalibration

#ifndef MARS_MCalibrationChargeCam
#include "MCalibrationChargeCam.h"
#endif
#ifndef MARS_MCalibrationRelTimeCam
#include "MCalibrationRelTimeCam.h"
#endif
#ifndef MARS_MCalibrationQECam
#include "MCalibrationQECam.h"
#endif
#ifndef MARS_MBadPixelsCam
#include "MBadPixelsCam.h"
#endif

class TCanvas;
class MHCamera;
class MCamEvent;
class MRunIter;
class MParList;
class MPedestalCam;
class MJCalibration : public MParContainer
{
private:

  TString fOutputPath;                                     // Path to the output files
  
  MRunIter *fRuns;                                         // Calibration files
  
  MBadPixelsCam          fBadPixels;                       // Bad Pixels cam, can be set from previous runs
  MCalibrationChargeCam  fCalibrationCam;                  // Calibration conversion factors FADC2Phe
  MCalibrationQECam      fQECam;                           // Quantum efficiency, can be set from previous runs
  MCalibrationRelTimeCam fRelTimeCam;                      // Calibration constants rel. times

  MCalibrationCam::PulserColor_t fColor;                   // Colour of the pulsed LEDs

  enum  Display_t   { kFullDisplay, kDataCheckDisplay, kNormalDisplay }; // Possible Display types
  
  Display_t fDisplayType;                                  // Chosen Display type

  Bool_t fRelTimes;                                        // Flag if relative times have to be calibrated
  Bool_t fDataCheck;                                       // Flag if the data check is run on raw data
  UInt_t fExtractorLevel;                                  // Level signal extractor (e.g. MExtractSignal2) 
  UInt_t fArrivalTimeLevel;                                // Level arr. time extractor (e.g. MArrivalTimeCalc2)
  
  void DrawProjection   ( MHCamera *obj, Int_t fit) const; // Draw projection of pixels values
  void DrawRadialProfile( MHCamera *obj)            const; // Draw projection of pixels values onto camera radius
  void CamDraw(TCanvas &c, const Int_t x, const Int_t y, const MHCamera &cam1,
               const Int_t fit, const Int_t rad=0);        // Draw histograms into the MStatusDisplay
  
  void   DisplayResult(MParList &plist);
  Bool_t WriteResult();

public:

  MJCalibration(const char *name=NULL, const char *title=NULL);
  
  void SetInput(MRunIter *iter) { fRuns=iter; }
  void SetOutputPath(const char *path=".");
  
  TString GetOutputFile() const;

  MCalibrationChargeCam  &GetCalibrationCam()     { return fCalibrationCam; }  
  MCalibrationRelTimeCam &GetRelTimeCam()         { return fRelTimeCam;     }
  MCalibrationQECam      &GetQECam()              { return fQECam;          }    
  const MBadPixelsCam    &GetBadPixels() const    { return fBadPixels;      }
  
  void SetBadPixels(const MBadPixelsCam &bad)    { bad.Copy(fBadPixels);   }
  void SetQECam    (const MCalibrationQECam &qe) { qe.Copy(fQECam);        }    
  void SetColor    (const MCalibrationCam::PulserColor_t color) { fColor = color; }

  // Displays
  void SetFullDisplay()      { fDisplayType = kFullDisplay;      }
  void SetDataCheckDisplay() { fDisplayType = kDataCheckDisplay; }
  void SetNormalDisplay()    { fDisplayType = kNormalDisplay;    }

  // Rel. Time
  void SetRelTimeCalibration(const Bool_t b=kTRUE) { fRelTimes         = b; }

  // Data Check
  void SetDataCheck         (const Bool_t b=kTRUE) { fDataCheck        = b; SetDataCheckDisplay(); }

  // Extractors
  void SetExtractorLevel    (const UInt_t i=1    ) { fExtractorLevel   = i; }
  void SetArrivalTimeLevel  (const UInt_t i=1    ) { fArrivalTimeLevel = i; }  
      
  
  Bool_t ReadCalibrationCam();
  Bool_t ProcessFile( MPedestalCam &pedcam );
  Bool_t Process    ( MPedestalCam &pedcam );
  
  ClassDef(MJCalibration, 0) // Tool to run a calibration per pulser colour and intensity
};

#endif
