Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Parser.cxx
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#include "HS3Schema.h"
14
15#include <algorithm>
16#include <cctype>
17#include <charconv>
18#include <cmath>
19#include <limits>
20#include <sstream>
21
23
24namespace RooFit {
25namespace HS3 {
26
27constexpr auto supportedVersion = "0.3";
28
29namespace {
30
31// Single source of truth for the mapping between the structured HS3 interpolation
32// descriptors and the RooFit integer codes of PiecewiseInterpolation and
33// FlexibleInterpVar. A code of `kUnrepresentable` means the descriptor cannot be
34// expressed by that class: FlexibleInterpVar internally remaps code 4 to code 5,
35// so it has no additive-linear or multiplicative-linear poly6 variant.
36struct InterpolationCodes {
37 Interpolation descriptor;
38 int piecewise = 0;
39 int flexible = 0;
40};
41
42constexpr int kUnrepresentable = -1;
43
44// clang-format off
45constexpr InterpolationCodes interpolationTable[]{
49 {additivePolynomialLinear, 4, kUnrepresentable},
51 {multiplicativePolynomialLinear, 6, kUnrepresentable},
52};
53// clang-format on
54
55int codeForClass(InterpolationCodes const &row, InterpolationClass interpolationClass)
56{
57 return interpolationClass == InterpolationClass::Piecewise ? row.piecewise : row.flexible;
58}
59
60} // namespace
61
62std::optional<Interpolation> interpolationFromCode(int code, InterpolationClass interpolationClass)
63{
64 // Code 3 was historically an unimplemented alias of code 2 for both classes,
65 // and FlexibleInterpVar treats code 5 identically to its canonical code 4.
66 if (code == 3) {
67 code = 2;
68 }
69 if (interpolationClass == InterpolationClass::Flexible && code == 5) {
70 code = 4;
71 }
72 for (auto const &row : interpolationTable) {
73 const int rowCode = codeForClass(row, interpolationClass);
74 if (rowCode != kUnrepresentable && rowCode == code) {
75 return row.descriptor;
76 }
77 }
78 return std::nullopt;
79}
80
81std::optional<int> codeFromInterpolation(Interpolation const &interpolation, InterpolationClass interpolationClass)
82{
83 for (auto const &row : interpolationTable) {
84 const int rowCode = codeForClass(row, interpolationClass);
85 if (rowCode != kUnrepresentable && row.descriptor == interpolation) {
86 return rowCode;
87 }
88 }
89 return std::nullopt;
90}
91
92bool hasErrors(Diagnostics const &diagnostics)
93{
94 return std::any_of(diagnostics.begin(), diagnostics.end(),
95 [](Diagnostic const &d) { return d.severity == Diagnostic::Severity::Error; });
96}
97
98std::string Cursor::path() const
99{
100 if (!_parent) {
101 return "";
102 }
103 std::string out = _parent->path();
104 if (_key) {
105 if (!out.empty()) {
106 out += ".";
107 }
108 out += _key;
109 return out;
110 }
111 out += "[" + std::to_string(_index) + "]";
112 if (_node.is_map() && _node.has_child("name") && !_node["name"].is_container() && !_node["name"].is_null()) {
113 out += "(" + _node["name"].val() + ")";
114 }
115 return out;
116}
117
118void Cursor::error(std::string const &message) const
119{
121}
122
123void Cursor::warning(std::string const &message) const
124{
126}
127
128std::size_t Cursor::numErrors() const
129{
130 return std::count_if(_diagnostics.begin(), _diagnostics.end(),
131 [](Diagnostic const &d) { return d.severity == Diagnostic::Severity::Error; });
132}
133
134Reader::Reader(Cursor const &cursor) : _cursor{cursor}, _nErrorsBefore{cursor.numErrors()} {}
135
136bool Reader::has(const char *key)
137{
138 if (!_cursor.node().has_child(key)) {
139 return false;
140 }
141 if (std::find(_seen.begin(), _seen.end(), key) == _seen.end()) {
142 _seen.emplace_back(key);
143 }
144 return true;
145}
146
148{
149 JSONNode const &node = _cursor.node();
150 // There is nothing to report if all components were read, and after an error it would only be noise.
151 if (!_reportUnread || !node.is_map() || node.num_children() == _seen.size() ||
153 return;
154 }
155 for (JSONNode const &child : node.children()) {
156 const std::string key = child.key();
157 if (std::find(_seen.begin(), _seen.end(), key) == _seen.end()) {
158 _cursor.warning("unknown component \"" + key + "\" is ignored");
159 }
160 }
161}
162
163void readValue(Cursor const &c, std::string &out)
164{
165 if (c.node().is_container() || c.node().is_null()) {
166 c.error("must be a string");
167 return;
168 }
169 out = c.node().val();
170}
171
172void readValue(Cursor const &c, double &out)
173{
174 if (!c.node().is_number()) {
175 c.error("must be a number");
176 return;
177 }
178 out = c.node().val_double();
179}
180
181void readValue(Cursor const &c, int &out)
182{
183 // Read through double, such that an integer that is encoded as a JSON float like 1e6 is accepted.
184 const double value = c.node().is_number() ? c.node().val_double() : 0.5;
185 if (value != std::floor(value) || std::abs(value) > static_cast<double>(std::numeric_limits<int>::max())) {
186 c.error("must be an integer");
187 return;
188 }
189 out = static_cast<int>(value);
190}
191
192void readValue(Cursor const &c, bool &out)
193{
194 if (std::optional<bool> value = c.node().try_val_bool()) {
195 out = *value;
196 } else {
197 c.error("must be a boolean");
198 }
199}
200
201bool parseVersion(std::string const &version, Version &out)
202{
203 std::size_t pos = 0;
204 auto readNumber = [&](int &number) {
205 const std::size_t begin = pos;
206 while (pos < version.size() && std::isdigit(static_cast<unsigned char>(version[pos]))) {
207 ++pos;
208 }
209 // Reject empty and absurdly long numbers, which std::stoi could not represent.
210 if (pos == begin || pos - begin > 9) {
211 return false;
212 }
213 number = std::stoi(version.substr(begin, pos - begin));
214 return true;
215 };
216 Version parsed;
217 if (!readNumber(parsed.major) || pos >= version.size() || version[pos] != '.') {
218 return false;
219 }
220 ++pos;
221 if (!readNumber(parsed.minor)) {
222 return false;
223 }
224 if (pos < version.size() && version[pos] == '.') {
225 ++pos;
226 if (!readNumber(parsed.patch)) {
227 parsed.patch = 0;
228 }
229 }
230 out = parsed;
231 return true;
232}
233
234namespace {
235
236// Read a bound of an axis. Infinite bounds are written as null.
237void readBound(Reader &ar, const char *key, std::optional<double> &out, double valueIfNull)
238{
239 if (!ar.has(key)) {
240 return;
241 }
242 Cursor c = ar.child(key);
243 if (c.node().is_null()) {
244 out = valueIfNull;
245 return;
246 }
247 out.emplace();
248 readValue(c, *out);
249}
250
251constexpr double infinity = std::numeric_limits<double>::infinity();
252
253// Read bin edges, which must be at least two strictly ascending numbers.
254void readEdges(Reader &ar, std::string const &axisName, std::vector<double> &edges)
255{
256 Cursor c = ar.child("edges");
257 const std::string context = "\"edges\" of axis '" + axisName + "' must ";
258 if (!c.node().is_seq()) {
259 c.error(context + "be a list");
260 return;
261 }
262 readValue(c, edges);
263 for (std::size_t i = 1; i < edges.size(); ++i) {
264 if (!(edges[i] > edges[i - 1])) {
265 c.error(context + "be strictly ascending");
266 return;
267 }
268 }
269 if (edges.size() < 2) {
270 c.error(context + "contain at least two values");
271 }
272}
273
274} // namespace
275
277{
278 ar("name", p.name);
279 ar("version", p.version);
280}
281
282void describe(Reader &ar, DomainAxis &axis)
283{
284 ar("name", axis.name);
285 // A fixed axis (HS3 #131): the domain of the parameter is the set with this single value, without range, binning or
286 // categories.
287 if (ar.has("value")) {
288 for (const char *key : {"min", "max", "nbins", "edges", "categories"}) {
289 if (ar.has(key)) {
290 ar.cursor().error("the axis '" + axis.name + "' has \"value\" and \"" + key +
291 "\", which exclude each other");
292 }
293 }
294 // Not read as an optional component, for which null would be the same as leaving it out.
295 ar("value", axis.fixedValue.emplace());
296 return;
297 }
298 readBound(ar, "min", axis.min, -infinity);
299 readBound(ar, "max", axis.max, infinity);
300 if (ar.has("edges")) {
301 // Not necessarily ascending, because RooFit also writes parameterized ranges as edges.
302 EdgesBinning binning;
303 ar("edges", binning.edges);
304 if (!binning.edges.empty()) {
305 if (!axis.min) {
306 axis.min = binning.edges.front();
307 }
308 if (!axis.max) {
309 axis.max = binning.edges.back();
310 }
311 }
312 axis.binning = std::move(binning);
313 } else if (ar.has("nbins")) {
314 RegularBinning binning;
315 ar("nbins", binning.nbins);
316 if (binning.nbins != 0) {
317 axis.binning = binning;
318 }
319 }
320}
321
323{
324 ar("name", axis.name);
325 // A fixed axis is only allowed in domains (HS3 0.3), which the validation reports.
326 axis.hasFixedValue = ar.has("value");
327 readBound(ar, "min", axis.min, -infinity);
328 readBound(ar, "max", axis.max, infinity);
329}
330
331void describe(Reader &ar, BinnedAxis &axis)
332{
333 ar("name", axis.name);
334 if (ar.has("edges")) {
335 EdgesBinning binning;
336 readEdges(ar, axis.name, binning.edges);
337 if (binning.edges.size() >= 2) {
338 axis.min = binning.edges.front();
339 axis.max = binning.edges.back();
340 }
341 // The standard requires "min" and "max" also for irregular binnings, which older versions of RooFit didn't write.
342 std::optional<double> min;
343 std::optional<double> max;
344 ar("min", min);
345 ar("max", max);
346 if ((min && *min != axis.min) || (max && *max != axis.max)) {
347 ar.cursor().error("\"min\" and \"max\" of axis '" + axis.name + "' must agree with the \"edges\"");
348 }
349 if (ar.has("nbins")) {
350 ar.cursor().error("axis '" + axis.name + "' must not have both \"edges\" and \"nbins\"");
351 }
352 axis.binning = std::move(binning);
353 return;
354 }
355 ar("min", axis.min);
356 ar("max", axis.max);
357 RegularBinning binning;
358 ar("nbins", binning.nbins);
359 axis.binning = binning;
360}
361
362// A categorical axis (HS3 #108) has unique non-empty labels, and no range or binning.
364{
365 ar("name", axis.name);
366 for (const char *key : {"min", "max", "nbins", "edges"}) {
367 if (ar.has(key)) {
368 ar.cursor().error("the axis '" + axis.name + "' is categorical and must not have \"" + key + "\"");
369 }
370 }
371 ar.has("categories");
372 JSONNode const &labels = ar.cursor().node()["categories"];
373 if (!labels.is_seq() || labels.num_children() == 0) {
374 ar.cursor().error("the axis '" + axis.name + "': \"categories\" must be a non-empty list of strings");
375 return;
376 }
377 for (JSONNode const &label : labels.children()) {
378 // An empty label is allowed: RooFit uses it for a column that does not apply to a component of a combined
379 // distribution.
380 if (label.is_container() || label.is_null() || label.is_number()) {
381 ar.cursor().error("the axis '" + axis.name + "': the entries of \"categories\" must be strings");
382 return;
383 }
384 if (std::find(axis.categories.begin(), axis.categories.end(), label.val()) != axis.categories.end()) {
385 ar.cursor().error("the axis '" + axis.name + "': the category \"" + label.val() + "\" is listed twice");
386 return;
387 }
388 axis.categories.push_back(label.val());
389 }
390}
391
392// The kind of an axis is determined by its components.
393template <class Axis_t>
394void describe(Reader &ar, std::variant<Axis_t, CategoricalAxis> &out)
395{
396 // A fixed axis is not categorical. That it has no categories is checked when it is read.
397 if (ar.has("categories") && !(std::is_same_v<Axis_t, DomainAxis> && ar.has("value"))) {
398 describe(ar, out.template emplace<CategoricalAxis>());
399 } else {
400 describe(ar, out.template emplace<Axis_t>());
401 }
402}
403
404void describe(Reader &ar, Domain &domain)
405{
406 ar("name", domain.name);
407 std::string type;
408 ar.optional("type", type);
409 if (type != "product_domain") {
410 ar.cursor().error("only domains of type \"product_domain\" are currently supported!");
411 return;
412 }
413 ar.optional("axes", domain.axes);
414}
415
417{
418 ar("name", out.name);
419 if (ar.has("value")) {
420 Cursor crsr = ar.child("value");
421 if (crsr.node().is_number()) {
422 out.value = crsr.node().val_double();
423 } else {
424 std::string label;
425 readValue(crsr, label);
426 out.value = std::move(label);
427 }
428 }
429 ar.optional("const", out.isConst);
430 ar("err", out.error);
431 ar("relErr", out.relError);
432 ar("nbins", out.nbins);
433}
434
436{
437 ar("name", point.name);
438 ar.optional("parameters", point.parameters);
439}
440
442{
443 ar("value", data.value);
444 ar("uncertainty", data.uncertainty);
445 ar.optional("axes", data.axes);
446}
447
449{
450 ar.optional("axes", data.axes);
451 ar("weights", data.weights);
452 data.hasEntriesUncertainties = ar.has("entries_uncertainties");
453
454 if (!ar.has("entries")) {
455 ar.cursor().error("does not define the required component \"entries\"");
456 return;
457 }
458 Cursor entries = ar.child("entries");
459 if (!entries.node().is_seq()) {
460 entries.error("key 'entries' is not a list!");
461 return;
462 }
463 if (data.weights && data.weights->size() != entries.node().num_children()) {
464 ar.cursor().error("inconsistent number of entries and weights!");
465 return;
466 }
467 data.entries.reserve(entries.node().num_children() * data.axes.size());
468 std::size_t iRow = 0;
469 for (JSONNode const &row : entries.node().children()) {
470 Cursor rowCursor{row, entries, iRow};
471 if (!row.is_seq()) {
472 rowCursor.error("coordinate point '" + std::to_string(iRow) + "' is not a list!");
473 return;
474 }
475 if (row.num_children() != data.axes.size()) {
476 rowCursor.error("inconsistent number of entries and observables!");
477 return;
478 }
479 std::size_t iAxis = 0;
480 for (JSONNode const &value : row.children()) {
481 Cursor valueCursor{value, rowCursor, iAxis};
482 if (auto const *category = std::get_if<CategoricalAxis>(&data.axes[iAxis])) {
483 std::string label;
484 readValue(valueCursor, label);
485 auto found = std::find(category->categories.begin(), category->categories.end(), label);
486 if (found == category->categories.end()) {
487 valueCursor.error("'" + label + "' is not a category of the axis '" + category->name + "'");
488 return;
489 }
490 data.entries.push_back(static_cast<double>(std::distance(category->categories.begin(), found)));
491 } else {
492 double coordinate = 0.0;
493 readValue(valueCursor, coordinate);
494 data.entries.push_back(coordinate);
495 }
496 ++iAxis;
497 }
498 ++iRow;
499 }
500}
501
502void describe(Reader &ar, GaussianUncertainty &uncertainty)
503{
504 if (ar.has("type")) {
505 std::string type;
506 ar("type", type);
507 if (type != "gaussian_uncertainty") {
508 ar.cursor().error("unsupported type of uncertainty '" + type + "'");
509 }
510 }
511 ar("sigma", uncertainty.sigma);
512 if (ar.has("correlation")) {
513 Cursor c = ar.child("correlation");
514 // Besides a matrix, it can be the number 0 to indicate that there is no correlation.
515 if (c.node().is_seq()) {
516 readValue(c, uncertainty.correlation);
517 } else if (!c.node().is_number() || c.node().val_double() != 0.0) {
518 c.error("must be a matrix or 0");
519 }
520 }
521}
522
524{
525 ar.optional("axes", data.axes);
526 ar("contents", data.contents);
527 ar("uncertainty", data.uncertainty);
528 // RooFit extension: older versions read the errors like in the samples of a HistFactory distribution.
529 if (ar.has("errors")) {
530 Cursor errors = ar.child("errors");
531 if (!errors.node().is_seq()) {
532 errors.error("errors are not in list form");
533 } else if (!data.uncertainty) {
534 data.uncertainty.emplace();
535 readValue(errors, data.uncertainty->sigma);
536 }
537 }
538}
539
540// A combined data set or distribution, whose components name what they select in `refKey`. Only the structure is
541// checked here: the columns and keys are checked against the domains by the validation, and the components when they
542// are built.
543template <class Ref_t>
544void readCombined(Reader &ar, std::string const &context, const char *refKey, Combined<Ref_t> &out)
545{
546 Cursor const &c = ar.cursor();
547 auto nonEmptyList = [&](const char *key) -> JSONNode const * {
548 if (!ar.has(key) || !c.node()[key].is_seq() || c.node()[key].num_children() == 0) {
549 return nullptr;
550 }
551 return &c.node()[key];
552 };
553 JSONNode const *columns = nonEmptyList("columns");
554 for (std::size_t i = 0; columns && i < columns->num_children(); ++i) {
555 JSONNode const &column = columns->child(i);
556 if (column.is_container() || column.val().empty()) {
557 columns = nullptr;
558 break;
559 }
560 out.columns.push_back(column.val());
561 }
562 if (!columns) {
563 c.error("\"columns\" of " + context + " must be a non-empty list of names of categorical axes");
564 return;
565 }
566 JSONNode const *components = nonEmptyList("components");
567 if (!components) {
568 c.error("\"components\" of " + context + " must be a non-empty list");
569 return;
570 }
571 for (JSONNode const &node : components->children()) {
572 if (!node.is_map() || !node.has_child("key") || !node.has_child(refKey)) {
573 c.error("every component of " + context + " must have a \"key\" and a \"" + refKey + "\"");
574 return;
575 }
576 JSONNode const &key = node["key"];
577 CombinedComponent<Ref_t> &component = out.components.emplace_back();
578 if (key.is_seq() && key.num_children() == out.columns.size()) {
579 for (JSONNode const &label : key.children()) {
580 component.key.push_back(label.is_container() ? std::string{} : label.val());
581 }
582 } else {
583 c.error("every component of " + context + " must have a \"key\" with " + std::to_string(out.columns.size()) +
584 " labels, one per column");
585 return;
586 }
587 component.ref.name = node[refKey].val();
588 }
589}
590
591void describe(Reader &ar, Data &out)
592{
593 ar("name", out.name);
594 std::string type;
595 ar("type", type);
596 if (type == "binned") {
597 describe(ar, out.body.emplace<BinnedData>());
598 } else if (type == "unbinned") {
599 describe(ar, out.body.emplace<UnbinnedData>());
600 } else if (type == "point") {
601 describe(ar, out.body.emplace<PointData>());
602 } else if (type == "combined") {
603 std::string const context = "the combined data set '" + out.name + "'";
604 auto &combined = out.body.emplace<CombinedData>();
605 if (ar.has("axes")) {
606 ar.cursor().error(context + " must not have \"axes\": its columns are declared by the domains");
607 } else {
608 readCombined(ar, context, "dataset", combined);
609 }
610 } else if (ar.has("type")) {
611 ar.cursor().error("unknown type of dataset '" + type + "'");
612 }
613}
614
615void describe(Reader &ar, Likelihood &likelihood)
616{
617 ar("name", likelihood.name);
618 ar.optional("aux_distributions", likelihood.auxDistributions);
619
620 std::vector<DistributionRef> distributions;
621 ar("distributions", distributions);
622 if (!ar.has("data")) {
623 ar.cursor().error("does not define the required component \"data\"");
624 return;
625 }
626 Cursor data = ar.child("data");
627 // A pure model, i.e. distributions without any data, which a ModelConfig can refer to.
628 if (data.node().is_null()) {
629 likelihood.pureModel = true;
630 for (auto &distribution : distributions) {
631 likelihood.terms.push_back({std::move(distribution), std::monostate{}});
632 }
633 return;
634 }
635 if (!data.node().is_seq()) {
636 data.error("must be an array");
637 return;
638 }
639 if (data.node().num_children() != distributions.size()) {
640 ar.cursor().error("\"distributions\" and \"data\" must have the same length");
641 return;
642 }
643 likelihood.terms.reserve(distributions.size());
644 std::size_t i = 0;
645 for (JSONNode const &node : data.node().children()) {
646 LikelihoodTerm term;
647 term.distribution = std::move(distributions[i]);
648 // Older versions of RooFit used the string "0" to say that there is no dataset.
649 if (node.is_number() || (!node.is_container() && !node.is_null() && node.val() == "0")) {
650 term.data = node.is_number() ? node.val_double() : 0.0;
651 } else {
652 DataRef ref;
653 readValue(Cursor{node, data, i}, ref);
654 term.data = std::move(ref);
655 }
656 likelihood.terms.push_back(std::move(term));
657 ++i;
658 }
659}
660
661void describe(Reader &ar, Analysis &analysis)
662{
663 ar("name", analysis.name);
664 ar("likelihood", analysis.likelihood);
665 // HS3 v0.2.9 used the singular keys "parameter_of_interest" (a list) and "domain" (a single name). They are still
666 // accepted, but the plural keys take precedence.
667 auto isLegacy = [&](const char *key, const char *legacyKey) {
668 if (ar.has(key) || !ar.has(legacyKey)) {
669 return false;
670 }
671 ar.cursor().warning(std::string("the key \"") + legacyKey + "\" of analysis '" + analysis.name +
672 "' is deprecated, please use \"" + key + "\" instead.");
673 return true;
674 };
675 ar.optional(isLegacy("parameters_of_interest", "parameter_of_interest") ? "parameter_of_interest"
676 : "parameters_of_interest",
677 analysis.parametersOfInterest);
678 if (!isLegacy("domains", "domain")) {
679 ar.optional("domains", analysis.domains);
680 } else if (ar.cursor().node()["domain"].is_seq()) {
681 ar.optional("domain", analysis.domains);
682 } else {
683 analysis.domains.emplace_back();
684 ar("domain", analysis.domains.back());
685 }
686 ar("init", analysis.init);
687 ar("prior", analysis.prior);
688}
689
690namespace {
691
692// The strings of the interpolation types and functions in the standard.
693std::optional<InterpolationType> interpolationTypeFromString(std::string const &str)
694{
696 if (str == toString(type)) {
697 return type;
698 }
699 }
700 return std::nullopt;
701}
702
703std::optional<InterpolationFunction> interpolationFunctionFromString(std::string const &str)
704{
707 if (str == toString(function)) {
708 return function;
709 }
710 }
711 return std::nullopt;
712}
713
714} // namespace
715
717{
718 return type == InterpolationType::Add ? "add" : "mult";
719}
720
721char const *toString(InterpolationFunction function)
722{
723 switch (function) {
724 case InterpolationFunction::Poly1: return "poly1";
725 case InterpolationFunction::Poly2: return "poly2";
726 case InterpolationFunction::Poly6: return "poly6";
727 case InterpolationFunction::Exp: return "exp";
728 }
729 return "";
730}
731
733{
734 switch (type) {
735 case ConstraintType::Gauss: return "Gauss";
736 case ConstraintType::Poisson: return "Poisson";
737 case ConstraintType::LogNormal: return "LogNormal";
738 }
739 return "";
740}
741
742void readValue(Cursor const &c, Interpolation &out)
743{
744 JSONNode const &node = c.node();
745 if (!node.is_map()) {
746 c.error("must be a struct with components 'type', 'in', and 'out'");
747 return;
748 }
749 // The reader reports the other components as unknown, unless there is an error.
750 Reader reader{c};
751 for (const char *component : {"type", "in", "out"}) {
752 if (!reader.has(component)) {
753 c.error(std::string("does not define the required '") + component + "' component");
754 return;
755 }
756 }
757 JSONNode const &typeNode = node["type"];
758 JSONNode const &inNode = node["in"];
759 JSONNode const &outNode = node["out"];
760 if (typeNode.is_container() || typeNode.is_null() || inNode.is_container() || inNode.is_null()) {
761 c.error("components 'type' and 'in' must be strings");
762 return;
763 }
764
765 auto type = interpolationTypeFromString(typeNode.val());
766 if (!type) {
767 c.error("has unknown interpolation type '" + typeNode.val() + "'");
768 return;
769 }
770 auto in = interpolationFunctionFromString(inNode.val());
771 if (!in) {
772 c.error("has unknown interpolation function '" + inNode.val() + "'");
773 return;
774 }
775 out.type = *type;
776 out.in = *in;
777 out.out.reset();
778
779 if (!outNode.is_null()) {
780 if (outNode.is_container()) {
781 c.error("component 'out' must be a string or null");
782 return;
783 }
784 out.out = interpolationFunctionFromString(outNode.val());
785 if (!out.out) {
786 c.error("has unknown extrapolation function '" + outNode.val() + "'");
787 }
788 }
789}
790
791namespace {
792
793// Constraints: "constraints" names the constraint distribution of each parameter, or is null for an unconstrained one.
794// Older documents give a type in "constraint", or a distribution in "constraint_name", to which upgradeDocument()
795// translated the even older forms.
796std::optional<ConstraintType> readConstraintType(Cursor const &c)
797{
798 std::string value;
799 readValue(c, value);
801 if (value == toString(type)) {
802 return type;
803 }
804 }
805 c.error("unknown constraint type '" + value + "', supported are \"Gauss\", \"Poisson\" and \"LogNormal\"");
806 return std::nullopt;
807}
808
809// The parameter of a modifier with a single parameter and its constraint are arrays with one entry.
810template <class T>
811void readSingleEntry(Reader &ar, const char *key, T &out)
812{
813 Cursor c = ar.child(key);
814 if (!c.node().is_seq() || c.node().num_children() != 1) {
815 c.error("must be an array with one entry");
816 return;
817 }
818 readValue(Cursor{c.node().child(0), c, std::size_t{0}}, out);
819}
820
821void readConstraint(Reader &ar, Constraint &out)
822{
823 if (ar.has("constraints")) {
824 std::optional<DistributionRef> pdf;
825 readSingleEntry(ar, "constraints", pdf);
826 if (pdf) {
827 out = ConstraintPdf{std::move(*pdf)};
828 }
829 } else if (ar.has("constraint_name")) {
830 ConstraintPdf pdf;
831 ar("constraint_name", pdf.pdf);
832 pdf.legacyName = true;
833 out = std::move(pdf);
834 } else if (ar.has("constraint")) {
835 if (auto type = readConstraintType(ar.child("constraint"))) {
836 out = *type;
837 }
838 }
839}
840
841void readGammaConstraint(Reader &ar, GammaConstraint &out)
842{
843 if (ar.has("constraints")) {
844 ar("constraints", out.emplace<GammaConstraintPdfs>().pdfs);
845 } else if (ar.has("constraint")) {
846 Cursor c = ar.child("constraint");
847 // "Const" is a RooFit extension.
848 if (!c.node().is_container() && !c.node().is_null() && c.node().val() == "Const") {
849 out = ConstantGammas{};
850 } else if (auto type = readConstraintType(c)) {
851 out = *type;
852 }
853 }
854}
855
856} // namespace
857
858// The parameter of a modifier with a single parameter is given in "parameters", or in "parameter" in older documents.
859void readParameter(Reader &ar, ParameterRef &parameter)
860{
861 if (ar.has("parameters")) {
862 readSingleEntry(ar, "parameters", parameter);
863 } else {
864 ar("parameter", parameter);
865 }
866}
867
868// The modifiers. The standard doesn't require them to have a name, but RooFit uses it to name the objects it creates.
870{
871 ar.optional("name", m.name);
872 readParameter(ar, m.parameter);
873 readConstraint(ar, m.constraint);
874}
875
876void describe(Reader &ar, HistoSysVariation &variation)
877{
878 ar("contents", variation.contents);
879 ar("errors", variation.errors);
880}
881
882// A normsys or histosys modifier. They have the same components, with numbers or histograms as variations.
883template <class Modifier_t>
884void describeSys(Reader &ar, Modifier_t &m)
885{
886 ar.optional("name", m.name);
887 readParameter(ar, m.parameter);
888 ar.nested("data", [&](Reader &data) {
889 data("hi", m.hi);
890 data("lo", m.lo);
891 });
892 readConstraint(ar, m.constraint);
893 ar("interpolation", m.interpolation);
894}
895
897{
898 ar.optional("name", m.name);
899 if (ar.has("parameter")) {
900 // A single name stands for one parameter per bin, which are named after it.
901 ar("parameter", m.name);
902 }
903 ar.optional("parameters", m.parameters);
904 if (ar.has("data")) {
905 ar.nested("data", [&](Reader &data) { data.optional("vals", m.vals); });
906 }
907 readGammaConstraint(ar, m.constraint);
908}
909
911{
912 ar.optional("name", m.name);
913 ar.optional("parameters", m.parameters);
914 readGammaConstraint(ar, m.constraint);
915}
916
918{
919 ar("name", m.name);
920}
921
922void describe(Reader &ar, Modifier &out)
923{
924 std::string type;
925 ar("type", type);
926 if (type == "normfactor") {
927 describe(ar, out.emplace<NormFactor>());
928 } else if (type == "normsys") {
929 describeSys(ar, out.emplace<NormSys>());
930 } else if (type == "histosys") {
931 describeSys(ar, out.emplace<HistoSys>());
932 } else if (type == "shapesys" || type == "shapefactor") {
933 describe(ar, out.emplace<ShapeSys>());
934 } else if (type == "staterror") {
935 describe(ar, out.emplace<StatError>());
936 } else if (type == "custom") {
937 describe(ar, out.emplace<CustomModifier>());
938 } else if (ar.has("type")) {
939 std::string name;
940 if (ar.has("name")) {
941 ar("name", name);
942 }
943 ar.cursor().error("modifier '" + name + "' of unknown type '" + type + "'");
944 }
945}
946
947void describe(Reader &ar, Sample &sample)
948{
949 ar("name", sample.name);
950 ar.nested("data", [&](Reader &data) {
951 data("contents", sample.contents);
952 data("errors", sample.errors);
953 });
954 ar.optional("modifiers", sample.modifiers);
955}
956
958{
959 ar.optional("relThreshold", config.relThreshold);
960 readGammaConstraint(ar, config.constraint);
961 // Written by upgradeDocument() for older documents.
962 ar.optional("implied_poisson_errors", config.impliedPoissonErrors);
963}
964
966{
967 if (ar.has("axes") && ar.cursor().node()["axes"].is_seq()) {
968 for (JSONNode const &axis : ar.cursor().node()["axes"].children()) {
969 if (axis.is_map() && axis.has_child("categories")) {
970 ar.cursor().error("the axis '" + (axis.has_child("name") ? axis["name"].val() : std::string{}) +
971 "' is categorical, but the axes of a HistFactory prediction are binned");
972 return;
973 }
974 }
975 }
976 ar("axes", dist.axes);
977 ar("default_interpolation", dist.defaultInterpolation);
978 ar("samples", dist.samples);
979 if (ar.has("staterror")) {
980 readValue(ar.child("staterror"), dist.statError);
981 }
982}
983
984namespace {
985
986void readDistribution(Cursor const &c, Distribution &out, ParseOptions const &options)
987{
988 if (!c.node().is_map()) {
989 c.error("must be a struct");
990 return;
991 }
992 Reader ar{c};
993 ar("name", out.name);
994 std::string type;
995 ar("type", type);
996 if (options.typedHistFactory && type == "histfactory_dist") {
997 describe(ar, out.body.emplace<HistFactoryDist>());
998 } else if (type == "combined_dist") {
999 std::string const context = "the combined_dist '" + out.name + "'";
1000 auto &combined = out.body.emplace<CombinedDist>();
1001 if (ar.has("extended")) {
1002 c.error(context + " must not have \"extended\": whether it is extended follows from its components");
1003 } else {
1004 readCombined(ar, context, "dist", combined);
1005 }
1006 } else {
1007 out.body = GenericObject{type, &c.node()};
1008 // The importers read the other components.
1009 ar.ignoreUnread();
1010 }
1011}
1012
1013} // namespace
1014
1015void describe(Reader &ar, Function &out)
1016{
1017 ar("name", out.name);
1018 ar("type", out.body.type);
1019 out.body.node = &ar.cursor().node();
1020 // The importers read the other components.
1021 ar.ignoreUnread();
1022}
1023
1024void describe(Reader &ar, Attributes &attributes)
1025{
1026 if (ar.has("dict")) {
1027 ar("dict", attributes.dict);
1028 }
1029 ar.optional("tags", attributes.tags);
1030 if (ar.has("is_const_var")) {
1031 int isConstVar = 0;
1032 ar("is_const_var", isConstVar);
1033 attributes.isConstVar = isConstVar == 1;
1034 }
1035}
1036
1038{
1039 ar("index_cat", info.indexCat);
1040 ar("labels", info.labels);
1041 ar("indices", info.indices);
1042 ar("distributions", info.distributions);
1043 if (info.labels.size() != info.indices.size() || info.labels.size() != info.distributions.size()) {
1044 ar.cursor().error("mismatch in number of labels, indices and distributions!");
1045 }
1046}
1047
1049{
1050 ar("index_cat", info.indexCat);
1051 ar("labels", info.labels);
1052 ar("indices", info.indices);
1053 if (info.labels.size() != info.indices.size()) {
1054 ar.cursor().error("mismatch in number of indices and labels!");
1055 }
1056}
1057
1059{
1060 ar("mcName", info.mcName);
1061 ar("pdfName", info.pdfName);
1062 ar("combined_data_name", info.combinedDataName);
1063 ar("observables", info.observables);
1064}
1065
1066namespace {
1067
1068// The content of "misc" is not supposed to change the result of evaluating the model, so problems with the step widths
1069// are only warnings.
1070void readStepWidths(Cursor const &misc, std::vector<StepWidth> &out)
1071{
1072 JSONNode const *node = misc.node().find("minimization", "parameter_stepwidths");
1073 if (!node) {
1074 return;
1075 }
1076 if (!node->is_seq()) {
1077 misc.warning("minimization.parameter_stepwidths is not a sequence, skipping.");
1078 return;
1079 }
1080 for (JSONNode const &item : node->children()) {
1081 if (!item.is_map() || !item.has_child("name") || !item.has_child("step_width") ||
1082 !item["step_width"].is_number() || item["name"].is_container() || item["name"].is_null()) {
1083 misc.warning("skipping malformed parameter_stepwidths entry.");
1084 continue;
1085 }
1086 out.push_back({item["name"].val(), item["step_width"].val_double()});
1087 }
1088}
1089
1090void readMisc(Cursor const &root, RooFitInternal &out)
1091{
1092 if (!root.node().has_child("misc")) {
1093 return;
1094 }
1095 Cursor misc{root.node()["misc"], root, "misc"};
1096 if (!misc.node().is_map()) {
1097 return;
1098 }
1099 readStepWidths(misc, out.stepWidths);
1100 if (!misc.node().has_child("ROOT_internal")) {
1101 return;
1102 }
1103 Cursor internal{misc.node()["ROOT_internal"], misc, "ROOT_internal"};
1104 if (!internal.node().is_map()) {
1105 return;
1106 }
1107 Reader ar{internal};
1108 ar.optional("attributes", out.attributes);
1109 ar.optional("combined_distributions", out.combinedDistributions);
1110 ar.optional("combined_datasets", out.combinedDatasets);
1111 ar.optional("ModelConfigs", out.modelConfigs);
1112 ar.optional("histfactory_constraints", out.channelConstraints);
1113 ar.optional("auxiliary_channels", out.auxiliaryChannels);
1114 ar.optional("bookkeeping_columns", out.bookkeepingColumns);
1115}
1116
1117void readMetadata(Cursor const &root, Metadata &out)
1118{
1119 JSONNode const *metadata = root.node().find("metadata");
1120 JSONNode const *versionNode = metadata ? metadata->find("hs3_version") : nullptr;
1121 if (!versionNode) {
1122 std::stringstream ss;
1123 ss << "The HS3 version is missing in the JSON!\n"
1124 << "Please include the HS3 version in the metadata field, e.g.:\n"
1125 << " \"metadata\" :\n"
1126 << " {\n"
1127 << " \"hs3_version\" : \"" << supportedVersion << "\"\n"
1128 << " }";
1129 root.error(ss.str());
1130 return;
1131 }
1132 Cursor metadataCursor{*metadata, root, "metadata"};
1133 Cursor versionCursor{*versionNode, metadataCursor, "hs3_version"};
1134 if (versionNode->is_container() || versionNode->is_null() || versionNode->is_number()) {
1135 versionCursor.error(std::string("The HS3 version in the metadata must be a string, e.g. \"") + supportedVersion +
1136 "\"");
1137 return;
1138 }
1139 out.hs3Version = versionNode->val();
1140
1141 Version supported;
1142 parseVersion(supportedVersion, supported);
1143 if (!parseVersion(out.hs3Version, out.version)) {
1144 versionCursor.warning("cannot parse hs3_version '" + out.hs3Version +
1145 "', expected a string of the form \"MAJOR.MINOR\".");
1146 } else if (supported < Version{out.version.major, out.version.minor}) {
1147 versionCursor.warning("the document has hs3_version '" + out.hs3Version +
1148 "', which is newer than the version supported by this RooFit ('" + supportedVersion +
1149 "'). Some content might not be imported correctly.");
1150 }
1151
1152 Reader ar{metadataCursor};
1153 ar.has("hs3_version");
1154 ar.optional("packages", out.packages);
1155 ar.optional("authors", out.authors);
1156 ar.optional("publications", out.publications);
1157 ar("description", out.description);
1158}
1159
1160} // namespace
1161
1162HS3Document parseDocument(JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options)
1163{
1164 HS3Document doc;
1165 Cursor cursor{root, diagnostics};
1166 readMetadata(cursor, doc.metadata);
1167 if (hasErrors(diagnostics)) {
1168 return doc;
1169 }
1170
1171 Reader ar{cursor};
1172 ar.has("metadata");
1173 ar.has("misc");
1174 if (ar.has("distributions") && !root["distributions"].is_null()) {
1175 Cursor distributions{root["distributions"], cursor, "distributions"};
1176 if (!distributions.node().is_seq()) {
1177 distributions.error("must be an array");
1178 } else {
1179 std::size_t i = 0;
1180 doc.distributions.reserve(distributions.node().num_children());
1181 for (JSONNode const &item : distributions.node().children()) {
1182 doc.distributions.emplace_back();
1183 readDistribution(Cursor{item, distributions, i}, doc.distributions.back(), options);
1184 ++i;
1185 }
1186 }
1187 }
1188 ar.optional("functions", doc.functions);
1189 ar.optional("data", doc.data);
1190 ar.optional("likelihoods", doc.likelihoods);
1191 ar.optional("domains", doc.domains);
1192 ar.optional("parameter_points", doc.parameterPoints);
1193 ar.optional("analyses", doc.analyses);
1194 readMisc(cursor, doc.rooFit);
1195 return doc;
1196}
1197
1199{
1200 HistFactoryDist out;
1201 Cursor cursor{distribution, diagnostics};
1202 if (!distribution.is_map()) {
1203 cursor.error("must be a struct");
1204 return out;
1205 }
1206 Reader ar{cursor};
1207 ar.has("name");
1208 ar.has("type");
1209 describe(ar, out);
1210 return out;
1211}
1212
1213void readValue(Cursor const &c, NameOrNumber &out)
1214{
1215 if (c.node().is_number()) {
1216 out = c.node().val_double();
1217 } else {
1218 readValue(c, out.emplace<std::string>());
1219 }
1220}
1221
1222std::optional<Interpolation> readInterpolationCode(Cursor const &c, InterpolationClass interpolationClass)
1223{
1224 JSONNode const &node = c.node();
1225 if (node.is_container() || node.is_null() || !node.has_val()) {
1226 c.error("must be a structured interpolation or a legacy integer code");
1227 return std::nullopt;
1228 }
1229 const std::string value = node.val();
1230 int code = 0;
1231 const auto result = std::from_chars(value.data(), value.data() + value.size(), code);
1232 if (result.ec != std::errc{} || result.ptr != value.data() + value.size()) {
1233 c.error("has invalid legacy interpolation code '" + value + "'");
1234 return std::nullopt;
1235 }
1236 auto interpolation = interpolationFromCode(code, interpolationClass);
1237 if (!interpolation) {
1238 c.error(std::string("has unsupported ") +
1239 (interpolationClass == InterpolationClass::Piecewise ? "PiecewiseInterpolation" : "FlexibleInterpVar") +
1240 " code " + value);
1241 }
1242 return interpolation;
1243}
1244
1245namespace {
1246
1247// The "interpolations" of the functions "interpolation" and "interpolation0d": one for all parameters, or one per
1248// parameter. Older versions of RooFit wrote integer "interpolationCodes". Without both, the interpolation is linear.
1249void readInterpolations(Reader &ar, std::size_t nParameters, InterpolationClass interpolationClass,
1250 std::vector<Interpolation> &out)
1251{
1252 out.assign(nParameters, additivePiecewiseLinear);
1253 if (ar.has("interpolations")) {
1254 Cursor c = ar.child("interpolations");
1255 const std::size_t errorsBefore = c.numErrors();
1256 std::vector<Interpolation> interpolations;
1257 readValue(c, interpolations);
1258 const std::size_t size = interpolations.size();
1259 if (c.numErrors() != errorsBefore) {
1260 return;
1261 }
1262 if (nParameters == 0 ? size != 0 : size != 1 && size != nParameters) {
1263 c.error("component 'interpolations' must contain either one descriptor or one descriptor per parameter (got " +
1264 std::to_string(size) + " for " + std::to_string(nParameters) + " parameters)");
1265 } else if (size == 1) {
1266 out.assign(nParameters, interpolations.front());
1267 } else {
1268 out = std::move(interpolations);
1269 }
1270 return;
1271 }
1272 if (!ar.has("interpolationCodes")) {
1273 return;
1274 }
1275 Cursor c = ar.child("interpolationCodes");
1276 if (!c.node().is_seq() || c.node().num_children() != nParameters) {
1277 c.error("legacy component 'interpolationCodes' must contain one code per parameter (got " +
1278 std::to_string(c.node().is_seq() ? c.node().num_children() : 0) + " for " + std::to_string(nParameters) +
1279 " parameters)");
1280 return;
1281 }
1282 std::size_t i = 0;
1283 for (JSONNode const &child : c.node().children()) {
1284 if (auto interpolation = readInterpolationCode(Cursor{child, c, i}, interpolationClass)) {
1285 out[i] = *interpolation;
1286 }
1287 ++i;
1288 }
1289}
1290
1291// The functions "interpolation" and "interpolation0d" have the variations "high" and "low" for each of the "vars".
1292template <class Function_t, class Fn>
1293Function_t parseInterpolated(JSONNode const &node, Diagnostics &diagnostics, InterpolationClass interpolationClass,
1294 Fn &&readOthers)
1295{
1296 Function_t out;
1297 Cursor cursor{node, diagnostics};
1298 if (!node.is_map()) {
1299 cursor.error("must be a struct");
1300 return out;
1301 }
1302 const std::size_t errorsBefore = cursor.numErrors();
1303 Reader ar{cursor};
1304 ar.has("name");
1305 ar.has("type");
1306 ar("nom", out.nominal);
1307 ar("vars", out.parameters);
1308 ar("high", out.high);
1309 ar("low", out.low);
1310 readOthers(ar, out);
1311 if (cursor.numErrors() == errorsBefore &&
1312 (out.high.size() != out.parameters.size() || out.low.size() != out.parameters.size())) {
1313 cursor.error("has non-matching lengths of 'vars', 'high' and 'low'");
1314 }
1315 readInterpolations(ar, out.parameters.size(), interpolationClass, out.interpolations);
1316 return out;
1317}
1318
1319} // namespace
1320
1321Distribution parseDistribution(JSONNode const &distribution, Diagnostics &diagnostics)
1322{
1323 Distribution out;
1324 readDistribution(Cursor{distribution, diagnostics}, out, {});
1325 return out;
1326}
1327
1329{
1330 Data out;
1331 readValue(Cursor{data, diagnostics}, out);
1332 return out;
1333}
1334
1335std::vector<BinnedAxis> parseBinnedAxes(JSONNode const &axes, Diagnostics &diagnostics)
1336{
1337 std::vector<BinnedAxis> out;
1338 readValue(Cursor{axes, diagnostics}, out);
1339 return out;
1340}
1341
1343{
1344 return parseInterpolated<InterpolatedFunction>(
1345 node, diagnostics, InterpolationClass::Piecewise,
1346 [](Reader &ar, InterpolatedFunction &out) { ar("positiveDefinite", out.positiveDefinite); });
1347}
1348
1350{
1351 return parseInterpolated<InterpolatedNumber>(node, diagnostics, InterpolationClass::Flexible,
1352 [](Reader &, InterpolatedNumber &) {});
1353}
1354
1355} // namespace HS3
1356} // namespace RooFit
Interpolation descriptor
Definition HS3Parser.cxx:37
int piecewise
Definition HS3Parser.cxx:38
int flexible
Definition HS3Parser.cxx:39
std::string message
#define d(i)
Definition RSha256.hxx:102
#define c(i)
Definition RSha256.hxx:101
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t cursor
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 result
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 GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
XID Cursor
Definition TGX11.h:36
std::string val() const
JSONNode const * find(std::string const &key) const
The component at the end of a path of keys, or nullptr if a component on the path doesn't exist.
bool has_child(std::string const &) const
JSONNode & child(size_t pos)
An HS3 document. It is move-only, because generic objects refer to the JSON tree of the document.
std::vector< HS3::Analysis > analyses
std::vector< HS3::Data > data
HS3::Metadata metadata
std::vector< HS3::Likelihood > likelihoods
std::vector< HS3::Function > functions
std::vector< HS3::ParameterPoint > parameterPoints
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
std::vector< HS3::Distribution > distributions
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
Detail::JSONNode const & node() const
Definition HS3Schema.h:43
std::string path() const
Definition HS3Parser.cxx:98
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
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 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
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.
HistFactoryDist parseHistFactoryDist(Detail::JSONNode const &distribution, Diagnostics &diagnostics)
Reads a HistFactory distribution that doesn't come with a document.
std::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
constexpr auto supportedVersion
Definition HS3Parser.cxx:27
Distribution parseDistribution(Detail::JSONNode const &distribution, Diagnostics &diagnostics)
Reads a distribution that doesn't come with a document.
void describeSys(Reader &ar, Modifier_t &m)
InterpolationClass
The RooFit classes that implement the interpolations, which encode them with integer codes.
constexpr Interpolation multiplicativePolynomialExponential
void readValue(Cursor const &c, std::string &out)
constexpr Interpolation additivePolynomialLinear
void readParameter(Reader &ar, ParameterRef &parameter)
void describe(Reader &ar, Package &p)
ConstraintType
The constraint types that older documents named, for which RooFit creates the constraint terms.
std::optional< Interpolation > interpolationFromCode(int code, InterpolationClass interpolationClass)
The interpolation for an integer code, or nothing if the class doesn't support the code.
Definition HS3Parser.cxx:62
char const * toString(InterpolationType type)
InterpolatedNumber parseInterpolatedNumber(Detail::JSONNode const &node, Diagnostics &diagnostics)
std::optional< Interpolation > readInterpolationCode(Cursor const &c, InterpolationClass interpolationClass)
Reads an integer interpolation code, as older versions of RooFit wrote them.
InterpolatedFunction parseInterpolatedFunction(Detail::JSONNode const &node, Diagnostics &diagnostics)
Reads the functions "interpolation" and "interpolation0d", which don't come with a document.
void readCombined(Reader &ar, std::string const &context, const char *refKey, Combined< Ref_t > &out)
std::optional< int > codeFromInterpolation(Interpolation const &interpolation, InterpolationClass interpolationClass)
The integer code of an interpolation, or nothing if the class can't represent the interpolation.
Definition HS3Parser.cxx:81
constexpr Interpolation multiplicativePolynomialLinear
bool hasErrors(Diagnostics const &diagnostics)
Whether there is a diagnostic with error severity.
Definition HS3Parser.cxx:92
constexpr Interpolation multiplicativePiecewiseExponential
std::vector< Diagnostic > Diagnostics
std::variant< Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs > GammaConstraint
bool parseVersion(std::string const &version, Version &out)
Reads the leading "MAJOR.MINOR" and the optional ".PATCH" of an HS3 version string.
std::variant< Unconstrained, ConstraintType, ConstraintPdf > Constraint
Data parseData(Detail::JSONNode const &data, Diagnostics &diagnostics)
Reads a data set that doesn't come with a document.
std::vector< BinnedAxis > parseBinnedAxes(Detail::JSONNode const &axes, Diagnostics &diagnostics)
Reads the axes of binned data that doesn't come with a document.
constexpr Interpolation additiveQuadraticLinear
constexpr Interpolation additivePiecewiseLinear
HS3Document parseDocument(Detail::JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options={})
Reads a document.
Combined< DistributionRef > CombinedDist
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
std::optional< DistributionRef > prior
std::optional< ParameterPointRef > init
LikelihoodRef likelihood
std::vector< DomainRef > domains
std::vector< ParameterRef > parametersOfInterest
bool isConstVar
How ROOT versions before HS3 0.3 marked named RooConstVars.
std::vector< std::string > tags
std::map< std::string, std::string > dict
The axis of binned data.
std::vector< std::string > categories
The data set or distribution that a combined data set or distribution selects for one key,...
std::vector< std::string > key
The data set of a RooSimultaneous as older versions of RooFit wrote it. It is only read.
std::vector< int > indices
std::vector< std::string > labels
A RooSimultaneous as older versions of RooFit wrote it, before HS3 had combined distributions....
std::vector< std::string > labels
std::vector< DistributionRef > distributions
Data sets or distributions that are selected by a key (HS3 #119). The columns are categorical axes of...
std::vector< CombinedComponent< Ref_t > > components
std::vector< std::string > columns
The axis of point data or unbinned data, which can't have a binning.
std::optional< double > max
std::optional< double > min
bool hasFixedValue
Whether the axis has a fixed "value", which is only allowed in domains.
Multiplies the sample with a function of the document.
std::string name
std::variant< PointData, UnbinnedData, BinnedData, CombinedData > body
The axis of a domain.
std::optional< double > min
std::optional< double > max
std::optional< Binning > binning
std::optional< double > fixedValue
std::vector< std::variant< DomainAxis, CategoricalAxis > > axes
std::vector< double > edges
std::vector< std::vector< double > > correlation
Empty if there is no correlation.
std::vector< double > sigma
Detail::JSONNode const * node
The object in the JSON tree that the document was read from.
std::vector< double > contents
std::optional< std::vector< double > > errors
Not in the standard, but older versions of RooFit read them.
The function "interpolation" of RooFit, implemented by PiecewiseInterpolation: a nominal function plu...
The function "interpolation0d" of RooFit, implemented by FlexibleInterpVar: a nominal value plus the ...
How a modifier of a HistFactory distribution interpolates and extrapolates its effect.
InterpolationType type
InterpolationFunction in
std::optional< InterpolationFunction > out
If not set, the function inside the bounds is used outside as well.
The data of a likelihood term: a data set, a number given in-line, or none (std::monostate) in a pure...
DistributionRef distribution
std::variant< DataRef, double, std::monostate > data
bool pureModel
Whether "data" is null: the likelihood only groups the distributions of a model without data (e....
std::vector< DistributionRef > auxDistributions
std::vector< LikelihoodTerm > terms
std::optional< std::string > pdfName
std::optional< std::vector< std::string > > observables
The observables of the ModelConfig, written if no data set of the likelihood gives them.
std::optional< std::string > mcName
std::optional< std::string > combinedDataName
std::vector< ParameterValue > parameters
std::optional< std::variant< double, std::string > > value
A string selects the state of a category.
std::optional< int > nbins
std::optional< double > error
std::optional< double > relError
Options for upgradeDocument() and parseDocument().
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::vector< double > contents
std::optional< std::vector< double > > errors
std::vector< Modifier > modifiers
Scales each bin by its own parameter. The modifier type "shapesys" is a deprecated alias of "shapefac...
Settings of the staterror modifiers that apply to the whole channel. This is a RooFit extension.
bool impliedPoissonErrors
Samples without "errors" have Poisson uncertainties, as in documents before HS3 0....
Scales each bin by a parameter, that is constrained by the statistical uncertainty of all samples tha...
TMarker m
Definition textangle.C:8