gstreamer
Loading...
Searching...
No Matches
publishDigitized.cc
Go to the documentation of this file.
1// gstreamer
4
5// c++
6#include <sstream>
7
8// Implementation summary:
9// Append digitized detector content to the current JSON event object.
10// This implementation keeps the JSON valid without performing in-place string editing.
11
12bool GstreamerJsonFactory::publishEventDigitizedDataImpl(const std::string& detectorName,
13 const std::vector<const GDigitizedData*>& digitizedData) {
14 if (!is_building_event) {
15 log->error(
16 ERR_PUBLISH_ERROR, "publishEventDigitizedDataImpl called without an active event in GstreamerJsonFactory");
17 return false;
18 }
19
20 // The current design appends digitized content into a reserved object nested under
21 // "detectors". This avoids rewriting previously emitted detector JSON.
22 if (digitizedData.empty()) return true;
23
24 static const char* marker = "\"digitized_by_detector\": {";
25 const std::string assembled = current_event.str();
26
27 const bool has_digitized_container = (assembled.find(marker) != std::string::npos);
28
29 if (!has_digitized_container) {
30 // Ensure the event already has a detectors object before reserving a nested map
31 // for digitized collections keyed by detector name.
32 if (!current_event_has_any_detector) {
33 current_event << ", \"detectors\": {";
34 current_event_has_any_detector = true;
35 }
36 else {
37 current_event << ", ";
38 }
39
40 current_event << "\"digitized_by_detector\": {";
41 }
42
43 // If the reserved object already contains one detector entry, append a comma
44 // before adding the next one.
45 const std::string updated = current_event.str();
46 if (!updated.empty()) {
47 char last = updated.back();
48 if (last != '{') current_event << ", ";
49 }
50
51 current_event << "\"" << jsonEscape(detectorName) << "\": [";
52
53 bool wrote_first_hit = false;
54 for (const auto* hit : digitizedData) {
55 if (!hit) continue;
56
57 if (wrote_first_hit) current_event << ", ";
58 wrote_first_hit = true;
59
60 current_event << "{";
61
62 const std::string addr = hit->getIdentityString();
63 current_event << "\"address\": \"" << jsonEscape(addr) << "\"";
64
65 current_event << ", \"vars\": {";
66
67 bool wrote_first_var = false;
68
69 // Integer observables:
70 // the argument 0 means "do not include SRO variables".
71 for (const auto& [name, value] : hit->getIntObservablesMap(0)) {
72 if (wrote_first_var) current_event << ", ";
73 wrote_first_var = true;
74 current_event << "\"" << jsonEscape(name) << "\": " << value;
75 }
76
77 // Floating-point observables.
78 for (const auto& [name, value] : hit->getDblObservablesMap(0)) {
79 if (wrote_first_var) current_event << ", ";
80 wrote_first_var = true;
81 current_event << "\"" << jsonEscape(name) << "\": " << value;
82 }
83
84 current_event << "}";
85 current_event << "}";
86 }
87
88 current_event << "]";
89
90 // Close the temporary digitized_by_detector object immediately so the enclosing
91 // event remains structurally valid after this call.
92 current_event << "}";
93
94 return true;
95}
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
Shared constants and error codes for the gstreamer module.
#define ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
JSON streamer plugin declarations.