Exemple #1
0
def make_threebody_plots(plt, reg, sig) :

    # get the canvases
    rcan = plt.ratioCanvas
    rcan.canvas.cd()
    rcan.upper_pad.cd()

    if plt.isLog() : rcan.upper_pad.SetLogy(True)

    # draw the frame
    hax = r.TH1F("axes","", int(plt.nbins), plt.x_range_min, plt.x_range_max)
    hax.SetMinimum(plt.y_range_min)
    hax.SetMaximum(plt.y_range_max)
    hax.GetXaxis().SetTitle(plt.x_label)
    hax.GetXaxis().SetTitleFont(42)
    hax.GetXaxis().SetLabelFont(42)
    hax.GetXaxis().SetLabelSize(0.035)
    hax.GetXaxis().SetTitleSize(0.048*0.85)
    hax.GetXaxis().SetTitleOffset(-999)
    hax.GetXaxis().SetLabelOffset(-999)

    hax.GetYaxis().SetTitle(plt.y_label)
    hax.GetYaxis().SetTitleFont(42)
    hax.GetYaxis().SetLabelFont(42)
    hax.GetYaxis().SetTitleOffset(1.4)
    hax.GetYaxis().SetLabelOffset(0.013)
    hax.GetYaxis().SetLabelSize(1.2*0.035)
    hax.GetYaxis().SetTitleSize(0.055*0.85)
    hax.Draw("axis")
    rcan.upper_pad.Update()

    # legend
    leg = pu.default_legend(xl=0.6, yl=0.65, xh=0.97, yh=0.90)
    leg.SetHeader("%s"%sig.displayname)
    leg.SetNColumns(1)
    
    #############################################################
    # get histogram for nom, 3-body (Right, Left, Mass-only)
    ############################################################
    h_nom = get_histogram(plt, sig, reg, "nom")
    h_r = get_histogram(plt, sig, reg, "right")
    h_l = get_histogram(plt, sig, reg, "left")
    h_m = get_histogram(plt, sig, reg, "mass") 

    #filetest = r.TFile.Open("/data/uclhc/uci/user/dantrim/ntuples/n0222/may23/mc/Raw/CENTRAL_387947.root")
    #treetest = filetest.Get("superNt")
    #c = r.TCanvas("c", "", 768, 768)
    #c.cd()
    #htest = r.TH1F("h","", 32, 0, 250)
    #cut = "(" + reg.tcut + ") * " + "eventweightNOPUPW" 
    #cut = r.TCut(cut)
    #sel = r.TCut("1")
    #cmd = "%s>>%s"%("MDR", htest.GetName()) 
    #treetest.Draw(cmd, cut*sel)
    #c.SaveAs("testing.eps")
    


    ############
    # hergiw
    #hf = r.TFile.Open("/data/uclhc/uci/user/dantrim/ntuples/n0222/apr17/mc/Raw/CENTRAL_406010.root")
    hf = r.TFile.Open("/data/uclhc/uci/user/dantrim/ntuples/n0222/may23/mc/Raw/CENTRAL_406010.root")
    herwig_250_160_tree = hf.Get("superNt")
    
    print "blah ", reg.tcut
    cut = "(" + reg.tcut + ") * " + "eventweightNOPUPW" 
    cut = r.TCut(cut)
    sel = r.TCut("1")
    hh = pu.th1f("h_herwig_" + "_" + sig.name + "_herwig", "", int(plt.nbins), plt.x_range_min, plt.x_range_max, plt.x_label, plt.y_label)
    hh.SetMinimum(plt.y_range_min)
    hh.SetMaximum(plt.y_range_max)
    hh.GetXaxis().SetTitleOffset(-999)
    hh.GetXaxis().SetLabelOffset(-999)
    hh.SetLineColor(r.kBlack)
    hh.SetLineWidth(2)
    hh.SetLineStyle(2)
    hh.SetFillStyle(0)
    cmd = "%s>>%s"%(plt.variable, hh.GetName()) 
    herwig_250_160_tree.Draw(cmd, cut * sel, "goff")


    histos = [h_nom, h_r, h_l, h_m]

    # overflows
    for h in histos:
        pu.add_overflow_to_lastbin(h)

    # colors
    for h in histos :
        color, name = get_color_from_name(h)
        h.SetLineColor(color)
        h.SetLineWidth(2)
        h.SetFillStyle(0)
        leg.AddEntry(h, name, "l") 

    # draw
    histos.append(hh)
    is_first = True
    for h in histos :
        if is_first :
            is_first = False
            rcan.upper_pad.cd()
            
            h.Draw("hist e")
            rcan.upper_pad.Update()
        else :
            rcan.upper_pad.cd()
            h.Draw("hist e same")
            rcan.upper_pad.Update()

    #hwerwig
    hh.SetLineColor(r.kRed)
    hh.SetFillStyle(0)
    hh.Draw("hist same")
    leg.AddEntry(hh, "Herwig++", "l")

    rcan.upper_pad.SetGridx(True)
    rcan.upper_pad.SetGridy(True)

    # draw the legend
    leg.Draw()


    ################################################
    ## ratio
    ################################################
    rcan.lower_pad.cd()

    h_den = h_nom.Clone("h_den")
    # y-axis
    yax = h_den.GetYaxis()
    yax.SetRangeUser(0,2)
    yax.SetTitle("Variation/Nominal")
    yax.SetTitleSize(0.14 * 0.75)
    yax.SetLabelSize(0.13)
    yax.SetLabelOffset(0.98 * 0.013)
    yax.SetTitleOffset(0.48)
    yax.SetLabelFont(42)
    yax.SetTitleFont(42)
    yax.SetNdivisions(5)
    # x-axis
    xax = h_den.GetXaxis()
    xax.SetTitleSize(1.0 * 0.14)
    xax.SetLabelSize(0.13)
    xax.SetLabelOffset(1.15*0.02)
    xax.SetTitleOffset(1.1)#0.2*2)
    #xax.SetTitleOffset(0.1 * xax.GetTitleOffset())
    xax.SetLabelFont(42)
    xax.SetTitleFont(42) 

    h_den.SetTickLength(0.06)
    h_den.Draw("AXIS")
    h_den.SetTitle(plt.x_label)

    # unity line
    pu.draw_line(plt.x_range_min, 1.0, plt.x_range_max, 1.0, color=r.kRed, style=2, width=1)

    # right
    h_rl = h_r.Clone("h_rl")
    h_rl.Divide(h_den)
    h_rl.Draw("hist e same")
    rcan.lower_pad.Update()

    # left
    h_ll = h_l.Clone("h_ll")
    h_ll.Divide(h_den)
    h_ll.Draw("hist e same")
    rcan.lower_pad.Update()

    # mass
    h_ml = h_m.Clone("h_ml")
    h_ml.Divide(h_den)
    h_ml.Draw("hist e same")
    rcan.lower_pad.Update()


    rcan.canvas.SaveAs("test_%s.eps"%plt.variable)
