20 #include <pybind11/pybind11.h>
21 #include <pybind11/stl.h>
23 namespace py = pybind11;
24 using namespace ActsExamples;
25 using namespace Acts::Python;
28 #if defined(__clang__)
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
34 using SequenceElement::SequenceElement;
37 py::gil_scoped_acquire acquire{};
42 py::gil_scoped_acquire acquire{};
47 py::gil_scoped_acquire acquire{};
52 py::gil_scoped_acquire acquire{};
56 #if defined(__clang__)
57 #pragma clang diagnostic pop
65 py::gil_scoped_acquire acquire{};
68 }
catch (py::error_already_set&
e) {
70 }
catch (std::runtime_error& e) {
71 throw py::type_error(
"Python algorithm did not conform to interface");
76 void trigger_divbyzero() {
77 volatile float j = 0.0;
78 volatile float r = 123 /
j;
82 void trigger_overflow() {
83 volatile float j = std::numeric_limits<float>::max();
84 volatile float r = j *
j;
88 void trigger_invalid() {
89 volatile float j = -1;
90 volatile float r = std::sqrt(j);
96 namespace Acts::Python {
98 auto [
m, mex] = ctx.
get(
"main",
"examples");
100 py::class_<ActsExamples::IWriter, std::shared_ptr<ActsExamples::IWriter>>(
103 py::class_<ActsExamples::IReader, std::shared_ptr<ActsExamples::IReader>>(
106 py::enum_<ProcessCode>(mex,
"ProcessCode")
108 .value(
"ABORT", ProcessCode::ABORT)
109 .value(
"END", ProcessCode::END);
111 py::class_<WhiteBoard>(mex,
"WhiteBoard")
113 return std::make_unique<WhiteBoard>(
116 py::arg(
"level"), py::arg(
"name") =
"WhiteBoard")
119 py::class_<Acts::GeometryContext>(
m,
"GeometryContext").def(py::init<>());
121 py::class_<AlgorithmContext>(mex,
"AlgorithmContext")
122 .def(py::init<size_t, size_t, WhiteBoard&>())
125 .def_property_readonly(
"eventStore",
127 return self.eventStore;
134 auto pySequenceElement =
136 std::shared_ptr<ActsExamples::SequenceElement>>(
137 mex,
"SequenceElement")
138 .def(py::init_alias<>())
145 PyIAlgorithm>(mex,
"IAlgorithm")
146 .def(py::init_alias<const std::string&, Acts::Logging::Level>(),
147 py::arg(
"name"), py::arg(
"level"))
153 py::class_<Sequencer>(mex,
"_Sequencer")
155 cfg.iterationCallback = []() {
156 py::gil_scoped_acquire gil;
157 if (PyErr_CheckSignals() != 0) {
158 throw py::error_already_set{};
161 return std::make_unique<Sequencer>(
cfg);
165 py::gil_scoped_release gil;
166 int res =
self.run();
167 if (res != EXIT_SUCCESS) {
168 throw std::runtime_error{
"Sequencer terminated abnormally"};
178 .def_property_readonly_static(
180 [](py::object ) {
return std::string{__FILE__}; });
182 auto c = py::class_<Config>(sequencer,
"Config").def(py::init<>());
198 py::class_<Sequencer::FpeMask>(sequencer,
"_FpeMask")
203 std::stringstream ss;
215 struct FpeMonitorContext {
216 std::optional<Acts::FpeMonitor> mon;
219 auto fpe = py::class_<Acts::FpeMonitor>(
m,
"FpeMonitor")
220 .def_static(
"_trigger_divbyzero", &trigger_divbyzero)
221 .def_static(
"_trigger_overflow", &trigger_overflow)
222 .def_static(
"_trigger_invalid", &trigger_invalid)
223 .def_static(
"context", []() {
return FpeMonitorContext(); });
225 fpe.def_property_readonly(
"result",
227 py::return_value_policy::reference_internal)
230 py::class_<Acts::FpeMonitor::Result>(fpe,
"Result")
235 std::stringstream
os;
240 py::class_<FpeMonitorContext>(
m,
"_FpeMonitorContext")
241 .def(
py::init([]() {
return std::make_unique<FpeMonitorContext>(); }))
246 return fm.mon.value();
248 py::return_value_policy::reference_internal)
249 .def(
"__exit__", [](FpeMonitorContext& fm, py::object ,
251 py::object ) { fm.mon.reset(); });
253 py::enum_<Acts::FpeType>(
m,
"FpeType")
254 .
value(
"INTDIV", Acts::FpeType::INTDIV)
255 .value(
"INTOVF", Acts::FpeType::INTOVF)
256 .value(
"FLTDIV", Acts::FpeType::FLTDIV)
257 .value(
"FLTOVF", Acts::FpeType::FLTOVF)
258 .value(
"FLTUND", Acts::FpeType::FLTUND)
259 .value(
"FLTRES", Acts::FpeType::FLTRES)
260 .value(
"FLTINV", Acts::FpeType::FLTINV)
261 .value(
"FLTSUB", Acts::FpeType::FLTSUB)
263 .def_property_readonly_static(
264 "values", [](py::object ) ->
const auto& {
265 static const std::vector<Acts::FpeType>
values = {
266 Acts::FpeType::INTDIV, Acts::FpeType::INTOVF,
267 Acts::FpeType::FLTDIV, Acts::FpeType::FLTOVF,
268 Acts::FpeType::FLTUND, Acts::FpeType::FLTRES,
269 Acts::FpeType::FLTINV, Acts::FpeType::FLTSUB};
273 py::register_exception<ActsExamples::FpeFailure>(
m,
"FpeFailure",
278 py::class_<RandomNumbers, std::shared_ptr<RandomNumbers>>(mex,
280 .def(py::init<const RandomNumbers::Config&>());
282 py::class_<ActsExamples::RandomEngine>(mex,
"RandomEngine").def(py::init<>());