4"""Utilities to run GENIE tools with per-call environment overrides.
6This module wraps common GENIE command-line tools (``gmkspl``, ``gevgen``,
7``gntpc``) and a ROOT helper to copy flux histograms into simulation files.
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
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.
21* The path to ``Messenger_laconic.xml`` is auto-detected from ``$GENIE`` if
22 defined and readable; otherwise the ``--message-thresholds`` option is
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"}
36from __future__
import annotations
42from collections.abc
import Mapping, Sequence
49 "generate_genie_events",
59PathLike = str | os.PathLike
61logger = logging.getLogger(__name__)
64def _merge_env(env_vars: Mapping[str, str |
None] |
None) -> dict[str, str]:
65 """Merge ``env_vars`` with the current environment.
70 Mapping of KEY -> VALUE to set for the child process. Use ``None`` to
71 **unset** a variable (remove it) for the child environment.
76 The environment dict to pass into ``subprocess.run``.
79 env: dict[str, str] = dict(os.environ)
81 for k, v
in env_vars.items():
91 env_vars: Mapping[str, str |
None] |
None =
None,
94) -> subprocess.CompletedProcess:
95 """Run a command with optional environment overrides.
100 Command and arguments, each as a separate list element. (No shell.)
102 Optional mapping of KEY -> VALUE to inject into the child environment.
103 Use ``None`` value to **unset** a variable.
105 If True (default), raise ``CalledProcessError`` on non-zero exit.
109 subprocess.CompletedProcess
110 The completed process object.
114 subprocess.CalledProcessError
115 If the command exits with non-zero status and ``check=True``.
119 cmd_str = shlex.join(args)
120 logger.info(
"Executing: %s", cmd_str)
122 return subprocess.run(args, env=env, check=check)
126 """Return the TH1D flux histogram name for a given neutrino PDG.
128 Naming convention (as used by input flux ROOT files):
129 - Neutrino (PDG > 0): ``'10' + |pdg|``
130 - Anti-neutrino (PDG < 0): ``'20' + |pdg|``
134 >>> get_1D_flux_name(12) # nu_e
136 >>> get_1D_flux_name(-12) # anti-nu_e
140 x = ROOT.TMath.Abs(nupdg)
141 return (
"10" if nupdg > 0
else "20") + str(x)
145 """Return the TH2D p–pT flux histogram name for a given neutrino PDG.
147 Naming convention (as used by input flux ROOT files):
148 - Neutrino (PDG > 0): ``'12' + |pdg|``
149 - Anti-neutrino (PDG < 0): ``'22' + |pdg|``
153 >>> get_2D_flux_name(12) # nu_e
155 >>> get_2D_flux_name(-12) # anti-nu_e
159 x = ROOT.TMath.Abs(nupdg)
160 return (
"12" if nupdg > 0
else "22") + str(x)
164 nupdglist: Sequence[int],
168 outputfile: PathLike,
169 env_vars: Mapping[str, str |
None] |
None =
None,
170) -> subprocess.CompletedProcess:
171 """Generate GENIE cross-section splines via ``gmkspl``.
176 Iterable of neutrino PDG codes (e.g. ``[12, 14, 16, -12, -14, -16]``).
178 GENIE nuclear target code (e.g. ``'1000080160'`` for O-16).
180 Maximum neutrino energy (GeV).
182 Number of knots for the spline generation.
184 Path to output XML spline file.
186 Per-call environment overrides (e.g. set ``GXMLPATH`` or ``GENIE``).
190 subprocess.CompletedProcess
193 inputnupdg =
",".join(str(p)
for p
in nupdglist)
207 return _run(args, env_vars=env_vars)
218 outputfile: PathLike,
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.
230 Number of events to generate.
232 Neutrino PDG code (e.g. 12, 14, 16 or negatives for anti-neutrinos).
234 Energy range in GeV (inclusive, inclusive as accepted by GENIE).
236 GENIE nuclear target code.
238 Path to ROOT file with flux histograms.
240 Path to GENIE cross-section spline XML file.
242 Output prefix/file for GENIE (as accepted by ``-o``).
244 Optional GENIE generator list (e.g. ``'CCDIS'``, ``'NC'``).
246 Optional RNG seed for reproducibility.
248 Optional integer run number (``--run``).
250 Per-call environment overrides (e.g. set/unset ``GXMLPATH``, ``GENIE``).
254 subprocess.CompletedProcess
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.
266 genie_root = env.get(
"GENIE")
268 candidate = os.path.join(genie_root,
"config",
"Messenger_laconic.xml")
269 if os.path.isfile(candidate):
284 args += [
"--message-thresholds", msg_xml]
288 f
"{inputflux},{get_1D_flux_name(nupdg)}",
295 if process
is not None:
296 args += [
"--event-generator-list", process]
298 args += [
"--seed", str(seed)]
300 args += [
"--run", str(irun)]
302 return _run(args, env_vars=env_vars)
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``.
315 Input GHEP file (e.g. ``gntp.0.ghep.root``).
317 Output GST file path.
319 Per-call environment overrides.
323 subprocess.CompletedProcess
326 args = [
"gntpc",
"-i", str(inputfile),
"-f",
"gst",
"-o", str(outputfile)]
327 return _run(args, env_vars=env_vars)
330def add_hists(inputflux: PathLike, simfile: PathLike, nupdg: int) ->
None:
331 """Copy the 2D p–pT flux histogram from ``inputflux`` into ``simfile``.
336 Path to ROOT file containing TH2D flux histograms.
338 Path to the simulation ROOT file to **update** (opened in 'UPDATE' mode).
340 Neutrino PDG code used to select the histogram (see ``get_2D_flux_name``).
345 If the requested histogram is not found in ``inputflux``.
347 If ROOT fails to open either file.
351 ROOT.TFile.Open(str(inputflux),
"READ")
as infile,
352 ROOT.TFile.Open(str(simfile),
"UPDATE")
as sfile,
355 obj = infile.Get(hname)
357 raise FileNotFoundError(f
"Histogram '{hname}' not found in {inputflux}")
367 """Parse ``KEY=VAL`` pairs for ``--env`` CLI flag."""
368 out: dict[str, str |
None] = {}
371 raise ValueError(f
"Expected KEY=VALUE, got '{p}'")
372 k, v = p.split(
"=", 1)
377def main(argv: Sequence[str] |
None =
None) -> int:
378 """Entry point for the command-line interface."""
381 parser = argparse.ArgumentParser(prog=
"genie-utils", description=
"GENIE helpers with per-call env overrides")
387 help=
"Set environment variable(s) for the child process (repeatable).",
394 help=
"Unset environment variable(s) for the child process (repeatable).",
396 parser.add_argument(
"-v",
"--verbose", action=
"count", default=0, help=
"Increase verbosity.")
397 sub = parser.add_subparsers(dest=
"cmd", required=
True)
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)
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)
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)
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)
428 args = parser.parse_args(argv)
431 level = logging.WARNING
432 if args.verbose == 1:
434 elif args.verbose >= 2:
435 level = logging.DEBUG
436 logging.basicConfig(level=level, format=
"%(levelname)s %(name)s: %(message)s")
439 env_vars: dict[str, str |
None] = {}
442 for key
in args.unset:
445 if args.cmd ==
"splines":
448 targetcode=args.target,
451 outputfile=args.output,
452 env_vars=env_vars
or None,
454 elif args.cmd ==
"generate":
456 nevents=args.nevents,
460 targetcode=args.target,
463 outputfile=args.output,
464 process=args.process,
467 env_vars=env_vars
or None,
469 elif args.cmd ==
"ntuples":
471 inputfile=args.input,
472 outputfile=args.output,
473 env_vars=env_vars
or None,
475 elif args.cmd ==
"add-hists":
476 add_hists(inputflux=args.flux, simfile=args.sim, nupdg=args.pdg)
478 parser.error(
"Unknown command")
483if __name__ ==
"__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)