Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONInterface.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFit_Detail_JSONInterface_h
14#define RooFit_Detail_JSONInterface_h
15
16#include <iostream>
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22namespace RooFit {
23namespace Detail {
24
25class JSONNode {
26public:
27 template <class Nd>
29 public:
30 class Impl {
31 public:
32 virtual ~Impl() = default;
33 virtual std::unique_ptr<Impl> clone() const = 0;
34 virtual void forward() = 0;
35 virtual void backward() = 0;
36 virtual Nd &current() = 0;
37 virtual bool equal(const Impl &other) const = 0;
38 };
39
40 child_iterator_t(std::unique_ptr<Impl> impl) : it(std::move(impl)) {}
41 child_iterator_t(const child_iterator_t &other) : it(std::move(other.it->clone())) {}
42
44 {
45 it->forward();
46 return *this;
47 }
49 {
50 it->backward();
51 return *this;
52 }
53 Nd &operator*() const { return it->current(); }
54 Nd &operator->() const { return it->current(); }
55
56 friend bool operator!=(child_iterator_t const &lhs, child_iterator_t const &rhs)
57 {
58 return !lhs.it->equal(*rhs.it);
59 }
60 friend bool operator==(child_iterator_t const &lhs, child_iterator_t const &rhs)
61 {
62 return lhs.it->equal(*rhs.it);
63 }
64
65 private:
66 std::unique_ptr<Impl> it;
67 };
68
71
72 template <class Nd>
75
76 public:
77 inline children_view_t(child_iterator_t<Nd> const &b_, child_iterator_t<Nd> const &e_) : b(b_), e(e_) {}
78
79 inline child_iterator_t<Nd> begin() const { return b; }
80 inline child_iterator_t<Nd> end() const { return e; }
81 };
82
83public:
84 virtual void writeJSON(std::ostream &os) const = 0;
85 virtual void writeYML(std::ostream &) const { throw std::runtime_error("YML not supported"); }
86
87public:
88 virtual JSONNode &operator<<(std::string const &s) = 0;
89 inline JSONNode &operator<<(const char *s) { return *this << std::string(s); }
90 virtual JSONNode &operator<<(int i) = 0;
91 virtual JSONNode &operator<<(double d) = 0;
92 virtual JSONNode &operator<<(bool b) = 0;
93 virtual const JSONNode &operator>>(std::string &v) const = 0;
94 virtual JSONNode &operator[](std::string const &k) = 0;
95 virtual const JSONNode &operator[](std::string const &k) const = 0;
96 virtual bool is_container() const = 0;
97 virtual bool is_map() const = 0;
98 virtual bool is_seq() const = 0;
99 virtual JSONNode &set_map() = 0;
100 virtual JSONNode &set_seq() = 0;
101 virtual void clear() = 0;
102
103 virtual std::string key() const = 0;
104 virtual std::string val() const = 0;
105 virtual int val_int() const { return atoi(this->val().c_str()); }
106 virtual double val_double() const { return std::stod(this->val()); }
107 virtual bool val_bool() const { return atoi(this->val().c_str()); }
108 template <class T>
109 T val_t() const;
110 virtual bool has_key() const = 0;
111 virtual bool has_val() const = 0;
112 virtual bool has_child(std::string const &) const = 0;
113 virtual JSONNode &append_child() = 0;
114 virtual size_t num_children() const = 0;
115
118
119 virtual children_view children();
120 virtual const_children_view children() const;
121 virtual JSONNode &child(size_t pos) = 0;
122 virtual const JSONNode &child(size_t pos) const = 0;
123
124 template <typename Collection>
125 void fill_seq(Collection const &coll)
126 {
127 set_seq();
128 for (auto const &item : coll) {
129 append_child() << item;
130 }
131 }
132
133 template <typename Collection>
134 void fill_seq(Collection const &coll, size_t nmax)
135 {
136 set_seq();
137 size_t n = 0;
138 for (auto const &item : coll) {
139 if (n >= nmax)
140 break;
141 append_child() << item;
142 ++n;
143 }
144 }
145
146 template <typename Collection, typename TransformationFunc>
147 void fill_seq(Collection const &coll, TransformationFunc func)
148 {
149 set_seq();
150 for (auto const &item : coll) {
151 append_child() << func(item);
152 }
153 }
154
155 template <typename Matrix>
156 void fill_mat(Matrix const &mat)
157 {
158 set_seq();
159 for (int i = 0; i < mat.GetNrows(); ++i) {
160 auto &row = append_child();
161 row.set_seq();
162 for (int j = 0; j < mat.GetNcols(); ++j) {
163 row.append_child() << mat(i, j);
164 }
165 }
166 }
167
168 JSONNode const *find(std::string const &key) const
169 {
170 auto &n = *this;
171 return n.has_child(key) ? &n[key] : nullptr;
172 }
173
174 template <typename... Keys_t>
175 JSONNode const *find(std::string const &key, Keys_t const &...keys) const
176 {
177 auto &n = *this;
178 return n.has_child(key) ? n[key].find(keys...) : nullptr;
179 }
180
181 JSONNode &get(std::string const &key)
182 {
183 auto &n = *this;
184 n.set_map();
185 return n[key];
186 }
187
188 template <typename... Keys_t>
189 JSONNode &get(std::string const &key, Keys_t const &...keys)
190 {
191 auto &next = get(key);
192 next.set_map();
193 return next.get(keys...);
194 }
195};
196
197class JSONTree {
198public:
199 virtual ~JSONTree() = default;
200
201 virtual JSONNode &rootnode() = 0;
202
203 static std::unique_ptr<JSONTree> create();
204 static std::unique_ptr<JSONTree> create(std::istream &is);
205 static std::unique_ptr<JSONTree> create(std::string const &str);
206
207 static std::string getBackend();
208 static void setBackend(std::string const &name);
209
210 static bool hasBackend(std::string const &name);
211
212private:
213 // Internally, we store the backend type with an enum to be more memory efficient.
214 enum class Backend { NlohmannJson, Ryml };
215
216 static Backend &getBackendEnum();
217
218 template <typename... Args>
219 static std::unique_ptr<JSONTree> createImpl(Args &&...args);
220};
221
222std::ostream &operator<<(std::ostream &os, RooFit::Detail::JSONNode const &s);
223
224template <class T>
225std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode::children_view const &cv)
226{
227 for (const auto &e : cv) {
228 v.push_back(e.val_t<T>());
229 }
230 return v;
231}
232
233template <class T>
234std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode::const_children_view const &cv)
235{
236 for (const auto &e : cv) {
237 v.push_back(e.val_t<T>());
238 }
239 return v;
240}
241
242template <class T>
243std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode const &n)
244{
245 if (!n.is_seq()) {
246 throw std::runtime_error("node " + n.key() + " is not of sequence type!");
247 }
248 v << n.children();
249 return v;
250}
251
252template <>
253inline int JSONNode::val_t<int>() const
254{
255 return val_int();
256}
257template <>
258inline double JSONNode::val_t<double>() const
259{
260 return val_double();
261}
262template <>
263inline bool JSONNode::val_t<bool>() const
264{
265 return val_bool();
266}
267template <>
268inline std::string JSONNode::val_t<std::string>() const
269{
270 return val();
271}
272
273} // namespace Detail
274} // namespace RooFit
275
276#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define e(i)
Definition RSha256.hxx:103
TObject * clone(const char *newname) const override
Definition RooChi2Var.h:9
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:397
char name[80]
Definition TGX11.cxx:110
virtual std::unique_ptr< Impl > clone() const =0
virtual bool equal(const Impl &other) const =0
child_iterator_t(std::unique_ptr< Impl > impl)
child_iterator_t(const child_iterator_t &other)
friend bool operator==(child_iterator_t const &lhs, child_iterator_t const &rhs)
friend bool operator!=(child_iterator_t const &lhs, child_iterator_t const &rhs)
child_iterator_t< Nd > begin() const
children_view_t(child_iterator_t< Nd > const &b_, child_iterator_t< Nd > const &e_)
child_iterator_t< Nd > end() const
virtual JSONNode & operator<<(std::string const &s)=0
virtual bool val_bool() const
JSONNode & get(std::string const &key)
virtual std::string val() const =0
virtual const JSONNode & operator>>(std::string &v) const =0
void fill_seq(Collection const &coll, size_t nmax)
JSONNode & get(std::string const &key, Keys_t const &...keys)
void fill_seq(Collection const &coll)
virtual JSONNode & set_map()=0
virtual JSONNode & append_child()=0
JSONNode const * find(std::string const &key, Keys_t const &...keys) const
virtual JSONNode & operator<<(double d)=0
virtual void clear()=0
virtual children_view children()
virtual size_t num_children() const =0
virtual JSONNode & child(size_t pos)=0
virtual JSONNode & set_seq()=0
virtual bool is_container() const =0
virtual void writeJSON(std::ostream &os) const =0
virtual bool is_seq() const =0
virtual const JSONNode & operator[](std::string const &k) const =0
virtual JSONNode & operator<<(bool b)=0
virtual void writeYML(std::ostream &) const
virtual bool is_map() const =0
virtual bool has_child(std::string const &) const =0
virtual std::string key() const =0
JSONNode & operator<<(const char *s)
void fill_seq(Collection const &coll, TransformationFunc func)
virtual JSONNode & operator[](std::string const &k)=0
void fill_mat(Matrix const &mat)
virtual bool has_key() const =0
virtual const JSONNode & child(size_t pos) const =0
virtual double val_double() const
JSONNode const * find(std::string const &key) const
virtual bool has_val() const =0
virtual int val_int() const
virtual JSONNode & operator<<(int i)=0
static void setBackend(std::string const &name)
Set the library that serves as the backend for the JSON interface.
static Backend & getBackendEnum()
static std::unique_ptr< JSONTree > create()
static bool hasBackend(std::string const &name)
Check if ROOT was compiled with support for a certain JSON backend library.
static std::string getBackend()
Returns the name of the library that serves as the backend for the JSON interface,...
static std::unique_ptr< JSONTree > createImpl(Args &&...args)
virtual ~JSONTree()=default
virtual JSONNode & rootnode()=0
Int_t GetNrows() const
Int_t GetNcols() const
const Int_t n
Definition legend1.C:16
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26