EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MaterialMapUtilsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MaterialMapUtilsTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
25 
26 #include <limits>
27 #include <random>
28 #include <vector>
29 
30 namespace bdata = boost::unit_test::data;
31 
33 
34 namespace Acts {
35 namespace Test {
36 
37 BOOST_AUTO_TEST_CASE(materialmap_creation) {
38  // Create grid values
39  std::vector<double> rPos = {0., 1., 2.};
40  std::vector<double> xPos = {0., 1., 2.};
41  std::vector<double> yPos = {0., 1., 2.};
42  std::vector<double> zPos = {0., 1., 2.};
43 
44  // Create material association in rz
45  std::vector<Material> material_rz;
46  for (int i = 0; i < 9; i++) {
47  material_rz.push_back(Material::fromMolarDensity(i, i, i, i, i));
48  }
49 
50  auto localToGlobalBin_rz = [](std::array<size_t, 2> binsRZ,
51  std::array<size_t, 2> nBinsRZ) {
52  return (binsRZ.at(1) * nBinsRZ.at(0) + binsRZ.at(0));
53  };
54  // Create material mapper in rz
55  auto mapper_rz =
56  materialMapperRZ(localToGlobalBin_rz, rPos, zPos, material_rz);
57  // check number of bins, minima & maxima
58  std::vector<size_t> nBins_rz = {rPos.size(), zPos.size()};
59  std::vector<double> minima_rz = {0., 0.};
60  std::vector<double> maxima_rz = {3., 3.};
61  BOOST_CHECK(mapper_rz.getNBins() == nBins_rz);
62  // Check minimum (should be first value because bin values are always
63  // assigned to the left boundary)
64  BOOST_CHECK(mapper_rz.getMin() == minima_rz);
65  // Check maximum (should be last value + 1 step because bin values are
66  // always assigned to the left boundary)
67  BOOST_CHECK(mapper_rz.getMax() == maxima_rz);
68 
69  // Create map in xyz
70  std::vector<Material> material_xyz;
71  for (int i = 0; i < 27; i++) {
72  material_xyz.push_back(Material::fromMolarDensity(i, i, i, i, i));
73  }
74 
75  auto localToGlobalBin_xyz = [](std::array<size_t, 3> binsXYZ,
76  std::array<size_t, 3> nBinsXYZ) {
77  return (binsXYZ.at(0) * (nBinsXYZ.at(1) * nBinsXYZ.at(2)) +
78  binsXYZ.at(1) * nBinsXYZ.at(2) + binsXYZ.at(2));
79  };
80 
81  // Create material mapper in xyz
82  auto mapper_xyz =
83  materialMapperXYZ(localToGlobalBin_xyz, xPos, yPos, zPos, material_xyz);
84  // Check number of bins, minima & maxima
85  std::vector<size_t> nBins_xyz = {xPos.size(), yPos.size(), zPos.size()};
86  std::vector<double> minima_xyz = {0., 0., 0.};
87  std::vector<double> maxima_xyz = {3., 3., 3.};
88  BOOST_CHECK(mapper_xyz.getNBins() == nBins_xyz);
89  // Check minimum (should be first value because bin values are always
90  // assigned to the left boundary)
91  BOOST_CHECK(mapper_xyz.getMin() == minima_xyz);
92  // Check maximum (should be last value + 1 step because bin values are
93  // always assigned to the left boundary)
94  BOOST_CHECK(mapper_xyz.getMax() == maxima_xyz);
95 
96  // Check if filled value is expected value in rz
97  Vector3D pos0_rz(0., 0., 0.);
98  Vector3D pos1_rz(1., 0., 1.);
99  Vector3D pos2_rz(0., 2., 2.);
100  auto value0_rz = mapper_rz.getMaterial(pos0_rz);
101  auto value1_rz = mapper_rz.getMaterial(pos1_rz);
102  auto value2_rz = mapper_rz.getMaterial(pos2_rz);
103  // Calculate what the value should be at this point
104  Material mat0_rz = material_rz.at(
105  localToGlobalBin_rz({{0, 0}}, {{rPos.size(), zPos.size()}}));
106  Material mat1_rz = material_rz.at(
107  localToGlobalBin_rz({{1, 1}}, {{rPos.size(), zPos.size()}}));
108  Material mat2_rz = material_rz.at(
109  localToGlobalBin_rz({{2, 2}}, {{rPos.size(), zPos.size()}}));
110 
111  // Check the value
112  // in rz case material is phi symmetric (check radius)
113  CHECK_CLOSE_ABS(value0_rz.parameters(), mat0_rz.parameters(), 1e-9);
114  CHECK_CLOSE_ABS(value1_rz.parameters(), mat1_rz.parameters(), 1e-9);
115  CHECK_CLOSE_ABS(value2_rz.parameters(), mat2_rz.parameters(), 1e-9);
116 
117  // Check if filled value is expected value in xyz
118  Vector3D pos0_xyz(0., 0., 0.);
119  Vector3D pos1_xyz(1., 1., 1.);
120  Vector3D pos2_xyz(2., 2., 2.);
121  auto value0_xyz = mapper_xyz.getMaterial(pos0_xyz);
122  auto value1_xyz = mapper_xyz.getMaterial(pos1_xyz);
123  auto value2_xyz = mapper_xyz.getMaterial(pos2_xyz);
124  // Calculate what the value should be at this point
125  Material mat0_xyz = material_xyz.at(localToGlobalBin_xyz(
126  {{0, 0, 0}}, {{xPos.size(), yPos.size(), zPos.size()}}));
127  Material mat1_xyz = material_xyz.at(localToGlobalBin_xyz(
128  {{1, 1, 1}}, {{xPos.size(), yPos.size(), zPos.size()}}));
129  Material mat2_xyz = material_xyz.at(localToGlobalBin_xyz(
130  {{2, 2, 2}}, {{xPos.size(), yPos.size(), zPos.size()}}));
131 
132  // Check the value
133  // in xyz case material is phi symmetric (check radius)
134  CHECK_CLOSE_ABS(value0_xyz.parameters(), mat0_xyz.parameters(), 1e-9);
135  CHECK_CLOSE_ABS(value1_xyz.parameters(), mat1_xyz.parameters(), 1e-9);
136  CHECK_CLOSE_ABS(value2_xyz.parameters(), mat2_xyz.parameters(), 1e-9);
137 }
138 } // namespace Test
139 } // namespace Acts