OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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 <string>
24#include <queue>
25#include <map>
26#include <vector>
27#include <tuple>
28#include <utility>
29#include <ostream>
30#include <iomanip>
31#include <sstream>
32
33#include <boost/filesystem.hpp>
34
37
38template <class T, unsigned Dim>
39class PartBunchBase;
40
42
43public:
44 // order: text, content
45 typedef std::pair<std::string, std::string> desc_t;
46
47 // order: name, type, description
48 typedef std::tuple<std::string,
49 std::string,
50 std::string> param_t;
51
52 // order: mode, no row counts
53 typedef std::pair<std::string, size_t> data_t;
54
55 // order: name, type, unit, description
56 typedef std::tuple<std::string,
57 std::string,
58 std::string,
59 std::string> cols_t;
60
61
62 SDDSWriter(const std::string& fname, bool restart);
63
64 virtual ~SDDSWriter() { };
65
66 virtual void write(const PartBunchBase<double, 3>* /*beam*/) { };
67
71 void rewindLines(size_t numberOfLines);
72
74
75 double getLastValue(const std::string& column);
76
77 bool exists() const;
78
79protected:
80
81 void addDescription(const std::string& text,
82 const std::string& content);
83
84 template<typename T>
85 void addParameter(const std::string& name,
86 const std::string& type,
87 const std::string& desc,
88 const T& value);
89
91
92 void addColumn(const std::string& name,
93 const std::string& type,
94 const std::string& unit,
95 const std::string& desc);
96
97 void addInfo(const std::string& mode,
98 const size_t& no_row_counts);
99
100 void writeRow();
101
102 void open();
103
104 void close();
105
111 void writeHeader();
112
113 template<typename T>
114 std::string toString(const T& val);
115
116 std::string fname_m;
117
124 std::ios_base::openmode mode_m;
125
127
128 /* The columns of the SDDS file need to be
129 * added only once.
130 */
131 bool hasColumns() const;
132
133private:
134
135 void writeDescription();
136
137 void writeParameters();
138
139 void writeColumns();
140
141 void writeInfo();
142
143 std::ofstream os_m;
144
145 std::string indent_m;
146
148 std::queue<param_t> params_m;
149 std::queue<std::string> paramValues_m;
151
152 static constexpr
153 unsigned int precision_m = 15;
154};
155
156
157inline
158bool SDDSWriter::exists() const {
159 return boost::filesystem::exists(fname_m);
160}
161
162
163inline
164void SDDSWriter::addDescription(const std::string& text,
165 const std::string& content)
166{
167 desc_m = std::make_pair(text, content);
168}
169
170
171template<typename T>
172void SDDSWriter::addParameter(const std::string& name,
173 const std::string& type,
174 const std::string& desc,
175 const T& value)
176{
177 params_m.push(std::make_tuple(name, type, desc));
178 std::stringstream ss;
179 ss << value;
180 paramValues_m.push(ss.str());
181}
182
183
184inline
185void SDDSWriter::addInfo(const std::string& mode,
186 const size_t& no_row_counts) {
187 info_m = std::make_pair(mode, no_row_counts);
188}
189
190
191inline
194}
195
196
197template<typename T>
198std::string SDDSWriter::toString(const T& val) {
199 std::ostringstream ss;
200 ss.precision(precision_m);
201 ss << val;
202 return ss.str();
203}
204
205
206inline
208 return columns_m.hasColumns();
209}
210
211#endif
const std::string name
boost::function< boost::tuple< double, bool >(arguments_t)> type
Definition: function.hpp:21
bool hasColumns() const
Definition: SDDSColumnSet.h:78
void writeRow(std::ostream &os) const
void replaceVersionString()
Definition: SDDSWriter.cpp:79
SDDSWriter(const std::string &fname, bool restart)
Definition: SDDSWriter.cpp:32
desc_t desc_m
Definition: SDDSWriter.h:147
std::tuple< std::string, std::string, std::string, std::string > cols_t
Definition: SDDSWriter.h:59
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
Definition: SDDSWriter.cpp:48
std::string toString(const T &val)
Definition: SDDSWriter.h:198
SDDSColumnSet columns_m
Definition: SDDSWriter.h:126
void writeInfo()
Definition: SDDSWriter.cpp:196
bool hasColumns() const
Definition: SDDSWriter.h:207
data_t info_m
Definition: SDDSWriter.h:150
static constexpr unsigned int precision_m
Definition: SDDSWriter.h:153
double getLastValue(const std::string &column)
Definition: SDDSWriter.cpp:125
void writeColumns()
Definition: SDDSWriter.cpp:191
void addDefaultParameters()
Definition: SDDSWriter.cpp:211
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:164
void writeParameters()
Definition: SDDSWriter.cpp:176
std::string indent_m
Definition: SDDSWriter.h:145
void open()
Definition: SDDSWriter.cpp:134
std::queue< std::string > paramValues_m
Definition: SDDSWriter.h:149
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:151
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:124
void writeDescription()
Definition: SDDSWriter.cpp:167
void close()
Definition: SDDSWriter.cpp:144
virtual void write(const PartBunchBase< double, 3 > *)
Definition: SDDSWriter.h:66
virtual ~SDDSWriter()
Definition: SDDSWriter.h:64
std::tuple< std::string, std::string, std::string > param_t
Definition: SDDSWriter.h:50
void writeRow()
Definition: SDDSWriter.h:192
bool exists() const
Definition: SDDSWriter.h:158
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition: SDDSWriter.h:185
std::queue< param_t > params_m
Definition: SDDSWriter.h:148
std::pair< std::string, size_t > data_t
Definition: SDDSWriter.h:53
std::string fname_m
Definition: SDDSWriter.h:116
std::ofstream os_m
Definition: SDDSWriter.h:143
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition: SDDSWriter.h:172
std::pair< std::string, std::string > desc_t
Definition: SDDSWriter.h:45
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc)