Exemplo n.º 1
0
    def getit(s):
        s.z = {}
        for year in 2015, 2016:
            if year == 2015:
                fn = suppl[s.name+'_2015'][0]
            else:
                fn = '/uscms_data/d2/tucker/crab_dirs/MiniTreeV16/%s.root' % s.name
            f = ROOT.TFile.Open(fn)
            t = f.Get('mfvMiniTree/t')
            hr = draw_hist_register(t, True)
            h,n = hr.draw('svdist', 'nvtx>=2', binning='400,0,4', get_n=True, goff=True)
            assert h.GetXaxis().GetBinLowEdge(1) == 0.
            assert h.GetXaxis().GetBinLowEdge(5) == 0.04
            assert h.GetXaxis().GetBinLowEdge(8) == 0.07
            N = norm_from_file(f)
            n = h.Integral(1,10000)
            b1 = h.Integral(1,4)
            b2 = h.Integral(5,7)
            b3 = h.Integral(8,10000)
            #print s.name, N, n, b1, b2, b3
            s.z[year] = (N,n,b1,b2,b3)

        N15, n15 = s.z[2015][:2]
        N16, n16 = s.z[2016][:2]
        s.ratio = [x * N16/N15 for x in clopper_pearson_poisson_means(n15, n16)]
        s.ratio[1] = s.ratio[0] - s.ratio[1]
        s.ratio[2] = s.ratio[2] - s.ratio[0]
        s.ratio = tuple(s.ratio)
        print s.name.ljust(30), '%.4f - %.4f + %.4f' % s.ratio
Exemplo n.º 2
0
def analyze(plots=False):
    if plots:
        set_style()
        ps = plot_saver(plot_dir('pdfweights', temp=True), size=(600, 600))

    fields = ('nominal', 'plus', 'minus', 'pdfunc', 'rel', 'relavg', 'n16',
              'n84', 'ctl', 'ctlrel', 'ctlrelmd')
    fmt = '%-45s' + ' %10s' * len(fields)
    print fmt % (('sname', ) + fields)

    ss = samples()
    for s in ss:
        f = ROOT.TFile.Open(s.fn)
        s.norm = norm_from_file(f)
        s.w = ac.int_lumi_2018 * 1e-3 / s.norm
        t = f.Get('mfvMiniTree/t')
        hr = draw_hist_register(t, True, True)
        if plots:
            for i in xrange(48, 148):
                h = hr.draw('weights[%i]' % i, 'nvtx>=2', binning='100,0,2')
                ps.save(str(i))

        cut = '(nvtx>=2)'  # '(nvtx>=2 && (gen_x[0]**2 + gen_y[0]**2)**0.5<0.08 && (gen_x[1]**2+gen_y[1]**2)**0.5 < 0.08)'
        h = hr.draw('weight', cut, binning='1,0,1')
        s.nominal, _ = tuple(s.w * x for x in get_integral(h))

        s.pdfunc, s.family = [], []
        for i in xrange(
                46, 148
        ):  # JMTBAD indices into misc_weights are fragile, need to put the pdf weights/info somewhere else than weights
            h = hr.draw('weight',
                        '%s*misc_weights[%i]' % (cut, i),
                        binning='1,0,1')
            v = tuple(s.w * x for x in get_integral(h))[0]
            (s.pdfunc if i < 48 else s.family).append(v)

        s.plus, s.minus = s.pdfunc
        s.pdfunc = abs(s.plus - s.minus) / 2
        s.plus = abs(s.plus - s.nominal)
        s.minus = abs(s.nominal - s.minus)
        s.rel = s.pdfunc / s.nominal

        s.family.sort()
        s.n16, s.n84 = s.family[16], s.family[83]
        s.ctl = (s.n84 - s.n16) / 2
        s.avg = avg(s.family)
        s.rms = rms(s.family)
        s.mdn = (s.family[49] + s.family[50]) / 2
        s.md = (s.n84 + s.n16) / 2
        s.relavg = s.pdfunc / s.avg
        s.ctlrel = s.ctl / s.nominal
        s.ctlrelmd = s.ctl / s.md

        print fmt.replace('%10s', '%10.4f') % (
            (s.name, ) + tuple(getattr(s, fi) for fi in fields))

    to_pickle(ss, 'pdfuncerts.gzpickle')
