5"""Run a full tracking benchmark: simulation -> reconstruction -> metrics.
7Fires a particle gun upstream of the straw tube spectrometer (T1-T4),
8runs digitisation and reconstruction (AR pattern recognition and GenFit by
9default), then computes tracking and vertexing performance metrics.
11Each phase (sim, reco) runs as a subprocess because FairRoot singletons
12prevent creating multiple FairRunSim instances in the same process.
15 python macro/run_tracking_benchmark.py -n 200 --seed 42 --tag test
16 python macro/run_tracking_benchmark.py -n 1000 --nTracks 5 --tag multi
19from __future__
import annotations
24from argparse
import ArgumentParser
26parser = ArgumentParser(description=
"Tracking performance benchmark for straw tube spectrometer")
27parser.add_argument(
"-n",
"--nEvents", type=int, default=1000, help=
"Number of events (default: 1000)")
28parser.add_argument(
"--pID", type=int, default=13, help=
"Particle PDG ID (default: 13, mu-)")
29parser.add_argument(
"--Estart", type=float, default=1.0, help=
"Start of energy range in GeV (default: 1)")
30parser.add_argument(
"--Eend", type=float, default=100.0, help=
"End of energy range in GeV (default: 100)")
31parser.add_argument(
"--Vz", type=float, default=8300.0, help=
"Gun z-position in cm (default: 8300, ~1m upstream of T1)")
32parser.add_argument(
"--Dx", type=float, default=200.0, help=
"Position spread in x [cm] (default: 200)")
33parser.add_argument(
"--Dy", type=float, default=300.0, help=
"Position spread in y [cm] (default: 300)")
34parser.add_argument(
"--nTracks", type=int, default=1, help=
"Tracks per event (default: 1)")
35parser.add_argument(
"--thetaMin", type=float, default=0, help=
"Minimum polar angle [deg] (default: 0)")
36parser.add_argument(
"--thetaMax", type=float, default=0, help=
"Maximum polar angle [deg] (default: 0)")
37parser.add_argument(
"--mixCharges", action=
"store_true", help=
"Generate equal mix of particle and antiparticle")
39 "--Dz", type=float, default=0.0, help=
"Vertex spread in z [cm]; uniform in (Vz - Dz/2, Vz + Dz/2) (default: 0)"
42 "--commonVertex", action=
"store_true", help=
"Give all tracks of an event one vertex, for vertexing studies"
46 choices=[
"genfit",
"acts"],
48 help=
"Track fitter (default: acts; genfit is deprecated)",
52 choices=[
"FH",
"AR",
"TemplateMatching",
"Truth",
"Segments"],
54 help=
"Pattern recognition passed to ShipReco.py (default: ShipReco.py's, Segments for acts and AR for genfit)",
59 help=
"Count secondaries as reconstructible too (needed for samples such as HNL decays)",
64 help=
"Attribute hits to every MC track crossing the straw (for busy events, where tracks share straws)",
66parser.add_argument(
"--skipSim", action=
"store_true", help=
"Reuse an existing simulation output with this tag")
67parser.add_argument(
"--skipReco", action=
"store_true", help=
"Reuse an existing reconstruction output")
68parser.add_argument(
"--tag", default=
"benchmark", help=
"Output file tag (default: benchmark)")
69parser.add_argument(
"--output-json", default=
None, help=
"JSON metrics output path")
70parser.add_argument(
"--seed", type=int, default=42, help=
"Random seed (default: 42)")
71parser.add_argument(
"-o",
"--outputDir", default=
".", help=
"Output directory (default: .)")
77 help=
"FairLogger verbosity: 0=info, 1=debug, 2=debug1, 3=debug2",
80options = parser.parse_args()
82if not os.path.exists(options.outputDir):
83 os.makedirs(options.outputDir)
84options.outputDir = os.path.abspath(options.outputDir)
87sim_file = os.path.join(options.outputDir, f
"sim_{tag}.root")
88geo_file = os.path.join(options.outputDir, f
"geo_{tag}.root")
89reco_file = os.path.join(options.outputDir, f
"sim_{tag}_rec.root")
91 os.path.abspath(options.output_json)
92 if options.output_json
93 else os.path.join(options.outputDir,
"tracking_metrics.json")
95histo_file = os.path.join(options.outputDir,
"tracking_benchmark_histos.root")
97fairship = os.environ.get(
"FAIRSHIP",
"")
100def run_phase(description: str, cmd: list[str], cwd: str |
None =
None) ->
None:
101 """Run a subprocess phase, raising on failure."""
103 print(f
"{description}")
105 result = subprocess.run(cmd, check=
False, cwd=cwd)
106 if result.returncode != 0:
107 print(f
"FAILED: {description} (exit code {result.returncode})")
108 sys.exit(result.returncode)
114sim_script = os.path.join(fairship,
"macro",
"run_simScript.py")
if fairship
else "macro/run_simScript.py"
119 str(options.nEvents),
149 "uniform3d" if options.Dz > 0
else "uniform",
153 str(options.nTracks),
155 str(options.thetaMin),
157 str(options.thetaMax),
159if options.mixCharges:
160 sim_cmd.append(
"--bothCharges")
161if options.commonVertex:
162 sim_cmd.append(
"--commonVertex")
164if not options.skipSim:
165 run_phase(
"Phase 1: Simulation", sim_cmd)
167if not os.path.exists(sim_file):
168 print(f
"ERROR: Simulation output {sim_file} not found")
174reco_script = os.path.join(fairship,
"macro",
"ShipReco.py")
if fairship
else "macro/ShipReco.py"
183 str(options.nEvents),
188 reco_cmd += [
"--patRec", options.patRec]
190 reco_cmd.append(
"--Debug")
192if not options.skipReco:
193 run_phase(
"Phase 2: Reconstruction", reco_cmd, cwd=options.outputDir)
195if not os.path.exists(reco_file):
196 print(f
"ERROR: Reconstruction output {reco_file} not found")
203print(
"Phase 3: Benchmark Metrics")
206import tracking_benchmark
209 sim_file, reco_file, geo_file, primary_only=
not options.allTracks, straw_purity=options.strawPurity
211bench.compute_metrics()
213bench.save_json(json_file)
214bench.save_histograms(histo_file)
217print(
"Tracking benchmark complete.")
218print(f
" Metrics: {json_file}")
219print(f
" Histograms: {histo_file}")
None run_phase(str description, list[str] cmd, str|None cwd=None)