Exemple #1
0
def get_table(vals, do_unicode=True, width=80):
    d = dict(Counter(vals))
    maxval = max([d[k] for k in d.keys()])
    def shorten(label):
        return label[:50]
    maxstrlen = max([len(shorten(k)) for k in d.keys()])
    scaleto=width-maxstrlen
    fillchar = "*"
    if do_unicode:
        fillchar = unichr(0x2589).encode('utf-8')
    tab = Table()
    for w in sorted(d, key=d.get, reverse=True):
        nfill = d[w] if maxval < scaleto else max(1,int(float(scaleto)*d[w]/maxval))
        strbuff = "{0} ({1})".format(fillchar*nfill,d[w])
        shortw = shorten(w)
        tab.add_row([shortw,strbuff])
    return tab
Exemple #2
0
def get_table(vals, do_unicode=True, width=80):
    d = dict(Counter(vals))
    maxval = max([d[k] for k in d.keys()])

    def shorten(label):
        return label[:50]

    maxstrlen = max([len(shorten(k)) for k in d.keys()])
    scaleto = width - maxstrlen
    fillchar = "*"
    if do_unicode:
        fillchar = unichr(0x2589).encode('utf-8')
    tab = Table()
    for w in sorted(d, key=d.get, reverse=True):
        nfill = d[w] if maxval < scaleto else max(
            1, int(float(scaleto) * d[w] / maxval))
        strbuff = "{0} ({1})".format(fillchar * nfill, d[w])
        shortw = shorten(w)
        tab.add_row([shortw, strbuff])
    return tab
Exemple #3
0
def listofdicts_to_table(lod):
    colnames = list(set(sum([thing.keys() for thing in lod], [])))

    # key is col name and value is maximum length of any entry in that column
    d_colsize = {}
    for thing in lod:
        for colname in colnames:
            val = str(thing.get(colname, ""))
            if colname not in d_colsize: d_colsize[colname] = len(colname) + 1
            d_colsize[colname] = max(len(val) + 1, d_colsize[colname])

    # sort colnames from longest string lengths to shortest
    colnames = sorted(colnames, key=d_colsize.get, reverse=True)

    try:
        from pytable import Table
        if not sys.stdout.isatty():
            raise Exception

        tab = Table()
        tab.set_column_names(colnames)

        for row in lod:
            tab.add_row([row.get(colname) for colname in colnames])
        tab.sort(column=colnames[0], descending=False)

        return "".join(tab.get_table_string())

    except:
        buff = ""
        header = ""
        for icol, colname in enumerate(colnames):
            header += (
                "%%%s%is" %
                ("-" if icol == 0 else "", d_colsize[colname])) % colname
        buff += header + "\n"
        for thing in lod:
            line = ""
            for icol, colname in enumerate(colnames):
                tmp = "%%%s%is" % ("-" if icol == 0 else "",
                                   d_colsize[colname])
                tmp = tmp % str(thing.get(colname, ""))
                line += tmp
            buff += line + "\n"

        return buff
Exemple #4
0
def listofdicts_to_table(lod):
    colnames = list(set(sum([thing.keys() for thing in lod],[])))

    # key is col name and value is maximum length of any entry in that column
    d_colsize = {}
    for thing in lod:
        for colname in colnames:
            val = str(thing.get(colname,""))
            if colname not in d_colsize: d_colsize[colname] = len(colname)+1
            d_colsize[colname] = max(len(val)+1, d_colsize[colname])

    # sort colnames from longest string lengths to shortest
    colnames = sorted(colnames, key=d_colsize.get, reverse=True)

    try:
        from pytable import Table
        if not sys.stdout.isatty():
            raise Exception

        tab = Table()
        tab.set_column_names(colnames)

        for row in lod:
            tab.add_row([row.get(colname) for colname in colnames])
        tab.sort(column=colnames[0], descending=False)

        return "".join(tab.get_table_string())

    except:
        buff = ""
        header = ""
        for icol,colname in enumerate(colnames):
            header += ("%%%s%is" % ("-" if icol==0 else "", d_colsize[colname])) % colname
        buff += header + "\n"
        for thing in lod:
            line = ""
            for icol,colname in enumerate(colnames):
                tmp = "%%%s%is" % ("-" if icol==0 else "", d_colsize[colname])
                tmp = tmp % str(thing.get(colname,""))
                line += tmp
            buff += line + "\n"

        return buff
