Example #1
0
def draw_all():
    f_in = root.TFile.Open('merged.root')
    t_in = f_in.Get('events')
    f_out = root.TFile.Open('hists.root', 'RECREATE')
    weight_strs = []
    for idx in xrange(len(weights)):
        if idx == 0:
            weight_str = 'normalizedWeight'
        else:
            weight_str = 'normalizedWeight*fabs(weights[%i])' % (idx - 1)
        weight_strs.append(weight_str)

    n_weights = len(weight_strs)
    n_per = 50
    nominal_arr = None
    for iw in xrange(0, n_weights, n_per):
        PInfo(sname, 'Extracting %i -> %i' % (iw, iw + n_per))

        xarr = read_tree(t_in, ['genBosonPt'] + weight_strs[iw:iw + n_per])

        for idx in xrange(iw, iw + n_per):
            if idx == n_weights:
                break
            h = hbase.Clone()
            weight_str = weight_strs[idx]
            weight_name = weights[idx]
            draw_hist(h, xarr, ['genBosonPt'], weight_str)
            f_out.WriteTObject(h, 'h_' + weight_name)

    f_out.Close()
    f_in.Close()
Example #2
0
def draw(s, variables):
    xarr1 = read_tree(t_in1, variables + [weight1[s]], cut1[s])
    xarr2 = read_tree(t_in2, variables + [weight2[s]], cut2[s])
    ret = {}
    for var in variables:
        h1 = hbase[var].Clone(s + '_1')
        h2 = hbase[var].Clone(s + '_2')
        draw_hist(h1, xarr1, [var], weight1[s])
        draw_hist(h2, xarr2, [var], weight2[s])
        # h1.SetMaximum(h1.GetMaximum()*2)
        # h2.SetMaximum(h2.GetMaximum()*2)
        ret[var] = {1: h1, 2: h2}
    return ret
Example #3
0
def draw(order):
    if order == 'nlo':
        f = f_nlo
        weightstr = tTIMES('normalizedWeight', nlo_corrections[args.process])
    else:
        f = f_lo
        weightstr = 'normalizedWeight'
    cutstr = tAND(selections[args.selection], proc_cuts[args.process])

    h = hbase.Clone('h_' + order)
    xarr = read_files([f], [ptstr, weightstr], cutstr, treename='Events')
    draw_hist(h, xarr, [ptstr], weightstr)
    print order, h.Integral(), cutstr

    return h
Example #4
0
def build_hists(cut,weight,observable):
    h = {}
    weight = weight%1
    cut = tAND(cut,add_cut)
    weights = {
            '13TeV' : weight,
            '8TeV' : weight.replace('sf_tt','sf_tt8TeV'),
            'none' : weight.replace('sf_tt','1')
    }
    xarr = read_tree(tree,[observable]+weights.values(),cut)
    for label,weight in weights.iteritems():
        h[label] = hbase.Clone(label+'_'+observable)
        draw_hist(h[label],xarr,[observable],weight)
        divide_bin_width(h[label])

    return h
Example #5
0
def draw(s):
    weight_ = weight
    cut_ = cut
    to_plot_ = [x[0] for x in to_plot]
    for k,v in repl[s].iteritems():
        cut_ = cut_.replace(k,v)
        for i in xrange(len(to_plot_)):
            to_plot_[i] = to_plot_[i].replace(k,v)
    cut_ = cut_.replace('dphipfmetUp','dphipfmet')
    cut_ = cut_.replace('dphipfmetDown','dphipfmet')
    xarr = read_tree(tree=t_in,branches=to_plot_+[weight_],cut=cut_)
    ret = {}
    for name,shifted_name in zip(to_plot,to_plot_):
        h = hbase[name[0]].Clone(shifted_name)
        draw_hist(h,xarr,[shifted_name],weight_)
        ret[name[0]] = h
    return ret
