ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HelpersTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HelpersTests.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 <bitset>
12 
15 
16 using namespace Acts::VectorHelpers;
17 
18 namespace Acts {
19 namespace Test {
20 
21 BOOST_AUTO_TEST_SUITE(Utilities)
22 
23 BOOST_AUTO_TEST_CASE(bitset_to_matrix_to_bitset) {
24  Eigen::Matrix<int, 4, 3> mat;
25  mat << 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0;
26 
27  std::bitset<4 * 3> act = matrixToBitset(mat);
28  std::bitset<4 * 3> exp{"101001011010"};
29 
30  BOOST_CHECK_EQUAL(exp, act);
31 
32  Eigen::Matrix<int, 4, 3> cnv;
33  cnv = bitsetToMatrix<decltype(cnv)>(act);
34 
35  BOOST_CHECK_EQUAL(mat, cnv);
36 }
37 
38 struct MyStruct {
39  double phi() const { return 42; }
40 };
41 
42 BOOST_AUTO_TEST_CASE(phi_helper_test) {
43  Vector3D v(0, 1, 0);
44  CHECK_CLOSE_ABS(phi(v), M_PI / 2., 1e-9);
45 
46  // should work with dynamic types as well
47  ActsVectorXd v2{3};
48  v2 << 0, 1, 0;
49  CHECK_CLOSE_ABS(phi(v2), M_PI / 2., 1e-9);
50 
51  MyStruct s;
52  BOOST_CHECK_EQUAL(phi(s), 42u);
53 }
54 
55 BOOST_AUTO_TEST_CASE(perp_helper_test) {
56  Vector3D v(1, 2, 3);
57  CHECK_CLOSE_ABS(perp(v), std::sqrt(1 + 2 * 2), 1e-9);
58 
59  // should work with dynamic types as well
60  ActsVectorXd v2{3};
61  v2 << 1, 2, 3;
62  CHECK_CLOSE_ABS(perp(v2), std::sqrt(1 + 2 * 2), 1e-9);
63 }
64 
65 BOOST_AUTO_TEST_CASE(theta_eta_test_helper) {
66  Vector3D v(1, 2, 3);
67  CHECK_CLOSE_ABS(theta(v), 0.640522, 1e-5);
68  CHECK_CLOSE_ABS(eta(v), 1.10359, 1e-5);
69 
70  // should work with dynamic types as well
71  ActsVectorXd v2{3};
72  v2 << 1, 2, 3;
73  CHECK_CLOSE_ABS(theta(v2), 0.640522, 1e-5);
74  CHECK_CLOSE_ABS(eta(v2), 1.10359, 1e-5);
75 }
76 
77 BOOST_AUTO_TEST_CASE(cross_test_helper) {
78  {
79  Vector3D v(1, 2, 3);
81  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
82 
83  ActsMatrixD<3, 3> act = cross(mat, v);
85  exp << -2, -1, 0, 4, 2, 0, -2, -1, 0;
86 
87  CHECK_CLOSE_ABS(act, exp, 1e-9);
88  }
89 
90  // should work with dynamic types as well
91  {
92  ActsVectorXd v{3};
93  v << 1, 2, 3;
94  ActsMatrixXd mat{3, 3};
95  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
96 
97  ActsMatrixXd act = cross(mat, v);
98  ActsMatrixXd exp{3, 3};
99  exp << -2, -1, 0, 4, 2, 0, -2, -1, 0;
100 
101  BOOST_CHECK(act.isApprox(exp, 1e-9));
102  }
103 }
104 
105 BOOST_AUTO_TEST_CASE(toString_test_helper) {
107  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
108  std::string out;
109  out = toString(mat);
110  BOOST_CHECK(out.size() > 0);
111 
112  Translation3D trl{Vector3D{1, 2, 3}};
113  out = toString(trl);
114  BOOST_CHECK(out.size() > 0);
115 
116  Transform3D trf;
117  trf = trl;
118  out = toString(trf);
119  BOOST_CHECK(out.size() > 0);
120 }
121 
122 BOOST_AUTO_TEST_CASE(shared_vector_helper_test) {
123  {
124  std::vector<std::shared_ptr<int>> vec;
125  vec = {std::make_shared<int>(5), std::make_shared<int>(9),
126  std::make_shared<int>(26), std::make_shared<int>(18473)};
127 
128  std::vector<int*> unpacked = unpack_shared_vector(vec);
129 
130  std::vector<int*> exp = {
131  vec[0].get(),
132  vec[1].get(),
133  vec[2].get(),
134  vec[3].get(),
135  };
136 
137  BOOST_CHECK_EQUAL_COLLECTIONS(unpacked.begin(), unpacked.end(), exp.begin(),
138  exp.end());
139  }
140 
141  // same for const
142  {
143  std::vector<std::shared_ptr<const int>> vec;
144  vec = {std::make_shared<const int>(5), std::make_shared<const int>(9),
145  std::make_shared<const int>(26), std::make_shared<const int>(18473)};
146 
147  std::vector<const int*> unpacked = unpack_shared_vector(vec);
148 
149  std::vector<const int*> exp = {
150  vec[0].get(),
151  vec[1].get(),
152  vec[2].get(),
153  vec[3].get(),
154  };
155 
156  BOOST_CHECK_EQUAL_COLLECTIONS(unpacked.begin(), unpacked.end(), exp.begin(),
157  exp.end());
158  }
159 }
160 
161 BOOST_AUTO_TEST_CASE(time_helper_test) {
162  {
163  SpacePointVector spaceTimeVec = {1., 2., 3., 4.};
164  double t = time(spaceTimeVec);
165  BOOST_CHECK_EQUAL(spaceTimeVec[3], t);
166 
167  double newTime = 17;
168  time(spaceTimeVec) = newTime;
169  BOOST_CHECK_EQUAL(spaceTimeVec[3], newTime);
170 
171  BoundVector boundVec;
172  boundVec << 1., 2., 3., 4., 5., 6.;
173  t = time(boundVec);
174  BOOST_CHECK_EQUAL(boundVec[eT], t);
175 
176  newTime = 28;
177  time(boundVec) = newTime;
178  BOOST_CHECK_EQUAL(boundVec[eT], newTime);
179 
180  FreeVector freeVec;
181  freeVec << 1., 2., 3., 4., 5., 6., 7., 8.;
182  t = time(freeVec);
183  BOOST_CHECK_EQUAL(freeVec[7], t);
184 
185  newTime = 352;
186  time(freeVec) = newTime;
187  BOOST_CHECK_EQUAL(freeVec[7], newTime);
188  }
189 
190  // same for const
191  {
192  // use temp object to initialize const vectors
193  SpacePointVector tempSpaceVec = {1., 2., 3., 4.};
194  const SpacePointVector spaceTimeVec(tempSpaceVec);
195  const double t1 = time(spaceTimeVec);
196  BOOST_CHECK_EQUAL(spaceTimeVec[3], t1);
197 
198  BoundVector tempBoundVec;
199  tempBoundVec << 1., 2., 3., 4., 5., 6.;
200  const BoundVector boundVec(tempBoundVec);
201  const double t2 = time(boundVec);
202  BOOST_CHECK_EQUAL(boundVec[eT], t2);
203 
204  FreeVector tempFreeVec;
205  tempFreeVec << 1., 2., 3., 4., 5., 6., 7., 8.;
206  const FreeVector freeVec(tempFreeVec);
207  const double t3 = time(freeVec);
208  BOOST_CHECK_EQUAL(freeVec[7], t3);
209  }
210 }
211 
212 BOOST_AUTO_TEST_CASE(position_helper_test) {
213  {
214  SpacePointVector spaceTimeVec = {1., 2., 3., 4.};
215  Vector3D pos = position(spaceTimeVec);
216  BOOST_CHECK_EQUAL(spaceTimeVec.head<3>(), pos);
217 
218  Vector3D newPos = {4., 7., 12.};
219  position(spaceTimeVec) = newPos;
220  BOOST_CHECK_EQUAL(spaceTimeVec.head<3>(), newPos);
221 
222  FreeVector freeVec;
223  freeVec << 1., 2., 3., 4., 5., 6., 7., 8.;
224  pos = position(freeVec);
225  BOOST_CHECK_EQUAL(freeVec.head<3>(), pos);
226 
227  newPos = {14., 17., 112.};
228  position(freeVec) = newPos;
229  BOOST_CHECK_EQUAL(freeVec.head<3>(), newPos);
230  }
231 
232  // same for const
233  {
234  // use temp object to initialize const vectors
235  SpacePointVector tempSpaceVec = {1., 2., 3., 4.};
236  const SpacePointVector spaceTimeVec(tempSpaceVec);
237  const Vector3D pos1 = position(spaceTimeVec);
238  BOOST_CHECK_EQUAL(spaceTimeVec.head<3>(), pos1);
239 
240  FreeVector tempFreeVec;
241  tempFreeVec << 1., 2., 3., 4., 5., 6., 7., 8.;
242  const FreeVector freeVec(tempFreeVec);
243  const Vector3D pos2 = position(freeVec);
244  BOOST_CHECK_EQUAL(freeVec.head<3>(), pos2);
245  }
246 }
247 
248 template <size_t I>
249 struct functor {
250  static constexpr size_t invoke() { return I * I * I; }
251 };
252 
253 BOOST_AUTO_TEST_CASE(test_matrix_dimension_switch) {
254  constexpr size_t imax = 20;
255  for (size_t i = 0; i < imax; i++) {
256  size_t val = template_switch<functor, 0, imax>(i);
257  BOOST_CHECK_EQUAL(val, i * i * i);
258  }
259 }
260 
261 BOOST_AUTO_TEST_SUITE_END()
262 } // namespace Test
263 } // namespace Acts