Skip to content

Commit

Permalink
refactor: Refactor OpenDataDetector path discovery in Examples (acts-…
Browse files Browse the repository at this point in the history
…project#3036)

We currently require to point the python path into a python examples directory so we can discover the ODD directory automatically. I think it might be easier to require setting an environment variable for the ODD path and start discovery from there.

This ODD path will be automatically set by `python/setup.sh`. Apart from that the user can set a path manually as before.
  • Loading branch information
andiwand authored and asalzburger committed May 21, 2024
1 parent d7d6e3c commit f11b829
Show file tree
Hide file tree
Showing 26 changed files with 116 additions and 209 deletions.
6 changes: 1 addition & 5 deletions CI/physmon/physmon_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from pathlib import Path

import acts

from common import getOpenDataDetectorDirectory
from acts.examples.odd import getOpenDataDetector

PhysmonSetup = collections.namedtuple(
Expand Down Expand Up @@ -35,9 +33,7 @@ def makeSetup() -> PhysmonSetup:
level=acts.logging.INFO,
)

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory(), matDeco
)
detector, trackingGeometry, decorators = getOpenDataDetector(matDeco)
setup = PhysmonSetup(
detector=detector,
trackingGeometry=trackingGeometry,
Expand Down
23 changes: 19 additions & 4 deletions Examples/Python/python/acts/examples/odd.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import os
import sys
import math
from pathlib import Path
from math import sqrt
import sys, os
from typing import Optional
import acts
import acts.examples


def getOpenDataDetectorDirectory():
odd_dir = os.environ.get("ODD_PATH")
if odd_dir is None:
raise RuntimeError("ODD_PATH environment variable not set")
odd_dir = Path(odd_dir)
return odd_dir


def getOpenDataDetector(
odd_dir: Path,
mdecorator=None,
odd_dir: Optional[Path] = None,
logLevel=acts.logging.INFO,
):
import acts.examples.dd4hep

customLogLevel = acts.examples.defaultLogging(logLevel=logLevel)

if odd_dir is None:
odd_dir = getOpenDataDetectorDirectory()
if not odd_dir.exists():
raise RuntimeError(f"OpenDataDetector not found at {odd_dir}")

odd_xml = odd_dir / "xml" / "OpenDataDetector.xml"
if not odd_xml.exists():
raise RuntimeError(f"OpenDataDetector.xml not found at {odd_xml}")
Expand Down Expand Up @@ -54,7 +69,7 @@ def getOpenDataDetector(

def geoid_hook(geoid, surface):
if geoid.volume() in volumeRadiusCutsMap:
r = sqrt(surface.center()[0] ** 2 + surface.center()[1] ** 2)
r = math.sqrt(surface.center()[0] ** 2 + surface.center()[1] ** 2)

geoid.setExtra(1)
for cut in volumeRadiusCutsMap[geoid.volume()]:
Expand Down
10 changes: 6 additions & 4 deletions Examples/Python/setup.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ export PYTHONPATH=$python_dir:$PYTHONPATH

# Make ODD available if possible
# This seems to be only reliable on Linux for now, not on MacOS
odd_dir=$python_dir/../thirdparty/OpenDataDetector/factory
if [ -d "$odd_dir" ]; then
odd_src_dir=@CMAKE_CURRENT_SOURCE_DIR@/../../thirdparty/OpenDataDetector
export ODD_PATH=$odd_src_dir
odd_bin_dir=$python_dir/../thirdparty/OpenDataDetector/factory
if [ -d "$odd_bin_dir" ]; then
if [[ "$(uname -s)" == "Linux" ]]; then
echo "INFO: Found OpenDataDetector and set it up"
export LD_LIBRARY_PATH=$odd_dir:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$odd_bin_dir:$LD_LIBRARY_PATH
fi
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "INFO: Found OpenDataDetector and set it up"
export DYLD_LIBRARY_PATH=$odd_dir:$DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$odd_bin_dir:$DYLD_LIBRARY_PATH
fi
fi

Expand Down
11 changes: 3 additions & 8 deletions Examples/Python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

import helpers
import helpers.hash_root
from common import getOpenDataDetectorDirectory
from acts.examples.odd import getOpenDataDetector

import pytest

import acts
import acts.examples
from acts.examples.odd import getOpenDataDetector

try:
import ROOT
Expand Down Expand Up @@ -267,9 +266,7 @@ def detector_config(request):
srcdir / "thirdparty/OpenDataDetector/data/odd-material-maps.root",
level=acts.logging.INFO,
)
detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory(), matDeco
)
detector, trackingGeometry, decorators = getOpenDataDetector(matDeco)
return DetectorConfig(
detector,
trackingGeometry,
Expand Down Expand Up @@ -367,9 +364,7 @@ def _factory(s):
def _do_material_recording(d: Path):
from material_recording import runMaterialRecording

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory()
)
detector, trackingGeometry, decorators = getOpenDataDetector()

detectorConstructionFactory = (
acts.examples.geant4.dd4hep.DDG4DetectorConstructionFactory(detector)
Expand Down
27 changes: 8 additions & 19 deletions Examples/Python/tests/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

from helpers import dd4hepEnabled

from common import getOpenDataDetectorDirectory
from acts.examples.odd import getOpenDataDetector

import acts.examples
from acts.examples.odd import getOpenDataDetector


def count_surfaces(geo):
Expand Down Expand Up @@ -55,30 +53,21 @@ def test_telescope_geometry():

@pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep is not set up")
def test_odd():
config = acts.MaterialMapJsonConverter.Config()
matDeco = acts.JsonMaterialDecorator(
rConfig=config,
jFileName=str(
getOpenDataDetectorDirectory() / "config/odd-material-mapping-config.json"
),
level=acts.logging.WARNING,
)
detector, trackingGeometry, decorators = getOpenDataDetector()

detector, geo, _ = getOpenDataDetector(getOpenDataDetectorDirectory(), matDeco)
trackingGeometry.visitSurfaces(check_extra_odd)

geo.visitSurfaces(check_extra_odd)

assert count_surfaces(geo) == 18824
assert count_surfaces(trackingGeometry) == 18824


def test_aligned_detector():
detector, geo, deco = acts.examples.AlignedDetector.create()
detector, trackingGeometry, decorators = acts.examples.AlignedDetector.create()

assert detector is not None
assert geo is not None
assert deco is not None
assert trackingGeometry is not None
assert decorators is not None

assert count_surfaces(geo) == 18728
assert count_surfaces(trackingGeometry) == 18728


import itertools
Expand Down
32 changes: 7 additions & 25 deletions Examples/Python/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from helpers import (
geant4Enabled,
rootEnabled,
dd4hepEnabled,
hepmc3Enabled,
pythia8Enabled,
Expand All @@ -23,18 +22,14 @@
failure_threshold,
)

pytestmark = pytest.mark.skipif(not rootEnabled, reason="ROOT not set up")


import acts
from acts.examples import (
Sequencer,
GenericDetector,
AlignedDetector,
)

from acts.examples.odd import getOpenDataDetector
from common import getOpenDataDetectorDirectory


u = acts.UnitConstants

Expand Down Expand Up @@ -150,9 +145,7 @@ def test_fatras(trk_geo, tmp_path, field, assert_root_hash):
@pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep not set up")
def test_geant4(tmp_path, assert_root_hash):
# This test literally only ensures that the geant 4 example can run without erroring out
getOpenDataDetector(
getOpenDataDetectorDirectory()
) # just to make sure it can build
getOpenDataDetector() # just to make sure it can build

csv = tmp_path / "csv"
csv.mkdir()
Expand Down Expand Up @@ -672,9 +665,7 @@ def test_material_mapping(material_recording, tmp_path, assert_root_hash):

s = Sequencer(numThreads=1)

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory()
)
detector, trackingGeometry, decorators = getOpenDataDetector()

from material_mapping import runMaterialMapping

