Skip to content
Open
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
26 changes: 16 additions & 10 deletions .github/scripts/run_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`. LHE counts can also be a MIN-MAX range.
"""

import argparse
Expand All @@ -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*ci-fragment:\s*([\w.-]+)\s*-->\s*$")
FENCE = re.compile(r"^\s*```\s*(bash|sh|shell)?\s*$")
Expand Down Expand Up @@ -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:COUNT_OR_RANGE")
parser.add_argument("--print", action="store_true", help="Print the script and exit")
args = parser.parse_args()
page = args.page.resolve()
Expand All @@ -79,14 +82,17 @@ 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:
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


Expand Down
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"}
15 changes: 15 additions & 0 deletions 2-gen-and-fastsim/2-1-event-generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,13 @@ Additional switches (for experts only):

:::

A short run at 91.2 GeV produces a 100-event LHE sample:

<!-- ci-fragment: bhlumi -->
```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.
Expand Down Expand Up @@ -738,6 +745,14 @@ babayaga -c babayaga.input -o bbyg.LHE

:::

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.

<!-- ci-fragment: babayaga -->
```bash
babayaga -f 15. -t 165. -e 91.2 -n 100 -o bbyg_100.LHE
```


### Herwig

Expand Down
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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:1-100)
Loading