gstreamer
Loading...
Searching...
No Matches
gstreamer.cc
Go to the documentation of this file.
1// gstreamer
2#include "gstreamer.h"
3
4// gemc
5#include "gutilities.h"
6
7// Implementation summary:
8// Common base-class logic for format validation, buffered event publication,
9// and immediate run publication. Concrete serialization remains in plugin hooks.
10
11const std::vector<std::string>& GStreamer::supported_formats() {
12 // Keep this list aligned with the available gstreamer_<format>_plugin factories.
13 static const std::vector<std::string> formats = {"jlabsro", "root", "ascii", "csv", "json"};
14 return formats;
15}
16
17bool GStreamer::is_valid_format(const std::string& format) {
18 const auto& supported = GStreamer::supported_formats();
19 const auto f = gutilities::convertToLowercase(format);
20 return std::find(supported.begin(), supported.end(), f) != supported.end();
21}
22
23
24// pragma todo: pass someting like map<string, bitset> to each detector to decide which data to publish
25void GStreamer::publishEventData(const std::shared_ptr<GEventDataCollection>& event_data) {
26 // The event collection and its header are required for any plugin to publish
27 // a meaningful event record.
28 if (!event_data) {
29 log->error(gstreamer::ERR_PUBLISH_ERROR, "event data is null in GStreamer::publishEventData");
30 }
31 if (!event_data->getHeader()) {
32 log->error(gstreamer::ERR_PUBLISH_ERROR, "event header is null in GStreamer::publishEventData");
33 }
34
35 // Retain ownership of the event until the buffer is flushed. This guarantees
36 // that raw pointers extracted later from hit collections remain valid.
37 eventBuffer.emplace_back(event_data);
38
39 // Once the configured threshold is reached, publish all buffered events in one pass.
40 if (eventBuffer.size() >= bufferFlushLimit) { flushEventBuffer(); }
41}
42
43
44// Implementation summary:
45// Run data are published immediately instead of being buffered. The publish order
46// mirrors the base-class run sequence: start, header, detector banks, end.
47void GStreamer::publishRunData(const std::shared_ptr<GRunDataCollection>& run_data) {
48 log->info(2, "GStreamer::publishRunData->startRun: ",
50
51 log->info(2, SFUNCTION_NAME, "->publishRunHeader -> ",
52 gutilities::success_or_fail(publishRunHeader(run_data->getHeader())));
53
54 // Iterate over each detector collection and expose it to the plugin as a
55 // vector of raw pointers. The owning run collection remains alive throughout
56 // this method call.
57 for (const auto& [sdname, gDataCollection] : run_data->getDataCollectionMap()) {
58 const GDataCollection* tdptr = gDataCollection.get();
59
60 // Extract digitized hits into a flat raw-pointer view expected by the hooks.
61 std::vector<const GDigitizedData*> digitizedPtrs;
62 digitizedPtrs.reserve(tdptr->getDigitizedData().size());
63
64 for (const auto& hit : tdptr->getDigitizedData()) { digitizedPtrs.push_back(hit.get()); }
65
66 log->info(2, SFUNCTION_NAME, "->publishEventDigitizedData for detector -> ", sdname,
68 }
69
70 log->info(2, "GStreamer::endEvent -> ",
72}
73
74
76 log->info(2, "GStreamer::flushEventBuffer -> flushing ", eventBuffer.size(), " events to file");
77
78 // Each buffered event is treated as read-only while the plugin hooks serialize it.
79 // The buffer's shared_ptr ownership keeps all event-owned hit objects alive during the flush.
80 for (const auto& eventData : eventBuffer) {
81 log->info(2, SFUNCTION_NAME, "->startEvent: ",
83
84 log->info(2, SFUNCTION_NAME, "->publishEventHeader -> ",
85 gutilities::success_or_fail(publishEventHeader(eventData->getHeader())));
86
87 log->info(2, SFUNCTION_NAME, "->publishEventGeneratedParticles -> generated: ",
89 eventData->getGeneratedParticles())));
90 log->info(2, SFUNCTION_NAME, "->publishEventGeneratedParticles -> generated_tracked: ",
92 eventData->getGeneratedTrackedParticles())));
93 if (eventData->hasAncestorBank()) {
94 log->info(2, SFUNCTION_NAME, "->publishEventAncestors: ",
95 gutilities::success_or_fail(publishEventAncestors(eventData->getAncestors())));
96 }
97
98 // Publish one detector collection at a time.
99 for (const auto& [sdname, gDataCollection] : eventData->getDataCollectionMap()) {
100 const GDataCollection* tdptr = gDataCollection.get();
101
102 // Convert ownership-bearing containers into temporary raw-pointer views.
103 // Plugins consume these views immediately and do not own the pointed data.
104 std::vector<const GTrueInfoData*> trueInfoPtrs;
105 std::vector<const GDigitizedData*> digitizedPtrs;
106 trueInfoPtrs.reserve(tdptr->getTrueInfoData().size());
107 digitizedPtrs.reserve(tdptr->getDigitizedData().size());
108
109 for (const auto& hit : tdptr->getTrueInfoData()) { trueInfoPtrs.push_back(hit.get()); }
110 for (const auto& hit : tdptr->getDigitizedData()) { digitizedPtrs.push_back(hit.get()); }
111
112 log->info(2, SFUNCTION_NAME, "->publishEventTrueInfoData for detector -> ", sdname,
114
115 log->info(2, SFUNCTION_NAME, "->publishEventDigitizedData for detector -> ", sdname,
117 }
118
119 log->info(2, "GStreamer::endEvent -> ", gutilities::success_or_fail(endEvent(eventData)));
120 }
121
122 // All buffered events have now been handed to the plugin hooks.
123 eventBuffer.clear();
124}
125
126// stream an individual frame
127// void GStreamer::publishFrameRunData(const std::shared_ptr<GFrameDataCollection>& frameRunData) {
128// TODO: add more infor like frame number or number of entries in paylod
129
130// log->info(2, "GStreamer::publishFrameRunData: ",
131// gutilities::success_or_fail(startStream(frameRunData)));
132// log->info(2, "GStreamer::publishFrameHeader: ",
133// gutilities::success_or_fail(publishFrameHeader(frameRunData->getHeader())));
134// log->info(2, "GStreamer::publishPayload: ",
135// gutilities::success_or_fail(publishPayload(frameRunData->getIntegralPayload())));
136// log->info(2, "GStreamer::endStream: ",
137// gutilities::success_or_fail(endStream(frameRunData)));
138// }
std::shared_ptr< GLogger > log
auto getDigitizedData() const -> const std::vector< std::unique_ptr< GDigitizedData > > &
auto getTrueInfoData() const -> const std::vector< std::unique_ptr< GTrueInfoData > > &
static const std::vector< std::string > & supported_formats()
Return the list of output format tokens supported by the module.
Definition gstreamer.cc:11
bool publishEventDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish the digitized hit bank for one detector.
Definition gstreamer.h:324
void publishEventData(const std::shared_ptr< GEventDataCollection > &event_data)
Queue one event for publication.
Definition gstreamer.cc:25
bool publishRunDigitizedData(const std::string &detectorName, const std::vector< const GDigitizedData * > &digitizedData)
Publish run-level digitized data for one detector.
Definition gstreamer.h:456
bool endRun(const std::shared_ptr< GRunDataCollection > &run_data)
End publishing one run-level collection.
Definition gstreamer.h:413
void flushEventBuffer()
Flush all buffered events to the backend in publish order.
Definition gstreamer.cc:75
static bool is_valid_format(const std::string &format)
Validate whether a format token is supported.
Definition gstreamer.cc:17
bool endEvent(const std::shared_ptr< GEventDataCollection > &event_data)
End publishing one buffered event.
Definition gstreamer.h:244
bool publishEventHeader(const std::unique_ptr< GEventHeader > &gevent_header)
Publish the event header for the current event sequence.
Definition gstreamer.h:268
bool startRun(const std::shared_ptr< GRunDataCollection > &run_data)
Begin publishing one run-level collection.
Definition gstreamer.h:387
void publishRunData(const std::shared_ptr< GRunDataCollection > &run_data)
Publish one run-level data collection immediately.
Definition gstreamer.cc:47
bool publishRunHeader(const std::unique_ptr< GRunHeader > &run_header)
Publish the run header for the current run sequence.
Definition gstreamer.h:434
bool startEvent(const std::shared_ptr< GEventDataCollection > &event_data)
Begin publishing one buffered event.
Definition gstreamer.h:218
bool publishEventAncestors(const GAncestorBank &ancestors)
Publishes the event-local ancestor bank.
Definition gstreamer.h:342
bool publishEventTrueInfoData(const std::string &detectorName, const std::vector< const GTrueInfoData * > &trueInfoData)
Publish the true-information hit bank for one detector.
Definition gstreamer.h:296
bool publishEventGeneratedParticles(const std::string &bankName, const GGeneratedParticleBank &particles)
Definition gstreamer.h:330
#define SFUNCTION_NAME
Core streaming interface and helper utilities for the gstreamer module.
constexpr int ERR_PUBLISH_ERROR
Publish sequence encountered invalid state or invalid input data.
std::string success_or_fail(bool condition)
string convertToLowercase(const string &str)