15#define _XOPEN_SOURCE 700
16#include "zstr/zstr.hpp"
31 const size_t lastdot = name.find_last_of(
".");
32 string fmt = Utils::toLower(lastdot == string::npos ? name : name.substr(lastdot+1));
35 if (compress && fmt ==
"gz"s) {
37 throw UserError(
"YODA was compiled without zlib support: can't write " + name);
39 const size_t lastbutonedot = (lastdot == string::npos) ? string::npos : name.find_last_of(
".", lastdot-1);
40 fmt = Utils::toLower(lastbutonedot == string::npos ? name : name.substr(lastbutonedot+1));
50 if (!w)
throw UserError(
"Format cannot be identified from string '" + name +
"'");
57 std::vector<const AnalysisObject*> vec{&ao};
63 void Writer::write(YODA_H5::File& file,
const std::vector<const AnalysisObject*>& aos) {
63 void Writer::write(YODA_H5::File& file,
const std::vector<const AnalysisObject*>& aos) {
…}
69 void Writer::write(ostream& stream,
const vector<const AnalysisObject*>& aos,
int precision) {
70 std::unique_ptr<std::ostream> zos;
71 std::ostream* os = &stream;
76 std::locale prev_locale = os->getloc();
77 os->imbue(std::locale::classic());
85 os =
new zstr::ostream(stream);
88 throw UserError(
"YODA was compiled without zlib support: can't write to a compressed stream");
98 if (!first) *os <<
"\n";
105 os->imbue(prev_locale);
69 void Writer::write(ostream& stream,
const vector<const AnalysisObject*>& aos,
int precision) {
…}
110 if (!ao)
throw WriteError(
"Attempting to write a null AnalysisObject*");
120 oss <<
"Unrecognised analysis object type " << ao.
type() <<
" in Writer::write";
AnalysisObject is the base class for histograms and scatters.
virtual std::string type() const
Get name of the analysis object type.
Generic unspecialised YODA runtime error.
Error for problems introduced outside YODA, to put it nicely.
Error for file writing errors.
static Writer & create()
Singleton creation function.
static Writer & create()
Singleton creation function.
static Writer & create()
Singleton creation function.
Pure virtual base class for various output writers.
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
void useCompression(const bool compress=true)
Use libz compression?
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.
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
virtual void writeAOS(YODA_H5::File &file, const vector< const AnalysisObject * > &aos)=0
void setAOPrecision(const bool needsDP=false)
Set precision of numerical quantities for current AO in this writer's output.
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Anonymous namespace to limit visibility.
static bool enableH5compression
Writer & mkWriter(const std::string &format_name)
Factory function to make a writer object by format name or a filename.