import ROOT as r
import sys

r.gROOT.SetBatch()
r.gStyle.SetOptStat(0)

infile, outfile = sys.argv[1:]

f = r.TFile(infile)
tps = f.Get("compareL1T/tps")

c = r.TCanvas()
c.SetRightMargin(c.GetRightMargin() * 1.5)
c.SaveAs(outfile + '[')

tps.Draw("et:et_emul>>etmatch(256,-0.25,128.25,256,-0.25,128.25)", "(version==1)", "COLZ")
r.gDirectory.Get("etmatch").SetTitle("HF only;Re-emul E_{T};L1T E_{T};")
c.SaveAs(outfile)

tps.Draw("soi:soi_emul>>soimatch(256,-0.5,255.5,256,-0.5,255.5)", "(version==1)", "COLZ")
r.gDirectory.Get("soimatch").SetTitle("HF only;Re-emul compressed E_{T};L1T compressed E_{T};")
c.SaveAs(outfile)

tps.Draw("fg1:fg1_emul>>mismatch", "(version==1 && soi==soi_emul)", "text COLZ")
r.gDirectory.Get("mismatch").SetTitle(";Re-emul FG;L1T FG;")
c.SaveAs(outfile)

tps.Draw("iphi:ieta>>histdet(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul) * (fg1-fg1_emul)", "COLZ")
r.gDirectory.Get("histdet").SetTitle(";ieta;iphi;#sum L1T FG - FG reemulated")
c.SaveAs(outfile)
c.SetLogz()
tps.Draw("iphi:ieta>>histdet2(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul) * ((fg1 && soi>0) && !fg1_emul)", "COLZ")
tps.Draw("iphi:ieta>>histdet3(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul)", "COLZ")
r.gDirectory.Get("histdet2").Divide(r.gDirectory.Get("histdet3"))
r.gDirectory.Get("histdet2").SetTitle("FG set in RAW w/ compressed E_{T} > 0 but not re-emulated (rel. freq.);ieta;iphi;(#sum L1T FG && !FG reemulated) / (#sum # TP)")
r.gDirectory.Get("histdet2").GetZaxis().SetRangeUser(0.0001, 1)
r.gDirectory.Get("histdet2").Draw("COLZ")
c.SaveAs(outfile)
tps.Draw("iphi:ieta>>histdet2(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul) * (!fg1 && fg1_emul)", "COLZ")
tps.Draw("iphi:ieta>>histdet3(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul)", "COLZ")
r.gDirectory.Get("histdet2").Divide(r.gDirectory.Get("histdet3"))
r.gDirectory.Get("histdet2").SetTitle("FG re-emulated but not set in RAW (rel. freq.);ieta;iphi;(#sum !L1T FG && FG reemulated) / (#sum # TP)")
r.gDirectory.Get("histdet2").GetZaxis().SetRangeUser(0.0001, 1)
r.gDirectory.Get("histdet2").Draw("COLZ")
c.SaveAs(outfile)
tps.Draw("iphi:ieta>>histdet2(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul) * fg1_emul", "COLZ")
tps.Draw("iphi:ieta>>histdet3(85,-42.5,42.5,73,0,72)", "(version==1 && soi==soi_emul)", "COLZ")
r.gDirectory.Get("histdet2").Divide(r.gDirectory.Get("histdet3"))
r.gDirectory.Get("histdet2").SetTitle("FG set when re-emulated (rel. freq.);ieta;iphi;(#sum FG reemulated) / (#sum # TP)")
r.gDirectory.Get("histdet2").GetZaxis().SetRangeUser(0.0001, 1)
r.gDirectory.Get("histdet2").Draw("COLZ")
c.SaveAs(outfile)
tps.Draw("fg1-fg1_emul:soi>>histsoi", "version==1 && soi==soi_emul", "COLZ")
r.gDirectory.Get("histsoi").SetTitle(";Compressed E_{T};L1T FG - FG reemulated;")
c.SetLogz()
c.SaveAs(outfile)
c.SetLogz(False)

tps.Draw("soi:abs(ieta)>>histset(12,29.5,41.5,61,-0.5,60.5)", "(version==1 && soi==soi_emul) && (fg1 > fg1_emul)", "COLZ")
r.gDirectory.Get("histset").SetTitle("FG in RAW, but not re-emulated;ieta;Compressed E_{T};")
c.SaveAs(outfile)

tps.Draw("soi:abs(ieta)>>histset(12,29.5,41.5,61,-0.5,60.5)", "(version==1 && soi==soi_emul) && (fg1 < fg1_emul)", "COLZ")
r.gDirectory.Get("histset").SetTitle("FG re-emulated, but not in RAW;ieta;Compressed E_{T};")
c.SaveAs(outfile)

tps.Draw("soi>>histspeca(256,0,256)", "(version==1 && soi==soi_emul) && fg1 > fg1_emul", "COLZ")
tps.Draw("soi>>histspecb(256,0,256)", "(version==1 && soi==soi_emul) && fg1 < fg1_emul", "COLZ")
tps.Draw("soi>>histspecc(256,0,256)", "(version==1 && soi==soi_emul) && fg1 != fg1_emul", "COLZ")
tps.Draw("soi>>histspecd(256,0,256)", "(version==1 && soi==soi_emul)", "COLZ")

r.gDirectory.Get("histspecc").Divide(r.gDirectory.Get("histspecd"))
r.gDirectory.Get("histspecc").SetLineColor(r.kBlack)
r.gDirectory.Get("histspecc").SetLineWidth(2)
r.gDirectory.Get("histspecc").SetTitle(";Compressed E_{T};Fraction")
r.gDirectory.Get("histspecc").SetMinimum(0)
r.gDirectory.Get("histspecc").SetMaximum(1)
r.gDirectory.Get("histspecc").Draw()
r.gDirectory.Get("histspeca").Divide(r.gDirectory.Get("histspecd"))
r.gDirectory.Get("histspeca").SetLineColor(r.kBlue)
r.gDirectory.Get("histspeca").SetLineWidth(2)
r.gDirectory.Get("histspeca").Draw("same")
r.gDirectory.Get("histspecb").Divide(r.gDirectory.Get("histspecd"))
r.gDirectory.Get("histspecb").SetLineColor(r.kRed)
r.gDirectory.Get("histspecb").SetLineStyle(r.kDashed)
r.gDirectory.Get("histspecb").SetLineWidth(3)
r.gDirectory.Get("histspecb").Draw("same")

legend = r.TLegend(0.59, 0.69, 0.84, 0.89)
legend.SetLineWidth(0)
legend.AddEntry("histspecc", "L1T FG != emul FG", "l")
legend.AddEntry("histspeca", "L1T FG > emul FG", "l")
legend.AddEntry("histspecb", "L1T FG < emul FG", "l")
legend.Draw()

c.SaveAs(outfile)

tps.Draw("abs(ieta)>>histspec2a(12,29.5,41.5)", "(version==1 && soi==soi_emul) && (fg1 > fg1_emul)")
tps.Draw("abs(ieta)>>histspec2b(12,29.5,41.5)", "(version==1 && soi==soi_emul) && (fg1 < fg1_emul)")
tps.Draw("abs(ieta)>>histspec2c(12,29.5,41.5)", "(version==1 && soi==soi_emul) && (fg1 != fg1_emul)")
tps.Draw("abs(ieta)>>histspec2d(12,29.5,41.5)", "(version==1 && soi==soi_emul)")

r.gDirectory.Get("histspec2c").Divide(r.gDirectory.Get("histspec2d"))
r.gDirectory.Get("histspec2c").SetLineColor(r.kBlack)
r.gDirectory.Get("histspec2c").SetLineWidth(2)
r.gDirectory.Get("histspec2c").SetTitle(";ieta;Fraction")
r.gDirectory.Get("histspec2c").SetMinimum(0)
r.gDirectory.Get("histspec2c").SetMaximum(1)
r.gDirectory.Get("histspec2c").Draw()
r.gDirectory.Get("histspec2a").Divide(r.gDirectory.Get("histspec2d"))
r.gDirectory.Get("histspec2a").SetLineColor(r.kBlue)
r.gDirectory.Get("histspec2a").SetLineWidth(2)
r.gDirectory.Get("histspec2a").Draw("same")
r.gDirectory.Get("histspec2b").Divide(r.gDirectory.Get("histspec2d"))
r.gDirectory.Get("histspec2b").SetLineColor(r.kRed)
r.gDirectory.Get("histspec2b").SetLineStyle(r.kDashed)
r.gDirectory.Get("histspec2b").SetLineWidth(3)
r.gDirectory.Get("histspec2b").Draw("same")

legend = r.TLegend(0.59, 0.69, 0.84, 0.89)
legend.SetLineWidth(0)
legend.AddEntry("histspec2c", "L1T FG != emul FG", "l")
legend.AddEntry("histspec2a", "L1T FG > emul FG", "l")
legend.AddEntry("histspec2b", "L1T FG < emul FG", "l")
legend.Draw()

c.SaveAs(outfile)

tps.Draw("fg1:fg1_emul>>mismatch", "(version==1 && soi==soi_emul) && (fg2_emul>0)", "text COLZ")
r.gDirectory.Get("mismatch").SetTitle("Depth 1 (L1) above threshold;Re-emul FG;L1T FG;")
c.SaveAs(outfile)

tps.Draw("fg1:fg1_emul>>mismatch", "(version==1 && soi==soi_emul) && (fg3_emul>0)", "text COLZ")
r.gDirectory.Get("mismatch").SetTitle("Depth 2 (S1) above threshold;Re-emul FG;L1T FG;")
c.SaveAs(outfile)

tps.Draw("fg1:fg1_emul>>mismatch", "(version==1 && soi==soi_emul) && (fg4_emul>0)", "text COLZ")
r.gDirectory.Get("mismatch").SetTitle("Depth 3 (L2) above threshold;Re-emul FG;L1T FG;")
c.SaveAs(outfile)

tps.Draw("fg1:fg1_emul>>mismatch", "(version==1 && soi==soi_emul) && (fg5_emul>0)", "text COLZ")
r.gDirectory.Get("mismatch").SetTitle("Depth 4 (L3) above threshold;Re-emul FG;L1T FG;")
c.SaveAs(outfile)

tps.Draw("fg1:(fg2_emul+fg3_emul+fg4_emul+fg5_emul)>>hx(5,-0.5,4.5,2,-0.5,1.5)", "(version==1 && soi==soi_emul)", "COLZ text")
r.gDirectory.Get("hx").SetTitle(";#sum re-emul FG(depth);L1T FG")
c.SaveAs(outfile)

tps.Draw("fg1_emul:(fg2_emul+fg3_emul+fg4_emul+fg5_emul)>>hx(5,-0.5,4.5,2,-0.5,1.5)", "(version==1 && soi==soi_emul)", "COLZ text")
r.gDirectory.Get("hx").SetTitle(";#sum re-emul FG(depth);re-emul FG")
c.SaveAs(outfile)

c.SaveAs(outfile + ']')
