Skip to content

Commit

Permalink
tgeo binding
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzburger committed Nov 21, 2024
1 parent 2cf38f7 commit d949c49
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
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
89 changes: 89 additions & 0 deletions Examples/Python/src/TGeo.cpp
Original file line number Diff line number Diff line change
@@ -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 <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 covnersion convention
/// @param convertMaterial is a flag to convert the material
tgeo.def("convertToElements",
[](const std::string& rootFileName,
const std::vector<std::string>& sensitiveMatches,
const std::string& localAxes, double scaleConversion,
bool convertMaterial) {
// Return vector
std::vector<std::shared_ptr<const Acts::TGeoDetectorElement>>
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
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

0 comments on commit d949c49

Please sign in to comment.