Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vertex_mu_scan.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file vertex_mu_scan.py
1 #!/usr/bin/env python3
2 from pathlib import Path
3 from typing import List, Optional
4 import re
5 
6 import uproot
7 import typer
8 import numpy
9 import matplotlib.pyplot
10 
11 
12 def main(files: List[Path], output: str, title: str = ""):
13 
14  mus = []
15 
16  for file in files:
17  m = re.match(".*mu(\d+).*", file.name)
18  mu = int(m.group(1))
19  mus.append(mu)
20 
21  mus = list(sorted(mus))
22 
23  fig, (ax, tax) = matplotlib.pyplot.subplots(
24  2, 1, gridspec_kw={"hspace": 0, "height_ratios": [2, 1]}
25  )
26 
27  for fitter, color in zip(("Iterative", "AMVF"), ["tab:blue", "tab:orange"]):
28  mean = numpy.array([])
29  stddev = numpy.array([])
30  ntrue_mean = numpy.array([])
31  ntrue_stddev = numpy.array([])
32  time = numpy.array([])
33 
34  for mu in mus:
35  for file in files:
36  if f"mu{mu}" in file.name and fitter in file.name:
37  break
38 
39  time_file = file.parent / f"{file.stem}_time.txt"
40  if time_file.exists():
41  time = numpy.append(time, float(time_file.read_text()))
42  else:
43  fime.append(float("nan"))
44 
45  rf = uproot.open(f"{file}:vertexing")
46 
47  nreco = rf["nRecoVtx"].array(library="np")
48  ntrue = rf["nTrueVtx"].array(library="np")
49 
50  nreco_mean = nreco.mean()
51 
52  nreco_std = nreco.std()
53 
54  mean = numpy.append(mean, nreco_mean)
55  stddev = numpy.append(stddev, nreco_std)
56 
57  ntrue_mean = numpy.append(ntrue_mean, ntrue.mean())
58  ntrue_stddev = numpy.append(ntrue_stddev, ntrue.std())
59 
60  ax.fill_between(mus, mean - stddev, mean + stddev, fc=color, alpha=0.2)
61  ax.plot(mus, mean, label=fitter, c=color)
62  tax.plot(mus, time, label=f"{fitter} time", c=color)
63 
64  ax.plot(mus, mus, label="truth", c="gray", ls="--")
65 
66  tax.set_xlabel("$\mu$")
67  ax.set_ylabel("Number of vertices")
68  tax.set_ylabel("wall time [s]")
69 
70  fig.align_labels()
71 
72  ax.legend()
73  tax.legend()
74  ax.set_title(title)
75 
76  fig.tight_layout()
77  fig.savefig(output)
78 
79 
80 if __name__ == "__main__":
81  typer.run(main)