yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
Writer.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2023 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_Writer_h
7#define YODA_Writer_h
8
10#include "YODA/Utils/Traits.h"
11#include <type_traits>
12#include <fstream>
13#include <iostream>
14
15namespace YODA {
16
17
19 class Writer {
20 public:
21
23 virtual ~Writer() {}
24
25
28
30 void write(const std::string& filename, const AnalysisObject& ao);
31
33 void write(std::ostream& stream, const AnalysisObject& ao) {
34 // std::vector<const AnalysisObject*> vec{&ao};
35 std::vector<const AnalysisObject*> vec{&ao};
36 write(stream, vec);
37 }
38
40 template <typename T>
41 typename std::enable_if_t<DerefableToAO<T>::value> //< -> void if valid
42 write(std::ostream& stream, const T& ao) {
43 write(stream, *ao);
44 }
45
47 template <typename T>
48 typename std::enable_if_t<DerefableToAO<T>::value> //< -> void if valid
49 write(const std::string& filename, const T& ao) {
50 write(filename, *ao);
51 }
52
54
55
58
65 void write(std::ostream& stream, const std::vector<const AnalysisObject*>& aos);
66
67
72 template <typename RANGE>
73 typename std::enable_if_t<CIterable<RANGE>::value>
74 write(std::ostream& stream, const RANGE& aos) {
75 write(stream, std::begin(aos), std::end(aos));
76 }
77
79 template <typename RANGE>
80 typename std::enable_if_t<CIterable<RANGE>::value>
81 write(const std::string& filename, const RANGE& aos) {
82 write(filename, std::begin(aos), std::end(aos));
83 }
84
86
87
90
95 template <typename AOITER>
96 void write(std::ostream& stream, const AOITER& begin, const AOITER& end) {
97 std::vector<const AnalysisObject*> vec;
98 // vec.reserve(std::distance(begin, end));
99 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
100 write(stream, vec);
101 }
102
103
108 template <typename AOITER>
109 void write(const std::string& filename, const AOITER& begin, const AOITER& end) {
110 std::vector<const AnalysisObject*> vec;
111 // vec.reserve(std::distance(begin, end));
112 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
113
114 if (filename != "-") {
115 try {
116 const size_t lastdot = filename.find_last_of(".");
117 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
118 const bool compress = (fmt == "gz");
119 useCompression(compress);
120 //
121 std::ofstream stream;
122 stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
123 stream.open(filename.c_str());
124 if (stream.fail())
125 throw WriteError("Writing to filename " + filename + " failed");
126 write(stream, vec);
127 } catch (std::ofstream::failure& e) {
128 throw WriteError("Writing to filename " + filename + " failed: " + e.what());
129 }
130 } else {
131 try {
132 write(std::cout, vec);
133 } catch (std::runtime_error& e) {
134 throw WriteError("Writing to stdout failed: " + std::string(e.what()));
135 }
136 }
137
138 }
139
141
143 void setPrecision(int precision) {
144 _precision = precision;
145 }
146
148 void setAOPrecision(const bool needsDP = false) {
149 _aoprecision = needsDP? std::numeric_limits<double>::max_digits10 : _precision;
150 }
151
153 void useCompression(const bool compress=true) {
154 _compress = compress;
155 }
156
157
158 protected:
159
162
164 virtual void writeHead(std::ostream&) {}
165
167 virtual void writeBody(std::ostream& stream, const AnalysisObject* ao);
168
170 virtual void writeBody(std::ostream& stream, const AnalysisObject& ao);
171
174 template <typename T>
175 typename std::enable_if_t<DerefableToAO<T>::value> //< -> void if valid
176 writeBody(std::ostream& stream, const T& ao) { writeBody(stream, *ao); }
177
179 virtual void writeFoot(std::ostream& stream) { stream << std::flush; }
180
182
183
186
187 virtual void writeAO(std::ostream& stream, const AnalysisObject& ao) = 0;
188
190
191
193 int _precision, _aoprecision;
194
196 bool _compress;
197
198 };
199
200
202 Writer& mkWriter(const std::string& format_name);
203
204
205}
206
207#endif
AnalysisObject is the base class for histograms and scatters.
Error for file writing errors.
Definition Exceptions.h:93
Pure virtual base class for various output writers.
Definition Writer.h:19
void write(std::ostream &stream, const AOITER &begin, const AOITER &end)
Definition Writer.h:96
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
Definition Writer.h:164
virtual ~Writer()
Virtual destructor.
Definition Writer.h:23
void write(std::ostream &stream, const AnalysisObject &ao)
Write out object ao to output stream stream.
Definition Writer.h:33
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
Definition Writer.h:179
std::enable_if_t< DerefableToAO< T >::value > write(const std::string &filename, const T &ao)
Write out pointer-like object ao to file filename.
Definition Writer.h:49
std::enable_if_t< CIterable< RANGE >::value > write(std::ostream &stream, const RANGE &aos)
Definition Writer.h:74
void useCompression(const bool compress=true)
Use libz compression?
Definition Writer.h:153
virtual void writeAO(std::ostream &stream, const AnalysisObject &ao)=0
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition Writer.cc:98
void write(const std::string &filename, const AOITER &begin, const AOITER &end)
Definition Writer.h:109
std::enable_if_t< CIterable< RANGE >::value > write(const std::string &filename, const RANGE &aos)
Write out a collection of objects objs to file filename.
Definition Writer.h:81
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition Writer.cc:49
std::enable_if_t< DerefableToAO< T >::value > writeBody(std::ostream &stream, const T &ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition Writer.h:176
void setAOPrecision(const bool needsDP=false)
Set precision of numerical quantities for current AO in this writer's output.
Definition Writer.h:148
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Definition Writer.h:143
std::enable_if_t< DerefableToAO< T >::value > write(std::ostream &stream, const T &ao)
Write out pointer-like object ao to output stream stream.
Definition Writer.h:42
Anonymous namespace to limit visibility.
Writer & mkWriter(const std::string &format_name)
Factory function to make a writer object by format name or a filename.
Definition Writer.cc:25