Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RMetaData.cxx
Go to the documentation of this file.
1// Author: Ivan Kabadzhov CERN 10/2022
2
3/*************************************************************************
4 * Copyright (C) 1995-2022, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
12#include <nlohmann/json.hpp>
13#include <stdexcept> // std::logic_error
14
16 nlohmann::json payload;
17};
18
19namespace ROOT {
20namespace RDF {
21namespace Experimental {
22
23RMetaData::RMetaData() : fJson{std::make_unique<Internal::RDF::RMetaDataJson>()} {}
24
25RMetaData::RMetaData(RMetaData const &other) : fJson{std::make_unique<Internal::RDF::RMetaDataJson>(*other.fJson)} {}
26
28
30{
31 fJson = std::make_unique<Internal::RDF::RMetaDataJson>(*other.fJson);
32 return *this;
33}
34
36
37RMetaData::~RMetaData() = default;
38
39/// @brief Add an RMetaData class instance.
40/// @param[in] key input key for a given RMetaData instance.
41/// @param[in] val value for a given RMetaData instance of type int.
42void RMetaData::Add(const std::string &key, int val)
43{
44 fJson->payload[key] = val;
45}
46
47/// @brief Add an RMetaData class instance.
48/// @param[in] key input key for a given RMetaData instance.
49/// @param[in] val metadata value for a given RMetaData instance of type double.
50void RMetaData::Add(const std::string &key, double val)
51{
52 fJson->payload[key] = val;
53}
54
55/// @brief Add an RMetaData class instance
56/// @param[in] key input key for a given RMetaData instance.
57/// @param[in] val metadata value for of type string.
58void RMetaData::Add(const std::string &key, const std::string &val)
59{
60 fJson->payload[key] = val;
61}
62
63/// @brief Dump the value of the metadata value given the key.
64/// @param[in] key input key for a given RMetaData instance.
65/// @return metadata value (as a string) associated with the input key, irrelevant of the actual type of the metadata
66/// value.
67std::string RMetaData::Dump(const std::string &key) const
68{
69 return fJson->payload[key].dump();
70}
71/// @brief Return the metadata value of type int given the key, or an error if the metadata value is of a non-int type.
72/// @param[in] key input key for a given RMetaData instance.
73int RMetaData::GetI(const std::string &key) const
74{
75 if (!fJson->payload.contains(key))
76 throw std::logic_error("No key with name " + key + " in the metadata object.");
77 if (!fJson->payload[key].is_number_integer())
78 throw std::logic_error("Metadata value found at key '" + key + "' is not of type int.");
79 return fJson->payload[key].get<int>();
80}
81/// @brief Return the metadata value of type double given the key, or an error if the metadata value is of a non-double
82/// type.
83/// @param[in] key input key for a given RMetaData instance.
84double RMetaData::GetD(const std::string &key) const
85{
86 if (!fJson->payload.contains(key))
87 throw std::logic_error("No key with name " + key + " in the metadata object.");
88 if (!fJson->payload[key].is_number_float())
89 throw std::logic_error("Metadata value found at key '" + key + "' is not of type double.");
90 return fJson->payload[key].get<double>();
91}
92
93/// @brief Return the metadata value of type string given the key, or an error if the metadata value is of a non-string
94/// type.
95/// @param[in] key input key for a given RMetaData instance.
96std::string RMetaData::GetS(const std::string &key) const
97{
98 if (!fJson->payload.contains(key))
99 throw std::logic_error("No key with name " + key + " in the metadata object.");
100 if (!fJson->payload[key].is_string())
101 throw std::logic_error("Metadata value found at key '" + key + "' is not of type string.");
102 return fJson->payload[key].get<std::string>();
103}
104/// @brief Return the metadata value of type int given the key, a default int metadata value if the key is not found, or
105/// an error if the metadata value is of a non-int type.
106/// @param[in] key input key for a given RMetaData instance.
107/// @param[in] defaultVal metadata value of type int which is read as default while a given key cannot be found in the
108/// dataset.
109int RMetaData::GetI(const std::string &key, int defaultVal) const
110{
111 if (!fJson->payload.contains(key))
112 return defaultVal;
113 if (!fJson->payload[key].is_number_integer())
114 throw std::logic_error("Metadata value found at key '" + key + "' is not of type int.");
115 return fJson->payload[key].get<int>();
116}
117/// @brief Return the metadata value of type int given the key, a default int metadata value if the key is not found, or
118/// an error if the metadata value is of a non-double type.
119/// @param[in] key input key for a given RMetaData instance.
120/// @param[in] defaultVal metadata value of type double which is read as default while a given key cannot be found in
121/// the dataset.
122double RMetaData::GetD(const std::string &key, double defaultVal) const
123{
124 if (!fJson->payload.contains(key))
125 return defaultVal;
126 if (!fJson->payload[key].is_number_float())
127 throw std::logic_error("Metadata value found at key '" + key + "' is not of type double.");
128 return fJson->payload[key].get<double>();
129}
130
131/// @brief Return the metadata value of type int given the key, a default int metadata value if the key is not found, or
132/// an error if the metadata value is of a non-string type.
133/// @param[in] key input key for a given RMetaData instance.
134/// @param[in] defaultVal metadata value of type string which is read as default while a given key cannot be found in
135/// the dataset.
136const std::string RMetaData::GetS(const std::string &key, const std::string &defaultVal) const
137{
138 if (!fJson->payload.contains(key))
139 return defaultVal;
140 if (!fJson->payload[key].is_string())
141 throw std::logic_error("Metadata value found at key '" + key + "' is not of type string.");
142 return fJson->payload[key].get<std::string>();
143}
144
145} // namespace Experimental
146} // namespace RDF
147} // namespace ROOT
Class behaving as a heterogenuous dictionary to store the metadata of a dataset.
Definition RMetaData.hxx:50
std::string Dump(const std::string &key) const
Dump the value of the metadata value given the key.
Definition RMetaData.cxx:67
std::unique_ptr< Internal::RDF::RMetaDataJson > fJson
Definition RMetaData.hxx:75
std::string GetS(const std::string &key) const
Return the metadata value of type string given the key, or an error if the metadata value is of a non...
Definition RMetaData.cxx:96
void Add(const std::string &key, int val)
Add an RMetaData class instance.
Definition RMetaData.cxx:42
double GetD(const std::string &key) const
Return the metadata value of type double given the key, or an error if the metadata value is of a non...
Definition RMetaData.cxx:84
int GetI(const std::string &key) const
Return the metadata value of type int given the key, or an error if the metadata value is of a non-in...
Definition RMetaData.cxx:73
RMetaData & operator=(RMetaData const &)
Definition RMetaData.cxx:29
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...