Example #1
0
 def loadhists(f):
     out = OrderedDict()
     for k in procnames.keys():
         out[k] = f.Get(var+"__" + k)
         out[k].SetTitle(procnames[k])
         Styling.mc_style(out[k], procstyles[k])
     return out
Example #2
0
def load_theta_format(inf, styles=None):
    from plots.common.sample_style import Styling
    if not styles:
        styles = {}

    hists = SystematicHistCollection()
    fi = File(inf)
    ROOT.gROOT.cd()
    #rate_sfs = load_fit_results("final_fit/results/%s.txt" % fit_results[channel])
    for root, dirs, items in fi.walk():
        for it in items:

            hn = hname_decode(it)
            variable, sample, syst, systdir = hn["var"], hn["sample"], hn["type"], hn["dir"]

            k = os.path.join(root, it)
            try:
                it = fi.Get(k).Clone()
            except:
                raise Exception("Could not copy object %s" % k)
            #Skip anything not a histogram
            if not isinstance(it, ROOT.TH1F):
                continue

            if sample == "DATA":
                sample = sample.lower()

            if sample.lower() != "data":
                sty = styles.get(sample.lower(), sample)
                try:
                    Styling.mc_style(it, sty)
                except:
                    logger.debug("Could not style histogram from sample: %s" % sample)
            else:
                Styling.data_style(it)
            hists[variable][syst][systdir][sample]= it

    #hists = hists.as_dict()
    fi.Close()

    return hists
Example #3
0
        if qcd.Integral() != 0:
            qcd.Scale(qcd_norm / qcd.Integral()
                      )  #rescale qcd template according to the best-fit value

#--------------Rebin----------------
    if hist_name == "cos_theta" or hist_name == "pt_lj" or hist_name == "eta_lj" or hist_name == "el_mva":  #<--sometimes need to change rebin to 1, if plots are weird (QCD not shown)
        nrebin = 4
    elif hist_name == "met" or hist_name == "mt_mu" or hist_name == "el_pt" or hist_name == "top_mass" or hist_name == "deltaR_lj" or hist_name == "deltaR_bj":
        nrebin = 2
    else:
        nrebin = 1

    for key in mc_to_plot:
        mc_to_plot[key].Rebin(nrebin)
        Styling.mc_style(mc_to_plot[key], key)

    qcd.Rebin(nrebin)
    Styling.mc_style(qcd, "QCD")

    data.Rebin(nrebin)
    Styling.data_style(data)

    #--------------stack----------------------------
    sum = ROOT.THStack("sum", "")
    h_sumMC = mc_to_plot["signal"].Clone("h_sumMC")

    for key in mc_to_plot:
        if key != "signal":
            h_sumMC.Add(mc_to_plot[key])
    h_sumMC.Add(qcd)
Example #4
0
    #Make some test MC histograms
    h_mc1 = ROOT.TH1F("h_mc1", "TTJets", 20, -5, 5)
    for i in range(1500):
        h_mc1.Fill(random.normalvariate(0, 1))

    h_mc2 = ROOT.TH1F("h_mc2", "t-channel", 20, -5, 5)
    for i in range(900):
        h_mc2.Fill(random.normalvariate(0, 0.9))

    h_mc3 = ROOT.TH1F("h_mc3", "W+Jets", 20, -5, 5)
    for i in range(5000):
        h_mc3.Fill(random.normalvariate(0,  1.2))

    #Set the histogram styles and colors
    from sample_style import Styling
    Styling.mc_style(h_mc1, "TTJets_FullLept")
    Styling.mc_style(h_mc2, "T_t")
    Styling.mc_style(h_mc3, "W1Jets_exclusive")
    Styling.data_style(h_d1)

    #Create the canvas
    canv = ROOT.TCanvas("c", "c")
    canv.SetWindowSize(500, 500)
    canv.SetCanvasSize(600, 600)

    #!!!!LOOK HERE!!!!!
    #----
    #Draw the stacked histograms
    #----
    stacks_d = OrderedDict() #<<< need to use OrderedDict to have data drawn last (dict does not preserve order)
    stacks_d["mc"] = [h_mc1, h_mc2, h_mc3] # <<< order is important here, mc1 is bottom-most
