ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterSet.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ParameterSet.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-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 #pragma once
10 // STL include(s)
11 #include <memory>
12 #include <optional>
13 #include <type_traits>
14 #include <utility>
15 
16 // Acts includes
29 
30 namespace Acts {
32 // forward type declaration for full parameter set
33 using FullParameterSet = typename detail::full_parset::type;
35 
80 template <ParID_t... params>
81 class ParameterSet {
82  private:
83  // local typedefs and constants
84  using ParSet_t = ParameterSet<params...>;
85  static constexpr unsigned int NPars =
86  sizeof...(params);
87 
88  // static assert to check that the template parameters are consistent
90  "parameter identifiers are not sorted");
91  static_assert(
92  detail::are_within<unsigned int, 0, eBoundParametersSize,
93  static_cast<unsigned int>(params)...>::value,
94  "parameter identifiers must be greater or "
95  "equal to zero and smaller than the total number of parameters");
96  static_assert(NPars > 0, "number of stored parameters can not be zero");
97  static_assert(
99  "number of stored parameters can not exceed number of total parameters");
100 
101  public:
102  // public typedefs
110 
121  template <typename... Tail>
122  ParameterSet(std::optional<CovMatrix_t> cov,
123  std::enable_if_t<sizeof...(Tail) + 1 == NPars, ParValue_t> head,
124  Tail... values)
125  : m_vValues(NPars) {
126  if (cov) {
127  m_optCovariance = std::move(*cov);
128  }
130  }
131 
144  ParameterSet(std::optional<CovMatrix_t> cov, const ParVector_t& values)
145  : m_vValues(NPars) {
146  if (cov) {
147  m_optCovariance = std::move(*cov);
148  }
150  }
151 
160 
168  if (copy.m_optCovariance) {
169  m_optCovariance = std::move(*copy.m_optCovariance);
170  }
171  }
172 
176  ~ParameterSet() = default;
177 
183  ParSet_t& operator=(const ParSet_t& rhs) {
184  m_vValues = rhs.m_vValues;
186  return *this;
187  }
188 
195  m_vValues = std::move(rhs.m_vValues);
196  m_optCovariance = std::move(rhs.m_optCovariance);
197  return *this;
198  }
199 
203  friend void swap(ParSet_t& first, ParSet_t& second) noexcept {
204  using std::swap;
205  swap(first.m_vValues, second.m_vValues);
206  swap(first.m_optCovariance, second.m_optCovariance);
207  }
208 
218  template <ParID_t parameter>
219  static constexpr size_t getIndex() {
220  return detail::get_position<ParID_t, parameter, params...>::value;
221  }
222 
234  template <size_t index>
235  static constexpr ParID_t getParID() {
236  return detail::at_index<ParID_t, index, params...>::value;
237  }
238 
248  template <ParID_t parameter>
250  return m_vValues(getIndex<parameter>());
251  }
252 
258  ParVector_t getParameters() const { return m_vValues; }
259 
270  template <ParID_t parameter>
272  m_vValues(getIndex<parameter>()) =
274  }
275 
287  }
288 
300  template <ParID_t parameter>
301  bool contains() const {
302  return detail::is_contained<ParID_t, parameter, params...>::value;
303  }
304 
313  const std::optional<CovMatrix_t>& getCovariance() const {
314  return m_optCovariance;
315  }
316 
329  template <ParID_t parameter>
331  if (m_optCovariance) {
332  size_t index = getIndex<parameter>();
333  return sqrt((*m_optCovariance)(index, index));
334  } else {
335  return -1;
336  }
337  }
338 
346  void setCovariance(const CovMatrix_t& cov) { m_optCovariance = cov; }
347 
355  bool operator==(const ParSet_t& rhs) const {
356  // shortcut comparison with myself
357  if (&rhs == this) {
358  return true;
359  }
360 
361  // parameter values
362  if (m_vValues != rhs.m_vValues) {
363  return false;
364  }
365  // both have covariance matrices set
366  if ((m_optCovariance && rhs.m_optCovariance) &&
367  (*m_optCovariance != *rhs.m_optCovariance)) {
368  return false;
369  }
370  // only one has a covariance matrix set
371  if ((m_optCovariance && !rhs.m_optCovariance) ||
372  (!m_optCovariance && rhs.m_optCovariance)) {
373  return false;
374  }
375 
376  return true;
377  }
378 
386  bool operator!=(const ParSet_t& rhs) const { return !(*this == rhs); }
387 
413  ParVector_t project(const FullParameterSet& fullParSet) const {
414  return projector() * fullParSet.getParameters();
415  }
416 
451 
452  template <
453  typename T = ParSet_t,
456  ParVector_t residual(const FullParameterSet& fullParSet) const {
457  return detail::residual_calculator<params...>::result(
458  m_vValues, projector() * fullParSet.getParameters());
459  }
460 
493  ParVector_t residual(const ParSet_t& otherParSet) const {
494  return detail::residual_calculator<params...>::result(
495  m_vValues, otherParSet.m_vValues);
496  }
497 
509  return sProjector;
510  }
511 
517  static constexpr unsigned int size() { return NPars; }
518 
532  detail::value_corrector<params...>::result(values);
533  }
534 
535  private:
536  ParVector_t m_vValues{ParVector_t::Zero()};
537 
538  std::optional<CovMatrix_t> m_optCovariance{
539  std::nullopt};
540 
541  static const Projection_t sProjector;
542 
543 };
544 
545 // initialize static class members
546 template <ParID_t... params>
547 constexpr unsigned int ParameterSet<params...>::NPars;
548 
549 template <ParID_t... params>
550 const typename ParameterSet<params...>::Projection_t
551  ParameterSet<params...>::sProjector = detail::make_projection_matrix<
552  eBoundParametersSize, static_cast<unsigned int>(params)...>::init();
553 } // namespace Acts