//Helper function for making pretty plots
//Renata Kopecna


#include "Design.hpp"

using namespace std;

void design_markers(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    histogram->SetMarkerStyle(8);
    histogram->SetMarkerColor(color);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.9);
    histogram->GetXaxis()->SetTitleOffset(1.0);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetLabelSize(0.045);
    histogram->GetYaxis()->SetLabelSize(0.045);

    histogram->SetLineColor(color);
}
void design_markers(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    histogram->SetMarkerStyle(8);
    histogram->SetMarkerColor(color);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.9);
    histogram->GetXaxis()->SetTitleOffset(1.0);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetLabelSize(0.045);
    histogram->GetYaxis()->SetLabelSize(0.045);

    histogram->SetLineColor(color);
}
void design_markers(TEfficiency *histogram, string xaxis_name, string yaxis_name, Color_t color){ //TODO
    gStyle->SetOptStat(0);
    histogram->SetMarkerStyle(8);
    histogram->SetMarkerColor(color);
    histogram->SetLineColor(color);
}

void design_lines(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    histogram->SetLineWidth(3);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.3);
    histogram->GetXaxis()->SetTitleOffset(1.1);

    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetXaxis()->SetLabelSize(0.04);

    histogram->SetLineColor(color);
}
void design_lines(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    histogram->SetLineWidth(3);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.3);
    histogram->GetXaxis()->SetTitleOffset(1.05);

    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetXaxis()->SetLabelSize(0.04);

    histogram->SetLineColor(color);
}
void design_lines(TH1 *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    histogram->SetLineWidth(3);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());

    histogram->GetYaxis()->SetTitleOffset(1.3);
    histogram->GetXaxis()->SetTitleOffset(1.05);

    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetXaxis()->SetLabelSize(0.04);

    histogram->SetLineColor(color);
}

void design_filled_plot(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t fill_color, int fill_style){
    gStyle->SetOptStat(0);

    histogram->SetLineWidth(2);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.0);
    histogram->GetXaxis()->SetTitleOffset(1.0);

    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.05);
    histogram->GetXaxis()->SetLabelSize(0.05);

    histogram->SetFillColor(fill_color);
    histogram->SetFillStyle(fill_style);
}
void design_filled_plot(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t fill_color, int fill_style){
    gStyle->SetOptStat(0);

    histogram->SetLineWidth(2);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetYaxis()->SetTitleOffset(1.0);
    histogram->GetXaxis()->SetTitleOffset(1.0);

    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.05);
    histogram->GetXaxis()->SetLabelSize(0.05);

    histogram->SetFillColor(fill_color);
    histogram->SetFillStyle(fill_style);
}
void design_filled_plot(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t fill_color, int fill_style, Color_t line_color){
    design_filled_plot(histogram,xaxis_name,yaxis_name,fill_color,fill_style);
    histogram->SetLineColor(line_color);
}
void design_filled_plot(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t fill_color, int fill_style , Color_t line_color){
    design_filled_plot(histogram,xaxis_name,yaxis_name,fill_color,fill_style);
    histogram->SetLineColor(line_color);
}

void design_canvas(TCanvas *canvas){    
    canvas->SetRightMargin(0.05);
    canvas->SetTopMargin(0.1);
    canvas->SetLeftMargin(0.13);
    canvas->SetBottomMargin(0.12);   
}
void design_canvas(TCanvas *canvas, string title){
    design_canvas(canvas);
    canvas->SetTitle(title.c_str());
}
void design_canvas_Bmass(TCanvas *canvas){
    canvas->SetRightMargin(0.03);
    canvas->SetTopMargin(0.1);
    canvas->SetLeftMargin(0.09);
    canvas->SetBottomMargin(0.12);
}
void design_canvas_Bmass(TCanvas *canvas, string title){
    design_canvas_Bmass(canvas);
    canvas->SetTitle(title.c_str());
}
TCanvas *c_TH2F(string c_name){
    TCanvas *c_eff = new TCanvas(c_name.c_str(),c_name.c_str(),750, 700);
    c_eff->SetRightMargin(0.14);
    gStyle->SetOptStat(0);
    c_eff->SetLeftMargin(0.14);
    c_eff->SetTopMargin(0.075);
    c_eff->SetBottomMargin(0.12);
    return c_eff;

}

void design_canvas_TH2F(TCanvas *canvas){
    canvas->SetRightMargin(0.15);
    canvas->SetTopMargin(0.04);
    canvas->SetBottomMargin(0.25);
    canvas->SetLeftMargin(0.25);
}
void design_canvas_fancy(TCanvas *canvas){
    canvas->SetRightMargin(0.515);
    canvas->SetTopMargin(0.05);
    canvas->SetBottomMargin(0.12);
    canvas->SetLeftMargin(0.3);
}

void design_TH2F(TH2F *histogram, string xaxis_name, string yaxis_name, string zaxis_name){
    gStyle->SetOptStat(0);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetZaxis()->SetTitle(zaxis_name.c_str());

    histogram->GetXaxis()->SetTitleOffset(0.95);
    histogram->GetYaxis()->SetTitleOffset(1.5);
    histogram->GetZaxis()->SetTitleOffset(0.8);

    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetZaxis()->SetTitleSize(0.04);

    histogram->GetXaxis()->SetLabelSize(0.04);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetZaxis()->SetLabelSize(0.04);
    //histogram->GetZaxis()->CenterTitle(kTRUE);
    histogram->SetTitle("");
}

void design_TH2F(TH2D *histogram, string xaxis_name, string yaxis_name, string zaxis_name){
    gStyle->SetOptStat(0);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    histogram->GetZaxis()->SetTitle(zaxis_name.c_str());

    histogram->GetXaxis()->SetTitleOffset(0.95);
    histogram->GetYaxis()->SetTitleOffset(1.5);
    histogram->GetZaxis()->SetTitleOffset(0.8);

    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
    histogram->GetZaxis()->SetTitleSize(0.04);

    histogram->GetXaxis()->SetLabelSize(0.04);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetZaxis()->SetLabelSize(0.04);
    //histogram->GetZaxis()->CenterTitle(kTRUE);
    histogram->SetTitle("");
}

void design_TH2F(TH2F *histogram, string xaxis_name, string yaxis_name, string zaxis_name, int palette){
    design_TH2F(histogram,xaxis_name,yaxis_name,zaxis_name);
    gStyle->SetPalette(palette);
}

