def get_hlt_prescales(runs): fn = 'prescales_temp/hltprescales.gzpickle' if os.path.isfile(fn): return from_pickle(fn) prescales = defaultdict(lambda: defaultdict(lambda: defaultdict(set))) l = len(runs) for i,run in enumerate(runs): print 'get_hlt_prescales run %i (%i/%i)' % (run, i+1, l) res = lumi_context('hltbyls', {run:None})[run] for line in res: ls = line[0] for hlt_name, prescale, duh, duh2 in line[-1]: prescales[hlt_name][prescale][run].add(ls) for hlt_name, duh in prescales.iteritems(): for prescale, duh2 in duh.iteritems(): for run in duh2: duh2[run] = collapse(duh2[run]) duh[prescale] = dict(duh2) prescales[hlt_name] = dict(duh) prescales = dict(prescales) to_pickle(prescales, fn) return prescales
def strip(cls, fn, new_fn): lls = from_pickle(fn, comp=True) for ll in lls: del ll.time del ll.beam_status del ll.energy to_pickle(lls, new_fn, comp=True)
def get_hlt_prescales(runs): fn = 'prescales_temp/hltprescales.gzpickle' if os.path.isfile(fn): return from_pickle(fn) prescales = defaultdict(lambda: defaultdict(lambda: defaultdict(set))) l = len(runs) for i, run in enumerate(runs): print 'get_hlt_prescales run %i (%i/%i)' % (run, i + 1, l) res = lumi_context('hltbyls', {run: None})[run] for line in res: ls = line[0] for hlt_name, prescale, duh, duh2 in line[-1]: prescales[hlt_name][prescale][run].add(ls) for hlt_name, duh in prescales.iteritems(): for prescale, duh2 in duh.iteritems(): for run in duh2: duh2[run] = collapse(duh2[run]) duh[prescale] = dict(duh2) prescales[hlt_name] = dict(duh) prescales = dict(prescales) to_pickle(prescales, fn) return prescales
def one_from_r(ex, name, csvs=True): if csvs: def read_csv(fn): lines = [x.strip() for x in open(fn).read().replace('"', '').split('\n') if x.strip()] lines.pop(0) vs = [] for line in lines: ws = [float(x) for x in line.split(',')] ws.pop(0) if len(ws) == 1: ws = ws[0] vs.append(ws) return vs x = read_csv('%s_x.csv' % ex) y = read_csv('%s_y.csv' % ex) z = read_csv('%s_z.csv' % ex) else: x,y,z = from_pickle('/uscms/home/tucker/afshome/%sxyz.gzpickle' % ex) assert sorted(x) == x assert sorted(y) == y nx = len(x) ny = len(y) x.append(x[-1] + (x[-1] - x[-2])) y.append(y[-1] + (y[-1] - y[-2])) #print ex, nx, ny x = array('d', x) y = array('d', y) h = ROOT.TH2F(name, '', len(x)-1, x, len(y)-1, y) h.SetStats(0) for ix in xrange(1, nx+1): for iy in xrange(1, ny+1): h.SetBinContent(ix, iy, z[ix-1][iy-1]) return h
def get_lumibylses(runs): fn = 'prescales_temp/lumibylses.gzpickle' if os.path.isfile(fn): return from_pickle(fn) d = {} for run in runs: d.update(parse_lumibyls(run)) to_pickle(d, fn) return d
def get_hlt_prescales_collapsed(runs, hlt_prescales=None): fn = 'prescales_temp/hltprescales_collapsed.gzpickle' if os.path.isfile(fn): return from_pickle(fn) if hlt_prescales is None: hlt_prescales = get_hlt_prescales(runs) hlt_prescales_collapsed = collapse_versions(hlt_prescales) to_pickle(hlt_prescales_collapsed, fn) return hlt_prescales_collapsed
def get_menus(runs): fn = 'prescales_temp/menus.gzpickle' if os.path.isfile(fn): return from_pickle(fn) menus = defaultdict(set) for run in runs: menu = lumi_context('hltmenu', {run:None})[run] for hlt_path, l1_seed, l1_bit in menu: assert l1_bit == 'n/a' or l1_seed == l1_bit menu = tuple([(hlt_path, l1_seed) for hlt_path, l1_seed, l1_bit in menu]) menus[menu].add(run) to_pickle(menus, fn) return menus
def get_menus(runs): fn = 'prescales_temp/menus.gzpickle' if os.path.isfile(fn): return from_pickle(fn) menus = defaultdict(set) for run in runs: menu = lumi_context('hltmenu', {run: None})[run] for hlt_path, l1_seed, l1_bit in menu: assert l1_bit == 'n/a' or l1_seed == l1_bit menu = tuple([(hlt_path, l1_seed) for hlt_path, l1_seed, l1_bit in menu]) menus[menu].add(run) to_pickle(menus, fn) return menus
def load(cls, fn, is_csv=False): if is_csv or fn.endswith('.csv'): return cls.load_csv(fn) return from_pickle(fn, comp=True)
def plot(): set_style() ps = plot_saver(plot_dir('pdfuncerts'), size=(600, 600), log=False) ps.c.SetLeftMargin(0.11) parabs = { 300: ((300, 0.06), (585, 0.02), (877, 0.009)), 1000: ((300, 0.03), (550, 0.01), (873, 0.006)), 10000: ((300, 0.04), (550, 0.015), (877, 0.005)), } lines = { 300: ((877, 0.009), (3000, 0.075)), 1000: ((873, 0.006), (3000, 0.05)), 10000: ((877, 0.005), (3000, 0.035)), } def fparab(points, _n=[0]): (x1, y1), (x2, y2), (x3, y3) = sorted(points) c1 = y1 / (x1 - x2) / (x1 - x3) c2 = y2 / (x2 - x1) / (x2 - x3) c3 = y3 / (x3 - x1) / (x3 - x2) a = c1 + c2 + c3 b = -((c2 + c3) * x1 + (c1 + c3) * x2 + (c1 + c2) * x3) c = c1 * x2 * x3 + c2 * x1 * x3 + c3 * x1 * x2 fm = '%.4g*x**2 + %.4g*x + %.4g' % (a, b, c) fcn = ROOT.TF1('parab%i' % _n[0], fm, x1, x3) _n[0] += 1 return fcn, fm def fline(points, _n=[0]): (x1, y1), (x2, y2) = sorted(points) m = (y2 - y1) / (x2 - x1) b = y1 - m * x1 fm = '%.4g*x + %.4g' % (m, b) fcn = ROOT.TF1('line%i' % _n[0], fm, x1, x2) _n[0] += 1 return fcn, fm samples = defaultdict(list) for s in from_pickle('pdfuncerts.gzpickle'): tau = int(s.name.split('tau')[1].split('um')[0]) samples[tau].append(s) for tau in 300, 1000, 10000: gneu, gstop = [], [] for s in sorted(samples[tau], key=lambda s: s.name): mass = float(s.name.split('_M')[1].split('_')[0]) num, den = s.nominal / s.w, s.norm e, ee = wilson_score_vpme(num, den) st = num**-0.5 print s.name, tau, mass, s.ctlrelmd, st, s.nominal, e, ee, num, den, clopper_pearson( num, den) (gneu if 'neu' in s.name else gstop).append((mass, s.ctlrelmd, ee)) gneu, gstop = tgraph(gneu) if gneu else None, tgraph(gstop) for g, c in (gneu, 4), (gstop, 2): if not g: continue g.SetTitle(';mass (GeV);relative acc. unc.') g.SetMarkerColor(c) g.SetLineColor(c) g.SetMarkerStyle(20) gstop.Draw('AP') if gneu: gneu.Draw('P') gstop.GetYaxis().SetRangeUser(0, 0.08) leg = ROOT.TLegend( 0.249, 0.742, 0.599, 0.852, '#tau = %s' % { 300: '300 #mum', 1000: '1 mm', 10000: '10 mm' }[tau]) leg.SetBorderSize(0) leg.AddEntry(gstop, 'dijet', 'LP') if gneu: leg.AddEntry(gneu, 'multijet', 'LP') leg.Draw() prb, pf = fparab(parabs[tau]) prb.Draw('same') lin, lf = fline(lines[tau]) lin.Draw('same') print tau, pf, lf ps.save('tau%06i' % tau)
def load(cls, fn, is_csv=False): if is_csv or fn.endswith('.csv') or fn.endswith('.csv.gz'): return cls.load_csv(fn) return from_pickle(fn, comp=True)
def plot(): set_style() ps = plot_saver(plot_dir('ren_fac_%s' % year), size=(600, 600), log=False) ps.c.SetLeftMargin(0.11) samples = defaultdict(list) for s in from_pickle('ren_fac.gzpickle'): tau = int(s.name.split('tau')[1].split('um')[0]) samples[tau].append(s) for tau in 300, 1000, 10000: gneu, gstop = [], [] for s in sorted(samples[tau], key=lambda s: s.name): mass = float(s.name.split('_M')[1].split('_')[0]) num, den = s.nominal / s.w, s.norm e, ee = wilson_score_vpme(num, den) st = num**-0.5 (gneu if 'neu' in s.name else gstop).append((mass, s.ctlrelmd, ee)) gneu, gstop = tgraph(gneu) if gneu else None, tgraph(gstop) for g, c in (gneu, 4), (gstop, 2): if not g: continue g.SetTitle(';mass (GeV);relative acc. unc.') g.SetMarkerColor(c) g.SetLineColor(c) g.SetMarkerStyle(20) gstop.Draw('AP') if gneu: gneu.Draw('P') gstop.GetYaxis().SetRangeUser(0, 0.1) leg = ROOT.TLegend( 0.249, 0.742, 0.599, 0.852, '#tau = %s' % { 300: '300 #mum', 1000: '1 mm', 10000: '10 mm' }[tau]) leg.SetBorderSize(0) leg.AddEntry(gstop, 'dijet', 'LP') if gneu: leg.AddEntry(gneu, 'multijet', 'LP') leg.Draw() fitopt = "RSQ0" fitdict = { (300, 500): "pol1", (500, 1000): "pol1", (1000, 3000): "pol0" } for key in fitdict: fitfunc = fitdict[key] fitrange = key gstop.Fit(fitfunc, fitopt, "", *fitrange) for kind in ["mfv_neu", "mfv_stopdbardbar"]: if kind == "mfv_neu": g = gneu elif kind == "mfv_stopdbardbar": g = gstop else: os.abort("invalid kind!") outstr = "('%s', %s, '%s') : lambda x: \n" % (kind, tau, year) fit_M_300_to_500 = g.Fit("pol1", fitopt, "", 300, 500) fit_M_500_to_1000 = g.Fit("pol1", fitopt, "", 500, 1000) fit_M_1000_to_3000 = g.Fit("pol0", fitopt, "", 1000, 3000) outstr += "%E + %E*x if x < 500 " % (fit_M_300_to_500.Parameter(0), fit_M_300_to_500.Parameter(1)) outstr += "else %E + %E*x if x < 1000 " % ( fit_M_500_to_1000.Parameter(0), fit_M_500_to_1000.Parameter(1)) outstr += "else %E," % (fit_M_1000_to_3000.Parameter(0)) print outstr ps.save('tau%06i' % tau)