import argparse
import sys
from pathlib import Path
from pwassist.parser import NamingScheme
from pwassist.pipeline.pipeline import Pipeline, PipelineConfig
_BIN_DIMENSIONS = {
"t": "t",
"e": "energy",
"energy": "energy",
"m": "mass",
"mass": "mass",
}
def _parse_bin(text: str) -> tuple[str, float, float]:
"""Parse 'DIM=LOW,HIGH' (DIM: t, e/energy, m/mass) into (dimension, low, high)."""
try:
dim, rng = text.split("=")
low, high = map(float, rng.split(","))
return _BIN_DIMENSIONS[dim.strip().lower()], low, high
except ValueError, KeyError:
raise argparse.ArgumentTypeError(
f"invalid bin '{text}'; expected DIM=LOW,HIGH with DIM in t, e, m"
" (e.g. t=0.075,0.100)"
)
[docs]
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="pwassist",
description="Catalog, preprocess, and assemble PWA fit results into "
"a Results object, optionally generating plots.",
)
parser.add_argument(
"root_dir", type=Path, help="Parent dir of mass_X-Y bin subdirectories."
)
parser.add_argument(
"--scheme",
choices=[s.value for s in NamingScheme],
default="auto",
help=(
"Naming scheme for amplitudes. If not specified, it will attempt to "
"auto-detect the scheme from a list of recognized schemes."
),
)
parser.add_argument(
"--final-state-parity",
type=int,
choices=[1, -1],
default=None,
help=(
"Total parity of final state particles (1 or -1). If your naming scheme"
" does not specify parity, then not setting this will affect your plot"
" legends."
),
)
parser.add_argument(
"--skip-steps",
nargs="*",
default=None,
help="Names of preprocessing steps you would like to skip.",
)
parser.add_argument(
"--save",
type=Path,
default=None,
help="Path to pickle the assembled Results object to.",
)
parser.add_argument(
"--write-report",
type=Path,
default=None,
help="Path to write the pipeline report summary to.",
)
parser.add_argument("--no-plots", action="store_true", help="Skip plot generation.")
parser.add_argument(
"--ignore-csv", nargs="*", default=None, help="CSV file names to ignore."
)
parser.add_argument(
"--ignore-bin",
type=_parse_bin,
action="append",
default=None,
metavar="DIM=LOW,HIGH",
help=(
"Kinematic bin to ignore, with all its files (repeatable). DIM is t, e,"
" or m; e.g. --ignore-bin t=0.075,0.100 drops that t-bin for all"
" energies and masses."
),
)
parser.add_argument(
"--plot-dir",
type=Path,
default=None,
help="Directory to write plots to.",
)
parser.add_argument(
"--coherent-sums",
nargs="*",
default=None,
help="Specific coherent sum group keys to plot (default: all).",
)
parser.add_argument("-v", "--verbose", action="store_true")
return parser
[docs]
def main(argv: list[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
config = PipelineConfig(
root_dir=args.root_dir,
naming_scheme=args.scheme,
final_state_parity=args.final_state_parity,
skip_preprocess_steps=args.skip_steps,
make_plots=not args.no_plots,
plot_output_dir=args.plot_dir,
coherent_sum_groups=args.coherent_sums,
save_path=args.save,
ignore_csv_files=args.ignore_csv,
ignore_bins=args.ignore_bin,
verbose=args.verbose,
)
try:
results, report = Pipeline(config).run()
if args.write_report:
# ensure extension is .txt
report_path = args.write_report.with_suffix(".txt")
if report_path.exists():
print(f"Warning: Overwriting existing report at {report_path}.")
report_path.write_text(report.summary())
else:
print(report.summary())
return 0
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())