void design_TH2F(TH2D *histogram, string xaxis_name, string yaxis_name, string zaxis_name, int palette){
    design_TH2F(histogram,xaxis_name,yaxis_name,zaxis_name);
    gStyle->SetPalette(palette);
}

void design_TH2F_ratio(TH2F *histogram, string xaxis_name, string yaxis_name, string zaxis_name, int palette){
    gStyle->SetOptStat(0);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    string zaxis_name_offset = "        " + zaxis_name;
    histogram->GetZaxis()->SetTitle(zaxis_name_offset.c_str());

    histogram->GetXaxis()->SetTitleOffset(1.2);
    histogram->GetYaxis()->SetTitleOffset(1.2);
    histogram->GetZaxis()->SetTitleOffset(1.2);
    histogram->GetZaxis()->SetLabelSize(0.03);
    histogram->GetZaxis()->CenterTitle(kTRUE);
    histogram->SetTitle("");
    gStyle->SetPalette(palette);
}
void design_TH2F_ratio(TH2D *histogram, string xaxis_name, string yaxis_name, string zaxis_name, int palette){
    gStyle->SetOptStat(0);
    histogram->GetXaxis()->SetTitle(xaxis_name.c_str());
    histogram->GetYaxis()->SetTitle(yaxis_name.c_str());
    string zaxis_name_offset = "        " + zaxis_name;
    histogram->GetZaxis()->SetTitle(zaxis_name_offset.c_str());

    histogram->GetXaxis()->SetTitleOffset(1.2);
    histogram->GetYaxis()->SetTitleOffset(1.2);
    histogram->GetZaxis()->SetTitleOffset(1.2);
    histogram->GetZaxis()->SetLabelSize(0.03);
    histogram->GetZaxis()->CenterTitle(kTRUE);
    histogram->SetTitle("");
    gStyle->SetPalette(palette);
}

void design_function(TF1 *graph, Color_t color){
    gStyle->SetOptStat(0);
    graph->SetLineWidth(3);
    graph->SetLineColor(color);

}
void design_function(TF1 *graph, Color_t color, int style){
    design_function(graph,color);
    graph->SetLineStyle(style);
}

void design_TH1F(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    design_markers(histogram, xaxis_name, yaxis_name, color);
    histogram->SetMarkerSize(3);
    histogram->GetXaxis()->SetTitleOffset(1.0);
    histogram->GetYaxis()->SetTitleOffset(1.6);
    histogram->SetTitle("");
    histogram->GetXaxis()->SetLabelSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
}
void design_TH1D(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t color){
    gStyle->SetOptStat(0);
    design_markers(histogram, xaxis_name, yaxis_name, color);
    histogram->SetMarkerSize(3);
    histogram->GetXaxis()->SetTitleOffset(1.0);
    histogram->GetYaxis()->SetTitleOffset(1.6);
    histogram->SetTitle("");
    histogram->GetXaxis()->SetLabelSize(0.05);
    histogram->GetYaxis()->SetLabelSize(0.04);
    histogram->GetXaxis()->SetTitleSize(0.05);
    histogram->GetYaxis()->SetTitleSize(0.05);
}
void design_TH1F(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t color, int marker_style){
    design_TH1F(histogram,xaxis_name,yaxis_name,color);
    histogram->SetMarkerStyle(marker_style);
}
void design_TH1D(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t color, int marker_style){
    design_TH1D(histogram,xaxis_name,yaxis_name,color);
    histogram->SetMarkerStyle(marker_style);
}
void design_TH1F(TH1F *histogram, string xaxis_name, string yaxis_name, Color_t color, int marker_style, double x_low, double x_up, double y_low, double y_up){
    design_TH1F(histogram,xaxis_name,yaxis_name,color,marker_style);

    histogram->GetXaxis()->SetRangeUser(x_low, x_up);
    histogram->GetYaxis()->SetRangeUser(y_low, y_up);
}
void design_TH1D(TH1D *histogram, string xaxis_name, string yaxis_name, Color_t color, int marker_style, double x_low, double x_up, double y_low, double y_up){
    design_TH1D(histogram,xaxis_name,yaxis_name,color,marker_style);
    histogram->GetXaxis()->SetRangeUser(x_low, x_up);
    histogram->GetYaxis()->SetRangeUser(y_low, y_up);
}

TLine *design_cut_line(float x1, float y1, float x2, float y2, Color_t color){
    TLine *line = new TLine(x1,y1,x2,y2);
    line->SetLineColor(color);
    line->SetLineWidth(5);
    return line;
}
TLine *design_ratio_line(float x1, float x2, Color_t color){
    TLine *line = new TLine(x1,1,x2,1);
    line->SetLineColor(color);
    line->SetLineWidth(3);
    line->SetLineStyle(2);
    return line;
}

TLine *design_veto_line(float x1, float y1, float x2, float y2, Color_t color){
    TLine *line = new TLine(x1,y1,x2,y2);
    line->SetLineColor(color);
    line->SetLineWidth(1);
    return line;
}
////////////////////////////////////
///
///
///         TMVA plots
///
///
////////////////////////////////////

bool DesignCorrelationPlots(TH2F *h_corr){
    h_corr->SetMarkerColor(kBlack); //in order for the text Draw option to be black
    h_corr->GetXaxis()->SetLabelSize(0.04);
    h_corr->GetYaxis()->SetLabelSize(0.04);
    gStyle->SetPalette(kRainBow);
    return true;
}

TCanvas *c_Correlation(string type){
    string c_name = "c_Correlation" + type;
    TCanvas *c_corr = new TCanvas(c_name.c_str(),c_name.c_str(),800, 750);
    c_corr->SetRightMargin(0.13);
    c_corr->SetLeftMargin(0.23);
    c_corr->SetTopMargin(0.075);
    c_corr->SetBottomMargin(0.12);
    return c_corr;
}

bool designVariablesSignalVsBackground(TH1F *h_variableS, TH1F *h_variableB){

    //Set plot height (find max(S,B) and add 10%
    double height = max(h_variableS->GetMaximum(),h_variableB->GetMaximum());
    height*=1.1;
    h_variableS->GetYaxis()->SetRangeUser(0,height);
    h_variableS->GetYaxis()->SetTitleOffset(1.23);

    //Add dN/N
    string YaxisTitle = h_variableS->GetYaxis()->GetTitle();
    replace(YaxisTitle,"dN","dN/N");
    h_variableS->GetYaxis()->SetTitle(YaxisTitle.c_str());

    //Add color
    h_variableS->SetLineColor(kBlue+2);
    h_variableS->SetFillStyle(1001);
    h_variableS->SetFillColor(38);
    h_variableS->SetLineWidth(2);


    h_variableB->SetLineColor(kRed+2);
    h_variableB->SetFillStyle(3354);
    h_variableB->SetFillColor(kRed);
    h_variableB->SetLineWidth(2);

    return 1;
}

