def proj(h, axis=None): if twoStage: oneD = getattr(h,'Projection'+axis)() oneD.SetFillStyle(h.GetFillStyle()) return [lib.symmAnti(oneD)] if axis=='X' else [(oneD,oneD)] else: return [lib.symmAnti(hist) for hist in self.proj(h)]
def format(sd): if sd.GetDimension() < 3: return ((sd, ) + tuple(lib.symmAnti(sd))) sd.SetTitle(";x;y;z") yz = sd.Project3D("zy e") yz_symm, yz_anti = lib.symmAnti(yz) yz_minusminus = yz.Clone(yz.GetName() + '_minusminus') yz_minusplus = yz.Clone(yz.GetName() + '_minusplus') z = sd.Project3D("ze") for iZ in range(1, 1 + sd.GetNbinsZ()): sd.GetZaxis().SetRange(iZ, iZ) xy = sd.Project3D("tmp%d_yxe" % iZ) x = xy.ProjectionX() M = lib.coupling(xy) M_symm, M_anti = lib.coupling_symmAnti(M) xsymm, xanti = lib.symmAnti(x) for iY in range(1, 1 + M.GetNbinsY()): yz_minusminus.SetBinContent( iY, iZ, sum( xanti.GetBinContent(iX) * M_anti.GetBinContent(iX, iY) for iX in range(1, 1 + x.GetNbinsX()))) yz_minusplus.SetBinContent( iY, iZ, sum( xsymm.GetBinContent(iX) * M_anti.GetBinContent(iX, iY) for iX in range(1, 1 + x.GetNbinsX()))) sd.GetZaxis().SetRange(0, sd.GetNbinsZ()) return yz, yz_symm, yz_anti, yz_minusminus, yz_minusplus
def format(sd): if sd.GetDimension()<3: return ((sd,) + tuple(lib.symmAnti(sd))) sd.SetTitle(";x;y;z") yz = sd.Project3D("zy e") yz_symm,yz_anti = lib.symmAnti(yz) yz_minusminus = yz.Clone(yz.GetName()+'_minusminus') yz_minusplus = yz.Clone(yz.GetName()+'_minusplus') z = sd.Project3D("ze") for iZ in range(1,1+sd.GetNbinsZ()): sd.GetZaxis().SetRange(iZ,iZ) xy = sd.Project3D("tmp%d_yxe"%iZ) x = xy.ProjectionX() M = lib.coupling(xy) M_symm,M_anti = lib.coupling_symmAnti(M) xsymm, xanti = lib.symmAnti(x) for iY in range(1,1+M.GetNbinsY()): yz_minusminus.SetBinContent(iY,iZ, sum(xanti.GetBinContent(iX) * M_anti.GetBinContent(iX,iY) for iX in range(1,1+x.GetNbinsX()))) yz_minusplus.SetBinContent(iY,iZ, sum(xsymm.GetBinContent(iX) * M_anti.GetBinContent(iX,iY) for iX in range(1,1+x.GetNbinsX()))) sd.GetZaxis().SetRange(0,sd.GetNbinsZ()) return yz,yz_symm,yz_anti,yz_minusminus,yz_minusplus
def __init__(self, props, outName, fName = 'data/stats_top_mu_ph_sn_jn_20.root', dName = 'genTopTanhDeltaAbsY', propOrder = []): tFile = r.TFile.Open(fName) hists = {} smax = 0 amax = 0 leg = r.TLegend(0.7,0.2,0.9,0.45) leg.SetBorderSize(0) leg.SetFillColor(r.kWhite) leg.SetTextFont(42) for key in (propOrder if propOrder else props): h = tFile.Get(dName + '/' + key) N = h.GetEntries() asymm = lib.asymmetry(h) print key, N, asymm h.UseCurrentStyle() h.Rebin(2) #h.SetTitle(';%s;(1/#sigma)(d#sigma/d%s)'%(Xl,Xl)) h.SetTitle(';%s;Probability / %.2f'%(Xl,h.GetBinWidth(1))) h.GetYaxis().SetTitleOffset(1) #h.Scale(1./h.Integral(),'width') h.Scale(1./h.Integral()) col,width,style = props[key] h.SetLineColor(col) h.SetLineWidth(width) h.SetLineStyle(style) h.GetXaxis().SetNdivisions(5,4,0,False) leg.AddEntry(h, labels[key] if key in labels else key, 'L') hists[key] = lib.symmAnti(h) smax = max(smax, hists[key][0].GetMaximum()) amax = max(amax, hists[key][1].GetMaximum()) c = r.TCanvas("canvas","", 800,800) c.Print(outName + '[') for i,(n,(h,_)) in enumerate(hists.items()): h.SetMaximum(1.15*smax) h.SetMinimum(0) h.Draw('hist' + ('' if not i else 'same')) leg.Draw() text.DrawTextNDC(0.45,0.96,"Symmetric") dostamp() c.Print(outName) for i,(n,(_,h)) in enumerate(hists.items()): h.SetMaximum(1.1*amax) h.SetMinimum(-1.1*amax) h.Draw('hist' + ('' if not i else 'same')) leg.Draw() text.DrawTextNDC(0.45,0.96,"Antisymmetric") dostamp() c.Print(outName) c.Print(outName + ']') tFile.Close()
def __init__(self, template): symm,anti = lib.symmAnti(template) for item in ['template','symm','anti']: setattr(self, item, eval(item)) self.nBinsX = symm.GetNbinsX() w = r.RooWorkspace('Workspace') roo.factory(w, "x[-1,1]") argset = w.argSet("x") arglist = r.RooArgList(argset) roo.wimport(w, r.RooDataHist('dh_template_both','', arglist, template)) roo.wimport(w, r.RooDataHist('dh_template_symm','', arglist, symm)) roo.wimport(w, r.RooHistPdf('pdf_template_both','',argset, w.data('dh_template_both'))) roo.wimport(w, r.RooHistPdf('pdf_template_symm','',argset, w.data('dh_template_symm'))) self.import_asymmetry(w) self.w = w
def display(fname, hists): can = r.TCanvas('can','',1200,600) can.Divide(2,1) colors = {'ph':r.kBlack,'mn':r.kBlue,'mg':r.kRed} keep = [] for i,(n,c) in enumerate(colors.items()): h = hists[n].Clone(n+'clone') h.Scale(1./h.Integral()) h.SetLineColor(c) h.SetTitle(";X_{L}") s,a = lib.symmAnti(h) s.SetMinimum(0) s.SetMaximum(1.2*s.GetMaximum()) can.cd(1) s.Draw('same' if i else '') can.cd(2) a.Draw('same' if i else '') keep.append(s) keep.append(a) can.Print(fname)
def visualize2D(self, printName=''): w = self.w titles = ['X_{L}', 'X_{T}', '#Delta'] for v, t in zip(self.observables, titles): w.arg(v).SetTitle(t) r.gROOT.ProcessLine(".L lib/tdrstyle.C") r.setTDRStyle() r.TGaxis.SetMaxDigits(4) canvas = r.TCanvas() canvas.Print(printName + '[') def expected_histogram(pdfname, expect=0): print pdfname, expect mod = w.pdf(pdfname) if not mod: w.Print() exit() args = ','.join(self.observables) exp = mod.generateBinned(w.argSet(args), expect, True, False) hist = exp.createHistogram(args, 5, 5, 5) hist.SetName(pdfname + 'genHist') return hist for j, lep in enumerate(['el', 'mu']): dhist = unqueue(self.channels[lep].samples['data'].datas[0], True) hist = expected_histogram('model_' + lep) print self.channels[lep].samples.keys() hists = dict([ (s, expected_histogram( lep + '_' + s + ('_both' if s in ['wj', 'st', 'ttgg'] else '_symm' if s == 'dy' else ''), w.arg('expect_%s_%s' % (lep, s)).getVal())) for s in self.channels[lep].samples if s not in ['data'] ]) hists['mj'] = expected_histogram( lep + '_mj', w.arg('expect_%s_mj' % lep).getVal()) stack = [('ttqq', ), ('ttqg', 'ttag'), ('ttgg', ), ('wj', ), ('mj', ), ('dy', 'st')][::-1] colors = [ r.kViolet, r.kBlue - 7, r.kBlue + 2, r.kGreen + 1, r.kRed, r.kGray ][::-1] stackers = [] for s, c in zip(stack, colors): h = hists[s[0]] for n in s[1:]: h.Add(hists[n]) h.SetFillColor(c) h.SetLineColor(c) stackers.append(h) for i in range(3): def proj(h): return getattr(h, 'Projection' + 'XYZ'[i])( h.GetName() + 'xyz'[i], 0, -1, 0 if i == 2 else 3, -1 if i == 2 else 3) model = proj(hist) data = proj(dhist) comps = [proj(h) for h in stackers] hstack = r.THStack('stack', '') for c in comps: hstack.Add(c) model.SetMinimum(0) model.SetLineColor(r.kBlue) data.SetMinimum(0) data.Draw() model.Draw('hist same') hstack.Draw('hist same') data.Draw('same') canvas.Print(printName) sys.stdout.write(' ') if i < 2: _, amodel = lib.symmAnti(model) _, adata = lib.symmAnti(data) astackers = [lib.symmAnti(s)[1] for s in stackers] amax = 1.3 * max(adata.GetMaximum(), amodel.GetMaximum()) adata.SetMaximum(amax) adata.SetMinimum(-amax) amodel.SetMinimum(-amax) amodel.SetMaximum(amax) adata.Draw() amodel.Draw('hist same') for a in astackers: a.Draw('hist same') canvas.Print(printName) sys.stdout.write(' ') continue canvas.cd(1 + j * 3 + i) f = w.arg(self.observables[i]).frame() f.SetLineColor(r.kWhite) mod = w.pdf('model_%s' % lep) toy = mod.generateBinned(w.argSet(','.join(self.observables)), 1e7) args = [r.RooFit.ProjWData(toy)] dargs = [ f, r.RooFit.MarkerSize(1.1), #r.RooFit.XErrorSize(0) ] w.data('data_%s' % lep).plotOn(*dargs) pf = '' if self.asymmetry else '_both' stack = [('_ttqq' + pf, ), ('_ttqg' + pf, '_ttag' + pf), ('_ttgg_both', ), ('_wj_both', ), ('qcd_*', ), ('_dy*', '_st_both')] colors = [ r.kViolet, r.kBlue - 7, r.kBlue + 2, r.kGreen + 1, r.kRed, r.kGray ] for iStack in range(len(stack)): sys.stdout.write(".,;:!|"[iStack]) sys.stdout.flush() comps = lep + (',' + lep).join(sum(stack[iStack:], ())) mod.plotOn(f, r.RooFit.Components(comps), r.RooFit.LineColor(colors[iStack]), r.RooFit.FillColor(colors[iStack]), r.RooFit.DrawOption('F'), *args) w.data('data_%s' % lep).plotOn(*dargs) #mod.plotOn(f, r.RooFit.LineColor(r.kOrange), *args) f.Draw() canvas.Print(printName) sys.stdout.write(' ') print canvas.Print(printName + ']') return
def __init__(self): i,j = 0,0 pars = measurement_pars(partitions[j]) R0_,diffR0_ = pars['R0_'] if type(pars['R0_'])==tuple else (pars['R0_'],None) channels = dict([(lep, inputs.channel_data(lep, 'QCD', signal=pars['signal'], rebin=True, dirPrefix="R%02d" % R0_)) for lep in ['el','mu'] ]) fname = 'qcdtemplates.pdf' y = 0.01 c = r.TCanvas('','',800,400) c.Divide(2,1) c.Print(fname + '[') for lep,chan in channels.items(): template = chan.samples['data'].datasX[0].Clone(lep+'template') template.Sumw2() Ndata = template.Integral() print Ndata for s,sd in chan.samples.items(): if s!='data': template.Add(sd.datasX[0], -fracs[lep][s]*Ndata/sd.datasX[0].Integral()) c.cd(1) scale = 1./sd.datasX[1].Integral() sd.datasX[1].SetTitle(sd.datasX[0].GetName().replace('_px','')) sd.datasX[1].Scale(scale) sd.datasX[2].Scale(scale) sd.datasX[1].SetMinimum(0) sd.datasX[2].SetMinimum(-y) sd.datasX[2].SetMaximum(y) sd.datasX[1].SetLineColor(colors[s]) sd.datasX[2].SetLineColor(colors[s]) sd.datasX[1].Draw() c.cd(2) sd.datasX[2].Draw() c.Print(fname) template.Scale(1./template.Integral()) tsymm,tanti = lib.symmAnti(template) c.cd(1) labels = {'el':'e','mu':'#mu'} tsymm.UseCurrentStyle() tanti.UseCurrentStyle() tsymm.SetMarkerStyle(0) tanti.SetMarkerStyle(0) tsymm.SetTitle(";%s+jets mj, symmetrized X_{L};(1/#sigma)#left(#partial#sigma / #partial^{}X_{L}#right)"%labels[lep]) tanti.SetTitle(";%s+jets mj, antisymmetrized X_{L};(1/#sigma)#left(#partial#sigma / #partial^{}X_{L}#right)"%labels[lep]) tanti.SetBinError(3,0) tsymm.SetLineColor(r.kRed) tanti.SetLineColor(r.kRed) tsymm.SetMinimum(0) tanti.SetMinimum(-y) tanti.SetMaximum(y) tsymm.GetXaxis().SetNdivisions(-5) tanti.GetXaxis().SetNdivisions(-5) tsymm.Draw('histe') c.cd(2) tanti.Draw('histe') c.Print(fname) c.Print(fname + ']')
def format(sd): return ((sd,) + tuple(lib.symmAnti(sd)))
def visualize2D(self, printName=''): w = self.w titles = ['X_{L}','X_{T}','#Delta'] for v,t in zip(self.observables,titles) : w.arg(v).SetTitle(t) r.gROOT.ProcessLine(".L lib/tdrstyle.C") r.setTDRStyle() r.TGaxis.SetMaxDigits(4) canvas = r.TCanvas() canvas.Print(printName+'[') def expected_histogram(pdfname, expect=0): print pdfname,expect mod = w.pdf(pdfname) if not mod: w.Print() exit() args = ','.join(self.observables) exp = mod.generateBinned(w.argSet(args), expect, True, False) hist = exp.createHistogram(args, 5, 5, 5) hist.SetName(pdfname+'genHist') return hist for j,lep in enumerate(['el','mu']): dhist = unqueue(self.channels[lep].samples['data'].datas[0], True) hist = expected_histogram('model_'+lep) print self.channels[lep].samples.keys() hists = dict([(s, expected_histogram(lep+'_'+s+('_both' if s in ['wj','st','ttgg'] else '_symm' if s=='dy' else ''), w.arg('expect_%s_%s'%(lep,s)).getVal())) for s in self.channels[lep].samples if s not in ['data']]) hists['mj'] = expected_histogram(lep+'_mj', w.arg('expect_%s_mj'%lep).getVal()) stack = [('ttqq',),('ttqg','ttag'),('ttgg',),('wj',),('mj',), ('dy','st')][::-1] colors = [r.kViolet,r.kBlue-7,r.kBlue+2,r.kGreen+1,r.kRed,r.kGray][::-1] stackers = [] for s,c in zip(stack,colors): h = hists[s[0]] for n in s[1:]: h.Add(hists[n]) h.SetFillColor(c) h.SetLineColor(c) stackers.append(h) for i in range(3): def proj(h): return getattr(h, 'Projection'+'XYZ'[i])(h.GetName()+'xyz'[i], 0, -1, 0 if i==2 else 3, -1 if i==2 else 3) model = proj(hist) data = proj(dhist) comps = [proj(h) for h in stackers] hstack = r.THStack('stack','') for c in comps: hstack.Add(c) model.SetMinimum(0) model.SetLineColor(r.kBlue) data.SetMinimum(0) data.Draw() model.Draw('hist same') hstack.Draw('hist same') data.Draw('same') canvas.Print(printName) sys.stdout.write(' ') if i<2: _,amodel = lib.symmAnti(model) _,adata = lib.symmAnti(data) astackers = [lib.symmAnti(s)[1] for s in stackers] amax = 1.3* max(adata.GetMaximum(),amodel.GetMaximum()) adata.SetMaximum(amax) adata.SetMinimum(-amax) amodel.SetMinimum(-amax) amodel.SetMaximum(amax) adata.Draw() amodel.Draw('hist same') for a in astackers: a.Draw('hist same') canvas.Print(printName) sys.stdout.write(' ') continue canvas.cd(1+j*3+i) f = w.arg(self.observables[i]).frame() f.SetLineColor(r.kWhite) mod = w.pdf('model_%s'%lep) toy = mod.generateBinned(w.argSet(','.join(self.observables)), 1e7) args = [r.RooFit.ProjWData(toy)] dargs = [f, r.RooFit.MarkerSize(1.1), #r.RooFit.XErrorSize(0) ] w.data('data_%s'%lep).plotOn(*dargs) pf = '' if self.asymmetry else '_both' stack = [('_ttqq'+pf,),('_ttqg'+pf,'_ttag'+pf),('_ttgg_both',),('_wj_both',), ('qcd_*',), ('_dy*','_st_both')] colors = [r.kViolet,r.kBlue-7,r.kBlue+2,r.kGreen+1,r.kRed,r.kGray] for iStack in range(len(stack)): sys.stdout.write(".,;:!|"[iStack]) sys.stdout.flush() comps = lep+(','+lep).join(sum(stack[iStack:],())) mod.plotOn(f, r.RooFit.Components(comps), r.RooFit.LineColor(colors[iStack]), r.RooFit.FillColor(colors[iStack]), r.RooFit.DrawOption('F'), *args) w.data('data_%s'%lep).plotOn(*dargs) #mod.plotOn(f, r.RooFit.LineColor(r.kOrange), *args) f.Draw() canvas.Print(printName) sys.stdout.write(' ') print canvas.Print(printName+']') return