15#include <nlohmann/json.hpp>
34 template <
class Nd,
class NdType,
class json_it>
37 std::unique_ptr<Impl> node;
39 const TJSONTree *
get_tree()
const {
return tree; }
40 TJSONTree *
get_tree() {
return tree; }
45 void writeJSON(std::ostream &os)
const override;
47 Node(TJSONTree *t, std::istream &is);
48 Node(TJSONTree *t, Impl &
other);
50 Node(
const Node &
other);
52 Node &
operator<<(std::string
const &s)
override;
56 const Node &
operator>>(std::string &
v)
const override;
57 Node &operator[](std::string
const &k)
override;
58 const Node &operator[](std::string
const &k)
const override;
59 bool is_container()
const override;
60 bool is_map()
const override;
61 bool is_seq()
const override;
62 bool is_null()
const override;
63 bool is_number()
const override;
64 Node &set_map()
override;
65 Node &set_seq()
override;
66 Node &set_null()
override;
67 void clear()
override;
68 std::string key()
const override;
69 std::string val()
const override;
70 int val_int()
const override;
71 double val_double()
const override;
72 bool val_bool()
const override;
73 bool has_key()
const override;
74 bool has_val()
const override;
75 bool has_child(std::string
const &)
const override;
76 Node &append_child()
override;
77 size_t num_children()
const override;
78 Node &
child(
size_t pos)
override;
79 const Node &
child(
size_t pos)
const override;
81 children_view children()
override;
82 const_children_view children()
const override;
87 std::list<Node> _nodecache;
92 TJSONTree(std::istream &is);
93 TJSONTree::Node &
incache(
const TJSONTree::Node &
n);
95 Node &rootnode()
override {
return root; }
101 return nlohmann::json::parse(is);
102 }
catch (
const nlohmann::json::exception &
ex) {
103 throw std::runtime_error(
ex.what());
109TJSONTree::TJSONTree() : root(
this) {};
111TJSONTree::TJSONTree(std::istream &is) : root(
this, is) {};
113TJSONTree::~TJSONTree()
115 TJSONTree::_nodecache.clear();
118TJSONTree::Node &TJSONTree::incache(
const TJSONTree::Node &
n)
120 _nodecache.push_back(
n);
121 return _nodecache.back();
124const TJSONTree::Node::Impl &TJSONTree::Node::get_node()
const
129TJSONTree::Node::Impl &TJSONTree::Node::get_node()
136class TJSONTree::Node::Impl {
139 std::string
const &key()
const {
return _key; }
140 virtual nlohmann::json &
get() = 0;
141 virtual const nlohmann::json &
get()
const = 0;
144 Impl(
const std::string &k) : _key(k) {}
145 virtual ~Impl() =
default;
146 static TJSONTree::Node &
mkNode(TJSONTree *t,
const std::string &k, nlohmann::json &
n);
147 static const TJSONTree::Node &
mkNode(
const TJSONTree *t,
const std::string &k,
const nlohmann::json &
n);
150class TJSONTree::Node::Impl::BaseNode :
public TJSONTree::Node::Impl {
154 nlohmann::json &
get()
override {
return node; }
155 const nlohmann::json &
get()
const override {
return node; }
156 BaseNode(std::istream &is) : Impl(
""), node(
parseWrapper(is)) {}
157 BaseNode() : Impl(
"") {}
160class TJSONTree::Node::Impl::NodeRef :
public TJSONTree::Node::Impl {
161 nlohmann::json &node;
164 nlohmann::json &
get()
override {
return node; }
165 const nlohmann::json &
get()
const override {
return node; }
166 NodeRef(
const std::string &k, nlohmann::json &
n) : Impl(k), node(
n) {}
170TJSONTree::Node &TJSONTree::Node::Impl::mkNode(TJSONTree *t,
const std::string &k, nlohmann::json &
n)
172 Node::Impl::NodeRef
ref(k,
n);
173 return t->incache(Node(t,
ref));
176const TJSONTree::Node &TJSONTree::Node::Impl::mkNode(
const TJSONTree *t,
const std::string &k,
const nlohmann::json &
n)
179 Node::Impl::NodeRef
ref(k,
const_cast<nlohmann::json &
>(
n));
180 return const_cast<TJSONTree *
>(t)->
incache(Node(
const_cast<TJSONTree *
>(t),
ref));
183TJSONTree::Node::Node(TJSONTree *t, std::istream &is) : tree(t), node(std::make_unique<Impl::BaseNode>(is)) {}
185TJSONTree::Node::Node(TJSONTree *t) : tree(t), node(std::make_unique<Impl::BaseNode>()) {}
187TJSONTree::Node::Node(TJSONTree *t, Impl &
other)
188 : tree(t), node(std::make_unique<Impl::NodeRef>(
other.key(),
other.
get()))
192TJSONTree::Node::Node(
const Node &
other) : Node(
other.tree, *
other.node) {}
194TJSONTree::Node::~Node() =
default;
198void TJSONTree::Node::writeJSON(std::ostream &os)
const
203TJSONTree::Node &TJSONTree::Node::operator<<(std::string
const &s)
209TJSONTree::Node &TJSONTree::Node::operator<<(
int i)
215TJSONTree::Node &TJSONTree::Node::operator<<(
double d)
221TJSONTree::Node &TJSONTree::Node::operator<<(
bool b)
227const TJSONTree::Node &TJSONTree::Node::operator>>(std::string &
v)
const
229 v = node->get().get<std::string>();
233TJSONTree::Node &TJSONTree::Node::operator[](std::string
const &k)
235 return Impl::mkNode(tree, k, node->get()[k]);
238const TJSONTree::Node &TJSONTree::Node::operator[](std::string
const &k)
const
240 return Impl::mkNode(tree, k, node->get()[k]);
243bool TJSONTree::Node::is_container()
const
245 return node->get().is_array() || node->get().is_object();
248bool TJSONTree::Node::is_map()
const
250 return node->get().is_object();
253bool TJSONTree::Node::is_seq()
const
255 return node->get().is_array();
258bool TJSONTree::Node::is_null()
const
260 return node->get().is_null();
263bool TJSONTree::Node::is_number()
const
265 return node->get().is_number();
273 if (node.type() == nlohmann::json::value_t::null) {
277 if (node.type() == nlohmann::json::value_t::string) {
278 if (node.get<std::string>().empty()) {
285TJSONTree::Node &TJSONTree::Node::set_map()
287 if (node->get().type() == nlohmann::json::value_t::object)
291 node->get() = nlohmann::json::object();
293 throw std::runtime_error(
"cannot declare \"" + this->key() +
"\" to be of map - type, already of type " +
294 node->get().type_name());
299TJSONTree::Node &TJSONTree::Node::set_seq()
301 if (node->get().type() == nlohmann::json::value_t::array)
305 node->get() = nlohmann::json::array();
307 throw std::runtime_error(
"cannot declare \"" + this->key() +
"\" to be of seq - type, already of type " +
308 node->get().type_name());
313TJSONTree::Node &TJSONTree::Node::set_null()
315 node->get() =
nullptr;
319void TJSONTree::Node::clear()
324std::string TJSONTree::Node::key()
const
329std::string TJSONTree::Node::val()
const
331 switch (node->get().type()) {
332 case nlohmann::json::value_t::string:
return node->get().get<std::string>();
333 case nlohmann::json::value_t::boolean:
return node->get().get<
bool>() ?
"true" :
"false";
334 case nlohmann::json::value_t::number_integer:
return std::to_string(node->get().get<
int>());
335 case nlohmann::json::value_t::number_unsigned:
return std::to_string(node->get().get<
unsigned int>());
336 case nlohmann::json::value_t::number_float: {
337 std::stringstream
ss;
338 ss << node->get().get<
double>();
342 throw std::runtime_error(
"node \"" + node->key() +
"\": implicit string conversion for type " +
343 node->get().type_name() +
" not supported!");
347int TJSONTree::Node::val_int()
const
349 return node->get().get<
int>();
351double TJSONTree::Node::val_double()
const
353 return node->get().get<
double>();
355bool TJSONTree::Node::val_bool()
const
357 auto const &nd = node->get();
360 if (nd.type() == nlohmann::json::value_t::number_unsigned) {
361 auto val = nd.get<
unsigned int>();
368 return nd.get<
bool>();
371bool TJSONTree::Node::has_key()
const
373 return !node->key().empty();
376bool TJSONTree::Node::has_val()
const
378 return node->get().is_primitive();
381bool TJSONTree::Node::has_child(std::string
const &
c)
const
383 return node->get().find(
c) != node->get().end();
386TJSONTree::Node &TJSONTree::Node::append_child()
388 node->get().push_back(
"");
389 return Impl::mkNode(tree,
"", node->get().back());
392size_t TJSONTree::Node::num_children()
const
394 return node->get().size();
397TJSONTree::Node &TJSONTree::Node::child(
size_t pos)
399 return Impl::mkNode(tree,
"", node->get().at(pos));
402const TJSONTree::Node &TJSONTree::Node::child(
size_t pos)
const
404 return Impl::mkNode(tree,
"", node->get().at(pos));
410template <
class Nd,
class NdType,
class json_it>
420 ChildItImpl(
const ChildItImpl &
other) : node(
other.node), iter(
other.iter) {}
422 std::unique_ptr<typename child_iterator::Impl> clone()
const override
424 return std::make_unique<ChildItImpl>(node, iter);
426 void forward()
override { ++iter; }
427 void backward()
override { --iter; }
428 Nd ¤t()
override
431 return TJSONTree::Node::Impl::mkNode(node.get_tree(),
"", iter.value());
433 return TJSONTree::Node::Impl::mkNode(node.get_tree(), iter.key(), iter.value());
436 bool equal(
const typename child_iterator::Impl &
other)
const override
441 return it && it->iter == this->iter;
451 using childIt = TJSONTree::Node::ChildItImpl<RooFit::Detail::JSONNode, TJSONTree::Node, json_iterator>;
452 return {child_iterator(std::make_unique<childIt>(*
this, childIt::POS::BEGIN)),
453 child_iterator(std::make_unique<childIt>(*
this, childIt::POS::END))};
458 TJSONTree::Node::ChildItImpl<const RooFit::Detail::JSONNode, const TJSONTree::Node, const_json_iterator>;
459 return {const_child_iterator(std::make_unique<childConstIt>(*
this, childConstIt::POS::BEGIN)),
460 const_child_iterator(std::make_unique<childConstIt>(*
this, childConstIt::POS::END))};
465template <
class Node_t>
469 ChildItImpl(
Node_t &
n,
size_t p) : node(
n), pos(
p) {}
470 ChildItImpl(
const ChildItImpl &
other) : node(
other.node), pos(
other.pos) {}
471 std::unique_ptr<typename child_iterator::Impl> clone()
const override
473 return std::make_unique<ChildItImpl>(node, pos);
475 void forward()
override { ++pos; }
477 Node_t ¤t()
override {
return node.child(pos); }
478 bool equal(
const typename child_iterator::Impl &
other)
const override
481 return it && &(it->node) == &(this->node) && (it->pos) == this->pos;
494template class JSONNode::child_iterator_t<JSONNode>;
495template class JSONNode::child_iterator_t<const JSONNode>;
497double JSONNode::val_double()
const
500 std::stringstream
ss{val()};
509bool JSONNode::is_number()
const
511 if (is_container() || is_null()) {
514 const std::string
text = val();
544template <
typename... Args>
545std::unique_ptr<JSONTree> JSONTree::createImpl(Args &&...args)
547 return std::make_unique<TJSONTree>(std::forward<Args>(args)...);
550std::unique_ptr<JSONTree> JSONTree::create()
555std::unique_ptr<JSONTree> JSONTree::create(std::istream &is)
557 return createImpl(is);
560std::unique_ptr<JSONTree> JSONTree::create(std::string
const &str)
562 std::stringstream
ss{str};
563 return JSONTree::create(
ss);
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
TBuffer & operator>>(TBuffer &buf, Tmpl *&obj)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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 text
virtual void writeJSON(std::ostream &os) const =0
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
void backward(LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
backward application of the weights (back-propagation of the error)