ECCE @ 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 
11 #include <limits>
12 #include <random>
13 #include <vector>
14 
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<std::pair<MaterialProperties, Vector3D>> matRecord;
88  Material mat1(1., 2., 3., 4., 5.);
89  Material mat2(6., 7., 8., 9., 10.);
90  Material vacuum;
91 
92  MaterialProperties matprop1(mat1, 1);
93  MaterialProperties matprop2(mat2, 1);
94 
95  matRecord.clear();
96  matRecord.push_back(std::make_pair(matprop1, pos1));
97  matRecord.push_back(std::make_pair(matprop2, pos2));
98 
99  MaterialGrid2D matMap =
100  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
101 
102  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.classificationNumbers(),
103  1e-4);
104  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.classificationNumbers(),
105  1e-4);
106  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.classificationNumbers());
107 }
108 
111 BOOST_AUTO_TEST_CASE(PhiZ_Grid_test) {
112  BinUtility bu(2, -2., 2., open, binZ);
113  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
114  auto bd = bu.binningData();
115  std::function<Acts::Vector2D(Acts::Vector3D)> transfoGlobalToLocal;
116 
117  Grid2D Grid = createGrid2D(bu, transfoGlobalToLocal);
118 
119  // Test Global To Local transform
120  Acts::Vector3D pos(1., 2., 3.);
121 
122  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
123  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], 3, 1e-4);
124 
125  // Test Grid
126  BOOST_CHECK_EQUAL(Grid.dimensions(), 2);
127 
128  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
129  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
130 
131  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
132  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
133 
134  float max1 =
135  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
136  float max2 =
137  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
138 
139  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
140  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
141 
142  // Test pos to index
143  Grid2D::index_t index1 = {1, 1};
144  Grid2D::index_t index2 = {1, 2};
145  Grid2D::index_t index3 = {2, 3};
146 
147  Acts::Vector3D pos1 = {-0.2, -1, -1};
148  Acts::Vector3D pos2 = {3.6, 0., -1.5};
149  Acts::Vector3D pos3 = {-1, 0.3, 0.8};
150 
151  for (int i = 0; i < 2; i++) {
152  BOOST_CHECK_EQUAL(
153  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
154  index1[i]);
155  BOOST_CHECK_EQUAL(
156  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
157  index2[i]);
158  BOOST_CHECK_EQUAL(
159  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
160  index3[i]);
161  }
162 
163  // Test material mapping
164 
165  std::vector<std::pair<MaterialProperties, Vector3D>> matRecord;
166  Material mat1(1., 2., 3., 4., 5.);
167  Material mat2(6., 7., 8., 9., 10.);
168  Material vacuum;
169 
170  MaterialProperties matprop1(mat1, 1);
171  MaterialProperties matprop2(mat2, 1);
172 
173  matRecord.clear();
174  matRecord.push_back(std::make_pair(matprop1, pos1));
175  matRecord.push_back(std::make_pair(matprop2, pos2));
176 
177  MaterialGrid2D matMap =
178  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
179 
180  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.classificationNumbers(),
181  1e-4);
182  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.classificationNumbers(),
183  1e-4);
184  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.classificationNumbers());
185 }
186 
188 BOOST_AUTO_TEST_CASE(Cubic_Grid_test) {
189  BinUtility bu(7, -3., 3., open, binX);
190  bu += BinUtility(3, -2., 2., open, binY);
191  bu += BinUtility(2, -1., 1., open, binZ);
192  auto bd = bu.binningData();
193  std::function<Acts::Vector3D(Acts::Vector3D)> transfoGlobalToLocal;
194 
195  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
196 
197  // Test Global To Local transform
198  Acts::Vector3D pos(1., 2., 3.);
199  BOOST_CHECK_EQUAL(pos, transfoGlobalToLocal(pos));
200 
201  // Test Grid
202  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
203 
204  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
205  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
206  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
207 
208  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
209  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
210  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
211 
212  float max1 =
213  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
214  float max2 =
215  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
216  float max3 =
217  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
218 
219  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
220  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
221  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
222 
223  // Test pos to index
224  Grid3D::index_t index1 = {1, 1, 1};
225  Grid3D::index_t index2 = {7, 2, 2};
226  Grid3D::index_t index3 = {1, 3, 2};
227 
228  Acts::Vector3D pos1 = {-2.6, -1.5, -0.7};
229  Acts::Vector3D pos2 = {2.8, 0, 0.2};
230  Acts::Vector3D pos3 = {-2.7, 1.8, 0.8};
231 
232  for (int i = 0; i < 3; i++) {
233  BOOST_CHECK_EQUAL(
234  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
235  index1[i]);
236  BOOST_CHECK_EQUAL(
237  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
238  index2[i]);
239  BOOST_CHECK_EQUAL(
240  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
241  index3[i]);
242  }
243  // Test material mapping
244 
245  std::vector<std::pair<MaterialProperties, Vector3D>> matRecord;
246  Material mat1(1., 2., 3., 4., 5.);
247  Material mat2(6., 7., 8., 9., 10.);
248  Material vacuum;
249 
250  MaterialProperties matprop1(mat1, 1);
251  MaterialProperties matprop2(mat2, 1);
252 
253  matRecord.clear();
254  matRecord.push_back(std::make_pair(matprop1, pos1));
255  matRecord.push_back(std::make_pair(matprop2, pos2));
256 
257  MaterialGrid3D matMap =
258  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
259 
260  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.classificationNumbers(),
261  1e-4);
262  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.classificationNumbers(),
263  1e-4);
264  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.classificationNumbers());
265 }
266 
268 BOOST_AUTO_TEST_CASE(Cylindrical_Grid_test) {
269  BinUtility bu(4, 1., 4., open, binR);
270  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
271  bu += BinUtility(2, -2., 2., open, binZ);
272  auto bd = bu.binningData();
273  std::function<Acts::Vector3D(Acts::Vector3D)> transfoGlobalToLocal;
274 
275  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
276 
277  // Test Global To Local transform
278  Acts::Vector3D pos(1., 2., 3.);
279 
280  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], sqrt(5), 1e-4);
281  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
282  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[2], 3, 1e-4);
283 
284  // Test Grid
285  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
286 
287  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
288  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
289  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
290 
291  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
292  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
293  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
294 
295  float max1 =
296  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
297  float max2 =
298  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
299  float max3 =
300  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
301 
302  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
303  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
304  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
305 
306  // Test pos to index
307  Grid3D::index_t index1 = {1, 1, 1};
308  Grid3D::index_t index2 = {4, 2, 1};
309  Grid3D::index_t index3 = {1, 3, 2};
310 
311  Acts::Vector3D pos1 = {-0.2, -1, -1};
312  Acts::Vector3D pos2 = {3.6, 0., -1.5};
313  Acts::Vector3D pos3 = {-1, 0.3, 0.8};
314 
315  for (int i = 0; i < 3; i++) {
316  BOOST_CHECK_EQUAL(
317  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
318  index1[i]);
319  BOOST_CHECK_EQUAL(
320  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
321  index2[i]);
322  BOOST_CHECK_EQUAL(
323  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
324  index3[i]);
325  }
326 
327  // Test material mapping
328 
329  std::vector<std::pair<MaterialProperties, Vector3D>> matRecord;
330  Material mat1(1., 2., 3., 4., 5.);
331  Material mat2(6., 7., 8., 9., 10.);
332  Material vacuum;
333 
334  MaterialProperties matprop1(mat1, 1);
335  MaterialProperties matprop2(mat2, 1);
336 
337  matRecord.clear();
338  matRecord.push_back(std::make_pair(matprop1, pos1));
339  matRecord.push_back(std::make_pair(matprop2, pos2));
340 
341  MaterialGrid3D matMap =
342  mapMaterialPoints(Grid, matRecord, transfoGlobalToLocal);
343 
344  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.classificationNumbers(),
345  1e-4);
346  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.classificationNumbers(),
347  1e-4);
348  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.classificationNumbers());
349 }
350 
351 } // namespace Test
352 } // namespace Acts