yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.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-2021 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_Writer_h
7 #define YODA_Writer_h
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Counter.h"
11 #include "YODA/Histo1D.h"
12 #include "YODA/Histo2D.h"
13 #include "YODA/Profile1D.h"
14 #include "YODA/Profile2D.h"
15 #include "YODA/Scatter1D.h"
16 #include "YODA/Scatter2D.h"
17 #include "YODA/Scatter3D.h"
18 #include "YODA/Utils/Traits.h"
19 #include <type_traits>
20 #include <fstream>
21 #include <ostream>
22 #include <string>
23 
24 namespace YODA {
25 
26 
28  class Writer {
29  public:
30 
32  virtual ~Writer() {}
33 
34 
37 
39  void write(const std::string& filename, const AnalysisObject& ao);
40 
42  void write(std::ostream& stream, const AnalysisObject& ao) {
43  // std::vector<const AnalysisObject*> vec{&ao};
44  std::vector<const AnalysisObject*> vec{&ao};
45  write(stream, vec);
46  }
47 
49  template <typename T>
50  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
51  write(std::ostream& stream, const T& ao) {
52  write(stream, *ao);
53  }
54 
56  template <typename T>
57  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
58  write(const std::string& filename, const T& ao) {
59  write(filename, *ao);
60  }
61 
63 
64 
67 
74  void write(std::ostream& stream, const std::vector<const AnalysisObject*>& aos);
75 
76 
81  template <typename RANGE>
82  typename std::enable_if<CIterable<RANGE>::value>::type
83  write(std::ostream& stream, const RANGE& aos) {
84  write(stream, std::begin(aos), std::end(aos));
85  }
86 
88  template <typename RANGE>
89  typename std::enable_if<CIterable<RANGE>::value>::type
90  write(const std::string& filename, const RANGE& aos) {
91  write(filename, std::begin(aos), std::end(aos));
92  }
93 
95 
96 
99 
104  template <typename AOITER>
105  void write(std::ostream& stream, const AOITER& begin, const AOITER& end) {
106  std::vector<const AnalysisObject*> vec;
107  // vec.reserve(std::distance(begin, end));
108  for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
109  write(stream, vec);
110  }
111 
112 
117  template <typename AOITER>
118  void write(const std::string& filename, const AOITER& begin, const AOITER& end) {
119  std::vector<const AnalysisObject*> vec;
120  // vec.reserve(std::distance(begin, end));
121  for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
122 
123  if (filename != "-") {
124  try {
125  const size_t lastdot = filename.find_last_of(".");
126  std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
127  const bool compress = (fmt == "gz");
128  useCompression(compress);
129  //
130  std::ofstream stream;
131  stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
132  stream.open(filename.c_str());
133  write(stream, vec);
134  } catch (std::ofstream::failure& e) {
135  throw WriteError("Writing to filename " + filename + " failed: " + e.what());
136  }
137  } else {
138  try {
139  write(std::cout, vec);
140  } catch (std::runtime_error& e) {
141  throw WriteError("Writing to stdout failed: " + std::string(e.what()));
142  }
143  }
144 
145  }
146 
148 
149 
151  void setPrecision(int precision) {
152  _precision = precision;
153  }
154 
156  void useCompression(bool compress=true) {
157  _compress = compress;
158  }
159 
160 
161  protected:
162 
165 
167  virtual void writeHead(std::ostream&) {}
168 
170  virtual void writeBody(std::ostream& stream, const AnalysisObject* ao);
171 
173  virtual void writeBody(std::ostream& stream, const AnalysisObject& ao);
174 
177  template <typename T>
178  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
179  writeBody(std::ostream& stream, const T& ao) { writeBody(stream, *ao); }
180 
182  virtual void writeFoot(std::ostream& stream) { stream << std::flush; }
183 
185 
186 
189 
190  virtual void writeCounter(std::ostream& stream, const Counter& c) = 0;
191  virtual void writeHisto1D(std::ostream& os, const Histo1D& h) = 0;
192  virtual void writeHisto2D(std::ostream& os, const Histo2D& h) = 0;
193  virtual void writeProfile1D(std::ostream& os, const Profile1D& p) = 0;
194  virtual void writeProfile2D(std::ostream& os, const Profile2D& p) = 0;
195  virtual void writeScatter1D(std::ostream& os, const Scatter1D& s) = 0;
196  virtual void writeScatter2D(std::ostream& os, const Scatter2D& s) = 0;
197  virtual void writeScatter3D(std::ostream& os, const Scatter3D& s) = 0;
198 
200 
201 
203  int _precision;
204 
206  bool _compress;
207 
208  };
209 
210 
212  Writer& mkWriter(const std::string& format_name);
213 
214 
215 }
216 
217 #endif
virtual void writeHisto1D(std::ostream &os, const Histo1D &h)=0
std::enable_if< CIterable< RANGE >::value >::type write(const std::string &filename, const RANGE &aos)
Write out a collection of objects objs to file filename.
Definition: Writer.h:90
virtual void writeScatter1D(std::ostream &os, const Scatter1D &s)=0
A very generic data type which is just a collection of 2D data points with errors.
Definition: Scatter2D.h:25
void write(std::ostream &stream, const AOITER &begin, const AOITER &end)
Definition: Writer.h:105
void useCompression(bool compress=true)
Use libz compression?
Definition: Writer.h:156
virtual void writeScatter2D(std::ostream &os, const Scatter2D &s)=0
virtual void writeHisto2D(std::ostream &os, const Histo2D &h)=0
Error for file writing errors.
Definition: Exceptions.h:93
A weighted counter.
Definition: Counter.h:24
void write(const std::string &filename, const AOITER &begin, const AOITER &end)
Definition: Writer.h:118
virtual ~Writer()
Virtual destructor.
Definition: Writer.h:32
A one-dimensional histogram.
Definition: Histo1D.h:28
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition: Writer.cc:92
void write(std::ostream &stream, const AnalysisObject &ao)
Write out object ao to output stream stream.
Definition: Writer.h:42
std::enable_if< DerefableToAO< T >::value >::type write(std::ostream &stream, const T &ao)
Write out pointer-like object ao to output stream stream.
Definition: Writer.h:51
std::enable_if< DerefableToAO< T >::value >::type write(const std::string &filename, const T &ao)
Write out pointer-like object ao to file filename.
Definition: Writer.h:58
virtual void writeProfile1D(std::ostream &os, const Profile1D &p)=0
A two-dimensional profile histogram.
Definition: Profile2D.h:32
std::enable_if< DerefableToAO< T >::value >::type writeBody(std::ostream &stream, const T &ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition: Writer.h:179
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50
Pure virtual base class for various output writers.
Definition: Writer.h:28
virtual void writeProfile2D(std::ostream &os, const Profile2D &p)=0
std::enable_if< CIterable< RANGE >::value >::type write(std::ostream &stream, const RANGE &aos)
Definition: Writer.h:83
void setPrecision(int precision)
Set precision of numerical quantities in this writer&#39;s output.
Definition: Writer.h:151
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:25
AnalysisObject is the base class for histograms and scatters.
virtual void writeCounter(std::ostream &stream, const Counter &c)=0
virtual void writeScatter3D(std::ostream &os, const Scatter3D &s)=0
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
Definition: Writer.h:167
A two-dimensional histogram.
Definition: Histo2D.h:32
A one-dimensional profile histogram.
Definition: Profile1D.h:35
Writer & mkWriter(const std::string &format_name)
Factory function to make a writer object by format name or a filename.
Definition: Writer.cc:25
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
Definition: Writer.h:182