Exemplo n.º 3
0
def getit(fn, ntk):
    f = ROOT.TFile(fn)
    den = norm_from_file(f)
    assert 3 <= ntk <= 5
    t = f.Get('mfvMiniTree%s/t' % ('Ntk%i' % ntk if ntk < 5 else ''))
    if not t:
        return None
    n1v = t.Draw('dist0', 'nvtx==1', 'goff')
    n2v = t.Draw('dist0', 'nvtx>=2', 'goff')
    return den, n1v, n2v
Exemplo n.º 4
0
def getit(fn, ntk):
    f = ROOT.TFile(fn)
    den = norm_from_file(f)
    assert 3 <= ntk <= 5
    t = f.Get('mfvMiniTree%s/t' % ('Ntk%i' % ntk if ntk < 5 else ''))
    if not t:
        return None
    n1v = t.Draw('dist0', 'nvtx==1', 'goff')
    n2v = t.Draw('dist0', 'nvtx>=2', 'goff')
    return den, n1v, n2v
Exemplo n.º 5
0
def crab_hadd(working_dir,
              new_name=None,
              new_dir=None,
              raise_on_empty=False,
              chunk_size=900,
              pattern=None,
              lpc_shortcut=False,
              range_filter=None):
    working_dir, new_name, new_dir = crab_hadd_args(working_dir, new_name,
                                                    new_dir)
    expected, files = crab_hadd_files(working_dir,
                                      lpc_shortcut,
                                      range_filter=range_filter)
    result = HaddBatchResult('crab', working_dir, new_name, new_dir, expected,
                             files)
    print '%s: expecting %i files if all jobs succeeded' % (working_dir,
                                                            expected)

    if pattern:
        if '/' not in pattern:
            pattern = '*/' + pattern
        files = fnmatch.filter(files, pattern)

    automatic_splitting = False
    pprinted = False
    jobs = []
    for f in files:
        jobnum = f.split('_')[-1].split('.root')[0]
        if crab_global_options.support_automatic_splitting and '-' in jobnum:
            automatic_splitting = True
            if not pprinted:
                pprint(files)
                pprinted = True
            it, jobnum = jobnum.split('-')
            it, jobnum = int(it), int(jobnum)
            assert it >= 1  # probe jobs "0-*" should not show up
            jobnum = it * 10000 + jobnum
        else:
            jobnum = int(jobnum)
        jobs.append(jobnum)
    jobs.sort()
    expected = range(1, expected + 1)

    if jobs != expected:
        print '\033[36;7m %i files found %s not what expected \033[m' % (
            len(jobs), crabify_list(jobs))
        missing = sorted(set(expected) - set(jobs))
        print '\033[36;7m    %i missing: %r \033[m' % (len(missing), ' '.join(
            str(j) for j in missing))

    l = len(files)
    if l == 0:
        result.success = False
        msg = 'crab_hadd: no files found in %s' % working_dir
        if raise_on_empty:
            raise CRABToolsException(msg)
        else:
            print '\033[36;7m', msg, '\033[m'
    elif l == 1:
        print working_dir, ': just one file found, copying'
        cmd = 'xrdcp -s %s %s' % (files[0], new_name)
        result.success = os.system(cmd) == 0
        if result.success and not new_name.startswith('root://'):
            os.chmod(new_name, 0644)
    else:
        result.success = hadd(new_name, files)

    if automatic_splitting:
        n = norm_from_file(new_name)
        sn, s = fn_to_sample(Samples, new_name)
        if not s:
            print colors.yellow(
                "\tnorm_from_file returns %r, couldn't get sample %s" %
                (n, sn))
        else:
            no1, no2 = s.datasets['main'].nevents_orig, s.datasets[
                'miniaod'].nevents_orig
            if n == no1 or n == no2:
                print '\tnorm_from_file returns nevents_orig = %i' % n
            else:
                print colors.yellow(
                    '\tnorm_from_file returns %r while %s.nevents_orig is %i (main) %i (miniaod'
                    % (n, sn, no1, no2))

    return result
