Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:44

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Material/GridSurfaceMaterial.hpp"
0012 #include "Acts/Material/Material.hpp"
0013 #include "Acts/Material/MaterialSlab.hpp"
0014 #include "Acts/Plugins/Json/MaterialJsonConverter.hpp"
0015 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0016 #include "Acts/Utilities/GridAccessHelpers.hpp"
0017 #include "Acts/Utilities/GridAxisGenerators.hpp"
0018 
0019 #include <array>
0020 #include <fstream>
0021 #include <memory>
0022 #include <vector>
0023 
0024 #include <nlohmann/json.hpp>
0025 
0026 BOOST_AUTO_TEST_SUITE(MaterialJsonIO)
0027 
0028 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial1DTests) {
0029   std::vector<Acts::MaterialSlab> material;
0030   material.emplace_back(Acts::Material(), 0.0);  // vacuum
0031   material.emplace_back(
0032       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0033   material.emplace_back(
0034       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 2.0);
0035   material.emplace_back(
0036       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 3.0);
0037 
0038   using EqBound = Acts::GridAxisGenerators::EqBound;
0039   using EqGrid = EqBound::grid_type<std::size_t>;
0040   using Point = EqGrid::point_t;
0041 
0042   EqBound eqBound{{0., 5.}, 5};
0043   EqGrid eqGrid{eqBound()};
0044 
0045   eqGrid.atPosition(Point{0.5}) = 1u;  // material 1
0046   eqGrid.atPosition(Point{1.5}) = 0u;  // vacuum
0047   eqGrid.atPosition(Point{2.5}) = 2u;  // material 2
0048   eqGrid.atPosition(Point{3.5}) = 2u;  // material 2
0049   eqGrid.atPosition(Point{4.5}) = 3u;  // material 3
0050 
0051   auto localX = std::make_unique<const Acts::GridAccess::LocalSubspace<0u>>();
0052   Acts::IndexedSurfaceMaterial<EqGrid>::BoundToGridLocalDelegate bToX;
0053   bToX.connect<&Acts::GridAccess::LocalSubspace<0u>::toGridLocal>(
0054       std::move(localX));
0055 
0056   auto globalX =
0057       std::make_unique<const Acts::GridAccess::GlobalSubspace<Acts::binX>>();
0058   Acts::IndexedSurfaceMaterial<EqGrid>::GlobalToGridLocalDelegate gToX;
0059   gToX.connect<&Acts::GridAccess::GlobalSubspace<Acts::binX>::toGridLocal>(
0060       std::move(globalX));
0061 
0062   Acts::IndexedSurfaceMaterial<EqGrid> ism(
0063       std::move(eqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0064       std::move(bToX), std::move(gToX));
0065 
0066   nlohmann::json jMaterial = &ism;
0067 
0068   // Run a few tests
0069   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0070 
0071   // Read it back in
0072   const Acts::ISurfaceMaterial* ismRead = nullptr;
0073   Acts::from_json(jMaterial, ismRead);
0074   BOOST_REQUIRE(ismRead != nullptr);
0075 
0076   // Check if it's the right type
0077   const Acts::IndexedSurfaceMaterial<EqGrid>* ismReadTyped =
0078       dynamic_cast<const Acts::IndexedSurfaceMaterial<EqGrid>*>(ismRead);
0079   BOOST_REQUIRE(ismReadTyped != nullptr);
0080 
0081   auto gridRead = ismReadTyped->grid();
0082   BOOST_CHECK(gridRead.atPosition(Point{0.5}) == 1u);  // material 1
0083   BOOST_CHECK(gridRead.atPosition(Point{1.5}) == 0u);  // vacuum
0084   BOOST_CHECK(gridRead.atPosition(Point{2.5}) == 2u);  // material 2
0085   BOOST_CHECK(gridRead.atPosition(Point{3.5}) == 2u);  // material 2
0086   BOOST_CHECK(gridRead.atPosition(Point{4.5}) == 3u);  // material 3
0087 
0088   // Check the accessor is there and the material is filled
0089   auto accessorRead = ismReadTyped->materialAccessor();
0090   CHECK_CLOSE_ABS(accessorRead.material[0].thickness(), 0.0, 1e-5);
0091   CHECK_CLOSE_ABS(accessorRead.material[1].thickness(), 1.0, 1e-5);
0092   CHECK_CLOSE_ABS(accessorRead.material[2].thickness(), 2.0, 1e-5);
0093   CHECK_CLOSE_ABS(accessorRead.material[3].thickness(), 3.0, 1e-5);
0094 }
0095 
0096 BOOST_AUTO_TEST_CASE(IndexedSurfaceMaterial2DTests) {
0097   std::vector<Acts::MaterialSlab> material;
0098   material.emplace_back(Acts::Material(), 1.0);  // vacuum
0099   material.emplace_back(
0100       Acts::Material::fromMolarDensity(1.0, 2.0, 3.0, 4.0, 5.0), 1.0);
0101   material.emplace_back(
0102       Acts::Material::fromMolarDensity(11.0, 12.0, 13.0, 14.0, 15.0), 1.0);
0103   material.emplace_back(
0104       Acts::Material::fromMolarDensity(21.0, 22.0, 23.0, 24.0, 25.0), 1.0);
0105 
0106   using EqBoundEqClosed = Acts::GridAxisGenerators::EqBoundEqClosed;
0107   using EqEqGrid = EqBoundEqClosed::grid_type<std::size_t>;
0108   using Point = EqEqGrid::point_t;
0109 
0110   EqBoundEqClosed eqeqBound{{-1., 1.}, 2, {-M_PI, M_PI}, 4};
0111   EqEqGrid eqeqGrid{eqeqBound()};
0112 
0113   eqeqGrid.atPosition(Point{-0.5, -M_PI * 0.75}) = 1u;  // material 1
0114   eqeqGrid.atPosition(Point{-0.5, -M_PI * 0.25}) = 1u;  // material 1
0115   eqeqGrid.atPosition(Point{-0.5, M_PI * 0.25}) = 0u;   // vacuum
0116   eqeqGrid.atPosition(Point{-0.5, M_PI * 0.75}) = 2u;   // material 2
0117 
0118   eqeqGrid.atPosition(Point{0.5, -M_PI * 0.75}) = 0u;  // vacuum
0119   eqeqGrid.atPosition(Point{0.5, -M_PI * 0.25}) = 3u;  // material 3
0120   eqeqGrid.atPosition(Point{0.5, M_PI * 0.25}) = 3u;   // material 3
0121   eqeqGrid.atPosition(Point{0.5, M_PI * 0.75}) = 0u;   // vacuum
0122 
0123   auto boundToGrid =
0124       std::make_unique<const Acts::GridAccess::LocalSubspace<0u, 1u>>();
0125   Acts::IndexedSurfaceMaterial<EqEqGrid>::BoundToGridLocalDelegate bToZPhi;
0126   bToZPhi.connect<&Acts::GridAccess::LocalSubspace<0u, 1u>::toGridLocal>(
0127       std::move(boundToGrid));
0128 
0129   // With z shift 10
0130   auto globalToGrid = std::make_unique<
0131       const Acts::GridAccess::GlobalSubspace<Acts::binZ, Acts::binPhi>>();
0132   Acts::IndexedSurfaceMaterial<EqEqGrid>::GlobalToGridLocalDelegate gToZphi;
0133   gToZphi.connect<
0134       &Acts::GridAccess::GlobalSubspace<Acts::binZ, Acts::binPhi>::toGridLocal>(
0135       std::move(globalToGrid));
0136 
0137   // Create the indexed material grid
0138   Acts::IndexedSurfaceMaterial<EqEqGrid> ism(
0139       std::move(eqeqGrid), Acts::IndexedMaterialAccessor{std::move(material)},
0140       std::move(bToZPhi), std::move(gToZphi));
0141 
0142   nlohmann::json jMaterial = &ism;
0143 
0144   // Run a few tests
0145   BOOST_REQUIRE(jMaterial.find("material") != jMaterial.end());
0146 
0147   // Read it back in
0148   const Acts::ISurfaceMaterial* ismRead = nullptr;
0149   Acts::from_json(jMaterial, ismRead);
0150   BOOST_REQUIRE(ismRead != nullptr);
0151 }
0152 
0153 BOOST_AUTO_TEST_SUITE_END()