def data_mc(var, cut_name, cut, weight, samples, out_dir, recreate, lumi, **kwargs): plot_name = "%s__%s" % (var, cut_name) plot_name = escape(plot_name) systematic = kwargs.get("systematic", "nominal") if recreate: hists = {} metadata = {} pbar = ProgressBar( widgets=["Plotting %s:" % plot_name] + widgets, maxval=sum([s.getEventCount() for s in samples]) ).start() nSamp = 1 for sample in samples: hname = sample.name if sample.isMC: hname_ = sample.name hist = sample.drawHistogram(var, str(cut), weight=str(weight), **kwargs) hist.Scale(sample.lumiScaleFactor(lumi)) hists[hname_] = hist metadata[hname_] = HistMetaData(sample_name=sample.name, process_name=sample.process_name) else: hist = sample.drawHistogram(var, str(cut), **kwargs) hists[hname] = hist metadata[hname] = HistMetaData(sample_name=sample.name, process_name=sample.process_name) pbar.update(nSamp) nSamp += sample.getEventCount() hist_coll = HistCollection(hists, metadata, plot_name) hist_coll.save(out_dir) logger.debug("saved hist collection %s" % (out_dir)) pbar.finish() hist_coll = HistCollection.load(out_dir + "/%s.root" % plot_name) logger.debug("loaded hist collection %s" % (out_dir + "/%s.root" % plot_name)) return hist_coll
def shape_variation(var, plot_range, varname, recreate, out_name): tot_hists = OrderedDict() tdrstyle() for wn, w in [ ("unw", Weight("1.0")), ("nominal", Weights.wjets_madgraph_shape_weight("nominal")), ("shape_up", Weights.wjets_madgraph_shape_weight("wjets_up")), ("shape_down", Weights.wjets_madgraph_shape_weight("wjets_down")) ]: p = data_mc( var, "2J0T_%s" % (wn), cut, Weights.total("mu", "nominal")*w, samples, ".", recreate, lumi_iso["mu"], plot_range=plot_range, ) for hn, h in p.hists.items(): logger.debug("%s %.2f" % (hn, h.Integral())) #sumw = [(sample.name, numpy.mean(root_numpy.root2array(sample.tfile.GetPath()[:-2], "trees/WJets_weights", branches=[str(w)])[str(w)])) for sample in samples] s = sum(p.hists.values()).Clone() tot_hists[wn] = s hc = HistCollection(tot_hists, name=out_name) for hn, h in hc.hists.items(): print hn, h.Integral() canv = plot_hists_dict(hc.hists, do_chi2=False, do_ks=True, x_label=varname, legend_pos="top-left") hc.hists.values()[0].SetTitle("shape variation") canv.SaveAs(out_name + ".png") hc.save(out_name) return hc, canv
recreate = True if recreate: for sn in sampnames: sample = Sample.fromFile(path + "/" + sn + ".root") for wn, w in weights: hi = sample.drawHistogram(var, str(cut), weight=w, plot_range=plot_range) hi.Scale(sample.lumiScaleFactor(lumi)) hists[wn][sample.name] = hi merged = dict() for wn, w in weights: merged[wn] = merge_hists(hists[wn], merge_cmds) hc = HistCollection(merged[wn], name="bweight_%s" % wn) 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():
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")