yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.1.0
YODA::Writer Class Referenceabstract

Pure virtual base class for various output writers. More...

#include <Writer.h>

Inheritance diagram for YODA::Writer:
YODA::WriterFLAT YODA::WriterH5 YODA::WriterYODA YODA::WriterYODA1

Public Member Functions

virtual ~Writer ()
 Virtual destructor.
 
void setPrecision (int precision)
 Set precision of numerical quantities in this writer's output.
 
void setAOPrecision (const bool needsDP=false)
 Set precision of numerical quantities for current AO in this writer's output.
 
void useCompression (const bool compress=true)
 Use libz compression?
 
Writing a single analysis object.
void write (const std::string &filename, const AnalysisObject &ao)
 Write out object ao to file filename.
 
void write (std::ostream &stream, const AnalysisObject &ao)
 Write out object ao to output stream stream.
 
template<typename T >
std::enable_if_t< DerefableToAO< T >::value > write (std::ostream &stream, const T &ao)
 Write out pointer-like object ao to output stream stream.
 
template<typename T >
std::enable_if_t< DerefableToAO< T >::value > write (const std::string &filename, const T &ao)
 Write out pointer-like object ao to file filename.
 
Writing multiple analysis objects by collection.
void write (YODA_H5::File &file, const std::vector< const AnalysisObject * > &aos)
 
void write (std::ostream &stream, const std::vector< const AnalysisObject * > &aos, int precision=-1)
 
template<typename RANGE >
std::enable_if_t< CIterable< RANGE >::value > write (std::ostream &stream, const RANGE &aos)
 
template<typename RANGE >
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.
 
Writing multiple analysis objects by iterator range.
template<typename AOITER >
void write (std::ostream &stream, const AOITER &begin, const AOITER &end, int precision=-1)
 
template<typename AOITER >
void write (const std::string &filename, const AOITER &begin, const AOITER &end)
 

Protected Member Functions

Main writer elements
virtual void writeHead (std::ostream &)
 Write any opening boilerplate required by the format to stream.
 
virtual void writeBody (std::ostream &stream, const AnalysisObject *ao)
 Write the body elements corresponding to AnalysisObject ao to stream.
 
virtual void writeBody (std::ostream &stream, const AnalysisObject &ao)
 Write the body elements corresponding to AnalysisObject pointer ao to stream.
 
template<typename T >
std::enable_if_t< DerefableToAO< T >::value > writeBody (std::ostream &stream, const T &ao)
 Write the body elements corresponding to AnalysisObject ao to stream.
 
virtual void writeFoot (std::ostream &stream)
 Write any closing boilerplate required by the format to stream.
 
Specific AO type writer implementations, to be implemented in derived classes
virtual void writeAO (std::ostream &stream, const AnalysisObject &ao)=0
 
virtual void writeAOS (YODA_H5::File &file, const vector< const AnalysisObject * > &aos)=0
 

Detailed Description

Pure virtual base class for various output writers.

Definition at line 31 of file Writer.h.

Constructor & Destructor Documentation

◆ ~Writer()

virtual YODA::Writer::~Writer ( )
inlinevirtual

Virtual destructor.

Definition at line 35 of file Writer.h.

35{}

Member Function Documentation

◆ setAOPrecision()

void YODA::Writer::setAOPrecision ( const bool  needsDP = false)
inline

Set precision of numerical quantities for current AO in this writer's output.

Definition at line 181 of file Writer.h.

181 {
182 if (needsDP) _aoprecision = std::numeric_limits<double>::max_digits10;
183 else if (_precision > 0) _aoprecision = _precision;
184 else _aoprecision = 6;
185 }

Referenced by write().

◆ setPrecision()

void YODA::Writer::setPrecision ( int  precision)
inline

Set precision of numerical quantities in this writer's output.

Definition at line 176 of file Writer.h.

176 {
177 _precision = precision;
178 }

Referenced by YODA::WriterFLAT::create(), YODA::WriterH5::create(), YODA::WriterYODA::create(), YODA::WriterYODA1::create(), YODA::write(), YODA::write(), YODA::write(), YODA::write(), YODA::write(), YODA::write(), and write().

◆ useCompression()

void YODA::Writer::useCompression ( const bool  compress = true)
inline

Use libz compression?

Definition at line 188 of file Writer.h.

188 {
189 _compress = compress;
190 }

Referenced by YODA::mkWriter(), and write().

◆ write() [1/10]

void YODA::Writer::write ( const std::string &  filename,
const AnalysisObject ao 
)

Write out object ao to file filename.

Definition at line 56 of file Writer.cc.

56 {
57 std::vector<const AnalysisObject*> vec{&ao};
58 write(filename, vec);
59 }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition Writer.cc:56

References write().

Referenced by write(), YODA::write(), write(), YODA::write(), write(), YODA::write(), write(), YODA::write(), YODA::write(), YODA::write(), write(), write(), write(), and write().

