From d949c494906362414b2877fa8bb60c041214820d Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Thu, 21 Nov 2024 11:17:31 +0100 Subject: [PATCH 1/5] tgeo binding --- Examples/Python/CMakeLists.txt | 7 +++ Examples/Python/src/ModuleEntry.cpp | 2 + Examples/Python/src/TGeo.cpp | 89 +++++++++++++++++++++++++++++ Examples/Python/src/TGeoStub.cpp | 15 +++++ 4 files changed, 113 insertions(+) create mode 100644 Examples/Python/src/TGeo.cpp create mode 100644 Examples/Python/src/TGeoStub.cpp diff --git a/Examples/Python/CMakeLists.txt b/Examples/Python/CMakeLists.txt index 0eb4b1ff4ec..dc359db0d0d 100644 --- a/Examples/Python/CMakeLists.txt +++ b/Examples/Python/CMakeLists.txt @@ -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) diff --git a/Examples/Python/src/ModuleEntry.cpp b/Examples/Python/src/ModuleEntry.cpp index a74d277f7a5..a78f04f9565 100644 --- a/Examples/Python/src/ModuleEntry.cpp +++ b/Examples/Python/src/ModuleEntry.cpp @@ -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); @@ -146,6 +147,7 @@ PYBIND11_MODULE(ActsPythonBindings, m) { addPythia8(ctx); addJson(ctx); addGeoModel(ctx); + addTGeo(ctx); addDetray(ctx); addHepMC3(ctx); addExaTrkXTrackFinding(ctx); diff --git a/Examples/Python/src/TGeo.cpp b/Examples/Python/src/TGeo.cpp new file mode 100644 index 00000000000..aab3b47bc23 --- /dev/null +++ b/Examples/Python/src/TGeo.cpp @@ -0,0 +1,89 @@ +// 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 + +#include +#include +#include +#include +#include + +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_>( + 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 covnersion convention + /// @param convertMaterial is a flag to convert the material + tgeo.def("convertToElements", + [](const std::string& rootFileName, + const std::vector& sensitiveMatches, + const std::string& localAxes, double scaleConversion, + bool convertMaterial) { + // Return vector + std::vector> + tgElements; + // TGeo import + TGeoManager::Import(rootFileName.c_str()); + if (gGeoManager != nullptr) { + auto tVolume = gGeoManager->GetTopVolume(); + if (tVolume != nullptr) { + TGeoHMatrix gmatrix = TGeoIdentity(tVolume->GetName()); + + TGeoParser::Options tgpOptions; + tgpOptions.volumeNames = {tVolume->GetName()}; + tgpOptions.targetNames = sensitiveMatches; + tgpOptions.parseRanges = {}; + tgpOptions.unit = scaleConversion; + TGeoParser::State tgpState; + tgpState.volume = tVolume; + tgpState.onBranch = true; + + TGeoParser::select(tgpState, tgpOptions, gmatrix); + tgElements.reserve(tgpState.selectedNodes.size()); + + for (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 diff --git a/Examples/Python/src/TGeoStub.cpp b/Examples/Python/src/TGeoStub.cpp new file mode 100644 index 00000000000..09a800e693f --- /dev/null +++ b/Examples/Python/src/TGeoStub.cpp @@ -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 From 5fd89bc50625ae4a4c81c7c620ef4afa5b0f6e64 Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Thu, 21 Nov 2024 11:24:08 +0100 Subject: [PATCH 2/5] fix spelling --- Examples/Python/src/TGeo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Python/src/TGeo.cpp b/Examples/Python/src/TGeo.cpp index aab3b47bc23..e02aa54eea8 100644 --- a/Examples/Python/src/TGeo.cpp +++ b/Examples/Python/src/TGeo.cpp @@ -42,7 +42,7 @@ void addTGeo(Context& ctx) { /// /// @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 covnersion convention + /// @param localAxes is the TGeo->ACTS axis conversion convention /// @param convertMaterial is a flag to convert the material tgeo.def("convertToElements", [](const std::string& rootFileName, From af94236468e23dc3f08cb6714032f2b338c0a5e4 Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Thu, 21 Nov 2024 14:48:23 +0100 Subject: [PATCH 3/5] remove unused parameter --- Examples/Python/src/TGeo.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Examples/Python/src/TGeo.cpp b/Examples/Python/src/TGeo.cpp index e02aa54eea8..67be234f731 100644 --- a/Examples/Python/src/TGeo.cpp +++ b/Examples/Python/src/TGeo.cpp @@ -43,12 +43,11 @@ void addTGeo(Context& ctx) { /// @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 convertMaterial is a flag to convert the material + /// @param scaleConversion is a unit scalor conversion factor tgeo.def("convertToElements", [](const std::string& rootFileName, const std::vector& sensitiveMatches, - const std::string& localAxes, double scaleConversion, - bool convertMaterial) { + const std::string& localAxes, double scaleConversion) { // Return vector std::vector> tgElements; From cab7f5dedb15ce0e819d17dbc7f0e179143f1a74 Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Tue, 3 Dec 2024 11:46:08 +0100 Subject: [PATCH 4/5] Update TGeo.cpp --- Examples/Python/src/TGeo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Python/src/TGeo.cpp b/Examples/Python/src/TGeo.cpp index 67be234f731..eafbeeacb68 100644 --- a/Examples/Python/src/TGeo.cpp +++ b/Examples/Python/src/TGeo.cpp @@ -44,7 +44,7 @@ void addTGeo(Context& ctx) { /// @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", + tgeo.def("_convertToElements", [](const std::string& rootFileName, const std::vector& sensitiveMatches, const std::string& localAxes, double scaleConversion) { From d13b719cc46668d5f92084901ba68b299b95c5ba Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Tue, 3 Dec 2024 17:03:29 +0100 Subject: [PATCH 5/5] Make SonarCloud happy --- Examples/Python/src/TGeo.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Examples/Python/src/TGeo.cpp b/Examples/Python/src/TGeo.cpp index eafbeeacb68..c67c2f3d653 100644 --- a/Examples/Python/src/TGeo.cpp +++ b/Examples/Python/src/TGeo.cpp @@ -61,7 +61,6 @@ void addTGeo(Context& ctx) { TGeoParser::Options tgpOptions; tgpOptions.volumeNames = {tVolume->GetName()}; tgpOptions.targetNames = sensitiveMatches; - tgpOptions.parseRanges = {}; tgpOptions.unit = scaleConversion; TGeoParser::State tgpState; tgpState.volume = tVolume; @@ -70,7 +69,7 @@ void addTGeo(Context& ctx) { TGeoParser::select(tgpState, tgpOptions, gmatrix); tgElements.reserve(tgpState.selectedNodes.size()); - for (auto& snode : tgpState.selectedNodes) { + for (const auto& snode : tgpState.selectedNodes) { auto identifier = Acts::TGeoDetectorElement::Identifier(); auto tgElement = TGeoLayerBuilder::defaultElementFactory( identifier, *snode.node, *snode.transform, localAxes,