feat: add example Python geometry plugin infrastructure#1094
Open
wdconinc wants to merge 7 commits into
Open
Conversation
Introduces a C++ trampoline plugin (epic_PythonDetector) that delegates
detector construction to a Python function, enabling geometry to be written
in Python using cppyy bindings instead of C++.
New files:
- src/PythonDetector_geo.cpp: DECLARE_DETELEMENT trampoline; reads 'module',
'function', and 'pythonpath' (with ${VAR} expansion) from the compact XML
<detector> element, calls the Python function via TPython::Exec, and
retrieves the resulting DetElement via SwapWithObjAtAddr.
- python/epic_geo_helpers.py: shared helper module providing cppyy workarounds
(XmlChar* helpers, xml_coll_t handle casting, operator bool() fix) and
type aliases; import with 'from epic_geo_helpers import *'.
- python/SimpleDisk_python_geo.py: working Python port of
SimpleDiskDetector_geo.cpp demonstrating the plugin interface.
- compact/tests/python_disk.xml: test compact XML using epic_PythonDetector.
- python/README.md: documentation covering usage, function signature,
helper module, and cppyy gotchas.
CMakeLists.txt: link ROOT::ROOTTPython into the epic plugin library and
install the python/ directory to share/epic/python/.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The try/except+traceback.print_exc() wrapper is now emitted inside the generated Python script in epic_PythonDetector, so individual plugin functions need no boilerplate to surface readable exception output. SimpleDisk_python_geo.py simplified: the outer create_detector wrapper and inner _create_detector_impl are merged back into a single create_detector function. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add #ifndef EPIC_PYTHON_HELPERS_DEFINED idempotency guard to cppyy.cppdef - Add _getAttrStr and _getAttrDbl Python wrappers (were missing) - Expand type aliases to cover all common shapes (Box, CutTube, Cone, ConeSegment, Sphere, Torus, Polycone, Trapezoid, SubtractionSolid, UnionSolid, IntersectionSolid) and transforms (Translation3D, Rotation3D, RotationX, RotationY, RotationZ, RotationZYX) - Improve docstrings and section comments to match upstream quality - Fix RotationY alias (was cppyy.gbl.ROOT.Math.RotationY, now _dd4hep.RotationY) - Update __all__ to reflect all additions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dd4hep::s (= second) in DD4hepUnits.h caused a shadow warning treated as error with -Werror,-Wshadow. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds infrastructure to support DD4hep detector geometry plugins written in Python by introducing a C++ trampoline detector element (epic_PythonDetector) that delegates construction to a Python function via ROOT/TPython, plus shared Python cppyy helper utilities and an example port.
Changes:
- Add
epic_PythonDetectortrampoline plugin that imports/calls a Python module function and transfers the resultingdd4hep::DetElementback to C++. - Add
python/epic_geo_helpers.py(cppyy workarounds + type aliases) andpython/SimpleDisk_python_geo.pyexample detector implementation. - Add documentation and a compact XML smoke-test; update build/install to link TPython and install Python sources.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/PythonDetector_geo.cpp | New C++ trampoline detector plugin that calls into Python and returns a DetElement. |
| python/epic_geo_helpers.py | Shared helper module providing cppyy/DD4hep XML workarounds and common type aliases. |
| python/SimpleDisk_python_geo.py | Example Python geometry plugin (port of SimpleDiskDetector_geo.cpp) demonstrating usage. |
| python/README.md | User documentation for the Python plugin mechanism and helper APIs. |
| compact/tests/python_disk.xml | Minimal compact XML to smoke-test the Python detector trampoline. |
| CMakeLists.txt | Link TPython into the plugin library and install Python .py files. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Add py_str() helper that produces a properly escaped Python single-quoted string literal from a C++ string, handling backslashes, quotes, newlines, carriage returns, tabs, and non-printable bytes. Use py_str() for pythonpath and module name injections. Replace _m.func() attribute access with getattr(_m, py_str(func))() so the function name also goes through a proper string literal rather than raw code injection. This prevents syntax errors (and potential misbehaviour) when any of these values contain single quotes, backslashes, or other special characters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Capybara summary for PR 1094
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Briefly, what does this PR introduce? Please link to any relevant presentations or discussions.
This PR introduces a C++ trampoline plugin (
epic_PythonDetector) that delegates detector construction to a Python function, enabling geometry to be written in Python using cppyy bindings instead of C++.New files:
src/PythonDetector_geo.cpp:DECLARE_DETELEMENTtrampoline; reads 'module', 'function', and 'pythonpath' (with${VAR}expansion) from the compact XML<detector>element, calls the Python function viaTPython::Exec, and retrieves the resultingDetElementviaSwapWithObjAtAddr.python/epic_geo_helpers.py: shared helper module providing cppyy workarounds (XmlChar*helpers,xml_coll_thandle casting,operator bool()fix) and type aliases; import withfrom epic_geo_helpers import *.python/SimpleDisk_python_geo.py: working Python port ofSimpleDiskDetector_geo.cppdemonstrating the plugin interface.compact/tests/python_disk.xml: test compact XML usingepic_PythonDetector.python/README.md: documentation covering usage, function signature, helper module, and cppyy gotchas.Modified files:
CMakeLists.txt: linkROOT::ROOTTPythoninto the epic plugin library and install thepython/directory toshare/epic/python/.What is the urgency of this PR?
What kind of change does this PR introduce?
Please check if any of the following apply
Ideation by human, problem space exploration with GitHub Copilot (Claude Sonnet 4.6), initial implementation by GitHub Copilot (Claude Sonnet 4.6), testing, review and modifications by human.