-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstability-simulator.py
executable file
·386 lines (316 loc) · 11.9 KB
/
stability-simulator.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
386
#!/usr/bin/env python3
"""
Luminosity Stability Simulator
==============================
Run toy experiments to simulate the "luminosity stability" problem. Any
luminosity algorithm may in principle drift over time with respect to the "true"
luminosity, in addition to having its own run-to-run statistical fluctuations.
This script simulates these luminosity algorithms under a variety of scenarios
to study how best to extract a stability uncertainty.
Author: Joey Carter <[email protected]>
"""
from __future__ import absolute_import, division, print_function
import argparse
import os
import sys
import numpy as np
import pandas as pd
import ROOT as root
import atlasplots as aplt
def _docstring(docstring):
"""Return summary of docstring"""
return " ".join(docstring.split("\n")[4:9]) if docstring else ""
def parse_args():
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(description=_docstring(__doc__))
parser.add_argument("--version", action="version", version="%(prog)s 0.1")
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="print verbose messages; multiple -v result in more verbose messages",
)
parser.add_argument(
"-n",
type=int,
default=100,
help="Number of runs to simulate (default: %(default)s)",
)
parser.add_argument(
"-o", "--outdir", default="output", help="Path to output directory"
)
args = parser.parse_args()
return args
def simulate(algos, args):
"""Run the simulation.
Parameters
----------
algos : dict
Dictionary of lumi algorithms (defined in main).
args : argparse Namespace
Command line arguments from argparse.
"""
# Index / run number: [1, 2, ..., 100]
x = np.arange(1, args.n + 1)
# Create dataframe to store true lumi and lumi algos per run
df = pd.DataFrame(index=pd.Index(x, name="run"))
# Normalized time
df["time"] = x / args.n
# True integrated luminosity per run
# Make it increasing vs time and normalized such that integrated L over all runs = 1
func = np.vectorize(lambda x: 1 - np.exp(-5 * x))
df["truth"] = func(df["time"]) / np.sum(func(df["time"]))
for algo, vals in algos.items():
df[algo] = (
df["truth"]
* (1 + df["time"] * vals["drift"] / 100)
* np.random.normal(1, vals["noise"] / 100, args.n)
)
if args.verbose >= 1:
print("Data:")
print(df)
return df
def plot_raw(df, algos, args):
"""Plot the raw simulation data.
Parameters
----------
df : pd.DataFrame
Data frame with per-run simulated data for each lumi algorithm.
algos : dict
Dictionary of lumi algorithms (defined in main).
args : argparse Namespace
Command line arguments from argparse.
"""
fig, ax = aplt.subplots(name="raw", figsize=(800, 600))
algo_names = sorted(list(set(df.columns).difference({"time"})))
algo_names.insert(0, algo_names.pop(algo_names.index("truth")))
ax.set_xlabel("Run number")
ax.set_ylabel("Run-integrated luminosity [norm]")
ax.text(0.2, 0.87, "#it{Lumi Simulation}")
ax.text(0.2, 0.82, "Drift and noise w.r.t. #it{L}_{true}", size=20)
for algo_name in algo_names:
graph = root.TGraph(
df.index.size,
df.index.values.astype(np.float64),
df[algo_name].values.astype(np.float64),
)
if algo_name == "truth":
ax.plot(
graph,
"P",
markercolor=root.kBlack,
markerstyle=root.kFullCircle,
label="Truth",
labelfmt="P",
)
else:
ax.plot(
graph,
"P",
markercolor=algos[algo_name]["color"],
markerstyle=algos[algo_name]["marker"],
label=f"{algo_name}, drift: {algos[algo_name]['drift']:g}%, noise: {algos[algo_name]['noise']:g}%",
labelfmt="P",
)
# Add margins around data
ax.add_margins(top=0.15, left=0.05, right=0.05, bottom=0.05)
# Add legend
legend_height = 0.05 * len(algo_names)
ax.legend(loc=(0.55, 0.50 - legend_height, 0.75, 0.50), textsize=20)
# Save plot
outfilename = (
f"{args.outdir}/lumi_stability_simulator.n{df.index.size}.raw_values.pdf"
)
fig.savefig(outfilename)
def plot_stability(df, algos, args):
"""Plot the traditional stability plot for the simulation.
Parameters
----------
df : pd.DataFrame
Data frame with per-run simulated data for each lumi algorithm.
algos : dict
Dictionary of lumi algorithms (defined in main).
args : argparse Namespace
Command line arguments from argparse.
"""
# Plot vs run number and vs luminosity fraction
indep_vars = [
("runnum", "Run number", df.index.values.astype(np.float64)),
(
"lumifrac",
"Luminosity fraction (#it{L}_{true})",
np.cumsum(df["truth"].values).astype(np.float64),
),
]
for indep_var in indep_vars:
# Create separate plot per denominator
for denom in set(df.columns).difference({"time"}):
# Select the set of algorithms for numerator
algo_names = set(df.columns).difference({"time", "truth", denom})
fig, ax = aplt.subplots(
name=f"stability_by_{indep_var[0]}_wrt_{denom}", figsize=(800, 600)
)
ax.set_xlabel(indep_var[1])
ax.set_ylabel(f"#it{{L}}_{{algo}}/#it{{L}}_{{{denom}}} #minus 1 [%]")
ax.text(0.2, 0.87, "#it{Lumi Simulation}")
ax.text(0.2, 0.82, "Drift and noise w.r.t. #it{L}_{true}", size=20)
if denom != "truth":
ax.text(
0.2,
0.77,
f"{denom}: drift: {algos[denom]['drift']:g}%, noise: {algos[denom]['noise']:g}%",
size=20,
)
# Draw line at y = 0
line = root.TLine(ax.get_xlim()[0], 0, ax.get_xlim()[1], 0)
ax.plot(line)
for algo_name in sorted(list(algo_names)):
graph = root.TGraph(
df.index.size,
indep_var[2],
((df[algo_name] / df[denom] - 1) * 100).values.astype(np.float64),
)
ax.plot(
graph,
"P",
markercolor=algos[algo_name]["color"],
markerstyle=algos[algo_name]["marker"],
label=f"{algo_name}, drift: {algos[algo_name]['drift']:g}%, noise: {algos[algo_name]['noise']:g}%",
labelfmt="P",
)
ax.set_ylim(-2, 4)
# Add margins around data
ax.add_margins(left=0.05, right=0.05)
# Update line at y = 0 after adding margins
line.SetX1(ax.get_xlim()[0])
line.SetX2(ax.get_xlim()[1])
# Add legend
legend_height = 0.05 * len(algo_names)
ax.legend(loc=(0.55, 0.92 - legend_height, 0.75, 0.92), textsize=20)
# Save plot
outfilename = f"{args.outdir}/lumi_stability_simulator.n{df.index.size}.stability.by_{indep_var[0]}.wrt_{denom}.pdf"
fig.savefig(outfilename)
def plot_lumi_weighted_hist(df, algos, args):
"""Plot the histograms of the luminosity-weighted lumi ratios to assess the
mean and RMS for each algorithm w.r.t. each other algorithm.
Parameters
----------
df : pd.DataFrame
Data frame with per-run simulated data for each lumi algorithm.
algos : dict
Dictionary of lumi algorithms (defined in main).
args : argparse Namespace
Command line arguments from argparse.
"""
# Create separate plot per denominator
for denom in set(df.columns).difference({"time"}):
# Select the set of algorithms for numerator
algo_names = set(df.columns).difference({"time", "truth", denom})
fig, ax = aplt.subplots(name=f"lumi_weighted_hist_{denom}", figsize=(800, 600))
ax.set_xlabel(
f"#it{{L}}-weighted #it{{L}}_{{algo}}/#it{{L}}_{{{denom}}} #minus 1 [%]"
)
ax.set_ylabel("Integrated luminosity [norm]")
ax.text(0.2, 0.87, "#it{Lumi Simulation}")
ax.text(0.2, 0.82, "Drift and noise w.r.t. #it{L}_{true}", size=20)
if denom != "truth":
ax.text(
0.2,
0.77,
f"{denom}: drift: {algos[denom]['drift']:g}%, noise: {algos[denom]['noise']:g}%",
size=20,
)
for i, algo_name in enumerate(sorted(list(algo_names))):
# Fill histogram
hist = root.TH1D(f"hist_{algo_name}_vs_{denom}", "", 40, -2, 2)
for index, row in df.iterrows():
hist.Fill((row[algo_name] / row[denom] - 1) * 100, row["truth"])
ax.plot(
hist,
"",
linecolor=algos[algo_name]["color"],
linestyle=algos[algo_name]["line"],
fillcolor=algos[algo_name]["color"],
fillalpha=0.05,
label=f"{algo_name}, drift: {algos[algo_name]['drift']:g}%, noise: {algos[algo_name]['noise']:g}%",
labelfmt="L",
)
# Add labels with histogram mean +/- RMS
label_yup = 0.65
if i == 0:
ax.text(0.2, label_yup + 0.05, "Mean #pm RMS", size=20)
ax.text(
0.2,
label_yup - i * 0.05,
f"{algo_name}: {hist.GetMean():.2g} #pm {hist.GetRMS():.2g} %",
size=20,
)
# Add margins around data
ax.add_margins(top=0.28)
# Add legend
legend_height = 0.05 * len(algo_names)
ax.legend(loc=(0.55, 0.92 - legend_height, 0.75, 0.92), textsize=20)
# Save plot
outfilename = f"{args.outdir}/lumi_stability_simulator.n{df.index.size}.lumi_weighted_hist.wrt_{denom}.pdf"
fig.savefig(outfilename)
def main():
try:
args = parse_args()
# Define lumi algorithms
# Format: {<algo name>: "drift": <drift wrt truth [%]>, "noise": <gaus noise [%]>}
# Also include plot-formatting options (colour, line/marker style)
# root.kBlack and root.kFullCircle are reserved for truth lumi
algos = {
"A": {
"drift": 0.0,
"noise": 0.1,
"color": root.kAzure - 2,
"marker": root.kOpenCircle,
"line": 1,
},
"B": {
"drift": 0.0,
"noise": 0.3,
"color": root.kOrange - 3,
"marker": root.kOpenSquare,
"line": 2,
},
"C": {
"drift": 0.5,
"noise": 0.1,
"color": root.kGreen + 2,
"marker": root.kOpenTriangleUp,
"line": 3,
},
"D": {
"drift": 0.5,
"noise": 0.3,
"color": root.kRed + 1,
"marker": root.kOpenTriangleDown,
"line": 4,
},
"E": {
"drift": -1.0,
"noise": 0.1,
"color": root.kViolet + 1,
"marker": root.kOpenDiamond,
"line": 5,
},
}
# Generate the simulated data and plot
df = simulate(algos, args)
# Set the ATLAS style
aplt.set_atlas_style()
# Create output directory
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
# Plot
plot_raw(df, algos, args)
plot_stability(df, algos, args)
plot_lumi_weighted_hist(df, algos, args)
except KeyboardInterrupt:
return 1
if __name__ == "__main__":
root.gROOT.SetBatch()
sys.exit(main())