def routine(hs, c): c.clear() for h in hs: h.drawstyle = 'hist' h.scale(1. / h.integral()) h.linewidth = 2 h.legendstyle = 'L' legend = Legend(hs, pad=c, leftmargin=0.5, margin=0.1, entryheight=0.02, textsize=12) xmin_, xmax_, ymin_, ymax_ = get_limits(hs) draw( hs, pad=c, ylimits=(0, ymax_), ) legend.Draw() t = LuminosityLabel( 'XX#rightarrow2A#rightarrow4#mu (500GeV, 1.2GeV, 300cm)') t.draw()
def draw_HT(HT_tt, HT_qcd, HT_wjet): #set line colors HT_tt.SetLineColor(4) HT_qcd.SetLineColor(8) HT_wjet.SetLineColor(2) HT_tt.legendstyle = 'L' HT_qcd.legendstyle = 'L' HT_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() HT_wjet.SetStats(0) HT_wjet.Draw('HIST') HT_tt.Draw('HIST SAME') HT_qcd.Draw('HIST SAME') #make legend l1 = Legend([HT_tt, HT_qcd, HT_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("HT.pdf")
def draw_DPHImj(DPHI_tt, DPHI_qcd, DPHI_wjet): #set line colors DPHI_tt.SetLineColor(4) DPHI_qcd.SetLineColor(8) DPHI_wjet.SetLineColor(2) DPHI_tt.legendstyle = 'L' DPHI_qcd.legendstyle = 'L' DPHI_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() DPHI_qcd.SetStats(0) DPHI_qcd.Draw('HIST') DPHI_tt.Draw('HIST SAME') DPHI_wjet.Draw('HIST SAME') #make legend l1 = Legend([DPHI_tt, DPHI_qcd, DPHI_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("DPHI_metjet.pdf")
def draw_MT(MT_tt, MT_qcd, MT_wjet): #set line colors MT_tt.SetLineColor(4) MT_qcd.SetLineColor(8) MT_wjet.SetLineColor(2) MT_tt.legendstyle = 'L' MT_qcd.legendstyle = 'L' MT_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() MT_qcd.SetStats(0) MT_qcd.Draw('HIST') MT_tt.Draw('HIST SAME') MT_wjet.Draw('HIST SAME') #make legend l1 = Legend([MT_tt, MT_qcd, MT_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("MT.pdf")
def routine(parampoint, color): # 2mu2e - egmiso hs = [] h = getattr(getattr(f.ch2mu2e.sig, parampoint), 'egmljisonopu') h.title = 'pv-based pu correction' h.linestyle = 'dashed' hs.append(h) h = getattr(getattr(f.ch2mu2e.sig, parampoint), 'egmljcorriso') h.title = '#rho-based pu correction' h.linestyle = 'solid' hs.append(h) h = getattr(getattr(f.ch2mu2e.sig, parampoint), 'egmljiso') h.title = 'no pu correction' h.linestyle = 'longdashdotdotdot' hs.append(h) for h in hs: h.color = color h.drawstyle = 'hist' h.linewidth = 2 h.legendstyle = 'L' legItems = [h for h in hs] draw(hs, logy=True) legend = Legend(legItems, pad=canvas, header=parampoint, margin=0.25, leftmargin=0.5, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) legend.Draw() title = TitleAsLatex('[2#mu2e] egm-type lepton-jet isolation comparison') title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') canvas.SaveAs('{}/ch2mu2e_egmljiso__{}.pdf'.format(outdir, parampoint)) canvas.Clear() # 4mu - maxiso hs = [] h = getattr(getattr(f.ch4mu.sig, parampoint), 'maxisonopu') h.title = 'pv-based pu correction' h.linestyle = 'dashed' hs.append(h) h = getattr(getattr(f.ch4mu.sig, parampoint), 'maxcorriso') h.title = '#rho-based pu correction' h.linestyle = 'solid' hs.append(h) h = getattr(getattr(f.ch4mu.sig, parampoint), 'maxiso') h.title = 'no pu correction' h.linestyle = 'longdashdotdotdot' hs.append(h) for h in hs: h.color = color h.drawstyle = 'hist' h.linewidth = 2 h.legendstyle = 'L' legItems = [h for h in hs] draw(hs, logy=True) legend = Legend(legItems, pad=canvas, header=parampoint, margin=0.25, leftmargin=0.5, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) legend.Draw() title = TitleAsLatex('[4#mu] max lepton-jet isolation comparison') title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') canvas.SaveAs('{}/ch4mu_maxljiso__{}.pdf'.format(outdir, parampoint)) canvas.Clear()
def routine(chan, varname, title): hs = [] effs = OrderedDict() chandir = getattr(f, 'ch' + chan) for it, sigtag in enumerate(sigTAGS): h = getattr(getattr(chandir.sig, sigtag), varname) # Hist h_total = h.integral(overflow=True) h_ = h.clone() for i in range(1, h.nbins() + 1): h_[i] = h.integral(xbin1=i, overflow=True) / h_total h_[i].error = 0 if i == 5: effs[sigtag] = h.integral(xbin1=i, overflow=True) / h_total h_.title = sigtag h_.drawstyle = 'hist' h_.color = sigCOLORS[it] h_.linewidth = 2 h_.legendstyle = 'L' hs.append(h_) mineff = min(effs.values()) aveeff = sum(effs.values()) / len(effs) effs['sig_ave'] = aveeff effs['sig_min'] = mineff h = getattr(chandir.data, varname) h_total = h.integral(overflow=True) if h_total != 0: h_ = h.empty_clone() for i in range(1, h.nbins() + 1): h_[i] = h.integral(xbin1=i, overflow=True) / h_total h_[i].error = 0 if i == 5: effs['data'] = h.integral(xbin1=i, overflow=True) / h_total h_.drawstyle = 'hist' h_.title = 'cosmic shower' h_.color = 'black' h_.linestyle = 'dashed' h_.legendstyle = 'L' h_.linewidth = 2 hs.append(h_) print('>', chan, varname) maxlen = max([len(k) for k in effs]) for k in effs: fmt = '{:%d}:{:.2f}' % (maxlen + 2) + '%' print(fmt.format(k, effs[k] * 100)) # legend = Legend(hs, pad=c, margin=0.1, topmargin=0.02, entryheight=0.02, textsize=12) legend = Legend(hs, pad=c, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=11) axes, limits = draw( hs, ylimits=(0., 1.5), ytitle='(forward) cut efficiency', ) legend.Draw() title = TitleAsLatex('[{}] {} cut efficiency'.format( chan.replace('mu', '#mu'), title)) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') ROOT.gPad.SetGrid() c.SaveAs('{}/ch{}_{}.pdf'.format(outdir, chan, varname)) c.Clear()
def extract_Electron(f_tt, f_qcd, f_wjet): # get trees from files t_tt = f_tt.Get("Delphes") t_qcd = f_qcd.Get("Delphes") t_wjet = f_wjet.Get("Delphes") # get number of entries tt_n_entries = t_tt.GetEntries() qcd_n_entries = t_qcd.GetEntries() wjet_n_entries = t_wjet.GetEntries() # define leaves var_tt = "Electron.PT" var_qcd = "Electron.PT" var_wjet = "Electron.PT" leaf_tt = t_tt.GetLeaf(var_tt) leaf_qcd = t_qcd.GetLeaf(var_qcd) leaf_wjet = t_wjet.GetLeaf(var_wjet) # create the histograms numElectrons_tt = Hist(NBINS,NLO,NHI, title = 'numElectrons_tt', legendstyle = 'L') numElectrons_qcd = Hist(NBINS,NLO,NHI, title = 'numElectrons_qcd', legendstyle = 'L') numElectrons_wjet = Hist(NBINS,NLO,NHI, title = 'numElectrons_wjet', legendstyle = 'L') # interesting values to plot max_ept_per_event_tt = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event tt', legendstyle = 'L') min_ept_per_event_tt = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event tt', legendstyle = 'L') max_ept_per_event_qcd = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event qcd', legendstyle = 'L') min_ept_per_event_qcd = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event qcd', legendstyle = 'L') max_ept_per_event_wjet = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event wjet', legendstyle = 'L') min_ept_per_event_wjet = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event wjet', legendstyle = 'L') # FILLING THE TREE fill_Electron_tree(tt_n_entries, t_tt, leaf_tt, numElectrons_tt, min_ept_per_event_tt, max_ept_per_event_tt) fill_Electron_tree(qcd_n_entries, t_qcd, leaf_qcd, numElectrons_qcd, min_ept_per_event_qcd, max_ept_per_event_qcd) fill_Electron_tree(wjet_n_entries, t_wjet, leaf_wjet, numElectrons_wjet, min_ept_per_event_wjet, max_ept_per_event_wjet) #set line colors numElectrons_tt.SetLineColor('blue') numElectrons_qcd.SetLineColor('green') numElectrons_wjet.SetLineColor('red') #begin drawing stuff c1 = Canvas() numElectrons_wjet.SetStats(0) numElectrons_wjet.Draw('HIST') numElectrons_tt.Draw('HIST SAME') numElectrons_qcd.Draw('HIST SAME') #make legend l1 = Legend([numElectrons_tt, numElectrons_qcd, numElectrons_wjet], textfont = 42, textsize = .03) l1.Draw() #save as pdf c1.SaveAs("../plots/ElectronPT_plots/numElectrons.pdf"); ################ MIN MAX STUFF # TT #set line colors max_ept_per_event_tt.SetLineColor('blue') min_ept_per_event_tt.SetLineColor('green') #begin drawing stuff c2 = Canvas() min_ept_per_event_tt.SetStats(0) min_ept_per_event_tt.Draw('HIST') max_ept_per_event_tt.Draw('HIST SAME') #make legend l2 = Legend([min_ept_per_event_tt, max_ept_per_event_tt], textfont = 42, textsize = .03) l2.Draw() #save as pdf c2.SaveAs("../plots/ElectronPT_plots/e_maxminpt_tt.pdf") # QCD #set line colors max_ept_per_event_qcd.SetLineColor('blue') min_ept_per_event_qcd.SetLineColor('green') #begin drawing stuff c3 = Canvas() max_ept_per_event_qcd.SetStats(0) max_ept_per_event_qcd.Draw('HIST') min_ept_per_event_qcd.Draw('HIST SAME') #make legend l3 = Legend([min_ept_per_event_qcd, max_ept_per_event_qcd], textfont = 42, textsize = .03) l3.Draw() #save as pdf c3.SaveAs("../plots/ElectronPT_plots/e_maxminpt_qcd.pdf") #WJET #set line colors max_ept_per_event_wjet.SetLineColor('blue') min_ept_per_event_wjet.SetLineColor('green') #begin drawing stuff c4 = Canvas() min_ept_per_event_wjet.SetStats(0) min_ept_per_event_wjet.Draw('HIST') max_ept_per_event_wjet.Draw('HIST SAME') #make legend l4 = Legend([min_ept_per_event_wjet, max_ept_per_event_wjet], textfont = 42, textsize = .03) l4.Draw() #save as pdf c4.SaveAs("../plots/ElectronPT_plots/e_maxminpt_wjet.pdf") #make the plots wait on screen wait(True)
# plot canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_simple.Draw() # create the legend legend = Legend([h_simple], pad=canvas, header='Header', leftmargin=0.05, rightmargin=0.5) legend.Draw() # 2D and 3D histograms are handled in the same way # the constructor arguments are repetitions of #bins, left bound, right bound. h2d = Hist2D(10, 0, 1, 50, -40, 10, name='2d hist') h3d = Hist3D(3, -1, 4, 10, -1000, -200, 2, 0, 1, name='3d hist') # variable-width bins may be created by passing the bin edges directly: h1d_variable = Hist([1, 4, 10, 100]) h2d_variable = Hist2D([2, 4, 7, 100, 200], [-100, -50, 0, 10, 20]) h3d_variable = Hist3D([1, 3, 10], [20, 50, 100], [-10, -5, 10, 20]) # variable-width and constant-width bins can be mixed: h2d_mixed = Hist2D([2, 10, 30], 10, 1, 5) # wait for you to close all open canvases before exiting
def main(args): fileName = '/data/nawoods/lepsForSIP/lepsForSIP_M2500.root' checker = MuonPtMatchChecker('Check Pt', [fileName]) checker.processSample() hs = checker.hists.copy() for var, h in hs.iteritems(): h.Sumw2() h.drawstyle = 'HIST' h.legendstyle = 'L' h.linewidth = 2 h.color = 'red' cRes = Canvas(1000,1000) hs['pfPtRes'].color = 'black' hs['pfPtRes'].title = 'PF' hs['tunePPtResUncor'].title = 'Tune P' (xax,yax), lims = draw([hs['pfPtRes'], hs['tunePPtResUncor']], cRes, xtitle='#frac{reco p_T - gen p_T}{gen p_T}', ytitle='Muons') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) legRes = Legend([hs['pfPtRes'], hs['tunePPtResUncor']], cRes) legRes.Draw("same") cRes.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptRes.png') cResComp = Canvas(1000,1000) (xax,yax), lims = draw(hs['resCompare'], cResComp, ytitle='Muons', xtitle='#frac{|PF p_T - gen p_T|}{gen p_T} - #frac{|tuneP p_T - gen p_T|}{gen p_T}') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) cResComp.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptResComp.png') cResCor = Canvas(1000,1000) hs['pfPtRes'].color = 'black' hs['pfPtRes'].title = 'PF' hs['tunePPtRes'].title = 'Tune P' (xax,yax), lims = draw([hs['pfPtRes'], hs['tunePPtRes']], cResCor, xtitle='#frac{reco p_T - gen p_T}{gen p_T}', ytitle='Muons') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) legResCor = Legend([hs['pfPtRes'], hs['tunePPtRes']], cResCor) legResCor.Draw("same") cResCor.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptResCor.png') cResCompCor = Canvas(1000,1000) (xax,yax), lims = draw(hs['resCompareBothCor'], cResCompCor, ytitle='Muons', xtitle='#frac{|PF p_T - gen p_T|}{gen p_T} - #frac{|tuneP p_T - gen p_T|}{gen p_T}') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) cResCompCor.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptResCompCor.png') cResUncor = Canvas(1000,1000) hs['pfPtResUncor'].color = 'black' hs['pfPtResUncor'].title = 'PF' hs['tunePPtResUncor'].title = 'Tune P' (xax,yax), lims = draw([hs['pfPtResUncor'], hs['tunePPtResUncor']], cResUncor, xtitle='#frac{reco p_T - gen p_T}{gen p_T}', ytitle='Muons') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) legResUncor = Legend([hs['pfPtResUncor'], hs['tunePPtResUncor']], cResUncor) legResUncor.Draw("same") cResUncor.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptResUncor.png') cResCompUncor = Canvas(1000,1000) (xax,yax), lims = draw(hs['resCompareUncor'], cResCompUncor, ytitle='Muons', xtitle='#frac{|PF p_T - gen p_T|}{gen p_T} - #frac{|tuneP p_T - gen p_T|}{gen p_T}') xax.SetTitleSize(0.4 * xax.GetTitleSize()) xax.SetTitleOffset(1.6) xax.SetLabelSize(0.7 * xax.GetLabelSize()) cResCompUncor.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/ptResCompUncor.png') hs2 = checker.hists2D.copy() hs2['pfPtResVsPt'].yaxis.title = '#frac{PF p_T - gen p_T}{gen p_T}' hs2['tunePPtResVsPt'].yaxis.title = '#frac{tuneP p_T - gen p_T}{gen p_T}' hs2['pfPtResVsPt'].yaxis.title = '#frac{|PF p_T - gen p_T|}{gen p_T} - #frac{|tuneP p_T - gen p_T|}{gen p_T}' for var, h in hs2.iteritems(): c = Canvas(1000,1000) h.xaxis.title = 'gen p_T' h.drawstyle = 'COLZ' h.xaxis.SetTitleSize(0.5 * h.xaxis.GetTitleSize()) h.xaxis.SetLabelSize(0.7 * h.xaxis.GetLabelSize()) h.xaxis.SetTitleOffset(1.4) h.yaxis.SetTitleSize(0.5 * h.yaxis.GetTitleSize()) h.yaxis.SetLabelSize(0.7 * h.yaxis.GetLabelSize()) h.yaxis.SetTitleOffset(1.4) h.draw() c.Print('/afs/cern.ch/user/n/nawoods/www/ptStudy/{}.png'.format(var))
def plot(self): evaluator = Evaluator(self.model, self.transformation, self.features) makedirs(self.plt_dir, exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log', 'root']), exist_ok=True) # NN evaluator print('============> starting reading the trees') print('Plots will be stored in: ', self.plt_dir) now = time() signal = [] if self.process_signals: signal = self.get_signal_samples(self.channel, self.base_dir, self.post_fix, self.selection_data, mini=self.mini_signals) else: signal = [] data = self.get_data_samples(self.channel, self.base_dir, self.post_fix, self.selection_data) mc = self.get_mc_samples(self.channel, self.base_dir, self.post_fix, self.selection_mc) print('============> it took %.2f seconds' % (time() - now)) # evaluate FR for isample in (mc + data): #+signal): isample.df['fr'] = evaluator.evaluate(isample.df) # already corrected, ready to be applied in lnt-not-tight isample.df['fr_corr'] = isample.df['fr'] / (1. - isample.df['fr']) # apply an extra selection to the pandas dataframes if len(self.pandas_selection): for isample in (mc + data + signal): isample.df = isample.df.query(self.pandas_selection) # split the dataframe in tight and lnt-not-tight (called simply lnt for short) print('============> splitting dataframe in tight and loose not tight') for isample in (mc + data + signal): isample.df_tight = isample.df.query(self.selection_tight) if isample not in signal: isample.df_lnt = isample.df.query(self.selection_lnt) # free some mem del isample.df gc.collect() print('============> ... done') # sort depending on their position in the stack mc.sort(key=lambda x: x.position_in_stack) # now we plot self.create_canvas(self.do_ratio) for ivar in variables: variable, bins, label, xlabel, ylabel, extra_sel = ivar.var, ivar.bins, ivar.label, ivar.xlabel, ivar.ylabel, ivar.extra_selection print('plotting', label) ###################################################################################### # plot MC stacks, in tight and lnt ###################################################################################### stack_prompt = [] stack_nonprompt = [] stack_nonprompt_control = [] for imc in mc: if extra_sel: mc_df_tight = imc.df_tight.query(extra_sel) mc_df_lnt = imc.df_lnt.query(extra_sel) else: mc_df_tight = imc.df_tight mc_df_lnt = imc.df_lnt histo_tight = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#t') weights = self.total_weight_calculator( mc_df_tight, ['weight'] + imc.extra_signal_weights, [self.lumi, imc.lumi_scaling]) histo_tight.fill_array(mc_df_tight[variable], weights=weights * self.relaxed_mc_scaling) histo_tight.fillstyle = 'solid' histo_tight.fillcolor = 'steelblue' if self.data_driven else imc.colour histo_tight.linewidth = 0 stack_prompt.append(histo_tight) # optionally remove the MC subtraction in loose-not-tight # may help if MC stats is terrible (and it often is) if self.data_driven: if self.mc_subtraction: histo_lnt = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#lnt') weights = self.total_weight_calculator( mc_df_lnt, ['weight', 'fr_corr'] + imc.extra_signal_weights, [-1., self.lumi, imc.lumi_scaling]) histo_lnt.fill_array(mc_df_lnt[variable], weights=weights * self.relaxed_mc_scaling) histo_lnt.fillstyle = 'solid' histo_lnt.fillcolor = 'skyblue' if self.data_driven else imc.colour histo_lnt.linewidth = 0 stack_nonprompt.append(histo_lnt) histo_lnt_control = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#lntcontrol') weights_control = self.total_weight_calculator( mc_df_lnt, ['weight'] + imc.extra_signal_weights, [self.lumi, imc.lumi_scaling]) histo_lnt_control.fill_array(mc_df_lnt[variable], weights=weights_control * self.relaxed_mc_scaling) histo_lnt_control.fillstyle = 'solid' histo_lnt_control.fillcolor = imc.colour histo_lnt_control.linewidth = 0 # print(histo_lnt_control) # print(histo_lnt_control.fillcolor) # print(imc.name, imc.colour) # print(histo_lnt_control.integral()) stack_nonprompt_control.append(histo_lnt_control) # merge different samples together (add the histograms) # prepare two temporary containers for the post-grouping histograms stack_prompt_tmp = [] stack_nonprompt_tmp = [] stack_nonprompt_control_tmp = [] for ini, fin in [(stack_prompt, stack_prompt_tmp), (stack_nonprompt, stack_nonprompt_tmp), (stack_nonprompt_control, stack_nonprompt_control_tmp)]: for k, v in groups.items(): grouped = [] for ihist in ini: if ihist.name.split('#')[0] in v: grouped.append(ihist) elif ihist.name.split('#')[0] not in togroup: fin.append(ihist) if len(grouped): group = sum(grouped) group.title = k group.name = '#'.join([k] + ihist.name.split('#')[1:]) group.fillstyle = grouped[0].fillstyle group.fillcolor = grouped[0].fillcolor group.linewidth = grouped[0].linewidth fin.append(group) stack_prompt = stack_prompt_tmp stack_nonprompt = stack_nonprompt_tmp stack_nonprompt_control = stack_nonprompt_control_tmp ###################################################################################### # plot the signals ###################################################################################### all_signals = [] signals_to_plot = [] for isig in signal: if variable not in self.datacards: if not isig.toplot: continue if variable == 'fr' or variable == 'fr_corr': continue if extra_sel: isig_df_tight = isig.df_tight.query(extra_sel) else: isig_df_tight = isig.df_tight histo_tight = Hist( bins, title=isig.label, markersize=0, legendstyle='L', name=isig.datacard_name + '#' + label ) # the "#" thing is a trick to give hists unique name, else ROOT complains weights = self.total_weight_calculator( isig_df_tight, ['weight'] + isig.extra_signal_weights, [self.lumi, isig.lumi_scaling]) histo_tight.fill_array(isig_df_tight[variable], weights=weights) histo_tight.color = isig.colour histo_tight.fillstyle = 'hollow' histo_tight.linewidth = 2 histo_tight.linestyle = 'dashed' histo_tight.drawstyle = 'HIST' all_signals.append(histo_tight) if isig.toplot: signals_to_plot.append(histo_tight) ###################################################################################### # plot the data ###################################################################################### data_prompt = [] data_nonprompt = [] data_nonprompt_control = [] for idata in data: if extra_sel: idata_df_tight = idata.df_tight.query(extra_sel) idata_df_lnt = idata.df_lnt.query(extra_sel) else: idata_df_tight = idata.df_tight idata_df_lnt = idata.df_lnt histo_tight = Hist(bins, title=idata.label, markersize=1, legendstyle='LEP') histo_tight.fill_array(idata_df_tight[variable]) data_prompt.append(histo_tight) if self.data_driven: histo_lnt = Hist(bins, title=idata.label, markersize=0, legendstyle='F') histo_lnt.fill_array(idata_df_lnt[variable], weights=idata_df_lnt.fr_corr) histo_lnt.fillstyle = 'solid' histo_lnt.fillcolor = 'skyblue' histo_lnt.linewidth = 0 histo_lnt_control = Hist(bins, title=idata.label, markersize=1, legendstyle='LEP') histo_lnt_control.fill_array(idata_df_lnt[variable]) data_nonprompt.append(histo_lnt) data_nonprompt_control.append(histo_lnt_control) if self.data_driven: # put the prompt backgrounds together all_exp_prompt = sum(stack_prompt) all_exp_prompt.title = 'prompt' # put the nonprompt backgrounds together all_exp_nonprompt = sum(stack_nonprompt + data_nonprompt) all_exp_nonprompt.fillstyle = 'solid' all_exp_nonprompt.fillcolor = 'skyblue' all_exp_nonprompt.linewidth = 0 all_exp_nonprompt.title = 'nonprompt' # create the stacks stack = HistStack([all_exp_prompt, all_exp_nonprompt], drawstyle='HIST', title='') stack_control = HistStack(stack_nonprompt_control, drawstyle='HIST', title='') else: stack = HistStack(stack_prompt, drawstyle='HIST', title='') # stat uncertainty hist_error = stack.sum #sum([all_exp_prompt, all_exp_nonprompt]) hist_error.drawstyle = 'E2' hist_error.fillstyle = '/' hist_error.color = 'gray' hist_error.title = 'stat. unc.' hist_error.legendstyle = 'F' if self.data_driven: hist_error_control = stack_control.sum hist_error_control.drawstyle = 'E2' hist_error_control.fillstyle = '/' hist_error_control.color = 'gray' hist_error_control.title = 'stat. unc.' hist_error_control.legendstyle = 'F' # put the data together all_obs_prompt = sum(data_prompt) all_obs_prompt.title = 'observed' if self.data_driven: all_obs_nonprompt_control = sum(data_nonprompt_control) all_obs_nonprompt_control.title = 'observed' all_obs_nonprompt_control.drawstyle = 'EP' # prepare the legend print(signals_to_plot) for jj in signals_to_plot: print(jj.name, jj.integral()) if len(signals_to_plot): legend = Legend([all_obs_prompt, stack, hist_error], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.025, entrysep=0.01, entryheight=0.04) legend_signals = Legend(signals_to_plot, pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.025, entrysep=0.01, entryheight=0.04) legend_signals.SetBorderSize(0) legend_signals.x1 = 0.42 legend_signals.y1 = 0.74 legend_signals.x2 = 0.88 legend_signals.y2 = 0.90 legend_signals.SetFillColor(0) legend.SetBorderSize(0) legend.x1 = 0.2 legend.y1 = 0.74 legend.x2 = 0.45 legend.y2 = 0.90 legend.SetFillColor(0) else: legend = Legend([all_obs_prompt, stack, hist_error], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) legend.SetBorderSize(0) legend.x1 = 0.55 legend.y1 = 0.74 legend.x2 = 0.88 legend.y2 = 0.90 legend.SetFillColor(0) # plot with ROOT, linear and log scale for islogy in [False, True]: things_to_plot = [stack, hist_error] if not self.blinded: things_to_plot.append(all_obs_prompt) # plot signals, as an option if self.plot_signals: things_to_plot += signals_to_plot # set the y axis range # FIXME! setting it by hand to each object as it doesn't work if passed to draw if islogy: yaxis_max = 40. * max( [ithing.max() for ithing in things_to_plot]) else: yaxis_max = 1.65 * max( [ithing.max() for ithing in things_to_plot]) if islogy: yaxis_min = 0.01 else: yaxis_min = 0. for ithing in things_to_plot: ithing.SetMaximum(yaxis_max) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy) # expectation uncertainty in the ratio pad ratio_exp_error = Hist(bins) ratio_data = Hist(bins) for ibin in hist_error.bins_range(): ratio_exp_error.set_bin_content(ibin, 1.) ratio_exp_error.set_bin_error( ibin, hist_error.get_bin_error(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.set_bin_content( ibin, all_obs_prompt.get_bin_content(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.set_bin_error( ibin, all_obs_prompt.get_bin_error(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.drawstyle = 'EP' ratio_data.title = '' ratio_exp_error.drawstyle = 'E2' ratio_exp_error.markersize = 0 ratio_exp_error.title = '' ratio_exp_error.fillstyle = '/' ratio_exp_error.color = 'gray' for ithing in [ratio_data, ratio_exp_error]: ithing.xaxis.set_label_size( ithing.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_label_size( ithing.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.xaxis.set_title_size( ithing.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_title_size( ithing.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_ndivisions(405) ithing.yaxis.set_title_offset(0.4) things_to_plot = [ratio_exp_error] if not self.blinded: things_to_plot.append(ratio_data) draw(things_to_plot, xtitle=xlabel, ytitle='obs/exp', pad=self.ratio_pad, logy=False, ylimits=(0.5, 1.5)) line = ROOT.TLine(min(bins), 1., max(bins), 1.) line.SetLineColor(ROOT.kBlack) line.SetLineWidth(1) self.ratio_pad.cd() line.Draw('same') # chi2_score_text = '\chi^{2}/NDF = %.1f' %(all_obs_prompt.Chi2Test(hist_error, 'UW CHI2/NDF')) chi2_score_text = 'p-value = %.2f' % (all_obs_prompt.Chi2Test( hist_error, 'UW')) chi2_score = ROOT.TLatex(0.7, 0.81, chi2_score_text) chi2_score.SetTextFont(43) chi2_score.SetTextSize(15) chi2_score.SetNDC() chi2_score.Draw('same') self.canvas.cd() # FIXME! add SS and OS channels if self.full_channel == 'mmm': channel = '\mu\mu\mu' elif self.full_channel == 'eee': channel = 'eee' elif self.full_channel == 'mem_os': channel = '\mu^{\pm}\mu^{\mp}e' elif self.full_channel == 'mem_ss': channel = '\mu^{\pm}\mu^{\pm}e' elif self.full_channel == 'eem_os': channel = 'e^{\pm}e^{\mp}\mu' elif self.full_channel == 'eem_ss': channel = 'e^{\pm}e^{\pm}\mu' else: assert False, 'ERROR: Channel not valid.' finalstate = ROOT.TLatex(0.68, 0.68, channel) finalstate.SetTextFont(43) finalstate.SetTextSize(25) finalstate.SetNDC() finalstate.Draw('same') self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) legend.Draw('same') if self.plot_signals: legend_signals.Draw('same') CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # plot distributions in loose not tight # check MC contamination there if self.data_driven and variable not in ['fr', 'fr_corr']: things_to_plot = [ stack_control, hist_error_control, all_obs_nonprompt_control ] # set the y axis range # FIXME! setting it by hand to each object as it doesn't work if passed to draw if islogy: yaxis_max = 40. * max( [ithing.max() for ithing in things_to_plot]) else: yaxis_max = 1.65 * max( [ithing.max() for ithing in things_to_plot]) if islogy: yaxis_min = 0.01 else: yaxis_min = 0. for ithing in things_to_plot: ithing.SetMaximum(yaxis_max) ithing.SetMinimum(yaxis_min) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy, ylimits=(yaxis_min, yaxis_max)) new_legend = Legend( stack_control.hists + [hist_error_control, all_obs_nonprompt_control], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) new_legend.SetBorderSize(0) new_legend.x1 = 0.55 new_legend.y1 = 0.71 new_legend.x2 = 0.88 new_legend.y2 = 0.90 new_legend.SetFillColor(0) # divide MC to subtract by data stack_nonprompt_control_scaled_list = [] for ihist in stack_control.hists: new_hist = copy(ihist) for ibin in new_hist.bins_range(): new_hist.SetBinContent( ibin, np.nan_to_num( np.divide( new_hist.GetBinContent(ibin), all_obs_nonprompt_control. GetBinContent(ibin)))) new_hist.SetBinError( ibin, np.nan_to_num( np.divide( new_hist.GetBinError(ibin), all_obs_nonprompt_control. GetBinContent(ibin)))) stack_nonprompt_control_scaled_list.append(new_hist) stack_control_scaled = HistStack( stack_nonprompt_control_scaled_list, drawstyle='HIST', title='') stack_control_scaled_err = stack_control_scaled.sum stack_control_scaled_err.drawstyle = 'E2' stack_control_scaled_err.fillstyle = '/' stack_control_scaled_err.color = 'gray' stack_control_scaled_err.title = 'stat. unc.' stack_control_scaled_err.legendstyle = 'F' draw([stack_control_scaled, stack_control_scaled_err], xtitle=xlabel, ytitle='MC/data', pad=self.ratio_pad, logy=False) stack_control_scaled.xaxis.set_label_size( stack_control_scaled.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_label_size( stack_control_scaled.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.xaxis.set_title_size( stack_control_scaled.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_title_size( stack_control_scaled.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_ndivisions(405) stack_control_scaled.yaxis.set_title_offset(0.4) stack_control_scaled.SetMinimum(0.) stack_control_scaled.SetMaximum(1.5) CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) # draw new legend new_legend.Draw('same') self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'lnt_region', 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # compare shapes in tight and loose not tight # data in tight all_obs_prompt_norm = copy(all_obs_prompt) if all_obs_prompt_norm.integral() != 0: all_obs_prompt_norm.Scale( np.nan_to_num( np.divide(1., all_obs_prompt_norm.integral()))) #import pdb; pdb.set_trace() all_obs_prompt_norm.drawstyle = 'hist e' all_obs_prompt_norm.linecolor = 'black' all_obs_prompt_norm.markersize = 0 all_obs_prompt_norm.legendstyle = 'LE' all_obs_prompt_norm.title = '' all_obs_prompt_norm.label = 'data - tight' # data MC subtracted in loose all_obs_prompt_mc_sub_norm = copy(all_obs_prompt) all_obs_prompt_mc_sub_norm.add(all_exp_prompt, -1) all_obs_prompt_mc_sub_norm.Scale( np.nan_to_num( np.divide(1., all_obs_prompt_mc_sub_norm.integral()))) all_obs_prompt_mc_sub_norm.drawstyle = 'hist e' all_obs_prompt_mc_sub_norm.linecolor = 'green' all_obs_prompt_mc_sub_norm.markersize = 0 all_obs_prompt_mc_sub_norm.legendstyle = 'LE' all_obs_prompt_mc_sub_norm.title = '' all_obs_prompt_mc_sub_norm.label = '(data-MC) - tight' # data in loose all_obs_nonprompt_control_norm = copy( all_obs_nonprompt_control) all_obs_nonprompt_control_norm.Scale( np.nan_to_num( np.divide( 1., all_obs_nonprompt_control_norm.integral()))) all_obs_nonprompt_control_norm.drawstyle = 'hist e' all_obs_nonprompt_control_norm.linecolor = 'red' all_obs_nonprompt_control_norm.markersize = 0 all_obs_nonprompt_control_norm.legendstyle = 'LE' all_obs_nonprompt_control_norm.title = '' all_obs_nonprompt_control_norm.label = 'data - l-n-t' # data MC subtracted in loose all_obs_nonprompt_control_mc_sub_norm = copy( all_obs_nonprompt_control) all_obs_nonprompt_control_mc_sub_norm.add( stack_control.sum, -1) all_obs_nonprompt_control_mc_sub_norm.Scale( np.nan_to_num( np.divide( 1., all_obs_nonprompt_control_mc_sub_norm.integral( )))) all_obs_nonprompt_control_mc_sub_norm.drawstyle = 'hist e' all_obs_nonprompt_control_mc_sub_norm.linecolor = 'blue' all_obs_nonprompt_control_mc_sub_norm.markersize = 0 all_obs_nonprompt_control_mc_sub_norm.legendstyle = 'LE' all_obs_nonprompt_control_mc_sub_norm.title = '' all_obs_nonprompt_control_mc_sub_norm.label = '(data-MC) - l-n-t' things_to_plot = [ all_obs_prompt_norm, all_obs_prompt_mc_sub_norm, all_obs_nonprompt_control_norm, all_obs_nonprompt_control_mc_sub_norm, ] yaxis_max = max([ii.GetMaximum() for ii in things_to_plot]) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy, ylimits=(yaxis_min, 1.55 * yaxis_max)) self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) shape_legend = Legend([], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) shape_legend.AddEntry(all_obs_prompt_norm, all_obs_prompt_norm.label, all_obs_prompt_norm.legendstyle) shape_legend.AddEntry( all_obs_prompt_mc_sub_norm, all_obs_prompt_mc_sub_norm.label, all_obs_prompt_mc_sub_norm.legendstyle) shape_legend.AddEntry( all_obs_nonprompt_control_norm, all_obs_nonprompt_control_norm.label, all_obs_nonprompt_control_norm.legendstyle) shape_legend.AddEntry( all_obs_nonprompt_control_mc_sub_norm, all_obs_nonprompt_control_mc_sub_norm.label, all_obs_nonprompt_control_mc_sub_norm.legendstyle) shape_legend.SetBorderSize(0) shape_legend.x1 = 0.50 shape_legend.y1 = 0.71 shape_legend.x2 = 0.88 shape_legend.y2 = 0.90 shape_legend.SetFillColor(0) shape_legend.Draw('same') # plot ratios all_obs_prompt_norm_ratio = copy(all_obs_prompt_norm) all_obs_prompt_mc_sub_norm_ratio = copy( all_obs_prompt_mc_sub_norm) all_obs_nonprompt_control_norm_ratio = copy( all_obs_nonprompt_control_norm) all_obs_nonprompt_control_mc_sub_norm_ratio = copy( all_obs_nonprompt_control_mc_sub_norm) all_obs_prompt_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) all_obs_nonprompt_control_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) all_obs_nonprompt_control_mc_sub_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) things_to_plot_ratio = [ all_obs_prompt_norm_ratio, all_obs_nonprompt_control_norm_ratio, all_obs_nonprompt_control_mc_sub_norm_ratio, ] for ithing in things_to_plot_ratio: ithing.xaxis.set_label_size( ithing.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_label_size( ithing.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.xaxis.set_title_size( ithing.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_title_size( ithing.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_ndivisions(405) ithing.yaxis.set_title_offset(0.4) ithing.SetMinimum(0.) ithing.SetMaximum(2.) draw(things_to_plot_ratio, xtitle=xlabel, ytitle='1/(data-MC)_{tight}', pad=self.ratio_pad, logy=False, ylimits=(0., 2.)) self.ratio_pad.cd() line.Draw('same') CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'shapes', 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # save only the datacards you want, don't flood everything if len(self.datacards) and label not in self.datacards: continue # FIXME! allow it to save datacards even for non data driven bkgs if self.data_driven: self.create_datacards(data=all_obs_prompt, bkgs={ 'prompt': all_exp_prompt, 'nonprompt': all_exp_nonprompt }, signals=all_signals, label=label)
def __make_overlay(self, pileup, threshold, hists, fits, labels, header): with preserve_current_style(): name = self.filename_format.format(pileup=pileup, threshold=threshold) xmin = int(hists[0].GetTotalHistogram().GetBinLowEdge(1)) xmax = int(hists[0].GetTotalHistogram().GetBinLowEdge( hists[0].GetTotalHistogram().GetNbinsX() + 1)) xtitle = "" if 'Jet' in self.online_title: xtitle = "Jet #it{p}_{T} (GeV)" if 'HT' in self.online_title: xtitle = "#it{H}_{T} (GeV)" if 'MET' in self.online_title: xtitle = "#it{E}_{T}^{miss} (GeV)" # Draw each efficiency (with fit) draw_args = { "xtitle": xtitle, "ytitle": "Efficiency", "xlimits": [xmin, xmax] } canvas = draw(hists, draw_args=draw_args) if len(fits) > 0: for fit, hist in zip(fits, hists): fit["asymmetric"].linecolor = hist.GetLineColor() fit["asymmetric"].Draw("same") # Add labels label_canvas() # Add a legend legend = Legend( len(hists), header=self.legend_title, topmargin=0.25, rightmargin=0.25, leftmargin=0.69, textsize=0.025, entryheight=0.028, ) for hist, label in zip(hists, labels): legend.AddEntry(hist, label) legend.SetBorderSize(0) legend.Draw() for val in [0.25, 0.5, 0.75, 0.95, 1.]: line = ROOT.TLine(xmin, val, xmax, val) line.SetLineStyle("dashed") line.SetLineColor(15) line.Draw() for t in xrange(xmin, xmax, 20): line = ROOT.TLine(t, 0., t, 1.) line.SetLineStyle("dashed") line.SetLineColor(15) line.Draw() # Save canvas to file self.save_canvas(canvas, name)
from rootpy.plotting import Graph, Canvas, Legend from rootpy.plotting.style import set_style from rootpy import ROOT set_style('ATLAS', shape='rect') ROOT.gROOT.SetBatch(True) gr_vbf = Graph(len(MASSES)) gr_ggh = Graph(len(MASSES)) for ip, (m, eff_vbf, eff_ggh) in enumerate(zip(MASSES, EFFS_VBF, EFFS_GGH)): gr_vbf.SetPoint(ip, m, eff_vbf) gr_ggh.SetPoint(ip, m, eff_ggh) gr_vbf.title = 'VBFH#rightarrow #tau#tau' gr_vbf.color = 'red' gr_vbf.xaxis.title = 'm_H [GeV]' gr_vbf.yaxis.title = 'Filter Efficiency' gr_vbf.yaxis.SetRangeUser(0, 0.6) gr_ggh.title = 'ggH#rightarrow #tau#tau' gr_ggh.color = 'blue' gr_ggh.xaxis.title = 'm_{H} [GeV]' c = Canvas() gr_vbf.Draw('APL') gr_ggh.Draw('SAMEPL') leg = Legend([gr_vbf, gr_ggh], pad=c, anchor='upper left') leg.Draw('same') c.SaveAs('filter_eff.png')
def draw_MuonPT(numMuons_tt, maxupt_tt, minupt_tt, numMuons_qcd, maxupt_qcd, minupt_qcd, numMuons_wjet, maxupt_wjet, minupt_wjet): #set line colors numMuons_tt.SetLineColor(4) numMuons_qcd.SetLineColor(8) numMuons_wjet.SetLineColor(2) numMuons_tt.legendstyle = 'L' numMuons_qcd.legendstyle = 'L' numMuons_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() numMuons_wjet.SetStats(0) numMuons_wjet.Draw('HIST') numMuons_tt.Draw('HIST SAME') numMuons_qcd.Draw('HIST SAME') #make legend l1 = Legend([numMuons_tt, numMuons_qcd, numMuons_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("MuonPT_numMuons.pdf") ################ MIN MAX STUFF # TT #set line colors maxupt_tt.SetLineColor(4) minupt_tt.SetLineColor(8) maxupt_tt.legendstyle = 'L' minupt_tt.legendstyle = 'L' #begin drawing stuff c2 = Canvas() minupt_tt.SetStats(0) minupt_tt.Draw('HIST') maxupt_tt.Draw('HIST SAME') #make legend l2 = Legend([minupt_tt, maxupt_tt], textfont=42, textsize=.03) l2.Draw() #save as pdf c2.SaveAs("MuonPT_maxminpt_tt.pdf") # QCD #set line colors maxupt_qcd.SetLineColor(4) minupt_qcd.SetLineColor(8) maxupt_qcd.legendstyle = 'L' minupt_qcd.legendstyle = 'L' #begin drawing stuff c3 = Canvas() maxupt_qcd.SetStats(0) maxupt_qcd.Draw('HIST') minupt_qcd.Draw('HIST SAME') #make legend l3 = Legend([minupt_qcd, maxupt_qcd], textfont=42, textsize=.03) l3.Draw() #save as pdf c3.SaveAs("MuonPT_maxminpt_qcd.pdf") #WJET #set line colors maxupt_wjet.SetLineColor(4) minupt_wjet.SetLineColor(8) maxupt_wjet.legendstyle = 'L' minupt_wjet.legendstyle = 'L' #begin drawing stuff c4 = Canvas() minupt_wjet.SetStats(0) minupt_wjet.Draw('HIST') maxupt_wjet.Draw('HIST SAME') #make legend l4 = Legend([minupt_wjet, maxupt_wjet], textfont=42, textsize=.03) l4.Draw() #save as pdf c4.SaveAs("MuonPT_maxminpt_wjet.pdf") # all max jets #set line colors maxupt_tt.SetLineColor(4) maxupt_qcd.SetLineColor(8) maxupt_wjet.SetLineColor(2) #begin drawing stuff c5 = Canvas() maxupt_qcd.SetStats(0) maxupt_qcd.Draw('HIST') maxupt_tt.Draw('HIST SAME') maxupt_wjet.Draw('HIST SAME') #make legend l5 = Legend([maxupt_tt, maxupt_qcd, maxupt_wjet], textfont=42, textsize=.03) l5.Draw() #save as pdf c5.SaveAs("muonpt_maxpts.pdf")
def draw_JetBTag(loose_tt, medium_tt, tight_tt, loose_qcd, medium_qcd, tight_qcd, loose_wjet, medium_wjet, tight_wjet): #set line colors loose_tt.SetLineColor(4) medium_tt.SetLineColor(4) tight_tt.SetLineColor(4) loose_qcd.SetLineColor(8) medium_qcd.SetLineColor(8) tight_qcd.SetLineColor(8) loose_wjet.SetLineColor(2) medium_wjet.SetLineColor(2) tight_wjet.SetLineColor(2) loose_tt.legendstyle = 'L' medium_tt.legendstyle = 'L' tight_tt.legendstyle = 'L' loose_qcd.legendstyle = 'L' medium_qcd.legendstyle = 'L' tight_qcd.legendstyle = 'L' loose_wjet.legendstyle = 'L' medium_wjet.legendstyle = 'L' tight_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() loose_wjet.SetStats(0) loose_wjet.Draw('HIST') loose_qcd.Draw('HIST SAME') loose_tt.Draw('HIST SAME') #make legend l1 = Legend([loose_tt, loose_qcd, loose_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("jetbtag_loose.pdf") c2 = Canvas() medium_wjet.SetStats(0) medium_wjet.Draw('HIST') medium_qcd.Draw('HIST SAME') medium_tt.Draw('HIST SAME') #make legend l2 = Legend([medium_tt, medium_qcd, medium_wjet], textfont=42, textsize=.03) l2.Draw() #save as pdf c2.SaveAs("jetbtag_medium.pdf") c3 = Canvas() tight_wjet.SetStats(0) tight_wjet.Draw('HIST') tight_qcd.Draw('HIST SAME') tight_tt.Draw('HIST SAME') #make legend l3 = Legend([tight_wjet, tight_qcd, tight_tt], textfont=42, textsize=.03) l3.Draw() #save as pdf c3.SaveAs("jetbtag_tight.pdf")
def main(args): fileList = glob('/data/nawoods/lepsForSIP/*.root') files = {int(f.split('_M')[1].split('.')[0]) : f for f in fileList} checkers = {m : MuonSIPChecker('m={}'.format(m), [f]) for m,f in files.iteritems()} sipRMS = Graph(len(fileList), type='errors') sipHists = [] dxyRMS = Graph(len(fileList), type='errors') dxyHists = [] dzRMS = Graph(len(fileList), type='errors') dzHists = [] ipHists = [] ipErrHists = [] fracFailing = Graph(len(fileList), type='errors') for i, m in enumerate(sorted(files.keys())): checkers[m].processSample() checkers[m].hists['sip'].Sumw2() sipRMS.SetPoint(i, float(m), checkers[m].hists['sip'].GetRMS()) sipRMS.SetPointError(i, 0., checkers[m].hists['sip'].GetRMSError()) sipHists.append(checkers[m].hists['sip']) sipHists[-1].color = getColor(m) sipHists[-1].title = "m_{{H}} = {}".format(m) sipHists[-1].drawstyle = 'hist' sipHists[-1].legendstyle = 'L' sipHists[-1].linewidth = 2 sipHists[-1].scale(1./sipHists[-1].integral()) ipHists.append(checkers[m].hists['ip']) ipHists[-1].color = getColor(m) ipHists[-1].title = "m_{{H}} = {}".format(m) ipHists[-1].drawstyle = 'hist' ipHists[-1].legendstyle = 'L' ipHists[-1].linewidth = 2 ipHists[-1].scale(1./ipHists[-1].integral()) ipErrHists.append(checkers[m].hists['ipErr']) ipErrHists[-1].color = getColor(m) ipErrHists[-1].title = "m_{{H}} = {}".format(m) ipErrHists[-1].drawstyle = 'hist' ipErrHists[-1].legendstyle = 'L' ipErrHists[-1].linewidth = 2 ipErrHists[-1].scale(1./ipErrHists[-1].integral()) checkers[m].hists['dxy'].Sumw2() dxyRMS.SetPoint(i, float(m), checkers[m].hists['dxy'].GetRMS()) dxyRMS.SetPointError(i, 0., checkers[m].hists['dxy'].GetRMSError()) dxyHists.append(checkers[m].hists['dxy']) dxyHists[-1].color = getColor(m) dxyHists[-1].title = "m_{{H}} = {}".format(m) dxyHists[-1].drawstyle = 'hist' dxyHists[-1].legendstyle = 'L' dxyHists[-1].linewidth = 2 dxyHists[-1].scale(1./dxyHists[-1].integral()) checkers[m].hists['dz'].Sumw2() dzRMS.SetPoint(i, float(m), checkers[m].hists['dz'].GetRMS()) dzRMS.SetPointError(i, 0., checkers[m].hists['dz'].GetRMSError()) dzHists.append(checkers[m].hists['dz']) dzHists[-1].color = getColor(m) dzHists[-1].title = "m_{{H}} = {}".format(m) dzHists[-1].drawstyle = 'hist' dzHists[-1].legendstyle = 'L' dzHists[-1].linewidth = 2 dzHists[-1].scale(1./dzHists[-1].integral()) fracFailing.SetPoint(i, float(m), 1. - float(checkers[m].nPassSIP) / checkers[m].nTot) cSIP = Canvas(1000,1000) draw(sipHists, cSIP, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}', ytitle='arb.') legSIP = Legend(sipHists, cSIP, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04) legSIP.Draw("same") cSIP.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sips.png') cSIPLog = Canvas(1000,1000) draw(sipHists, cSIPLog, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}', ytitle='arb.', logy=True) legSIPLog = Legend(sipHists, cSIPLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03) legSIPLog.Draw("same") cSIPLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sipsLog.png') cSIPRMS = Canvas(1000, 1000) sipRMS.color = 'b' sipRMS.drawstyle = 'PE' sipRMS.legendstyle = 'PE' draw(sipRMS, cSIPRMS, xtitle="m_{H}", ytitle="RMS(SIP_{3D})", xlimits=(0.,2600.)) cSIPRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sipRMS.png') cIP = Canvas(1000,1000) draw(ipHists, cIP, xtitle='IP_{3D}', ytitle='arb.') legIP = Legend(ipHists, cIP, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04) legIP.Draw("same") cIP.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ips.png') cIPLog = Canvas(1000,1000) draw(ipHists, cIPLog, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}', ytitle='arb.', logy=True) legIPLog = Legend(ipHists, cIPLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03) legIPLog.Draw("same") cIPLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipsLog.png') cIPErr = Canvas(1000,1000) draw(ipErrHists, cIPErr, xtitle='#sigma_{IP_{3D}}', ytitle='arb.') legIPErr = Legend(ipErrHists, cIPErr, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04) legIPErr.Draw("same") cIPErr.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipErrs.png') cIPErrLog = Canvas(1000,1000) draw(ipErrHists, cIPErrLog, xtitle='#sigma_{IP_{3D}}', ytitle='arb.', logy=True) legIPErrLog = Legend(ipErrHists, cIPErrLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03) legIPErrLog.Draw("same") cIPErrLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipErrsLog.png') cFail = Canvas(1000, 1000) fracFailing.color = 'b' fracFailing.drawstyle = 'PE' fracFailing.legendstyle = 'PE' draw(fracFailing, cSIPRMS, xtitle="m_{H}", ytitle="Fraction failing SIP", xlimits=(0.,2600.)) cSIPRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/fracFailing.png') cDXY = Canvas(1000,1000) draw(dxyHists, cDXY, xtitle='#Delta_{xy}', ytitle='arb.') legDXY = Legend(dxyHists, cDXY, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04) legDXY.Draw("same") cDXY.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dxys.png') cDXYRMS = Canvas(1000, 1000) dxyRMS.color = 'b' dxyRMS.drawstyle = 'PE' dxyRMS.legendstyle = 'PE' draw(dxyRMS, cDXYRMS, xtitle="m_{H}", ytitle="RMS(#Delta_{xy})", xlimits=(0.,2600.)) cDXYRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dxyRMS.png') cDZ = Canvas(1000,1000) draw(dzHists, cDZ, xtitle='#Delta_{z}', ytitle='arb.') legDZ = Legend(dzHists, cDZ, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04) legDZ.Draw("same") cDZ.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dzs.png') cDZRMS = Canvas(1000, 1000) dzRMS.color = 'b' dzRMS.drawstyle = 'PE' dzRMS.legendstyle = 'PE' draw(dzRMS, cDZRMS, xtitle="m_{H}", ytitle="RMS(#Delta_{z})", xlimits=(0.,2600.)) cDZRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dzRMS.png')
def extract_JetBTag(f_tt, f_qcd, f_wjet): # get trees from files t_tt = f_tt.Get("Delphes") t_qcd = f_qcd.Get("Delphes") t_wjet = f_wjet.Get("Delphes") # get number of entries tt_n_entries = t_tt.GetEntries() qcd_n_entries = t_qcd.GetEntries() wjet_n_entries = t_wjet.GetEntries() # define leaves var_tt = "Jet.BTag" var_qcd = "Jet.BTag" var_wjet = "Jet.BTag" leaf_tt = t_tt.GetLeaf(var_tt) leaf_qcd = t_qcd.GetLeaf(var_qcd) leaf_wjet = t_wjet.GetLeaf(var_wjet) # create the histograms loose_tt = Hist(NBINS, NLO, NHI, title='loose_tt', legendstyle='L') medium_tt = Hist(NBINS, NLO, NHI, title='medium_tt', legendstyle='L') tight_tt = Hist(NBINS, NLO, NHI, title='tight_tt', legendstyle='L') loose_qcd = Hist(NBINS, NLO, NHI, title='loose_qcd', legendstyle='L') medium_qcd = Hist(NBINS, NLO, NHI, title='medium_qcd', legendstyle='L') tight_qcd = Hist(NBINS, NLO, NHI, title='tight_qcd', legendstyle='L') loose_wjet = Hist(NBINS, NLO, NHI, title='loose_wjet', legendstyle='L') medium_wjet = Hist(NBINS, NLO, NHI, title='medium_wjet', legendstyle='L') tight_wjet = Hist(NBINS, NLO, NHI, title='tight_wjet', legendstyle='L') # FILLING THE TREE fill_JetBTag_tree(tt_n_entries, t_tt, leaf_tt, loose_tt, medium_tt, tight_tt) fill_JetBTag_tree(qcd_n_entries, t_qcd, leaf_qcd, loose_qcd, medium_qcd, tight_qcd) fill_JetBTag_tree(wjet_n_entries, t_wjet, leaf_wjet, loose_wjet, medium_wjet, tight_wjet) #set line colors loose_tt.SetLineColor('blue') medium_tt.SetLineColor('green') tight_tt.SetLineColor('red') loose_qcd.SetLineColor('blue') medium_qcd.SetLineColor('green') tight_qcd.SetLineColor('red') loose_wjet.SetLineColor('blue') medium_wjet.SetLineColor('green') tight_wjet.SetLineColor('red') #begin drawing stuff c1 = Canvas() tight_tt.SetStats(0) tight_tt.Draw('HIST') medium_tt.Draw('HIST SAME') loose_tt.Draw('HIST SAME') #make legend l1 = Legend([tight_tt, medium_tt, loose_tt], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("../plots/JetBTag_plots/btag_tt.pdf") c2 = Canvas() tight_qcd.SetStats(0) tight_qcd.Draw('HIST') medium_qcd.Draw('HIST SAME') loose_qcd.Draw('HIST SAME') #make legend l2 = Legend([tight_qcd, medium_qcd, loose_qcd], textfont=42, textsize=.03) l2.Draw() #save as pdf c2.SaveAs("../plots/JetBTag_plots/btag_qcd.pdf") c3 = Canvas() tight_wjet.SetStats(0) tight_wjet.Draw('HIST') medium_wjet.Draw('HIST SAME') loose_wjet.Draw('HIST SAME') #make legend l3 = Legend([tight_wjet, medium_wjet, loose_wjet], textfont=42, textsize=.03) l3.Draw() #save as pdf c3.SaveAs("../plots/JetBTag_plots/btag_wjet.pdf") wait(True)
def routine(chan, var, direction, binidx, titlecontent): hs = [] effs = OrderedDict() chandir = getattr(f, 'ch' + chan) for t in chandir.sig.keys(): sigtag = t.name h = getattr(getattr(chandir.sig, sigtag), var) # Hist h_total = h.integral(overflow=True) h_ = h.clone() for i in range(1, h.nbins() + 1): if direction == 'forward': h_sub = h.integral(xbin1=i, overflow=True) elif direction == 'backward': h_sub = h.integral(1, xbin2=i) h_[i] = h_sub / h_total h_[i].error = 0 if i == binidx: effs[sigtag] = h_sub / h_total h_.title = sigtag h_.drawstyle = 'PLC hist' h_.legendstyle = 'L' hs.append(h_) mineff = min(effs.values()) aveeff = sum(effs.values()) / len(effs) effs['sig_ave'] = aveeff effs['sig_min'] = mineff for h in getattr(chandir.bkg, var): h_total = h.integral(overflow=True) if h_total == 0: continue h_ = h.empty_clone() for i in range(1, h.nbins() + 1): if direction == 'forward': h_sub = h.integral(xbin1=i, overflow=True) elif direction == 'backward': h_sub = h.integral(1, xbin2=i) h_[i] = h_sub / h_total h_[i].error = 0 if i == binidx: effs[h.title] = h_sub / h_total h_.drawstyle = 'hist' h_.title = h.title h_.color = bkgCOLORS[h.title] h_.linestyle = 'dashed' h_.legendstyle = 'L' h_.linewidth = 2 hs.append(h_) if 'dphi' in var: for h in hs: xax = h.xaxis xax.SetNdivisions(-310) xax.ChangeLabel(2, -1, -1, -1, -1, -1, "#frac{#pi}{10}") xax.ChangeLabel(3, -1, -1, -1, -1, -1, "#frac{#pi}{5}") xax.ChangeLabel(4, -1, -1, -1, -1, -1, "#frac{3#pi}{10}") xax.ChangeLabel(5, -1, -1, -1, -1, -1, "#frac{2#pi}{5}") xax.ChangeLabel(6, -1, -1, -1, -1, -1, "#frac{#pi}{2}") xax.ChangeLabel(7, -1, -1, -1, -1, -1, "#frac{3#pi}{5}") xax.ChangeLabel(8, -1, -1, -1, -1, -1, "#frac{7#pi}{10}") xax.ChangeLabel(9, -1, -1, -1, -1, -1, "#frac{4#pi}{5}") xax.ChangeLabel(10, -1, -1, -1, -1, -1, "#frac{9#pi}{10}") xax.ChangeLabel(11, -1, -1, -1, -1, -1, "#pi") print('>', chan, var, direction) maxlen = max([len(k) for k in effs]) for k in effs: fmt = '{:%d}:{:.2f}' % (maxlen + 2) + '%' print(fmt.format(k, effs[k] * 100)) legend = Legend(hs, pad=c, margin=0.1, topmargin=0.02, entryheight=0.02, textsize=12) axes, limits = draw( hs, ylimits=(0, 1.5), logy=False, ytitle='({}) cut efficiency'.format(direction), ) legend.Draw() title = TitleAsLatex('[{}]'.format(chan.replace('mu', '#mu')) + ' {} cut efficiency'.format(titlecontent)) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') ROOT.gPad.SetGrid() c.SaveAs('{}/ch{}_{}.pdf'.format(outdir, chan, var)) c.Clear()
def calc_fake_factors(name, numers_data, numers_bkg, denoms_data, denoms_bkg, labels, xtitle, ytitle, save=None, ylimits=None): assert numers_data, numers_bkg if not isinstance(numers_data, list): numers_data = [numers_data] if not isinstance(numers_bkg, list): numers_bkg = [numers_bkg] if not isinstance(denoms_data, list): denoms_data = [denoms_data] if not isinstance(denoms_bkg, list): denoms_bkg = [denoms_bkg] if save is None: save = ['pdf', 'png'] stuff = dict() objects = list() colors = [ ipyhep.style.red, ipyhep.style.blue, ipyhep.style.orange, ipyhep.style.violet, ] i_ff = 0 for numer_data, numer_bkg, denom_data, denom_bkg in zip( numers_data, numers_bkg, denoms_data, denoms_bkg): for h in (numer_data, numer_bkg, denom_data, denom_bkg): if h and not h.GetSumw2N(): h.Sumw2() h_numer = numer_data.Clone() h_numer.SetDirectory(0) if numer_bkg: set_negative_bins_zero(numer_bkg) h_numer.Add(numer_bkg, -1.0) h_denom = denom_data.Clone() h_denom.SetDirectory(0) if denom_bkg: set_negative_bins_zero(denom_bkg) h_denom.Add(denom_bkg, -1.0) h_ff = h_numer.Clone() h_ff.SetDirectory(0) divide_option = '' h_ff.Divide(h_numer, h_denom, 1.0, 1.0, divide_option) h_ff.linecolor = colors[i_ff] h_ff.markercolor = colors[i_ff] h_ff.markerstyle = 20 h_ff.markersize = 1.2 h_ff.fillstyle = ipyhep.style.fill_hollow h_ff.drawstyle = 'PE' h_ff.legendstyle = 'LP' if labels: h_ff.title = '%s' % labels[i_ff] else: h_ff.title = '%i' % i_ff h_ff.name = 'h_ff_%s' % h_ff.title stuff['h_ff_%s' % h_ff.title] = h_ff objects.append(h_ff) i_ff += 1 ## make canvas canvas = Canvas(800, 600) stuff['canvas'] = canvas xmin, xmax, ymin, ymax = get_limits(objects) xlimits = (xmin, xmax) # ylimits = (ymin, ymax) ylimits = ylimits or (0.0, 1.0) draw(objects, pad=canvas, xtitle=xtitle, ytitle=ytitle, xlimits=xlimits, ylimits=ylimits) ## legend header = '' legend = Legend(objects, pad=canvas, header=header, textsize=16, topmargin=0.02, leftmargin=0.60, rightmargin=0.02, entrysep=0.01, entryheight=0.04) legend.Draw() stuff['legend'] = legend ## save figures if save: ipyhep.file.save_figures(canvas, 'h_ff', save) ## save fake factors timestamp = time.strftime('%Y_%m_%d_%Hh%M') outfile = 'fakefactors-%s.root' % timestamp f_out = None for h_ff in objects: f_out = ipyhep.file.write(h_ff, outfile) # f_out.Close() # ipyhep.file.close_all_files() return stuff
def draw_ratio(a, b, field, category, textsize=22, ratio_range=(0, 2), ratio_line_values=[0.5, 1, 1.5], optional_label_text=None, normalize=True, logy=False): """ Draw a canvas with two Hists normalized to unity on top and a ratio plot between the two hist Parameters: - a: Nominal Hist (denominator in the ratio) - b: Shifted Hist (numerator in the ratio) - field: variable field (see variables.py) - category: analysis category (see categories/*) """ if field in VARIABLES: xtitle = VARIABLES[field]['root'] else: xtitle = field plot = RatioPlot( xtitle=xtitle, ytitle='{0}Events'.format('Normalized ' if normalize else ''), ratio_title='A / B', ratio_limits=ratio_range, ratio_line_values=ratio_line_values, logy=logy) if normalize: a_integral = a.integral() if a_integral != 0: a /= a_integral b_integral = b.integral() if b_integral != 0: b /= b_integral a.title = 'A: ' + a.title b.title = 'B: ' + b.title a.color = 'black' b.color = 'red' a.legendstyle = 'L' b.legendstyle = 'L' a.markersize = 0 b.markersize = 0 a.linewidth = 2 b.linewidth = 2 a.fillstyle = 'hollow' b.fillstyle = 'hollow' a.linestyle = 'solid' b.linestyle = 'dashed' a.drawstyle = 'hist E0' b.drawstyle = 'hist E0' plot.draw('main', [a, b], ypadding=(0.3, 0.)) ratio = Hist.divide(a, b, fill_value=-1) ratio.drawstyle = 'hist' ratio.color = 'black' ratio_band = Graph(ratio, fillstyle='/', fillcolor='black', linewidth=0) ratio_band.drawstyle = '20' plot.draw('ratio', [ratio_band, ratio]) with plot.pad('main') as pad: # legend leg = Legend([a, b], 0.2, 0.2, 0.45, margin=0.35, textsize=textsize) leg.Draw() # draw the category label if category is not None: label = ROOT.TLatex(pad.GetLeftMargin() + 0.04, 0.87, category.label) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) label.Draw() # show p-value and chi^2 pvalue = a.Chi2Test(b, 'WW') pvalue_label = ROOT.TLatex(pad.GetLeftMargin() + 0.04, 0.8, "p-value={0:.2f}".format(pvalue)) pvalue_label.SetNDC(True) pvalue_label.SetTextFont(43) pvalue_label.SetTextSize(textsize) pvalue_label.Draw() chi2 = a.Chi2Test(b, 'WW CHI2/NDF') chi2_label = ROOT.TLatex( pad.GetLeftMargin() + 0.04, 0.72, "#frac{{#chi^{{2}}}}{{ndf}}={0:.2f}".format(chi2)) chi2_label.SetNDC(True) chi2_label.SetTextFont(43) chi2_label.SetTextSize(textsize) chi2_label.Draw() if optional_label_text is not None: optional_label = ROOT.TLatex(pad.GetLeftMargin() + 0.55, 0.87, optional_label_text) optional_label.SetNDC(True) optional_label.SetTextFont(43) optional_label.SetTextSize(textsize) optional_label.Draw() if ATLAS_LABEL.lower() == 'internal': x = 0.67 y = 1 - pad.GetTopMargin() + 0.005 else: x = (1. - pad.GetRightMargin() - 0.03) - len(ATLAS_LABEL) * 0.025 y = 1 - pad.GetTopMargin() + 0.01 ATLAS_label(x, y, sep=0.132, pad=pad, sqrts=None, text=ATLAS_LABEL, textsize=textsize) return plot
def stack(x, *args, **kwargs): ## parse arguments _data = kwargs.pop('data', None) _bkgs = kwargs.pop('bkgs', None) _sigs = kwargs.pop('sigs', None) _treename = kwargs.pop('treename', None) _datasearchpath = kwargs.pop('datasearchpath', None) _datadrivensearchpath = kwargs.pop('datadrivensearchpath', None) _bkgsearchpath = kwargs.pop('bkgsearchpath', None) _sigsearchpath = kwargs.pop('sigsearchpath', None) _lumi = kwargs.pop('lumi', None) global data global bkgs global sigs global treename global datasearchpath global datadrivensearchpath global bkgsearchpath global sigsearchpath global lumi data = _data or data bkgs = _bkgs or bkgs sigs = _sigs or sigs treename = _treename or treename datasearchpath = _datasearchpath or datasearchpath datadrivensearchpath = _datadrivensearchpath or datadrivensearchpath bkgsearchpath = _bkgsearchpath or bkgsearchpath sigsearchpath = _sigsearchpath or sigsearchpath if _lumi: lumi = float(_lumi) xtitle = kwargs.pop('xtitle', '') ytitle = kwargs.pop('ytitle', '') logx = bool(kwargs.pop('logx', False)) logy = bool(kwargs.pop('logy', False)) blind = kwargs.pop('blind', None) has_blinded_data = False ## save stuff to bookkeep and return stuff = dict() stuff['x'] = x ## get data histogram h_data = None if data: sp = datasearchpath # HACK: just data to True! newx = '%s::%s::%s' % (sp, treename, x) h_data = ipyhep.tree.project(newx, *args, **kwargs) if h_data: stuff['h_data'] = h_data ## blind the data? if h_data and not blind is None: if isinstance(blind, tuple): blind1, blind2 = blind nbins = h_data.GetNbinsX() for i_bin in xrange(1, nbins + 2): # skip underflow (but not overflow) xval1 = h_data.GetXaxis().GetBinLowEdge(i_bin) xval2 = h_data.GetXaxis().GetBinUpEdge(i_bin) if xval1 >= blind1 and xval2 <= blind2: h_data.SetBinContent(i_bin, 0.0) h_data.SetBinError(i_bin, 0.0) has_blinded_data = True else: nbins = h_data.GetNbinsX() for i_bin in xrange(1, nbins + 2): # skip underflow (but not overflow) xval = h_data.GetXaxis().GetBinLowEdge(i_bin) if xval >= blind: h_data.SetBinContent(i_bin, 0.0) h_data.SetBinError(i_bin, 0.0) has_blinded_data = True ## get background histograms h_bkgs = list() n_bkgs = list() if bkgs: for bkg in bkgs: if isinstance(bkg, list): h_subtotal = None for dsid in bkg: assert isinstance(dsid, str) h_bkg = None if dsid.isdigit(): ## mc backgrounds sp = bkgsearchpath % int(dsid) newx = '%s::%s::%s' % (sp, treename, x) h_bkg = ipyhep.tree.project(newx, *args, **kwargs) else: ## data-driven backgrounds assert dsid == 'fakes' or dsid == 'efakes' sp = datadrivensearchpath % dsid newx = '%s::%s::%s' % (sp, treename, x) h_bkg = ipyhep.tree.project(newx, *args, **kwargs) if h_bkg: if h_subtotal: h_subtotal.Add(h_bkg) else: h_subtotal = h_bkg.Clone() if h_subtotal: h_bkgs.append(h_subtotal) dsid = bkg[0] n_bkgs.append(dsid) else: dsid = bkg assert isinstance(dsid, str) h_bkg = None if dsid.isdigit(): ## mc backgrounds sp = bkgsearchpath % int(dsid) newx = '%s::%s::%s' % (sp, treename, x) h_bkg = ipyhep.tree.project(newx, *args, **kwargs) else: ## data-driven backgrounds assert dsid == 'fakes' or dsid == 'efakes' sp = datadrivensearchpath % dsid newx = '%s::%s::%s' % (sp, treename, x) h_bkg = ipyhep.tree.project(newx, *args, **kwargs) if h_bkg: h_bkgs.append(h_bkg) n_bkgs.append(dsid) if h_bkgs: stuff['h_bkgs'] = h_bkgs ## get signal histograms h_sigs = list() n_sigs = list() if sigs: for dsid in sigs: sp = sigsearchpath % int(dsid) newx = '%s::%s::%s' % (sp, treename, x) h_sig = ipyhep.tree.project(newx, *args, **kwargs) if h_sig: h_sigs.append(h_sig) n_sigs.append(dsid) if h_sigs: stuff['h_sigs'] = h_sigs assert h_sigs ## style data if h_data: h_data.title = 'Data' h_data.linecolor = ipyhep.style.black h_data.linewidth = 2 h_data.markercolor = ipyhep.style.black h_data.markerstyle = 20 h_data.markersize = 1.2 h_data.fillstyle = ipyhep.style.fill_hollow h_data.drawstyle = 'PE' h_data.legendstyle = 'LP' ## scale and style background histograms if h_bkgs: assert len(h_bkgs) == len(n_bkgs), '%s\n%s' % (h_bkgs, n_bkgs) for h, dsid in zip(h_bkgs, n_bkgs): sf = ipyhep.sampleops.get_sf(dsid) if dsid.isdigit(): sf *= lumi / __ntuple_lumi h.Scale(sf) h.title = ipyhep.sampleops.get_label(dsid) h.linecolor = ipyhep.style.black h.linewidth = 1 h.markercolor = ipyhep.sampleops.get_color(dsid) h.fillcolor = ipyhep.sampleops.get_color(dsid) h.fillstyle = ipyhep.style.fill_solid h.legendstyle = 'F' ## calculate stat error on total background h_bkg_total = None if h_bkgs: for h_bkg in h_bkgs: if h_bkg_total: h_bkg_total.Add(h_bkg) else: h_bkg_total = h_bkg.Clone() stuff['h_bkg_total'] = h_bkg_total ## style h_bkg_total if h_bkg_total: h_bkg_total.title = 'stat. uncert.' h_bkg_total.linecolor = ipyhep.style.black h_bkg_total.linewidth = 1 h_bkg_total.markerstyle = 0 h_bkg_total.fillcolor = ipyhep.style.dark_gray h_bkg_total.fillstyle = ipyhep.style.fill_lines h_bkg_total.drawstyle = 'E2' h_bkg_total.legendstyle = 'LF' ## scale and style signal histograms if h_sigs: assert len(h_sigs) == len(n_sigs) for h, dsid in zip(h_sigs, n_sigs): sf = ipyhep.sampleops.get_sf(dsid) sf *= lumi / __ntuple_lumi h.Scale(sf) h.title = ipyhep.sampleops.get_label(dsid) h.linecolor = ipyhep.sampleops.get_color(dsid) h.linewidth = 3 h.fillstyle = ipyhep.style.fill_hollow h.markerstyle = 0 h.drawstyle = 'HIST' h.legendstyle = 'L' ## build list of all_hists all_hists = list() main_hists = list() if h_data: all_hists.append(h_data) main_hists.append(h_data) if h_bkgs: all_hists.extend(h_bkgs) main_hists.extend(h_bkgs) if h_bkg_total: all_hists.append(h_bkg_total) main_hists.append(h_bkg_total) if h_sigs: all_hists.extend(h_sigs) ## get statistics if all_hists: stats_list = list() for h in all_hists: stats_list.extend(get_stats(h)) html = convert_table_to_html(convert_stats_to_table(stats_list)) stuff['html'] = html ## renormalize for bin widths bins = kwargs.pop('bins', None) if bins and isinstance(bins, list): for h in all_hists: renormalize_for_bin_widths(h, bins) ## stack background histograms if h_bkgs: assert len(h_bkgs) == len(n_bkgs), '%s\n%s' % (h_bkgs, n_bkgs) h_bkgs.reverse() n_bkgs.reverse() hstack = HistStack() for h in h_bkgs: hstack.Add(h) hstack.title = 'stack sum' hstack.drawstyle = 'HIST' stuff['stack'] = hstack h_bkgs.reverse() n_bkgs.reverse() # ## convert data to TGraphAsymmErrors # g_data = None # if h_data: # if __use_poissonize: # g_data = poissonize.GetPoissonizedGraph(h_data) # else: # g_data = ROOT.TGraphAsymmErrors() # i_g = 0 # nbins = h_data.GetNbinsX() # for i_bin in xrange(1, nbins+1): # skip underflow/overflow # c = h_data.GetBinContent(i_bin) # e = h_data.GetBinError(i_bin) # if c != 0.0: # g_data.SetPoint(i_g, h_data.GetBinCenter(i_bin), c) # g_ratio.SetPointError(i_g, # h_data.GetBinWidth(i_bin)/2., # h_data.GetBinWidth(i_bin)/2., # e, # e) # i_g += 1 ## build list of objects to draw objects = list() if h_bkgs: objects.append(stuff['stack']) objects.append(stuff['h_bkg_total']) if h_sigs: objects.extend(h_sigs) if h_data: objects.append(h_data) ## set xlimits and ylimits ypadding = 0.21 logy_crop_value = 7e-3 xmin, xmax, ymin, ymax = 0.0, 1.0, 0.0, 1.0 if objects: xmin, xmax, ymin, ymax = get_limits(objects, logx=logx, logy=logy, ypadding=ypadding, logy_crop_value=logy_crop_value) if logy: ymin = 7e-3 else: ymin = 0.0 xlimits = (xmin, xmax) ylimits = (ymin, ymax) stuff['xlimits'] = xlimits stuff['ylimits'] = ylimits ## remove xtitle for do_ratio _xtitle = xtitle if h_data and h_bkg_total and kwargs.get('do_ratio'): _xtitle = '' ## make canvas canvas = Canvas(800, 600) stuff['canvas'] = canvas ## draw the objects if objects: canvas.cd() draw(objects, pad=canvas, xtitle=_xtitle, ytitle=ytitle, xlimits=xlimits, ylimits=ylimits) ## set log x/y, for some reason doesn't work before draw if logx or logy: if logx: canvas.SetLogx() if logy: canvas.SetLogy() canvas.Update() ## draw blind_line if has_blinded_data: if isinstance(blind, tuple): blind_list = list(blind) else: blind_list = [blind] blind_lines = list() for bl in blind_list: line_y1 = ymin line_y2 = ymax blind_line = ROOT.TLine(bl, line_y1, bl, line_y2) blind_line.SetLineColor(ROOT.kGray + 2) blind_line.SetLineStyle(7) blind_line.SetLineWidth(2) blind_line.Draw() blind_lines.append(blind_line) stuff['blind_lines'] = blind_lines canvas.Update() ## legend lefty = True if h_bkg_total: lefty = is_left_sided(h_bkg_total) elif h_data: lefty = is_left_sided(h_data) elif h_sigs: lefty = is_left_sided(h_sigs[0]) if main_hists: header = '%.1f fb^{-1}, 13 TeV' % (lumi / 1000.0) if lefty: legend = Legend(main_hists, pad=canvas, header=header, textsize=16, topmargin=0.03, leftmargin=0.60, rightmargin=0.02, entrysep=0.01, entryheight=0.04) else: legend = Legend(main_hists, pad=canvas, header=header, textsize=16, topmargin=0.03, leftmargin=0.03, rightmargin=0.59, entrysep=0.01, entryheight=0.04) legend.Draw() stuff['legend'] = legend if h_sigs: # header = 'ATLAS Internal' header = '' if lefty: legend2 = Legend(h_sigs, pad=canvas, header=header, textsize=16, topmargin=0.03, leftmargin=0.37, rightmargin=0.23, entrysep=0.01, entryheight=0.04) else: legend2 = Legend(h_sigs, pad=canvas, header=header, textsize=16, topmargin=0.03, leftmargin=0.20, rightmargin=0.40, entrysep=0.01, entryheight=0.04) legend2.Draw() stuff['legend2'] = legend2 ## do_ratio if h_data and h_bkg_total and kwargs.get('do_ratio'): ## top canvas top_canvas = stuff.pop('canvas') stuff['top_canvas'] = top_canvas ## make SM/SM with error band: h_ratio_band i_sfratio = int(kwargs.get('sfratio', -1)) if i_sfratio < 0: # ratio plot of Data/Model h_ratio_band = h_bkg_total.Clone() nbins = h_ratio_band.GetNbinsX() for i_bin in xrange(nbins + 2): h_ratio_band.SetBinContent(i_bin, 1.0) c = h_bkg_total.GetBinContent(i_bin) e = h_bkg_total.GetBinError(i_bin) / c if c > 0.0 else 0.0 h_ratio_band.SetBinError(i_bin, e) stuff['h_ratio_band'] = h_ratio_band else: # ratio plot of Scale Factor for ith background hi = h_bkgs[i_sfratio] h_ratio_band = hi.Clone() nbins = h_ratio_band.GetNbinsX() for i_bin in xrange(nbins + 2): h_ratio_band.SetBinContent(i_bin, 1.0) c = hi.GetBinContent(i_bin) e = hi.GetBinError(i_bin) / c if c > 0.0 else 0.0 h_ratio_band.SetBinError(i_bin, e) stuff['h_ratio_band'] = h_ratio_band ## make data/(SM) h_ratio if i_sfratio < 0: h_ratio = h_data.Clone() h_ratio.Divide(h_data, h_bkg_total, 1.0, 1.0) stuff['h_ratio'] = h_ratio else: ## SF1 = 1.0 + (data - MCtot) / MC1 sfname = kwargs.get('sfname') sffile = kwargs.get('sffile') if not sfname: sfname = 'h_sf' hi = h_bkgs[i_sfratio] h_numer = h_data.Clone() h_numer.Add(h_bkg_total, -1.0) ## do the division h_ratio = h_data.Clone(sfname) h_ratio.Divide(h_numer, hi, 1.0, 1.0) ## add the 1.0 nbins = h_ratio.GetNbinsX() for i_bin in xrange(nbins + 2): c = h_ratio.GetBinContent(i_bin) h_ratio.SetBinContent(i_bin, c + 1.0) h_ratio_band.SetBinContent(i_bin, c + 1.0) ## ignore bins with no data for SF for i_bin in xrange(nbins + 2): c = h_data.GetBinContent(i_bin) if c <= 0: h_ratio.SetBinContent(i_bin, 0.0) h_ratio.SetBinError(i_bin, 0.0) h_ratio_band.SetBinError(i_bin, 0.0) stuff['h_ratio'] = h_ratio if sffile: f_out = ipyhep.file.write(h_ratio, sffile) # f_out.Close() ## convert ratio to a TGraphErrors so that Draw('E0') ## shows error bars for points off the pad g_ratio = ROOT.TGraphErrors() i_g = 0 for i_bin in xrange(1, nbins + 1): # skip underflow/overflow ratio_content = h_ratio.GetBinContent(i_bin) if ratio_content != 0.0: g_ratio.SetPoint(i_g, h_ratio.GetBinCenter(i_bin), ratio_content) g_ratio.SetPointError(i_g, h_ratio.GetBinWidth(i_bin) / 2., h_ratio.GetBinError(i_bin)) i_g += 1 else: h_ratio.SetBinError(i_bin, 0.0) stuff['g_ratio'] = g_ratio ## style ratio h_ratio_band.title = 'bkg uncert.' if i_sfratio < 0: h_ratio_band.linecolor = ipyhep.style.yellow else: h_ratio_band.linecolor = ipyhep.style.light_gray h_ratio_band.linewidth = 0 h_ratio_band.markerstyle = 0 if i_sfratio < 0: h_ratio_band.fillcolor = ipyhep.style.yellow else: h_ratio_band.linecolor = ipyhep.style.light_gray h_ratio_band.fillstyle = ipyhep.style.fill_solid h_ratio_band.drawstyle = 'E2' h_ratio_band.legendstyle = 'F' h_ratio.title = 'ratio' h_ratio.linecolor = ipyhep.style.black h_ratio.linewidth = 2 h_ratio.markercolor = ipyhep.style.black h_ratio.markerstyle = 20 h_ratio.markersize = 1.2 h_ratio.fillstyle = ipyhep.style.fill_hollow h_ratio.drawstyle = 'PE' h_ratio.legendstyle = 'LP' ## bottom canvas bottom_canvas = Canvas(800, 600) bottom_canvas.cd() stuff['bottom_canvas'] = bottom_canvas ## set ratio ylimits ratio_min = kwargs.get('ratio_min', -0.2) ratio_max = kwargs.get('ratio_max', 2.2) ratio_ylimits = (ratio_min, ratio_max) ## draw ratio band if i_sfratio < 0: _ytitle = 'Data / Model' else: hi = h_bkgs[i_sfratio] _ytitle = 'SF(%s)' % hi.title draw([h_ratio_band], pad=bottom_canvas, xtitle=xtitle, ytitle=_ytitle, xlimits=xlimits, ylimits=ratio_ylimits) ## set log x/y, for some reason doesn't work before draw? if logx: bottom_canvas.SetLogx() bottom_canvas.Update() ### make horiz lines in ratio plot every 0.5: line_ys = [ y / 10.0 for y in range(10 * int(round(ratio_min)), 10 * int(round(ratio_max)) + 5, 5) ] line_x1 = canvas.GetUxmin() line_x2 = canvas.GetUxmax() line_xwidth = abs(line_x2 - line_x1) lines = [] for line_y in line_ys: line = ROOT.TLine(line_x1 + 0.02 * line_xwidth, line_y, line_x2 - 0.02 * line_xwidth, line_y) line.SetLineWidth(1) line.SetLineStyle(7) if line_y == 1.0: line.SetLineColor(ROOT.kGray + 2) else: line.SetLineColor(ROOT.kGray + 0) line.Draw() lines.append(line) stuff['lines'] = lines ## draw blind_line if has_blinded_data: if isinstance(blind, tuple): blind_list = list(blind) else: blind_list = [blind] blind_lines = list() for bl in blind_list: line_y1 = ymin line_y2 = ymax blind_line = ROOT.TLine(bl, line_y1, bl, line_y2) blind_line.SetLineColor(ROOT.kGray + 2) blind_line.SetLineStyle(7) blind_line.SetLineWidth(2) blind_line.Draw() blind_lines.append(blind_line) stuff['blind_lines2'] = blind_lines canvas.Update() ## draw ratio g_ratio.Draw('PE0') # h_ratio.GetYaxis().SetRangeUser(ratio_min, ratio_max) # h_ratio.Draw('PE,SAME') ## shared canvas shared_canvas = Canvas(800, 800) shared_plot = plot_shared_axis(top_canvas, bottom_canvas, canvas=shared_canvas, split=0.35, axissep=0.01) stuff['canvas'] = shared_canvas canvas = shared_canvas ## save figures save = kwargs.get('save') if save is None: # NOTE: save can be False to skip saving save = ['pdf', 'png'] if save: ipyhep.file.save_figures(canvas, x, save) global results results = stuff return stuff
def routine(chan): hs=[] effs = OrderedDict() chandir = getattr(f, 'ch'+chan) for it, sigtag in enumerate(sigTAGS): h = getattr(getattr(chandir.sig, sigtag), 'dphi') # Hist h_total = h.integral(overflow=True) if h_total==0: continue h_ = h.clone() for i in range(1, h.nbins()+1): h_[i] = h.integral(xbin1=i, overflow=True)/h_total h_[i].error = 0 if i==22: effs[sigtag] = h.integral(xbin1=i, overflow=True)/h_total h_.title = sigtag h_.color = sigCOLORS[it] h_.drawstyle = 'hist' h_.linewidth=2 h_.legendstyle='L' hs.append(h_) mineff = min(effs.values()) aveeff = sum(effs.values())/len(effs) effs['sig_ave'] = aveeff effs['sig_min'] = mineff for h in getattr(chandir.bkg, 'dphi'): h_total = h.integral(overflow=True) if h_total==0: continue h_ = h.empty_clone() for i in range(1, h.nbins()+1): h_[i] = h.integral(xbin1=i, overflow=True)/h_total h_[i].error = 0 if i==22: effs[h.title] = h.integral(xbin1=i, overflow=True)/h_total h_.drawstyle = 'hist' h_.title=h.title h_.color = bkgCOLORS[h.title] h_.linestyle = 'dashed' h_.legendstyle='L' h_.linewidth=2 hs.append(h_) for h in hs: xax = h.xaxis decorate_axis_pi(xax) print('>',chan) maxlen = max([len(k) for k in effs]) for k in effs: fmt = '{:%d}:{:.2f}'%(maxlen+2)+'%' print(fmt.format(k, effs[k]*100)) legend = Legend(hs, pad=c, margin=0.1, topmargin=0.02, entryheight=0.02, entrysep=0.01, textsize=12) axes, limits =draw(hs, ylimits=(0., 1.5), ytitle='(forward) cut efficiency',) legend.Draw() title = TitleAsLatex('[{}] lepton-jet pair |#Delta#phi| cut efficiency'.format(chan.replace('mu', '#mu'))) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') ROOT.gPad.SetGrid() c.SaveAs('{}/ch{}_dphi.pdf'.format(outdir, chan)) c.Clear()
def draw_curve_8(_func, name, title, ylow, yhigh, num_errs=1): """ Draw 8TeV trigger efficiency curves """ graphs = [] for (trigger, color) in triggers: tool = ROOT.TrigTauEfficiency() tool.loadInputFile( os.path.join(base, 'triggerSF_{0}.root'.format(trigger))) func = getattr(tool, _func) eff = np.array( map(lambda x: func(x, eta, 0, period, prong, wpflag, eveto), pt)) errs_low = [] errs_high = [] for ierr in xrange(num_errs): eff_low = np.array( map(lambda x: func(x, eta, -1, period, prong, wpflag, eveto), pt)) eff_high = np.array( map(lambda x: func(x, eta, 1, period, prong, wpflag, eveto), pt)) errs_low.append(eff_low) errs_high.append(eff_high) # quadrature sum of error eff_low = np.sqrt( np.sum([np.power(err, 2) for err in errs_low], axis=0)) eff_high = np.sqrt( np.sum([np.power(err, 2) for err in errs_high], axis=0)) graph = Graph(len(pt), name=trigger) for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)): graph.SetPoint(i, p / 1000, e) graph.SetPointError(i, 0.4, 0.4, e_low, e_high) graph.linecolor = color graph.linewidth = 2 graph.fillstyle = '/' graph.fillcolor = color graphs.append(graph) c = Canvas() leg = Legend(len(graphs), pad=c, topmargin=0.6, leftmargin=0.3, textsize=25, margin=0.2) for i, g in enumerate(graphs): if i == 0: g.Draw('3AL') g.xaxis.title = '#font[52]{p}_{T} [GeV]' g.xaxis.SetLimits(20, 100) g.yaxis.SetLimits(ylow, yhigh) g.yaxis.SetRangeUser(ylow, yhigh) g.yaxis.title = title else: g.Draw('3L SAME') leg.AddEntry(g, g.name, 'L') leg.Draw() lines = [] for thresh in (25, 35): line = Line(thresh, ylow, thresh, yhigh) line.linestyle = 'dashed' line.linewidth = 2 line.Draw() lines.append(line) c.SaveAs('trigger_{0}.png'.format(name)) c.SaveAs('trigger_{0}.eps'.format(name))
def __make_overlay(self, pileup, threshold, hists, fits, labels, header): with preserve_current_style(): name = self.filename_format.format(pileup=pileup, threshold=threshold) # Draw each efficiency (with fit) draw_args = {"xtitle": self.offline_title, "ytitle": "Efficiency"} # TODO: special case should not be implemented here! if 'Jet' in name and 'HiRange' in name: draw_args['xlimits'] = [20, 2000] canvas = draw(hists, draw_args=draw_args) if len(fits) > 0: for fit, hist in zip(fits, hists): fit["asymmetric"].linecolor = hist.GetLineColor() fit["asymmetric"].Draw("same") # Add labels label_canvas() # Add a legend legend = Legend( len(hists), header=self.legend_title, topmargin=0.35, rightmargin=0.3, leftmargin=0.7, textsize=0.025, entryheight=0.028, ) for hist, label in zip(hists, labels): legend.AddEntry(hist, label) legend.SetBorderSize(0) legend.Draw() xmin = 0 xmax = self.x_max # TODO: also specialisation, needs removal if ("HT" in name): xmax = 800 xmin = 30 if ("MET" in name): xmin = 0 xmax = 400 if ("Jet" in name): xmin = 20 xmax = 400 if ("HiRange" in name): xmax = 2000 for val in [0.25, 0.5, 0.75, 0.95, 1.]: line = ROOT.TLine(xmin, val, xmax, val) line.SetLineStyle("dashed") line.SetLineColor(15) line.Draw() for val in range(100, xmax, 100): line = ROOT.TLine(val, 0., val, 1.) line.SetLineStyle("dashed") line.SetLineColor(15) line.Draw() # Save canvas to file self.save_canvas(canvas, name)
def routine(chandir, bkgh, optm_region, outdir): canvas = Canvas() ROOT.gPad.SetGrid() for massKey in chandir.keys(): mboundstate = int(massKey.name.split('_')[0].replace('mXX-', '')) NORM_FACTOR_SIGNAL = 30./genxsec[mboundstate] # norm to 30/fb massDir = getattr(chandir, massKey.name) _outdir = os.path.join(outdir, massKey.name) if not os.path.isdir(_outdir): os.makedirs(_outdir) boundary_edges = {} for lifetimeKey in massDir.keys(): lifetimeDir = getattr(massDir, lifetimeKey.name) sigh = lifetimeDir.dphiIso2D_nominal sigh.scale( NORM_FACTOR_SIGNAL ) ###### ZA map ###### zah = create_za_map(sigh, bkgh, optm_region=optm_region) zah.Draw('colz') zah.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) mkh, xedge, yedge, maxval = mark_maximum_bin(zah) boundary_edges[lifetimeKey.name] = (xedge, yedge, maxval) # bookkeeping for overlap plot label = '(|#Delta#phi|, iso): {:.2f}, {:.2f} '.format(xedge, yedge)+'Z_{A}: '+'{:.3f}'.format(maxval) mkh.Draw('p same') if outdir.endswith('ch4mu'): title = TitleAsLatex('[4#mu {}] '.format(massKey.name)+'proxy significance Z_{A}') if outdir.endswith('ch2mu2e'): title = TitleAsLatex('[2#mu2e {}] '.format(massKey.name)+'proxy significance Z_{A}') title.Draw() leg = Legend(1, margin=0.25, leftmargin=0.05, rightmargin=0.5, topmargin=0.05, entrysep=0.01, entryheight=0.02, textsize=15, header=lifetimeKey.name) leg.AddEntry(mkh, label=label) leg.Draw() canvas.SaveAs('{}/ZA__{}.png'.format(_outdir, lifetimeKey.name)) canvas.SaveAs('{}/ZA__{}.pdf'.format(_outdir, lifetimeKey.name)) canvas.Clear() ###### signal distribution ###### sigc = sigh.clone() sigc.Draw('colz') if sigc.integral(): sigc.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) if outdir.endswith('ch4mu'): title = TitleAsLatex('[4#mu SR {}] '.format(massKey.name)+'|#Delta#phi| vs. maxIso') if outdir.endswith('ch2mu2e'): title = TitleAsLatex('[2#mu2e SR {}] '.format(massKey.name)+'|#Delta#phi| vs. Iso') sigc.yaxis.title='egm lepton-jet iso' title.Draw() decorate_axis_pi(sigc.xaxis) hline = Line(sigc.xaxis.GetXmin(), yedge, sigc.xaxis.GetXmax(), yedge) vline = Line(xedge, sigc.yaxis.GetXmin(), xedge, sigc.yaxis.GetXmax()) for l in [hline, vline]: l.linewidth=2 l.color='red' l.Draw() canvas.SaveAs('{}/IsoDphi_{}.png'.format(_outdir, lifetimeKey.name)) canvas.SaveAs('{}/IsoDphi_{}.pdf'.format(_outdir, lifetimeKey.name)) canvas.Clear() ###### optimal boundaries ###### boundsh = sigh.clone() boundsh.Reset() boundsh.Draw('col') decorate_axis_pi(boundsh.xaxis) leg = Legend(len(boundary_edges), margin=0.25, leftmargin=0.05, rightmargin=0.5, topmargin=0.05, entrysep=0.01, entryheight=0.02, textsize=12) get_lxy = lambda t : float(t.split('_')[0].split('-')[-1].replace('p', '.')) for i, k in enumerate(sorted(boundary_edges, key=get_lxy)): bx, by, za = boundary_edges[k] hline = Line(boundsh.xaxis.GetXmin(), by, boundsh.xaxis.GetXmax(), by) vline = Line(bx, boundsh.yaxis.GetXmin(), bx, boundsh.yaxis.GetXmax()) label = '{:20}'.format(k) label+='({:.2f}, {:.2f})'.format(bx, by) label+=' Z_{A}: %.3f'%za leg.AddEntry(hline, label=label, style='L') for l in (hline, vline): l.linewidth=2 l.color=sigCOLORS[i] if i%2==0: l.linestyle='dashed' l.Draw() leg.Draw() if outdir.endswith('ch4mu'): title = TitleAsLatex('[4#mu {}] '.format(massKey.name)+'optimial boundaries') if outdir.endswith('ch2mu2e'): title = TitleAsLatex('[2#mu2e SR {}] '.format(massKey.name)+'optimal boundaries') title.Draw() canvas.SaveAs('{}/boundaries.png'.format(_outdir)) canvas.SaveAs('{}/boundaries.pdf'.format(_outdir)) canvas.Clear()
def routine(chan, direction): hs=[] effs = OrderedDict() chandir = getattr(f, 'ch'+chan) for t in chandir.sig.keys(): sigtag = t.name h = getattr(getattr(chandir.sig, sigtag), 'invm_inc500') # Hist h_total = h.integral(overflow=True) h_ = h.clone() for i in range(1, h.nbins()+1): if direction=='forward': h_sub = h.integral(xbin1=i, overflow=True) elif direction=='backward': h_sub = h.integral(1, xbin2=i) h_[i] = h_sub/h_total h_[i].error = 0 if i==12 and direction=='backward': effs[sigtag] = h_sub/h_total if i==13 and direction=='forward': effs[sigtag] = h_sub/h_total h_.title = sigtag h_.drawstyle = 'PLC hist' h_.legendstyle='L' hs.append(h_) mineff = min(effs.values()) aveeff = sum(effs.values())/len(effs) effs['sig_ave'] = aveeff effs['sig_min'] = mineff for h in getattr(chandir.bkg, 'invm_inc500'): h_total = h.integral(overflow=True) if h_total==0: continue h_ = h.empty_clone() for i in range(1, h.nbins()+1): if direction=='forward': h_sub = h.integral(xbin1=i, overflow=True) elif direction=='backward': h_sub = h.integral(1, xbin2=i) h_[i] = h_sub/h_total h_[i].error = 0 if i==12 and direction=='backward': effs[h.title] = h_sub/h_total if i==13 and direction=='forward': effs[h.title] = h_sub/h_total h_.drawstyle = 'hist' h_.title=h.title h_.color = bkgCOLORS[h.title] h_.linestyle = 'dashed' h_.legendstyle='L' h_.linewidth=2 hs.append(h_) print('>',chan, direction) maxlen = max([len(k) for k in effs]) for k in effs: fmt = '{:%d}:{:.2f}'%(maxlen+2)+'%' print(fmt.format(k, effs[k]*100)) legend = Legend(hs, pad=c, margin=0.1, topmargin=0.02, entryheight=0.02, textsize=12) axes, limits =draw(hs, ylimits=(0,1.5), logy=False, ytitle='({}) cut efficiency'.format(direction),) legend.Draw() title = TitleAsLatex('[{}] lepton-jet pair invM cut efficiency'.format(chan.replace('mu', '#mu'))) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') ROOT.gPad.SetGrid() c.SaveAs('{}/ch{}_invm_{}.pdf'.format(outdir, chan, direction)) c.Clear()
def make_plot(raw_data, binning, title_text, outfn): h = Hist(*binning, drawstyle='hist e1', color=sigCOLORS[0], linewidth=2, title=';Percent difference[%];Events') for sample, value in raw_data.items(): h.Fill(value['variation'], value['count']) hc = asrootpy(h.GetCumulative()) hc.linecolor = 'gold' hc.fillcolor = 'lightyellow' hc.fillstyle = 'solid' hc.scale(h.max(include_error=True) / hc.max()) xmin_, xmax_, ymin_, ymax_ = get_limits([h, hc]) draw([hc, h], ylimits=(0, ymax_)) x95, x99 = None, None cumsum = 0 for i in range(1, h.GetNbinsX() + 1): cumsum += h.GetBinContent(i) if x95 is None and cumsum / h.Integral() > 0.95: x95 = h.GetXaxis().GetBinUpEdge(i) if x99 is None and cumsum / h.Integral() > 0.99: x99 = h.GetXaxis().GetBinUpEdge(i) title = TitleAsLatex(title_text) title.Draw() # print(title_text, ROOT.gPad.GetUymax(), hc.max()) # draw a second axis on the right. ROOT.gPad.SetTicks( 1, 0 ) # Draw top ticks but not right ticks (https://root.cern.ch/root/roottalk/roottalk99/2908.html) low, high = 0, ROOT.gPad.GetUymax() / hc.max() raxis = ROOT.TGaxis(ROOT.gPad.GetUxmax(), ROOT.gPad.GetUymin(), ROOT.gPad.GetUxmax(), ROOT.gPad.GetUymax(), low, high, 510, "+L") raxis.SetLabelSize(0.03) raxis.SetLabelFont(42) raxis.SetLabelColor(convert_color('gold', 'root')) raxis.Draw() frame = canvas.FindObject('TFrame') lo, hi = frame.GetY1(), frame.GetY2() l95 = ROOT.TLine(x95, lo, x95, hi) l95.SetLineStyle(2) l95.SetLineColor(convert_color(sigCOLORS[1], 'root')) l95.SetLineWidth(2) l95.Draw() l99 = ROOT.TLine(x99, lo, x99, hi) l99.SetLineStyle(3) l99.SetLineColor(convert_color(sigCOLORS[2], 'root')) l99.Draw() leg = Legend(3, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) leg.AddEntry(hc, label='cumulative (norm.)', style='LF') leg.AddEntry(l95, label='95% @ {}'.format(x95), style='L') leg.AddEntry(l99, label='99% @ {}'.format(x99), style='L') leg.Draw() canvas.SaveAs(outfn) canvas.clear()
def routine(varname, chan): htitle = None hs = [] chandir = getattr(f, 'ch' + chan) for t in chandir.sig.keys(): sigtag = t.name h = getattr(getattr(chandir.sig, sigtag), varname) # Hist h_total = h.integral(overflow=True) if not htitle: htitle = h.title h_ = h.clone() for i in range(1, h.nbins() + 1): h_[i] = h.integral(xbin1=i, overflow=True) / h_total h_[i].error = 0 h_.title = sigtag h_.drawstyle = 'PLC hist' h_.legendstyle = 'L' hs.append(h_) # chandir.bkg.mind0 # HistStack, hs # total = sum([h.integral(overflow=True) for h in hs]) # total = hs.Integral(start=1, end=h.nbins()+1) binIdbyDy = None binLowEdgebyDy = None for h in getattr(chandir.bkg, varname): h_total = h.integral(overflow=True) if h_total == 0: continue h_ = h.empty_clone() for i in range(1, h.nbins() + 1): h_[i] = h.integral(xbin1=i, overflow=True) / h_total h_[i].error = 0 if h.title.startswith( 'DYJets') and h_[i].value < 0.2 and binIdbyDy is None: binIdbyDy = i binLowEdgebyDy = h_[i].x.low h_.drawstyle = 'hist' h_.title = h.title h_.color = bkgCOLORS[h.title] h_.linestyle = 'dashed' h_.legendstyle = 'L' h_.linewidth = 2 hs.append(h_) ## print signal efficiency if binIdbyDy and chan == '2mu2e': # print('$'*20, varname, chan, binLowEdgebyDy) colname = varname + ' @{}'.format(binLowEdgebyDy) for h in hs: # print('{:30} {:.2f}%'.format(h.title, h[binIdbyDy].value*100)) table_data[h.title][colname] = '{:.2f}%'.format( h[binIdbyDy].value * 100) xmin_, xmax_, ymin_, ymax_ = get_limits(hs, logx=True) legend = Legend(hs, pad=c, margin=0.1, topmargin=0.02, entryheight=0.02, textsize=12) axes, limits = draw(hs, ylimits=(0, 1.8), ytitle='cut efficiency', logx=True, logx_crop_value=0.1) axes[0].SetMoreLogLabels() ROOT.gPad.RedrawAxis() legend.Draw() title = TitleAsLatex('[{}] {}'.format(chan.replace('mu', '#mu'), htitle)) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') c.SaveAs('{}/ch{}_{}.pdf'.format(outdir, chan, varname)) c.Clear()
def draw_shape(a, b, field, category, textsize=22, optional_label_text=None, normalize=True, logy=False): """ Draw a canvas with two Hists normalized to unity Parameters: - a: Nominal Hist (denominator in the ratio) - b: Shifted Hist (numerator in the ratio) - field: variable field (see variables.py) - category: analysis category (see categories/*) """ xtitle = get_xtitle(field) plot = SimplePlot( xtitle=xtitle, ytitle='{0}Events'.format('Normalized ' if normalize else ''), logy=logy) if normalize: a_integral = a.integral() if a_integral != 0: a /= a_integral b_integral = b.integral() if b_integral != 0: b /= b_integral a.title = 'A: ' + a.title b.title = 'B: ' + b.title a.color = 'black' b.color = 'red' a.legendstyle = 'L' b.legendstyle = 'L' a.markersize = 0 b.markersize = 0 a.linewidth = 2 b.linewidth = 2 a.fillstyle = 'hollow' b.fillstyle = 'hollow' a.linestyle = 'solid' b.linestyle = 'dashed' a.drawstyle = 'hist E0' b.drawstyle = 'hist E0' plot.draw('main', [a, b], ypadding=(0.3, 0.)) with plot.pad('main') as pad: # legend # leg = Legend([a, b], 0.2, 0.2, 0.45, # margin=0.35, textsize=textsize) leg = Legend([a, b]) leg.Draw() # draw the category label if category is not None: label = ROOT.TLatex(pad.GetLeftMargin() + 0.04, 0.87, category.label) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) label.Draw() # show p-value and chi^2 pvalue = a.Chi2Test(b, 'WW') pvalue_label = ROOT.TLatex(pad.GetLeftMargin() + 0.04, 0.8, "p-value={0:.2f}".format(pvalue)) pvalue_label.SetNDC(True) pvalue_label.SetTextFont(43) pvalue_label.SetTextSize(textsize) pvalue_label.Draw() chi2 = a.Chi2Test(b, 'WW CHI2/NDF') chi2_label = ROOT.TLatex( pad.GetLeftMargin() + 0.04, 0.72, "#frac{{#chi^{{2}}}}{{ndf}}={0:.2f}".format(chi2)) chi2_label.SetNDC(True) chi2_label.SetTextFont(43) chi2_label.SetTextSize(textsize) chi2_label.Draw() if optional_label_text is not None: optional_label = ROOT.TLatex(pad.GetLeftMargin() + 0.55, 0.87, optional_label_text) optional_label.SetNDC(True) optional_label.SetTextFont(43) optional_label.SetTextSize(textsize) optional_label.Draw() if ATLAS_LABEL.lower() == 'internal': x = 0.67 y = 1 - pad.GetTopMargin() + 0.005 else: x = (1. - pad.GetRightMargin() - 0.03) - len(ATLAS_LABEL) * 0.025 y = 1 - pad.GetTopMargin() + 0.01 ATLAS_label(x, y, sep=0.132, pad=pad, sqrts=None, text=ATLAS_LABEL, textsize=textsize) return plot
h = getattr(massdir, tag).dplxy h.linewidth = 2 h.drawstyle = 'PLC hist' h.scale(1. / h.integral()) h.legendstyle = 'L' h.title = 'lxy: {}cm c#tau: {}mm'.format(lxy, ctau) hsd[float(lxy)] = h hs = [hsd[k] for k in sorted(hsd)] leg = Legend(hs, pad=c, leftmargin=0.5, margin=0.1, entryheight=0.02, textsize=12) draw(hs, pad=c, xlimits=(1e-1, 500), logy=True, logx=True) leg.Draw() t = LuminosityLabel('XX#rightarrow2A#rightarrow4#mu ({}, {}) GeV'.format( mxx, ma)) t.draw() c.SaveAs(os.path.join(outdir, 'ch4mu_dplxy.pdf'.format(outdir))) c.clear() massdir = getattr(f.ch2mu2e, masstag) lxytags = [x.name for x in massdir.keys()] # lxy-0p3_ctau-0p0025 hsd = {} for tag in lxytags: lxy_, ctau_ = tag.split('_') lxy = lxy_.split('-')[1].replace('p', '.') ctau = ctau_.split('-')[1].replace('p', '.')
def main(): args = parse_arguments() channel = args.channel variable = args.variable SetPlotStyle() config = XSectionConfig(13) method = 'TUnfold' files_for_response = [ File(config.unfolding_central, 'read') ] files_for_toys = [ File(config.unfolding_central, 'read') ] print variable tau_value = get_tau_value(config, channel, variable) print tau_value pullHistogram = None for file_for_response in files_for_response: _, _, h_response, _ = get_unfold_histogram_tuple( inputfile=file_for_response, variable=variable, channel=channel, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) if pullHistogram is None: pullHistogram = Hist2D( h_response.GetNbinsY(), 1, h_response.GetNbinsY()+1, 1000, -10, 10 ) pullHistogram.SetDirectory(0) for file_for_toys in files_for_toys: _, _, h_response_for_toys, _ = get_unfold_histogram_tuple( inputfile=file_for_toys, variable=variable, channel=channel, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) for i in range(0,5000): if i % 100 == 0: print 'Toy number :',i toy_response = makeToyResponse( h_response_for_toys.Clone() ) toy_measured = asrootpy(toy_response.ProjectionX('px',1)) toy_truth = asrootpy(h_response_for_toys.ProjectionY()) toy_response_unfolding = makeToyResponse( h_response.Clone() ) toy_response_unfolding.Scale( toy_response.integral(overflow=True) / toy_response_unfolding.integral(overflow=True) ) # Unfold toy data with independent toy response unfolding = Unfolding( toy_measured, toy_truth, toy_measured, toy_response_unfolding, None, method='TUnfold', tau=tau_value) unfolded_results = unfolding.unfold() cov, cor, mc_cov = unfolding.get_covariance_matrix() total_statistical_covariance = cov + mc_cov for i in range(0,total_statistical_covariance.shape[0] ): unfolded_results.SetBinError(i+1, np.sqrt( total_statistical_covariance[i,i] ) ) for bin in range(1,unfolded_results.GetNbinsX() + 1 ): diff = unfolded_results.GetBinContent(bin) - toy_truth.GetBinContent(bin) pull = diff / unfolded_results.GetBinError( bin ) pullHistogram.Fill( bin, pull ) c = Canvas() pullHistogram.Draw('COLZ') plots = r.TObjArray() # for bin in range(1,pullHistogram.GetNbinsX()): # slice = pullHistogram.ProjectionY('slice',bin,bin) # slice.Draw('HIST') # c.Update() # slice.Fit('gaus') # raw_input(bin) pullHistogram.FitSlicesY(0,0,-1,0,'QNR',plots) means = None widths = None for p in plots: if p.GetName()[-2:] == '_1': means = p elif p.GetName()[-2:] == '_2': widths = p means.GetYaxis().SetRangeUser(-2,2) means.SetMarkerColor(2) means.SetLineColor(2) means.GetXaxis().SetTitle(latex_labels.variables_NonLatex[variable]) means.Draw() widths.SetMarkerColor(4) widths.SetLineColor(4) widths.GetXaxis().SetTitle(latex_labels.variables_NonLatex[variable]) widths.Draw('SAME') l = Legend([], leftmargin=0.45, margin=0.3, topmargin=0.7, entryheight=0.7, entrysep = 0.2) l.AddEntry( means, 'Pull mean', 'P') l.AddEntry( widths, 'Pull width', 'P') l.Draw() c.Update() truth_response = asrootpy( h_response.ProjectionY() ) truth_toys = asrootpy( h_response_for_toys.ProjectionY() ) diff_truth = truth_response - truth_toys outputDir = 'plots/unfolding/pulls/new/' outputName = '{dir}/{variable}_{channel}.pdf'.format( dir = outputDir, variable = variable, channel = channel) make_folder_if_not_exists(outputDir) c.SaveAs(outputName)