|
| 1 | +# from __future__ import division # the result of the division will be always a float |
| 2 | +from argparse import ArgumentParser |
| 3 | +import tools.pandas_utilities as pu |
| 4 | + |
| 5 | +from config.latex_labels import variables_latex, variables_NonLatex |
| 6 | +from config.variable_binning import bin_edges_vis, bin_edges_full |
| 7 | +from config import XSectionConfig |
| 8 | +from tools.file_utilities import make_folder_if_not_exists |
| 9 | +from tools.hist_utilities import values_and_errors_to_hist, hist_to_value_error_tuplelist |
| 10 | + |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import rootpy.plotting.root2matplotlib as rplt |
| 13 | +from config import CMS |
| 14 | + |
| 15 | +# dynamic matplotlib settings |
| 16 | +from matplotlib import rc |
| 17 | +rc( 'font', **CMS.font ) |
| 18 | +rc( 'text', usetex = False ) |
| 19 | + |
| 20 | +def plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder): |
| 21 | + ''' |
| 22 | + Plot the systematic uncertainties |
| 23 | + ''' |
| 24 | + x_limits = [bin_edges[0], bin_edges[-1]] |
| 25 | + y_limits = [0,0.6] |
| 26 | + fig_syst = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' ) |
| 27 | + ax_syst = fig_syst.add_subplot(1, 1, 1) |
| 28 | + ax_syst.minorticks_on() |
| 29 | + ax_syst.xaxis.labelpad = 12 |
| 30 | + ax_syst.yaxis.labelpad = 12 |
| 31 | + |
| 32 | + error_hists = {} |
| 33 | + stat_hist = None |
| 34 | + |
| 35 | + for syst, vals in systematic_uncertainties.iteritems(): |
| 36 | + if syst == 'statistical': |
| 37 | + stat_hist = values_and_errors_to_hist( vals, [], bin_edges ) |
| 38 | + elif syst == 'systematic': |
| 39 | + full_syst_hist = values_and_errors_to_hist( vals, [], bin_edges ) |
| 40 | + elif syst == 'central': |
| 41 | + central_hist = values_and_errors_to_hist( vals, [], bin_edges ) |
| 42 | + else: |
| 43 | + error_hists[syst] = values_and_errors_to_hist( vals, [], bin_edges ) |
| 44 | + |
| 45 | + plt.tick_params( **CMS.axis_label_major ) |
| 46 | + plt.tick_params( **CMS.axis_label_minor ) |
| 47 | + |
| 48 | + colours = ['red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink' ] |
| 49 | + for source, colour in zip (error_hists.keys(), colours): |
| 50 | + hist = error_hists[source] |
| 51 | + hist.linewidth = 4 |
| 52 | + hist.color = colour |
| 53 | + rplt.hist( hist, stacked=False, axes = ax_syst, label = source ) |
| 54 | + |
| 55 | + stat_hist.linewidth = 4 |
| 56 | + stat_hist.color = 'black' |
| 57 | + stat_hist.linestyle = 'dashed' |
| 58 | + rplt.hist( stat_hist, stacked=False, axes = ax_syst, label = 'stat.' ) |
| 59 | + |
| 60 | + full_syst_hist.linewidth = 4 |
| 61 | + full_syst_hist.color = 'black' |
| 62 | + rplt.hist( full_syst_hist, stacked=False, axes = ax_syst, label = 'tot syst.' ) |
| 63 | + |
| 64 | + leg = plt.legend(loc=1,prop={'size':30},ncol=2) |
| 65 | + leg.draw_frame(False) |
| 66 | + |
| 67 | + x_title = variables_NonLatex[variable] |
| 68 | + if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']: |
| 69 | + x_title += ' [GeV]' |
| 70 | + |
| 71 | + ax_syst.set_xlim( x_limits ) |
| 72 | + ax_syst.set_ylim( y_limits ) |
| 73 | + plt.xlabel( x_title, CMS.x_axis_title ) |
| 74 | + plt.ylabel( 'Relative Uncertainty', CMS.y_axis_title) |
| 75 | + plt.tight_layout() |
| 76 | + |
| 77 | + file_template = output_folder + '{var}_systematics_{com}TeV.pdf'.format( |
| 78 | + var = variable, |
| 79 | + com = measurement_config.centre_of_mass_energy, |
| 80 | + ) |
| 81 | + fig_syst.savefig(file_template) |
| 82 | + print "Written plots to {f}".format(f = file_template) |
| 83 | + return |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + parser = ArgumentParser(__doc__) |
| 87 | + parser.add_argument( |
| 88 | + "-p", |
| 89 | + "--path", |
| 90 | + dest="path", |
| 91 | + default='data/normalisation/background_subtraction', |
| 92 | + help="set path to JSON files" |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + "-o", |
| 96 | + "--output_folder", |
| 97 | + dest="output_folder", |
| 98 | + default='tables/', |
| 99 | + help="set path to save tables" |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + "-v", |
| 103 | + "--variable", |
| 104 | + dest="variable", |
| 105 | + default='MET', |
| 106 | + help="set variable to plot (MET, HT, ST, MT, WPT)" |
| 107 | + ) |
| 108 | + parser.add_argument( |
| 109 | + '--visiblePS', |
| 110 | + dest = "visiblePS", |
| 111 | + action = "store_true", |
| 112 | + help = "Unfold to visible phase space" |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "-c", |
| 116 | + "--centre-of-mass-energy", |
| 117 | + dest="CoM", |
| 118 | + default=13, |
| 119 | + type=int, |
| 120 | + help="set the centre of mass energy for analysis. Default = 13 [TeV]" |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "-u", |
| 124 | + "--unfolding_method", |
| 125 | + dest = "unfolding_method", |
| 126 | + default = 'TUnfold', |
| 127 | + help = "Unfolding method: TUnfold (default)" |
| 128 | + ) |
| 129 | + |
| 130 | + args = parser.parse_args() |
| 131 | + |
| 132 | + path = args.path |
| 133 | + com = args.CoM |
| 134 | + method = args.unfolding_method |
| 135 | + variable = args.variable |
| 136 | + output_folder = args.output_folder |
| 137 | + ps_vis = args.visiblePS |
| 138 | + |
| 139 | + phase_space = 'FullPS' |
| 140 | + bin_edges = bin_edges_full[variable] |
| 141 | + if ps_vis: |
| 142 | + phase_space = 'VisiblePS' |
| 143 | + bin_edges = bin_edges_vis[variable] |
| 144 | + measurement_config = XSectionConfig(com) |
| 145 | + |
| 146 | + |
| 147 | + for channel in ['electron', 'muon', 'combined', 'combinedBeforeUnfolding']: |
| 148 | + |
| 149 | + input_file = '{basepath}/{com}TeV/{var}/{ps}/central/normalised_xsection_{channel}_{method}_summary_relative.txt'.format( |
| 150 | + basepath = path, |
| 151 | + com = com, |
| 152 | + var = variable, |
| 153 | + ps = phase_space, |
| 154 | + channel = channel, |
| 155 | + method = method, |
| 156 | + ) |
| 157 | + output_folder = 'plots/systematics/{channel}/{ps}/'.format( |
| 158 | + channel = channel, |
| 159 | + ps = phase_space, |
| 160 | + ) |
| 161 | + make_folder_if_not_exists(output_folder) |
| 162 | + |
| 163 | + systematic_uncertainties = pu.file_to_df(input_file) |
| 164 | + |
| 165 | + plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder) |
| 166 | + |
0 commit comments