示例#1
0
    hcomphep.SetFillColor(ROOT.kRed)
    hcomphep.SetFillStyle('/')
    hcomphep.SetLineStyle('dashed')
    hcomphep.SetMarkerSize(0)

    Styling.data_style(data_post)

    hi = [data_post, htrue, hcomphep]
    chi2 = data_post.Chi2Test(htrue, "WW CHI2/NDF")
    #htrue.SetTitle(htrue.GetTitle() + " #chi^{2}/#nu = %.1f" % chi2)
    #hi_norm = hi
    hi_norm = map(post_normalize, hi)
    for h in hi[1:]:
        h.SetMarkerSize(0)
    of = OutputFolder(subdir='unfolding/%s' % lep)
    canv = plot_hists(hi_norm, x_label="cos #theta*", draw_cmd=len(hi)*["E1"], y_label="a.u.")
    #leg = legend(hi, styles=['p', 'f'], legend_pos='bottom-right', nudge_x=-0.29, nudge_y=-0.08)
    leg = legend(hi, styles=['p', 'f'], legend_pos='top-left', nudge_y=-0.14)
    lb = lumi_textbox(lumi,
        pos='top-center',
        line2="#scale[1.5]{A = %.2f #pm %.2f (stat.) #pm %.2f (syst.)}" % (
            measured_asym, measured_asym_errs[lep][0],  measured_asym_errs[lep][1]
        ), nudge_y=0.03
    )
    pmi = PlotMetaInfo(
        'costheta_unfolded',
        '2j1t_mva_loose {0}'.format(channel),
        'weighted to lumi={0}, sf(tchan)={1}'.format(lumi, fitpars[lep]),
        [args.infileMC, args.infileD]
    )
示例#2
0
if __name__=="__main__":
	samp = Sample.fromFile("~/Documents/stpol/data/out_step3_joosep_11_07_19_44/mu/iso/nominal/W4Jets_exclusive.root")
	samp.tree.AddFriend("trees/WJets_weights", samp.file_name)

	tdrstyle()
	
	cut = str(Cuts.final(2,0)*Cuts.Wflavour("W_heavy"))
	mean_weight = samp.drawHistogram(str(Weights.wjets_madgraph_weight("nominal")), cut, weight="1.0", plot_range=[200, 0, 2]).hist.GetMean()
	print "mean weight=%.2f" % mean_weight
	hi0 = samp.drawHistogram("cos_theta", cut, weight="1.0", plot_range=[20, -1, 1]).hist
	hi0.Scale(samp.lumiScaleFactor(20000))
	hi1 = samp.drawHistogram("cos_theta", cut, weight=str(Weights.wjets_madgraph_weight("nominal")), plot_range=[20, -1, 1]).hist
	hi1.Scale(samp.lumiScaleFactor(20000))
	hi2 = samp.drawHistogram("cos_theta", cut, weight=str(Weights.wjets_madgraph_weight("wjets_up")), plot_range=[20, -1, 1]).hist
	hi2.Scale(samp.lumiScaleFactor(20000))
	hi3 = samp.drawHistogram("cos_theta", cut, weight=str(Weights.wjets_madgraph_weight("wjets_down")), plot_range=[20, -1, 1]).hist
	hi3.Scale(samp.lumiScaleFactor(20000))

	hists = [hi0, hi1, hi2, hi3]
	#for h in hists:
	#	h.Scale(1.0/h.Integral())
	hi0.SetTitle("unweighted")
	hi1.SetTitle("weighted")
	hi2.SetTitle("weighted wjets_up")
	hi3.SetTitle("weighted wjets_down")
	ColorStyleGen.style_hists(hists)
	canv = plot_hists(hists, x_label="cos #theta")
	leg = legend(hists, styles=["f", "f"], nudge_x=-0.2)
	hi0.SetTitle("WJets hf yield with variations, 2J0T")
	canv.Update()
示例#3
0
def plot_ratios(cut_name, cut, samples, out_dir, recreate, flavour_scenario=flavour_scenarios[0]):
    out_dir += "/" + cut_name
    mkdir_p(out_dir)

    colls = dict()

    samples_WJets = filter(lambda x: sample_types.is_wjets(x.name), samples)

    for sc in flavour_scenario:
        logger.info("Drawing ratio with cut %s" % sc)
        cut_ = cut*getattr(Cuts, sc)
        colls[sc] = data_mc(costheta["var"], cut_name + "__" + sc, cut_, Weights.total()*Weights.mu, samples_WJets, out_dir, recreate, LUMI_TOTAL, plot_range=costheta["range"])

    logger.debug(colls[flavour_scenario[0]].hists["weight__nominal/cut__all/WJets_sherpa_nominal"].Integral())
    logger.debug(colls[flavour_scenario[1]].hists["weight__nominal/cut__all/WJets_sherpa_nominal"].Integral())
    coll = dict()
    for k, c in colls.items():
        for hn, h in c.hists.items():
            coll[hn + "/" + k] = h

    for k, h in coll.items():
        logger.debug("%s = %s" % (k, str([y for y in h.y()])))

    logger.debug(coll)
    #coll = HistCollection(coll, name=cut_name)

    merges = {}
    for sc in flavour_scenario:
        merges["madgraph/%s" % sc] = ["weight__nominal/cut__all/W[1-4]Jets_exclusive/%s" % sc]
        merges["sherpa/unweighted/%s" % sc] = ["weight__nominal/cut__all/WJets_sherpa_nominal/%s" % sc]
        merges["sherpa/weighted/%s" % sc] = ["weight__sherpa_flavour/cut__all/WJets_sherpa_nominal/%s" % sc]

    merged = merge_hists(coll, merges)
    for k, h in merged.items():
        logger.debug("%s = %s" % (k, str([y for y in h.y()])))
    hists_flavour = dict()
    hists_flavour["madgraph"] = ROOT.TH1F("madgraph", "madgraph", len(flavour_scenario), 0, len(flavour_scenario)-1)
    hists_flavour["sherpa/unweighted"] = ROOT.TH1F("sherpa_unw", "sherpa unweighted", len(flavour_scenario), 0, len(flavour_scenario)-1)
    hists_flavour["sherpa/weighted"] = ROOT.TH1F("sherpa_rew", "sherpa weighted", len(flavour_scenario), 0, len(flavour_scenario)-1)

    for i, sc in zip(range(1,len(flavour_scenario)+1), flavour_scenario):
        sh1_int, sh1_err = calc_int_err(merged["sherpa/unweighted/%s" % sc])
        sh2_int, sh2_err = calc_int_err(merged["sherpa/weighted/%s" % sc])
        mg_int, mg_err = calc_int_err(merged["madgraph/%s" % sc])
        logger.debug("%.2f %.2f" % (sh1_int, sh1_err))
        logger.debug("%.2f %.2f" % (sh2_int, sh2_err))
        logger.debug("%.2f %.2f" % (mg_int, mg_err))
        hists_flavour["madgraph"].SetBinContent(i, mg_int)
        hists_flavour["madgraph"].SetBinError(i, mg_err)
        hists_flavour["sherpa/unweighted"].SetBinContent(i, sh1_int)
        hists_flavour["sherpa/unweighted"].SetBinError(i, sh1_err)
        hists_flavour["sherpa/weighted"].SetBinContent(i, sh2_int)
        hists_flavour["sherpa/weighted"].SetBinError(i, sh2_err)

        hists_flavour["madgraph"].GetXaxis().SetBinLabel(i, sc)
        hists_flavour["sherpa/unweighted"].GetXaxis().SetBinLabel(i, sc)
        hists_flavour["sherpa/weighted"].GetXaxis().SetBinLabel(i, sc)

    hists_flavour["sherpa/weighted"].Sumw2()
    hists_flavour["sherpa/unweighted"].Sumw2()
    hists_flavour["madgraph"].Sumw2()

    hists_flavour["ratio/unweighted"] = hists_flavour["madgraph"].Clone("ratio_unw")
    hists_flavour["ratio/unweighted"].Divide(hists_flavour["sherpa/unweighted"])
    hists_flavour["ratio/weighted"] = hists_flavour["madgraph"].Clone("ratio_rew")
    hists_flavour["ratio/weighted"].Divide(hists_flavour["sherpa/weighted"])

    for i, sc in zip(range(1,len(flavour_scenario)+1), flavour_scenario):
        logger.info("weights[%s] = %.6f; //error=%.6f [%d]" % (sc, hists_flavour["ratio/unweighted"].GetBinContent(i), hists_flavour["ratio/unweighted"].GetBinError(i), i))

    flavour_ratio_coll = HistCollection(hists_flavour, name="hists__flavour_ratios")
    flavour_ratio_coll.save(out_dir)

    for sc in flavour_scenario:
        hists = [merged["madgraph/%s" % sc], merged["sherpa/unweighted/%s" % sc], merged["sherpa/weighted/%s" % sc]]
        for hist in hists:
            norm(hist)
            #hist.SetName(sc)
            #hist.SetTitle(sc)
        ColorStyleGen.style_hists(hists)
        canv = plot_hists(hists, x_label=costheta["varname"])
        leg = legend(hists, styles=["f", "f"], nudge_x=-0.2)
        chi2 = hists[0].Chi2Test(hists[1], "WW CHI2/NDF")
        hists[0].SetTitle("madgraph to sherpa comparison #chi^{2}/ndf=%.2f" % chi2)
        canv.Update()
        canv.SaveAs(out_dir + "/flavours__%s.png" % (sc))

    md_merged = dict()
    for sc in flavour_scenario:
        logger.info("Calculating ratio for %s" % sc)
        hi = merged["sherpa/unweighted/%s" % sc].Clone("ratio__%s" % sc)
        hi.Divide(merged["madgraph/%s" % sc])
        merged[hi.GetName()] = hi

    hc_merged = HistCollection(merged, md_merged, "hists__costheta_flavours_merged")
    hc_merged.save(out_dir)
    logger.info("Saved merged histogram collection")
示例#4
0
def plot_sherpa_vs_madgraph(var, cut_name, cut, samples, out_dir, recreate=False, **kwargs):
    hname = var["varname"]
    out_dir = out_dir + "/" + cut_name
    if recreate and os.path.exists(out_dir):
        logger.info("Output directory %s exists, removing" % out_dir)
        shutil.rmtree(out_dir)
    mkdir_p(out_dir)

    logger.info("Using output directory %s" % out_dir)


    logger.info("Using output directory %s" % out_dir)

    coll = data_mc(var["var"], cut_name, cut, Weights.total()*Weights.mu, samples, out_dir, recreate, LUMI_TOTAL, reweight_madgraph=True, flavour_split=True, plot_range=var["range"], **kwargs)

    logging.debug(str(coll.hists))
    for hn, hist in coll.hists.items():
        sample_name = coll.metadata[hn].sample_name
        process_name = coll.metadata[hn].process_name
        match = re.match(".*/cut__flavour__(W_[Hl][Hl])/.*", hn)
        if match:
            flavour_scenario = match.group(1)
        else:
            flavour_scenario = None

        try:
            if sample_types.is_mc(sample_name):
                Styling.mc_style(hist, process_name)
            else:
                Styling.data_style(hist)
        except KeyError as e:
            logger.warning("Couldn't style histogram %s" % hn)

        if flavour_scenario:
            logger.debug("Matched flavour split histogram %s, %s" % (hn, flavour_scenario))
            #Styling.mc_style(hist, process_name)
            if re.match("W_H[lH]", flavour_scenario):
                logger.debug("Changing colour of %s" % (hn))
                hist.SetFillColor(hist.GetFillColor()+1)
                hist.SetLineColor(hist.GetLineColor()+1)

    logger.debug("pre merge: %s" % str([ (hn, coll.hists[hn].GetLineColor()) for hn in coll.hists.keys() if "sherpa" in hn]))
    merges = dict()

    merge_cmds = get_merge_cmds()
    merge_cmds.pop("WJets")
    merges["madgraph/unweighted"] = merge_cmds.copy()
    merges["madgraph/weighted"] = merge_cmds.copy()
    merges["sherpa/unweighted"] = merge_cmds.copy()
    merges["sherpa/weighted"] = merge_cmds.copy()


    merges["sherpa/unweighted"]["WJets_hf"] = ["weight__nominal/cut__flavour__W_heavy/WJets_sherpa_nominal"]
    merges["sherpa/unweighted"]["WJets_lf"] = ["weight__nominal/cut__flavour__W_light/WJets_sherpa_nominal"]
    merges["sherpa/weighted"]["WJets_hf"] = ["weight__sherpa_flavour/cut__flavour__W_heavy/WJets_sherpa_nominal"]
    merges["sherpa/weighted"]["WJets_lf"] = ["weight__sherpa_flavour/cut__flavour__W_light/WJets_sherpa_nominal"]
    merges["madgraph/unweighted"]["WJets_hf"] = ["weight__nominal/cut__flavour__W_heavy/W[1-4]Jets_exclusive"]
    merges["madgraph/unweighted"]["WJets_lf"] = ["weight__nominal/cut__flavour__W_light/W[1-4]Jets_exclusive"]
    merges["madgraph/weighted"]["WJets_hf"] = ["weight__reweight_madgraph/cut__flavour__W_heavy/W[1-4]Jets_exclusive"]
    merges["madgraph/weighted"]["WJets_lf"] = ["weight__reweight_madgraph/cut__flavour__W_light/W[1-4]Jets_exclusive"]

    hmerged = dict()
    for k in merges.keys():
        hmerged[k] = merge_hists(copy.deepcopy(coll.hists), merges[k])

    logger.debug("post merge: %s" % str([ (hn, hmerged["sherpa/weighted"][hn].GetLineColor()) for hn in hmerged["sherpa/weighted"].keys()]))

    #w_mg_sh = 1.0416259307303726 #sherpa to madgraph ratio
    w_mg_sh = 1.0821535639376414
    hmerged["sherpa/weighted"]["WJets_hf"].Scale(w_mg_sh)
    hmerged["sherpa/weighted"]["WJets_lf"].Scale(w_mg_sh)

    logger.info("Drawing madgraph unweighted plot")
    canv = ROOT.TCanvas("c2", "c2")
    suffix = "__%s__%s" % (var["var"], cut_name)
    suffix = escape(suffix)
    plot(canv, "madgraph_unw"+suffix, hmerged["madgraph/unweighted"], out_dir, **kwargs)

    kwargs = dict({"x_label": var["varname"]}, **kwargs)

    for k, v in hmerged.items():
        logger.debug("Group %s" % k)
        for hn, h in v.items():
            logger.debug("Sample %s = %.2f" % (hn, h.Integral()))
        logger.info("%s data=%.2f" % (k, v["data"].Integral()))
        logger.info("%s MC=%.2f" % (k, sum([h.Integral() for k, h in v.items() if k!="data"])))

    hists_flavours_merged = dict()
    hists_flavours_merged["madgraph/weighted"] = merge_hists(hmerged["madgraph/weighted"], {"WJets": ["WJets_hf", "WJets_lf"]})
    hists_flavours_merged["madgraph/unweighted"] = merge_hists(hmerged["madgraph/unweighted"], {"WJets": ["WJets_hf", "WJets_lf"]})
    hists_flavours_merged["sherpa/unweighted"] = merge_hists(hmerged["sherpa/unweighted"], {"WJets": ["WJets_hf", "WJets_lf"]})
    hists_flavours_merged["sherpa/weighted"] = merge_hists(hmerged["sherpa/weighted"], {"WJets": ["WJets_hf", "WJets_lf"]})

    logger.info("Drawing sherpa weighted plot")
    canv = ROOT.TCanvas("c1", "c1")
    plot(canv, "sherpa_rew"+suffix, hmerged["sherpa/weighted"], out_dir, **kwargs)

    logger.info("Drawing sherpa unweighted plot")
    canv = ROOT.TCanvas("c1", "c1")
    plot(canv, "sherpa_unw"+suffix, hmerged["sherpa/unweighted"], out_dir, **kwargs)

    logger.info("Drawing madgraph plot")
    canv = ROOT.TCanvas("c2", "c2")
    plot(canv, "madgraph_rew"+suffix, hmerged["madgraph/weighted"], out_dir, **kwargs)

    total_madgraph = copy.deepcopy(hmerged["madgraph/unweighted"])
    merged_colls = dict()
    for k, v in hmerged.items():
        merged_colls[k] = HistCollection(copy.deepcopy(v), name=k)
    logger.info("Drawing sherpa vs. madgraph shape comparison plots")

    hists = [
        ("sherpa unw hf", hmerged["sherpa/unweighted"]["WJets_hf"]),
        ("sherpa rew hf", hmerged["sherpa/weighted"]["WJets_hf"]),
        ("madgraph unw hf", hmerged["madgraph/unweighted"]["WJets_hf"]),
        ("madgraph rew hf", hmerged["madgraph/weighted"]["WJets_hf"]),
    ]
    hists = copy.deepcopy(hists)
    for hn, h in hists:
        h.SetTitle(hn + " %.2f" % h.Integral())
        h.Scale(1.0/h.Integral())
    hists = [h[1] for h in hists]
    ColorStyleGen.style_hists(hists)
    canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    leg = legend(hists, styles=["f", "f"], **kwargs)
    canv.SaveAs(out_dir + "/weighted_flavour_hf_%s.png" % hname)
    canv.Close()

    hists = [
        ("data", hmerged["madgraph/unweighted"]["data"]),
        ("sherpa", hists_flavours_merged["sherpa/unweighted"]["WJets"]),
        ("madgraph", hists_flavours_merged["madgraph/unweighted"]["WJets"]),
    ]
    hists = copy.deepcopy(hists)
    for hn, h in hists:
        h.SetTitle(hn + " %.2f" % h.Integral())
        h.Scale(1.0/h.Integral())
    hists = [h[1] for h in hists]
    ColorStyleGen.style_hists(hists)
    canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    leg = legend(hists, styles=["f", "f"], **kwargs)
    canv.SaveAs(out_dir + "/unweighted_sherpa_mg_%s.png" % hname)
    canv.Close()

    hists = [
        ("data", hmerged["madgraph/unweighted"]["data"]),
        ("sherpa", hists_flavours_merged["sherpa/weighted"]["WJets"]),
        ("madgraph", hists_flavours_merged["madgraph/weighted"]["WJets"]),
    ]
    hists = copy.deepcopy(hists)
    for hn, h in hists:
        h.SetTitle(hn + " %.2f" % h.Integral())
        h.Scale(1.0/h.Integral())
    hists = [h[1] for h in hists]
    ColorStyleGen.style_hists(hists)
    canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    leg = legend(hists, styles=["f", "f"], **kwargs)
    canv.SaveAs(out_dir + "/weighted_sherpa_mg_%s.png" % hname)
    canv.Close()

    hists = [
        ("sherpa unw lf", hmerged["sherpa/unweighted"]["WJets_lf"]),
        ("sherpa rew lf", hmerged["sherpa/weighted"]["WJets_lf"]),
        ("madgraph unw lf", hmerged["madgraph/unweighted"]["WJets_lf"]),
        ("madgraph rew lf", hmerged["madgraph/weighted"]["WJets_lf"]),
    ]
    hists = copy.deepcopy(hists)
    for hn, h in hists:
        h.SetTitle(hn + " %.2f" % h.Integral())
        h.Scale(1.0/h.Integral())
    hists = [h[1] for h in hists]
    ColorStyleGen.style_hists(hists)
    canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    leg = legend(hists, styles=["f", "f"], **kwargs)
    canv.SaveAs(out_dir + "/weighted_flavour_lf_%s.png" % hname)
    canv.Close()

    hists = [
        ("data", hmerged["madgraph/unweighted"]["data"]),
        ("madgraph unw", hists_flavours_merged["madgraph/unweighted"]["WJets"]),
        ("madgraph rew", hists_flavours_merged["madgraph/weighted"]["WJets"]),
        ("sherpa unw", hists_flavours_merged["sherpa/unweighted"]["WJets"]),
        ("sherpa rew", hists_flavours_merged["sherpa/weighted"]["WJets"]),
    ]
    hists = copy.deepcopy(hists)
    for hn, h in hists:
        h.SetTitle(hn + " %.2f" % h.Integral())
        h.Scale(1.0/h.Integral())
    hists = [h[1] for h in hists]
    ColorStyleGen.style_hists(hists)
    canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    leg = legend(hists, styles=["f", "f"], **kwargs)
    hists[0].SetTitle("")
    canv.Update()
    canv.SaveAs(out_dir + "/shapes_%s.png" % hname)
    canv.Close()

    # hists = [
    #     ("sherpa hf", hmerged["sherpa"]["WJets_hf"]),
    #     ("madgraph unw hf", hmerged["madgraph/unweighted"]["WJets_hf"]),
    #     ("madgraph rew hf", hmerged["madgraph/weighted"]["WJets_hf"]),
    # ]
    # hists = copy.deepcopy(hists)
    # for hn, h in hists:
    #     h.SetTitle(hn + " %.2f" % h.Integral())
    #     h.Scale(1.0/h.Integral())
    # hists = [h[1] for h in hists]
    # ColorStyleGen.style_hists(hists)
    # canv = plot_hists(hists, x_label=var["varname"], do_chi2=True)
    # leg = legend(hists, styles=["f", "f"], **kwargs)
    # hists[0].SetTitle("madgraph sherpa rew hf")
    # canv.SaveAs(out_dir + "/shapes_hf_%s.png" % hname)
    # canv.Close()

    return coll, merged_colls
