Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringHelpers.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file StringHelpers.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2023 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 
9 #pragma once
10 
12 
13 #include <iomanip>
14 #include <iostream>
15 #include <string>
16 
17 #include "Eigen/Dense"
18 
19 namespace Acts {
20 
21 namespace detail {
22 inline double roundWithPrecision(double val, int precision) {
23  if (val < 0 && std::abs(val) * std::pow(10, precision) < 1.) {
24  return -val;
25  }
26  return val;
27 }
28 } // namespace detail
29 
37 template <typename derived_t>
38 inline std::string toString(const Eigen::MatrixBase<derived_t>& matrix,
39  int precision = 4, const std::string& offset = "") {
40  std::ostringstream sout;
41 
42  sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
43  if (matrix.cols() == 1) {
44  sout << "(";
45  for (int i = 0; i < matrix.rows(); ++i) {
46  double val = detail::roundWithPrecision(matrix(i, 0), precision);
47  sout << val;
48  if (i != matrix.rows() - 1) {
49  sout << ", ";
50  }
51  }
52  sout << ")";
53  } else {
54  for (int i = 0; i < matrix.rows(); ++i) {
55  for (int j = 0; j < matrix.cols(); ++j) {
56  if (j == 0) {
57  sout << "(";
58  }
59  double val = detail::roundWithPrecision(matrix(i, j), precision);
60  sout << val;
61  if (j == matrix.cols() - 1) {
62  sout << ")";
63  } else {
64  sout << ", ";
65  }
66  }
67  if (i != matrix.rows() -
68  1) { // make the end line and the offset in the next line
69  sout << std::endl;
70  sout << offset;
71  }
72  }
73  }
74  return sout.str();
75 }
76 
82  int precision = 4) {
83  Acts::Vector3 trans;
84  trans[0] = translation.x();
85  trans[1] = translation.y();
86  trans[2] = translation.z();
87  return toString(trans, precision);
88 }
89 
96  int precision = 4, const std::string& offset = "") {
97  std::ostringstream sout;
98  sout << "Translation : " << toString(transform.translation(), precision)
99  << std::endl;
100  std::string rotationOffset = offset + " ";
101  sout << offset << "Rotation : "
102  << toString(transform.rotation(), precision + 2, rotationOffset);
103  return sout.str();
104 }
105 
106 } // namespace Acts