◆ write() [2/10]

template<typename AOITER >
void YODA::Writer::write ( const std::string &  filename,
const AOITER &  begin,
const AOITER &  end 
)
inline

Write out the objects specified by start iterator begin and end iterator end to file filename.

Todo:
Add SFINAE trait checking for AOITER = DerefableToAO

Definition at line 130 of file Writer.h.

130 {
131 std::vector<const AnalysisObject*> vec;
132 // vec.reserve(std::distance(begin, end));
133 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
134
135 if (filename != "-") {
136 try {
137 const size_t lastdot = filename.find_last_of(".");
138 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
139 const bool compress = (fmt == "gz") || (fmt == "h5" && enableH5compression);
140 useCompression(compress);
141 #ifdef HAVE_HDF5
142 // check if the requested format is H5
143 if (Utils::startswith(fmt, "h5")) {
144 try {
145 YODA_H5::File h5(filename, YODA_H5::File::OpenOrCreate | YODA_H5::File::Truncate);
146 write(h5, vec);
147 } catch(...) {
148 throw WriteError("Failed to open HDF5 file " + filename);
149 }
150 return;
151 }
152 #endif
153 // try writing to stream
154 std::ofstream stream;
155 stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
156 stream.open(filename.c_str());
157 if (stream.fail())
158 throw WriteError("Writing to filename " + filename + " failed");
159 write(stream, vec);
160 } catch (std::ofstream::failure& e) {
161 throw WriteError("Writing to filename " + filename + " failed: " + e.what());
162 }
163 } else {
164 try {
165 write(std::cout, vec);
166 } catch (std::runtime_error& e) {
167 throw WriteError("Writing to stdout failed: " + std::string(e.what()));
168 }
169 }
170
171 }
void useCompression(const bool compress=true)
Use libz compression?
Definition Writer.h:188
static bool enableH5compression
Definition Writer.h:28

References YODA::enableH5compression, useCompression(), and write().

◆ write() [3/10]

template<typename RANGE >
std::enable_if_t< CIterable< RANGE >::value > YODA::Writer::write ( const std::string &  filename,
const RANGE &  aos 
)
inline

Write out a collection of objects objs to file filename.

Definition at line 102 of file Writer.h.

102 {
103 write(filename, std::begin(aos), std::end(aos));
104 }

References write().

◆ write() [4/10]

template<typename T >
std::enable_if_t< DerefableToAO< T >::value > YODA::Writer::write ( const std::string &  filename,
const T &  ao 
)
inline

Write out pointer-like object ao to file filename.

Definition at line 60 of file Writer.h.

60 {
61 write(filename, *ao);
62 }

References write().

◆ write() [5/10]

void YODA::Writer::write ( std::ostream &  stream,
const AnalysisObject ao 
)
inline

Write out object ao to output stream stream.

Definition at line 45 of file Writer.h.

45 {
46 std::vector<const AnalysisObject*> vec{&ao};
47 write(stream, vec);
48 }

References write().

◆ write() [6/10]

template<typename AOITER >
void YODA::Writer::write ( std::ostream &  stream,
const AOITER &  begin,
const AOITER &  end,
int  precision = -1 
)
inline

Write out the objects specified by start iterator begin and end iterator end to output stream stream.

Todo:
Add SFINAE trait checking for AOITER = DerefableToAO

Definition at line 117 of file Writer.h.

117 {
118 std::vector<const AnalysisObject*> vec;
119 // vec.reserve(std::distance(begin, end));
120 for (AOITER ipao = begin; ipao != end; ++ipao) vec.push_back(&(**ipao));
121 write(stream, vec, precision);
122 }

References write().

◆ write() [7/10]

template<typename RANGE >
std::enable_if_t< CIterable< RANGE >::value > YODA::Writer::write ( std::ostream &  stream,
const RANGE &  aos 
)
inline

Write out a collection of objects objs to output stream stream.

Note
The enable_if call checks whether RANGE is const_iterable, if yes the return type is void. If not, this template will not be a candidate in the lookup

Definition at line 95 of file Writer.h.

95 {
96 write(stream, std::begin(aos), std::end(aos));
97 }

References write().

◆ write() [8/10]

void YODA::Writer::write ( std::ostream &  stream,
const std::vector< const AnalysisObject * > &  aos,
int  precision = -1 
)

Write out a vector of AO pointers (untemplated=exact type-match) to the given stream

Note
This is the canonical function for implementing AO writing to stream: all others call this.
Among other reasons, this is non-inline to hide zstr from the public API
Todo:
Remove the head/body/foot distinction?

Definition at line 69 of file Writer.cc.

