Skip to content

k4bench.cli

k4bench.cli

Command-line interface for k4bench.

Entry point registered as k4bench in pyproject.toml.

Usage examples

Single baseline run::

k4bench --xml ALLEGRO.xml \
         --ddsim-args="--enableGun --gun.particle e- --gun.distribution uniform"

Full sweep (baseline + one run per detector removed)::

k4bench --xml ALLEGRO.xml --sweep \
         --ddsim-args="--enableGun --gun.particle e- --gun.distribution uniform"

Simulate with only specific detectors::

k4bench --xml ALLEGRO.xml \
         --include-only ECalBarrel HCalBarrel \
         --ddsim-args="--enableGun --gun.particle e- --gun.distribution uniform"

Simulate with all detectors except specific ones::

k4bench --xml ALLEGRO.xml \
         --exclude-only ECalBarrel HCalBarrel \
         --ddsim-args="--enableGun --gun.particle e- --gun.distribution uniform"

Control output::

k4bench --xml ALLEGRO.xml \
         --output-dir logs/ \
         --pickle results.pkl \
         --ddsim-args="--enableGun --gun.particle e- --gun.distribution uniform"

main

main(argv: list[str] | None = None) -> int

Parse arguments, run the benchmark, save results.

Returns the exit code (0 = success, 1 = error).

Source code in k4bench/cli.py
def main(argv: list[str] | None = None) -> int:
    """Parse arguments, run the benchmark, save results.

    Returns the exit code (0 = success, 1 = error).
    """
    parser = _build_parser()
    args = parser.parse_args(argv)

    if args.output_dir is None:
        args.output_dir = DEFAULT_LOG_ROOT / args.xml.stem

    try:
        config = _build_config(args)
    except (ValueError, SystemExit) as exc:
        print(f"Error: {exc}", file=sys.stderr)
        return 1

    results = run_sweep(config)

    print_summary(results)

    save_csv(results, args.output_dir)

    if args.pickle:
        pickle_path = args.output_dir / args.pickle
        pickle_path.parent.mkdir(parents=True, exist_ok=True)
        pickle_path.write_bytes(pickle.dumps(results))
        print(f"Results pickled to {pickle_path}")

    failed = [r for r in results if not r.succeeded]
    if failed:
        print(f"\n{len(failed)} run(s) failed: {[r.label for r in failed]}")
        return 1

    return 0