ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignmentContextTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlignmentContextTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-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 <array>
12 #include <memory>
13 
20 #include "Acts/Utilities/Units.hpp"
21 
22 using namespace Acts::UnitLiterals;
23 
24 namespace Acts {
25 namespace Test {
26 
30  std::shared_ptr<const std::array<Transform3D, 2>> alignmentStore = nullptr;
31 
33  unsigned int alignmentIndex{0};
34 
37 
39  AlignmentContext(std::shared_ptr<const std::array<Transform3D, 2>> aStore,
40  unsigned int aIndex = 0)
41  : alignmentStore(std::move(aStore)), alignmentIndex(aIndex) {}
42 };
43 
49  public:
50  // Deleted default constructor
51  AlignableDetectorElement() = delete;
52 
59  AlignableDetectorElement(std::shared_ptr<const Transform3D> transform,
60  std::shared_ptr<const PlanarBounds> pBounds,
61  double thickness)
63  m_elementTransform(std::move(transform)),
64  m_elementThickness(thickness) {
65  auto mutableSurface = Surface::makeShared<PlaneSurface>(pBounds, *this);
66  m_elementSurface = mutableSurface;
67  }
68 
70  ~AlignableDetectorElement() override { /*nop */
71  }
72 
78  const Transform3D& transform(const GeometryContext& gctx) const override;
79 
81  const Surface& surface() const override;
82 
84  double thickness() const override;
85 
86  private:
88  std::shared_ptr<const Transform3D> m_elementTransform;
90  std::shared_ptr<const Surface> m_elementSurface{nullptr};
92  double m_elementThickness{0.};
93 };
94 
96  const GeometryContext& gctx) const {
97  auto alignContext = std::any_cast<AlignmentContext>(gctx);
98  if (alignContext.alignmentStore != nullptr and
99  alignContext.alignmentIndex < 2) {
100  return (*(alignContext.alignmentStore))[alignContext.alignmentIndex];
101  }
102  return (*m_elementTransform);
103 }
104 
106  return *m_elementSurface;
107 }
108 
109 inline double AlignableDetectorElement::thickness() const {
110  return m_elementThickness;
111 }
112 
114 BOOST_AUTO_TEST_CASE(AlignmentContextTests) {
115  // The nominal and alingments
116  Vector3D nominalCenter(0., 0., 0.);
117  Vector3D negativeCenter(0., 0., -1.);
118  Vector3D positiveCenter(0., 0., 1.);
119 
120  // Checkpoints
121  Vector3D onNominal(3., 3., 0.);
122  Vector3D onNegative(3., 3., -1.);
123  Vector3D onPositive(3., 3., 1.);
124 
125  // Local position
126  Vector2D localPosition(3., 3.);
127 
128  // A position place holder and dymmy momentum
129  Vector3D globalPosition(100_cm, 100_cm, 100_cm);
130  Vector3D dummyMomentum(4., 4., 4.);
131 
132  Transform3D negativeTransform = Transform3D::Identity();
133  negativeTransform.translation() = negativeCenter;
134 
135  Transform3D positiveTransform = Transform3D::Identity();
136  positiveTransform.translation() = positiveCenter;
137 
138  std::array<Transform3D, 2> alignmentArray = {negativeTransform,
139  positiveTransform};
140 
141  std::shared_ptr<const std::array<Transform3D, 2>> alignmentStore =
142  std::make_shared<const std::array<Transform3D, 2>>(alignmentArray);
143 
144  // The detector element at nominal position
145  AlignableDetectorElement alignedElement(
146  std::make_shared<const Transform3D>(Transform3D::Identity()),
147  std::make_shared<const RectangleBounds>(100_cm, 100_cm), 1_mm);
148 
149  const auto& alignedSurface = alignedElement.surface();
150 
151  // The alignment centexts
152  AlignmentContext defaultContext{};
153  AlignmentContext negativeContext(alignmentStore, 0);
154  AlignmentContext positiveContext(alignmentStore, 1);
155 
156  // Test the transforms
157  BOOST_CHECK(alignedSurface.transform(defaultContext)
158  .isApprox(Transform3D::Identity()));
159  BOOST_CHECK(
160  alignedSurface.transform(negativeContext).isApprox(negativeTransform));
161  BOOST_CHECK(
162  alignedSurface.transform(positiveContext).isApprox(positiveTransform));
163 
164  // Test the centers
165  BOOST_CHECK_EQUAL(alignedSurface.center(defaultContext), nominalCenter);
166  BOOST_CHECK_EQUAL(alignedSurface.center(negativeContext), negativeCenter);
167  BOOST_CHECK_EQUAL(alignedSurface.center(positiveContext), positiveCenter);
168 
169  // Test OnSurface
170  BOOST_CHECK(
171  alignedSurface.isOnSurface(defaultContext, onNominal, dummyMomentum));
172  BOOST_CHECK(
173  alignedSurface.isOnSurface(negativeContext, onNegative, dummyMomentum));
174  BOOST_CHECK(
175  alignedSurface.isOnSurface(positiveContext, onPositive, dummyMomentum));
176 
177  // Test local to Global and vice versa
178  alignedSurface.localToGlobal(defaultContext, localPosition, dummyMomentum,
179  globalPosition);
180  BOOST_CHECK_EQUAL(globalPosition, onNominal);
181  alignedSurface.globalToLocal(defaultContext, onNominal, dummyMomentum,
182  localPosition);
183  BOOST_CHECK_EQUAL(localPosition, Vector2D(3., 3.));
184 
185  alignedSurface.localToGlobal(negativeContext, localPosition, dummyMomentum,
186  globalPosition);
187  BOOST_CHECK_EQUAL(globalPosition, onNegative);
188  alignedSurface.globalToLocal(negativeContext, onNegative, dummyMomentum,
189  localPosition);
190  BOOST_CHECK_EQUAL(localPosition, Vector2D(3., 3.));
191 
192  alignedSurface.localToGlobal(positiveContext, localPosition, dummyMomentum,
193  globalPosition);
194  BOOST_CHECK_EQUAL(globalPosition, onPositive);
195  alignedSurface.globalToLocal(positiveContext, onPositive, dummyMomentum,
196  localPosition);
197  BOOST_CHECK_EQUAL(localPosition, Vector2D(3., 3.));
198 }
199 
200 } // namespace Test
201 } // namespace Acts