File indexing completed on 2025-07-18 09:11:49
0001
0002
0003
0004
0005
0006 #ifndef YODA_Writer_h
0007 #define YODA_Writer_h
0008
0009 #include "YODA/AnalysisObject.h"
0010 #include "YODA/Utils/Traits.h"
0011
0012 #ifdef HAVE_HDF5
0013 #include <YODA/Config/BuildConfig.h>
0014 #ifdef WITH_HIGHFIVE
0015 #include <YODA/highfive/H5File.hpp>
0016 #else
0017 #include <highfive/H5File.hpp>
0018 #define YODA_H5 HighFive
0019 #endif
0020 #endif
0021
0022 #include <type_traits>
0023 #include <fstream>
0024 #include <iostream>
0025
0026 namespace YODA {
0027
0028 static bool enableH5compression = Utils::getEnvParam("YODA_HDF5_COMPRESSION", true);
0029
0030
0031 class Writer {
0032 public:
0033
0034
0035 virtual ~Writer() {}
0036
0037
0038
0039
0040
0041
0042 void write(const std::string& filename, const AnalysisObject& ao);
0043
0044
0045 void write(std::ostream& stream, const AnalysisObject& ao) {
0046 std::vector<const AnalysisObject*> vec{&ao};
0047 write(stream, vec);
0048 }
0049
0050
0051 template <typename T>
0052 typename std::enable_if_t<DerefableToAO<T>::value>
0053 write(std::ostream& stream, const T& ao) {
0054 write(stream, *ao);
0055 }
0056
0057
0058 template <typename T>
0059 typename std::enable_if_t<DerefableToAO<T>::value>
0060 write(const std::string& filename, const T& ao) {
0061 write(filename, *ao);
0062 }
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 #ifdef HAVE_HDF5
0077 void write(YODA_H5::File& file, const std::vector<const AnalysisObject*>& aos);
0078 #endif
0079
0080
0081
0082
0083
0084
0085
0086 void write(std::ostream& stream, const std::vector<const AnalysisObject*>& aos, int precision = -1);
0087
0088
0089
0090
0091
0092
0093 template <typename RANGE>
0094 typename std::enable_if_t<CIterable<RANGE>::value>
0095 write(std::ostream& stream, const RANGE& aos) {
0096 write(stream, std::begin(aos), std::end(aos));
0097 }
0098
0099
0100 template <typename RANGE>
0101 typename std::enable_if_t<CIterable<RANGE>::value>
0102 write(const std::string& filename, const RANGE& aos) {
0103 write(filename, std::begin(aos), std::end(aos));
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 template <typename AOITER>
0117 void write(std::ostream& stream, const AOITER& begin, const AOITER& end, int precision = -1) {
0118 std::vector<const AnalysisObject*> vec;
0119
0120 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
0121 write(stream, vec, precision);
0122 }
0123
0124
0125
0126
0127
0128
0129 template <typename AOITER>
0130 void write(const std::string& filename, const AOITER& begin, const AOITER& end) {
0131 std::vector<const AnalysisObject*> vec;
0132
0133 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
0134
0135 if (filename != "-") {
0136 try {
0137 const size_t lastdot = filename.find_last_of(".");
0138 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
0139 const bool compress = (fmt == "gz") || (fmt == "h5" && enableH5compression);
0140 useCompression(compress);
0141 #ifdef HAVE_HDF5
0142
0143 if (Utils::startswith(fmt, "h5")) {
0144 try {
0145 YODA_H5::File h5(filename, YODA_H5::File::OpenOrCreate | YODA_H5::File::Truncate);
0146 write(h5, vec);
0147 } catch(...) {
0148 throw WriteError("Failed to open HDF5 file " + filename);
0149 }
0150 return;
0151 }
0152 #endif
0153
0154 std::ofstream stream;
0155 stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0156 stream.open(filename.c_str());
0157 if (stream.fail())
0158 throw WriteError("Writing to filename " + filename + " failed");
0159 write(stream, vec);
0160 } catch (std::ofstream::failure& e) {
0161 throw WriteError("Writing to filename " + filename + " failed: " + e.what());
0162 }
0163 } else {
0164 try {
0165 write(std::cout, vec);
0166 } catch (std::runtime_error& e) {
0167 throw WriteError("Writing to stdout failed: " + std::string(e.what()));
0168 }
0169 }
0170
0171 }
0172
0173
0174
0175
0176 void setPrecision(int precision) {
0177 _precision = precision;
0178 }
0179
0180
0181 void setAOPrecision(const bool needsDP = false) {
0182 if (needsDP) _aoprecision = std::numeric_limits<double>::max_digits10;
0183 else if (_precision > 0) _aoprecision = _precision;
0184 else _aoprecision = 6;
0185 }
0186
0187
0188 void useCompression(const bool compress=true) {
0189 _compress = compress;
0190 }
0191
0192
0193 protected:
0194
0195
0196
0197
0198
0199 virtual void writeHead(std::ostream&) {}
0200
0201
0202 virtual void writeBody(std::ostream& stream, const AnalysisObject* ao);
0203
0204
0205 virtual void writeBody(std::ostream& stream, const AnalysisObject& ao);
0206
0207
0208
0209 template <typename T>
0210 typename std::enable_if_t<DerefableToAO<T>::value>
0211 writeBody(std::ostream& stream, const T& ao) { writeBody(stream, *ao); }
0212
0213
0214 virtual void writeFoot(std::ostream& stream) { stream << std::flush; }
0215
0216
0217
0218
0219
0220
0221
0222 virtual void writeAO(std::ostream& stream, const AnalysisObject& ao) = 0;
0223
0224 #ifdef HAVE_HDF5
0225 virtual void writeAOS(YODA_H5::File& file, const vector<const AnalysisObject*>& aos) = 0;
0226 #endif
0227
0228
0229
0230
0231
0232 int _precision, _aoprecision;
0233
0234
0235 bool _compress;
0236
0237 };
0238
0239
0240
0241 Writer& mkWriter(const std::string& format_name);
0242
0243
0244 }
0245
0246 #endif