import ROOT  as r
import tdrstyle
from ROOT import TH1, TEfficiency, TFile, TCanvas, TAxis

####### Inputs
tfile = TFile.Open('output_SingleMuon_C2_Loose.root')
num   = tfile.Get('h_2BTag_ht_numerator')
den   = tfile.Get('h_ht_denominator')
eff   = TEfficiency(num,den)
Xaxis = 'Offline 6th jet p_{T} [GeV]'
Yaxis = 'Efficiency'
name  = 'test'
Lumi  = 5.5

xmin = 450
xmax = 1000

###### Do not touch below here (hopefully)
tdrstyle.setTDRStyle()



# CMS inputs
H_ref = 600;
W_ref = 800;
W = W_ref
H  = H_ref

T = 0.08*H_ref
B = 0.13*H_ref
L = 0.12*W_ref
R = 0.04*W_ref

# setting up canvas
canvas = TCanvas("c2","c2",50,50,W,H)
canvas.SetFillColor(0)
canvas.SetBorderMode(0)
canvas.SetFrameFillStyle(0)
canvas.SetFrameBorderMode(0)
canvas.SetLeftMargin( L/W )
canvas.SetRightMargin( R/W )
canvas.SetTopMargin( T/H )
canvas.SetBottomMargin( B/H )
canvas.SetTickx(0)
canvas.SetTicky(0)
canvas.SetGridy()
canvas.SetGridx()

# setting up lumi text
texlumi = r.TLatex(-20.,50., '%2.1f fb^{-1}, #sqrt{s} = 13 TeV (2017)'%Lumi)
texlumi.SetNDC();
texlumi.SetTextAlign(12);
texlumi.SetX(0.49);
texlumi.SetY(0.97);
texlumi.SetTextFont(42);
texlumi.SetTextSize(0.06);
texlumi.SetTextSizePixels(22);

#settign up cms text
texcms = r.TLatex(0.,0., 'CMS');
texcms.SetNDC();
texcms.SetTextAlign(12);
texcms.SetX(0.15);
texcms.SetY(0.89);
texcms.SetTextSize(0.06);
texcms.SetTextSizePixels(22);

# setting up preliminary text
texPrelim = r.TLatex(0.,0., 'Preliminary');
texPrelim.SetNDC();
texPrelim.SetTextAlign(12);
texPrelim.SetX(0.15);
texPrelim.SetY(0.83);
texPrelim.SetTextFont(52);
texPrelim.SetTextSize(0.052);
texPrelim.SetTextSizePixels(22);


# setting up tefficiency
eff.Draw('A,P')
r.gPad.Update()
eff.GetPaintedGraph().SetMaximum(1.2)
eff.GetPaintedGraph().SetMinimum(0. )
eff.GetPaintedGraph().GetXaxis().SetLimits(xmin,xmax)
eff.SetTitle(' ; {xtitle} ; {ytitle}'.format(xtitle=Xaxis, ytitle=Yaxis))
eff.GetPaintedGraph().GetYaxis().SetTitleOffset(1)
eff.GetPaintedGraph().GetXaxis().SetTitleOffset(1.015)
eff.GetPaintedGraph().GetXaxis().SetLabelOffset(0.004)
eff.SetMarkerStyle(r.kFullCircle)
eff.SetMarkerSize(0.9); eff.GetPaintedGraph().SetLineColor(r.kRed)
#eff.GetPaintedGraph().GetXaxis().SetTitleOffset(1)

r.gPad.Modified()
r.gPad.Update()

# drawing

eff.Draw('ap')

texlumi.Draw('same')
texPrelim.Draw("same")
texcms.Draw("same")

# saving
canvas.SaveAs(name + '.pdf')
canvas.SaveAs(name + '.png')
canvas.SaveAs(name + '.root')


raw_input('meh')