Example #5
0
    hdata = Hist(20, -1, 1, name="mc")
    hmc = Hist(20, -1, 1, name="data")

    for i in range(10000):
        hdata.Fill(nr.normal(0, 1))
    for i in range(100000):
        hmc.Fill(nr.normal(0.05, 1))
    hmc.Scale(1.05 * hdata.Integral()/ hmc.Integral())

    c = ROOT.TCanvas("c1")

    p1 = subpad(c)

    from plots.common.sample_style import Styling
    Styling.mc_style(hmc, "T_t")
    Styling.data_style(hdata)
    hmc.Draw("hist")
    hdata.Draw("e1 same")

    hsyst_up = hmc.Clone()
    hsyst_up.Scale(1.02)

    #FIXME: do random smearing
    for i in range(1, hsyst_up.nbins()):
        pass
    hsyst_down = hmc.Clone()
    hsyst_down.Scale(0.98)

    p2, hist_ratio = plot_data_mc_ratio(c, hmc, hdata, syst_hists=(hsyst_up, hsyst_down))
    #c.Update()
Example #6
0
        print "QCD estimated from data = " + str(qcd_norm)

        if qcd.Integral() != 0:
            qcd.Scale(qcd_norm/qcd.Integral()) #rescale qcd template according to the best-fit value

#--------------Rebin----------------
    if hist_name == "cos_theta" or hist_name == "pt_lj" or hist_name == "eta_lj" or hist_name == "el_mva": #<--sometimes need to change rebin to 1, if plots are weird (QCD not shown)
        nrebin = 4
    elif hist_name == "met" or hist_name == "mt_mu" or hist_name == "el_pt" or hist_name == "top_mass" or hist_name == "deltaR_lj" or hist_name == "deltaR_bj":
        nrebin = 2
    else:
        nrebin =1

    for key in mc_to_plot:
        mc_to_plot[key].Rebin(nrebin)
        Styling.mc_style(mc_to_plot[key],key)

    qcd.Rebin(nrebin)
    Styling.mc_style(qcd,"QCD")

    data.Rebin(nrebin)
    Styling.data_style(data)

#--------------stack----------------------------
    sum = ROOT.THStack("sum","")
    h_sumMC = mc_to_plot["signal"].Clone("h_sumMC")

    for key in mc_to_plot:
        if key != "signal":
            h_sumMC.Add(mc_to_plot[key])
    h_sumMC.Add(qcd)
Example #7
0
            hcomphep = hi
    hcomphep.Scale(fitpars[lep])
    hcomphep.SetTitle("generated (CompHEP)")

    logger.info("Data={0:.0f}, powheg={1:.0f}, comphep={0:.0f}".format(data_post.Integral(), htrue.Integral(), hcomphep.Integral()))
    #Normalize to same area
    #htrue.Scale(data_post.Integral() / htrue.Integral())
    measured_asym = asymmetry(data_post, len(binning)/2 + 1)


    #####################
    # Plot the unfolded #
    #####################
    from plots.common.sample_style import Styling
    #Styling.mc_style(pe_post, 'T_t')
    Styling.mc_style(htrue, 'T_t')
    htrue.SetLineColor(ROOT.kGreen)
    htrue.SetFillColor(ROOT.kGreen)
    htrue.SetMarkerSize(0)

    Styling.mc_style(hcomphep, 'T_t')
    hcomphep.SetLineColor(ROOT.kRed)
    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")
Example #8
0
def data_mc_plot(samples, plot_def, name, lepton_channel, lumi, weight, physics_processes, use_antiiso=False):

    logger.info('Plot in progress %s' % name)

    merge_cmds = PhysicsProcess.get_merge_dict(physics_processes) #The actual merge dictionary

    var = plot_def['var']

    #Id var is a list/tuple, assume
    if not isinstance(var, basestring):
        try:
            if lepton_channel == 'ele':
                var = var[0]
            elif lepton_channel == 'mu':
                var = var[1]
        except Exception as e:
            logger.error("Plot variable 'var' specification incorrect for multi-variable plot: %s" % str(var))
            raise e
    cut = None
    if lepton_channel == 'ele':
        cut = plot_def['elecut']
    elif lepton_channel == 'mu':
        cut = plot_def['mucut']

    cut_str = str(cut)

    plot_range = plot_def['range']

    do_norm = False
    if 'normalize' in plot_def.keys() and plot_def['normalize']:
        do_norm = True
    hists_mc = dict()
    hists_data = dict()
    for name, sample in samples.items():
        logger.debug("Starting to plot %s" % name)
        if sample.isMC:
            hist = sample.drawHistogram(var, cut_str, weight=str(weight), plot_range=plot_range)
            hist.Scale(sample.lumiScaleFactor(lumi))
            hists_mc[sample.name] = hist
            if do_norm:
                Styling.mc_style_nostack(hists_mc[sample.name], sample.name)
            else:
                Styling.mc_style(hists_mc[sample.name], sample.name)

            if "fitpars" in plot_def.keys():
                rescale_to_fit(sample.name, hist, plot_def["fitpars"][lepton_channel])
        elif "antiiso" in name and plot_def['estQcd'] and not use_antiiso:

            # Make loose template
            #Y U NO LOOP :) -JP
            region = '2j1t'
            if '2j0t' in plot_def['estQcd']: region='2j0t'
            if '3j0t' in plot_def['estQcd']: region='3j0t'
            if '3j1t' in plot_def['estQcd']: region='3j1t'
            if '3j2t' in plot_def['estQcd']: region='3j2t'

            qcd_extra_cut = Cuts.deltaR(0.3)*Cuts.antiiso(lepton_channel)

            #Take the loose template with a good shape from the N-jet, M-tag, post lepton selection region with high statistics
            qcd_loose_cut = cutlist[region]*cutlist['presel_'+lepton_channel]*qcd_extra_cut

            #Take the template which can be correctly normalized from the actual region with inverted isolation cuts
            qcd_cut = cut*qcd_extra_cut

            hist_qcd_loose = sample.drawHistogram(var, str(qcd_loose_cut), weight="1.0", plot_range=plot_range)
            hist_qcd = sample.drawHistogram(var, str(qcd_cut), weight="1.0", plot_range=plot_range)
            logger.debug("Using the QCD scale factor %s: %.2f" % (plot_def['estQcd'], qcdScale[lepton_channel][plot_def['estQcd']]))
            

            hist_qcd.Scale(qcdScale[lepton_channel][plot_def['estQcd']])
            hist_qcd_loose.Scale(hist_qcd.Integral()/hist_qcd_loose.Integral())
            if var=='cos_theta':
                hist_qcd=hist_qcd_loose
            sampn = "QCD"+sample.name

            #Rescale the QCD histogram to the eta_lj fit
            if "fitpars" in plot_def.keys():
                rescale_to_fit(sampn, hist_qcd, plot_def["fitpars"][lepton_channel])

            hists_mc[sampn] = hist_qcd
            hists_mc[sampn].SetTitle('QCD')
            if do_norm:
                Styling.mc_style_nostack(hists_mc[sampn], 'QCD')
            else:
                Styling.mc_style(hists_mc[sampn], 'QCD')

        #Real ordinary data in the isolated region
        elif not "antiiso" in name or use_antiiso:
            hist_data = sample.drawHistogram(var, cut_str, weight="1.0", plot_range=plot_range)
            hist_data.SetTitle('Data')
            Styling.data_style(hist_data)
            hists_data[name] = hist_data


    if len(hists_data.values())==0:
        raise Exception("Couldn't draw the data histogram")

    #Combine the subsamples to physical processes
    hist_data = sum(hists_data.values())
    merge_cmds['QCD']=["QCD"+merge_cmds['data'][0]]
    order=['QCD']+PhysicsProcess.desired_plot_order
    if plot_def['log']:
        order = PhysicsProcess.desired_plot_order_log+['QCD']
    merged_hists = merge_hists(hists_mc, merge_cmds, order=order)

    if hist_data.Integral()<=0:
        logger.error(hists_data)
        logger.error("hist_data.entries = %d" % hist_data.GetEntries())
        logger.error("hist_data.integral = %d" % hist_data.Integral())
        raise Exception("Histogram for data was empty. Something went wrong, please check.")

    if do_norm:
        for k,v in merged_hists.items():
            v.Scale(1./v.Integral())
        hist_data.Scale(1./hist_data.Integral())

    htot = sum(merged_hists.values())

    chi2 = hist_data.Chi2Test(htot, "UW CHI2/NDF")
    if chi2>20:#FIXME: uglyness
        logger.error("The chi2 between data and MC is large (%s, chi2=%.2f). You may have errors with your samples!" %
            (name, chi2)
        )
        logger.info("MC  : %s" % " ".join(map(lambda x: "%.1f" % x, list(htot.y()))))
        logger.info("DATA: %s" % " ".join(map(lambda x: "%.1f" % x, list(hist_data.y()))))
        logger.info("diff: %s" % str(
            " ".join(map(lambda x: "%.1f" % x, numpy.abs(numpy.array(list(htot.y())) - numpy.array(list(hist_data.y())))))
        ))

    merged_hists_l = merged_hists.values()

    PhysicsProcess.name_histograms(physics_processes, merged_hists)

    leg_style = ['p','f']
    if do_norm:
        leg_style=['p','l']
    leg = legend([hist_data] + list(reversed(merged_hists_l)), legend_pos=plot_def['labloc'], styles=leg_style)

    canv = ROOT.TCanvas()

    #Make the stacks
    stacks_d = OrderedDict()
    stacks_d["mc"] = merged_hists_l
    stacks_d["data"] = [hist_data]

    #label
    xlab = plot_def['xlab']
    if not isinstance(xlab, basestring):
        if lepton_channel == 'ele':
            xlab = xlab[0]
        else:
            xlab = xlab[1]
    ylab = 'N / '+str((1.*(plot_range[2]-plot_range[1])/plot_range[0]))
    if plot_def['gev']:
        ylab+=' GeV'
    fact = 1.5
    if plot_def['log']:
        fact = 10

    plow=0.3
    if do_norm:
        plow=0

    #Make a separate pad for the stack plot
    p1 = ROOT.TPad("p1", "p1", 0, plow, 1, 1)
    p1.Draw()
    p1.SetTicks(1, 1);
    p1.SetGrid();
    p1.SetFillStyle(0);
    p1.cd()

    stacks = plot_hists_stacked(p1, stacks_d, x_label=xlab, y_label=ylab, max_bin_mult = fact, do_log_y = plot_def['log'], stack = (not do_norm))

    #Put the the lumi box where the legend is not
    boxloc = 'top-right'
    if plot_def['labloc'] == 'top-right':
        boxloc = 'top-left'
    chan = 'Electron'
    if lepton_channel == "mu":
        chan = 'Muon'

    additional_comments = ""
    if 'cutname' in plot_def.keys():
        additional_comments += ", " + plot_def['cutname'][lepton_channel]
    lbox = lumi_textbox(lumi,
        boxloc,
        'preliminary',
        chan + ' channel' + additional_comments
    )

    #Draw everything
    lbox.Draw()
    leg.Draw()
    canv.Draw()

    #Keep the handles just in case
    canv.PAD1 = p1
    canv.STACKS = stacks
    canv.LEGEND = legend
    canv.LUMIBOX = lbox

    return canv, merged_hists, htot, hist_data
