FairShip
Loading...
Searching...
No Matches
genie_interface.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4"""Utilities to run GENIE tools with per-call environment overrides.
5
6This module wraps common GENIE command-line tools (``gmkspl``, ``gevgen``,
7``gntpc``) and a ROOT helper to copy flux histograms into simulation files.
8It provides:
9
10- Safe subprocess execution (no ``shell=True``)
11- Per-call environment overrides (set/unset variables like ``GXMLPATH``)
12- Helpful printing/logging of the exact command line
13
14Notes:
15-----
16* A Python process cannot modify its parent shell environment. Use the
17 ``env_vars=...`` argument to set/unset variables **for the child process**
18 only. If you need to persist environment variables, put them in your shell
19 rc (e.g. ``~/.bashrc``) or wrap execution in ``source/env`` scripts.
20
21* The path to ``Messenger_laconic.xml`` is auto-detected from ``$GENIE`` if
22 defined and readable; otherwise the ``--message-thresholds`` option is
23 omitted.
24
25Example:
26-------
27>>> from genie_interface import generate_genie_events
28>>> generate_genie_events(
29... nevents=1000, nupdg=14, emin=1, emax=100, targetcode="1000080160",
30... inputflux="/path/flux.root", spline="/path/xsec.xml", outputfile="out",
31... env_vars={"GXMLPATH": "/opt/genie/config", "GENIE": "/cvmfs/.../genie"}
32... )
33
34"""
35
36from __future__ import annotations
37
38import logging
39import os
40import shlex
41import subprocess
42from collections.abc import Mapping, Sequence
43
44import ROOT # type: ignore
45
46__all__ = [
47 "_run",
48 "add_hists",
49 "generate_genie_events",
50 "get_1D_flux_name",
51 "get_2D_flux_name",
52 "main",
53 "make_ntuples",
54 "make_splines",
55]
56
57__version__ = "0.1.0"
58
59PathLike = str | os.PathLike
60
61logger = logging.getLogger(__name__)
62
63
64def _merge_env(env_vars: Mapping[str, str | None] | None) -> dict[str, str]:
65 """Merge ``env_vars`` with the current environment.
66
67 Parameters
68 ----------
69 env_vars:
70 Mapping of KEY -> VALUE to set for the child process. Use ``None`` to
71 **unset** a variable (remove it) for the child environment.
72
73 Returns
74 -------
75 dict
76 The environment dict to pass into ``subprocess.run``.
77
78 """
79 env: dict[str, str] = dict(os.environ)
80 if env_vars:
81 for k, v in env_vars.items():
82 if v is None:
83 env.pop(k, None)
84 else:
85 env[k] = str(v)
86 return env
87
88
89def _run(
90 args: Sequence[str],
91 env_vars: Mapping[str, str | None] | None = None,
92 *,
93 check: bool = True,
94) -> subprocess.CompletedProcess:
95 """Run a command with optional environment overrides.
96
97 Parameters
98 ----------
99 args:
100 Command and arguments, each as a separate list element. (No shell.)
101 env_vars:
102 Optional mapping of KEY -> VALUE to inject into the child environment.
103 Use ``None`` value to **unset** a variable.
104 check:
105 If True (default), raise ``CalledProcessError`` on non-zero exit.
106
107 Returns
108 -------
109 subprocess.CompletedProcess
110 The completed process object.
111
112 Raises
113 ------
114 subprocess.CalledProcessError
115 If the command exits with non-zero status and ``check=True``.
116
117 """
118 env = _merge_env(env_vars)
119 cmd_str = shlex.join(args)
120 logger.info("Executing: %s", cmd_str)
121 print(">>", cmd_str)
122 return subprocess.run(args, env=env, check=check)
123
124
125def get_1D_flux_name(nupdg: int) -> str:
126 """Return the TH1D flux histogram name for a given neutrino PDG.
127
128 Naming convention (as used by input flux ROOT files):
129 - Neutrino (PDG > 0): ``'10' + |pdg|``
130 - Anti-neutrino (PDG < 0): ``'20' + |pdg|``
131
132 Examples
133 --------
134 >>> get_1D_flux_name(12) # nu_e
135 '1012'
136 >>> get_1D_flux_name(-12) # anti-nu_e
137 '2012'
138
139 """
140 x = ROOT.TMath.Abs(nupdg)
141 return ("10" if nupdg > 0 else "20") + str(x)
142
143
144def get_2D_flux_name(nupdg: int) -> str:
145 """Return the TH2D p–pT flux histogram name for a given neutrino PDG.
146
147 Naming convention (as used by input flux ROOT files):
148 - Neutrino (PDG > 0): ``'12' + |pdg|``
149 - Anti-neutrino (PDG < 0): ``'22' + |pdg|``
150
151 Examples
152 --------
153 >>> get_2D_flux_name(12) # nu_e
154 '1212'
155 >>> get_2D_flux_name(-12) # anti-nu_e
156 '2212'
157
158 """
159 x = ROOT.TMath.Abs(nupdg)
160 return ("12" if nupdg > 0 else "22") + str(x)
161
162
164 nupdglist: Sequence[int],
165 targetcode: str,
166 emax: int | float,
167 nknots: int,
168 outputfile: PathLike,
169 env_vars: Mapping[str, str | None] | None = None,
170) -> subprocess.CompletedProcess:
171 """Generate GENIE cross-section splines via ``gmkspl``.
172
173 Parameters
174 ----------
175 nupdglist:
176 Iterable of neutrino PDG codes (e.g. ``[12, 14, 16, -12, -14, -16]``).
177 targetcode:
178 GENIE nuclear target code (e.g. ``'1000080160'`` for O-16).
179 emax:
180 Maximum neutrino energy (GeV).
181 nknots:
182 Number of knots for the spline generation.
183 outputfile:
184 Path to output XML spline file.
185 env_vars:
186 Per-call environment overrides (e.g. set ``GXMLPATH`` or ``GENIE``).
187
188 Returns
189 -------
190 subprocess.CompletedProcess
191
192 """
193 inputnupdg = ",".join(str(p) for p in nupdglist)
194 args = [
195 "gmkspl",
196 "-p",
197 inputnupdg,
198 "-t",
199 targetcode,
200 "-n",
201 str(nknots),
202 "-e",
203 str(emax),
204 "-o",
205 str(outputfile),
206 ]
207 return _run(args, env_vars=env_vars)
208
209
211 nevents: int,
212 nupdg: int,
213 emin: int | float,
214 emax: int | float,
215 targetcode: str,
216 inputflux: PathLike,
217 spline: PathLike,
218 outputfile: PathLike,
219 *,
220 process: str | None = None,
221 seed: int | None = None,
222 irun: int | None = None,
223 env_vars: Mapping[str, str | None] | None = None,
224) -> subprocess.CompletedProcess:
225 """Run GENIE ``gevgen`` to generate events.
226
227 Parameters
228 ----------
229 nevents:
230 Number of events to generate.
231 nupdg:
232 Neutrino PDG code (e.g. 12, 14, 16 or negatives for anti-neutrinos).
233 emin, emax:
234 Energy range in GeV (inclusive, inclusive as accepted by GENIE).
235 targetcode:
236 GENIE nuclear target code.
237 inputflux:
238 Path to ROOT file with flux histograms.
239 spline:
240 Path to GENIE cross-section spline XML file.
241 outputfile:
242 Output prefix/file for GENIE (as accepted by ``-o``).
243 process:
244 Optional GENIE generator list (e.g. ``'CCDIS'``, ``'NC'``).
245 seed:
246 Optional RNG seed for reproducibility.
247 irun:
248 Optional integer run number (``--run``).
249 env_vars:
250 Per-call environment overrides (e.g. set/unset ``GXMLPATH``, ``GENIE``).
251
252 Returns
253 -------
254 subprocess.CompletedProcess
255
256 Notes
257 -----
258 If ``GENIE`` is defined in the child environment, this function will try
259 to pass ``--message-thresholds $GENIE/config/Messenger_laconic.xml`` if
260 the file exists; otherwise that option is omitted.
261
262 """
263 env = _merge_env(env_vars)
264
265 msg_xml = None
266 genie_root = env.get("GENIE")
267 if genie_root:
268 candidate = os.path.join(genie_root, "config", "Messenger_laconic.xml")
269 if os.path.isfile(candidate):
270 msg_xml = candidate
271
272 args = [
273 "gevgen",
274 "-n",
275 str(nevents),
276 "-p",
277 str(nupdg),
278 "-t",
279 targetcode,
280 "-e",
281 f"{emin},{emax}",
282 ]
283 if msg_xml:
284 args += ["--message-thresholds", msg_xml]
285
286 args += [
287 "-f",
288 f"{inputflux},{get_1D_flux_name(nupdg)}",
289 "--cross-sections",
290 str(spline),
291 "-o",
292 str(outputfile),
293 ]
294
295 if process is not None:
296 args += ["--event-generator-list", process]
297 if seed is not None:
298 args += ["--seed", str(seed)]
299 if irun is not None:
300 args += ["--run", str(irun)]
301
302 return _run(args, env_vars=env_vars)
303
304
306 inputfile: PathLike,
307 outputfile: PathLike,
308 env_vars: Mapping[str, str | None] | None = None,
309) -> subprocess.CompletedProcess:
310 """Convert a GENIE ``.ghep.root`` file to GST via ``gntpc``.
311
312 Parameters
313 ----------
314 inputfile:
315 Input GHEP file (e.g. ``gntp.0.ghep.root``).
316 outputfile:
317 Output GST file path.
318 env_vars:
319 Per-call environment overrides.
320
321 Returns
322 -------
323 subprocess.CompletedProcess
324
325 """
326 args = ["gntpc", "-i", str(inputfile), "-f", "gst", "-o", str(outputfile)]
327 return _run(args, env_vars=env_vars)
328
329
330def add_hists(inputflux: PathLike, simfile: PathLike, nupdg: int) -> None:
331 """Copy the 2D p–pT flux histogram from ``inputflux`` into ``simfile``.
332
333 Parameters
334 ----------
335 inputflux:
336 Path to ROOT file containing TH2D flux histograms.
337 simfile:
338 Path to the simulation ROOT file to **update** (opened in 'UPDATE' mode).
339 nupdg:
340 Neutrino PDG code used to select the histogram (see ``get_2D_flux_name``).
341
342 Raises
343 ------
344 FileNotFoundError
345 If the requested histogram is not found in ``inputflux``.
346 OSError
347 If ROOT fails to open either file.
348
349 """
350 with (
351 ROOT.TFile.Open(str(inputflux), "READ") as infile,
352 ROOT.TFile.Open(str(simfile), "UPDATE") as sfile,
353 ):
354 hname = get_2D_flux_name(nupdg)
355 obj = infile.Get(hname)
356 if not obj:
357 raise FileNotFoundError(f"Histogram '{hname}' not found in {inputflux}")
358 # Write into the current directory of sfile
359 sfile.cd()
360 obj.Write()
361
362
363# --------------------------- CLI ---------------------------------------------
364
365
366def _parse_env_kv(pairs: Sequence[str]) -> dict[str, str | None]:
367 """Parse ``KEY=VAL`` pairs for ``--env`` CLI flag."""
368 out: dict[str, str | None] = {}
369 for p in pairs:
370 if "=" not in p:
371 raise ValueError(f"Expected KEY=VALUE, got '{p}'")
372 k, v = p.split("=", 1)
373 out[k] = v
374 return out
375
376
377def main(argv: Sequence[str] | None = None) -> int:
378 """Entry point for the command-line interface."""
379 import argparse
380
381 parser = argparse.ArgumentParser(prog="genie-utils", description="GENIE helpers with per-call env overrides")
382 parser.add_argument(
383 "--env",
384 metavar="KEY=VALUE",
385 action="append",
386 default=[],
387 help="Set environment variable(s) for the child process (repeatable).",
388 )
389 parser.add_argument(
390 "--unset",
391 metavar="KEY",
392 action="append",
393 default=[],
394 help="Unset environment variable(s) for the child process (repeatable).",
395 )
396 parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity.")
397 sub = parser.add_subparsers(dest="cmd", required=True)
398
399 p_spl = sub.add_parser("splines", help="Run gmkspl")
400 p_spl.add_argument("-p", "--pdg", nargs="+", type=int, required=True)
401 p_spl.add_argument("-t", "--target", required=True)
402 p_spl.add_argument("-e", "--emax", type=float, required=True)
403 p_spl.add_argument("-n", "--nknots", type=int, required=True)
404 p_spl.add_argument("-o", "--output", required=True)
405
406 p_gen = sub.add_parser("generate", help="Run gevgen")
407 p_gen.add_argument("-n", "--nevents", type=int, required=True)
408 p_gen.add_argument("-p", "--pdg", type=int, required=True)
409 p_gen.add_argument("--emin", type=float, required=True)
410 p_gen.add_argument("--emax", type=float, required=True)
411 p_gen.add_argument("-t", "--target", required=True)
412 p_gen.add_argument("-f", "--flux", required=True)
413 p_gen.add_argument("-s", "--spline", required=True)
414 p_gen.add_argument("-o", "--output", required=True)
415 p_gen.add_argument("--process", default=None)
416 p_gen.add_argument("--seed", type=int, default=None)
417 p_gen.add_argument("--run", type=int, dest="irun", default=None)
418
419 p_nt = sub.add_parser("ntuples", help="Run gntpc (ghep -> gst)")
420 p_nt.add_argument("-i", "--input", required=True)
421 p_nt.add_argument("-o", "--output", required=True)
422
423 p_add = sub.add_parser("add-hists", help="Copy 2D flux hist to sim file")
424 p_add.add_argument("-f", "--flux", required=True)
425 p_add.add_argument("-s", "--sim", required=True)
426 p_add.add_argument("-p", "--pdg", type=int, required=True)
427
428 args = parser.parse_args(argv)
429
430 # logging
431 level = logging.WARNING
432 if args.verbose == 1:
433 level = logging.INFO
434 elif args.verbose >= 2:
435 level = logging.DEBUG
436 logging.basicConfig(level=level, format="%(levelname)s %(name)s: %(message)s")
437
438 # env merging for CLI
439 env_vars: dict[str, str | None] = {}
440 if args.env:
441 env_vars.update(_parse_env_kv(args.env))
442 for key in args.unset:
443 env_vars[key] = None
444
445 if args.cmd == "splines":
447 nupdglist=args.pdg,
448 targetcode=args.target,
449 emax=args.emax,
450 nknots=args.nknots,
451 outputfile=args.output,
452 env_vars=env_vars or None,
453 )
454 elif args.cmd == "generate":
456 nevents=args.nevents,
457 nupdg=args.pdg,
458 emin=args.emin,
459 emax=args.emax,
460 targetcode=args.target,
461 inputflux=args.flux,
462 spline=args.spline,
463 outputfile=args.output,
464 process=args.process,
465 seed=args.seed,
466 irun=args.irun,
467 env_vars=env_vars or None,
468 )
469 elif args.cmd == "ntuples":
471 inputfile=args.input,
472 outputfile=args.output,
473 env_vars=env_vars or None,
474 )
475 elif args.cmd == "add-hists":
476 add_hists(inputflux=args.flux, simfile=args.sim, nupdg=args.pdg)
477 else:
478 parser.error("Unknown command")
479
480 return 0
481
482
483if __name__ == "__main__":
484 main()
subprocess.CompletedProcess generate_genie_events(int nevents, int nupdg, int|float emin, int|float emax, str targetcode, PathLike inputflux, PathLike spline, PathLike outputfile, *, str|None process=None, int|None seed=None, int|None irun=None, Mapping[str, str|None]|None env_vars=None)
str get_1D_flux_name(int nupdg)
subprocess.CompletedProcess _run(Sequence[str] args, Mapping[str, str|None]|None env_vars=None, *, bool check=True)
dict[str, str|None] _parse_env_kv(Sequence[str] pairs)
subprocess.CompletedProcess make_splines(Sequence[int] nupdglist, str targetcode, int|float emax, int nknots, PathLike outputfile, Mapping[str, str|None]|None env_vars=None)
None add_hists(PathLike inputflux, PathLike simfile, int nupdg)
dict[str, str] _merge_env(Mapping[str, str|None]|None env_vars)
subprocess.CompletedProcess make_ntuples(PathLike inputfile, PathLike outputfile, Mapping[str, str|None]|None env_vars=None)
str get_2D_flux_name(int nupdg)