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
8bool GstreamerCsvFactory::openConnection() {
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(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(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(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(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_generated_tracked());
52 }
53 ofile_generated_tracked << "evn, timestamp, thread_id, bank, name, pid, type, multiplicity, p, theta, phi, vx, vy, vz\n";
54 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: opened file " + filename_generated_tracked());
55 }
56
57 return true;
58}
59
60bool GstreamerCsvFactory::closeConnectionImpl() {
61 // The public closeConnection() wrapper already flushes buffered events before this method runs.
62 const bool had_ancestor_stream = ofile_ancestors.is_open();
63
64 if (ofile_true_info.is_open()) ofile_true_info.close();
65 if (ofile_digitized.is_open()) ofile_digitized.close();
66 if (ofile_generated.is_open()) ofile_generated.close();
67 if (ofile_generated_tracked.is_open()) ofile_generated_tracked.close();
68 if (ofile_ancestors.is_open()) ofile_ancestors.close();
69
70 if (ofile_true_info.is_open()) {
71 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_true_info());
72 }
73 if (ofile_digitized.is_open()) {
74 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_digitized());
75 }
76 if (ofile_generated.is_open()) {
77 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_generated());
78 }
79 if (ofile_generated_tracked.is_open()) {
80 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_generated_tracked());
81 }
82 if (ofile_ancestors.is_open()) {
83 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_ancestors());
84 }
85
86 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_true_info());
87 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_digitized());
88 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated());
89 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated_tracked());
90 if (had_ancestor_stream) {
91 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_ancestors());
92 }
93
94 return true;
95}
96
97std::ofstream& GstreamerCsvFactory::generated_stream_for_bank(const std::string& bankName) {
98 return bankName == "generated_tracked" ? ofile_generated_tracked : ofile_generated;
99}
100
101bool GstreamerCsvFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
102 const GGeneratedParticleBank& particles) {
103 auto& stream = generated_stream_for_bank(bankName);
104 if (!stream.is_open()) {
105 log->error(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, "Error: can't access generated CSV stream");
106 }
107
108 for (const auto& particle : particles) {
109 stream << event_number << ", " << timestamp << ", " << thread_id << ", " << bankName << ", "
110 << particle.name << ", "
111 << particle.pid << ", "
112 << particle.type << ", "
113 << particle.multiplicity << ", "
114 << particle.p << ", "
115 << particle.theta << ", "
116 << particle.phi << ", "
117 << particle.vx << ", "
118 << particle.vy << ", "
119 << particle.vz << "\n";
120 }
121
122 return true;
123}
124
125bool GstreamerCsvFactory::publishEventAncestorsImpl(const GAncestorBank& ancestors) {
126 if (!ofile_ancestors.is_open()) {
127 ofile_ancestors.open(filename_ancestors(), std::ios::out | std::ios::trunc);
128 if (!ofile_ancestors.is_open() || !ofile_ancestors) {
129 log->error(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename_ancestors());
130 }
131 ofile_ancestors << "evn, timestamp, thread_id, pid, tid, mtid, trackE, px, py, pz, vx, vy, vz\n";
132 }
133
134 for (const auto& ancestor : ancestors) {
135 ofile_ancestors << event_number << ", " << timestamp << ", " << thread_id << ", "
136 << ancestor.pid << ", " << ancestor.tid << ", " << ancestor.mtid << ", "
137 << ancestor.trackE << ", " << ancestor.px << ", " << ancestor.py << ", "
138 << ancestor.pz << ", " << ancestor.vx << ", " << ancestor.vy << ", "
139 << ancestor.vz << "\n";
140 }
141 return true;
142}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
std::vector< GAncestorData > GAncestorBank
std::vector< GGeneratedParticleData > GGeneratedParticleBank
CSV streamer plugin declarations.
Shared constants and error codes for the gstreamer module.
#define ERR_CANTCLOSEOUTPUT
Output medium could not be closed cleanly.
#define ERR_CANTOPENOUTPUT
Output medium could not be opened successfully.