Skip to content

Commit 258c8b6

Browse files
committed
Merge pull request BristolTopGroup#281 from kreczko/missing-bits
Some missing bits
2 parents b2b56c8 + 56bbbdb commit 258c8b6

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

src/unfolding_tests/bias_test.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'''
2+
This module creates the bias tests for each variable &
3+
channel (electron, muon, combined).
4+
'''
5+
from rootpy.io import File
6+
7+
from config.variable_binning import bin_edges
8+
from tools.Unfolding import Unfolding, get_unfold_histogram_tuple
9+
from config.cross_section_config import XSectionConfig
10+
from tools.plotting import compare_measurements, Histogram_properties
11+
from config import latex_labels
12+
13+
14+
def main():
15+
config = XSectionConfig(13)
16+
# method = 'RooUnfoldSvd'
17+
method = 'RooUnfoldBayes'
18+
file_for_data = File(config.unfolding_powheg_herwig, 'read')
19+
file_for_unfolding = File(config.unfolding_madgraphMLM, 'read')
20+
for channel in ['electron', 'muon', 'combined']:
21+
for variable in bin_edges.keys():
22+
tau_value = get_tau_value(config, channel, variable)
23+
h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
24+
inputfile=file_for_unfolding,
25+
variable=variable,
26+
channel=channel,
27+
met_type=config.met_type,
28+
centre_of_mass=config.centre_of_mass_energy,
29+
ttbar_xsection=config.ttbar_xsection,
30+
luminosity=config.luminosity,
31+
load_fakes=False,
32+
visiblePS=False,
33+
)
34+
h_data_model, h_data, _, _ = get_unfold_histogram_tuple(
35+
inputfile=file_for_data,
36+
variable=variable,
37+
channel=channel,
38+
met_type=config.met_type,
39+
centre_of_mass=config.centre_of_mass_energy,
40+
ttbar_xsection=config.ttbar_xsection,
41+
luminosity=config.luminosity,
42+
load_fakes=False,
43+
visiblePS=False,
44+
)
45+
46+
unfolding = Unfolding(
47+
h_truth, h_measured, h_response, h_fakes,
48+
method=method, k_value=-1, tau=tau_value)
49+
50+
unfolded_data = unfolding.unfold(h_data)
51+
plot_bias(h_truth, h_data_model, unfolded_data, variable, channel,
52+
config.centre_of_mass_energy, method)
53+
54+
55+
def get_tau_value(config, channel, variable):
56+
if channel == 'electron':
57+
return config.tau_values_electron[variable]
58+
if channel == 'muon':
59+
return config.tau_values_muon[variable]
60+
if channel == 'combined':
61+
return config.tau_values_combined[variable]
62+
63+
64+
def plot_bias(h_unfold_model, h_data_model, unfolded_data, variable,
65+
channel, come, method):
66+
hp = Histogram_properties()
67+
hp.name = '{channel}_bias_test_for_{variable}_at_{come}TeV'.format(
68+
channel=channel,
69+
variable=variable,
70+
come=come,
71+
)
72+
v_latex = latex_labels.variables_latex[variable]
73+
unit = ''
74+
if variable in ['HT', 'ST', 'MET', 'WPT']:
75+
unit = ' [GeV]'
76+
hp.x_axis_title = v_latex + unit
77+
hp.y_axis_title = 'Events'
78+
hp.title = 'Closure tests for {variable}'.format(variable=v_latex)
79+
80+
output_folder = 'plots/unfolding/bias_test/{0}/'.format(method)
81+
82+
compare_measurements(models={'MC truth': h_data_model,
83+
'unfold model': h_unfold_model},
84+
measurements={'unfolded reco': unfolded_data},
85+
show_measurement_errors=True,
86+
histogram_properties=hp,
87+
save_folder=output_folder,
88+
save_as=['png', 'pdf'])
89+
90+
if __name__ == '__main__':
91+
main()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
This module creates the closure tests for each variable &
3+
channel (electron, muon, combined).
4+
'''
5+
from rootpy.io import File
6+
7+
from config.variable_binning import bin_edges
8+
from tools.Unfolding import Unfolding, get_unfold_histogram_tuple
9+
from config.cross_section_config import XSectionConfig
10+
from tools.plotting import compare_measurements, Histogram_properties
11+
from config import latex_labels
12+
13+
14+
def main():
15+
config = XSectionConfig(13)
16+
# method = 'RooUnfoldSvd'
17+
method = 'RooUnfoldBayes'
18+
file_for_unfolding = File(config.unfolding_central, 'read')
19+
for channel in ['electron', 'muon', 'combined']:
20+
for variable in bin_edges.keys():
21+
tau_value = get_tau_value(config, channel, variable)
22+
h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
23+
inputfile=file_for_unfolding,
24+
variable=variable,
25+
channel=channel,
26+
met_type=config.met_type,
27+
centre_of_mass=config.centre_of_mass_energy,
28+
ttbar_xsection=config.ttbar_xsection,
29+
luminosity=config.luminosity,
30+
load_fakes=False,
31+
visiblePS=False,
32+
)
33+
unfolding = Unfolding(
34+
h_truth, h_measured, h_response, h_fakes,
35+
method=method, k_value=-1, tau=tau_value)
36+
37+
unfolded_data = unfolding.closureTest()
38+
plot_closure(h_truth, unfolded_data, variable, channel,
39+
config.centre_of_mass_energy, method)
40+
41+
42+
def get_tau_value(config, channel, variable):
43+
if channel == 'electron':
44+
return config.tau_values_electron[variable]
45+
if channel == 'muon':
46+
return config.tau_values_muon[variable]
47+
if channel == 'combined':
48+
return config.tau_values_combined[variable]
49+
50+
51+
def plot_closure(h_truth, unfolded_data, variable, channel, come, method):
52+
hp = Histogram_properties()
53+
hp.name = '{channel}_closure_test_for_{variable}_at_{come}TeV'.format(
54+
channel=channel,
55+
variable=variable,
56+
come=come,
57+
)
58+
v_latex = latex_labels.variables_latex[variable]
59+
unit = ''
60+
if variable in ['HT', 'ST', 'MET', 'WPT']:
61+
unit = ' [GeV]'
62+
hp.x_axis_title = v_latex + unit
63+
hp.y_axis_title = 'Events'
64+
hp.title = 'Closure tests for {variable}'.format(variable=v_latex)
65+
66+
output_folder = 'plots/unfolding/closure_test/{0}/'.format(method)
67+
68+
compare_measurements(models={'MC truth': h_truth},
69+
measurements={'unfolded reco': unfolded_data},
70+
show_measurement_errors=True,
71+
histogram_properties=hp,
72+
save_folder=output_folder,
73+
save_as=['png', 'pdf'])
74+
75+
if __name__ == '__main__':
76+
main()

0 commit comments

Comments
 (0)