forked from elaird/hcalraw
-
Notifications
You must be signed in to change notification settings - Fork 2
/
processUSC.py
executable file
·335 lines (262 loc) · 9.63 KB
/
processUSC.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
#!/usr/bin/env python2
import os
import sys
import utils
def commandOutput(command):
d = utils.commandOutputFull(command)
d["command"] = command
return d
def stdout(cmd="", checkErr=True):
out = commandOutput(cmd)
if checkErr:
assert not out["stderr"], "%s \n\n %s" % (cmd, out["stderr"])
return out["stdout"].split("\n")
def blob(d={}, newlines=["&&", "|&", ">&"]):
def s(x):
o = str(x)
for item in newlines:
o = o.replace(item, "%s\n" % item)
return o
lines = []
for key, value in sorted(d.iteritems()):
lines += ["* %s *" % key, "", s(value), "", ""]
return "\n".join(lines)
def prepareRunList(dest="", hcalRuns=""):
dirName = os.path.dirname(dest)
if not os.path.exists(dirName):
print "Creating %s" % dirName
os.makedirs(dirName)
if os.path.exists(dest):
try:
os.remove(dest)
except OSError, e:
assert e.errno == 2
stdout("wget -O %s %s" % (dest, hcalRuns), checkErr=False)
def makeDir(baseDir="", run=0):
out = "%s/%d" % (baseDir, run)
if not os.path.exists(out):
os.mkdir(out)
return out
def flags(runDir="", run=0, suffix="", dependsUpon=[]):
def proc(s):
return "%s/processing.%s" % (runDir, s)
def done(s):
return "%s/.processed.%s" % (runDir, s)
yes = (True, proc(suffix), done(suffix))
no = (False, "", "")
notReady = filter(lambda x: not os.path.exists(done(x)), dependsUpon)
if os.path.exists(proc(suffix)):
print "Run %d (%s) is being processed already." % (run, suffix)
return no
elif notReady:
print "These dependencies are not ready:", notReady
return no
elif os.path.exists(done(suffix)):
return no
else:
return yes
def selectedRuns(select=lambda x: False, runListFile=""):
f = open(runListFile)
out = []
for line in f:
fields = line.split()
if len(fields) != 4:
continue
if select(fields[3]):
try:
run = int(fields[0])
out.append(run)
except ValueError:
continue
f.close()
return out
def goodRun(rootFile=""):
if not rootFile:
return
try:
return int(rootFile[-11:-5])
except ValueError:
print "Could not determine run number from file named '%s'." % rootFile
return
def env():
l = ["cd ~%s/public/hcalraw_pro" % os.environ["USER"]]
if "cern.ch" in os.environ.get("HOSTNAME", ""):
l.append("source env/lxplus6.sh")
return l
def gitLog(inputFile="", outputDir="", run=0):
cmds = ["git log | grep %s | head -1" % s for s in ["commit", "Date"]]
return commandOutput(" && ".join(env() + cmds))
def oneRun(args=[], outputFile="", suppress=["Xrd", "nologin"]):
out = " && ".join(env() + ["./oneRun.py "])
out += " ".join(args)
for item in suppress:
out += " |& grep -v %s" % item
out += " >& %s" % outputFile
return out
def dumpFibering(inputFile="", outputDir="", run=0):
outputFile = "%s/cabled.txt" % outputDir
args = ["--file1='%s'" % inputFile,
"--feds1=uHCAL",
"--plugins=patterns",
]
return commandOutput(oneRun(args, outputFile))
def compareFibering_vme(inputFile="", outputDir="", run=0):
return compareFibering(inputFile, outputDir, run, "data/ref_vme_G.txt")
def compareFibering_utca(inputFile="", outputDir="", run=0):
return compareFibering(inputFile, outputDir, run, "data/ref_utca_G.txt")
def compareFibering(inputFile="", outputDir="", run=0, ref=""):
assert ref
summary = ref.replace("data/ref_", "summary_").replace("_G", "")
cmd = "cat %s/cabled.txt | ./diff.py %s > %s/%s" % (outputDir, ref,
outputDir, summary)
return commandOutput(" && ".join(env() + [cmd]))
def utcaArgs(inputFile=""):
return ["--file1='%s'" % inputFile,
"--feds1=929 --feds2=721",
"--identity-map --no-warn-skip16"]
def dumpEventsNoColor(**kargs):
kargs["color"] = False
return dumpEvents(**kargs)
def dumpEvents(inputFile="", outputDir="", run=0, n=3, color=True):
args = utcaArgs(inputFile) + ["--dump=4 --nevents=%d --usc-summer2014" % n]
outputFile = "%s/%devents.txt" % (outputDir, n)
if color:
outputFile = outputFile.replace(".txt", "_color.txt")
else:
args.append("--no-color")
cmd = oneRun(args, outputFile=outputFile)
return commandOutput(cmd)
def compare(inputFile="", outputDir="", run=0, stem="compare", moreArgs=[]):
args = utcaArgs(inputFile)
args += moreArgs
args += ["--nevents=1000",
"--no-color",
"--output-file=%s/%s.root" % (outputDir, stem),
]
cmd = oneRun(args, outputFile="%s/%s.txt" % (outputDir, stem))
return commandOutput(cmd)
def report(d={}, subject=""):
print subject
if d.get("stderr") or d.get("returncode"):
if os.path.exists("/bin/mail"):
cmd = "echo '%s' |& mail -s '%s' %s" % (blob(d),
subject,
os.environ["USER"])
os.system(cmd)
else:
print blob(d)
def runs(runListFile="",
minimumRun=None,
maximumRun=None,
select=lambda x: False,
):
assert runListFile
runs = filter(lambda x: minimumRun <= x, selectedRuns(select, runListFile))
if maximumRun:
runs = filter(lambda x: x <= maximumRun, runs)
return runs
def go(baseDir="",
runs=[],
process=lambda inputFile, outputDir, run: {},
dependsUpon=[],
urlPrefix="http://cmsdoc.cern.ch",
jobCheck="oneRun",
nProcMax=5,
debug=False,
):
not_found = []
for run in runs:
processes = stdout("ps -ef | grep %s | grep %s" % (os.environ["USER"], jobCheck))
if nProcMax < len(processes):
msg = "Already %d processes:" % len(processes)
msg += "\n".join(processes)
sys.exit(msg)
runDir = makeDir(baseDir, run)
suffix = process.__name__
ready, procFlag, doneFlag = flags(runDir=runDir,
run=run,
suffix=suffix,
dependsUpon=dependsUpon)
if debug:
print run, ready, procFlag, doneFlag, process
if ready:
fileName = "/store/group/dpg_hcal/comm_hcal/USC/USC_%d.root" % run
if utils.commandOutputFull("eos stat %s" % fileName)["returncode"]:
not_found.append(run)
continue
else:
fileName = "root://eoscms.cern.ch/%s" % fileName
stdout("touch %s" % procFlag)
d = process(inputFile=fileName,
outputDir=runDir,
run=run)
stdout("rm %s" % procFlag)
urlSuffix = runDir[7+runDir.find("public/"):]
url = "%s/~%s/%s" % (urlPrefix, os.environ["USER"], urlSuffix)
report(d, subject='Run %d: %s (%s)' % (run, suffix, url))
if not d["returncode"]:
with open(doneFlag, "w") as f:
print >> f, blob(d)
if not_found:
print "Runs not found in EOS:"
print not_found
def extraRuns(fileName=""):
out = []
if not os.path.exists(fileName):
return out
f = open(fileName)
for iLine, line in enumerate(f):
if line and line[0] == "#":
continue
fields = line.split()
if len(fields) != 1:
print "len(fields) %d != 1 in line %d of '%s'" % (len(fields), 1 + iLine, fileName)
continue
try:
out.append(int(fields[0]))
except ValueError:
print "Could not determine run number in line %d of '%s'." % (1 + iLine, fileName)
continue
f.close()
return out
if __name__ == "__main__":
runListFile = "%s/public/html/runlist.txt" % os.environ["HOME"]
prepareRunList(dest=runListFile,
hcalRuns="http://cmshcalweb01.cern.ch/HCALruns.txt",
)
fiberIdRuns = runs(runListFile=runListFile,
minimumRun=236631,
select=lambda x: "FiberID" in x,
) + extraRuns("%s/public/FiberID/extraruns.txt" % os.environ["HOME"])
for func, deps in [(dumpFibering, []),
(compareFibering_vme, ["dumpFibering"]),
(compareFibering_utca, ["dumpFibering"]),
]:
go(baseDir="%s/public/FiberID" % os.environ["HOME"],
process=func,
dependsUpon=deps,
runs=fiberIdRuns + [],
)
sys.exit()
utcaRuns = runs(runListFile=runListFile,
minimumRun=219866,
select=lambda x: ("/HF/" in x) and ("no_utca" not in x) and ("SelfTrigger" not in x),
)
utcaDir = "%s/public/uTCA" % os.environ["HOME"]
for func in [#gitLog,
dumpEvents,
dumpEventsNoColor,
compare,
]:
go(baseDir=utcaDir,
process=func,
runs=utcaRuns+[],
)
# summary pdf
def pdf(**_):
inFiles = sorted(stdout("cd %s; find | grep compare.pdf" % utcaDir))
cmd = "cd %s" % utcaDir
cmd += " && gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=%s/all.pdf " % utcaDir
cmd += " ".join(inFiles)
return commandOutput(cmd)
go(baseDir=utcaDir, process=pdf, runs=utcaRuns[-1:])