ATLAS Offline Software
Loading...
Searching...
No Matches
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
13#include <iomanip>
14#include <iostream>
15#include <string>
16#include <string_view>
17#include <cmath>
18#include <cassert>
19
20namespace Amg {
21
36
37inline double roundWithPrecision(double val, int precision) {
38 assert(precision>=0);
39 const double scale = std::pow(10.0, precision);
40 const double rounded = std::round(val * scale) / scale;
41 return rounded == 0.0 ? 0.0 : rounded; //avoids a -0 result.
42}
43
44inline std::string toString(const MatrixX& matrix, int precision = 4,
45 std::string_view offset = "") {
46 std::ostringstream sout;
47
48 sout << std::setiosflags(std::ios::fixed) << std::setprecision(precision);
49 if (matrix.cols() == 1) {
50 sout << "(";
51 for (int i = 0; i < matrix.rows(); ++i) {
52 double val = roundWithPrecision(matrix(i, 0), precision);
53 sout << val;
54 if (i != matrix.rows() - 1)
55 sout << ", ";
56 }
57 sout << ")";
58 } else {
59 for (int i = 0; i < matrix.rows(); ++i) {
60 for (int j = 0; j < matrix.cols(); ++j) {
61 if (j == 0)
62 sout << "(";
63 double val = roundWithPrecision(matrix(i, j), precision);
64 sout << val;
65 if (j == matrix.cols() - 1)
66 sout << ")";
67 else
68 sout << ", ";
69 }
70 if (i != matrix.rows() - 1) { // make the end line and the offset in the next line
71 sout << std::endl;
72 sout << offset;
73 }
74 }
75 }
76 return sout.str();
77}
78
79} // namespace Amg
80
81#endif /* EVENTPRIMITIVESTOSTRINGCONVERTER_H_ */
Definition of ATLAS Math & Geometry primitives (Amg).
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
double roundWithPrecision(double val, int precision)
EventPrimitvesToStringConverter.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.