OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SDDSWriter.h
Go to the documentation of this file.
1//
2// Class SDDSWriter
3// This class is the base class for all SDDS writers.
4//
5// Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6// Christof Metzger-Kraus, Open Sourcerer
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
19#ifndef OPAL_SDDS_WRITER_H
20#define OPAL_SDDS_WRITER_H
21
22#include <fstream>
23#include <iomanip>
24#include <map>
25#include <ostream>
26#include <queue>
27#include <sstream>
28#include <string>
29#include <tuple>
30#include <utility>
31#include <vector>
32#include <filesystem>
35
36template <class T, unsigned Dim>
37class PartBunchBase;
38
40
41public:
42 // order: text, content
43 typedef std::pair<std::string, std::string> desc_t;
44
45 // order: name, type, description
46 typedef std::tuple<std::string,
47 std::string,
48 std::string> param_t;
49
50 // order: mode, no row counts
51 typedef std::pair<std::string, size_t> data_t;
52
53 // order: name, type, unit, description
54 typedef std::tuple<std::string,
55 std::string,
56 std::string,
57 std::string> cols_t;
58
59 SDDSWriter(const std::string& fname, bool restart);
60
61 virtual ~SDDSWriter() { };
62
63 virtual void write(PartBunchBase<double, 3>* /*beam*/) { };
64
68 void rewindLines(size_t numberOfLines);
69
71
72 double getLastValue(const std::string& column);
73
74 bool exists() const;
75
76protected:
77 void addDescription(const std::string& text,
78 const std::string& content);
79
80 template<typename T>
81 void addParameter(const std::string& name,
82 const std::string& type,
83 const std::string& desc,
84 const T& value);
85
87
88 void addColumn(const std::string& name,
89 const std::string& type,
90 const std::string& unit,
91 const std::string& desc);
92
93 void addInfo(const std::string& mode,
94 const size_t& no_row_counts);
95
96 void writeRow();
97
98 void open();
99
100 void close();
101
107 void writeHeader();
108
109 template<typename T>
110 std::string toString(const T& val);
111
112 std::string fname_m;
113
120 std::ios_base::openmode mode_m;
121
123
124 /* The columns of the SDDS file need to be
125 * added only once.
126 */
127 bool hasColumns() const;
128
129private:
130 void writeDescription();
131
132 void writeParameters();
133
134 void writeColumns();
135
136 void writeInfo();
137
138 std::ofstream os_m;
139
140 std::string indent_m;
141
143 std::queue<param_t> params_m;
144 std::queue<std::string> paramValues_m;
146
147 static constexpr
148 unsigned int precision_m = 15;
149};
150
151
152inline
153bool SDDSWriter::exists() const {
154 return std::filesystem::exists(fname_m);
155}
156
157
158inline
159void SDDSWriter::addDescription(const std::string& text,
160 const std::string& content) {
161 desc_m = std::make_pair(text, content);
162}
163
164
165template<typename T>
166void SDDSWriter::addParameter(const std::string& name,
167 const std::string& type,
168 const std::string& desc,
169 const T& value) {
170 params_m.push(std::make_tuple(name, type, desc));
171 std::stringstream ss;
172 ss << value;
173 paramValues_m.push(ss.str());
174}
175
176
177inline
178void SDDSWriter::addInfo(const std::string& mode,
179 const size_t& no_row_counts) {
180 info_m = std::make_pair(mode, no_row_counts);
181}
182
183
184inline
186 columns_m.writeRow(os_m);
187}
188
189
190template<typename T>
191std::string SDDSWriter::toString(const T& val) {
192 std::ostringstream ss;
193 ss.precision(precision_m);
194 ss << val;
195 return ss.str();
196}
197
198
199inline
201 return columns_m.hasColumns();
202}
203
204#endif
const std::string name
void replaceVersionString()
SDDSWriter(const std::string &fname, bool restart)
desc_t desc_m
Definition SDDSWriter.h:142
std::tuple< std::string, std::string, std::string, std::string > cols_t
Definition SDDSWriter.h:57
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
virtual void write(PartBunchBase< double, 3 > *)
Definition SDDSWriter.h:63
std::string toString(const T &val)
Definition SDDSWriter.h:191
SDDSColumnSet columns_m
Definition SDDSWriter.h:122
void writeInfo()
bool hasColumns() const
Definition SDDSWriter.h:200
data_t info_m
Definition SDDSWriter.h:145
static constexpr unsigned int precision_m
Definition SDDSWriter.h:148
double getLastValue(const std::string &column)
void writeColumns()
void addDefaultParameters()
void addDescription(const std::string &text, const std::string &content)
Definition SDDSWriter.h:159
void writeParameters()
std::string indent_m
Definition SDDSWriter.h:140
std::queue< std::string > paramValues_m
Definition SDDSWriter.h:144
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:120
void writeDescription()
virtual ~SDDSWriter()
Definition SDDSWriter.h:61
std::tuple< std::string, std::string, std::string > param_t
Definition SDDSWriter.h:48
void writeRow()
Definition SDDSWriter.h:185
bool exists() const
Definition SDDSWriter.h:153
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition SDDSWriter.h:178
std::queue< param_t > params_m
Definition SDDSWriter.h:143
std::pair< std::string, size_t > data_t
Definition SDDSWriter.h:51
std::string fname_m
Definition SDDSWriter.h:112
std::ofstream os_m
Definition SDDSWriter.h:138
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition SDDSWriter.h:166
std::pair< std::string, std::string > desc_t
Definition SDDSWriter.h:43
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc)