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: TGeo python binding #3885

Merged
merged 13 commits into from
Dec 6, 2024
7 changes: 7 additions & 0 deletions Examples/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ else()
target_sources(ActsPythonBindings PRIVATE src/GeoModelStub.cpp)
endif()

if(ACTS_BUILD_PLUGIN_TGEO)
target_link_libraries(ActsPythonBindings PUBLIC ActsPluginTGeo)
target_sources(ActsPythonBindings PRIVATE src/TGeo.cpp)
else()
target_sources(ActsPythonBindings PRIVATE src/TGeoStub.cpp)
endif()

if(ACTS_BUILD_PLUGIN_TRACCC)
target_link_libraries(ActsPythonBindings PUBLIC ActsPluginDetray)
target_sources(ActsPythonBindings PRIVATE src/Detray.cpp)
Expand Down
2 changes: 2 additions & 0 deletions Examples/Python/src/ModuleEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void addUtilities(Context& ctx);
void addDigitization(Context& ctx);
void addPythia8(Context& ctx);
void addGeoModel(Context& ctx);
void addTGeo(Context& ctx);
void addJson(Context& ctx);
void addDetray(Context& ctx);
void addHepMC3(Context& ctx);
Expand Down Expand Up @@ -146,6 +147,7 @@ PYBIND11_MODULE(ActsPythonBindings, m) {
addPythia8(ctx);
addJson(ctx);
addGeoModel(ctx);
addTGeo(ctx);
addDetray(ctx);
addHepMC3(ctx);
addExaTrkXTrackFinding(ctx);
Expand Down
87 changes: 87 additions & 0 deletions Examples/Python/src/TGeo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.

#include "Acts/Plugins/Python/Utilities.hpp"
#include "Acts/Plugins/TGeo/TGeoDetectorElement.hpp"
#include "Acts/Plugins/TGeo/TGeoLayerBuilder.hpp"
#include "Acts/Plugins/TGeo/TGeoParser.hpp"

#include <vector>

#include <TGeoManager.h>
#include <TGeoVolume.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl/filesystem.h>

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

namespace Acts::Python {
void addTGeo(Context& ctx) {
auto [m, mex] = ctx.get("main", "examples");

auto tgeo = mex.def_submodule("tgeo");

{
py::class_<Acts::TGeoDetectorElement,
std::shared_ptr<Acts::TGeoDetectorElement>>(
tgeo, "TGeoDetectorElement")
.def("surface", [](const Acts::TGeoDetectorElement& self) {
return self.surface().getSharedPtr();
});
}

{
/// Helper function to test if the automatic geometry conversion works
///
/// @param rootFileName is the name of the GDML file
/// @param sensitiveMatches is a list of strings to match sensitive volumes
/// @param localAxes is the TGeo->ACTS axis conversion convention
/// @param scaleConversion is a unit scalor conversion factor
tgeo.def("_convertToElements",
[](const std::string& rootFileName,
const std::vector<std::string>& sensitiveMatches,
const std::string& localAxes, double scaleConversion) {
// Return vector
std::vector<std::shared_ptr<const Acts::TGeoDetectorElement>>
tgElements;
// TGeo import
TGeoManager::Import(rootFileName.c_str());
if (gGeoManager != nullptr) {
asalzburger marked this conversation as resolved.
Show resolved Hide resolved
auto tVolume = gGeoManager->GetTopVolume();
if (tVolume != nullptr) {
TGeoHMatrix gmatrix = TGeoIdentity(tVolume->GetName());

TGeoParser::Options tgpOptions;
tgpOptions.volumeNames = {tVolume->GetName()};
tgpOptions.targetNames = sensitiveMatches;
tgpOptions.unit = scaleConversion;
TGeoParser::State tgpState;
tgpState.volume = tVolume;
tgpState.onBranch = true;

TGeoParser::select(tgpState, tgpOptions, gmatrix);
tgElements.reserve(tgpState.selectedNodes.size());

for (const auto& snode : tgpState.selectedNodes) {
auto identifier = Acts::TGeoDetectorElement::Identifier();
auto tgElement = TGeoLayerBuilder::defaultElementFactory(
identifier, *snode.node, *snode.transform, localAxes,
scaleConversion, nullptr);
tgElements.emplace_back(tgElement);
}
}
}
// Return the elements
return tgElements;
});
}
}

} // namespace Acts::Python
15 changes: 15 additions & 0 deletions Examples/Python/src/TGeoStub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.

namespace Acts::Python {
struct Context;
} // namespace Acts::Python

namespace Acts::Python {
void addTGeo(Context& /*ctx*/) {}
} // namespace Acts::Python
Loading