Example #6
0
def draw_all():
    f_in = root.TFile.Open('merged.root')
    t_in = f_in.Get('events')
    f_out = root.TFile.Open('hists.root', 'RECREATE')
    weight_strs = []
    for idx in xrange(len(weights)):
        if idx == 0:
            weight_str = 'normalizedWeight'
        else:
            weight_str = 'normalizedWeight*weights[%i]' % (idx - 1)
        weight_strs.append(weight_str)
    xarr = read_tree(t_in, ['genBosonPt'] + weight_strs)

    for idx in xrange(len(weights)):
        h = hbase.Clone()
        weight_str = weight_strs[idx]
        weight_name = weights[idx]
        draw_hist(h, xarr, ['genBosonPt'], weight_str)
        f_out.WriteTObject(h, 'h_' + weight_name)

    f_out.Close()
    f_in.Close()
Example #7
0
 def __draw(self, proc, weight_map, xarr):
     for dist in self._distributions:
         weights_nominal = xarr[weight_map['nominal']]
         draw_hist(hist=dist.histograms[proc.name],
                   xarr=xarr,
                   fields=(dist.formula, ),
                   weight=weight_map['nominal'])
         if proc.process_type != root.kData:
             for syst in self._systematics:
                 if proc.use_common_weight:
                     weights_up = weight_map['%s_Up' % syst.name]
                     weights_down = weight_map['%s_Down' % syst.name]
                 else:
                     weights_up = weight_map['nominal']
                     weights_down = weight_map['nominal']
                 draw_hist(hist=dist.systs[syst.name][0],
                           xarr=xarr,
                           fields=(dist.formula, ),
                           weight=weights_up)
                 draw_hist(hist=dist.systs[syst.formula][1],
                           xarr=xarr,
                           fields=(dist.formula, ),
                           weight=weights_down)
