forked from legend-exp/legend-dataflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
381 lines (325 loc) · 14.4 KB
/
Snakefile
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
import pathlib, os, json, sys
import scripts as ds
from scripts.util.patterns import *
# Set with `snakemake --configfile=/path/to/your/config.json`
# configfile: "have/to/specify/path/to/your/config.json"
subst_vars_in_snakemake_config(workflow, config)
setup = config["setups"]["l200"]
configs = config_path(setup)
chan_maps = chan_map_path(setup)
swenv = runcmd(setup)
basedir = workflow.basedir
localrules: do_nothing, autogen_keylist, gen_filelist, autogen_output
rule do_nothing:
input:
onstart:
print("Starting workflow")
shell(f'rm {pars_path(setup)}/key_resolve.jsonl || true')
ds.pars_key_resolve.write_par_catalog(setup,['-*-*-*-cal'],os.path.join(pars_path(setup),'key_resolve.jsonl'))
onsuccess:
print("Workflow finished, no error")
shell("rm *.gen || true")
ds.check_log_files(log_path(setup) , "summary.log")
#shell(f'rm {filelist_path(setup)}/* || true')
#Placeholder, can email or maybe put message in slack
onerror:
print("An error occurred :( ")
# Auto-generate "all[-{detector}[-{measurement}[-{run}[-{timestamp}]]]].keylist"
# based on available tier0 files.
rule autogen_keylist:
input:
configs
output:
temp(os.path.join(filelist_path(setup),"all{keypart}.filekeylist"))
params:
setup = lambda wildcards: setup
script:
"scripts/create_keylist.py"
rule autogen_daqkeylist:
output:
temp(os.path.join(filelist_path(setup),"all{keypart}.daqkeylist"))
params:
setup = lambda wildcards: setup
script:
"scripts/create_daqlist.py"
rule build_channel_keylist:
params:
timestamp = "{timestamp}",
datatype = "cal"
output:
temp(os.path.join(filelist_path(setup),"all-{experiment}-{period}-{run}-cal-{timestamp}-channels.chankeylist"))
shell:
"{swenv} python3 -B {basedir}/scripts/create_chankeylist.py --configs {configs} --timestamp {params.timestamp} --datatype {params.datatype} --output_file {output} "
def get_keylist_file(wildcards):
if wildcards.tier =="daq" and wildcards.extension=="file":
return os.path.join(filelist_path(setup),"{label}.daqkeylist")
else:
return os.path.join(filelist_path(setup),"{label}.{extension}keylist")
checkpoint gen_filelist:
input:
get_keylist_file
output:
os.path.join(filelist_path(setup),"{label}-{tier}.{extension}list")
params:
setup = lambda wildcards: setup
script:
"scripts/create_{wildcards.extension}list.py"
def read_filelist(wildcards):
with checkpoints.gen_filelist.get(label=wildcards.label, tier=wildcards.tier, extension="file").output[0].open() as f:
files = f.read().splitlines()
return files
# Create "{label}-{tier}.gen", based on "{label}.keylist" via
# "{label}-{tier}.filelist". Will implicitly trigger creation of all files
# in "{label}-{tier}.filelist".
# Example: "all[-{detector}[-{measurement}[-{run}[-{timestamp}]]]]-{tier}.gen":
rule autogen_output:
input:
read_filelist
output:
"{label}-{tier}.gen"
run:
pathlib.Path(output[0]).touch()
rule sort_data:
input:
get_pattern_unsorted_data(setup)
output:
get_pattern_tier_daq(setup)
shell:
"if [ $USER = 'test' ]; then mv {input} {output}; else echo 'action not allowed for $USER' ;fi"
rule build_raw:
input:
get_pattern_tier_daq(setup)
params:
timestamp = "{timestamp}",
datatype = "{datatype}"
output:
get_pattern_tier_raw(setup)
log:
get_pattern_log(setup, "tier_raw")
group: "tier-raw"
resources:
mem_swap=110,
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/build_raw.py --log {log} --configs {configs} --chan_maps {chan_maps} --datatype {params.datatype} --timestamp {params.timestamp} {input} {output}"
#This rule builds the tcm files each raw file
rule build_tier_tcm:
input:
get_pattern_tier_raw(setup)
output:
get_pattern_tier_tcm(setup)
log:
get_pattern_log(setup, "tier_tcm")
group: "tier-tcm"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/build_tcm.py --log {log} --configs {configs} {input} {output}"
def read_filelist_raw_cal_channel(wildcards):
label = f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal"
with checkpoints.gen_filelist.get(label=label, tier="raw", extension="file").output[0].open() as f:
files = f.read().splitlines()
return files
rule build_pars_dsp_tau:
input:
files = read_filelist_raw_cal_channel
params:
timestamp = "{timestamp}",
datatype = "cal",
channel = "{channel}"
output:
decay_const = temp(get_pattern_pars_tmp_channel(setup, "dsp", "decay_constant")),
plots = temp(get_pattern_plts_tmp_channel(setup, "dsp","decay_constant"))
log:
get_pattern_log_channel(setup, "par_dsp_decay_constant")
group: "par-dsp"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/pars_dsp_tau.py --configs {configs} --log {log} --datatype {params.datatype} --timestamp {params.timestamp} --channel {params.channel} --plot_path {output.plots} --output_file {output.decay_const} {input.files} "
#This rule builds the optimal energy filter parameters for the dsp using calibration dsp files
rule build_pars_dsp_eopt:
input:
files = os.path.join(filelist_path(setup),"all-{experiment}-{period}-{run}-cal-raw.filelist"),
decay_const = get_pattern_pars_tmp_channel(setup, "dsp", "decay_constant"),
inplots = get_pattern_plts_tmp_channel(setup, "dsp","decay_constant")
params:
timestamp = "{timestamp}",
datatype = "cal",
channel = "{channel}"
output:
dsp_pars = temp(get_pattern_pars_tmp_channel(setup, "dsp")),
qbb_grid = temp(get_pattern_pars_tmp_channel(setup, "dsp", "energy_grid")),
plots = temp(get_pattern_plts_tmp_channel(setup, "dsp"))
log:
get_pattern_log_channel(setup, "pars_dsp_eopt")
group: "par-dsp"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/pars_dsp_eopt.py --log {log} --configs {configs} --datatype {params.datatype} --timestamp {params.timestamp} --channel {params.channel} --raw_filelist {input.files} --inplots {input.inplots} --decay_const {input.decay_const} --plot_path {output.plots} --qbb_grid_path {output.qbb_grid} --final_dsp_pars {output.dsp_pars}" # {input.peak_files}
def read_filelist_pars_dsp_cal_channel(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="dsp", extension="chan").output[0].open() as f:
files = f.read().splitlines()
return files
def read_filelist_plts_dsp_cal_channel(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="dsp", extension="chan").output[0].open() as f:
files = f.read().splitlines()
files = [file.replace("par", "plt").replace("json", "pkl") for file in files]
return files
def read_filelist_pars_dsp_cal_channel_results(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="dsp_energy_grid", extension="chan").output[0].open() as f:
files = f.read().splitlines()
return files
rule build_pars_dsp:
input:
read_filelist_pars_dsp_cal_channel,
read_filelist_pars_dsp_cal_channel_results,
read_filelist_plts_dsp_cal_channel
output:
get_pattern_par_dsp(setup),
get_pattern_par_dsp(setup, name="energy_grid"),
get_pattern_plts(setup, "dsp")
group: "merge-dsp"
shell:
"{swenv} python3 -B {basedir}/scripts/merge_channels.py --input {input} --output {output}"
def get_pars_dsp_file(wildcards):
"""
This function will get the pars file for the run checking the pars_overwrite
"""
out = ds.pars_catalog.get_par_file(setup, wildcards.timestamp, "dsp")
return out
rule build_dsp:
input:
raw_file = get_pattern_tier_raw(setup),
tcm_file = get_pattern_tier_tcm(setup),
pars_file = ancient(get_pars_dsp_file)
params:
timestamp = "{timestamp}",
datatype = "{datatype}"
output:
get_pattern_tier_dsp(setup)
log:
get_pattern_log(setup, "tier_dsp")
group: "tier-dsp"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/build_dsp.py --log {log} --configs {configs} --pars_file {input.pars_file} --datatype {params.datatype} --timestamp {params.timestamp} --input {input.raw_file} --output {output}"
def read_filelist_dsp_cal(wildcards):
label = f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal"
with checkpoints.gen_filelist.get(label=label, tier="dsp", extension="file").output[0].open() as f:
files = f.read().splitlines()
return files
#This rule builds the energy calibration using the calibration dsp files
rule build_energy_calibration:
input:
files = read_filelist_dsp_cal,
ctc_dict = ancient(get_pars_dsp_file)
params:
timestamp = "{timestamp}",
datatype = "cal",
channel = "{channel}"
output:
ecal_file = temp(get_pattern_pars_tmp_channel(setup, "hit", "energy_cal")),
results_file = temp(get_pattern_pars_tmp_channel(setup, "hit", "energy_cal_results")),
plot_file = temp(get_pattern_plts_tmp_channel(setup, "hit","energy_cal"))
log:
get_pattern_log_channel(setup, "pars_hit_energy_cal")
group: "par-hit"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/pars_hit_ecal.py --log {log} --datatype {params.datatype} --timestamp {params.timestamp} --channel {params.channel} --configs {configs} --plot_path {output.plot_file} --save_path {output.ecal_file} --ctc_dict {input.ctc_dict} --results_path {output.results_file} --files {input.files}" #
#This rule builds the a/e calibration using the calibration dsp files
rule build_aoe_calibration:
input:
files = os.path.join(filelist_path(setup),"all-{experiment}-{period}-{run}-cal-dsp.filelist"),
ecal_file = get_pattern_pars_tmp_channel(setup, "hit", "energy_cal"),
eres_file = get_pattern_pars_tmp_channel(setup, "hit", "energy_cal_results"),
inplots = get_pattern_plts_tmp_channel(setup, "hit","energy_cal")
params:
timestamp = "{timestamp}",
datatype = "cal",
channel = "{channel}"
output:
hit_pars = temp(get_pattern_pars_tmp_channel(setup, "hit")),
aoe_results = temp(get_pattern_pars_tmp_channel(setup, "hit", "results")),
plot_file = temp(get_pattern_plts_tmp_channel(setup, "hit"))
log:
get_pattern_log_channel(setup, "pars_hit_aoe_cal")
group: "par-hit"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/pars_hit_aoe.py --log {log} --configs {configs} --datatype {params.datatype} --timestamp {params.timestamp} --inplots {input.inplots} --channel {params.channel} --aoe_results {output.aoe_results} --hit_pars {output.hit_pars} --plot_file {output.plot_file} --eres_file {input.eres_file} --ecal_file {input.ecal_file} {input.files}"
def read_filelist_pars_hit_cal_channel(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="hit", extension="chan").output[0].open() as f:
files = f.read().splitlines()
return files
def read_filelist_plts_hit_cal_channel(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="hit", extension="chan").output[0].open() as f:
files = f.read().splitlines()
files = [file.replace("par", "plt").replace("json", "pkl") for file in files]
return files
def read_filelist_pars_hit_cal_channel_results(wildcards):
"""
This function will read the filelist of the channels and return a list of dsp files one for each channel
"""
label=f"all-{wildcards.experiment}-{wildcards.period}-{wildcards.run}-cal-{wildcards.timestamp}-channels"
with checkpoints.gen_filelist.get(label=label, tier="hit_results", extension="chan").output[0].open() as f:
files = f.read().splitlines()
return files
checkpoint build_pars_hit:
input:
read_filelist_pars_hit_cal_channel,
read_filelist_pars_hit_cal_channel_results,
read_filelist_plts_hit_cal_channel
output:
get_pattern_par_hit(setup),
get_pattern_par_hit(setup, name="results"),
get_pattern_plts(setup, "hit")
group: "merge-hit"
shell:
"{swenv} python3 -B {basedir}/scripts/merge_channels.py --input {input} --output {output}"
def get_pars_hit_file(wildcards):
"""
This function will get the pars file for the run checking the pars_overwrite
"""
return ds.pars_catalog.get_par_file(setup, wildcards.timestamp, "hit")
rule build_hit:
input:
dsp_file = get_pattern_tier_dsp(setup),
pars_file = get_pars_hit_file
output:
get_pattern_tier_hit(setup)
params:
timestamp = "{timestamp}",
datatype = "{datatype}"
log:
get_pattern_log(setup, "tier_hit")
group: "tier-hit"
resources:
runtime=300
shell:
"{swenv} python3 -B {basedir}/scripts/build_hit.py --configs {configs} --log {log} --datatype {params.datatype} --timestamp {params.timestamp} --pars_file {input.pars_file} --output {output} --input {input.dsp_file} "