EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LineSurfaceTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file LineSurfaceTests.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/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
20 
21 #include <limits>
22 
23 namespace utf = boost::unit_test;
24 
25 namespace Acts {
26 
27 namespace Test {
28 
29 // Create a test context
31 
32 // using boost::test_tools::output_test_stream;
33 
34 BOOST_AUTO_TEST_SUITE(Surfaces)
36 BOOST_AUTO_TEST_CASE(LineSurface_Constructors_test) {
37  // Default ctor is deleted
38  // LineSurfaceStub l;
39  // ctor with translation, radius, halfz
40  Translation3D translation{0., 1., 2.};
41  Transform3D transform(translation);
42  auto pTransform = Transform3D(translation);
43  const double radius{2.0}, halfz{20.};
44  BOOST_CHECK(LineSurfaceStub(pTransform, radius, halfz).constructedOk());
45  // ctor with nullptr for LineBounds
46  BOOST_CHECK(LineSurfaceStub(pTransform).constructedOk());
47  // ctor with LineBounds
48  auto pLineBounds = std::make_shared<const LineBounds>(2., 10.0);
49  BOOST_CHECK(LineSurfaceStub(pTransform, pLineBounds).constructedOk());
50  // ctor with LineBounds, detector element, Identifier
51  auto pMaterial =
52  std::make_shared<const HomogeneousSurfaceMaterial>(makePercentSlab());
53  DetectorElementStub detElement{pTransform, pLineBounds, 0.2, pMaterial};
54  BOOST_CHECK(LineSurfaceStub(pLineBounds, detElement).constructedOk());
55  LineSurfaceStub lineToCopy(pTransform, 2.0, 20.);
56  // Copy ctor
57  BOOST_CHECK(LineSurfaceStub(lineToCopy).constructedOk());
58  // Copied and transformed ctor
59  BOOST_CHECK(
60  LineSurfaceStub(tgContext, lineToCopy, transform).constructedOk());
61 
63  DetectorElementStub detElem;
64  BOOST_CHECK_THROW(LineSurfaceStub nullBounds(nullptr, detElem),
66 
67  BOOST_TEST_MESSAGE(
68  "All LineSurface constructors are callable without problem");
69 }
71 BOOST_AUTO_TEST_CASE(LineSurface_allNamedMethods_test) {
72  // binningPosition()
73  Translation3D translation{0., 1., 2.};
74  Transform3D transform(translation);
75  LineSurfaceStub line(transform, 2.0, 20.);
76  Vector3D referencePosition{0., 1., 2.};
77  CHECK_CLOSE_ABS(referencePosition, line.binningPosition(tgContext, binX),
78  1e-6);
79  //
80  // bounds()
81  auto pLineBounds = std::make_shared<const LineBounds>(2., 10.0);
82  LineSurfaceStub boundedLine(transform, pLineBounds);
83  const LineBounds& bounds =
84  dynamic_cast<const LineBounds&>(boundedLine.bounds());
85  BOOST_CHECK_EQUAL(bounds, LineBounds(2., 10.0));
86  //
87  // globalToLocal()
88  Vector3D gpos{0., 1., 0.};
89  const Vector3D mom{20., 0., 0.}; // needs more realistic parameters
90  Vector2D localPosition = line.globalToLocal(tgContext, gpos, mom).value();
91  const Vector2D expectedResult{0, -2};
92  CHECK_CLOSE_ABS(expectedResult, localPosition, 1e-6);
93  //
94  // intersection
95  const Vector3D direction{0., 1., 2.};
96  BoundaryCheck bcheck(false);
97  auto sfIntersection =
98  line.intersect(tgContext, {0., 0., 0.}, direction.normalized(), bcheck);
99  BOOST_CHECK(bool(sfIntersection));
100  Vector3D expectedIntersection(0, 1., 2.);
101  CHECK_CLOSE_ABS(sfIntersection.intersection.position, expectedIntersection,
102  1e-6); // need more tests..
103  BOOST_CHECK_EQUAL(sfIntersection.object, &line);
104  //
105  // isOnSurface
106  const Vector3D insidePosition{0., 2.5, 0.};
107  BOOST_CHECK(line.isOnSurface(tgContext, insidePosition, mom,
108  false)); // need better test here
109  const Vector3D outsidePosition{100., 100., 200.};
110  BOOST_CHECK(!line.isOnSurface(tgContext, outsidePosition, mom, true));
111  //
112  // localToGlobal
113  Vector3D returnedGlobalPosition{0., 0., 0.};
114  // Vector2D localPosition{0., 0.};
115  const Vector3D momentum{300., 200., 0.}; // find better values!
116  returnedGlobalPosition =
117  line.localToGlobal(tgContext, localPosition, momentum);
118  const Vector3D expectedGlobalPosition{0, 1, 0};
119  CHECK_CLOSE_ABS(returnedGlobalPosition, expectedGlobalPosition, 1e-6);
120  //
121  // referenceFrame
122  Vector3D globalPosition{0., 0., 0.};
123  auto returnedRotationMatrix =
124  line.referenceFrame(tgContext, globalPosition, momentum);
125  double v0 = std::cos(std::atan(2. / 3.));
126  double v1 = std::sin(std::atan(2. / 3.));
127  RotationMatrix3D expectedRotationMatrix;
128  expectedRotationMatrix << -v1, 0., v0, v0, 0., v1, 0., 1., -0.;
129  // std::cout<<returnedRotationMatrix<<std::endl;
130  // std::cout<<expectedRotationMatrix<<std::endl;
131  CHECK_CLOSE_OR_SMALL(returnedRotationMatrix, expectedRotationMatrix, 1e-6,
132  1e-9);
133  //
134  // name()
135  boost::test_tools::output_test_stream output;
136  output << line.name();
137  BOOST_CHECK(output.is_equal("Acts::LineSurface"));
138  //
139  // normal
140  Vector3D normalVector{0., 0., 1.}; // line direction is same as normal????
141  CHECK_CLOSE_ABS(line.normal(tgContext), normalVector, 1e-6);
142  //
143  // pathCorrection
144  auto any3DVector = normalVector;
145  CHECK_CLOSE_REL(line.pathCorrection(tgContext, any3DVector, any3DVector), 1.,
146  1e-6);
147 }
149 BOOST_AUTO_TEST_CASE(LineSurface_assignment_test) {
150  Translation3D translation{0., 1., 2.};
151  Transform3D transform(translation);
152  LineSurfaceStub originalLine(transform, 2.0, 20.);
153  LineSurfaceStub assignedLine(transform, 1.0, 1.0);
154  BOOST_CHECK(assignedLine != originalLine); // operator != from base
155  assignedLine = originalLine;
156  BOOST_CHECK(assignedLine == originalLine); // operator == from base
157 }
158 
160 BOOST_AUTO_TEST_CASE(LineSurfaceAlignment) {
161  Translation3D translation{0., 1., 2.};
162  Transform3D transform(translation);
163  LineSurfaceStub line(transform, 2.0, 20.);
164 
165  const auto& rotation = transform.rotation();
166  // The local frame z axis
167  const Vector3D localZAxis = rotation.col(2);
168  // Check the local z axis is aligned to global z axis
169  CHECK_CLOSE_ABS(localZAxis, Vector3D(0., 0., 1.), 1e-15);
170 
172  Vector3D globalPosition{1, 2, 4};
173  Vector3D momentum{-1, 1, 1};
174  Vector3D direction = momentum.normalized();
175 
176  // Call the function to calculate the derivative of local frame axes w.r.t its
177  // rotation
178  const auto& [rotToLocalXAxis, rotToLocalYAxis, rotToLocalZAxis] =
180 
181  // (a) Test the derivative of path length w.r.t. alignment parameters
182  const AlignmentRowVector& alignToPath = line.alignmentToPathDerivative(
183  tgContext, rotToLocalZAxis, globalPosition, direction);
184  // The expected results
185  AlignmentRowVector expAlignToPath = AlignmentRowVector::Zero();
186  const double value = std::sqrt(3) / 2;
187  expAlignToPath << -value, value, 0, -3 * value, -value, 0;
188  // Check if the calculated derivative is as expected
189  CHECK_CLOSE_ABS(alignToPath, expAlignToPath, 1e-10);
190 
191  // (b) Test the derivative of bound track parameters local position w.r.t.
192  // position in local 3D Cartesian coordinates
193  const auto& loc3DToLocBound =
194  line.localCartesianToBoundLocalDerivative(tgContext, globalPosition);
195  // Check if the result is as expected
196  LocalCartesianToBoundLocalMatrix expLoc3DToLocBound =
197  LocalCartesianToBoundLocalMatrix::Zero();
198  expLoc3DToLocBound << 1 / std::sqrt(2), 1 / std::sqrt(2), 0, 0, 0, 1;
199  CHECK_CLOSE_ABS(loc3DToLocBound, expLoc3DToLocBound, 1e-10);
200 }
201 
202 BOOST_AUTO_TEST_SUITE_END()
203 
204 } // namespace Test
205 
206 } // namespace Acts