Skip to content

Commit 1768f44

Browse files
asalzburgernjacazio
authored andcommitted
feat: PythonBindings for plugins (acts-project#4750)
This PR moves the Python bindings of Plugin-related code into `Python/Plugins` while keeping the exact same functionality (and tests running).
1 parent 1964e24 commit 1768f44

38 files changed

Lines changed: 1311 additions & 1048 deletions

Examples/Python/CMakeLists.txt

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,11 @@ else()
102102
target_sources(ActsPythonBindings PRIVATE src/GeoModelStub.cpp)
103103
endif()
104104

105-
if(ACTS_BUILD_PLUGIN_ROOT)
106-
target_link_libraries(ActsPythonBindings PUBLIC Acts::PluginRoot)
107-
target_sources(ActsPythonBindings PRIVATE src/TGeo.cpp)
108-
else()
109-
target_sources(ActsPythonBindings PRIVATE src/TGeoStub.cpp)
110-
endif()
111-
112105
if(ACTS_BUILD_PLUGIN_TRACCC)
113-
target_link_libraries(ActsPythonBindings PUBLIC Acts::PluginDetray)
114-
target_sources(ActsPythonBindings PRIVATE src/Detray.cpp)
115106
target_link_libraries(ActsPythonBindings PUBLIC Acts::PluginCovfie)
116-
target_sources(ActsPythonBindings PRIVATE src/Covfie.cpp)
117107
target_link_libraries(ActsPythonBindings PUBLIC Acts::ExamplesTraccc)
118108
target_sources(ActsPythonBindings PRIVATE src/Traccc.cpp)
119109
else()
120-
target_sources(ActsPythonBindings PRIVATE src/DetrayStub.cpp)
121-
target_sources(ActsPythonBindings PRIVATE src/CovfieStub.cpp)
122110
target_sources(ActsPythonBindings PRIVATE src/TracccStub.cpp)
123111
endif()
124112

@@ -130,20 +118,25 @@ else()
130118
endif()
131119

132120
if(ACTS_BUILD_PLUGIN_DD4HEP AND ACTS_BUILD_EXAMPLES_DD4HEP)
133-
pybind11_add_module(ActsPythonBindingsDD4hep src/DD4hepComponent.cpp)
121+
pybind11_add_module(ActsExamplesPythonBindingsDD4hep src/DD4hepComponent.cpp)
134122
target_link_libraries(
135-
ActsPythonBindingsDD4hep
123+
ActsExamplesPythonBindingsDD4hep
136124
PUBLIC Acts::PythonUtilities Acts::ExamplesDetectorDD4hep
137125
)
138-
add_dependencies(ActsPythonBindings ActsPythonBindingsDD4hep)
126+
add_dependencies(ActsPythonBindings ActsExamplesPythonBindingsDD4hep)
139127

140-
install(TARGETS ActsPythonBindingsDD4hep DESTINATION ${_python_install_dir})
128+
install(
129+
TARGETS
130+
ActsExamplesPythonBindingsDD4hep
131+
DESTINATION
132+
${_python_install_dir}
133+
)
141134
set_target_properties(
142-
ActsPythonBindingsDD4hep
135+
ActsExamplesPythonBindingsDD4hep
143136
PROPERTIES INSTALL_RPATH "\$ORIGIN/../../${CMAKE_INSTALL_LIBDIR}"
144137
)
145138
set_target_properties(
146-
ActsPythonBindingsDD4hep
139+
ActsExamplesPythonBindingsDD4hep
147140
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${_python_dir}/acts
148141
)
149142
list(APPEND py_files examples/dd4hep.py)
@@ -160,24 +153,29 @@ else()
160153
endif()
161154

162155
if(ACTS_BUILD_EXAMPLES_GEANT4)
163-
pybind11_add_module(ActsPythonBindingsGeant4 src/Geant4Component.cpp)
156+
pybind11_add_module(ActsExamplesPythonBindingsGeant4 src/Geant4Component.cpp)
164157
target_link_libraries(
165-
ActsPythonBindingsGeant4
158+
ActsExamplesPythonBindingsGeant4
166159
PUBLIC
167160
Acts::ExamplesGeant4
168161
Acts::ExamplesDetectorGeant4
169162
Acts::PythonUtilities
170163
Acts::ExamplesMuonSpectrometerMockupDetector
171164
)
172-
add_dependencies(ActsPythonBindings ActsPythonBindingsGeant4)
165+
add_dependencies(ActsPythonBindings ActsExamplesPythonBindingsGeant4)
173166

174-
install(TARGETS ActsPythonBindingsGeant4 DESTINATION ${_python_install_dir})
167+
install(
168+
TARGETS
169+
ActsExamplesPythonBindingsGeant4
170+
DESTINATION
171+
${_python_install_dir}
172+
)
175173
set_target_properties(
176-
ActsPythonBindingsGeant4
174+
ActsExamplesPythonBindingsGeant4
177175
PROPERTIES INSTALL_RPATH "\$ORIGIN/../../${CMAKE_INSTALL_LIBDIR}"
178176
)
179177
set_target_properties(
180-
ActsPythonBindingsGeant4
178+
ActsExamplesPythonBindingsGeant4
181179
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${_python_dir}/acts
182180
)
183181
list(APPEND py_files examples/geant4/__init__.py)

Examples/Python/python/acts/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from . import ActsPythonBindings
1010
from ._adapter import _patch_config
1111

12+
1213
if (
1314
"ACTS_LOG_FAILURE_THRESHOLD" in os.environ
1415
and os.environ["ACTS_LOG_FAILURE_THRESHOLD"] != logging.getFailureThreshold().name
@@ -52,19 +53,21 @@ def _decoratorFromFile(file: Union[str, Path], **kwargs):
5253

5354
kwargs.setdefault("level", ActsPythonBindings.logging.INFO)
5455

56+
from .ActsPluginsPythonBindingsJson import (
57+
MaterialMapJsonConverter,
58+
JsonMaterialDecorator,
59+
)
60+
from .ActsPluginsPythonBindingsRoot import RootMaterialDecorator
61+
5562
if file.suffix in (".json", ".cbor"):
56-
c = ActsPythonBindings.MaterialMapJsonConverter.Config()
63+
c = MaterialMapJsonConverter.Config()
5764
for k in kwargs.keys():
5865
if hasattr(c, k):
5966
setattr(c, k, kwargs.pop(k))
6067

61-
return ActsPythonBindings.JsonMaterialDecorator(
62-
jFileName=str(file), rConfig=c, **kwargs
63-
)
68+
return JsonMaterialDecorator(jFileName=str(file), rConfig=c, **kwargs)
6469
elif file.suffix == ".root":
65-
return ActsPythonBindings._examples.RootMaterialDecorator(
66-
fileName=str(file), **kwargs
67-
)
70+
return RootMaterialDecorator(fileName=str(file), **kwargs)
6871
else:
6972
raise ValueError(f"Unknown file type {file.suffix}")
7073

Examples/Python/python/acts/examples/dd4hep.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
# try importing and see if it works in order to provide a useful error message
77
try:
88
subprocess.check_call(
9-
[sys.executable, "-c", "from acts import ActsPythonBindingsDD4hep"]
9+
[
10+
sys.executable,
11+
"-c",
12+
"from acts import ActsPluginsPythonBindingsDD4hep; from acts import ActsExamplesPythonBindingsDD4hep",
13+
]
1014
)
1115
except subprocess.CalledProcessError as e:
1216
print("Error encountered importing DD4hep. Likely you need to set LD_LIBRARY_PATH.")
1317
sys.exit(1)
1418

1519
from acts._adapter import _patch_config
16-
from acts import ActsPythonBindingsDD4hep
20+
from acts import ActsPluginsPythonBindingsDD4hep
21+
from acts.ActsPluginsPythonBindingsDD4hep import *
1722

18-
_patch_config(ActsPythonBindingsDD4hep)
23+
from acts import ActsExamplesPythonBindingsDD4hep
1924

20-
from acts.ActsPythonBindingsDD4hep import *
25+
_patch_config(ActsExamplesPythonBindingsDD4hep)
26+
27+
from acts.ActsExamplesPythonBindingsDD4hep import *

Examples/Python/python/acts/examples/geant4/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
# try importing and see if it works in order to provide a useful error message
66
try:
77
subprocess.check_call(
8-
[sys.executable, "-c", "from acts import ActsPythonBindingsGeant4"]
8+
[sys.executable, "-c", "from acts import ActsExamplesPythonBindingsGeant4"]
99
)
1010
except subprocess.CalledProcessError as e:
1111
print("Error encountered importing DD4hep. Likely you need to set LD_LIBRARY_PATH.")
1212
sys.exit(1)
1313

1414

1515
from acts._adapter import _patch_config
16-
from acts import ActsPythonBindingsGeant4
16+
from acts import ActsExamplesPythonBindingsGeant4
1717

18-
_patch_config(ActsPythonBindingsGeant4)
18+
_patch_config(ActsExamplesPythonBindingsGeant4)
1919

20-
from acts.ActsPythonBindingsGeant4 import *
20+
from acts.ActsExamplesPythonBindingsGeant4 import *

Examples/Python/python/acts/examples/odd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66
import acts
77
import acts.examples
8+
from acts import root
89
import warnings
910

1011

@@ -69,7 +70,7 @@ def getOpenDataDetector(
6970
raise RuntimeError(msg)
7071

7172
if materialDecorator is None:
72-
materialDecorator = acts.examples.RootMaterialDecorator(
73+
materialDecorator = acts.root.RootMaterialDecorator(
7374
fileName=str(odd_dir / "data/odd-material-maps.root"),
7475
level=customLogLevel(minLevel=acts.logging.WARNING),
7576
)

Examples/Python/src/CovfieStub.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

Examples/Python/src/DD4hepComponent.cpp

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this
77
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
88

9-
#include "Acts/Detector/GeometryIdGenerator.hpp"
109
#include "Acts/Utilities/Logger.hpp"
1110
#include "ActsExamples/DD4hepDetector/AlignedDD4hepDetectorElement.hpp"
1211
#include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
1312
#include "ActsExamples/DD4hepDetector/OpenDataDetector.hpp"
1413
#include "ActsPlugins/DD4hep/DD4hepDetectorElement.hpp"
15-
#include "ActsPlugins/DD4hep/DD4hepDetectorStructure.hpp"
16-
#include "ActsPlugins/DD4hep/DD4hepFieldAdapter.hpp"
17-
#include "ActsPlugins/DD4hep/DD4hepIdentifierMapper.hpp"
1814
#include "ActsPython/Utilities/Helpers.hpp"
1915
#include "ActsPython/Utilities/Macros.hpp"
2016

2117
#include <memory>
2218
#include <utility>
2319

24-
#include <DD4hep/DetElement.h>
25-
#include <DD4hep/Fields.h>
2620
#include <pybind11/functional.h>
2721
#include <pybind11/pybind11.h>
2822
#include <pybind11/stl.h>
@@ -34,22 +28,13 @@ using namespace pybind11::literals;
3428
using namespace ActsExamples;
3529
using namespace ActsPython;
3630

37-
PYBIND11_MODULE(ActsPythonBindingsDD4hep, m) {
31+
PYBIND11_MODULE(ActsExamplesPythonBindingsDD4hep, m) {
3832
{
39-
py::class_<DD4hepDetectorElement, DetectorElementBase,
40-
std::shared_ptr<DD4hepDetectorElement>>(m,
41-
"DD4hepDetectorElement");
42-
4333
py::class_<AlignedDD4hepDetectorElement, DD4hepDetectorElement,
4434
std::shared_ptr<AlignedDD4hepDetectorElement>>(
4535
m, "AlignedDD4hepDetectorElement");
4636
}
4737

48-
{
49-
py::class_<dd4hep::DetElement, std::shared_ptr<dd4hep::DetElement>>(
50-
m, "DD4hepDetElement");
51-
}
52-
5338
{
5439
auto base =
5540
py::class_<DD4hepDetectorBase, Detector,
@@ -93,81 +78,4 @@ PYBIND11_MODULE(ActsPythonBindingsDD4hep, m) {
9378

9479
patchKwargsConstructor(c);
9580
}
96-
97-
{
98-
py::class_<DD4hepFieldAdapter, MagneticFieldProvider,
99-
std::shared_ptr<DD4hepFieldAdapter>>(m, "DD4hepFieldAdapter");
100-
}
101-
102-
{
103-
m.def(
104-
"createDD4hepIdGeoIdMap",
105-
[](const TrackingGeometry& tGeometry)
106-
-> std::map<DD4hepDetectorElement::DD4hepVolumeID,
107-
GeometryIdentifier> {
108-
// The surface visitor
109-
struct DD4hepIdGrabber {
110-
std::map<DD4hepDetectorElement::DD4hepVolumeID, GeometryIdentifier>
111-
dd4hepIdGeoIdMap;
112-
113-
void operator()(const Surface* surface) {
114-
const auto* dde = surface->associatedDetectorElement();
115-
const auto* dd4hepDetElement =
116-
dynamic_cast<const DD4hepDetectorElement*>(dde);
117-
// Check if it is valid
118-
if (dd4hepDetElement != nullptr) {
119-
dd4hep::DDSegmentation::VolumeID dd4hepID =
120-
dd4hepDetElement->sourceElement().volumeID();
121-
auto geoID = surface->geometryId();
122-
dd4hepIdGeoIdMap[dd4hepID] = geoID;
123-
}
124-
}
125-
};
126-
127-
// Create an instance
128-
DD4hepIdGrabber dd4hepIdGrabber;
129-
// Visit the surfaces & return what you have
130-
tGeometry.visitSurfaces(dd4hepIdGrabber);
131-
return dd4hepIdGrabber.dd4hepIdGeoIdMap;
132-
});
133-
}
134-
135-
{
136-
using Options = DD4hepDetectorStructure::Options;
137-
auto o = py::class_<Options>(m, "DD4hepDetectorOptions").def(py::init<>());
138-
ACTS_PYTHON_STRUCT(o, logLevel, emulateToGraph, geoIdGenerator,
139-
materialDecorator);
140-
141-
patchKwargsConstructor(o);
142-
143-
m.def("attachDD4hepGeoIdMapper",
144-
[](DD4hepDetectorStructure::Options& options,
145-
const std::map<DD4hepDetectorElement::DD4hepVolumeID,
146-
GeometryIdentifier>& dd4hepIdGeoIdMap) {
147-
// The Geo mapper
148-
auto geoIdMapper = std::make_shared<const DD4hepIdentifierMapper>(
149-
DD4hepIdentifierMapper::Config{dd4hepIdGeoIdMap},
150-
getDefaultLogger("GeometryIdMapper", options.logLevel));
151-
152-
// A remaining recursive logger
153-
auto geoIdGenerator =
154-
std::make_shared<const Experimental::GeometryIdGenerator>(
155-
Experimental::GeometryIdGenerator::Config{},
156-
getDefaultLogger("GeometryIdGenerator", options.logLevel));
157-
158-
std::tuple<std::shared_ptr<const Experimental::GeometryIdGenerator>,
159-
std::shared_ptr<const DD4hepIdentifierMapper>>
160-
chainedGenerators = {geoIdGenerator, geoIdMapper};
161-
162-
auto chainedGeoIdGenerator =
163-
std::make_shared<const Experimental::ChainedGeometryIdGenerator<
164-
std::shared_ptr<const Experimental::GeometryIdGenerator>,
165-
std::shared_ptr<const DD4hepIdentifierMapper>>>(
166-
std::move(chainedGenerators),
167-
getDefaultLogger("ChainedGeometryIdGenerator",
168-
options.logLevel));
169-
170-
options.geoIdGenerator = chainedGeoIdGenerator;
171-
});
172-
}
17381
}

Examples/Python/src/DetrayStub.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)