Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignmentHelperTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlignmentHelperTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 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/unit_test.hpp>
10 
14 
15 #include <algorithm>
16 #include <cmath>
17 #include <utility>
18 
19 namespace Acts {
20 namespace Test {
21 
24 BOOST_AUTO_TEST_CASE(alignment_helper_test) {
25  // (a) Test with non-identity rotation matrix
26  // Rotation angle parameters
27  const double alpha = M_PI;
28  const double beta = 0;
29  const double gamma = M_PI / 2;
30  // rotation around x axis
31  AngleAxis3 rotX(alpha, Vector3(1., 0., 0.));
32  // rotation around y axis
33  AngleAxis3 rotY(beta, Vector3(0., 1., 0.));
34  // rotation around z axis
35  AngleAxis3 rotZ(gamma, Vector3(0., 0., 1.));
36  double sz = std::sin(gamma);
37  double cz = std::cos(gamma);
38  double sy = std::sin(beta);
39  double cy = std::cos(beta);
40  double sx = std::sin(alpha);
41  double cx = std::cos(alpha);
42 
43  // Calculate the expected rotation matrix for rotZ * rotY * rotX,
44  // (i.e. first rotation around x axis, then y axis, last z axis):
45  // [ cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*sy ]
46  // [ cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx ]
47  // [ -sy cy*sx cy*cx ]
48  RotationMatrix3 expRot = RotationMatrix3::Zero();
49  expRot.col(0) = Vector3(cz * cy, cy * sz, -sy);
50  expRot.col(1) =
51  Vector3(cz * sy * sx - cx * sz, cz * cx + sz * sy * sx, cy * sx);
52  expRot.col(2) =
53  Vector3(sz * sx + cz * cx * sy, cx * sz * sy - cz * sx, cy * cx);
54 
55  // Calculate the expected derivative of local x axis to its rotation
56  RotationMatrix3 expRotToXAxis = RotationMatrix3::Zero();
57  expRotToXAxis.col(0) = Vector3(0, 0, 0);
58  expRotToXAxis.col(1) = Vector3(-cz * sy, -sz * sy, -cy);
59  expRotToXAxis.col(2) = Vector3(-sz * cy, cz * cy, 0);
60 
61  // Calculate the expected derivative of local y axis to its rotation
62  RotationMatrix3 expRotToYAxis = RotationMatrix3::Zero();
63  expRotToYAxis.col(0) =
64  Vector3(cz * sy * cx + sz * sx, sz * sy * cx - cz * sx, cy * cx);
65  expRotToYAxis.col(1) = Vector3(cz * cy * sx, sz * cy * sx, -sy * sx);
66  expRotToYAxis.col(2) =
67  Vector3(-sz * sy * sx - cz * cx, cz * sy * sx - sz * cx, 0);
68 
69  // Calculate the expected derivative of local z axis to its rotation
70  RotationMatrix3 expRotToZAxis = RotationMatrix3::Zero();
71  expRotToZAxis.col(0) =
72  Vector3(sz * cx - cz * sy * sx, -sz * sy * sx - cz * cx, -cy * sx);
73  expRotToZAxis.col(1) = Vector3(cz * cy * cx, sz * cy * cx, -sy * cx);
74  expRotToZAxis.col(2) =
75  Vector3(cz * sx - sz * sy * cx, cz * sy * cx + sz * sx, 0);
76 
77  // Construct a transform
78  Translation3 translation(Vector3(0., 0., 0.));
79  Transform3 transform(translation);
80  // Rotation with rotZ * rotY * rotX
81  transform *= rotZ;
82  transform *= rotY;
83  transform *= rotX;
84  // Get the rotation of the transform
85  const auto rotation = transform.rotation();
86 
87  // Check if the rotation matrix is as expected
88  CHECK_CLOSE_ABS(rotation, expRot, 1e-15);
89 
90  // Call the alignment helper to calculate the derivative of local frame axes
91  // w.r.t its rotation
92  const auto& [rotToLocalXAxis, rotToLocalYAxis, rotToLocalZAxis] =
94 
95  // Check if the derivative for local x axis is as expected
96  CHECK_CLOSE_ABS(rotToLocalXAxis, expRotToXAxis, 1e-15);
97 
98  // Check if the derivative for local y axis is as expected
99  CHECK_CLOSE_ABS(rotToLocalYAxis, expRotToYAxis, 1e-15);
100 
101  // Check if the derivative for local z axis is as expected
102  CHECK_CLOSE_ABS(rotToLocalZAxis, expRotToZAxis, 1e-15);
103 
104  // (b) Test with identity rotation matrix
105  RotationMatrix3 iRotation = RotationMatrix3::Identity();
106 
107  // Call the alignment helper to calculate the derivative of local frame axes
108  // w.r.t its rotation
109  const auto& [irotToLocalXAxis, irotToLocalYAxis, irotToLocalZAxis] =
111 
112  // The expected derivatives
113  expRotToXAxis << 0, 0, 0, 0, 0, 1, 0, -1, 0;
114  expRotToYAxis << 0, 0, -1, 0, 0, 0, 1, 0, 0;
115  expRotToZAxis << 0, 1, 0, -1, 0, 0, 0, 0, 0;
116 
117  // Check if the derivative for local x axis is as expected
118  CHECK_CLOSE_ABS(irotToLocalXAxis, expRotToXAxis, 1e-15);
119 
120  // Check if the derivative for local y axis is as expected
121  CHECK_CLOSE_ABS(irotToLocalYAxis, expRotToYAxis, 1e-15);
122 
123  // Check if the derivative for local z axis is as expected
124  CHECK_CLOSE_ABS(irotToLocalZAxis, expRotToZAxis, 1e-15);
125 }
126 } // namespace Test
127 } // namespace Acts