Exemplo n.º 6
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()
Exemplo n.º 7
0
def effs(fn):
    global tot_sum, tot_var

    f = ROOT.TFile(fn)

    def get_n(dir_name):
        h = f.Get('%s/h_npv' % dir_name)
        return h.Integral(0,1000000) if integral else h.GetEntries()

    den = norm_from_file(fn)
    sname = os.path.basename(fn).replace('.root','')
    sample = getattr(Samples, sname, None)
    if sample:
        weight = sample.xsec * int_lumi / den
        weighted = True
    else:
        weight = 1.
        weighted = False

    if sigreg:
        namenumall = 'mfvEventHistosSigReg'
        namenumvtx = 'mfvVertexHistosSigReg/h_nsv'
    elif nvtx == 1:
        namenumall = 'mfvEventHistosOnlyOneVtx'
        namenumvtx = 'mfvVertexHistosOnlyOneVtx/h_nsv'
    elif presel:
        namenumall = 'mfvEventHistosPreSel'
        namenumvtx = None
    elif nocuts:
        namenumall = 'mfvEventHistosNoCuts'
        namenumvtx = None
    else:
        namenumall = 'mfvEventHistosFullSel'
        namenumvtx = None

    namenumall = ntk + namenumall
    if namenumvtx:
        namenumvtx = ntk + namenumvtx

    numall = get_n(namenumall)
    if namenumvtx is not None:
        h = f.Get(namenumvtx)
        numvtx = h.Integral(h.FindBin(nvtx), 1000000)
    else:
        numvtx = -1

    tot_sum += numall * weight
    tot_var += numall * weight**2
    if csv:
        print '%s,%e,%f,%f,%f,%f,%f' % (sname, weight, den, numall, float(numall)/den, numall*weight, numall**0.5 * weight)
    else:
        print '%s (w = %.3e): # ev: %10.1f  pass evt+vtx: %5.1f -> %5.3e  pass vtx only: %5.1f -> %5.3e' % (sname.ljust(30), weight, den, numall, float(numall)/den, numvtx, float(numvtx)/den)
        if weighted:
            print '  weighted to %.1f/pb: %5.2f +/- %5.2f' % (int_lumi, numall*weight, numall**0.5 * weight)
        else:
            print '  number of events: %5.2f +/- %5.2f' % (numall*weight, numall**0.5 * weight)
        if cuts:
            nm1s_name = 'h_nm1_%s' % sname
            h_nm1_abs = ROOT.TH1F(nm1s_name + '_abs', ';cut;abs. eff. w/o cut', len(cuts)+1, 0, len(cuts)+1)
            h_nm1_rel = ROOT.TH1F(nm1s_name + '_rel', ';cut;n-1 eff.', len(cuts), 0, len(cuts))
            for icut, cut in enumerate(cuts):
                h_nm1_abs.GetXaxis().SetBinLabel(icut+1, cut)
                h_nm1_rel.GetXaxis().SetBinLabel(icut+1, cut)
                nm1 = get_n('evtHst%sVNo%s' % (nvtx, cut))
                nm1_abs = float(nm1)/den
                nm1_rel = float(numall)/nm1 if nm1 > 0 else -1
                h_nm1_abs.SetBinContent(icut+1, nm1_abs)
                h_nm1_rel.SetBinContent(icut+1, nm1_rel)
                print '    remove %s cut: %5i -> %5.3e (n-1: %5.3e)' % (cut.ljust(max_cut_name_len), nm1, nm1_abs, nm1_rel)
            h_nm1_abs.GetXaxis().SetBinLabel(len(cuts)+1, 'all')
            h_nm1_abs.SetBinContent(len(cuts)+1, float(numall)/den)
            def draw(h):
                if not plots:
                    return
                h.SetStats(0)
                h.GetYaxis().SetRangeUser(0,1.05)
                h.SetMarkerSize(2)
                h.Draw('hist text')
                ps.save(h.GetName())
            draw(h_nm1_abs)
            draw(h_nm1_rel)
