ATLAS Offline Software
ParametersBase.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 ///////////////////////////////////////////////////////////////////
6 // ParametersBase.icc, (c) ATLAS Detector software
7 ///////////////////////////////////////////////////////////////////
8 
9 // Gaudi
10 #include "GaudiKernel/MsgStream.h"
11 // Trk
12 #include "TrkEventPrimitives/ParamDefs.h"
13 // STD
14 #include <iomanip>
15 #include <ostream>
16 #include <sstream>
17 #include <utility>
18 
19 namespace Trk {
20 
21 namespace {
22 template <typename T>
23 int sgn(const T& val) {
24  return (val > 0) - (val < 0);
25 }
26 } // namespace
27 
28 // Helper protected ctor*/
29 template <int DIM, class T>
30 ParametersBase<DIM, T>::ParametersBase(
31  const AmgVector(DIM) parameters,
32  std::optional<AmgSymMatrix(DIM)>&& covariance, const T chargeDef)
33  // cppcheck-suppress missingReturn; false positive
34  : ParametersCommon<DIM, T>(parameters, std::move(covariance), chargeDef) {}
35 
36 // Helper protected ctor
37 template <int DIM, class T>
38 ParametersBase<DIM, T>::ParametersBase(
39  std::optional<AmgSymMatrix(DIM)>&& covariance)
40  // cppcheck-suppress missingReturn; false positive
41  : ParametersCommon<DIM, T>(std::move(covariance)) {}
42 
43 // Protected Constructor with local arguments - persistency only
44 template <int DIM, class T>
45 ParametersBase<DIM, T>::ParametersBase(
46  const AmgVector(DIM) & parameters,
47  std::optional<AmgSymMatrix(DIM)>&& covariance)
48  : ParametersCommon<DIM, T>(parameters, std::move(covariance)) {}
49 
50 
51 template<int DIM, class T>
52 double
53 ParametersBase<DIM, T>::charge() const
54 {
55  return m_chargeDef.charge();
56 }
57 
58 template<int DIM, class T>
59 const Amg::Vector3D&
60 ParametersBase<DIM, T>::position() const
61 {
62  return m_position;
63 }
64 
65 template<int DIM, class T>
66 const Amg::Vector3D&
67 ParametersBase<DIM, T>::momentum() const
68 {
69  return m_momentum;
70 }
71 
72 template <int DIM, class T>
73 double ParametersBase<DIM, T>::pT() const {
74  return momentum().perp();
75 }
76 
77 template <int DIM, class T>
78 double ParametersBase<DIM, T>::eta() const {
79  return momentum().eta();
80 }
81 
82 /** equality operator */
83 template <int DIM, class T>
84 bool ParametersBase<DIM, T>::operator==(
85  const ParametersBase<DIM, T>& rhs) const {
86  // tolerance for comparisons
87  constexpr double tolerance = 1e-8;
88 
89  // compare parameters
90  if (!this->parameters().isApprox(rhs.parameters(), tolerance)) {
91  return false;
92  }
93 
94  // compare covariance
95  if (((this->covariance() != nullptr) && (rhs.covariance() != nullptr) &&
96  !this->covariance()->isApprox(*rhs.covariance(), tolerance)) ||
97  (!this->covariance() !=
98  !rhs.covariance())) { // <-- this is: covariance()
99  // XOR pCast->covariance()
100  return false;
101  }
102 
103  // compare position
104  if (!this->position().isApprox(rhs.position(), tolerance)) {
105  return false;
106  }
107 
108  // compare momentum
109  if (!this->momentum().isApprox(rhs.momentum(), tolerance)) {
110  return false;
111  }
112 
113  // compare charge definition
114  if (m_chargeDef != rhs.m_chargeDef) {
115  return false;
116  }
117 
118  return true;
119 }
120 
121 template <int DIM, class T>
122 MsgStream& ParametersBase<DIM, T>::dump(MsgStream& sl) const {
123  std::ostringstream output{};
124  dump(output);
125  sl << output.str();
126  return sl;
127 }
128 
129 template <int DIM, class T>
130 std::ostream& ParametersBase<DIM, T>::dump(std::ostream& sl) const {
131  const std::string nl{"\n"};
132  sl << std::setiosflags(std::ios::fixed);
133  sl << std::setprecision(7);
134  sl << " * TrackParameters on Surface" << nl;
135  sl << " * loc1 : " << this->parameters()[Trk::loc1] << nl;
136  sl << " * loc2 : " << this->parameters()[Trk::loc2] << nl;
137  sl << " * phi : " << this->parameters()[Trk::phi] << nl;
138  sl << " * Theta : " << this->parameters()[Trk::theta] << nl;
139  sl << " * q/p : " << this->parameters()[Trk::qOverP] << nl;
140  if (this->parameters().rows() > 5) {
141  sl << " * mass : " << this->parameters()[Trk::trkMass]
142  << " (extended parameters)" << nl;
143  }
144  sl << " * charge: " << this->charge() << nl;
145  sl << " * covariance matrix = " << this->covariance() << nl;
146  sl << " * corresponding global parameters:" << nl;
147  sl << " * position (x, y, z ) = (" << this->position().x() << ", "
148  << this->position().y() << ", " << this->position().z() << ")" << nl;
149  sl << " * momentum (px, py, pz) = (" << this->momentum().x() << ", "
150  << this->momentum().y() << ", " << this->momentum().z() << ")" << nl;
151  sl << std::setprecision(-1);
152  if (this->hasSurface()) {
153  sl << "associated surface:" << nl;
154  this->associatedSurface().dump(sl) << std::endl;
155  } else {
156  sl << "no associated surface" << std::endl;
157  }
158 
159  return sl;
160 }
161 
162 template <int DIM, class T>
163 MsgStream& operator<<(MsgStream& sl, const Trk::ParametersBase<DIM, T>& p) {
164  return p.dump(sl);
165 }
166 
167 template <int DIM, class T>
168 std::ostream& operator<<(std::ostream& sl,
169  const Trk::ParametersBase<DIM, T>& p) {
170  return p.dump(sl);
171 }
172 } // end of namespace Trk