-
Notifications
You must be signed in to change notification settings - Fork 7
/
coffea-adl-benchmarks.py
343 lines (287 loc) · 10.8 KB
/
coffea-adl-benchmarks.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
#!/usr/bin/env python
import os.path
import subprocess
import time
from itertools import product
import awkward as ak
import hist
import numpy as np
import pandas as pd
import psutil
import tqdm
from coffea import processor, nanoevents
# The opendata files are non-standard NanoAOD, so some optional data columns are missing
processor.NanoAODSchema.warn_missing_crossrefs = False
proc = psutil.Process()
def run(query, chunksize, workers, file):
if not file.startswith("/dev/shm"):
# https://stackoverflow.com/questions/9551838/how-to-purge-disk-i-o-caches-on-linux
try:
subprocess.run("sync", check=True)
subprocess.run(
["sudo", "bash", "-c", "echo 3 > /proc/sys/vm/drop_caches"], check=True
)
except PermissionError:
pass
tic = time.monotonic()
cputic = proc.cpu_times()
if workers > 1:
executor = processor.FuturesExecutor(workers=workers, status=False)
else:
executor = processor.IterativeExecutor(status=False)
runner = processor.Runner(
executor=executor,
schema=nanoevents.NanoAODSchema,
savemetrics=True,
chunksize=chunksize,
)
output, metrics = runner(
fileset={"SingleMu": [file]}, treename="Events", processor_instance=query(),
)
toc = time.monotonic()
cputoc = proc.cpu_times()
metrics["query"] = query.__name__
metrics["tgt_chunksize"] = chunksize
metrics["chunksize"] = metrics["entries"] / metrics["chunks"]
metrics["workers"] = workers
metrics["walltime"] = toc - tic
metrics["path"] = os.path.dirname(file)
metrics.update(
{
n: f - i
for n, f, i in zip(
"user system children_user children_system iowait".split(),
cputoc,
cputic,
)
}
)
return output, metrics
class Q1Processor(processor.ProcessorABC):
"""Plot the <i>E</i><sub>T</sub><sup>miss</sup> of all events."""
def process(self, events):
return (
hist.Hist.new.Reg(100, 0, 200, name="met", label="$E_{T}^{miss}$ [GeV]")
.Double()
.fill(events.MET.pt)
)
def postprocess(self, accumulator):
return accumulator
class Q2Processor(processor.ProcessorABC):
"""Plot the <i>p</i><sub>T</sub> of all jets."""
def process(self, events):
return (
hist.Hist.new.Reg(100, 0, 200, name="ptj", label="Jet $p_{T}$ [GeV]")
.Double()
.fill(ak.flatten(events.Jet.pt))
)
def postprocess(self, accumulator):
return accumulator
class Q2Kin2DProcessor(processor.ProcessorABC):
"""Plot the <i>p</i><sub>T</sub> of all jets."""
def process(self, events):
return (
hist.Hist.new.Reg(100, 0, 200, name="ptj", label="Jet $p_{T}$ [GeV]")
.Reg(100, -5, 5, name="etaj", label=r"Jet $\eta$")
.Double()
.fill(ak.flatten(events.Jet.pt), ak.flatten(events.Jet.eta))
)
def postprocess(self, accumulator):
return accumulator
class Q2Kin3DProcessor(processor.ProcessorABC):
"""Plot the <i>p</i><sub>T</sub> of all jets."""
def process(self, events):
return (
hist.Hist.new.Reg(100, 0, 200, name="ptj", label="Jet $p_{T}$ [GeV]")
.Reg(100, -5, 5, name="etaj", label=r"Jet $\eta$")
.Reg(100, -np.pi, np.pi, name="phij", label=r"Jet $\phi$")
.Double()
.fill(
ak.flatten(events.Jet.pt),
ak.flatten(events.Jet.eta),
ak.flatten(events.Jet.phi),
)
)
def postprocess(self, accumulator):
return accumulator
class Q3Processor(processor.ProcessorABC):
"""Plot the <i>p</i><sub>T</sub> of jets with |<i>η</i>| < 1."""
def process(self, events):
return (
hist.Hist.new.Reg(100, 0, 200, name="ptj", label="Jet $p_{T}$ [GeV]")
.Double()
.fill(ak.flatten(events.Jet[abs(events.Jet.eta) < 1].pt))
)
def postprocess(self, accumulator):
return accumulator
class Q4Processor(processor.ProcessorABC):
"""Plot the <i>E</i><sub>T</sub><sup>miss</sup> of events that have at least
two jets with <i>p</i><sub>T</sub> > 40 GeV.
"""
def process(self, events):
has2jets = ak.sum(events.Jet.pt > 40, axis=1) >= 2
return (
hist.Hist.new.Reg(100, 0, 200, name="met", label="$E_{T}^{miss}$ [GeV]")
.Double()
.fill(events[has2jets].MET.pt)
)
def postprocess(self, accumulator):
return accumulator
class Q5Processor(processor.ProcessorABC):
"""Plot the <i>E</i><sub>T</sub><sup>miss</sup> of events that have an
opposite-charge muon pair with an invariant mass between 60 and 120 GeV.
"""
def process(self, events):
mupair = ak.combinations(events.Muon, 2)
with np.errstate(invalid="ignore"):
pairmass = (mupair.slot0 + mupair.slot1).mass
goodevent = ak.any(
(pairmass > 60)
& (pairmass < 120)
& (mupair.slot0.charge == -mupair.slot1.charge),
axis=1,
)
return (
hist.Hist.new.Reg(100, 0, 200, name="met", label="$E_{T}^{miss}$ [GeV]")
.Double()
.fill(events[goodevent].MET.pt)
)
def postprocess(self, accumulator):
return accumulator
class Q6Processor(processor.ProcessorABC):
"""For events with at least three jets, plot the <i>p</i><sub>T</sub> of the trijet
four-momentum that has the invariant mass closest to 172.5 GeV in each event and
plot the maximum <i>b</i>-tagging discriminant value among the jets in this trijet.
"""
def process(self, events):
jets = ak.zip(
{k: getattr(events.Jet, k) for k in ["x", "y", "z", "t", "btag"]},
with_name="LorentzVector",
behavior=events.Jet.behavior,
)
trijet = ak.combinations(jets, 3, fields=["j1", "j2", "j3"])
trijet["p4"] = trijet.j1 + trijet.j2 + trijet.j3
trijet = ak.flatten(
trijet[ak.singletons(ak.argmin(abs(trijet.p4.mass - 172.5), axis=1))]
)
maxBtag = np.maximum(
trijet.j1.btag, np.maximum(trijet.j2.btag, trijet.j3.btag,),
)
return {
"trijetpt": hist.Hist.new.Reg(
100, 0, 200, name="pt3j", label="Trijet $p_{T}$ [GeV]"
)
.Double()
.fill(trijet.p4.pt),
"maxbtag": hist.Hist.new.Reg(
100, 0, 1, name="btag", label="Max jet b-tag score"
)
.Double()
.fill(maxBtag),
}
def postprocess(self, accumulator):
return accumulator
class Q7Processor(processor.ProcessorABC):
"""Plot the scalar sum in each event of the <i>p</i><sub>T</sub> of jets with
<i>p</i><sub>T</sub> > 30 GeV that are not within 0.4 in Δ<i>R</i> of any light
lepton with <i>p</i><sub>T</sub> > 10 GeV.
"""
def process(self, events):
cleanjets = events.Jet[
ak.all(
events.Jet.metric_table(events.Muon[events.Muon.pt > 10]) >= 0.4, axis=2
)
& ak.all(
events.Jet.metric_table(events.Electron[events.Electron.pt > 10])
>= 0.4,
axis=2,
)
& (events.Jet.pt > 30)
]
return (
hist.Hist.new.Reg(
100, 0, 200, name="sumjetpt", label=r"Jet $\sum p_{T}$ [GeV]"
)
.Double()
.fill(ak.sum(cleanjets.pt, axis=1))
)
def postprocess(self, accumulator):
return accumulator
class Q8Processor(processor.ProcessorABC):
"""For events with at least three light leptons and a same-flavor
opposite-charge light lepton pair, find such a pair that has the
invariant mass closest to 91.2 GeV in each event and plot the transverse
mass of the system consisting of the missing tranverse momentum and
the highest-<i>p</i><sub>T</sub> light lepton not in this pair.
"""
def process(self, events):
events["Electron", "pdgId"] = -11 * events.Electron.charge
events["Muon", "pdgId"] = -13 * events.Muon.charge
events["leptons"] = ak.concatenate([events.Electron, events.Muon], axis=1,)
events = events[ak.num(events.leptons) >= 3]
pair = ak.argcombinations(events.leptons, 2, fields=["l1", "l2"])
pair = pair[(events.leptons[pair.l1].pdgId == -events.leptons[pair.l2].pdgId)]
with np.errstate(invalid="ignore"):
pair = pair[
ak.singletons(
ak.argmin(
abs(
(events.leptons[pair.l1] + events.leptons[pair.l2]).mass
- 91.2
),
axis=1,
)
)
]
events = events[ak.num(pair) > 0]
pair = pair[ak.num(pair) > 0][:, 0]
l3 = ak.local_index(events.leptons)
l3 = l3[(l3 != pair.l1) & (l3 != pair.l2)]
l3 = l3[ak.argmax(events.leptons[l3].pt, axis=1, keepdims=True)]
l3 = events.leptons[l3][:, 0]
mt = np.sqrt(2 * l3.pt * events.MET.pt * (1 - np.cos(events.MET.delta_phi(l3))))
return (
hist.Hist.new.Reg(
100, 0, 200, name="mt", label=r"$\ell$-MET transverse mass [GeV]"
)
.Double()
.fill(mt)
)
def postprocess(self, accumulator):
return accumulator
if __name__ == "__main__":
queries = [
Q1Processor,
Q2Processor,
Q3Processor,
Q4Processor,
Q5Processor,
Q6Processor,
Q7Processor,
Q8Processor,
]
chunksizes = [2 ** 13, 2 ** 15, 2 ** 17, 2 ** 19, 2 ** 21]
ncores = [1, 3, 12, 24, 48]
files = [
"/dev/shm/Run2012B_SingleMu.root",
# "/ssd/Run2012B_SingleMu.root",
# "/magnetic/Run2012B_SingleMu.root",
]
benchpoints = list(product(queries, chunksizes, [24], files))
benchpoints += list(product(queries, [2 ** 19], ncores, files))
queries = [Q2Processor, Q2Kin2DProcessor, Q2Kin3DProcessor]
ncores = [12, 18, 24]
chunksizes = [2 ** 17, 2 ** 18, 2 ** 19]
benchpoints += list(product(queries, chunksizes, ncores, files))
benchpoints = list(set(benchpoints))
results = []
for query, chunksize, workers, file in tqdm.tqdm(benchpoints):
_, metrics = run(query, chunksize, workers, file)
del metrics["columns"]
results.append(metrics)
df = pd.DataFrame(results)
df["us*core/evt"] = df["walltime"] * 1e6 * df["workers"] / df["entries"]
df["b/evt"] = df["bytesread"] / df["entries"]
df["MB/s/core"] = df["bytesread"] * 1e-6 / df["workers"] / df["walltime"]
print(df)
df.to_pickle("results.pkl")