#include "TROOT.h"
 
#include "TFile.h"
 
#include "TBenchmark.h"
 
#include "TH1F.h"
 
#include "TH2F.h"
 
gROOT->Reset();
 
 
 
#include "TCanvas.h"
 
#include "TStyle.h"
 
#include "TPad.h"
 
#include "TF1.h"
 
#include "TGraph.h"
 
#include "TSpectrum.h"
 
 
 
#include "DrootHelper.cpp"
 
 
 
                       
 
#define NCH 2
 
 
 
//-------------------------------------------------------------------------------------
 
 
 
void SetGS() {
 
  const UInt_t Number = 2;
 
  Double_t Red[Number]   = {0.00, 1.00};
 
  Double_t Green[Number] = {0.00, 1.00};
 
  Double_t Blue[Number]  = {0.00, 1.00};
 
  Double_t Stops[Number] = {0.00, 1.00};
 
  
 
  Int_t nb=50;
 
  TColor::CreateGradientColorTable(Number, Stops, Red, Green, Blue, nb);
 
}
 
 
 
 
 
//-------------------------------------------------------------------------------------
 
Double_t ConstantStep(Double_t x)
 
{
 
        if(x<=0) return 0.;
 
        else return 1.;
 
}
 
 
 
// =======================================================================================================
 
 
 
void plotCorTDC(TH1F *hp1d, char *hTitle, double fitmin, double fitmax, double drawmin, double drawmax)
 
{
 
        char sbuff[256];
 
        
 
        TLegend *leg1 = new TLegend(0.57,0.76,0.88,0.88);       
 
        leg1->SetFillColor(0);
 
        leg1->SetBorderSize(1); 
 
        leg1->SetTextSize(0.05); 
 
        leg1->SetTextAlign(22);
 
 
 
        //gStyle->SetOptFit(111);
 
        sprintf(sbuff, "%s;Time[ns];", hTitle);
 
        hp1d->SetTitle(sbuff);
 
        //~ hp1d->GetXaxis()->SetRangeUser(2*fitmin, 2*fitmax);
 
        hp1d->GetXaxis()->SetRangeUser(drawmin, drawmax);
 
        //~ hcTDC->DrawClone();
 
 
 
        //~ TF1 *fs = new TF1("fs","pol0(0) + gaus(1)");
 
        
 
        TF1 *fs = new TF1("fs","gaus(0) + ([3]*exp((x)/[4]) + [5]) * (0.5+0.5*TMath::Erf((x-[6])/[7]))");
 
        fs->SetParName(0, "G_Const"); fs->SetParName(1, "G_Mean"); fs->SetParName(2, "G_Sigma");
 
        fs->SetParName(3, "Exp_p0"); fs->SetParName(4, "Exp_p1"); fs->SetParName(5, "Exp_p2");
 
        fs->SetParName(6, "Erf_p0"); fs->SetParName(7, "Erf_p1");
 
//~ 
 
        TF1 *fg1 = new TF1("fg1", "gaus(0)"); fg1->SetNpx(400);
 
        TF1 *fe1 = new TF1("fe1", "[0]*exp((x)/[1]) + [2]");
 
        TF1 *ferf1 = new TF1("ferf1", "[0]*(0.5+0.5*TMath::Erf((x-[1])/[2]))");
 
        TF1 *fe1erf = new TF1("fe1erf", "([0]*exp((x)/[1]) + [2]) * (0.5+0.5*TMath::Erf((x-[3])/[4]))"); fe1erf->SetNpx(400);
 
 
 
        double fitampl = hp1d->GetMaximum();
 
        double fitcenter = hp1d->GetBinCenter(hp1d->GetMaximumBin());
 
 
 
        fs->SetNpx(400);
 
        fs->SetParameters(fitampl, fitcenter, 0.01,   1e2, -0.1, 1e1,   0, 0.05);
 
        
 
        // G + Er + Ex                  
 
        //fs->SetParLimits( 1, -0.1, 0.1);
 
        //fs->SetParLimits( 2, 0.018, 0.5);
 
        fs->SetParLimits( 4, -0.3, 0);
 
        //fs->SetParLimits( 6, -1e-1, 1e-1);
 
        fs->SetParLimits( 7, 0.04, 0.6);
 
        
 
        //~ // G
 
        //~ fs->SetParameters(fitampl/100., fitampl, fitcenter, 0.01);
 
        
 
        hp1d->Fit(fs,"Q","",fitmin, fitmax);
 
 
 
        //~ printf("Chi / NDF = %lf/%lf\n", fs->GetChisquare(), fs->GetNDF());
 
 
 
        fg1->SetParameters(fs->GetParameter(0), fs->GetParameter(1), fs->GetParameter(2));
 
        //~ fe1->SetParameters(fs->GetParameter(3), fs->GetParameter(4), fs->GetParameter(5));
 
        //~ ferf1->SetParameters(1e4, fs->GetParameter(6), fs->GetParameter(7));
 
        fe1erf->SetParameters(fs->GetParameter(3), fs->GetParameter(4), fs->GetParameter(5), fs->GetParameter(6), fs->GetParameter(7));
 
 
 
        fg1->SetLineColor(kRed);    fg1->SetLineWidth(1); fg1->SetLineStyle(1); fg1->SetRange(fitmin, fitmax); fg1->DrawCopy("SAME");
 
        //~ fe1->SetLineColor(kBlue);   fe1->SetLineWidth(1); fe1->SetLineStyle(1); fe1->SetRange(fitmin, fitmax); fe1->DrawCopy("SAME");
 
        //~ ferf1->SetLineColor(kYellow); ferf1->SetLineWidth(1);ferf1->SetLineStyle(1);ferf1->SetRange(fitmin, fitmax);ferf1->DrawCopy("SAME");
 
        fe1erf->SetLineColor(kBlue); fe1erf->SetLineWidth(1);fe1erf->SetLineStyle(1);fe1erf->SetRange(fitmin, fitmax);fe1erf->DrawCopy("SAME");
 
 
 
        double HalfMax = fitampl/2.0;
 
        double MinHalfMax = fs->GetX(HalfMax, -0.5, fitcenter);
 
        double MaxHalfMax = fs->GetX(HalfMax, fitcenter, 1.5);
 
        double FWHM = MaxHalfMax - MinHalfMax;
 
        double sigmaFWHM = fs->GetParameter(2);
 
        printf("Corrected %s resolution FWHM = %.1lf [ps]\n", hTitle, FWHM*1000);
 
        //~ fprintf(fp,"%lf ", FWHM*1000);
 
 
 
        sprintf(sbuff, "FWHM = %.0lf ps", FWHM*1000);
 
        leg1->AddEntry((TF1*)fs->Clone(),sbuff, "L");
 
        leg1->Draw();
 
}
 
 
 
// =======================================================================================================
 
 
 
void plots2(char *fname="480", char *plopt="d", int chich=0, double fitcenter = 1190., double fitw = 70., double comhi_range_hi = 2100., int batch_q=0)
 
{
 
    char sbuff[256];
 
    
 
        char *fitopt="crystal1";
 
        double inmin=-10;
 
         double inmax=10;
 
        double Ecut_lo = 450.;
 
  double Ecut_hi = 625.;
 
  
 
  
 
        int printeps = 0;
 
        int printgif=-1, double fitlo=1.0, double fithi=1.0;
 
        int ch=0;
 
        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;
 
        
 
        DrSetDrawStyle();
 
 
 
  TCanvas *c[64];
 
  int cc=-1;
 
  char hname[256], htitle[256];
 
  TH1F *hp1d, *hp1dcut; TH2F *hp2d;
 
  
 
  TLegend *leg[64];
 
  int legi = -1;
 
  TH1F *legentry[64];
 
  int legentryi = -1;
 
  
 
  TGraph *graph[16];
 
        int igraph=-1;
 
  
 
  int colarr[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, kOrange, 10,11,12,13,14,15,16,17,18,19};
 
  int legsty[] = {1,7,2,3,9,6,9,5,6};
 
  
 
  TF1 *fg = new TF1("fg", "gaus"); fg->SetNpx(300);
 
    
 
        TF1 *fgg = new TF1("fgg", "gaus(0)+gaus(3)"); fg->SetNpx(300);
 
        fgg->SetParName(0,"G1_Const"); fgg->SetParName(3,"G2_Const");
 
        fgg->SetParName(1,"G1_Mean" ); fgg->SetParName(4,"G2_Mean" );
 
        fgg->SetParName(2,"G1_Sigma"); fgg->SetParName(5,"G2_Sigma");
 
        
 
        TF1 *fg3 = new TF1("fg3", "gaus(0)+gaus(3)+gaus(6)"); fg3->SetNpx(300);
 
        fg3->SetParName(0,"G1_Const"); fg3->SetParName(3,"G2_Const"); fg3->SetParName(6,"G3_Const");
 
        fg3->SetParName(1,"G1_Mean" ); fg3->SetParName(4,"G2_Mean" ); fg3->SetParName(7,"G3_Mean" );
 
        fg3->SetParName(2,"G1_Sigma"); fg3->SetParName(5,"G2_Sigma"); fg3->SetParName(8,"G3_Sigma");
 
        
 
        TF1 *fg4 = new TF1("fg4", "gaus(0)+gaus(3)+gaus(6)+gaus(9)"); fg4->SetNpx(300);
 
        fg4->SetParName(0,"G1_Const"); fg4->SetParName(3,"G2_Const"); fg4->SetParName(6,"G3_Const"); fg4->SetParName(9,"G4_Const");
 
        fg4->SetParName(1,"G1_Mean" ); fg4->SetParName(4,"G2_Mean" ); fg4->SetParName(7,"G3_Mean" ); fg4->SetParName(10,"G4_Mean" );
 
        fg4->SetParName(2,"G1_Sigma"); fg4->SetParName(5,"G2_Sigma"); fg4->SetParName(8,"G3_Sigma"); fg4->SetParName(11,"G4_Sigma");
 
        
 
        TF1 *fg5 = new TF1("fg5", "gaus(0)+gaus(3)+gaus(6)+gaus(9)+gaus(12)"); fg5->SetNpx(300);
 
        fg5->SetParName(0,"G1_Const"); fg5->SetParName(3,"G2_Const"); fg5->SetParName(6,"G3_Const"); fg5->SetParName(9,"G4_Const");  fg5->SetParName(12,"G5_Const");
 
        fg5->SetParName(1,"G1_Mean" ); fg5->SetParName(4,"G2_Mean" ); fg5->SetParName(7,"G3_Mean" ); fg5->SetParName(10,"G4_Mean" ); fg5->SetParName(13,"G5_Mean" );
 
        fg5->SetParName(2,"G1_Sigma"); fg5->SetParName(5,"G2_Sigma"); fg5->SetParName(8,"G3_Sigma"); fg5->SetParName(11,"G4_Sigma"); fg5->SetParName(14,"G5_Sigma");
 
    
 
// -------------------------------------------------------------------  
 
 
 
    if( strchr(plopt, 'a') != NULL ) {
 
        
 
        //~ gStyle->SetOptStat(1111);   
 
        gStyle->SetOptStat(0);  
 
        gStyle->SetOptFit(0);   
 
        
 
        c[++cc] = new TCanvas("a", "a", 0, 0, 1600, 900);
 
                c[cc]->Divide(2,2);
 
        
 
        int draw_cuts = 0;
 
                int chmap[] = {0,1,2,31};
 
                
 
        for(int ich = 0;ich<4;ich++) {
 
                        (c[cc]->cd(ich+1))->SetLogy(1);
 
            sprintf(hname, "hadc%d", chmap[ich]);
 
            hp1d = DrTH1F(dir, hname, "");
 
            sprintf(htitle, "ADC%d;ADC;Count", chmap[ich]); hp1d->SetTitle(htitle);
 
            //~ hp1d->GetXaxis()->SetRangeUser(0,2000);
 
            //~ hp1d->GetYaxis()->SetRangeUser(0.9,hp1d->GetMaximum()*1.1);
 
            hp1d->SetLineColor(kBlack);
 
            hp1d->DrawClone();
 
            
 
            if(draw_cuts) {
 
                sprintf(hname, "hadc_cut%d", chmap[ich]);
 
                hp1dcut = DrTH1F(dir, hname, "");
 
                hp1dcut->SetLineColor(kRed);
 
                hp1dcut->DrawClone("SAME");
 
            }
 
                }
 
                       
 
        sprintf(fullname, "gif/%s_a.gif", fname);  c[cc]->SaveAs(fullname);
 
    }
 
    
 
// -------------------------------------------------------------------  
 
 
 
    if( strchr(plopt, 't') != NULL ) {
 
        
 
        gStyle->SetOptStat(1111);       
 
        //~ gStyle->SetOptStat(0);      
 
        gStyle->SetOptFit(0);   
 
        
 
        c[++cc] = new TCanvas("t", "t", 0, 0, 1600, 900);
 
                c[cc]->Divide(2,2);
 
        
 
        int draw_cuts = 0;
 
                int chmap[] = {0,1,2,31};
 
                
 
        for(int ich = 0;ich<4;ich++) {
 
                        (c[cc]->cd(ich+1))->SetLogy(1);
 
            sprintf(hname, "htdc%d", chmap[ich]);
 
            hp1d = DrTH1F(dir, hname, "");
 
            sprintf(htitle, "TDC%d - TRIGGER;Time [ns];Count", chmap[ich]); hp1d->SetTitle(htitle);
 
            hp1d->GetXaxis()->SetRangeUser(-50,50);
 
            //~ hp1d->GetYaxis()->SetRangeUser(0.9,hp1d->GetMaximum()*1.1);
 
            hp1d->SetLineColor(kBlack);
 
            hp1d->DrawClone();
 
            
 
            if(draw_cuts) {
 
                sprintf(hname, "htdc_cut%d", chmap[ich]);
 
                hp1dcut = DrTH1F(dir, hname, "");
 
                hp1dcut->SetLineColor(kRed);
 
                hp1dcut->DrawClone("SAME");
 
            }
 
                }
 
                       
 
        sprintf(fullname, "gif/%s_t.gif", fname);  c[cc]->SaveAs(fullname);
 
    }
 
    
 
// -------------------------------------------------------------------  
 
 
 
    if( strchr(plopt, 'f') != NULL ) {
 
        
 
        gStyle->SetOptStat(1111);       
 
        //~ gStyle->SetOptStat(0);      
 
        gStyle->SetOptFit(0);   
 
        
 
        c[++cc] = new TCanvas("f", "f", 0, 0, 1600, 900);
 
                c[cc]->Divide(2,2);
 
        
 
        int draw_cuts = 0;
 
                int chmap[] = {0,1,2,31};
 
                
 
        for(int ich = 0;ich<4;ich++) {
 
                        (c[cc]->cd(ich+1))->SetLogy(1);
 
            sprintf(hname, "hctdc%d", chmap[ich]);
 
            hp1d = DrTH1F(dir, hname, "");
 
            sprintf(htitle, "corTDC%d - TRIGGER;Time [ns];Count", chmap[ich]); hp1d->SetTitle(htitle);
 
            hp1d->GetXaxis()->SetRangeUser(-30,30);
 
            //~ hp1d->GetYaxis()->SetRangeUser(0.9,hp1d->GetMaximum()*1.1);
 
            hp1d->SetLineColor(kBlack);
 
            hp1d->DrawClone();
 
            
 
            if(draw_cuts) {
 
                sprintf(hname, "hctdc_cut%d", chmap[ich]);
 
                hp1dcut = DrTH1F(dir, hname, "");
 
                hp1dcut->SetLineColor(kRed);
 
                hp1dcut->DrawClone("SAME");
 
            }
 
                }
 
                       
 
        sprintf(fullname, "gif/%s_f.gif", fname);  c[cc]->SaveAs(fullname);
 
    }
 
    
 
// -------------------------------------------------------------------  
 
 
 
    if( strchr(plopt, 'c') != NULL ) {
 
        
 
        //gStyle->SetOptStat(1111);     
 
        gStyle->SetOptStat(0);  
 
        gStyle->SetOptFit(0);   
 
        
 
        c[++cc] = new TCanvas("c", "c", 0, 0, 1600, 900);
 
                c[cc]->Divide(2,2);
 
        
 
        int draw_cuts = 0;
 
                int chmap[] = {0,1,2,31};
 
                
 
        for(int ich = 0;ich<4;ich++) {
 
                        (c[cc]->cd(ich+1))->SetLogz(1);
 
            sprintf(hname, "hcor%d", chmap[ich]);
 
            hp2d = DrTH2F(dir, hname, "");
 
            sprintf(htitle, "TDC%d - TRIGGER;ADC;Time [ns];Count", chmap[ich]); hp2d->SetTitle(htitle);
 
            hp2d->GetXaxis()->SetRangeUser(0,2048);
 
            hp2d->DrawClone("COLZ");
 
                }
 
                       
 
        sprintf(fullname, "gif/%s_c.gif", fname);  c[cc]->SaveAs(fullname);
 
    }
 
//~ // -------------------------------------------------------------------      
 
 
 
    //~ if( strchr(plopt, 'c') != NULL ) {
 
        
 
        gStyle->SetOptStat(1111);       
 
        //~ gStyle->SetOptStat(0);      
 
        //~ gStyle->SetOptFit(0);       
 
        
 
        //~ c[++cc] = new TCanvas("c", "c", 0, 0, 1600, 900);
 
        
 
        //~ int draw_cuts = 0;
 
                
 
        //~ int ich = chich;
 
                        //~ (c[cc]->cd(1))->SetLogz(1);
 
            //~ sprintf(hname, "hcor%d", ich);
 
            //~ hp2d = DrTH2F(dir, hname, "");
 
            //~ sprintf(htitle, "TDC%d - TRIGGER;ADC;Time [ns];Count", ich); hp2d->SetTitle(htitle);
 
            //~ hp2d->GetXaxis()->SetRangeUser(512,2048);
 
            //~ hp2d->GetYaxis()->SetRangeUser(-10,20);
 
            //~ hp2d->DrawClone("COLZ");
 
                
 
                       
 
        //~ sprintf(fullname, "gif/%s_c.gif", fname);  c[cc]->SaveAs(fullname);
 
    //~ }
 
    
 
// -------------------------------------------------------------------  
 
}