#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 "RTUtil.h"
 
 
 
#define NCH 1
 
#define TDC_BIN (25./1000.) //1 TDC bin in ns
 
#define MIKRO_BIN 0.3595/1000. //1 mikro step in mm
 
 
 
#define HFILL_COLOR 18
 
 
 
void plots(char *fname, char *plopt="atc", double fitw=1.0, char *fitf="g")
 
{
 
        char fullname[256];
 
        
 
        //get ROOT file with histograms
 
        char fnameroot[1024];
 
        TFile * rootfile;
 
        TDirectory *dir;
 
        
 
        sprintf(fnameroot, "root/%s.root", fname); 
 
        rootfile = (TFile *) gROOT->FindObject(fname); 
 
        if(rootfile==NULL) rootfile = new TFile(fnameroot);
 
        if(rootfile==NULL) {
 
          printf("Cannot open root file %s!!!\n",fnameroot); 
 
          return;
 
        }
 
        dir = (TDirectory*) rootfile;
 
        
 
        
 
        // 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->SetPadRightMargin(0.1);
 
        
 
  RTCanvas *c[16];
 
  int cc=-1;
 
  char hname[256];
 
  TH1F *hp1d; TH2F *hp2d;
 
  
 
// ADCs -----------------------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 'a') != NULL ) {
 
        c[++cc] = new RTCanvas("QDC", fname, 50*(cc+1), 0, 600, 850);
 
                //c[cc]->Divide(2,4);
 
                c[cc]->Divide(NCH);
 
                
 
                for(int i=0;i<NCH;i++) {
 
                        c[cc]->cd(i+1); gPad->SetLogy();
 
                                sprintf(hname, "hadcpos%d",i); hp1d = (TH1F *) dir->Get(hname);
 
                                
 
                                hp1d->SetTitle("QDC; QDC; N");
 
                                //(hp1d->GetXaxis())->SetRangeUser(0.,2500.);
 
                                hp1d->DrawCopy();
 
                }       
 
                sprintf(fullname, "ps/%s_QDC.eps", fname);
 
                c[cc]->SaveAs(fullname);
 
  }
 
   
 
// Correlation 2d plots -------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 'c') != NULL ) {                    
 
    c[++cc] = new RTCanvas("TDCvsQDC", fname, 50*(cc+1), 0, 600, 850);
 
                //c[cc]->Divide(3,3);
 
                c[cc]->Divide(NCH);
 
                        
 
                for(int i=0;i<NCH;i++) {
 
                        c[cc]->cd(i+1); gPad->SetLogz();
 
                                sprintf(hname, "hcorpos%d",i); hp2d = (TH2F *) dir->Get(hname);
 
                                
 
                                hp2d->SetTitle("TDCvsQDC; QDC; TDC [ns]");
 
                                (hp2d->GetXaxis())->SetRangeUser(0.,500.);
 
                                //hp2d->GetYaxis()->SetRangeUser(-1.5,5.0);
 
                                hp2d->DrawCopy("COLZ");
 
                }       
 
                sprintf(fullname, "ps/%s_cor.eps", fname);
 
                c[cc]->SaveAs(fullname);
 
  }
 
   
 
   
 