Example #9
0
    #Define the variable, cut, weight and lumi
    var = "cos_theta"
    cut_name = "2j1t"
    cut_str = str(Cuts.n_jets(2)*Cuts.n_tags(1)*Cuts.lepton_veto*Cuts.one_muon*Cuts.mt_mu*Cuts.top_mass_sig*Cuts.eta_lj)
    weight_str = "1.0"
    lumi = 20000 #FIXME: take from the step2 output filelists/step2/latest/iso/nominal/luminosity.txt

    #nbins, min, max
    plot_range= [20, -1, 1]

    for name, sample in samples.items():
        if sample.isMC:
            hist = sample.drawHistogram(var, cut_str, weight=weight_str, plot_range=plot_range)
            hist.Scale(sample.lumiScaleFactor(lumi)*mc_sf)
            hists_mc[sample.name] = hist.hist
            Styling.mc_style(hists_mc[sample.name], sample.name)
        elif name == "SingleMu":
            hist_data = sample.drawHistogram(var, cut_str, weight="1.0", plot_range=plot_range)
            Styling.data_style(hist_data)

        elif name == "SingleMu_aiso":
            hist_qcd = sample.drawHistogram(var, cut_str, weight="1.0", plot_range=plot_range)
            #hist_qcd.
            pass

    #Combine the subsamples to physical processes
    merged_hists = merge_hists(hists_mc, merge_cmds).values()

    #Some printout
    for h in merged_hists + [hist_data]:
        print h.GetName(), h.GetTitle(), h.Integral()
