gstreamer
Loading...
Searching...
No Matches
gstreamerCSVConnection.cc
Go to the documentation of this file.
1// gstreamer
4
5// Implementation summary:
6// Manage the lifetime of the two CSV streams used by the plugin.
7
9 // Both streams must be open for the CSV backend to operate correctly.
10 if (ofile_true_info.is_open() && ofile_digitized.is_open() &&
11 ofile_generated.is_open() && ofile_generated_tracked.is_open()) {
12 return true;
13 }
14
15 if (!ofile_true_info.is_open()) {
16 ofile_true_info.clear();
17 ofile_true_info.open(filename_true_info(), std::ios::out | std::ios::trunc);
18
19 if (!ofile_true_info.is_open() || !ofile_true_info) {
20 log->error(
21 gstreamer::ERR_CANTOPENOUTPUT, "%1 could not open file %2", SFUNCTION_NAME,
22 filename_true_info());
23 }
24
25 log->info(1, "%1GstreamerCsvFactory: opened file %2", SFUNCTION_NAME, filename_true_info());
26 }
27
28 if (!ofile_digitized.is_open()) {
29 ofile_digitized.clear();
30 ofile_digitized.open(filename_digitized(), std::ios::out | std::ios::trunc);
31
32 if (!ofile_digitized.is_open() || !ofile_digitized) {
33 log->error(
34 gstreamer::ERR_CANTOPENOUTPUT, "%1 could not open file %2", SFUNCTION_NAME,
35 filename_digitized());
36 }
37
38 log->info(1, "%1GstreamerCsvFactory: opened file %2", SFUNCTION_NAME, filename_digitized());
39 }
40
41 if (!ofile_generated.is_open()) {
42 ofile_generated.clear();
43 ofile_generated.open(filename_generated(), std::ios::out | std::ios::trunc);
44 if (!ofile_generated.is_open() || !ofile_generated) {
45 log->error(
46 gstreamer::ERR_CANTOPENOUTPUT, "%1 could not open file %2", SFUNCTION_NAME,
47 filename_generated());
48 }
49 ofile_generated << "evn, timestamp, thread_id, bank, name, pid, type, multiplicity, p, theta, phi, vx, vy, vz\n";
50 log->info(1, "%1GstreamerCsvFactory: opened file %2", SFUNCTION_NAME, filename_generated());
51 }
52
53 if (!ofile_generated_tracked.is_open()) {
54 ofile_generated_tracked.clear();
55 ofile_generated_tracked.open(filename_generated_tracked(), std::ios::out | std::ios::trunc);
56 if (!ofile_generated_tracked.is_open() || !ofile_generated_tracked) {
57 log->error(
58 gstreamer::ERR_CANTOPENOUTPUT, "%1 could not open file %2", SFUNCTION_NAME,
59 filename_generated_tracked());
60 }
61 ofile_generated_tracked << "evn, timestamp, thread_id, bank, name, pid, type, multiplicity, p, theta, phi, vx, vy, vz\n";
62 log->info(1, "%1GstreamerCsvFactory: opened file %2", SFUNCTION_NAME, filename_generated_tracked());
63 }
64
65 return true;
66}
67
69 // The public closeConnection() wrapper already flushes buffered events before this method runs.
70 const bool had_ancestor_stream = ofile_ancestors.is_open();
71
72 if (ofile_true_info.is_open()) ofile_true_info.close();
73 if (ofile_digitized.is_open()) ofile_digitized.close();
74 if (ofile_generated.is_open()) ofile_generated.close();
75 if (ofile_generated_tracked.is_open()) ofile_generated_tracked.close();
76 if (ofile_ancestors.is_open()) ofile_ancestors.close();
77
78 if (ofile_true_info.is_open()) {
79 log->error(
80 gstreamer::ERR_CANTCLOSEOUTPUT, "%1 could not close file %2", SFUNCTION_NAME, filename_true_info());
81 }
82 if (ofile_digitized.is_open()) {
83 log->error(
84 gstreamer::ERR_CANTCLOSEOUTPUT, "%1 could not close file %2", SFUNCTION_NAME, filename_digitized());
85 }
86 if (ofile_generated.is_open()) {
87 log->error(
88 gstreamer::ERR_CANTCLOSEOUTPUT, "%1 could not close file %2", SFUNCTION_NAME, filename_generated());
89 }
90 if (ofile_generated_tracked.is_open()) {
91 log->error(
92 gstreamer::ERR_CANTCLOSEOUTPUT, "%1 could not close file %2", SFUNCTION_NAME,
93 filename_generated_tracked());
94 }
95 if (ofile_ancestors.is_open()) {
96 log->error(
97 gstreamer::ERR_CANTCLOSEOUTPUT, "%1 could not close file %2", SFUNCTION_NAME, filename_ancestors());
98 }
99
100 log->info(1, "%1GstreamerCsvFactory: closed file %2", SFUNCTION_NAME, filename_true_info());
101 log->info(1, "%1GstreamerCsvFactory: closed file %2", SFUNCTION_NAME, filename_digitized());
102 log->info(1, "%1GstreamerCsvFactory: closed file %2", SFUNCTION_NAME, filename_generated());
103 log->info(1, "%1GstreamerCsvFactory: closed file %2", SFUNCTION_NAME, filename_generated_tracked());
104 if (had_ancestor_stream) {
105 log->info(1, "%1GstreamerCsvFactory: closed file %2", SFUNCTION_NAME, filename_ancestors());
106 }
107
108 return true;
109}
110
111std::ofstream& GstreamerCsvFactory::generated_stream_for_bank(const std::string& bankName) {
112 return bankName == "generated_tracked" ? ofile_generated_tracked : ofile_generated;
113}
114
115bool GstreamerCsvFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
116 const GGeneratedParticleBank& particles) {
117 auto& stream = generated_stream_for_bank(bankName);
118 if (!stream.is_open()) {
119 log->error(gstreamer::ERR_CANTOPENOUTPUT, "%1Error: can't access generated CSV stream", SFUNCTION_NAME);
120 }
121
122 for (const auto& particle : particles) {
123 stream << event_number << ", " << timestamp << ", " << thread_id << ", " << bankName << ", "
124 << particle.name << ", "
125 << particle.pid << ", "
126 << particle.type << ", "
127 << particle.multiplicity << ", "
128 << particle.p << ", "
129 << particle.theta << ", "
130 << particle.phi << ", "
131 << particle.vx << ", "
132 << particle.vy << ", "
133 << particle.vz << "\n";
134 }
135
136 return true;
137}
138
139bool GstreamerCsvFactory::publishEventAncestorsImpl(const GAncestorBank& ancestors) {
140 if (!ofile_ancestors.is_open()) {
141 ofile_ancestors.open(filename_ancestors(), std::ios::out | std::ios::trunc);
142 if (!ofile_ancestors.is_open() || !ofile_ancestors) {
143 log->error(
144 gstreamer::ERR_CANTOPENOUTPUT, "%1 could not open file %2", SFUNCTION_NAME,
145 filename_ancestors());
146 }
147 ofile_ancestors << "evn, timestamp, thread_id, pid, tid, mtid, trackE, px, py, pz, vx, vy, vz\n";
148 }
149
150 for (const auto& ancestor : ancestors) {
151 ofile_ancestors << event_number << ", " << timestamp << ", " << thread_id << ", "
152 << ancestor.pid << ", " << ancestor.tid << ", " << ancestor.mtid << ", "
153 << ancestor.trackE << ", " << ancestor.px << ", " << ancestor.py << ", "
154 << ancestor.pz << ", " << ancestor.vx << ", " << ancestor.vy << ", "
155 << ancestor.vz << "\n";
156 }
157 return true;
158}
std::shared_ptr< GLogger > log
virtual bool closeConnectionImpl()
Plugin-specific close implementation hook.
Definition gstreamer.h:127
virtual bool openConnection()
Open the output medium used by this streamer.
Definition gstreamer.h:104
std::vector< GAncestorData > GAncestorBank
std::vector< GGeneratedParticleData > GGeneratedParticleBank
CSV streamer plugin declarations.
Shared constants and error codes for the gstreamer module.
constexpr int ERR_CANTOPENOUTPUT
Output medium could not be opened successfully.
constexpr int ERR_CANTCLOSEOUTPUT
Output medium could not be closed cleanly.