Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Schema.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN, Nov 2026
5 *
6 * Copyright (c) 2026, 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_HS3Schema_h
14#define RooFit_HS3Schema_h
15
16#include "HS3Document.h"
17
19
20#include <cstddef>
21#include <string>
22#include <string_view>
23#include <type_traits>
24#include <utility>
25#include <vector>
26
27namespace RooFit {
28namespace HS3 {
29
30/// A position in the JSON tree. Keeps track of how it got there, in order to create the paths in error messages only
31/// when they are needed.
32class Cursor {
33public:
34 Cursor(Detail::JSONNode const &node, Diagnostics &diagnostics) : _node{node}, _diagnostics{diagnostics} {}
35 Cursor(Detail::JSONNode const &node, Cursor const &parent, const char *key)
36 : _node{node}, _diagnostics{parent._diagnostics}, _parent{&parent}, _key{key}
37 {
38 }
39 Cursor(Detail::JSONNode const &node, Cursor const &parent, std::size_t index)
40 : _node{node}, _diagnostics{parent._diagnostics}, _parent{&parent}, _index{index}
41 {
42 }
43
44 Detail::JSONNode const &node() const { return _node; }
45 std::string path() const;
46 void error(std::string const &message) const;
47 void warning(std::string const &message) const;
48 std::size_t numErrors() const;
49
50private:
53 Cursor const *_parent = nullptr;
54 const char *_key = nullptr;
55 std::size_t _index = 0;
56};
57
58/// Reads the components of a struct.
59class Reader {
60public:
61 explicit Reader(Cursor const &cursor);
62 /// Reports the components that were not read, unless reading the struct gave an error.
63 ~Reader();
64 Reader(Reader const &) = delete;
65 Reader &operator=(Reader const &) = delete;
66
67 /// Read a required component, or an optional one if the field is a std::optional.
68 template <class T>
69 void operator()(const char *key, T &field);
70 template <class T>
71 void operator()(const char *key, std::optional<T> &field);
72
73 /// Read an array that can be omitted, which is equivalent to an empty one.
74 template <class T>
75 void list(const char *key, std::vector<T> &field);
76
77 /// Read the components of a struct that is nested in a component.
78 template <class Fn>
79 void nested(const char *key, Fn &&fn);
80
81 /// Whether the struct has a component. Checking for a component counts as reading it.
82 bool has(const char *key);
83
84 Cursor const &cursor() const { return _cursor; }
85
86private:
88 std::vector<std::string> _seen;
89 std::size_t _nErrorsBefore = 0;
90};
91
92void readValue(Cursor const &c, std::string &out);
93void readValue(Cursor const &c, double &out);
94void readValue(Cursor const &c, int &out);
95void readValue(Cursor const &c, bool &out);
96void readValue(Cursor const &c, ParameterValue &out);
97void readValue(Cursor const &c, Data &out);
98void readValue(Cursor const &c, Interpolation &out);
99void readValue(Cursor const &c, NameOrNumber &out);
100
101/// Reads an integer interpolation code, as older versions of RooFit wrote them.
102std::optional<Interpolation> readInterpolationCode(Cursor const &c, InterpolationClass interpolationClass);
103
104template <class Tag>
105void readValue(Cursor const &c, Ref<Tag> &out)
106{
107 readValue(c, out.name);
108}
109
110template <class T>
111void readValue(Cursor const &c, std::vector<T> &out)
112{
113 Detail::JSONNode const &node = c.node();
114 if (!node.is_seq()) {
115 c.error("must be an array");
116 return;
117 }
118 out.clear();
119 out.reserve(node.num_children());
120 std::size_t i = 0;
121 for (Detail::JSONNode const &child : node.children()) {
122 out.emplace_back();
123 readValue(Cursor{child, c, i}, out.back());
124 ++i;
125 }
126}
127
128/// Everything that has a describe() function is read from a struct.
129template <class T>
130auto readValue(Cursor const &c, T &out) -> std::void_t<decltype(describe(std::declval<Reader &>(), out))>
131{
132 if (!c.node().is_map()) {
133 c.error("must be a struct");
134 return;
135 }
136 Reader reader{c};
137 describe(reader, out);
138}
139
140template <class T>
141void Reader::operator()(const char *key, T &field)
142{
143 if (!has(key)) {
144 _cursor.error(std::string("does not define the required component \"") + key + "\"");
145 return;
146 }
147 readValue(Cursor{_cursor.node()[key], _cursor, key}, field);
148}
149
150template <class T>
151void Reader::operator()(const char *key, std::optional<T> &field)
152{
153 if (!has(key)) {
154 return;
155 }
156 Detail::JSONNode const &node = _cursor.node()[key];
157 if (node.is_null()) {
158 return;
159 }
160 field.emplace();
161 readValue(Cursor{node, _cursor, key}, *field);
162}
163
164template <class T>
165void Reader::list(const char *key, std::vector<T> &field)
166{
167 if (!has(key)) {
168 return;
169 }
170 Detail::JSONNode const &node = _cursor.node()[key];
171 if (node.is_null()) {
172 return;
173 }
174 readValue(Cursor{node, _cursor, key}, field);
175}
176
177template <class Fn>
178void Reader::nested(const char *key, Fn &&fn)
179{
180 if (!has(key)) {
181 _cursor.error(std::string("does not define the required component \"") + key + "\"");
182 return;
183 }
184 Cursor child{_cursor.node()[key], _cursor, key};
185 if (!child.node().is_map()) {
186 child.error("must be a struct");
187 return;
188 }
189 Reader reader{child};
190 fn(reader);
191}
192
193} // namespace HS3
194} // namespace RooFit
195
196#endif
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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
A position in the JSON tree.
Definition HS3Schema.h:32
const char * _key
Definition HS3Schema.h:54
std::size_t numErrors() const
void warning(std::string const &message) const
Cursor(Detail::JSONNode const &node, Cursor const &parent, std::size_t index)
Definition HS3Schema.h:39
Detail::JSONNode const & node() const
Definition HS3Schema.h:44
std::string path() const
Cursor(Detail::JSONNode const &node, Diagnostics &diagnostics)
Definition HS3Schema.h:34
std::size_t _index
Definition HS3Schema.h:55
Diagnostics & _diagnostics
Definition HS3Schema.h:52
Cursor const * _parent
Definition HS3Schema.h:53
Detail::JSONNode const & _node
Definition HS3Schema.h:51
Cursor(Detail::JSONNode const &node, Cursor const &parent, const char *key)
Definition HS3Schema.h:35
void error(std::string const &message) const
Reads the components of a struct.
Definition HS3Schema.h:59
~Reader()
Reports the components that were not read, unless reading the struct gave an error.
Reader(Cursor const &cursor)
bool has(const char *key)
Whether the struct has a component. Checking for a component counts as reading it.
std::vector< std::string > _seen
Definition HS3Schema.h:88
void operator()(const char *key, T &field)
Read a required component, or an optional one if the field is a std::optional.
Definition HS3Schema.h:141
void nested(const char *key, Fn &&fn)
Read the components of a struct that is nested in a component.
Definition HS3Schema.h:178
std::size_t _nErrorsBefore
Definition HS3Schema.h:89
Cursor const & cursor() const
Definition HS3Schema.h:84
Cursor const & _cursor
Definition HS3Schema.h:87
Reader & operator=(Reader const &)=delete
void list(const char *key, std::vector< T > &field)
Read an array that can be omitted, which is equivalent to an empty one.
Definition HS3Schema.h:165
Reader(Reader const &)=delete
Types that represent an HS3 document.
std::variant< std::string, double > NameOrNumber
A name, or a number for a constant.
InterpolationClass
The RooFit classes that implement the interpolations, which encode them with integer codes.
void readValue(Cursor const &c, std::string &out)
void describe(Reader &ar, Package &p)
std::optional< Interpolation > readInterpolationCode(Cursor const &c, InterpolationClass interpolationClass)
Reads an integer interpolation code, as older versions of RooFit wrote them.
std::vector< Diagnostic > Diagnostics
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
How a modifier of a HistFactory distribution interpolates and extrapolates its effect.
A name that says what it refers to, so that for example a DataRef can't be used as a DomainRef.
Definition HS3Document.h:63
std::string name
Definition HS3Document.h:64