From 4337e1080266a819f80c02a7acbb24812459a0ff Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 12 Nov 2026 14:44:30 +0100 Subject: [PATCH 1/2] Validate Herwig and MadGraph5 tutorial output in CI --- .github/scripts/run_fragments.py | 11 ++++- .github/scripts/validate_hepmc.py | 40 ++++++++++++++++ .../2-1-event-generation/README.md | 46 +++++++++++++++++++ CMakeLists.txt | 6 +++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/validate_hepmc.py diff --git a/.github/scripts/run_fragments.py b/.github/scripts/run_fragments.py index 23a0fab8..abe5504f 100755 --- a/.github/scripts/run_fragments.py +++ b/.github/scripts/run_fragments.py @@ -12,7 +12,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, check EDM4hep outputs with `--validate FILE:EVENTS` and LHE outputs -with `--validate-lhe FILE:EVENTS`. +with `--validate-lhe FILE:EVENTS`, or HepMC3 ASCII outputs with +`--validate-hepmc FILE:EVENTS`. """ import argparse @@ -24,6 +25,7 @@ from pathlib import Path from validate_edm4hep import validate as validate_edm4hep +from validate_hepmc import validate as validate_hepmc from validate_lhe import validate as validate_lhe MARKER = re.compile(r"^\s*\s*$") @@ -60,6 +62,7 @@ def main(): 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-hepmc", 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() @@ -82,7 +85,11 @@ def main(): ) return 1 failed = False - checks = [(args.validate, validate_edm4hep), (args.validate_lhe, validate_lhe)] + checks = [ + (args.validate, validate_edm4hep), + (args.validate_lhe, validate_lhe), + (args.validate_hepmc, validate_hepmc), + ] for items, validate in checks: for item in items: name, events = item.rsplit(":", 1) diff --git a/.github/scripts/validate_hepmc.py b/.github/scripts/validate_hepmc.py new file mode 100644 index 00000000..c3a59279 --- /dev/null +++ b/.github/scripts/validate_hepmc.py @@ -0,0 +1,40 @@ +"""Check the framing and event count of a HepMC3 ASCII file.""" + +START = "HepMC::Asciiv3-START_EVENT_LISTING" +END = "HepMC::Asciiv3-END_EVENT_LISTING" + + +def validate(path, expected): + if expected <= 0: + raise ValueError("Expected a positive event count") + + count = 0 + with path.open() as stream: + if not next(stream, "").startswith("HepMC::Version ") or next(stream, "").strip() != START: + raise ValueError("Expected a HepMC3 ASCII header") + + declared_particles = particles = 0 + for line in stream: + record = line.split() + if not record: + continue + if record[0] in ("E", END): + if particles != declared_particles: + raise ValueError(f"Particle count mismatch in HepMC event {count}") + if record[0] == END: + break + count += 1 + declared_particles = int(record[3]) + if declared_particles <= 0: + raise ValueError(f"Empty HepMC event {count}") + particles = 0 + elif record[0] == "P": + particles += 1 + else: + raise ValueError("Missing HepMC3 end marker") + if any(line.strip() for line in stream): + raise ValueError("Unexpected data after the HepMC3 end marker") + + if count != expected: + raise ValueError(f"Event count mismatch: {count} != {expected}") + return {"valid": True, "entries": count, "scope": "HepMC3 ASCII framing 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..3b2bc01d 100644 --- a/2-gen-and-fastsim/2-1-event-generation/README.md +++ b/2-gen-and-fastsim/2-1-event-generation/README.md @@ -764,6 +764,27 @@ which Herwig /cvmfs/sw.hsf.org/spackages7/herwig3/7.2.3/x86_64-centos7-gcc11.2.0-opt/q3pxd/bin/Herwig ``` +This example runs Herwig's built-in electron–positron setup at 91.2 GeV. +It generates 100 quark–antiquark events and writes the showered and hadronized +events in HepMC3 ASCII format. + + +```bash +cat > fcc-lep.in <<'HERWIG' +read snippets/EECollider.in +cd /Herwig/MatrixElements +insert SubProcess:MatrixElements 0 MEee2gZ2qq +cd /Herwig/Generators +set EventGenerator:EventHandler:LuminosityFunction:Energy 91.2 +read snippets/HepMC.in +set /Herwig/Analysis/HepMC:Format GenEventHepMC3 +set /Herwig/Analysis/HepMC:Filename herwig_100.hepmc +saverun fcc-lep EventGenerator +HERWIG +Herwig read fcc-lep.in +Herwig run fcc-lep.run -N 100 +``` + ### MadGraph5 @@ -776,6 +797,31 @@ which mg5_aMC /cvmfs/sw.hsf.org/spackages7/madgraph5amc/2.8.1/x86_64-centos7-gcc11.2.0-opt/nlauf/bin/mg5_aMC ``` +This example generates 100 electron–positron dimuon events at 91.2 GeV. +MadGraph writes the unweighted hard-process events in LHE format. + + +```bash +cat > fcc-mumu.mg5 <<'MADGRAPH' +set run_mode 0 +set automatic_html_opening False +generate e+ e- > mu+ mu- +output fcc_mumu -f +launch fcc_mumu +shower=OFF +done +set lpp1 0 +set lpp2 0 +set ebeam1 45.6 +set ebeam2 45.6 +set nevents 100 +set use_syst False +done +MADGRAPH +mg5_aMC fcc-mumu.mg5 +gzip -dc fcc_mumu/Events/run_01/unweighted_events.lhe.gz > madgraph5_100.lhe +``` + ## Hands-on case study: ditau events with Pythia8, Whizard and KKMCee diff --git a/CMakeLists.txt b/CMakeLists.txt index db77f742..ca014e91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,3 +51,9 @@ add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" bhlumi add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" babayaga --validate-lhe bbyg_100.LHE:100) + +add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" herwig + --validate-hepmc herwig_100.hepmc:100) + +add_fragment_test("2-gen-and-fastsim/2-1-event-generation/README" madgraph5 + --validate-lhe madgraph5_100.lhe:100) From 3846d8ae58cb89b133eec288074d743c697ad6ae Mon Sep 17 00:00:00 2001 From: Ianna Osborne Date: Thu, 12 Nov 2026 15:27:40 +0100 Subject: [PATCH 2/2] Set up Herwig runtime dependencies in CI --- .github/scripts/setup_herwig_ci.sh | 36 ++++++++++++++++++++++++++++++ .github/workflows/test.yml | 1 + 2 files changed, 37 insertions(+) create mode 100644 .github/scripts/setup_herwig_ci.sh diff --git a/.github/scripts/setup_herwig_ci.sh b/.github/scripts/setup_herwig_ci.sh new file mode 100644 index 00000000..2a50e0ee --- /dev/null +++ b/.github/scripts/setup_herwig_ci.sh @@ -0,0 +1,36 @@ +# Source after the Key4hep setup, from the CTest build directory. +# Some Key4hep builds omit ThePEG's nested lib directory from Herwig's search path. +herwig_binary=$(command -v Herwig) +herwig_links=$(ldd "$herwig_binary") +thepeg_soname=$(awk '$1 ~ /^libThePEG\.so\./ && $3 == "not" {print $1; exit}' <<< "$herwig_links") +if [[ -n "$thepeg_soname" ]]; then + # Look alongside the selected Herwig package in the active CVMFS stack. + herwig_package=$(dirname "$(dirname "$(realpath "$herwig_binary")")") + packages_dir=$(dirname "$herwig_package") + stack_dir=$(dirname "$packages_dir") + thepeg_libraries=() + for candidate in "$stack_dir"/thepeg/*/lib/ThePEG/"$thepeg_soname" \ + "$packages_dir"/thepeg-*/lib/ThePEG/"$thepeg_soname"; do + if [[ -f "$candidate" ]]; then + thepeg_libraries+=("$candidate") + fi + done + if [[ ${#thepeg_libraries[@]} -ne 1 ]]; then + printf 'Expected one %s alongside Herwig in %s, found %s\n' \ + "$thepeg_soname" "$herwig_package" "${#thepeg_libraries[@]}" >&2 + return 1 + fi + thepeg_libdir=$(dirname "${thepeg_libraries[0]}") + export LD_LIBRARY_PATH="$thepeg_libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" +fi + +# Herwig loads these defaults even for the electron–positron steering. +pdf_dir="$PWD/herwig-pdf" +for pdf_set in CT14lo CT14nlo; do + mkdir -p "$pdf_dir/$pdf_set" + for pdf_file in "$pdf_set.info" "${pdf_set}_0000.dat"; do + curl -fsSL --retry 3 "https://lhapdfsets.web.cern.ch/current/$pdf_set/$pdf_file" \ + -o "$pdf_dir/$pdf_set/$pdf_file" + done +done +export LHAPDF_DATA_PATH="$pdf_dir${LHAPDF_DATA_PATH:+:$LHAPDF_DATA_PATH}" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e742a34a..de42595b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,6 +36,7 @@ jobs: source ${{ matrix.STACK }} ${{ contains(matrix.STACK, 'sw-nightlies') && '--spack' || '' }};\ mkdir build;\ cd build;\ + source ../.github/scripts/setup_herwig_ci.sh;\ cmake ..;\ make;\ ctest --output-on-failure --no-tests=error;