Skip to content

Commit

Permalink
feat: Detray plugin geometry (#3299)
Browse files Browse the repository at this point in the history
  • Loading branch information
EleniXoch authored Jul 4, 2024
1 parent b10e8e2 commit 47e534e
Show file tree
Hide file tree
Showing 10 changed files with 702 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 @@ -84,6 +84,13 @@ else()
target_sources(ActsPythonBindings PRIVATE src/GeoModelStub.cpp)
endif()

if(ACTS_BUILD_PLUGIN_DETRAY)
target_link_libraries(ActsPythonBindings PUBLIC ActsPluginDetray)
target_sources(ActsPythonBindings PRIVATE src/Detray.cpp)
else()
target_sources(ActsPythonBindings PRIVATE src/DetrayStub.cpp)
endif()

if(ACTS_BUILD_PLUGIN_ACTSVG)
target_link_libraries(ActsPythonBindings PUBLIC ActsExamplesIoSvg)
target_sources(ActsPythonBindings PRIVATE src/Svg.cpp)
Expand Down
64 changes: 64 additions & 0 deletions Examples/Python/src/Detray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of the Acts project.
//
// Copyright (C) 2024 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/Detector/Detector.hpp"
#include "Acts/Plugins/Detray/DetrayConverter.hpp"
#include "Acts/Plugins/Python/Utilities.hpp"

#include <memory>
#include <string>

#include <pybind11/pybind11.h>
#include <vecmem/memory/host_memory_resource.hpp>
#include <vecmem/memory/memory_resource.hpp>

#include "detray/builders/detector_builder.hpp"
#include "detray/io/frontend/detector_reader_config.hpp"

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

using namespace Acts;
using namespace detray;
using namespace detray::io::detail;

namespace Acts::Python {

void addDetray(Context& ctx) {
auto [m, mex] = ctx.get("main", "examples");

{
py::class_<detector<default_metadata>,
std::shared_ptr<detector<default_metadata>>>(m,
"detray_detector");
}

{ mex.def("writeToJson", &DetrayConverter::writeToJson); }
/**
{
/// @brief Converts an Acts::Detector to a detray::detector
mex.def("convertDetectorToDetray",
[](const Acts::GeometryContext& gctx,
const Acts::Experimental::Detector& acts_detector,
const std::string& name) -> auto
{//detector<default_metadata>
// Create a host memory resource
vecmem::host_memory_resource host_mr;
// Convert Acts detector to detray detector using the
detray converter function auto det_tuple =
DetrayConverter::detrayConvert(acts_detector, gctx, host_mr);
return true; //TO DO:: cannot return tuple
});
}
**/
}
} // namespace Acts::Python
13 changes: 13 additions & 0 deletions Examples/Python/src/DetrayStub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is part of the Acts project.
//
// Copyright (C) 2024 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/Plugins/Python/Utilities.hpp"

namespace Acts::Python {
void addDetray(Context& /*ctx*/) {}
} // 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 @@ -71,6 +71,7 @@ void addDigitization(Context& ctx);
void addPythia8(Context& ctx);
void addGeoModel(Context& ctx);
void addJson(Context& ctx);
void addDetray(Context& ctx);
void addHepMC3(Context& ctx);
void addExaTrkXTrackFinding(Context& ctx);
void addSvg(Context& ctx);
Expand Down Expand Up @@ -132,6 +133,7 @@ PYBIND11_MODULE(ActsPythonBindings, m) {
addPythia8(ctx);
addJson(ctx);
addGeoModel(ctx);
addDetray(ctx);
addHepMC3(ctx);
addExaTrkXTrackFinding(ctx);
addObj(ctx);
Expand Down
2 changes: 2 additions & 0 deletions Examples/Scripts/Python/detector_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@
)

acts.examples.writeDetectorToJsonDetray(geoContext, detector, "odd-detray")

# det_detector = acts.examples.DetrayConverter(geoContext, detector,"odd-detray")
1 change: 1 addition & 0 deletions Plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_component_if(Json PluginJson ACTS_BUILD_PLUGIN_JSON)
add_component_if(Legacy PluginLegacy ACTS_BUILD_PLUGIN_LEGACY)
add_component_if(Onnx PluginOnnx ACTS_BUILD_PLUGIN_ONNX)
add_component_if(ExaTrkX PluginExaTrkX ACTS_BUILD_PLUGIN_EXATRKX)
add_component_if(Detray PluginDetray ACTS_BUILD_PLUGIN_DETRAY)

# dependent plugins. depend either on a independent plugins or on one another
add_component_if(TGeo PluginTGeo ACTS_BUILD_PLUGIN_TGEO)
Expand Down
34 changes: 34 additions & 0 deletions Plugins/Detray/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
add_library(
ActsPluginDetray SHARED
src/DetrayConverter.cpp)

add_dependencies(ActsPluginDetray
detray::core
covfie::core
vecmem::core)

target_include_directories(
ActsPluginDetray
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:include>)

target_link_libraries(
ActsPluginDetray
PUBLIC
ActsCore
detray::core
detray::core_array
detray::io
detray::utils
detray::svgtools
vecmem::core)

install(
TARGETS ActsPluginDetray
EXPORT ActsPluginDetrayTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY include/Acts
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
117 changes: 117 additions & 0 deletions Plugins/Detray/include/Acts/Plugins/Detray/DetrayConversionHelper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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/.

#pragma once

#include "Acts/Surfaces/SurfaceBounds.hpp"
#include "Acts/Utilities/BinningData.hpp"

#include <string>
#include <tuple>

#include <nlohmann/json.hpp>

#include "detray/io/frontend/definitions.hpp"

namespace Acts::DetrayConversionHelper {

/// @brief Helper function to switch keys from ACTS to detray
///
/// DETRAY types @todo change to detray imports when available
/// annulus2 = 0u,
/// cuboid3 = 1u,
/// cylinder2 = 2u,
/// cylinder3 = 3u,
/// portal_cylinder2 = 4u,
/// rectangle2 = 5u,
/// ring2 = 6u,
/// trapezoid2 = 7u,
/// cell_wire = 8u,
/// straw_wire = 9u,
/// single1 = 10u,
/// single2 = 11u,
/// single3 = 12u,
/// unknown = 13u
///
/// @param sBounds is the surface bounds type
/// @param portal is the flag for conversion into detray portal format
///
/// @return type and value array in detray format
inline static std::tuple<unsigned int, std::vector<ActsScalar>> maskFromBounds(
const Acts::SurfaceBounds& sBounds, bool portal = false) {
auto bType = sBounds.type();
auto bValues = sBounds.values();
// Return value
unsigned int type = 13u;
std::vector<double> boundaries = bValues;
// Special treatment for some portals
if (portal && bType == SurfaceBounds::BoundsType::eCylinder) {
boundaries = {bValues[0u], -bValues[1u], bValues[1u]};
type = 4u;
} else {
switch (bType) {
case SurfaceBounds::BoundsType::eAnnulus: {
type = 0u;
} break;
case SurfaceBounds::BoundsType::eRectangle: {
type = 5u;
// ACTS: eMinX = 0, eMinY = 1, eMaxX = 2, eMaxY = 3,
// detray: e_half_x, e_half_y
boundaries = {0.5 * (bValues[2] - bValues[0]),
0.5 * (bValues[3] - bValues[1])};
} break;
case SurfaceBounds::BoundsType::eCylinder: {
boundaries = {bValues[0u], -bValues[1u], bValues[1u]};
type = 2u;
} break;
case SurfaceBounds::BoundsType::eTrapezoid: {
type = 7u;
boundaries = {bValues[0u], bValues[1u], bValues[2u],
1 / (2 * bValues[2u])};
} break;
case SurfaceBounds::BoundsType::eDisc: {
boundaries = {bValues[0u], bValues[1u]};
type = 6u;
} break;
default:
break;
}
}
return std::tie(type, boundaries);
}

/// Determine the acceleration link from a grid
///
/// @param casts are the grid axes cast types
///
/// @return the acceleration link idnetifier
template <typename binning_values_t>
inline static std::size_t accelerationLink(const binning_values_t& casts) {
// Default is `brute_force`
std::size_t accLink = detray::io::accel_id::brute_force;
if (casts.size() == 2u) {
if (casts[0u] == binX && casts[1u] == binY) {
accLink = detray::io::accel_id::cartesian2_grid;
} else if (casts[0u] == binR && casts[1u] == binPhi) {
accLink = detray::io::accel_id::polar2_grid;
} else if (casts[0u] == binZ && casts[1u] == binPhi) {
accLink = detray::io::accel_id::cylinder2_grid;
} else if (casts[0u] == binZ && casts[1u] == binR) {
accLink = detray::io::accel_id::cylinder3_grid;
}
} else if (casts.size() == 3u) {
if (casts[0u] == binX && casts[1u] == binY && casts[2u] == binZ) {
accLink = detray::io::accel_id::cuboid3_grid;
} else if (casts[0u] == binZ && casts[1u] == binPhi && casts[2u] == binR) {
accLink = detray::io::accel_id::cylinder3_grid;
}
}
return accLink;
}

} // namespace Acts::DetrayConversionHelper
Loading

0 comments on commit 47e534e

Please sign in to comment.