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(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_true_info());
21 }
22
23 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: opened file " + filename_true_info());
24 }
25
26 if (!ofile_digitized.is_open()) {
27 ofile_digitized.clear();
28 ofile_digitized.open(filename_digitized(), std::ios::out | std::ios::trunc);
29
30 if (!ofile_digitized.is_open() || !ofile_digitized) {
31 log->error(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_digitized());
32 }
33
34 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: opened file " + filename_digitized());
35 }
36
37 if (!ofile_generated.is_open()) {
38 ofile_generated.clear();
39 ofile_generated.open(filename_generated(), std::ios::out | std::ios::trunc);
40 if (!ofile_generated.is_open() || !ofile_generated) {
41 log->error(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_generated());
42 }
43 ofile_generated << "evn, timestamp, thread_id, bank, name, pid, type, multiplicity, p, theta, phi, vx, vy, vz\n";
44 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: opened file " + filename_generated());
45 }
46
47 if (!ofile_generated_tracked.is_open()) {
48 ofile_generated_tracked.clear();
49 ofile_generated_tracked.open(filename_generated_tracked(), std::ios::out | std::ios::trunc);
50 if (!ofile_generated_tracked.is_open() || !ofile_generated_tracked) {
51 log->error(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ",
52 filename_generated_tracked());
53 }
54 ofile_generated_tracked << "evn, timestamp, thread_id, bank, name, pid, type, multiplicity, p, theta, phi, vx, vy, vz\n";
55 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: opened file " + filename_generated_tracked());
56 }
57
58 return true;
59}
60
62 // The public closeConnection() wrapper already flushes buffered events before this method runs.
63 const bool had_ancestor_stream = ofile_ancestors.is_open();
64
65 if (ofile_true_info.is_open()) ofile_true_info.close();
66 if (ofile_digitized.is_open()) ofile_digitized.close();
67 if (ofile_generated.is_open()) ofile_generated.close();
68 if (ofile_generated_tracked.is_open()) ofile_generated_tracked.close();
69 if (ofile_ancestors.is_open()) ofile_ancestors.close();
70
71 if (ofile_true_info.is_open()) {
72 log->error(gstreamer::ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_true_info());
73 }
74 if (ofile_digitized.is_open()) {
75 log->error(gstreamer::ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_digitized());
76 }
77 if (ofile_generated.is_open()) {
78 log->error(gstreamer::ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_generated());
79 }
80 if (ofile_generated_tracked.is_open()) {
81 log->error(gstreamer::ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME,
82 " could not close file " + filename_generated_tracked());
83 }
84 if (ofile_ancestors.is_open()) {
85 log->error(gstreamer::ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_ancestors());
86 }
87
88 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_true_info());
89 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_digitized());
90 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated());
91 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated_tracked());
92 if (had_ancestor_stream) {
93 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_ancestors());
94 }
95
96 return true;
97}
98
99std::ofstream& GstreamerCsvFactory::generated_stream_for_bank(const std::string& bankName) {
100 return bankName == "generated_tracked" ? ofile_generated_tracked : ofile_generated;
101}
102
103bool GstreamerCsvFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
104 const GGeneratedParticleBank& particles) {
105 auto& stream = generated_stream_for_bank(bankName);
106 if (!stream.is_open()) {
107 log->error(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, "Error: can't access generated CSV stream");
108 }
109
110 for (const auto& particle : particles) {
111 stream << event_number << ", " << timestamp << ", " << thread_id << ", " << bankName << ", "
112 << particle.name << ", "
113 << particle.pid << ", "
114 << particle.type << ", "
115 << particle.multiplicity << ", "
116 << particle.p << ", "
117 << particle.theta << ", "
118 << particle.phi << ", "
119 << particle.vx << ", "
120 << particle.vy << ", "
121 << particle.vz << "\n";
122 }
123
124 return true;
125}
126
127bool GstreamerCsvFactory::publishEventAncestorsImpl(const GAncestorBank& ancestors) {
128 if (!ofile_ancestors.is_open()) {
129 ofile_ancestors.open(filename_ancestors(), std::ios::out | std::ios::trunc);
130 if (!ofile_ancestors.is_open() || !ofile_ancestors) {
131 log->error(gstreamer::ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_ancestors());
132 }
133 ofile_ancestors << "evn, timestamp, thread_id, pid, tid, mtid, trackE, px, py, pz, vx, vy, vz\n";
134 }
135
136 for (const auto& ancestor : ancestors) {
137 ofile_ancestors << event_number << ", " << timestamp << ", " << thread_id << ", "
138 << ancestor.pid << ", " << ancestor.tid << ", " << ancestor.mtid << ", "
139 << ancestor.trackE << ", " << ancestor.px << ", " << ancestor.py << ", "
140 << ancestor.pz << ", " << ancestor.vx << ", " << ancestor.vy << ", "
141 << ancestor.vz << "\n";
142 }
143 return true;
144}
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.