Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AxesTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AxesTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 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 <cstddef>
16 #include <vector>
17 
18 namespace Acts {
19 
20 using namespace detail;
21 
22 namespace Test {
23 
24 BOOST_AUTO_TEST_CASE(equidistant_axis) {
25  EquidistantAxis a(0.0, 10.0, 10u);
26 
27  // general binning properties
28  BOOST_CHECK_EQUAL(a.getNBins(), 10u);
29  BOOST_CHECK_EQUAL(a.getMax(), 10.);
30  BOOST_CHECK_EQUAL(a.getMin(), 0.);
31  BOOST_CHECK_EQUAL(a.getBinWidth(), 1.);
32 
33  // bin index calculation
34  BOOST_CHECK_EQUAL(a.getBin(-0.3), 0u);
35  BOOST_CHECK_EQUAL(a.getBin(-0.), 1u);
36  BOOST_CHECK_EQUAL(a.getBin(0.), 1u);
37  BOOST_CHECK_EQUAL(a.getBin(0.7), 1u);
38  BOOST_CHECK_EQUAL(a.getBin(1), 2u);
39  BOOST_CHECK_EQUAL(a.getBin(1.2), 2u);
40  BOOST_CHECK_EQUAL(a.getBin(2.), 3u);
41  BOOST_CHECK_EQUAL(a.getBin(2.7), 3u);
42  BOOST_CHECK_EQUAL(a.getBin(3.), 4u);
43  BOOST_CHECK_EQUAL(a.getBin(3.6), 4u);
44  BOOST_CHECK_EQUAL(a.getBin(4.), 5u);
45  BOOST_CHECK_EQUAL(a.getBin(4.98), 5u);
46  BOOST_CHECK_EQUAL(a.getBin(5.), 6u);
47  BOOST_CHECK_EQUAL(a.getBin(5.12), 6u);
48  BOOST_CHECK_EQUAL(a.getBin(6.), 7u);
49  BOOST_CHECK_EQUAL(a.getBin(6.00001), 7u);
50  BOOST_CHECK_EQUAL(a.getBin(7.), 8u);
51  BOOST_CHECK_EQUAL(a.getBin(7.5), 8u);
52  BOOST_CHECK_EQUAL(a.getBin(8.), 9u);
53  BOOST_CHECK_EQUAL(a.getBin(8.1), 9u);
54  BOOST_CHECK_EQUAL(a.getBin(9.), 10u);
55  BOOST_CHECK_EQUAL(a.getBin(9.999), 10u);
56  BOOST_CHECK_EQUAL(a.getBin(10.), 11u);
57  BOOST_CHECK_EQUAL(a.getBin(100.3), 11u);
58 
59  // lower bin boundaries
60  BOOST_CHECK_EQUAL(a.getBinLowerBound(1), 0.);
61  BOOST_CHECK_EQUAL(a.getBinLowerBound(2), 1.);
62  BOOST_CHECK_EQUAL(a.getBinLowerBound(3), 2.);
63  BOOST_CHECK_EQUAL(a.getBinLowerBound(4), 3.);
64  BOOST_CHECK_EQUAL(a.getBinLowerBound(5), 4.);
65  BOOST_CHECK_EQUAL(a.getBinLowerBound(6), 5.);
66  BOOST_CHECK_EQUAL(a.getBinLowerBound(7), 6.);
67  BOOST_CHECK_EQUAL(a.getBinLowerBound(8), 7.);
68  BOOST_CHECK_EQUAL(a.getBinLowerBound(9), 8.);
69  BOOST_CHECK_EQUAL(a.getBinLowerBound(10), 9.);
70 
71  // upper bin boundaries
72  BOOST_CHECK_EQUAL(a.getBinUpperBound(1), 1.);
73  BOOST_CHECK_EQUAL(a.getBinUpperBound(2), 2.);
74  BOOST_CHECK_EQUAL(a.getBinUpperBound(3), 3.);
75  BOOST_CHECK_EQUAL(a.getBinUpperBound(4), 4.);
76  BOOST_CHECK_EQUAL(a.getBinUpperBound(5), 5.);
77  BOOST_CHECK_EQUAL(a.getBinUpperBound(6), 6.);
78  BOOST_CHECK_EQUAL(a.getBinUpperBound(7), 7.);
79  BOOST_CHECK_EQUAL(a.getBinUpperBound(8), 8.);
80  BOOST_CHECK_EQUAL(a.getBinUpperBound(9), 9.);
81  BOOST_CHECK_EQUAL(a.getBinUpperBound(10), 10.);
82 
83  // bin centers
84  BOOST_CHECK_EQUAL(a.getBinCenter(1), 0.5);
85  BOOST_CHECK_EQUAL(a.getBinCenter(2), 1.5);
86  BOOST_CHECK_EQUAL(a.getBinCenter(3), 2.5);
87  BOOST_CHECK_EQUAL(a.getBinCenter(4), 3.5);
88  BOOST_CHECK_EQUAL(a.getBinCenter(5), 4.5);
89  BOOST_CHECK_EQUAL(a.getBinCenter(6), 5.5);
90  BOOST_CHECK_EQUAL(a.getBinCenter(7), 6.5);
91  BOOST_CHECK_EQUAL(a.getBinCenter(8), 7.5);
92  BOOST_CHECK_EQUAL(a.getBinCenter(9), 8.5);
93  BOOST_CHECK_EQUAL(a.getBinCenter(10), 9.5);
94 
95  // inside check
96  BOOST_CHECK(not a.isInside(-0.2));
97  BOOST_CHECK(a.isInside(0.));
98  BOOST_CHECK(a.isInside(3.));
99  BOOST_CHECK(not a.isInside(10.));
100  BOOST_CHECK(not a.isInside(12.));
101 }
102 
103 BOOST_AUTO_TEST_CASE(variable_axis) {
104  VariableAxis a({0, 0.5, 3, 4.5, 6});
105 
106  // general binning properties
107  BOOST_CHECK_EQUAL(a.getNBins(), 4u);
108  BOOST_CHECK_EQUAL(a.getMax(), 6.);
109  BOOST_CHECK_EQUAL(a.getMin(), 0.);
110 
111  // bin index calculation
112  BOOST_CHECK_EQUAL(a.getBin(-0.3), 0u);
113  BOOST_CHECK_EQUAL(a.getBin(-0.), 1u);
114  BOOST_CHECK_EQUAL(a.getBin(0.), 1u);
115  BOOST_CHECK_EQUAL(a.getBin(0.3), 1u);
116  BOOST_CHECK_EQUAL(a.getBin(0.5), 2u);
117  BOOST_CHECK_EQUAL(a.getBin(1.2), 2u);
118  BOOST_CHECK_EQUAL(a.getBin(2.7), 2u);
119  BOOST_CHECK_EQUAL(a.getBin(3.), 3u);
120  BOOST_CHECK_EQUAL(a.getBin(4.49999), 3u);
121  BOOST_CHECK_EQUAL(a.getBin(4.5), 4u);
122  BOOST_CHECK_EQUAL(a.getBin(5.12), 4u);
123  BOOST_CHECK_EQUAL(a.getBin(6.), 5u);
124  BOOST_CHECK_EQUAL(a.getBin(6.00001), 5u);
125  BOOST_CHECK_EQUAL(a.getBin(7.5), 5u);
126 
127  // lower bin boundaries
128  BOOST_CHECK_EQUAL(a.getBinLowerBound(1), 0.);
129  BOOST_CHECK_EQUAL(a.getBinLowerBound(2), 0.5);
130  BOOST_CHECK_EQUAL(a.getBinLowerBound(3), 3.);
131  BOOST_CHECK_EQUAL(a.getBinLowerBound(4), 4.5);
132 
133  // upper bin boundaries
134  BOOST_CHECK_EQUAL(a.getBinUpperBound(1), 0.5);
135  BOOST_CHECK_EQUAL(a.getBinUpperBound(2), 3.);
136  BOOST_CHECK_EQUAL(a.getBinUpperBound(3), 4.5);
137  BOOST_CHECK_EQUAL(a.getBinUpperBound(4), 6.);
138 
139  // bin centers
140  BOOST_CHECK_EQUAL(a.getBinCenter(1), 0.25);
141  BOOST_CHECK_EQUAL(a.getBinCenter(2), 1.75);
142  BOOST_CHECK_EQUAL(a.getBinCenter(3), 3.75);
143  BOOST_CHECK_EQUAL(a.getBinCenter(4), 5.25);
144 
145  // inside check
146  BOOST_CHECK(not a.isInside(-0.2));
147  BOOST_CHECK(a.isInside(0.));
148  BOOST_CHECK(a.isInside(3.));
149  BOOST_CHECK(not a.isInside(6.));
150  BOOST_CHECK(not a.isInside(12.));
151 }
152 
155 
156  // normal inside
157  BOOST_CHECK_EQUAL(a.getBin(0.5), 1u);
158  BOOST_CHECK_EQUAL(a.getBin(9.5), 10u);
159 
160  // out of bounds, but is open
161  // -> should clamp
162  BOOST_CHECK_EQUAL(a.getBin(-0.5), 1u);
163  BOOST_CHECK_EQUAL(a.getBin(10.5), 10u);
164 
166  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
167 
168  // normal inside
169  BOOST_CHECK_EQUAL(b.getBin(0.5), 1u);
170  BOOST_CHECK_EQUAL(b.getBin(9.5), 10u);
171 
172  // out of bounds, but is open
173  // -> should clamp
174  BOOST_CHECK_EQUAL(b.getBin(-0.5), 1u);
175  BOOST_CHECK_EQUAL(b.getBin(10.5), 10u);
176 }
177 
178 BOOST_AUTO_TEST_CASE(closed_axis) {
180 
181  // normal inside
182  BOOST_CHECK_EQUAL(a.getBin(0.5), 1u);
183  BOOST_CHECK_EQUAL(a.getBin(9.5), 10u);
184 
185  // out of bounds, but is closed
186  // -> should wrap to opposite side bin
187  BOOST_CHECK_EQUAL(a.getBin(-0.5), 10u);
188  BOOST_CHECK_EQUAL(a.getBin(10.5), 1u);
189 
191  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
192 
193  // normal inside
194  BOOST_CHECK_EQUAL(b.getBin(0.5), 1u);
195  BOOST_CHECK_EQUAL(b.getBin(9.5), 10u);
196 
197  // out of bounds, but is closed
198  // -> should wrap to opposite side bin
199  BOOST_CHECK_EQUAL(b.getBin(-0.5), 10u);
200  BOOST_CHECK_EQUAL(b.getBin(10.5), 1u);
201 }
202 
203 BOOST_AUTO_TEST_CASE(neighborhood) {
204  using bins_t = std::vector<size_t>;
206 
207  BOOST_CHECK(a1.neighborHoodIndices(0, 1).collect() == bins_t({0, 1}));
208  BOOST_CHECK(a1.neighborHoodIndices(1, 1).collect() == bins_t({0, 1, 2}));
209  BOOST_CHECK(a1.neighborHoodIndices(11, 1).collect() == bins_t({10, 11}));
210  BOOST_CHECK(a1.neighborHoodIndices(10, 1).collect() == bins_t({9, 10, 11}));
211  BOOST_CHECK(a1.neighborHoodIndices(5, 1).collect() == bins_t({4, 5, 6}));
212  BOOST_CHECK(a1.neighborHoodIndices(5, {-1, 0}).collect() == bins_t({4, 5}));
213  BOOST_CHECK(a1.neighborHoodIndices(5, {0, 1}).collect() == bins_t({5, 6}));
214 
215  BOOST_CHECK(a1.neighborHoodIndices(0, 2).collect() == bins_t({0, 1, 2}));
216  BOOST_CHECK(a1.neighborHoodIndices(1, 2).collect() == bins_t({0, 1, 2, 3}));
217  BOOST_CHECK(a1.neighborHoodIndices(11, 2).collect() == bins_t({9, 10, 11}));
218  BOOST_CHECK(a1.neighborHoodIndices(10, 2).collect() ==
219  bins_t({8, 9, 10, 11}));
220  BOOST_CHECK(a1.neighborHoodIndices(5, 2).collect() ==
221  bins_t({3, 4, 5, 6, 7}));
222 
224  {0.0, 2.0, 4.0, 9.0, 10.0});
225  BOOST_CHECK(a2.neighborHoodIndices(0, 1).collect() == bins_t({0, 1}));
226  BOOST_CHECK(a2.neighborHoodIndices(1, 1).collect() == bins_t({0, 1, 2}));
227  BOOST_CHECK(a2.neighborHoodIndices(5, 1).collect() == bins_t({4, 5}));
228  BOOST_CHECK(a2.neighborHoodIndices(4, 1).collect() == bins_t({3, 4, 5}));
229  BOOST_CHECK(a2.neighborHoodIndices(4, {-1, 0}).collect() == bins_t({3, 4}));
230  BOOST_CHECK(a2.neighborHoodIndices(2, 1).collect() == bins_t({1, 2, 3}));
231  BOOST_CHECK(a2.neighborHoodIndices(2, {0, 1}).collect() == bins_t({2, 3}));
232 
233  BOOST_CHECK(a2.neighborHoodIndices(0, 2).collect() == bins_t({0, 1, 2}));
234  BOOST_CHECK(a2.neighborHoodIndices(1, 2).collect() == bins_t({0, 1, 2, 3}));
235  BOOST_CHECK(a2.neighborHoodIndices(5, 2).collect() == bins_t({3, 4, 5}));
236  BOOST_CHECK(a2.neighborHoodIndices(4, 2).collect() == bins_t({2, 3, 4, 5}));
237  BOOST_CHECK(a2.neighborHoodIndices(3, 2).collect() ==
238  bins_t({1, 2, 3, 4, 5}));
239 
241 
242  BOOST_CHECK(a3.neighborHoodIndices(0, 1).collect() == bins_t({}));
243  BOOST_CHECK(a3.neighborHoodIndices(1, 1).collect() == bins_t({1, 2}));
244  BOOST_CHECK(a3.neighborHoodIndices(11, 1).collect() == bins_t({}));
245  BOOST_CHECK(a3.neighborHoodIndices(10, 1).collect() == bins_t({9, 10}));
246  BOOST_CHECK(a3.neighborHoodIndices(10, {0, 1}).collect() == bins_t({10}));
247  BOOST_CHECK(a3.neighborHoodIndices(5, 1).collect() == bins_t({4, 5, 6}));
248  BOOST_CHECK(a3.neighborHoodIndices(5, {-1, 0}).collect() == bins_t({4, 5}));
249  BOOST_CHECK(a3.neighborHoodIndices(5, {0, 1}).collect() == bins_t({5, 6}));
250 
251  BOOST_CHECK(a3.neighborHoodIndices(0, 2).collect() == bins_t({}));
252  BOOST_CHECK(a3.neighborHoodIndices(1, 2).collect() == bins_t({1, 2, 3}));
253  BOOST_CHECK(a3.neighborHoodIndices(11, 2).collect() == bins_t({}));
254  BOOST_CHECK(a3.neighborHoodIndices(10, 2).collect() == bins_t({8, 9, 10}));
255  BOOST_CHECK(a3.neighborHoodIndices(5, 2).collect() ==
256  bins_t({3, 4, 5, 6, 7}));
257 
259 
260  BOOST_CHECK(a4.neighborHoodIndices(0, 1).collect() == bins_t({}));
261  BOOST_CHECK(a4.neighborHoodIndices(1, 1).collect() == bins_t({10, 1, 2}));
262  BOOST_CHECK(a4.neighborHoodIndices(11, 1).collect() == bins_t({}));
263  BOOST_CHECK(a4.neighborHoodIndices(10, 1).collect() == bins_t({9, 10, 1}));
264  BOOST_CHECK(a4.neighborHoodIndices(10, {0, 1}).collect() == bins_t({10, 1}));
265  BOOST_CHECK(a4.neighborHoodIndices(5, 1).collect() == bins_t({4, 5, 6}));
266  BOOST_CHECK(a4.neighborHoodIndices(5, {-1, 0}).collect() == bins_t({4, 5}));
267  BOOST_CHECK(a4.neighborHoodIndices(5, {0, 1}).collect() == bins_t({5, 6}));
268 
269  BOOST_CHECK(a4.neighborHoodIndices(0, 2).collect() == bins_t({}));
270  BOOST_CHECK(a4.neighborHoodIndices(1, 2).collect() ==
271  bins_t({9, 10, 1, 2, 3}));
272  BOOST_CHECK(a4.neighborHoodIndices(11, 2).collect() == bins_t({}));
273  BOOST_CHECK(a4.neighborHoodIndices(10, 2).collect() ==
274  bins_t({8, 9, 10, 1, 2}));
275  BOOST_CHECK(a4.neighborHoodIndices(5, 2).collect() ==
276  bins_t({3, 4, 5, 6, 7}));
277 
279  {0.0, 2.0, 4.0, 9.0, 9.5, 10.0});
280  BOOST_CHECK(a5.neighborHoodIndices(0, 1).collect() == bins_t({}));
281  BOOST_CHECK(a5.neighborHoodIndices(1, 1).collect() == bins_t({1, 2}));
282  BOOST_CHECK(a5.neighborHoodIndices(6, 1).collect() == bins_t({}));
283  BOOST_CHECK(a5.neighborHoodIndices(5, 1).collect() == bins_t({4, 5}));
284  BOOST_CHECK(a5.neighborHoodIndices(5, {0, 1}).collect() == bins_t({5}));
285  BOOST_CHECK(a5.neighborHoodIndices(2, 1).collect() == bins_t({1, 2, 3}));
286  BOOST_CHECK(a5.neighborHoodIndices(2, {-1, 0}).collect() == bins_t({1, 2}));
287  BOOST_CHECK(a5.neighborHoodIndices(2, {0, 1}).collect() == bins_t({2, 3}));
288 
289  BOOST_CHECK(a5.neighborHoodIndices(0, 2).collect() == bins_t({}));
290  BOOST_CHECK(a5.neighborHoodIndices(1, 2).collect() == bins_t({1, 2, 3}));
291  BOOST_CHECK(a5.neighborHoodIndices(6, 2).collect() == bins_t({}));
292  BOOST_CHECK(a5.neighborHoodIndices(5, 2).collect() == bins_t({3, 4, 5}));
293  BOOST_CHECK(a5.neighborHoodIndices(3, 2).collect() ==
294  bins_t({1, 2, 3, 4, 5}));
295 
297  {0.0, 2.0, 4.0, 9.0, 9.5, 10.0});
298  BOOST_CHECK(a6.neighborHoodIndices(0, 1).collect() == bins_t({}));
299  BOOST_CHECK(a6.neighborHoodIndices(1, 1).collect() == bins_t({5, 1, 2}));
300  BOOST_CHECK(a6.neighborHoodIndices(6, 1).collect() == bins_t({}));
301  BOOST_CHECK(a6.neighborHoodIndices(5, 1).collect() == bins_t({4, 5, 1}));
302  BOOST_CHECK(a6.neighborHoodIndices(5, {0, 1}).collect() == bins_t({5, 1}));
303  BOOST_CHECK(a6.neighborHoodIndices(2, 1).collect() == bins_t({1, 2, 3}));
304  BOOST_CHECK(a6.neighborHoodIndices(2, {-1, 0}).collect() == bins_t({1, 2}));
305  BOOST_CHECK(a6.neighborHoodIndices(2, {0, 1}).collect() == bins_t({2, 3}));
306 
307  BOOST_CHECK(a6.neighborHoodIndices(0, 2).collect() == bins_t({}));
308  BOOST_CHECK(a6.neighborHoodIndices(1, 2).collect() ==
309  bins_t({4, 5, 1, 2, 3}));
310  BOOST_CHECK(a6.neighborHoodIndices(6, 2).collect() == bins_t({}));
311  BOOST_CHECK(a6.neighborHoodIndices(5, 2).collect() ==
312  bins_t({3, 4, 5, 1, 2}));
313  BOOST_CHECK(a6.neighborHoodIndices(3, 2).collect() ==
314  bins_t({1, 2, 3, 4, 5}));
315  BOOST_CHECK(a6.neighborHoodIndices(3, {0, 2}).collect() == bins_t({3, 4, 5}));
316 
317  BOOST_CHECK(a6.neighborHoodIndices(1, 3).collect() ==
318  bins_t({1, 2, 3, 4, 5}));
319  BOOST_CHECK(a6.neighborHoodIndices(5, 3).collect() ==
320  bins_t({1, 2, 3, 4, 5}));
321 }
322 
325  BOOST_CHECK_EQUAL(a1.wrapBin(0), 0u);
326  BOOST_CHECK_EQUAL(a1.wrapBin(1), 1u);
327  BOOST_CHECK_EQUAL(a1.wrapBin(-1), 0u);
328  BOOST_CHECK_EQUAL(a1.wrapBin(10), 10u);
329  BOOST_CHECK_EQUAL(a1.wrapBin(11), 11u);
330  BOOST_CHECK_EQUAL(a1.wrapBin(12), 11u);
331 
333  BOOST_CHECK_EQUAL(a2.wrapBin(0), 1u);
334  BOOST_CHECK_EQUAL(a2.wrapBin(1), 1u);
335  BOOST_CHECK_EQUAL(a2.wrapBin(-1), 1u);
336  BOOST_CHECK_EQUAL(a2.wrapBin(10), 10u);
337  BOOST_CHECK_EQUAL(a2.wrapBin(11), 10u);
338  BOOST_CHECK_EQUAL(a2.wrapBin(12), 10u);
339 
341  BOOST_CHECK_EQUAL(a3.wrapBin(0), 10u);
342  BOOST_CHECK_EQUAL(a3.wrapBin(1), 1u);
343  BOOST_CHECK_EQUAL(a3.wrapBin(-1), 9u);
344  BOOST_CHECK_EQUAL(a3.wrapBin(10), 10u);
345  BOOST_CHECK_EQUAL(a3.wrapBin(11), 1u);
346  BOOST_CHECK_EQUAL(a3.wrapBin(12), 2u);
347 
349  {0.0, 2.0, 4.0, 9.0, 10.0});
350  BOOST_CHECK_EQUAL(a4.wrapBin(0), 0u);
351  BOOST_CHECK_EQUAL(a4.wrapBin(1), 1u);
352  BOOST_CHECK_EQUAL(a4.wrapBin(-1), 0u);
353  BOOST_CHECK_EQUAL(a4.wrapBin(4), 4u);
354  BOOST_CHECK_EQUAL(a4.wrapBin(5), 5u);
355  BOOST_CHECK_EQUAL(a4.wrapBin(6), 5u);
356 
358  {0.0, 2.0, 4.0, 9.0, 9.5, 10.0});
359  BOOST_CHECK_EQUAL(a5.wrapBin(0), 1u);
360  BOOST_CHECK_EQUAL(a5.wrapBin(1), 1u);
361  BOOST_CHECK_EQUAL(a5.wrapBin(-1), 1u);
362  BOOST_CHECK_EQUAL(a5.wrapBin(4), 4u);
363  BOOST_CHECK_EQUAL(a5.wrapBin(5), 5u);
364  BOOST_CHECK_EQUAL(a5.wrapBin(6), 5u);
365 
367  {0.0, 2.0, 4.0, 9.0, 9.5, 10.0});
368  BOOST_CHECK_EQUAL(a6.wrapBin(0), 5u);
369  BOOST_CHECK_EQUAL(a6.wrapBin(1), 1u);
370  BOOST_CHECK_EQUAL(a6.wrapBin(-1), 4u);
371  BOOST_CHECK_EQUAL(a6.wrapBin(4), 4u);
372  BOOST_CHECK_EQUAL(a6.wrapBin(5), 5u);
373  BOOST_CHECK_EQUAL(a6.wrapBin(6), 1u);
374  BOOST_CHECK_EQUAL(a6.wrapBin(7), 2u);
375 }
376 
377 } // namespace Test
378 
379 } // namespace Acts