Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Document.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_HS3Document_h
14#define RooFit_HS3Document_h
15
16#include <functional>
17#include <map>
18#include <memory>
19#include <optional>
20#include <set>
21#include <string>
22#include <tuple>
23#include <variant>
24#include <vector>
25
26namespace RooFit {
27
28namespace Detail {
29class JSONNode;
30class JSONTree;
31} // namespace Detail
32
33/// Types that represent an HS3 document. They follow the structure of the HS3 standard, and this header must not depend
34/// on RooFit: creating RooFit objects from a document is a separate step.
35namespace HS3 {
36
37struct Version {
38 int major = 0;
39 int minor = 0;
40 int patch = 0;
41};
42
43inline bool operator<(Version const &l, Version const &r)
44{
45 return std::tie(l.major, l.minor, l.patch) < std::tie(r.major, r.minor, r.patch);
46}
47
48/// Combines lambdas into one visitor for std::visit(). The compiler complains about the alternatives without lambda.
49template <class... Ts>
50struct overloaded : Ts... {
51 using Ts::operator()...;
52};
53template <class... Ts>
55
56/// The name of the alternative that a variant holds, e.g. of an axis or of a modifier.
57template <class... Ts>
58std::string const &nameOf(std::variant<Ts...> const &v)
59{
60 return std::visit([](auto const &alternative) -> std::string const & { return alternative.name; }, v);
61}
62
63/// The object with the name `name` among the objects of a section, e.g. `findByName(doc.data, "obsData")`, or nullptr.
64template <class Objects_t>
65auto findByName(Objects_t &objects, std::string const &name) -> decltype(&*objects.begin())
66{
67 for (auto &object : objects) {
68 if (object.name == name) {
69 return &object;
70 }
71 }
72 return nullptr;
73}
74
75/// The body of the object `name` if it is a `Body_t`, e.g. `find<CombinedData>(doc.data, "obsData")`, or nullptr.
76template <class Body_t, class Objects_t>
77auto find(Objects_t &objects, std::string const &name) -> decltype(std::get_if<Body_t>(&objects.begin()->body))
78{
79 auto *object = findByName(objects, name);
80 return object ? std::get_if<Body_t>(&object->body) : nullptr;
81}
82
83/// A name that says what it refers to, so that for example a DataRef can't be used as a DomainRef.
84template <class Tag>
85struct Ref {
86 std::string name;
87};
88
95
96struct Package {
97 std::string name;
98 std::string version;
99};
100
101struct Metadata {
102 std::string hs3Version; ///< As it was written in the document, empty for fragments of a document.
103 Version version; ///< The parsed version. Versions that can't be parsed count as the oldest one.
104 std::vector<Package> packages;
105 std::vector<std::string> authors;
106 std::vector<std::string> publications;
107 std::optional<std::string> description;
108};
109
111 int nbins = 0;
112};
114 std::vector<double> edges;
115};
116using Binning = std::variant<RegularBinning, EdgesBinning>;
117
118/// The axis of a domain. Infinite bounds are written as null, and bounds can be missing in older documents. The binning
119/// is a RooFit extension. A fixed axis has a single value instead, which excludes bounds and a binning.
121 std::string name;
122 std::optional<double> fixedValue;
123 std::optional<double> min;
124 std::optional<double> max;
125 std::optional<Binning> binning;
126};
127
128/// The axis of point data or unbinned data, which can't have a binning.
130 std::string name;
131 std::optional<double> min;
132 std::optional<double> max;
133 /// Whether the axis has a fixed "value", which is only allowed in domains. Older versions of RooFit wrote it for
134 /// the constant observables of data sets. The value is not used.
135 bool hasFixedValue = false;
136};
137
138/// The axis of binned data.
140 std::string name;
141 double min = 0.0;
142 double max = 0.0;
144};
145
147 std::string name;
148 std::vector<std::string> categories;
149};
150
151struct Domain {
152 std::string name;
153 std::vector<std::variant<DomainAxis, CategoricalAxis>> axes;
154};
155
156/// The domain in which RooFit writes the ranges of all variables. It is a convention of RooFit, not of the standard.
157inline constexpr char const *defaultDomainName = "default_domain";
158
159/// Where RooFit takes the ranges of variables from: the "default_domain" if there is one, otherwise the first domain by
160/// name that has the variable. The domains must outlive this object.
162public:
163 explicit DomainRanges(std::vector<Domain> const &domains);
164 /// The axis that defines the range of a variable, or null.
165 DomainAxis const *find(std::string const &name) const;
166 /// Whether any domain has the variable.
167 bool contains(std::string const &name) const { return _names.count(name); }
168
169private:
170 std::map<std::string, DomainAxis const *> _ranges;
171 std::set<std::string> _names;
172};
173
175 std::string name;
176 std::optional<std::variant<double, std::string>> value; ///< A string selects the state of a category.
177 bool isConst = false;
178 // RooFit extensions that were written by older versions:
179 std::optional<double> error;
180 std::optional<double> relError;
181 std::optional<int> nbins;
182};
183
185 std::string name;
186 std::vector<ParameterValue> parameters;
187};
188
190 std::vector<double> sigma;
191 std::vector<std::vector<double>> correlation; ///< Empty if there is no correlation.
192};
193
194struct PointData {
195 double value = 0.0;
196 std::optional<double> uncertainty;
197 std::vector<ContinuousAxis> axes;
198};
199
201 std::vector<std::variant<ContinuousAxis, CategoricalAxis>> axes;
202 /// The coordinates, row by row. A value on a categorical axis is the index in the list of categories of that axis.
203 std::vector<double> entries;
204 std::optional<std::vector<double>> weights;
206
207 std::size_t numEntries() const { return axes.empty() ? 0 : entries.size() / axes.size(); }
208};
209
211 std::vector<std::variant<BinnedAxis, CategoricalAxis>> axes;
212 std::vector<double> contents;
213 std::optional<GaussianUncertainty> uncertainty;
214};
215
216/// The data set or distribution that a combined data set or distribution selects for one key, which has one label per
217/// column.
218template <class Ref_t>
220 std::vector<std::string> key;
221 Ref_t ref;
222};
223
224/// Data sets or distributions that are selected by a key (HS3 #119). The columns are categorical axes of the domains.
225template <class Ref_t>
226struct Combined {
227 std::vector<std::string> columns;
228 std::vector<CombinedComponent<Ref_t>> components;
229
230 /// The component for a key, or nullptr.
231 CombinedComponent<Ref_t> const *find(std::vector<std::string> const &key) const
232 {
233 for (auto const &component : components) {
234 if (component.key == key) {
235 return &component;
236 }
237 }
238 return nullptr;
239 }
240};
241
244
245struct Data {
246 std::string name;
247 std::variant<PointData, UnbinnedData, BinnedData, CombinedData> body;
248
249 /// The number of factors that a distribution paired with the data contributes to a likelihood: the number of
250 /// entries of unbinned data, and one for point data and binned data. Combined data gives one as well, although
251 /// it is its components that are paired with distributions.
252 std::size_t numEntries() const
253 {
254 auto const *unbinned = std::get_if<UnbinnedData>(&body);
255 return unbinned ? unbinned->numEntries() : 1;
256 }
257};
258
259/// The data of a likelihood term: a data set, a number given in-line, or none (std::monostate) in a pure model.
262 std::variant<DataRef, double, std::monostate> data;
263};
264
266 std::string name;
267 std::vector<LikelihoodTerm> terms;
268 std::vector<DistributionRef> auxDistributions;
269 /// Whether "data" is null: the likelihood only groups the distributions of a model without data (e.g. for toy
270 /// generation), and it can't be evaluated before a data set is created.
271 bool pureModel = false;
272};
273
274struct Analysis {
275 std::string name;
277 std::vector<ParameterRef> parametersOfInterest;
278 std::vector<DomainRef> domains;
279 std::optional<ParameterPointRef> init;
280 std::optional<DistributionRef> prior;
281};
282
283/// A distribution or function that is not interpreted by the document, but by the importers that are registered for
284/// its type in RooFit::JSONIO::importers().
286 std::string type;
287 /// The object in the JSON tree that the document was read from. In a document that is exported, the distributions
288 /// refer to what the exporters wrote once all of them are written, and the functions don't have a node.
289 Detail::JSONNode const *node = nullptr;
290};
291
293 Add,
294 Mult
295};
296
298 Poly1,
299 Poly2,
300 Poly6,
301 Exp
302};
303
304/// How a modifier of a HistFactory distribution interpolates and extrapolates its effect.
308 std::optional<InterpolationFunction> out; ///< If not set, the function inside the bounds is used outside as well.
309};
310
311inline bool operator==(Interpolation const &l, Interpolation const &r)
312{
313 return l.type == r.type && l.in == r.in && l.out == r.out;
314}
315inline bool operator!=(Interpolation const &l, Interpolation const &r)
316{
317 return !(l == r);
318}
319inline bool operator<(Interpolation const &l, Interpolation const &r)
320{
321 return std::tie(l.type, l.in, l.out) < std::tie(r.type, r.in, r.out);
322}
323
326inline constexpr Interpolation multiplicativePiecewiseExponential{InterpolationType::Mult, InterpolationFunction::Exp,
327 std::nullopt};
336
337char const *toString(InterpolationType type);
338char const *toString(InterpolationFunction function);
339
340/// The RooFit classes that implement the interpolations, which encode them with integer codes.
342 Piecewise,
344};
345
346/// The interpolation for an integer code, or nothing if the class doesn't support the code.
347std::optional<Interpolation> interpolationFromCode(int code, InterpolationClass interpolationClass);
348/// The integer code of an interpolation, or nothing if the class can't represent the interpolation.
349std::optional<int> codeFromInterpolation(Interpolation const &interpolation, InterpolationClass interpolationClass);
350
351/// The constraint types that older documents named, for which RooFit creates the constraint terms.
352enum class ConstraintType {
353 Gauss,
354 Poisson,
356};
357
358char const *toString(ConstraintType type);
359
360/// The parameter of the modifier is not constrained.
362
363/// The constraint term is a distribution of the document, named in "constraints".
366 /// Named in "constraint_name" by an older document, whose constraint distributions of normsys and histosys
367 /// modifiers may use the global observable "nom_<parameter>" without defining it, because RooFit created it.
368 bool legacyName = false;
369};
370
371using Constraint = std::variant<Unconstrained, ConstraintType, ConstraintPdf>;
372
374{
375 return true;
376}
377inline bool operator==(ConstraintPdf const &l, ConstraintPdf const &r)
378{
379 return l.pdf.name == r.pdf.name;
380}
381
387
388struct NormSys {
389 std::string name;
391 double hi = 1.0;
392 double lo = 1.0;
394 std::optional<Interpolation> interpolation;
395};
396
398 std::vector<double> contents;
399 std::optional<std::vector<double>> errors; ///< Not in the standard, but older versions of RooFit read them.
400};
401
410
411/// The gamma parameters are constant, which is a RooFit extension.
413
414/// Every gamma parameter has its own constraint term, or none, as named in "constraints".
416 std::vector<std::optional<DistributionRef>> pdfs;
417};
418
419using GammaConstraint = std::variant<Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs>;
420
421/// Scales each bin by its own parameter. The modifier type "shapesys" is a deprecated alias of "shapefactor".
422struct ShapeSys {
423 std::string name;
424 std::vector<ParameterRef> parameters; ///< If empty, the parameters are named after the modifier.
425 std::vector<double> vals;
427};
428
429/// Scales each bin by a parameter, that is constrained by the statistical uncertainty of all samples that have this
430/// modifier.
431struct StatError {
432 std::string name;
433 std::vector<ParameterRef> parameters; ///< If empty, the parameters are named after the channel.
435};
436
437/// Multiplies the sample with a function of the document.
439 std::string name;
440};
441
442using Modifier = std::variant<NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier>;
443
444/// The type of a modifier, as the standard names it.
445char const *typeOf(Modifier const &modifier);
446
447struct Sample {
448 std::string name;
449 std::vector<double> contents;
450 std::optional<std::vector<double>> errors;
451 std::vector<Modifier> modifiers;
452};
453
454/// Settings of the staterror modifiers that apply to the whole channel. This is a RooFit extension.
456 double relThreshold = 0.0;
458 /// Samples without "errors" have Poisson uncertainties, as in documents before HS3 0.3.
460};
461
463 std::vector<BinnedAxis> axes;
464 std::optional<Interpolation> defaultInterpolation;
465 std::vector<Sample> samples;
467};
468
470 std::string name;
471 std::variant<GenericObject, HistFactoryDist, CombinedDist> body;
472};
473
474struct Function {
475 std::string name;
477};
478
479/// A name, or a number for a constant.
480using NameOrNumber = std::variant<std::string, double>;
481
482/// The function "interpolation" of RooFit, implemented by PiecewiseInterpolation: a nominal function plus the
483/// interpolated differences to the variations "high" and "low" for each parameter.
485 std::string nominal;
486 std::vector<NameOrNumber> parameters;
487 std::vector<NameOrNumber> high;
488 std::vector<NameOrNumber> low;
489 std::vector<Interpolation> interpolations; ///< One per parameter.
490 bool positiveDefinite = false;
491};
492
493/// The function "interpolation0d" of RooFit, implemented by FlexibleInterpVar: a nominal value plus the interpolated
494/// differences to the variations "high" and "low" for each parameter.
496 double nominal = 0.0;
497 std::vector<std::string> parameters;
498 std::vector<double> high;
499 std::vector<double> low;
500 std::vector<Interpolation> interpolations; ///< One per parameter.
501};
502
503// The following types are stored in the "misc" section, for which the HS3 standard doesn't prescribe a format.
504
506 std::map<std::string, std::string> dict;
507 std::vector<std::string> tags;
508 bool isConstVar = false; ///< How ROOT versions before HS3 0.3 marked named RooConstVars.
509};
510
511/// A RooSimultaneous as older versions of RooFit wrote it, before HS3 had combined distributions. It is only read.
513 std::string indexCat;
514 std::vector<std::string> labels;
515 std::vector<int> indices;
516 std::vector<DistributionRef> distributions;
517};
518
519/// The data set of a RooSimultaneous as older versions of RooFit wrote it. It is only read.
521 std::string indexCat;
522 std::vector<std::string> labels;
523 std::vector<int> indices;
524};
525
527 std::optional<std::string> mcName;
528 std::optional<std::string> pdfName;
529 std::optional<std::string> combinedDataName;
530 /// The observables of the ModelConfig, written if no data set of the likelihood gives them.
531 std::optional<std::vector<std::string>> observables;
532};
533
534struct StepWidth {
535 std::string name;
536 double width = 0.0;
537};
538
539/// The content of misc.ROOT_internal and misc.minimization.
541 std::map<std::string, Attributes> attributes;
542 std::map<std::string, CombinedDistribution> combinedDistributions;
543 std::map<std::string, CombinedDataset> combinedDatasets;
544 std::map<std::string, ModelConfigInfo> modelConfigs;
545 std::vector<StepWidth> stepWidths;
546 /// The constraint terms of HistFactory distributions that no modifier names, by distribution. They are
547 /// also listed in the "aux_distributions" of the likelihoods, which is what other implementations use.
548 std::map<std::string, std::vector<DistributionRef>> channelConstraints;
549 /// The components of a combined distribution that are auxiliary measurements (constraint terms paired with the
550 /// data set of their observed values), by combined distribution. RooFit can read them back as constraint terms.
551 std::map<std::string, std::vector<DistributionRef>> auxiliaryChannels;
552 /// Axes that the components of a combined data set share, but that are no variate of any distribution, e.g. an
553 /// event number, by combined data set. They have no statistical meaning, and may be shared.
554 std::map<std::string, std::vector<std::string>> bookkeepingColumns;
555};
556
558 enum class Severity {
559 Warning,
560 Error
561 };
563 std::string path; ///< Where in the document, for example "distributions[3](pdf).x".
564 std::string message;
565};
566
567using Diagnostics = std::vector<Diagnostic>;
568
569/// Whether there is a diagnostic with error severity.
570bool hasErrors(Diagnostics const &diagnostics);
571
572} // namespace HS3
573
574class HS3Document;
576
577namespace HS3 {
578struct ValidateOptions;
579std::optional<ValidatedHS3Document>
580validate(HS3Document doc, Diagnostics &diagnostics, ValidateOptions const &options);
581
582/// What readAuxiliaryChannelsAsConstraints() changed.
584 std::set<std::string> terms; ///< The components that are no longer components of their combined distribution.
585 std::set<std::string> data; ///< Their data sets, which hold the observed values of global observables.
586 std::vector<std::string> warnings;
587};
588
589/// RooFit writes an array of constraints with the data set of its observed values as a component of the combined
590/// distribution of the model (RooJSONFactoryWSTool::Config::exportConstraintData), and marks it in
591/// misc.ROOT_internal.auxiliary_channels. Unless these components are to stay components (`asChannels`), the
592/// document is changed to what RooFit wrote before: the component and its data set become a term of every likelihood
593/// that has the combined distribution, which is then read as a constraint term with global observables. The label of
594/// the component is taken out of the index category.
595///
596/// The same is done for a document that lists a distribution twice: as a component of a combined distribution whose
597/// data set has no entries for it, and as a term of its own with data. It contributes once, as the term, but a
598/// component without data is not a constraint term for RooFit, which would leave it out of the likelihood. Such a
599/// document is read like the ones above, with a warning. If the components stay components, the data of the term
600/// becomes the data of the component instead, and the term is removed.
602
603/// A number given in-line in the "data" of a likelihood, or a point data set without axes, is the value of the single
604/// variate of the paired distribution (HS3 0.3). The numbers become point data sets named "<distribution>_data", and
605/// the point data sets get their axis, so that every term of a likelihood refers to a data set, or has no data.
606/// Returns the errors: a distribution without a single variate, or a point data set with an axis that is not the
607/// variate of its distribution.
608std::vector<std::string> resolvePointData(ValidatedHS3Document &doc);
609} // namespace HS3
610
611/// An HS3 document. It is move-only, because generic objects refer to the JSON tree of the document.
613public:
615 std::vector<HS3::Distribution> distributions;
616 std::vector<HS3::Function> functions;
617 std::vector<HS3::Data> data;
618 std::vector<HS3::Likelihood> likelihoods;
619 std::vector<HS3::Domain> domains;
620 std::vector<HS3::ParameterPoint> parameterPoints;
621 std::vector<HS3::Analysis> analyses;
623
624 HS3Document() = default;
627 HS3Document(HS3Document const &) = delete;
629};
630
631namespace HS3 {
632
633/// Options for upgradeDocument() and parseDocument().
635 /// If false, HistFactory distributions stay generic objects, because a user registered their own importer.
636 bool typedHistFactory = true;
637};
638
639/// Rewrites everything that older versions of RooFit wrote in a way that is not part of the standard into the form that
640/// parseDocument() understands.
641void upgradeDocument(Detail::JSONNode &root, Diagnostics &diagnostics, ParseOptions const &options = {});
642
643/// Same as upgradeDocument(), but for a single HistFactory distribution that doesn't come with a document. Names that
644/// can't be resolved in the document are assumed to refer to distributions.
645void upgradeHistFactoryDist(Detail::JSONNode &distribution, Version const &version, Diagnostics &diagnostics);
646
647/// Reads a document. The generic objects in the document refer to nodes of the JSON tree, so the tree must outlive the
648/// document. Problems are reported in `diagnostics`, and if there are errors the document must not be used.
649HS3Document parseDocument(Detail::JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options = {});
650
651/// Reads a distribution that doesn't come with a document.
652Distribution parseDistribution(Detail::JSONNode const &distribution, Diagnostics &diagnostics);
653
654/// Reads a HistFactory distribution that doesn't come with a document.
655HistFactoryDist parseHistFactoryDist(Detail::JSONNode const &distribution, Diagnostics &diagnostics);
656
657/// Reads a data set that doesn't come with a document.
658Data parseData(Detail::JSONNode const &data, Diagnostics &diagnostics);
659
660/// Reads the axes of binned data that doesn't come with a document.
661std::vector<BinnedAxis> parseBinnedAxes(Detail::JSONNode const &axes, Diagnostics &diagnostics);
662
663/// Reads the functions "interpolation" and "interpolation0d", which don't come with a document. Older versions of
664/// RooFit wrote integer "interpolationCodes" instead of "interpolations".
665InterpolatedFunction parseInterpolatedFunction(Detail::JSONNode const &node, Diagnostics &diagnostics);
666InterpolatedNumber parseInterpolatedNumber(Detail::JSONNode const &node, Diagnostics &diagnostics);
667
668/// Writes everything but the distributions and functions, which are written one by one while they are exported: the
669/// generic ones by the exporters, and the others with functions like writeHistFactoryDist().
670void writeDocument(Detail::JSONNode &root, HS3Document const &doc);
671
672/// Writes a HistFactory distribution, without its name.
673void writeHistFactoryDist(Detail::JSONNode &node, HistFactoryDist const &dist);
674
675/// Writes a combined distribution, without its name.
676void writeCombinedDist(Detail::JSONNode &node, CombinedDist const &dist);
677
678/// Writes the axes, contents and uncertainties of binned data, without its name and type.
679void writeBinnedData(Detail::JSONNode &node, BinnedData const &data);
680
681/// Writes an axis of binned data.
682void writeAxis(Detail::JSONNode &node, BinnedAxis const &axis);
683
684/// Writes the functions "interpolation" and "interpolation0d", with a single interpolation if they are all the same.
685void writeInterpolatedFunction(Detail::JSONNode &node, InterpolatedFunction const &function);
686void writeInterpolatedNumber(Detail::JSONNode &node, InterpolatedNumber const &function);
687
688void writeInterpolation(Detail::JSONNode &node, Interpolation const &interpolation);
689
690/// Writes an array of numbers, each as an integer if it has no fractional part.
691void writeContents(Detail::JSONNode &node, std::vector<double> const &values);
692
693/// Options for validate().
695 /// Whether the document has to follow the current standard, or if older conventions of RooFit are tolerated.
696 bool strict = false;
697 /// Whether a name that is not defined in the document is known in some other way, e.g. in the workspace.
698 std::function<bool(std::string const &)> isExternal;
699};
700
701} // namespace HS3
702
703/// A document that passed validate(). Only validate() can create it, so the code that builds RooFit objects can rely on
704/// the checks. Afterwards, it can only be changed by the functions of this layer that adapt it to what RooFit can
705/// build, which are trusted to leave it consistent.
707public:
708 HS3Document const &doc() const { return _doc; }
709
710private:
711 explicit ValidatedHS3Document(HS3Document doc) : _doc{std::move(doc)} {}
712 friend std::optional<ValidatedHS3Document>
715 friend std::vector<std::string> HS3::resolvePointData(ValidatedHS3Document &doc);
716
718};
719
720namespace HS3 {
721
722/// Checks what can be checked without knowing how the objects are built: unique names, that the references can be
723/// resolved, and that the sizes of arrays are consistent. Gives nothing if there are errors.
724std::optional<ValidatedHS3Document>
725validate(HS3Document doc, Diagnostics &diagnostics, ValidateOptions const &options = {});
726
727/// The checks of validate(), for a document that is still needed if it doesn't pass them.
728void checkDocument(HS3Document const &doc, Diagnostics &diagnostics, ValidateOptions const &options = {});
729
730/// Same as validate(), but for a single HistFactory or combined distribution that doesn't come with a document.
731void validateHistFactoryDist(HistFactoryDist const &dist, std::string const &name, Diagnostics &diagnostics);
732void validateCombinedDist(CombinedDist const &dist, std::string const &name, Diagnostics &diagnostics);
733
734/// Reads the leading "MAJOR.MINOR" and the optional ".PATCH" of an HS3 version string.
735bool parseVersion(std::string const &version, Version &out);
736
737/// Combines the modifiers of a sample that have the same type and name, which HistFactory models can have but
738/// documents can't: normsys by multiplying their variations, and histosys by adding up their differences to the
739/// nominal contents. The modifiers of a type must be sorted by name. Other duplicates are errors.
740void mergeDuplicateModifiers(HistFactoryDist &dist, std::string const &channel, Diagnostics &diagnostics);
741
742/// Makes the most common interpolation of the modifiers the "default_interpolation", which they then don't repeat.
743void factorOutDefaultInterpolation(HistFactoryDist &dist);
744
745// HS3 requires the terms of a likelihood, and the components of a combined distribution or data set, to have distinct
746// variates. For this, the generic distributions are read as far as the standard says what they consist of (e.g. the
747// factors of a product), through the nodes that the document refers to.
748
749/// Describes each variate that is shared although it must be distinct: by the terms of a likelihood, by the
750/// components of a combined distribution that no likelihood lists (the others are checked through the likelihood), or
751/// by the components of a combined data set, except for bookkeeping columns.
752std::vector<std::string> sharedVariates(HS3Document const &doc);
753
754/// In HS3 0.3 (HS3 PR #154, decision on HS3 #156), the "constraints" of a HistFactory modifier add no term to a
755/// likelihood: every likelihood that contains the channel must list each named distribution exactly once, in
756/// "distributions" or "aux_distributions", or as a factor of a product_dist or a component of a combined_dist reached
757/// from them. One message for each named distribution of each likelihood that doesn't. For older documents, RooFit
758/// adds the named constraints itself (see sharedVariates()).
759///
760/// A distribution contributes one factor per entry of the data that it is paired with, so that being listed is not
761/// the same as contributing: a component of a combined_dist without a component of the paired combined data set, or
762/// with one that has no entries, contributes nothing. A constraint that contributes once elsewhere is then fine, but
763/// since listing it in the model and again on its own is probably unintended, `warnings` gets a message. So it does
764/// for every component of a combined distribution that has no data component at all.
765std::vector<std::string> unlistedConstraints(HS3Document const &doc, std::vector<std::string> *warnings = nullptr);
766
767/// The distributions that are components of a combined distribution of a likelihood, with a component of the paired
768/// combined data set that has entries: measurements of their own.
769std::set<std::string> componentsWithData(HS3Document const &doc);
770
771/// A canonical form of the component `component` of an array distribution with the type `componentType` and the
772/// default parameters `defaults` (may be nullptr): the type and all fields after applying the defaults, sorted, with
773/// numbers in a canonical representation. Two components with the same form are identical in the sense of HS3: they
774/// are one factor of a union composition, also if they belong to different array distributions.
775std::string arrayComponentForm(std::string const &componentType, RooFit::Detail::JSONNode const &component,
776 RooFit::Detail::JSONNode const *defaults);
777
778/// The variates of the "distributions" of a likelihood, which it pairs with data, with the columns of its combined
779/// distributions.
780std::set<std::string> variatesOf(HS3Document const &doc, Likelihood const &likelihood);
781
782/// The variates of the distribution `distribution`, without the columns of a combined distribution.
783std::set<std::string> variatesOf(HS3Document const &doc, std::string const &distribution);
784
785/// The axes that several components of a combined data set have, by combined data set.
786std::map<std::string, std::set<std::string>> sharedAxes(HS3Document const &doc);
787
788/// The bookkeeping columns by combined data set: the declared ones (RooFitInternal::bookkeepingColumns), except for
789/// those that are a variate of a distribution after all. They are exempt from the requirement of distinct variates.
790std::map<std::string, std::set<std::string>> bookkeepingColumns(HS3Document const &doc);
791
792/// Writes each term of the likelihoods once: a factor of a product that is a term of the likelihood as well would enter
793/// it twice, so it is removed from the "factors" of the product in `root`, the JSON tree that the document is written
794/// to. Only products that no other distribution uses are changed (being a component of a combined distribution is not
795/// a use), only factors that are terms of every likelihood that has the product as a term, and a product whose factors
796/// are all such terms is left as it is. Returns the removed factors by product.
797std::map<std::string, std::set<std::string>> writeLikelihoodTermsOnce(HS3Document const &doc, Detail::JSONNode &root);
798
799} // namespace HS3
800} // namespace RooFit
801
802#endif
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 WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t modifier
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
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
HS3Document(HS3Document const &)=delete
std::vector< HS3::Likelihood > likelihoods
std::vector< HS3::Function > functions
std::vector< HS3::ParameterPoint > parameterPoints
HS3Document & operator=(HS3Document const &)=delete
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
HS3Document(HS3Document &&)=default
std::vector< HS3::Distribution > distributions
HS3Document & operator=(HS3Document &&)=default
Where RooFit takes the ranges of variables from: the "default_domain" if there is one,...
DomainAxis const * find(std::string const &name) const
The axis that defines the range of a variable, or null.
bool contains(std::string const &name) const
Whether any domain has the variable.
DomainRanges(std::vector< Domain > const &domains)
std::map< std::string, DomainAxis const * > _ranges
std::set< std::string > _names
A document that passed validate().
ValidatedHS3Document(HS3Document doc)
HS3Document const & doc() const
Types that represent an HS3 document.
AuxiliaryConstraints readAuxiliaryChannelsAsConstraints(ValidatedHS3Document &doc, bool asChannels)
RooFit writes an array of constraints with the data set of its observed values as a component of the ...
std::string arrayComponentForm(std::string const &componentType, RooFit::Detail::JSONNode const &component, RooFit::Detail::JSONNode const *defaults)
A canonical form of the component component of an array distribution with the type componentType and ...
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::vector< std::string > unlistedConstraints(HS3Document const &doc, std::vector< std::string > *warnings=nullptr)
In HS3 0.3 (HS3 PR #154, decision on HS3 #156), the "constraints" of a HistFactory modifier add no te...
void validateHistFactoryDist(HistFactoryDist const &dist, std::string const &name, Diagnostics &diagnostics)
Same as validate(), but for a single HistFactory or combined distribution that doesn't come with a do...
void checkDocument(HS3Document const &doc, Diagnostics &diagnostics, ValidateOptions const &options={})
The checks of validate(), for a document that is still needed if it doesn't pass them.
auto findByName(Objects_t &objects, std::string const &name) -> decltype(&*objects.begin())
The object with the name name among the objects of a section, e.g. findByName(doc....
Definition HS3Document.h:65
void writeInterpolatedFunction(Detail::JSONNode &node, InterpolatedFunction const &function)
Writes the functions "interpolation" and "interpolation0d", with a single interpolation if they are a...
std::map< std::string, std::set< std::string > > writeLikelihoodTermsOnce(HS3Document const &doc, Detail::JSONNode &root)
Writes each term of the likelihoods once: a factor of a product that is a term of the likelihood as w...
std::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
std::string const & nameOf(std::variant< Ts... > const &v)
The name of the alternative that a variant holds, e.g. of an axis or of a modifier.
Definition HS3Document.h:58
Distribution parseDistribution(Detail::JSONNode const &distribution, Diagnostics &diagnostics)
Reads a distribution that doesn't come with a document.
std::variant< RegularBinning, EdgesBinning > Binning
std::set< std::string > variatesOf(HS3Document const &doc, Likelihood const &likelihood)
The variates of the "distributions" of a likelihood, which it pairs with data, with the columns of it...
InterpolationClass
The RooFit classes that implement the interpolations, which encode them with integer codes.
std::map< std::string, std::set< std::string > > sharedAxes(HS3Document const &doc)
The axes that several components of a combined data set have, by combined data set.
constexpr Interpolation multiplicativePolynomialExponential
constexpr Interpolation additivePolynomialLinear
constexpr char const * defaultDomainName
The domain in which RooFit writes the ranges of all variables. It is a convention of RooFit,...
std::optional< ValidatedHS3Document > validate(HS3Document doc, Diagnostics &diagnostics, ValidateOptions const &options)
Checks what can be checked without knowing how the objects are built: unique names,...
void mergeDuplicateModifiers(HistFactoryDist &dist, std::string const &channel, Diagnostics &diagnostics)
Combines the modifiers of a sample that have the same type and name, which HistFactory models can hav...
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
void factorOutDefaultInterpolation(HistFactoryDist &dist)
Makes the most common interpolation of the modifiers the "default_interpolation", which they then don...
std::set< std::string > componentsWithData(HS3Document const &doc)
The distributions that are components of a combined distribution of a likelihood, with a component of...
bool operator<(Version const &l, Version const &r)
Definition HS3Document.h:43
char const * toString(InterpolationType type)
InterpolatedNumber parseInterpolatedNumber(Detail::JSONNode const &node, Diagnostics &diagnostics)
bool operator!=(Interpolation const &l, Interpolation const &r)
void upgradeHistFactoryDist(Detail::JSONNode &distribution, Version const &version, Diagnostics &diagnostics)
Same as upgradeDocument(), but for a single HistFactory distribution that doesn't come with a documen...
void writeDocument(Detail::JSONNode &root, HS3Document const &doc)
Writes everything but the distributions and functions, which are written one by one while they are ex...
char const * typeOf(Modifier const &modifier)
The type of a modifier, as the standard names it.
InterpolatedFunction parseInterpolatedFunction(Detail::JSONNode const &node, Diagnostics &diagnostics)
Reads the functions "interpolation" and "interpolation0d", which don't come with a document.
void writeContents(Detail::JSONNode &node, std::vector< double > const &values)
Writes an array of numbers, each as an integer if it has no fractional part.
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
std::map< std::string, std::set< std::string > > bookkeepingColumns(HS3Document const &doc)
The bookkeeping columns by combined data set: the declared ones (RooFitInternal::bookkeepingColumns),...
void writeInterpolatedNumber(Detail::JSONNode &node, InterpolatedNumber const &function)
void writeHistFactoryDist(Detail::JSONNode &node, HistFactoryDist const &dist)
Writes a HistFactory distribution, without its name.
auto find(Objects_t &objects, std::string const &name) -> decltype(std::get_if< Body_t >(&objects.begin() ->body))
The body of the object name if it is a Body_t, e.g. find<CombinedData>(doc.data, "obsData"),...
Definition HS3Document.h:77
std::vector< std::string > sharedVariates(HS3Document const &doc)
Describes each variate that is shared although it must be distinct: by the terms of a likelihood,...
void writeAxis(Detail::JSONNode &node, BinnedAxis const &axis)
Writes an axis of binned data.
std::vector< Diagnostic > Diagnostics
void writeInterpolation(Detail::JSONNode &node, Interpolation const &interpolation)
std::variant< Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs > GammaConstraint
void upgradeDocument(Detail::JSONNode &root, Diagnostics &diagnostics, ParseOptions const &options={})
Rewrites everything that older versions of RooFit wrote in a way that is not part of the standard int...
bool parseVersion(std::string const &version, Version &out)
Reads the leading "MAJOR.MINOR" and the optional ".PATCH" of an HS3 version string.
std::vector< std::string > resolvePointData(ValidatedHS3Document &doc)
A number given in-line in the "data" of a likelihood, or a point data set without axes,...
void writeBinnedData(Detail::JSONNode &node, BinnedData const &data)
Writes the axes, contents and uncertainties of binned data, without its name and type.
overloaded(Ts...) -> overloaded< Ts... >
std::variant< Unconstrained, ConstraintType, ConstraintPdf > Constraint
void writeCombinedDist(Detail::JSONNode &node, CombinedDist const &dist)
Writes a combined distribution, without its name.
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.
bool operator==(Interpolation const &l, Interpolation const &r)
constexpr Interpolation additiveQuadraticLinear
constexpr Interpolation additivePiecewiseLinear
HS3Document parseDocument(Detail::JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options={})
Reads a document.
Combined< DistributionRef > CombinedDist
void validateCombinedDist(CombinedDist const &dist, std::string const &name, Diagnostics &diagnostics)
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
What readAuxiliaryChannelsAsConstraints() changed.
std::set< std::string > data
Their data sets, which hold the observed values of global observables.
std::set< std::string > terms
The components that are no longer components of their combined distribution.
std::vector< std::string > warnings
The axis of binned data.
std::vector< std::variant< BinnedAxis, CategoricalAxis > > axes
std::optional< GaussianUncertainty > uncertainty
std::vector< double > contents
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...
CombinedComponent< Ref_t > const * find(std::vector< std::string > const &key) const
The component for a key, or nullptr.
std::vector< CombinedComponent< Ref_t > > components
std::vector< std::string > columns
The gamma parameters are constant, which is a RooFit extension.
The constraint term is a distribution of the document, named in "constraints".
bool legacyName
Named in "constraint_name" by an older document, whose constraint distributions of normsys and histos...
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::size_t numEntries() const
The number of factors that a distribution paired with the data contributes to a likelihood: the numbe...
std::variant< PointData, UnbinnedData, BinnedData, CombinedData > body
std::string path
Where in the document, for example "distributions[3](pdf).x".
std::variant< GenericObject, HistFactoryDist, CombinedDist > 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
Every gamma parameter has its own constraint term, or none, as named in "constraints".
std::vector< std::optional< DistributionRef > > pdfs
std::vector< std::vector< double > > correlation
Empty if there is no correlation.
std::vector< double > sigma
A distribution or function that is not interpreted by the document, but by the importers that are reg...
Detail::JSONNode const * node
The object in the JSON tree that the document was read from.
std::optional< Interpolation > defaultInterpolation
std::vector< Sample > samples
std::vector< BinnedAxis > axes
std::vector< double > contents
std::optional< std::vector< double > > errors
Not in the standard, but older versions of RooFit read them.
std::optional< Interpolation > interpolation
HistoSysVariation lo
HistoSysVariation hi
ParameterRef parameter
The function "interpolation" of RooFit, implemented by PiecewiseInterpolation: a nominal function plu...
std::vector< Interpolation > interpolations
One per parameter.
std::vector< NameOrNumber > parameters
std::vector< NameOrNumber > low
std::vector< NameOrNumber > high
The function "interpolation0d" of RooFit, implemented by FlexibleInterpVar: a nominal value plus the ...
std::vector< double > high
std::vector< double > low
std::vector< std::string > parameters
std::vector< Interpolation > interpolations
One per parameter.
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 > description
std::string hs3Version
As it was written in the document, empty for fragments of a document.
std::vector< std::string > publications
Version version
The parsed version. Versions that can't be parsed count as the oldest one.
std::vector< std::string > authors
std::vector< Package > packages
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
ParameterRef parameter
std::optional< Interpolation > interpolation
std::string version
Definition HS3Document.h:98
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().
bool typedHistFactory
If false, HistFactory distributions stay generic objects, because a user registered their own importe...
std::optional< double > uncertainty
std::vector< ContinuousAxis > axes
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
The content of misc.ROOT_internal and misc.minimization.
std::map< std::string, Attributes > attributes
std::map< std::string, std::vector< DistributionRef > > auxiliaryChannels
The components of a combined distribution that are auxiliary measurements (constraint terms paired wi...
std::map< std::string, std::vector< DistributionRef > > channelConstraints
The constraint terms of HistFactory distributions that no modifier names, by distribution.
std::map< std::string, CombinedDataset > combinedDatasets
std::vector< StepWidth > stepWidths
std::map< std::string, std::vector< std::string > > bookkeepingColumns
Axes that the components of a combined data set share, but that are no variate of any distribution,...
std::map< std::string, CombinedDistribution > combinedDistributions
std::map< std::string, ModelConfigInfo > modelConfigs
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...
std::vector< ParameterRef > parameters
If empty, the parameters are named after the modifier.
GammaConstraint constraint
std::vector< double > vals
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...
std::vector< ParameterRef > parameters
If empty, the parameters are named after the channel.
GammaConstraint constraint
std::optional< std::vector< double > > weights
std::vector< std::variant< ContinuousAxis, CategoricalAxis > > axes
std::vector< double > entries
The coordinates, row by row. A value on a categorical axis is the index in the list of categories of ...
std::size_t numEntries() const
The parameter of the modifier is not constrained.
Options for validate().
bool strict
Whether the document has to follow the current standard, or if older conventions of RooFit are tolera...
std::function< bool(std::string const &) isExternal)
Whether a name that is not defined in the document is known in some other way, e.g....
Combines lambdas into one visitor for std::visit(). The compiler complains about the alternatives wit...
Definition HS3Document.h:50
TLine l
Definition textangle.C:4