|
8 | 8 |
|
9 | 9 | #include "Acts/Utilities/Logger.hpp" |
10 | 10 | #include "ActsExamples/Framework/AlgorithmContext.hpp" |
| 11 | +#include "ActsExamples/Framework/DataHandle.hpp" |
11 | 12 | #include "ActsExamples/Framework/IAlgorithm.hpp" |
12 | 13 | #include "ActsExamples/Framework/IReader.hpp" |
13 | 14 | #include "ActsExamples/Framework/IWriter.hpp" |
|
16 | 17 | #include "ActsExamples/Framework/SequenceElement.hpp" |
17 | 18 | #include "ActsExamples/Framework/Sequencer.hpp" |
18 | 19 | #include "ActsExamples/Framework/WhiteBoard.hpp" |
19 | | -#include "ActsPython/Utilities/Helpers.hpp" |
20 | 20 | #include "ActsPython/Utilities/Macros.hpp" |
| 21 | +#include "ActsPython/Utilities/WhiteBoardTypeRegistry.hpp" |
21 | 22 |
|
| 23 | +#include <stdexcept> |
| 24 | + |
| 25 | +#include <boost/core/demangle.hpp> |
22 | 26 | #include <pybind11/pybind11.h> |
23 | 27 | #include <pybind11/stl.h> |
24 | 28 |
|
@@ -82,11 +86,72 @@ class PyIAlgorithm : public IAlgorithm { |
82 | 86 | } |
83 | 87 | } |
84 | 88 |
|
| 89 | + ProcessCode initialize() override { |
| 90 | + py::gil_scoped_acquire acquire{}; |
| 91 | + PYBIND11_OVERRIDE(ProcessCode, IAlgorithm, initialize, ); |
| 92 | + } |
| 93 | + |
| 94 | + ProcessCode finalize() override { |
| 95 | + py::gil_scoped_acquire acquire{}; |
| 96 | + PYBIND11_OVERRIDE(ProcessCode, IAlgorithm, finalize, ); |
| 97 | + } |
| 98 | + |
85 | 99 | std::string_view typeName() const override { return "Algorithm"; } |
86 | 100 |
|
87 | 101 | const Acts::Logger& pyLogger() const { return logger(); } |
88 | 102 | }; |
89 | 103 |
|
| 104 | +class PyReadDataHandle : public ReadDataHandleBase { |
| 105 | + public: |
| 106 | + PyReadDataHandle(SequenceElement* parent, py::object pytype, |
| 107 | + const std::string& name) |
| 108 | + : ReadDataHandleBase(parent, name) { |
| 109 | + m_entry = WhiteBoardRegistry::find(pytype); |
| 110 | + if (m_entry == nullptr) { |
| 111 | + throw py::type_error("Type '" + |
| 112 | + pytype.attr("__qualname__").cast<std::string>() + |
| 113 | + "' is not registered for WhiteBoard access"); |
| 114 | + } |
| 115 | + if (m_entry->typeinfo == nullptr) { |
| 116 | + throw py::type_error("Type '" + |
| 117 | + pytype.attr("__qualname__").cast<std::string>() + |
| 118 | + "' is not registered for WhiteBoard access"); |
| 119 | + } |
| 120 | + |
| 121 | + registerAsReadHandle(); |
| 122 | + } |
| 123 | + |
| 124 | + const std::type_info& typeInfo() const override { return *m_entry->typeinfo; } |
| 125 | + |
| 126 | + std::uint64_t typeHash() const override { return m_entry->typeHash; } |
| 127 | + |
| 128 | + py::object call(const py::object& wbPy) const { |
| 129 | + if (!isInitialized()) { |
| 130 | + throw std::runtime_error("ReadDataHandle '" + name() + |
| 131 | + "' not initialized"); |
| 132 | + } |
| 133 | + const auto& wb = wbPy.cast<const ActsExamples::WhiteBoard&>(); |
| 134 | + |
| 135 | + if (!wb.exists(key())) { |
| 136 | + throw py::key_error("Key '" + key() + "' does not exist"); |
| 137 | + } |
| 138 | + |
| 139 | + const auto& holder = getHolder(wb); |
| 140 | + |
| 141 | + if (m_entry->typeHash != holder->typeHash()) { |
| 142 | + const auto& expected = boost::core::demangle(m_entry->typeinfo->name()); |
| 143 | + const auto& actual = boost::core::demangle(holder->type().name()); |
| 144 | + throw py::type_error("Type mismatch for key '" + key() + "'. Expected " + |
| 145 | + expected + " but got " + actual); |
| 146 | + } |
| 147 | + |
| 148 | + return m_entry->fn(holder->data(), wbPy); |
| 149 | + } |
| 150 | + |
| 151 | + private: |
| 152 | + const WhiteBoardRegistry::RegistryEntry* m_entry{nullptr}; |
| 153 | +}; |
| 154 | + |
90 | 155 | void trigger_divbyzero() { |
91 | 156 | volatile float j = 0.0; |
92 | 157 | volatile float r = 123 / j; // MARK: divbyzero |
@@ -132,6 +197,23 @@ void addFramework(py::module& mex) { |
132 | 197 | .def("exists", &WhiteBoard::exists) |
133 | 198 | .def_property_readonly("keys", &WhiteBoard::getKeys); |
134 | 199 |
|
| 200 | + py::class_<PyReadDataHandle>(mex, "ReadDataHandle") |
| 201 | + .def(py::init([](const py::object& parent_py, py::object pytype, |
| 202 | + const std::string& name) { |
| 203 | + auto* parent = parent_py.cast<SequenceElement*>(); |
| 204 | + return std::make_unique<PyReadDataHandle>(parent, |
| 205 | + std::move(pytype), name); |
| 206 | + }), |
| 207 | + py::arg("parent"), py::arg("type"), py::arg("name"), |
| 208 | + py::keep_alive<1, 2>()) |
| 209 | + .def( |
| 210 | + "initialize", |
| 211 | + [](PyReadDataHandle& self, std::string_view key) { |
| 212 | + self.initialize(key); |
| 213 | + }, |
| 214 | + py::arg("key")) |
| 215 | + .def("__call__", &PyReadDataHandle::call, py::arg("whiteboard")); |
| 216 | + |
135 | 217 | py::class_<AlgorithmContext>(mex, "AlgorithmContext") |
136 | 218 | .def(py::init<std::size_t, std::size_t, WhiteBoard&, std::size_t>(), |
137 | 219 | "alg"_a, "event"_a, "store"_a, "thread"_a) |
|
0 commit comments