Example #10
0
    for i in range(1500):
        h_mc1.Fill(random.normalvariate(0, 1))

    h_mc2 = ROOT.TH1F("h_mc2", "t-channel", 20, -5, 5)

    for i in range(900):
        h_mc2.Fill(random.normalvariate(0, 0.9))

    h_mc3 = ROOT.TH1F("h_mc3", "W+Jets", 20, -5, 5)
    h_mc3.SetTitle(pretty_names.sample_names["W1Jets_exclusive"])
    for i in range(5000):
        h_mc3.Fill(random.normalvariate(0, 1.2))

    #Set the histogram styles and colors
    from plots.common.sample_style import Styling
    Styling.mc_style(h_mc1, "TTJets_FullLept")
    Styling.mc_style(h_mc2, "T_t")
    Styling.mc_style(h_mc3, "W1Jets_exclusive")
    Styling.data_style(h_d1)

    #Create the canvas
    canv = ROOT.TCanvas("c", "c")
    canv.SetWindowSize(500, 500)
    canv.SetCanvasSize(600, 600)

    #!!!!LOOK HERE!!!!!
    #----
    #Draw the stacked histograms
    #----
    stacks_d = OrderedDict(
    )  #<<< need to use OrderedDict to have data drawn last (dict does not preserve order)