Exemple #5
0
import math
import random as rand
import noise
from pytable import Table

WIDTH = 16
HEIGHT = 8
CELL_SIZE = 20

pygame.display.init()
screen = pygame.display.set_mode((WIDTH * CELL_SIZE, HEIGHT * CELL_SIZE))
s = pygame.Surface((WIDTH * CELL_SIZE, HEIGHT * CELL_SIZE))
s.fill((0, 0, 0))
clock = pygame.time.Clock()

table = Table()

grid = [[[0, 0, 0] for i in range(HEIGHT)] for j in range(WIDTH)]


def clear_grid():
    global grid
    grid = [[[0, 0, 0] for i in range(HEIGHT)] for j in range(WIDTH)]


def hsv_to_rgb(x):
    y = colorsys.hsv_to_rgb(x[0], x[1], x[2])
    return [y[0] * 254, y[1] * 254, y[2] * 254]


def rgb_to_hsv(x):
Exemple #6
0
import random as rand
import noise
from pytable import Table

WIDTH = 16
HEIGHT = 8
CELL_SIZE = 20


pygame.display.init()
screen = pygame.display.set_mode((WIDTH*CELL_SIZE,HEIGHT*CELL_SIZE))
s = pygame.Surface((WIDTH*CELL_SIZE,HEIGHT*CELL_SIZE))
s.fill((0,0,0))
clock = pygame.time.Clock()

table = Table()

grid = [[[0,0,0] for i in range(HEIGHT)] for j in range(WIDTH)]

def clear_grid():
    global grid
    grid = [[[0,0,0] for i in range(HEIGHT)] for j in range(WIDTH)]

def hsv_to_rgb(x):
    y = colorsys.hsv_to_rgb(x[0], x[1], x[2])
    return [y[0]*254, y[1]*254, y[2]*254]
    
def rgb_to_hsv(x):
    y = colorsys.rgb_to_hsv(x[0], x[1], x[2])
    return [y[0], y[1], y[2]]
    
    stobe_org = [StoBErr(*x) for x in zip(sy_org, y_org, sye_org, sye_org)]
    stob_wtc = [s/sqrt(s+b) for s,b in zip(sy_wtc, y_wtc)]
    stobe_wtc = [StoBErr(*x) for x in zip(sy_wtc, y_wtc, sye_wtc, sye_wtc)]
    printYieldAndErrTable2(srNames, stob_org, stob_wtc, stobe_org, stobe_wtc)

if __name__ == '__main__':

    f1 = r.TFile('../StopLooper/output/temp14/allBkg_25ns.root')
    f2 = r.TFile('../StopLooper/output/temp14/Signal_T2tt.root')
    f3 = r.TFile('../StopLooper/output/newbin0/allBkg_25ns.root')
    f4 = r.TFile('../StopLooper/output/newbin0/SMS_T2tt.root')

    srNames = ['srA', 'srB', 'srC', 'srD', 'srE', 'srF', 'srG', 'srH', 'srI',]

    bkgs = ['2lep', '1lepW', '1lepTop', 'Znunu']
    tab = Table()
    tab.set_column_names(['srName']+srNames)
    for bg in bkgs:
        ylds, errs = getYieldAndErrsFromTopoSRs(f1, srNames, '_'+bg)
        tab.add_row([bg] + [E(y,e).round(2) for y, e in zip(ylds, errs)])
    tab.add_line()
    bgylds, bgerrs = getYieldAndErrsFromTopoSRs(f1, srNames)
    bylds_org = [E(y,e).round(2) for y, e in zip(bgylds, bgerrs)]
    tab.add_row(['Total background'] + bylds_org)
    tab.add_line()
    sigylds, sigerrs = getYieldAndErrsFromTopoSRs(f2, srNames, '_1200_50')
    tab.add_row(['T2tt_1200_50'] + [E(y,e).round(2) for y, e in zip(sigylds, sigerrs)])

    # tab.print_table()

    srNames  = ['srA', 'srB', 'srC', 'srD', 'srE', 'srF', 'srG', 'srH']
