@@ -17,80 +17,54 @@ bool GstreamerJsonFactory::publishEventDigitizedDataImpl(const std::string&
1717 return false ;
1818 }
1919
20- // The current design appends digitized content into a reserved object nested under
21- // "detectors". This avoids rewriting previously emitted detector JSON.
20+ // Build this detector's digitized array into a standalone entry and buffer it.
21+ // endEventImpl emits all buffered entries together as the "digitized_by_detector"
22+ // object, which keeps the JSON valid regardless of how true-info and digitized
23+ // publish calls interleave across detectors.
2224 if (digitizedData.empty ()) return true ;
2325
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) << " \" : [" ;
26+ std::ostringstream entry;
27+ entry << " \" " << jsonEscape (detectorName) << " \" : [" ;
5228
5329 bool wrote_first_hit = false ;
5430 for (const auto * hit : digitizedData) {
5531 if (!hit) continue ;
5632
57- if (wrote_first_hit) current_event << " , " ;
33+ if (wrote_first_hit) entry << " , " ;
5834 wrote_first_hit = true ;
5935
60- current_event << " {" ;
36+ entry << " {" ;
6137
6238 auto addr = getIdentityString (hit->getIdentity ());
6339
64- current_event << " \" address\" : \" " << jsonEscape (addr) << " \" " ;
40+ entry << " \" address\" : \" " << jsonEscape (addr) << " \" " ;
6541
66- current_event << " , \" vars\" : {" ;
42+ entry << " , \" vars\" : {" ;
6743
6844 bool wrote_first_var = false ;
6945
7046 // Integer observables:
7147 // the argument 0 means "do not include SRO variables".
7248 for (const auto & [name, value] : hit->getIntObservablesMap (0 )) {
73- if (wrote_first_var) current_event << " , " ;
49+ if (wrote_first_var) entry << " , " ;
7450 wrote_first_var = true ;
75- current_event << " \" " << jsonEscape (name) << " \" : " << value;
51+ entry << " \" " << jsonEscape (name) << " \" : " << value;
7652 }
7753
7854 // Floating-point observables.
7955 for (const auto & [name, value] : hit->getDblObservablesMap (0 )) {
80- if (wrote_first_var) current_event << " , " ;
56+ if (wrote_first_var) entry << " , " ;
8157 wrote_first_var = true ;
82- current_event << " \" " << jsonEscape (name) << " \" : " << value;
58+ entry << " \" " << jsonEscape (name) << " \" : " << value;
8359 }
8460
85- current_event << " }" ;
86- current_event << " }" ;
61+ entry << " }" ;
62+ entry << " }" ;
8763 }
8864
89- current_event << " ]" ;
65+ entry << " ]" ;
9066
91- // Close the temporary digitized_by_detector object immediately so the enclosing
92- // event remains structurally valid after this call.
93- current_event << " }" ;
67+ current_event_digitized_entries.push_back (entry.str ());
9468
9569 return true ;
9670}
0 commit comments