Belle II Software development
Belle2::HTML Namespace Reference

return information on objects in a nice format. More...

Functions

std::string getString (const TMatrixFBase &matrix, int precision=2, bool color=true)
 get HTML table representing a matrix.
 
std::string getString (const TMatrixDBase &matrix, int precision=2, bool color=true)
 get HTML table representing a matrix (double precision).
 
std::string getString (const ROOT::Math::XYZVector &vec, int precision=2)
 get a string with vector coordinates: (x, y, z).
 
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).
 
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.
 
std::string getHexDump (const int *buf, int length)
 Create hexdump of given buffer.
 
std::string htmlToPlainText (const std::string &html)
 Reformat given HTML string into terminal-friendly plain text.
 
std::string escape (const std::string &str)
 Convert &, <, > etc.
 
std::string unescape (const std::string &str)
 inverse of escape()
 

Detailed Description

return information on objects in a nice format.

See also
RelationsObject::getInfoHTML()

Function Documentation

◆ chooseUnitOfLength()

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.

valid unit types are (um, cm) for now. the maximum of the vector entries defines the unit.

Definition at line 102 of file HTML.cc.

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}

◆ escape()

std::string escape ( const std::string &  str)

Convert &, <, > etc.

to entities. (not a complete list!)

Definition at line 159 of file HTML.cc.

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}

◆ getHexDump()

std::string getHexDump ( const int *  buf,
int  length 
)

Create hexdump of given buffer.

Parameters
bufthe buffer
lengthsize of buf in 32bit words

Definition at line 121 of file HTML.cc.

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}

◆ getString() [1/3]

std::string getString ( const ROOT::Math::XYZVector &  vec,
int  precision = 2 
)

get a string with vector coordinates: (x, y, z).

(uses fixed-length output).

Parameters
vecthe vector to be printend
precisionThe amount of digits to use

Definition at line 76 of file HTML.cc.

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}

◆ getString() [2/3]

std::string getString ( const TMatrixDBase &  matrix,
int  precision = 2,
bool  color = true 
)

get HTML table representing a matrix (double precision).

Parameters
matrixThe matrix to be represented
precisionThe amount of significant digits to use
colorIf true, vary background colour depending on value.

Definition at line 36 of file HTML.cc.

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}

◆ getString() [3/3]

std::string getString ( const TMatrixFBase &  matrix,
int  precision = 2,
bool  color = true 
)

get HTML table representing a matrix.

Parameters
matrixThe matrix to be represented
precisionThe amount of significant digits to use
colorIf true, vary background colour depending on value.

Definition at line 24 of file HTML.cc.

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}
std::string getString(const TMatrixFBase &matrix, int precision=2, bool color=true)
get HTML table representing a matrix.
Definition: HTML.cc:24

◆ getStringConvertToUnit()

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).

(uses fixed-length output). converts floating point value from standard framework unit to given unit.

Parameters
vecthe vector to be printend
precisionThe amount of digits to use
unitTypeDefines the unit to convert the vector values from standard framework unit.

Definition at line 85 of file HTML.cc.

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}
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

◆ htmlToPlainText()

std::string htmlToPlainText ( const std::string &  html)

Reformat given HTML string into terminal-friendly plain text.

Definition at line 138 of file HTML.cc.

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}
std::string unescape(const std::string &str)
inverse of escape()
Definition: HTML.cc:169

◆ unescape()

std::string unescape ( const std::string &  str)

inverse of escape()

Definition at line 169 of file HTML.cc.

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}