Esempio n. 1
0
def get_histogram(samples, proc, cut, variable, applysf=False):

    # Check the requested process 
    available_processes = ["data", "top", "w", "dy", "qcd"]
    if proc not in available_processes:
        print "ERROR: unrecognized process", proc
        sys.exit(-1)

    # Check the cut to determine whether this is one lepton process or two lepton
    # This is because the ntuple used for one lepton and two lepton is different
    # Therefore one need to check this to retrieve the right mc ntuple
    isonelep = cut.find("One") != -1

    # Checking the flavor of the lepton this histogram retreival is interested in
    ismu = cut.find("Mu") != -1

    # Checking if it is a 2d histogram
    # The convention is that the histogram names in loop.py all the 2d histogram must have "_vs_" name
    is2d = variable.find("_vs_") != -1

    # Checking if it is a three lepton fake rate channel
    # The convention is that if it is a three lepton related it will have "3l" in the cut name
    is3l = cut.find("3l") != -1

    # Setting the proc path
    tqprocpath = "/"
    if proc == "data": tqprocpath = "/data/mm" if ismu else "/data/ee"
    if proc == "top": tqprocpath = "/top"
    if proc == "w": tqprocpath = "/Wonelep" if isonelep else "/W/HT"
    if proc == "dy": tqprocpath = "/Zonelep" if isonelep else "/Z"
    if proc == "qcd": tqprocpath = "/qcd/mu" if ismu else "/qcd/el"

    # Setting the nice name
    nicename = "/"
    if proc == "data": nicename = "Data"
    if proc == "top": nicename = "t#bar{t}"
    if proc == "w": nicename = "W"
    if proc == "dy": nicename = "DY"
    if proc == "qcd": nicename = "QCD"

    # Retrieve the histograms
    hist = samples.getHistogram(tqprocpath, "{}/{}".format(cut, variable)).Clone(nicename)

    # Apply ewk sf
    if applysf:
        sf = 1.0
        if samples == samples_cn: sf = (musf_cn if ismu else elsf_cn) if not is3l else (mu3lsf_cn if ismu else el3lsf_cn)
        if samples == samples_up: sf = (musf_up if ismu else elsf_up) if not is3l else (mu3lsf_up if ismu else el3lsf_up)
        if samples == samples_dn: sf = (musf_dn if ismu else elsf_dn) if not is3l else (mu3lsf_dn if ismu else el3lsf_dn)
        if proc == "top": hist.Scale(sf)
        if proc == "w"  : hist.Scale(sf)
        if proc == "dy" : hist.Scale(sf)

    # If 2D plot, flatten it
    if is2d:
        hist = p.flatten_th2(hist)

    return hist
Esempio n. 2
0
def plot_stack(histname,
               output_name,
               systs=None,
               options={},
               plotfunc=p.plot_hist):
    ismu = str(histname).find("Mu") != -1
    is2d = str(histname).find("_vs_") != -1
    # Options
    alloptions = {
        "ratio_range": [0.4, 1.6],
        #"nbins": 30,
        #"autobin": True,
        "legend_scalex": 1.0,
        "legend_scaley": 1.0,
        "legend_smart": True,
        "legend_alignment": "topleft",
        "output_name": "{}/{}_stack.pdf".format(output_plot_dir, output_name),
        "bkg_sort_method": "unsorted",
        "divide_by_bin_width":
        True if output_name.find("varbin") != -1 else False,
        "yaxis_log": True if output_name.find("varbin") != -1 else False,
        "yaxis_range": [1e3, 1e10] if output_name.find("varbin") != -1 else [],
    }
    alloptions.update(options)
    sigs = []
    bgs = [
        samples.getHistogram("/top", histname).Clone("Top") if not is2d else
        p.flatten_th2(samples.getHistogram("/top", histname).Clone("Top")),
        samples.getHistogram("/Zonelep", histname).Clone("DY") if not is2d else
        p.flatten_th2(samples.getHistogram("/Zonelep", histname).Clone("DY")),
        samples.getHistogram("/Wonelep", histname).Clone("W") if not is2d else
        p.flatten_th2(samples.getHistogram("/Wonelep", histname).Clone("W")),
        samples.getHistogram("/qcd/mu" if ismu else "/qcd/el",
                             histname).Clone("QCD")
        if not is2d else p.flatten_th2(
            samples.getHistogram("/qcd/mu" if ismu else "/qcd/el",
                                 histname).Clone("QCD")),
    ]
    dataname = "/data"
    if histname.find("El") != -1: dataname = "/data/ee"
    if histname.find("Mu") != -1: dataname = "/data/mm"
    data = samples.getHistogram(
        dataname, histname).Clone("Data") if not is2d else p.flatten_th2(
            samples.getHistogram(dataname, histname).Clone("Data"))
    if histname.find("HLT") != -1:
        totalbkg = p.get_total_hist(bgs)
        ratio = samples.getHistogram(
            dataname, histname).Clone("Data") if not is2d else p.flatten_th2(
                samples.getHistogram(dataname, histname).Clone("Data"))
        ratio.Rebin(ratio.GetNbinsX())
        totalbkg.Rebin(totalbkg.GetNbinsX())
        totalbkg.Divide(ratio)
        print histname
        totalbkg.Print("all")
    if (histname.find("OneMuTightEWKCR3NoNvtxRewgt/nvtx") != -1
            or histname.find("OneElTightEWKCR3NoNvtxRewgt/nvtx") != -1
        ) and options["nbins"] != 5:
        totalbkg = p.get_total_hist(bgs[:-1])
        nvtxweight = samples.getHistogram(
            dataname,
            histname).Clone(output_name) if not is2d else p.flatten_th2(
                samples.getHistogram(dataname, histname).Clone(output_name))
        scale = nvtxweight.Integral() / totalbkg.Integral()
        nvtxweight.Divide(totalbkg)
        nvtxweight.Scale(1. / scale)
        #nvtxweight.Print("all")
        #f = ROOT.TFile("{}.root".format(output_name),"recreate")
        #nvtxweight.Write()
        #f.Close()
    if options["nbins"] == 5 and histname.find("TightEWKCR/MT") != -1:
        totalbkg = p.get_total_hist(bgs[:-1])
        ratio = samples.getHistogram(
            dataname, histname).Clone("Data") if not is2d else p.flatten_th2(
                samples.getHistogram(dataname, histname).Clone("Data"))
        ratio.Rebin(4)
        totalbkg.Rebin(4)
        ratio.Divide(totalbkg)
        print "EWK NF", histname, ratio.GetBinContent(3), ratio.GetBinError(3)
    colors = [2005, 2003, 2001, 920]
    sf = musf if ismu else elsf
    if histname.find("MTOneLep") == -1:
        bgs[0].Scale(sf)
        bgs[1].Scale(sf)
        bgs[2].Scale(sf)
    if histname.find("HLT") != -1:
        bgs = bgs[:-1]
    plotfunc(sigs=sigs,
             bgs=bgs,
             data=data,
             colors=colors,
             syst=systs,
             options=alloptions)
Esempio n. 3
0
def fakerate2dcomp(histname,
                   output_name,
                   systs=None,
                   options={},
                   plotfunc=p.plot_hist):
    if str(histname).find("Loose") == -1: return
    if str(histname).find("One") == -1: return
    if str(histname).find("corr") == -1: return
    ismu = str(histname).find("Mu") != -1

    qcdsample = "/qcd/mu" if ismu else ((
        "/qcd/el/bcToE" if not doW else "/qcd/el/EM")
                                        if not docombinedqcdel else "/qcd/el")
    ttbarsample = testsample
    qcdsamplename = "QCD" if ismu else "QCD"
    ttbarsamplename = testsamplename

    qcdhistname = histname
    ttbarhistname = histname.replace("One", "Two")
    ttbarhistname = ttbarhistname.replace(
        "lep_", "mu_") if ismu else ttbarhistname.replace("lep_", "el_")

    #print qcdhistname, ttbarhistname

    qcdhistnum = p.flatten_th2(
        samples.getHistogram(qcdsample,
                             str(qcdhistname).replace(
                                 "Loose", "Tight")).Clone(qcdsamplename))
    qcdhistden = p.flatten_th2(
        samples.getHistogram(qcdsample, qcdhistname).Clone(qcdsamplename))
    ttbarhistnum = p.flatten_th2(
        samples.getHistogram(ttbarsample,
                             str(ttbarhistname).replace(
                                 "Loose", "Tight")).Clone(ttbarsamplename))
    ttbarhistden = p.flatten_th2(
        samples.getHistogram(ttbarsample,
                             ttbarhistname).Clone(ttbarsamplename))

    qcdhistnum.Divide(qcdhistden)
    ttbarhistnum.Divide(ttbarhistden)

    # Options
    alloptions = {
        "ratio_range": [0.0, 2.0 if ismu else 3.0],
        "yaxis_range": [0.0, 0.35 if ismu else (0.6 if not doW else 1.0)],
        #"nbins": 30,
        "draw_points": True,
        "autobin": True,
        "legend_scalex": 0.8,
        "legend_scaley": 0.8,
        "legend_smart": True,
        "legend_alignment": "topleft",
        "legend_datalabel": testsamplelegendname,
        "output_name": "{}/fr_closure_{}.pdf".format(output_plot_dir,
                                                     output_name)
    }
    #samples.getHistogram(sample, str(histname).replace("Loose", "Tight")).Clone(samplename).Print("all")
    #histnum = p.flatten_th2(samples.getHistogram(sample, str(histname).replace("Loose", "Tight")).Clone(samplename))
    #histden = p.flatten_th2(samples.getHistogram(sample, histname).Clone(samplename))
    alloptions.update(options)
    sigs = []
    bgs = [qcdhistnum]
    data = ttbarhistnum
    colors = [2]
    plotfunc(sigs=sigs,
             bgs=bgs,
             data=data,
             colors=colors,
             syst=systs,
             options=alloptions)
Esempio n. 4
0
def fakerate2d(histname,
               output_name,
               systs=None,
               options={},
               plotfunc=p.plot_hist):
    if str(histname).find("Loose") == -1: return
    isqcd = str(histname).find("One") != -1
    ismu = str(histname).find("Mu") != -1
    ispredict = str(histname).find("Predict") != -1

    sample = testsample
    if isqcd and ismu:
        sample = "/qcd/mu"
    elif isqcd and not ismu:
        if doW:
            sample = "/qcd/el/EM"
        else:
            sample = "/qcd/el/bcToE"
        #sample = "/qcd/el"
        if docombinedqcdel:
            sample = "/qcd/el"
    samplename = (
        ("t#bar{t} estimation" if not doW else "W estimation") if
        not docombinedqcdel else "W + t#bar{t} estimation") if ispredict else (
            "QCD Loose" if isqcd else
            ("ttbar Loose" if not doW else "W Loose"))
    color = 920 if isqcd else (2005 if not doW else 2001)
    # Options
    alloptions = {
        "ratio_range": [0.0, 2.0] if ispredict else [0.0, 0.3],
        #"nbins": 30,
        "autobin":
        True,
        "legend_scalex":
        0.8,
        "legend_scaley":
        0.8,
        "legend_smart":
        True,
        "legend_alignment":
        "topleft",
        "legend_datalabel":
        samplename.replace("estimation", "prediction")
        if ispredict else samplename.replace("Loose", "Tight"),
        "output_name":
        "{}/fr_{}.pdf".format(output_plot_dir, output_name),
        "hist_disable_xerrors":
        True if str(histname).find("varbin") != -1 else False,
    }
    #samples.getHistogram(sample, str(histname).replace("Loose", "Tight")).Clone(samplename).Print("all")
    histnum = p.flatten_th2(
        samples.getHistogram(sample,
                             str(histname).replace("Loose",
                                                   "Tight")).Clone(samplename))
    histden = p.flatten_th2(
        samples.getHistogram(sample, histname).Clone(samplename))
    alloptions.update(options)
    sigs = []
    bgs = [histden]
    data = histnum
    colors = [color]
    if docombinedqcdel:
        bgs = [
            p.flatten_th2(
                samples.getHistogram("/top",
                                     histname).Clone("t#bar{t} estimation")),
            p.flatten_th2(
                samples.getHistogram("/W/HT", histname).Clone("W estimation"))
        ]
        colors = [2005, 2001]
    plotfunc(sigs=sigs,
             bgs=bgs,
             data=data,
             colors=colors,
             syst=systs,
             options=alloptions)
Esempio n. 5
0
def plot_datadriven_fakeratecomp(histname,
                                 output_name,
                                 systs=None,
                                 options={},
                                 plotfunc=p.plot_hist):
    if str(histname).find("Loose") == -1: return
    ismu = str(histname).find("Mu") != -1
    is2d = str(histname).find("_vs_") != -1
    # Options
    alloptions = {
        "ratio_range": [0.0, 2.0],
        #"nbins": 30,
        "autobin":
        True,
        "legend_scalex":
        1.0,
        "legend_scaley":
        1.0,
        "legend_smart":
        True,
        "legend_alignment":
        "topleft",
        "legend_datalabel":
        "Data FR",
        "output_name":
        "{}/{}_fakeratecomp.pdf".format(output_plot_dir, output_name),
        "bkg_sort_method":
        "unsorted",
        "draw_points":
        True,
    }
    alloptions.update(options)
    sigs = []
    # Loose
    bgs = [
        samples.getHistogram("/top", histname).Clone("Top") if not is2d else
        p.flatten_th2(samples.getHistogram("/top", histname).Clone("Top")),
        samples.getHistogram("/Zonelep", histname).Clone("DY") if not is2d else
        p.flatten_th2(samples.getHistogram("/Zonelep", histname).Clone("DY")),
        samples.getHistogram("/Wonelep", histname).Clone("W") if not is2d else
        p.flatten_th2(samples.getHistogram("/Wonelep", histname).Clone("W")),
        #samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD") if not is2d else p.flatten_th2(samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD")),
    ]
    dataname = "/data"
    if histname.find("El") != -1: dataname = "/data/ee"
    if histname.find("Mu") != -1: dataname = "/data/mm"
    ddqcd = samples.getHistogram(
        dataname, histname).Clone("Data") if not is2d else p.flatten_th2(
            samples.getHistogram(dataname, histname).Clone("Data"))
    qcd = samples.getHistogram(
        "/qcd/mu" if ismu else "/qcd/el",
        histname).Clone("QCD") if not is2d else p.flatten_th2(
            samples.getHistogram("/qcd/mu" if ismu else "/qcd/el",
                                 histname).Clone("QCD"))
    totalbkg = p.get_total_hist(bgs)
    # tight
    bgstight = [
        samples.getHistogram(
            "/top",
            str(histname).replace(
                "Loose", "Tight")).Clone("Top") if not is2d else p.flatten_th2(
                    samples.getHistogram(
                        "/top",
                        str(histname).replace("Loose", "Tight")).Clone("Top")),
        samples.getHistogram(
            "/Zonelep",
            str(histname).replace(
                "Loose", "Tight")).Clone("DY") if not is2d else p.flatten_th2(
                    samples.getHistogram(
                        "/Zonelep",
                        str(histname).replace("Loose", "Tight")).Clone("DY")),
        samples.getHistogram(
            "/Wonelep",
            str(histname).replace(
                "Loose", "Tight")).Clone("W") if not is2d else p.flatten_th2(
                    samples.getHistogram(
                        "/Wonelep",
                        str(histname).replace("Loose", "Tight")).Clone("W")),
        #samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", str(histname).replace("Loose","Tight")).Clone("QCD") if not is2d else p.flatten_th2(samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", str(histname).replace("Loose","Tight")).Clone("QCD")),
    ]
    dataname = "/data"
    if str(histname).replace("Loose", "Tight").find("El") != -1:
        dataname = "/data/ee"
    if str(histname).replace("Loose", "Tight").find("Mu") != -1:
        dataname = "/data/mm"
    ddqcdtight = samples.getHistogram(
        dataname,
        str(histname).replace(
            "Loose", "Tight")).Clone("Data") if not is2d else p.flatten_th2(
                samples.getHistogram(dataname,
                                     str(histname).replace(
                                         "Loose", "Tight")).Clone("Data"))
    qcdtight = samples.getHistogram(
        "/qcd/mu" if ismu else "/qcd/el",
        str(histname).replace(
            "Loose", "Tight")).Clone("QCD") if not is2d else p.flatten_th2(
                samples.getHistogram("/qcd/mu" if ismu else "/qcd/el",
                                     str(histname).replace(
                                         "Loose", "Tight")).Clone("QCD"))
    totalbkgtight = p.get_total_hist(bgstight)
    # Get data fakerate
    p.remove_overflow(totalbkg)
    p.remove_overflow(totalbkgtight)
    p.remove_overflow(ddqcd)
    p.remove_overflow(ddqcdtight)
    sf = -musf if ismu else -elsf
    ddqcd.Add(totalbkg, sf)
    ddqcdtight.Add(totalbkgtight, sf)
    ddqcdtight.Divide(ddqcd)
    # Get QCD fakerate
    p.remove_overflow(qcd)
    p.remove_overflow(qcdtight)
    qcdtight.Divide(qcd)
    bgs = [qcdtight.Clone("QCD FR")]
    data = ddqcdtight
    # Compute data driven fake rate
    colors = [2]
    plotfunc(sigs=sigs,
             bgs=bgs,
             data=data,
             colors=colors,
             syst=systs,
             options=alloptions)