bool designResponseSignalVsBackground(TH1F *h_responseS, TH1F *h_responseB, string method){


    //Set plot height (find max(S,B) and add 20%
    double height = max(h_responseS->GetMaximum(),h_responseB->GetMaximum());
    height*= (method != "BDT" ? 5.0 : 1.4);
    h_responseS->GetYaxis()->SetRangeUser( (method == "BDT" ? 0.00 : 0.01) ,height); //prevent log from crashing
    h_responseS->GetYaxis()->SetTitleOffset(1.25);

    //Set axis titles
    string XaxisTitle = method + " response";
    string YaxisTitle = "(1/N) dN/dx";

    h_responseS->GetXaxis()->SetTitle(XaxisTitle.c_str());
    h_responseS->GetYaxis()->SetTitle(YaxisTitle.c_str());
    h_responseS->SetTitle(XaxisTitle.c_str());

    //Set Xaxis range
    //if (method != "MLP") h_responseS->GetXaxis()->SetRangeUser(-1,1);
    //else h_responseS->GetXaxis()->SetRangeUser(0,1);

    //Add color
    h_responseS->SetFillStyle(1001);
    h_responseS->SetFillColor(38);
    h_responseS->SetLineWidth(0);
    h_responseS->SetLineColor(kBlue+2);
    h_responseS->SetLineWidth(1);
    h_responseS->SetMarkerSize(0);

    h_responseB->SetFillStyle(3354);
    h_responseB->SetFillColor(kRed);
    h_responseB->SetLineColor(kRed+2);
    h_responseB->SetLineWidth(1);
    h_responseB->SetMarkerSize(0);

    return 1;
}

bool designOvertraining(TH1F *h_responseS, TH1F *h_responseB){

    //Add color
    h_responseS->SetLineColor(kBlue+2);
    h_responseS->SetLineWidth(3);
    h_responseS->SetMarkerColor(kBlue+2);
    h_responseS->SetMarkerStyle(20);


    h_responseB->SetLineColor(kRed+2);
    h_responseB->SetLineWidth(3);
    h_responseB->SetMarkerColor(kRed+2);
    h_responseB->SetMarkerStyle(20);

    return 1;
}

TCanvas *c_VariablesSignalVsBackground(string variable){
    string c_name = "c_" + variable;
    TCanvas *c_Var = new TCanvas(c_name.c_str(),c_name.c_str(),750, 600);
    gStyle->SetOptStat(0);
    c_Var->SetRightMargin(0.05);
    c_Var->SetLeftMargin(0.18);
    c_Var->SetTopMargin(0.05);
    c_Var->SetBottomMargin(0.17);
    return c_Var;
}

TCanvas *c_Efficiency(string c_name){
    TCanvas *c_eff = new TCanvas(c_name.c_str(),c_name.c_str(),750, 525);
    c_eff->SetRightMargin(0.13);
    gStyle->SetOptStat(0);
    c_eff->SetLeftMargin(0.23);
    c_eff->SetTopMargin(0.075);
    c_eff->SetBottomMargin(0.12);
    return c_eff;
}

bool designEfficiency(TH1F *h_efficiencySignal,TH1F *h_efficiencyBackground,TH1F *h_purity,TH1F *h_significance, string method){

    //Set axis titles
    string XaxisTitle = "Cut value applied on " + method + " output";
    h_efficiencySignal->GetXaxis()->SetTitle(XaxisTitle.c_str());
    h_efficiencySignal->GetYaxis()->SetTitle("Efficiency (purity)");
    h_significance->GetYaxis()->SetTitle("Significance");

    //Set titles
    h_efficiencySignal->SetTitle("Cut efficiencies and optimal cut value");
    h_significance->SetTitle("");

    //Add top margin for legend boxes
    h_efficiencySignal->GetYaxis()->SetRangeUser(0,1.24);
    h_significance->GetYaxis()->SetRangeUser(0,h_significance->GetMaximum()*1.24);


    //Add color
    h_efficiencySignal->SetLineColor(kBlue+2);
    h_efficiencySignal->SetLineWidth(3);

    h_efficiencyBackground->SetLineColor(kRed);
    h_efficiencyBackground->SetLineWidth(3);

    h_purity->SetLineColor(kMagenta);
    h_purity->SetLineWidth(3);

    h_significance->SetLineColor(kGreen+2);
    h_significance->SetLineWidth(3);
    h_significance->GetYaxis()->SetLabelColor(kGreen+2);
    h_significance->GetYaxis()->SetTitleColor(kGreen+2);

    return 1;

}

TPaveText *significanceText(double xmin, int nSig, int nBkg, double maxSignificance, double maxSignificanceMLPcut){
    TPaveText *text = new TPaveText(xmin+0.01,0.05,(1-xmin)*0.8,0.3);
    text->SetFillColor(0);
    text->SetFillStyle(0);
    text->SetTextAlign(12);
    text->SetTextSize(0.04);
    text->SetBorderSize(0);

    string s_text = "For " + to_string(nSig) + " sigEvts and " + to_string(nBkg) + " bkgEvts";
    text->AddText(s_text.c_str());

    stringstream stream;    //workaround for to_string() precision
    stream << fixed << setprecision(2) << maxSignificance;
    s_text = "the maximum significance is " + stream.str();
    text->AddText(s_text.c_str());
    stream.str(""); //clear stream
    stream << fixed << setprecision(2) << maxSignificanceMLPcut;
    s_text = "when cutting at " + stream.str() + ".";
    text->AddText(s_text.c_str());

    return text;
}
TPaveText *text_effFromPi0(double yieldSig, double yieldBkg, double ymax){
    TPaveText *text = new TPaveText(145,ymax*0.9,160,ymax*1.15);
    text->SetFillColor(0);
    text->SetFillStyle(0);
    text->SetTextAlign(12);
    text->SetTextSize(0.04);
    text->SetBorderSize(0);
    text->AddText(Form("Sig yield: %i",int(yieldSig)));
    text->AddText(Form("Bkg yield: %i",int(yieldBkg)));
    return text;
}