Exemple #2
0
def make_wp_comparison_plot(variable, sample, reg) :

    print 50*"-"
    print "Plotting WP comparison : %s"%variable


    nice_names = {}
    nice_names["bjets_n"] = "b-jet Multiplicity"
    nice_names["sjets_n"] = "Non b-tagged jet Multiplicity"
    nice_names["bjets_pt0"] = "Lead b-jet p_{T} [GeV]"
    nice_names["bjets_pt1"] = "2nd b-jet p_{T} [GeV]"
    nice_names["bjets_pt2"] = "3rd b-jet p_{T} [GeV]"
    nice_names["bjets_pt3"] = "4th b-jet p_{T} [GeV]"
    nice_names["bjets_nTrk0"] = "Lead b-jet # tracks"
    nice_names["bjets_nTrk1"] = "2nd b-jet # tracks"
    nice_names["bjets_nTrk2"] = "3rd b-jet # tracks"
    nice_names["bjets_nTrk3"] = "4th b-jet # Ttracks"
    nice_names["bjets_jvt0"] = "Lead b-jet JVT"
    nice_names["bjets_jvt1"] = "2nd b-jet JVT"
    nice_names["bjets_jvt2"] = "3rd b-jet JVT"
    nice_names["bjets_jvt3"] = "4th b-jet JVT"
    nice_names["bjets_eta0"] = "Lead b-jet #eta"
    nice_names["bjets_eta1"] = "2nd b-jet #eta"
    nice_names["bjets_eta2"] = "3rd b-jet #eta"
    nice_names["bjets_eta3"] = "4th b-jet #eta"
    nice_names["mt2"] = "m_{t2} [GeV]"
    nice_names["MDR"] = "M_{#Delta}^{R} [GeV]"
    nice_names["DPB_vSS"] = "#Delta#phi_{#beta}^{R}"

    bounds = {}
    bounds["bjets_n"] = [1,0,10,1e9]
    bounds["sjets_n"] = [1,0,10,1e9]
    bounds["bjets_pt0"] = [10, 0, 350,1e9]
    bounds["bjets_pt1"] = [10, 0, 350,1e9]
    bounds["bjets_pt2"] = [10, 0, 200,1e9]
    bounds["bjets_pt3"] = [10, 0, 200,1e9]
    bounds["bjets_nTrk0"] = [1, 0, 30, 1e9]
    bounds["bjets_nTrk1"] = [1, 0, 30, 1e9]
    bounds["bjets_nTrk2"] = [1, 0, 30, 1e9]
    bounds["bjets_nTrk3"] = [1, 0, 30, 1e9]
    bounds["bjets_jvt0"] = [0.02, 0, 1, 1e9]
    bounds["bjets_jvt1"] = [0.02, 0, 1, 1e9]
    bounds["bjets_jvt2"] = [0.02, 0, 1, 1e9]
    bounds["bjets_jvt3"] = [0.02, 0, 1, 1e9]
    bounds["bjets_eta0"] = [0.2, -3, 3, 1e9]
    bounds["bjets_eta1"] = [0.2, -3, 3, 1e9]
    bounds["bjets_eta2"] = [0.2, -3, 3, 1e9]
    bounds["bjets_eta3"] = [0.2, -3, 3, 1e9]
    bounds["mt2"] = [10, 0, 300, 1e9]
    bounds["MDR"] = [10, 0, 300, 1e9]
    bounds["DPB_vSS"] = [0.1, 0, 3.2, 1e9]

    variables = {}
    variables["bjets_n"] =     { 70 : "nBJets70", 77 : "nBJets", 85 : "nBJets85" , 0 : "nBJetsMatched"}
    variables["sjets_n"] =     { 70 : "nSJets70", 77 : "nSJets", 85 : "nSJets85" , 0 : "nSJetsMatched"}
    variables["bjets_pt0"] =   { 70 : "bj70_pt[0]", 77 : "bj_pt[0]", 85 : "bj85_pt[0]", 0 : "bjMatched_pt[0]"}
    variables["bjets_pt1"] =   { 70 : "bj70_pt[1]", 77 : "bj_pt[1]", 85 : "bj85_pt[1]", 0 : "bjMatched_pt[1]" }
    variables["bjets_pt2"] =   { 70 : "bj70_pt[2]", 77 : "bj_pt[2]", 85 : "bj85_pt[2]", 0 : "bjMatched_pt[2]" }
    variables["bjets_pt3"] =   { 70 : "bj70_pt[3]", 77 : "bj_pt[3]", 85 : "bj85_pt[3]", 0 : "bjMatched_pt[3]" }
    variables["bjets_nTrk0"] = { 70 : "bj70_nTrk[0]", 77 : "bj_nTrk[0]", 85 : "bj85_nTrk[0]", 0 : "bjMatched_nTrk[0]" }
    variables["bjets_nTrk1"] = { 70 : "bj70_nTrk[1]", 77 : "bj_nTrk[1]", 85 : "bj85_nTrk[1]", 0 : "bjMatched_nTrk[1]" }
    variables["bjets_nTrk2"] = { 70 : "bj70_nTrk[2]", 77 : "bj_nTrk[2]", 85 : "bj85_nTrk[2]", 0 : "bjMatched_nTrk[2]" }
    variables["bjets_nTrk3"] = { 70 : "bj70_nTrk[3]", 77 : "bj_nTrk[3]", 85 : "bj85_nTrk[3]", 0 : "bjMatched_nTrk[3]" }
    variables["bjets_jvt0"] =  { 70 : "bj70_jvt[0]", 77 : "bj_jvt[0]", 85 : "bj85_jvt[0]" , 0 : "bjMatched_jvt[0]"}
    variables["bjets_jvt1"] =  { 70 : "bj70_jvt[1]", 77 : "bj_jvt[1]", 85 : "bj85_jvt[1]" , 0 : "bjMatched_jvt[1]"}
    variables["bjets_jvt2"] =  { 70 : "bj70_jvt[2]", 77 : "bj_jvt[2]", 85 : "bj85_jvt[2]" , 0 : "bjMatched_jvt[2]"}
    variables["bjets_jvt3"] =  { 70 : "bj70_jvt[3]", 77 : "bj_jvt[3]", 85 : "bj85_jvt[3]" , 0 : "bjMatched_jvt[3]"}
    variables["bjets_eta0"] =  { 70 : "bj70_eta[0]", 77 : "bj_eta[0]", 85 : "bj85_eta[0]", 0 : "bjMatched_eta[0]" }
    variables["bjets_eta1"] =  { 70 : "bj70_eta[1]", 77 : "bj_eta[1]", 85 : "bj85_eta[1]", 0 : "bjMatched_eta[1]" }
    variables["bjets_eta2"] =  { 70 : "bj70_eta[2]", 77 : "bj_eta[2]", 85 : "bj85_eta[2]", 0 : "bjMatched_eta[2]" }
    variables["bjets_eta3"] =  { 70 : "bj70_eta[3]", 77 : "bj_eta[3]", 85 : "bj85_eta[3]", 0 : "bjMatched_eta[3]" }
    variables["mt2"] = { 70 : "mt2", 77 : "mt2", 85 : "mt2", 0 : "mt2" }
    variables["MDR"] = { 70 : "MDR", 77 : "MDR", 85 : "MDR", 0 : "MDR" }
    variables["DPB_vSS"] = { 70 : "DPB_vSS", 77 : "DPB_vSS", 85 : "DPB_vSS", 0 : "DPB_vSS" }

    colors = {}
    colors[70] = 30
    colors[77] = 38
    colors[85] = 46
    colors[0] = r.kBlack

    
    p = plot.Plot1D()
    p.initialize("%s"%str(reg.name), variable, "wp_comp_%s_%s"%(reg.name, variable))
    p.labels(x=nice_names[variable], y = "Events")
    p.xax(bounds[variable][0], bounds[variable][1], bounds[variable][2])
    p.yax(0.1, bounds[variable][3])
    p.doLogY = True
    p.setRatioCanvas(p.name)

    # canvas
    rcan = p.ratioCanvas
    rcan.canvas.cd()
    rcan.upper_pad.cd()

    if p.isLog() : rcan.upper_pad.SetLogy(True)
    rcan.upper_pad.Update()

    # axes
    hax = r.TH1F("axis", "", int(p.nbins), p.x_range_min, p.x_range_max)
    hax.SetMinimum(p.y_range_min)
    hax.SetMaximum(1e9)
    hax.GetXaxis().SetTitle(p.x_label)
    hax.GetXaxis().SetTitleFont(42)
    hax.GetXaxis().SetLabelFont(42)
    hax.GetXaxis().SetLabelSize(0.035)
    hax.GetXaxis().SetTitleOffset(-999)

    hax.GetYaxis().SetTitle(p.y_label)
    hax.GetYaxis().SetTitleFont(42)
    hax.GetYaxis().SetLabelFont(42)
    hax.GetYaxis().SetTitleOffset(1.4)
    hax.GetYaxis().SetLabelOffset(0.013)
    hax.GetYaxis().SetLabelSize(1.2 * 0.035)
    hax.GetYaxis().SetTitleSize(0.055 * 0.85)
    hax.Draw()
    rcan.upper_pad.Update()

    # legend
    leg = pu.default_legend(xl=0.55, yl=0.71, xh=0.93, yh=0.90)

    # hists for different WP
    histos = {}

    working_points = [70, 77, 85, 0]
    for wp in working_points :
        if ("data" in sample.name or "run" in sample.name) and wp==0 : continue
        h = pu.th1f("h_" + variable + "_" + str(wp), "", int(p.nbins), p.x_range_min, p.x_range_max, p.x_label, p.y_label)
        h.SetLineColor(colors[wp])
        if wp == 0 :
            h.SetLineStyle(2)
        h.SetFillStyle(0)
        h.SetLineWidth(2)
        h.GetXaxis().SetLabelOffset(-999)
        h.SetMaximum(1e9)
        h.Sumw2

        weight_str = "eventweight"
        cut = ""

        if wp == 70 :
            reg.tcut += " && nBJets70>0"
        elif wp == 77 :
            reg.tcut += " && nBJets>0"
        elif wp == 85 :
            reg.tcut += " && nBJets85>0"
        elif wp == 0 :
            reg.tcut += " && nBJetsMatched>0"

        if "data" not in sample.name or "run" not in sample.name :
            cut = "(" + reg.tcut + ") * %s * %s"%(weight_str, str(8.42))
        else :
            cut = "(" + reg.tcut + ")"
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>+%s"%(variables[variable][wp], h.GetName()) 
        sample.tree.Draw(cmd, cut * sel, "goff")

        # print the yield +/- stat error

        stat_err = r.Double(0.0)
        integral = h.IntegralAndError(0,-1, stat_err)
        print "%s : %.2f +/- %.2f"%(str(wp), integral, stat_err)

        # add overflow
        pu.add_overflow_to_lastbin(h)

        if wp != 0 :
            leg.AddEntry(h, "%s %s%% MV2c10 WP"%(sample.displayname, str(wp)), "l")
        elif wp==0 and ("data" not in sample.name or "run" not in sample.name) :
            leg.AddEntry(h, "%s 77%% MV2c10 + Truth Match"%sample.displayname, "l")

        histos[wp] = h
        rcan.upper_pad.Update()

     
    r.gPad.SetGrid()
    histos[70].Draw("hist")
    histos[77].Draw("hist same")
    histos[85].Draw("hist same")
    if len(histos)==4 :
        histos[0].Draw("hist same")
    rcan.upper_pad.Update()

    #draw the legend
    leg.Draw()

    # text
    pu.draw_text(text="ATLAS", x=0.18, y =0.85, size=0.06, font=72) 
    pu.draw_text(text="Internal", x=0.325, y =0.85, size =0.06, font=42)
    if not ("run" in sample.name.lower() or "data" in sample.name.lower()) :
        pu.draw_text(text="L = ~27 fb^{-1}, #sqrt{s} = 13 TeV", x=0.18, y=0.79, size=0.04)
    else :
        pu.draw_text(text="L = ~200 pb^{-1}, #sqrt{s} = 13 TeV", x=0.18, y =0.79, size=0.04)
    pu.draw_text(text=reg.displayname, x=0.18, y=0.74, size=0.04)


    r.gPad.SetTickx()
    r.gPad.SetTicky()


    ######## Lower Pad
    rcan.lower_pad.cd()

    #h_den = histos[77].Clone("h_den")

    ## y-axis
    #yax = h_den.GetYaxis()
    #yax.SetRangeUser(0,2)
    #yax.SetTitle("WP X / WP 77%")
    #yax.SetTitleSize(0.1 * 0.83)
    #yax.SetLabelSize(0.1 * 0.81)
    #yax.SetLabelOffset(0.98 * 0.013 * 1.08)
    #yax.SetTitleOffset(0.45*1.2)
    #yax.SetLabelFont(42)
    #yax.SetTitleFont(42)
    #yax.SetNdivisions(5)

    ## x-axis
    #xax = h_den.GetXaxis()
    #xax.SetTitleSize(1.1 * 0.14)
    #xax.SetLabelSize(yax.GetLabelSize())
    #xax.SetLabelOffset(1.15*0.02)
    #xax.SetTitleOffset(0.85*xax.GetTitleOffset())
    #xax.SetLabelFont(42)
    #xax.SetTitleFont(42)

    #h_den.SetTickLength(0.06)
    #h_den.Draw("axis")
    #rcan.lower_pad.Update()


    # 70
    h_70 = histos[70].Clone("h70")
    # y-axis
    yax = h_70.GetYaxis()
    #yax.SetRangeUser(0,2)
    yax.SetTitle("WP X / WP 77%")
    yax.SetTitleSize(0.1 * 0.83)
    yax.SetLabelSize(0.1 * 0.81)
    yax.SetLabelOffset(0.98 * 0.013 * 1.08)
    yax.SetTitleOffset(0.45*1.2)
    yax.SetLabelFont(42)
    yax.SetTitleFont(42)
    yax.SetNdivisions(5)

    # x-axis
    xax = h_70.GetXaxis()
    xax.SetTitleSize(1.0 * 0.14)
    xax.SetLabelSize(yax.GetLabelSize())
    xax.SetLabelOffset(1.15*0.02)
    xax.SetTitleOffset(0.85*xax.GetTitleOffset())
    xax.SetLabelFont(42)
    xax.SetTitleFont(42)


    h_77 = histos[77].Clone("h77")
    h_85 = histos[85].Clone("h85")

    h_70.GetYaxis().SetRangeUser(0,5)
    h_77.GetYaxis().SetRangeUser(0,5)
    h_85.GetYaxis().SetRangeUser(0,5)
    h_70.SetNdivisions(5)
    h_77.SetNdivisions(5)
    h_85.SetNdivisions(5)

    h_70.Divide(h_77)
    h_85.Divide(h_77)

    h_70.Draw("hist")
    h_85.Draw("hist same")

    if len(histos) == 4 :
        h_0 = histos[0].Clone("h0")
        h_0.GetYaxis().SetRangeUser(0,5)
        h_0.SetNdivisions(5)
        h_0.Divide(h_77)
        h_0.Draw("hist same")
    rcan.lower_pad.Update()

    # lines
    pu.draw_line(p.x_range_min, 1.0, p.x_range_max, 1.0, color=r.kRed, style=2, width=1)

    # save
    outname = p.name + "_" + sample.name + ".eps"
    rcan.canvas.SaveAs(outname)
    out = "./bjet_check_plots/"
    utils.mv_file_to_dir(outname, out, True)
    fullname = out + "/" + outname
    print "%s saved to : %s"%(outname, os.path.abspath(fullname))
