pdb.set_trace() for variable in variables: cfg_main.var = variable if qcd_from_same_sign: qcd.var = variable # Can put into function but we will not want it by default if we take normalisations from e.g. high MT if w_qcd_mssm_method: wjets.var = variable # Can put into function but we will not want it by default if we take normalisations from e.g. high MT qcd.var = variable wjets_ss.var = variable if variable.name in ['svfit_mass', 'svfit_transverse_mass']: if cut.name in ['inclusive', 'nobtag']: variable.binning = binning_mssm elif cut.name in ['btag']: variable.binning = binning_mssm_btag plot = createHistogram(cfg_main, verbose=False) createDefaultGroups(plot) if not w_qcd_mssm_method: plot.Group('W', ['W', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) # plot.Group('Electroweak', ['Diboson', 'W']) # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style) HistDrawer.draw(plot, plot_dir='plots/' + cut.name) if variable.name in ['mvis', 'svfit_transverse_mass', 'svfit_mass']: plot.WriteDataCard(filename='datacard_{}.root'.format( variable.name), dir='mt_' + cut.name, mode='UPDATE') #mt = mu-tau
if sample.name != 'Data': # Subtract background from data sample.scale = -1. qcd = HistogramCfg(name='QCD', var=None, cfgs=samples_ss, cut=inc_cut, lumi=40.0) samples.append(qcd) # Taken from Variables.py, can get subset with e.g. getVars(['mt', 'mvis']) variables = all_vars cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=inc_cut, lumi=40.0) for cut_name in cuts: cfg_example.cut = cuts[cut_name] if qcd_from_same_sign and 'OS' in cut_name: qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge', 'l1_charge == l2_charge') for variable in variables: cfg_example.var = variable if qcd_from_same_sign: qcd.var = variable # Can put into function but we will not want it by default if we take normalisations from e.g. high MT plot = createHistogram(cfg_example, verbose=True) plot.Group('Diboson', ['ZZ', 'WZ', 'WW']) plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style) HistDrawer.draw(plot, plot_dir='plots/'+cut_name)
# variables = [ # VariableCfg(name='mvis', binning={'nbinsx':35, 'xmin':0, 'xmax':350}, unit='GeV', xtitle='m_{vis}') # ] for cut_name in cuts: if qcd_from_same_sign and not 'SS' in cut_name : cfg_example = HistogramCfg(name='example', var=None, cfgs=samples_qcdfromss, cut=inc_cut, lumi=lumi, weight=total_weight) else: cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=inc_cut, lumi=lumi, weight=total_weight) cfg_example.cut = cuts[cut_name] if qcd_from_same_sign and not 'SS' in cut_name: qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge', 'l1_charge == l2_charge') cfg_example.vars = variables if qcd_from_same_sign: qcd.vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT plots = createHistograms(cfg_example, verbose=True) for variable in variables: plot = plots[variable.name] plot.Group('VV', ['WWTo1L1Nu2Q', 'WZTo1L1Nu2Q', 'WZTo1L3Nu', 'WZTo2L2Q', 'VVTo2L2Nu', 'ZZTo2L2Q', 'ZZTo4L']) plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TBarToLeptons_tch_powheg', 'TToLeptons_tch_powheg']) plot.Group('ZLL', ['ZL', 'ZJ'], style=plot.Hist('ZL').style) plot.Group('W', ['W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) plot.Group('Electroweak', ['W', 'VV', 'Single t']) base_dir = 'plotsG/' if data2016G else 'plots/' HistDrawer.draw(plot, plot_dir=base_dir+cut_name, channel='e#mu')
def plotDataOverMCEff(hist_mc_tight, hist_mc_loose, hist_data_tight, hist_data_loose, plot_name='fakerate.pdf'): g = TGraphAsymmErrors(hist_mc_tight) g.Divide(hist_mc_tight, hist_mc_loose) g.GetYaxis().SetTitle('Fake rate') g.GetXaxis().SetTitle(hist_mc_tight.GetXaxis().GetTitle()) g.GetYaxis().SetTitleOffset(1.2) g.GetYaxis().SetTitleOffset(1.3) g.SetLineColor(2) g.SetMarkerColor(2) g_data = TGraphAsymmErrors(hist_data_tight) g_data.Divide(hist_data_tight, hist_data_loose) g_data.GetYaxis().SetTitle('Fake rate') g_data.GetXaxis().SetTitle(hist_data_tight.GetXaxis().GetTitle()) g_data.GetYaxis().SetTitleOffset(1.2) g_data.GetYaxis().SetTitleOffset(1.3) g_data.SetMarkerColor(1) g_vals = g.GetY() g_data_vals = g_data.GetY() g_ratio = g_data.Clone('ratio') for i in xrange(g_data.GetN()): ratio = g_data_vals[i] / g_vals[i] if g_vals[i] else 0. g_ratio.SetPoint(i, g.GetX()[i], ratio) rel_y_low = math.sqrt((g_data.GetErrorYlow(i) / g_data_vals[i])**2 + ( g.GetErrorYlow(i) / g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0. g_ratio.SetPointEYlow(i, rel_y_low * ratio) rel_y_high = math.sqrt( (g_data.GetErrorYhigh(i) / g_data_vals[i])**2 + (g.GetErrorYhigh(i) / g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0. g_ratio.SetPointEYhigh(i, rel_y_high * ratio) # Gymnastics to get same label sizes etc in ratio and main plot ytp_ratio = 2. xtp_ratio = 2. # hr.GetYaxis().SetNdivisions(4) g_ratio.GetYaxis().SetTitleSize(g.GetYaxis().GetTitleSize() * xtp_ratio) g_ratio.GetXaxis().SetTitleSize(g.GetXaxis().GetTitleSize() * ytp_ratio) g_ratio.GetYaxis().SetTitleOffset(g.GetYaxis().GetTitleOffset() / xtp_ratio) g_ratio.GetXaxis().SetTitleOffset( g.GetXaxis().GetTitleOffset()) # / ytp_ratio) g_ratio.GetYaxis().SetLabelSize(g.GetYaxis().GetLabelSize() * xtp_ratio) g_ratio.GetXaxis().SetLabelSize(g.GetXaxis().GetLabelSize() * ytp_ratio) g_data.GetXaxis().SetLabelColor(0) g_data.GetXaxis().SetLabelSize(0) g.GetXaxis().SetLabelColor(0) g.GetXaxis().SetLabelSize(0) g_ratio.GetXaxis().SetTitle(g.GetXaxis().GetTitle()) # maxy = 1.1 * min(g.GetMaximum(), g_data.GetMaximum(), 0.2) g.GetYaxis().SetRangeUser(0.001, 0.2) cv, pad, padr = HistDrawer.buildCanvas() pad.cd() g.Draw('AP') g_data.Draw('P') legend = TLegend(0.23, 0.73, 0.43, 0.91) legend.SetFillColor(0) legend.SetFillStyle(0) legend.SetLineColor(0) legend.SetLineWidth(0) legend.AddEntry(g.GetName(), 'MC', 'lep') legend.AddEntry(g_data.GetName(), 'Observed', 'lep') legend.Draw() padr.cd() g_ratio.GetYaxis().SetRangeUser(0.51, 1.49) g_ratio.GetYaxis().SetTitle('Obs/MC') g_ratio.Draw('AP') drawRatioLines(g_ratio) cv.Print(plot_name)
def makePlots(variables, cuts, total_weight, all_samples, samples, friend_func, mode='control', dc_postfix='', make_plots=True, optimisation=False): sample_names = set() ams_dict = {} from CMGTools.H2TauTau.proto.plotter.cut import Cut # def_iso_cut = inc_sig_tau1_iso & inc_sig_tau2_iso iso_cuts = { # 'vvtight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>5.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>5.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>3.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>3.5')), 'vtight_relax2nd': (Cut('l1_byIsolationMVArun2v1DBoldDMwLT>4.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>4.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>2.5')), 'loose_not_vtight': (Cut( 'l1_byIsolationMVArun2v1DBoldDMwLT>1.5 && l1_byIsolationMVArun2v1DBoldDMwLT<4.5' ) & Cut( 'l2_byIsolationMVArun2v1DBoldDMwLT>1.5&&l2_byIsolationMVArun2v1DBoldDMwLT<4.5' ), Cut('l1_byIsolationMVArun2v1DBoldDMwLT<1.5 && l1_byIsolationMVArun2v1DBoldDMwLT>0.5' ) & Cut('l2_byIsolationMVArun2v1DBoldDMwLT<1.5 && l2_byIsolationMVArun2v1DBoldDMwLT>0.5' )), 'one_loose_other_vtight': (Cut( '(l1_byIsolationMVArun2v1DBoldDMwLT>4.5 && (l2_byIsolationMVArun2v1DBoldDMwLT>1.5&&l2_byIsolationMVArun2v1DBoldDMwLT<4.5)) || (l2_byIsolationMVArun2v1DBoldDMwLT>4.5 && (l1_byIsolationMVArun2v1DBoldDMwLT>1.5&&l1_byIsolationMVArun2v1DBoldDMwLT<4.5)) ' ), Cut('l1_byIsolationMVArun2v1DBoldDMwLT<1.5 && l1_byIsolationMVArun2v1DBoldDMwLT>0.5' ) & Cut('l2_byIsolationMVArun2v1DBoldDMwLT<1.5 && l2_byIsolationMVArun2v1DBoldDMwLT>0.5' )), # 'vtight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>4.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>2.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>2.5')), # 'tight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>3.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>3.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>3.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>1.5')), # 'medium':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>2.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>2.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>0.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>0.5')), # 'loose':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>1.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>1.5'), Cut('1')), # 'vloose':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>0.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>0.5'), Cut('1')), } # iso_cuts = { # 'l1_vvtight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>5.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>5.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>3.5')), # 'l1_vtight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>4.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>4.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>2.5')), # 'l1_tight':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>3.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>3.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>1.5')), # 'l1_medium':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>2.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>2.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>0.5')), # 'l1_loose':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>1.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'), Cut('l1_byIsolationMVArun2v1DBoldDMwLT>1.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>0.5')), # 'l1_vloose':(Cut('l1_byIsolationMVArun2v1DBoldDMwLT>0.5') & Cut('l2_byIsolationMVArun2v1DBoldDMwLT>4.5'),Cut('l1_byIsolationMVArun2v1DBoldDMwLT>0.5') & Cut('1')), # } for cut in cuts: for iso_cut_name, (iso_cut, max_iso_cut) in iso_cuts.items(): # iso and charge cuts, need to have them explicitly for the QCD estimation # max_iso_cut = Cut('l1_byIsolationMVArun2v1DBoldDMwLT > 2.5 && l2_byIsolationMVArun2v1DBoldDMwLT > 2.5') iso_sideband_cut = (~iso_cut) & max_iso_cut charge_cut = Cut('l1_charge != l2_charge') isSS = 'SS' in cut.name all_samples_qcd = qcd_estimation( cut.cut & iso_sideband_cut & (charge_cut if not isSS else ~charge_cut), # shape sideband cut.cut & iso_cut & (~charge_cut), # norm sideband 1 cut.cut & iso_sideband_cut & (~charge_cut), # norm sideband 2 all_samples if mode in ['mssm'] else samples, int_lumi, total_weight, verbose=verbose, friend_func=friend_func) # now include charge and isolation too the_cut = MyCut( cut.name + iso_cut_name, cut.cut & iso_cut & (charge_cut if not isSS else ~charge_cut)) # for variable in variables: cfg_total = HistogramCfg(name=the_cut.name, vars=variables, cfgs=all_samples_qcd, cut=str(the_cut.cut), lumi=int_lumi, weight=total_weight) # all_samples_qcd[-1].vars = variables if mode == 'mva_train': createTrees(cfg_total, '/data1/steggema/tt/MVATrees', verbose=True) continue plots = createHistograms(cfg_total, verbose=True, friend_func=friend_func) for variable in variables: plot = plots[variable.name] plot.Group('Single t', [ 'T_tWch', 'TBar_tWch', 'TToLeptons_tch_powheg', 'TBarToLeptons_tch_powheg' ]) # 'TToLeptons_sch', plot.Group('VV', [ 'VVTo2L2Nu', 'ZZTo2L2Q', 'WWTo1L1Nu2Q', 'WZTo1L3Nu', 'ZZTo4L', 'WZTo2L2Q', 'WZTo1L1Nu2Q', 'Single t' ]) # 'WZTo3L', plot.Group( 'ZTT', ['ZTT', 'ZTT1Jets', 'ZTT2Jets', 'ZTT3Jets', 'ZTT4Jets']) plot.Group('ZJ', ['ZJ', 'ZJ1Jets', 'ZJ2Jets', 'ZJ3Jets', 'ZJ4Jets']) plot.Group('ZL', ['ZL', 'ZL1Jets', 'ZL2Jets', 'ZL3Jets', 'ZL4Jets']) plot.Group( 'W', ['WJetsToLNu', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) plot.Group('Electroweak', ['W', 'VV', 'Single t', 'ZJ']) if optimisation: plot.DrawStack('HIST') print plot for signal_hist in plot.SignalHists(): sample_names.add(signal_hist.name) ams = ams_hists_rebin(signal_hist.weighted, plot.BGHist().weighted) if variable.name == 'mt_total_mssm' and signal_hist.name == 'ggH1800': print ams_hists_rebin(signal_hist.weighted, plot.BGHist().weighted, debug=True) # import pdb; pdb.set_trace() ams_dict[variable.name + '__' + the_cut.name + '__' + signal_hist.name + '_'] = ams if not make_plots: continue blindxmin = 0.7 if 'mva' in variable.name else None blindxmax = 1.00001 if 'mva' in variable.name else None if variable.name == 'mt2': blindxmin = 60. blindxmax = variable.binning['xmax'] if variable.name == 'mt_sum': blindxmin = 250. blindxmax = variable.binning['xmax'] if variable.name == 'mt_total': blindxmin = 200. blindxmax = variable.binning['xmax'] plot_dir = 'plot_' + the_cut.name HistDrawer.draw(plot, channel='#tau_{h}#tau_{h}', plot_dir=plot_dir, blindxmin=blindxmin, blindxmax=blindxmax) # HistDrawer.drawRatio(plot, channel='#tau_{h}#tau_{h}') plot.UnGroup('Electroweak') #, ['W', 'VV', 'Single t', 'ZJ']) plot.Group('VV', ['VV', 'Single t']) if variable.name in [ 'mt_total', 'svfit_mass', 'mt_total_mssm', 'mt_total_mssm_fine' ]: plot.WriteDataCard( filename=plot_dir + '/htt_tt.inputs-sm-13TeV_{var}{postfix}.root'.format( var=variable.name, postfix=dc_postfix), dir='tt_' + cut.name, mode='UPDATE') # Save AMS dict import pickle pickle.dump(ams_dict, open('opt.pkl', 'wb')) if optimisation: print '\nOptimisation results:' all_vals = ams_dict.items() for sample_name in sample_names: vals = [v for v in all_vals if sample_name + '_' in v[0]] vals.sort(key=itemgetter(1)) for key, item in vals: print item, key print '\nBy variable' for variable in variables: name = variable.name print '\nResults for variable', name for key, item in vals: if key.startswith(name + '__'): print item, key
def makePlots(variables, cuts, total_weight, sample_dict, hist_dict, qcd_from_same_sign, w_qcd_mssm_method, mt_cut, friend_func, dc_postfix, make_plots=True): for cut in cuts: if qcd_from_same_sign and not 'SS' in cut.name and not w_qcd_mssm_method: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['samples_qcdfromss'], cut=cut.cut, lumi=int_lumi, weight=total_weight) elif w_qcd_mssm_method: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['samples_mssm_method'], cut=cut.cut, lumi=int_lumi, weight=total_weight) hist_dict[ 'wjets'].cut = cut.cut # since wjets is a sub-HistogramCfg else: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['all_samples'], cut=cut.cut, lumi=int_lumi, weight=total_weight) if qcd_from_same_sign and not 'SS' in cut.name: hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge', 'l1_charge == l2_charge') if w_qcd_mssm_method: hist_dict['stacknow_highmt_os'].name = 'HighMTOS' + cut.name hist_dict['stacknow_highmt_ss'].name = 'HighMTSS' + cut.name hist_dict['wjets_incl_os'].name = 'WInclOS' + cut.name hist_dict['wjets_incl_ss'].name = 'WInclSS' + cut.name hist_dict['wjets_highmt_os'].name = 'WHighMTOS' + cut.name hist_dict['wjets_ss'].name = 'WJetsSS' + cut.name hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge', 'l1_charge == l2_charge') hist_dict['wjets_ss'].cut = cut.cut.replace( 'l1_charge != l2_charge', 'l1_charge == l2_charge') hist_dict['stacknow_highmt_os'].cut = cut.cut.replace( mt_cut, 'mt>70') + '&& mt>70' hist_dict['stacknow_highmt_ss'].cut = cut.cut.replace( 'l1_charge != l2_charge', 'l1_charge == l2_charge').replace( mt_cut, 'mt>70') + '&& mt>70' hist_dict['wjets_incl_os'].cut = cut.cut.replace(mt_cut, '1.') hist_dict['wjets_incl_ss'].cut = cut.cut.replace( 'l1_charge != l2_charge', 'l1_charge == l2_charge').replace(mt_cut, '1.') hist_dict['wjets_highmt_os'].cut = cut.cut.replace( mt_cut, 'mt>70') + '&& mt>70' plot_w_os = createHistogram(hist_dict['wjets_incl_os'], verbose=False, friend_func=friend_func) plot_w_ss = createHistogram(hist_dict['wjets_incl_ss'], verbose=False, friend_func=friend_func) r_w_os_ss = plot_w_os.GetStack().totalHist.Yield( ) / plot_w_ss.GetStack().totalHist.Yield() print 'Inclusive W OS/SS ratio:', r_w_os_ss plot_highmt_os = createHistogram(hist_dict['stacknow_highmt_os'], all_stack=True, verbose=False, friend_func=friend_func) plot_highmt_ss = createHistogram(hist_dict['stacknow_highmt_ss'], all_stack=True, verbose=False, friend_func=friend_func) createDefaultGroups(plot_highmt_os) createDefaultGroups(plot_highmt_ss) yield_highmt_os = plot_highmt_os.GetStack().totalHist.Yield() yield_highmt_ss = plot_highmt_ss.GetStack().totalHist.Yield() plot_w_highmt_os = createHistogram(hist_dict['wjets_highmt_os'], verbose=False, friend_func=friend_func) yield_w_highmt_os = plot_w_highmt_os.GetStack().totalHist.Yield() if r_w_os_ss < r_qcd_os_ss: print 'WARNING, OS/SS ratio larger for QCD than for W+jets!', r_w_os_ss, r_qcd_os_ss yield_estimation = r_w_os_ss * (yield_highmt_os - r_qcd_os_ss * yield_highmt_ss) / ( r_w_os_ss - r_qcd_os_ss) print 'High MT W+jets estimated yield', yield_estimation print 'High MT W+jets MC yield', yield_w_highmt_os w_sf = 0. if yield_w_highmt_os: w_sf = yield_estimation / yield_w_highmt_os else: print 'Warning: no MC events in high MT W+jets' print 'W+jets scale factor:', w_sf, '\n' if cut.name == 'vbf_highmva0': print 'VBF very tight category, fixing W+jets SF to 1' w_sf = 1. hist_dict['wjets'].total_scale = w_sf hist_dict['wjets_ss'].total_scale = -w_sf cfg_main.vars = variables if qcd_from_same_sign: hist_dict[ 'qcd'].vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT if w_qcd_mssm_method: hist_dict[ 'wjets'].vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT hist_dict['qcd'].vars = variables hist_dict['wjets_ss'].vars = variables for variable in variables: if variable.name in [ 'svfit_mass', 'svfit_transverse_mass', 'mvis' ] and 'mssm' in mode: if cut.name in ['inclusive', 'nobtag']: variable.binning = binning_mssm elif cut.name in ['btag']: variable.binning = binning_mssm_btag plots = createHistograms(cfg_main, verbose=False, friend_func=friend_func) for variable in variables: # for plot in plots.itervalues(): plot = plots[variable.name] createDefaultGroups(plot) if not w_qcd_mssm_method: plot.Group('W', ['W', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) # plot.Group('Electroweak', ['Diboson', 'W']) # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style) if make_plots: HistDrawer.draw(plot, plot_dir='plots/' + cut.name) if variable.name in [ 'mvis', 'svfit_transverse_mass', 'svfit_mass', 'mva', 'mva2div1', 'mva1', 'mva2', 'l2_nc_ratio' ]: plot.WriteDataCard( filename='datacard_{mode}_{var}.root'.format( mode=mode, var=variable.name), dir='mt_' + cut.name, mode='UPDATE', postfix=dc_postfix) #mt = mu-tau
lumi=int_lumi, weight=total_weight) else: cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=inc_cut, lumi=int_lumi, weight=total_weight) cfg_example.cut = cuts[cut_name] if qcd_from_same_sign and 'OS' in cut_name: qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge', 'l1_charge == l2_charge') cfg_example.vars = variables if qcd_from_same_sign: qcd.vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT plots = createHistograms(cfg_example, verbose=True) for variable in variables: plot = plots[variable.name] plot.Group('VV', ['ZZ', 'WZ', 'WW', 'T_tWch', 'TBar_tWch']) plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) plot.Group('ZLL', ['ZL', 'ZJ'], style=plot.Hist('ZL').style) plot.Group('Electroweak', ['W', 'VV']) HistDrawer.draw(plot, plot_dir='plots/' + cut_name, channel='#mu#mu') # plot.WriteDataCard(filename='datacard_mm.root', dir='mm_' + cut_name)
var=None, cfgs=samples_qcdfromss, cut=inc_cut, lumi=int_lumi, weight=total_weight) else: cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=inc_cut, lumi=int_lumi, weight=total_weight) cfg_example.cut = cuts[cut_name] if qcd_from_same_sign and not 'SS' in cut_name: qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge', 'l1_charge == l2_charge') for variable in variables: cfg_example.var = variable if qcd_from_same_sign: qcd.var = variable # Can put into function but we will not want it by default if we take normalisations from e.g. high MT plot = createHistogram(cfg_example, verbose=True) plot.Group('VV', ['ZZ', 'WZ', 'WW', 'T_tWch', 'TBar_tWch']) # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style) HistDrawer.draw(plot, plot_dir='plots/RemoveLowMET_' + cut_name) plot.WriteDataCard(filename='datacard_mvis.root', dir='mt_' + cut_name)
def makePlots(variables, cuts, total_weight, sample_dict, hist_dict, qcd_from_same_sign, w_qcd_mssm_method, mt_cut, friend_func, dc_postfix, make_plots=True, create_trees=False): ams_dict = {} sample_names = set() for cut in cuts: if qcd_from_same_sign and not 'SS' in cut.name and not w_qcd_mssm_method: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['samples_qcdfromss'], cut=cut.cut, lumi=int_lumi, weight=total_weight) elif w_qcd_mssm_method: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['samples_mssm_method'], cut=cut.cut, lumi=int_lumi, weight=total_weight) hist_dict[ 'wjets'].cut = cut.cut # since wjets is a sub-HistogramCfg else: cfg_main = HistogramCfg(name=cut.name, var=None, cfgs=sample_dict['all_samples'], cut=cut.cut, lumi=int_lumi, weight=total_weight) if qcd_from_same_sign and not 'SS' in cut.name: hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge', 'l1_charge == l2_charge') if w_qcd_mssm_method: estimateQCDWMSSM(hist_dict, cut, mt_cut, friend_func=friend_func, r_qcd_os_ss=r_qcd_os_ss) cfg_main.vars = variables if qcd_from_same_sign: hist_dict[ 'qcd'].vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT if w_qcd_mssm_method: hist_dict[ 'wjets'].vars = variables # Can put into function but we will not want it by default if we take normalisations from e.g. high MT hist_dict['qcd'].vars = variables hist_dict['wjets_ss'].vars = variables for variable in variables: if variable.name in [ 'svfit_mass', 'svfit_transverse_mass', 'mvis' ] and 'mssm' in mode: if cut.name in ['inclusive', 'nobtag']: variable.binning = binning_mssm elif cut.name in ['btag']: variable.binning = binning_mssm_btag if create_trees: createTrees(cfg_main, '/data1/steggema/mt/MVATrees', verbose=True) continue plots = createHistograms(cfg_main, verbose=False, friend_func=friend_func) for variable in variables: # for plot in plots.itervalues(): plot = plots[variable.name] createDefaultGroups(plot) if not w_qcd_mssm_method: plot.Group('W', ['W', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) plot.Group('Electroweak', ['VV', 'W']) # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) plot.Group('ZTT', ['ZTT', 'ZJ'], style=plot.Hist('ZTT').style) if make_plots: HistDrawer.draw(plot, plot_dir='plots/' + cut.name) if variable.name in [ 'mvis', 'svfit_transverse_mass', 'svfit_mass', 'mva', 'mva2div1', 'mva1', 'mva2', 'l2_nc_ratio' ]: plot.WriteDataCard( filename='datacard_{mode}_{var}.root'.format( mode=mode, var=variable.name), dir='mt_' + cut.name, mode='UPDATE', postfix=dc_postfix) #mt = mu-tau for signal_hist in plot.SignalHists(): sample_names.add(signal_hist.name) ams_dict[variable.name + '__' + cut.name + '__' + signal_hist.name + '_'] = ams_hists( signal_hist.weighted, plot.BGHist().weighted) print '\nOptimisation results:' all_vals = ams_dict.items() for sample_name in sample_names: vals = [v for v in all_vals if sample_name + '_' in v[0]] vals.sort(key=itemgetter(1)) for key, item in vals: print item, key print '\nBy variable' for variable in variables: name = variable.name print '\nResults for variable', name for key, item in vals: if key.startswith(name + '__'): print item, key
for cut in cuts: cfg_total = HistogramCfg(name=cut.name, vars=variables, cfgs=all_samples, cut=str(cut.cut), lumi=int_lumi, weight=total_weight) plots = createHistograms( cfg_total, verbose=True, ) for variable in variables: if variable.name == 'cand_refit_tau_mass' or variable.name == 'cand_tau_mass': blindxmin = 1.777 - 0.040 blindxmax = 1.777 + 0.040 else: blindxmin = None blindxmax = None plot = plots[variable.name] # override HTT default style plot.histPref = histPref plot._ApplyPrefs() HistDrawer.draw(plot, channel='', plot_dir='plot_%s' % cut.name, blindxmin=blindxmin, blindxmax=blindxmax)
SampleCfg(name='WJetsToLNu', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WJetsToLNu.xSection, sumweights=WJetsToLNu.nGenEvents), SampleCfg(name='TTJets', dir_name='TTLep_pow', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TT_pow.xSection, sumweights=TT_pow.nGenEvents), SampleCfg(name='T_tWch', dir_name='T_tWch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=T_tWch.xSection, sumweights=T_tWch.nGenEvents), SampleCfg(name='TBar_tWch', dir_name='TBar_tWch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TBar_tWch.xSection, sumweights=TBar_tWch.nGenEvents), SampleCfg(name='TToLeptons_tch', dir_name='TToLeptons_tch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TToLeptons_tch.xSection, sumweights=TBar_tWch.nGenEvents), SampleCfg(name='TToLeptons_sch', dir_name='TToLeptons_sch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TToLeptons_sch.xSection, sumweights=TToLeptons_sch.nGenEvents), SampleCfg(name='ZZ', dir_name='ZZp8', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=ZZp8.xSection, sumweights=ZZp8.nGenEvents), SampleCfg(name='WZ', dir_name='WZp8', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WZp8.xSection, sumweights=WZp8.nGenEvents), SampleCfg(name='WW', dir_name='WWTo2L2Nu', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WWTo2L2Nu.xSection, sumweights=WWTo2L2Nu.nGenEvents), SampleCfg(name='QCD', dir_name='QCD_Mu15', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=QCD_Mu15.xSection), SampleCfg(name='Data', dir_name='SingleMuon_Run2015B', ana_dir=data_dir, tree_prod_name=tree_prod_name, is_data=True), ] # -> Can add cross sections for samples either explicitly, or from file, or from cfg for sample in samples: setSumWeights(sample) # Taken from Variables.py, can get subset with e.g. getVars(['mt', 'mvis']) variables = all_vars cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=example_cut, lumi=40.0) for variable in variables: cfg_example.var = variable plot = createHistogram(cfg_example) plot.Group('Diboson', ['ZZ', 'WZ', 'WW']) plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) HistDrawer.draw(plot) # HistDrawer.drawRatio(plot)
plot = createHistogram(cfg_total) plot.Group('VV', [ 'ZZTo2L2Q', 'WWTo1L1Nu2Q', 'WZTo1L3Nu', 'ZZTo4L', 'WZTo3L', 'WZTo2L2Q', 'WZTo1L1Nu2Q', 'T_tWch', 'TBar_tWch', 'TToLeptons_tch_powheg', 'TBarToLeptons_tch_powheg' ]) plot.Group('ZTT', ['ZTT', 'ZTT1Jets', 'ZTT2Jets', 'ZTT3Jets', 'ZTT4Jets']) plot.Group('ZJ', ['ZJ', 'ZJ1Jets', 'ZJ2Jets', 'ZJ3Jets', 'ZJ4Jets']) plot.Group('ZL', ['ZL', 'ZL1Jets', 'ZL2Jets', 'ZL3Jets', 'ZL4Jets']) plot.Group('WJets', ['WJetsToLNu', 'W1JetsToLNu', 'W4JetsToLNu']) plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch']) HistDrawer.draw(plot, channel='#tau_{h}#tau_{h}', plot_dir='plot_%s' % cut.name) # HistDrawer.drawRatio(plot, channel='#tau_{h}#tau_{h}') if variable.name == 'mvis': plot.WriteDataCard(filename='plot_%s/htt_tt.inputs-sm-13TeV.root' % cut.name, dir='tt_' + cut.name, mode='UPDATE') if variable.name == 'svfit_mass': plot.WriteDataCard( filename='plot_%s/htt_tt.inputs-sm-13TeV_svFit.root' % cut.name, dir='tt_' + cut.name, mode='UPDATE')
def plotDataOverMCEff(hist_mc_tight, hist_mc_loose, hist_data_tight, hist_data_loose, plot_name='fakerate.pdf', mc_leg='MC', obs_leg='Observed', ratio_leg='Obs/MC'): g = TGraphAsymmErrors(hist_mc_tight) g.Divide(hist_mc_tight, hist_mc_loose) g.GetYaxis().SetTitle('Misidentification rate') g.GetXaxis().SetTitle(hist_mc_tight.GetXaxis().GetTitle()) g.GetYaxis().SetTitleOffset(1.2) g.GetYaxis().SetTitleOffset(1.3) g.SetLineColor(2) g.SetMarkerColor(2) g_data = TGraphAsymmErrors(hist_data_tight) g_data.Divide(hist_data_tight, hist_data_loose) # if g_data.GetN() != hist_data_tight.GetNbinsX(): # import pdb; pdb.set_trace() g_data.GetYaxis().SetTitle('Misidentification rate') g_data.GetXaxis().SetTitle(hist_data_tight.GetXaxis().GetTitle()) g_data.GetYaxis().SetTitleOffset(1.2) g_data.GetYaxis().SetTitleOffset(1.3) g_data.SetMarkerColor(1) g_vals = g.GetY() g_data_vals = g_data.GetY() g_ratio = g_data.Clone('ratio') for i in xrange(g_data.GetN()): ratio = g_data_vals[i]/g_vals[i] if g_vals[i] else 0. g_ratio.SetPoint(i, g.GetX()[i], ratio) rel_y_low = math.sqrt((g_data.GetErrorYlow(i)/g_data_vals[i])**2 + (g.GetErrorYlow(i)/g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0. g_ratio.SetPointEYlow(i, rel_y_low * ratio) rel_y_high = math.sqrt((g_data.GetErrorYhigh(i)/g_data_vals[i])**2 + (g.GetErrorYhigh(i)/g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0. g_ratio.SetPointEYhigh(i, rel_y_high * ratio) # Gymnastics to get same label sizes etc in ratio and main plot ytp_ratio = 2. xtp_ratio = 2. # hr.GetYaxis().SetNdivisions(4) g_ratio.GetYaxis().SetTitleSize(g.GetYaxis().GetTitleSize() * xtp_ratio) g_ratio.GetXaxis().SetTitleSize(g.GetXaxis().GetTitleSize() * ytp_ratio) g_ratio.GetYaxis().SetTitleOffset(g.GetYaxis().GetTitleOffset() / xtp_ratio) g_ratio.GetXaxis().SetTitleOffset(g.GetXaxis().GetTitleOffset()) # / ytp_ratio) g_ratio.GetYaxis().SetLabelSize(g.GetYaxis().GetLabelSize() * xtp_ratio) g_ratio.GetXaxis().SetLabelSize(g.GetXaxis().GetLabelSize() * ytp_ratio) g_data.GetXaxis().SetLabelColor(0) g_data.GetXaxis().SetLabelSize(0) g.GetXaxis().SetLabelColor(0) g.GetXaxis().SetLabelSize(0) g_ratio.GetXaxis().SetTitle(g.GetXaxis().GetTitle()) maxy = 1.3 * max(g.GetMaximum(), g_data.GetMaximum(), 0.05) g.GetYaxis().SetRangeUser(0.0011, maxy) cv, pad, padr = HistDrawer.buildCanvas() pad.cd() g.Draw('AP') g_data.Draw('P') legend = TLegend(0.23, 0.73, 0.43, 0.91) legend.SetFillColor(0) legend.SetFillStyle(0) legend.SetLineColor(0) legend.SetLineWidth(0) legend.AddEntry(g.GetName(), mc_leg, 'lep') legend.AddEntry(g_data.GetName(), obs_leg, 'lep') legend.Draw() padr.cd() g_ratio.GetYaxis().SetRangeUser(0.01, 1.99) g_ratio.GetYaxis().SetTitle(ratio_leg) g_ratio.Draw('AP') drawRatioLines(g_ratio) cv.Print(plot_name) g.GetYaxis().SetRangeUser(0.0001, 1) pad.SetLogy(True) cv.Print(plot_name.replace('.', '_log.')) f = ROOT.TFile(plot_name.replace('.', '_log.').replace('.pdf', '.root'), 'RECREATE') g.Write() g_data.Write() cv.Write() f.Close()
def plotMCEffs(input_list, plot_name='fakerate.pdf'): '''input_list expects tuples with structure: (hist tight, hist loose, legend line, colour) ''' graphs = [] for (hist_tight, hist_loose, legend, colour, style) in input_list: g = TGraphAsymmErrors(hist_tight) g.Divide(hist_tight, hist_loose) g.GetYaxis().SetTitle('Misidentification rate') g.GetXaxis().SetTitle(hist_tight.GetXaxis().GetTitle()) g.GetYaxis().SetTitleOffset(1.2) g.GetYaxis().SetTitleOffset(1.3) g.SetLineColor(colour) g.SetLineStyle(style) g.SetMarkerColor(colour) g.SetMarkerStyle(19+style) g.legend = legend graphs.append(g) ratio_graphs = [] base_graph = graphs[0] for graph in graphs[1:]: g_vals = base_graph.GetY() g_data_vals = graph.GetY() g_ratio = graph.Clone('ratio') for i in xrange(graph.GetN()): ratio = g_data_vals[i]/g_vals[i] if g_vals[i] else 0. g_ratio.SetPoint(i, base_graph.GetX()[i], ratio) rel_y_low = math.sqrt((graph.GetErrorYlow(i)/g_data_vals[i])**2 + (base_graph.GetErrorYlow(i)/g_vals[i])**2) if g_data_vals[i] > 0.0000001 and g_vals[i] > 0.0000001 else 0. g_ratio.SetPointEYlow(i, rel_y_low * ratio) rel_y_high = math.sqrt((graph.GetErrorYhigh(i)/g_data_vals[i])**2 + (base_graph.GetErrorYhigh(i)/g_vals[i])**2) if g_data_vals[i] > 0.0000001 and g_vals[i] > 0.0000001 else 0. g_ratio.SetPointEYhigh(i, rel_y_high * ratio) ratio_graphs.append(g_ratio) # Gymnastics to get same label sizes etc in ratio and main plot ytp_ratio = 2. xtp_ratio = 2. # hr.GetYaxis().SetNdivisions(4) for g_ratio in ratio_graphs: g_ratio.GetYaxis().SetTitleSize(g.GetYaxis().GetTitleSize() * xtp_ratio) g_ratio.GetXaxis().SetTitleSize(g.GetXaxis().GetTitleSize() * ytp_ratio) g_ratio.GetYaxis().SetTitleOffset(g.GetYaxis().GetTitleOffset() / xtp_ratio) g_ratio.GetXaxis().SetTitleOffset(g.GetXaxis().GetTitleOffset()) # / ytp_ratio) g_ratio.GetYaxis().SetLabelSize(g.GetYaxis().GetLabelSize() * xtp_ratio) g_ratio.GetXaxis().SetLabelSize(g.GetXaxis().GetLabelSize() * ytp_ratio) g_ratio.GetXaxis().SetTitle(base_graph.GetXaxis().GetTitle()) for graph in graphs: graph.GetXaxis().SetLabelColor(0) graph.GetXaxis().SetLabelSize(0) maxy = 1.3 * max([gr.GetMaximum() for gr in graphs] + [1./1.25]) for g in graphs: g.GetYaxis().SetRangeUser(0.0011, maxy) cv, pad, padr = HistDrawer.buildCanvas() pad.cd() base_graph.Draw('AP') for graph in graphs[1:]: graph.Draw('P') legend = TLegend(0.63, 0.63, 0.93, 0.91) legend.SetFillColor(0) legend.SetFillStyle(0) legend.SetLineColor(0) legend.SetLineWidth(0) legend2 = TLegend(0.78, 0.63, 0.93, 0.91) legend2.SetFillColor(0) legend2.SetFillStyle(0) legend2.SetLineColor(0) legend2.SetLineWidth(0) for graph in graphs: if 'opp' in graph.legend: legend2.AddEntry(graph.GetName(), graph.legend, 'lep') else: legend.AddEntry(graph.GetName(), graph.legend, 'lep') legend.Draw() legend2.Draw() padr.cd() for g_ratio in ratio_graphs: g_ratio.GetYaxis().SetRangeUser(0.01, 1.99) g_ratio.GetYaxis().SetTitle('Ratio to '+base_graph.legend) g_ratio.Draw('AP' if g_ratio == ratio_graphs[0] else 'P') drawRatioLines(g_ratio) cv.Print(plot_name) g.GetYaxis().SetRangeUser(0.0001, 1) pad.SetLogy(True) cv.Print(plot_name.replace('.', '_log.')) f = ROOT.TFile(plot_name.replace('.', '_log.').replace('.pdf', '.root'), 'RECREATE') g.Write() # g_data.Write() cv.Write() f.Close()
plot.Group('W', ['WJetsToLNu', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets']) plot.Group('Electroweak', ['W', 'VV', 'Single t', 'ZJ']) if optimisation: plot.DrawStack('HIST') print plot for signal_hist in plot.SignalHists(): sample_names.add(signal_hist.name) ams_dict[variable.name + '__' + cut.name + '__' + signal_hist.name + '_'] = ams_hists( signal_hist.weighted, plot.BGHist().weighted) continue HistDrawer.draw(plot, channel='#tau_{h}#tau_{h}', plot_dir='plot_%s' % cut.name) #, blindxmin=30., blindxmax=50.1) # HistDrawer.drawRatio(plot, channel='#tau_{h}#tau_{h}') # if variable.name == 'mvis': # plot.WriteDataCard(filename='plot_%s/htt_tt.inputs-sm-13TeV.root' %cut.name, dir='tt_' + cut.name, mode='UPDATE') if variable.name == 'svfit_mass': plot.WriteDataCard( filename='plot_%s/htt_tt.inputs-sm-13TeV_svFit.root' % cut.name, dir='tt_' + cut.name, mode='UPDATE') if optimisation: print '\nOptimisation results:' all_vals = ams_dict.items()
['ZTT', 'ZTT1Jets', 'ZTT2Jets', 'ZTT3Jets', 'ZTT4Jets']) plot.Group('ZJ', ['ZJ', 'ZJ1Jets', 'ZJ2Jets', 'ZJ3Jets', 'ZJ4Jets']) plot.Group('ZL', ['ZL', 'ZL1Jets', 'ZL2Jets', 'ZL3Jets', 'ZL4Jets']) plot.Group('W', [ 'W', 'WJetsToLNu_HT100to200', 'WJetsToLNu_HT200to400', 'WJetsToLNu_HT400to600', 'WJetsToLNu_HT600toInf' ]) # out_dir = 'fakeplots/'+cut_name if plot is plot_tight else 'fakeplots/loose'+cut_name # HistDrawer.draw(plot, plot_dir='fakeplots/'+cut_name) HistDrawer.draw(plot_stack_tight, plot_dir='fakeplots/stack_' + cut_name + signal_cut_name + '_tight') HistDrawer.draw(plot_stack_loose, plot_dir='fakeplots/stack_' + cut_name + signal_cut_name + '_loose') if only_stack: continue print '#### MC only tight' print plot_tight print '#### MC only loose' print plot_loose print '#### Data-MC only tight' print plot_data_tight print '#### Data-MC only loose'