コード例 #1
0
def nevents_plot():
    in_f = ROOT.TFile(gp.fn)
    out_f = ROOT.TFile('nevents.root', 'recreate')
    kinds, masses, taus, nmasses, ntaus = axes(in_f)

    for kind in kinds:
        h = ROOT.TH2D('nevents_%s' % kind, ';mass (GeV);#tau (mm)', nmasses, masses, ntaus, taus)

        for ibin in xrange(1, nmasses+1):
            mass = h.GetXaxis().GetBinLowEdge(ibin)
            for jbin in xrange(1, ntaus+1):
                tau = h.GetYaxis().GetBinLowEdge(jbin)

                try:
                    isample = name2isample(in_f, details2name(kind, tau, mass))
                except ValueError:
                    continue

                nev = nevents(in_f, isample)

                h.SetBinContent(ibin, jbin, nev)
                h.SetBinError  (ibin, jbin, nev**0.5)

        out_f.cd()
        h.Write()

    out_f.Write()
    out_f.Close()
コード例 #2
0
def event_scale_factors():
    from signal_efficiency import SignalEfficiencyCombiner
    for which, years in ('2017p8', [2017,2018]), :
        combiner = SignalEfficiencyCombiner(years)
        in_f = combiner.inputs[0].f
        out_f = ROOT.TFile('event_TrackMover_scale_factors_%s.root' % which, 'recreate')

        kinds, masses, taus, nmasses, ntaus = axes(in_f)
        taus.remove(30.)
        ntaus -= 1

        for kind in kinds:
            h = ROOT.TH2D('event_TrackMover_scale_factors_%s' % kind, ';mass (GeV);#tau (mm)', nmasses, masses, ntaus, taus)

            for ibin in xrange(1, nmasses+1):
                mass = h.GetXaxis().GetBinLowEdge(ibin)
                for jbin in xrange(1, ntaus+1):
                    tau = h.GetYaxis().GetBinLowEdge(jbin)
                    pt = kind, tau, mass

                    try:
                        name = details2name(*pt)
                        isample = name2isample(in_f, name)
                    except ValueError as exc:
                        if tau < 100:
                            print colors.warning('problem getting %r : isample %r exc %s' % (pt, isample, exc))
                        continue # leave holes in the plot to know which samples are missing

                    event_SF = None
                    if which == '2017p8' :
                        vtx_SF_2017 = sig_datamcSF_2017p8(name+"_2017")
                        vtx_SF_2018 = sig_datamcSF_2017p8(name+"_2018")
                        event_SF_2017 = vtx_SF_2017**2
                        event_SF_2018 = vtx_SF_2018**2
                        lumi2017 = gp.int_lumis[gp.years.index("2017")]
                        lumi2018 = gp.int_lumis[gp.years.index("2018")]
                        event_SF = (event_SF_2017*lumi2017 + event_SF_2018*lumi2018) / (lumi2017+lumi2018)
                        
                        # for table of SFs, with bins 0.1--0.3\mm & 0.3--1\mm & 1--10\mm & 10--100\mm,
                        # take the ~midpoint of each bin and put its SF in the table (averaged where necessary)
                        if mass == 1600 :
                            if tau == 0.2 or tau == 0.6 or tau == 0.7 or tau == 4 or tau == 7 or tau == 55 :
                                print("for SF table:",name, round(event_SF, 4))

                    else : 
                        sys.exit('event_scale_factors not set up to handle %s! Exiting.' % which)

                    h.SetBinContent(ibin, jbin, event_SF)
                    h.SetBinError  (ibin, jbin, 0) # JMTBAD

            out_f.cd()
            h.Write()

        out_f.Write()
        out_f.Close()
コード例 #3
0
def make():
    def warning():
        return
        for i in xrange(20):
            print colors.error("don't forget: anything?")

    warning()

    assert not os.path.exists(gp.fn)
    ROOT.TH1.AddDirectory(0)
    f = ROOT.TFile(gp.fn, 'recreate')

    make_bkg(f)

    name_list = {}
    make_signals_2015p6(f, name_list)
    make_signals_2017p8(f, name_list)

    title = name_list.pop('title')
    isamples = sorted([s.isample for s in name_list.itervalues()],
                      reverse=True)
    assert isamples == range(-1, isamples[-1] - 1, -1)

    nsigs = len(isamples)
    h = ROOT.TH1C('name_list', title, nsigs, 0, nsigs)
    missing = defaultdict(list)
    for s in name_list.itervalues():
        miss = tuple(sorted(set(gp.years) - s.years))
        missing[miss].append(s.name)
        c = sum((int(year in s.years) << y) for y, year in enumerate(gp.years))
        h.GetXaxis().SetBinLabel(-s.isample, s.name)
        h.SetBinContent(-s.isample, c)
    f.cd()
    h.Write()
    f.Close()

    if missing:
        w = colors.warning
        print w('missing summary:')
        for k in sorted(missing):
            if not k:
                continue
            l = sorted(missing[k])
            n = len(l)
            print w(
                repr(tuple(int(y) for y in sorted(k))) + ': %i samples' % n)
            for i in xrange(0, n, 6):
                for j in xrange(i, min(i + 6, n)):
                    print w('    ' + l[j]),
                print

    warning()
コード例 #4
0
def draw():
    ps = plot_saver(plot_dir('o2t_templates_run2'), size=(600,600))
    f = ROOT.TFile(gp.fn)

    whiches = [
        ('multijet', [
            (name2isample(f, 'mfv_neu_tau00100um_M0800'), ROOT.kRed,      'multijet M = 800 GeV, #tau = 100 #mum'),
            (name2isample(f, 'mfv_neu_tau00300um_M0800'), ROOT.kGreen+2,  'multijet M = 800 GeV, #tau = 300 #mum'),
            (name2isample(f, 'mfv_neu_tau01000um_M0800'), ROOT.kBlue,     'multijet M = 800 GeV, #tau = 1 mm'),
            (name2isample(f, 'mfv_neu_tau10000um_M0800'), ROOT.kMagenta,  'multijet M = 800 GeV, #tau = 10 mm'),
            (name2isample(f, 'mfv_neu_tau30000um_M0800'), ROOT.kOrange+2, 'multijet M = 800 GeV, #tau = 30 mm'),
            ]),
        ('dijet', [
            (name2isample(f, 'mfv_ddbar_tau00100um_M0800'), ROOT.kRed,      'dijet M = 800 GeV, #tau = 100 #mum'),
            (name2isample(f, 'mfv_ddbar_tau00300um_M0800'), ROOT.kGreen+2,  'dijet M = 800 GeV, #tau = 300 #mum'),
            (name2isample(f, 'mfv_ddbar_tau01000um_M0800'), ROOT.kBlue,     'dijet M = 800 GeV, #tau = 1 mm'),
            (name2isample(f, 'mfv_ddbar_tau10000um_M0800'), ROOT.kMagenta,  'dijet M = 800 GeV, #tau = 10 mm'),
            (name2isample(f, 'mfv_ddbar_tau30000um_M0800'), ROOT.kOrange+2, 'dijet M = 800 GeV, #tau = 30 mm'),
            ]),
        ]

    def fmt(h, name, color, save=[]):
        binning = to_array(0., 0.04, 0.07, 0.15)
        h = h.Rebin(len(binning)-1, name, binning)
        h.Sumw2()
        h.SetStats(0)
        h.SetLineWidth(3)
        h.SetLineColor(color)
        h.SetTitle(';d_{VV} (cm);event rate (unit norm.)')
        move_overflow_into_last_bin(h)
        h.Scale(1./h.Integral(0,h.GetNbinsX()+2))
        save.append(h)
        return h
    
    for which_name, which in whiches: 
        hbkg = fmt(f.Get('h_bkg_dvv'), 'bkg', ROOT.kBlack)
        hbkg.Draw('hist e')
        hbkg.GetYaxis().SetRangeUser(0,1.5)

        leg = ROOT.TLegend(0.142, 0.657, 0.702, 0.857)
        leg.SetBorderSize(0)
        leg.AddEntry(hbkg, 'Background template d_{VV}^{C}', 'LE')

        for isample, color, title in which:
            h = fmt(f.Get('h_signal_%i_dvv' % isample), title, color)
            h.Draw('hist e same')
            leg.AddEntry(h, title, 'LE')

        leg.Draw()

        ps.save(which_name, log=False)
コード例 #5
0
    def __init__(self,
                 data_fn,
                 mc_fn,
                 data_path='pileup',
                 mc_path='PileupDist/h_npu',
                 tol=1e-9,
                 raise_tol=True):
        from JMTucker.Tools.ROOTTools import ROOT

        self.data_f = ROOT.TFile(data_fn)
        self.mc_f = ROOT.TFile(mc_fn)
        self.data_h = self.data_f.Get(data_path)
        self.mc_h = self.mc_f.Get(mc_path)

        def norm(h):
            h = h.Clone(h.GetName() + '_norm')
            h.Sumw2()
            h.Scale(1 / h.Integral(1, h.GetNbinsX() + 1))
            return h

        self.data_h_orig = self.data_h
        self.mc_h_orig = self.mc_h

        self.data_h = norm(self.data_h)
        self.mc_h = norm(self.mc_h)

        self.ndata = self.data_h.GetNbinsX()
        assert self.ndata == self.mc_h.GetNbinsX()  # JMTBAD

        self.weights = []

        for i in xrange(1, self.ndata + 1):
            d = self.data_h.GetBinContent(i)
            m = self.mc_h.GetBinContent(i)
            w = -1
            if m == 0:
                if d > tol:
                    msg = 'm == 0 and d = %g > tol=%g for i = %i' % (d, tol, i)
                    if raise_tol:
                        raise ValueError(msg)
                    else:
                        print msg
                w = 0
            else:
                w = d / m
            self.weights.append(w)

        while self.weights[-1] == 0:
            self.weights.pop()
コード例 #6
0
def points(f=None):
    if f is None:
        f = ROOT.TFile(gp.fn)
    kinds  = sorted(set(s.kind for s in sample_iterator(f)))
    masses = sorted(set(s.mass for s in sample_iterator(f)))
    taus   = sorted(set(s.tau  for s in sample_iterator(f)))
    return kinds, masses, taus
コード例 #7
0
def signal_efficiency():
    from signal_efficiency import SignalEfficiencyCombiner
    for which, years in ('run2', [2016, 2017, 2018]), ('2017p8', [2017, 2018]):
        combiner = SignalEfficiencyCombiner(years)
        in_f = combiner.inputs[0].f
        out_f = ROOT.TFile('signal_efficiency_%s.root' % which, 'recreate')

        kinds, masses, taus, nmasses, ntaus = axes(in_f)
        taus.remove(30.)
        ntaus -= 1

        for kind in kinds:
            h = ROOT.TH2D('signal_efficiency_%s' % kind,
                          ';mass (GeV);#tau (mm)', nmasses, masses, ntaus,
                          taus)

            for ibin in xrange(1, nmasses + 1):
                mass = h.GetXaxis().GetBinLowEdge(ibin)
                for jbin in xrange(1, ntaus + 1):
                    tau = h.GetYaxis().GetBinLowEdge(jbin)
                    pt = kind, tau, mass

                    isample, r = None, None
                    try:
                        isample = name2isample(in_f, details2name(*pt))
                        r = combiner.combine(isample)
                    except ValueError as exc:
                        if tau < 100:
                            print colors.warning(
                                'problem getting %r : isample %r exc %s' %
                                (pt, isample, exc))
                        continue  # leave holes in the plot to know which samples are missing

                    h.SetBinContent(ibin, jbin, r.total_sig_eff)
                    h.SetBinError(ibin, jbin, 0)  # JMTBAD

            out_f.cd()
            h.Write()

        out_f.Write()
        out_f.Close()
