Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Measurement.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Measurement.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-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 #pragma once
10 
17 
18 #include <array>
19 #include <cstddef>
20 #include <iosfwd>
21 #include <variant>
22 
23 namespace Acts {
24 
46 template <typename indices_t, size_t kSize>
47 class Measurement {
48  static constexpr size_t kFullSize = detail::kParametersSize<indices_t>;
49 
51 
52  public:
53  using Scalar = ActsScalar;
62 
74  template <typename parameters_t, typename covariance_t>
75  Measurement(SourceLink source, const std::array<indices_t, kSize>& indices,
76  const Eigen::MatrixBase<parameters_t>& params,
77  const Eigen::MatrixBase<covariance_t>& cov)
78  : m_source(std::move(source)),
79  m_subspace(indices),
80  m_params(params),
81  m_cov(cov) {
82  // TODO we should be able to support arbitrary ordering, by sorting the
83  // indices and reordering parameters/covariance. since the parameter order
84  // can be modified by the user, the user can not always know what the
85  // right order is. another option is too remove the requirement for index
86  // ordering from the subspace types, but that will make it harder to
87  // refactor their implementation later on.
88  }
90  Measurement() = delete;
91  Measurement(const Measurement&) = default;
92  Measurement(Measurement&&) = default;
93  ~Measurement() = default;
94  Measurement& operator=(const Measurement&) = default;
95  Measurement& operator=(Measurement&&) = default;
96 
98  const SourceLink& sourceLink() const { return m_source; }
99 
101  static constexpr size_t size() { return kSize; }
102 
104  bool contains(indices_t i) const { return m_subspace.contains(i); }
105 
107  constexpr std::array<indices_t, kSize> indices() const {
108  std::array<uint8_t, kSize> subInds = m_subspace.indices();
109  std::array<indices_t, kSize> inds{};
110  for (size_t i = 0; i < kSize; i++) {
111  inds[i] = static_cast<indices_t>(subInds[i]);
112  }
113  return inds;
114  }
115 
117  const ParametersVector& parameters() const { return m_params; }
118 
120  const CovarianceMatrix& covariance() const { return m_cov; }
121 
124  return m_subspace.template projector<Scalar>();
125  }
126 
134  return m_subspace.template expander<Scalar>();
135  }
136 
145  ParametersVector res = ParametersVector::Zero();
146  detail::calculateResiduals(static_cast<indices_t>(kSize),
147  m_subspace.indices(), reference, m_params, res);
148  return res;
149  }
150 
151  std::ostream& operator<<(std::ostream& os) const {
152  detail::printMeasurement(os, static_cast<indices_t>(kSize),
153  m_subspace.indices().data(), m_params.data(),
154  m_cov.data());
155  return os;
156  }
157 
158  private:
163 };
164 
188 template <typename parameters_t, typename covariance_t, typename indices_t,
189  typename... tail_indices_t>
191  const Eigen::MatrixBase<parameters_t>& params,
192  const Eigen::MatrixBase<covariance_t>& cov,
193  indices_t index0, tail_indices_t... tailIndices)
194  -> Measurement<indices_t, 1u + sizeof...(tail_indices_t)> {
195  using IndexContainer = std::array<indices_t, 1u + sizeof...(tail_indices_t)>;
196  return {std::move(source), IndexContainer{index0, tailIndices...}, params,
197  cov};
198 }
199 
200 namespace detail {
202 
203 // Recursive construction of the measurement variant. `kN` is counted down until
204 // zero while the sizes are accumulated in the parameter pack.
205 //
206 // Example:
207 //
208 // VariantMeasurementGenerator<..., 4>
209 // -> VariantMeasurementGenerator<..., 3, 4>
210 // -> VariantMeasurementGenerator<..., 2, 3, 4>
211 // -> VariantMeasurementGenerator<..., 1, 2, 3, 4>
212 // -> VariantMeasurementGenerator<..., 0, 1, 2, 3, 4>
213 //
214 template <typename indices_t, size_t kN, size_t... kSizes>
215 struct VariantMeasurementGenerator
216  : VariantMeasurementGenerator<indices_t, kN - 1u, kN, kSizes...> {};
217 template <typename indices_t, size_t... kSizes>
218 struct VariantMeasurementGenerator<indices_t, 0u, kSizes...> {
219  using Type = std::variant<Measurement<indices_t, kSizes>...>;
220 };
221 
223 } // namespace detail
224 
228 template <typename indices_t>
229 using VariantMeasurement = typename detail::VariantMeasurementGenerator<
230  indices_t, detail::kParametersSize<indices_t>>::Type;
231 
235 
239 
240 template <typename indices_t>
241 std::ostream& operator<<(std::ostream& os,
242  const VariantMeasurement<indices_t>& vm) {
243  return std::visit([&](const auto& m) { return (os << m); }, vm);
244 }
245 
246 } // namespace Acts