TCanvas *c_ROCplot(string c_name){
    TCanvas *c_ROCs = new TCanvas(c_name.c_str(),c_name.c_str(),650, 600);
    gStyle->SetOptStat(0);
    c_ROCs->SetRightMargin(0.055);
    c_ROCs->SetLeftMargin(0.175);
    c_ROCs->SetTopMargin(0.055);
    c_ROCs->SetBottomMargin(0.175);
    c_ROCs->SetGridx();
    c_ROCs->SetGridy();
    return c_ROCs;
}

void designROC(TH1F* h_ROC, int index){
    if ( 9 <= index ) index += 20; //remove ugly white-ish colors
    h_ROC->SetLineColor(index+1);

    h_ROC->SetLineWidth(2);
    h_ROC->GetXaxis()->SetTitleOffset(0.95);
    h_ROC->GetYaxis()->SetTitleOffset(1.1);
    h_ROC->GetXaxis()->SetLabelSize(0.05);
    h_ROC->GetYaxis()->SetLabelSize(0.05);
    h_ROC->GetYaxis()->SetNdivisions(505);

    h_ROC->GetXaxis()->SetRangeUser(0.5,1.0);
    h_ROC->GetYaxis()->SetRangeUser(0.5,1.0);
    h_ROC->SetTitle("ROCs");

}

TCanvas *c_L0MuonEff(string c_name){
    TCanvas *c_L0Muon = new TCanvas(c_name.c_str(),c_name.c_str(),650, 600);
    gStyle->SetOptStat(0);
    c_L0Muon->SetRightMargin(0.075);
    c_L0Muon->SetLeftMargin(0.18);
    c_L0Muon->SetTopMargin(0.05);
    c_L0Muon->SetBottomMargin(0.19);
    return c_L0Muon;
}

void designL0MuonEffHistos(TH1D *L0MuonEffData,TH1D *L0MuonEffMC, TH1D *L0MuonEffRatio){
    L0MuonEffData->GetYaxis()->SetRangeUser(0.5,1.3);
    L0MuonEffData->SetTitle("L0Muon efficiencies");
    L0MuonEffData->GetYaxis()->SetTitle("Efficiency");
    L0MuonEffData->GetXaxis()->SetTitle("max(#mu^{-}_{p_{T}},#mu^{+}_{p_{T}})");
    L0MuonEffData->GetXaxis()->SetTitleOffset(1.2);
    L0MuonEffData->GetYaxis()->SetTitleOffset(1.2);

    L0MuonEffMC->GetYaxis()->SetTitle("Efficiency");
    L0MuonEffMC->GetXaxis()->SetTitle("max(#mu^{-}_{p_{T}},#mu^{+}_{p_{T}})");
    L0MuonEffMC->GetXaxis()->SetTitleOffset(1.2);
    L0MuonEffMC->GetYaxis()->SetTitleOffset(1.2);

    L0MuonEffData->GetYaxis()->SetTitle("Efficiency");
    L0MuonEffRatio->GetXaxis()->SetTitle("max(#mu^{-}_{p_{T}},#mu^{+}_{p_{T}})");
    L0MuonEffData->GetXaxis()->SetTitleOffset(1.2);
    L0MuonEffRatio->GetYaxis()->SetTitleOffset(1.2);

    L0MuonEffData->SetLineColor(kBlack);
    L0MuonEffMC->SetLineColor(kRed);
    L0MuonEffRatio->SetLineColor(kBlue);

    L0MuonEffData->SetLineWidth(2);
    L0MuonEffMC->SetLineWidth(2);
    L0MuonEffRatio->SetLineWidth(2);

    L0MuonEffData->SetMarkerColor(kBlack);
    L0MuonEffMC->SetMarkerColor(kRed);
    L0MuonEffRatio->SetMarkerColor(kBlue);

    L0MuonEffData->SetMarkerStyle(0);
    L0MuonEffMC->SetMarkerStyle(0);
    L0MuonEffRatio->SetMarkerStyle(0);

}

//TODO: make it compile on its own possibly
TCanvas * designL0MuonEfficiencies(string year, string magnet,TH1D *L0MuonEffData,TH1D *L0MuonEffMC, TH1D *L0MuonEffRatio, string extraString){
    string name = "eff_" + extraString;
    TCanvas *c_L0Eff = c_L0MuonEff(name.c_str());
    c_L0Eff->cd();

    designL0MuonEffHistos(L0MuonEffData, L0MuonEffMC, L0MuonEffRatio);
    //line at 1
    TLine *line = design_ratio_line(L0MuonEffData->GetXaxis()->GetXmin(),L0MuonEffData->GetXaxis()->GetXmax(),kBlack);
    line->SetLineWidth(1);
    line->SetLineStyle(1);

    //Add legend

    TLegend *leg = new TLegend(0.45,0.25,0.9,0.45);
    leg->AddEntry(L0MuonEffData, "Data eff","l");
    string s_leg = extraString + " eff";
    leg->AddEntry(L0MuonEffMC, s_leg.c_str(),"l");
    leg->AddEntry(L0MuonEffRatio, "Data eff/MC eff","l");
    leg->SetTextSize(0.05);

    L0MuonEffData->Draw();
    line->Draw("SAME");
    L0MuonEffMC->Draw("SAME");
    L0MuonEffRatio->Draw("SAME");

    addLHCbtag(0.56, 0.87, year , 1.0);

    leg->Draw("SAME");

    string output_name = GetEfficiencyFile("L0Muon",year,magnet,false,false,false,false,false,false,false,false,extraString);
    replace (output_name,".root",".eps");
    c_L0Eff->SaveAs(output_name.c_str());

    return c_L0Eff;

}

void designBasicEfficiency(TGraphErrors *graph, string title, string xAxisName, string effName, double lowRange, double highRange){

    graph->SetName(title.c_str());
    graph->SetLineColor(kGreen+3);
    graph->SetMarkerStyle(20);
    graph->SetMarkerColor(kGreen+3);
    graph->GetXaxis()->SetTitle(xAxisName.c_str());
    graph->GetYaxis()->SetTitle(Form("#varepsilon_%s",effName.c_str()));
    graph->GetYaxis()->SetTitleSize(0.07);
    graph->GetYaxis()->SetRangeUser(lowRange,highRange);
}

