def get_systematics(variable,channel,com,method): input_file = 'data/normalisation/background_subtraction/13TeV/{var}/VisiblePS/central/xsection_normalised_{channel}_{method}_summary_relative.txt'.format( var = variable, channel = channel, method = method, ) systematic_uncertainties = file_to_df(input_file)['systematic'] sys_up = [] sys_down = [] for i in range(0, len(systematic_uncertainties) ): sys_up.append( 1 + systematic_uncertainties[i] ) sys_down.append( 1 - systematic_uncertainties[i] ) return values_and_errors_to_hist( sys_up, [], bin_edges_vis[variable] ), values_and_errors_to_hist( sys_down, [], bin_edges_vis[variable] )
def plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder, subcategories=[], subname='', plot_largest=False, plot_foreground=None): ''' Plot the systematic uncertainties ''' if not subcategories: subcategories = systematic_uncertainties.keys() x_limits = [bin_edges[0], bin_edges[-1]] # y_limits = [-0.6,0.6] y_limits = [0, 0.4] fig_syst = plt.figure(figsize=(20, 16), dpi=400, facecolor='white') ax_syst = fig_syst.add_subplot(1, 1, 1) ax_syst.minorticks_on() ax_syst.xaxis.labelpad = 12 ax_syst.yaxis.labelpad = 12 error_hists_up = {} error_hists_down = {} stat_hist = None for syst, vals in systematic_uncertainties.iteritems(): if syst == 'central': n = len(systematic_uncertainties[syst]) continue elif syst == 'statistical': stat_hist_up = values_and_errors_to_hist(vals, [], bin_edges) stat_hist_down = values_and_errors_to_hist(-vals, [], bin_edges) elif syst == 'systematic': syst_hist_up = values_and_errors_to_hist(vals, [], bin_edges) syst_hist_down = values_and_errors_to_hist(-vals, [], bin_edges) elif syst in subcategories: error_hists_up[syst] = values_and_errors_to_hist( vals, [], bin_edges) error_hists_down[syst] = values_and_errors_to_hist( -vals, [], bin_edges) else: continue if plot_largest: largest_syst = [] for bin_i in range(n): high = [] for syst, vals in systematic_uncertainties.iteritems(): if syst == 'central': continue if syst == 'statistical': continue if syst == 'systematic': continue high.append([syst, vals[bin_i]]) high = sorted(high, key=itemgetter(1), reverse=True) # Retrieve highest systematics if high[0][0] not in largest_syst: largest_syst.append(high[0][0]) elif high[1][0] not in largest_syst: largest_syst.append(high[1][0]) else: continue rplt.fill_between(syst_hist_up, syst_hist_down, color='gold', label='Syst.') rplt.fill_between(stat_hist_down, stat_hist_up, color='0.75', label='Stat.') plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) colours = [ 'red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 'darkgoldenrod', 'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink', 'deepskyblue' ] * 2 if len(colours) < len(error_hists_up.keys()): print '---> Need to add more colours!!!' for error_hists in [error_hists_up, error_hists_down]: for i, source, in enumerate(error_hists.keys()): hist = error_hists[source] hist.linewidth = 4 hist.color = colours[i] if plot_largest: if source not in largest_syst: hist.linestyle = 'dashed' hist.alpha = 0.4 hist.linewidth = 2 # Only label systematic once if plot_foreground and plot_foreground in error_hists.keys(): source = '' if error_hists == error_hists_up: rplt.hist(hist, stacked=False, label=source) else: rplt.hist(hist, stacked=False, label='') if plot_foreground and plot_foreground in error_hists.keys(): hist = error_hists[plot_foreground] hist.color = 'black' rplt.hist(hist, stacked=False, label=source) leg = plt.legend(loc='upper right', prop={'size': 25}, ncol=3) # leg = plt.legend(loc='upper right',prop={'size':20},ncol=4) leg.draw_frame(False) x_title = variables_NonLatex[variable] if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']: x_title += ' [GeV]' ax_syst.set_xlim(x_limits) ax_syst.set_ylim(y_limits) plt.xlabel(x_title, CMS.x_axis_title) plt.ylabel('Relative Uncertainty', CMS.y_axis_title) template = '%.1f fb$^{-1}$ (%d TeV)' label = template % (measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy) plt.title(label, loc='right', **CMS.title) logo_location = (0.05, 0.98) prelim_location = (0.05, 0.92) channel_location = (0.05, 0.86) # plt.text(logo_location[0], logo_location[1], # "CMS", # transform=ax_syst.transAxes, # fontsize=42, # verticalalignment='top', # horizontalalignment='left' # ) # # preliminary # plt.text(prelim_location[0], prelim_location[1], # r"\emph{Preliminary}", # transform=ax_syst.transAxes, # fontsize=42, # verticalalignment='top', # horizontalalignment='left' # ) # # channel text # plt.text(channel_location[0], channel_location[1], # r"\emph{%s}" % channel, # transform=ax_syst.transAxes, # fontsize=40, # verticalalignment='top', # horizontalalignment='left' # ) plt.tight_layout() file_template = output_folder + '{var}_systematics_{com}TeV'.format( var=variable, com=measurement_config.centre_of_mass_energy, ) if subname: file_template = file_template + '_' + subname file_template += '.pdf' fig_syst.savefig(file_template) print "Written plots to {f}".format(f=file_template) # plt.show() return
def plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder, subcategories = [], subname = '', plot_largest = False): ''' Plot the systematic uncertainties ''' print subcategories if not subcategories: subcategories = systematic_uncertainties.keys() x_limits = [bin_edges[0], bin_edges[-1]] # y_limits = [-0.6,0.6] y_limits = [0,0.4] fig_syst = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' ) ax_syst = fig_syst.add_subplot(1, 1, 1) ax_syst.minorticks_on() ax_syst.xaxis.labelpad = 12 ax_syst.yaxis.labelpad = 12 error_hists_up = {} error_hists_down = {} stat_hist = None for syst, vals in systematic_uncertainties.iteritems(): if syst == 'central': n = len(systematic_uncertainties[syst]) continue elif syst == 'statistical': stat_hist_up = values_and_errors_to_hist( vals, [], bin_edges ) stat_hist_down = values_and_errors_to_hist( -vals, [], bin_edges ) elif syst == 'systematic': syst_hist_up = values_and_errors_to_hist( vals, [], bin_edges ) syst_hist_down = values_and_errors_to_hist( -vals, [], bin_edges ) elif syst in subcategories: error_hists_up[syst] = values_and_errors_to_hist( vals, [], bin_edges ) error_hists_down[syst] = values_and_errors_to_hist( -vals, [], bin_edges ) else: continue if plot_largest: largest_syst = [] for bin_i in range( n ): high = [] for syst, vals in systematic_uncertainties.iteritems(): if syst == 'central': continue if syst == 'statistical': continue if syst == 'systematic': continue high.append([syst,vals[bin_i]]) high = sorted(high, key = itemgetter(1), reverse=True) # Retrieve highest systematics if high[0][0] not in largest_syst: largest_syst.append(high[0][0]) elif high[1][0] not in largest_syst: largest_syst.append(high[1][0]) else: continue rplt.fill_between( syst_hist_up, syst_hist_down, color = 'yellow', label='Syst.' ) rplt.fill_between( stat_hist_down, stat_hist_up, color = 'grey', label='Stat.' ) plt.tick_params( **CMS.axis_label_major ) plt.tick_params( **CMS.axis_label_minor ) colours = ['red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink', 'deepskyblue' ] # if len(colours) < len(error_hists.keys()): # print '---> Need to add more colours!!!' for error_hists in [error_hists_up, error_hists_down]: for i, source, in enumerate(error_hists.keys()): hist = error_hists[source] hist.linewidth = 4 hist.color = colours[i] if plot_largest: if source not in largest_syst: hist.linestyle = 'dashed' hist.alpha = 0.4 hist.linewidth = 2 # Only label systematic once if error_hists == error_hists_up: rplt.hist( hist, stacked=False, label = source ) else: rplt.hist( hist, stacked=False, label = '' ) leg = plt.legend(loc='upper right',prop={'size':25},ncol=3) # leg = plt.legend(loc='upper right',prop={'size':20},ncol=4) leg.draw_frame(False) x_title = variables_NonLatex[variable] if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']: x_title += ' [GeV]' ax_syst.set_xlim( x_limits ) ax_syst.set_ylim( y_limits ) plt.xlabel( x_title, CMS.x_axis_title ) plt.ylabel( 'Relative Uncertainty', CMS.y_axis_title) template = '%.1f fb$^{-1}$ (%d TeV)' label = template % ( measurement_config.new_luminosity/1000, measurement_config.centre_of_mass_energy) plt.title( label,loc='right', **CMS.title ) logo_location = (0.05, 0.98) prelim_location = (0.05, 0.92) channel_location = ( 0.05, 0.86) # plt.text(logo_location[0], logo_location[1], # "CMS", # transform=ax_syst.transAxes, # fontsize=42, # verticalalignment='top', # horizontalalignment='left' # ) # # preliminary # plt.text(prelim_location[0], prelim_location[1], # r"\emph{Preliminary}", # transform=ax_syst.transAxes, # fontsize=42, # verticalalignment='top', # horizontalalignment='left' # ) # # channel text # plt.text(channel_location[0], channel_location[1], # r"\emph{%s}" % channel, # transform=ax_syst.transAxes, # fontsize=40, # verticalalignment='top', # horizontalalignment='left' # ) plt.tight_layout() file_template = output_folder + '{var}_systematics_{com}TeV'.format( var = variable, com = measurement_config.centre_of_mass_energy, ) if subname: file_template = file_template + '_' + subname file_template += '.pdf' fig_syst.savefig(file_template) print "Written plots to {f}".format(f = file_template) # plt.show() return
def plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder): ''' Plot the systematic uncertainties ''' x_limits = [bin_edges[0], bin_edges[-1]] y_limits = [0,0.6] fig_syst = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' ) ax_syst = fig_syst.add_subplot(1, 1, 1) ax_syst.minorticks_on() ax_syst.xaxis.labelpad = 12 ax_syst.yaxis.labelpad = 12 error_hists = {} stat_hist = None for syst, vals in systematic_uncertainties.iteritems(): if syst == 'statistical': stat_hist = values_and_errors_to_hist( vals, [], bin_edges ) elif syst == 'systematic': full_syst_hist = values_and_errors_to_hist( vals, [], bin_edges ) elif syst == 'central': central_hist = values_and_errors_to_hist( vals, [], bin_edges ) else: error_hists[syst] = values_and_errors_to_hist( vals, [], bin_edges ) plt.tick_params( **CMS.axis_label_major ) plt.tick_params( **CMS.axis_label_minor ) colours = ['red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink' ] for source, colour in zip (error_hists.keys(), colours): hist = error_hists[source] hist.linewidth = 4 hist.color = colour rplt.hist( hist, stacked=False, axes = ax_syst, label = source ) stat_hist.linewidth = 4 stat_hist.color = 'black' stat_hist.linestyle = 'dashed' rplt.hist( stat_hist, stacked=False, axes = ax_syst, label = 'stat.' ) full_syst_hist.linewidth = 4 full_syst_hist.color = 'black' rplt.hist( full_syst_hist, stacked=False, axes = ax_syst, label = 'tot syst.' ) leg = plt.legend(loc=1,prop={'size':30},ncol=2) leg.draw_frame(False) x_title = variables_NonLatex[variable] if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']: x_title += ' [GeV]' ax_syst.set_xlim( x_limits ) ax_syst.set_ylim( y_limits ) plt.xlabel( x_title, CMS.x_axis_title ) plt.ylabel( 'Relative Uncertainty', CMS.y_axis_title) plt.tight_layout() file_template = output_folder + '{var}_systematics_{com}TeV.pdf'.format( var = variable, com = measurement_config.centre_of_mass_energy, ) fig_syst.savefig(file_template) print "Written plots to {f}".format(f = file_template) return
def make_error_plot(errorHists, bins): global output_folder, variable # For each up/down source, reduce to one set of numbers symmetricErrorHists = {} for source, hist in errorHists.iteritems(): if ( (variable == "HT" or variable == "NJets" or variable == "lepton_pt" or variable == "abs_lepton_eta") and source in measurement_config.met_systematics and not "JES" in source and not "JER" in source ): continue if "down" in source or "-" in source or "lower" in source or "Down" in source: # Find up version upHist = None newSource = "" if "down" in source: upHist = errorHists[source.replace("down", "up")] newSource = source.replace("down", "") elif "Down" in source: upHist = errorHists[source.replace("Down", "Up")] newSource = source.replace("Down", "") elif "-" in source: upHist = errorHists[source.replace("-", "+")] newSource = source.replace("-", "") elif "lower" in source: upHist = errorHists[source.replace("lower", "upper")] newSource = source.replace("lower", "") if newSource[-1] == "_": newSource = newSource[:-1] # if '_' in newSource: # newSource = newSource.replace('_','') symmetricErrorHists[newSource] = [] for errorup, errordown in zip(hist, upHist): newError = max(abs(errorup), abs(errordown)) symmetricErrorHists[newSource].append(newError) elif "TTJets_hadronisation" in source or "QCD_shape" in source or "TTJets_NLOgenerator" in source: symmetricErrorHists[source] = [abs(i) for i in hist] x_limits = [bins[0], bins[-1]] y_limits = [0, 0.6] plt.figure(figsize=(20, 16), dpi=200, facecolor="white") ax0 = plt.axes() ax0.minorticks_on() ax0.xaxis.labelpad = 12 ax0.yaxis.labelpad = 12 ax0.set_xlim(x_limits) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) statisticalErrorHists = values_and_errors_to_hist(errorHists["statistical"], [], bins) for source, hist in symmetricErrorHists.iteritems(): symmetricErrorHists[source] = values_and_errors_to_hist(hist, [], bins) colours = [ "silver", "r", "tan", "chartreuse", "cadetblue", "dodgerblue", "pink", "hotpink", "coral", "forestgreen", "cyan", "teal", "crimson", "darkmagenta", "olive", "slateblue", "deepskyblue", "orange", "r", ] for source, colour in zip(symmetricErrorHists.keys(), colours): hist = symmetricErrorHists[source] hist.linewidth = 4 hist.color = colour rplt.hist(hist, stacked=False, axes=ax0, cmap=my_cmap, vmin=1, label=source) statisticalErrorHists.linewidth = 4 statisticalErrorHists.color = "black" statisticalErrorHists.linestyle = "dashed" rplt.hist(statisticalErrorHists, stacked=False, axes=ax0, cmap=my_cmap, vmin=1, label="stat.") ax0.set_ylim(y_limits) leg = plt.legend(loc=1, prop={"size": 40}, ncol=2) leg.draw_frame(False) x_title = variables_NonLatex[variable] if variable in ["HT", "MET", "WPT", "ST", "lepton_pt"]: x_title += " [GeV]" plt.xlabel(x_title, CMS.x_axis_title) plt.ylabel("Relative Uncertainty", CMS.y_axis_title) plt.tight_layout() path = output_folder + "/" + variable + "/" make_folder_if_not_exists(path) file_template = path + "/%s_systematics_%dTeV_%s.pdf" % ( variable, measurement_config.centre_of_mass_energy, channel, ) plt.savefig(file_template) pass