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()) {
13 log->error(gstreamer::ERR_CANTOPENOUTPUT, "%1Error: can't access %2", SFUNCTION_NAME, filename());
14 }
15 if (!event_data) {
16 log->error(gstreamer::ERR_PUBLISH_ERROR, "event_data is null in GstreamerJsonFactory::startEventImpl");
17 return false;
18 }
19 if (!event_data->getHeader()) {
20 log->error(
21 gstreamer::ERR_PUBLISH_ERROR, "event header is null in GstreamerJsonFactory::startEventImpl");
22 return false;
23 }
24
25 // Ensure the top-level JSON document is initialized for event output.
26 ensureFileInitializedForType("event");
27
28 // Reset all per-event assembly state.
29 is_building_event = true;
30 current_event.str(std::string());
31 current_event.clear();
32 current_event_has_header = false;
33 current_event_has_any_detector = false;
34 current_event_has_generated = false;
35 current_event_digitized_entries.clear();
36
37 // Cache the event number early so it is available to the root event object.
38 event_number = event_data->getHeader()->getG4LocalEvn();
39
40 current_event << "{";
41 current_event << "\"event_number\": " << event_number;
42
43 // Open the header object now. publishEventHeaderImpl() fills its contents.
44 current_event << ", \"header\": {";
45 return true;
46}
47
48bool GstreamerJsonFactory::endEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) {
49 if (!is_building_event) {
50 log->error(
52 "endEventImpl called without an active event in GstreamerJsonFactory");
53 return false;
54 }
55
56 // If the caller skipped the header step, emit a minimal fallback and close the header
57 // object (startEventImpl opened it; publishEventHeaderImpl normally writes its fields
58 // and closes it).
59 if (!current_event_has_header) {
60 current_event << "\"timestamp\": \"\", \"thread_id\": -1}";
61 }
62
63 // Ensure a "detectors" object exists so the schema stays predictable and any buffered
64 // digitized data has a place to live, even when no true-information banks were published.
65 bool detectors_has_content = current_event_has_any_detector;
66 if (!current_event_has_any_detector) {
67 current_event << ", \"detectors\": {";
68 current_event_has_any_detector = true;
69 }
70
71 // Emit all buffered digitized detector arrays as a single, well-formed
72 // "digitized_by_detector" object nested inside "detectors".
73 if (!current_event_digitized_entries.empty()) {
74 if (detectors_has_content) { current_event << ", "; }
75 current_event << "\"digitized_by_detector\": {";
76 bool first = true;
77 for (const auto& entry : current_event_digitized_entries) {
78 if (!first) { current_event << ", "; }
79 first = false;
80 current_event << entry;
81 }
82 current_event << "}";
83 }
84
85 current_event << "}"; // close "detectors"
86 current_event << "}"; // close the event object
87
88 writeTopLevelEntry(current_event.str());
89
90 is_building_event = false;
91
92 (void)event_data;
93 return true;
94}
95
96bool GstreamerJsonFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
97 const GGeneratedParticleBank& particles) {
98 if (!is_building_event) {
99 log->error(
101 "publishEventGeneratedParticlesImpl called without an active event in GstreamerJsonFactory");
102 return false;
103 }
104
105 if (!current_event_has_generated) {
106 current_event << ", \"generated\": {";
107 current_event_has_generated = true;
108 }
109 else {
110 current_event << ", ";
111 }
112
113 current_event << "\"" << jsonEscape(bankName) << "\": [";
114
115 bool wrote_first_particle = false;
116 for (const auto& particle : particles) {
117 if (wrote_first_particle) { current_event << ", "; }
118 wrote_first_particle = true;
119
120 current_event << "{"
121 << "\"name\": \"" << jsonEscape(particle.name) << "\""
122 << ", \"pid\": " << particle.pid
123 << ", \"type\": " << particle.type
124 << ", \"multiplicity\": " << particle.multiplicity
125 << ", \"p\": " << particle.p
126 << ", \"theta\": " << particle.theta
127 << ", \"phi\": " << particle.phi
128 << ", \"vx\": " << particle.vx
129 << ", \"vy\": " << particle.vy
130 << ", \"vz\": " << particle.vz
131 << "}";
132 }
133
134 current_event << "]";
135 if (bankName == "generated_tracked") {
136 current_event << "}";
137 }
138
139 return true;
140}
141
142bool GstreamerJsonFactory::publishEventAncestorsImpl(const GAncestorBank& ancestors) {
143 if (!is_building_event) {
144 log->error(
146 "publishEventAncestorsImpl called without an active event in GstreamerJsonFactory");
147 return false;
148 }
149
150 current_event << ", \"ancestors\": [";
151 bool first = true;
152 for (const auto& ancestor : ancestors) {
153 if (!first) { current_event << ", "; }
154 first = false;
155 current_event << "{\"pid\": " << ancestor.pid
156 << ", \"tid\": " << ancestor.tid
157 << ", \"mtid\": " << ancestor.mtid
158 << ", \"trackE\": " << ancestor.trackE
159 << ", \"px\": " << ancestor.px
160 << ", \"py\": " << ancestor.py
161 << ", \"pz\": " << ancestor.pz
162 << ", \"vx\": " << ancestor.vx
163 << ", \"vy\": " << ancestor.vy
164 << ", \"vz\": " << ancestor.vz
165 << "}";
166 }
167 current_event << "]";
168 return true;
169}
std::shared_ptr< GLogger > log
std::vector< GAncestorData > GAncestorBank
std::vector< GGeneratedParticleData > GGeneratedParticleBank
Shared constants and error codes for the gstreamer module.
JSON streamer plugin declarations.
constexpr int ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
constexpr int ERR_CANTOPENOUTPUT
Output medium could not be opened successfully.