def print_all_selection(s,twiki=False, fmt = None): stream = s.stream if twiki: print twikirow('Region/Sample',[group.name for group in stream.groups]) else: print row('Region/Sample',[group.name for group in stream.groups],fcol=fcol,col=col, fmt = fmt) sf = ROOT.TFile('%s/%s_regions.root'%(s.OUTPUTDIR,s.stream.name)) if not fmt: print '-'*(fcol+col*len(s.groups)) print_region(sf,stream.select,s.groups,entry = Drawer.upsilon, eff = False,twiki=twiki, f = False, fmt = fmt)
def KS_test(sys, region): rf = tesf sf = tesf gsys = {} for group in groups: rf.cd() print group.name if group.name == 'Wlnu': if 'OS' in region: wregion = 'WCR_OS' elif 'SS' in region: wregion = 'WCR_SS' nom = hist.get_dataW(rf, mu, entry, wregion, verbose=False) else: nom = rf.Get('h_upsilon_%s_%s' % (group.name, region)) #print group.name, nom.Integral() #continue for i, s in enumerate(sys): #if 'PU' in s: continue for ud in ['UP', 'DOWN']: #ud = ud.lower() if not s + '_' + ud in gsys: gsys[s + '_' + ud] = [] if not nom.Integral(): gsys[s + '_' + ud].append('-') continue if group.name == 'Wlnu': sf.cd() shift = hist.get_dataW(rf, mu, entry, wregion, verbose=False, sf=sf, sysf=s + '_' + ud) else: sf.cd() shift = sf.Get('h_upsilon_%s_%s_%s_%s' % (group.name, region, s, ud)) print shift.Integral() gsys[s + '_' + ud].append(nom.KolmogorovTest(shift)) print row('', [group.name for group in groups], fcol=15, col=15, fmt='latex') for s in sys: #if 'PU' in s: continue for ud in ['UP', 'DOWN']: #ud = ud.lower() what = s + '_' + ud for i, k in enumerate(gsys[what]): if k < 0.9: gsys[what][i] = '\\textcolor{red}{%.4f}' % k whatp = '\\textcolor{red}{%s}' % what else: whatp = what print row(whatp, gsys[what], fcol=15, col=10, fmt='latex')
def reco(): mode = '0' for entry in ['', '20', '40', '60']: entry = 'upsilon' + entry print row(entry, [ sens_reco(-1, mode, entry=entry), sens_reco(0, mode, entry=entry), sens_reco(1, mode, entry=entry) ], fmt='latex')
def print_regions(s,twiki=False, fmt = None, f = False): stream = s.stream if twiki: print twikirow('Region/Sample',[group.name for group in stream.groups]) else: print row('Region/Sample',[group.name for group in stream.groups],fcol=fcol,col=col, fmt = fmt) sf = ROOT.TFile('%s/%s_regions.root'%(s.OUTPUTDIR,s.stream.name)) if not fmt: print '-'*(fcol+col*len(stream.groups)) print_region(sf,stream.SR,stream.groups, fmt = fmt, f = f) if not s.control: return 0 for regions in s.control: if not fmt: print '-'*(fcol+col*len(stream.groups)) print_region(sf,regions,stream.groups,twiki=twiki, fmt = fmt, f = f )
def print_region(mu,reg, pdg=False):#,regions,groups,entry=Drawer.upsilon_CR,eff=False,twiki=False, fmt = None, f = False): a = [] rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) regions = getattr(mu,reg) groups = [mu.data, mu.signal, mu.LH, mu.RH, mu.ttbar, mu.Zlljet, mu.Zltau, mu.Wlnu] entry = Drawer.upsilon_CR fmt = '' if pdg: fmt = 'latex' print row('',[group.name for group in groups],fcol=fcol,col=col,fmt=fmt) for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 nEW = 0 for group in groups: if 'Wlnu' in group.name: wreg = region.name.replace('QCD','WCR') wreg = wreg.replace('SR','WCR') #print region h = hist.get_dataW(rf,mu,entry, wreg, where = reg) #print h.Integral() else: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) norm = h.Integral(0,h.GetNbinsX()+1) if group.isData: cutgroup.append(int(norm)) else: #print h.GetName() v,e = hist.IntegralError(h, of = True) if pdg: cutgroup.append( '%g +/- %g' % pdg_simple.pdg_round(v,e)) else: cutgroup.append( '%.2f +/- %.2f' % (v,e)) n.append(norm) if group.isData: nData = norm elif 'LH' in group.name: nL = norm elif 'RH' in group.name: nR = norm elif 'Ztautau' in group.name: nS = norm elif 'Wlnu' not in group.name: nEW += norm if i == 0: a = list(n); l=a #if twiki: print twikirow(region.name,cutgroup) print row(region.name,cutgroup,fcol=fcol,col=col, fmt = fmt)
def table_EW_Samples(): col = 15; fcol = 15; fmt = "latex" groups = [Groups.AlpgenPythiaWenu,Groups.AlpgenPythiaWmunu,Groups.AlpgenPythiaWtaunu,Groups.AlpgenPythiaZee,Groups.AlpgenPythiaZmumu] nP = ["Np0","Np1","Np2","Np3","Np4","Np5"] for g in groups: for nPi,gi in zip(nP,g): gi.load() print row("\\%s+%sjets"%(g.name,Npi[-1]), [int(gi.totalEvents), gi.xstr], fcol=fcol,col=col,fmt=fmt) gi.close() tt = Groups.Powheg_ttbar tt.load() print row("\\ttbar", [tt.totalEvents, tt.xstr],fcol=fcol,col=col,fmt="latex" ) tt.close()
def true(): mode = '4' print row('', [sens_true(-1, mode), sens_true(0, mode), sens_true(1, mode)], fmt='latex') entry = 'upsilon20' print row(entry, [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex') entry = 'upsilon60' print row('upsilon40', [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex') entry = 'upsilon40' print row('upsilon60', [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex')
def get_OS_SS(rf, entry, group, region, kOS=ErrorFloat(1.0, 0.0), kSS=ErrorFloat(1.0, 0.0), rQCD=None, verbose=False): # you can include rQCD in kSS if rQCD: kSS = kSS * rQCD OS = get_OS(rf, entry, group, region, kOS=kOS) SS = get_SS(rf, entry, group, region, kSS=kSS) os_ = '%.0f +/- %.0f' % (IntegralError(OS)[0], IntegralError(OS)[1]) ss_ = '%.0f +/- %.0f' % (IntegralError(SS)[0], IntegralError(SS)[1]) OS.Add(SS, -1.0) os_ss_ = '%.0f +/- %.0f' % (IntegralError(OS)[0], IntegralError(OS)[1]) if verbose: print row(group.name, [os_, ss_, os_ss_], fcol=fcol, col=col) return OS
def print_latex_full(): print '\\documentclass[11pt,oneside,a4paper]{article}' print '\\usepackage{rotating}' print '\\usepackage[usenames]{color}' print '\\begin{document}' for sys in lepsys + tausys + lepES + JES: print '\\begin{table}' print '\\centering' print '\\caption{\\bf{%s}}' % sys.replace('_', '\_') print '\\begin{tabular}{lrrrrrr}' print row('', [group.name for group in groups], fmt='latex') for region in [ 'SR_OS', 'SR_SS', 'WCR_OS', 'WCR_SS', 'QCD_OS', 'QCD_SS' ]: NormStatFull(sys, region) print '\\end{tabular}' print '\\end{table}' print '\\end{document}'
def NormStatFull(s, region): rf = regf sf = tesf sigma_up = [] sigma_down = [] intervals = [] entry = Drawer.upsilon_20 if 'SR' not in region: entry = Drawer.upsilon_CR for group in groups: rf.cd() nom = rf.Get('h_%s_%s_%s' % (entry.name, group.name, region)) nom = nom.Integral() if not nom: sigma_up.append('-') sigma_down.append('-') intervals.append('-') continue for ud in ['UP', 'DOWN']: #ud = ud.lower() #if group.name == 'Wlnu': # sf.cd() # shift = hist.get_dataW(rf, mu, entry, wregion, verbose = False, sf = sf, sysf = s+'_'+ud) #else: sf.cd() shift = sf.Get('h_%s_%s_%s_%s_%s' % (entry.name, group.name, region, s, ud)) sigmastat = abs(shift.Integral() - nom) / math.sqrt(nom) if ud == 'UP': sigma_up.append(sigmastat) shift_up = shift.Integral() else: sigma_down.append(sigmastat) shift_down = shift.Integral() s_up = shift_up / nom s_down = shift_down / nom if (s_up < 1 and s_down < 1) or (s_up > 1 and s_down > 1): intervals.append('\\textcolor{red}{%.3f,%.3f}' % (s_down, s_up)) else: intervals.append('%.3f,%.3f' % (s_down, s_up)) print row(region, ['' for group in groups], fmt='latex') print row('$\\sigma_up$', sigma_up, fmt='latex') print row('$\\sigma_down$', sigma_down, fmt='latex') print row('down,up', intervals, fmt='latex') print '\n\\hline'
def sim_modes(sim="Reco"): rf = ROOT.TFile('~/analysis/truth/cxx/thesis/PythiaFull30.root') entries = ['upsilon','upsilon20','upsilon40','upsilon60'] modes = ['0','3','4','5'] fracs = [] for i,mode in enumerate(modes): fracs.append([]) for j,entry in enumerate(entries): fracs[i].append( ErrorFloat(*hist.IntegralError(rf.Get('%s_%s_%s_LH' % (sim,entry,mode))))) fracs[i].append(ErrorFloat(*hist.IntegralError(rf.Get('%s_%s_%s_RH' % (sim,entry,mode))))) nTotal = list(fracs[0]) nOther = None for i,m in enumerate(modes): if i == 1: nOther = list(fracs[0]) for j in xrange(len(fracs[i])): fracs[i][j] = fracs[i][j]/nTotal[j] fracs[i][j] = round(fracs[i][j].val,2) if nOther: nOther[j]-=fracs[i][j] print row( modelegend[m], fracs[i], fmt='latex' , flt = '%.2f') print row('Other',nOther, fmt = 'latex', flt = '%.2f')
def table_Signal_Samples(): col = 15 fcol = 15 fmt = "latex" xsec_total = 0 g = Groups.Ztautau gL = Groups.LeftZtautau gR = Groups.RightZtautau Nl = ErrorFloat(0,0) Nr = ErrorFloat(0,0) for s in g: xsec_total+=s.xsec NL = 0 NR = 0 pol = 0 nP = ["Np0","Np1","Np2","Np3","Np4","Np5"] for nPi,gi,gLi,gRi in zip(nP,g,gL,gR): gi.load(); giN = gi.totalEvents; gi.close() gLi.load(); gLiN = gLi.totalEvents; gLi.close() gRi.load(); gRiN = gRi.totalEvents; gRi.close() nL = ErrorFloat(gLiN,math.sqrt(gLiN)) nR = ErrorFloat(gRiN,math.sqrt(gRiN)) pol += (gRiN -gLiN)/(giN) *gi.xsec/xsec_total NL += gLiN*gi.xsec/xsec_total/(gLiN+gRiN) NR += gRiN*gi.xsec/xsec_total/(gLiN+gRiN) Nl += nL*ErrorFloat(gi.xsec/xsec_total,0)/(nL+nR) Nr += nR*ErrorFloat(gi.xsec/xsec_total,0)/(nL+nR) #print nPi, (gRiN -gLiN)/(giN) print row(nPi, [int(giN),int(gLiN),int(gRiN),gi.xstr],fcol=fcol,col=col,fmt=fmt)
def backgrounds(mu): rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR,mu.name)) #rf = ROOT.TFile('~/panalysis/output/%s_Zpt_NoWeight/%s_regions.root' % (mu.name,mu.name)) eff = True f = False fcol=15;col=15;fmt="latex" entry = Drawer.visMass k_entry = Drawer.upsilon_CR regions = mu.cutflow #regions = mu.SR verbose = True groups = [mu.Zltau,mu.Zlljet,mu.ttbar,mu.Wlnu] kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose) a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 for group in groups: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) if 'W' not in group.name: cutgroup.append( '%i +/- %i' % hist.IntegralError(h)) n.append(h.Integral(0,h.GetNbinsX()+1)) else: nW = ErrorFloat(*hist.IntegralError(h))*kW_OS cutgroup.append('%i +/- %i' % (nW.val,nW.err)) n.append(nW.val) if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a else: print row(cuts[i]+'\t\t',cutgroup,fcol=fcol,col=col, fmt = fmt) if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def print_region(rf,regions,groups,entry=Drawer.upsilon,eff=False,twiki=False, fmt = None, f = False): a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 for group in groups: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) cutgroup.append( '%i +/- %i' % hist.IntegralError(h)) n.append(h.Integral(0,h.GetNbinsX()+1)) if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: #print group.name mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a if twiki: print twikirow(region.name,cutgroup) else: print row(region.name,cutgroup,fcol=fcol,col=col, fmt = fmt) if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def xfrac(): mode = '3' entry = 'costh' print row(entry, [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex') entry = 'xfrac' print row(entry, [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex') mode = '4' entry = 'costh' print row(entry + ',rho', [ sens_true(-1, mode, entry=entry), sens_true(0, mode, entry=entry), sens_true(1, mode, entry=entry) ], fmt='latex')
def cutflow(mu, pdg = True, smnorm = False): rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) eff = False do_pol = False f = False fcol=25;col=22;fmt="latex" entry = Drawer.upsilon regions = mu.noteflow pt = -0.144 groups = [mu.data, mu.signal, mu.LH, mu.RH, mu.ttbar, mu.Zlljet, mu.Zltau, mu.Wlnu] a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 nL = None for group in groups: if group.isData: h = rf.Get('h_%s_%s_%s' % ('upsilon',group.name,region.name)) else: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) #print 'h_%s_%s_%s' % (entry.name,group.name,region.name) norm = h.Integral(0,h.GetNbinsX()+1) if group.isData: cutgroup.append(int(norm)) else: v,e = hist.IntegralError(h, of = True) if smnorm: if 'LH' in group.name: v = v*sm_lh e = e*sm_lh if 'RH' in group.name: v = v*sm_rh e = e*sm_rh if pdg: cutgroup.append( '%i +/- %i' % pdg_simple.pdg_round(v,e)) else: cutgroup.append( '%.2f +/- %.2f' % (v,e)) n.append(h.Integral(0,h.GetNbinsX()+1)) if 'L' in group.name: nL = norm elif 'R' in group.name: nR = norm if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: #print group.name mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a print row('', [group.name for group in groups], fcol =fcol, col = col) if nL and do_pol: nL = 0.5*(1-pt)*nL nR = 0.5*(1+pt)*nR print row(cuts[i]+'\t\t',cutgroup + [(nR-nL)/(nR+nL)],fcol=fcol,col=col, fmt = fmt) else: #print row(region.name , cutgroup , fcol=fcol,col=col, fmt = fmt) print row(cuts[i], cutgroup , fcol=fcol,col=col, fmt = fmt) #print (nR-nL)/nR if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def NormStat(sys, region='SR_OS'): gsys = {} for group in groups: rf.cd() if group.name == 'Wlnu': if 'OS' in region: wregion = 'WCR_OS' elif 'SS' in region: wregion = 'WCR_SS' nom = hist.get_dataW(rf, mu, entry, wregion, verbose=False) else: nom = rf.Get('h_upsilon_%s_%s' % (group.name, region)) #print group.name,nom.Integral() #continue for i, s in enumerate(syst): if 'PU' in s: continue for ud in ['UP', 'DOWN']: ud = ud.lower() if not s + '_' + ud in gsys: gsys[s + '_' + ud] = [] if not nom.Integral(): gsys[s + '_' + ud].append('-') continue if group.name == 'Wlnu': sf.cd() shift = hist.get_dataW(rf, mu, entry, wregion, verbose=False, sf=sf, sysf=s + '_' + ud) else: sf.cd() shift = sf.Get('h_upsilon_%s_%s_%s_%s' % (group.name, region, s, ud)) sigmastat = abs(shift.Integral() - nom.Integral()) / math.sqrt( nom.Integral()) gsys[s + '_' + ud].append(sigmastat) print row('', [group.name for group in groups], fcol=15, col=15, fmt='latex') for s in syst: if 'PU' in s: continue for ud in ['UP', 'DOWN']: ud = ud.lower() what = s + '_' + ud whatp = what imax = -1 kmax = 0 for i, k in enumerate(gsys[what]): if k > 0.01: gsys[what][i] = '\\textcolor{red}{%.4f}' % k whatp = '\\textcolor{red}{%s}' % what if k > kmax: kmax = k imax = i if k <= 0.: gsys[what][i] = '-' if imax >= 0: gsys[what][imax] = gsys[what][imax].replace('red', 'blue') print row(whatp, gsys[what], fcol=15, col=10, fmt='latex')
def signal_region(mu): #rf = ROOT.TFile('~/panalysis/output/%s_thesis/test/%s_regions.root' % (mu.name,mu.name)) rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) region = 'SR_OS' k_entry = Drawer.upsilon_CR entry = Drawer.upsilon verbose = False fmt = 'latex' kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose) kW_OS_L = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.LH]) kW_OS_R = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.RH]) pdg_simple.testpdg(kW_OS.val,kW_OS.err) print 'kWOS %g +/ %g' % ( pdg_simple.pdg_round(kW_OS.val,kW_OS.err)) EST = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] BG = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] for group in [mu.data,mu.Zlljet,mu.Zltau,mu.ttbar]: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region)) nG = ErrorFloat(*hist.IntegralError(h)) if group.isData: print row( group.latex, [int(nG.val),'',''], fmt=fmt) else: print row( group.latex, ['%i +/- %i'%(nG.val,nG.err),'',''], fmt=fmt, makeint=True) if not group.isData: for i in xrange(len(EST)): EST[i]+=nG BG[i]+=nG print row('', ['SM','Left-Handed','Right-Handed'], fmt = fmt) signal = [] SB = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region)) nG = ErrorFloat(*hist.IntegralError(h)) signal.append('%i +/- %i'%(nG.val,nG.err)) EST[i]+=nG SB[i]=nG print row(mu.signal.latex, signal, fmt=fmt, makeint = True) wjets = [] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): h = hist.get_dataW(rf, mu, Drawer.upsilon, 'WCR_OS', verbose = False, groups = mu.MC + [group]) print '**' nG = ErrorFloat(*hist.IntegralError(h)) wjets.append('%g +/- %g'%( pdg_simple.pdg_round((nG).val,(nG).err))) EST[i]+=nG BG[i]+=nG print row(mu.Wlnu.latex, wjets, fmt=fmt) #print row(group.latex, [nG]) QCD = [] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): kW = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[group]) kWSS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='SS', verbose = verbose, groups = mu.MC+[group]) rqcd = hist.calc_rQCD(rf,mu,k_entry,'QCD',kW_OS = kW, kW_SS = kWSS, verbose = verbose, groups = mu.MC+[group]) print '%g +/- %g' % pdg_simple.pdg_round(rqcd.val,rqcd.err) qcd = hist.get_QCD(rf,mu,entry,'SR_SS', kW = kWSS, rqcd = rqcd, groups = mu.MC +[group]) nG = ErrorFloat(*hist.IntegralError(qcd)) QCD.append('%g +/- %g'% pdg_simple.pdg_round(nG.val,nG.err)) EST[i]+=nG BG[i]+=nG print row('Multijet', QCD, fmt=fmt) for i in xrange(len(EST)): EST[i] = '%i +/- %i'%(EST[i].val,EST[i].err) SB[i] = SB[i]/BG[i] print row('Total Estimate', EST, fmt = fmt) print row('S:B', SB, fmt = fmt)