Exemple #8
0
def write_table(data,
                bgs,
                outname=None,
                signal=None,
                extra_hists=[],
                precision=2,
                sep=u"\u00B1".encode("utf-8"),
                binedge_fmt="{}-{}",
                fix_negative=True,
                binlabels=[],
                show_errors=True,
                cell_callback=None):
    tab = Table()
    sumbgs = sum(bgs)
    databg = data / sumbgs
    if signal is not None:
        procs = bgs + [sumbgs, data, databg, signal]
        cnames = [bg.get_attr("label")
                  for bg in bgs] + ["Total bkg", "Data", "Data/bkg", "tttt"]
    else:
        procs = bgs + [sumbgs, data, databg]
        cnames = [bg.get_attr("label")
                  for bg in bgs] + ["Total bkg", "Data", "Data/bkg"]
    for eh in extra_hists:
        procs.append(eh)
        cnames.append(eh.get_attr("label"))
    tab.set_column_names(["bin"] + cnames)
    if outname:
        sep = "+-"
    binpairs = zip(data.edges[:-1], data.edges[1:])
    #tab.set_theme_basic()
    tab.set_theme_latex()
    for ibin, binrow in enumerate(binpairs):
        row = [("[%s]" % binedge_fmt).format(binrow[0], binrow[1])]
        if ibin < len(binlabels):
            row = [binlabels[ibin]]
        for iproc, proc in enumerate(procs):
            if fix_negative:
                cent = max(proc.counts[ibin], 0.)
            else:
                cent = proc.counts[ibin]
            err = proc.errors[ibin]
            if show_errors:
                tmp = ("{0:5.%if} {1}{2:%i.%if}" %
                       (precision, precision + 3, precision)).format(
                           cent, sep, err)
            else:
                tmp = ("{0:5.%if}" % (precision)).format(cent)
            if cell_callback: tmp = cell_callback(tmp)
            row.append(tmp)
        tab.add_row(row)
    tab.add_line()

    row = ["total"]
    for iproc, proc in enumerate(procs):
        if iproc == len(procs) - (1 + (signal is not None) + len(extra_hists)):
            totbg = E(sum(sumbgs.counts), np.sum(sumbgs.errors**2.)**0.5)
            totdata = E(sum(data.counts))
            ratio = totdata / totbg
            cent, err = ratio[0], ratio[1]
            precision = max(precision, 2) if precision != 0 else 0
        else:
            cent = sum(proc.counts)
            err = np.sum(proc.errors**2.)**0.5
        if show_errors:
            tmp = ("{0:5.%if} {1}{2:%i.%if}" %
                   (precision, precision + 3, precision)).format(
                       cent, sep, err)
        else:
            tmp = ("{0:5.%if}" % (precision)).format(cent)
            if cell_callback: tmp = cell_callback(tmp)
        row.append(tmp)
    tab.add_row(row)

    if outname:
        with open(outname, "w") as fhout:
            # towrite = "".join(tab.get_table_string(show_row_separators=False,show_alternating=False))
            towrite = "".join(
                tab.get_table_strings(show_row_separators=False,
                                      show_alternating=False))
            fhout.write(towrite)
            parts = towrite.split("\n")
            header = parts[:3]
            binparts = parts[3:-4]
            total = parts[-4:-1]
            table_info = {
                "header": "<br>".join(header),
                "bins": binparts,
                "total": "<br>".join(total)
            }
            return table_info
    return tab