Exemplo n.º 8
0
def analyze(plots=False):
    if plots:
        set_style()
        ps = plot_saver(plot_dir('scaleweights', temp=True), size=(600, 600))

    fields = ('nominal', 'n16', 'n84', 'ctl', 'ctlrel', 'ctlrelmd')
    fmt = '%-45s' + ' %10s' * len(fields)
    print fmt % (('sname', ) + fields)

    ss = samples()
    for s in ss:
        f = ROOT.TFile.Open(s.fn)
        s.norm = norm_from_file(f)
        s.w = ac.int_lumi_2018 * 1e-3 / s.norm
        t = f.Get('mfvMiniTree/t')
        hr = draw_hist_register(t, True, True)

        weight_str_list = [
            "ren_weight_up", "ren_weight_dn", "fac_weight_up", "fac_weight_dn",
            "ren_weight_up*fac_weight_up", "ren_weight_dn*fac_weight_dn"
        ]

        cut = '(nvtx>=2)'  # '(nvtx>=2 && (gen_x[0]**2 + gen_y[0]**2)**0.5<0.08 && (gen_x[1]**2+gen_y[1]**2)**0.5 < 0.08)'
        h = hr.draw('weight', cut, binning='1,0,1')
        s.nominal, _ = tuple(s.w * x for x in get_integral(h))

        s.scaleunc, s.family = [], []
        for weight_str in weight_str_list:

            # because I didn't pick better names...
            if "ren" in weight_str and "fac" in weight_str:
                weight_name_for_norm = "sum_weight_ren_fac"
            elif "ren" in weight_str:
                weight_name_for_norm = "sum_weight_ren"
            elif "fac" in weight_str:
                weight_name_for_norm = "sum_weight_fac"

            if "_up" in weight_str:
                weight_name_for_norm += "_up"
            elif "_dn" in weight_str:
                weight_name_for_norm += "_dn"

            s.norm = norm_from_file_weight(f, weight_name=weight_name_for_norm)
            s.w = ac.int_lumi_2018 * 1e-3 / s.norm

            h = hr.draw('weight', '%s*%s' % (cut, weight_str), binning='1,0,1')
            v = tuple(s.w * x for x in get_integral(h))[0]
            #print '%s*%s, weight*nvtx==2 = %s' % (cut,weight_str,v)
            s.family.append(v)

        if len(weight_str_list) == 6:
            s.family.sort()
            s.n16, s.n84 = s.family[1], s.family[4]  # since total is 6
            s.ctl = max(abs(s.family[0] - s.nominal),
                        abs(s.family[5] -
                            s.nominal))  # i.e. maximum deviation ==> envelope
            #s.ctl = (s.n84 - s.n16)/2
            s.avg = avg(s.family)
            s.rms = rms(s.family)
            s.mdn = (s.family[2] + s.family[3]) / 2
            s.md = (s.n84 + s.n16) / 2
            #s.relavg = s.scaleunc / s.avg
            if s.nominal == 0:
                print "s.nominal is 0!!!!!!!!!!!!"
                s.nominal = 0.00001
            if s.md == 0:
                print "s.md is 0!!!!!!!!!!!!"
                s.md = 0.00001
            s.ctlrel = s.ctl / s.nominal
            s.ctlrelmd = s.ctl / s.md

            print fmt.replace('%10s', '%10.4f') % (
                (s.name, ) + tuple(getattr(s, fi) for fi in fields))
        else:
            print "invalid weight_str_list!!!!!!!!!!!!!!!!!"

    to_pickle(ss, 'ren_fac.gzpickle')
