Skip to content

Commit 5371f35

Browse files
DouglasBurnskreczko
authored andcommitted
Redo 05_Tables [WIP] (#340)
Redo 05_Tables
1 parent 523cf26 commit 5371f35

5 files changed

Lines changed: 308 additions & 46 deletions

File tree

src/cross_section_measurement/03_calculate_systematics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,5 @@
136136
# Write central +- error to JSON. Group of systematics in question is included in outputfile name.
137137
# Summary if you want to specify specific list. e.g. GeneratorOnly etc
138138
write_normalised_xsection_measurement(opts, full_measurement, full_unfolded_measurement, summary = '' )
139+
write_systematic_xsection_measurement(opts, unfolded_x_sec_with_symmetrised_systematics, full_unfolded_measurement, summary = '' )
139140

src/cross_section_measurement/04_make_plots_matplotlib.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def read_xsection_measurement_results( category, channel ):
6868
h_normalised_xsection_amcatnlo = value_error_tuplelist_to_hist( normalised_xsection_unfolded['amcatnlo'], edges )
6969
h_normalised_xsection_madgraphMLM = value_error_tuplelist_to_hist( normalised_xsection_unfolded['madgraphMLM'], edges )
7070
h_normalised_xsection_powhegHerwigpp = value_error_tuplelist_to_hist( normalised_xsection_unfolded['powhegHerwig'], edges )
71-
h_normalised_xsection_amcatnloHerwigpp = value_error_tuplelist_to_hist( normalised_xsection_unfolded['amcatnloHerwig'], edges )
71+
# h_normalised_xsection_amcatnloHerwigpp = value_error_tuplelist_to_hist( normalised_xsection_unfolded['amcatnloHerwig'], edges )
7272

73-
h_normalised_xsection_scaleup = value_error_tuplelist_to_hist( normalised_xsection_unfolded['scaleup'], edges )
74-
h_normalised_xsection_scaledown = value_error_tuplelist_to_hist( normalised_xsection_unfolded['scaledown'], edges )
73+
# h_normalised_xsection_scaleup = value_error_tuplelist_to_hist( normalised_xsection_unfolded['scaleup'], edges )
74+
# h_normalised_xsection_scaledown = value_error_tuplelist_to_hist( normalised_xsection_unfolded['scaledown'], edges )
7575
h_normalised_xsection_massup = value_error_tuplelist_to_hist( normalised_xsection_unfolded['massup'], edges )
7676
h_normalised_xsection_massdown = value_error_tuplelist_to_hist( normalised_xsection_unfolded['massdown'], edges )
7777

@@ -80,12 +80,12 @@ def read_xsection_measurement_results( category, channel ):
8080
'amcatnloPythia8':h_normalised_xsection_amcatnlo,
8181
'madgraphMLM':h_normalised_xsection_madgraphMLM,
8282
'powhegHerwig':h_normalised_xsection_powhegHerwigpp,
83-
'amcatnloHerwig':h_normalised_xsection_amcatnloHerwigpp,
83+
# 'amcatnloHerwig':h_normalised_xsection_amcatnloHerwigpp,
8484
})
8585

8686
histograms_normalised_xsection_systematics_shifts.update( {'powhegPythia8':h_normalised_xsection_powhegPythia8,
87-
'scaledown': h_normalised_xsection_scaledown,
88-
'scaleup': h_normalised_xsection_scaleup,
87+
# 'scaledown': h_normalised_xsection_scaledown,
88+
# 'scaleup': h_normalised_xsection_scaleup,
8989
'massdown': h_normalised_xsection_massdown,
9090
'massup': h_normalised_xsection_massup
9191
})
@@ -434,9 +434,9 @@ def make_plots( histograms, category, output_folder, histname, show_ratio = True
434434
elif 'amcatnloPythia8' in key or 'massup' in key:
435435
hist.SetLineColor( 807 )
436436
dashes[key] = [20,5]
437-
elif 'amcatnloHerwig' in key:
438-
hist.SetLineColor( 734 )
439-
dashes[key] = [15,5]
437+
# elif 'amcatnloHerwig' in key:
438+
# hist.SetLineColor( 734 )
439+
# dashes[key] = [15,5]
440440
elif 'MCATNLO' in key or 'scaleup' in key:
441441
hist.SetLineColor( 619 )
442442
dashes[key] = [5,5,10,5]
@@ -831,7 +831,7 @@ def get_unit_string(fit_variable):
831831
parser.add_option( "-u", "--unfolding_method", dest = "unfolding_method", default = 'TUnfold',
832832
help = "Unfolding method: TUnfold (default), RooUnfoldSvd, TSVDUnfold, RooUnfoldTUnfold, RooUnfoldInvert, RooUnfoldBinByBin, RooUnfoldBayes" )
833833

834-
output_formats = ['png', 'pdf']
834+
output_formats = ['pdf']
835835
( options, args ) = parser.parse_args()
836836
if options.debug:
837837
log.setLevel(log.DEBUG)
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+

tools/pandas_utilities.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pandas as pd
2+
import os
3+
4+
pd.set_option('display.max_columns', None)
5+
pd.set_option('display.max_colwidth', 4096)
6+
pd.set_option('display.max_rows', 50)
7+
pd.set_option('display.width', 1000)
8+
9+
def dict_to_df(d):
10+
'''
11+
Transform a dictionary nto a dataframe
12+
'''
13+
df = pd.DataFrame(d)
14+
return df
15+
16+
def list_to_series(l):
17+
'''
18+
Transform a list into a series
19+
'''
20+
s = pd.Series( l )
21+
return s
22+
23+
def df_to_file(filename, df):
24+
'''
25+
Save a dataframe to an output text file
26+
Nicely human readable
27+
'''
28+
with open(filename,'w') as f:
29+
df.to_string(f, index=True)
30+
f.write('\n')
31+
print('DataFrame written to {}'.format(f))
32+
f.close()
33+
return
34+
35+
def file_to_df(f):
36+
'''
37+
Read a dataframe from file
38+
'''
39+
if os.path.exists(f):
40+
with open(f,'r') as f:
41+
df = pd.read_table(f, delim_whitespace=True)
42+
print('DataFrame read form {}'.format(f))
43+
f.close()
44+
else:
45+
print "Could not find {} ".format(f)
46+
return
47+
return df
48+
49+
def append_to_df(df, df_new):
50+
'''
51+
Append something to dataframe.
52+
Must have the same number of indexes
53+
'''
54+
# Overwrite existing columns of the same name
55+
for newcols in df_new.columns:
56+
for cols in df.columns:
57+
if newcols == cols:
58+
del df[cols]
59+
df = pd.concat([df, df_new], axis=1)
60+
return df
61+
62+
def divide_by_series(s1, s2):
63+
'''
64+
Divide one series by another
65+
'''
66+
s = s1.div(s2)
67+
return s

0 commit comments

Comments
 (0)