Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-08 08:31:31

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/Utilities/GridAxisGenerators.hpp"
0012 #include "Acts/Utilities/IAxis.hpp"
0013 #include "ActsPlugins/Json/GridJsonConverter.hpp"
0014 
0015 #include <array>
0016 #include <fstream>
0017 #include <memory>
0018 #include <numbers>
0019 #include <vector>
0020 
0021 #include <nlohmann/json.hpp>
0022 
0023 using namespace Acts;
0024 
0025 namespace ActsTests {
0026 
0027 BOOST_AUTO_TEST_SUITE(JsonSuite)
0028 
0029 BOOST_AUTO_TEST_CASE(Grid1DSingleEntry) {
0030   // Bound equidistant
0031   using EqBound = GridAxisGenerators::EqBound;
0032 
0033   EqBound eqBound{{0., 5.}, 5};
0034   // Create the grid with the provided axis generator
0035   using GridTypeEQB = typename EqBound::template grid_type<std::size_t>;
0036   GridTypeEQB eqBoundGrid(eqBound());
0037 
0038   eqBoundGrid.at(1u) = 1u;
0039   eqBoundGrid.at(2u) = 2u;
0040   eqBoundGrid.at(3u) = 3u;
0041   eqBoundGrid.at(4u) = 4u;
0042   eqBoundGrid.at(5u) = 5u;
0043 
0044   auto p1 = typename GridTypeEQB::point_t{0.5};
0045   BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p1), 1u);
0046   auto p2 = typename GridTypeEQB::point_t{1.5};
0047   BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p2), 2u);
0048   auto p3 = typename GridTypeEQB::point_t{2.5};
0049   BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p3), 3u);
0050   auto p4 = typename GridTypeEQB::point_t{3.5};
0051   BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p4), 4u);
0052   auto p5 = typename GridTypeEQB::point_t{4.5};
0053   BOOST_CHECK_EQUAL(eqBoundGrid.atPosition(p5), 5u);
0054 
0055   nlohmann::json eqBoundJson = GridJsonConverter::toJson(eqBoundGrid);
0056 
0057   auto eqBoundGridRead =
0058       GridJsonConverter::fromJson<EqBound, std::size_t>(eqBoundJson, eqBound);
0059 
0060   BOOST_CHECK_EQUAL(eqBoundGridRead.at(1u), 1u);
0061   BOOST_CHECK_EQUAL(eqBoundGridRead.at(2u), 2u);
0062   BOOST_CHECK_EQUAL(eqBoundGridRead.at(3u), 3u);
0063   BOOST_CHECK_EQUAL(eqBoundGridRead.at(4u), 4u);
0064   BOOST_CHECK_EQUAL(eqBoundGridRead.at(5u), 5u);
0065 
0066   // Bound variable
0067   using VarBound = GridAxisGenerators::VarBound;
0068 
0069   VarBound varBound{{10., 11., 22., 333., 4444., 55555.}};
0070   // Create the grid with the provided axis generator
0071   using GridTypeEQV = typename VarBound::template grid_type<std::size_t>;
0072   GridTypeEQV varBoundGrid(varBound());
0073 
0074   varBoundGrid.at(1u) = 1u;
0075   varBoundGrid.at(2u) = 2u;
0076   varBoundGrid.at(3u) = 3u;
0077   varBoundGrid.at(4u) = 4u;
0078   varBoundGrid.at(5u) = 5u;
0079 
0080   nlohmann::json varBoundJson = GridJsonConverter::toJson(varBoundGrid);
0081 
0082   auto varBoundGridRead = GridJsonConverter::fromJson<VarBound, std::size_t>(
0083       varBoundJson, varBound);
0084 
0085   BOOST_CHECK_EQUAL(varBoundGridRead.at(1u), 1u);
0086   BOOST_CHECK_EQUAL(varBoundGridRead.at(2u), 2u);
0087   BOOST_CHECK_EQUAL(varBoundGridRead.at(3u), 3u);
0088   BOOST_CHECK_EQUAL(varBoundGridRead.at(4u), 4u);
0089   BOOST_CHECK_EQUAL(varBoundGridRead.at(5u), 5u);
0090 
0091   // Closed equidistant
0092   using EqClosed = GridAxisGenerators::EqClosed;
0093 
0094   EqClosed eqClosed{{0., 5.}, 5};
0095   // Create the grid with the provided axis generator
0096   using GridTypeEQC = typename EqClosed::template grid_type<std::size_t>;
0097   GridTypeEQC eqClosedGrid(eqClosed());
0098 
0099   eqClosedGrid.at(1u) = 1u;
0100   eqClosedGrid.at(2u) = 2u;
0101   eqClosedGrid.at(3u) = 3u;
0102   eqClosedGrid.at(4u) = 4u;
0103   eqClosedGrid.at(5u) = 5u;
0104 
0105   nlohmann::json eqClosedJson = GridJsonConverter::toJson(eqClosedGrid);
0106 
0107   auto eqClosedGridRead = GridJsonConverter::fromJson<EqClosed, std::size_t>(
0108       eqClosedJson, 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   // Bound equidistant
0119   using EqBound = GridAxisGenerators::EqBound;
0120 
0121   EqBound eqBound{{0., 5.}, 5};
0122   // Create the grid with the provided axis generator
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 = GridJsonConverter::toJson(eqBoundGrid);
0134 
0135   auto eqBoundGridRead =
0136       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 = GridAxisGenerators::EqBoundEqBound;
0148 
0149   EqBoundEqBound eqBound2{{0., 5.}, 5, {0., 2.}, 2};
0150   // Create the grid with the provided axis generator
0151   using GridTypeEQB2 = typename EqBoundEqBound::template grid_type<std::size_t>;
0152   GridTypeEQB2 eqBound2Grid(eqBound2());
0153 
0154   // Let's write in local coordinates
0155   using GridPoint = typename GridTypeEQB2::point_t;
0156 
0157   // First row access
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   // Second row access
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 = GridJsonConverter::toJson(eqBound2Grid);
0182 
0183   auto eqBound2JsonRead =
0184       GridJsonConverter::fromJson<EqBoundEqBound, std::size_t>(eqBound2Json,
0185                                                                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 = GridAxisGenerators::EqBoundEqClosed;
0201 
0202   EqBoundEqClosed eqBoundEqClosed{
0203       {-6., 6.}, 3, {-std::numbers::pi, std::numbers::pi}, 3};
0204   // Create the grid with the provided axis generator
0205   using GridTypeEQBEQC =
0206       typename EqBoundEqClosed::template grid_type<std::size_t>;
0207   GridTypeEQBEQC eqBoundEqClosedGrid(eqBoundEqClosed());
0208 
0209   // Let's write in local coordinates
0210   using GridPoint = typename GridTypeEQBEQC::point_t;
0211 
0212   // First row access
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   // Middle row access
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   // Last row access
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       GridJsonConverter::toJson(eqBoundEqClosedGrid);
0238 
0239   auto eqBoundEqClosedJsonRead =
0240       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 BOOST_AUTO_TEST_CASE(GridAnyToJson1D) {
0257   using EqBound = GridAxisGenerators::EqBound;
0258 
0259   EqBound eqBound{{0., 5.}, 5};
0260   using GridTypeEQB = typename EqBound::template grid_type<std::size_t>;
0261   GridTypeEQB eqBoundGrid(eqBound());
0262 
0263   eqBoundGrid.at(1u) = 1u;
0264   eqBoundGrid.at(2u) = 2u;
0265   eqBoundGrid.at(3u) = 3u;
0266   eqBoundGrid.at(4u) = 4u;
0267   eqBoundGrid.at(5u) = 5u;
0268 
0269   nlohmann::json jExpected = GridJsonConverter::toJson(eqBoundGrid);
0270   nlohmann::json jAny = GridJsonConverter::toJsonAny<std::size_t>(
0271       eqBoundGrid, AnyGridConstView<std::size_t>(eqBoundGrid));
0272 
0273   BOOST_CHECK_EQUAL(jAny, jExpected);
0274 }
0275 
0276 BOOST_AUTO_TEST_CASE(GridAnyToJson2D) {
0277   using EqBoundEqClosed = GridAxisGenerators::EqBoundEqClosed;
0278 
0279   EqBoundEqClosed eqBoundEqClosed{
0280       {-6., 6.}, 3, {-std::numbers::pi, std::numbers::pi}, 3};
0281   using GridTypeEQBEQC =
0282       typename EqBoundEqClosed::template grid_type<std::size_t>;
0283   GridTypeEQBEQC eqBoundEqClosedGrid(eqBoundEqClosed());
0284 
0285   using GridPoint = typename GridTypeEQBEQC::point_t;
0286   eqBoundEqClosedGrid.atPosition(GridPoint{-5., -2.}) = 11u;
0287   eqBoundEqClosedGrid.atPosition(GridPoint{0., -2.}) = 12u;
0288   eqBoundEqClosedGrid.atPosition(GridPoint{5., -2.}) = 13u;
0289   eqBoundEqClosedGrid.atPosition(GridPoint{-5., 0.}) = 21u;
0290   eqBoundEqClosedGrid.atPosition(GridPoint{0., 0.}) = 22u;
0291   eqBoundEqClosedGrid.atPosition(GridPoint{5., 0.}) = 23u;
0292   eqBoundEqClosedGrid.atPosition(GridPoint{-5., 2.}) = 31u;
0293   eqBoundEqClosedGrid.atPosition(GridPoint{0., 2.}) = 32u;
0294   eqBoundEqClosedGrid.atPosition(GridPoint{5., 2.}) = 33u;
0295 
0296   nlohmann::json jExpected = GridJsonConverter::toJson(eqBoundEqClosedGrid);
0297   nlohmann::json jAny = GridJsonConverter::toJsonAny<std::size_t>(
0298       eqBoundEqClosedGrid, AnyGridConstView<std::size_t>(eqBoundEqClosedGrid));
0299 
0300   BOOST_CHECK_EQUAL(jAny, jExpected);
0301 }
0302 
0303 BOOST_AUTO_TEST_CASE(AxisJsonConverterEquidistantBound) {
0304   auto axis = IAxis::createEquidistant(AxisBoundaryType::Bound, -5., 5., 10);
0305   nlohmann::json j = AxisJsonConverter::toJson(*axis);
0306   auto read = AxisJsonConverter::fromJson(j);
0307   BOOST_REQUIRE(read != nullptr);
0308   BOOST_CHECK_EQUAL(read->getBoundaryType(), AxisBoundaryType::Bound);
0309   BOOST_CHECK_EQUAL(read->getNBins(), 10u);
0310   BOOST_CHECK_EQUAL(read->getMin(), -5.);
0311   BOOST_CHECK_EQUAL(read->getMax(), 5.);
0312   BOOST_CHECK(read->isEquidistant());
0313 }
0314 
0315 BOOST_AUTO_TEST_CASE(AxisJsonConverterEquidistantClosed) {
0316   auto axis = IAxis::createEquidistant(AxisBoundaryType::Closed,
0317                                        -std::numbers::pi, std::numbers::pi, 36);
0318   nlohmann::json j = AxisJsonConverter::toJson(*axis);
0319   auto read = AxisJsonConverter::fromJson(j);
0320   BOOST_REQUIRE(read != nullptr);
0321   BOOST_CHECK_EQUAL(read->getBoundaryType(), AxisBoundaryType::Closed);
0322   BOOST_CHECK_EQUAL(read->getNBins(), 36u);
0323   BOOST_CHECK(read->isEquidistant());
0324 }
0325 
0326 BOOST_AUTO_TEST_CASE(AxisJsonConverterVariableBound) {
0327   // Exercises the "boundaries" key — the fix changed from reading "edges"
0328   std::vector<double> edges = {0., 10., 25., 60., 130.};
0329   auto axis = IAxis::createVariable(AxisBoundaryType::Bound, edges);
0330   nlohmann::json j = AxisJsonConverter::toJson(*axis);
0331   BOOST_CHECK_EQUAL(j.at("type").get<AxisType>(), AxisType::Variable);
0332   BOOST_CHECK(j.contains("boundaries"));
0333   BOOST_CHECK(!j.contains("edges"));
0334   auto read = AxisJsonConverter::fromJson(j);
0335   BOOST_REQUIRE(read != nullptr);
0336   BOOST_CHECK_EQUAL(read->getBoundaryType(), AxisBoundaryType::Bound);
0337   BOOST_CHECK_EQUAL(read->getNBins(), edges.size() - 1);
0338   BOOST_CHECK(!read->isEquidistant());
0339   BOOST_CHECK(read->getBinEdges() == edges);
0340 }
0341 
0342 BOOST_AUTO_TEST_CASE(AxisJsonConverterDirection) {
0343   // The axis direction is round-tripped when present ...
0344   auto axis = IAxis::createEquidistant(AxisBoundaryType::Bound, -5., 5., 10,
0345                                        AxisDirection::AxisZ);
0346   nlohmann::json j = AxisJsonConverter::toJson(*axis);
0347   BOOST_CHECK(j.contains("direction"));
0348   auto read = AxisJsonConverter::fromJson(j);
0349   BOOST_REQUIRE(read != nullptr);
0350   BOOST_CHECK(read->getDirection() == AxisDirection::AxisZ);
0351 
0352   // ... and stays absent when not set
0353   auto axisNoDir =
0354       IAxis::createEquidistant(AxisBoundaryType::Bound, -5., 5., 10);
0355   nlohmann::json jNoDir = AxisJsonConverter::toJson(*axisNoDir);
0356   BOOST_CHECK(!jNoDir.contains("direction"));
0357   auto readNoDir = AxisJsonConverter::fromJson(jNoDir);
0358   BOOST_REQUIRE(readNoDir != nullptr);
0359   BOOST_CHECK(!readNoDir->getDirection().has_value());
0360 }
0361 
0362 BOOST_AUTO_TEST_SUITE_END()
0363 
0364 }  // namespace ActsTests