Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/scripts/run_fragments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""Run tagged shell fragments of a tutorial page exactly as written, then check outputs.

Mark each fence to run with an HTML comment on the line before it (invisible in
the rendered page):

<!-- ci-fragment: pythia8 -->
```bash
k4run pythia.py -n 500 ...
```

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`, or HepMC3 ASCII outputs with
`--validate-hepmc FILE:EVENTS`. LHE counts can also be a MIN-MAX range.
"""

import argparse
import json
import re
import subprocess
import sys
import tempfile
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*ci-fragment:\s*([\w.-]+)\s*-->\s*$")
FENCE = re.compile(r"^\s*```\s*(bash|sh|shell)?\s*$")


def extract(page, tag):
lines = page.read_text().splitlines()
fragments = []
for number, line in enumerate(lines):
match = MARKER.match(line)
if not match or match.group(1) != tag:
continue
opening = lines[number + 1] if number + 1 < len(lines) else ""
if not FENCE.match(opening) or not FENCE.match(opening).group(1):
sys.exit(f"{page}:{number + 1}: ci-fragment marker must precede a ```bash fence")
body = []
for inner in lines[number + 2 :]:
if FENCE.match(inner):
break
body.append(inner)
else:
sys.exit(f"{page}:{number + 2}: unterminated fence")
fragments.append((number + 3, "\n".join(body)))
if not fragments:
# Never pass vacuously: a renamed tag or reformatted page must fail loudly.
sys.exit(f"No fragments tagged {tag!r} in {page}")
return fragments


def main():
parser = argparse.ArgumentParser(description=__doc__)
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:COUNT_OR_RANGE")
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()
script = ["set -euo pipefail"]
for line, body in extract(page, args.tag):
script += [f"echo '+++ {page.name}:{line}' >&2", body]
script = "\n".join(script) + "\n"
if args.print:
print(script, end="")
return 0
with tempfile.TemporaryDirectory(prefix=f"fragments-{args.tag}-") as work:
work = Path(work)
(work / "fragments.sh").write_text(script)
status = subprocess.run(["bash", "fragments.sh"], cwd=work).returncode
if status:
print(
f"FAILED: fragment exited with status {status}; the failing fence is the "
f"last '+++ {page.name}:LINE' marker above",
file=sys.stderr,
)
return 1
failed = False
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)
try:
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
print(f"{name}: {json.dumps(result, sort_keys=True)}", flush=True)
return 1 if failed else 0


if __name__ == "__main__":
sys.exit(main())
36 changes: 36 additions & 0 deletions .github/scripts/setup_herwig_ci.sh
Original file line number Diff line number Diff line change
@@ -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}"
82 changes: 82 additions & 0 deletions .github/scripts/validate_edm4hep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Adapted from ianna/mcp-server-key4hep at bde7af183f57d14cabc809f1d35b281cd4d5dd69.
"""Read every event with podio's backend-independent reader.

This is structural validation, not a physics-reference comparison. Importing
edm4hep loads dictionaries required by podio. Run in the selected stack.
"""

import math
from pathlib import Path


def validate(path, expected):
import edm4hep # noqa: F401
import ROOT
from podio.reading import get_reader

if expected <= 0 or not Path(path).is_file() or Path(path).stat().st_size == 0:
raise ValueError("Expected a nonempty output and a positive event count")
reader = get_reader(str(path))
if "events" not in reader.categories:
raise ValueError("No events category")
frames = reader.get("events")
if len(frames) != expected:
raise ValueError(f"Event count mismatch: {len(frames)} != {expected}")
particles_total = 0
schema = None
for frame in frames:
names = set(frame.getAvailableCollections())
if "MCParticles" not in names:
raise ValueError("Missing MCParticles collection")
if schema is None:
schema = sorted(names)
elif sorted(names) != schema:
raise ValueError("Collection names change between events")
# Force deserialization of every collection, not just the tree header.
for name in names:
collection = frame.get(name)
for _ in collection:
pass
particles = frame.get("MCParticles")
# podio returns std::string_view from getTypeName(). Depending on the
# cppyy/ROOT version, str(view) may describe the proxy instead of its
# contents. Check the dictionary-backed C++ class, not its string proxy.
if not isinstance(particles, ROOT.edm4hep.MCParticleCollection):
actual = getattr(type(particles), "__cpp_name__", type(particles).__name__)
raise ValueError(
"MCParticles has the wrong EDM4hep collection type: "
f"expected edm4hep::MCParticleCollection, got {actual}"
)
identifiers = set()
for particle in particles:
oid = particle.getObjectID()
identifiers.add((oid.collectionID, oid.index))
momentum = particle.getMomentum()
if not all(
math.isfinite(v)
for v in (
momentum.x,
momentum.y,
momentum.z,
particle.getMass(),
particle.getCharge(),
)
):
raise ValueError("Non-finite MCParticle values")
for particle in particles:
for related in [*particle.getParents(), *particle.getDaughters()]:
if not related.isAvailable():
raise ValueError("Unresolved MCParticle relation")
oid = related.getObjectID()
if (oid.collectionID, oid.index) not in identifiers:
raise ValueError("MCParticle relation leaves the required collection")
particles_total += len(particles)
if particles_total == 0:
raise ValueError("No MCParticles in generated sample")
return {
"valid": True,
"entries": expected,
"particles": particles_total,
"collections": schema,
"scope": "EDM4hep structure and MCParticle relations",
}
40 changes: 40 additions & 0 deletions .github/scripts/validate_hepmc.py
Original file line number Diff line number Diff line change
@@ -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"}
40 changes: 40 additions & 0 deletions .github/scripts/validate_lhe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Check the structure and event count of a Les Houches Event file."""

from xml.etree import ElementTree


def validate(path, expected):
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"))
_, 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 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"}
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ jobs:
-d ghcr.io/key4hep/key4hep-images/alma9:latest /bin/bash
- name: Run tests
run: |
docker exec CI_container /bin/bash -c \
'curl -LO https://github.com/jgm/pandoc/releases/download/3.10/pandoc-3.10-linux-amd64.tar.gz;\
docker exec CI_container /bin/bash -e -c \
'curl -fLO https://github.com/jgm/pandoc/releases/download/3.10/pandoc-3.10-linux-amd64.tar.gz;\
tar -xzf pandoc-3.10-linux-amd64.tar.gz;\
cp pandoc-3.10/bin/pandoc /usr/local/bin/;\
cd ./Package;\
source ${{ matrix.STACK }} ${{ contains(matrix.STACK, 'sw-nightlies') && '--spack' || '' }};\
mkdir build;\
cd build;\
source ../.github/scripts/setup_herwig_ci.sh;\
cmake ..;\
make;\
make test;
ctest --output-on-failure --no-tests=error;
'
Loading