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 auto addr = getIdentityString(hit->getIdentity());
63
64 current_event << "\"address\": \"" << jsonEscape(addr) << "\"";
65
66 current_event << ", \"vars\": {";
67
68 bool wrote_first_var = false;
69
70 // Integer observables:
71 // the argument 0 means "do not include SRO variables".
72 for (const auto& [name, value] : hit->getIntObservablesMap(0)) {
73 if (wrote_first_var) current_event << ", ";
74 wrote_first_var = true;
75 current_event << "\"" << jsonEscape(name) << "\": " << value;
76 }
77
78 // Floating-point observables.
79 for (const auto& [name, value] : hit->getDblObservablesMap(0)) {
80 if (wrote_first_var) current_event << ", ";
81 wrote_first_var = true;
82 current_event << "\"" << jsonEscape(name) << "\": " << value;
83 }
84
85 current_event << "}";
86 current_event << "}";
87 }
88
89 current_event << "]";
90
91 // Close the temporary digitized_by_detector object immediately so the enclosing
92 // event remains structurally valid after this call.
93 current_event << "}";
94
95 return true;
96}
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
std::string getIdentityString(std::vector< GIdentifier > gidentity)
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.