Analysis Software
Documentation for sPHENIX simulation 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 
17 
18 #include <array>
19 #include <cstddef>
20 #include <vector>
21 
22 namespace bdata = boost::unit_test::data;
23 
25 
26 namespace Acts {
27 namespace Test {
28 
29 BOOST_AUTO_TEST_CASE(materialmap_creation) {
30  // Create grid values
31  std::vector<double> rPos = {0., 1., 2.};
32  std::vector<double> xPos = {0., 1., 2.};
33  std::vector<double> yPos = {0., 1., 2.};
34  std::vector<double> zPos = {0., 1., 2.};
35 
36  // Create material association in rz
37  std::vector<Material> material_rz;
38  for (int i = 0; i < 9; i++) {
39  material_rz.push_back(Material::fromMolarDensity(i, i, i, i, i));
40  }
41 
42  auto localToGlobalBin_rz = [](std::array<size_t, 2> binsRZ,
43  std::array<size_t, 2> nBinsRZ) {
44  return (binsRZ.at(1) * nBinsRZ.at(0) + binsRZ.at(0));
45  };
46  // Create material mapper in rz
47  auto mapper_rz =
48  materialMapperRZ(localToGlobalBin_rz, rPos, zPos, material_rz);
49  // check number of bins, minima & maxima
50  std::vector<size_t> nBins_rz = {rPos.size(), zPos.size()};
51  std::vector<double> minima_rz = {0., 0.};
52  std::vector<double> maxima_rz = {3., 3.};
53  BOOST_CHECK(mapper_rz.getNBins() == nBins_rz);
54  // Check minimum (should be first value because bin values are always
55  // assigned to the left boundary)
56  BOOST_CHECK(mapper_rz.getMin() == minima_rz);
57  // Check maximum (should be last value + 1 step because bin values are
58  // always assigned to the left boundary)
59  BOOST_CHECK(mapper_rz.getMax() == maxima_rz);
60 
61  // Create map in xyz
62  std::vector<Material> material_xyz;
63  for (int i = 0; i < 27; i++) {
64  material_xyz.push_back(Material::fromMolarDensity(i, i, i, i, i));
65  }
66 
67  auto localToGlobalBin_xyz = [](std::array<size_t, 3> binsXYZ,
68  std::array<size_t, 3> nBinsXYZ) {
69  return (binsXYZ.at(0) * (nBinsXYZ.at(1) * nBinsXYZ.at(2)) +
70  binsXYZ.at(1) * nBinsXYZ.at(2) + binsXYZ.at(2));
71  };
72 
73  // Create material mapper in xyz
74  auto mapper_xyz =
75  materialMapperXYZ(localToGlobalBin_xyz, xPos, yPos, zPos, material_xyz);
76  // Check number of bins, minima & maxima
77  std::vector<size_t> nBins_xyz = {xPos.size(), yPos.size(), zPos.size()};
78  std::vector<double> minima_xyz = {0., 0., 0.};
79  std::vector<double> maxima_xyz = {3., 3., 3.};
80  BOOST_CHECK(mapper_xyz.getNBins() == nBins_xyz);
81  // Check minimum (should be first value because bin values are always
82  // assigned to the left boundary)
83  BOOST_CHECK(mapper_xyz.getMin() == minima_xyz);
84  // Check maximum (should be last value + 1 step because bin values are
85  // always assigned to the left boundary)
86  BOOST_CHECK(mapper_xyz.getMax() == maxima_xyz);
87 
88  // Check if filled value is expected value in rz
89  Vector3 pos0_rz(0., 0., 0.);
90  Vector3 pos1_rz(1., 0., 1.);
91  Vector3 pos2_rz(0., 2., 2.);
92  auto value0_rz = mapper_rz.getMaterial(pos0_rz);
93  auto value1_rz = mapper_rz.getMaterial(pos1_rz);
94  auto value2_rz = mapper_rz.getMaterial(pos2_rz);
95  // Calculate what the value should be at this point
96  Material mat0_rz = material_rz.at(
97  localToGlobalBin_rz({{0, 0}}, {{rPos.size(), zPos.size()}}));
98  Material mat1_rz = material_rz.at(
99  localToGlobalBin_rz({{1, 1}}, {{rPos.size(), zPos.size()}}));
100  Material mat2_rz = material_rz.at(
101  localToGlobalBin_rz({{2, 2}}, {{rPos.size(), zPos.size()}}));
102 
103  // Check the value
104  // in rz case material is phi symmetric (check radius)
105  CHECK_CLOSE_ABS(value0_rz.parameters(), mat0_rz.parameters(), 1e-9);
106  CHECK_CLOSE_ABS(value1_rz.parameters(), mat1_rz.parameters(), 1e-9);
107  CHECK_CLOSE_ABS(value2_rz.parameters(), mat2_rz.parameters(), 1e-9);
108 
109  // Check if filled value is expected value in xyz
110  Vector3 pos0_xyz(0., 0., 0.);
111  Vector3 pos1_xyz(1., 1., 1.);
112  Vector3 pos2_xyz(2., 2., 2.);
113  auto value0_xyz = mapper_xyz.getMaterial(pos0_xyz);
114  auto value1_xyz = mapper_xyz.getMaterial(pos1_xyz);
115  auto value2_xyz = mapper_xyz.getMaterial(pos2_xyz);
116  // Calculate what the value should be at this point
117  Material mat0_xyz = material_xyz.at(localToGlobalBin_xyz(
118  {{0, 0, 0}}, {{xPos.size(), yPos.size(), zPos.size()}}));
119  Material mat1_xyz = material_xyz.at(localToGlobalBin_xyz(
120  {{1, 1, 1}}, {{xPos.size(), yPos.size(), zPos.size()}}));
121  Material mat2_xyz = material_xyz.at(localToGlobalBin_xyz(
122  {{2, 2, 2}}, {{xPos.size(), yPos.size(), zPos.size()}}));
123 
124  // Check the value
125  // in xyz case material is phi symmetric (check radius)
126  CHECK_CLOSE_ABS(value0_xyz.parameters(), mat0_xyz.parameters(), 1e-9);
127  CHECK_CLOSE_ABS(value1_xyz.parameters(), mat1_xyz.parameters(), 1e-9);
128  CHECK_CLOSE_ABS(value2_xyz.parameters(), mat2_xyz.parameters(), 1e-9);
129 }
130 } // namespace Test
131 } // namespace Acts