Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CovarianceTransportTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CovarianceTransportTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
18 
19 #include <memory>
20 #include <optional>
21 #include <tuple>
22 #include <variant>
23 
24 namespace tt = boost::test_tools;
25 
26 namespace Acts {
27 namespace Test {
28 
29 BOOST_AUTO_TEST_CASE(covariance_transport_invalid) {
30  CovarianceCache covCache;
31  BOOST_CHECK(covCache.applyTransport == false);
32 }
33 
34 BOOST_AUTO_TEST_CASE(covariance_transport_bound_start) {
35  // Create a test context
37 
38  Vector3 position{1., 2., 3.};
39  ActsScalar time = 4.;
40  ActsScalar qop = 0.125;
41  Vector3 direction = Vector3(5., 6., 7.).normalized();
42  auto planeSurface = Surface::makeShared<PlaneSurface>(position, direction);
43  auto otherSurface = Surface::makeShared<PlaneSurface>(
44  position, Vector3(6., 7., 8.).normalized());
45 
46  // Free & bound parameters
47  FreeVector freeParameters;
48  freeParameters << position[0], position[1], position[2], time, direction[0],
49  direction[1], direction[2], qop;
50 
51  BoundVector boundParameters;
52  boundParameters << 0., 0., VectorHelpers::phi(direction),
53  VectorHelpers::theta(direction), qop, time;
54 
55  BoundSquareMatrix boundCovariance = 8. * BoundSquareMatrix::Identity();
56 
57  CovarianceCache covCache(tgContext, *planeSurface, position, boundParameters,
58  boundCovariance);
59  // Test that the transport should be applied now
60  BOOST_CHECK(covCache.applyTransport == true);
61  // Test that the set covariance is 5x5 and what it has been set
62  BOOST_CHECK_EQUAL(std::get<BoundSquareMatrix>(covCache.covariance),
63  boundCovariance);
64  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(covCache.covariance),
65  std::bad_variant_access);
66  // Test that the bound to free jacobian has a value
67  BOOST_CHECK(covCache.boundToFreeJacobian.has_value());
68  BOOST_CHECK(not covCache.boundToFreeJacobian.value().isApprox(
69  BoundToFreeMatrix::Zero()));
70  // Test that the free transport jacobian, derivative is properly set up
71  BOOST_CHECK_EQUAL(covCache.freeTransportJacobian, FreeMatrix::Identity());
72  BOOST_CHECK_EQUAL(covCache.freeToPathDerivatives, FreeVector::Zero());
73  // Check that the surface is set
74  BOOST_CHECK_EQUAL(covCache.atSurface, planeSurface.get());
75  BOOST_CHECK_EQUAL(covCache.atPosition, position);
76 
77  // Transport bound to the same bound surface
78  auto covJacAtBound = transportCovarianceToBound(tgContext, *planeSurface,
79  freeParameters, covCache);
80  BOOST_CHECK(boundCovariance.isApprox(
81  std::get<BoundSquareMatrix>(covCache.covariance)));
82  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(covCache.covariance),
83  std::bad_variant_access);
84 
85  auto variantCovariance = std::get<0>(covJacAtBound);
86  auto variantJacobian = std::get<1>(covJacAtBound);
87  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
88  std::bad_variant_access);
89 
90  // Transport bound to curvilinear on the same place
91  auto covJacAtCurv = transportCovarianceToCurvilinear(direction, covCache);
92  BOOST_CHECK(boundCovariance.isApprox(
93  std::get<BoundSquareMatrix>(covCache.covariance)));
94  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(covCache.covariance),
95  std::bad_variant_access);
96 
97  variantCovariance = std::get<0>(covJacAtCurv);
98  variantJacobian = std::get<1>(covJacAtCurv);
99  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
100  std::bad_variant_access);
101 
102  // Transport bound to Free on the same place
103  auto covJacAtFree = transportCovarianceToFree(covCache);
104  variantCovariance = std::get<0>(covJacAtFree);
105  variantJacobian = std::get<1>(covJacAtFree);
106  BOOST_CHECK_THROW(std::get<BoundSquareMatrix>(variantCovariance),
107  std::bad_variant_access);
108 
109  // Transport bound to another bound surface
110  covJacAtBound = transportCovarianceToBound(tgContext, *otherSurface,
111  freeParameters, covCache);
112 
113  variantCovariance = std::get<0>(covJacAtBound);
114  variantJacobian = std::get<1>(covJacAtBound);
115  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
116  std::bad_variant_access);
117 }
118 
119 BOOST_AUTO_TEST_CASE(covariance_transport_curvilinear_start) {
120  // Create a test context
122 
123  Vector3 position{1., 2., 3.};
124  Vector3 direction = Vector3(5., 6., 7.).normalized();
125  ActsScalar time = 4.;
126  ActsScalar qop = 0.125;
127 
128  // Free & bound parameters
129  FreeVector freeParameters;
130  freeParameters << position[0], position[1], position[2], time, direction[0],
131  direction[1], direction[2], qop;
132 
133  auto planeSurface = Surface::makeShared<PlaneSurface>(position, direction);
134 
135  BoundSquareMatrix boundCovariance = 8. * BoundSquareMatrix::Identity();
136 
137  CovarianceCache covCache(position, direction, boundCovariance);
138  // Test that the transport should be applied now
139  BOOST_CHECK(covCache.applyTransport == true);
140  // Test that the set covariance is 5x5 and what it has been set
141  BOOST_CHECK_EQUAL(std::get<BoundSquareMatrix>(covCache.covariance),
142  boundCovariance);
143  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(covCache.covariance),
144  std::bad_variant_access);
145 
146  // Test that the bound to free jacobian has a value
147  BOOST_CHECK(covCache.boundToFreeJacobian.has_value());
148  BOOST_CHECK(not covCache.boundToFreeJacobian.value().isApprox(
149  BoundToFreeMatrix::Zero()));
150  // Test that the free transport jacobian, derivative is properly set up
151  BOOST_CHECK_EQUAL(covCache.freeTransportJacobian, FreeMatrix::Identity());
152  BOOST_CHECK_EQUAL(covCache.freeToPathDerivatives, FreeVector::Zero());
153  // Check that the surface is set
154  BOOST_CHECK_EQUAL(covCache.atSurface, nullptr);
155  BOOST_CHECK_EQUAL(covCache.atPosition, position);
156 
157  // Transport bound to the same bound surface
158  auto covJacAtBound = transportCovarianceToBound(tgContext, *planeSurface,
159  freeParameters, covCache);
160  auto variantCovariance = std::get<0>(covJacAtBound);
161  auto variantJacobian = std::get<1>(covJacAtBound);
162  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
163  std::bad_variant_access);
164 
165  // Transport bound to curvilinear on the same place
166  auto covJacAtCurv = transportCovarianceToCurvilinear(direction, covCache);
167  variantCovariance = std::get<0>(covJacAtCurv);
168  variantJacobian = std::get<1>(covJacAtCurv);
169  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
170  std::bad_variant_access);
171 
172  // Transport bound to Free on the same place
173  auto covJacAtFree = transportCovarianceToFree(covCache);
174  variantCovariance = std::get<0>(covJacAtFree);
175  variantJacobian = std::get<1>(covJacAtFree);
176  BOOST_CHECK_THROW(std::get<BoundSquareMatrix>(variantCovariance),
177  std::bad_variant_access);
178 }
179 
180 BOOST_AUTO_TEST_CASE(covariance_transport_free_start) {
181  // Create a test context
183 
184  // Some start parameters
185  Vector3 position{1., 2., 3.};
186  ActsScalar time = 4.;
187  Vector3 direction = Vector3(5., 6., 7.).normalized();
188 
189  auto planeSurface = Surface::makeShared<PlaneSurface>(position, direction);
190 
191  ActsScalar qop = 0.125;
192  FreeVector freeParameters;
193  freeParameters << position[0], position[1], position[2], time, direction[0],
194  direction[1], direction[2], qop;
195 
196  FreeSquareMatrix freeCovariance = 8. * FreeSquareMatrix::Identity();
197 
198  CovarianceCache covCache(freeParameters, freeCovariance);
199  BOOST_CHECK(covCache.applyTransport == true);
200  // Test that the set covariance is 5x5 and what it has been set
201  BOOST_CHECK_THROW(std::get<BoundSquareMatrix>(covCache.covariance),
202  std::bad_variant_access);
203  BOOST_CHECK_EQUAL(std::get<FreeSquareMatrix>(covCache.covariance),
204  freeCovariance);
205  // Test that the bound to free jacobian has NO value
206  BOOST_CHECK(not covCache.boundToFreeJacobian.has_value());
207  // Test that the free transport jacobian, derivative is properly set up
208  BOOST_CHECK_EQUAL(covCache.freeTransportJacobian, FreeMatrix::Identity());
209  BOOST_CHECK_EQUAL(covCache.freeToPathDerivatives, FreeVector::Zero());
210  // Check that the surface is NOT set
211  BOOST_CHECK_EQUAL(covCache.atSurface, nullptr);
212  BOOST_CHECK_EQUAL(covCache.atPosition, position);
213 
214  // Transport bound to the same bound surface
215  auto covJacAtBound = transportCovarianceToBound(tgContext, *planeSurface,
216  freeParameters, covCache);
217 
218  auto variantCovariance = std::get<0>(covJacAtBound);
219  auto variantJacobian = std::get<1>(covJacAtBound);
220  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
221  std::bad_variant_access);
222 
223  // Transport bound to curvilinear on the same place
224  auto covJacAtCurv = transportCovarianceToCurvilinear(direction, covCache);
225  variantCovariance = std::get<0>(covJacAtCurv);
226  variantJacobian = std::get<1>(covJacAtCurv);
227  BOOST_CHECK_THROW(std::get<FreeSquareMatrix>(variantCovariance),
228  std::bad_variant_access);
229 
230  // Transport bound to Free on the same place
231  auto covJacAtFree = transportCovarianceToFree(covCache);
232  variantCovariance = std::get<0>(covJacAtFree);
233  variantJacobian = std::get<1>(covJacAtFree);
234  BOOST_CHECK_THROW(std::get<BoundSquareMatrix>(variantCovariance),
235  std::bad_variant_access);
236 }
237 
238 } // namespace Test
239 } // namespace Acts