#ifndef _TDC_H_
#define _TDC_H_

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TROOT.h"
#include "TString.h"

//#include "TOPreco.h"


class TDC {
private:
   int _ch_num;
   int _tdc_num;
   float pdf_min;

   std::vector< std::vector<float> > pdf;

   float sum_all_content , sum_all_pdf;
   std::vector<float> sum_ch_pdf;

   std::string pdf_filename;
   bool b_pdffile;

   //variable for plot
   int stat , l_col , l_w , l_sty;
   float scale;
   TString title , xtitle , ytitle;

   bool flag_ring;
   std::vector< TH1F* > h_pdf;
   TH2F *h_ring;

public:
   TDC( int ch_num , int tdc_num );
   ~TDC( void ){};

   //--General--//
   void Print( void );
   void Write( std::string outpdf_file , std::string header = "<pdf file>" ); //Output pdf[][] to file
   void SetPdfMin( float min = 0 ){ pdf_min = min; };

   float Integral( int ch = -1 , int start_bin = 0 , int end_bin = 4096 ); //ch=-1:all
   float GetSum( int ch = -1 ){ if( ch < 0 ){ return sum_all_pdf; }else{ return sum_ch_pdf[ch]; } }; //ch=-1:all
   float GetPdf( int ch , int tdc ){ return pdf[ch][tdc]; }; //PDF with CH and TDC count from PDF file
   std::vector< std::vector<float> > GetPdf( void ){ return pdf; }; //PDF with CH and TDC count from PDF file

   std::vector< TH1F* > GetHist( void ){ return h_pdf; };
   float GetSumContent( void ){ return sum_all_content; };

   void Clear( void );

   //-- Make PDF file --//
   //PDF from Data root file 
   void SetRootFile( TFile *root_file , std::string cut = "" );
   void SetRootFile( TFile *root_file , std::string cut = "" , int start_bin = 0, int end_bin = 4096 );//for cut TDC minus window
   //PDF from MC root file 
   void SetRootMCFile( TFile *f , std::string cut = "" );
   //PDF from list
   void SetPdfFile( std::string file );

   //PDF from TOPreco
   //void SetPdfReco( float *pos , float *ang , float mom , float beta = 1. );

   //-- PDF controll --//
   void SetTdcOffset( std::vector<int> offset );
   void SetTdcOffset( int offset );

   //-- Draw Histgram --//
   void SetStats( int st ){ stat = st;};
   void SetLineColor( int col ){ l_col = col;};
   void SetLineStyle( int sty ){ l_sty = sty;};
   void SetLineWidth( int w ){ l_w = w;};
   void SetTitle( TString t ){ title = t;};
   void SetXTitle( TString t ){ xtitle = t;};
   void SetYTitle( TString t ){ ytitle = t;};
   void Scale( float s ){ scale = s;};

   void Draw( int ch , int start_bin = 0 , int end_bin = 4096 , TString option = "" );
   void DrawPdf( int ch , int start_bin = 0 , int end_bin = 4096 , TString option = "" );
   void DrawRingImage( TString option = "colz" , int tdc_start_bin = 0 , int tdc_end_bin = 4096 , int ch_start = 0 , int ch_end = 36 );

};

#endif //_TDC_H_
