gstreamer
Loading...
Searching...
No Matches
event.cc
Go to the documentation of this file.
1// gstreamer
4
5// c++
6#include <sstream>
7
8// Implementation summary:
9// Start and finalize one JSON event object using the ordered event publish sequence.
10
11bool GstreamerJsonFactory::startEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) {
12 if (!ofile.is_open()) { log->error(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, "Error: can't access ", filename()); }
13 if (!event_data) {
14 log->error(ERR_PUBLISH_ERROR, "event_data is null in GstreamerJsonFactory::startEventImpl");
15 return false;
16 }
17 if (!event_data->getHeader()) {
18 log->error(ERR_PUBLISH_ERROR, "event header is null in GstreamerJsonFactory::startEventImpl");
19 return false;
20 }
21
22 // Ensure the top-level JSON document is initialized for event output.
23 ensureFileInitializedForType("event");
24
25 // Reset all per-event assembly state.
26 is_building_event = true;
27 current_event.str(std::string());
28 current_event.clear();
29 current_event_has_header = false;
30 current_event_has_any_detector = false;
31 current_event_has_generated = false;
32 current_event_digitized_entries.clear();
33
34 // Cache the event number early so it is available to the root event object.
35 event_number = event_data->getHeader()->getG4LocalEvn();
36
37 current_event << "{";
38 current_event << "\"event_number\": " << event_number;
39
40 // Open the header object now. publishEventHeaderImpl() fills its contents.
41 current_event << ", \"header\": {";
42 return true;
43}
44
45bool GstreamerJsonFactory::endEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) {
46 if (!is_building_event) {
47 log->error(ERR_PUBLISH_ERROR, "endEventImpl called without an active event in GstreamerJsonFactory");
48 return false;
49 }
50
51 // If the caller skipped the header step, emit a minimal fallback and close the header
52 // object (startEventImpl opened it; publishEventHeaderImpl normally writes its fields
53 // and closes it).
54 if (!current_event_has_header) {
55 current_event << "\"timestamp\": \"\", \"thread_id\": -1}";
56 }
57
58 // Ensure a "detectors" object exists so the schema stays predictable and any buffered
59 // digitized data has a place to live, even when no true-information banks were published.
60 bool detectors_has_content = current_event_has_any_detector;
61 if (!current_event_has_any_detector) {
62 current_event << ", \"detectors\": {";
63 current_event_has_any_detector = true;
64 }
65
66 // Emit all buffered digitized detector arrays as a single, well-formed
67 // "digitized_by_detector" object nested inside "detectors".
68 if (!current_event_digitized_entries.empty()) {
69 if (detectors_has_content) { current_event << ", "; }
70 current_event << "\"digitized_by_detector\": {";
71 bool first = true;
72 for (const auto& entry : current_event_digitized_entries) {
73 if (!first) { current_event << ", "; }
74 first = false;
75 current_event << entry;
76 }
77 current_event << "}";
78 }
79
80 current_event << "}"; // close "detectors"
81 current_event << "}"; // close the event object
82
83 writeTopLevelEntry(current_event.str());
84
85 is_building_event = false;
86
87 (void)event_data;
88 return true;
89}
90
91bool GstreamerJsonFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
92 const GGeneratedParticleBank& particles) {
93 if (!is_building_event) {
95 "publishEventGeneratedParticlesImpl called without an active event in GstreamerJsonFactory");
96 return false;
97 }
98
99 if (!current_event_has_generated) {
100 current_event << ", \"generated\": {";
101 current_event_has_generated = true;
102 }
103 else {
104 current_event << ", ";
105 }
106
107 current_event << "\"" << jsonEscape(bankName) << "\": [";
108
109 bool wrote_first_particle = false;
110 for (const auto& particle : particles) {
111 if (wrote_first_particle) { current_event << ", "; }
112 wrote_first_particle = true;
113
114 current_event << "{"
115 << "\"name\": \"" << jsonEscape(particle.name) << "\""
116 << ", \"pid\": " << particle.pid
117 << ", \"type\": " << particle.type
118 << ", \"multiplicity\": " << particle.multiplicity
119 << ", \"p\": " << particle.p
120 << ", \"theta\": " << particle.theta
121 << ", \"phi\": " << particle.phi
122 << ", \"vx\": " << particle.vx
123 << ", \"vy\": " << particle.vy
124 << ", \"vz\": " << particle.vz
125 << "}";
126 }
127
128 current_event << "]";
129 if (bankName == "generated_tracked") {
130 current_event << "}";
131 }
132
133 return true;
134}
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
std::vector< GGeneratedParticleData > GGeneratedParticleBank
Shared constants and error codes for the gstreamer module.
#define ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
#define ERR_CANTOPENOUTPUT
Output medium could not be opened successfully.
JSON streamer plugin declarations.