Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2019 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/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Material/InterpolatedMaterialMap.hpp"
0014 #include "Acts/Material/Material.hpp"
0015 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0016 #include "Acts/Utilities/Grid.hpp"
0017 #include "Acts/Utilities/detail/Axis.hpp"
0018 #include "Acts/Utilities/detail/AxisFwd.hpp"
0019 
0020 #include <array>
0021 #include <cstddef>
0022 #include <functional>
0023 #include <iosfwd>
0024 #include <optional>
0025 #include <tuple>
0026 #include <utility>
0027 #include <vector>
0028 
0029 namespace Acts::Test {
0030 
0031 constexpr unsigned int dim = 2;
0032 using grid_t = Grid<Acts::Material::ParametersVector, detail::EquidistantAxis,
0033                     detail::EquidistantAxis>;
0034 
0035 ActsVector<dim> trafoGlobalToLocal(const Vector3& global) {
0036   return {global.x(), global.y()};
0037 }
0038 
0039 BOOST_AUTO_TEST_CASE(InterpolatedMaterialMap_MaterialCell_test) {
0040   // Build a material cell
0041   std::array<double, dim> lowerLeft{{0., 0.}};
0042   std::array<double, dim> upperRight{{1., 1.}};
0043   Acts::Material::ParametersVector mat;
0044   mat << 1, 2, 3, 4, 5;
0045   std::array<Acts::Material::ParametersVector, 4> matArray = {mat, mat, mat,
0046                                                               mat};
0047 
0048   MaterialMapper<grid_t>::MaterialCell materialCell(
0049       trafoGlobalToLocal, lowerLeft, upperRight, matArray);
0050 
0051   // Test InterpolatedMaterialMap::MaterialCell<DIM>::isInside method
0052   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(0.5, 0.5, 0.5)), true);
0053   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(-1., 0., 0.)), false);
0054   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(0., -1., 0.)), false);
0055   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(0., 0., -1.)), true);
0056   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(2., 0., 0.)), false);
0057   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(0., 2., 0.)), false);
0058   BOOST_CHECK_EQUAL(materialCell.isInside(Vector3(0., 0., 2.)), true);
0059 
0060   // Test the getter
0061   CHECK_CLOSE_REL(materialCell.getMaterial({0.5, 0.5, 0.5}), Material(mat),
0062                   1e-4);
0063 }
0064 
0065 BOOST_AUTO_TEST_CASE(InterpolatedMaterialMap_MaterialMapper_test) {
0066   // Create the axes for the grid
0067   detail::EquidistantAxis axisX(0, 3, 3);
0068   detail::EquidistantAxis axisY(0, 3, 3);
0069 
0070   // The material mapping grid
0071   auto grid = grid_t(std::make_tuple(std::move(axisX), std::move(axisY)));
0072   Acts::Material::ParametersVector mat;
0073   mat << 1, 2, 3, 4, 5;
0074 
0075   for (std::size_t i = 0; i < grid.size(); i++) {
0076     grid.at(i) = mat;
0077   }
0078   MaterialMapper<grid_t> matMap(trafoGlobalToLocal, grid);
0079 
0080   // Test Material getter
0081   CHECK_CLOSE_REL(matMap.getMaterial({0.5, 0.5, 0.5}), Material(mat), 1e-4);
0082 
0083   // Test the MaterialCell getter
0084   MaterialMapper<grid_t>::MaterialCell matCell =
0085       matMap.getMaterialCell({0.5, 0.5, 0.5});
0086   CHECK_CLOSE_REL(matCell.getMaterial({0.5, 0.5, 0.5}), Material(mat), 1e-4);
0087 
0088   // Test the number of bins getter
0089   std::vector<std::size_t> nBins = matMap.getNBins();
0090   BOOST_CHECK_EQUAL(nBins[0], 3u);
0091   BOOST_CHECK_EQUAL(nBins[1], 3u);
0092 
0093   // Test the lower limits
0094   std::vector<double> limits = matMap.getMin();
0095   CHECK_CLOSE_ABS(limits[0], 0., 1e-4);
0096   CHECK_CLOSE_ABS(limits[1], 0., 1e-4);
0097 
0098   // Test the upper limits
0099   limits = matMap.getMax();
0100   CHECK_CLOSE_REL(limits[0], 3., 1e-4);
0101   CHECK_CLOSE_REL(limits[1], 3., 1e-4);
0102 
0103   // Test the inside check
0104   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(1., 1., 1.)), true);
0105   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(-1., 0., 0.)), false);
0106   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(0., -1., 0.)), false);
0107   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(0., 0., -1.)), true);
0108   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(4., 0., 0.)), false);
0109   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(0., 4., 0.)), false);
0110   BOOST_CHECK_EQUAL(matMap.isInside(Vector3(0., 0., 4.)), true);
0111 
0112   // Test the grid getter
0113   auto matMapGrid = matMap.getGrid();
0114   for (unsigned int i = 0; i < dim; i++) {
0115     BOOST_CHECK_EQUAL(grid.numLocalBins()[i], matMapGrid.numLocalBins()[i]);
0116     BOOST_CHECK_EQUAL(grid.minPosition()[i], matMapGrid.minPosition()[i]);
0117     BOOST_CHECK_EQUAL(grid.maxPosition()[i], matMapGrid.maxPosition()[i]);
0118   }
0119   for (std::size_t i = 0; i < grid.size(); i++) {
0120     CHECK_CLOSE_REL(grid.at(i), matMapGrid.at(i), 1e-4);
0121   }
0122 }
0123 
0124 BOOST_AUTO_TEST_CASE(InterpolatedMaterialMap_test) {
0125   // Create the axes for the grid
0126   detail::EquidistantAxis axisX(0, 3, 3);
0127   detail::EquidistantAxis axisY(0, 3, 3);
0128 
0129   // The material mapping grid
0130   auto grid = grid_t(std::make_tuple(std::move(axisX), std::move(axisY)));
0131   Acts::Material::ParametersVector mat;
0132   mat << 1, 2, 3, 4, 5;
0133 
0134   for (std::size_t i = 0; i < grid.size(); i++) {
0135     grid.at(i) = mat;
0136   }
0137   MaterialMapper<grid_t> matMap(trafoGlobalToLocal, grid);
0138   InterpolatedMaterialMap ipolMatMap(std::move(matMap));
0139 
0140   // Test the material getter
0141   CHECK_CLOSE_REL(ipolMatMap.material({0.5, 0.5, 0.5}), Material(mat), 1e-4);
0142 
0143   // Test the material getter with a cache
0144   // Build a material cell
0145   std::array<double, dim> lowerLeft{{0., 0.}};
0146   std::array<double, dim> upperRight{{1., 1.}};
0147   std::array<Acts::Material::ParametersVector, 4> matArray = {mat, mat, mat,
0148                                                               mat};
0149 
0150   MaterialMapper<grid_t>::MaterialCell materialCell(
0151       trafoGlobalToLocal, lowerLeft, upperRight, matArray);
0152   InterpolatedMaterialMap<MaterialMapper<grid_t>>::Cache cache;
0153   cache.matCell = materialCell;
0154   cache.initialized = true;
0155   CHECK_CLOSE_REL(ipolMatMap.getMaterial(Vector3(0.5, 0.5, 0.5), cache),
0156                   Material(mat), 1e-4);
0157 
0158   // Test the inside check
0159   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(1., 1., 1.)), true);
0160   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(-1., 0., 0.)), false);
0161   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(0., -1., 0.)), false);
0162   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(0., 0., -1.)), true);
0163   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(4., 0., 0.)), false);
0164   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(0., 4., 0.)), false);
0165   BOOST_CHECK_EQUAL(ipolMatMap.isInside(Vector3(0., 0., 4.)), true);
0166 }
0167 }  // namespace Acts::Test