Exemple #3
0
def make_ttbar_comp_plots(var, region, samples) :
    print 50*"-"
    print "Making ttbar comparison plots, variable: %s"%var.name


    p = plot.Plot1D()
    p.initialize("%s"%str(region.name), var.name, "ttbar_comp_%s_%s"%(region.name, var.name))
    p.labels(x=var.nice_name, y = "Arb. Units")
    p.xax(var.bounds[reg.name][0], var.bounds[reg.name][1], var.bounds[reg.name][2])
    p.yax(0.1, var.bounds[reg.name][3])
    p.doLogY = True
    p.setRatioCanvas(p.name)

    # canvas
    rcan = p.ratioCanvas
    rcan.canvas.cd()
    rcan.upper_pad.cd()

    if p.isLog() : rcan.upper_pad.SetLogy(True)
    rcan.upper_pad.Update()

    # axes
    hax = r.TH1F("axis", "", int(p.nbins), p.x_range_min, p.x_range_max)
    hax.SetMinimum(p.y_range_min)
    hax.SetMaximum(10000)
    hax.GetXaxis().SetTitle(var.nice_name)
    hax.GetXaxis().SetTitleFont(42)
    hax.GetXaxis().SetLabelFont(42)
    hax.GetXaxis().SetLabelSize(0.035)
    hax.GetXaxis().SetTitleOffset(-999)

    hax.GetYaxis().SetTitle(p.y_label)
    hax.GetYaxis().SetTitleFont(42)
    hax.GetYaxis().SetLabelFont(42)
    hax.GetYaxis().SetTitleOffset(1.4)
    hax.GetYaxis().SetLabelOffset(0.013)
    hax.GetYaxis().SetLabelSize(1.2 * 0.035)
    hax.GetYaxis().SetTitleSize(0.055 * 0.85)
    hax.Draw()
    rcan.upper_pad.Update()

    # legend
    leg = pu.default_legend(xl=0.55, yl=0.71, xh=0.93, yh=0.90)

    # histos
    histos_tmp = {}
    display_names = {}
    samples_for_order = ["nominal", "sherpa", "mgp"]
    colors = {}
    for s in samples :
        h = pu.th1f("h_" + var.name + "_" + str(s.name), "", int(p.nbins), p.x_range_min, p.x_range_max, p.x_label, p.y_label)
        h.SetLineColor(s.color)
        h.SetFillStyle(0)
        h.SetLineWidth(2)
        h.GetXaxis().SetLabelOffset(-999)
        h.SetMaximum(10000)
        h.Sumw2

        display_names[s.name] = s.displayname
        colors[s.name] = s.color

        lumi_scale = 8.42 # not neeed for normalized distributions
        weight_str = "eventweight * %s"%lumi_scale

        cut = "(" + region.tcut + ") * %s"%weight_str
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>+%s"%(var.name, h.GetName())
        s.tree.Draw(cmd, cut * sel, "goff") 

        #print yield stuffs
        stat_err = r.Double(0.0)
        integral = h.IntegralAndError(0,-1,stat_err)
        print "%s : %.2f +/- %.2f"%(s.name, integral, stat_err) 


        # add overflow
        pu.add_overflow_to_lastbin(h)

        # norm to unity
        h.Scale(1/h.Integral())

        histos_tmp[s.name] = h
        rcan.upper_pad.Update()

    r.gPad.SetGrid()

    histos = {}
    for sname in samples_for_order :
        for sn, hist in histos_tmp.iteritems() :
            if sn == sname :
                histos[sname] = hist 
                leg.AddEntry(hist, display_names[sname], "l")

    histos["nominal"].Draw("hist")
    histos["sherpa"].Draw("hist same")
    histos["mgp"].Draw("hist same")

    # draw the legend
    leg.Draw()


    ############### lower pad
    rcan.lower_pad.cd()

    # nom
    h_nom = histos["nominal"].Clone("nominal")
    # sherpa
    h_sherpa = histos["sherpa"].Clone("sherpa")
    # mgpythia
    h_mgp = histos["mgp"].Clone("mgp")

    # y-axis
    yax = h_sherpa.GetYaxis()
    yax.SetTitle("Other/Nominal")
    yax.SetTitleSize(0.1 * 0.83)
    yax.SetLabelSize(0.1 * 0.81)
    yax.SetLabelOffset(0.98 * 0.013 * 1.08)
    yax.SetTitleOffset(0.45 * 1.2)
    yax.SetLabelFont(42)
    yax.SetTitleFont(42)
    yax.SetNdivisions(5)

    # x-axis
    xax = h_sherpa.GetXaxis()
    xax.SetTitleSize(1.0 * 0.14)
    xax.SetLabelSize(yax.GetLabelSize())
    xax.SetLabelOffset(1.15 * 0.02)
    xax.SetTitleOffset(0.85 * xax.GetTitleOffset())
    xax.SetLabelFont(42)
    xax.SetTitleFont(42)

    h_nom.GetYaxis().SetRangeUser(0,5)
    h_sherpa.GetYaxis().SetRangeUser(0,5)
    h_mgp.GetYaxis().SetRangeUser(0,5)
    h_nom.SetNdivisions(5)
    h_sherpa.SetNdivisions(5)
    h_mgp.SetNdivisions(5)

    h_sherpa.Divide(h_nom)
    h_mgp.Divide(h_nom)

    maxy_ = 5
    if "bj_pt[0]" in var.name or "bj_pt[1]" in var.name:
        maxy_ = 2
    elif "bj_pt[2]" in var.name :
        maxy_ = 3
    elif "nBJets" in var.name :
        maxy_ = 2
    elif "nSJets" in var.name :
        maxy_ = 3
    elif "eta" in var.name :
        maxy_ = 2
    elif "dphi_ll" in var.name or "pTll" in var.name :
        maxy_ = 2
    elif "l_pt" in var.name :
        maxy_ = 2
    elif "meff" in var.name or "met" in var.name :
        maxy_ = 2
    elif "sj_pt" in var.name :
        maxy_ = 2

    h_sherpa.GetYaxis().SetRangeUser(0,maxy_)
    h_mgp.GetYaxis().SetRangeUser(0,maxy_)

    h_sherpa.SetMarkerStyle(20)
    h_sherpa.SetMarkerColor(colors["sherpa"])
    h_mgp.SetMarkerStyle(20)
    h_mgp.SetMarkerColor(colors["mgp"])

    h_sherpa.Draw("pe")
    h_mgp.Draw("pe same")

    rcan.lower_pad.Update()

    # lines
    pu.draw_line(p.x_range_min, 1.0, p.x_range_max, 1.0, color=r.kRed, style=2, width=1)


    # save
    outname = p.name + ".eps"
    rcan.canvas.SaveAs(outname)
    out = "./ttbar_comp_plots/"
    utils.mv_file_to_dir(outname, out, True)
    fullname = out + "/" + outname
    print "%s saved to : %s"%(outname, os.path.abspath(fullname))