Exemple #9
0
    def main():

        p.makedir("./plots/{}/{}/{}/".format(input_ntup_tag, analysis_tag,
                                             "ss" if isSS else "3l"))
        p.copy_nice_plot("./plots/{}/{}/{}/".format(input_ntup_tag,
                                                    analysis_tag,
                                                    "ss" if isSS else "3l"))

        mu_isos()
        el_isos()
        mu_fakerate()
        el_fakerate()

        ofile = r.TFile(
            "histmap/fakerate_ss.root" if isSS else "histmap/fakerate_3l.root",
            "recreate")

        prescale_muHLT17 = get_prescale("TwoMuHLT17__Mll")
        prescale_elHLT23 = get_prescale("TwoElHLT23__Mll")
        print prescale_muHLT17, prescale_elHLT23
        #prescale_muHLT17 = 1
        #prescale_elHLT23 = 1

        plot("OneEl__nj", prescale_muHLT17)
        plot("OneEl__pt", prescale_muHLT17)
        plot("OneEl__MET", prescale_muHLT17)
        plot("OneElHighMET__MT", prescale_elHLT23)
        plot("OneElEWKCR__MT", prescale_elHLT23)
        plot("OneElEWKCR__MET", prescale_elHLT23)
        plot("OneElEWKCR__Nvtx", prescale_elHLT23)
        plot("OneElMR__MT", prescale_elHLT23)
        plot("OneElMR__ptcorr", prescale_elHLT23)
        plot("OneElTightMR__ptcorr", prescale_elHLT23)

        el_sf, el_sferr = ewksf("OneElEWKCR__MT", prescale_elHLT23)
        plot("OneElMR__ptcorrvarbin", prescale_elHLT23, el_sf)
        plot("OneElTightMR__ptcorrvarbin", prescale_elHLT23, el_sf)
        fakerate("OneElTightMR__pt", "OneElMR__pt", prescale_elHLT23, el_sf,
                 el_sferr, ofile)
        fakerate("OneElTightMR__ptcorrvarbin", "OneElMR__ptcorrvarbin",
                 prescale_elHLT23, el_sf, el_sferr, ofile)
        fakerate("OneElTightMR__etavarbin", "OneElMR__etavarbin",
                 prescale_elHLT23, el_sf, el_sferr, ofile)
        fakerate("OneElTightMR__ptcorretarolled", "OneElMR__ptcorretarolled",
                 prescale_elHLT23, el_sf, el_sferr, ofile)
        fakerate("OneElTightMR__ptcorretarolledcoarse",
                 "OneElMR__ptcorretarolledcoarse", prescale_elHLT23, el_sf,
                 el_sferr, ofile)

        plot("OneMu__nj", prescale_muHLT17)
        plot("OneMu__pt", prescale_muHLT17)
        plot("OneMu__MET", prescale_muHLT17)
        plot("OneMuHighMET__MT", prescale_muHLT17)
        plot("OneMuEWKCR__MT", prescale_muHLT17)
        plot("OneMuEWKCR__MET", prescale_muHLT17)
        plot("OneMuEWKCR__Nvtx", prescale_muHLT17)
        plot("OneMuMR__MT", prescale_muHLT17)
        plot("OneMuMR__ptcorr", prescale_muHLT17)
        plot("OneMuTightMR__ptcorr", prescale_muHLT17)
        #    plot("OneMuMR__lepmotherid"       , prescale_muHLT17)
        #    plot("MuClosureTight__mumotherid" , prescale_muHLT17)
        #    plot("OneMuMR__lepmotherid"       , prescale_muHLT17)
        #    plot("MuClosureLoose__mumotherid" , prescale_muHLT17)
        #    plot("MuClosureLooseEta0Pt1__mumotherid" , prescale_muHLT17)
        #    plot("MuClosureLooseEta1Pt1__mumotherid" , prescale_muHLT17)

        plot("OneMuEWKCREta0Pt3__MT", prescale_muHLT17)
        plot("OneMuEWKCREta1Pt3__MT", prescale_muHLT17)

        mu_sf, mu_sferr = ewksf("OneMuEWKCR__MT", prescale_muHLT17)
        plot("OneMuMR__ptcorrvarbin", prescale_muHLT17)
        plot("OneMuTightMR__ptcorrvarbin", prescale_muHLT17)
        fakerate("OneMuTightMR__pt", "OneMuMR__pt", prescale_muHLT17, mu_sf,
                 mu_sferr, ofile)
        fakerate("OneMuTightMR__ptcorrvarbin", "OneMuMR__ptcorrvarbin",
                 prescale_muHLT17, mu_sf, mu_sferr, ofile)
        fakerate("OneMuTightMR__etavarbin", "OneMuMR__etavarbin",
                 prescale_muHLT17, mu_sf, mu_sferr, ofile)
        fakerate("OneMuTightMR__ptcorretarolled", "OneMuMR__ptcorretarolled",
                 prescale_muHLT17, mu_sf, mu_sferr, ofile)
        fakerate("OneMuTightMR__ptcorretarolledcoarse",
                 "OneMuMR__ptcorretarolledcoarse", prescale_muHLT17, mu_sf,
                 mu_sferr, ofile)
        x = Table()
        x.add_column("type of scale factors", [
            "Prescale $e$",
            "Prescale $\mu$",
            "$e$ prompt SF",
            "$\mu$ prompt SF",
        ])
        x.add_column("scale factor values", [
            "{:.2f}".format(prescale_elHLT23),
            "{:.2f}".format(prescale_muHLT17), "{:.2f} $\\pm$ {:.2f}".format(
                el_sf, el_sferr), "{:.2f} $\\pm$ {:.2f}".format(
                    mu_sf, mu_sferr)
        ])
        ru.write_tex_table(x,
                           "./plots/{}/{}/{}/scalefactors.tex".format(
                               input_ntup_tag, analysis_tag,
                               "ss" if isSS else "3l"),
                           caption="Scale factors used for {} channel".format(
                               "same-sign" if isSS else "three-lepton"))
        print "PRINTHERE", "prescale_muHLT17 , prescale_elHLT23,", prescale_muHLT17, prescale_elHLT23
        print "PRINTHERE", "el_sf", el_sf, el_sferr
        print "PRINTHERE", "mu_sf", mu_sf, mu_sferr
        closure_plot("MuClosureTight__MT", "MuClosureTightPredict__MT")
        closure_plot("ElClosureTight__MT", "ElClosureTightPredict__MT")
        closure_plot("MuClosureTightBVeto__MT",
                     "MuClosureTightBVetoPredict__MT")
        closure_plot("ElClosureTightBVeto__MT",
                     "ElClosureTightBVetoPredict__MT")
        closure_plot("MuClosureTightNbgeq2__MT",
                     "MuClosureTightNbgeq2Predict__MT")
        closure_plot("ElClosureTightNbgeq2__MT",
                     "ElClosureTightNbgeq2Predict__MT")
        closure_plot("MuClosureTightNbgeq1__MT",
                     "MuClosureTightNbgeq1Predict__MT")
        closure_plot("ElClosureTightNbgeq1__MT",
                     "ElClosureTightNbgeq1Predict__MT")
        closure_plot("ElClosureTightBVeto__Mll",
                     "ElClosureTightBVetoPredict__Mll", 6)
        closure_plot("ElClosureTightBVeto__MET",
                     "ElClosureTightBVetoPredict__MET", 6)
        closure_plot("ElClosureTightBVeto__MT",
                     "ElClosureTightBVetoPredict__MT", 6)
        closure_plot("ElClosureTightBVeto__ptcorr",
                     "ElClosureTightBVetoPredict__ptcorr", 6)
        closure_plot("MuClosureTightBVeto__Mll",
                     "MuClosureTightBVetoPredict__Mll", 6)
        closure_plot("MuClosureTightBVeto__MET",
                     "MuClosureTightBVetoPredict__MET", 6)
        closure_plot("MuClosureTightBVeto__MT",
                     "MuClosureTightBVetoPredict__MT", 6)
        closure_plot("MuClosureTightBVeto__ptcorr",
                     "MuClosureTightBVetoPredict__ptcorr", 6)
