From 0b77db11e469d3536bd647e2d5661ed881284238 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 12 Nov 2026 14:37:17 +0100 Subject: [PATCH 1/2] Validate BHLUMI and BabaYaga LHE output in CI --- .github/scripts/run_fragments.py | 25 ++++++++----- .github/scripts/validate_lhe.py | 37 +++++++++++++++++++ .../2-1-event-generation/README.md | 14 +++++++ CMakeLists.txt | 8 +++- 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 .github/scripts/validate_lhe.py diff --git a/.github/scripts/run_fragments.py b/.github/scripts/run_fragments.py index 7a2f6370..23a0fab8 100755 --- a/.github/scripts/run_fragments.py +++ b/.github/scripts/run_fragments.py @@ -11,7 +11,8 @@ All fragments with the same tag run, in page order, in ONE fresh bash shell inside an empty directory, so `cd` and variables carry over as they would for a reader. -Afterwards every `--validate FILE:EVENTS` output is checked with the local EDM4hep validator. +Afterwards, check EDM4hep outputs with `--validate FILE:EVENTS` and LHE outputs +with `--validate-lhe FILE:EVENTS`. """ import argparse @@ -22,7 +23,8 @@ import tempfile from pathlib import Path -from validate_edm4hep import validate +from validate_edm4hep import validate as validate_edm4hep +from validate_lhe import validate as validate_lhe MARKER = re.compile(r"^\s*\s*$") FENCE = re.compile(r"^\s*```\s*(bash|sh|shell)?\s*$") @@ -57,6 +59,7 @@ def main(): parser.add_argument("page", type=Path) parser.add_argument("tag") parser.add_argument("--validate", action="append", default=[], metavar="FILE:EVENTS") + parser.add_argument("--validate-lhe", action="append", default=[], metavar="FILE:EVENTS") parser.add_argument("--print", action="store_true", help="Print the script and exit") args = parser.parse_args() page = args.page.resolve() @@ -79,14 +82,16 @@ def main(): ) return 1 failed = False - for item in args.validate: - name, events = item.rsplit(":", 1) - try: - result = validate(work / name, int(events)) - except Exception as exc: - result = {"valid": False, "error": f"{type(exc).__name__}: {exc}"} - failed = True - print(f"{name}: {json.dumps(result, sort_keys=True)}", flush=True) + checks = [(args.validate, validate_edm4hep), (args.validate_lhe, validate_lhe)] + for items, validate in checks: + for item in items: + name, events = item.rsplit(":", 1) + try: + result = validate(work / name, int(events)) + except Exception as exc: + result = {"valid": False, "error": f"{type(exc).__name__}: {exc}"} + failed = True + print(f"{name}: {json.dumps(result, sort_keys=True)}", flush=True) return 1 if failed else 0 diff --git a/.github/scripts/validate_lhe.py b/.github/scripts/validate_lhe.py new file mode 100644 index 00000000..ffab3c1d --- /dev/null +++ b/.github/scripts/validate_lhe.py @@ -0,0 +1,37 @@ +"""Check the structure and event count of a Les Houches Event file.""" + +from xml.etree import ElementTree + + +def validate(path, expected): + if expected <= 0: + raise ValueError("Expected a positive event count") + + with path.open("rb") as stream: + elements = ElementTree.iterparse(stream, events=("start", "end")) + _, root = next(elements) + if root.tag != "LesHouchesEvents": + raise ValueError("Expected a LesHouchesEvents document") + + depth = 1 + initialized = False + count = 0 + for action, element in elements: + if action == "start": + depth += 1 + continue + if depth == 2: + if element.tag == "init": + initialized = bool(element.text and element.text.strip()) + elif element.tag == "event": + if not initialized: + raise ValueError("Missing LHE initialization before events") + count += 1 + if not element.text or not element.text.strip(): + raise ValueError(f"Empty LHE event {count}") + root.remove(element) + depth -= 1 + + if count != expected: + raise ValueError(f"Event count mismatch: {count} != {expected}") + return {"valid": True, "entries": count, "scope": "LHE structure and event count"} diff --git a/2-gen-and-fastsim/2-1-event-generation/README.md b/2-gen-and-fastsim/2-1-event-generation/README.md index da7372cb..37bfcd63 100644 --- a/2-gen-and-fastsim/2-1-event-generation/README.md +++ b/2-gen-and-fastsim/2-1-event-generation/README.md @@ -687,6 +687,13 @@ Additional switches (for experts only): ::: +A short run at 91.2 GeV produces a 100-event LHE sample: + + +```bash +BHLUMI -f 0.022 -t 0.082 -x 0.001 -e 91.2 -n 100 -o bhlumi_100.LHE +``` + ### BabaYaga `BabaYaga` is a Monte Carlo generator of two-photons events used at LEP. @@ -738,6 +745,13 @@ babayaga -c babayaga.input -o bbyg.LHE ::: +A short run at 91.2 GeV produces a 100-event LHE sample: + + +```bash +babayaga -f 15. -t 165. -e 91.2 -n 100 -o bbyg_100.LHE +``` + ### Herwig diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b08b4ff..db77f742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ add_nb_test("3-analysis/3-1-higgs-analysis/README") add_nb_test("4-full-detector-sim/FccCaloPerformance/CaloFullSimExercise") # Shell fragments run verbatim (see .github/scripts/run_fragments.py), followed by -# structural EDM4hep validation of the files the tutorial tells readers to produce. +# structural validation of the files the tutorial tells readers to produce. function(add_fragment_test page tag) get_filename_component(_page_dir ${CMAKE_SOURCE_DIR}/${page} DIRECTORY) add_test(NAME ${page}-fragments-${tag} @@ -45,3 +45,9 @@ add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" whizard-ditau add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" kkmcee-ditau --validate gen/kk_tautau_ecm91/events_1.root:10000) + +add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" bhlumi + --validate-lhe bhlumi_100.LHE:100) + +add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" babayaga + --validate-lhe bbyg_100.LHE:100) From d836ac6895dd5d7d7e115b072816c20a2f569878 Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 12 Nov 2026 15:27:03 +0100 Subject: [PATCH 2/2] Validate the accepted BabaYaga event count --- .github/scripts/run_fragments.py | 7 ++++--- .github/scripts/validate_lhe.py | 11 +++++++---- 2-gen-and-fastsim/2-1-event-generation/README.md | 3 ++- CMakeLists.txt | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.github/scripts/run_fragments.py b/.github/scripts/run_fragments.py index 23a0fab8..0d66288a 100755 --- a/.github/scripts/run_fragments.py +++ b/.github/scripts/run_fragments.py @@ -12,7 +12,7 @@ All fragments with the same tag run, in page order, in ONE fresh bash shell inside an empty directory, so `cd` and variables carry over as they would for a reader. Afterwards, check EDM4hep outputs with `--validate FILE:EVENTS` and LHE outputs -with `--validate-lhe FILE:EVENTS`. +with `--validate-lhe FILE:EVENTS`. LHE counts can also be a MIN-MAX range. """ import argparse @@ -59,7 +59,7 @@ def main(): parser.add_argument("page", type=Path) parser.add_argument("tag") parser.add_argument("--validate", action="append", default=[], metavar="FILE:EVENTS") - parser.add_argument("--validate-lhe", action="append", default=[], metavar="FILE:EVENTS") + parser.add_argument("--validate-lhe", action="append", default=[], metavar="FILE:COUNT_OR_RANGE") parser.add_argument("--print", action="store_true", help="Print the script and exit") args = parser.parse_args() page = args.page.resolve() @@ -87,7 +87,8 @@ def main(): for item in items: name, events = item.rsplit(":", 1) try: - result = validate(work / name, int(events)) + count = events if validate is validate_lhe else int(events) + result = validate(work / name, count) except Exception as exc: result = {"valid": False, "error": f"{type(exc).__name__}: {exc}"} failed = True diff --git a/.github/scripts/validate_lhe.py b/.github/scripts/validate_lhe.py index ffab3c1d..9368d0ac 100644 --- a/.github/scripts/validate_lhe.py +++ b/.github/scripts/validate_lhe.py @@ -4,8 +4,11 @@ def validate(path, expected): - if expected <= 0: - raise ValueError("Expected a positive event count") + minimum, separator, maximum = str(expected).partition("-") + minimum = int(minimum) + maximum = int(maximum) if separator else minimum + if not 0 < minimum <= maximum: + raise ValueError("Expected a positive event count or an increasing range") with path.open("rb") as stream: elements = ElementTree.iterparse(stream, events=("start", "end")) @@ -32,6 +35,6 @@ def validate(path, expected): root.remove(element) depth -= 1 - if count != expected: - raise ValueError(f"Event count mismatch: {count} != {expected}") + if not minimum <= count <= maximum: + raise ValueError(f"Event count mismatch: got {count}, expected {expected}") return {"valid": True, "entries": count, "scope": "LHE structure and event count"} diff --git a/2-gen-and-fastsim/2-1-event-generation/README.md b/2-gen-and-fastsim/2-1-event-generation/README.md index 37bfcd63..5d511e98 100644 --- a/2-gen-and-fastsim/2-1-event-generation/README.md +++ b/2-gen-and-fastsim/2-1-event-generation/README.md @@ -745,7 +745,8 @@ babayaga -c babayaga.input -o bbyg.LHE ::: -A short run at 91.2 GeV produces a 100-event LHE sample: +This short run at 91.2 GeV requests 100 trials. BabaYaga writes the events +that pass its cuts, so the LHE file can contain fewer than 100 events. ```bash diff --git a/CMakeLists.txt b/CMakeLists.txt index db77f742..88054b34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,4 +50,4 @@ add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" bhlumi --validate-lhe bhlumi_100.LHE:100) add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" babayaga - --validate-lhe bbyg_100.LHE:100) + --validate-lhe bbyg_100.LHE:1-100)