69 {
70 std::unique_ptr<std::ostream> zos;
71 std::ostream* os = &stream;
72
73 setPrecision(precision);
74
75 // Write numbers in the "C" locale
76 std::locale prev_locale = os->getloc();
77 os->imbue(std::locale::classic());
78
79 // Wrap the stream if needed
80 if (_compress) {
81 #ifdef HAVE_LIBZ
82 // Doesn't work to always create zstr wrapper: have to only create if compressing :-/
83 // zstr::ostream zstream(stream);
84 // ostream& os = _compress ? zstream : stream;
85 os = new zstr::ostream(stream);
86 zos.reset(os);
87 #else
88 throw UserError("YODA was compiled without zlib support: can't write to a compressed stream");
89 #endif
90 }
91
92 // Write the data components
94 writeHead(*os);
95 bool first = true;
96 for (const AnalysisObject* aoptr : aos) {
97 setAOPrecision( aoptr->annotation("WriterDoublePrecision", 0) );
98 if (!first) *os << "\n"; //< blank line between items
99 writeBody(*os, aoptr);
100 first = false;
101 }
102 writeFoot(*os);
103 *os << flush;
104
105 os->imbue(prev_locale);
106 }
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
Definition Writer.h:199
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
Definition Writer.h:214
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition Writer.cc:109
void setAOPrecision(const bool needsDP=false)
Set precision of numerical quantities for current AO in this writer's output.
Definition Writer.h:181
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Definition Writer.h:176

References setAOPrecision(), setPrecision(), writeBody(), writeFoot(), and writeHead().

◆ write() [9/10]

template<typename T >
std::enable_if_t< DerefableToAO< T >::value > YODA::Writer::write ( std::ostream &  stream,
const T &  ao 
)
inline

Write out pointer-like object ao to output stream stream.

Definition at line 53 of file Writer.h.

53 {
54 write(stream, *ao);
55 }

References write().

◆ write() [10/10]

void YODA::Writer::write ( YODA_H5::File &  file,
const std::vector< const AnalysisObject * > &  aos 
)

Write out a vector of AO pointers (untemplated=exact type-match) to the given stream

Note
This is the canonical function for implementing AO writing to H5: all others call this.
Among other reasons, this is non-inline to hide zstr from the public API

Definition at line 63 of file Writer.cc.

63 {
64 writeAOS(file, aos);
65 }
virtual void writeAOS(YODA_H5::File &file, const vector< const AnalysisObject * > &aos)=0

References writeAOS().

◆ writeAO()

virtual void YODA::Writer::writeAO ( std::ostream &  stream,
const AnalysisObject ao 
)
protectedpure virtual

Implemented in YODA::WriterFLAT, YODA::WriterYODA, and YODA::WriterYODA1.

Referenced by writeBody().

◆ writeAOS()

virtual void YODA::Writer::writeAOS ( YODA_H5::File &  file,
const vector< const AnalysisObject * > &  aos 
)
protectedpure virtual

Implemented in YODA::WriterH5.

Referenced by write().

◆ writeBody() [1/3]

void YODA::Writer::writeBody ( std::ostream &  stream,
const AnalysisObject ao 
)
protectedvirtual

Write the body elements corresponding to AnalysisObject pointer ao to stream.

Definition at line 114 of file Writer.cc.

114 {
115 try {
116 writeAO(stream, ao);
117 }
118 catch(...) {
119 ostringstream oss;
120 oss << "Unrecognised analysis object type " << ao.type() << " in Writer::write";
121 throw Exception(oss.str());
122 }
123 }
virtual void writeAO(std::ostream &stream, const AnalysisObject &ao)=0

References YODA::AnalysisObject::type(), and writeAO().

◆ writeBody() [2/3]

void YODA::Writer::writeBody ( std::ostream &  stream,
const AnalysisObject ao 
)
protectedvirtual

Write the body elements corresponding to AnalysisObject ao to stream.

Definition at line 109 of file Writer.cc.

109 {
110 if (!ao) throw WriteError("Attempting to write a null AnalysisObject*");
111 writeBody(stream, *ao);
112 }

References writeBody().

Referenced by write(), and writeBody().

◆ writeBody() [3/3]

template<typename T >
std::enable_if_t< DerefableToAO< T >::value > YODA::Writer::writeBody ( std::ostream &  stream,
const T &  ao 
)
inlineprotected

Write the body elements corresponding to AnalysisObject ao to stream.

Note
Requires that ao is dereferenceable to an AnalysisObject, via the DerefableToAO<T> trait,

Definition at line 211 of file Writer.h.

211{ writeBody(stream, *ao); }

References writeBody().

Referenced by writeBody().

◆ writeFoot()

virtual void YODA::Writer::writeFoot ( std::ostream &  stream)
inlineprotectedvirtual

Write any closing boilerplate required by the format to stream.

Definition at line 214 of file Writer.h.

214{ stream << std::flush; }

Referenced by write().

◆ writeHead()

virtual void YODA::Writer::writeHead ( std::ostream &  )
inlineprotectedvirtual

Write any opening boilerplate required by the format to stream.

Definition at line 199 of file Writer.h.

199{}

Referenced by write().


The documentation for this class was generated from the following files: