hc.save(".") colls = dict() for wn, w in weights: hc = HistCollection.load("./bweight_%s.root" % wn) colls[wn] = hc for proc in merge_cmds.keys(): hists = OrderedDict([(wn, colls[wn].hists[proc]) for wn, w in weights]) for hn, h in hists.items(): h.SetTitle(pretty_names_weights[hn]) hists_normed = copy.deepcopy(hists) for hn, h in hists_normed.items(): norm(h, setName=False) if hn!="unw": h.Divide(hists_normed["unw"]) hists_normed["unw"].Divide(hists_normed["unw"]) #hists_normed.pop("unw") c1 = plot_hists_dict(hists, False, **plot_args) c1.Update() c1.SaveAs("weighted_%s.pdf" % proc) c2 = plot_hists_dict(hists_normed, setNames=False, draw_cmd="HIST", min_bin=0.990, max_bin=1.01, legend_pos="top-left", **plot_args) c2.Update()
def normalize(hn, h): norm(h) return h
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")