Skip to content

feat: add example Python geometry plugin infrastructure#1094

Open
wdconinc wants to merge 7 commits into
mainfrom
python-geometry-plugins
Open

feat: add example Python geometry plugin infrastructure#1094
wdconinc wants to merge 7 commits into
mainfrom
python-geometry-plugins

Conversation

@wdconinc

Copy link
Copy Markdown
Contributor

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_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.

Modified files:

  • CMakeLists.txt: link ROOT::ROOTTPython into the epic plugin library and install the python/ directory to share/epic/python/.

What is the urgency of this PR?

  • High (please describe reason below)
  • Medium
  • Low

What kind of change does this PR introduce?

  • Bug fix (issue #__)
  • New feature (issue #__)
  • Optimization (issue #__)
  • Updated constants (issue #__)
  • Updated documentation
  • other: __

Please check if any of the following apply

  • This PR introduces breaking changes. Please describe changes users need to make below.
  • This PR changes default behavior. Please describe changes below.
  • AI was used in preparing this PR. Please describe usage below.

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.

wdconinc and others added 3 commits April 29, 2026 20:23
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>
Copilot AI review requested due to automatic review settings April 30, 2026 01:39
@github-actions github-actions Bot added topic: documentation Improvements or additions to documentation topic: infrastructure Regarding build system, CI, CD labels Apr 30, 2026
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_PythonDetector trampoline plugin that imports/calls a Python module function and transfers the resulting dd4hep::DetElement back to C++.
  • Add python/epic_geo_helpers.py (cppyy workarounds + type aliases) and python/SimpleDisk_python_geo.py example 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.

Comment thread src/PythonDetector_geo.cpp
Comment thread CMakeLists.txt
Comment thread src/PythonDetector_geo.cpp
Comment thread src/PythonDetector_geo.cpp Outdated
wdconinc and others added 3 commits April 29, 2026 21:03
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>
@github-actions

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: documentation Improvements or additions to documentation topic: infrastructure Regarding build system, CI, CD

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants