-
Notifications
You must be signed in to change notification settings - Fork 0
/
module3_worker.py
executable file
·155 lines (121 loc) · 5.19 KB
/
module3_worker.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
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import random
import shutil
from subprocess import call
import sys
import time
import pandas as pd
from zeroworker import LockfileListReader, LockfileListWriter
# 250MB:
# TESTFILE = '/global/cfs/cdirs/dune/www/data/Module3/packet/ramp_up/self_trigger_adc_calo_target-50-packet-2023_02_01_05_15_CET.h5'
# 1.4GB:
# TESTFILE = '/global/cfs/cdirs/dune/www/data/Module3/packet/ramp_up/self_trigger_adc_calo_target-50-packet-2023_02_03_08_48_CET.h5'
# We get 129GB out! keep-vwfms?!?
# 5.5GB
TESTFILE = '/global/cfs/cdirs/dune/www/data/Module3/run2/packet/tpc12/tpc_12-packet-2023_02_22_13_26_CET.h5'
class Module3Worker:
BASEDIR = Path('/global/cfs/cdirs/dune/www/data/Module3/run3')
def __init__(self, config='module3', runlist_path='ndlar_flow/runlist.txt'):
self.config = config
self.runlist = self.read_runlist(runlist_path)
# self.lightdict = self.read_lightdict()
@staticmethod
def read_runlist(path: str) -> pd.DataFrame:
return pd.read_csv(path, sep=r'\s+')
# @classmethod
# def read_lightdict(cls) -> dict[str, Path]:
# result = {}
# lightbase = cls.BASEDIR.joinpath('LRS')
# for path in lightbase.rglob('*.data'):
# result[path.name] = path
# return result
def find_lightfiles(self, packet_path: Path) -> Path:
row = self.runlist.query(f'charge_filename == "{packet_path}"').iloc[0]
return row['light_filename'].split(',')
# rawname = self.get_rawname(packet_path)
# row = self.runlist.query(f'charge_filename == "{rawname}"').iloc[0]
# return self.lightdict[row['light_filename']]
@staticmethod
def get_outname(packet_path: Path) -> str:
return packet_path.name.replace('-packet-', '-reco-')
# @staticmethod
# def get_rawname(packet_path: Path) -> str:
# return packet_path.name.replace('-packet-', '-binary-')
@classmethod
def get_outpath(cls, packet_path: Path) -> Path:
# ramp_up
reldir = packet_path.parent.relative_to(cls.BASEDIR.joinpath('packet'))
# /global/cfs/cdirs/dune/www/data/Module3/reco/ramp_up
outdir = cls.BASEDIR.joinpath('reco', reldir)
# outdir.mkdir(parents=True, exist_ok=True)
outname = cls.get_outname(packet_path)
# /global/cfs/cdirs/dune/www/data/Module3/reco/ramp_up/self_trigger_adc_calo_target-50-reco-2023_02_01_05_15_CET.h5
return outdir.joinpath(outname)
@classmethod
def get_tmppath(cls, packet_path: Path) -> Path:
# UNCOMMENT BELOW to use PSCRATCH
# tmpdir = Path(os.getenv('SCRATCH')).joinpath('calibizer')
# COMMENT BELOW to use PSCRATCH
reldir = packet_path.parent.relative_to(cls.BASEDIR.joinpath('packet'))
tmpdir = cls.BASEDIR.joinpath('reco.tmp', reldir)
tmpdir.mkdir(parents=True, exist_ok=True)
# /pscratch/sd/m/mkramer/self_trigger_adc_calo_target-50-reco-2023_02_01_05_15_CET.h5
outname = cls.get_outname(packet_path)
tmppath = tmpdir.joinpath(outname)
return tmppath
def process(self, packet_path):
# /global/cfs/cdirs/dune/www/data/Module3/packet/ramp_up/self_trigger_adc_calo_target-50-packet-2023_02_01_05_15_CET.h5
packet_path = Path(packet_path)
assert packet_path.exists()
assert packet_path.is_relative_to(self.BASEDIR.joinpath('packet'))
assert packet_path.name.find('-packet-') != -1
lightpathlist = self.find_lightfiles(packet_path)
# assert all(Path(p).exists() for p in lightpathlist)
lightpaths = ' '.join(lightpathlist)
outpath = self.get_outpath(packet_path)
tmppath = self.get_tmppath(packet_path)
tmppath.unlink(missing_ok=True)
cmd = f'time ./run_module0_flow-{self.config}.sh {tmppath} {packet_path} {lightpaths}'
# print(cmd + '\n')
# return # XXX
retcode = call(cmd, shell=True)
if retcode == 0:
outpath.parent.mkdir(parents=True, exist_ok=True)
shutil.move(tmppath, outpath)
return retcode
def test():
mw = Module3Worker()
mw.process(TESTFILE)
def main():
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
ap = argparse.ArgumentParser()
ap.add_argument('infile')
ap.add_argument('-c', '--config', default='module3')
ap.add_argument('--immortal', action='store_true')
ap.add_argument('--singleshot', action='store_true')
ap.add_argument('--randsleep', type=int)
args = ap.parse_args()
if args.randsleep:
time.sleep(random.random() * args.randsleep)
reader = LockfileListReader(args.infile)
logger = LockfileListWriter(args.infile+'.done')
mw = Module3Worker(config=args.config)
with logger:
while True:
try:
path = next(reader)
retcode = mw.process(path)
logger.log(f'{path} {retcode}')
if args.singleshot:
break
except StopIteration:
if args.immortal:
time.sleep(60)
else:
break
if __name__ == '__main__':
main()