OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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#include <fstream>
22#include <iomanip>
23#include <map>
24#include <ostream>
25#include <queue>
26#include <sstream>
27#include <string>
28#include <tuple>
29#include <utility>
30#include <vector>
31#include "OPALTypes.h"
32
33#include <boost/filesystem.hpp>
34
37
39public:
40 // order: text, content
41 typedef std::pair<std::string, std::string> desc_t;
42
43 // order: name, type, description
44 typedef std::tuple<std::string, std::string, std::string> param_t;
45
46 // order: mode, no row counts
47 typedef std::pair<std::string, size_t> data_t;
48
49 // order: name, type, unit, description
50 typedef std::tuple<std::string, std::string, std::string, std::string> cols_t;
51
52 SDDSWriter(const std::string& fname, bool restart);
53
54 virtual ~SDDSWriter(){};
55
56 virtual void write(const PartBunch_t* /*beam*/){};
57
61 void rewindLines(size_t numberOfLines);
62
64
65 double getLastValue(const std::string& column);
66
67 bool exists() const;
68
69protected:
70 void addDescription(const std::string& text, const std::string& content);
71
72 template <typename T>
73 void addParameter(
74 const std::string& name, const std::string& type, const std::string& desc, const T& value);
75
77
79 const std::string& name, const std::string& type, const std::string& unit,
80 const std::string& desc);
81
82 void addInfo(const std::string& mode, const size_t& no_row_counts);
83
84 void writeRow();
85
86 void open();
87
88 void close();
89
95 void writeHeader();
96
97 template <typename T>
98 std::string toString(const T& val);
99
100 std::string fname_m;
101
108 std::ios_base::openmode mode_m;
109
111
112 /* The columns of the SDDS file need to be
113 * added only once.
114 */
115 bool hasColumns() const;
116
117private:
118 void writeDescription();
119
120 void writeParameters();
121
122 void writeColumns();
123
124 void writeInfo();
125
126 std::ofstream os_m;
127
128 std::string indent_m;
129
131 std::queue<param_t> params_m;
132 std::queue<std::string> paramValues_m;
134
135 static constexpr unsigned int precision_m = 15;
136};
137
138inline bool SDDSWriter::exists() const {
139 return boost::filesystem::exists(fname_m);
140}
141
142inline void SDDSWriter::addDescription(const std::string& text, const std::string& content) {
143 desc_m = std::make_pair(text, content);
144}
145
146template <typename T>
148 const std::string& name, const std::string& type, const std::string& desc, const T& value) {
149 params_m.push(std::make_tuple(name, type, desc));
150 std::stringstream ss;
151 ss << value;
152 paramValues_m.push(ss.str());
153}
154
155inline void SDDSWriter::addInfo(const std::string& mode, const size_t& no_row_counts) {
156 info_m = std::make_pair(mode, no_row_counts);
157}
158
159inline void SDDSWriter::writeRow() {
160 columns_m.writeRow(os_m);
161}
162
163template <typename T>
164std::string SDDSWriter::toString(const T& val) {
165 std::ostringstream ss;
166 ss.precision(precision_m);
167 ss << val;
168 return ss.str();
169}
170
171inline bool SDDSWriter::hasColumns() const {
172 return columns_m.hasColumns();
173}
174
175#endif
PartBunch< PLayout_t< double, 3 >, double, 3 > PartBunch_t
double T
Definition datatypes.h:7
void replaceVersionString()
SDDSWriter(const std::string &fname, bool restart)
desc_t desc_m
Definition SDDSWriter.h:130
std::tuple< std::string, std::string, std::string, std::string > cols_t
Definition SDDSWriter.h:50
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
std::string toString(const T &val)
Definition SDDSWriter.h:164
SDDSColumnSet columns_m
Definition SDDSWriter.h:110
void writeInfo()
bool hasColumns() const
Definition SDDSWriter.h:171
data_t info_m
Definition SDDSWriter.h:133
static constexpr unsigned int precision_m
Definition SDDSWriter.h:135
double getLastValue(const std::string &column)
void writeColumns()
void addDefaultParameters()
void addDescription(const std::string &text, const std::string &content)
Definition SDDSWriter.h:142
void writeParameters()
std::string indent_m
Definition SDDSWriter.h:128
std::queue< std::string > paramValues_m
Definition SDDSWriter.h:132
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:108
void writeDescription()
virtual ~SDDSWriter()
Definition SDDSWriter.h:54
std::tuple< std::string, std::string, std::string > param_t
Definition SDDSWriter.h:44
void writeRow()
Definition SDDSWriter.h:159
bool exists() const
Definition SDDSWriter.h:138
virtual void write(const PartBunch_t *)
Definition SDDSWriter.h:56
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition SDDSWriter.h:155
std::queue< param_t > params_m
Definition SDDSWriter.h:131
std::pair< std::string, size_t > data_t
Definition SDDSWriter.h:47
std::string fname_m
Definition SDDSWriter.h:100
std::ofstream os_m
Definition SDDSWriter.h:126
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition SDDSWriter.h:147
std::pair< std::string, std::string > desc_t
Definition SDDSWriter.h:41
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc)