Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-07 07:46:03

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/EventData/detail/PrintParameters.hpp"
0010 
0011 #include "Acts/Geometry/GeometryIdentifier.hpp"
0012 #include "Acts/Surfaces/Surface.hpp"
0013 
0014 #include <array>
0015 #include <cstddef>
0016 #include <iomanip>
0017 #include <ostream>
0018 
0019 namespace Acts {
0020 
0021 namespace {
0022 
0023 constexpr std::array<const char*, eBoundSize> makeBoundNames() {
0024   std::array<const char*, eBoundSize> names = {nullptr};
0025   // must be set by index since the order is user-configurable
0026   names[eBoundLoc0] = "loc0:";
0027   names[eBoundLoc1] = "loc1:";
0028   names[eBoundTime] = "time:";
0029   names[eBoundPhi] = "phi:";
0030   names[eBoundTheta] = "theta:";
0031   names[eBoundQOverP] = "q/p:";
0032   return names;
0033 }
0034 
0035 constexpr std::array<const char*, eFreeSize> makeFreeNames() {
0036   std::array<const char*, eFreeSize> names = {nullptr};
0037   // must be set by index since the order is user-configurable
0038   names[eFreePos0] = "pos0:";
0039   names[eFreePos1] = "pos1:";
0040   names[eFreePos2] = "pos2:";
0041   names[eFreeTime] = "time:";
0042   names[eFreeDir0] = "dir0:";
0043   names[eFreeDir1] = "dir1:";
0044   names[eFreeDir2] = "dir2:";
0045   names[eFreeQOverP] = "q/p:";
0046   return names;
0047 }
0048 
0049 constexpr std::array<std::size_t, 8> kMonotonic = {
0050     0, 1, 2, 3, 4, 5, 6, 7,
0051 };
0052 
0053 constexpr std::size_t kNamesMaxSize = 6;
0054 
0055 /// Print parameters and associated covariance.
0056 ///
0057 /// @param os Output stream
0058 /// @param names Container with all names
0059 /// @param nameIndices Identify the name for each parameter value
0060 /// @param params Parameter values
0061 /// @param cov Covariance matrix
0062 ///
0063 /// The output format format is
0064 ///
0065 ///     name0: value0 +- stddev0  corr00
0066 ///     name1: value1 +- stddev1  corr10 corr11
0067 ///     name2: value2 +- stddev2  corr20 corr21 corr22
0068 ///     ...
0069 ///
0070 /// w/o a newline for the last line for better compatibility with the logging
0071 /// macros.
0072 template <typename names_container_t, typename indices_container_t,
0073           typename parameters_t, typename covariance_t>
0074 void printParametersCovariance(std::ostream& os, const names_container_t& names,
0075                                const indices_container_t& nameIndices,
0076                                const Eigen::MatrixBase<parameters_t>& params,
0077                                const Eigen::MatrixBase<covariance_t>& cov) {
0078   EIGEN_STATIC_ASSERT_VECTOR_ONLY(parameters_t);
0079 
0080   // save stream formatting state
0081   auto flags = os.flags();
0082   auto precision = os.precision();
0083 
0084   // compute the standard deviations
0085   auto stddev = cov.diagonal().cwiseSqrt().eval();
0086 
0087   for (Eigen::Index i = 0; i < params.size(); ++i) {
0088     // no newline after the last line. e.g. the log macros automatically add a
0089     // newline and having a finishing newline would lead to empty lines.
0090     if (0 < i) {
0091       os << '\n';
0092     }
0093     // show name
0094     os << std::setw(kNamesMaxSize) << std::left << names[nameIndices[i]];
0095     // show value
0096     os << " ";
0097     os << std::defaultfloat << std::setprecision(4);
0098     os << std::setw(10) << std::right << params[i];
0099     // show standard deviation
0100     os << " +- ";
0101     os << std::setw(10) << std::left << stddev[i];
0102     // show lower-triangular part of the correlation matrix
0103     os << " ";
0104     os << std::fixed << std::setprecision(3);
0105     for (Eigen::Index j = 0; j <= i; ++j) {
0106       auto corr = cov(i, j) / (stddev[i] * stddev[j]);
0107       os << " " << std::setw(6) << std::right << corr;
0108     }
0109   }
0110 
0111   // restore previous stream formatting state
0112   os.flags(flags);
0113   os.precision(precision);
0114 }
0115 
0116 /// Print parameters only.
0117 ///
0118 /// @param os Output stream
0119 /// @param names Container with all names
0120 /// @param nameIndices Identify the name for each parameter value
0121 /// @param params Parameter values
0122 ///
0123 /// The output format format is
0124 ///
0125 ///     name0: value0
0126 ///     name1: value1
0127 ///     name2: value2
0128 ///     ...
0129 ///
0130 /// w/o a newline for the last line for better compatibility with the logging
0131 /// macros.
0132 template <typename names_container_t, typename indices_container_t,
0133           typename parameters_t>
0134 void printParameters(std::ostream& os, const names_container_t& names,
0135                      const indices_container_t& nameIndices,
0136                      const Eigen::MatrixBase<parameters_t>& params) {
0137   EIGEN_STATIC_ASSERT_VECTOR_ONLY(parameters_t);
0138 
0139   // save stream formatting state
0140   auto flags = os.flags();
0141   auto precision = os.precision();
0142 
0143   for (Eigen::Index i = 0; i < params.size(); ++i) {
0144     // no newline after the last line. e.g. the log macros automatically add a
0145     // newline and having a finishing newline would lead to empty lines.
0146     if (0 < i) {
0147       os << '\n';
0148     }
0149     // show name
0150     os << std::setw(kNamesMaxSize) << std::left << names[nameIndices[i]];
0151     // show value
0152     os << " ";
0153     os << std::defaultfloat << std::setprecision(4);
0154     os << std::setw(10) << std::right << params[i];
0155   }
0156 
0157   // restore previous stream formatting state
0158   os.flags(flags);
0159   os.precision(precision);
0160 }
0161 
0162 using ParametersMap = Eigen::Map<const DynamicVector>;
0163 using CovarianceMap = Eigen::Map<const DynamicMatrix>;
0164 
0165 }  // namespace
0166 
0167 void detail::printBoundParameters(std::ostream& os, const Surface& surface,
0168                                   const ParticleHypothesis& particleHypothesis,
0169                                   const BoundVector& params,
0170                                   const BoundMatrix* cov) {
0171   if (cov != nullptr) {
0172     printParametersCovariance(os, makeBoundNames(), kMonotonic, params, *cov);
0173   } else {
0174     printParameters(os, makeBoundNames(), kMonotonic, params);
0175   }
0176   os << "\non surface " << surface.geometryId() << " of type "
0177      << surface.name();
0178   os << "\nwith " << particleHypothesis;
0179 }
0180 
0181 void detail::printFreeParameters(std::ostream& os,
0182                                  const ParticleHypothesis& particleHypothesis,
0183                                  const FreeVector& params,
0184                                  const FreeMatrix* cov) {
0185   if (cov != nullptr) {
0186     printParametersCovariance(os, makeFreeNames(), kMonotonic, params, *cov);
0187   } else {
0188     printParameters(os, makeFreeNames(), kMonotonic, params);
0189   }
0190   os << "\nwith " << particleHypothesis;
0191 }
0192 
0193 void detail::printMeasurement(std::ostream& os, BoundIndices size,
0194                               const std::uint8_t* indices, const double* params,
0195                               const double* cov) {
0196   auto s = static_cast<Eigen::Index>(size);
0197   printParametersCovariance(os, makeBoundNames(), indices,
0198                             ParametersMap(params, s), CovarianceMap(cov, s, s));
0199 }
0200 
0201 void detail::printMeasurement(std::ostream& os, FreeIndices size,
0202                               const std::uint8_t* indices, const double* params,
0203                               const double* cov) {
0204   auto s = static_cast<Eigen::Index>(size);
0205   printParametersCovariance(os, makeFreeNames(), indices,
0206                             ParametersMap(params, s), CovarianceMap(cov, s, s));
0207 }
0208 
0209 }  // namespace Acts