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

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

#include <Reader.h>

Inheritance diagram for YODA::Reader:
YODA::ReaderFLAT YODA::ReaderH5 YODA::ReaderYODA

Public Types

using TypeRegisterItr = typename std::unordered_map< std::string, std::unique_ptr< AOReaderBase > >::const_iterator
 Convenience alias for AO Reader.
 

Public Member Functions

virtual ~Reader ()
 Virtual destructor.
 
Reading multiple analysis objects,
template<typename CONT >
std::enable_if_t< YODA::Pushable< CONT, AnalysisObject * >::value > read (std::istream &stream, CONT &aos, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects objs from output stream stream.
 
virtual void read (std::istream &stream, std::vector< AnalysisObject * > &aos, const std::string &match="", const std::string &unmatch="")=0
 Read in a collection of objects objs from output stream stream.
 
virtual void read (const YODA_H5::File &file, std::vector< AnalysisObject * > &aos, const std::string &match="", const std::string &unmatch="")=0
 Read in a collection of objects objs from an HDF5 file.
 
std::vector< AnalysisObject * > read (std::istream &stream, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects from output stream stream.
 
std::vector< AnalysisObject * > read (const YODA_H5::File &file, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects from an H5 file.
 
template<typename CONT >
std::enable_if_t< YODA::Pushable< CONT, AnalysisObject * >::value > read (const std::string &filename, CONT &aos, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects objs from file filename.
 
void read (const std::string &filename, std::vector< AnalysisObject * > &aos, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects objs from file filename.
 
std::vector< AnalysisObject * > read (const std::string &filename, const std::string &match="", const std::string &unmatch="")
 Read in a collection of objects from output stream stream.
 
Utilities
template<typename T >
void registerType ()
 AO type registration.
 
bool patternCheck (const std::string &path, const std::vector< std::regex > &patterns, const std::vector< std::regex > &unpatterns)
 Check if a string matches any of the given patterns, and that it doesn't match any unpatterns (for path filtering)
 

Detailed Description

Pure virtual base class for various output writers.

Definition at line 35 of file Reader.h.

Member Typedef Documentation

◆ TypeRegisterItr

using YODA::Reader::TypeRegisterItr = typename std::unordered_map<std::string, std::unique_ptr<AOReaderBase> >::const_iterator

Convenience alias for AO Reader.

Definition at line 40 of file Reader.h.

Constructor & Destructor Documentation

◆ ~Reader()

virtual YODA::Reader::~Reader ( )
inlinevirtual

Virtual destructor.

Definition at line 43 of file Reader.h.

43 {
44 // This is technically leaking memory, but since this
45 // is a (static) singleton, there should be no issue.
46 // Cython relies on it for data-structure alignment, too.
47 for (auto& aor : _register) { aor.second.release(); }
48 }

Member Function Documentation

◆ patternCheck()

bool YODA::Reader::patternCheck ( const std::string &  path,
const std::vector< std::regex > &  patterns,
const std::vector< std::regex > &  unpatterns 
)
inline

Check if a string matches any of the given patterns, and that it doesn't match any unpatterns (for path filtering)

Definition at line 200 of file Reader.h.

201 {
202 bool skip = false;
203 if (patterns.size()) {
204 skip = true;
205 for (const std::regex& re : patterns) {
206 if (std::regex_search(path, re)) { skip = false; break; }
207 }
208 }
209 if (!skip && unpatterns.size()) {
210 for (const std::regex& re : unpatterns) {
211 if (std::regex_search(path, re)) { skip = true; break; }
212 }
213 }
214 return !skip;
215 }

Referenced by YODA::ReaderH5::read().

◆ read() [1/8]

std::vector< AnalysisObject * > YODA::Reader::read ( const std::string &  filename,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects from output stream stream.

This version returns a vector by value, involving copying, and is hence less CPU efficient than the alternative version where a vector is filled by reference.

Definition at line 178 of file Reader.h.

179 {
180 std::vector<AnalysisObject*> rtn;
181 read(filename, rtn, match, unmatch);
182 return rtn;
183 }
std::enable_if_t< YODA::Pushable< CONT, AnalysisObject * >::value > read(std::istream &stream, CONT &aos, const std::string &match="", const std::string &unmatch="")
Read in a collection of objects objs from output stream stream.
Definition Reader.h:62

References read().

◆ read() [2/8]

template<typename CONT >
std::enable_if_t< YODA::Pushable< CONT, AnalysisObject * >::value > YODA::Reader::read ( const std::string &  filename,
CONT &  aos,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects objs from file filename.

This version fills (actually, appends to) a variable supplied container Note: SFINAE is used to check for a void push_back(const AnalysisObject*) method

Todo:
Extend SFINAE Pushable cf. Writer to allow adding to containers of smart ptr type

Definition at line 124 of file Reader.h.

124 {
125 // if CONT==std::vector<AnalysisObject*>, the compiler should select
126 // the virtual method below, since it prefers non-templated methods in the lookup
127 // otherwise we would enter a endless recursion. Check in case of problems.
128 std::vector<AnalysisObject*> v_aos;
129 read(filename, v_aos, match, unmatch);
130 for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
131 }

References read().

◆ read() [3/8]

void YODA::Reader::read ( const std::string &  filename,
std::vector< AnalysisObject * > &  aos,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects objs from file filename.

This version fills (actually, appends to) a supplied vector, avoiding copying, and is hence CPU efficient.

Definition at line 138 of file Reader.h.

139 {
140 if (filename != "-") {
141 try {
142 const size_t lastdot = filename.find_last_of(".");
143 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
144 #ifdef HAVE_HDF5
145 // check if the requested format is H5
146 if (Utils::startswith(fmt, "h5")) {
147 try {
148 const YODA_H5::File h5(filename, YODA_H5::File::ReadOnly);
149 read(h5, aos, match, unmatch);
150 } catch (YODA::ReadError& e) {
151 throw ReadError("Reading from filename " + filename + " failed: " + e.what());
152 }
153 return;
154 }
155 #endif
156 std::ifstream instream;
157 instream.open(filename.c_str());
158 if (instream.fail())
159 throw ReadError("Reading from filename " + filename + " failed");
160 read(instream, aos, match, unmatch);
161 instream.close();
162 } catch (std::ifstream::failure& e) {
163 throw ReadError("Reading from filename " + filename + " failed: " + e.what());
164 }
165 } else {
166 try {
167 read(std::cin, aos, match, unmatch);
168 } catch (std::runtime_error& e) {
169 throw ReadError("Reading from stdin failed: " + std::string(e.what()));
170 }
171 }
172 }
Error for file reading errors.
Definition Exceptions.h:72

References read().

◆ read() [4/8]

std::vector< AnalysisObject * > YODA::Reader::read ( const YODA_H5::File &  file,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects from an H5 file.

This version returns a vector by value, involving copying, and is hence less CPU efficient than the alternative version where a vector is filled by reference.

Definition at line 106 of file Reader.h.

107 {
108 std::vector<AnalysisObject*> rtn;
109 read(file, rtn, match, unmatch);
110 return rtn;
111 }

References read().

◆ read() [5/8]

virtual void YODA::Reader::read ( const YODA_H5::File &  file,
std::vector< AnalysisObject * > &  aos,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
pure virtual

Read in a collection of objects objs from an HDF5 file.

This version fills (actually, appends to) a supplied vector, avoiding copying, and is hence CPU efficient.

Implemented in YODA::ReaderH5.

◆ read() [6/8]

std::vector< AnalysisObject * > YODA::Reader::read ( std::istream &  stream,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects from output stream stream.

This version returns a vector by value, involving copying, and is hence less CPU efficient than the alternative version where a vector is filled by reference.

Definition at line 93 of file Reader.h.

94 {
95 std::vector<AnalysisObject*> rtn;
96 read(stream, rtn, match, unmatch);
97 return rtn;
98 }

References read().

◆ read() [7/8]

template<typename CONT >
std::enable_if_t< YODA::Pushable< CONT, AnalysisObject * >::value > YODA::Reader::read ( std::istream &  stream,
CONT &  aos,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
inline

Read in a collection of objects objs from output stream stream.

This version fills (actually, appends to) a variable supplied container Note: SFINAE is used to check for a void push_back(const AnalysisObject*) method

Todo:
Extend SFINAE Pushable cf. Writer to allow adding to containers of smart ptr type

Definition at line 62 of file Reader.h.

62 {
63 // if CONT==std::vector<AnalysisObject*>, the compiler should select
64 // the virtual method below, since it prefers non-templated methods in the lookup
65 // otherwise we would enter a endless recursion. Check in case of problems.
66 std::vector<AnalysisObject*> v_aos;
67 read(stream, v_aos, match, unmatch);
68 for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
69 }

References read().

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

◆ read() [8/8]

virtual void YODA::Reader::read ( std::istream &  stream,
std::vector< AnalysisObject * > &  aos,
const std::string &  match = "",
const std::string &  unmatch = "" 
)
pure virtual

Read in a collection of objects objs from output stream stream.

This version fills (actually, appends to) a supplied vector, avoiding copying, and is hence CPU efficient.

Implemented in YODA::ReaderFLAT, and YODA::ReaderYODA.

◆ registerType()

template<typename T >
void YODA::Reader::registerType ( )
inline

AO type registration.

Definition at line 192 of file Reader.h.

192 {
193 const string key = Utils::toUpper(T().type());
194 const TypeRegisterItr& res = _register.find(key);
195 if (res == _register.end()) _register[key] = std::make_unique<AOReader<T>>();
196 }
typename std::unordered_map< std::string, std::unique_ptr< AOReaderBase > >::const_iterator TypeRegisterItr
Convenience alias for AO Reader.
Definition Reader.h:40

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