Expand Down Expand Up @@ -715,7 +706,6 @@ def test_material_mapping(material_recording, tmp_path, assert_root_hash):
del detector

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory(),
mdecorator=acts.IMaterialDecorator.fromFile(mat_file),
)

Expand Down Expand Up @@ -752,7 +742,6 @@ def test_volume_material_mapping(material_recording, tmp_path, assert_root_hash)
assert json.load(fh)

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory(),
mdecorator=acts.IMaterialDecorator.fromFile(geo_map),
)

Expand Down Expand Up @@ -796,7 +785,6 @@ def test_volume_material_mapping(material_recording, tmp_path, assert_root_hash)
del detector

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory(),
mdecorator=acts.IMaterialDecorator.fromFile(mat_file),
)

Expand Down Expand Up @@ -826,7 +814,7 @@ def test_volume_material_mapping(material_recording, tmp_path, assert_root_hash)
[
(GenericDetector.create, 450),
pytest.param(
functools.partial(getOpenDataDetector, getOpenDataDetectorDirectory()),
getOpenDataDetector,
540,
marks=[
pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep not set up"),
Expand Down Expand Up @@ -1141,9 +1129,7 @@ def test_ckf_tracks_example(
@pytest.mark.slow
def test_full_chain_odd_example(tmp_path):
# This test literally only ensures that the full chain example can run without erroring out
getOpenDataDetector(
getOpenDataDetectorDirectory()
) # just to make sure it can build
getOpenDataDetector() # just to make sure it can build

script = (
Path(__file__).parent.parent.parent.parent
Expand Down Expand Up @@ -1173,9 +1159,7 @@ def test_full_chain_odd_example(tmp_path):
@pytest.mark.slow
def test_full_chain_odd_example_pythia_geant4(tmp_path):
# This test literally only ensures that the full chain example can run without erroring out
getOpenDataDetector(
getOpenDataDetectorDirectory()
) # just to make sure it can build
getOpenDataDetector() # just to make sure it can build

script = (
Path(__file__).parent.parent.parent.parent
Expand Down Expand Up @@ -1217,9 +1201,7 @@ def test_ML_Ambiguity_Solver(tmp_path, assert_root_hash):
output_dir = "odd_output"
assert not (tmp_path / root_file).exists()
# This test literally only ensures that the full chain example can run without erroring out
getOpenDataDetector(
getOpenDataDetectorDirectory()
) # just to make sure it can build
getOpenDataDetector() # just to make sure it can build

script = (
Path(__file__).parent.parent.parent.parent
Expand Down
8 changes: 2 additions & 6 deletions Examples/Python/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
edm4hepEnabled,
AssertCollectionExistsAlg,
)
from common import getOpenDataDetectorDirectory

from acts.examples.odd import getOpenDataDetector

import acts
from acts import PlanarModuleStepper, UnitConstants as u
Expand All @@ -30,6 +27,7 @@
PlanarSteppingAlgorithm,
Sequencer,
)
from acts.examples.odd import getOpenDataDetector, getOpenDataDetectorDirectory


@pytest.mark.root
Expand Down Expand Up @@ -367,9 +365,7 @@ def test_edm4hep_simhit_particle_reader(tmp_path):

assert os.path.exists(tmp_file)

detector, trackingGeometry, decorators = getOpenDataDetector(
getOpenDataDetectorDirectory()
)
detector, trackingGeometry, decorators = getOpenDataDetector()

s = Sequencer(numThreads=1)

Expand Down
6 changes: 1 addition & 5 deletions Examples/Python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
)

import acts

from common import getOpenDataDetectorDirectory

from acts import PlanarModuleStepper, UnitConstants as u


from acts.examples import (
ObjPropagationStepsWriter,
TrackFinderPerformanceWriter,
Expand Down Expand Up @@ -51,6 +46,7 @@
Sequencer,
GenericDetector,
)
from acts.examples.odd import getOpenDataDetectorDirectory


@pytest.mark.obj
Expand Down
Loading

0 comments on commit f11b829

Please sign in to comment.