EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MaterialGridHelperTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MaterialGridHelperTests.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/unit_test.hpp>
10 
14 
15 #include <limits>
16 #include <random>
17 #include <vector>
18 
19 namespace Acts {
20 
21 namespace Test {
22 
23 using RecordedMaterial = std::vector<std::pair<Acts::Material, Acts::Vector3D>>;
25 using Grid2D =
27 using Grid3D =
30 using MaterialGrid3D =
32 
35 BOOST_AUTO_TEST_CASE(Square_Grid_test) {
36  BinUtility bu(7, -3., 3., open, binX);
37  bu += BinUtility(3, -2., 2., open, binY);
38  auto bd = bu.binningData();
39  std::function<Acts::Vector2D(Acts::Vector3D)> transfoGlobalToLocal;
40 
41  Grid2D Grid = createGrid2D(bu, transfoGlobalToLocal);
42 
43  // Test Global To Local transform
44  Acts::Vector3D pos(1., 2., 3.);
45  Acts::Vector2D pos_2d(1., 2.);
46  BOOST_CHECK_EQUAL(pos_2d, transfoGlobalToLocal(pos));
47 
48  // Test Grid
49  BOOST_CHECK_EQUAL(Grid.dimensions(), 2);
50 
51  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
52  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
53 
54  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
55  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
56 
57  float max1 =
58  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
59  float max2 =
60  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
61 
62  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
63  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
64 
65  // Test pos to index
66  Grid2D::index_t index1 = {1, 1};
67  Grid2D::index_t index2 = {7, 2};
68  Grid2D::index_t index3 = {1, 3};
69 
70  Acts::Vector3D pos1 = {-2.6, -1.5, -0.7};
71  Acts::Vector3D pos2 = {2.8, 0, 0.2};
72  Acts::Vector3D pos3 = {-2.7, 1.8, 0.8};
73 
74  for (int i = 0; i < 2; i++) {
75  BOOST_CHECK_EQUAL(
76  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
77  index1[i]);
78  BOOST_CHECK_EQUAL(
79  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
80  index2[i]);
81  BOOST_CHECK_EQUAL(
82  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
83  index3[i]);
84  }
85  // Test material mapping
86 
87  std::vector<Acts::Vector3D> vectPos1;
88  vectPos1.push_back(pos1);
89  std::vector<Acts::Vector3D> vectPos2;
90  vectPos2.push_back(pos2);
91  std::vector<Acts::Vector3D> vectPos3;
92  vectPos3.push_back(pos3);
93 
94  std::vector<std::pair<MaterialSlab, std::vector<Vector3D>>> matRecord;
95  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
96  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
97  Material vacuum;
98 
99  MaterialSlab matprop1(mat1, 1);
100  MaterialSlab matprop2(mat2, 1);
101 
102  matRecord.clear();
103  matRecord.push_back(std::make_pair(matprop1, vectPos1));
104  matRecord.push_back(std::make_pair(matprop2, vectPos2));
105 
106  MaterialGrid2D matMap =
107  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
108 
109  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
110  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
111  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
112 }
113 
116 BOOST_AUTO_TEST_CASE(PhiZ_Grid_test) {
117  BinUtility bu(2, -2., 2., open, binZ);
118  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
119  auto bd = bu.binningData();
120  std::function<Acts::Vector2D(Acts::Vector3D)> transfoGlobalToLocal;
121 
122  Grid2D Grid = createGrid2D(bu, transfoGlobalToLocal);
123 
124  // Test Global To Local transform
125  Acts::Vector3D pos(1., 2., 3.);
126 
127  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
128  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], 3, 1e-4);
129 
130  // Test Grid
131  BOOST_CHECK_EQUAL(Grid.dimensions(), 2);
132 
133  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
134  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
135 
136  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
137  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
138 
139  float max1 =
140  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
141  float max2 =
142  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
143 
144  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
145  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
146 
147  // Test pos to index
148  Grid2D::index_t index1 = {1, 1};
149  Grid2D::index_t index2 = {1, 2};
150  Grid2D::index_t index3 = {2, 3};
151 
152  Acts::Vector3D pos1 = {-0.2, -1, -1};
153  Acts::Vector3D pos2 = {3.6, 0., -1.5};
154  Acts::Vector3D pos3 = {-1, 0.3, 0.8};
155 
156  for (int i = 0; i < 2; i++) {
157  BOOST_CHECK_EQUAL(
158  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
159  index1[i]);
160  BOOST_CHECK_EQUAL(
161  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
162  index2[i]);
163  BOOST_CHECK_EQUAL(
164  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
165  index3[i]);
166  }
167 
168  // Test material mapping
169  std::vector<Acts::Vector3D> vectPos1;
170  vectPos1.push_back(pos1);
171  std::vector<Acts::Vector3D> vectPos2;
172  vectPos2.push_back(pos2);
173  std::vector<Acts::Vector3D> vectPos3;
174  vectPos3.push_back(pos3);
175 
176  std::vector<std::pair<MaterialSlab, std::vector<Vector3D>>> matRecord;
177  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
178  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
179  Material vacuum;
180 
181  MaterialSlab matprop1(mat1, 1);
182  MaterialSlab matprop2(mat2, 1);
183 
184  matRecord.clear();
185  matRecord.push_back(std::make_pair(matprop1, vectPos1));
186  matRecord.push_back(std::make_pair(matprop2, vectPos2));
187 
188  MaterialGrid2D matMap =
189  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
190 
191  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
192  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
193  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
194 }
195 
197 BOOST_AUTO_TEST_CASE(Cubic_Grid_test) {
198  BinUtility bu(7, -3., 3., open, binX);
199  bu += BinUtility(3, -2., 2., open, binY);
200  bu += BinUtility(2, -1., 1., open, binZ);
201  auto bd = bu.binningData();
202  std::function<Acts::Vector3D(Acts::Vector3D)> transfoGlobalToLocal;
203 
204  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
205 
206  // Test Global To Local transform
207  Acts::Vector3D pos(1., 2., 3.);
208  BOOST_CHECK_EQUAL(pos, transfoGlobalToLocal(pos));
209 
210  // Test Grid
211  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
212 
213  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
214  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
215  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
216 
217  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
218  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
219  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
220 
221  float max1 =
222  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
223  float max2 =
224  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
225  float max3 =
226  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
227 
228  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
229  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
230  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
231 
232  // Test pos to index
233  Grid3D::index_t index1 = {1, 1, 1};
234  Grid3D::index_t index2 = {7, 2, 2};
235  Grid3D::index_t index3 = {1, 3, 2};
236 
237  Acts::Vector3D pos1 = {-2.6, -1.5, -0.7};
238  Acts::Vector3D pos2 = {2.8, 0, 0.2};
239  Acts::Vector3D pos3 = {-2.7, 1.8, 0.8};
240 
241  for (int i = 0; i < 3; i++) {
242  BOOST_CHECK_EQUAL(
243  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
244  index1[i]);
245  BOOST_CHECK_EQUAL(
246  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
247  index2[i]);
248  BOOST_CHECK_EQUAL(
249  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
250  index3[i]);
251  }
252  // Test material mapping
253  std::vector<Acts::Vector3D> vectPos1;
254  vectPos1.push_back(pos1);
255  std::vector<Acts::Vector3D> vectPos2;
256  vectPos2.push_back(pos2);
257  std::vector<Acts::Vector3D> vectPos3;
258  vectPos3.push_back(pos3);
259 
260  std::vector<std::pair<MaterialSlab, std::vector<Vector3D>>> matRecord;
261  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
262  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
263  Material vacuum;
264 
265  MaterialSlab matprop1(mat1, 1);
266  MaterialSlab matprop2(mat2, 1);
267 
268  matRecord.clear();
269  matRecord.push_back(std::make_pair(matprop1, vectPos1));
270  matRecord.push_back(std::make_pair(matprop2, vectPos2));
271 
272  MaterialGrid3D matMap =
273  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
274 
275  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
276  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
277  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
278 }
279 
281 BOOST_AUTO_TEST_CASE(Cylindrical_Grid_test) {
282  BinUtility bu(4, 1., 4., open, binR);
283  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
284  bu += BinUtility(2, -2., 2., open, binZ);
285  auto bd = bu.binningData();
286  std::function<Acts::Vector3D(Acts::Vector3D)> transfoGlobalToLocal;
287 
288  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
289 
290  // Test Global To Local transform
291  Acts::Vector3D pos(1., 2., 3.);
292 
293  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], sqrt(5), 1e-4);
294  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
295  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[2], 3, 1e-4);
296 
297  // Test Grid
298  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
299 
300  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
301  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
302  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
303 
304  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
305  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
306  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
307 
308  float max1 =
309  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
310  float max2 =
311  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
312  float max3 =
313  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
314 
315  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
316  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
317  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
318 
319  // Test pos to index
320  Grid3D::index_t index1 = {1, 1, 1};
321  Grid3D::index_t index2 = {4, 2, 1};
322  Grid3D::index_t index3 = {1, 3, 2};
323 
324  Acts::Vector3D pos1 = {-0.2, -1, -1};
325  Acts::Vector3D pos2 = {3.6, 0., -1.5};
326  Acts::Vector3D pos3 = {-1, 0.3, 0.8};
327 
328  for (int i = 0; i < 3; i++) {
329  BOOST_CHECK_EQUAL(
330  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
331  index1[i]);
332  BOOST_CHECK_EQUAL(
333  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
334  index2[i]);
335  BOOST_CHECK_EQUAL(
336  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
337  index3[i]);
338  }
339 
340  // Test material mapping
341  std::vector<Acts::Vector3D> vectPos1;
342  vectPos1.push_back(pos1);
343  std::vector<Acts::Vector3D> vectPos2;
344  vectPos2.push_back(pos2);
345  std::vector<Acts::Vector3D> vectPos3;
346  vectPos3.push_back(pos3);
347 
348  std::vector<std::pair<MaterialSlab, std::vector<Vector3D>>> matRecord;
349  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
350  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
351  Material vacuum;
352 
353  MaterialSlab matprop1(mat1, 1);
354  MaterialSlab matprop2(mat2, 1);
355 
356  matRecord.clear();
357  matRecord.push_back(std::make_pair(matprop1, vectPos1));
358  matRecord.push_back(std::make_pair(matprop2, vectPos2));
359 
360  MaterialGrid3D matMap =
361  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
362 
363  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
364  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
365  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
366 }
367 
368 } // namespace Test
369 } // namespace Acts