Back to home page

EIC code displayed by LXR

 
 

    


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

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 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Utilities/Enumerate.hpp"
0013 
0014 #include <iomanip>
0015 #include <iostream>
0016 #include <string>
0017 
0018 #include "Eigen/Dense"
0019 
0020 namespace Acts {
0021 
0022 namespace detail {
0023 inline double roundWithPrecision(double val, int precision) {
0024   if (val < 0 && std::abs(val) * std::pow(10, precision) < 1.) {
0025     return -val;
0026   }
0027   return val;
0028 }
0029 }  // namespace detail
0030 
0031 /// Print out a matrix in a structured way.
0032 ///
0033 /// @tparam derived_t Type of the matrix
0034 /// @param matrix The matrix to print
0035 /// @param precision Numeric output precision
0036 /// @param offset Offset in front of matrix lines
0037 /// @return The printed string
0038 template <typename derived_t>
0039 inline std::string toString(const Eigen::MatrixBase<derived_t>& matrix,
0040                             int precision = 4, const std::string& offset = "") {
0041   std::ostringstream sout;
0042 
0043   sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
0044   if (matrix.cols() == 1) {
0045     sout << "(";
0046     for (int i = 0; i < matrix.rows(); ++i) {
0047       double val = detail::roundWithPrecision(matrix(i, 0), precision);
0048       sout << val;
0049       if (i != matrix.rows() - 1) {
0050         sout << ", ";
0051       }
0052     }
0053     sout << ")";
0054   } else {
0055     for (int i = 0; i < matrix.rows(); ++i) {
0056       for (int j = 0; j < matrix.cols(); ++j) {
0057         if (j == 0) {
0058           sout << "(";
0059         }
0060         double val = detail::roundWithPrecision(matrix(i, j), precision);
0061         sout << val;
0062         if (j == matrix.cols() - 1) {
0063           sout << ")";
0064         } else {
0065           sout << ", ";
0066         }
0067       }
0068       if (i != matrix.rows() -
0069                    1) {  // make the end line and the offset in the next line
0070         sout << std::endl;
0071         sout << offset;
0072       }
0073     }
0074   }
0075   return sout.str();
0076 }
0077 
0078 /// Print out a translation in a structured way.
0079 /// @param translation The translation to print
0080 /// @param precision Numeric output precision
0081 /// @return The printed string
0082 inline std::string toString(const Acts::Translation3& translation,
0083                             int precision = 4) {
0084   Acts::Vector3 trans;
0085   trans[0] = translation.x();
0086   trans[1] = translation.y();
0087   trans[2] = translation.z();
0088   return toString(trans, precision);
0089 }
0090 
0091 /// Print out a transform in a structured way.
0092 /// @param transform The transform to print
0093 /// @param precision Numeric output precision
0094 /// @param offset Offset in front of matrix lines
0095 /// @return The printed string
0096 inline std::string toString(const Acts::Transform3& transform,
0097                             int precision = 4, const std::string& offset = "") {
0098   std::ostringstream sout;
0099   sout << "Translation : " << toString(transform.translation(), precision)
0100        << std::endl;
0101   std::string rotationOffset = offset + "              ";
0102   sout << offset << "Rotation    : "
0103        << toString(transform.rotation(), precision + 2, rotationOffset);
0104   return sout.str();
0105 }
0106 
0107 /// Print out a vector of double
0108 /// @param pVector The vector to print
0109 /// @param precision Numeric output precision
0110 inline std::string toString(const std::vector<double>& pVector,
0111                             int precision = 4) {
0112   std::ostringstream sout;
0113   sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
0114   sout << "(";
0115   for (const auto [i, val] : enumerate(pVector)) {
0116     sout << val;
0117     if (i != pVector.size() - 1) {
0118       sout << ", ";
0119     }
0120   }
0121   sout << ")";
0122   return sout.str();
0123 }
0124 
0125 }  // namespace Acts