Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeasurementTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MeasurementTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2020 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/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
19 
20 #include <algorithm>
21 #include <array>
22 #include <cmath>
23 #include <limits>
24 #include <random>
25 #include <tuple>
26 #include <utility>
27 #include <variant>
28 #include <vector>
29 
30 using namespace Acts;
31 using namespace Acts::Test;
33 namespace bd = boost::unit_test::data;
34 
35 namespace {
36 constexpr BoundIndices boundIndices[] = {
38 };
39 constexpr FreeIndices freeIndices[] = {
42 };
43 const TestSourceLink sourceOrig;
44 const Acts::SourceLink source{sourceOrig};
45 // fix seed for reproducible tests
46 std::default_random_engine rng(123);
47 } // namespace
48 
49 // the underlying subspace implementation is already tested exhaustively in a
50 // separate unit test. here we only test concrete extreme cases and
51 // measurement-specific functionality.
52 
53 BOOST_AUTO_TEST_SUITE(EventDataMeasurement)
54 
55 BOOST_DATA_TEST_CASE(FixedBoundOne, bd::make(boundIndices), index) {
56  auto [params, cov] = generateParametersCovariance<ActsScalar, 1u>(rng);
57  auto meas = makeMeasurement(source, params, cov, index);
58 
59  BOOST_CHECK_EQUAL(meas.size(), 1);
60  for (auto i : boundIndices) {
61  if (i == index) {
62  BOOST_CHECK(meas.contains(i));
63  } else {
64  BOOST_CHECK(not meas.contains(i));
65  }
66  }
67  BOOST_CHECK_EQUAL(meas.parameters(), params);
68  BOOST_CHECK_EQUAL(meas.covariance(), cov);
69  BOOST_CHECK_EQUAL(meas.sourceLink().template get<TestSourceLink>(),
70  sourceOrig);
71 }
72 
73 BOOST_AUTO_TEST_CASE(FixedBoundAll) {
74  auto [params, cov] = generateBoundParametersCovariance(rng);
75  auto meas = makeMeasurement(source, params, cov, eBoundLoc0, eBoundLoc1,
77 
78  BOOST_CHECK_EQUAL(meas.size(), eBoundSize);
79  for (auto i : boundIndices) {
80  BOOST_CHECK(meas.contains(i));
81  }
82  BOOST_CHECK_EQUAL(meas.parameters(), params);
83  BOOST_CHECK_EQUAL(meas.covariance(), cov);
84  BOOST_CHECK_EQUAL(meas.sourceLink().get<TestSourceLink>(), sourceOrig);
85 }
86 
87 namespace {
88 // example data for phi residual tests. each entry contains
89 //
90 // measured, reference, expected residual
91 //
92 const std::vector<std::tuple<double, double, double>> kPhiDataset = {
93  // measurement and reference in bounds and close
94  {0.5, 0.75, -0.25},
95  // measurement and reference in bounds but at different edges
96  {0.25, 2 * M_PI - 0.25, 0.5},
97  {2 * M_PI - 0.125, 0.125, -0.25},
98  // measurement in bounds, reference ouf-of-bounds, both near lower edge
99  {0.25, -0.25, 0.5},
100  // measurement in bounds, reference ouf-of-bounds, both near upper edge
101  {2 * M_PI - 0.25, 2 * M_PI + 0.25, -0.5},
102  // measurement out-of-bounds, reference in bounds, both near lower edge
103  {-0.25, 0.25, -0.5},
104  // measurement out-of-bounds, reference in bounds, both near upper edge
105  {2 * M_PI + 0.25, 2 * M_PI - 0.25, 0.5},
106 };
107 } // namespace
108 
109 BOOST_DATA_TEST_CASE(BoundResidualsPhi, bd::make(kPhiDataset), phiMea, phiRef,
110  phiRes) {
111  using MeasurementVector = Acts::ActsVector<1>;
112  using MeasurementCovariance = Acts::ActsSquareMatrix<1>;
113 
114  // prepare measurement
115  MeasurementVector params = MeasurementVector::Zero();
116  MeasurementCovariance cov = MeasurementCovariance::Zero();
117  params[0] = phiMea;
118  auto measurement = makeMeasurement(source, params, cov, eBoundPhi);
119  // prepare reference parameters
120  Acts::BoundVector reference = Acts::BoundVector::Zero();
121  reference[eBoundPhi] = phiRef;
122 
123  // compute and check residual
124  auto res = measurement.residuals(reference);
125  CHECK_CLOSE_ABS(res[0], phiRes, std::numeric_limits<ActsScalar>::epsilon());
126 }
127 
128 BOOST_DATA_TEST_CASE(FixedFreeOne, bd::make(freeIndices), index) {
129  auto [params, cov] = generateParametersCovariance<ActsScalar, 1u>(rng);
130  auto meas = makeMeasurement(source, params, cov, index);
131 
132  BOOST_CHECK_EQUAL(meas.size(), 1);
133  for (auto i : freeIndices) {
134  if (i == index) {
135  BOOST_CHECK(meas.contains(i));
136  } else {
137  BOOST_CHECK(not meas.contains(i));
138  }
139  }
140  BOOST_CHECK_EQUAL(meas.parameters(), params);
141  BOOST_CHECK_EQUAL(meas.covariance(), cov);
142  BOOST_CHECK_EQUAL(meas.sourceLink().template get<TestSourceLink>(),
143  sourceOrig);
144 
145  // all free parameters are unrestricted and we know the expected residual.
146  constexpr auto tol = std::numeric_limits<ActsScalar>::epsilon();
147  auto [ref, refCov] = generateFreeParametersCovariance(rng);
148  auto res = meas.residuals(ref);
149  CHECK_CLOSE_ABS(res[0], params[0] - ref[index], tol);
150 }
151 
152 BOOST_AUTO_TEST_CASE(FixedFreeAll) {
153  auto [params, cov] = generateFreeParametersCovariance(rng);
154  auto meas =
157 
158  BOOST_CHECK_EQUAL(meas.size(), eFreeSize);
159  for (auto i : freeIndices) {
160  BOOST_CHECK(meas.contains(i));
161  }
162  BOOST_CHECK_EQUAL(meas.parameters(), params);
163  BOOST_CHECK_EQUAL(meas.covariance(), cov);
164  BOOST_CHECK_EQUAL(meas.sourceLink().get<TestSourceLink>(), sourceOrig);
165 
166  // all free parameters are unrestricted and we know the expected residual.
167  constexpr auto tol = std::numeric_limits<ActsScalar>::epsilon();
168  auto [ref, refCov] = generateFreeParametersCovariance(rng);
169  CHECK_CLOSE_ABS(meas.residuals(ref), params - ref, tol);
170 }
171 
172 BOOST_AUTO_TEST_CASE(VariantBound) {
173  // generate w/ a single parameter
174  auto [par1, cov1] = generateParametersCovariance<ActsScalar, 1u>(rng);
176  makeMeasurement(source, par1, cov1, eBoundTheta);
177  std::visit(
178  [](const auto& m) {
179  BOOST_CHECK_EQUAL(m.size(), 1);
180  BOOST_CHECK(not m.contains(eBoundLoc0));
181  BOOST_CHECK(not m.contains(eBoundLoc1));
182  BOOST_CHECK(not m.contains(eBoundTime));
183  BOOST_CHECK(not m.contains(eBoundPhi));
184  BOOST_CHECK(m.contains(eBoundTheta));
185  BOOST_CHECK(not m.contains(eBoundQOverP));
186  },
187  meas);
188 
189  // reassign w/ all parameters
190  auto [parN, covN] = generateBoundParametersCovariance(rng);
191  meas = makeMeasurement(source, parN, covN, eBoundLoc0, eBoundLoc1, eBoundPhi,
193  std::visit(
194  [](const auto& m) {
195  BOOST_CHECK_EQUAL(m.size(), eBoundSize);
196  BOOST_CHECK(m.contains(eBoundLoc0));
197  BOOST_CHECK(m.contains(eBoundLoc1));
198  BOOST_CHECK(m.contains(eBoundTime));
199  BOOST_CHECK(m.contains(eBoundPhi));
200  BOOST_CHECK(m.contains(eBoundTheta));
201  BOOST_CHECK(m.contains(eBoundQOverP));
202  },
203  meas);
204 }
205 
206 BOOST_AUTO_TEST_CASE(VariantFree) {
207  // generate w/ two parameters
208  auto [par2, cov2] = generateParametersCovariance<ActsScalar, 2u>(rng);
210  makeMeasurement(source, par2, cov2, eFreePos2, eFreeTime);
211  std::visit(
212  [](const auto& m) {
213  BOOST_CHECK_EQUAL(m.size(), 2);
214  BOOST_CHECK(not m.contains(eFreePos0));
215  BOOST_CHECK(not m.contains(eFreePos1));
216  BOOST_CHECK(m.contains(eFreePos2));
217  BOOST_CHECK(m.contains(eFreeTime));
218  BOOST_CHECK(not m.contains(eFreeDir0));
219  BOOST_CHECK(not m.contains(eFreeDir1));
220  BOOST_CHECK(not m.contains(eFreeDir2));
221  BOOST_CHECK(not m.contains(eFreeQOverP));
222  },
223  meas);
224 
225  // reassign w/ all parameters
226  auto [parN, covN] = generateFreeParametersCovariance(rng);
227  meas =
230  std::visit(
231  [](const auto& m) {
232  BOOST_CHECK_EQUAL(m.size(), eFreeSize);
233  BOOST_CHECK(m.contains(eFreePos0));
234  BOOST_CHECK(m.contains(eFreePos1));
235  BOOST_CHECK(m.contains(eFreePos2));
236  BOOST_CHECK(m.contains(eFreeTime));
237  BOOST_CHECK(m.contains(eFreeDir0));
238  BOOST_CHECK(m.contains(eFreeDir1));
239  BOOST_CHECK(m.contains(eFreeDir2));
240  BOOST_CHECK(m.contains(eFreeQOverP));
241  },
242  meas);
243 }
244 
245 BOOST_AUTO_TEST_SUITE_END()