def make_MDR_nm1_plot(reg, backgrounds, data) :

    print "make_MDR_nm1_plot    %s"%reg.name

    #mdr_nice = "E_{V}^{P} [GeV]"
    mdr_nice = "M_{#Delta}^{R} [GeV]"

    p = plot.Plot1D()
    p.initialize(reg.name, "MDR", "%s_MDR"%(reg.name))
    p.labels(x=mdr_nice, y = "Entries")
    if reg.name == "srwNM1" :
        p.yax(0.1, 10000)
    elif reg.name == "srwNM1SF" :
        p.yax(0.1, 20000)
    elif reg.name == "srtNM1" :
        p.yax(0.1, 50000)
    elif reg.name == "srtNM1SF" :
        p.yax(0.1, 50000)
    p.doLogY = True
    p.setRatioCanvas(p.name)

    ### canvas get
    rcan = p.ratioCanvas
    rcan.canvas.cd()
    rcan.upper_pad.cd()

    if p.isLog() : rcan.upper_pad.SetLogy(True)
    rcan.upper_pad.Update()

    binlow = []
    if reg.name == "srwNM1":
        binlow = [80,95,110,130,150,180]#100,140]#,130]#,130]#75,90,110,130,160]
    elif reg.name == "srwNM1SF" :
        binlow = [80,95,110,130,150,180]
        #binlow = [80,95,110,130,150,180,200]
    elif reg.name == "srtNM1" :
        binlow = [70,80,95,110,130,150,180]#100,140]#,130]#,130]#75,90,110,130,160]
    elif reg.name == "srtNM1SF" :
        binlow = [70,80,95,110,130,150,180]
        #binlow = [70,80,95,110,130,150,180,200]
        #binlow = [70,80,90,100,110,120,130,140,150,160,170,180,200]

    hax = r.TH1F("axes", "", len(binlow)-1, array('d',binlow))
    hax.SetMinimum(p.y_range_min)
    hax.SetMaximum(p.y_range_max)
    hax.GetXaxis().SetTitle(p.x_label)
    hax.GetXaxis().SetTitleFont(42)
    hax.GetXaxis().SetLabelFont(42)
    hax.GetXaxis().SetLabelSize(0.035)
    hax.GetXaxis().SetTitleSize(0.048 * 0.85)
    hax.GetXaxis().SetTitleOffset(-999)
    hax.GetXaxis().SetLabelOffset(-999)

    hax.GetYaxis().SetTitle(p.y_label)
    hax.GetYaxis().SetTitleFont(42)
    hax.GetYaxis().SetLabelFont(42)
    hax.GetYaxis().SetTitleOffset(1.4)
    hax.GetYaxis().SetLabelOffset(0.013)
    hax.GetYaxis().SetLabelSize(1.2 * 0.035)
    hax.GetYaxis().SetTitleSize(0.055 * 0.85)
    hax.Draw()
    rcan.upper_pad.Update()

    stack = r.THStack("stack_"+p.name,"")

    # legend
    leg = pu.default_legend(xl=0.55,yl=0.71,xh=0.93,yh=0.90)
    #leg = pu.default_legend(xl=0.55,yl=0.65,xh=0.93,yh=0.90)
    leg.SetNColumns(2)

    # histos
    histos = []
    h_nom_fake = None

    n_total_sm_yield = 0.
    for b in backgrounds :
        if b.isSignal() : continue
        if "data" in b.name.lower() : continue
        hist_name = "MDR"
        h = r.TH1F("h_" + b.treename+"_"+hist_name, "", len(binlow)-1, array('d',binlow))
        font = 42
        h.SetTitleFont(font)

        #x-axis
        xx = h.GetXaxis()
        xx.SetTitle(p.x_label)
        xx.SetTitleOffset(1.2*xx.GetTitleOffset())
        xx.SetTitleFont(font)
        xx.SetLabelFont(font)

        #y-axis
        yy = h.GetYaxis()
        yy.SetTitle(p.y_label)
        yy.SetTitleOffset(1.2*yy.GetTitleOffset())
        yy.SetTitleFont(font)
        yy.SetLabelFont(font)

        h.Sumw2()

        h.SetLineColor(b.color) # stop2L people have no sense
        #h.SetLineColor(r.kBlack)
        h.GetXaxis().SetLabelOffset(-999)
        h.SetFillColor(b.color)
        h.SetFillStyle(1001)

        # cuts
        weight_str = ""
        if "fakes" in b.name :
            weight_str = "FakeWeight"
        else :
            weight_str = "eventweight"

        norm_factor = "1"
        if "ttbar" in b.name :
            norm_factor = "0.99"
        elif "vv" in b.name  and "SF" in reg.name :
            norm_factor = "1.23"
        elif "vv" in b.name and "SF" not in reg.name :
            norm_factor = "1.27"
        cut = "(" + reg.tcut + ") * %s * %s * %s"%(weight_str, str(b.scale_factor), norm_factor)
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>+%s"%(p.variable, h.GetName())
        blah = b.tree.Draw(cmd, cut * sel, "goff")

        # print yields
        stat_err = r.Double(0.0)
        integral = h.IntegralAndError(0,-1,stat_err)
        n_total_sm_yield += float(integral)
        print "%s: %.2f +/- %.2f"%(b.name, integral, stat_err)

        # get variation histos
        if doSys and 'fakes' not in b.name and (integral>0)  : getSystHists(p, reg, b, integral, h)
        #if doSys and 'fakes' not in b.name and (integral>0) and "zjets" not in b.name : getSystHists(p, reg, b, integral, h)

        # add overflow
        pu.add_overflow_to_lastbin(h)

        # set negative bin yields to 0
        for ibin in xrange(h.GetXaxis().GetNbins()) :
            if h.GetBinContent(ibin+1) < 0. :
                print "Setting bin %d for %s to 0"%(ibin, b.name)
                h.SetBinContent(ibin+1, 0)
                h.SetBinError(ibin+1,0)

        if "fakes" in b.name :
            h_nom_fake = h.Clone("fakes_nominal_histo")

        leg.AddEntry(h, b.displayname, "f")
        if integral > 0 :
            histos.append(h)
        rcan.upper_pad.Update()


    # order the histos
    histos = sorted(histos, key=lambda h: h.Integral(), reverse=False)
    for h in histos :
        stack.Add(h)
    rcan.upper_pad.Update()
    #hist_sm = stack.GetStack().Last().Clone("hist_sm")
        

    # now get the data points
    hd = r.TH1F("h_data_"+reg.name,"", len(binlow)-1, array('d',binlow))
    hd.Sumw2()
    cut = "(" + reg.tcut + ")"
    cut = r.TCut(cut)
    sel = r.TCut("1")
    cmd = "%s>>+%s"%(p.variable, hd.GetName()) 
    data.tree.Draw(cmd, cut * sel, "goff")
    hd.GetXaxis().SetLabelOffset(-999)

    print "Total SM: %.2f"%(n_total_sm_yield)

    # print yeild +/- stat err
    stat_err = r.Double(0.0)
    integral = hd.IntegralAndError(0,-1,stat_err)
    print "Data: %.2f +/- %.2f"%(integral, stat_err)
    pu.add_overflow_to_lastbin(hd)

    gdata = pu.convert_errors_to_poisson(hd)
    gdata.SetLineWidth(2)
    gdata.SetMarkerStyle(20)
    gdata.SetMarkerSize(1.5)
    #gdata.SetMarkerSize(1.1)
    gdata.SetLineColor(1)
    leg.AddEntry(gdata, "Data", "p")
    rcan.upper_pad.Update()
    
    #############################
    # systematics loop
    r.gStyle.SetHatchesSpacing(0.9)

    # dummy histo for legend
    mcError = r.TH1F("mcError", "mcError", 2,0,2)
    mcError.SetFillStyle(3354)
    mcError.SetFillColor(r.kBlack)
    mcError.SetLineColor(r.TColor.GetColor("#FC0F1D"))
    mcError.SetLineWidth(3)
    leg.AddEntry(mcError, "Total SM", "fl")

    # histogram for total stack
    totalSM = stack.GetStack().Last().Clone("totalSM")
    nominalAsymErrors = pu.th1_to_tgraph(totalSM)
    nominalAsymErrors.SetMarkerSize(0)
    nominalAsymErrors.SetLineWidth(0)
    nominalAsymErrors.SetFillStyle(3354)
    nominalAsymErrors.SetFillColor(r.kGray + 3)
   # leg.AddEntry(nominalAsymErrors, "Bkg. Uncert.", "f")


    tmp = []
    backgrounds_original = backgrounds
    for b in backgrounds :
        if "bwn" in b.name : continue
        if "fakes" in b.name : continue
        #if "zjets" in b.name : continue
        tmp.append(b)
    backgrounds = tmp
    if doSys :
        # totalSystHisto will hold each samples'
        # variation
        totalSysHisto = totalSM.Clone()
        totalSysHisto.Reset("ICE")
        transient = r.TGraphAsymmErrors()

        # add to the error band the contribution from the up-variations 
        systematics_up = [s.up_name for s in backgrounds[0].systList]
        for up_sys in systematics_up :

            new_name = ""
            if "syst_" in up_sys :
                new_name = "syst_" + up_sys.replace("syst_","")
            else :
                new_name = up_sys
            up_sys = new_name

            transient = r.TGraphAsymmErrors()
            transient.Clear()
            for b in backgrounds :
                #if b.name in avoid_bkg : continue
                #if "zjets" in b.name : continue
                if "bwn" in b.name : continue
                if b.isSignal() : continue
                if 'fakes' in b.name : continue
                for syst in b.systList :
                    if "EG_SCALE_ALL" in syst.up_name  and "zjets" in b.name : continue

                    new_name = ""
                    if "syst_" in syst.up_name :
                        new_name = "syst_" + syst.up_name.replace("syst_","")
                    else :
                        new_name = syst.up_name
                    syst.up_name = new_name

                    if syst.up_name != up_sys : continue
                    #print "[%s] adding %s to up histo : %.2f (%.2f)"%(up_sys, b.name, syst.up_histo.Integral(), totalSM.Integral())
                    totalSysHisto.Add(syst.up_histo)
            if h_nom_fake :
                totalSysHisto.Add(h_nom_fake)
            transient = pu.th1_to_tgraph(totalSysHisto)
            #print " > %s: %s"%(b.name, up_sys)
            pu.add_to_band(transient, nominalAsymErrors)#, up_sys)
            totalSysHisto.Reset()

        # add to the error band the contribution from the down-variations
        systematics_down = [s.down_name for s in backgrounds[0].systList]
        for dn_sys in systematics_down :

            new_name = ""
            if "syst_" in dn_sys :
                new_name = "syst_" + dn_sys.replace("syst_","")
            else :
                new_name = dn_sys
            dn_sys = new_name

            transient = r.TGraphAsymmErrors()
            transient.Clear()
            for b in backgrounds :
                #if "zjets" in b.name : continue
                if "bwn" in b.name : continue
                #if b.name in avoid_bkg : continue
                if b.isSignal() : continue
                if 'fakes' in b.name : continue
                for syst in b.systList :
                    if "EG_SCALE_ALL" in syst.down_name  and "zjets" in b.name : continue

                    new_name = ""
                    if "syst_" in syst.down_name :
                        new_name = "syst_" + syst.down_name.replace("syst_","")
                    else :
                        new_name = syst.down_name
                    syst.down_name = new_name

                    if syst.down_name != dn_sys : continue
                    #print "[%s] adding %s to down histo : %.2f"%(dn_sys, b.name, syst.down_histo.Integral())
                   # if syst.isOneSided() : continue

                   # if "JER" in syst.name : continue
                   # if "ResoPerp" in syst.name or "ResoPara" in syst.name : continue
                    totalSysHisto.Add(syst.down_histo)
            if h_nom_fake :
                totalSysHisto.Add(h_nom_fake)
            transient = pu.th1_to_tgraph(totalSysHisto)
            #print " > %s: %s"%(b.name, dn_sys)
            pu.add_to_band(transient, nominalAsymErrors)#, dn_sys)
            totalSysHisto.Reset()

    # draw mc stack
    stack.Draw("HIST SAME")
    stack.SetMinimum(p.y_range_min)
    stack.SetMaximum(p.y_range_max)
    rcan.upper_pad.Update()

    # draw the error band
    nominalAsymErrors.Draw("same && E2")
    
    # draw the total bkg line
    hist_sm = stack.GetStack().Last().Clone("hist_sm")
    hist_sm.SetLineColor(r.TColor.GetColor("#FC0F1D"))
    hist_sm.SetLineWidth(mcError.GetLineWidth())
    hist_sm.SetLineStyle(1)
    hist_sm.SetFillStyle(0)
    hist_sm.Draw("hist same")


    ####################################
    # plot signal
    leg_sig = pu.default_legend(xl=0.55,yl=0.56,xh=0.91,yh=0.71)
    #leg_sig = pu.default_legend(xl=0.55,yl=0.35,xh=0.93,yh=0.60)
    leg_sig.SetNColumns(1)
    sig_histos = []
    for s in backgrounds_original :
        if not s.isSignal() : continue
        hist_name = "MDR"
        h = r.TH1F("h_" + b.treename+"_"+hist_name, "", len(binlow)-1, array('d',binlow))
        h.SetLineWidth(2)
        h.SetLineStyle(2)
        h.SetLineColor(s.color)
        h.GetXaxis().SetLabelOffset(-999)
        h.SetFillStyle(0)
        h.Sumw2()

        # cut and make the sample weighted, applying the scale_factor
        cut = "(" + reg.tcut + ") * eventweightNOPUPW * susy3BodyRightPol *" + str(s.scale_factor)
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>+%s"%(p.variable, h.GetName())
        s.tree.Draw(cmd, cut * sel, "goff")

        # print the yield +/- stat error
        stat_err = r.Double(0.0)
        integral = h.IntegralAndError(0,-1,stat_err)
        print "%s: %.2f +/- %.2f"%(s.name, integral, stat_err)
        
        # add overflow
        pu.add_overflow_to_lastbin(h)

        #h_dummy = r.TH1F("dummy","",2,0,2)
        #h_dummy.SetFillStyle(1001)
        #h_dummy.SetFillColor(r.kWhite)
        #h_dummy.SetLineColor(r.kWhite)

        leg_sig.AddEntry(h, s.displayname, "l")
        #leg_sig.AddEntry(h_dummy, "", "fl")
        sig_histos.append(h)
        rcan.upper_pad.Update()

    # draw the signals
    for hsig in sig_histos :
        hsig.Draw("hist same")

    # draw the dta graph
    gdata.Draw("option same pz 0")
    leg.Draw()
    leg_sig.Draw()
    r.gPad.RedrawAxis()

    # add some text/labels
    #pu.draw_text(text="#it{ATLAS} Preliminary",x=0.18,y=0.85, size=0.06)
    pu.draw_text(text="ATLAS",x=0.18,y=0.85,size=0.06,font=72)
    pu.draw_text(text="Preliminary",x=0.325,y=0.85,size=0.06,font=42)
    pu.draw_text(text="L = 13.3 fb^{-1}, #sqrt{s} = 13 TeV",x=0.18,y=0.79, size=0.04)
    pu.draw_text(text=reg.displayname,      x=0.18,y=0.74, size=0.04)

    r.gPad.SetTickx()
    r.gPad.SetTicky()

    rcan.canvas.Update()

    ########################################
    # lower pad
    ########################################
    rcan.lower_pad.cd()

    # get the total SM histo
    h_sm = stack.GetStack().Last().Clone("h_sm")

    # yaxis
    yax = h_sm.GetYaxis()
    yax.SetRangeUser(0,2)
    yax.SetTitle("Data/SM")
    
    yax.SetTitleSize(0.14 * 0.83)
    yax.SetLabelSize(0.13 * 0.81)
    yax.SetLabelOffset(0.98 * 0.013 * 1.08)
    yax.SetTitleOffset(0.45 * 1.2)
    yax.SetLabelFont(42)
    yax.SetTitleFont(42)
    yax.SetNdivisions(5) 

