# variables = getVars(['l1_reliso05', 'l2_reliso05']) # variables = [ # VariableCfg(name='mvis', binning={'nbinsx':35, 'xmin':0, 'xmax':350}, unit='GeV', xtitle='m_{vis}') # ] for cut_name in cuts: cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut='', lumi=int_lumi, weight=total_weight) cfg_example.cut = cuts[cut_name] cfg_example.vars = variables channel = "l#nujj" if cut_name.find("_mu") >= 0: channel = "#mu#nujj" elif cut_name.find("_e") >= 0: channel = "e#nujj" if cut_name.find("HP") >= 0: channel += " HP" elif cut_name.find("LP") >= 0: channel += " LP" plots = createHistograms(cfg_example, verbose=False) for variable in variables: plot = plots[variable.name] plot.Group('Diboson', ['WWTo1L1Nu2Q', 'WWTo1L1Nu2Q', 'WZTo1L1Nu2Q'])
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
# 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 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