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
39void RMetaData::Add(const std::string &key, int val)
40{
41 fJson->payload[key] = val;
42}
43
44void RMetaData::Add(const std::string &key, double val)
45{
46 fJson->payload[key] = val;
47}
48
49void RMetaData::Add(const std::string &key, const std::string &val)
50{
51 fJson->payload[key] = val;
52}
53
54std::string RMetaData::Dump(const std::string &key) const
55{
56 return fJson->payload[key].dump();
57}
58
59int RMetaData::GetI(const std::string &key) const
60{
61 if (!fJson->payload.contains(key))
62 throw std::logic_error("No key with name " + key + " in the metadata object.");
63 if (!fJson->payload[key].is_number_integer())
64 throw std::logic_error("Key " + key + " is not of type int.");
65 return fJson->payload[key].get<int>();
66}
67
68double RMetaData::GetD(const std::string &key) const
69{
70 if (!fJson->payload.contains(key))
71 throw std::logic_error("No key with name " + key + " in the metadata object.");
72 if (!fJson->payload[key].is_number_float())
73 throw std::logic_error("Key " + key + " is not of type double.");
74 return fJson->payload[key].get<double>();
75}
76
77std::string RMetaData::GetS(const std::string &key) const
78{
79 if (!fJson->payload.contains(key))
80 throw std::logic_error("No key with name " + key + " in the metadata object.");
81 if (!fJson->payload[key].is_string())
82 throw std::logic_error("Key " + key + " is not of type string.");
83 return fJson->payload[key].get<std::string>();
84}
85
86int RMetaData::GetI(const std::string &key, int defaultVal) const
87{
88 if (!fJson->payload.contains(key))
89 return defaultVal;
90 if (!fJson->payload[key].is_number_integer())
91 throw std::logic_error("Key " + key + " is not of type int.");
92 return fJson->payload[key].get<int>();
93}
94
95double RMetaData::GetD(const std::string &key, double defaultVal) const
96{
97 if (!fJson->payload.contains(key))
98 return defaultVal;
99 if (!fJson->payload[key].is_number_float())
100 throw std::logic_error("Key " + key + " is not of type double.");
101 return fJson->payload[key].get<double>();
102}
103
104const std::string RMetaData::GetS(const std::string &key, const std::string &defaultVal) const
105{
106 if (!fJson->payload.contains(key))
107 return defaultVal;
108 if (!fJson->payload[key].is_string())
109 throw std::logic_error("Key " + key + " is not of type string.");
110 return fJson->payload[key].get<std::string>();
111}
112
113} // namespace Experimental
114} // namespace RDF
115} // namespace ROOT
Class behaving as a heterogenuous dictionary to store dataset metadata.
Definition RMetaData.hxx:40
std::string Dump(const std::string &key) const
Definition RMetaData.cxx:54
std::unique_ptr< Internal::RDF::RMetaDataJson > fJson
Definition RMetaData.hxx:65
std::string GetS(const std::string &key) const
Definition RMetaData.cxx:77
void Add(const std::string &key, int val)
Definition RMetaData.cxx:39
double GetD(const std::string &key) const
Definition RMetaData.cxx:68
int GetI(const std::string &key) const
Definition RMetaData.cxx:59
RMetaData & operator=(RMetaData const &)
Definition RMetaData.cxx:29
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.