#include "TROOT.h"
 
#include "TFile.h"
 
#include "TBenchmark.h"
 
#include "TH1F.h"
 
#include "TH2F.h"
 
#include "TCanvas.h"
 
#include "TStyle.h"
 
#include "TPad.h"
 
#include "TF1.h"
 
#include "TGraph.h"
 
#include "TSpectrum.h"
 
#include "stdio.h"
 
 
 
//#include "../RTUtil.h"
 
 
 
int plot_tdc(char* filename = "test", char *plopt="t", int chXstart=0, int chXend=7, int chYstart=0, int chYend=7)
 
{
 
  const int c_nChannels = 64;
 
  char fullname[256];
 
  
 
  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->SetOptStat("ne");
 
        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();
 
        gStyle->SetPadRightMargin(0.15);
 
        gStyle->SetPadLeftMargin(0.12);
 
        //gStyle->SetTitleYOffset(1.4);
 
        
 
        if( strchr(plopt
, 't') != NULL 
) {    
    TCanvas* canvas1 = new TCanvas("canvas1","canvas1",1000,1000);
 
    TH2F* htdc = (TH2F*) rootfile->Get("htdc");
 
    canvas1->cd();
 
    htdc->Draw("colz");
 
    
 
    TH2F* h_correctedTDC = (TH2F*) rootfile->Get("h_correctedTDC");
 
    TCanvas* canvas6 = new TCanvas("canvas6","canvas6",500,500);
 
    canvas6->cd();
 
    h_correctedTDC->Draw("colz");
 
    
 
    TH1D* channelY = htdc->ProjectionX("",54,54);
 
    channelY->SetStats(0);
 
    channelY->SetTitle(";TDC [ns];Events");
 
    channelY->GetYaxis()->SetTitleOffset(1.7);
 
    //channelY->GetXaxis()->SetRangeUser(-40,40);
 
    //channelY->Draw();
 
 
 
    TF1* f_gaus = new TF1("f_gaus","gaus(0)+gaus(3)",-10,20);
 
    f_gaus->SetParNames("Constant1","Mean1","Sigma1","Constant2","Mean2","Sigma2");
 
    f_gaus->SetParameters(channelY->GetMaximum(), channelY->GetBinCenter(channelY->GetMaximumBin()), 0.5,
 
                          channelY->GetMaximum(), channelY->GetBinCenter(channelY->GetMaximumBin())/10.0, 0.05);
 
    /*channelY->Fit(f_gaus,"0ql","",f_gaus->GetParameter(1)-0.1,f_gaus->GetParameter(1)+0.05);
 
    channelY->Fit(f_gaus,"ql","",f_gaus->GetParameter(1)-0.1,f_gaus->GetParameter(1)+0.05);
 
    channelY->Fit(f_gaus,"0qlr","",-20,20);
 
    channelY->Fit(f_gaus,"qlr","",f_gaus->GetParameter(1)-2*f_gaus->GetParameter(2),
 
                                 f_gaus->GetParameter(1)+2*f_gaus->GetParameter(2));*/
 
  }
 
  
 
  if( strchr(plopt
, 'all') != NULL 
) {  
    TCanvas *canvas2 = new TCanvas("canvas2","Hits x;;",2000,2000);
 
    TCanvas *canvas3 = new TCanvas("canvas3","Hits y;;",2000,2000);
 
    canvas2->Divide(8,8);
 
    canvas3->Divide(8,8);
 
    TH1F* h_hitsx;
 
    TH1F* h_hitsy;
 
    for(int i=0; i<64; i++) {
 
      canvas2->cd(i+1);
 
      char hname[128];
 
      h_hitsx = (TH1F*)rootfile->Get(hname);
 
      h_hitsx->Draw();
 
      canvas3->cd(i+1);
 
      h_hitsy = (TH1F*)rootfile->Get(hname);
 
      h_hitsy->Draw();  
 
    }
 
  }
 
  
 
  if( strchr(plopt
, 'x') != NULL 
) {  
    TCanvas *canvas10 = new TCanvas("canvas10","Ch x;;",500,500);
 
    TH1F* h_hitsx;
 
    canvas10->cd();
 
    char hname[128];
 
    sprintf(hname
, "hnhitsx%d", chXstart
);  
    h_hitsx = (TH1F*)rootfile->Get(hname);
 
    h_hitsx->Draw(); 
 
  }
 
  
 
  if( strchr(plopt
, 'y') != NULL 
) {  
    TCanvas *canvas11 = new TCanvas("canvas11","Ch x;;",500,500);
 
    TH1F* h_hitsy;
 
    canvas11->cd();
 
    char hname[128];
 
    sprintf(hname
, "hnhitsy%d", chXstart
);  
    h_hitsy = (TH1F*)rootfile->Get(hname);
 
    h_hitsy->Draw();
 
  }
 
  
 
  if( strstr(plopt
, "share") != NULL 
) {  
   /*TCanvas *canvas4 = new TCanvas("canvas1","canvas1",1000,1000);
 
   int nChannels = chYend-chYstart+1;
 
   int ncols = nChannels/2;
 
   printf("nch %d nch\\2 %d\n", nChannels, ncols);
 
   canvas4->Divide(2,ncols);
 
   TH1F* h_hitsy;
 
   for(int i=chYstart; i<=chYend; i++){
 
     canvas4->cd(i-chYstart+1);
 
     char hname[128];
 
     int chPosition = map[0][i];
 
     sprintf(hname, "hnhitsy%d", chPosition);
 
     h_hitsy = (TH1F*)rootfile->Get(hname);
 
     h_hitsy->Draw();
 
   }*/
 
   
 
   TCanvas *canvas5 = new TCanvas("canvas5","canvas5",500,500);
 
   canvas5->cd();
 
   for(int i=chYstart; i<=chYend; i++) {
 
     TH1F* h_hitsy;
 
     char hname[128];
 
     int chPosition = map[chXstart][i];
 
     sprintf(hname
, "hnhitsy%d", chPosition
);  
     h_hitsy = (TH1F*)rootfile->Get(hname);
 
     h_hitsy->SetTitle("Scan Y;y [mm]; Entries");
 
     h_hitsy->GetYaxis()->SetTitleOffset(1.3);
 
     h_hitsy->SetStats(0);
 
     if (i == chYstart) 
 
      h_hitsy->Draw();
 
     else {
 
      h_hitsy->SetLineColor(i+1);
 
      h_hitsy->Draw("same");
 
     }
 
   }
 
   sprintf(fullname
, "ps/%s_Yshare.eps", filename
);  
         canvas5->SaveAs(fullname);
 
  }
 
  
 
  if (strstr(plopt
, "2d") != NULL
) {  
    
 
    int nX = chXend - chXstart + 1;
 
    int nY = chYend - chYstart + 1;
 
    TCanvas* canvas7 = new TCanvas("canvas7","canvas7", nX*250,nY*250);
 
    printf("nx %d ny %d\n",nX
,nY
);  
    canvas7->Divide(nX,nY);
 
    for(int i=chYstart; i<=chYend; i++) {
 
      for(int j=chXstart; j<=chXend; j++) {
 
      int canvasPosition = nX*(i-chYstart)+(j-chXstart)+1;
 
      printf("canvas %d\n",canvasPosition
);  
        canvas7->cd(canvasPosition);
 
        char hname[128];
 
        int chPosition = map[j][i];
 
        sprintf(hname
, "h2d%d", chPosition
);  
        TH2F* h_2d = (TH2F*)rootfile->Get(hname);
 
        h_2d->Draw("colz");
 
      } //x
 
    }
 
  /*  
 
    TCanvas* canvas8 = new TCanvas("canvas8","canvas8", 1000,1000);
 
      char hname[128];
 
      int chPosition = map[0][7];
 
      sprintf(hname, "h2d%d", chPosition);
 
      TH2F* h_2d1 = (TH2F*)rootfile->Get(hname);
 
      chPosition = map[0][8];
 
      sprintf(hname, "h2d%d", chPosition);
 
      TH2F* h_2d2 = (TH2F*)rootfile->Get(hname);
 
      h_2d1->SetOption("colz");
 
      h_2d1->Draw();
 
      h_2d2->SetOption("colz");
 
      h_2d2->Draw("same");*/
 
  }
 
  
 
  if( strstr(plopt
, "sum") != NULL 
) {  
    int nX = chXend - chXstart + 1;
 
    int nY = chYend - chYstart + 1;
 
          TCanvas* canvas12 = new TCanvas("canvas12","c2",1000,1000);
 
          gStyle->SetOptStat(11);
 
          //c2->Divide(1,4);
 
          //TVirtualPad *pad=c2->cd();
 
          //pad->SetLogz();
 
          TH2F* h[64];
 
          for(int i=chYstart; i<=chYend; i++) {
 
      for(int j=chXstart; j<=chXend; j++) {
 
      int chPosition = map[j][i];
 
      char hname[128];
 
            sprintf(hname
, "h2d%d", chPosition
);    
            int histogram = nX*(i-chYstart)+(j-chXstart);
 
            h[histogram] = (TH2F *) rootfile->Get(hname);
 
            if (histogram) h[0]->Add(h[histogram]);
 
    }
 
    }
 
    h[0]->Draw("COLZ");
 
  }
 
  
 
  if (strstr(plopt
, "beam") != NULL
) {  
  
 
  TCanvas* canvas9 = new TCanvas("canvas9","canvas9", 500,500);
 
  char hname[128];
 
  TH1F* h_laser = (TH1F*)rootfile->Get(hname);
 
  h_laser->Draw();
 
  h_laser->SetStats(1);
 
  
 
  TF1* err = new TF1("err","[0]+[1]*TMath::Erf((x-[2])/[3])");
 
  err->SetParameters(0,h_laser->GetMaximum(),17.04,0.0001);
 
  h_laser->Fit(err,"l");
 
  }
 
    
 
  return(0);
 
}