#    print "title size: ", yax.GetTitleSize()
#    print "label size : ", yax.GetLabelSize()
#    print "lab off      : ", yax.GetLabelOffset()
#    print "title off        : ", yax.GetTitleOffset()
    
    # xaxis
    xax = h_sm.GetXaxis()
    xax.SetTitleSize(1.1 * 0.14)
    xax.SetLabelSize(yax.GetLabelSize())
    #xax.SetLabelSize(0.13)
    xax.SetLabelOffset(1.15*0.02)
    xax.SetTitleOffset(0.85 * xax.GetTitleOffset())
    xax.SetLabelFont(42)
    xax.SetTitleFont(42)
   
    h_sm.SetTickLength(0.06)
    h_sm.Draw("AXIS")
    
    rcan.lower_pad.Update()

    # get the ratio-error band
    ratioBand = r.TGraphAsymmErrors(nominalAsymErrors)
    pu.buildRatioErrorBand(nominalAsymErrors, ratioBand)

    # draw lines
    pu.draw_line(binlow[0], 1.0, binlow[-1], 1.0,color=r.kRed,style=2,width=1)
    pu.draw_line(binlow[0], 0.5, binlow[-1], 0.5,style=3,width=1)
    pu.draw_line(binlow[0], 1.5, binlow[-1], 1.5,style=3,width=1)

    # convert to tgraphs to get the ratio
    #g_data = pu.th1_to_tgraph(hd)
    g_data = pu.convert_errors_to_poisson(hd)
    #g_data = gdata
    g_sm = pu.th1_to_tgraph(h_sm)
    g_ratio = pu.tgraphAsymmErrors_divide(g_data, g_sm)

    # For Data/MC only use the statistical error for data
    # since we explicity draw the MC error band
    nominalAsymErrorsNoSys = r.TGraphAsymmErrors(nominalAsymErrors)
    for i in xrange(nominalAsymErrorsNoSys.GetN()) :
        nominalAsymErrorsNoSys.SetPointError(i,0,0,0,0)
        #nominalAsymErrorsNoSys.SetPointError(i-1,0,0,0,0)
    ratio_raw = pu.tgraphAsymmErrors_divide(g_data, nominalAsymErrorsNoSys)
    ratio = r.TGraphAsymmErrors() 

    x1, y1 = r.Double(0.0), r.Double(0.0)
    index = 0
    for i in xrange(ratio_raw.GetN()) :
        ratio_raw.GetPoint(i, x1, y1)
       # print "raw: (i, x, y) = (%d, %f, %f)"%(int(i),float(x1),float(y1))
       # xx, yy = r.Double(0.0), r.Double(0.0)
       # xn, yn = r.Double(0.0), r.Double(0.0)
       # xg, yg = r.Double(0.0), r.Double(0.0)
       # g_data.GetPoint(i, xx, yy)
       # nominalAsymErrorsNoSys.GetPoint(i, xn, yn)
       # g_sm.GetPoint(i, xg, yg)
       # print "gdata: (i, x, y) = (%d, %f, %f)"%(int(i),float(xx),float(yy))
       # print "nomAs: (i, x, y) = (%d, %f, %f)"%(int(i),float(xn),float(yn))
       # print "g_sm : (i, x, y) = (%d, %f, %f)"%(int(i),float(xg),float(yg))
        x_, y_ = r.Double(0.0), r.Double(0.0)
        xx, yy = r.Double(0.0), r.Double(0.0)
        xr, yr = r.Double(0.0), r.Double(0.0)
        g_data.GetPoint(i, x_, y_)
        gdata.GetPoint(i, xx, yy)
        g_ratio.GetPoint(i, xr, yr)
        print "gdata   (%f,%f) EYH : %f    EYL: %f"%(xx, yy, gdata.GetErrorYhigh(i), gdata.GetErrorYlow(i))
        print "g_data  (%f,%f) EYH : %f    EYL: %f"%(x_, y_, g_data.GetErrorYhigh(i), g_data.GetErrorYlow(i)) 
        print "g_ratio (%f,%f) EYH: %f    EYL: %f"%(xr, yr, g_ratio.GetErrorYhigh(i), g_ratio.GetErrorYlow(i))
        print 50*"-"
        if y1 > 0. :
            ratio.SetPoint(index, x1, y1)
            ratio.SetPointError(index, ratio_raw.GetErrorXlow(i), ratio_raw.GetErrorXhigh(i), ratio_raw.GetErrorYlow(i), ratio_raw.GetErrorYhigh(i))
            index+=1
    #print "SETTING RATIO_RAW TO RATIO"
    #ratio = ratio_raw
    ratio.SetLineWidth(2)
    ratio.SetMarkerStyle(20)
    ratio.SetMarkerSize(1.5)
    #ratio.SetMarkerSize(1.1)
    ratio.SetLineColor(1)
    ratio.SetMarkerSize(1.5)
    ratio.Draw("option pz 0")
    rcan.lower_pad.Update()

    ratioBand.Draw("same && E2")
    rcan.lower_pad.Update()
    



    #########################################
    # save
    #########################################
    outname = p.name + "_prelim.eps"
    rcan.canvas.SaveAs(outname)
    out = indir + "/plots/" + outdir
    utils.mv_file_to_dir(outname, out, True)
    fullname = out + "/" + outname
    print "%s saved to : %s"%(outname, os.path.abspath(fullname))
