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
33 // Cache the event number early so it is available to the root event object.
34 event_number = event_data->getHeader()->getG4LocalEvn();
35
36 current_event << "{";
37 current_event << "\"event_number\": " << event_number;
38
39 // Open the header object now. publishEventHeaderImpl() fills its contents.
40 current_event << ", \"header\": {";
41 return true;
42}
43
44bool GstreamerJsonFactory::endEventImpl(const std::shared_ptr<GEventDataCollection>& event_data) {
45 if (!is_building_event) {
46 log->error(ERR_PUBLISH_ERROR, "endEventImpl called without an active event in GstreamerJsonFactory");
47 return false;
48 }
49
50 // If the caller skipped the header step, emit a minimal fallback so the JSON remains valid.
51 if (!current_event_has_header) {
52 current_event << "\"timestamp\": \"\", \"thread_id\": -1";
53 }
54
55 // Detector publishers leave the "detectors" object open so digitized data
56 // can be appended after true-information banks without rewriting strings.
57 if (current_event_has_any_detector) {
58 current_event << "}";
59 }
60 current_event << "}";
61
62 // Keep the event schema predictable even when no detector banks were published.
63 if (!current_event_has_any_detector) {
64 current_event << ", \"detectors\": {}";
65 }
66
67 current_event << "}";
68
69 writeTopLevelEntry(current_event.str());
70
71 is_building_event = false;
72
73 (void)event_data;
74 return true;
75}
76
77bool GstreamerJsonFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
78 const GGeneratedParticleBank& particles) {
79 if (!is_building_event) {
81 "publishEventGeneratedParticlesImpl called without an active event in GstreamerJsonFactory");
82 return false;
83 }
84
85 if (!current_event_has_generated) {
86 current_event << ", \"generated\": {";
87 current_event_has_generated = true;
88 }
89 else {
90 current_event << ", ";
91 }
92
93 current_event << "\"" << jsonEscape(bankName) << "\": [";
94
95 bool wrote_first_particle = false;
96 for (const auto& particle : particles) {
97 if (wrote_first_particle) { current_event << ", "; }
98 wrote_first_particle = true;
99
100 current_event << "{"
101 << "\"name\": \"" << jsonEscape(particle.name) << "\""
102 << ", \"pid\": " << particle.pid
103 << ", \"type\": " << particle.type
104 << ", \"multiplicity\": " << particle.multiplicity
105 << ", \"p\": " << particle.p
106 << ", \"theta\": " << particle.theta
107 << ", \"phi\": " << particle.phi
108 << ", \"vx\": " << particle.vx
109 << ", \"vy\": " << particle.vy
110 << ", \"vz\": " << particle.vz
111 << "}";
112 }
113
114 current_event << "]";
115 if (bankName == "generated_tracked") {
116 current_event << "}";
117 }
118
119 return true;
120}
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.