Example #11
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
Example #12
0
    #Make some test MC histograms
    h_mc1 = ROOT.TH1F("h_mc1", "A", 20, -5, 5)
    for i in range(1500):
        h_mc1.Fill(random.normalvariate(0, 1))

    h_mc2 = ROOT.TH1F("h_mc2", "B", 20, -5, 5)
    for i in range(900):
        h_mc2.Fill(random.normalvariate(0, 0.9))

    h_mc3 = ROOT.TH1F("h_mc3", "C", 20, -5, 5)
    for i in range(5000):
        h_mc3.Fill(random.normalvariate(0,  1.2))

    #Set the histogram styles and colors
    from sample_style import Styling
    Styling.mc_style(h_mc1, "TTJets_FullLept")
    Styling.mc_style(h_mc2, "WJets_sherpa")
    Styling.mc_style(h_mc3, "WW")
    Styling.data_style(h_d1)

    #Create the canvas
    canv = ROOT.TCanvas("c", "c")

    #!!!!LOOK HERE!!!!!
    #----
    #Draw the stacked histograms
    #----
    stacks_d = OrderedDict() #<<< need to use OrderedDict to have data drawn last (dict does not preserve order)
    stacks_d["mc"] = [h_mc1, h_mc2, h_mc3] # <<< order is important here, mc1 is bottom-most
    stacks_d["data"] = [h_d1]
    stacks = plot_hists_stacked(
Example #13
0
def plot_fit(fit, infile, outfile, result):
    tdrstyle()

    spl = fit.filename.split("__")
    var = spl[1]    

    procstyles = OrderedDict()
    procstyles["tchan"] = "T_t"
    procstyles["wzjets"] = "WJets_inclusive"
    procstyles["top"] = "TTJets_FullLept"
    procstyles["qcd"] = "QCD"

    procnames = OrderedDict()
    procnames["wzjets"] = "W, Z"
    procnames["top"] = "t, #bar{t}"
    procnames["tchan"] = "signal (t-channel)"
    procnames["qcd"] = "QCD"

    hists_mc_pre = OrderedDict()
    hists_mc_post = OrderedDict()
    print "IN",infile
    print outfile
    fi1 = File(infile)
    fi2 = File(outfile)
    hist_data = fi1.Get(var+"__DATA")

    def loadhists(f):
        out = OrderedDict()
        for k in procnames.keys():
            out[k] = f.Get(var+"__" + k)
            out[k].SetTitle(procnames[k])
            Styling.mc_style(out[k], procstyles[k])
        return out
    hists_mc_pre = loadhists(fi1)
    hists_mc_post = loadhists(fi2)
    print hists_mc_pre, hists_mc_post
    Styling.data_style(hist_data)



    of = OutputFolder(subdir="plots/final_fit")
    def plot_data_mc(hists_mc, hist_data, name):
        canv = ROOT.TCanvas()
        p1 = ROOT.TPad("p1", "p1", 0, 0.3, 1, 1)
        p1.Draw()
        p1.SetTicks(1, 1);
        p1.SetGrid();
        p1.SetFillStyle(0);
        p1.cd()

        stacks_d = OrderedDict()
        print "MC",hists_mc
        print "VAL",hists_mc.values()
        stacks_d["mc"] = hists_mc.values()
        stacks_d["data"] = [hist_data]
        stacks = plot_hists_stacked(
            p1,
            stacks_d,
            x_label=var,
            y_label="",
            do_log_y=True
        )
        leg = legend([hist_data] + list(reversed(hists_mc.values())), styles=["p", "f"])
        print canv, hist_data
        print get_stack_total_hist(stacks["mc"])
        ratio_pad, hratio = plot_data_mc_ratio(canv, get_stack_total_hist(stacks["mc"]), hist_data)

        plot_info = PlotMetaInfo(
            name,
            "CUT",
            "WEIGHT",
            [infile],
            subdir=fit.name,
            comments=str(result[SIGNAL])
        )
        of.savePlot(canv, plot_info)
        canv.Close()
    plot_data_mc(hists_mc_post, hist_data, "post_fit")
    plot_data_mc(hists_mc_pre, hist_data, "pre_fit")
Example #14
0
        make_histos("2J", Cuts.one_muon * Cuts.n_jets(2), samps, out_dir)
        # make_histos("2J1T", Cuts.final(2, 1), samps, out_dir)
    if doPlots:
        cut_name = "2J"
        fnames = glob.glob(out_dir + "/%s/WJets_flavour_fracs__*.root" % cut_name)
        files = {}
        hists = {}
        for fn in fnames:
            files[re.match(".*__(.*).root", fn).group(1)] = File(fn)
        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)