Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:42:29

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 /// @brief Define a generic concept whether an object can be piped to an ostream / cout
0032 /// @tparam ObjType: Generic class type
0033 template <typename ObjType>
0034 concept hasPrintOperator = requires(const ObjType& obj, std::ostream& ostr) {
0035   { ostr << obj } -> std::same_as<std::ostream&>;
0036 };
0037 
0038 /// Print out a matrix in a structured way.
0039 ///
0040 /// @tparam derived_t Type of the matrix
0041 /// @param matrix The matrix to print
0042 /// @param precision Numeric output precision
0043 /// @param offset Offset in front of matrix lines
0044 /// @return The printed string
0045 template <typename derived_t>
0046 inline std::string toString(const Eigen::MatrixBase<derived_t>& matrix,
0047                             int precision = 4, const std::string& offset = "") {
0048   std::ostringstream sout;
0049 
0050   sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
0051   if (matrix.cols() == 1) {
0052     sout << "(";
0053     for (int i = 0; i < matrix.rows(); ++i) {
0054       double val = detail::roundWithPrecision(matrix(i, 0), precision);
0055       sout << val;
0056       if (i != matrix.rows() - 1) {
0057         sout << ", ";
0058       }
0059     }
0060     sout << ")";
0061   } else {
0062     for (int i = 0; i < matrix.rows(); ++i) {
0063       for (int j = 0; j < matrix.cols(); ++j) {
0064         if (j == 0) {
0065           sout << "(";
0066         }
0067         double val = detail::roundWithPrecision(matrix(i, j), precision);
0068         sout << val;
0069         if (j == matrix.cols() - 1) {
0070           sout << ")";
0071         } else {
0072           sout << ", ";
0073         }
0074       }
0075       if (i != matrix.rows() -
0076                    1) {  // make the end line and the offset in the next line
0077         sout << std::endl;
0078         sout << offset;
0079       }
0080     }
0081   }
0082   return sout.str();
0083 }
0084 
0085 /// Print out a translation in a structured way.
0086 /// @param translation The translation to print
0087 /// @param precision Numeric output precision
0088 /// @return The printed string
0089 inline std::string toString(const Acts::Translation3& translation,
0090                             int precision = 4) {
0091   Acts::Vector3 trans;
0092   trans[0] = translation.x();
0093   trans[1] = translation.y();
0094   trans[2] = translation.z();
0095   return toString(trans, precision);
0096 }
0097 
0098 /// Print out a transform in a structured way.
0099 /// @param transform The transform to print
0100 /// @param precision Numeric output precision
0101 /// @param offset Offset in front of matrix lines
0102 /// @return The printed string
0103 inline std::string toString(const Acts::Transform3& transform,
0104                             int precision = 4, const std::string& offset = "") {
0105   std::ostringstream sout;
0106   sout << "Translation : " << toString(transform.translation(), precision)
0107        << std::endl;
0108   std::string rotationOffset = offset + "              ";
0109   sout << offset << "Rotation    : "
0110        << toString(transform.rotation(), precision + 2, rotationOffset);
0111   return sout.str();
0112 }
0113 
0114 /// Print out a vector of double
0115 /// @param pVector The vector to print
0116 /// @param precision Numeric output precision
0117 /// @return A formatted string representation of the vector
0118 inline std::string toString(const std::vector<double>& pVector,
0119                             int precision = 4) {
0120   std::ostringstream sout;
0121   sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
0122   sout << "(";
0123   for (const auto [i, val] : enumerate(pVector)) {
0124     sout << val;
0125     if (i != pVector.size() - 1) {
0126       sout << ", ";
0127     }
0128   }
0129   sout << ")";
0130   return sout.str();
0131 }
0132 
0133 }  // namespace Acts