#include "TROOT.h"
 
#include "TFile.h"
 
#include "TBenchmark.h"
 
#include "TH1F.h"
 
#include "TH2F.h"
 
#include "TH3F.h"
 
#include "TCanvas.h"
 
#include "TStyle.h"
 
#include "TPad.h"
 
#include "TF1.h"
 
#include "TGraph.h"
 
#include "TSpectrum.h"
 
#include "stdio.h"
 
 
 
//#include "include/RTUtil.h"
 
 
 
int tdc(char filename[256] = "test", int chX=0, int chY=0, double rangeLeft=-16, double rangeRight=16, bool debug = false)
 
{
 
  //const int c_nChannels = 64;
 
  //const double c_xOffset = 2.2; // mm
 
  //const double c_yOffset = 2.3;
 
  
 
  int map[8][8]={{32,34,53,55,40,42,61,63},
 
                 {48,50,37,39,56,58,45,47},
 
                 {33,35,52,54,41,43,60,62},
 
                 {49,51,36,38,57,59,44,46},
 
                 {17,19,4,6,25,27,12,14},
 
                 {1,3,20,22,9,11,28,30},
 
                 {16,18,5,7,24,26,13,15},
 
                 {0,2,21,23,8,10,29,31}
 
                };
 
  
 
  char fnameroot[256];
 
  TFile* rootfile;
 
        sprintf(fnameroot
, "root/%s.root", filename
);   
        rootfile = (TFile *) gROOT->FindObject(filename); 
 
        if(rootfile==NULL) rootfile = new TFile(fnameroot);
 
        if(rootfile==NULL) {
 
          printf("Cannot open root file %s!!!\n",fnameroot
);   
          return(0);
 
        }
 
 
 
        // set draw style
 
        gStyle->SetPalette(1, 0);
 
        
 
        gStyle->SetPaperSize(TStyle::kA4);
 
        gStyle->SetStatBorderSize(1);
 
        gStyle->SetFrameBorderMode(0);
 
        gStyle->SetFrameFillColor(0);
 
        gStyle->SetCanvasBorderMode(0);
 
        gStyle->SetPadBorderMode(0);
 
        gStyle->SetPadColor(0);
 
        gStyle->SetCanvasColor(0);
 
        gStyle->SetStatColor(0);
 
        gStyle->SetOptFit(11);
 
        gStyle->SetOptStat("ne");
 
        gStyle->SetPadTopMargin(0.15);
 
        gStyle->SetPadBottomMargin(0.15);
 
        gStyle->SetPadRightMargin(0.15);
 
        gStyle->SetPadLeftMargin(0.15);
 
        //gStyle->SetTitleYOffset(1.4);
 
         
 
    TCanvas* canvas1 = new TCanvas("canvas1","canvas1",1000,1000);
 
    TH2F* htdc = (TH2F*) rootfile->Get("htdc");
 
    canvas1->cd();
 
    htdc->Draw("colz");
 
    
 
    TH3F* h_3D = (TH3F*) rootfile->Get("h_correctedTDC");
 
    TCanvas* canvas2 = new TCanvas("canvas2","canvas2",800,800);
 
    canvas2->cd();
 
    //h_correctedTDC->Draw("colz");
 
    
 
    TH2D* h_correctedTDC = (TH2D*) h_3D->Project3D("xz");
 
    h_correctedTDC->SetTitle("; t [ns]; Channel");
 
    h_correctedTDC->Draw("colz");
 
    TCanvas* canvas3 = new TCanvas("canvas3","canvas3",800,800);
 
    canvas2->cd(2);
 
    TH1D* h_allTDCbins = h_correctedTDC->ProjectionX("", 1, 64);
 
    h_allTDCbins->Draw();
 
    /*
 
    int binY = map[chX][chY];
 
    TH1D* channelY = h_correctedTDC->ProjectionX("",binY+1,binY+1);
 
    channelY->SetStats(0);
 
    
 
    char title[256];
 
    sprintf(title,"Channel %d;t [ns];Events", binY);
 
    channelY->SetTitle(title);
 
    channelY->GetYaxis()->SetTitleOffset(1.7);
 
    TAxis* xAxis = h_correctedTDC->GetXaxis();
 
    int range = xAxis->GetBinUpEdge(xAxis->GetLast()+1);
 
    channelY->GetXaxis()->SetRangeUser(-range, range);
 
    channelY->Draw();
 
 
 
    TF1* f_gaus1 = new TF1("f_gaus1","[0] + gaus(1)", rangeLeft,rangeRight);
 
    TF1* f_gaus2 = new TF1("f_gaus2","[0] + gaus(1) + gaus(4)",-8,8);
 
    f_gaus1->SetParNames("Linear","Norm","#mu","#sigma");
 
    f_gaus2->SetParNames("Linear","Norm1","Mean1","Sigma1","Norm2","Mean2","Sigma2");
 
    Int_t n = channelY->GetMaximum();
 
    Float_t mean = channelY->GetBinCenter(channelY->GetMaximumBin());
 
    
 
    f_gaus1->SetParameters(channelY->GetMinimum(), n, mean, 2.0);
 
    channelY->Fit(f_gaus1,"0");
 
    channelY->Fit(f_gaus1,"r");
 
    */
 
    //Fit in range +- 3 sigma
 
    //channelY->Fit(f_gaus1,"r","", f_gaus1->GetParameter(2)-3*f_gaus1->GetParameter(3),
 
      //                            f_gaus1->GetParameter(2)+3*f_gaus1->GetParameter(3));
 
    
 
 
 
    //f_gaus2->SetParameters(-1, n, mean-0.1, 0.01,
 
      //                         n, mean+0.1, 0.1);
 
    //channelY->Fit(f_gaus2,"0");
 
    //channelY->Fit(f_gaus2,"rl");
 
    // set range to +- 3 sigma
 
    //channelY->Fit(f_gaus,"lr","",f_gaus->GetParameter(2)-3*f_gaus->GetParameter(3),
 
      //                           f_gaus->GetParameter(2)+3*f_gaus->GetParameter(3));
 
                             
 
  return (0);
 
  }