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

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

#include <Writer.h>

Inheritance diagram for YODA::Writer:
YODA::WriterAIDA YODA::WriterFLAT YODA::WriterYODA

Public Member Functions

virtual ~Writer ()
 Virtual destructor. More...
 
void setPrecision (int precision)
 Set precision of numerical quantities in this writer's output. More...
 
void useCompression (bool compress=true)
 Use libz compression? More...
 
Writing a single analysis object.
void write (const std::string &filename, const AnalysisObject &ao)
 Write out object ao to file filename. More...
 
void write (std::ostream &stream, const AnalysisObject &ao)
 Write out object ao to output stream stream. More...
 
template<typename T >
std::enable_if< DerefableToAO< T >::value >::type write (std::ostream &stream, const T &ao)
 Write out pointer-like object ao to output stream stream. More...
 
template<typename T >
std::enable_if< DerefableToAO< T >::value >::type write (const std::string &filename, const T &ao)
 Write out pointer-like object ao to file filename. More...
 
Writing multiple analysis objects by collection.
void write (std::ostream &stream, const std::vector< const AnalysisObject *> &aos)
 
template<typename RANGE >
std::enable_if< CIterable< RANGE >::value >::type write (std::ostream &stream, const RANGE &aos)
 
template<typename RANGE >
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. More...
 
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. More...
 
virtual void writeBody (std::ostream &stream, const AnalysisObject *ao)
 Write the body elements corresponding to AnalysisObject ao to stream. More...
 
virtual void writeBody (std::ostream &stream, const AnalysisObject &ao)
 Write the body elements corresponding to AnalysisObject pointer ao to stream. More...
 
template<typename T >
std::enable_if< DerefableToAO< T >::value >::type writeBody (std::ostream &stream, const T &ao)
 Write the body elements corresponding to AnalysisObject ao to stream. More...
 
virtual void writeFoot (std::ostream &stream)
 Write any closing boilerplate required by the format to stream. More...
 
Specific AO type writer implementations, to be implemented in derived classes
virtual void writeCounter (std::ostream &stream, const Counter &c)=0
 
virtual void writeHisto1D (std::ostream &os, const Histo1D &h)=0
 
virtual void writeHisto2D (std::ostream &os, const Histo2D &h)=0
 
virtual void writeProfile1D (std::ostream &os, const Profile1D &p)=0
 
virtual void writeProfile2D (std::ostream &os, const Profile2D &p)=0
 
virtual void writeScatter1D (std::ostream &os, const Scatter1D &s)=0
 
virtual void writeScatter2D (std::ostream &os, const Scatter2D &s)=0
 
virtual void writeScatter3D (std::ostream &os, const Scatter3D &s)=0
 

Detailed Description

Pure virtual base class for various output writers.

Definition at line 28 of file Writer.h.

Constructor & Destructor Documentation

◆ ~Writer()

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

Virtual destructor.

Definition at line 32 of file Writer.h.

References write().

32 {}

Member Function Documentation

◆ setPrecision()

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

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

Definition at line 154 of file Writer.h.

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

154  {
155  _precision = precision;
156  }

◆ useCompression()

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

Use libz compression?

Definition at line 159 of file Writer.h.

159  {
160  _compress = compress;
161  }

◆ write() [1/9]

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

Write out object ao to file filename.

Definition at line 50 of file Writer.cc.

References YODA::write().

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

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

◆ write() [2/9]

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

Write out object ao to output stream stream.

Definition at line 42 of file Writer.h.

References write().

42  {
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  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [3/9]

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

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

Definition at line 54 of file Writer.h.

References write().

54  {
55  write(stream, *ao);
56  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [4/9]

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

Write out pointer-like object ao to file filename.

Definition at line 61 of file Writer.h.

References write().

61  {
62  write(filename, *ao);
63  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [5/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 56 of file Writer.cc.

56  {
57  std::unique_ptr<std::ostream> zos;
58  std::ostream* os = &stream;
59 
60  // Wrap the stream if needed
61  if (_compress) {
62  #ifdef HAVE_LIBZ
63  // Doesn't work to always create zstr wrapper: have to only create if compressing :-/
64  // zstr::ostream zstream(stream);
65  // ostream& os = _compress ? zstream : stream;
66  os = new zstr::ostream(stream);
67  zos.reset(os);
68  #else
69  throw UserError("YODA was compiled without zlib support: can't write to a compressed stream");
70  #endif
71  }
72 
73  // Write the data components
75  writeHead(*os);
76  bool first = true;
77  for (const AnalysisObject* aoptr : aos) {
78  try {
79  if (!first) *os << "\n"; //< blank line between items
80  writeBody(*os, aoptr);
81  first = false;
82  } catch (const LowStatsError& ex) {
84  std::cerr << "LowStatsError in writing AnalysisObject " << aoptr->title() << ":\n" << ex.what() << "\n";
85  }
86  }
87  writeFoot(*os);
88  *os << flush;
89  }
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition: Writer.cc:92
virtual void writeHead(std::ostream &)
Write any opening boilerplate required by the format to stream.
Definition: Writer.h:170
virtual void writeFoot(std::ostream &stream)
Write any closing boilerplate required by the format to stream.
Definition: Writer.h:185

◆ write() [6/9]

template<typename RANGE >
std::enable_if<CIterable<RANGE>::value>::type 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 86 of file Writer.h.

References write().

86  {
87  write(stream, std::begin(aos), std::end(aos));
88  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [7/9]

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

Write out a collection of objects objs to file filename.

Definition at line 93 of file Writer.h.

References write().

93  {
94  write(filename, std::begin(aos), std::end(aos));
95  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [8/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 108 of file Writer.h.

References write().

108  {
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  }
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ write() [9/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 121 of file Writer.h.

121  {
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  }
void useCompression(bool compress=true)
Use libz compression?
Definition: Writer.h:159
void write(const std::string &filename, const AnalysisObject &ao)
Write out object ao to file filename.
Definition: Writer.cc:50

◆ writeBody() [1/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 92 of file Writer.cc.

Referenced by writeHead().

92  {
93  if (!ao) throw WriteError("Attempting to write a null AnalysisObject*");
94  writeBody(stream, *ao);
95  }
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition: Writer.cc:92

◆ writeBody() [2/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 97 of file Writer.cc.

References YODA::AnalysisObject::type().

97  {
98  const string aotype = ao.type();
99  if (aotype == "Counter") {
100  writeCounter(stream, dynamic_cast<const Counter&>(ao));
101  } else if (aotype == "Histo1D") {
102  writeHisto1D(stream, dynamic_cast<const Histo1D&>(ao));
103  } else if (aotype == "Histo2D") {
104  writeHisto2D(stream, dynamic_cast<const Histo2D&>(ao));
105  } else if (aotype == "Profile1D") {
106  writeProfile1D(stream, dynamic_cast<const Profile1D&>(ao));
107  } else if (aotype == "Profile2D") {
108  writeProfile2D(stream, dynamic_cast<const Profile2D&>(ao));
109  } else if (aotype == "Scatter1D") {
110  writeScatter1D(stream, dynamic_cast<const Scatter1D&>(ao));
111  } else if (aotype == "Scatter2D") {
112  writeScatter2D(stream, dynamic_cast<const Scatter2D&>(ao));
113  } else if (aotype == "Scatter3D") {
114  writeScatter3D(stream, dynamic_cast<const Scatter3D&>(ao));
115  } else if (aotype[0] == '_') {
116  // Skip writing AO types with underscore prefixes (needed e.g. for Rivet wrappers)
117  // maybe write a comment line in the output
118  } else {
119  ostringstream oss;
120  oss << "Unrecognised analysis object type " << aotype << " in Writer::write";
121  throw Exception(oss.str());
122  }
123  }
virtual void writeHisto1D(std::ostream &os, const Histo1D &h)=0
virtual void writeScatter1D(std::ostream &os, const Scatter1D &s)=0
virtual void writeScatter2D(std::ostream &os, const Scatter2D &s)=0
virtual void writeHisto2D(std::ostream &os, const Histo2D &h)=0
virtual void writeProfile1D(std::ostream &os, const Profile1D &p)=0
virtual void writeProfile2D(std::ostream &os, const Profile2D &p)=0
virtual void writeCounter(std::ostream &stream, const Counter &c)=0
virtual void writeScatter3D(std::ostream &os, const Scatter3D &s)=0

◆ writeBody() [3/3]

template<typename T >
std::enable_if<DerefableToAO<T>::value>::type 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 182 of file Writer.h.

References writeBody().

Referenced by writeBody().

182 { writeBody(stream, *ao); }
virtual void writeBody(std::ostream &stream, const AnalysisObject *ao)
Write the body elements corresponding to AnalysisObject ao to stream.
Definition: Writer.cc:92

◆ writeCounter()

virtual void YODA::Writer::writeCounter ( std::ostream &  stream,
const Counter c 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeFoot()

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

Write any closing boilerplate required by the format to stream.

Reimplemented in YODA::WriterAIDA.

Definition at line 185 of file Writer.h.

References YODA::mkWriter(), writeCounter(), writeHisto1D(), writeHisto2D(), writeProfile1D(), writeProfile2D(), writeScatter1D(), writeScatter2D(), and writeScatter3D().

185 { stream << std::flush; }

◆ writeHead()

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

Write any opening boilerplate required by the format to stream.

Reimplemented in YODA::WriterAIDA.

Definition at line 170 of file Writer.h.

References writeBody().

170 {}

◆ writeHisto1D()

virtual void YODA::Writer::writeHisto1D ( std::ostream &  os,
const Histo1D h 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeHisto2D()

virtual void YODA::Writer::writeHisto2D ( std::ostream &  os,
const Histo2D h 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeProfile1D()

virtual void YODA::Writer::writeProfile1D ( std::ostream &  os,
const Profile1D p 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeProfile2D()

virtual void YODA::Writer::writeProfile2D ( std::ostream &  os,
const Profile2D p 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeScatter1D()

virtual void YODA::Writer::writeScatter1D ( std::ostream &  os,
const Scatter1D s 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeScatter2D()

virtual void YODA::Writer::writeScatter2D ( std::ostream &  os,
const Scatter2D s 
)
protectedpure virtual

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

Referenced by writeFoot().

◆ writeScatter3D()

virtual void YODA::Writer::writeScatter3D ( std::ostream &  os,
const Scatter3D s 
)
protectedpure virtual

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

Referenced by writeFoot().


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