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

feat: Bind some Gen1 geometry building functionality to python #3448

Merged
merged 19 commits into from
Aug 22, 2024
2 changes: 0 additions & 2 deletions Core/include/Acts/Geometry/CylinderVolumeHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class CylinderVolumeHelper : public ITrackingVolumeHelper {
std::unique_ptr<const Logger> logger = getDefaultLogger(
"CylinderVolumeHelper", Logging::INFO));

~CylinderVolumeHelper() override = default;

/// Create a TrackingVolume* from a set of layers and (optional) parameters
///
/// @param gctx is the geometry context for witch the volume is built
Expand Down
3 changes: 0 additions & 3 deletions Core/include/Acts/Geometry/TrackingVolumeArrayCreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class TrackingVolumeArrayCreator : public ITrackingVolumeArrayCreator {
Logging::INFO))
: m_logger(std::move(logger)) {}

/// Destructor
~TrackingVolumeArrayCreator() override = default;

/// create a tracking volume array
///
/// @param [in] gctx the geometry context for this building
Expand Down
4 changes: 4 additions & 0 deletions Core/src/Geometry/CylinderVolumeHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ Acts::CylinderVolumeHelper::createContainerTrackingVolume(
auto lastVolume = volumes.end();

for (std::size_t ivol = 0; firstVolume != lastVolume; ++firstVolume, ++ivol) {
if (*firstVolume == nullptr) {
ACTS_ERROR("Volume " << ivol << " is nullptr, return nullptr");
return nullptr;
}
ACTS_VERBOSE(" - volume (" << ivol
<< ") is : " << (*firstVolume)->volumeName());
ACTS_VERBOSE(" at position : " << (*firstVolume)->center().x() << ", "
Expand Down
1 change: 1 addition & 0 deletions Examples/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pybind11_add_module(ActsPythonBindings
src/Digitization.cpp
src/Material.cpp
src/Geometry.cpp
src/GeometryBuildingGen1.cpp
src/ExampleAlgorithms.cpp
src/MagneticField.cpp
src/Output.cpp
Expand Down
38 changes: 27 additions & 11 deletions Examples/Python/src/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ void addGeometry(Context& ctx) {
{
py::class_<Acts::TrackingGeometry, std::shared_ptr<Acts::TrackingGeometry>>(
m, "TrackingGeometry")
.def(py::init([](const MutableTrackingVolumePtr& volPtr,
std::shared_ptr<const IMaterialDecorator> matDec,
const GeometryIdentifierHook& hook,
Acts::Logging::Level level) {
auto logger = Acts::getDefaultLogger("TrackingGeometry", level);
auto trkGeo = std::make_shared<Acts::TrackingGeometry>(
volPtr, matDec.get(), hook, *logger);
return trkGeo;
}))
.def("visitSurfaces",
[](Acts::TrackingGeometry& self, py::function& func) {
self.visitSurfaces(func);
Expand All @@ -163,21 +172,28 @@ void addGeometry(Context& ctx) {
}

{
py::class_<Acts::Volume, std::shared_ptr<Acts::Volume>>(m, "Volume")
.def_static(
"makeCylinderVolume",
[](double r, double halfZ) {
auto bounds =
std::make_shared<Acts::CylinderVolumeBounds>(0, r, halfZ);
return std::make_shared<Acts::Volume>(Transform3::Identity(),
bounds);
},
"r"_a, "halfZ"_a);
py::class_<Acts::VolumeBounds, std::shared_ptr<Acts::VolumeBounds>>(
m, "VolumeBounds");

py::class_<Acts::CylinderVolumeBounds,
std::shared_ptr<Acts::CylinderVolumeBounds>, Acts::VolumeBounds>(
m, "CylinderVolumeBounds")
.def(py::init<ActsScalar, ActsScalar, ActsScalar, ActsScalar,
ActsScalar, ActsScalar, ActsScalar>(),
"rmin"_a, "rmax"_a, "halfz"_a, "halfphi"_a = M_PI, "avgphi"_a = 0.,
"bevelMinZ"_a = 0., "bevelMaxZ"_a = 0.);
}

{
py::class_<Acts::Volume, std::shared_ptr<Acts::Volume>>(m, "Volume");

py::class_<Acts::TrackingVolume, Acts::Volume,
std::shared_ptr<Acts::TrackingVolume>>(m, "TrackingVolume");
std::shared_ptr<Acts::TrackingVolume>>(m, "TrackingVolume")
.def(py::init([](std::shared_ptr<const Acts::VolumeBounds> bounds,
std::string name) {
return std::make_shared<Acts::TrackingVolume>(Transform3::Identity(),
bounds, name);
}));
}

{
Expand Down
137 changes: 137 additions & 0 deletions Examples/Python/src/GeometryBuildingGen1.cpp
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
andiwand marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// This file is part of the Acts project.
//
// Copyright (C) 2021 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "Acts/Geometry/CylinderVolumeBounds.hpp"
#include "Acts/Geometry/CylinderVolumeHelper.hpp"
#include "Acts/Geometry/Layer.hpp"
#include "Acts/Geometry/LayerArrayCreator.hpp"
#include "Acts/Geometry/LayerCreator.hpp"
#include "Acts/Geometry/SurfaceArrayCreator.hpp"
#include "Acts/Geometry/TrackingVolume.hpp"
#include "Acts/Geometry/TrackingVolumeArrayCreator.hpp"
#include "Acts/Geometry/VolumeBounds.hpp"
#include "Acts/Plugins/Python/Utilities.hpp"

#include <array>
#include <memory>
#include <unordered_map>
#include <vector>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;
using namespace pybind11::literals;

namespace Acts::Python {
void addGeometryBuildingGen1(Context &ctx) {
auto m = ctx.get("main");

using SurfacePtrVector = std::vector<std::shared_ptr<const Surface>>;

py::class_<Acts::Layer, std::shared_ptr<Acts::Layer>>(m, "Layer");

{
auto creator =
py::class_<Acts::LayerCreator>(m, "LayerCreator")
.def(py::init<Acts::LayerCreator::Config>())
.def("cylinderLayer",
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
[](const Acts::LayerCreator &self, const GeometryContext &gctx,
SurfacePtrVector surfaces, std::size_t binsPhi,
std::size_t binsZ) {
return self.cylinderLayer(gctx, surfaces, binsPhi, binsZ);
})
.def("discLayer",
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
[](const Acts::LayerCreator &self, const GeometryContext &gctx,
SurfacePtrVector surfaces, std::size_t binsR,
std::size_t binsPhi) {
return self.discLayer(gctx, surfaces, binsR, binsPhi);
});

auto config =
py::class_<LayerCreator::Config>(creator, "Config").def(py::init<>());

ACTS_PYTHON_STRUCT_BEGIN(config, LayerCreator::Config);
ACTS_PYTHON_MEMBER(surfaceArrayCreator);
ACTS_PYTHON_MEMBER(cylinderZtolerance);
ACTS_PYTHON_MEMBER(cylinderPhiTolerance);
ACTS_PYTHON_STRUCT_END();
}

{
using Creator = Acts::SurfaceArrayCreator;
using Config = typename Creator::Config;

auto creator =
py::class_<Creator, std::shared_ptr<Creator>>(m, "SurfaceArrayCreator")
.def(py::init<Config>());

py::class_<Config>(creator, "Config").def(py::init<>());
}

{
using Base = Acts::ILayerArrayCreator;
using Creator = Acts::LayerArrayCreator;
using Config = typename Creator::Config;

py::class_<Base, std::shared_ptr<Base>>(m, "ILayerArrayCreator");

auto creator = py::class_<Creator, std::shared_ptr<Creator>, Base>(
m, "LayerArrayCreator")
.def(py::init<Config>());

py::class_<Config>(creator, "Config").def(py::init<>());
}

{
using Base = Acts::ITrackingVolumeArrayCreator;
using Creator = Acts::TrackingVolumeArrayCreator;
using Config = typename Creator::Config;

py::class_<Base, std::shared_ptr<Base>>(m, "ITrackingVolumeArrayCreator");

auto creator = py::class_<Creator, std::shared_ptr<Creator>, Base>(
m, "TrackingVolumeArrayCreator")
.def(py::init<Config>());

py::class_<Config>(creator, "Config").def(py::init<>());
}

{
auto helper =
py::class_<Acts::CylinderVolumeHelper>(m, "CylinderVolumeHelper")
.def(py::init([](const Acts::CylinderVolumeHelper::Config &cfg,
Acts::Logging::Level level) {
return Acts::CylinderVolumeHelper(
cfg, Acts::getDefaultLogger("CylinderVolumeHelper", level));
}))
.def("createTrackingVolume",
[](const Acts::CylinderVolumeHelper &self,
GeometryContext gctx, const LayerVector &layers,
std::shared_ptr<VolumeBounds> volumeBounds,
const Transform3 &trafo, const std::string &name) {
return self.createTrackingVolume(
gctx, layers, {}, volumeBounds, {}, trafo, name);
})
.def("createContainerTrackingVolume",
&Acts::CylinderVolumeHelper::createContainerTrackingVolume);

auto config = py::class_<CylinderVolumeHelper::Config>(helper, "Config")
.def(py::init<>());

ACTS_PYTHON_STRUCT_BEGIN(config, CylinderVolumeHelper::Config);
ACTS_PYTHON_MEMBER(layerArrayCreator);
ACTS_PYTHON_MEMBER(trackingVolumeArrayCreator);
ACTS_PYTHON_MEMBER(passiveLayerThickness);
ACTS_PYTHON_MEMBER(passiveLayerPhiBins);
ACTS_PYTHON_MEMBER(passiveLayerRzBins);
ACTS_PYTHON_STRUCT_END();
}
}

} // namespace Acts::Python
2 changes: 2 additions & 0 deletions Examples/Python/src/ModuleEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void addEventData(Context& ctx);
void addPropagation(Context& ctx);

void addGeometry(Context& ctx);
void addGeometryBuildingGen1(Context& ctx);
void addExperimentalGeometry(Context& ctx);

void addMagneticField(Context& ctx);
Expand Down Expand Up @@ -116,6 +117,7 @@ PYBIND11_MODULE(ActsPythonBindings, m) {
addEventData(ctx);

addPropagation(ctx);
addGeometryBuildingGen1(ctx);
addGeometry(ctx);
addExperimentalGeometry(ctx);

Expand Down
Loading