Exemplo n.º 9
0
def effs(fn):
    global tot_sum, tot_var

    f = ROOT.TFile(fn)

    def get_n(dir_name):
        h = f.Get('%s/h_npv' % dir_name)
        return h.Integral(0, 1000000) if integral else h.GetEntries()

    den = norm_from_file(fn)
    sname = os.path.basename(fn).replace('.root', '')
    sample = getattr(Samples, sname, None)
    if sample:
        weight = sample.xsec * int_lumi / den
        weighted = True
    else:
        weight = 1.
        weighted = False

    if sigreg:
        namenumall = 'mfvEventHistosSigReg'
        namenumvtx = 'mfvVertexHistosSigReg/h_nsv'
    elif nvtx == 1:
        namenumall = 'mfvEventHistosOnlyOneVtx'
        namenumvtx = 'mfvVertexHistosOnlyOneVtx/h_nsv'
    elif presel:
        namenumall = 'mfvEventHistosPreSel'
        namenumvtx = None
    elif nocuts:
        namenumall = 'mfvEventHistosNoCuts'
        namenumvtx = None
    else:
        namenumall = 'mfvEventHistosFullSel'
        namenumvtx = None

    namenumall = ntk + namenumall
    if namenumvtx:
        namenumvtx = ntk + namenumvtx

    numall = get_n(namenumall)
    if namenumvtx is not None:
        h = f.Get(namenumvtx)
        numvtx = h.Integral(h.FindBin(nvtx), 1000000)
    else:
        numvtx = -1

    tot_sum += numall * weight
    tot_var += numall * weight**2
    if csv:
        print '%s,%e,%f,%f,%f,%f,%f' % (sname, weight, den, numall,
                                        float(numall) / den, numall * weight,
                                        numall**0.5 * weight)
    else:
        print '%s (w = %.3e): # ev: %10.1f  pass evt+vtx: %5.1f -> %5.3e  pass vtx only: %5.1f -> %5.3e' % (
            sname.ljust(30), weight, den, numall, float(numall) / den, numvtx,
            float(numvtx) / den)
        if weighted:
            print '  weighted to %.1f/pb: %5.2f +/- %5.2f' % (
                int_lumi, numall * weight, numall**0.5 * weight)
        else:
            print '  number of events: %5.2f +/- %5.2f' % (
                numall * weight, numall**0.5 * weight)
        if cuts:
            nm1s_name = 'h_nm1_%s' % sname
            h_nm1_abs = ROOT.TH1F(nm1s_name + '_abs', ';cut;abs. eff. w/o cut',
                                  len(cuts) + 1, 0,
                                  len(cuts) + 1)
            h_nm1_rel = ROOT.TH1F(nm1s_name + '_rel', ';cut;n-1 eff.',
                                  len(cuts), 0, len(cuts))
            for icut, cut in enumerate(cuts):
                h_nm1_abs.GetXaxis().SetBinLabel(icut + 1, cut)
                h_nm1_rel.GetXaxis().SetBinLabel(icut + 1, cut)
                nm1 = get_n('%sevtHst%sVNo%s' % (ntk, nvtx, cut))
                nm1_abs = float(nm1) / den
                nm1_rel = float(numall) / nm1 if nm1 > 0 else -1
                h_nm1_abs.SetBinContent(icut + 1, nm1_abs)
                h_nm1_rel.SetBinContent(icut + 1, nm1_rel)
                print '    remove %s cut: %5i -> %5.3e (n-1: %5.3e)' % (
                    cut.ljust(max_cut_name_len), nm1, nm1_abs, nm1_rel)
                print '    weighted to %.1f/pb: %5.2f +/- %5.2f' % (
                    int_lumi, nm1 * weight, abs(nm1)**0.5 * weight)
            h_nm1_abs.GetXaxis().SetBinLabel(len(cuts) + 1, 'all')
            h_nm1_abs.SetBinContent(len(cuts) + 1, float(numall) / den)

            def draw(h):
                if not plots:
                    return
                h.SetStats(0)
                h.GetYaxis().SetRangeUser(0, 1.05)
                h.SetMarkerSize(2)
                h.Draw('hist text')
                ps.save(h.GetName())

            draw(h_nm1_abs)
            draw(h_nm1_rel)