Exemple #10
0
def printTableDataDriven(f, srNames, crname=''):
    if crname != '' and crname[-1] != ' ': crname += ' '

    tab = Table()
    preds = getYieldEInTopoBins(f, srNames, 'metbins')
    alpha = getYieldEInTopoBins(f, srNames, 'alphaHist')
    purity = getYieldEInTopoBins(f, srNames, 'CRpurity')
    yMC_SR = getYieldEInTopoBins(f, srNames, 'MCyields_SR')
    yMC_CR = getYieldEInTopoBins(f, srNames, 'MCyields_CR')
    yld_CR = getYieldEInTopoBins(f, srNames, 'datayields_CR')

    metrange = getBinningFromTopoSRs(f, srNames)
    tab.add_column(
        'SR name',
        sum([[sr] * n for sr, n in zip(srNames, map(len, preds))], []))
    tab.add_column('MET [GeV]',
                   [m[0] + ' -- ' + m[1] for m in sum(metrange, [])])
    tab.add_column(crname + 'MC SR', [y.round(2) for y in sum(yMC_SR, [])])
    tab.add_column(crname + 'MC CR', [y.round(2) for y in sum(yMC_CR, [])])
    tab.add_column(crname + 'Purity', [y.round(2) for y in sum(purity, [])])
    tab.add_column(crname + 'TF_{CR}^{SR}',
                   [y.round(2).val for y in sum(alpha, [])])
    tab.add_column(crname + 'data CR', [y.round(2) for y in sum(yld_CR, [])])
    tab.add_column(crname + 'R_{CR}(data/MC)',
                   [(d / m).round(2)
                    for d, m in zip(sum(yld_CR, []), sum(yMC_CR, []))])
    tab.add_column(crname + 'Prediction', [y.round(2) for y in sum(preds, [])])

    tab.print_table()

    return tab
Exemple #11
0
def printTableWithMETextrpInfo(f1, f2, srNames, crname=''):
    if crname != '' and crname[-1] != ' ': crname += ' '

    tab = Table()
    preds1 = getYieldEInTopoBins(f1, srNames, 'metbins')
    alpha1 = getYieldEInTopoBins(f1, srNames, 'alphaHist')
    purity = getYieldEInTopoBins(f1, srNames, 'CRpurity')
    yMC_SR = getYieldEInTopoBins(f1, srNames, 'MCyields_SR')
    yMC_CR = getYieldEInTopoBins(f1, srNames, 'MCyields_CR')
    yld_CR = getYieldEInTopoBins(f1, srNames, 'datayields_CR')

    yMCaCR = getYieldEInTopoBins(f2, srNames, 'MCyields_CR')
    yldaCR = getYieldEInTopoBins(f2, srNames, 'datayields_CR')
    preds2 = getYieldEInTopoBins(f2, srNames, 'metbins')
    alpha2 = getYieldEInTopoBins(f2, srNames, 'alphaHist')

    metrange = getBinningFromTopoSRs(f1, srNames)

    tab.add_column(
        'SR name',
        sum([[sr] * n for sr, n in zip(srNames, map(len, preds1))], []))
    tab.add_column('MET range [GeV]',
                   [m[0] + ' -- ' + m[1] for m in sum(metrange, [])])
    tab.add_column(crname + 'MC SR', [y.round(2) for y in sum(yMC_SR, [])])
    tab.add_column(crname + 'MC CR raw', [y.round(2) for y in sum(yMC_CR, [])])
    tab.add_column(crname + 'Purity',
                   [y.round(2).val for y in sum(purity, [])])
    tab.add_column(crname + 'TF_{CR}^{SR}',
                   [y.round(2) for y in sum(alpha1, [])])
    tab.add_column(crname + 'data CR raw',
                   [y.round(2) for y in sum(yld_CR, [])])
    tab.add_column(crname + 'CR data/MC raw',
                   [(d / m).round(2)
                    for d, m in zip(sum(yld_CR, []), sum(yMC_CR, []))])
    tab.add_column(crname + 'Pred (no extrp)',
                   [y.round(2) for y in sum(preds1, [])])

    tab.add_column(crname + 'MC CR comb',
                   [y.round(2) for y in sum(yMCaCR, [])])
    tab.add_column(crname + 'data CR comb',
                   [y.round(2) for y in sum(yldaCR, [])])
    tab.add_column(crname + 'CR data/MC comb',
                   [(d / m).round(2)
                    for d, m in zip(sum(yldaCR, []), sum(yMCaCR, []))])
    tab.add_column(crname + 'Pred (MET extrp)',
                   [y.round(2) for y in sum(preds2, [])])

    tab.print_table()

    return tab