OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 <filesystem>
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,
75 const T& value);
76
78
80 const std::string& name, const std::string& type, const std::string& unit,
81 const std::string& desc);
82
83 void addInfo(const std::string& mode, const size_t& no_row_counts);
84
85 void writeRow();
86
87 void open();
88
89 void close();
90
96 void writeHeader();
97
98 template <typename T>
99 std::string toString(const T& val);
100
101 std::string fname_m;
102
109 std::ios_base::openmode mode_m;
110
112
113 /* The columns of the SDDS file need to be
114 * added only once.
115 */
116 bool hasColumns() const;
117
118private:
119 void writeDescription();
120
121 void writeParameters();
122
123 void writeColumns();
124
125 void writeInfo();
126
127 std::ofstream os_m;
128
129 std::string indent_m;
130
132 std::queue<param_t> params_m;
133 std::queue<std::string> paramValues_m;
135
136 static constexpr unsigned int precision_m = 15;
137};
138
139inline bool SDDSWriter::exists() const { return std::filesystem::exists(fname_m); }
140
141inline void SDDSWriter::addDescription(const std::string& text, const std::string& content) {
142 desc_m = std::make_pair(text, content);
143}
144
145template <typename T>
147 const std::string& name, const std::string& type, const std::string& desc, const T& value) {
148 params_m.push(std::make_tuple(name, type, desc));
149 std::stringstream ss;
150 ss << value;
151 paramValues_m.push(ss.str());
152}
153
154inline void SDDSWriter::addInfo(const std::string& mode, const size_t& no_row_counts) {
155 info_m = std::make_pair(mode, no_row_counts);
156}
157
159
160template <typename T>
161std::string SDDSWriter::toString(const T& val) {
162 std::ostringstream ss;
163 ss.precision(precision_m);
164 ss << val;
165 return ss.str();
166}
167
168inline bool SDDSWriter::hasColumns() const { return columns_m.hasColumns(); }
169
170#endif
double T
Definition OPALTypes.h:8
bool hasColumns() const
void writeRow(std::ostream &os) const
void replaceVersionString()
desc_t desc_m
Definition SDDSWriter.h:131
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:161
virtual void write(const PartBunch_t &)
Definition SDDSWriter.h:56
SDDSColumnSet columns_m
Definition SDDSWriter.h:111
void writeInfo()
bool hasColumns() const
Definition SDDSWriter.h:168
data_t info_m
Definition SDDSWriter.h:134
static constexpr unsigned int precision_m
Definition SDDSWriter.h:136
double getLastValue(const std::string &column)
void writeColumns()
void addDefaultParameters()
void addDescription(const std::string &text, const std::string &content)
Definition SDDSWriter.h:141
void writeParameters()
std::string indent_m
Definition SDDSWriter.h:129
std::queue< std::string > paramValues_m
Definition SDDSWriter.h:133
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:109
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:158
bool exists() const
Definition SDDSWriter.h:139
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition SDDSWriter.h:154
std::queue< param_t > params_m
Definition SDDSWriter.h:132
std::pair< std::string, size_t > data_t
Definition SDDSWriter.h:47
std::string fname_m
Definition SDDSWriter.h:101
std::ofstream os_m
Definition SDDSWriter.h:127
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition SDDSWriter.h:146
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)