Ejemplo n.º 1
0
def make_plotsRatio(plot, region, backgrounds) :

    print "make_plotsRatio    Plotting %s"%plot.name

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

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

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

    yax = hax.GetYaxis()
    yax.SetTitle(plot.y_label)
    yax.SetTitleFont(42)
    yax.SetLabelFont(42)
    yax.SetTitleOffset(1.4)
    yax.SetLabelOffset(0.013)
    yax.SetLabelSize(1.2 * 0.035)
    yax.SetTitleSize(0.055 * 0.85)

    hax.Draw()
    rcan.upper_pad.Update()

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

    histos = [] 
    n_drawn = 0
    for b in backgrounds :
        hist_name = ""
        if "abs" in plot.variable :
            replace_var = plot.variable.replace("abs(", "")
            replace_var = replace_var.replace(")", "")
            hist_name = replace_var
        else : hist_name = plot.variable
        h = None
        h = pu.th1f("h_"+b.treename+"_"+b.name+"_"+hist_name, "", int(plot.nbins), plot.x_range_min, plot.x_range_max, plot.x_label, plot.y_label)
        h.SetMinimum(0.1)
        h.SetLineColor(b.color)
        h.GetXaxis().SetLabelOffset(-999)
        h.SetLineWidth(2)
        h.SetFillStyle(0)
        h.Sumw2()

        idx = ""
        if "WW" in b.name or "ww" in b.name :
            idx = "WW"
        elif "WZ" in b.name or "wz" in b.name :
            idx = "WZ"

        sumw = get_sumw(idx)
        xsec = get_xsec(idx)
        lumi = 3300.0 # 3.3/fb

        weight_str = "eventweight * %s * %s / %s"%(str(xsec), str(lumi), str(sumw))
        print "weight = %s"%weight_str

        cut = "(" + reg.tcut + ") * %s "%weight_str
        #cut = "(" + reg.tcut + ") * eventweight * " + str(b.scale_factor)
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>%s"%(plot.variable, h.GetName())
        b.tree.Draw(cmd, cut * sel, "goff")

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

        # add overflow
        pu.add_overflow_to_lastbin(h)
        leg.AddEntry(h, b.displayname, "l")
        histos.append(h)
        #rcan.upper_pad.Update()
        #rcan.canvas.Update()

    h_WW = None
    h_WZ = None
    for h in histos :
        if "WW" in h.GetName() or "ww" in h.GetName() :
            h_WW = h
            rcan.upper_pad.Update()
        elif "WZ" in h.GetName() or "wz" in h.GetName() :
            h_WZ = h
            rcan.upper_pad.Update()
        else :
            print "Expecting a  histogram with name that has 'WZ/wz' or 'WW/ww' in it"
            sys.exit()
    h_WW.Draw()
    h_WZ.Draw("same")
Ejemplo n.º 2
0
def make_znRatioPlots(backgrounds, signals, region, plot) :
    print "make_znRatioPlots    Plottings %s"%plot.name

    # get canvases
    ratiocan = plot.getCanvas()
    ratiocan.canvas.cd()

    # go to the upper-pad
    ratiocan.upper_pad.cd()
    ratiocan.upper_pad.Draw()
    ratiocan.middle_pad.cd()
    ratiocan.middle_pad.Draw()
    ratiocan.lower_pad.cd()
    ratiocan.lower_pad.Draw()

    ratiocan.canvas.cd()
    ratiocan.upper_pad.cd()

    if plot.isLog() : ratiocan.upper_pad.SetLogy(True)
    ratiocan.upper_pad.Update()

    # stack for MC backgrounds

    ratiocan.upper_pad.cd()
    hax = r.TH1F("axes", "", int(plot.nbins), plot.x_range_min, plot.x_range_max)
    hax.SetMinimum(plot.y_range_min)
    hax.SetMaximum(plot.y_range_max)
    hax.GetXaxis().SetTitle(plot.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(plot.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()
    ratiocan.upper_pad.Update()
    
    stack = r.THStack("stack_"+plot.name, "")

    # legend
    leg = pu.default_legend(xl=0.5, yl=0.6, xh=0.93, yh=0.90)
    #leg = pu.default_legend(xl=0.65, yl=0.72, xh=0.93, yh=0.90)
    leg.SetNColumns(2)

    ### loop through the background MC and add to stack
    histos = []
    hist_dict = {} # associate the background with its histogram name

    hist_name = ""
    if "abs" in plot.variable :
        replace_var = plot.variable.replace("abs(","")
        replace_var = replace_var.replace(")","")
        hist_name = replace_var
    elif "MDR_v1_t1_0 - MDR_i1_t1_0" in plot.variable :
        hist_name = "RATIO"
    elif "RPT_0/RPZ_0" in plot.variable :
        hist_name = "RPTZratio"
    elif "H_11_SS/H_21_SS" in plot.variable :
        hist_name = "RH11SSH21SS"
    elif "H_11_SS/H_11_S1" in plot.variable :
        hist_name = "RH11SSH11S1"
    elif "xH_11_S1/xH_42_SS" in plot.variable :
        hist_name = "xH_11_S1_over_xH_42_SS"
    elif "xH_42_SS_T/xH_42_SS" in plot.variable :
        hist_name = "xH_42_SS_T_over_xH_42_SS"
    elif "xH_11_SS/xH_42_SS_T" in plot.variable :
        hist_name = "xH_11_SS_over_xH_42_SS_T"
    elif "(xNV[0]-xNV[1])/(xNV[0]+xNV[1])" in plot.variable :
        hist_name = "n_obs_asy"
    elif "xH_11_SS/xH_42_SS_T" in plot.variable :
        hist_name = "xH_11_SSoverxH_42_SS_T"
    elif "xH_42_SS_T/xH_11_SS" in plot.variable :
        hist_name = "xH_42_SS_ToverxH_11_SS"
    else : hist_name = plot.variable

    for b in backgrounds :
        h = pu.th1f("h_"+b.treename+"_"+hist_name, "", int(plot.nbins), plot.x_range_min, plot.x_range_max, plot.x_label, plot.y_label)
        h.SetLineColor(r.kBlack)
        h.SetFillColor(b.color)
        h.SetFillStyle(1001)
        h.Sumw2

        # cut and make the sample weighted, applying the scale_factor
        cut = "(" + reg.tcut + ") * eventweight * " + str(b.scale_factor)
        cut = r.TCut(cut)
        sel = r.TCut("1")
        cmd = "%s>>%s"%(plot.variable, h.GetName())
        b.tree.Draw(cmd, cut * sel, "goff")
        
        # print the yield +/- stat error
        stat_error = r.Double(0.0)
        integral = h.IntegralAndError(0, -1, stat_error)
        print "%s: %.2f +/- %.2f"%(b.name, integral, stat_error)

        # add overflow
        pu.add_overflow_to_lastbin(h)

        # add to group
        histos.append(h)
        hist_dict[h.GetName()] = b.displayname
     #   ratiocan.upper_pad.Update()

    # order the histograms and add to legend
    histos = sorted(histos, key = lambda h: h.Integral(), reverse = False)
    for his in histos :
        stack.Add(his)
    
    histos_leg = sorted(histos, key = lambda h: h.Integral(), reverse = True)
    for hl in histos_leg :
        leg.AddEntry(hl, hist_dict[hl.GetName()], "f")
        #leg.AddEntry(hl, hist_dict[hl.GetName()], "fl")
    ### total SM histo
    totalSM = stack.GetStack().Last().Clone("totalSM")

    ### container for zn pads
    sig_values = {}

    ### now plot the signal points

    sig_histos = []
    for s in signals :

        sig_values[s.name] = {} # { up : [], down : [] }

        hs = pu.th1f("h_"+s.treename+"_"+hist_name, "", int(plot.nbins), plot.x_range_min, plot.x_range_max, plot.x_label, plot.y_label)
        hs.SetLineColor(s.color)
        hs.SetLineStyle(2)
        hs.SetLineWidth(2) 
        hs.SetFillStyle(0)
        hs.Sumw2

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

        # print the yield +/- stat error
        stat_error = r.Double(0.0)
        integral = hs.IntegralAndError(0, -1, stat_error)
        print "%s: %.2f +/- %.2f"%(s.name, integral, stat_error)

        # add overflow
        pu.add_overflow_to_lastbin(hs)

        # add to legend
        leg.AddEntry(hs, s.displayname, "l")

        hs.SetMaximum(plot.y_range_max)
        hs.SetMinimum(plot.y_range_min)

        sig_histos.append(hs)
      #  ratiocan.upper_pad.Update()

        # get the zn-per-bin values
        # "up" : Zn values for lower-cuts (i.e. SR includes everything to the right of the cut)
        # "down" : Zn values for upper-cuts (i.e. SR includes everything to the left of the cut)
        if 'zn' in method :
            sig_values[s.name]['up'], sig_values[s.name]['down'] = get_zn_per_bin(hs, totalSM)

        # get the total zn for this selection
        # and attach the the signal point
        significance, nbkg, nsig, rel_bkgerr = 0, 0, 0, 0 
        if 'zn' in method :
            significance, nbkg, nsig, rel_bkgerr = get_zn_for_selection(hs, totalSM)
            s.sig_val = significance
            s.n_bkg = nbkg
            s.n_sig = nsig
            s.n_bkgerr = rel_bkgerr

    signame = ''
    if 'zn' in method : signame = "Zn"
    print " ++ --------------------------------------- ++ "
    print "     Significance (%s) for selection %s"%(signame, reg.displayname)
    print ""
    print "   # bkg : %.2f +/- %.2f             "%(nbkg, nbkg * rel_bkgerr)
    for sigpoint in signals :
        print "   # %s: %.2f                  "%(sigpoint.displayname, sigpoint.n_sig)
        print "       Z --> %.2f"%sigpoint.sig_val
    print " ++ --------------------------------------- ++ "

#    if method == "zn" : sys.exit()
    if method == "zn" or method == "fasimov" : return


    ### draw MC backgrounds
    ratiocan.upper_pad.Update()
    
    # now draw the stack without the axis
    stack.Draw("HIST same")

    ### draw signals
    for h_sig in sig_histos :
        h_sig.Draw("same")
    ratiocan.canvas.Update()

    leg.Draw()
    ratiocan.canvas.Update()
    r.gPad.RedrawAxis()

    #pu.draw_text_on_top(text=reg.tcut, size = 0.02)
    pu.draw_text(text="#it{ATLAS} Internal",x=0.18,y=0.83, size = 0.06)
    pu.draw_text(text="13 TeV, 10/fb", x=0.18,y=0.73, size = 0.06)
    #pu.draw_text(text="13 TeV, 10 fb^{-1}", x=0.18,y=0.8)

    #########################
    ## now draw zn

    ### up
    sig_up_histos = []
    sig_down_histos = []
    for sig in signals :
        updown = ['up', 'down']
        for dir in updown :
            zns = sig_values[sig.name][dir]
            hz = pu.th1f("h_sig_"+dir+"_" + sig.name, "", int(plot.nbins), plot.x_range_min, plot.x_range_max, "","")
            hz.SetMarkerStyle(20)
            hz.SetMarkerColor(sig.color)
            hz.SetMarkerSize(0.3 * hz.GetMarkerSize())
            max_y = 2.5
            for ibin, zn in enumerate(zns) :
                if zn > 2.5 :
                    max_y = 4
                elif zn > 4 :
                    max_y = 5
            hz.SetMaximum(max_y)
            hz.SetMinimum(0.0)
            for ibin, zn in enumerate(zns) :
                hz.SetBinContent(ibin, r.Double(zn))
            if 'up' in dir : sig_up_histos.append(hz)
            elif 'down' in dir : sig_down_histos.append(hz)

    ratiocan.middle_pad.cd()
    is_first = True
    for hzn in sig_up_histos :
        if is_first : 
            hzn.Draw("p")
            set_ratio_style(hzn, "mid", "")
            is_first = False
        else :
            hzn.Draw("same p")
    ratiocan.middle_pad.Update()

    ratiocan.lower_pad.cd()
    is_first = True
    for hzn in sig_down_histos :
        if is_first : 
            hzn.Draw("p")
            is_first = False
            set_ratio_style(hzn, "low", plot.x_label)
        else :
            hzn.Draw("same p")
    ratiocan.lower_pad.Update()

    #### save
    outname = plot.name + ".eps"
    ratiocan.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))