コード例 #8
0
 def draw(self, fn):
     from JMTucker.Tools.ROOTTools import ROOT, draw_in_order, differentiate_stat_box
     self.data_h.SetLineColor(ROOT.kBlack)
     self.mc_h.SetLineColor(ROOT.kRed)
     self.data_h.SetLineWidth(2)
     self.mc_h.SetLineWidth(2)
     c = ROOT.TCanvas('c', '', 1000, 800)
     draw_in_order([[self.data_h, self.mc_h], ''], sames=True)
     c.Update()
     differentiate_stat_box(self.mc_h)
     if '.' in os.path.basename(fn):
         c.SaveAs(fn)
     else:
         c.SaveAs(fn + '.png')
         c.SaveAs(fn + '.root')
コード例 #9
0
 def __call__(self, era, eta):
     if era == '2017D' or era == '2017E':
         era = '2017DE'
     name = era + eta
     if self.fcns.has_key(name):
         return self.fcns[name]
     offset = self.eras.index(era) * 4 + self.etas.index(eta) * 2
     pars = [float(x) for x in self.lines[offset].split('{')[1].split('}')[0].split(',')]
     fcn = self.lines[offset+1].split(' = ')[1].replace(';','').replace('p_dxy[', '[p')
     assert '[p%i]' % (len(pars)-1) in fcn        
     fcn = self.fcns[name] = ROOT.TF1('fcn_' + name, fcn, 1, 200)
     fcn.SetParameters(*pars)
     if self.debug:
         print era, eta, offset, self.lines[offset], self.lines[offset+1], len(pars), pars
     return fcn
コード例 #10
0
ファイル: process.py プロジェクト: pkotamnives/cmssw-usercode
def fromtree(fn, _jobre=re.compile(r'_(\d+)\.root$')):
    bn = os.path.basename(fn)
    f = ROOT.TFile(fn)
    ll = [x for x in detree(f.Get('limit'), 'limit:iToy', xform=(float, int))]
    if bn.startswith('expected'):
        mo = _jobre.search(bn)
        if mo:
            njobsexp = 1
            ntoysexp = ntoysperjob
        else:
            njobsexp = njobs
            ntoysexp = njobs * ntoysperjob

        if len(
                ll
        ) == 2 * ntoysexp:  # for some reason, there are two entries for every iToy when --saveToys used
            ll2 = []
            for mi, (l, i) in enumerate(ll):
                assert (mi / 2 % ntoysperjob) + 1 == i
                if mi % 2 == 1:
                    ll2.append(l)
                elif mi % 2 * ntoysperjob == 0:  # and the limit value saved in the first entry for the first toy in each batch is ~1e-300!
                    assert l < 1e-300
                elif mi > 0:
                    assert l == ll2[-1]
            assert len(ll2) == ntoysexp
            return ll2
        else:
            if len(ll) != ntoysexp:
                raise ValueError('fromtree %s failed: len(ll) = %i != %i' %
                                 (fn, len(ll), ntoysexp))
            jobordercheck = [b for _, b in ll]
            jobordershouldbe = range(1, ntoysperjob + 1) * njobsexp
            if jobordercheck != jobordershouldbe:
                raise ValueError('fromtree %s failed: jobordercheck %r != $r' %
                                 (fn, jobordercheck, jobordershouldbe))
            return [a for a, _ in ll]
    else:
        assert bn == 'observed.root' and len(ll) == 1 and ll[0][1] == 0
        return [ll[0][0]]
コード例 #11
0
            h.Write()

        out_f.Write()
        out_f.Close()

if __name__ == '__main__':
    if 'make' in sys.argv:
        make()
    elif 'uncert' in sys.argv:
        sig_uncert_2017p8(sys.argv[sys.argv.index('uncert')+1], debug=True)
    elif 'draw' in sys.argv:
        draw()
    elif 'compare' in sys.argv:
        compare(*sys.argv[2:5])
    elif 'ngen' in sys.argv:
        f = ROOT.TFile(gp.fn)
        years = sys.argv[sys.argv.index('ngen'):]
        for s in sample_iterator(f, years, slices_1d=True):
            print '%6i' % s.isample, s.name.ljust(35), ' '.join(['%10s']*gp.nyears) % tuple(ngen(f, s.isample, year) for year in gp.years)
        #nevents_plot()
    elif 'points' in sys.argv:
        print 'kinds = %r\nmasses = %r\ntaus = %r' % points()
    elif 'iter' in sys.argv:
        include_2016 = 'include_2016' in sys.argv
        years = ('2016','2017','2018') if include_2016 else ('2017','2018')
        samples = sample_iterator(ROOT.TFile('limitsinput.root'),
                                  require_years=years,
                                  test='test_batch' in sys.argv,
                                  slices_1d='slices_1d' in sys.argv,
                                  )
        names = set(s.name for s in samples)
コード例 #12
0
def phi(v):
    return atan2(v[1], v[0])

