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 <map>
18#include <unordered_map>
19#include <memory>
20#include <optional>
21#include <stdexcept>
22#include <string>
23#include <vector>
24
25namespace RooFit {
26namespace Detail {
27
28class JSONNode {
29public:
30 template <class Nd>
32 public:
33 explicit child_iterator_t(Nd *const *pos) : _pos(pos) {}
34
36 {
37 ++_pos;
38 return *this;
39 }
41 {
42 --_pos;
43 return *this;
44 }
45 Nd &operator*() const { return **_pos; }
46 Nd &operator->() const { return **_pos; }
47
48 friend bool operator!=(child_iterator_t const &lhs, child_iterator_t const &rhs) { return lhs._pos != rhs._pos; }
49 friend bool operator==(child_iterator_t const &lhs, child_iterator_t const &rhs) { return lhs._pos == rhs._pos; }
50
51 private:
52 Nd *const *_pos;
53 };
54
57
58 template <class Nd>
61
62 public:
63 inline children_view_t(child_iterator_t<Nd> const &b_, child_iterator_t<Nd> const &e_) : b(b_), e(e_) {}
64
65 inline child_iterator_t<Nd> begin() const { return b; }
66 inline child_iterator_t<Nd> end() const { return e; }
67 };
68
69 // A node refers to a value of its tree. Copying it would copy that reference, not the value.
70 JSONNode(JSONNode const &) = delete;
71 JSONNode &operator=(JSONNode const &) = delete;
72
73public:
74 void writeJSON(std::ostream &os) const;
75
76public:
77 JSONNode &operator<<(std::string const &s);
78 inline JSONNode &operator<<(const char *s) { return *this << std::string(s); }
79 JSONNode &operator<<(int i);
80 JSONNode &operator<<(double d);
81 JSONNode &operator<<(bool b);
82 const JSONNode &operator>>(std::string &v) const;
83 JSONNode &operator[](std::string const &k);
84 const JSONNode &operator[](std::string const &k) const;
85 bool is_container() const;
86 bool is_map() const;
87 bool is_seq() const;
88 bool is_null() const;
89 bool is_number() const;
93 void clear();
94 /// Remove a child of a map, if it exists.
95 void erase(std::string const &key);
96
97 std::string key() const;
98 std::string val() const;
99 int val_int() const;
100 double val_double() const;
101 bool val_bool() const;
102 /// The value as a boolean, or std::nullopt if it is none. Like val_bool(), the integers 0 and 1 are accepted.
103 std::optional<bool> try_val_bool() const;
104 template <class T>
105 T val_t() const;
106 bool has_key() const;
107 bool has_val() const;
108 bool has_child(std::string const &) const;
110 size_t num_children() const;
111
114
117 JSONNode &child(size_t pos);
118 const JSONNode &child(size_t pos) const;
119
120 template <typename Collection>
121 void fill_seq(Collection const &coll)
122 {
123 set_seq();
124 for (auto const &item : coll) {
125 append_child() << item;
126 }
127 }
128
129 template <typename Collection>
130 void fill_seq(Collection const &coll, size_t nmax)
131 {
132 set_seq();
133 size_t n = 0;
134 for (auto const &item : coll) {
135 if (n >= nmax)
136 break;
137 append_child() << item;
138 ++n;
139 }
140 }
141
142 template <typename Collection, typename TransformationFunc>
143 void fill_seq(Collection const &coll, TransformationFunc func)
144 {
145 set_seq();
146 for (auto const &item : coll) {
147 append_child() << func(item);
148 }
149 }
150
151 template <typename Matrix>
152 void fill_mat(Matrix const &mat)
153 {
154 set_seq();
155 for (int i = 0; i < mat.GetNrows(); ++i) {
156 auto &row = append_child();
157 row.set_seq();
158 for (int j = 0; j < mat.GetNcols(); ++j) {
159 row.append_child() << mat(i, j);
160 }
161 }
162 }
163
164 JSONNode const *find(std::string const &key) const
165 {
166 auto &n = *this;
167 return n.has_child(key) ? &n[key] : nullptr;
168 }
169
170 template <typename... Keys_t>
171 JSONNode const *find(std::string const &key, Keys_t const &...keys) const
172 {
173 auto &n = *this;
174 return n.has_child(key) ? n[key].find(keys...) : nullptr;
175 }
176
177 JSONNode &get(std::string const &key)
178 {
179 auto &n = *this;
180 n.set_map();
181 return n[key];
182 }
183
184 template <typename... Keys_t>
185 JSONNode &get(std::string const &key, Keys_t const &...keys)
186 {
187 auto &next = get(key);
188 next.set_map();
189 return next.get(keys...);
190 }
191
192private:
193 friend class JSONTree;
194 friend class JSONReadRecorder;
195
196 // Owns the values and the nodes of a tree. It is defined in the only source file that knows the JSON library.
197 class Storage;
198
199 JSONNode(Storage &storage, void *value, std::string key);
200
202 void *_value; // of the value type of the JSON library
203 std::string _key;
204};
205
206class JSONTree {
207public:
209
211
212 static std::unique_ptr<JSONTree> create();
213 static std::unique_ptr<JSONTree> create(std::istream &is);
214 static std::unique_ptr<JSONTree> create(std::string const &str);
215
216private:
217 explicit JSONTree(std::unique_ptr<JSONNode::Storage> storage);
218
219 std::unique_ptr<JSONNode::Storage> _storage;
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 <class Key, class T, class Hash, class KeyEqual, class Allocator>
254operator<<(RooFit::Detail::JSONNode &n, const std::unordered_map<Key, T, Hash, KeyEqual, Allocator> &m)
255{
256 n.set_map();
257 for (const auto &it : m) {
258 n[it.first] << it.second;
259 }
260 return n;
261}
262
263template <class Key, class T, class Compare, class Allocator>
264RooFit::Detail::JSONNode &operator<<(RooFit::Detail::JSONNode &n, const std::map<Key, T, Compare, Allocator> &m)
265{
266 n.set_map();
267 for (const auto &it : m) {
268 n[it.first] << it.second;
269 }
270 return n;
271}
272
273template <>
274inline int JSONNode::val_t<int>() const
275{
276 return val_int();
277}
278template <>
279inline double JSONNode::val_t<double>() const
280{
281 return val_double();
282}
283template <>
284inline bool JSONNode::val_t<bool>() const
285{
286 return val_bool();
287}
288template <>
289inline std::string JSONNode::val_t<std::string>() const
290{
291 return val();
292}
293
294} // namespace Detail
295} // namespace RooFit
296
297#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define e(i)
Definition RSha256.hxx:103
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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
JSONNode & operator[](std::string const &k)
JSONNode & get(std::string const &key)
void erase(std::string const &key)
Remove a child of a map, if it exists.
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)
JSONNode const * find(std::string const &key, Keys_t const &...keys) const
void writeJSON(std::ostream &os) const
JSONNode & operator<<(std::string const &s)
std::string val() const
JSONNode & operator=(JSONNode const &)=delete
JSONNode & operator<<(const char *s)
void fill_seq(Collection const &coll, TransformationFunc func)
void fill_mat(Matrix const &mat)
JSONNode const * find(std::string const &key) const
JSONNode(JSONNode const &)=delete
const JSONNode & operator>>(std::string &v) const
bool has_child(std::string const &) const
std::string key() const
std::optional< bool > try_val_bool() const
The value as a boolean, or std::nullopt if it is none. Like val_bool(), the integers 0 and 1 are acce...
Records which components of a JSON map are read while it is alive, through any node of the tree.
static std::unique_ptr< JSONTree > create()
JSONTree(std::unique_ptr< JSONNode::Storage > storage)
std::unique_ptr< JSONNode::Storage > _storage
Int_t GetNrows() const
Int_t GetNcols() const
const Int_t n
Definition legend1.C:16
std::ostream & operator<<(std::ostream &os, RooFit::Detail::JSONNode const &s)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
TMarker m
Definition textangle.C:8