File indexing completed on 2026-11-10 09:29:25
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Geometry/GeometryIdentifier.hpp"
0014 #include "Acts/Geometry/TrackingGeometry.hpp"
0015 #include "Acts/Geometry/TrackingVolume.hpp"
0016 #include "Acts/Surfaces/PlaneSurface.hpp"
0017 #include "Acts/Surfaces/RectangleBounds.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019 #include "Acts/Utilities/Logger.hpp"
0020 #include "ActsPlugins/Detray/DetrayGeometryConverter.hpp"
0021 #include "ActsPlugins/Detray/DetrayPayloadConverter.hpp"
0022 #include "ActsPlugins/Detray/DetraySurfaceIdMap.hpp"
0023 #include "ActsTests/CommonHelpers/CylindricalTrackingGeometry.hpp"
0024
0025 #include <cstddef>
0026 #include <memory>
0027 #include <stdexcept>
0028 #include <vector>
0029
0030 #include <detray/core/detector.hpp>
0031 #include <detray/detectors/default_metadata.hpp>
0032 #include <detray/geometry/identifier.hpp>
0033 #include <vecmem/memory/host_memory_resource.hpp>
0034
0035 using namespace Acts;
0036 using namespace ActsPlugins;
0037
0038 namespace ActsTests {
0039
0040 BOOST_AUTO_TEST_SUITE(DetrayConversion)
0041
0042 BOOST_AUTO_TEST_CASE(DetraySurfaceIdMapFromEntries) {
0043 using detray::geometry::identifier;
0044
0045 const DetraySurfaceIdMap emptyMap;
0046 BOOST_CHECK(emptyMap.empty());
0047
0048 auto makeSurface = [](GeometryIdentifier id) {
0049 auto surface = Surface::makeShared<PlaneSurface>(
0050 Transform3::Identity(), std::make_shared<RectangleBounds>(1., 1.));
0051 surface->assignGeometryId(id);
0052 return surface;
0053 };
0054
0055 const auto id1 = GeometryIdentifier().withVolume(1).withSensitive(1);
0056 const auto id2 = GeometryIdentifier().withVolume(1).withSensitive(2);
0057 const auto surface1 = makeSurface(id1);
0058 const auto surface2 = makeSurface(id2);
0059
0060 const std::vector<DetraySurfaceIdMap::Entry> entries = {
0061 {identifier{10}, surface1.get()}, {identifier{11}, surface2.get()}};
0062 const DetraySurfaceIdMap map(entries);
0063
0064 BOOST_CHECK_EQUAL(map.size(), 2u);
0065 BOOST_CHECK(map.toDetray(id1).value() == identifier{10});
0066 BOOST_CHECK(map.toDetray(id2).value() == identifier{11});
0067 BOOST_CHECK_EQUAL(map.fromDetray(identifier{10}).value(), id1);
0068 BOOST_CHECK_EQUAL(map.fromDetray(identifier{11}).value(), id2);
0069 BOOST_CHECK_EQUAL(map.surface(identifier{10}), surface1.get());
0070 BOOST_CHECK_EQUAL(map.surface(identifier{11}), surface2.get());
0071
0072
0073 BOOST_CHECK(!map.toDetray(GeometryIdentifier().withVolume(2)).has_value());
0074 BOOST_CHECK(!map.fromDetray(identifier{12}).has_value());
0075 BOOST_CHECK_EQUAL(map.surface(identifier{12}), nullptr);
0076
0077
0078 const auto surface3 = makeSurface(GeometryIdentifier().withVolume(3));
0079 const std::vector<DetraySurfaceIdMap::Entry> duplicateDetray = {
0080 {identifier{10}, surface1.get()}, {identifier{10}, surface3.get()}};
0081 BOOST_CHECK_THROW(DetraySurfaceIdMap{duplicateDetray}, std::invalid_argument);
0082 const std::vector<DetraySurfaceIdMap::Entry> duplicateActs = {
0083 {identifier{10}, surface1.get()}, {identifier{12}, surface1.get()}};
0084 BOOST_CHECK_THROW(DetraySurfaceIdMap{duplicateActs}, std::invalid_argument);
0085 const std::vector<DetraySurfaceIdMap::Entry> nullSurface = {
0086 {identifier{10}, nullptr}};
0087 BOOST_CHECK_THROW(DetraySurfaceIdMap{nullSurface}, std::invalid_argument);
0088 }
0089
0090 BOOST_AUTO_TEST_CASE(DetraySurfaceIdMapFromConversion) {
0091 using detector_t =
0092 detray::host::detector<detray::default_metadata<detray::array<double>>>;
0093
0094 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0095
0096 CylindricalTrackingGeometry cGeometry(gctx, true);
0097 std::shared_ptr<const TrackingGeometry> tGeometry =
0098 cGeometry(*getDefaultLogger("Geo", Logging::INFO));
0099
0100 DetrayPayloadConverter::Config payloadCfg;
0101 tGeometry->apply([&payloadCfg](const TrackingVolume& volume) {
0102 if (volume.volumeName() == "Beampipe") {
0103 payloadCfg.beampipeVolume = &volume;
0104 }
0105 });
0106
0107 DetrayGeometryConverter::Config cfg;
0108 cfg.payloadConverter = std::make_shared<DetrayPayloadConverter>(
0109 payloadCfg, getDefaultLogger("PayloadCnv", Logging::INFO));
0110 DetrayGeometryConverter converter(cfg,
0111 getDefaultLogger("GeoCnv", Logging::INFO));
0112
0113 vecmem::host_memory_resource mr;
0114 auto geometry = converter.convert<detector_t::metadata>(mr, gctx, tGeometry);
0115 const detector_t& detector = *geometry.detector;
0116 const DetraySurfaceIdMap& map = geometry.surfaceIds;
0117
0118
0119
0120 std::size_t nSensitive = 0;
0121 for (const auto& surface : detector.surfaces()) {
0122 if (!surface.is_sensitive()) {
0123 BOOST_CHECK(!map.fromDetray(surface.identifier()).has_value());
0124 BOOST_CHECK_EQUAL(map.surface(surface.identifier()), nullptr);
0125 continue;
0126 }
0127 ++nSensitive;
0128 const GeometryIdentifier id(surface.source);
0129 BOOST_CHECK_EQUAL(map.fromDetray(surface.identifier()).value(), id);
0130 BOOST_CHECK(map.toDetray(id).value() == surface.identifier());
0131 BOOST_CHECK_EQUAL(map.surface(surface.identifier()),
0132 tGeometry->findSurface(id));
0133 }
0134 BOOST_CHECK_GT(nSensitive, 0u);
0135 BOOST_CHECK_EQUAL(map.size(), nSensitive);
0136
0137
0138 std::size_t nActsSensitive = 0;
0139 tGeometry->visitSurfaces([&](const Surface* surface) {
0140 ++nActsSensitive;
0141 BOOST_CHECK(map.toDetray(surface->geometryId()).has_value());
0142 });
0143 BOOST_CHECK_EQUAL(nActsSensitive, map.size());
0144
0145
0146 const DetraySurfaceIdMap rebuilt(detector, *tGeometry);
0147 BOOST_CHECK_EQUAL(rebuilt.size(), map.size());
0148 }
0149
0150 BOOST_AUTO_TEST_SUITE_END()
0151
0152 }