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 <type_traits>
23#include <utility>
24#include <vector>
25
26namespace RooFit {
27namespace HS3 {
28
29/// A position in the JSON tree. Keeps track of how it got there, in order to create the paths in error messages only
30/// when they are needed.
31class Cursor {
32public:
33 Cursor(Detail::JSONNode const &node, Diagnostics &diagnostics) : _node{node}, _diagnostics{diagnostics} {}
34 Cursor(Detail::JSONNode const &node, Cursor const &parent, const char *key)
35 : _node{node}, _diagnostics{parent._diagnostics}, _parent{&parent}, _key{key}
36 {
37 }
38 Cursor(Detail::JSONNode const &node, Cursor const &parent, std::size_t index)
39 : _node{node}, _diagnostics{parent._diagnostics}, _parent{&parent}, _index{index}
40 {
41 }
42
43 Detail::JSONNode const &node() const { return _node; }
44 std::string path() const;
45 void error(std::string const &message) const;
46 void warning(std::string const &message) const;
47 std::size_t numErrors() const;
48
49private:
52 Cursor const *_parent = nullptr;
53 const char *_key = nullptr;
54 std::size_t _index = 0;
55};
56
57/// Reads the components of a struct.
58class Reader {
59public:
60 explicit Reader(Cursor const &cursor);
61 /// Reports the components that were not read, unless reading the struct gave an error.
62 ~Reader();
63 Reader(Reader const &) = delete;
64 Reader &operator=(Reader const &) = delete;
65
66 /// Read a required component, or an optional one if the field is a std::optional.
67 template <class T>
68 void operator()(const char *key, T &field);
69 template <class T>
70 void operator()(const char *key, std::optional<T> &field);
71
72 /// Read a component that can be omitted or null, which leaves the field as it is, e.g. empty for an array.
73 template <class T>
74 void optional(const char *key, T &field);
75
76 /// Read the components of a struct that is nested in a component.
77 template <class Fn>
78 void nested(const char *key, Fn &&fn);
79
80 /// Whether the struct has a component. Checking for a component counts as reading it.
81 bool has(const char *key);
82
83 /// Don't report the components that were not read, because they are read later by someone else.
84 void ignoreUnread() { _reportUnread = false; }
85
86 Cursor const &cursor() const { return _cursor; }
87 /// The position of a component.
88 Cursor child(const char *key) const { return {_cursor.node()[key], _cursor, key}; }
89
90private:
92 std::vector<std::string> _seen;
93 std::size_t _nErrorsBefore = 0;
94 bool _reportUnread = true;
95};
96
97void readValue(Cursor const &c, std::string &out);
98void readValue(Cursor const &c, double &out);
99void readValue(Cursor const &c, int &out);
100void readValue(Cursor const &c, bool &out);
101void readValue(Cursor const &c, Interpolation &out);
102void readValue(Cursor const &c, NameOrNumber &out);
103
104/// Reads an integer interpolation code, as older versions of RooFit wrote them.
105std::optional<Interpolation> readInterpolationCode(Cursor const &c, InterpolationClass interpolationClass);
106
107template <class Tag>
108void readValue(Cursor const &c, Ref<Tag> &out)
109{
110 readValue(c, out.name);
111}
112
113/// A value that can be null, like the constraint of a parameter that is not constrained.
114template <class T>
115void readValue(Cursor const &c, std::optional<T> &out)
116{
117 if (!c.node().is_null()) {
118 readValue(c, out.emplace());
119 }
120}
121
122template <class T>
123void readValue(Cursor const &c, std::vector<T> &out)
124{
125 Detail::JSONNode const &node = c.node();
126 if (!node.is_seq()) {
127 c.error("must be an array");
128 return;
129 }
130 out.clear();
131 out.reserve(node.num_children());
132 std::size_t i = 0;
133 for (Detail::JSONNode const &child : node.children()) {
134 out.emplace_back();
135 readValue(Cursor{child, c, i}, out.back());
136 ++i;
137 }
138}
139
140/// A struct that has a value of one type for each name.
141template <class T>
142void readValue(Cursor const &c, std::map<std::string, T> &out)
143{
144 if (!c.node().is_map()) {
145 c.error("must be a struct");
146 return;
147 }
148 for (Detail::JSONNode const &item : c.node().children()) {
149 const std::string key = item.key();
150 readValue(Cursor{item, c, key.c_str()}, out[key]);
151 }
152}
153
154/// Everything that has a describe() function is read from a struct.
155template <class T>
156auto readValue(Cursor const &c, T &out) -> std::void_t<decltype(describe(std::declval<Reader &>(), out))>
157{
158 if (!c.node().is_map()) {
159 c.error("must be a struct");
160 return;
161 }
162 Reader reader{c};
163 describe(reader, out);
164}
165
166template <class T>
167void Reader::operator()(const char *key, T &field)
168{
169 if (!has(key)) {
170 _cursor.error(std::string("does not define the required component \"") + key + "\"");
171 return;
172 }
173 readValue(child(key), field);
174}
175
176template <class T>
177void Reader::operator()(const char *key, std::optional<T> &field)
178{
179 if (has(key)) {
180 readValue(child(key), field);
181 }
182}
183
184template <class T>
185void Reader::optional(const char *key, T &field)
186{
187 if (has(key) && !_cursor.node()[key].is_null()) {
188 readValue(child(key), field);
189 }
190}
191
192template <class Fn>
193void Reader::nested(const char *key, Fn &&fn)
194{
195 if (!has(key)) {
196 _cursor.error(std::string("does not define the required component \"") + key + "\"");
197 return;
198 }
199 Cursor c = child(key);
200 if (!c.node().is_map()) {
201 c.error("must be a struct");
202 return;
203 }
204 Reader reader{c};
205 fn(reader);
206}
207
208} // namespace HS3
209} // namespace RooFit
210
211#endif
std::string message
#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:31
const char * _key
Definition HS3Schema.h:53
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:38
Detail::JSONNode const & node() const
Definition HS3Schema.h:43
std::string path() const
Definition HS3Parser.cxx:98
Cursor(Detail::JSONNode const &node, Diagnostics &diagnostics)
Definition HS3Schema.h:33
std::size_t _index
Definition HS3Schema.h:54
Diagnostics & _diagnostics
Definition HS3Schema.h:51
Cursor const * _parent
Definition HS3Schema.h:52
Detail::JSONNode const & _node
Definition HS3Schema.h:50
Cursor(Detail::JSONNode const &node, Cursor const &parent, const char *key)
Definition HS3Schema.h:34
void error(std::string const &message) const
Reads the components of a struct.
Definition HS3Schema.h:58
void ignoreUnread()
Don't report the components that were not read, because they are read later by someone else.
Definition HS3Schema.h:84
~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:92
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:167
void optional(const char *key, T &field)
Read a component that can be omitted or null, which leaves the field as it is, e.g....
Definition HS3Schema.h:185
void nested(const char *key, Fn &&fn)
Read the components of a struct that is nested in a component.
Definition HS3Schema.h:193
std::size_t _nErrorsBefore
Definition HS3Schema.h:93
Cursor const & cursor() const
Definition HS3Schema.h:86
Cursor const & _cursor
Definition HS3Schema.h:91
Reader & operator=(Reader const &)=delete
Reader(Reader const &)=delete
Cursor child(const char *key) const
The position of a component.
Definition HS3Schema.h:88
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:85
std::string name
Definition HS3Document.h:86