-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_UL_campaign.py
executable file
·636 lines (584 loc) · 17.7 KB
/
create_UL_campaign.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env python
import argparse
import os
import stat
import yaml
import sys
if sys.version_info[0] == 2:
print("You need to run this with Python 3")
raise SystemExit
from scripts.EmbeddingTask import (
Preselection,
FullTask,
Nano,
aggregateMiniAOD,
PuppiOnMini,
)
from scripts.create_tmux_while import create_tmux_while
from scripts.filelist_generator import (
PreselectionFilelist,
FullFilelist,
NanoFilelist,
AggregatedMiniAODFilelist,
PuppiOnMiniFilelist,
)
import getpass
from rich.console import Console
console = Console()
def parse_arguments():
parser = argparse.ArgumentParser(
description="Setup Grid Control for Embedding Production"
)
parser.add_argument("--workdir", type=str, help="path to the workdir", default="")
parser.add_argument(
"--era",
type=str,
# choices=['2016_preVFP', '2016_postVFP', '2017', '2018'],
choices=["2016-HIPM", "2016", "2017", "2018"],
required=True,
help="Era used for the production",
)
parser.add_argument(
"--final-state",
type=str,
required=False,
choices=["MuTau", "ElTau", "ElMu", "TauTau", "MuEmb", "ElEmb"],
help="Name the final state you want to process",
)
parser.add_argument(
"--run",
type=str,
nargs="+",
help="Name or list of the runs you want to process, use all to process all runs of an era",
)
parser.add_argument(
"--mode",
type=str,
required=True,
choices=["preselection", "full", "aggregate", "rerunpuppi", "nanoaod"],
help="Select preselection mode, full embedding mode, aggregate mode, rerun puppi mode, or nanoaod mode",
)
parser.add_argument(
"--task",
type=str,
required=True,
choices=[
"setup_cmssw",
"upload_tarballs",
"setup_jobs",
"run_production",
"create_filelist",
"publish_dataset",
],
help="Different commands that are possible",
)
parser.add_argument(
"--backend",
type=str,
choices=["etp", "naf", "lxplus"],
default="etp",
help="Select the condor backend that is used.",
)
parser.add_argument(
"--custom-configdir",
type=str,
help="If this is set, use the configdir from the given folder",
)
parser.add_argument(
"--mc",
action="store_true",
help="If this is set, mc embedding is run instead of data embedding",
)
parser.add_argument(
"--no_tmux",
action="store_true",
help="If this is set, no tmux is used to run the jobs",
)
parser.add_argument(
"--user",
type=str,
default="",
help="if you have a different username on NAF and ETP you have to choose your ETP username here",
)
return parser.parse_args()
def possible_runs(era):
config = yaml.load(open("scripts/ul_config.yaml", "r"))
runlist = config["runlist"][era]
return runlist
def get_inputfolder(era):
if era == "2018":
return "Run2018_CMSSW_10_6_28_UL"
elif era == "2017":
return "Run2017_CMSSW_10_6_28_UL"
elif era == "2016":
return "Run2016_CMSSW_10_6_28_UL"
elif era == "2016-HIPM":
return "Run2016-HIPM_CMSSW_10_6_28_UL"
else:
raise ValueError("Era not supported")
class Task(object):
def __init__(
self,
era,
workdir,
configdir,
config,
backend,
run,
user,
no_tmux=False,
isMC=False,
):
self.era = era
self.workdir = workdir
self.config = config
self.configdir = configdir
self.isMC = isMC
self.runlist = self.validate_run(run)
self.gc_path = os.path.abspath("grid-control")
self.backend = backend
self.no_tmux = no_tmux
self.cmssw_versions = {
"main": self.config["cmssw_version"][self.era]["main"],
"hlt": self.config["cmssw_version"][self.era]["hlt"],
}
self.identifier = "data_{}".format(self.era)
if isMC:
self.identifier = "mc_{}".format(self.era)
if args.user:
self.username = args.user
else:
self.username = getpass.getuser()
def run_production(self):
console.rule(
"Running production for - {era} - {run}".format(
era=self.era, run=self.runlist
)
)
configlist = []
for run in self.runlist:
configlist.append(
"{name}/{run}.conf".format(name=self.task.get_name(), run=run)
)
if self.no_tmux:
console.rule("Using old script")
out_file = open("{name}/while.sh".format(name=self.task.get_name()), "w")
out_file.write("#!/bin/bash\n")
out_file.write("\n")
out_file.write("touch .lock\n")
out_file.write("\n")
out_file.write('while [ -f ".lock" ]\n')
out_file.write("do\n")
for config in configlist:
out_file.write(
"python2 {gc_path}/go.py {configpath} -G \n".format(
gc_path=self.gc_path, configpath=config
)
)
out_file.write('echo "rm .lock"\n')
out_file.write("sleep 2\n")
out_file.write("done\n")
out_file.close()
os.chmod("{name}/while.sh".format(name=self.task.get_name()), stat.S_IRWXU)
os.system("./{name}/while.sh".format(name=self.task.get_name()))
else:
console.rule("Using tmux version")
script = create_tmux_while(
taskname=self.task.get_name(),
configlist=configlist,
gc_path=self.gc_path,
tmux_path=self.config["tmux_path"],
)
os.system("./{}".format(script))
def setup_env(self):
console.rule("Setting up main CMSSW")
os.system(
"bash scripts/UL_checkouts/checkout_UL_{ERA}.sh {VERSION}".format(
ERA=self.era, VERSION=self.cmssw_versions["main"]
)
)
console.rule("Setting up HLT CMSSW ")
os.system(
"bash scripts/UL_checkouts/checkout_UL_{ERA}_HLT.sh {VERSION}".format(
ERA=self.era, VERSION=self.cmssw_versions["hlt"]
)
)
def validate_run(self, run):
runlist = []
if type(run) is not list:
run = [run]
if run == ["all"]:
runlist = self.config["runlist"][self.era]
else:
for single_run in run:
if single_run in self.config["runlist"][self.era]:
runlist.append(single_run)
else:
console.log(
"Run name unknown: Known runs are: {}".format(
self.config["runlist"][self.era]
)
)
exit()
return runlist
@classmethod
def build_filelist(self):
pass
@classmethod
def publish_dataset(self):
pass
@classmethod
def setup_cmsRun(self):
pass
@classmethod
def upload_tarballs(self):
pass
class PreselectionTask(Task):
def __init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
):
Task.__init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
)
self.task = Preselection(
era=self.era,
workdir=self.workdir,
identifier=self.identifier,
runs=self.runlist,
user=self.username,
inputfolder=get_inputfolder(era),
config=self.config,
isMC=self.isMC,
input_samples=self.config["input_samples"][self.era],
)
def build_filelist(self):
for run in self.runlist:
filelist = PreselectionFilelist(
configdir=self.configdir,
era=self.era,
grid_control_path=self.gc_path,
run=run,
finalstate="preselection",
isMC=self.isMC,
)
filelist.build_filelist()
def setup_cmsRun(self):
self.task.setup_all()
def upload_tarballs(self):
console.log("Not needed for preselection --> Exiting ")
def publish_dataset(self):
console.log("Ne need to publish preselection datasets --> Exiting")
class AggregateMiniAODTask(Task):
def __init__(
self,
era,
workdir,
configdir,
config,
backend,
run,
user,
finalstate,
no_tmux,
isMC,
):
Task.__init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
)
self.finalstate = finalstate
self.task = aggregateMiniAOD(
era=self.era,
finalstate=self.finalstate,
workdir=self.workdir,
identifier=self.identifier,
runs=self.runlist,
user=self.username,
inputfolder=get_inputfolder(era),
config=self.config,
isMC=self.isMC,
)
def build_filelist(self):
for run in self.runlist:
filelist = AggregatedMiniAODFilelist(
configdir=self.configdir,
era=self.era,
grid_control_path=self.gc_path,
run=run,
finalstate=self.finalstate,
isMC=self.isMC,
)
filelist.build_filelist()
def setup_cmsRun(self):
self.task.setup_all()
def upload_tarballs(self):
console.log("Not needed for preselection --> Exiting ")
def publish_dataset(self):
console.log("To be implemented ...")
class PuppiOnMiniTask(Task):
def __init__(
self,
era,
workdir,
configdir,
config,
backend,
run,
user,
finalstate,
no_tmux,
isMC,
):
Task.__init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
)
self.finalstate = finalstate
self.task = PuppiOnMini(
era=self.era,
finalstate=self.finalstate,
workdir=self.workdir,
identifier=self.identifier,
runs=self.runlist,
user=self.username,
inputfolder=get_inputfolder(era),
config=self.config,
isMC=self.isMC,
)
def build_filelist(self):
for run in self.runlist:
filelist = PuppiOnMiniFilelist(
configdir=self.configdir,
era=self.era,
grid_control_path=self.gc_path,
run=run,
finalstate=self.finalstate,
isMC=self.isMC,
)
filelist.build_filelist()
def setup_cmsRun(self):
self.task.setup_all()
def upload_tarballs(self):
console.log("Not needed for preselection --> Exiting ")
def publish_dataset(self):
console.log("To be implemented ...")
class NanoTask(Task):
def __init__(
self,
era,
workdir,
configdir,
config,
backend,
run,
user,
finalstate,
no_tmux,
isMC,
):
Task.__init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
)
self.finalstate = finalstate
self.task = Nano(
era=self.era,
finalstate=self.finalstate,
workdir=self.workdir,
identifier=self.identifier,
runs=self.runlist,
user=self.username,
inputfolder=get_inputfolder(era),
config=self.config,
isMC=self.isMC,
)
def build_filelist(self):
for run in self.runlist:
filelist = NanoFilelist(
configdir=self.configdir,
era=self.era,
grid_control_path=self.gc_path,
run=run,
finalstate=self.finalstate,
isMC=self.isMC,
)
filelist.build_filelist()
def setup_cmsRun(self):
self.task.setup_all()
def upload_tarballs(self):
console.log("Not needed for preselection --> Exiting ")
def publish_dataset(self):
console.log("To be implemented ...")
class EmbeddingTask(Task):
def __init__(
self,
era,
workdir,
configdir,
config,
backend,
run,
user,
finalstate,
no_tmux,
isMC,
):
Task.__init__(
self, era, workdir, configdir, config, backend, run, user, no_tmux, isMC
)
self.finalstate = finalstate
self.task = FullTask(
finalstate=self.finalstate,
era=self.era,
workdir=self.workdir,
identifier=self.identifier,
runs=self.runlist,
user=self.username,
inputfolder=get_inputfolder(era),
config=self.config,
isMC=self.isMC,
backend=self.backend,
)
def build_filelist(self):
for run in self.runlist:
filelist = FullFilelist(
configdir=self.configdir,
era=self.era,
grid_control_path=self.gc_path,
run=run,
finalstate=self.finalstate,
isMC=self.isMC,
)
filelist.build_filelist()
def publish_dataset(self):
console.log("Implementation to be done")
def setup_cmsRun(self):
self.task.setup_all()
def upload_tarballs(self):
console.rule("building tarball...")
for version in self.cmssw_versions.values():
outputfile = "cmssw_{VERSION}.tar.gz".format(VERSION=version)
cmd = "tar --dereference -czf {FILE} {CMSSW_FOLDER}/*".format(
FILE=outputfile,
CMSSW_FOLDER=version,
)
console.log(cmd)
os.system(cmd)
console.log("finished building tarball...")
console.log("upload tarball...")
cmd = "gfal-copy -f {outputfile} {tarballpath}/{TARBALLNAME}".format(
outputfile=outputfile,
tarballpath=config["output_paths"]["tarballs"].replace(
"{USER}", self.username
),
TARBALLNAME=outputfile,
)
os.system(cmd)
console.log(cmd)
console.rule("finished uploading tarball...")
if __name__ == "__main__":
args = parse_arguments()
config = yaml.safe_load(open("scripts/ul_config.yaml", "r"))
if args.final_state is None:
finalstate = "NotSet"
else:
finalstate = args.final_state
if args.custom_configdir:
configdir = args.custom_configdir
else:
configdir = os.path.dirname(os.path.realpath(__file__))
# first setup CMSSW environments
if args.mode == "preselection":
task = PreselectionTask(
args.era,
args.workdir,
configdir,
config,
args.backend,
args.run,
args.user,
args.no_tmux,
args.mc,
)
elif args.mode == "nanoaod":
task = NanoTask(
args.era,
args.workdir,
configdir,
config,
args.backend,
args.run,
args.user,
finalstate,
args.no_tmux,
args.mc,
)
elif args.mode == "aggregate":
task = AggregateMiniAODTask(
args.era,
args.workdir,
configdir,
config,
args.backend,
args.run,
args.user,
finalstate,
args.no_tmux,
args.mc,
)
elif args.mode == "rerunpuppi":
task = PuppiOnMiniTask(
args.era,
args.workdir,
configdir,
config,
args.backend,
args.run,
args.user,
finalstate,
args.no_tmux,
args.mc,
)
else:
task = EmbeddingTask(
args.era,
args.workdir,
configdir,
config,
args.backend,
args.run,
args.user,
finalstate,
args.no_tmux,
args.mc,
)
if args.task == "setup_cmssw":
task.setup_env()
elif args.task == "upload_tarballs":
task.upload_tarballs()
elif args.task == "run_production":
if args.final_state is None:
raise ValueError("Final state not specified")
if args.run is None:
raise ValueError("run not specified")
task.run_production()
elif args.task == "create_filelist":
if args.final_state is None:
raise ValueError("Final state not specified")
if args.run is None:
raise ValueError("run not specified")
task.build_filelist()
elif args.task == "setup_jobs":
if args.final_state is None:
raise ValueError("Final state not specified")
if args.run is None:
raise ValueError("run not specified")
if args.workdir == "":
console.log(
"No workdir is set, please specify the workdir using --workdir /path/to/workdir"
)
raise Exception
task.setup_cmsRun()
elif args.task == "publish_dataset":
if args.final_state is None:
raise ValueError("Final state not specified")
task.publish_dataset()
else:
exit()