Logo ROOT  
Reference Guide
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 private:
41 std::unique_ptr<Impl> it;
42
43 public:
44 child_iterator_t(std::unique_ptr<Impl> impl) : it(std::move(impl)) {}
45 child_iterator_t(const child_iterator_t &other) : it(std::move(other.it->clone())) {}
46
48 {
49 it->forward();
50 return *this;
51 }
53 {
54 it->backward();
55 return *this;
56 }
57 Nd &operator*() const { return it->current(); }
58 Nd &operator->() const { return it->current(); }
59
60 bool operator!=(const child_iterator_t &that) const { return !this->it->equal(*that.it); }
61 bool operator==(const child_iterator_t &that) const { return this->it->equal(*that.it); }
62 };
63
66
67 template <class Nd>
70
71 public:
72 inline children_view_t(child_iterator_t<Nd> const &b_, child_iterator_t<Nd> const &e_) : b(b_), e(e_) {}
73
74 inline child_iterator_t<Nd> begin() const { return b; }
75 inline child_iterator_t<Nd> end() const { return e; }
76 };
77
78public:
79 virtual void writeJSON(std::ostream &os) const = 0;
80 virtual void writeYML(std::ostream &) const { throw std::runtime_error("YML not supported"); }
81
82public:
83 virtual JSONNode &operator<<(std::string const &s) = 0;
84 virtual JSONNode &operator<<(int i) = 0;
85 virtual JSONNode &operator<<(double d) = 0;
86 virtual const JSONNode &operator>>(std::string &v) const = 0;
87 virtual JSONNode &operator[](std::string const &k) = 0;
88 virtual JSONNode &operator[](size_t pos) = 0;
89 virtual const JSONNode &operator[](std::string const &k) const = 0;
90 virtual const JSONNode &operator[](size_t pos) const = 0;
91 virtual bool is_container() const = 0;
92 virtual bool is_map() const = 0;
93 virtual bool is_seq() const = 0;
94 virtual void set_map() = 0;
95 virtual void set_seq() = 0;
96 virtual void clear() = 0;
97
98 virtual std::string key() const = 0;
99 virtual std::string val() const = 0;
100 virtual int val_int() const { return atoi(this->val().c_str()); }
101 virtual double val_double() const { return std::stod(this->val()); }
102 virtual bool val_bool() const { return atoi(this->val().c_str()); }
103 template <class T>
104 T val_t() const;
105 virtual bool has_key() const = 0;
106 virtual bool has_val() const = 0;
107 virtual bool has_child(std::string const &) const = 0;
108 virtual JSONNode &append_child() = 0;
109 virtual size_t num_children() const = 0;
110
113
114 virtual children_view children();
115 virtual const_children_view children() const;
116 virtual JSONNode &child(size_t pos) = 0;
117 virtual const JSONNode &child(size_t pos) const = 0;
118
119 template <typename Collection>
120 void fill_seq(Collection const &coll)
121 {
122 set_seq();
123 for (auto const &item : coll) {
124 append_child() << item;
125 }
126 }
127
128 template <typename Collection, typename TransformationFunc>
129 void fill_seq(Collection const &coll, TransformationFunc func)
130 {
131 set_seq();
132 for (auto const &item : coll) {
133 append_child() << func(item);
134 }
135 }
136
137 template <typename Matrix>
138 void fill_mat(Matrix const &mat)
139 {
140 set_seq();
141 for (int i = 0; i < mat.GetNrows(); ++i) {
142 auto &row = append_child();
143 row.set_seq();
144 for (int j = 0; j < mat.GetNcols(); ++j) {
145 row.append_child() << mat(i, j);
146 }
147 }
148 }
149};
150
151class JSONTree {
152public:
153 virtual ~JSONTree() = default;
154
155 virtual JSONNode &rootnode() = 0;
156
157 static std::unique_ptr<JSONTree> create();
158 static std::unique_ptr<JSONTree> create(std::istream &is);
159
160 static std::string getBackend();
161 static void setBackend(std::string const &name);
162
163 static bool hasBackend(std::string const &name);
164
165private:
166 // Internally, we store the backend type with an enum to be more memory efficient.
167 enum class Backend { NlohmannJson, Ryml };
168
169 static Backend &getBackendEnum();
170};
171
172std::ostream &operator<<(std::ostream &os, RooFit::Detail::JSONNode const &s);
173
174template <class T>
175std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode::children_view const &cv)
176{
177 for (const auto &e : cv) {
178 v.push_back(e.val_t<T>());
179 }
180 return v;
181}
182
183template <class T>
184std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode::const_children_view const &cv)
185{
186 for (const auto &e : cv) {
187 v.push_back(e.val_t<T>());
188 }
189 return v;
190}
191
192template <class T>
193std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode const &n)
194{
195 if (!n.is_seq()) {
196 throw std::runtime_error("node " + n.key() + " is not of sequence type!");
197 }
198 v << n.children();
199 return v;
200}
201
202} // namespace Detail
203} // namespace RooFit
204
205#endif
#define d(i)
Definition: RSha256.hxx:102
#define e(i)
Definition: RSha256.hxx:103
char name[80]
Definition: TGX11.cxx:110
virtual std::unique_ptr< Impl > clone() const =0
virtual bool equal(const Impl &other) const =0
bool operator==(const child_iterator_t &that) const
Definition: JSONInterface.h:61
bool operator!=(const child_iterator_t &that) const
Definition: JSONInterface.h:60
child_iterator_t(std::unique_ptr< Impl > impl)
Definition: JSONInterface.h:44
child_iterator_t(const child_iterator_t &other)
Definition: JSONInterface.h:45
child_iterator_t< Nd > begin() const
Definition: JSONInterface.h:74
children_view_t(child_iterator_t< Nd > const &b_, child_iterator_t< Nd > const &e_)
Definition: JSONInterface.h:72
child_iterator_t< Nd > end() const
Definition: JSONInterface.h:75
virtual JSONNode & operator<<(std::string const &s)=0
virtual bool val_bool() const
virtual void set_map()=0
virtual std::string val() const =0
virtual const JSONNode & operator>>(std::string &v) const =0
virtual JSONNode & operator[](size_t pos)=0
virtual void set_seq()=0
void fill_seq(Collection const &coll)
virtual const JSONNode & operator[](size_t pos) const =0
virtual JSONNode & append_child()=0
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 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 void writeYML(std::ostream &) const
Definition: JSONInterface.h:80
virtual bool is_map() const =0
virtual bool has_child(std::string const &) const =0
virtual std::string key() const =0
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
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,...
virtual ~JSONTree()=default
virtual JSONNode & rootnode()=0
Int_t GetNrows() const
Definition: TMatrixTBase.h:123
Int_t GetNcols() const
Definition: TMatrixTBase.h:126
const Int_t n
Definition: legend1.C:16
double T(double x)
Definition: ChebyshevPol.h:34
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: Common.h:18
static constexpr double s