void designBasicEfficiency(TGraphErrors *graph, string title, string xAxisName, string effName, double lowRange, double highRange, Color_t color){
    graph->SetName(title.c_str());
    graph->SetLineColor(color);
    graph->SetMarkerStyle(20);
    graph->SetMarkerColor(color);
    graph->GetXaxis()->SetTitle(xAxisName.c_str());
    graph->GetYaxis()->SetTitle(Form("#varepsilon_%s",effName.c_str()));
    graph->GetYaxis()->SetTitleSize(0.05);
    graph->GetYaxis()->SetRangeUser(lowRange,highRange);
}

void designMultiGraphEfficiency(TMultiGraph *graph, string title, string xAxisName, string effName, double lowRange, double highRange){
    graph->SetName(title.c_str());
    graph->GetXaxis()->SetTitle(xAxisName.c_str());
    graph->GetYaxis()->SetTitle(Form("#varepsilon_%s",effName.c_str()));
    graph->GetYaxis()->SetTitleSize(0.05);
    graph->GetYaxis()->SetRangeUser(lowRange,highRange);
}


////////////////////////////////////
///
///
///         generic plots
///
///
////////////////////////////////////
TCanvas *c_canvas(string name){
    TCanvas *canvas = new TCanvas(name.c_str(),name.c_str(),750, 600);
    gStyle->SetOptStat(0);
    canvas->SetRightMargin(0.08);
    canvas->SetTopMargin(0.05);
    canvas->SetBottomMargin(0.12);
    canvas->SetLeftMargin(0.15);
    return canvas;
}

void designTGraph(TGraphErrors *graph, string title, string xAxisName, string yAxisName, Color_t color, Int_t markerStyle){

    graph->SetName(title.c_str());
    graph->SetLineColor(color);
    //graph->SetLineWidth(0);
    graph->SetMarkerStyle(markerStyle);
    graph->SetMarkerColor(color);
    graph->GetXaxis()->SetTitle(xAxisName.c_str());
    graph->GetYaxis()->SetTitle(yAxisName.c_str());
    gStyle->SetOptStat(0);

    graph->GetXaxis()->SetTitleOffset(1.0);
    graph->GetYaxis()->SetTitleOffset(1.7);
    graph->SetTitle("");
    graph->GetXaxis()->SetLabelSize(0.07);
    graph->GetYaxis()->SetLabelSize(0.05);
    graph->GetXaxis()->SetTitleSize(0.05);
    graph->GetYaxis()->SetTitleSize(0.04);

    return;

}

TH1D *generalHistogram(string hist_name, string title, int nBins, double low_edge, double high_edge, string xaxis_name, string yaxis_name, Color_t color){
    TH1D *histogram = new TH1D(hist_name.c_str(), title.c_str(),nBins,low_edge,high_edge);
    design_TH1D(histogram, xaxis_name, yaxis_name, color);
    return histogram;
}

TH1D *BmassHistogram(string hist_name, string title, string xaxis_name,Color_t color){
    TH1D *histogram = generalHistogram(hist_name, title, 100, cut_B_plus_M_low, cut_B_plus_M_high, xaxis_name, "Counts a.u.", color);
    histogram->SetLineWidth(2);
    return histogram;
}

void TM_canvas(string name, TH1D *all, TH1D *TMed, TH1D *notTMed, string year, bool ReferenceChannel, bool PHSP, bool B0, bool K1, bool Inc){

    string path = getTMplotsPath(name, year, ReferenceChannel, PHSP, B0, K1, Inc);

    TCanvas *canvas = c_canvas(name);
    canvas->cd();
    all->SetLineWidth(2);
    TMed->SetLineWidth(2);
    notTMed->SetLineWidth(2);

    all->Draw();
    TMed->Draw("SAME");
    notTMed->Draw("SAME");

    canvas->Print(path.c_str(),"eps");
    replace(path,".eps",".root");
    canvas->Print(path.c_str(),"root");
    delete canvas;
    return;

}

void designMultiGraph(TMultiGraph *multGraph, string name, string xaxis_name, string yaxis_name, double lowx, double highx, double lowy, double highy){
    designMultiGraph(multGraph, name, xaxis_name, yaxis_name);
    multGraph->GetXaxis()->SetRangeUser(lowx, highx);
    multGraph->GetYaxis()->SetRangeUser(lowy, highy);
    return;
}

void designMultiGraph(TMultiGraph *multGraph, string name, string xaxis_name, string yaxis_name){

    //One needs to draw the multGraph first!
    multGraph->SetName(name.c_str());
    multGraph->GetXaxis()->SetTitle(xaxis_name.c_str());
    multGraph->GetYaxis()->SetTitle(yaxis_name.c_str());
    gStyle->SetOptStat(0);

    multGraph->GetXaxis()->SetTitleOffset(0.9);
    multGraph->GetYaxis()->SetTitleOffset(1.1);
    multGraph->SetTitle("");
    multGraph->GetXaxis()->SetLabelSize(0.05);
    multGraph->GetYaxis()->SetLabelSize(0.05);
    multGraph->GetXaxis()->SetTitleSize(0.05);
    multGraph->GetYaxis()->SetTitleSize(0.05);
    return;
}

void addYearTag(Float_t x = 0.6, Float_t y = 0.85, string year = "", int Run = 0, Int_t color = 1, Float_t Scaling = 1.0){
    TLatex* lhcbtext = new TLatex();
    lhcbtext->SetTextFont(132);
    lhcbtext->SetTextColor(color);
    lhcbtext->SetTextSize(0.07*Scaling);
    lhcbtext->SetTextAlign(13);
    lhcbtext->SetNDC(1);
    lhcbtext->DrawLatex(x,y,getYearRunTag(Run,year).c_str());
    return;
}

void addAnyTag(Float_t x = 0.6, Float_t y = 0.85, string text = "", Int_t color = 1, Float_t Scaling = 1.0){
    TLatex* lhcbtext = new TLatex();
    lhcbtext->SetTextFont(132);
    lhcbtext->SetTextColor(color);
    lhcbtext->SetTextSize(0.07*Scaling);
    lhcbtext->SetTextAlign(13);
    lhcbtext->SetNDC(1);
    lhcbtext->DrawLatex(x,y,text.c_str());
    return;
}




////////////////////////////////////
///
///
///     Truth-matching stuff
///
///
////////////////////////////////////

TCanvas *c_for_TH2D(string name){
    TCanvas *c_TH2D = new TCanvas(name.c_str(),name.c_str(),800, 750);
    c_TH2D->SetRightMargin(0.13);
    c_TH2D->SetLeftMargin(0.13);
    c_TH2D->SetTopMargin(0.075);
    c_TH2D->SetBottomMargin(0.13);
    return c_TH2D;
}