def analyze(plots=False):
    if plots:
        set_style()
        ps = plot_saver(plot_dir('scaleweights', temp=True), size=(600, 600))

    fields = ('nominal', 'n16', 'n84', 'ctl', 'ctlrel', 'ctlrelmd')
    fmt = '%-45s' + ' %10s' * len(fields)
    print fmt % (('sname', ) + fields)

    ss = samples()
    for s in ss:
        f = ROOT.TFile.Open(s.fn)
        s.norm = norm_from_file(f)
        s.w = ac.int_lumi_2018 * 1e-3 / s.norm
        t = f.Get('mfvMiniTree/t')
        hr = draw_hist_register(t, True, True)

        myrange = (14, 46)  # decorrelated variations - recommended!
        #myrange = (6,10) # simple 2x up/down

        if plots:
            for i in xrange(*myrange):
                h = hr.draw('weights[%i]' % i, 'nvtx>=2', binning='100,0,2')
                ps.save(str(i))

        cut = '(nvtx>=2)'  # '(nvtx>=2 && (gen_x[0]**2 + gen_y[0]**2)**0.5<0.08 && (gen_x[1]**2+gen_y[1]**2)**0.5 < 0.08)'
        h = hr.draw('weight', cut, binning='1,0,1')
        s.nominal, _ = tuple(s.w * x for x in get_integral(h))

        s.scaleunc, s.family = [], []
        for i in xrange(
                *myrange
        ):  # JMTBAD indices into misc_weights are fragile, need to put the pdf weights/info somewhere else than weights
            h = hr.draw('weight',
                        '%s*misc_weights[%i]' % (cut, i),
                        binning='1,0,1')
            v = tuple(s.w * x for x in get_integral(h))[0]
            s.family.append(v)

        if myrange == (14, 46):
            s.family.sort()
            s.n16, s.n84 = s.family[5], s.family[27]  # since total is 32
            s.ctl = (s.n84 - s.n16) / 2
            s.avg = avg(s.family)
            s.rms = rms(s.family)
            s.mdn = (s.family[15] + s.family[16]) / 2
            s.md = (s.n84 + s.n16) / 2
            #s.relavg = s.scaleunc / s.avg
            if s.nominal == 0:
                print "s.nominal is 0!!!!!!!!!!!!"
                s.nominal = 0.00001
            if s.md == 0:
                print "s.md is 0!!!!!!!!!!!!"
                s.md = 0.00001
            s.ctlrel = s.ctl / s.nominal
            s.ctlrelmd = s.ctl / s.md

            print fmt.replace('%10s', '%10.4f') % (
                (s.name, ) + tuple(getattr(s, fi) for fi in fields))
        elif myrange == (6, 10):
            s.family.sort()
            s.n16, s.n84 = s.family[1], s.family[2]  # since total is 4
            s.ctl = (s.n84 - s.n16) / 2
            s.avg = avg(s.family)
            s.rms = rms(s.family)
            s.mdn = (s.family[1] + s.family[2]) / 2
            s.md = (s.n84 + s.n16) / 2
            s.ctlrel = s.ctl / s.nominal
            s.ctlrelmd = s.ctl / s.md

            print fmt.replace('%10s', '%10.4f') % (
                (s.name, ) + tuple(getattr(s, fi) for fi in fields))
            print "avg %10.4f, rms %10.4f, rel %10.4f" % (s.avg, s.rms,
                                                          s.rms / s.avg)
        else:
            print "invalid range!!!!!!!!!!!!!!!!!"

    to_pickle(ss, 'alphaS_parton_shower_uncerts.gzpickle')