def dphi(v0, v1):
    phi0 = phi(v0)
    phi1 = phi(v1)
    res = phi0 - phi1
    while res > pi:
        res -= 2*pi
    while res <= -pi:
        res += 2*pi
    return res

h_y_x = []
for i in xrange(0,10):
    h_y_x.append(ROOT.TH2F('svdist2d_%i'%i, '%i um <= svdist2d < %i um;x (cm);y (cm)'%(100*i,100*(i+1)), 200, -0.1, 0.1, 200, -0.1, 0.1))

h_dz = ROOT.TH1F('h_dz', '', 100, -0.2, 0.2)
h_dphi = ROOT.TH1F('h_phi', '', 100, -pi, pi)

h_y_x_g = [[] for x in xrange(10)]
h_dzs = [ROOT.TH1F('h_dz_%i' % x, '', 100, -0.2, 0.2) for x in xrange(10)]
h_dphis = [ROOT.TH1F('h_dphi_%i' % x, '', 100, -pi, pi) for x in xrange(10)]

for sample, ntracks01, svdist2d, v0, v1 in events:
    svdist2d *= 10000
    bin = int(svdist2d / 100)
    #print svdist2d, bin, event[3][0]-0.244, event[3][1]-0.3928, event[4][0]-0.244, event[4][1]-0.3928

    dz = v0[-1] - v1[-1]
    v0 = debs(v0)
コード例 #13
0
 def make_h_fcn(nm, fcn):
     h = ROOT.TH1D(nm, '%s - %s' % (fn1, fn2), nnames, 0, nnames)
     h_fcns.append((h, fcn))
     return h
コード例 #14
0
def compare(fn1, fn2, outbase):
    f1, f2 = fs = ROOT.TFile.Open(fn1), ROOT.TFile.Open(fn2)
    samples_1 = dict((name, isample) for isample, name in isamplename_iterator(f1))
    samples_2 = dict((name, isample) for isample, name in isamplename_iterator(f2))
    names_1 = set(samples_1)
    names_2 = set(samples_2)

    names = sorted(names_1 & names_2)
    nnames = len(names)
    if not names:
        print 'no samples in common between %s and %s' % (fn1, fn2)
        return
    else:
        print '%i samples in common' % nnames

    if names_1 != names_2:
        print '# samples only in %s: %i' % (fn1, len(names_1 - names_2))
        #pprint(sorted(names_1 - names_2))
        print '# samples only in %s: %i' % (fn2, len(names_2 - names_1))
        #pprint(sorted(names_2 - names_1))

    h_fcns = []
    def make_h_fcn(nm, fcn):
        h = ROOT.TH1D(nm, '%s - %s' % (fn1, fn2), nnames, 0, nnames)
        h_fcns.append((h, fcn))
        return h

    h_eff1v = make_h_fcn('eff1v', eff1v)
    h_eff2v = make_h_fcn('eff2v', eff2v)
    h_dbvmean = make_h_fcn('dbvmean', lambda f,isample: (h_dbv(f,isample).GetMean(), h_dbv(f,isample).GetMeanError()))
    h_dbvrms  = make_h_fcn('dbvrms',  lambda f,isample: (h_dbv(f,isample).GetRMS(),  h_dbv(f,isample).GetRMSError() ))
    h_dvvmean = make_h_fcn('dvvmean', lambda f,isample: (h_dvv(f,isample).GetMean(), h_dvv(f,isample).GetMeanError()))
    h_dvvrms  = make_h_fcn('dvvrms',  lambda f,isample: (h_dvv(f,isample).GetRMS(),  h_dvv(f,isample).GetRMSError() ))

    all_stat_same, all_same = True, True

    for iname, name in enumerate(names):
        isamples = samples_1[name], samples_2[name]
        for h, fcn in h_fcns:
            (v1,e1), (v2, e2) = [fcn(f, isample) for f,isample in zip(fs, isamples)]
            h.GetXaxis().SetBinLabel(iname+1, name)
            d = v1 - v2
            e = (e1**2 + e2**2)**0.5
            h.SetBinContent(iname+1, d)
            h.SetBinError  (iname+1, e)
            if abs(d) > 1e-5:
                all_same = False
            if abs(d) > e:
                all_stat_same = False

    c = ROOT.TCanvas('c', '', 1000, 800)
    line = ROOT.TLine(0, 0, nnames, 0)
    line.SetLineStyle(2)
    line.SetLineWidth(2)

    for i,(h,_) in enumerate(h_fcns):
        h.SetStats(0)
        h.SetLineWidth(2)
        h.Draw('e')
        line.Draw()
        for ext in 'root', 'png':
            c.SaveAs(outbase + '/%i_%s.%s' % (i, h.GetName(), ext))

    print 'all same? %r statistically? %r' % (all_same, all_stat_same)
コード例 #15
0
#!/usr/bin/env python

import sys
from pprint import pprint
from JMTucker.Tools.ROOTTools import ROOT, flatten_directory, check_consistency

ROOT.TH1.AddDirectory(0)

fn1, fn2 = fns = [x for x in sys.argv[1:] if '.root' in x]
print 'comparing', fn1, fn2
f1, f2 = fs = [ROOT.TFile(fn) for fn in fns]

d1 = set(flatten_directory(f1))
d2 = set(flatten_directory(f2))

# could remap
#assert sorted(d1) == sorted(d2)

d = sorted(d1 & d2)
in1not2 = sorted(d1 - d2)
in2not1 = sorted(d2 - d1)
if 'small' in sys.argv:
    print 'comparing %i hists (and not %i+%i of them)' % (len(d), len(in1not2),
                                                          len(in2not1))
