Analysis Software
Documentation for sPHENIX simulation 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 
22 
23 #include <cmath>
24 #include <functional>
25 #include <memory>
26 #include <utility>
27 #include <vector>
28 
29 namespace Acts {
30 
31 namespace Test {
32 
33 using RecordedMaterial = std::vector<std::pair<Acts::Material, Acts::Vector3>>;
35 using Grid2D =
37 using Grid3D =
39 using MaterialGrid2D =
41 using MaterialGrid3D =
43 
46 BOOST_AUTO_TEST_CASE(Square_Grid_test) {
47  BinUtility bu(7, -3., 3., open, binX);
48  bu += BinUtility(3, -2., 2., open, binY);
49  auto bd = bu.binningData();
50  std::function<Acts::Vector2(Acts::Vector3)> transfoGlobalToLocal;
51 
52  Grid2D Grid = createGrid2D(bu, transfoGlobalToLocal);
53 
54  // Test Global To Local transform
55  Acts::Vector3 pos(1., 2., 3.);
56  Acts::Vector2 pos_2d(1., 2.);
57  BOOST_CHECK_EQUAL(pos_2d, transfoGlobalToLocal(pos));
58 
59  // Test Grid
60  BOOST_CHECK_EQUAL(Grid.dimensions(), 2);
61 
62  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
63  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
64 
65  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
66  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
67 
68  float max1 =
69  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
70  float max2 =
71  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
72 
73  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
74  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
75 
76  // Test pos to index
77  Grid2D::index_t index1 = {1, 1};
78  Grid2D::index_t index2 = {7, 2};
79  Grid2D::index_t index3 = {1, 3};
80 
81  Acts::Vector3 pos1 = {-2.6, -1.5, -0.7};
82  Acts::Vector3 pos2 = {2.8, 0, 0.2};
83  Acts::Vector3 pos3 = {-2.7, 1.8, 0.8};
84 
85  for (int i = 0; i < 2; i++) {
86  BOOST_CHECK_EQUAL(
87  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
88  index1[i]);
89  BOOST_CHECK_EQUAL(
90  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
91  index2[i]);
92  BOOST_CHECK_EQUAL(
93  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
94  index3[i]);
95  }
96  // Test material mapping
97 
98  std::vector<Acts::Vector3> vectPos1;
99  vectPos1.push_back(pos1);
100  std::vector<Acts::Vector3> vectPos2;
101  vectPos2.push_back(pos2);
102  std::vector<Acts::Vector3> vectPos3;
103  vectPos3.push_back(pos3);
104 
105  std::vector<std::pair<MaterialSlab, std::vector<Vector3>>> matRecord;
106  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
107  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
108  Material vacuum;
109 
110  MaterialSlab matprop1(mat1, 1);
111  MaterialSlab matprop2(mat2, 1);
112 
113  matRecord.clear();
114  matRecord.push_back(std::make_pair(matprop1, vectPos1));
115  matRecord.push_back(std::make_pair(matprop2, vectPos2));
116 
117  // Walk over each property
118  for (const auto& rm : matRecord) {
119  // Walk over each point associated with the properties
120  for (const auto& point : rm.second) {
121  // Search for fitting grid point and accumulate
123  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(point));
124  Grid.atLocalBins(index).accumulate(rm.first);
125  }
126  }
127 
128  MaterialGrid2D matMap = mapMaterialPoints(Grid);
129 
130  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
131  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
132  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
133 }
134 
137 BOOST_AUTO_TEST_CASE(PhiZ_Grid_test) {
138  BinUtility bu(2, -2., 2., open, binZ);
139  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
140  auto bd = bu.binningData();
141  std::function<Acts::Vector2(Acts::Vector3)> transfoGlobalToLocal;
142 
143  Grid2D Grid = createGrid2D(bu, transfoGlobalToLocal);
144 
145  // Test Global To Local transform
146  Acts::Vector3 pos(1., 2., 3.);
147 
148  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
149  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], 3, 1e-4);
150 
151  // Test Grid
152  BOOST_CHECK_EQUAL(Grid.dimensions(), 2);
153 
154  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
155  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
156 
157  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
158  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
159 
160  float max1 =
161  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
162  float max2 =
163  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
164 
165  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
166  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
167 
168  // Test pos to index
169  Grid2D::index_t index1 = {1, 1};
170  Grid2D::index_t index2 = {1, 2};
171  Grid2D::index_t index3 = {2, 3};
172 
173  Acts::Vector3 pos1 = {-0.2, -1, -1};
174  Acts::Vector3 pos2 = {3.6, 0., -1.5};
175  Acts::Vector3 pos3 = {-1, 0.3, 0.8};
176 
177  for (int i = 0; i < 2; i++) {
178  BOOST_CHECK_EQUAL(
179  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
180  index1[i]);
181  BOOST_CHECK_EQUAL(
182  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
183  index2[i]);
184  BOOST_CHECK_EQUAL(
185  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
186  index3[i]);
187  }
188 
189  // Test material mapping
190  std::vector<Acts::Vector3> vectPos1;
191  vectPos1.push_back(pos1);
192  std::vector<Acts::Vector3> vectPos2;
193  vectPos2.push_back(pos2);
194  std::vector<Acts::Vector3> vectPos3;
195  vectPos3.push_back(pos3);
196 
197  std::vector<std::pair<MaterialSlab, std::vector<Vector3>>> matRecord;
198  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
199  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
200  Material vacuum;
201 
202  MaterialSlab matprop1(mat1, 1);
203  MaterialSlab matprop2(mat2, 1);
204 
205  matRecord.clear();
206  matRecord.push_back(std::make_pair(matprop1, vectPos1));
207  matRecord.push_back(std::make_pair(matprop2, vectPos2));
208 
209  // Walk over each property
210  for (const auto& rm : matRecord) {
211  // Walk over each point associated with the properties
212  for (const auto& point : rm.second) {
213  // Search for fitting grid point and accumulate
215  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(point));
216  Grid.atLocalBins(index).accumulate(rm.first);
217  }
218  }
219 
220  MaterialGrid2D matMap = mapMaterialPoints(Grid);
221 
222  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
223  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
224  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
225 }
226 
228 BOOST_AUTO_TEST_CASE(Cubic_Grid_test) {
229  BinUtility bu(7, -3., 3., open, binX);
230  bu += BinUtility(3, -2., 2., open, binY);
231  bu += BinUtility(2, -1., 1., open, binZ);
232  auto bd = bu.binningData();
233  std::function<Acts::Vector3(Acts::Vector3)> transfoGlobalToLocal;
234 
235  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
236 
237  // Test Global To Local transform
238  Acts::Vector3 pos(1., 2., 3.);
239  BOOST_CHECK_EQUAL(pos, transfoGlobalToLocal(pos));
240 
241  // Test Grid
242  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
243 
244  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
245  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
246  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
247 
248  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
249  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
250  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
251 
252  float max1 =
253  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
254  float max2 =
255  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
256  float max3 =
257  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
258 
259  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
260  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
261  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
262 
263  // Test pos to index
264  Grid3D::index_t index1 = {1, 1, 1};
265  Grid3D::index_t index2 = {7, 2, 2};
266  Grid3D::index_t index3 = {1, 3, 2};
267 
268  Acts::Vector3 pos1 = {-2.6, -1.5, -0.7};
269  Acts::Vector3 pos2 = {2.8, 0, 0.2};
270  Acts::Vector3 pos3 = {-2.7, 1.8, 0.8};
271 
272  for (int i = 0; i < 3; i++) {
273  BOOST_CHECK_EQUAL(
274  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
275  index1[i]);
276  BOOST_CHECK_EQUAL(
277  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
278  index2[i]);
279  BOOST_CHECK_EQUAL(
280  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
281  index3[i]);
282  }
283  // Test material mapping
284  std::vector<Acts::Vector3> vectPos1;
285  vectPos1.push_back(pos1);
286  std::vector<Acts::Vector3> vectPos2;
287  vectPos2.push_back(pos2);
288  std::vector<Acts::Vector3> vectPos3;
289  vectPos3.push_back(pos3);
290 
291  std::vector<std::pair<MaterialSlab, std::vector<Vector3>>> matRecord;
292  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
293  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
294  Material vacuum;
295 
296  MaterialSlab matprop1(mat1, 1);
297  MaterialSlab matprop2(mat2, 1);
298 
299  matRecord.clear();
300  matRecord.push_back(std::make_pair(matprop1, vectPos1));
301  matRecord.push_back(std::make_pair(matprop2, vectPos2));
302 
303  // Walk over each property
304  for (const auto& rm : matRecord) {
305  // Walk over each point associated with the properties
306  for (const auto& point : rm.second) {
307  // Search for fitting grid point and accumulate
309  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(point));
310  Grid.atLocalBins(index).accumulate(rm.first);
311  }
312  }
313 
314  MaterialGrid3D matMap = mapMaterialPoints(Grid);
315 
316  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
317  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
318  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
319 }
320 
322 BOOST_AUTO_TEST_CASE(Cylindrical_Grid_test) {
323  BinUtility bu(4, 1., 4., open, binR);
324  bu += BinUtility(3, -M_PI, M_PI, closed, binPhi);
325  bu += BinUtility(2, -2., 2., open, binZ);
326  auto bd = bu.binningData();
327  std::function<Acts::Vector3(Acts::Vector3)> transfoGlobalToLocal;
328 
329  Grid3D Grid = createGrid3D(bu, transfoGlobalToLocal);
330 
331  // Test Global To Local transform
332  Acts::Vector3 pos(1., 2., 3.);
333 
334  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[0], sqrt(5), 1e-4);
335  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[1], atan2(2, 1), 1e-4);
336  CHECK_CLOSE_REL(transfoGlobalToLocal(pos)[2], 3, 1e-4);
337 
338  // Test Grid
339  BOOST_CHECK_EQUAL(Grid.dimensions(), 3);
340 
341  BOOST_CHECK_EQUAL(Grid.numLocalBins()[0], bd[0].bins());
342  BOOST_CHECK_EQUAL(Grid.numLocalBins()[1], bd[1].bins());
343  BOOST_CHECK_EQUAL(Grid.numLocalBins()[2], bd[2].bins());
344 
345  BOOST_CHECK_EQUAL(Grid.minPosition()[0], bd[0].min);
346  BOOST_CHECK_EQUAL(Grid.minPosition()[1], bd[1].min);
347  BOOST_CHECK_EQUAL(Grid.minPosition()[2], bd[2].min);
348 
349  float max1 =
350  bd[0].max + std::fabs(bd[0].max - bd[0].min) / (bd[0].bins() - 1);
351  float max2 =
352  bd[1].max + std::fabs(bd[1].max - bd[1].min) / (bd[1].bins() - 1);
353  float max3 =
354  bd[2].max + std::fabs(bd[2].max - bd[2].min) / (bd[2].bins() - 1);
355 
356  BOOST_CHECK_EQUAL(Grid.maxPosition()[0], max1);
357  BOOST_CHECK_EQUAL(Grid.maxPosition()[1], max2);
358  BOOST_CHECK_EQUAL(Grid.maxPosition()[2], max3);
359 
360  // Test pos to index
361  Grid3D::index_t index1 = {1, 1, 1};
362  Grid3D::index_t index2 = {4, 2, 1};
363  Grid3D::index_t index3 = {1, 3, 2};
364 
365  Acts::Vector3 pos1 = {-0.2, -1, -1};
366  Acts::Vector3 pos2 = {3.6, 0., -1.5};
367  Acts::Vector3 pos3 = {-1, 0.3, 0.8};
368 
369  for (int i = 0; i < 3; i++) {
370  BOOST_CHECK_EQUAL(
371  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos1))[i],
372  index1[i]);
373  BOOST_CHECK_EQUAL(
374  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos2))[i],
375  index2[i]);
376  BOOST_CHECK_EQUAL(
377  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(pos3))[i],
378  index3[i]);
379  }
380 
381  // Test material mapping
382  std::vector<Acts::Vector3> vectPos1;
383  vectPos1.push_back(pos1);
384  std::vector<Acts::Vector3> vectPos2;
385  vectPos2.push_back(pos2);
386  std::vector<Acts::Vector3> vectPos3;
387  vectPos3.push_back(pos3);
388 
389  std::vector<std::pair<MaterialSlab, std::vector<Vector3>>> matRecord;
390  Material mat1 = Material::fromMolarDensity(1., 2., 3., 4., 5.);
391  Material mat2 = Material::fromMolarDensity(6., 7., 8., 9., 10.);
392  Material vacuum;
393 
394  MaterialSlab matprop1(mat1, 1);
395  MaterialSlab matprop2(mat2, 1);
396 
397  matRecord.clear();
398  matRecord.push_back(std::make_pair(matprop1, vectPos1));
399  matRecord.push_back(std::make_pair(matprop2, vectPos2));
400 
401  // Walk over each property
402  for (const auto& rm : matRecord) {
403  // Walk over each point associated with the properties
404  for (const auto& point : rm.second) {
405  // Search for fitting grid point and accumulate
407  Grid.localBinsFromLowerLeftEdge(transfoGlobalToLocal(point));
408  Grid.atLocalBins(index).accumulate(rm.first);
409  }
410  }
411 
412  MaterialGrid3D matMap = mapMaterialPoints(Grid);
413 
414  CHECK_CLOSE_REL(matMap.atLocalBins(index1), mat1.parameters(), 1e-4);
415  CHECK_CLOSE_REL(matMap.atLocalBins(index2), mat2.parameters(), 1e-4);
416  BOOST_CHECK_EQUAL(matMap.atLocalBins(index3), vacuum.parameters());
417 }
418 
419 } // namespace Test
420 } // namespace Acts