yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.1.0
ReaderH5.cc
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#include "YODA/ReaderH5.h"
7#include "YODA/Exceptions.h"
9
10#include <map>
11#include <string>
12
13using namespace std;
14
15namespace YODA {
16
17
18 namespace {
19
20 template <typename... Args, class F>
21 constexpr void for_each_arg(F&& f) {
22 (( f(Args{}) ), ...);
23 }
24 }
25
26
29 static ReaderH5 _instance;
30 _instance.registerDefaultTypes<double,int,string>();
31 return _instance;
32 }
33
34 template<typename ... Args>
35 void ReaderH5::registerDefaultTypes() {
36 registerType<Counter>();
37 registerType<Estimate0D>();
38 registerType<Scatter1D>();
39 registerType<Scatter2D>();
40 registerType<Scatter3D>();
41
42 // add BinnedHisto/BinnedProfile in 1D
43 for_each_arg<Args...>([&](auto&& arg) {
44 using A1 = decay_t<decltype(arg)>;
45 using BH = BinnedHisto<A1>;
46 registerType<BH>();
47 using BP = BinnedProfile<A1>;
48 registerType<BP>();
49 using BE = BinnedEstimate<A1>;
50 registerType<BE>();
51
52 // add BinnedHisto/BinnedProfile in 2D
53 for_each_arg<Args...>([&](auto&& arg) {
54 using A2 = decay_t<decltype(arg)>;
55 using BH = BinnedHisto<A1,A2>;
56 registerType<BH>();
57 using BP = BinnedProfile<A1,A2>;
58 registerType<BP>();
59 using BE = BinnedEstimate<A1,A2>;
60 registerType<BE>();
61
62 // add BinnedHisto/BinnedProfile in 3D
63 for_each_arg<Args...>([&](auto&& arg) {
64 using A3 = decay_t<decltype(arg)>;
65 //using BH = BinnedHisto<A1,A2,A3>;
66 //registerType<BH>();
67 //using BP = BinnedProfile<A1,A2,A3>;
68 //registerType<BP>();
69 using BE = BinnedEstimate<A1,A2,A3>;
70 registerType<BE>();
71 });
72 });
73 });
74 registerType<HistoND<3>>();
75 }
76
77
78 void ReaderH5::read(const YODA_H5::File& file, vector<AnalysisObject*>& aos,
79 const string& match, const string& unmatch) {
80
81 vector<regex> patterns, unpatterns;
82 for (const string& pat : Utils::split(match, ",")) { patterns.push_back(regex(pat)); }
83 for (const string& pat : Utils::split(unmatch, ",")) { unpatterns.push_back(regex(pat)); }
84
85 // Load edge information
86 H5FileManager reader(file);
87
88 // Prepare registry
89 TypeRegisterItr thisAOR = _register.cend();
90 const TypeRegisterItr endAOR = _register.cend();
91
92 while (reader.next()) {
93
94 // Check that type has been loaded
95 thisAOR = _register.find(reader.type());
96 if (thisAOR == endAOR) {
97 throw ReadError("Unexpected context found: " + reader.type());
98 }
99
100 // Check if path is being (un-)matched
101 //
102 // Since the edge types are not known until run time,
103 // (empty) objects still need to be instantiated
104 // from the type registry in order to be able to
105 // choose the correct set of edges to skip.
106 const bool pattern_pass = patternCheck(reader.path(), patterns, unpatterns);
107 if (!pattern_pass) {
108 thisAOR->second->skip(reader);
109 continue;
110 }
111
112 // Construct AO
113 aos.push_back(thisAOR->second->mkFromH5(reader));
114 }
115 }
116
117}
User-facing BinnedDbn class in arbitrary dimension.
Definition BinnedDbn.h:55
Forward declaration.
Helper class to extract AO information from a H5 file.
Definition H5Utils.h:272
const string & path() const
Path of current AO.
Definition H5Utils.h:323
bool next()
Loads next AO from file.
Definition H5Utils.h:308
const string & type() const
Type of current AO.
Definition H5Utils.h:328
Error for file reading errors.
Definition Exceptions.h:72
Persistency reader from YODA text data format.
Definition ReaderH5.h:15
static Reader & create()
Singleton creation function.
Definition ReaderH5.cc:28
void read(const YODA_H5::File &file, std::vector< AnalysisObject * > &aos, const std::string &match="", const std::string &unmatch="")
Read in a collection of objects objs from an HDF5 file.
Definition ReaderH5.cc:78
Pure virtual base class for various output writers.
Definition Reader.h:35
typename std::unordered_map< std::string, std::unique_ptr< AOReaderBase > >::const_iterator TypeRegisterItr
Convenience alias for AO Reader.
Definition Reader.h:40
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:200
Anonymous namespace to limit visibility.