forked from cms-tau-pog/NanoProd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_missing_crabjobs_locally.py
385 lines (341 loc) · 11.9 KB
/
run_missing_crabjobs_locally.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
import os
import sys
import json
from tqdm import tqdm
from datetime import datetime
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from subprocess import call
import yaml
thisdir = os.path.realpath(os.path.dirname(__file__))
if not thisdir in sys.path:
sys.path.append(thisdir)
from wlcg_dbs_interface import WLCGInterface
# from RunKit.nanoProdWrapper import create_PSet
from RunKit.sh_tools import sh_call
interface = WLCGInterface()
verbosity = 0
def run_custom_nano_command(
input_file: str,
nevents: int=-1,
era: str="Run2_2017",
sampleType: str = "mc",
**kwargs,
):
# first build the cms PSet
# print(f"creating PSet.py for input '{input_file}'")
cmd = f"python3 {thisdir}/RunKit/nanoProdWrapper.py".split()
cmd += f"customise=NanoProd/NanoProd/customize.customize".split()
cmd += f"skimCfg={thisdir}/NanoProd/config/skim_uhh.yaml maxEvents={nevents}".split()
cmd += f"sampleType={sampleType} storeFailed=True era={era}".split()
cmd += f"inputFiles=file:./{input_file} writePSet=True skimSetup=skim".split()
cmd += f"skimSetupFailed=skim_failed createTar=False".split()
# from IPython import embed; embed()
sh_call([" ".join(cmd)], shell=True, catch_stdout=False, split='\n', env=interface.getCmsswEnv())
print("done writing PSet.py")
cmd=f'{thisdir}/RunKit/crabJob.sh'
print("executing job")
sh_call([cmd], shell=True, catch_stdout=False, split='\n', env=interface.getCmsswEnv())
def run_job(
lfn: str,
tmp_dir: str,
wlcg_path:str,
output_name:str,
fail_on_exception: bool=False,
**kwargs,
):
# cd into tmp dir. If it doesn't exist, create it
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
cwd = os.getcwd()
os.chdir(tmp_dir)
local_lfn_name = os.path.basename(lfn)
# copy lfn locally
if not os.path.exists(local_lfn_name):
interface.get_remote_file(
filepath=lfn,
target=local_lfn_name
)
if not os.path.exists(local_lfn_name):
msg = f"Unable to load '{lfn}'"
if fail_on_exception:
raise ValueError(msg)
else:
print(msg)
os.chdir(cwd)
return
final_output = os.path.abspath("nano_0.root")
# run the job with this LFN
run_custom_nano_command(
input_file=local_lfn_name,
**kwargs,
)
# copy the output to the (remote) WLCG site
# from IPython import embed; embed()
final_target = f"{wlcg_path}/{output_name}"
try:
interface.move_file_to_remote(
local_file=final_output,
target_file=final_target,
route_url=None
)
except Exception as e:
print(f"Problems when moving file '{final_output}' to '{final_target}'")
raise e
# change back to the original directory after we're done
os.chdir(cwd)
def build_wlcg_path(
wlcg_prefix: str,
wlcg_dir: str,
sample_name: str,
crab_dirname: str,
time_stamp: str,
job_output: str,
wlcg_template: str=os.path.join("{wlcg_prefix}{wlcg_dir}",
"{sample_name}",
"{crab_dirname}",
"{time_stamp}",
"{job_output}"
),
):
return wlcg_template.format(
wlcg_prefix=wlcg_prefix,
wlcg_dir=wlcg_dir,
sample_name=sample_name,
crab_dirname=crab_dirname,
time_stamp=time_stamp,
job_output=job_output,
)
def load_config_info(config, sample, key="sampleType"):
sampleType=None
# try to find information about the sample in the yaml config
sample_info = config.get(sample, dict())
sampleType = sample_info.get(key)
# if no sample specific information can be found, look for the general
# config section
if not sampleType:
general_config = config.get("config", dict())
sample_info = general_config.get("params", dict())
# try again
sampleType = sample_info.get(key)
if not sampleType:
raise ValueError(f"Could not load key '{key}' for sample '{sample}'")
return sampleType
def main(
*args,
wlcg_prefix: str,
wlcg_dir: str,
missing_files_json: str,
sample_config: str,
veto_dirs: list[str]=None,
tmp_dir: str="./tmp",
remote_dir_suffix: str="recovery_3",
**kwargs,
):
if not veto_dirs:
veto_dirs=list()
# load dictionary with missing lfns
missing_lfn_dict = dict()
with open(missing_files_json) as f:
missing_lfn_dict = json.load(f)
# filter out samples that are accounted for in the list of veto directories
missing_samples = list(filter(
lambda x: not any(dir.strip("/").endswith(x) for dir in veto_dirs),
missing_lfn_dict
))
# from IPython import embed; embed()
pbar_samples = tqdm(missing_samples)
# create a time stamp that mimics the crab format
timestamp = '{:%y%m%d_%H%M%S}'.format(datetime.now())
local_job_summary = dict()
with open(sample_config) as f:
sample_dict = yaml.load(f, yaml.Loader)
for sample in pbar_samples:
pbar_samples.set_description(f"Run missing jobs for sample '{sample}'")
sampleType = load_config_info(config=sample_dict, sample=sample, key="sampleType")
era = load_config_info(config=sample_dict, sample=sample, key="era")
# load das key for this sample
das_key = interface.load_das_key(
sample_name=sample,
sample_config=sample_config
)
# get campaign name
sample_campaign = interface.get_campaign_name(das_key=das_key)
# loop through the list of missing lfns
pbar_missing_lfns = tqdm(missing_lfn_dict[sample]["missing_lfns"])
missing_lfns = list()
final_remote_dir = (f"crab_{sample}_{remote_dir_suffix}"
if not remote_dir_suffix == ""
else f"crab_{sample}"
)
for i, lfn in enumerate(pbar_missing_lfns):
lfn_shortname = "/".join(lfn.split("/")[-3:])
fname = ".".join(os.path.basename(lfn).split(".")[:-1])
pbar_missing_lfns.set_description(f"Running LFN {lfn_shortname}")
blocknumber = int(i/10000)
wlcg_path = build_wlcg_path(
wlcg_prefix=wlcg_prefix,
wlcg_dir=wlcg_dir,
sample_name=sample_campaign,
crab_dirname=final_remote_dir,
time_stamp=timestamp,
job_output=f"{blocknumber:04d}"
)
run_job(
lfn=lfn,
tmp_dir=os.path.join(tmp_dir, sample, fname),
wlcg_path=wlcg_path,
output_name=f"nano_{i}.root",
sampleType=sampleType,
era=era,
)
missing_lfns.append(lfn)
local_job_summary[sample] = {
"timestamp": timestamp,
"remote_dir": final_remote_dir,
"lfns": missing_lfns,
}
with open("local_job_summary.json", "w") as f:
json.dump(local_job_summary, f, indent=4)
def parse_arguments():
description = """
Script to run missing crab jobs locally and move the to the target
(remote) site WLCG via gfal2.
This script requires two things:
- sample-config: path to the sample config that maps a sample name to
the corresponding DAS key for the miniAOD
- missing-files-json: path to .json file containing the missing LFNs.
The .json file with missing LFNs must have the following format:
{
"SAMPLE_NAME": {
"missing": LIST_OF_LFNS,
...
}
}
where 'SAMPLE_NAME' is also the key in the sample-config.
This format is e.g. automatically produced with `check_crab_jobs.py'
Please make sure to make gfal2 available to your python3 installation.
On lxplus, you can do this with
source "/cvmfs/grid.cern.ch/centos7-ui-160522/etc/profile.d/setup-c7-ui-python3-example.sh" ""
Note that this only works on SLC7 machines at the moment, not on CentOS 8.
"""
usage = "python %(prog)s [options] path/to/directories/containting/crab_base_dirs"
parser = ArgumentParser(
# usage=usage,
description=description,
formatter_class=RawDescriptionHelpFormatter)
parser.add_argument(
"-w", "--wlcg-dir",
help=" ".join("""
path to your WLCG directory that is the final destination for your
crab jobs. On T2_DESY, this would be your DCACHE (/pnfs) directory
""".split()),
metavar="PATH/TO/YOUR/WLCG/DIRECTORY",
type=str,
default=None,
required=True,
dest="wlcg_dir",
)
parser.add_argument(
"--wlcg-prefix",
help=" ".join(
"""
Prefix to contact the WLCG directory at the remote site.
Defaults to prefix for T2_DESY
(srm://dcache-se-cms.desy.de:8443/srm/managerv2?SFN=)
""".split()
),
type=str,
default="srm://dcache-se-cms.desy.de:8443/srm/managerv2?SFN=",
dest="wlcg_prefix"
)
parser.add_argument(
"-r", "--remote-dir-suffix",
help=" ".join(
"""
safe the output of the jobs you run locally in this
directory in the (remote) WLCG site. The path on the remote
site is formed as
'{wlcg-dir}/{sample_campaign}/crab_{sample_name}_{remote-dir-suffix}/{time-stamp}'.
Defaults to 'recovery_3'
""".split()
),
type=str,
default="recovery_3",
dest="remote_dir_suffix",
)
parser.add_argument(
"--sample-config", "-c",
help=" ".join(
"""
path to sample config that contains the information about the
original miniaod files (and thus the original name of the sample).
Config must be the in yaml format!
""".split()
),
default=None,
required=True,
dest="sample_config",
metavar="path/to/sample_config.yaml"
)
parser.add_argument(
"-j", "--json",
help=" ".join(
"""
file that contains the missing lfns
(see structure in description)
""".split()
),
dest="missing_files_json",
required=True,
metavar="path/to/summary.json",
type=str,
)
parser.add_argument(
"-v", "--verbosity",
type=int,
default=0,
help=" ".join(
"""
control the verbosity of the output. Currently implemented levels
0: just print number of files/outputs for each sample
1: actually print the paths for the different files
""".split()
)
)
parser.add_argument(
"-t", "--tmp-dir",
help=" ".join(
"""
directory to actually run the jobs in. Defaults to './tmp'
""".split()
),
type=str,
dest="tmp_dir",
metavar="path/to/temporary_directory",
default="./tmp"
)
parser.add_argument(
"--veto-dirs",
help=" ".join(
"""
veto samples in the missing files json that are accounted
for in this list.
""".split()
),
metavar="path/to/crab_dirs",
nargs="+",
required=False,
dest="veto_dirs"
)
args = parser.parse_args()
# from IPython import embed; embed()
if not os.path.exists(args.sample_config):
parser.error(f"file {args.sample_config} does not exist!")
if not os.path.exists(args.missing_files_json):
parser.error(f"file {args.missing_files_json} does not exist!")
global verbosity
verbosity = args.verbosity
return args
if __name__ == '__main__':
args = parse_arguments()
main(**vars(args))