ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleFreeParameters.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SingleFreeParameters.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 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 
11 #include <iomanip>
12 #include <ostream>
13 
16 
17 namespace Acts {
18 
32 template <class ChargePolicy>
36  "ChargePolicy must either be 'Acts::ChargedPolicy' or "
37  "'Acts::NeutralPolicy");
38 
39  public:
43 
45  ~SingleFreeParameters() = default;
46 
52  template <typename T = ChargePolicy,
54  SingleFreeParameters(std::optional<CovMatrix_t> cov,
55  const FreeVector& parValues)
56  : m_parameters(parValues),
57  m_oChargePolicy((0 < parValues(eFreeQOverP)) ? 1. : -1.),
58  m_covariance(std::move(cov)) {}
59 
65  template <typename T = ChargePolicy,
67  SingleFreeParameters(std::optional<CovMatrix_t> cov,
68  const FreeVector& parValues)
69  : m_parameters(parValues),
70  m_oChargePolicy(),
71  m_covariance(std::move(cov)) {}
72 
80  // Check for self-assignment
81  if (this != &rhs) {
82  m_oChargePolicy = rhs.m_oChargePolicy;
83  m_parameters = rhs.m_parameters;
84  m_covariance = rhs.m_covariance;
85  }
86  return *this;
87  }
88 
96  // Check for self-assignment
97  if (this != &rhs) {
98  m_oChargePolicy = std::move(rhs.m_oChargePolicy);
99  m_parameters = std::move(rhs.m_parameters);
100  m_covariance = std::move(rhs.m_covariance);
101  }
102  return *this;
103  }
104 
109  : m_parameters(copy.m_parameters),
110  m_oChargePolicy(copy.m_oChargePolicy),
111  m_covariance(copy.m_covariance) {}
112 
117  this->operator=(
119  }
120 
124  FreeVector parameters() const { return m_parameters; }
125 
131  template <unsigned int par,
132  std::enable_if_t<par<eFreeParametersSize, int> = 0> ParValue_t get()
133  const {
134  return m_parameters(par);
135  }
136 
143  template <unsigned int par,
144  std::enable_if_t<par<eFreeParametersSize, int> = 0> ParValue_t
145  uncertainty() const {
146  return std::sqrt(m_covariance->coeff(par, par));
147  }
148 
157  const std::optional<CovMatrix_t>& covariance() const { return m_covariance; }
158 
162  Vector3D position() const { return m_parameters.template head<3>(); }
163 
167  Vector3D momentum() const {
168  return m_parameters.template segment<3>(4) / std::abs(get<7>());
169  }
170 
174  double charge() const { return m_oChargePolicy.getCharge(); }
175 
179  double time() const { return m_parameters(3); }
180 
187  bool operator==(const SingleFreeParameters& rhs) const {
188  auto casted = dynamic_cast<decltype(this)>(&rhs);
189  if (!casted) {
190  return false;
191  }
192 
193  // Both have covariance matrices set
194  if ((m_covariance.has_value() && casted->m_covariance.has_value()) &&
195  (*m_covariance != *casted->m_covariance)) {
196  return false;
197  }
198  // Only one has a covariance matrix set
199  if ((m_covariance.has_value() && !casted->m_covariance.has_value()) ||
200  (!m_covariance.has_value() && casted->m_covariance.has_value())) {
201  return false;
202  }
203 
204  return (m_oChargePolicy == casted->m_oChargePolicy &&
205  m_parameters == casted->m_parameters);
206  }
207 
211  bool operator!=(const SingleFreeParameters& rhs) const {
212  return !(*this == rhs);
213  }
214 
223  template <unsigned int par,
224  std::enable_if_t<par<eFreeParametersSize, int> = 0> void set(
225  const GeometryContext& /*gctx*/, ParValue_t newValue) {
226  m_parameters(par) = newValue;
227  }
228 
234  std::ostream& print(std::ostream& sl) const {
235  // Set stream output format
236  auto old_precision = sl.precision(7);
237  auto old_flags = sl.setf(std::ios::fixed);
238 
239  // Fill stream with content
240  sl << " * FreeTrackParameters: ";
241  sl << parameters().transpose() << std::endl;
242  sl << " * charge: " << charge() << std::endl;
243  if (covariance().has_value()) {
244  sl << " * covariance matrix:\n" << *covariance() << std::endl;
245  } else {
246  sl << " * no covariance matrix stored" << std::endl;
247  }
248 
249  // Reset stream format
250  sl.precision(old_precision);
251  sl.setf(old_flags);
252 
253  return sl;
254  }
255 
263  friend std::ostream& operator<<(std::ostream& out,
264  const SingleFreeParameters& sfp) {
265  sfp.print(out);
266  return out;
267  }
268 
269  private:
270  FreeVector m_parameters;
271  ChargePolicy m_oChargePolicy;
272 
273  std::optional<CovMatrix_t> m_covariance;
274 };
275 
276 } // namespace Acts