Exemplo n.º 11
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()
Exemplo n.º 12
0
import sys, glob, os
from JMTucker.Tools.Sample import norm_from_file
from JMTucker.Tools import Samples, colors

path = sys.argv[1]
dataset = sys.argv[2]
target = float(sys.argv[3]) if len(sys.argv) > 3 else 0.5

for fn in glob.glob(os.path.join(path, '*.root')):
    sname = os.path.basename(fn).replace('.root', '')
    s = getattr(Samples, sname)
    s.set_curr_dataset(dataset)
    n = norm_from_file(fn)
    frac = n/s.nevents_orig
    delta = abs(frac - target)/target
    if delta > 0.01:
        color = colors.red
    elif delta > 1e-3:
        color = colors.yellow
    else:
        color = colors.green
    print color('%-50s %15i %15i %e' % (fn, s.nevents_orig, n, frac))
Exemplo n.º 13
0
def crab_hadd(working_dir, new_name=None, new_dir=None, raise_on_empty=False, chunk_size=900, pattern=None, lpc_shortcut=False, range_filter=None):
    working_dir, new_name, new_dir = crab_hadd_args(working_dir, new_name, new_dir)
    expected, files = crab_hadd_files(working_dir, lpc_shortcut, range_filter=range_filter)
    print '%s: expecting %i files if all jobs succeeded' % (working_dir, expected)

    if pattern:
        if '/' not in pattern:
            pattern = '*/' + pattern
        files = fnmatch.filter(files, pattern)

    automatic_splitting = False
    pprinted = False
    jobs = []
    for f in files:
        jobnum = f.split('_')[-1].split('.root')[0]
        if crab_global_options.support_automatic_splitting and '-' in jobnum:
            automatic_splitting = True
            if not pprinted:
                pprint(files)
                pprinted = True
            it, jobnum = jobnum.split('-')
            it, jobnum = int(it), int(jobnum)
            assert it >= 1 # probe jobs "0-*" should not show up
            jobnum = it*10000 + jobnum
        else:
            jobnum = int(jobnum)
        jobs.append(jobnum)
    jobs.sort()
    expected = range(1, expected+1)

    if jobs != expected:
        print '\033[36;7m %i files found %s not what expected \033[m' % (len(jobs), crabify_list(jobs))
        missing = sorted(set(expected) - set(jobs))
        print '\033[36;7m    %i missing: %r \033[m' % (len(missing), ' '.join(str(j) for j in missing))

    l = len(files)
    if l == 0:
        msg = 'crab_hadd: no files found in %s' % working_dir
        if raise_on_empty:
            raise CRABToolsException(msg)
        else:
            print '\033[36;7m', msg, '\033[m'
    elif l == 1:
        print working_dir, ': just one file found, copying'
        cmd = 'xrdcp -s %s %s' % (files[0], new_name)
        os.system(cmd)
        os.chmod(new_name, 0644)
    else:
        hadd(new_name, files)

    if automatic_splitting:
        n = norm_from_file(new_name)
        sn, s = fn_to_sample(Samples, new_name)
        if not s:
            print colors.yellow("\tnorm_from_file returns %r, couldn't get sample %s" % (n, sn))
        else:
            no1, no2 = s.datasets['main'].nevents_orig, s.datasets['miniaod'].nevents_orig
            if n == no1 or n == no2:
                print '\tnorm_from_file returns nevents_orig = %i' % n
            else:
                print colors.yellow('\tnorm_from_file returns %r while %s.nevents_orig is %i (main) %i (miniaod' % (n, sn, no1, no2))

    return new_name