Skip to content

Commit 5958beb

Browse files
committed
Improve heatmap visualization
1 parent 8ba9c56 commit 5958beb

File tree

4 files changed

+117
-47
lines changed

4 files changed

+117
-47
lines changed

src/examples/tutorial_with_lightweight_datamodel/04_ascii_heatmap_writer/AsciiHeatmap_writer.cc

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "AsciiHeatmap_writer.h"
66
#include "CalorimeterHit.h"
7+
#include <string>
78

89
AsciiHeatmap_writer::AsciiHeatmap_writer() {
910
SetTypeName(NAME_OF_THIS); // Provide JANA with this class's name
@@ -15,33 +16,49 @@ AsciiHeatmap_writer::AsciiHeatmap_writer() {
1516

1617
void AsciiHeatmap_writer::Init() {
1718
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer::Init: Initializing heatmap";
19+
m_heatmap.resize(m_cell_cols() * m_cell_rows());
20+
21+
if (*m_use_unicode) {
22+
m_ramp = {" ", "", "", "", ""};
23+
m_box_topleft = "";
24+
m_box_topright = "";
25+
m_box_bottomleft = "";
26+
m_box_bottomright = "";
27+
m_box_vertical = "";
28+
m_box_horizontal = "";
29+
}
30+
else {
31+
m_ramp = {" ", ".", ":", "%", "#"};
32+
m_box_topleft = "+";
33+
m_box_topright = "+";
34+
m_box_bottomleft = "+";
35+
m_box_bottomright = "+";
36+
m_box_vertical = "|";
37+
m_box_horizontal = "-";
38+
}
39+
}
1840

19-
m_heatmap = std::make_unique<double[]>(m_cell_cols() * m_cell_rows());
41+
void AsciiHeatmap_writer::ProcessSequential(const JEvent& event) {
42+
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer::Process, Event #" << event.GetEventNumber();
2043

44+
// Clear heatmap
2145
for (size_t i=0; i < m_cell_rows(); ++i) {
2246
for (size_t j=0; j < m_cell_cols(); ++j) {
2347
m_heatmap[i* m_cell_cols() + j] = 0;
2448
}
2549
}
26-
}
27-
28-
void AsciiHeatmap_writer::ProcessSequential(const JEvent& event) {
29-
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer::Process, Event #" << event.GetEventNumber();
3050

51+
// Populate heatmap with sum of hit values
3152
for (const CalorimeterHit* hit : m_hits_in()) {
3253
if (hit->row < (int) m_cell_rows() && hit->col < (int) m_cell_cols()) {
33-
m_heatmap[hit->row* m_cell_cols() + hit->col] = hit->energy;
54+
m_heatmap[hit->row* m_cell_cols() + hit->col] += hit->energy;
3455
}
3556
else {
3657
LOG_WARN(GetLogger()) << "Hit at row=" << hit->row << ", col=" << hit->col << " does not fit on heatmap";
3758
}
3859
}
39-
}
40-
41-
void AsciiHeatmap_writer::Finish() {
42-
// Close any resources
43-
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer::Finish: Displaying heatmap";
4460

61+
// Find min and max values
4562
double min_value = m_heatmap[0];
4663
double max_value = m_heatmap[0];
4764

@@ -53,27 +70,35 @@ void AsciiHeatmap_writer::Finish() {
5370
}
5471
}
5572

56-
std::cout << " +";
73+
// Show heatmap
74+
std::cout << "Event: " << event.GetEventNumber() << std::endl;
75+
std::cout << " " << m_box_topleft;
5776
for (size_t j=0; j<m_cell_cols(); ++j) {
58-
std::cout << "-";
77+
std::cout << m_box_horizontal << m_box_horizontal;
5978
}
60-
std::cout << "+" << std::endl;
79+
std::cout << m_box_topright << std::endl;
80+
6181

62-
char ramp[] = " .:#";
6382
for (size_t i=0; i<m_cell_rows(); ++i) {
64-
std::cout << " |";
83+
std::cout << " " << m_box_vertical;
6584
for (size_t j=0; j<m_cell_cols(); ++j) {
6685
double value = m_heatmap[i*m_cell_cols() + j];
67-
int shade = (max_value == min_value) ? 0 : int((value - min_value)/(max_value - min_value) * 3);
68-
std::cout << ramp[shade];
86+
int shade = (max_value == min_value) ? 0 : int((value - min_value)/(max_value - min_value) * 4);
87+
std::cout << m_ramp[shade] << m_ramp[shade]; // Double up so that cells look square
6988
}
70-
std::cout << "|" << std::endl;
89+
std::cout << m_box_vertical << std::endl;
7190
}
7291

73-
std::cout << " +";
92+
std::cout << " " << m_box_bottomleft;
7493
for (size_t j=0; j<m_cell_cols(); ++j) {
75-
std::cout << "-";
94+
std::cout << m_box_horizontal << m_box_horizontal;
7695
}
77-
std::cout << "+" << std::endl;
96+
std::cout << m_box_bottomright << std::endl;
97+
98+
}
99+
100+
void AsciiHeatmap_writer::Finish() {
101+
// Close any resources, write any files
102+
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer::Finish";
78103
}
79104

src/examples/tutorial_with_lightweight_datamodel/04_ascii_heatmap_writer/AsciiHeatmap_writer.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ class AsciiHeatmap_writer : public JEventProcessor {
1515
// In a more realistic example, these would be obtained from the geometry interface
1616
Parameter<size_t> m_cell_cols {this, "cell_cols", 20, "Number of columns in the detector"};
1717
Parameter<size_t> m_cell_rows {this, "cell_rows", 10, "Number of rows in the detector"};
18+
Parameter<bool> m_use_unicode {this, "use_unicode", true, "Use Unicode visualization vs plain ASCII"};
1819

1920
// Declare the resource (or a handle to the resource) that is protected by this JEventProcessor
20-
std::unique_ptr<double[]> m_heatmap;
21+
std::vector<double> m_heatmap;
22+
23+
// Characters to print
24+
std::vector<std::string> m_ramp;
25+
std::string m_box_topleft;
26+
std::string m_box_topright;
27+
std::string m_box_bottomleft;
28+
std::string m_box_bottomright;
29+
std::string m_box_horizontal;
30+
std::string m_box_vertical;
2131

2232
public:
2333

src/examples/tutorial_with_lightweight_datamodel/04_ascii_heatmap_writer/AsciiHeatmap_writer_legacy.cc

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@ AsciiHeatmap_writer_legacy::AsciiHeatmap_writer_legacy() {
1616
void AsciiHeatmap_writer_legacy::Init() {
1717
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer_legacy::Init: Initializing heatmap";
1818

19-
m_heatmap = std::make_unique<double[]>(m_cell_cols() * m_cell_rows());
20-
21-
for (size_t i=0; i < m_cell_rows(); ++i) {
22-
for (size_t j=0; j < m_cell_cols(); ++j) {
23-
m_heatmap[i* m_cell_cols() + j] = 0;
24-
}
19+
m_heatmap.resize(m_cell_cols() * m_cell_rows());
20+
21+
if (*m_use_unicode) {
22+
m_ramp = {" ", "", "", "", ""};
23+
m_box_topleft = "";
24+
m_box_topright = "";
25+
m_box_bottomleft = "";
26+
m_box_bottomright = "";
27+
m_box_vertical = "";
28+
m_box_horizontal = "";
29+
}
30+
else {
31+
m_ramp = {" ", ".", ":", "%", "#"};
32+
m_box_topleft = "+";
33+
m_box_topright = "+";
34+
m_box_bottomleft = "+";
35+
m_box_bottomright = "+";
36+
m_box_vertical = "|";
37+
m_box_horizontal = "-";
2538
}
2639
}
2740

@@ -37,20 +50,25 @@ void AsciiHeatmap_writer_legacy::Process(const std::shared_ptr<const JEvent>& ev
3750

3851
/// Do the rest sequentially
3952
/// Now we are free to access shared state such as m_heatmap
53+
54+
// First we clear the heatmap buffer
55+
for (size_t i=0; i < m_cell_rows(); ++i) {
56+
for (size_t j=0; j < m_cell_cols(); ++j) {
57+
m_heatmap[i* m_cell_cols() + j] = 0;
58+
}
59+
}
60+
61+
// Populate heatmap with sum of hit values
4062
for (const CalorimeterHit* hit : hits) {
4163
if (hit->row < (int) m_cell_rows() && hit->col < (int) m_cell_cols()) {
42-
m_heatmap[hit->row* m_cell_cols() + hit->col] = hit->energy;
64+
m_heatmap[hit->row* m_cell_cols() + hit->col] += hit->energy;
4365
}
4466
else {
4567
LOG_WARN(GetLogger()) << "Hit at row=" << hit->row << ", col=" << hit->col << " does not fit on heatmap";
4668
}
4769
}
48-
}
49-
50-
void AsciiHeatmap_writer_legacy::Finish() {
51-
// Close any resources
52-
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer_legacy::Finish: Displaying heatmap";
5370

71+
// Find min and max values
5472
double min_value = m_heatmap[0];
5573
double max_value = m_heatmap[0];
5674

@@ -62,27 +80,35 @@ void AsciiHeatmap_writer_legacy::Finish() {
6280
}
6381
}
6482

65-
std::cout << " +";
83+
// Show heatmap
84+
std::cout << "Event: " << event->GetEventNumber() << std::endl;
85+
std::cout << " " << m_box_topleft;
6686
for (size_t j=0; j<m_cell_cols(); ++j) {
67-
std::cout << "-";
87+
std::cout << m_box_horizontal << m_box_horizontal;
6888
}
69-
std::cout << "+" << std::endl;
89+
std::cout << m_box_topright << std::endl;
90+
7091

71-
char ramp[] = " .:#";
7292
for (size_t i=0; i<m_cell_rows(); ++i) {
73-
std::cout << " |";
93+
std::cout << " " << m_box_vertical;
7494
for (size_t j=0; j<m_cell_cols(); ++j) {
7595
double value = m_heatmap[i*m_cell_cols() + j];
76-
int shade = (max_value == min_value) ? 0 : int((value - min_value)/(max_value - min_value) * 3);
77-
std::cout << ramp[shade];
96+
int shade = (max_value == min_value) ? 0 : int((value - min_value)/(max_value - min_value) * 4);
97+
std::cout << m_ramp[shade] << m_ramp[shade]; // Double up so that cells look square
7898
}
79-
std::cout << "|" << std::endl;
99+
std::cout << m_box_vertical << std::endl;
80100
}
81101

82-
std::cout << " +";
102+
std::cout << " " << m_box_bottomleft;
83103
for (size_t j=0; j<m_cell_cols(); ++j) {
84-
std::cout << "-";
104+
std::cout << m_box_horizontal << m_box_horizontal;
85105
}
86-
std::cout << "+" << std::endl;
106+
std::cout << m_box_bottomright << std::endl;
107+
}
108+
109+
void AsciiHeatmap_writer_legacy::Finish() {
110+
// Close any resources
111+
LOG_INFO(GetLogger()) << "AsciiHeatmap_writer_legacy::Finish: Displaying heatmap";
112+
87113
}
88114

src/examples/tutorial_with_lightweight_datamodel/04_ascii_heatmap_writer/AsciiHeatmap_writer_legacy.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ class AsciiHeatmap_writer_legacy : public JEventProcessor {
1111

1212
Parameter<size_t> m_cell_cols {this, "cell_cols", 20, "Number of columns in the detector"};
1313
Parameter<size_t> m_cell_rows {this, "cell_rows", 10, "Number of rows in the detector"};
14+
Parameter<bool> m_use_unicode {this, "use_unicode", true, "Use Unicode visualization vs plain ASCII"};
1415

15-
std::unique_ptr<double[]> m_heatmap;
16+
std::vector<double> m_heatmap;
17+
18+
std::vector<std::string> m_ramp;
19+
std::string m_box_topleft;
20+
std::string m_box_topright;
21+
std::string m_box_bottomleft;
22+
std::string m_box_bottomright;
23+
std::string m_box_horizontal;
24+
std::string m_box_vertical;
1625

1726
public:
1827

0 commit comments

Comments
 (0)