Exemple #5
0
def make_lumi_plot(samples, region):
    print "make_lumi_plot()    Making lumi plot for %s" % region.name

    lumican = lumiCanvas("lumican_%s" % region.name)
    lumican.canvas.cd()

    # only plot those runs with >= 10/pb of data
    new_samples = []
    for s in samples:
        if s.lumi >= 10:
            new_samples.append(s)
    samples = new_samples

    # only 2015
    # new_samples = []
    # for s in samples :
    #    if int(s.run_number) < 297730 : new_samples.append(s)
    # samples = new_samples

    lumican.pad.cd()
    h = pu.th1f("h_lumi_" + region.name, "", int(len(samples)), 0, int(len(samples)), "Run Number", "Events/pb^{-1}")

    # h.SetBinErrorOption(r.TH1.kPoisson)
    # h.Sumw2()

    samples = sorted(samples, key=lambda x: x.run_number, reverse=False)

    maxy = -1

    sum_ev = 0.0

    lumi_list = []

    for ibin, s_ in enumerate(samples):
        yld = s_.yields[region.name]
        lumi = s_.lumi
        lumi_list.append(lumi)
        norm = float(yld) / float(lumi)
        sum_ev += norm
        if norm > maxy:
            maxy = norm
        h.SetBinContent(ibin + 1, norm)
        h.SetBinError(ibin + 1, sqrt(yld) / float(lumi))
        h.GetXaxis().SetBinLabel(ibin + 1, "%s" % str(s_.run_number))

    avg_norm = float(sum_ev) / len(samples)

    maxima = {}
    maxima["srwPreselEE"] = 10.0
    maxima["srwPreselMM"] = 10.0
    maxima["srwPreselDF"] = 1.8
    maxima["srtPreselEE"] = 5.0
    maxima["srtPreselMM"] = 5.0
    maxima["srtPreselDF"] = 8.0

    # h.SetMaximum(maxima[region.name])
    h.SetMaximum(2.15 * avg_norm)
    h.SetMarkerColor(r.TColor.GetColor("#386672"))
    h.SetMarkerSize(1.5 * h.GetMarkerSize())

    h.SetMinimum(0)

    h.SetFillColor(0)
    h.SetLineColor(r.kBlack)

    ## labels
    h.GetXaxis().LabelsOption("v")
    h.GetXaxis().SetLabelFont(42)
    h.GetXaxis().SetLabelOffset(3.2 * h.GetXaxis().GetLabelOffset())
    h.GetXaxis().SetLabelSize(1.2 * h.GetXaxis().GetLabelSize())

    ## title
    # x
    h.GetXaxis().SetTitleSize(1.75 * h.GetXaxis().GetTitleSize())
    h.GetXaxis().SetTitleOffset(1.5 * h.GetXaxis().GetTitleOffset())

    # y
    h.GetYaxis().SetTitleOffset(0.5 * h.GetYaxis().GetTitleOffset())
    h.GetYaxis().SetTitleSize(1.25 * h.GetYaxis().GetTitleSize())

    # draw average norm lumi line
    pu.draw_line(0, avg_norm, len(samples), avg_norm, color=r.kRed, style=2, width=1)

    h.Draw("hist e")

    h.Fit("pol1")
    lumican.canvas.Update()

    h.Draw()
    pu.draw_line(0, avg_norm, len(samples), avg_norm, color=r.kRed, style=2, width=1)

    ## get periods and lines
    #  period_lines = get_periodlines(samples, h.GetMaximum(), region.name)
    #  for line in period_lines :
    #      line.Draw()

    pu.draw_text_on_top(region.displayname + "  2015 + 2016 (DS1)")

    lumican.canvas.Update()
    lumican.canvas.SaveAs("normlumi_%s.eps" % region.name)
