forked from farakiko/boostedhiggs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
452 lines (375 loc) · 15.2 KB
/
run.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/python
import argparse
import json
import os
import pickle as pkl
import time
import pandas as pd
import uproot
from coffea import nanoevents, processor
nanoevents.PFNanoAODSchema.warn_missing_crossrefs = False
import sys
sys.path.insert(0, "")
#sys.path.append("boostedhiggs/LundReweighting")
#sys.path.append("boostedhiggs/LundReweighting/utils")
# # from utils.LundReweighter import *
# # from utils.Utils import *
# import LundReweighter
def main(args):
# make directory for output
if not os.path.exists("./outfiles"):
os.makedirs("./outfiles")
channels = ["ele", "mu"]
if args.channels:
channels = args.channels.split(",")
# if --macos is specified in args, process only the files provided
if args.macos:
files = {}
#files[args.sample] = [f"rootfiles2/rootfiles/{args.sample}/file{i+1}.root" for i in range(1)]
files[args.sample] = [f"{args.sample}/file{i+1}.root" for i in range(1)]
# if --local is specified in args, process only the args.sample provided
elif args.local:
files = {}
with open(f"fileset/pfnanoindex_{args.pfnano}_{args.year}.json", "r") as f:
files_all = json.load(f)
for subdir in files_all[args.year]:
for key, flist in files_all[args.year][subdir].items():
if key in args.sample:
files[key] = ["root://cmseos.fnal.gov/" + f for f in flist]
else:
# get samples
if "metadata" in args.config:
with open(args.config, "r") as f:
files = json.load(f)
else:
if not args.config or not args.configkey:
raise Exception("No config or configkey provided for condor jobs")
# hopefully this step is avoided in condor jobs that have metadata.json
from condor.file_utils import loadFiles
files, _ = loadFiles(
args.config,
args.configkey,
args.year,
args.pfnano,
args.sample.split(","),
)
# print(files)
if not files:
print("Did not find files.. Exiting.")
exit(1)
# build fileset with files to run per job
fileset = {}
starti = args.starti
job_name = "/" + str(starti * args.n)
if args.n != -1:
job_name += "-" + str(args.starti * args.n + args.n)
for sample, flist in files.items():
if args.sample:
if sample not in args.sample.split(","):
continue
if args.n != -1:
fileset[sample] = flist[args.starti * args.n : args.starti * args.n + args.n]
else:
fileset[sample] = flist
print(
len(list(fileset.keys())),
"Samples in fileset to be processed: ",
list(fileset.keys()),
)
print(fileset)
print(f"Number of files: {len(fileset[list(fileset.keys())[0]])}")
# define processor
year = args.year.replace("APV", "")
yearmod = ""
if "APV" in args.year:
yearmod = "APV"
if args.processor == "hww":
from boostedhiggs.hwwprocessor import HwwProcessor
p = HwwProcessor(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
#getLPweights=args.getLPweights,
#uselooselep=args.uselooselep,
#fakevalidation=args.fakevalidation,
output_location="./outfiles" + job_name,
)
elif args.processor == "vh":
from boostedhiggs.vhprocessor import vhProcessor
p = vhProcessor(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "ak4":
from boostedhiggs.vhprocessorAK4 import vhprocessorAK4
p = vhprocessorAK4(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "frclosure":
from boostedhiggs.fakeRateLooseButNotTightClosure import fakeRateLooseButNotTightClosure
p = fakeRateLooseButNotTightClosure(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "vhC":
from boostedhiggs.vhprocessorClosure import vhProcessorClosure
p = vhProcessorClosure(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "vhWH":
from boostedhiggs.vhprocessorWH import vhProcessorWH
p = vhProcessorWH(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "vhZH":
from boostedhiggs.vhprocessorZH import vhProcessorZH
p = vhProcessorZH(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "mini":
from boostedhiggs.vhprocessorMini import vhprocessorMini
p = vhprocessorMini(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "wjv":
from boostedhiggs.vhprocessorWJetCalibV import vhprocessorWJetCalibV
p = vhprocessorWJetCalibV(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "wj":
from boostedhiggs.vhprocessorWJetCalib import vhprocessorWJetCalib
p = vhprocessorWJetCalib(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "cutflow":
from boostedhiggs.cutflow import vhcutflow
p = vhcutflow(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "prompt":
from boostedhiggs.prompt import promptProcessor
p = promptProcessor(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "vhb":
from boostedhiggs.vhprocessor_b import vhprocessor_b
p = vhprocessor_b(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "frdy":
from boostedhiggs.fakeRateDYSF import fakeRateDYSF
p = fakeRateDYSF(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "fr":
from boostedhiggs.fakeRate import fakeRate
p = fakeRate(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "frloose":
from boostedhiggs.fakeRateLooseButNotTight import fakeRateLooseButNotTight
p = fakeRateLooseButNotTight(
year=year,
yearmod=yearmod,
channels=channels,
inference=args.inference,
systematics=args.systematics,
output_location="./outfiles" + job_name,
)
elif args.processor == "lumi":
from boostedhiggs.lumi_processor import LumiProcessor
p = LumiProcessor(year=args.year, yearmod=yearmod, output_location=f"./outfiles/{job_name}")
elif args.processor == "input":
# define processor
from boostedhiggs.inputprocessor import InputProcessor
assert args.inference is True, "enable --inference to run skimmer"
p = InputProcessor(year=args.year, output_location=f"./outfiles/{job_name}")
elif args.processor == "fakes":
# define processor
from boostedhiggs.fakesprocessor import FakesProcessor
p = FakesProcessor(year=year, yearmod=yearmod, output_location=f"./outfiles/{job_name}")
else:
from boostedhiggs.trigger_efficiencies_processor import (
TriggerEfficienciesProcessor,
)
p = TriggerEfficienciesProcessor(year=args.year)
tic = time.time()
if args.executor == "dask":
from coffea.nanoevents import NanoeventsSchemaPlugin
from distributed import Client
from lpcjobqueue import LPCCondorCluster
cluster = LPCCondorCluster(
ship_env=True,
transfer_input_files="boostedhiggs",
)
client = Client(cluster)
nanoevents_plugin = NanoeventsSchemaPlugin()
client.register_worker_plugin(nanoevents_plugin)
cluster.adapt(minimum=1, maximum=30)
print("Waiting for at least one worker")
client.wait_for_workers(1)
# does treereduction help?
executor = processor.DaskExecutor(status=True, client=client, treereduction=2)
else:
uproot.open.defaults["xrootd_handler"] = uproot.source.xrootd.MultithreadedXRootDSource
if args.executor == "futures":
executor = processor.FuturesExecutor(status=True)
else:
executor = processor.IterativeExecutor(status=True)
nanoevents.PFNanoAODSchema.mixins["SV"] = "PFCand"
run = processor.Runner(
executor=executor,
savemetrics=True,
schema=nanoevents.PFNanoAODSchema,
chunksize=args.chunksize,
)
out, metrics = run(fileset, "Events", processor_instance=p)
elapsed = time.time() - tic
print(f"Metrics: {metrics}")
print(f"Finished in {elapsed:.1f}s")
if args.processor == "input":
# merge parquet
data = pd.read_parquet(f"./outfiles/{job_name}/parquet")
data.to_parquet(f"./outfiles/{job_name}.parquet")
# remove unmerged parquet files
os.system("rm -rf ./outfiles/" + job_name)
else:
# dump to pickle
filehandler = open("./outfiles/" + job_name + ".pkl", "wb")
pkl.dump(out, filehandler)
filehandler.close()
# merge parquet
for ch in channels:
data = pd.read_parquet("./outfiles/" + job_name + ch + "/parquet")
data.to_parquet("./outfiles/" + job_name + "_" + ch + ".parquet")
# remove old parquet files
os.system("rm -rf ./outfiles/" + job_name + ch)
if __name__ == "__main__":
# e.g.
# noqa: run locally on lpc (hww mc) as: python run.py --year 2017 --processor hww --pfnano v2_2 --n 1 --starti 0 --config samples_inclusive.yaml --key mc
# noqa: run locally on lpc (hww trigger) as: python run.py --year 2017 --processor trigger --pfnano v2_2 --n 1 --starti 0 --sample GluGluHToWW_Pt-200ToInf_M-125 --local --channels ele --config samples_inclusive.yaml --key mc
# noqa: run locally on single file (hww): python run.py --year 2017 --processor hww --pfnano v2_2 --n 1 --starti 0 --sample GluGluHToWW_Pt-200ToInf_M-125 --local --channels ele --config samples_inclusive.yaml --key mc
# noqa LP: python run.py --year 2017 --processor hww --pfnano v2_2 --n 1 --starti 0 --sample GluGluHToWW_Pt-200ToInf_M-125 --local --channels ele,mu --config samples_inclusive.yaml --key mc --getLPweights --inference
# noqa Fakes: python run.py --year 2017 --processor fakes --pfnano v2_2 --n 1 --starti 0 --sample GluGluHToWW_Pt-200ToInf_M-125 --local --channels ele,mu --config samples_inclusive.yaml --key mc
parser = argparse.ArgumentParser()
parser.add_argument("--year", dest="year", default="2017", help="year", type=str)
parser.add_argument("--starti", dest="starti", default=0, help="start index of files", type=int)
parser.add_argument("--n", dest="n", default=-1, help="number of files to process", type=int)
parser.add_argument("--config", dest="config", default=None, help="path to datafiles", type=str)
parser.add_argument(
"--key",
dest="configkey",
default=None,
help="config key: [data, mc, ... ]",
type=str,
)
parser.add_argument("--sample", dest="sample", default=None, help="specify sample", type=str)
parser.add_argument("--processor", dest="processor", required=True, help="processor", type=str)
parser.add_argument(
"--chunksize",
dest="chunksize",
default=10000,
help="chunk size in processor",
type=int,
)
parser.add_argument("--channels", dest="channels", default=None, help="channels separated by commas")
parser.add_argument(
"--executor",
type=str,
default="futures",
choices=["futures", "iterative", "dask"],
help="type of processor executor",
)
parser.add_argument(
"--pfnano",
dest="pfnano",
type=str,
default="v2_2",
help="pfnano version",
)
parser.add_argument("--macos", dest="macos", action="store_true")
parser.add_argument("--local", dest="local", action="store_true")
parser.add_argument("--inference", dest="inference", action="store_true")
parser.add_argument("--no-inference", dest="inference", action="store_false")
parser.add_argument("--systematics", dest="systematics", action="store_true")
parser.add_argument("--no-systematics", dest="systematics", action="store_false")
parser.add_argument("--getLPweights", dest="getLPweights", action="store_true")
parser.add_argument("--no-getLPweights", dest="getLPweights", action="store_false")
parser.add_argument("--uselooselep", dest="uselooselep", action="store_true")
parser.add_argument("--no-uselooselep", dest="uselooselep", action="store_false")
# fakes
# parser.add_argument("--fakevalidation", dest="fakevalidation", action="store_true")
# parser.add_argument("--no-fakevalidation", dest="fakevalidation", action="store_false")
parser.set_defaults(inference=False)
args = parser.parse_args()
main(args)