elif in1not2 or in2not1:
    print 'warning: not comparing these:'
    print 'in f1 not f2:'
    pprint(in1not2)
    print 'in f2 not f1:'
    pprint(in2not1)
    print
コード例 #16
0
def book(out_name, hs=[]):
    bins = binning()
    h = ROOT.TH1F(out_name, '', len(bins) - 1, bins)
    hs.append(h)
    return h
コード例 #17
0
        mass = float(mass)
        tau, tauunit = float(tau_s[:-2]), tau_s[-2:]
        if tauunit == 'um':
            tau *= 1e-4
        elif tauunit == 'mm':
            tau *= 1e-1

        nevents_expected = 10000
        if model == 'GluinoGluinoToNeutralinoNeutralinoTo2T2B2S':
            nevents_missing_allowed = 0
            folder_name = 'Lsps'
        elif model == 'StopStopTo2Dbar2Dbar':
            nevents_missing_allowed = 10
            folder_name = 'Hs'

        f = ROOT.TFile(fn)
        hs = [f.Get('mfvGenHistos/%s#%i/M' % (folder_name, i)) for i in 0, 1]
        ok = all(nevents_expected - h.GetEntries() <= nevents_missing_allowed
                 and abs(h.GetMean() - mass) < 0.01 for h in hs)

        h = f.Get('mfvGenHistos/h_ctaubig')
        xmax = h.GetXaxis().GetXmax()
        mean_expected = (tau - exp(-xmax / tau) *
                         (xmax + tau)) / (1 - exp(-xmax / tau))
        ok = ok and nevents_expected - h.GetEntries(
        ) / 2. <= nevents_missing_allowed and abs(
            h.GetMean() - mean_expected) < 2 * h.GetMeanError()

        print(colors.green if ok else colors.red)(
            '%s (%s %s %s) %s (%s %s) %s (%s)' %
            (fn, hs[0].GetEntries(), hs[1].GetEntries(), h.GetEntries(), mass,
コード例 #18
0
        return 6
    elif mass == 1200:
        return 7
    elif mass == 1600:
        return 8
    elif mass == 3000:
        return 9

matched = []
not_matched = []
x = []
y = []
ex = []
ey = []
gs = []
l1 = ROOT.TLegend(0.75,0.1,0.95,0.5)
l2 = ROOT.TLegend(0.75,0.5,0.95,0.9)
for sample in samples:
    if ctau not in sample.name:
        continue

    fn = os.path.join(os.path.expanduser(file_path), '%s.root' % sample.name)
    if not os.path.isfile(fn):
        print colors.red('%s is missing' % fn) + '\n'
        continue

    print sample.name

    f = ROOT.TFile(fn)
    nrec = f.Get('mfvTheoristRecipe%s/h_gen%s_dvv' % (rec_den, match)).GetEntries()
    ngen = f.Get('mfvGen%s/h_gen%s_dvv' % (gen_den, match)).GetEntries()
コード例 #19
0
def make_bkg(f):
    # the first 3 sets of values override what's in 2v_from_jets
    observed = (1,0,0), (0,0,0), (0,0,0)
    bkg_n1v  = 1183, 1303, 908
    bkg_n2v  = 1, 0.241, 0.111 # this is not supposed to be the sum of observed, but could/should be set to the pre-fit expectation (predict2v.py)
    # but bkg_c1v is checked against what's in 2v_from_jets
    bkg_c1v = (0.509, 0.374, 0.117), (0.709, 0.257, 0.034), (0.650, 0.313, 0.037)

    bkg_uncert = [(1.25, 1.25, 1.69), (1.16, 1.30, 1.51), (1.19, 1.30, 1.91)] # combine lnN convention

    import statmodel
    bkg_uncert_stat = [statmodel.ebins['data100pc_%s_5track' % year] for year in gp.years]
    for y in xrange(gp.nyears):
        bkg_uncert[y] = [(a**2 + b**2)**0.5 for a,b in zip(bkg_uncert[y], bkg_uncert_stat[y])] # JMTBAD use proper gmN?

    bkg_stat_uncert = [(1.071, 1.128, 1.465), (1.173, 1.216, 1.454), (1.217, 1.238, 1.586)] # stat errors uncorrelated
    bkg_syst_uncert = [(0.758, 1.218, 1.542), (0.743, 1.338, 1.389), (0.766, 1.315, 1.760)] # syst error anti-correlated

    def bkg_fn(year, which='default'):
        path = '/uscms/home/tucker/public/mfv/'
        if year == '2016':
            return path + '2v_from_jets_data_2015p6_5track_default_v15_v5.root'
        else:
            if which == 'c':
                which = 'btag_corrected_nom'
            else:
                assert which == 'default'
            return path + '2v_from_jets_data_%s_5track_%s_V27m.root' % (year, which)

    def bkg_f(year, which='default', _c={}):
        k = year, which
        if not _c.has_key(k):
            _c[k] = ROOT.TFile.Open(bkg_fn(*k))
        return _c[k]

    def cbkg_f(year):
        return bkg_f(year, which='c')

    ##

    print 'make_bkg:',
    for y, year in enumerate(gp.years):
        h_int_lumi = ROOT.TH1D('h_int_lumi_%s' % year, '', 1, 0, 1)
        h_int_lumi.SetBinContent(1, gp.int_lumis[y])

        h_observed = ROOT.TH1D('h_observed_%s' % year, '', gp.nbins, gp.bins)
        for i,v in enumerate(observed[y]):
            h_observed.SetBinContent(i+1, v)
    
        h_bkg_dbv = to_TH1D( bkg_f(year).Get('h_1v_dbv'),  'h_bkg_dbv_%s' % year)
        h_bkg_dvv = to_TH1D(cbkg_f(year).Get('h_c1v_dvv'), 'h_bkg_dvv_%s' % year)

        h_bkg_dbv.Scale(bkg_n1v[y]/get_integral(h_bkg_dbv)[0])
        h_bkg_dvv.Scale(bkg_n2v[y]/get_integral(h_bkg_dvv)[0])

        h = h_bkg_dvv_rebin = h_bkg_dvv.Rebin(gp.nbins, 'h_bkg_dvv_rebin_%s' % year, gp.bins)
        move_overflow_into_last_bin(h_bkg_dvv_rebin)
        for i in xrange(gp.nbins):
            if abs(bkg_c1v[y][i] - h.GetBinContent(i+1)/bkg_n2v[y]) > 0.001:
                print y,i, bkg_c1v[y][i], h.GetBinContent(i+1)/bkg_n1v[y]
                assert 0

        h_bkg_uncert = ROOT.TH1D('h_bkg_uncert_%s' % year, '', gp.nbins, gp.bins)
        h_bkg_stat_uncert = ROOT.TH1D('h_bkg_uncert_stat_%s' % year, '', gp.nbins, gp.bins)
        h_bkg_syst_uncert = ROOT.TH1D('h_bkg_uncert_syst_%s' % year, '', gp.nbins, gp.bins)
        for i,v in enumerate(bkg_uncert[y]):
            h_bkg_uncert.SetBinContent(i+1, v)
        for i,v in enumerate(bkg_stat_uncert[y]):
            h_bkg_stat_uncert.SetBinContent(i+1, v)
        for i,v in enumerate(bkg_syst_uncert[y]):
            h_bkg_syst_uncert.SetBinContent(i+1, v)

        f.cd()            
        for h in h_int_lumi, h_observed, h_bkg_dbv, h_bkg_dvv, h_bkg_dvv_rebin, h_bkg_uncert, h_bkg_stat_uncert, h_bkg_syst_uncert:
            h.SetTitle('')
            h.Write()

    print 'done'
コード例 #20
0
def make_signals_2015p6(f, name_list):
    # Pull 2015+6 values from previous-format limitsinput file(s).
    # (Not all 2017+8 signal points exist in the final one for the
    # 2016 paper, but we make new ones now using the scanpack2017p8_2016 branch.)
    # Rescale+add how SignalEfficiencyCombiner did previously.  Go
    # ahead and store the rate already scaled by the integrated
    # luminosity. This makes the 2016 weighted sum situation easier
    # downstream.

    fnpairs = [ # (hip, nonhip)--the first pair are to be the final 2016 paper one, the rest define newly generated signal points
        ('/uscms/home/tucker/public/mfv/limitsinput_data_v15v5_scanpack_merge_hip_1_2_2p6_3_3p6_removeddbar.root', 
         '/uscms/home/tucker/public/mfv/limitsinput_data_v15v5_scanpack_merge_1_1p5_2_2p5_2p7_3_3p5_removeddbar.root',),
        ('/uscms/home/tucker/public/mfv/limitsinput_scanpack1D2016missing_hip.root',
         '/uscms/home/tucker/public/mfv/limitsinput_scanpack1D2016missing.root',),
        ('/uscms/home/tucker/public/mfv/limitsinput_scanpack4p6_hip.root',
         '/uscms/home/tucker/public/mfv/limitsinput_scanpack4p6.root',),
        ]

    def trigmult2016(x):
        return 1 # this was 0.99 in signal_efficiency, but now it's already in int_lumi at top
    def sf20156(x, pars=(0.9784, -1128., 1444.)):
        return trigmult2016(x) * (2. - pars[0] * ROOT.TMath.Erf((x-pars[1])/pars[2]))
    sfs = [sf20156, trigmult2016, trigmult2016]
    int_lumis = [2592.4, 19492., 16059.]

    for ipair, (fn_2016_hip, fn_2016_nonhip) in enumerate(fnpairs):
        name_list['title'] = name_list.get('title', '') + '+%s+%s' % (fn_2016_hip, fn_2016_nonhip)
        f_2015 = f_2016 = f_2016_nonhip = ROOT.TFile.Open(fn_2016_nonhip)
        f_2016_hip = ROOT.TFile.Open(fn_2016_hip)
        h_name_list_2016 = f_2016_nonhip.Get('name_list') # already checked that the two files are in sync
        def _hs(n):
            return [f_2015.Get(n), f_2016_hip.Get(n), f_2016_nonhip.Get(n)]

        nsigs = h_name_list_2016.GetNbinsX()
        for ibin in xrange(1, nsigs+1):
            if (ibin-1)%(nsigs/20) == 0:
                sys.stdout.write('\rmake_signals_2015p6: pair %i/%i, %i/%i' % (ipair+1, len(fnpairs), ibin, nsigs)); sys.stdout.flush()

            old_isample = -ibin
            new_isample = old_isample if ipair == 0 else signalset.next_isample()
            old_bn = 'h_signal_%i_' % old_isample
            new_bn = 'h_signal_%i_' % new_isample

            # convert sample name to new format--really only more digits in tau field
            old_name = f_2016.Get(old_bn + 'norm').GetTitle()
            mass = name2mass(old_name) # needed in scalefactor too
            new_name = details2name(name2kind(old_name), name2tau(old_name), mass)

            if name_list.has_key(new_name):
                print colors.warning('\nwarning: %s found in ipair %i with isample %i was found in previous ipair %s with isample %i, skipping' % (new_name, ipair, old_isample, name_list[new_name].ipair, name_list[new_name].isample))
                continue

            name_list[new_name] = ss = signalset(new_name, '2016', new_isample)
            ss.ipair = ipair

            norms = _hs(old_bn + 'norm')
            ngens = [1e-3/h.GetBinContent(2) for h in norms]
            ngentot = sum(ngens[1:]) # hip/nonhip are the only independent samples, 2015 is just 2016 that we rescale

            for n in 'dbv', 'dphi', 'dvv', 'dvv_rebin':
                hs = _hs(old_bn + n)
                for h,ng,sf,il in zip(hs, ngens, sfs, int_lumis):
                    if n == 'dbv' or n == 'dvv':
                        h.Rebin(10) # did too many bins last time
                    h.Scale(sf(mass) * 1e-3 * il / ng)

                h = hs.pop(0)
                for h2 in hs:
                    h.Add(h2)

                h.SetName(new_bn + n + '_2016')
                h.SetTitle(new_name + '_2016')
                f.cd(); h.Write()

            f.cd()

            h = ROOT.TH1D(new_bn + 'uncert_2016', new_name + '_2016', gp.nbins, gp.bins)
            uncerts = [0.24, sig_uncert_pdf(new_name + '_2016')] # ignore old hist, just use the previous total 24%
            for ib in xrange(1,gp.nbins+1): h.SetBinContent(ib, 1+sum(x**2 for x in uncerts)**0.5)
            h.Write()

            h = ROOT.TH1D(new_bn + 'ngen_2016', new_name + '_2016', 1,0,1)
            h.SetBinContent(1, ngentot)
            h.Write()

        print '\rmake_signals_2015p6: done with pair %i             ' % (ipair+1)
コード例 #21
0
import sys
from JMTucker.Tools.ROOTTools import ROOT
from binning import binning


def book(out_name, hs=[]):
    bins = binning()
    h = ROOT.TH1F(out_name, '', len(bins) - 1, bins)
    hs.append(h)
    return h


f = ROOT.TFile('export.root', 'recreate')

execfile(sys.argv[1])

f.Write()
f.Close()
コード例 #22
0
def make_signals_2017p8(f, name_list):
    # 2017,8 are from minitrees (the 100kevt official samples) and scanpack.
    scanpack_list = '/uscms/home/tucker/public/mfv/scanpacks/2017p8/scanpack1D_4_4p7_4p8.merged.list.gz'
    trees = '/uscms_data/d2/tucker/crab_dirs/MiniTreeV27m/mfv*.root'
    title = []
    sigs = {}

    if scanpack_list:
        title.append(scanpack_list)
        sigs.update(eval(GzipFile(scanpack_list).read()))
    if trees:
        title.append(trees)
        sigs.update({os.path.basename(fn).replace('.root', '') : [fn] for fn in sorted(glob(trees))}) # overrides scanpack entries
    name_list['title'] = name_list.get('title', '') + '+' + '+'.join(title)

    nsigs = len(sigs)
    for isig, (name_year, fns) in enumerate(sigs.iteritems()):
        if isig%(nsigs/20) == 0:
            sys.stdout.write('\rmake_signals_2017p8: %i/%i' % (isig, nsigs)); sys.stdout.flush()

        name, year = name_year.rsplit('_',1)
        if name_list.has_key(name):
            s = name_list[name]
            s.years.add(year)
        else:
            s = name_list[name] = signalset(name, year)
        n = lambda x: 'h_signal_%i_%s_%s'  % (s.isample, x, year)

        ngen = 0.
        t = ROOT.TChain('mfvMiniTree/t')
        for fn in fns:
            sig_f = ROOT.TFile.Open(fn)
            ngen += sig_f.Get('mfvWeight/h_sums').GetBinContent(1)
            sig_f.Close()
            t.Add(fn)

        if year == '2017' and gp.l1eeprefiring_2017:
            t.SetAlias('jet_l1ee', 'abs(jet_eta) > 2.25 && jet_pt > 100')
            t.SetAlias('njets_l1ee', 'Sum$(!jet_l1ee)')
            t.SetAlias('jetht_l1ee', 'Sum$(jet_pt * (jet_pt > 40 && !jet_l1ee))')
            t.SetAlias('limitsinput_pass', '(njets_l1ee >= 4 && jetht_l1ee >= 1200)')
        elif year == '2018' and gp.hem1516_2018:
            t.SetAlias('jet_hem1516', 'jet_eta < -1.3 && jet_phi < -0.87 && jet_phi > -1.57')
            t.SetAlias('njets_hem1516', 'Sum$(!jet_hem1516)')
            t.SetAlias('jetht_hem1516', 'Sum$(jet_pt * (jet_pt > 40 && !jet_hem1516))')
            t.SetAlias('limitsinput_pass', '(njets_hem1516 >= 4 && jetht_hem1516 >= 1200) || (rndm < 0.36)') # silly way to represent effect only on 64% of the data
        else:
            t.SetAlias('limitsinput_pass', '1==1')

        iyear = gp.years.index(year)
        scale = 1e-3 * gp.int_lumis[iyear] / ngen
        data_mc_scale = sig_datamcSF_2017p8(name_year)

        ROOT.TH1.AddDirectory(1) # the Draw>> output goes off into the ether without this stupid crap
        h_dbv  = ROOT.TH1D(n('dbv'),  '', 1250, 0, 2.5)
        h_dvv  = ROOT.TH1D(n('dvv'),  '', 400, 0, 4)
        h_dphi = ROOT.TH1D(n('dphi'), '', 10, -3.15, 3.15)
        t.Draw('dist0>>%s'  % n('dbv'),  'weight*(limitsinput_pass && nvtx==1)')
        t.Draw('svdist>>%s' % n('dvv'),  'weight*(limitsinput_pass && nvtx>=2)')
        t.Draw('svdphi>>%s' % n('dphi'), 'weight*(limitsinput_pass && nvtx>=2)')
        ROOT.TH1.AddDirectory(0)
        for h in h_dbv, h_dvv, h_dphi:
            h.SetDirectory(0)
            h.Scale(scale)
            if 'dbv' in h.GetTitle():
                h.Scale(data_mc_scale)
            else:
                h.Scale(data_mc_scale**2)
        
        h_dvv_rebin = h_dvv.Rebin(gp.nbins, n('dvv_rebin'), gp.bins)
        move_overflow_into_last_bin(h_dvv_rebin)

        h_ngen = ROOT.TH1D(n('ngen'), '', 1,0,1)
        h_ngen.SetBinContent(1, ngen)
        
        h_uncert = ROOT.TH1D(n('uncert'), '', gp.nbins, gp.bins)
        for i,v in enumerate(sig_uncert_2017p8(name_year)):
            h_uncert.SetBinContent(i+1, v)
        
        f.cd()
        for h in h_dbv, h_dphi, h_dvv, h_dvv_rebin, h_uncert, h_ngen:
            h.SetTitle(name_year)
            h.Write()

    print '\rmake_signals_2017p8: done       '
コード例 #23
0
def cutplayMangle(filename):
    debug = False

    file = ROOT.TFile.Open(filename)
    if not file.IsOpen():
        raise RuntimeError('could not open %s' % filename)

    norm = norm_from_file(filename)
    x = file.Get('SimpleTriggerEfficiency/triggers_pass_num')

    cutscans = {}
    nm1 = None

    # Extract from the big list of bins the numbers separately for each
    # cut.
    for i in xrange(1, x.GetNbinsX() + 1):
        label = x.GetXaxis().GetBinLabel(i)

        if label == 'nm1':
            nm1 = x.GetBinContent(i), x.GetBinError(i)
            continue

        try:
            cut, cut_val = label.split('X')
        except ValueError:
            print 'warning: could not parse label %s' % label
            continue

        if not cutscans.has_key(cut):
            cutscans[cut] = []
        cutscans[cut].append((cut_val, x.GetBinContent(i)))

    h_intot = file.Get('SimpleTriggerEfficiency/triggers_pass_den')
    ntot = h_intot.GetBinContent(1), h_intot.GetBinError(1)

    file.Close()

    output_fn = filename.replace('.root', '_mangled.root')
    output_file = ROOT.TFile(output_fn, 'RECREATE')

    sample_name = os.path.basename(filename).replace('.root', '')
    try:
        sample = getattr(Samples, sample_name)
    except AttributeError:
        sample = None
    h_ntot = ROOT.TH1F('ntot', '', 1, 0, 1)
    scale = ntot[0] / norm if sample is not None else 1.
    h_ntot.SetBinContent(1, ntot[0] / scale)
    h_ntot.SetBinError(1, ntot[1] / scale)

    if nm1 is not None:
        h_nm1 = ROOT.TH1F('nm1', '', 1, 0, 1)
        h_nm1.SetBinContent(1, nm1[0])
        h_nm1.SetBinError(1, nm1[1])
    else:
        raise ValueError('did not find nm1 value in input')

    output_hists = []

    for cut_name, scan in cutscans.iteritems():
        if debug:
            print 'cut: %s' % cut_name

        hmin = depize(scan[0][0])
        hminplus1 = depize(scan[1][0])
        binwidth = hminplus1 - hmin
        hmaxminus1 = depize(scan[-1][0])
        hmax = hmaxminus1 + binwidth
        if debug:
            print 'hmin: ', hmin, ' hminplus1: ', hminplus1, ' binwidth: ', binwidth, ' hmaxminus1: ', hmaxminus1, ' hmax: ', hmax

        hist = ROOT.TH1F(cut_name, '', len(scan), hmin, hmax)
        output_hists.append(hist)

        for cut_bin_name, nevents in scan:
            bin = hist.FindBin(depize(cut_bin_name) + 0.00001)
            bin_edge = hist.GetBinLowEdge(bin)
            if debug:
                print 'cut bin name: %s ibin: %i bin_edge: %s  number of events: %f' % (
                    cut_bin_name, bin, bin_edge, nevents)
            hist.SetBinContent(bin, nevents)

    output_file.Write()
    output_file.Close()
コード例 #24
0
        out_f.Write()
        out_f.Close()


if __name__ == '__main__':
    if 'make' in sys.argv:
        make()
    elif 'uncert' in sys.argv:
        sig_uncert_2017p8(sys.argv[sys.argv.index('uncert') + 1], debug=True)
    elif 'draw' in sys.argv:
        draw()
    elif 'compare' in sys.argv:
        compare(*sys.argv[2:5])
    elif 'ngen' in sys.argv:
        f = ROOT.TFile(gp.fn)
        years = sys.argv[sys.argv.index('ngen'):]
        for s in sample_iterator(f, years, slices_1d=True):
            print '%6i' % s.isample, s.name.ljust(35), ' '.join(
                ['%10s'] * gp.nyears) % tuple(
                    ngen(f, s.isample, year) for year in gp.years)
        #nevents_plot()
    elif 'points' in sys.argv:
        print 'kinds = %r\nmasses = %r\ntaus = %r' % points()
    elif 'iter' in sys.argv:
        include_2016 = 'include_2016' in sys.argv
        years = ('2016', '2017', '2018') if include_2016 else ('2017', '2018')
        samples = sample_iterator(
            ROOT.TFile('limitsinput.root'),
            require_years=years,
            test='test_batch' in sys.argv,