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
63 if (ofile_true_info.is_open()) ofile_true_info.close();
64 if (ofile_digitized.is_open()) ofile_digitized.close();
65 if (ofile_generated.is_open()) ofile_generated.close();
66 if (ofile_generated_tracked.is_open()) ofile_generated_tracked.close();
67
68 if (ofile_true_info.is_open()) {
69 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_true_info());
70 }
71 if (ofile_digitized.is_open()) {
72 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_digitized());
73 }
74 if (ofile_generated.is_open()) {
75 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_generated());
76 }
77 if (ofile_generated_tracked.is_open()) {
78 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file " + filename_generated_tracked());
79 }
80
81 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_true_info());
82 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_digitized());
83 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated());
84 log->info(1, SFUNCTION_NAME, "GstreamerCsvFactory: closed file " + filename_generated_tracked());
85
86 return true;
87}
88
89std::ofstream& GstreamerCsvFactory::generated_stream_for_bank(const std::string& bankName) {
90 return bankName == "generated_tracked" ? ofile_generated_tracked : ofile_generated;
91}
92
93bool GstreamerCsvFactory::publishEventGeneratedParticlesImpl(const std::string& bankName,
94 const GGeneratedParticleBank& particles) {
95 auto& stream = generated_stream_for_bank(bankName);
96 if (!stream.is_open()) {
97 log->error(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, "Error: can't access generated CSV stream");
98 }
99
100 for (const auto& particle : particles) {
101 stream << event_number << ", " << timestamp << ", " << thread_id << ", " << bankName << ", "
102 << particle.name << ", "
103 << particle.pid << ", "
104 << particle.type << ", "
105 << particle.multiplicity << ", "
106 << particle.p << ", "
107 << particle.theta << ", "
108 << particle.phi << ", "
109 << particle.vx << ", "
110 << particle.vy << ", "
111 << particle.vz << "\n";
112 }
113
114 return true;
115}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
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.