FairShip
Loading...
Searching...
No Matches
split_weight_uncertainties Namespace Reference

Functions

 make_parser (description)
 log_edges (spec)
 print_table (args, n_generated, edges, values, errors, naive, n_eff)
 main ()

Detailed Description

Momentum spectrum at a scoring plane with event-level uncertainties.

Example for weighted samples with split branches, e.g. run_fixedTarget.py with
--kaon-pion-splits: the clones of one decay are correlated, so the error of a
bin has to come from the spread of per-event weight sums rather than from
sqrt(sum w^2) over hits. The table compares both and lists the effective number
of independent events per bin; below about 20 the error itself is unreliable.

    python examples/split_weight_uncertainties.py -f out/*/pythia8_*.root -o spectrum.root

The output holds only the accumulator, so outputs of several jobs can be merged
with hadd. Get the per-event spectrum from the (merged) file with
EventHistogram.from_root(f, name).values() and .errors(), or in C++ with
EventHistogram::Read(f, name)->MakeResult().

split_weight_uncertainties_rdf.py does the same with RDataFrame and the C++
class, also multithreaded.

Function Documentation

◆ log_edges()

split_weight_uncertainties.log_edges ( spec)

Definition at line 45 of file split_weight_uncertainties.py.

45def log_edges(spec):
46 low, high, n = spec.split(",")
47 return np.geomspace(float(low), float(high), int(n) + 1)
48
49

◆ main()

split_weight_uncertainties.main ( )

Definition at line 64 of file split_weight_uncertainties.py.

64def main():
65 args = make_parser(__doc__.splitlines()[0]).parse_args()
66 edges = log_edges(args.bins)
68 args.name, edges, title=f"{args.branch};p (GeV/c);per event", covariance=True, n_replicas=args.replicas
69 )
70
71 n_generated = 0
72 for name in args.files:
73 f = ROOT.TFile.Open(name)
74 n_generated += generated_events(f)
75 tree = f.Get("cbmsim")
76 tree.SetBranchStatus("*", 0)
77 for branch in ("MCEventHeader", "MCTrack", args.branch):
78 tree.SetBranchStatus(f"{branch}*", 1)
79 for event in tree:
80 header = event.MCEventHeader
81 tracks = event.MCTrack
82 momenta, weights = [], []
83 for point in getattr(event, args.branch):
84 if args.pdg and abs(point.PdgCode()) != args.pdg:
85 continue
86 momenta.append(math.hypot(point.GetPx(), point.GetPy(), point.GetPz()))
87 weights.append(tracks[point.GetTrackID()].GetWeight())
88 h.fill(momenta, weights)
89 h.end_event(make_event_key(header.GetRunID(), header.GetEventID()))
90 f.Close()
91 h.add_empty_events(n_generated - h.n_events)
92
93 print_table(args, n_generated, edges, h.values(), h.errors(), h.naive_errors(), h.n_eff())
94
95 out = ROOT.TFile(args.output, "RECREATE")
96 h.to_root(out)
97 out.Close()
98 print(f"written to {args.output}")
99
100

◆ make_parser()

split_weight_uncertainties.make_parser ( description)
Options shared with split_weight_uncertainties_rdf.py.

Definition at line 32 of file split_weight_uncertainties.py.

32def make_parser(description):
33 """Options shared with split_weight_uncertainties_rdf.py."""
34 ap = argparse.ArgumentParser(description=description)
35 ap.add_argument("-f", "--files", nargs="+", required=True, help="simulation output files")
36 ap.add_argument("-o", "--output", default="split_weight_uncertainties.root")
37 ap.add_argument("--branch", default="PlaneHAPoint", help="point branch of the scoring plane")
38 ap.add_argument("--pdg", type=int, default=13, help="|PDG code| to select, 0 for all")
39 ap.add_argument("--bins", default="5,400,20", help="log-spaced momentum bins: low,high,n (GeV/c)")
40 ap.add_argument("--replicas", type=int, default=0, help="bootstrap replicas (0 = none)")
41 ap.add_argument("--name", default="p_plane")
42 return ap
43
44

◆ print_table()

split_weight_uncertainties.print_table ( args,
n_generated,
edges,
values,
errors,
naive,
n_eff )

Definition at line 50 of file split_weight_uncertainties.py.

50def print_table(args, n_generated, edges, values, errors, naive, n_eff):
51 print(f"{n_generated} generated events, {args.branch}, |pdg| = {args.pdg or 'all'}")
52 print(f"{'p (GeV/c)':>15} {'per event':>11} {'error':>10} {'sqrt(Σw²)':>10} {'ratio':>6} {'n_eff':>7}")
53 for b in range(len(values)):
54 if values[b] == 0 and errors[b] == 0:
55 continue
56 ratio = errors[b] / naive[b] if naive[b] > 0 else float("nan")
57 flag = " <- unreliable" if n_eff[b] < 20 else ""
58 print(
59 f"{edges[b]:7.1f}–{edges[b + 1]:<7.1f} {values[b]:11.4e} {errors[b]:10.3e} {naive[b]:10.3e}"
60 f" {ratio:6.1f} {n_eff[b]:7.1f}{flag}"
61 )
62
63