Analysis Software
Documentation for sPHENIX simulation 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 
19 
20 #include <array>
21 #include <memory>
22 #include <utility>
23 
24 namespace Acts {
25 class PlanarBounds;
26 } // namespace Acts
27 
28 using namespace Acts::UnitLiterals;
29 
30 namespace Acts {
31 namespace Test {
32 
36  std::shared_ptr<const std::array<Transform3, 2>> alignmentStore = nullptr;
37 
39  unsigned int alignmentIndex{0};
40 
42  AlignmentContext() = default;
43 
45  AlignmentContext(std::shared_ptr<const std::array<Transform3, 2>> aStore,
46  unsigned int aIndex = 0)
47  : alignmentStore(std::move(aStore)), alignmentIndex(aIndex) {}
48 };
49 
55  public:
56  // Deleted default constructor
57  AlignableDetectorElement() = delete;
58 
65  AlignableDetectorElement(std::shared_ptr<const Transform3> transform,
66  const std::shared_ptr<const PlanarBounds>& pBounds,
67  double thickness)
69  m_elementTransform(std::move(transform)),
70  m_elementThickness(thickness) {
71  m_elementSurface = Surface::makeShared<PlaneSurface>(pBounds, *this);
72  }
73 
75  ~AlignableDetectorElement() override = default;
76 
82  const Transform3& transform(const GeometryContext& gctx) const override;
83 
85  const Surface& surface() const override;
86 
88  Surface& surface() override;
89 
91  double thickness() const override;
92 
93  private:
95  std::shared_ptr<const Transform3> m_elementTransform;
97  std::shared_ptr<Surface> m_elementSurface{nullptr};
99  double m_elementThickness{0.};
100 };
101 
103  const GeometryContext& gctx) const {
104  auto alignContext = gctx.get<AlignmentContext>();
105  if (alignContext.alignmentStore != nullptr and
106  alignContext.alignmentIndex < 2) {
107  return (*(alignContext.alignmentStore))[alignContext.alignmentIndex];
108  }
109  return (*m_elementTransform);
110 }
111 
113  return *m_elementSurface;
114 }
115 
117  return *m_elementSurface;
118 }
119 
120 inline double AlignableDetectorElement::thickness() const {
121  return m_elementThickness;
122 }
123 
125 BOOST_AUTO_TEST_CASE(AlignmentContextTests) {
126  // The nominal and alignments
127  Vector3 nominalCenter(0., 0., 0.);
128  Vector3 negativeCenter(0., 0., -1.);
129  Vector3 positiveCenter(0., 0., 1.);
130 
131  // Checkpoints
132  Vector3 onNominal(3., 3., 0.);
133  Vector3 onNegative(3., 3., -1.);
134  Vector3 onPositive(3., 3., 1.);
135 
136  // Local position
137  Vector2 localPosition(3., 3.);
138 
139  // A position placeholder and dummy momentum
140  Vector3 globalPosition(100_cm, 100_cm, 100_cm);
141  Vector3 dummyMomentum(4., 4., 4.);
142 
143  Transform3 negativeTransform = Transform3::Identity();
144  negativeTransform.translation() = negativeCenter;
145 
146  Transform3 positiveTransform = Transform3::Identity();
147  positiveTransform.translation() = positiveCenter;
148 
149  std::array<Transform3, 2> alignmentArray = {negativeTransform,
150  positiveTransform};
151 
152  std::shared_ptr<const std::array<Transform3, 2>> alignmentStore =
153  std::make_shared<const std::array<Transform3, 2>>(alignmentArray);
154 
155  // The detector element at nominal position
156  AlignableDetectorElement alignedElement(
157  std::make_shared<const Transform3>(Transform3::Identity()),
158  std::make_shared<const RectangleBounds>(100_cm, 100_cm), 1_mm);
159 
160  const auto& alignedSurface = alignedElement.surface();
161 
162  // The alignment contexts
163  GeometryContext defaultContext{AlignmentContext{}};
164  GeometryContext negativeContext{AlignmentContext{alignmentStore, 0}};
165  GeometryContext positiveContext{AlignmentContext{alignmentStore, 1}};
166 
167  // Test the transforms
168  BOOST_CHECK(alignedSurface.transform(defaultContext)
169  .isApprox(Transform3::Identity()));
170  BOOST_CHECK(
171  alignedSurface.transform(negativeContext).isApprox(negativeTransform));
172  BOOST_CHECK(
173  alignedSurface.transform(positiveContext).isApprox(positiveTransform));
174 
175  // Test the centers
176  BOOST_CHECK_EQUAL(alignedSurface.center(defaultContext), nominalCenter);
177  BOOST_CHECK_EQUAL(alignedSurface.center(negativeContext), negativeCenter);
178  BOOST_CHECK_EQUAL(alignedSurface.center(positiveContext), positiveCenter);
179 
180  // Test OnSurface
181  BOOST_CHECK(
182  alignedSurface.isOnSurface(defaultContext, onNominal, dummyMomentum));
183  BOOST_CHECK(
184  alignedSurface.isOnSurface(negativeContext, onNegative, dummyMomentum));
185  BOOST_CHECK(
186  alignedSurface.isOnSurface(positiveContext, onPositive, dummyMomentum));
187 
188  // Test local to Global and vice versa
189  globalPosition = alignedSurface.localToGlobal(defaultContext, localPosition,
190  dummyMomentum);
191  BOOST_CHECK_EQUAL(globalPosition, onNominal);
192  localPosition =
193  alignedSurface.globalToLocal(defaultContext, onNominal, dummyMomentum)
194  .value();
195  BOOST_CHECK_EQUAL(localPosition, Vector2(3., 3.));
196 
197  globalPosition = alignedSurface.localToGlobal(negativeContext, localPosition,
198  dummyMomentum);
199  BOOST_CHECK_EQUAL(globalPosition, onNegative);
200  localPosition =
201  alignedSurface.globalToLocal(negativeContext, onNegative, dummyMomentum)
202  .value();
203  BOOST_CHECK_EQUAL(localPosition, Vector2(3., 3.));
204 
205  globalPosition = alignedSurface.localToGlobal(positiveContext, localPosition,
206  dummyMomentum);
207  BOOST_CHECK_EQUAL(globalPosition, onPositive);
208  localPosition =
209  alignedSurface.globalToLocal(positiveContext, onPositive, dummyMomentum)
210  .value();
211  BOOST_CHECK_EQUAL(localPosition, Vector2(3., 3.));
212 }
213 
214 } // namespace Test
215 } // namespace Acts