Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add macro to simplify algorithm binding #1510

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class HepMCProcessExtractor final : public ActsExamples::BareAlgorithm {
ActsExamples::ProcessCode execute(
const AlgorithmContext& context) const final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// The config object
Config m_cfg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class HitsPrinter : public BareAlgorithm {

ProcessCode execute(const AlgorithmContext& ctx) const;

const Config& config() const { return m_cfg; }

private:
Config m_cfg;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ParticlesPrinter : public BareAlgorithm {

ProcessCode execute(const AlgorithmContext& ctx) const;

const Config& config() const { return m_cfg; }

private:
Config m_cfg;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class TrackParametersPrinter : public BareAlgorithm {

ProcessCode execute(const AlgorithmContext& ctx) const;

const Config& config() const { return m_cfg; }

private:
Config m_cfg;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class CsvMeasurementWriter final : public WriterT<MeasurementContainer> {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// This implementation holds the actual writing method
/// and is called by the WriterT<>::write interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class CsvParticleWriter final : public WriterT<SimParticleContainer> {
/// @params lvl is the logging level
CsvParticleWriter(const Config& cfg, Acts::Logging::Level lvl);

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// Type-specific write implementation.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class CsvSpacepointWriter final : public WriterT<SimSpacePointContainer> {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// This implementation holds the actual writing method
/// and is called by the WriterT<>::write interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CsvTrackingGeometryWriter : public IWriter {
/// Write geometry using the default context.
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
Config m_cfg;
const Acts::TrackingVolume* m_world;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class HepMC3AsciiReader final : public IReader {
/// Read out data from the input stream.
ProcessCode read(const ActsExamples::AlgorithmContext& ctx) final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// The configuration of this writer
Config m_cfg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class HepMC3AsciiWriter final : public WriterT<std::vector<HepMC3::GenEvent>> {
const ActsExamples::AlgorithmContext& ctx,
const std::vector<HepMC3::GenEvent>& events) final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// The configuration of this writer
Config m_cfg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class RootNuclearInteractionParametersWriter final
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// @brief Write method called by the base class
/// @param [in] ctx is the algorithm context for event information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class ObjPropagationStepsWriter
return ActsExamples::ProcessCode::SUCCESS;
}

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
Config m_cfg; ///!< Internal configuration represenation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CKFPerformanceWriter final : public WriterT<TrajectoriesContainer> {
/// Finalize plots.
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
ProcessCode writeT(const AlgorithmContext& ctx,
const TrajectoriesContainer& trajectories) final override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class SeedingPerformanceWriter final : public WriterT<ProtoTrackContainer> {
/// Finalize plots.
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
ProcessCode writeT(const AlgorithmContext& ctx,
const ProtoTrackContainer& tracks) final override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,8 @@ ActsExamples::ProcessCode ActsExamples::TrackFinderPerformanceWriter::endRun() {
m_impl->close();
return ProcessCode::SUCCESS;
}

const ActsExamples::TrackFinderPerformanceWriter::Config&
ActsExamples::TrackFinderPerformanceWriter::config() const {
return m_impl->cfg;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class TrackFinderPerformanceWriter final : public WriterT<ProtoTrackContainer> {

ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const;

private:
ProcessCode writeT(const AlgorithmContext& ctx,
const ProtoTrackContainer& tracks) final override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class TrackFitterPerformanceWriter final
/// Finalize plots.
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
ProcessCode writeT(const AlgorithmContext& ctx,
const TrajectoriesContainer& trajectories) final override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class RootMaterialDecorator : public Acts::IMaterialDecorator {
}
}

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// The config class
Config m_cfg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class RootMaterialWriter : public IMaterialWriter {
/// @param tGeometry is the TrackingGeometry
void write(const Acts::TrackingGeometry& tGeometry);

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// Collect the material from the tracking geometry
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class RootParticleWriter final : public WriterT<SimParticleContainer> {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// Type-specific write implementation.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class RootPlanarClusterWriter
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// This implementation holds the actual writing method
/// and is called by the WriterT<>::write interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class RootPropagationStepsWriter
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// This implementation holds the actual writing method
/// and is called by the WriterT<>::write interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class RootSimHitWriter final : public WriterT<SimHitContainer> {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// Type-specific write implementation.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class RootTrackParameterWriter final : public TrackParameterWriter {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// @brief Write method called by the base class
/// @param [in] ctx is the algorithm context for event information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class RootTrajectoryStatesWriter final : public WriterT<TrajectoriesContainer> {
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// @brief Write method called by the base class
/// @param [in] ctx is the algorithm context for event information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class RootTrajectorySummaryWriter final
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// @brief Write method called by the base class
/// @param [in] ctx is the algorithm context for event information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class RootVertexPerformanceWriter final
/// End-of-run hook
ProcessCode endRun() final override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

protected:
/// @brief Write method called by the base class
/// @param [in] ctx is the algorithm context for event information
Expand Down
91 changes: 83 additions & 8 deletions Examples/Python/include/Acts/Plugins/Python/Utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

#pragma once

#include "Acts/Utilities/TypeTraits.hpp"

#include <string>
#include <unordered_map>

#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#include <pybind11/pybind11.h>

namespace Acts::Python {
Expand All @@ -20,8 +24,7 @@ struct Context {

pybind11::module_& get(const std::string& name) { return *modules.at(name); }

template <typename... Args,
typename = std::enable_if_t<sizeof...(Args) >= 2> >
template <typename... Args, typename = std::enable_if_t<sizeof...(Args) >= 2>>
auto get(Args&&... args) {
return std::make_tuple((*modules.at(args))...);
}
Expand Down Expand Up @@ -50,19 +53,91 @@ void patchKwargsConstructor(T& c) {
pybind11::module::import("acts._adapter").attr("_patchKwargsConstructor")(c);
}

METHOD_TRAIT(write_method_trait_t, write);

} // namespace Acts::Python

#define ACTS_PYTHON_MEMBER(name) \
_binding_instance.def_readwrite(#name, &_struct_type::name)

#define ACTS_PYTHON_STRUCT_BEGIN(obj, cls) \
{ \
auto& _binding_instance = obj; \
using _struct_type = cls; \
do { \
#define ACTS_PYTHON_STRUCT_BEGIN(obj, cls) \
{ \
[[maybe_unused]] auto& _binding_instance = obj; \
using _struct_type = cls; \
do { \
} while (0)

#define ACTS_PYTHON_STRUCT_END() \
} \
do { \
} while (0)

} // namespace Acts::Python
/// This macro is needed to use the BOOST_PP_SEQ_FOR_EACH loop macro
#define ACTS_PYTHON_MEMBER_LOOP(r, data, elem) ACTS_PYTHON_MEMBER(elem);

/// A macro that uses Boost.Preprocessor to create the python binding for and
/// algorithm and the additional config struct.
#define ACTS_PYTHON_DECLARE_ALGORITHM(algorithm, mod, name, ...) \
do { \
using Alg = algorithm; \
using Config = Alg::Config; \
auto alg = \
py::class_<Alg, ActsExamples::BareAlgorithm, std::shared_ptr<Alg>>( \
mod, name) \
.def(py::init<const Config&, Acts::Logging::Level>(), \
py::arg("config"), py::arg("level")) \
.def_property_readonly("config", &Alg::config); \
\
auto c = py::class_<Config>(alg, "Config").def(py::init<>()); \
ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
ACTS_PYTHON_STRUCT_END(); \
} while (0)

/// Similar as above for writers
#define ACTS_PYTHON_DECLARE_WRITER(writer, mod, name, ...) \
do { \
using Writer = writer; \
using Config = Writer::Config; \
auto w = \
py::class_<Writer, ActsExamples::IWriter, std::shared_ptr<Writer>>( \
mod, name) \
.def(py::init<const Config&, Acts::Logging::Level>(), \
py::arg("config"), py::arg("level")) \
.def_property_readonly("config", &Writer::config); \
\
constexpr bool has_write_method = \
Acts::Concepts::has_method<Writer, ActsExamples::ProcessCode, \
Acts::Python::write_method_trait_t, \
const ActsExamples::AlgorithmContext&>; \
\
if constexpr (has_write_method) { \
w.def("write", &Writer::write); \
} \
\
auto c = py::class_<Config>(w, "Config").def(py::init<>()); \
ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
ACTS_PYTHON_STRUCT_END(); \
} while (0)

/// Similar as above for readers
#define ACTS_PYTHON_DECLARE_READER(reader, mod, name, ...) \
do { \
using Reader = reader; \
using Config = Reader::Config; \
auto r = \
py::class_<Reader, ActsExamples::IReader, std::shared_ptr<Reader>>( \
mod, name) \
.def(py::init<const Config&, Acts::Logging::Level>(), \
py::arg("config"), py::arg("level")) \
.def_property_readonly("config", &Reader::config); \
\
auto c = py::class_<Config>(r, "Config").def(py::init<>()); \
ACTS_PYTHON_STRUCT_BEGIN(c, Config); \
BOOST_PP_SEQ_FOR_EACH(ACTS_PYTHON_MEMBER_LOOP, _, \
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
ACTS_PYTHON_STRUCT_END(); \
} while (0)
Loading