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

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

#include <Writer.h>

Inheritance diagram for YODA::Writer:
YODA::WriterFLAT 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 (std::ostream &stream, const std::vector< const AnalysisObject * > &aos)
 
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)
 
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
 

Detailed Description

Pure virtual base class for various output writers.

Definition at line 19 of file Writer.h.

Constructor & Destructor Documentation

◆ ~Writer()

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

Virtual destructor.

Definition at line 23 of file Writer.h.

23{}

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 148 of file Writer.h.

148 {
149 _aoprecision = needsDP? std::numeric_limits<double>::max_digits10 : _precision;
150 }

Referenced by write().

◆ setPrecision()

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

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

Definition at line 143 of file Writer.h.

143 {
144 _precision = precision;
145 }

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

◆ useCompression()

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

Use libz compression?

Definition at line 153 of file Writer.h.

153 {
154 _compress = compress;
155 }

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

◆ write() [1/9]

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

Write out object ao to file filename.

Definition at line 49 of file Writer.cc.

49 {
50 std::vector<const AnalysisObject*> vec{&ao};
51 write(filename, vec);
52 }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition Writer.cc:49

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/9]

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 109 of file Writer.h.

109 {
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 }
void useCompression(const bool compress=true)
Use libz compression?
Definition Writer.h:153

References useCompression(), and write().

◆ write() [3/9]

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 81 of file Writer.h.

81 {
82 write(filename, std::begin(aos), std::end(aos));
83 }

References write().

◆ write() [4/9]

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 49 of file Writer.h.

49 {
50 write(filename, *ao);
51 }

References write().

◆ write() [5/9]

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

Write out object ao to output stream stream.

Definition at line 33 of file Writer.h.

33 {
34 // std::vector<const AnalysisObject*> vec{&ao};
35 std::vector<const AnalysisObject*> vec{&ao};
36 write(stream, vec);
37 }

References write().

◆ write() [6/9]

template<typename AOITER >
void YODA::Writer::write ( std::ostream &  stream,
const AOITER &  begin,
const AOITER &  end 
)
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 96 of file Writer.h.

96 {
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 }

References write().

◆ write() [7/9]

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 74 of file Writer.h.

74 {
75 write(stream, std::begin(aos), std::end(aos));
76 }

References write().

◆ write() [8/9]

void YODA::Writer::write ( std::ostream &  stream,
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: all others call this. Hence the specific AOs type.
Among other reasons, this is non-inline to hide zstr from the public API
Todo:
Remove the head/body/foot distinction?
Todo:
Why specifically LowStatsError?

Definition at line 55 of file Writer.cc.

55 {
56 std::unique_ptr<std::ostream> zos;
57 std::ostream* os = &stream;
58
59 // Write numbers in the "C" locale
60 std::locale prev_locale = os->getloc();
61 os->imbue(std::locale::classic());
62
63 // Wrap the stream if needed
64 if (_compress) {
65 #ifdef HAVE_LIBZ
66 // Doesn't work to always create zstr wrapper: have to only create if compressing :-/
67 // zstr::ostream zstream(stream);
68 // ostream& os = _compress ? zstream : stream;
69 os = new zstr::ostream(stream);
70 zos.reset(os);
71 #else
72 throw UserError("YODA was compiled without zlib support: can't write to a compressed stream");
73 #endif
74 }
75
76 // Write the data components
78 writeHead(*os);
79 bool first = true;
80 for (const AnalysisObject* aoptr : aos) {
81 setAOPrecision( aoptr->annotation("WriterDoublePrecision", 0) );
82 try {
83 if (!first) *os << "\n"; //< blank line between items
84 writeBody(*os, aoptr);
85 first = false;
86 } catch (const LowStatsError& ex) {
88 std::cerr << "LowStatsError in writing AnalysisObject " << aoptr->title() << ":\n" << ex.what() << "\n";
89 }
90 }
91 writeFoot(*os);
92 *os << flush;
93
94 os->imbue(prev_locale);
95 }
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
Definition Writer.h:164
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
Definition Writer.h:179
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition Writer.cc:98
void setAOPrecision(const bool needsDP=false)
Set precision of numerical quantities for current AO in this writer's output.
Definition Writer.h:148

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

◆ write() [9/9]

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 42 of file Writer.h.

42 {
43 write(stream, *ao);
44 }

References write().

◆ 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().

◆ 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 103 of file Writer.cc.

103 {
104 try {
105 writeAO(stream, ao);
106 }
107 catch(...) {
108 ostringstream oss;
109 oss << "Unrecognised analysis object type " << ao.type() << " in Writer::write";
110 throw Exception(oss.str());
111 }
112 }
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 98 of file Writer.cc.

98 {
99 if (!ao) throw WriteError("Attempting to write a null AnalysisObject*");
100 writeBody(stream, *ao);
101 }

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 176 of file Writer.h.

176{ 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 179 of file Writer.h.

179{ 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 164 of file Writer.h.

164{}

Referenced by write().


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