yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
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-2017 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 
36 
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;
45  vec.push_back(&ao);
46  std::cout << std::endl;
47  write(stream, vec);
48  std::cout << std::endl;
49  }
50 
52  template <typename T>
53  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
54  write(std::ostream& stream, const T& ao) {
55  write(stream, *ao);
56  }
57 
59  template <typename T>
60  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
61  write(const std::string& filename, const T& ao) {
62  write(filename, *ao);
63  }
64 
66 
67 
69 
70 
77  void write(std::ostream& stream, const std::vector<const AnalysisObject*>& aos);
78 
79 
84  template <typename RANGE>
85  typename std::enable_if<CIterable<RANGE>::value>::type
86  write(std::ostream& stream, const RANGE& aos) {
87  write(stream, std::begin(aos), std::end(aos));
88  }
89 
91  template <typename RANGE>
92  typename std::enable_if<CIterable<RANGE>::value>::type
93  write(const std::string& filename, const RANGE& aos) {
94  write(filename, std::begin(aos), std::end(aos));
95  }
96 
98 
99 
101 
102 
107  template <typename AOITER>
108  void write(std::ostream& stream, const AOITER& begin, const AOITER& end) {
109  std::vector<const AnalysisObject*> vec;
110  // vec.reserve(std::distance(begin, end));
111  for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
112  write(stream, vec);
113  }
114 
115 
120  template <typename AOITER>
121  void write(const std::string& filename, const AOITER& begin, const AOITER& end) {
122  std::vector<const AnalysisObject*> vec;
123  // vec.reserve(std::distance(begin, end));
124  for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
125 
126  if (filename != "-") {
127  try {
128  const size_t lastdot = filename.find_last_of(".");
129  std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
130  const bool compress = (fmt == "gz");
131  useCompression(compress);
132  //
133  std::ofstream stream;
134  stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
135  stream.open(filename.c_str());
136  write(stream, vec);
137  } catch (std::ofstream::failure& e) {
138  throw WriteError("Writing to filename " + filename + " failed: " + e.what());
139  }
140  } else {
141  try {
142  write(std::cout, vec);
143  } catch (std::runtime_error& e) {
144  throw WriteError("Writing to stdout failed: " + std::string(e.what()));
145  }
146  }
147 
148  }
149 
151 
152 
154  void setPrecision(int precision) {
155  _precision = precision;
156  }
157 
159  void useCompression(bool compress=true) {
160  _compress = compress;
161  }
162 
163 
164  protected:
165 
167 
168 
170  virtual void writeHead(std::ostream&) {}
171 
173  virtual void writeBody(std::ostream& stream, const AnalysisObject* ao);
174 
176  virtual void writeBody(std::ostream& stream, const AnalysisObject& ao);
177 
180  template <typename T>
181  typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
182  writeBody(std::ostream& stream, const T& ao) { writeBody(stream, *ao); }
183 
185  virtual void writeFoot(std::ostream& stream) { stream << std::flush; }
186 
188 
189 
191 
192 
193  virtual void writeCounter(std::ostream& stream, const Counter& c) = 0;
194  virtual void writeHisto1D(std::ostream& os, const Histo1D& h) = 0;
195  virtual void writeHisto2D(std::ostream& os, const Histo2D& h) = 0;
196  virtual void writeProfile1D(std::ostream& os, const Profile1D& p) = 0;
197  virtual void writeProfile2D(std::ostream& os, const Profile2D& p) = 0;
198  virtual void writeScatter1D(std::ostream& os, const Scatter1D& s) = 0;
199  virtual void writeScatter2D(std::ostream& os, const Scatter2D& s) = 0;
200  virtual void writeScatter3D(std::ostream& os, const Scatter3D& s) = 0;
201 
203 
204 
206  int _precision;
207 
209  bool _compress;
210 
211  };
212 
213 
215  Writer& mkWriter(const std::string& format_name);
216 
217 
218 }
219 
220 #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:93
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:24
void write(std::ostream &stream, const AOITER &begin, const AOITER &end)
Definition: Writer.h:108
void useCompression(bool compress=true)
Use libz compression?
Definition: Writer.h:159
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:23
void write(const std::string &filename, const AOITER &begin, const AOITER &end)
Definition: Writer.h:121
virtual ~Writer()
Virtual destructor.
Definition: Writer.h:32
A one-dimensional histogram.
Definition: Histo1D.h:26
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:54
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:61
virtual void writeProfile1D(std::ostream &os, const Profile1D &p)=0
A two-dimensional profile histogram.
Definition: Profile2D.h:31
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:182
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:86
void setPrecision(int precision)
Set precision of numerical quantities in this writer&#39;s output.
Definition: Writer.h:154
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:24
AnalysisObject is the base class for histograms and scatters.
virtual void writeCounter(std::ostream &stream, const Counter &c)=0
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:23
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:170
A two-dimensional histogram.
Definition: Histo2D.h:31
A one-dimensional profile histogram.
Definition: Profile1D.h:33
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:185