Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrintParameters.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PrintParameters.cpp
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 
10 
13 
14 #include <array>
15 #include <cstddef>
16 #include <iomanip>
17 #include <ostream>
18 #include <string>
19 
20 namespace {
21 
22 constexpr std::array<const char*, Acts::eBoundSize> makeBoundNames() {
23  std::array<const char*, Acts::eBoundSize> names = {nullptr};
24  // must be set by index since the order is user-configurable
25  names[Acts::eBoundLoc0] = "loc0:";
26  names[Acts::eBoundLoc1] = "loc1:";
27  names[Acts::eBoundTime] = "time:";
28  names[Acts::eBoundPhi] = "phi:";
29  names[Acts::eBoundTheta] = "theta:";
30  names[Acts::eBoundQOverP] = "q/p:";
31  return names;
32 }
33 
34 constexpr std::array<const char*, Acts::eFreeSize> makeFreeNames() {
35  std::array<const char*, Acts::eFreeSize> names = {nullptr};
36  // must be set by index since the order is user-configurable
37  names[Acts::eFreePos0] = "pos0:";
38  names[Acts::eFreePos1] = "pos1:";
39  names[Acts::eFreePos2] = "pos2:";
40  names[Acts::eFreeTime] = "time:";
41  names[Acts::eFreeDir0] = "dir0:";
42  names[Acts::eFreeDir1] = "dir1:";
43  names[Acts::eFreeDir2] = "dir2:";
44  names[Acts::eFreeQOverP] = "q/p:";
45  return names;
46 }
47 
48 constexpr std::array<std::size_t, 8> kMonotonic = {
49  0, 1, 2, 3, 4, 5, 6, 7,
50 };
51 
52 constexpr std::size_t kNamesMaxSize = 6;
53 
71 template <typename names_container_t, typename indices_container_t,
72  typename parameters_t, typename covariance_t>
73 void printParametersCovariance(std::ostream& os, const names_container_t& names,
74  const indices_container_t& nameIndices,
75  const Eigen::MatrixBase<parameters_t>& params,
76  const Eigen::MatrixBase<covariance_t>& cov) {
77  EIGEN_STATIC_ASSERT_VECTOR_ONLY(parameters_t);
78 
79  // save stream formatting state
80  auto flags = os.flags();
81  auto precision = os.precision();
82 
83  // compute the standard deviations
84  auto stddev = cov.diagonal().cwiseSqrt().eval();
85 
86  for (Eigen::Index i = 0; i < params.size(); ++i) {
87  // no newline after the last line. e.g. the log macros automatically add a
88  // newline and having a finishing newline would lead to empty lines.
89  if (0 < i) {
90  os << '\n';
91  }
92  // show name
93  os << std::setw(kNamesMaxSize) << std::left << names[nameIndices[i]];
94  // show value
95  os << " ";
96  os << std::defaultfloat << std::setprecision(4);
97  os << std::setw(10) << std::right << params[i];
98  // show standard deviation
99  os << " +- ";
100  os << std::setw(10) << std::left << stddev[i];
101  // show lower-triangular part of the correlation matrix
102  os << " ";
103  os << std::fixed << std::setprecision(3);
104  for (Eigen::Index j = 0; j <= i; ++j) {
105  auto corr = cov(i, j) / (stddev[i] * stddev[j]);
106  os << " " << std::setw(6) << std::right << corr;
107  }
108  }
109 
110  // restore previous stream formatting state
111  os.flags(flags);
112  os.precision(precision);
113 }
114 
131 template <typename names_container_t, typename indices_container_t,
132  typename parameters_t>
133 void printParameters(std::ostream& os, const names_container_t& names,
134  const indices_container_t& nameIndices,
135  const Eigen::MatrixBase<parameters_t>& params) {
136  EIGEN_STATIC_ASSERT_VECTOR_ONLY(parameters_t);
137 
138  // save stream formatting state
139  auto flags = os.flags();
140  auto precision = os.precision();
141 
142  for (Eigen::Index i = 0; i < params.size(); ++i) {
143  // no newline after the last line. e.g. the log macros automatically add a
144  // newline and having a finishing newline would lead to empty lines.
145  if (0 < i) {
146  os << '\n';
147  }
148  // show name
149  os << std::setw(kNamesMaxSize) << std::left << names[nameIndices[i]];
150  // show value
151  os << " ";
152  os << std::defaultfloat << std::setprecision(4);
153  os << std::setw(10) << std::right << params[i];
154  }
155 
156  // restore previous stream formatting state
157  os.flags(flags);
158  os.precision(precision);
159 }
160 
161 using ParametersMap = Eigen::Map<const Acts::ActsDynamicVector>;
162 using CovarianceMap = Eigen::Map<const Acts::ActsDynamicMatrix>;
163 
164 } // namespace
165 
167  const Acts::Surface& surface,
168  const Acts::BoundVector& params,
169  const Acts::BoundSquareMatrix* cov) {
170  if (cov != nullptr) {
171  printParametersCovariance(os, makeBoundNames(), kMonotonic, params, *cov);
172  } else {
173  printParameters(os, makeBoundNames(), kMonotonic, params);
174  }
175  os << "\non surface " << surface.geometryId() << " of type "
176  << surface.name();
177 }
178 
179 void Acts::detail::printFreeParameters(std::ostream& os,
180  const Acts::FreeVector& params,
181  const Acts::FreeMatrix* cov) {
182  if (cov != nullptr) {
183  printParametersCovariance(os, makeFreeNames(), kMonotonic, params, *cov);
184  } else {
185  printParameters(os, makeFreeNames(), kMonotonic, params);
186  }
187 }
188 
190  const uint8_t* indices,
191  const ActsScalar* params,
192  const ActsScalar* cov) {
193  auto s = static_cast<Eigen::Index>(size);
194  printParametersCovariance(os, makeBoundNames(), indices,
195  ParametersMap(params, s), CovarianceMap(cov, s, s));
196 }
197 
199  const uint8_t* indices,
200  const ActsScalar* params,
201  const ActsScalar* cov) {
202  auto s = static_cast<Eigen::Index>(size);
203  printParametersCovariance(os, makeFreeNames(), indices,
204  ParametersMap(params, s), CovarianceMap(cov, s, s));
205 }