Exemple #6
0
    # x-axis
    axx = h_ratio.GetXaxis()
    axx.SetTitle(plot.x_label)
    axx.SetTitleSize(1.2 * 0.14)
    axx.SetLabelSize(0.13)
#    axx.SetLabelOffset(1.15 * 0.02)
    axx.SetTitleOffset(0.85 * axx.GetTitleOffset())
    axx.SetLabelFont(42)
    axx.SetTitleFont(42)

    h_ratio.SetTickLength(0.06)
    h_ratio.Draw("AXIS")
    rcan.lower_pad.Update()

    # draw lines
    pu.draw_line(plot.x_range_min, 1.0, plot.x_range_max, 1.0, color=r.kRed, style=2, width=1)
    pu.draw_line(plot.x_range_min, 0.5, plot.x_range_max, 0.5, style=3, width=1)
    pu.draw_line(plot.x_range_min, 1.5, plot.x_range_max, 1.5, style=3, width=1)
    
    h_WWr = h_WW.Clone("ww_rat")
    h_WZr = h_WZ.Clone("wz_rat")

    h_WZr.Divide(h_WWr)
    h_WZr.SetMarkerStyle(20)
    h_WZr.SetLineColor(r.kBlack)
    h_WZr.Draw("same")
    rcan.lower_pad.Update()

    outname = plot.name + ".eps"
    rcan.canvas.SaveAs(outname)
    out = indir + "/plots/" + outdir