// Fits of corrected TDCs -----------------------------------------------------------------------------
 
 
 
  TF1 *fg = new TF1("fg", "gaus");
 
        TF1 *fgg = new TF1("fgg", "gaus(0)+gaus(3)");
 
        fgg->SetParNames("Constant","Mean","Sigma","Constant2","Mean2","Sigma2");
 
  if( strchr(plopt, 'f') != NULL ) {
 
        c[++cc] = new RTCanvas("Corrected TDCs", fname, 50*(cc+1), 0, 600, 850);
 
                //c[cc]->Divide(2,4);
 
                c[cc]->Divide(NCH);
 
 
 
                for(int i=0;i<NCH;i++) {
 
                  c[cc]->cd(i+1); gPad->SetLogy();
 
                        sprintf(hname, "hctdc%d",i); hp1d = (TH1F *) dir->Get(hname);
 
                        hp1d->SetTitle("cTDC; TDC [ns]; N");
 
                        (hp1d->GetXaxis())->SetRangeUser(-40*TDC_BIN,160*TDC_BIN);
 
                        if( hp1d->GetMaximum() < 50 ) continue;
 
                        //(hp1d->GetXaxis())->SetRangeUser(-0.25,0.75);
 
                        
 
                        if( strcmp(fitf, "g")==0 ) {
 
                        fg->SetParameters(hp1d->GetMaximum(), hp1d->GetBinCenter(hp1d->GetMaximumBin()), 0.05);
 
                        hp1d->Fit(fg, "0ql", "", fg->GetParameter(1)-0.1*fitw, fg->GetParameter(1)+0.05*fitw);
 
                        hp1d->Fit(fg, "ql", "", fg->GetParameter(1)-0.1*fitw, fg->GetParameter(1)+0.05*fitw);
 
                        
 
                        printf("Ch[%d] Sigma =%6.1lfps\n",i,fg->GetParameter(2)*1000.);
 
      } 
 
      else if( strcmp(fitf, "gg")==0 ) {
 
                        //fgg->SetParameters(2600.,0*TDC_BIN,2*TDC_BIN, 100.,0*TDC_BIN,10*TDC_BIN);
 
                        fgg->SetParameters(hp1d->GetMaximum(), hp1d->GetBinCenter(hp1d->GetMaximumBin()), 0.03,
 
                                          hp1d->GetMaximum(), hp1d->GetBinCenter(hp1d->GetMaximumBin())/10., 0.1);
 
                        hp1d->Fit(fgg, "0ql", "", -50*TDC_BIN, 50*TDC_BIN);
 
                        hp1d->Fit(fgg, "ql", "", fgg->GetParameter(1)-2.5*fitw*fgg->GetParameter(2), 
 
                                                 fgg->GetParameter(1)+3.5*fitw*fgg->GetParameter(2));
 
                                                 
 
                        printf("Ch[%d] Sigma =%6.1lfps\n",i,fgg->GetParameter(2)*1000.);
 
/*
 
                  fg->SetRange(-50., 200.);
 
                        fg->SetParameters(fgg->GetParameter(0), fgg->GetParameter(1), fgg->GetParameter(2));
 
                        fg->SetLineColor(2);
 
                        fg->DrawCopy("LSAME");
 
                        fg->SetParameters(fgg->GetParameter(3), fgg->GetParameter(4), fgg->GetParameter(5));
 
                        fg->SetLineColor(3);
 
                        fg->DrawCopy("LSAME");
 
*/
 
      } else {printf("Wrong fit function (parameter 4)!!!\n"); return;}
 
                }       
 
                sprintf(fullname, "ps/%s_cTDC.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  }  
 
 
 
// TDCs -----------------------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 't') != NULL ) {    
 
        c[++cc] = new RTCanvas("Raw TDC", fname, 50*(cc+1), 0, 600, 850);
 
                //c[cc]->Divide(2,4);
 
                c[cc]->Divide(NCH);
 
                        
 
                for(int i=0;i<NCH;i++) {
 
                        c[cc]->cd(i+1); gPad->SetLogy();
 
                                sprintf(hname, "htdcpos%d",i); hp1d = (TH1F *) dir->Get(hname);
 
                                hp1d->SetTitle("TDC; TDC [ns]; N");
 
                                (hp1d->GetXaxis())->SetRangeUser(-2,5);
 
                                hp1d->DrawCopy();
 
                                if( hp1d->GetMaximum() < 50 ) continue;
 
                                
 
                          fg->SetParameters(hp1d->GetMaximum(), hp1d->GetBinCenter(hp1d->GetMaximumBin()), 0.25);         
 
                          hp1d->Fit(fg,"0QL", "", fg->GetParameter(1)-0.5, fg->GetParameter(1)+0.25);
 
                          hp1d->Fit(fg,"QL", "", fg->GetParameter(1)-0.5, fg->GetParameter(1)+0.25);
 
 
 
                    printf("Ch[%d] RAW Sigma =%6.1lfps\n",i,fg->GetParameter(2)*1000.);
 
                }       
 
                sprintf(fullname, "ps/%s_TDC.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  } 
 
  
 
// Y scans -----------------------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 'y') != NULL ) {    
 
        c[++cc] = new RTCanvas("Yscans", fname, 50*(cc+1), 0, 600, 850);
 
                //c[cc]->Divide(2,4);
 
                c[cc]->Divide(NCH);
 
                        
 
                for(int i=0;i<NCH;i++) {
 
                        c[cc]->cd(i+1); //gPad->SetLogy();
 
                                sprintf(hname, "hnhitsy%d",i); 
 
                                hp1d = (TH1F *) dir->Get(hname);
 
                                hp1d->SetTitle("YScan; x; N");
 
                                //(hp1d->GetXaxis())->SetRangeUser(-80*TDC_BIN,200*TDC_BIN);
 
                                  //if(i==0) 
 
                                  hp1d->DrawCopy();
 
          //else hp1d->DrawCopy("SAME");
 
                }       
 
                sprintf(fullname, "ps/%s_Ys.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  } 
 
  
 
// X scans -----------------------------------------------------------------------------------------------
 
  TF1 *ferf = new TF1("ferf","[0]+[1]*(1+TMath::Erf((x-[2])/[3]))");
 
  //int cmap[NCH]={40,1,2,8,4,5,6,41};
 
  int cmap[NCH]={1};
 
  if( strchr(plopt, 'x') != NULL ) {    
 
        c[++cc] = new RTCanvas("Xscans", fname, 50*(cc+1), 0, 600, 430);
 
                //c[cc]->Divide(2,4);
 
                //c[cc]->Divide(NCH);
 
/*    
 
    sprintf(hname, "hnhitsx%d",4); hp1d = (TH1F *) dir->Get(hname);
 
                hp1d->SetTitle("XScan; x; N");
 
                
 
                ferf->SetParameters(130,1600,43.4,1.0);
 
                ferf->SetParNames("Offset","Constant","Mean","Sigma");
 
                hp1d->Fit(ferf,"QL");
 
*/    
 
                        
 
                for(int i=0;i<1;i++) {
 
 
 
                  sprintf(hname, "hnhitsx%d",i); hp1d = (TH1F *) dir->Get(hname);
 
                        hp1d->SetTitle("XScan; x; N");
 
                  hp1d->SetLineColor(cmap[i]);
 
                  (hp1d->GetYaxis())->SetRangeUser(0,hp1d->GetMaximum()*1.2);
 
      if(i==0) hp1d->DrawCopy();
 
      else hp1d->DrawCopy("SAME");
 
 
 
//                      c[cc]->cd(i+1); //gPad->SetLogy();
 
//                              sprintf(hname, "hnhitsx%d",i); hp1d = (TH1F *) dir->Get(hname);
 
//                              hp1d->SetTitle("XScan; x; N");
 
                                //(hp1d->GetXaxis())->SetRangeUser(-80*TDC_BIN,200*TDC_BIN);
 
//                              hp1d->DrawCopy();
 
        }
 
        
 
                sprintf(fullname, "ps/%s_Xs.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  }   
 
 
 
// tdc vs. MCP OUT tdc -----------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 'm') != NULL ) {    
 
        c[++cc] = new RTCanvas("TDCvsMCPOUT", fname, 50*(cc+1), 0, 400, 850);
 
                //c[cc]->Divide(2,4);
 
                c[cc]->Divide(NCH);
 
                        
 
                for(int i=0;i<NCH;i++) {
 
      c[cc]->cd(i+1); gPad->SetLogz();
 
                        sprintf(hname, "hmcpoutcor%d",i); hp2d = (TH2F *) dir->Get(hname);
 
                        hp2d->SetTitle("TDCvsMCPOUT; MCP OUT TDC [ns]; TDC [ns]");
 
                        //(hp1d->GetXaxis())->SetRangeUser(-80*TDC_BIN,200*TDC_BIN);
 
                        hp2d->DrawCopy("COLZ");
 
                }       
 
                sprintf(fullname, "ps/%s_TDCvsMCPOUT.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  } 
 
  
 
// Charge sharing -----------------------------------------------------------------------------------
 
 
 
  if( strchr(plopt, 's') != NULL ) {    
 
        c[++cc] = new RTCanvas("ChargeSharing", fname, 50*(cc+1), 0, 600, 850);
 
                c[cc]->Divide(1,2);
 
                c[cc]->cd(1); gPad->SetLogz();
 
                        sprintf(hname, "hshare"); hp2d = (TH2F *) dir->Get(hname);
 
                        hp2d->SetTitle("ChargeSharing; x [mm]; ADC1/(ADC1+ADC2)");
 
                        hp2d->DrawCopy("COLZ");
 
          
 
          c[cc]->cd(2); //gPad->SetLogy();
 
            TH1F *hprojx = (TH1F*)hp2d->ProjectionX("ProjectionX", 59, 61);
 
                        hprojx->Fit("gaus","QL","", hprojx->GetMean()-hprojx->GetRMS(), hprojx->GetMean()+hprojx->GetRMS());
 
                sprintf(fullname, "ps/%s_share.eps", fname); 
 
                c[cc]->SaveAs(fullname);
 
  }   
 
  
 
        //rootfile->Close();
 
}