Example #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()
    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()
Example #3
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
Example #4
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()
Example #5
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()
Example #6
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)
Example #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()
Example #8
0
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]]
Example #9
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()
Example #10
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()
Example #11
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,
Example #12
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)
Example #13
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
Example #14
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,