-
Notifications
You must be signed in to change notification settings - Fork 6
/
submit.py
359 lines (348 loc) · 12.9 KB
/
submit.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import json
import time
import dask
import pickle
import argparse
import datetime
import numpy as np
import wprime_plus_b.utils
from pathlib import Path
from coffea import processor
from utils import get_filesets
from dask.distributed import Client
from humanfriendly import format_timespan
from distributed.diagnostics.plugin import UploadDirectory
from wprime_plus_b.utils import paths
from wprime_plus_b.processors.trigger_efficiency_processor import (
TriggerEfficiencyProcessor,
)
from wprime_plus_b.processors.btag_efficiency_processor import BTagEfficiencyProcessor
from wprime_plus_b.processors.ttbar_analysis import TtbarAnalysis
from wprime_plus_b.processors.ztoll_processor import ZToLLProcessor
from wprime_plus_b.processors.qcd_analysis import QcdAnalysis
from wprime_plus_b.selections.ttbar.config import (
ttbar_electron_selection,
ttbar_muon_selection,
ttbar_jet_selection,
ttbar_tau_selection
)
from wprime_plus_b.selections.ztoll.config import (
ztoll_electron_selection,
ztoll_muon_selection,
ztoll_jet_selection,
)
from wprime_plus_b.selections.qcd.config import (
qcd_electron_selection,
qcd_muon_selection,
qcd_jet_selection,
qcd_tau_selection
)
def main(args):
args = vars(args)
# define processors and executors
processors = {
"ttbar": TtbarAnalysis,
"ztoll": ZToLLProcessor,
"qcd": QcdAnalysis,
"btag_eff": BTagEfficiencyProcessor,
"trigger_eff": TriggerEfficiencyProcessor,
}
processor_args = [
"year",
"yearmod",
"channel",
"lepton_flavor",
"output_type",
"syst",
]
processor_kwargs = {k: args[k] for k in processor_args if args[k]}
executors = {
"iterative": processor.iterative_executor,
"futures": processor.futures_executor,
"dask": processor.dask_executor,
}
executor_args = {
"schema": processor.NanoAODSchema,
}
if args["executor"] == "futures":
executor_args.update({"workers": args["workers"]})
if args["executor"] == "dask":
client = Client("tls://localhost:8786")
executor_args.update({"client": client})
# upload local directory to dask workers
try:
client.register_worker_plugin(
UploadDirectory(f"{Path.cwd()}", restart=True, update_path=True),
nanny=True,
)
print(f"Uploaded {Path.cwd()} succesfully")
except OSError:
print("Failed to upload the directory")
# get .json filesets for sample
filesets = get_filesets(
sample=args["sample"],
year=args["year"] + args["yearmod"],
facility=args["facility"],
)
for sample, fileset_path in filesets.items():
if len(args["nsample"]) != 0:
samples_keys = args["nsample"].split(",")
if sample.split("_")[-1] not in samples_keys:
continue
print(f"Processing {sample}")
fileset = {}
with open(fileset_path, "r") as handle:
data = json.load(handle)
for root_file in data.values():
if args["nfiles"] != -1:
root_file = root_file[: args["nfiles"]]
if sample.startswith("Signal"):
fileset[sample] = [f"root://eoscms.cern.ch//eos/cms/" + file for file in root_file]
elif args["facility"] == "coffea-casa":
fileset[sample] = [f"root://xcache/" + file for file in root_file]
else:
fileset[sample] = root_file
# run processor
t0 = time.monotonic()
out = processor.run_uproot_job(
fileset,
treename="Events",
processor_instance=processors[args["processor"]](**processor_kwargs),
executor=executors[args["executor"]],
executor_args=executor_args,
)
exec_time = format_timespan(time.monotonic() - t0)
# get metadata
metadata = {"walltime": exec_time}
metadata.update({"fileset": fileset[sample]})
if "metadata" in out[sample]:
output_metadata = out[sample]["metadata"]
# save number of raw initial events
metadata.update({"raw_initial_nevents": float(output_metadata["raw_initial_nevents"])})
# save number of weighted initial events
if args["processor"] == "qcd":
if args["channel"] != "all":
metadata.update({"sumw": float(output_metadata[args["channel"]]["sumw"])})
else:
sumws = {}
for r in ["A", "B", "C", "D"]:
sumws[r] = float(output_metadata[r]["sumw"])
metadata.update({"sumw": sumws})
else:
metadata.update({"sumw": float(output_metadata["sumw"])})
# save qcd metadata
if args["processor"] in ["qcd"]:
metadata.update({"nevents": {}})
region = args["channel"]
if region != "all":
metadata["nevents"].update({region: {}})
metadata["nevents"][region]["raw_final_nevents"] = str(
output_metadata[region]["raw_final_nevents"]
)
metadata["nevents"][region]["weighted_final_nevents"] = str(
output_metadata[region]["weighted_final_nevents"]
)
elif region == "all":
for r in ["A", "B", "C", "D"]:
metadata["nevents"].update({r: {}})
metadata["nevents"][r]["raw_final_nevents"] = str(
output_metadata[r]["raw_final_nevents"]
)
metadata["nevents"][r]["weighted_final_nevents"] = str(
output_metadata[r]["weighted_final_nevents"]
)
# save ttbar and ztoll metadata
if args["processor"] in ["ttbar", "ztoll"]:
# save raw and weighted number of events after selection
if "raw_final_nevents" in output_metadata:
metadata.update(
{"raw_final_nevents": float(output_metadata["raw_final_nevents"])}
)
metadata.update(
{"weighted_final_nevents": float(output_metadata["weighted_final_nevents"])}
)
else:
metadata.update(
{"raw_final_nevents": 0.}
)
metadata.update(
{"weighted_final_nevents": 0.}
)
# save cutflow to metadata
for cut_selection, nevents in output_metadata["cutflow"].items():
output_metadata["cutflow"][cut_selection] = str(nevents)
metadata.update({"cutflow": output_metadata["cutflow"]})
for weight, statistics in output_metadata["weight_statistics"].items():
output_metadata["weight_statistics"][weight] = str(statistics)
metadata.update(
{"weight_statistics": output_metadata["weight_statistics"]}
)
# save selectios to metadata
if args["processor"] == "ttbar":
selections = {
"electron_selection": ttbar_electron_selection[args["channel"]][
args["lepton_flavor"]
],
"muon_selection": ttbar_muon_selection[args["channel"]][
args["lepton_flavor"]
],
"jet_selection": ttbar_jet_selection[args["channel"]][
args["lepton_flavor"]
],
"tau_selection": ttbar_tau_selection[args["channel"]][
args["lepton_flavor"]
]
}
metadata.update({"selections": selections})
elif args["processor"] == "ztoll":
selections = {
"electron_selection": ztoll_electron_selection,
"muon_selection": ztoll_muon_selection,
"jet_selection": ztoll_jet_selection,
}
metadata.update({"selections": selections})
elif args["processor"] == "qcd":
region = args["channel"]
if region != "all":
selections = {
"electron_selection": qcd_electron_selection[region][args["lepton_flavor"]],
"muon_selection": qcd_muon_selection[region][args["lepton_flavor"]],
"jet_selection": qcd_jet_selection[region][args["lepton_flavor"]],
"tau_selection": qcd_tau_selection[region][args["lepton_flavor"]],
}
metadata.update({"selections": selections})
elif region == "all":
selections = {}
for r in ["A", "B", "C", "D"]:
selections[r] = {
"electron_selection": qcd_electron_selection[r][args["lepton_flavor"]],
"muon_selection": qcd_muon_selection[r][args["lepton_flavor"]],
"jet_selection": qcd_jet_selection[r][args["lepton_flavor"]],
"tau_selection": qcd_tau_selection[r][args["lepton_flavor"]],
}
metadata.update({"selections": selections})
# save args to metadata
args_dict = args.copy()
metadata.update(args_dict)
del out[sample]["metadata"]
# save output data and metadata
with open(f"{args['output_path']}/metadata/{sample}_metadata.json", "w") as f:
f.write(json.dumps(metadata))
with open(f"{args['output_path']}/{sample}.pkl", "wb") as handle:
pickle.dump(out, handle, protocol=pickle.HIGHEST_PROTOCOL)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--processor",
dest="processor",
type=str,
default="",
help="processor to be used {ttbar, ztoll, qcd, trigger_eff, btag_eff} (default ttbar)",
)
parser.add_argument(
"--channel",
dest="channel",
type=str,
default="",
help="channel to be processed",
)
parser.add_argument(
"--lepton_flavor",
dest="lepton_flavor",
type=str,
default="",
help="lepton flavor to be processed {'mu', 'ele'}",
)
parser.add_argument(
"--sample",
dest="sample",
type=str,
default="",
help="sample key to be processed",
)
parser.add_argument(
"--year",
dest="year",
type=str,
default="",
help="year of the data {2016, 2017, 2018} (default 2017)",
)
parser.add_argument(
"--yearmod",
dest="yearmod",
type=str,
default="",
help="year modifier {'', 'APV'} (default '')",
)
parser.add_argument(
"--executor",
dest="executor",
type=str,
default="",
help="executor to be used {iterative, futures, dask} (default iterative)",
)
parser.add_argument(
"--workers",
dest="workers",
type=int,
default=4,
help="number of workers to use with futures executor (default 4)",
)
parser.add_argument(
"--nfiles",
dest="nfiles",
type=int,
default=1,
help="number of .root files to be processed by sample. To run all files use -1 (default 1)",
)
parser.add_argument(
"--nsample",
dest="nsample",
type=str,
default="",
help="partitions to run (--nsample 1,2,3 will only run partitions 1,2 and 3)",
)
parser.add_argument(
"--chunksize",
dest="chunksize",
type=int,
default=50000,
help="number of chunks to process",
)
parser.add_argument(
"--output_type",
dest="output_type",
type=str,
default="",
help="type of output {hist, array}",
)
parser.add_argument(
"--syst",
dest="syst",
type=str,
default="",
help="systematic to apply {'nominal', 'jet', 'met', 'full'}",
)
parser.add_argument(
"--facility",
dest="facility",
type=str,
default="",
help="facility to launch jobs {coffea-casa, lxplus}",
)
parser.add_argument(
"--tag",
dest="tag",
type=str,
default="",
help="tag to reference output files directory",
)
parser.add_argument(
"--output_path",
dest="output_path",
type=str,
default="",
help="output path directory",
)
args = parser.parse_args()
main(args)