-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplot_nominal.py
312 lines (284 loc) · 13.7 KB
/
plot_nominal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import HiggsAnalysis.KITHiggsToTauTau.plotting.higgsplot as higgsplot
import argparse
from copy import deepcopy
import logging
logger = logging.getLogger("")
def parse_arguments():
parser = argparse.ArgumentParser(
description=
"Plot categories using HarryPlotter from shapes produced by shape-producer module."
)
parser.add_argument("--comparison", action="store_true", help="Plot Embedded - MC comparison. --emb has to be set.")
parser.add_argument("--emb", action="store_true", help="Embedded prefix")
parser.add_argument("--ff", action="store_true", help="Use fake-factors")
parser.add_argument(
"-v",
"--variables",
nargs="+",
type=str,
required=True,
help="Variable on x-axis")
parser.add_argument(
"--categories",
nargs="+",
type=str,
required=False, default=None,
help="Categories")
parser.add_argument("--era", type=str, default="Run2018", help="Era")
parser.add_argument(
"--lumi", type=float, default=None, help="Integrated Luminosity")
parser.add_argument("--mass", type=str, default="125", help="Mass")
parser.add_argument(
"--additional-arguments",
type=str,
default="",
help="Additional higgsplot.py arguments")
parser.add_argument(
"--output-dir",
type=str,
default="plots",
help="Output directory for plots")
parser.add_argument(
"--y-log",
action="store_true",
default=False,
help="Use logarithmic y-axis")
parser.add_argument(
"-c",
"--channels", nargs="+", type=str, required=True, help="Channel")
parser.add_argument(
"--analysis", type=str, default="smhtt", help="Analysis")
parser.add_argument(
"--shapes",
type=str,
default=None,
help="ROOT files with shapes of processes")
parser.add_argument(
"--x-label", type=str, default=None, help="Label on x-axis")
parser.add_argument("--chi2", action="store_true", help="Print chi2 value")
parser.add_argument(
"--num-processes",
type=int,
default=24,
help="Number of processes used for plotting")
parser.add_argument("--filename-prefix", type=str, default="", help="filename prefix")
parser.add_argument("--www", action="store_true", help="webplotting")
parser.add_argument("--www-dir", type=str, default=None,
help='Directory structure where the plots will be uploaded. {date} expressions will be replaced by date.')
parser.add_argument("--www-no-overwrite", action='store_true', default=False, help="Don't overwrite remote file. [Default: %(default)s]")
parser.add_argument("--no-overwrite", "--keep-both", "--keep", "-k", action='store_true', default=False, help="Don't overwrite output file. [Default: %(default)s]")
parser.add_argument("--log-level", default="debug", help="log level. [Default: %(default)s]")
parser.add_argument("--redo-cache", action="store_true",
help="Do not use inputs from cached trees, but overwrite them. [Default: False for absolute paths, True for relative paths]")
return parser.parse_args()
def setup_logging(output_file, level=logging.DEBUG):
logger.setLevel(level)
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
file_handler = logging.FileHandler(output_file, "w")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
config_template = {
"colors": ["#B7B7B7", "#000000"],
"filename": "",
"files": [None],
"labels": ["", ""],
"legend": [0.4, 0.52, 0.88, 0.83],
"legend_cols": 2,
"legend_markers": ["ELP", "ELP"],
"stacks": ["ratio_bkg", "ratio_data"],
"markers": ["E2", "P"],
"formats": ["pdf", "png"],
"title": "",
"cms": True,
"extra_text": "Preliminary",
"energies": [13],
"nicks_blacklist": ["noplot"],
"analysis_modules": ["Ratio"],
"ratio_result_nicks": ["ratio_Bkg", "ratio_Data"],
"y_subplot_lims": [0.62, 1.5],
"y_label": "N_{evts}",
"y_subplot_label": "#scale[0.8]{stat bkg unc.}",
"subplot_lines": [0.8, 1.0, 1.2]
}
logvars = ["nbtag","njets","jpt_1","jpt_2"]
def main(args):
signal_names=[]
signal=[]
if args.emb and args.ff:
bkg_processes_names = [
"emb", "zll", "ttl", "vvl", "fakes"
]
bkg_processes = ["EMB", "ZL", "TTL", "VVL", "jetFakes"]
elif args.emb:
bkg_processes_names = ["emb", "zll","zj", "ttl", "ttj","vvl", "vvj", "w", "qcd"]
bkg_processes = ["EMB", "ZL", "ZJ","TTL", "TTJ","VVL", "VVJ", "W", "QCD"]
elif args.ff:
bkg_processes_names = [
"ztt", "zll", "ttt", "ttl", "vvt", "vvl", "fakes"
] # enforced by HarryPlotter
bkg_processes = ["ZTT", "ZL", "TTT", "TTL", "VVT", "VVL", "jetFakes"]
else:
bkg_processes_names = [
"ztt", "zll","zj","ttl", "ttt","ttj","vvl","vvt","vvj","w","qcd"]
bkg_processes = ["ZTT", "ZL", "ZJ","TTL","TTT", "TTJ", "VVL","VVT","VVJ","W","QCD"]
channels = args.channels
analysis = args.analysis
era = args.era
if args.lumi is None:
if "2016" in era:
lumi = 35.9
elif "2017" in era:
lumi = 41.5
elif "2018" in era:
lumi = 59.7
else:
lumi=args.lumi
output_dir = args.output_dir
y_log = args.y_log
mass = args.mass
variables = args.variables
categories = [c for c in args.categories] if args.categories is not None else None
if "all" in variables:
variables = ["mt_1","mt_2", "pt_1","pt_2", "eta_1", "eta_2", "m_vis", "ptvis", "npv", "njets", "nbtag", "jpt_1", "jpt_2", "jeta_1", "jeta_2", "met", "mjj", "dijetpt", "pZetaMissVis", "m_1", "m_2", "decayMode_1", "decayMode_2", "iso_1", "iso_2", "rho", "mt_tot", "d0_1", "d0_2", "dZ_1", "dZ_2"]
if args.www:
config_template['www'] = ''
if args.www_dir:
config_template['www_dir'] = args.www_dir
if args.www_no_overwrite:
config_template['www_no_overwrite'] = True
config_template['no_overwrite'] = True
if args.no_overwrite:
config_template['no_overwrite'] = True
if args.redo_cache:
config_template['redo_cache'] = True
if args.filename_prefix != '':
args.filename_prefix = args.filename_prefix if args.filename_prefix[0] == '_' else '_' + args.filename_prefix
# if args.log_level == 'debug':
# config['log_level'] =
configs = []
for channel in channels:
if args.categories is None:
categories = [channel+"_"+v for v in variables]
shapes = args.shapes if args.shapes is not None else "shapes_{}.root".format(channel)
if "em" in channel:
bkg_processes = [p for p in bkg_processes if p not in ["ZJ", "TTJ", "VVJ"]]
bkg_processes_names = [p for p in bkg_processes_names if p not in ["zj", "ttj", "vvj"]]
if len(variables)==1:
variables=[variables[0]]*len(categories)
for variable, category in zip(variables, categories):
config = deepcopy(config_template)
if args.chi2:
config["analysis_modules"].append("AddHistograms")
config["analysis_modules"].append("Chi2Test")
config["add_nicks"] = [" ".join(bkg_processes_names)]
config["add_result_nicks"] = ["tot_background_noplot"]
config["chi2test_nicks"] = ["tot_background_noplot data"]
config["chi2test_compare"] = ["UU CHI2/NDF"]
config["files"] = [shapes]
config["lumis"] = [lumi]
config["year"] = era.strip("Run")
config["output_dir"] = output_dir+"/"+channel
config["y_log"] = True if ((variable in logvars) or y_log) else False
config["y_rel_lims"] = [5, 500] if (variable in logvars) else [0.9, 1.5]
config["markers"] = ["HIST"] * len(bkg_processes_names) + ["LINE"]*len(signal_names) + ["P"] + ["E2"] + ["LINE"]*len(signal_names) + ["P"]
config["legend_markers"] = ["F"] * (len(bkg_processes_names)) + ["LINE"]*len(signal_names) + ["ELP"] + ["E2"] + ["L"]*len(signal_names) + ["P"]
config["labels"] = bkg_processes_names + signal_names + ["data"
] + [""]*len(signal_names) + config["labels"]
config["colors"] = bkg_processes_names + signal_names + ["data"
] + ["#B7B7B7"] + signal_names + ["#000000"]
config["nicks"] = bkg_processes_names + signal_names + ["data"]
config["x_expressions"] = [
"#" + "#".join([
channel, category, process, analysis, era, variable, mass
]) + "#" for process in bkg_processes+signal
] + [
"#" + "#".join([
channel, category, "data_obs", analysis, era, variable,
mass
]) + "#"
]
if args.emb == False:
config["filename"] = "_".join(
[channel, category, analysis, era, variable, mass]) + args.filename_prefix
else:
config["filename"] = "_".join(
[channel, category, analysis, era, variable, mass,"emb"]) + args.filename_prefix
if args.comparison:
config["filename"] = "_".join(
[channel, category, analysis, era, variable, mass,"comparison"]) + args.filename_prefix
if args.ff:
config["filename"] = config["filename"]+"_ff"
if not args.x_label == None:
config["x_label"] = args.x_label
else:
config["x_label"] = "_".join([channel, variable])
config["title"] = "_".join(["channel", channel])
config["stacks"] = ["mc"] * len(bkg_processes_names) + signal_names + [
"data"
] + [x+"Ratio" for x in signal_names] + config["stacks"]
config["ratio_denominator_nicks"] = [
" ".join(bkg_processes_names)
] * (2+len(signal_names))
config["ratio_numerator_nicks"] = [" ".join(bkg_processes_names)] + ["data"]
config["ratio_result_nicks"] = ["bkg_ratio"] + [x+"_ratio" for x in signal_names] + ["data_ratio"]
if args.comparison:
config["markers"] = ["HIST"] + ["LINE"] + ["HIST"] * (len(bkg_processes_names)-1) + ["EX0"] + ["E2", "EX0"] + ["EX0"]
config["legend_markers"] = ["F"] + ["L"] + ["F"] * (len(bkg_processes_names)-1) + ["PEX0"] + ["F", "PEX0"] + ["PEX0"]
config["labels"] = [bkg_processes_names[0]] + ["Z #rightarrow #tau#tau (simulation)"] + bkg_processes_names[1:] + ["data"
] + ["Bkg. (embedded) unc.", "Obs./Bkg. (simulation)", "Obs./Bkg. (embedded)"]
config["colors"] = [bkg_processes_names[0]] + ["#C70039"] + bkg_processes_names[1:] + ["data"
] + ["#B7B7B7", " #C70039 ", "#000000"]
config["subplot_legend"] = [
0.195,
0.77,
0.92,
0.93
],
config["subplot_legend_cols"] = 3
config["subplot_legend_fontsize"] = 0.06
config["y_subplot_label"] = ""
config["legend_fontsize"] = 0.044
bkg_processes_names_noplot = ["ztt_noplot","ttt_noplot","vvt_noplot"] + [x+"_noplot" for x in bkg_processes_names if not "emb" in x]
config["nicks"] = bkg_processes_names + bkg_processes_names_noplot + ["data"]
config["analysis_modules"].append("AddHistograms")
config["add_nicks"] = [" ".join(bkg_processes_names_noplot)]
config["add_result_nicks"] = ["ztt"]
config["add_result_position"] = 1
config["x_expressions"] = [
"#" + "#".join([
channel, category, process, analysis, era, variable, mass
]) + "#" for process in bkg_processes
] + [
"#" + "#".join([
channel, category, process, analysis, era, variable, mass
]) + "#" for process in ["ZTT","TTT","VVT"] + [x for x in bkg_processes if x!="EMB"]
] + [
"#" + "#".join([
channel, category, "data_obs", analysis, era, variable,
mass
]) + "#"
]
config["stacks"] = ["mc"] + ["ztt"] + ["mc"] * (len(bkg_processes_names)-1) + ["data"] + config_template["stacks"] + ["ratio_data_zttmc"]
config["ratio_denominator_nicks"] = [" ".join(bkg_processes_names)] + [" ".join(bkg_processes_names_noplot)] + [" ".join(bkg_processes_names)]
config["ratio_numerator_nicks"] = [
" ".join(bkg_processes_names), "data", "data"
]
config["ratio_result_nicks"] = ["ratio_Bkg", "ratio_Data_to_MC", "ratio_Data"]
configs.append(config)
higgsplot.HiggsPlotter(
list_of_config_dicts=configs,
list_of_args_strings=[args.additional_arguments],
n_processes=args.num_processes)
if __name__ == "__main__":
args = parse_arguments()
if args.log_level == 'debug':
ll = logging.DEBUG
else:
ll = logging.INFO
setup_logging("plot_nominal.log", ll)
main(args)