ATLAS Offline Software
EventPrimitivesToStringConverter.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // EventPrimitivesToStringConverter.h, (c) ATLAS Detector software
8 
9 #ifndef EVENTPRIMITIVESTOSTRINGCONVERTER_H_
10 #define EVENTPRIMITIVESTOSTRINGCONVERTER_H_
11 
12 #include <iomanip>
13 #include <iostream>
14 #include <string>
15 
17 
18 namespace Amg {
19 
35 inline double roundWithPrecision(double val, int precision) {
36  if (val < 0 && std::abs(val) * std::pow(10, precision) < 1.)
37  return -val;
38  return val;
39 }
40 
41 inline std::string toString(const MatrixX& matrix, int precision = 4,
42  const std::string& offset = "") {
43  std::ostringstream sout;
44 
45  sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
46  if (matrix.cols() == 1) {
47  sout << "(";
48  for (int i = 0; i < matrix.rows(); ++i) {
49  double val = roundWithPrecision(matrix(i, 0), precision);
50  sout << val;
51  if (i != matrix.rows() - 1)
52  sout << ", ";
53  }
54  sout << ")";
55  } else {
56  for (int i = 0; i < matrix.rows(); ++i) {
57  for (int j = 0; j < matrix.cols(); ++j) {
58  if (j == 0)
59  sout << "(";
60  double val = roundWithPrecision(matrix(i, j), precision);
61  sout << val;
62  if (j == matrix.cols() - 1)
63  sout << ")";
64  else
65  sout << ", ";
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 
77 } // namespace Amg
78 
79 #endif /* EVENTPRIMITIVESTOSTRINGCONVERTER_H_ */
Amg::MatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Definition: EventPrimitives.h:29
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
lumiFormat.i
int i
Definition: lumiFormat.py:92
EventPrimitives.h
Amg
Definition of ATLAS Math & Geometry primitives (Amg)
Definition: AmgStringHelpers.h:19
python.testIfMatch.matrix
matrix
Definition: testIfMatch.py:66
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
Amg::roundWithPrecision
double roundWithPrecision(double val, int precision)
EventPrimitvesToStringConverter.
Definition: EventPrimitivesToStringConverter.h:35