yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
Reader.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2023 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_READER_H
7#define YODA_READER_H
8
10#include "YODA/Utils/Traits.h"
11#include <memory>
12#include <fstream>
13#include <iostream>
14#include <sstream>
15#include <type_traits>
16#include <unordered_map>
17#include <vector>
18
19namespace YODA {
20
21
23 class Reader {
24
25 public:
26
28 using TypeRegisterItr = typename std::unordered_map<std::string, std::unique_ptr<AOReaderBase>>::const_iterator;
29
31 virtual ~Reader() {
32 // This is technically leaking memory, but since this
33 // is a (static) singleton, there should be no issue.
34 // Cython relies on it for data-structure alignment, too.
35 for (auto& aor : _register) { aor.second.release(); }
36 }
37
38
41
48 template<typename CONT>
49 typename std::enable_if_t<YODA::Pushable<CONT,AnalysisObject*>::value>
50 read(std::istream& stream, CONT& aos, const std::string& match = "", const std::string& unmatch = "") {
51 // if CONT==std::vector<AnalysisObject*>, the compiler should select
52 // the virtual method below, since it prefers non-templated methods in the lookup
53 // otherwise we would enter a endless recursion. Check in case of problems.
54 std::vector<AnalysisObject*> v_aos;
55 read(stream, v_aos, match, unmatch);
56 for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
57 }
58
64 virtual void read(std::istream& stream, std::vector<AnalysisObject*>& aos,
65 const std::string& match = "", const std::string& unmatch = "") = 0;
66
71 std::vector<AnalysisObject*> read(std::istream& stream, const std::string& match = "",
72 const std::string& unmatch = "") {
73 std::vector<AnalysisObject*> rtn;
74 read(stream, rtn, match, unmatch);
75 return rtn;
76 }
77
78
86 template<typename CONT>
87 typename std::enable_if_t<YODA::Pushable<CONT,AnalysisObject*>::value>
88 read(const std::string& filename, CONT& aos, const std::string& match = "", const std::string& unmatch = "") {
89 // if CONT==std::vector<AnalysisObject*>, the compiler should select
90 // the virtual method below, since it prefers non-templated methods in the lookup
91 // otherwise we would enter a endless recursion. Check in case of problems.
92 std::vector<AnalysisObject*> v_aos;
93 read(filename, v_aos, match, unmatch);
94 for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
95 }
96
102 void read(const std::string& filename, std::vector<AnalysisObject*>& aos, const std::string& match = "",
103 const std::string& unmatch = "") {
104 if (filename != "-") {
105 try {
106 std::ifstream instream;
107 instream.open(filename.c_str());
108 if (instream.fail())
109 throw ReadError("Reading from filename " + filename + " failed");
110 read(instream, aos, match, unmatch);
111 instream.close();
112 } catch (std::ifstream::failure& e) {
113 throw ReadError("Reading from filename " + filename + " failed: " + e.what());
114 }
115 } else {
116 try {
117 read(std::cin, aos, match, unmatch);
118 } catch (std::runtime_error& e) {
119 throw ReadError("Reading from stdin failed: " + std::string(e.what()));
120 }
121 }
122 }
123
128 std::vector<AnalysisObject*> read(const std::string& filename, const std::string& match = "",
129 const std::string& unmatch = "") {
130 std::vector<AnalysisObject*> rtn;
131 read(filename, rtn, match, unmatch);
132 return rtn;
133 }
134
136
139
141 template<typename T>
143 const string key = Utils::toUpper(T().type());
144 const TypeRegisterItr& res = _register.find(key);
145 if (res == _register.end()) _register[key] = std::make_unique<AOReader<T>>();
146 }
147
150 bool patternCheck(const std::string& path, const std::vector<std::regex>& patterns,
151 const std::vector<std::regex>& unpatterns) {
152 bool skip = false;
153 if (patterns.size()) {
154 skip = true;
155 for (const std::regex& re : patterns) {
156 if (std::regex_search(path, re)) { skip = false; break; }
157 }
158 }
159 if (!skip && unpatterns.size()) {
160 for (const std::regex& re : unpatterns) {
161 if (std::regex_search(path, re)) { skip = true; break; }
162 }
163 }
164 return !skip;
165 }
166
168
170 std::unordered_map<string, std::unique_ptr<AOReaderBase>> _register;
171
172 };
173
174
176 Reader& mkReader(const std::string& format_name);
177
178
179}
180
181#endif
AnalysisObject is the base class for histograms and scatters.
Error for file reading errors.
Definition Exceptions.h:86
Pure virtual base class for various output writers.
Definition Reader.h:23
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.
Definition Reader.h:88
void registerType()
AO type registration.
Definition Reader.h:142
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 ~Reader()
Virtual destructor.
Definition Reader.h:31
typename std::unordered_map< std::string, std::unique_ptr< AOReaderBase > >::const_iterator TypeRegisterItr
Convenience alias for AO Reader.
Definition Reader.h:28
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.
Definition Reader.h:128
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:50
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.
Definition Reader.h:102
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 pa...
Definition Reader.h:150
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.
Definition Reader.h:71
Anonymous namespace to limit visibility.
Reader & mkReader(const std::string &format_name)
Factory function to make a reader object by format name or a filename.
Definition Reader.cc:15