bool designTH2D(TH2F *hist){
    hist->SetMarkerColor(kBlack); //in order for the text Draw option to be black
    hist->GetXaxis()->SetLabelSize(0.04);
    hist->GetYaxis()->SetLabelSize(0.04);
    gStyle->SetPalette(kRainBow);
    return true;
}

TH1D *hist_cosThetaK(string hist_name, string title, Color_t color, bool cos){
    TH1D *histogram = generalHistogram(hist_name, title, 75, cos ? -1.0 : 0.0, cos ? 1.0 : 3.15, cos ? "cos(#theta_{K})" : "#theta_{K}", "Counts a.u.", color);
    histogram->SetLineWidth(2);
    return histogram;
}

TH1D *hist_cosAngle(string angle, string hist_name, string title, Color_t color, bool cos, bool norm){

    if      (angle == "thetak") angle = "#theta_{K}";
    else if (angle == "thetal") angle = "#theta_{L}";
    else if (angle == "phi")    angle = "#phi";
    else{
        coutERROR("Wrong angle selected! Select from thetak, thetal and phi.");
        return 0;
    }
    if (cos) angle = "cos(" + angle + ")";

    TH1D *histogram = generalHistogram(hist_name, title, norm ? 20 : 50, cos ? -1.0 : 0.0, cos ? 1.0 : 3.15, angle.c_str(), "Counts a.u.", color);
    histogram->SetLineWidth(2);
    histogram->SetMarkerSize(1.0);
    return histogram;
}

////////////////////////////////////
///
///
///     Mass Fit stuff
///
///
////////////////////////////////////

void designMassFitFrame(RooPlot* frame, float pullHeight){

    float textsize = gStyle->GetTextSize();
    if (Kst2Kpluspi0Resolved && UseDTF)frame->SetTitle("event distribution #B^{+} with fixed M_{#pi^{0}}");
    else frame->SetTitle("event distribution #B^{+}");

    frame->GetXaxis()->SetNoExponent(); //<-- spoils MaxDigits settings, so don't use it on other axis
    frame->GetXaxis()->SetLabelSize(0.0);//don't print labels
    frame->GetYaxis()->SetRangeUser(0.001, frame->GetMaximum()*1.2);//don't print 0-label
    frame->GetXaxis()->SetTickLength(frame->GetXaxis()->GetTickLength()/(1-pullHeight));
    frame->GetXaxis()->SetNdivisions(gStyle->GetNdivisions("X"));
    frame->GetYaxis()->SetTitleSize(textsize/(1-pullHeight));
    frame->GetYaxis()->SetLabelSize(textsize/(1-pullHeight));
    frame->GetYaxis()->SetNdivisions(gStyle->GetNdivisions("Y"));
    frame->GetYaxis()->SetTitleOffset(1.06*(1-pullHeight));
    return;
}

void designPullFrame(RooPlot *pullFrame, RooPlot *frame_m, float pullHeight){

    RooHist* frame2 = frame_m->pullHist("data", "pdf") ;
    float textsize = gStyle->GetTextSize();
    float pullFrameRange = 6.5; //This is +/- boundaries of the pull

    //make red boxes
    pullFrame->addPlotable(frame2,"X0 B");
    pullFrame->getAttFill()->SetFillColor(kRed+1);

    //format the x- and y-axis of the pull plot
    TGaxis::SetExponentOffset(1e+9,1e+9,"x");//offset = pad size * 1e+7
    pullFrame->GetXaxis()->SetRangeUser(get_cut_B_plus_M_low(2), cut_B_plus_M_high); //TODO: sort low B mass cut

    pullFrame->GetXaxis()->SetTitleOffset(1.05);
    pullFrame->GetXaxis()->SetTitleSize  (textsize/pullHeight);
    pullFrame->GetXaxis()->SetLabelSize  (textsize/pullHeight);
    pullFrame->GetXaxis()->SetTickLength (frame_m->GetXaxis()->GetTickLength()/(pullHeight/(1-pullHeight)));
    pullFrame->GetXaxis()->SetNdivisions (gStyle->GetNdivisions("X"));

    pullFrame->GetYaxis()->SetLabelSize  (textsize/pullHeight);
    pullFrame->GetYaxis()->SetTitleSize  (textsize/pullHeight);
    pullFrame->GetYaxis()->SetNdivisions (3, 9, 0, kTRUE);//gStyle->GetNdivisions("Y"));
    pullFrame->GetYaxis()->SetTitleOffset(1.06*pullHeight);
    pullFrame->GetYaxis()->SetRangeUser  (-pullFrameRange, pullFrameRange);
    pullFrame->GetYaxis()->SetTitle("Pull");

    return ;

}

TLine *threeSigmaLine (bool plus){
    TLine * line  = new TLine(get_cut_B_plus_M_low("2016"), plus ? +3. : -3, cut_B_plus_M_high, plus ? +3. : -3); //TODO: sort low B mass cut
    line->SetLineStyle(5);
    line->SetLineWidth(gStyle->GetLineWidth());
    return line;
}



////////////////////////////////////
///
///
///     reweighting control plots
///
///
////////////////////////////////////

void drawKolmogorovTest(TH1D *hist_nTracks, TH1D * hist_nTracks_MC, string canvasPath, string TMtype){
    TCanvas* c1 = new TCanvas("c1", "c1");

    //get yRange
    double maxY= hist_nTracks->GetMaximum();
    if(hist_nTracks_MC->GetMaximum()>maxY)maxY=hist_nTracks_MC->GetMaximum();
    hist_nTracks->SetMinimum(0.);
    hist_nTracks->SetMaximum(maxY*1.2);
    design_TH1D(hist_nTracks, firstMClatex.c_str(),"Events ( norm. )", kBlack);
    hist_nTracks->SetMarkerSize(1);

    //format colors and draw
    hist_nTracks_MC->SetLineStyle(0);

    hist_nTracks_MC->GetXaxis()->SetTitle(firstMClatex.c_str());
    hist_nTracks_MC->GetYaxis()->SetTitle("Events ( norm. )");

    hist_nTracks_MC->SetLineColor(kRed);
    hist_nTracks_MC->SetFillColor(kRed);
    hist_nTracks_MC->SetMarkerColor(kRed);
    hist_nTracks_MC->SetMarkerSize(0);
    hist_nTracks_MC->SetMinimum(0.);
    hist_nTracks_MC->SetMaximum(maxY*1.2);
    hist_nTracks_MC->Draw("E2");
    hist_nTracks->Draw("SAME");

    //define plot for Kolmogorov test
    //Kolmogorov test shouldn't be used at binned data (one should use TMath::Kolmogorov)
    //<<1 means NOT compatible
    double KolmoTest = hist_nTracks->KolmogorovTest(hist_nTracks_MC);
    TPaveText *KolmOut= new TPaveText(0.55,0.6,0.85,0.7,"NDC");
    KolmOut->AddText(Form("Kolmogorov Test : %.3f ", KolmoTest));
    KolmOut->SetLineColor(kWhite);
    KolmOut->SetFillColor(kWhite);
    KolmOut->SetShadowColor(0);
    KolmOut->SetTextSize(0.04);
    KolmOut->SetTextColor(kRed);

    TLegend *leg = new TLegend(0.55,0.7,0.85,0.85);
    leg->SetHeader(" ");
    leg->AddEntry(hist_nTracks,"Data","LEP");
    leg->AddEntry(hist_nTracks_MC,"MC","F");
    leg->SetLineColor(kWhite);
    leg->SetFillColor(kWhite);
    leg->SetTextSize(0.05);
    KolmOut->Draw();
    leg->Draw();

    addLHCbtag(0.65, 0.88, "");
    replace(canvasPath,".root",TMtype+".root");
    c1->SaveAs(canvasPath.c_str());
    replace(canvasPath,".root",".eps");
    c1->Print(canvasPath.c_str());

    delete leg;
    c1->Clear();
    delete c1;

    return;
}

void drawWeightRatio(TH1D *hist_w, string canvasPath, bool is2D, string TMtype){
    TCanvas* c2 = new TCanvas("c2", "c2");
    c2->SetGridy();
    c2->cd();

    //format colors and draw
    hist_w->GetYaxis()->SetRangeUser(-2., 6.);
    if (is2D) design_TH1D(hist_w,seconMClatex,"2 x 1D Weights of sim. Events",kBlue);
    else design_TH1D(hist_w,firstMClatex,"1D Weights of sim. Events",kBlue);
    hist_w->SetMarkerSize(1);
    hist_w->Draw("E1");
    addLHCbtag(0.25, 0.9, "");

    replace(canvasPath,".root",TMtype+".root");
    c2->SaveAs(canvasPath.c_str());
    replace(canvasPath,".root",".eps");
    c2->Print(canvasPath.c_str());

    c2->Clear();
    delete c2;
    return;
}

void drawWeightCorrelation(TH2D *hCorrelationCheck, double CorrelationCoefficent, string canvasPath){
    TCanvas * c4 = new TCanvas("c4", "c4");
    c4->SetRightMargin(0.19);
    c4->cd();
    //Adjust NTrack range
    hCorrelationCheck->GetXaxis()->SetRangeUser(0,150);
    //hCorrelationCheck->GetZaxis()->SetRangeUser(-5,270);
    hCorrelationCheck->GetZaxis()->SetTitleOffset(0.9);

    //format colors and draw
    hCorrelationCheck->GetXaxis()->SetTitle(firstMClatex.c_str());
    hCorrelationCheck->GetYaxis()->SetTitle("log(p_{T}(B^{+})) [MeV]");
    hCorrelationCheck->GetZaxis()->SetTitle("Entries [a.u.]");
    hCorrelationCheck->Draw("COLZ"); //TODO

    addLHCbtag(0.4, 0.9, "data", 0);
    TLatex* CorrCoe = new TLatex();
    CorrCoe->SetTextFont(132);
    CorrCoe->SetTextColor(0);
    CorrCoe->SetTextSize(0.05);
    CorrCoe->SetTextAlign(13);
    CorrCoe->SetNDC(1);
    CorrCoe->DrawLatex(0.4,0.83,Form("Correlation coefficient: %.3f", CorrelationCoefficent));


    c4->SaveAs(canvasPath.c_str());
    replace(canvasPath,".root",".eps");
    c4->Print(canvasPath.c_str());

    c4->Clear();
    delete c4;
    return;
}


////////////////////////////////////
///
///
///     BDT scan
///
///
////////////////////////////////////

void designYieldGraph(TGraphErrors *graph, int Run, string year, string tag, string basicPath, TFile *TGraphOutput, bool fineScan){

    if (graph == NULL){
        coutWarning("Graph " + tag + " is void.");
        return;
    }
    coutInfo("Saving " + tag + " to file and plotting.");

    string graphName = string(TMVAmethod) + " ";
    graphName.append(tag);
    graphName.append( Run == 0 ? year : string("Run" + to_string(Run)) );

    designTGraph(graph,graphName,string(TMVAmethod)+"response", graphName, kBlue+3, 20);
    graph->SetMarkerSize(0.6);
    if (fineScan) graph->GetXaxis()->SetRangeUser(0.9,1.0);

    //Save in a file
    TGraphOutput->cd();
    graph->Write("",TObject::kWriteDelete);

    //Save in a canvas
    TCanvas * c1 = new TCanvas("c1", "c1");
    c1->cd();
    graph->Draw("ap");
    string path = basicPath;
    replace(path,".root","_"+tag+".eps");
    c1->SaveAs(path.c_str(),"eps");
    return;
}


////////////////////////////////////
///
///
///     Yiled plots
///
///
////////////////////////////////////

void design_YieldInQ2(int Run, TGraphErrors *graphSig, TGraphErrors *graphBkg, TGraphErrors *graphSignificance, TGraphErrors *CMS, bool fixRange){

    string mainName = "Q2_Run" + to_string(Run);

    designTGraph(graphSig,"Signal","Q2","Yield",kBlack,20);
    designTGraph(graphBkg,"Background","Q2","Yield",kRed,20);
    graphSig->SetLineStyle(1);
    graphSig->SetLineWidth(1);
    graphSig->SetMarkerSize(0.5);
    graphBkg->SetLineStyle(1);
    graphBkg->SetLineWidth(1);
    graphBkg->SetMarkerSize(0.65);

    designTGraph(graphSignificance,"Significance","Q2","S/(S+B)",kBlue,20);
    designTGraph(CMS,"Significance_CMS","Q2","S/(S+B)",kGreen+2,20);

    graphSignificance->GetYaxis()->SetRangeUser(0.0,graphSignificance->GetY()[0]*1.5);
    graphSignificance->GetXaxis()->SetRangeUser(0.01,20);
    CMS->GetXaxis()->SetRangeUser(0.01,20);
    graphSignificance->SetLineWidth(3);
    CMS->SetLineWidth(3);

    //Create multigraphs
    TMultiGraph *mg = new TMultiGraph();
    mg->Add(graphSig,"AP");
    mg->Add(graphBkg,"AP");
    mg->Draw("AP");
    mg->GetYaxis()->SetTitle("Yield");
    mg->GetYaxis()->SetRangeUser(0.0,graphBkg->GetY()[1]*1.5);
    mg->GetXaxis()->SetRangeUser(0.01,20);
    mg->GetXaxis()->SetTitle("");

    TMultiGraph *mg_FoM = new TMultiGraph();
    mg_FoM->Add(graphSignificance,"AP");
    mg_FoM->Add(CMS,"AP");
    mg_FoM->Draw("AP");
    mg_FoM->GetYaxis()->SetTitle("S/(S+B)");
    mg_FoM->GetXaxis()->SetRangeUser(0.01,20);
    mg_FoM->GetXaxis()->SetTitle("q^{2} [GeV^{2}]");


    //Create a TCanvas
    TCanvas *c = c_Efficiency(mainName.c_str());
    c->cd();

    //Create two pads (to get significance y-axis on the right)
    TPad *pad1 = new TPad("pad1","",0,0,1,1);
    TPad *pad2 = new TPad("pad2","",0,0,1,1);

    pad2->SetFillStyle(4000); //will be transparent
    pad2->SetFrameFillStyle(0);
    pad1->SetRightMargin(0.16);
    pad2->SetRightMargin(0.16);
    pad1->Draw();
    pad1->cd();

    //Add legends
    TLegend *leg = new TLegend(0.15,0.93,0.52,0.82);
    leg->AddEntry(graphSig, "Signal yield","l");
    leg->AddEntry(graphBkg, "Background yield","l");

    TLegend *legSignificance = new TLegend(0.52,0.93,0.8,0.82);
    legSignificance->AddEntry(graphSignificance, "Significance","l");
    legSignificance->AddEntry(CMS, "CMS","l");

    //Plot it
    pad1->cd();
    gStyle->SetGridColor(kGray);
    pad1->SetGridy();
    pad1->SetGridx();
    mg->Draw("SAME");
    mg->GetYaxis()->SetTitle("Yield");
    mg->GetYaxis()->SetRangeUser(0.0,graphBkg->GetY()[1]*1.5);
    mg->GetXaxis()->SetRangeUser(0.01,20);
    mg->GetXaxis()->SetLabelSize(0);
    mg->GetYaxis()->SetTickLength(0);
    leg->Draw("SAME");
    pad1->Update();
    c->cd();

    pad2->Draw();
    pad2->cd();
    //Get axis on the left side
    TGaxis *axis = new TGaxis(20,0.01,20,graphSignificance->GetY()[0]*1.5,0,graphSignificance->GetY()[0]*1.75,510,"+L");
    axis->SetTitle("S/sqrt(S+B)");
    axis->SetTextFont(21);
    mg_FoM->Draw("AP");
    mg_FoM->GetXaxis()->SetRangeUser(0.01,20);
    mg_FoM->GetYaxis()->SetRangeUser(0.0,graphSignificance->GetY()[0]*1.5);
    mg_FoM->GetYaxis()->SetLabelSize(0);
    mg_FoM->GetYaxis()->SetTickLength(0);
    mg_FoM->GetYaxis()->SetTitle("");
    axis->Draw("SAME");
    legSignificance->Draw("SAME");
    pad2->Update();
    c->cd();

    //Save it
    string path = GetBDTScanFile("","both",Run,false,false,false);
    replace(path,"BDTscan","Q2");
    if (!fixRange) replace(path,".root","_sigma.root");
    TFile *file = new TFile (path.c_str(), "RECREATE");
    file->cd();
    graphSig->Write();
    graphBkg->Write();
    graphSignificance->Write();
    CMS->Write();
    c->Write();
    file->Close();

    replace(path,".root",".eps");
    c->SaveAs(path.c_str());
    return;

}

void design_SignificanceInQ2(int Run, TGraphErrors *graphSignificance, TGraphErrors *CMS, bool fixRange){
    string mainName = "Q2_Run" + to_string(Run);

    designTGraph(graphSignificance,"Significance","Q2","S/(S+B)",kBlue,20);
    designTGraph(CMS,"Significance_CMS","Q2","S/(S+B)",kGreen+2,20);

    graphSignificance->GetYaxis()->SetRangeUser(0.0,graphSignificance->GetY()[0]*1.5);
    graphSignificance->GetXaxis()->SetRangeUser(0.01,20);
    CMS->GetXaxis()->SetRangeUser(0.01,20);
    graphSignificance->SetLineWidth(3);
    CMS->SetLineWidth(3);

    //Create multigraph
    TMultiGraph *mg_FoM = new TMultiGraph();
    mg_FoM->Add(graphSignificance,"AP");
    mg_FoM->Add(CMS,"AP");
    mg_FoM->Draw("AP");

    //Create a TCanvas
    TCanvas *c = c_canvas(mainName.c_str());
    gStyle->SetGridColor(kGray);
    c->SetGridy();
    c->SetGridx();
    c->SetBottomMargin(0.16);

    //Add legend
    TLegend *legSignificance = new TLegend(0.35,0.9,0.65,0.75);
    legSignificance->AddEntry(graphSignificance, "Significance","l");
    legSignificance->AddEntry(CMS, "CMS","l");

    //Plot it
    c->cd();
    mg_FoM->Draw("AP");
    mg_FoM->GetYaxis()->SetTitle("S/(S+B)");
    mg_FoM->GetXaxis()->SetRangeUser(0.01,20);
    mg_FoM->GetYaxis()->SetRangeUser(0.0,graphSignificance->GetY()[0]*1.5);
    mg_FoM->GetXaxis()->SetTitle("q^{2} [GeV^{2}]");
    legSignificance->Draw("SAME");
    c->Update();

    //Save it
    string path = GetBDTScanFile("","both",Run,false,false,false);
    replace(path,"BDTscan","Q2_sigOnly");
    if (!fixRange) replace(path,".root","_sigma.root");

    TFile *file = new TFile (path.c_str(), "RECREATE");
    file->cd();
    graphSignificance->Write();
    CMS->Write();
    c->Write();
    file->Close();


    replace(path,".root",".eps");
    c->SaveAs(path.c_str());

    return;

}







