Belle II Software light-2406-ragdoll
HTML.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/utilities/HTML.h>
9
10#include <boost/algorithm/string/replace.hpp>
11#include <regex>
12
13#include <TMatrixFBase.h>
14#include <TMatrixD.h>
15
16#include <framework/gearbox/Unit.h>
17
18#include <iomanip>
19#include <sstream>
20
21using namespace Belle2;
22using namespace boost::algorithm;
23
24std::string HTML::getString(const TMatrixFBase& matrix, int precision, bool color)
25{
26 const int nr = matrix.GetNrows();
27 const int nc = matrix.GetNcols();
28
29 TMatrixD matrix_double(nr, nc);
30 for (int ir = 0; ir < nr; ir++)
31 for (int ic = 0; ic < nc; ic++)
32 matrix_double(ir, ic) = matrix(ir, ic);
33 return getString(matrix_double, precision, color);
34}
35
36std::string HTML::getString(const TMatrixDBase& matrix, int precision, bool color)
37{
38 std::stringstream stream;
39 stream.precision(precision);
40
41 stream << "<table padding=0>";
42 const double max = matrix.Max();
43 const double min = matrix.Min();
44 for (int i = 0; i < matrix.GetNrows(); i++) {
45 stream << "<tr>";
46 for (int k = 0; k < matrix.GetNcols(); k++) {
47 const double value = matrix(i, k);
48 stream << "<td align=right";
49 if (color and value != 0.0) {
50 //0 white, min cyan, max red
51 int b = 255;
52 int r = 255;
53 int g = 255;
54 if (value > 0)
55 b = g = 255 - 180 * value / max;
56 else
57 r = 255 - 200 * value / min;
58
59 stream << " bgcolor=" << std::setfill('0') << std::hex;
60 stream << std::setw(2) << r;
61 stream << std::setw(2) << g;
62 stream << std::setw(2) << b;
63 stream << std::dec << std::setfill(' ');
64 }
65 stream << ">" << std::setw(11) << value;
66 stream << "</td>";
67 }
68 stream << "</tr>";
69 }
70 stream << "</table>";
71 stream << "<br>";
72
73 return stream.str();
74}
75
76std::string HTML::getString(const ROOT::Math::XYZVector& vec, int precision)
77{
78 std::stringstream stream;
79 stream.precision(precision);
80
81 stream << std::fixed << "(" << vec.X() << ", " << vec.Y() << ", " << vec.Z() << ")";
82 return stream.str();
83}
84
85std::string HTML::getStringConvertToUnit(const ROOT::Math::XYZVector& vec, int precision, const std::string& unitType)
86{
87 std::stringstream stream;
88 stream.precision(precision);
89 std::string displayedUnitType = unitType;
90
91 if (unitType == "um")
92 displayedUnitType = "\x0b5m";
93
94
95 stream << std::fixed << "(" << Unit::convertValueToUnit(vec.X(), unitType) << ", "
96 << Unit::convertValueToUnit(vec.Y(), unitType) << ", " << Unit::convertValueToUnit(vec.Z(), unitType)
97 << ") " << displayedUnitType;
98
99
100 return stream.str();
101}
102std::string HTML::chooseUnitOfLength(const ROOT::Math::XYZVector& vec)
103{
104 double xyz [3];
105 std::string unitType;
106 vec.GetCoordinates(xyz);
107 double max = 0;
108
109 for (auto entry : xyz)
110 if (std::abs(entry) > max)
111 max = std::abs(entry);
112
113 // choose specific range for that the unit is useful
114 if (max < 0.1)
115 unitType = "um";
116 else
117 unitType = "cm";
118 return unitType;
119}
120
121std::string HTML::getHexDump(const int* buf, int length)
122{
123 std::stringstream stream;
124 stream << "<small><tt>";
125 char str[10];
126 //print everything in blocks of 4 bytes, 4 blocks per line
127 for (int i = 0; i < length; i++) {
128 //byte = 2letters
129 //4byte = 8 letters
130 snprintf(str, sizeof(str), "%08x ", buf[i]);
131 stream << str;
132 }
133 stream << "</tt></small>";
134 return stream.str();
135}
136
137
138std::string HTML::htmlToPlainText(const std::string& html)
139{
140 std::string tmp = html;
141 //conversions to plaintext
142 replace_all(tmp, "<br>", "\n");
143 replace_all(tmp, "</p>", "\n");
144 replace_all(tmp, "</tr>", "\n");
145 replace_all(tmp, "&nbsp;", " ");
146
147 //remove non-ascii \mu
148 replace_all(tmp, "\x0b5", "u");
149
150 //remove all unknown tags (non-greedy match)
151 const static std::regex tagRegex("<.*?>");
152 tmp = std::regex_replace(tmp, tagRegex, "");
153
154 tmp = unescape(tmp);
155
156 return tmp;
157}
158
159std::string HTML::escape(const std::string& str)
160{
161 std::string tmp = str;
162 replace_all(tmp, "&", "&amp;"); //must be first
163 replace_all(tmp, ">", "&gt;");
164 replace_all(tmp, "<", "&lt;");
165 replace_all(tmp, "\"", "&quot;");
166 return tmp;
167}
168
169std::string HTML::unescape(const std::string& str)
170{
171 std::string tmp = str;
172 //replace entities (important ones at least)
173 replace_all(tmp, "&quot;", "\"");
174 replace_all(tmp, "&gt;", ">");
175 replace_all(tmp, "&lt;", "<");
176 replace_all(tmp, "&amp;", "&"); //must be last
177 return tmp;
178}
static double convertValueToUnit(double value, const std::string &unitString)
Converts a floating point value from the standard framework unit to the given unit.
Definition: UnitConst.cc:139
std::string escape(const std::string &str)
Convert &, <, > etc.
Definition: HTML.cc:159
std::string getStringConvertToUnit(const ROOT::Math::XYZVector &vec, int precision=2, const std::string &unitType="cm")
get a string with vector coordinates: (x, y, z).
Definition: HTML.cc:85
std::string chooseUnitOfLength(const ROOT::Math::XYZVector &vec)
get a string with a unit type to convert a vector, so that it is easily readable.
Definition: HTML.cc:102
std::string getHexDump(const int *buf, int length)
Create hexdump of given buffer.
Definition: HTML.cc:121
std::string unescape(const std::string &str)
inverse of escape()
Definition: HTML.cc:169
std::string getString(const TMatrixFBase &matrix, int precision=2, bool color=true)
get HTML table representing a matrix.
Definition: HTML.cc:24
std::string htmlToPlainText(const std::string &html)
Reformat given HTML string into terminal-friendly plain text.
Definition: HTML.cc:138
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24