Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-01 09:22:18

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Geometry/NavigationPolicyFactory.hpp"
0014 #include "Acts/Geometry/TrackingGeometry.hpp"
0015 #include "Acts/Geometry/TrackingVolume.hpp"
0016 #include "Acts/Navigation/INavigationPolicy.hpp"
0017 #include "Acts/Utilities/Logger.hpp"
0018 #include "ActsPlugins/Detray/DetrayJsonConverter.hpp"
0019 #include "ActsPlugins/Detray/DetrayPayloadConverter.hpp"
0020 #include "ActsPlugins/Detray/DetraySurfaceGridPolicy.hpp"
0021 #include "ActsPlugins/Json/TrackingGeometryJsonConverter.hpp"
0022 #include "ActsTests/CommonHelpers/CylindricalTrackingGeometry.hpp"
0023 
0024 #include <map>
0025 #include <memory>
0026 #include <set>
0027 #include <string>
0028 #include <vector>
0029 
0030 #include <detray/geometry/identifier.hpp>
0031 #include <detray/io/frontend/payloads.hpp>
0032 
0033 using namespace Acts;
0034 using namespace Acts::UnitLiterals;
0035 using namespace ActsPlugins;
0036 
0037 namespace ActsTests {
0038 
0039 namespace {
0040 
0041 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0042 
0043 std::shared_ptr<TrackingGeometry> buildGeometry(
0044     std::optional<DetraySurfaceGridConfig> policyConfig = std::nullopt) {
0045   // The builder owns the detector elements, keep it alive for the test
0046   static std::vector<std::unique_ptr<CylindricalTrackingGeometry>> builders;
0047   auto& cGeometry = *builders.emplace_back(
0048       std::make_unique<CylindricalTrackingGeometry>(gctx, true));
0049   if (policyConfig.has_value()) {
0050     cGeometry.extendLayerNavigationPolicy =
0051         [cfg = *policyConfig](NavigationPolicyFactory&& factory) {
0052           return std::move(factory).add<DetraySurfaceGridPolicy>(cfg);
0053         };
0054   }
0055   return cGeometry(*getDefaultLogger("Geo", Logging::WARNING));
0056 }
0057 
0058 /// Layer name -> bin index -> surface indices
0059 using GridContents =
0060     std::map<std::string,
0061              std::map<std::pair<unsigned, unsigned>, std::set<std::size_t>>>;
0062 
0063 struct Converted {
0064   GridContents grids;
0065   /// Layer name -> sensitive surface indices
0066   std::map<std::string, std::set<std::size_t>> sensitives;
0067 };
0068 
0069 Converted convert(const TrackingGeometry& geometry) {
0070   DetrayPayloadConverter::Config cfg;
0071   geometry.apply([&cfg](const TrackingVolume& volume) {
0072     if (volume.volumeName() == "Beampipe") {
0073       cfg.beampipeVolume = &volume;
0074     }
0075   });
0076   DetrayPayloadConverter converter(cfg,
0077                                    getDefaultLogger("Cnv", Logging::WARNING));
0078   auto payloads = converter.convertTrackingGeometry(gctx, geometry);
0079 
0080   std::map<std::size_t, std::string> names;
0081   Converted out;
0082   for (const auto& volume : payloads.geometry.volumes) {
0083     names[volume.index.link] = volume.name;
0084     for (const auto& [i, surface] : enumerate(volume.surfaces)) {
0085       if (surface.type == detray::surface_id::e_sensitive) {
0086         out.sensitives[volume.name].insert(i);
0087       }
0088     }
0089   }
0090   if (!payloads.surface_grids.has_value()) {
0091     return out;
0092   }
0093   for (const auto& [volumeIndex, grids] : payloads.surface_grids->grids) {
0094     BOOST_REQUIRE_EQUAL(grids.size(), 1u);
0095     auto& contents = out.grids[names.at(volumeIndex)];
0096     for (const auto& bin : grids.front().bins) {
0097       contents[{bin.loc_index.at(0), bin.loc_index.at(1)}].insert(
0098           bin.content.begin(), bin.content.end());
0099     }
0100   }
0101   return out;
0102 }
0103 
0104 std::size_t entries(const GridContents& grids) {
0105   std::size_t n = 0;
0106   for (const auto& [name, bins] : grids) {
0107     for (const auto& [bin, content] : bins) {
0108       n += content.size();
0109     }
0110   }
0111   return n;
0112 }
0113 
0114 DetraySurfaceGridConfig straightFromOrigin() {
0115   DetraySurfaceGridConfig cfg;
0116   cfg.vertexZHalfRange = 0.;
0117   cfg.nVertexZ = 1;
0118   cfg.minPt = 0.;
0119   return cfg;
0120 }
0121 
0122 }  // namespace
0123 
0124 BOOST_AUTO_TEST_SUITE(DetraySuite)
0125 
0126 BOOST_AUTO_TEST_CASE(DetraySurfaceGridFillReachesEverySensitive) {
0127   auto geometry = buildGeometry(DetraySurfaceGridConfig{});
0128   const Converted filled = convert(*geometry);
0129 
0130   BOOST_CHECK_EQUAL(filled.grids.size(), 4u);
0131   for (const auto& [name, bins] : filled.grids) {
0132     std::set<std::size_t> seen;
0133     for (const auto& [bin, content] : bins) {
0134       seen.insert(content.begin(), content.end());
0135     }
0136     BOOST_CHECK(seen == filled.sensitives.at(name));
0137   }
0138 }
0139 
0140 BOOST_AUTO_TEST_CASE(DetraySurfaceGridOnlyWithPolicy) {
0141   // A surface array alone has no detray grid: its bins don't serve detray's
0142   // lookup
0143   auto geometry = buildGeometry();
0144   BOOST_CHECK(convert(*geometry).grids.empty());
0145 }
0146 
0147 BOOST_AUTO_TEST_CASE(DetraySurfaceGridFillFamilyWidensBins) {
0148   const Converted narrow = convert(*buildGeometry(straightFromOrigin()));
0149   const Converted wide = convert(*buildGeometry(DetraySurfaceGridConfig{}));
0150 
0151   // A wider track family only adds surfaces to bins
0152   for (const auto& [name, bins] : narrow.grids) {
0153     for (const auto& [bin, content] : bins) {
0154       const auto& wider = wide.grids.at(name).at(bin);
0155       BOOST_CHECK(std::ranges::includes(wider, content));
0156     }
0157   }
0158   BOOST_CHECK_GT(entries(wide.grids), entries(narrow.grids));
0159 }
0160 
0161 BOOST_AUTO_TEST_CASE(DetraySurfaceGridPolicyJsonRoundTrip) {
0162   DetraySurfaceGridConfig cfg = straightFromOrigin();
0163   cfg.bField = 3_T;
0164   cfg.numberOfBinsFactor = 1.5;
0165   auto geometry = buildGeometry(cfg);
0166 
0167   // Without registration the writer does not know the policy
0168   TrackingGeometryJsonConverter plainWriter(
0169       TrackingGeometryJsonConverter::Config::defaultConfig());
0170   BOOST_CHECK_THROW(plainWriter.toJson(gctx, *geometry), std::exception);
0171 
0172   auto jsonCfg = TrackingGeometryJsonConverter::Config::defaultConfig();
0173   addDetrayNavigationPolicies(jsonCfg);
0174   TrackingGeometryJsonConverter converter(jsonCfg);
0175   auto readBack = converter.fromJson(gctx, converter.toJson(gctx, *geometry));
0176 
0177   std::size_t nPolicies = 0;
0178   readBack->apply([&](const TrackingVolume& volume) {
0179     const INavigationPolicy* policy = volume.navigationPolicy();
0180     if (policy == nullptr) {
0181       return;
0182     }
0183     policy->visit([&](const INavigationPolicy& p) {
0184       if (const auto* grid = dynamic_cast<const DetraySurfaceGridPolicy*>(&p);
0185           grid != nullptr) {
0186         ++nPolicies;
0187         BOOST_CHECK_EQUAL(grid->config().vertexZHalfRange,
0188                           cfg.vertexZHalfRange);
0189         BOOST_CHECK_EQUAL(grid->config().minPt, cfg.minPt);
0190         BOOST_CHECK_EQUAL(grid->config().bField, cfg.bField);
0191         BOOST_CHECK_EQUAL(grid->config().nVertexZ, cfg.nVertexZ);
0192         BOOST_CHECK_EQUAL(grid->config().nCurvature, cfg.nCurvature);
0193         BOOST_CHECK(grid->config().numberOfBinsFactor ==
0194                     cfg.numberOfBinsFactor);
0195       }
0196     });
0197   });
0198   BOOST_CHECK_EQUAL(nPolicies, 4u);
0199 
0200   // The read-back geometry converts to the same grids
0201   BOOST_CHECK(convert(*readBack).grids == convert(*geometry).grids);
0202 }
0203 
0204 BOOST_AUTO_TEST_CASE(DetraySurfaceGridLookupRadius) {
0205   auto radii = [&](bool onLayer) {
0206     DetraySurfaceGridConfig fill;
0207     fill.lookupOnLayer = onLayer;
0208     auto geometry = buildGeometry(fill);
0209     DetrayPayloadConverter::Config cfg;
0210     geometry->apply([&cfg](const TrackingVolume& volume) {
0211       if (volume.volumeName() == "Beampipe") {
0212         cfg.beampipeVolume = &volume;
0213       }
0214     });
0215     DetrayPayloadConverter converter(cfg,
0216                                      getDefaultLogger("Cnv", Logging::WARNING));
0217     auto payloads = converter.convertTrackingGeometry(gctx, *geometry);
0218     std::vector<std::optional<double>> out;
0219     for (const auto& [volume, grids] : payloads.surface_grids->grids) {
0220       out.push_back(grids.front().reference_radius);
0221     }
0222     return out;
0223   };
0224 
0225   // On the layer: the mean module radius, inside the layer volume
0226   const auto onLayer = radii(true);
0227   BOOST_CHECK_EQUAL(onLayer.size(), 4u);
0228   for (const auto& r : onLayer) {
0229     BOOST_REQUIRE(r.has_value());
0230     BOOST_CHECK_GT(*r, 0.);
0231   }
0232   // Otherwise detray derives it from the portals
0233   for (const auto& r : radii(false)) {
0234     BOOST_CHECK(!r.has_value());
0235   }
0236 }
0237 
0238 BOOST_AUTO_TEST_SUITE_END()
0239 
0240 }  // namespace ActsTests