示例#5
0
        logging.info("files = %s" % str(files))

        for sn, fi in files.items():
            hi = fi.Get("flavour_counts")
            hi.SetName(sn)
            pn = sn
            Styling.mc_style(hi, pn)
            hists[sn] = hi

        merges = OrderedDict()
        merges["W (madgraph)"] = ["W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive"]
        merges["W (sherpa)"] = ["WJets_sherpa"]
        hists_merged = merge_hists(hists, merges)
        hists = hists_merged.values()

        ColorStyleGen.style_hists(hists)
        for h in hists:
            logging.info(list(h.y()))
            h.SetFillColor(ROOT.kWhite)
            h.Scale(1.0 / h.Integral())
        c = plot_hists(hists, x_label="flavour(j,j)", y_label="", draw_cmd="HIST E1")
        leg = legend(hists, styles=len(hists) * ["f"], nudge_x=-0.1)
        hists[0].SetTitle("Jet flavour fraction n %s" % cut_name)
        # hists[0].SetFillColor(ROOT.kGreen+2)
        hists[0].SetMinimum(10 ** -3)
        hists[0].SetMaximum(10)
        # hists[0].SetLineColor(ROOT.kGreen+2)
        c.SetLogy()
        c.SaveAs(out_dir + "/flavour_%s.pdf" % cut_name)
        c.Close()