File indexing completed on 2025-01-18 09:13:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Plugins/Json/GridJsonConverter.hpp"
0012 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0013 #include "Acts/Utilities/GridAccessHelpers.hpp"
0014 #include "Acts/Utilities/GridAxisGenerators.hpp"
0015
0016 #include <array>
0017 #include <fstream>
0018 #include <memory>
0019 #include <numbers>
0020 #include <vector>
0021
0022 #include <nlohmann/json.hpp>
0023
0024 BOOST_AUTO_TEST_SUITE(GridJsonConversion)
0025
0026 BOOST_AUTO_TEST_CASE(Grid1DSingleEntry) {
0027
0028 using EqBound = Acts::GridAxisGenerators::EqBound;
0029
0030 EqBound eqBound{{0., 5.}, 5};
0031
0032 using GridTypeEQB = typename EqBound::template grid_type<std::size_t>;
0033 GridTypeEQB eqBoundGrid(eqBound());
0034
0035 eqBoundGrid.at(1u) = 1u;
0036 eqBoundGrid.at(2u) = 2u;
0037 eqBoundGrid.at(3u) = 3u;
0038 eqBoundGrid.at(4u) = 4u;
0039 eqBoundGrid.at(5u) = 5u;
0040
0041 auto p1 = typename GridTypeEQB::point_t{0.5};
0042 BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p1), 1u);
0043 auto p2 = typename GridTypeEQB::point_t{1.5};
0044 BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p2), 2u);
0045 auto p3 = typename GridTypeEQB::point_t{2.5};
0046 BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p3), 3u);
0047 auto p4 = typename GridTypeEQB::point_t{3.5};
0048 BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p4), 4u);
0049 auto p5 = typename GridTypeEQB::point_t{4.5};
0050 BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p5), 5u);
0051
0052 nlohmann::json eqBoundJson = Acts::GridJsonConverter::toJson(eqBoundGrid);
0053
0054 auto eqBoundGridRead =
0055 Acts::GridJsonConverter::fromJson<EqBound, std::size_t>(eqBoundJson,
0056 eqBound);
0057
0058 BOOST_CHECK_EQUAL(eqBoundGridRead.at(1u), 1u);
0059 BOOST_CHECK_EQUAL(eqBoundGridRead.at(2u), 2u);
0060 BOOST_CHECK_EQUAL(eqBoundGridRead.at(3u), 3u);
0061 BOOST_CHECK_EQUAL(eqBoundGridRead.at(4u), 4u);
0062 BOOST_CHECK_EQUAL(eqBoundGridRead.at(5u), 5u);
0063
0064
0065 using VarBound = Acts::GridAxisGenerators::VarBound;
0066
0067 VarBound varBound{{10., 11., 22., 333., 4444., 55555.}};
0068
0069 using GridTypeEQV = typename VarBound::template grid_type<std::size_t>;
0070 GridTypeEQV varBoundGrid(varBound());
0071
0072 varBoundGrid.at(1u) = 1u;
0073 varBoundGrid.at(2u) = 2u;
0074 varBoundGrid.at(3u) = 3u;
0075 varBoundGrid.at(4u) = 4u;
0076 varBoundGrid.at(5u) = 5u;
0077
0078 nlohmann::json varBoundJson = Acts::GridJsonConverter::toJson(varBoundGrid);
0079
0080 auto varBoundGridRead =
0081 Acts::GridJsonConverter::fromJson<VarBound, std::size_t>(varBoundJson,
0082 varBound);
0083
0084 BOOST_CHECK_EQUAL(varBoundGridRead.at(1u), 1u);
0085 BOOST_CHECK_EQUAL(varBoundGridRead.at(2u), 2u);
0086 BOOST_CHECK_EQUAL(varBoundGridRead.at(3u), 3u);
0087 BOOST_CHECK_EQUAL(varBoundGridRead.at(4u), 4u);
0088 BOOST_CHECK_EQUAL(varBoundGridRead.at(5u), 5u);
0089
0090
0091 using EqClosed = Acts::GridAxisGenerators::EqClosed;
0092
0093 EqClosed eqClosed{{0., 5.}, 5};
0094
0095 using GridTypeEQC = typename EqClosed::template grid_type<std::size_t>;
0096 GridTypeEQC eqClosedGrid(eqClosed());
0097
0098 eqClosedGrid.at(1u) = 1u;
0099 eqClosedGrid.at(2u) = 2u;
0100 eqClosedGrid.at(3u) = 3u;
0101 eqClosedGrid.at(4u) = 4u;
0102 eqClosedGrid.at(5u) = 5u;
0103
0104 nlohmann::json eqClosedJson = Acts::GridJsonConverter::toJson(eqClosedGrid);
0105
0106 auto eqClosedGridRead =
0107 Acts::GridJsonConverter::fromJson<EqClosed, std::size_t>(eqClosedJson,
0108 eqClosed);
0109
0110 BOOST_CHECK_EQUAL(eqClosedGridRead.at(1u), 1u);
0111 BOOST_CHECK_EQUAL(eqClosedGridRead.at(2u), 2u);
0112 BOOST_CHECK_EQUAL(eqClosedGridRead.at(3u), 3u);
0113 BOOST_CHECK_EQUAL(eqClosedGridRead.at(4u), 4u);
0114 BOOST_CHECK_EQUAL(eqClosedGridRead.at(5u), 5u);
0115 }
0116
0117 BOOST_AUTO_TEST_CASE(Grid1DArrayEntry) {
0118
0119 using EqBound = Acts::GridAxisGenerators::EqBound;
0120
0121 EqBound eqBound{{0., 5.}, 5};
0122
0123 using GridTypeEQB =
0124 typename EqBound::template grid_type<std::array<std::size_t, 2u>>;
0125 GridTypeEQB eqBoundGrid(eqBound());
0126
0127 eqBoundGrid.at(1u) = {1u, 1u};
0128 eqBoundGrid.at(2u) = {2u, 2u};
0129 eqBoundGrid.at(3u) = {3u, 3u};
0130 eqBoundGrid.at(4u) = {4u, 4u};
0131 eqBoundGrid.at(5u) = {5u, 5u};
0132
0133 nlohmann::json eqBoundJson = Acts::GridJsonConverter::toJson(eqBoundGrid);
0134
0135 auto eqBoundGridRead =
0136 Acts::GridJsonConverter::fromJson<EqBound, std::array<std::size_t, 2u>>(
0137 eqBoundJson, eqBound);
0138
0139 BOOST_CHECK((eqBoundGridRead.at(1u) == std::array<std::size_t, 2u>{1u, 1u}));
0140 BOOST_CHECK((eqBoundGridRead.at(2u) == std::array<std::size_t, 2u>{2u, 2u}));
0141 BOOST_CHECK((eqBoundGridRead.at(3u) == std::array<std::size_t, 2u>{3u, 3u}));
0142 BOOST_CHECK((eqBoundGridRead.at(4u) == std::array<std::size_t, 2u>{4u, 4u}));
0143 BOOST_CHECK((eqBoundGridRead.at(5u) == std::array<std::size_t, 2u>{5u, 5u}));
0144 }
0145
0146 BOOST_AUTO_TEST_CASE(Grid2DSingleEntryBound) {
0147 using EqBoundEqBound = Acts::GridAxisGenerators::EqBoundEqBound;
0148
0149 EqBoundEqBound eqBound2{{0., 5.}, 5, {0., 2.}, 2};
0150
0151 using GridTypeEQB2 = typename EqBoundEqBound::template grid_type<std::size_t>;
0152 GridTypeEQB2 eqBound2Grid(eqBound2());
0153
0154
0155 using GridPoint = typename GridTypeEQB2::point_t;
0156
0157
0158 GridPoint p11{0.5, 0.5};
0159 GridPoint p12{1.5, 0.5};
0160 GridPoint p13{2.5, 0.5};
0161 GridPoint p14{3.5, 0.5};
0162 GridPoint p15{4.5, 0.5};
0163 eqBound2Grid.atPosition(p11) = 11u;
0164 eqBound2Grid.atPosition(p12) = 12u;
0165 eqBound2Grid.atPosition(p13) = 13u;
0166 eqBound2Grid.atPosition(p14) = 14u;
0167 eqBound2Grid.atPosition(p15) = 15u;
0168
0169
0170 GridPoint p21{0.5, 1.5};
0171 GridPoint p22{1.5, 1.5};
0172 GridPoint p23{2.5, 1.5};
0173 GridPoint p24{3.5, 1.5};
0174 GridPoint p25{4.5, 1.5};
0175 eqBound2Grid.atPosition(p21) = 21u;
0176 eqBound2Grid.atPosition(p22) = 22u;
0177 eqBound2Grid.atPosition(p23) = 23u;
0178 eqBound2Grid.atPosition(p24) = 24u;
0179 eqBound2Grid.atPosition(p25) = 25u;
0180
0181 nlohmann::json eqBound2Json = Acts::GridJsonConverter::toJson(eqBound2Grid);
0182
0183 auto eqBound2JsonRead =
0184 Acts::GridJsonConverter::fromJson<EqBoundEqBound, std::size_t>(
0185 eqBound2Json, eqBound2);
0186
0187 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p11), 11u);
0188 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p12), 12u);
0189 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p13), 13u);
0190 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p14), 14u);
0191 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p15), 15u);
0192 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p21), 21u);
0193 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p22), 22u);
0194 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p23), 23u);
0195 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p24), 24u);
0196 BOOST_CHECK_EQUAL(eqBound2JsonRead.atPosition(p25), 25u);
0197 }
0198
0199 BOOST_AUTO_TEST_CASE(Grid2DSingleEntryBoundClosed) {
0200 using EqBoundEqClosed = Acts::GridAxisGenerators::EqBoundEqClosed;
0201
0202 EqBoundEqClosed eqBoundEqClosed{
0203 {-6., 6.}, 3, {-std::numbers::pi, std::numbers::pi}, 3};
0204
0205 using GridTypeEQBEQC =
0206 typename EqBoundEqClosed::template grid_type<std::size_t>;
0207 GridTypeEQBEQC eqBoundEqClosedGrid(eqBoundEqClosed());
0208
0209
0210 using GridPoint = typename GridTypeEQBEQC::point_t;
0211
0212
0213 GridPoint p11{-5, -2.};
0214 GridPoint p12{0., -2};
0215 GridPoint p13{5, -2};
0216 eqBoundEqClosedGrid.atPosition(p11) = 11u;
0217 eqBoundEqClosedGrid.atPosition(p12) = 12u;
0218 eqBoundEqClosedGrid.atPosition(p13) = 13u;
0219
0220
0221 GridPoint p21{-5., 0.};
0222 GridPoint p22{0., 0.};
0223 GridPoint p23{5., 0.};
0224 eqBoundEqClosedGrid.atPosition(p21) = 21u;
0225 eqBoundEqClosedGrid.atPosition(p22) = 22u;
0226 eqBoundEqClosedGrid.atPosition(p23) = 23u;
0227
0228
0229 GridPoint p31{-5., 2.};
0230 GridPoint p32{0., 2.};
0231 GridPoint p33{5., 2.};
0232 eqBoundEqClosedGrid.atPosition(p31) = 31u;
0233 eqBoundEqClosedGrid.atPosition(p32) = 32u;
0234 eqBoundEqClosedGrid.atPosition(p33) = 33u;
0235
0236 nlohmann::json eqBoundEqClosedJson =
0237 Acts::GridJsonConverter::toJson(eqBoundEqClosedGrid);
0238
0239 auto eqBoundEqClosedJsonRead =
0240 Acts::GridJsonConverter::fromJson<EqBoundEqClosed, std::size_t>(
0241 eqBoundEqClosedJson, eqBoundEqClosed);
0242
0243 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p11), 11u);
0244 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p12), 12u);
0245 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p13), 13u);
0246
0247 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p21), 21u);
0248 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p22), 22u);
0249 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p23), 23u);
0250
0251 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p31), 31u);
0252 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p32), 32u);
0253 BOOST_CHECK_EQUAL(eqBoundEqClosedJsonRead.atPosition(p33), 33u);
0254 }
0255
0256 namespace {
0257 template <typename ReferenceType, typename CheckTypeUniquePtr>
0258 bool checkType(const ReferenceType& ,
0259 const CheckTypeUniquePtr& g2l) {
0260 return (dynamic_cast<const ReferenceType*>(g2l.get()) != nullptr);
0261 }
0262
0263 template <typename SubspactTuple>
0264 void checkGlobalSubspaceTuple(const SubspactTuple& sstuple) {
0265
0266 std::vector<nlohmann::json> jsspace;
0267 std::apply(
0268 [&](auto&&... vals) {
0269 (jsspace.push_back(Acts::GridAccessJsonConverter::toJson(vals)), ...);
0270 },
0271 sstuple);
0272
0273
0274 for (auto& jss : jsspace) {
0275 BOOST_CHECK(!jss.empty());
0276 }
0277
0278
0279 std::vector<std::unique_ptr<const Acts::GridAccess::IGlobalToGridLocal>>
0280 sspaceRead;
0281 for (auto& jss : jsspace) {
0282 sspaceRead.push_back(
0283 Acts::GridAccessJsonConverter::globalToGridLocalFromJson(jss));
0284 if (jss["accessors"].size() == 1u) {
0285 auto delegate =
0286 Acts::GridAccessJsonConverter::globalToGridLocal1DimDelegateFromJson(
0287 jss);
0288 BOOST_CHECK(delegate.connected());
0289 } else if (jss["accessors"].size() == 2u) {
0290 auto delegate =
0291 Acts::GridAccessJsonConverter::globalToGridLocal2DimDelegateFromJson(
0292 jss);
0293 BOOST_CHECK(delegate.connected());
0294 } else {
0295 BOOST_CHECK(false);
0296 }
0297 }
0298
0299
0300 for (auto& ssp : sspaceRead) {
0301 BOOST_CHECK(ssp != nullptr);
0302 }
0303
0304
0305 std::size_t irn = 0;
0306 bool good = true;
0307 std::apply(
0308 [&](auto&&... vals) {
0309 ((good = good && checkType(vals, sspaceRead[irn++])), ...);
0310 },
0311 sstuple);
0312 BOOST_CHECK(good);
0313
0314 Acts::Transform3 tTransform;
0315 tTransform.pretranslate(Acts::Vector3{0., 0., 100.});
0316
0317
0318 std::vector<nlohmann::json> jsspaceTransform;
0319 std::apply(
0320 [&](auto... vals) {
0321 (jsspaceTransform.push_back(Acts::GridAccessJsonConverter::toJson(
0322 Acts::GridAccess::Affine3Transformed<decltype(vals)>(vals,
0323 tTransform))),
0324 ...);
0325 },
0326 sstuple);
0327
0328
0329 for (auto& jss : jsspaceTransform) {
0330 BOOST_CHECK(!jss.empty());
0331 BOOST_CHECK(jss.find("transform") != jss.end());
0332 }
0333
0334
0335 std::vector<std::unique_ptr<const Acts::GridAccess::IGlobalToGridLocal>>
0336 sspaceTransformRead;
0337 for (auto& jss : jsspaceTransform) {
0338 sspaceTransformRead.push_back(
0339 Acts::GridAccessJsonConverter::globalToGridLocalFromJson(jss));
0340 }
0341
0342
0343 for (auto& ssp : sspaceTransformRead) {
0344 BOOST_CHECK(ssp != nullptr);
0345 }
0346
0347
0348 irn = 0;
0349 good = true;
0350 std::apply(
0351 [&](auto... vals) {
0352 ((good = good &&
0353 checkType(Acts::GridAccess::Affine3Transformed<decltype(vals)>(
0354 vals, tTransform),
0355 sspaceTransformRead[irn++])),
0356 ...);
0357 },
0358 sstuple);
0359 BOOST_CHECK(good);
0360 }
0361
0362 }
0363
0364 BOOST_AUTO_TEST_CASE(GlobalSubSpaceTests1D) {
0365
0366 const std::tuple<
0367 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisX>,
0368 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisY>,
0369 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisZ>,
0370 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisR>,
0371 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisPhi>,
0372 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisEta>>
0373 sspace1D;
0374
0375
0376 checkGlobalSubspaceTuple(sspace1D);
0377 }
0378
0379 BOOST_AUTO_TEST_CASE(GlobalSubSpaceTests2D) {
0380
0381 const std::tuple<
0382 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisX,
0383 Acts::AxisDirection::AxisY>,
0384 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisY,
0385 Acts::AxisDirection::AxisX>,
0386 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisX,
0387 Acts::AxisDirection::AxisZ>,
0388 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisZ,
0389 Acts::AxisDirection::AxisX>,
0390 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisY,
0391 Acts::AxisDirection::AxisZ>,
0392 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisZ,
0393 Acts::AxisDirection::AxisY>,
0394 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisR,
0395 Acts::AxisDirection::AxisPhi>,
0396 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisPhi,
0397 Acts::AxisDirection::AxisR>,
0398 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisZ,
0399 Acts::AxisDirection::AxisPhi>,
0400 Acts::GridAccess::GlobalSubspace<Acts::AxisDirection::AxisPhi,
0401 Acts::AxisDirection::AxisZ>>
0402 sspace2D = {};
0403
0404
0405 checkGlobalSubspaceTuple(sspace2D);
0406 }
0407
0408 BOOST_AUTO_TEST_CASE(LocalSubspaceTests) {
0409 const std::tuple<Acts::GridAccess::LocalSubspace<0u>,
0410 Acts::GridAccess::LocalSubspace<1u>,
0411 Acts::GridAccess::LocalSubspace<0u, 1u>,
0412 Acts::GridAccess::LocalSubspace<1u, 0u>>
0413 lspace1D;
0414
0415
0416 std::vector<nlohmann::json> jlspace;
0417 std::apply(
0418 [&](auto&&... vals) {
0419 (jlspace.push_back(Acts::GridAccessJsonConverter::toJson(vals)), ...);
0420 },
0421 lspace1D);
0422
0423
0424 for (auto& jls : jlspace) {
0425 BOOST_CHECK(!jls.empty());
0426 }
0427
0428 std::vector<std::unique_ptr<const Acts::GridAccess::IBoundToGridLocal>>
0429 lspaceRead;
0430 for (auto& jls : jlspace) {
0431 lspaceRead.push_back(
0432 Acts::GridAccessJsonConverter::boundToGridLocalFromJson(jls));
0433 if (jls["accessors"].size() == 1u) {
0434 auto delegate =
0435 Acts::GridAccessJsonConverter::boundToGridLocal1DimDelegateFromJson(
0436 jls);
0437 BOOST_CHECK(delegate.connected());
0438 } else if (jls["accessors"].size() == 2u) {
0439 auto delegate =
0440 Acts::GridAccessJsonConverter::boundToGridLocal2DimDelegateFromJson(
0441 jls);
0442 BOOST_CHECK(delegate.connected());
0443 } else {
0444 BOOST_CHECK(false);
0445 }
0446 }
0447
0448
0449 for (auto& lsp : lspaceRead) {
0450 BOOST_CHECK(lsp != nullptr);
0451 }
0452
0453
0454 std::size_t irn = 0;
0455 bool good = true;
0456 std::apply(
0457 [&](auto&&... vals) {
0458 ((good = good && checkType(vals, lspaceRead[irn++])), ...);
0459 },
0460 lspace1D);
0461 BOOST_CHECK(good);
0462 }
0463
0464 BOOST_AUTO_TEST_CASE(BoundCylinderToZPhiTest) {
0465 Acts::GridAccess::BoundCylinderToZPhi boundCylinderToZPhi(100., 10.);
0466
0467 nlohmann::json jboundCylinderToZPhi =
0468 Acts::GridAccessJsonConverter::toJson(boundCylinderToZPhi);
0469
0470
0471 BOOST_CHECK(!jboundCylinderToZPhi.empty());
0472
0473 auto boundCylinderToZPhiRead =
0474 Acts::GridAccessJsonConverter::boundToGridLocalFromJson(
0475 jboundCylinderToZPhi);
0476
0477
0478 BOOST_REQUIRE(boundCylinderToZPhiRead != nullptr);
0479
0480 const Acts::GridAccess::BoundCylinderToZPhi* bct =
0481 dynamic_cast<const Acts::GridAccess::BoundCylinderToZPhi*>(
0482 boundCylinderToZPhiRead.get());
0483
0484 auto delegate =
0485 Acts::GridAccessJsonConverter::boundToGridLocal2DimDelegateFromJson(
0486 jboundCylinderToZPhi);
0487 BOOST_CHECK(delegate.connected());
0488
0489 BOOST_REQUIRE(bct != nullptr);
0490 CHECK_CLOSE_ABS(bct->radius, 100., 1e-5);
0491 CHECK_CLOSE_ABS(bct->shift, 10., 1e-5);
0492 }
0493
0494 BOOST_AUTO_TEST_SUITE_END()