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