//
// CaryatidAna
//
//            by Anatael Cabrera <anatael.cabrera@physics.ox.ac.uk>
//

#ifndef CaryatidAna_h
#define CaryatidAna_h

#include <TFile.h>
#include <TH1.h>
#include <TH2.h>
#include <TCanvas.h>
#include <TLine.h>
#include <TTree.h>
#include <TText.h>
#include <TPolyLine.h>

#include <Caryatid.h>

#include "MessageService/MsgService.h"

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string>

class CaryatidAna {

 public:
  
  //Singleton class:
  static CaryatidAna* InstantiateMe();

  //Executables:
  void DoOfflineAnalysis(const Int_t entriesMax = -1);
  void DoOnlineAnalysis(Char_t * fileName);

 private:

  CaryatidAna();
  ~CaryatidAna();

  //Methods used internally:
  Bool_t   LoadPedTree();
  Bool_t   LoadDAQTrees();
  void     InitAnaVariables();

  Int_t    GetSPData(const Int_t entriesMax);
  void     ProcessCaryatids();
  void     OutputTables();
  void     CompareDAQTables();
  void     CheckDataQuality();
  void     ReportThisGuy();
  void     Report();

  void     GetPlots();

  Float_t  FindFirstBin( const TH1* histogram );
  Float_t  FindLastBin( const TH1* histogram );

  Int_t    GetTPMTsparTh() {return 99;}

  void     CreateReportFile();

 public:

  //
  //Configuration variables:
  //

  Int_t       gTruncType;
  Int_t       gSparsFactor;
  Int_t       gTruncLimit;
  Bool_t      gPlotEachPage;

  //Sparsification/Pedestal tables output:
  Bool_t      gOutputPedestalTable;
  Bool_t      gOutputSparsTable;
  Bool_t      gDebugTable;

  //Pedestal data is labeled as RawVaDigit::EVaErrorCode.kNonStdData. 
  //  Assert flags to ensure that the above is checked for.
  Bool_t      gCheckError2;

  //Comparison with DAQ Tables & set tolerance thresholds:
  Bool_t      gComparePedTable;
  Bool_t      gCompareSparTable;

  Int_t       gTolerancePedTable;
  Int_t       gToleranceSparTable;

  Bool_t      gDoOutliersAnalysis;

  Bool_t      gPlots;
  Bool_t      gQuickPlots;
  Bool_t      gAllRootFile;

  //File name of the run to be analysed:
  std::string     gFileName;

  std::map<Int_t,Caryatid*> mySP;

  Int_t       frun;




 private:

  ofstream    fReportFile;

  static const Int_t ADCMAX = 2000;
  static const Int_t ADCMIN = 1;

  //
  // Class variables:
  //
  TFile*      fFile;

  TCanvas*    fplotFile_c;

  TTree*      fPedRawTree;
  //DAQ tables:
  TTree*      fPedDAQTree;
  TTree*      fSparDAQTree;

  static CaryatidAna* myCaryatidAna;

  Int_t           ferrorNot2;

  //Tree variables:
  Int_t           ferror;
  Int_t           fcrate;
  Int_t           fvarc;
  Int_t           fvmm;
  Int_t           fvfb;
  Int_t           fva;
  Int_t           fvach;
  Int_t           fadc;
  Int_t           ftdc;
  Int_t           fplane;
  Double_t         ftUTC;

  Int_t           pedcrate;
  Int_t           pedvarc;
  Int_t           pedvmm;
  Int_t           pedvfb;
  Int_t           pedva;
  Int_t           pedvach;
  Int_t           pedadc;
  Float_t         pedLoadTime;

  Int_t           sparcrate;
  Int_t           sparvarc;
  Int_t           sparvmm;
  Int_t           sparvfb;
  Int_t           sparva;
  Int_t           sparvach;
  Int_t           sparadc;
  Float_t         sparLoadTime;

  //
  // Analysis variables:  Can I do: "are included in "AnaVariables.h""?????????????
  //
  //With Truncation 1:
  TH1F*     fMeanT1_h;
  TH1F*     fRmsT1_h;

  //With Truncation 2:
  TH1F*     fMeanT2_h;
  TH1F*     fRmsT2_h;

  //Components with truncation:
  TH1F*     fRmsPMT_h;
  TH1F*     fRmsCM_h;
  TH1F*     fRmsPINLG_h;
  TH1F*     fRmsPINHG_h;

  //With no truncation:
  TH1F*     fMeanRaw_h;
  TH1F*     fRmsRaw_h;

  //Component with truncation:
  TH1F*     fRmsRawPMT_h;
  TH1F*     fRmsRawCM_h;
  TH1F*     fRmsRawPINLG_h;
  TH1F*     fRmsRawPINHG_h;

  //Outliers Occupancy:
  TH1F*     fTruncOcc_h;
  TH1F*     fTruncOccCM_h;
  TH1F*     fTruncOccVA_h;
  TH1F*     fOutDetMap_h;

  //Outliers Multiplicity:
  TH1F*    fPerTrunc_h;

  //Sparcification threshoold histogram:
  TH1F*           fSparc_h;
  TH1F*           fSparcRaw_h;
  TH1F*           fSparcLGPIN_h;
  TH1F*           fSparcHGPIN_h;

  //Map of pedestals:
  TH2F*           fMeanMap_h;
  TH2F*           fRmsMap_h; 

  TH2F*           fRmsMapT_h;
 
  TH2F*           fEntriesMap_h;

  TH2F*           fSparcMap_h;

  TH2F*           fOutMap_h;

  TH1F*           fDeltaSparc_h;
  TH2F*           fDeltaSparcMap_h;

  TH1F*           fDeltaMean_h;
  TH2F*           fDeltaCompMean_h;

  TH2F*           fDeltaMeanMapT_h;
  TH2F*           fDeltaRmsMapT_h;

  TH2F*           fErrorMap_h;
  
  TCanvas *fpederrorpad;  
  TPad *fpederrorpad2;  
  TCanvas **fvarccan;
  TPad **fvarcpad;
  TH2F **fvarcevent;
  TLine **fvline;
  TText **fvt;
  TText **fcratenums;
  TPolyLine **f1;

  TLine* fLines[Caryatid::CRATEMAX][Caryatid::VARCMAX];
};

#endif
