Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:19

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