Example #8
0
    def draw_all(self, outdir):
        if not self.canvas.HasLegend():
            self.canvas.InitLegend()

        f_out = root.TFile(outdir+'hists.root', 'UPDATE')
        if f_out.IsZombie():
            f_out.close()
            f_out = root.TFile(outdir+'hists.root', 'RECREATE')
        f_buffer_path = '/tmp/%s/buffer_%i.root'%(getenv('USER'), root.gSystem.GetPid())
        f_buffer = root.TFile(f_buffer_path, 'RECREATE')
        f_buffer.cd()

        variables = []
        for dist in self.__distributions:
            for syst in self.__systematics:
                syst.hists = dist.generate_syst(syst.name)

            for proc in self.__processes:
                dist.generate_hist(proc.name)

            variables.append(dist.name)

        # loop through each process
        for proc in self.__processes:
            # figure out the nominal weight and cut strings
            final_weight = '1'
            final_cut = self.cut
            if (proc.process_type!=root.kData and proc.use_common_weight):
                final_weight = self.mc_weight
            if (proc.process_type<=root.kSignal3 and proc.process_type!=root.kData):
                final_weight = tTIMES(final_weight, str(self.signal_scale))
            if self.eventmod:
                if (p.process_type==root.kData):
                    final_cut = tAND(final_cut, '(%s%%%i)==0'%(self.eventnumber, self.eventmod))
                else:
                    final_weight = tTIMES(final_weight, str(1./self.eventmod))
            final_cut = tAND(final_cut, proc.additional_cut)
            final_weight = tTIMES(final_weight, proc.additional_weight)

            weight_map = {'nominal' : final_weight}
            weights = [final_weight]
            if proc.process_type!=root.kData:
                for syst in self.__systematics:
                    if proc.use_common_weight:
                        up_weight = syst.generate_weight(final_weight, True)
                        down_weight = syst.generate_weight(final_weight, False)
                    else:
                        continue
                    weight_map['%s_Up'%(syst.name)] = up_weight
                    weight_map['%s_Down'%(syst.name)] = down_weight
                    weights.append(up_weight)
                    weights.append(down_weight)
            
            xarr = proc.read(variables, weights, final_cut)

            for dist in self.__distributions:
                vals = xarr[dist.name]
                weights_nominal = xarr[weight_map['nominal']]
                draw_hist(hist = dist.histograms[proc.name], 
                          xarr = xarr, 
                          fields = (dist.name, ), 
                          weight = weight_map['nominal'])
                if proc.process_type!=root.kData:
                    for syst in self.__systematics:
                        if proc.use_common_weight:
                            weights_up = weight_map['%s_Up'%syst.name]
                            weights_down = weight_map['%s_Down'%syst.name]
                        else:
                            weights_up = weight_map['nominal']
                            weights_down = weight_map['nominal']
                        draw_hist(hist = dist.systs[syst.name][0], 
                                  xarr = xarr, 
                                  fields = (dist.name, ), 
                                  weight = weights_up)
                        draw_hist(hist = dist.systs[syst.name][1], 
                                  xarr = xarr, 
                                  fields = (dist.name, ), 
                                  weight = weights_down)

        # everything is filled,  now draw the histograms!
        for dist in self.__distributions:
            if dist.name=="1":
                totals = {'bg':0,  'sig':0,  'data':0}
                errs = {'bg':0,  'sig':0,  'data':0}
                table_format = '%-25s | %15f +/- %15f'

                table = ['%-25s | %15s +/- %15s'%('Process', 'Yield', 'Stat. unc.')]
                table.append("=================================================================")

                for proc in self.__processes:
                    h = dist.histograms[proc.name]
                    integral = h.GetBinContent(1)
                    error = h.GetBinError(1)
                    table.append(table_format%(proc.name, integral, error))
                    if proc.process_type==root.kData:
                        proc_label = 'data'
                    elif proc.process_type<=root.kSignal3:
                        proc_label = 'sig'
                    else:
                        proc_label = 'bg'
                    totals[proc_label] += integral
                    errs[proc_label] += pow(error, 2)

                for k, v in errs.iteritems():
                    errs[k] = np.sqrt(v)

                table.append("=================================================================")
                table.append(table_format%('MC(bkg)', totals['bg'], errs['bg']))
                table.append(table_format%('MC(sig)', totals['sig'], errs['sig']))
                table.append(table_format%('Data', totals['data'], errs['data']))

                table.append("=================================================================")
                if totals['bg']:
                    table.append('S/B=%.3f, S/sqrtB=%.3f'%(totals['sig']/totals['bg'], 
                                                           totals['sig']/np.sqrt(totals['bg'])))

                with open(outdir+'yields.txt', 'w') as fyields:
                    fyields.write('\n'.join(table))

                for t in table:
                    PInfo('plot_utility.PlotUtility.Dump', t)

            h_unscaled = {'data':None, 'mc':None} # used for chi2 calc
            for proc in self.__processes:
                h = dist.histograms[proc.name]
                if self.do_overflow:
                    fix_overflow(h)
                if self.do_underflow:
                    fix_underflow(h)
                if proc.process_type==root.kData:
                    h_unscaled['data'] = h.Clone()
                elif proc.process_type>root.kSignal3:
                    if not h_unscaled['mc']:
                        h_unscaled['mc'] = h.Clone()
                    else:
                        h_unscaled['mc'].Add(h)
                if dist.is_variable:
                    divide_bin_width(h)
                if dist.ybounds:
                    h.SetMinimum(ybounds[0])
                    h.SetMaximum(ybounds[1])
                h.GetXaxis().SetTitle(dist.xlabel)
                h.GetYaxis().SetTitle(dist.ylabel)
                h.SetTitle('')
                if self.canvas.IsStack():
                    h.SetLineWidth(2)
                if proc.dashed:
                    h.SetLineStyle(2)
                elif proc.dotted:
                    h.SetLineStyle(3)
                if (proc.process_type<=root.kSignal3 
                    and proc.process_type!=root.kData 
                    and self.signal_scale!=1):
                    self.canvas.AddHistogram(h, '%.1f#times%s'%(self.signal_scale, proc.name), proc.process_type)
                else:
                    self.canvas.AddHistogram(h, proc.name, proc.process_type)
                f_out.WriteTObject(h, h.GetName(), "overwrite")
            for syst in self.__systematics:
                hup, hdown = dist.systs[syst.name]
                for h in [hup, hdown]:
                    if self.do_overflow:
                        fix_overflow(h)
                    if self.do_underflow:
                        fix_underflow(h)
                    if dist.is_variable:
                        divide_bin_width(h)
                    h.SetLineWidth(3)
                    h.SetLineColor(syst.color)
                self.canvas.AddSystematic(hup, 'hist', syst.name)
                self.canvas.AddSystematic(hdown, 'hist')
                f_out.WriteTObject(hup, hup.GetName(), "overwrite")
                f_out.WriteTObject(hdown, hdown.GetName(), "overwrite")

            # output the canvas
            if dist.calc_chi2:
                p = h_unscaled['data'].Chi2Test(h_unscaled['mc'],'UW')
                self.canvas.AddPlotLabel('P(#chi^{2}|NDoF)=%.3g'%(p),0.6,0.5,False,42,.04)
            self.canvas.Logy(False)
            self.canvas.Draw(outdir, dist.filename)
            self.canvas.ClearLegend()
            if dist.calc_chi2:
                p = h_unscaled['data'].Chi2Test(h_unscaled['mc'],'UW')
                self.canvas.AddPlotLabel('P(#chi^{2}|NDoF)=%.3g'%(p),0.6,0.5,False,42,.04)
            self.canvas.Logy(True)
            self.canvas.Draw(outdir, dist.filename+'_logy')

            self.canvas.Reset(False)

        f_out.Close()
        f_buffer.Close()
        system('rm %s'%f_buffer_path)
