Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONInterface.cxx
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
14
15#include "JSONParser.h"
16#ifdef ROOFIT_WITH_RYML
17#include "RYMLParser.h"
18#endif
19
20#include <sstream>
21
22namespace {
23template <class Node_t>
24class ChildItImpl final : public RooFit::Detail::JSONNode::child_iterator_t<Node_t>::Impl {
25public:
27 ChildItImpl(Node_t &n, size_t p) : node(n), pos(p) {}
28 ChildItImpl(const ChildItImpl &other) : node(other.node), pos(other.pos) {}
29 std::unique_ptr<typename child_iterator::Impl> clone() const override
30 {
31 return std::make_unique<ChildItImpl>(node, pos);
32 }
33 void forward() override { ++pos; }
34 void backward() override { --pos; }
35 Node_t &current() override { return node.child(pos); }
36 bool equal(const typename child_iterator::Impl &other) const override
37 {
38 auto it = dynamic_cast<const ChildItImpl<Node_t> *>(&other);
39 return it && &(it->node) == &(this->node) && (it->pos) == this->pos;
40 }
41
42private:
43 Node_t &node;
44 size_t pos;
45};
46} // namespace
47
48namespace RooFit {
49namespace Detail {
50
51template class JSONNode::child_iterator_t<JSONNode>;
52template class JSONNode::child_iterator_t<const JSONNode>;
53
55{
56 return {child_iterator(std::make_unique<::ChildItImpl<JSONNode>>(*this, 0)),
57 child_iterator(std::make_unique<::ChildItImpl<JSONNode>>(*this, this->num_children()))};
58}
60{
61 return {const_child_iterator(std::make_unique<::ChildItImpl<const JSONNode>>(*this, 0)),
62 const_child_iterator(std::make_unique<::ChildItImpl<const JSONNode>>(*this, this->num_children()))};
63}
64
65std::ostream &operator<<(std::ostream &os, JSONNode const &s)
66{
67 s.writeJSON(os);
68 return os;
69}
70
71template <typename... Args>
72std::unique_ptr<JSONTree> JSONTree::createImpl(Args &&...args)
73{
75#ifdef ROOFIT_WITH_RYML
76 return std::make_unique<TRYMLTree>(std::forward<Args>(args)...);
77#else
78 throw std::runtime_error(
79 "Requesting JSON tree with rapidyaml backend, but rapidyaml could not be found by ROOT when it was compiled.");
80#endif
81 }
82 return std::make_unique<TJSONTree>(std::forward<Args>(args)...);
83}
84
85std::unique_ptr<JSONTree> JSONTree::create()
86{
87 return createImpl();
88}
89
90std::unique_ptr<JSONTree> JSONTree::create(std::istream &is)
91{
92 return createImpl(is);
93}
94
95std::unique_ptr<JSONTree> JSONTree::create(std::string const &str)
96{
97 std::stringstream ss{str};
98 return JSONTree::create(ss);
99}
100
101/// Check if ROOT was compiled with support for a certain JSON backend library.
102/// \param[in] name Name of the backend.
103bool JSONTree::hasBackend(std::string const &name)
104{
105 if (name == "rapidyaml") {
106#ifdef ROOFIT_WITH_RYML
107 return true;
108#else
109 return false;
110#endif
111 }
112 if (name == "nlohmann-json")
113 return true;
114 return false;
115}
116
118{
119 static Backend backend = Backend::NlohmannJson;
120 return backend;
121}
122
123/// Returns the name of the library that serves as the backend for the JSON
124/// interface, which is either `"nlohmann-json"` or `"rapidyaml"`.
125/// \return Backend name as a string.
127{
128 return getBackendEnum() == Backend::Ryml ? "rapidyaml" : "nlohmann-json";
129}
130
131/// Set the library that serves as the backend for the JSON interface. Note that the `"rapidyaml"` backend is only
132/// supported if rapidyaml was found on the system when ROOT was compiled. \param[in] name Name of the backend, can be
133/// either `"nlohmann-json"` or `"rapidyaml"`.
134void JSONTree::setBackend(std::string const &name)
135{
136 if (name == "rapidyaml")
138 if (name == "nlohmann-json")
140}
141
142} // namespace Detail
143} // namespace RooFit
TObject * clone(const char *newname) const override
Definition RooChi2Var.h:9
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:397
winID h TVirtualViewer3D TVirtualGLPainter p
char name[80]
Definition TGX11.cxx:110
child_iterator_t< JSONNode > child_iterator
virtual children_view children()
virtual size_t num_children() const =0
virtual void writeJSON(std::ostream &os) const =0
child_iterator_t< const JSONNode > const_child_iterator
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)
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
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)