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
tree = args.tree #The enabled systematic up/down variation sample prefixes to put on the ratio plot if args.do_systs: systs = { "JES": ("EnUp", "EnDown"), "JER": ["ResUp", "ResDown"], "MET": ["UnclusteredEnUp", "UnclusteredEnDown"], } else: systs = {} for lepton_channel in args.channels: logger.info("Plotting lepton channel %s" % lepton_channel) weight = Weights.total(lepton_channel)*Weights.wjets_madgraph_shape_weight()*Weights.wjets_madgraph_flat_weight() physics_processes = PhysicsProcess.get_proc_dict(lepton_channel=lepton_channel)#Contains the information about merging samples and proper pretty names for samples merge_cmds = PhysicsProcess.get_merge_dict(physics_processes) #The actual merge dictionary lumi = lumis[lepton_channel] isoreg='iso' use_antiiso = False if args.use_antiiso: isoreg='antiiso' use_antiiso = True #Get the file lists flist = get_file_list( merge_cmds,
from plots.common.sample import Sample from plots.common.sample_style import ColorStyleGen from plots.common.hist_plots import plot_hists from plots.common.cuts import Cuts, Weights from plots.common.legend import legend from plots.common.tdrstyle import tdrstyle 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")
lumi = lumis[args.lumitag]['iso'][proc] flist = [] for i in ['sig','bg']: for j in ['train','eval']: flist += mvaFileList[i][j] logger.debug('Used file list: %s',str(flist)) # Read in the samples samples = {} for f in flist: samples[f] = Sample.fromFile(args.indir+'/%s/%s.root' % (proc,f), tree_name='Events_MVA') # Set the weight expression weightString = str(Weights.total(proc) * Weights.wjets_madgraph_shape_weight() * Weights.wjets_madgraph_flat_weight()) # Which file do we use to write our TMVA trainings ext='' if len(rVar): ext='_sans' for v in rVar: ext+='_'+v if len(cVar): ext='_with' for v in cVar: ext+='_'+v
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
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")
"var": "phi_met", "binning": [20, -3.14, 3.14] } variables["cos_theta"] = { "name": "cos_theta", "var": "cos_theta", "binning": [20, -1, 1] } variables["bdt"] = { "name": "bdt", "var": Cuts.mva_vars['mu'], "binning": [60, -1, 1] } weights = [ ("weight__nominal", Weights.total_weight("mu")), ] c1 = ( "2j0t", Cuts.mt_mu()*Cuts.n_jets(2)*Cuts.n_tags(0)*Cuts.lepton("mu")*Cuts.hlt("mu"), ) c2 = ( "2j1t", Cuts.mt_mu()*Cuts.n_jets(2)*Cuts.n_tags(1)*Cuts.lepton("mu")*Cuts.hlt("mu"), ) c3 = ( "final_cb_2j1t", Cuts.final(2,1), )
#Define all the variables that we want to see variables = [ ("cos_theta", "cos_theta", [20, -1, 1]), #Demonstrate some weird binning ("cos_theta_binned", "cos_theta", [-1, -0.4674, -0.2558, -0.1028, 0.0, 0.116, 0.2126, 0.2956, 0.379, 0.4568, 0.5302, 0.6038, 0.6822, 0.7694, 1]), #ROOT functions can be used ("abs_eta_lj", "abs(eta_lj)", [20, 0, 5]), ("bdt", Cuts.mva_vars['mu'], [60, -1, 1]), ] #Define all the weight strategies that we want to apply weights = [ ("weight__nominal", Weights.total_weight("mu")), #Demonstrate reweighting ("weight__puw", Weights.pu("nominal")), ("weight__puw_up", Weights.pu("up")), ] #All the cut regions that we are interested in cuts = [ ("2j0t", Cuts.mt_mu()*Cuts.n_jets(2)*Cuts.n_tags(0)*Cuts.lepton("mu")*Cuts.hlt("mu")), ("2j1t", Cuts.mt_mu()*Cuts.n_jets(2)*Cuts.n_tags(1)*Cuts.lepton("mu")*Cuts.hlt("mu")), ("final_cb_2j1t", Cuts.final(2,1)) ] #Construct the analysis chain snodes, out = analysis_tree(cuts, weights, variables, args.infiles, args.outfile)