Example #9
0
        hratio.SetBinError(ib,e_num/v_den)
    return hratio

def build_hists(cut,weight,observable):
    h = {}
    weight = weight%1
    cut = tAND(cut,add_cut)
    weights = {
            '13TeV' : weight,
            '8TeV' : weight.replace('sf_tt','sf_tt8TeV'),
            'none' : weight.replace('sf_tt','1')
    }
    xarr = read_tree(tree,[observable]+weights.values(),cut)
    for label,weight in weights.iteritems():
        h[label] = hbase.Clone(label+'_'+observable)
        draw_hist(h[label],xarr,[observable],weight)
        divide_bin_width(h[label])

    return h



h_sig = build_hists(sel.cuts['signal'],sel.weights['signal'],'pfmet')
h_con = build_hists(sel.cuts['singlemuontop'],sel.weights['singlemuontop'],'pfUWmag')

for k in h_sig:
    h_sig[k].Divide(h_con[k])

h_sig['8TeV'] = divide(h_sig['8TeV'],h_sig['13TeV'])
h_sig['none'] = divide(h_sig['none'],h_sig['13TeV'])
h_sig['13TeV'] = divide(h_sig['13TeV'],h_sig['13TeV'])
def interpolate():
    f_interp_path = basedir + '/interpolate/hists_couplings/%s.root' % (
        args.mass)
    f_interp = root.TFile.Open(f_interp_path)

    PInfo(sname, 'We want to interpolate point %s' % args.mass)

    if not args.mass_reco:
        m_V, m_DM = [int(x) for x in args.mass.split('_')]
        offshell = (2 * m_DM >= m_V)
        frescos = map(
            lambda x: x.split('/')[-1].replace('.root', '').replace(
                'fittingForest_signal_vector_', ''),
            glob(basedir + '/signals_3/fittingForest_signal_vector*root'))
        frescos = map(lambda x: tuple(map(lambda y: int(y), x.split('_'))),
                      frescos)
        m_V_recos = [x[0] for x in frescos]
        m_V_reco = min(m_V_recos, key=lambda x: abs(m_V - x))

        m_DM_recos = [x[1] for x in frescos if x[0] == m_V_reco]
        m_DM_recos = [x for x in m_DM_recos if ((2 * x >= m_V) == offshell)]
        if len(m_DM_recos) == 0:
            m_DM_recos = [x[1] for x in frescos if x[0] == m_V_reco]
        m_DM_reco = min(m_DM_recos, key=lambda x: abs(m_DM - x))

        args.mass_reco = '%i_%i' % (m_V_reco, m_DM_reco)

    PInfo(sname,
          'We have chosen as the reconstructed point %s' % args.mass_reco)

    f_reco_path = basedir + '/signals_3/fittingForest_signal_vector_%s.root' % (
        args.mass_reco)
    f_reco = root.TFile.Open(f_reco_path)
    t_reco = f_reco.Get('%s_signal' % (args.mass_reco))
    f_reco_gen_path = basedir + '/interpolate/hists/%s.root' % (args.mass_reco)
    f_reco_gen = root.TFile.Open(f_reco_gen_path)

    system('mkdir -p %s/interpolate/interpolated_couplings2test/' % basedir)
    t_out = t_reco.Clone()
    metname = 'min(met,999.9999)'
    branches = [metname]
    interp2reco = {}
    for g in couplings_interp:
        # find the reco g
        # g_reco = min(couplings_reco, key = lambda x : euclid(parse_coupling_name(g), parse_coupling_name(x)))
        g_reco = 'nominal'  # override
        PInfo(sname, 'Interpolating %s with %s' % (g, g_reco))
        interp2reco[g] = g_reco
        h_interp = f_interp.Get('h_%s' % g)
        h_reco_gen = f_reco_gen.Get('h_%s' % g_reco)
        h_ratio = h_interp.Clone()
        h_ratio.Divide(h_reco_gen)
        ba.newBranchName = 'interp_%s' % g
        ba.AddBranchFromHistogram(t_reco, h_ratio)
        if g_reco == 'nominal':
            weightname = 'weight*interp_%s' % g
        else:
            weightname = 'weight*%s*interp_%s' % (g_reco.replace(
                '_nlo', '').replace('rw_', ''), g)
        branches.append(weightname)
        for s in systs:
            branches.append(weightname.replace('weight', s))
    xarr = {}
    xarr['tight'] = read_tree(t_reco, branches, 'top_ecf_bdt>0.45 && met>250')
    xarr['loose'] = read_tree(
        t_reco, branches, 'top_ecf_bdt>0.1 && top_ecf_bdt<0.45 && met>250')

    f_out = root.TFile(
        '%s/interpolate/interpolated_couplings2test/interp_%s.root' %
        (basedir, args.mass), 'RECREATE')
    PInfo(
        sname,
        'Creating %s/interpolate/interpolated_couplings2test/interp_%s.root' %
        (basedir, args.mass))
    for g in couplings_interp:
        f_out.cd()
        folder = f_out.mkdir(g)
        h_templates = {}
        g_reco = interp2reco[g]
        if g_reco == 'nominal':
            weightname = 'weight*interp_%s' % g
        else:
            weightname = 'weight*%s*interp_%s' % (g_reco.replace(
                '_nlo', '').replace('rw_', ''), g)
        for t in ['tight', 'loose']:
            h_templates[t] = h_base.Clone()
            draw_hist(h_templates[t], xarr[t], [metname], weightname)
            for s in systs:
                hname = '%s_%s' % (t, s)
                h_templates[hname] = h_base.Clone()
                draw_hist(h_templates[hname], xarr[t], [metname],
                          weightname.replace('weight', s))
        for k, h in h_templates.iteritems():
            folder.WriteTObject(h, 'h_%s' % (k))
    f_out.Close()
Example #11
0
def draw(tree,weight,cut):
    h = hbase.Clone()
    xarr = read_tree(tree,['met',weight],cut)
    draw_hist(h,xarr,['met'],weight)
    return h
Example #12
0
def get_hist(cut):
    h = hbase.Clone()
    xarr = read_tree(events,['pfmetnomu'],cut)
    draw_hist(h,xarr,['pfmetnomu'],None)
    return h