Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Writer.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// Writes the typed representation of HS3 documents. The keys are the same as in HS3Parser.cxx, which the round-trip
14// tests in testHS3Document.cxx check.
15
16#include "HS3Document.h"
17#include "JSONIOUtils.h"
18
20
21#include <algorithm>
22#include <cmath>
23#include <limits>
24
26
27namespace RooFit {
28namespace HS3 {
29namespace {
30
31// Infinite bounds are written as null.
32void writeBound(JSONNode &node, double value)
33{
34 if (std::isinf(value)) {
35 node.set_null();
36 } else {
37 node << value;
38 }
39}
40
41void writeStrings(JSONNode &node, std::vector<std::string> const &values)
42{
43 node.fill_seq(values);
44}
45
46template <class Tag>
47void writeRefs(JSONNode &node, std::vector<Ref<Tag>> const &refs)
48{
49 node.set_seq();
50 for (auto const &ref : refs) {
51 node.append_child() << ref.name;
52 }
53}
54
55JSONNode &appendNamed(JSONNode &array, std::string const &name)
56{
58 child["name"] << name;
59 return child;
60}
61
62void writeBinning(JSONNode &node, Binning const &binning)
63{
64 if (auto const *edges = std::get_if<EdgesBinning>(&binning)) {
65 node["edges"].fill_seq(edges->edges);
66 } else {
67 node["nbins"] << std::get<RegularBinning>(binning).nbins;
68 }
69}
70
71void writeAxis(JSONNode &node, BinnedAxis const &axis)
72{
73 node.set_map();
74 node["name"] << axis.name;
75 // The range of an irregular binning is given by its "edges": HS3 0.3 forbids "min" and "max" alongside them.
76 if (!std::holds_alternative<EdgesBinning>(axis.binning)) {
77 node["min"] << axis.min;
78 node["max"] << axis.max;
79 }
80 writeBinning(node, axis.binning);
81}
82
83void writeAxis(JSONNode &node, ContinuousAxis const &axis)
84{
85 node.set_map();
86 node["name"] << axis.name;
87 if (axis.min) {
88 writeBound(node["min"], *axis.min);
89 }
90 if (axis.max) {
91 writeBound(node["max"], *axis.max);
92 }
93}
94
95void writeAxis(JSONNode &node, CategoricalAxis const &axis)
96{
97 node.set_map();
98 node["name"] << axis.name;
99 writeStrings(node["categories"], axis.categories);
100}
101
102// An infinite or missing bound of a domain axis is omitted, which means that the range is unbounded.
103void writeAxis(JSONNode &node, DomainAxis const &axis)
104{
105 node.set_map();
106 node["name"] << axis.name;
107 if (axis.fixedValue) {
108 node[fixedValueKey] << *axis.fixedValue;
109 return;
110 }
111 // HS3 0.3 forbids "min" and "max" alongside "edges", which give the range. They are only written if they differ from
112 // the edges, which would otherwise be lost.
113 auto const *edges = axis.binning ? std::get_if<EdgesBinning>(&*axis.binning) : nullptr;
114 const bool rangeFromEdges = edges && !edges->edges.empty() && axis.min && axis.max &&
115 *axis.min == edges->edges.front() && *axis.max == edges->edges.back();
116 if (!rangeFromEdges && axis.min && !std::isinf(*axis.min)) {
117 node["min"] << *axis.min;
118 }
119 if (!rangeFromEdges && axis.max && !std::isinf(*axis.max)) {
120 node["max"] << *axis.max;
121 }
122 if (axis.binning) {
123 writeBinning(node, *axis.binning);
124 }
125}
126
127template <class... Axes>
128void writeAxes(JSONNode &node, std::vector<std::variant<Axes...>> const &axes)
129{
130 node.set_seq();
131 for (auto const &axis : axes) {
132 std::visit([&](auto const &a) { writeAxis(node.append_child(), a); }, axis);
133 }
134}
135
136template <class Axis>
137void writeAxes(JSONNode &node, std::vector<Axis> const &axes)
138{
139 node.set_seq();
140 for (auto const &axis : axes) {
141 writeAxis(node.append_child(), axis);
142 }
143}
144
145// The parameter of a modifier with a single parameter, and the name of its constraint distribution. A constraint type
146// is written in the form of older documents.
147void writeParameter(JSONNode &mod, ParameterRef const &parameter, Constraint const &constraint)
148{
149 mod["parameters"].set_seq().append_child() << parameter.name;
150 std::visit(
151 overloaded{[](Unconstrained) {}, [&](ConstraintType type) { mod["constraint"] << toString(type); },
152 [&](ConstraintPdf const &pdf) { mod["constraints"].set_seq().append_child() << pdf.pdf.name; }},
153 constraint);
154}
155
156void writeGammaConstraint(JSONNode &node, GammaConstraint const &constraint, const char *typeKey)
157{
158 std::visit(overloaded{[](Unconstrained) {}, [&](ConstraintType type) { node[typeKey] << toString(type); },
159 [&](ConstantGammas) { node[typeKey] << "Const"; },
160 [&](GammaConstraintPdfs const &pdfs) {
161 JSONNode &names = node["constraints"].set_seq();
162 for (auto const &pdf : pdfs.pdfs) {
163 if (pdf) {
164 names.append_child() << pdf->name;
165 } else {
166 names.append_child().set_null();
167 }
168 }
169 }},
170 constraint);
171}
172
173JSONNode &appendModifier(JSONNode &modifiers, std::string const &name, const char *type)
174{
175 JSONNode &mod = modifiers.append_child().set_map();
176 mod["name"] << name;
177 mod["type"] << type;
178 return mod;
179}
180
181void writeModifier(JSONNode &modifiers, Modifier const &modifier)
182{
183 std::visit(overloaded{
184 [&](NormFactor const &m) {
185 JSONNode &mod = appendModifier(modifiers, m.name, "normfactor");
186 writeParameter(mod, m.parameter, m.constraint);
187 },
188 [&](NormSys const &m) {
189 JSONNode &mod = appendModifier(modifiers, m.name, "normsys");
190 writeParameter(mod, m.parameter, m.constraint);
191 if (m.interpolation) {
192 writeInterpolation(mod["interpolation"], *m.interpolation);
193 }
194 JSONNode &data = mod["data"].set_map();
195 data["lo"] << m.lo;
196 data["hi"] << m.hi;
197 },
198 [&](HistoSys const &m) {
199 JSONNode &mod = appendModifier(modifiers, m.name, "histosys");
200 writeParameter(mod, m.parameter, m.constraint);
201 if (m.interpolation) {
202 writeInterpolation(mod["interpolation"], *m.interpolation);
203 }
204 JSONNode &data = mod["data"].set_map();
205 writeContents(data["lo"].set_map()["contents"], m.lo.contents);
206 writeContents(data["hi"].set_map()["contents"], m.hi.contents);
207 },
208 [&](ShapeSys const &m) {
209 JSONNode &mod = appendModifier(modifiers, m.name, "shapefactor");
210 writeRefs(mod["parameters"], m.parameters);
211 writeGammaConstraint(mod, m.constraint, "constraint");
212 // The values are only used by the constraint types of older documents.
213 if (!m.vals.empty()) {
214 mod["data"].set_map()["vals"].fill_seq(m.vals);
215 }
216 },
217 [&](StatError const &m) {
218 JSONNode &mod = appendModifier(modifiers, m.name, "staterror");
219 writeRefs(mod["parameters"], m.parameters);
220 writeGammaConstraint(mod, m.constraint, "constraint");
221 },
222 [&](CustomModifier const &m) { appendModifier(modifiers, m.name, "custom"); },
223 },
224 modifier);
225}
226
227void writeParameterPoint(JSONNode &points, ParameterPoint const &point)
228{
229 JSONNode &parameters = appendNamed(points, point.name)["parameters"].set_seq();
230 for (auto const &p : point.parameters) {
231 JSONNode &node = appendNamed(parameters, p.name);
232 if (p.value) {
233 std::visit([&](auto const &value) { node["value"] << value; }, *p.value);
234 }
235 if (p.isConst) {
236 node["const"] << true;
237 }
238 }
239}
240
241void writeData(JSONNode &dataArray, Data const &data)
242{
243 JSONNode &node = appendNamed(dataArray, data.name);
244 std::visit(overloaded{
245 [&](BinnedData const &d) {
246 node["type"] << "binned";
247 writeAxes(node["axes"], d.axes);
248 writeContents(node["contents"], d.contents);
249 if (d.uncertainty) {
250 JSONNode &uncertainty = node["uncertainty"].set_map();
251 uncertainty["type"] << "gaussian_uncertainty";
252 uncertainty["sigma"].fill_seq(d.uncertainty->sigma);
253 }
254 },
255 [&](UnbinnedData const &d) {
256 node["type"] << "unbinned";
257 writeAxes(node["axes"], d.axes);
258 JSONNode &entries = node["entries"].set_seq();
259 const std::size_t nAxes = d.axes.size();
260 for (std::size_t i = 0; i < d.numEntries(); ++i) {
261 JSONNode &row = entries.append_child().set_seq();
262 for (std::size_t j = 0; j < nAxes; ++j) {
263 const double value = d.entries[i * nAxes + j];
264 if (auto const *category = std::get_if<CategoricalAxis>(&d.axes[j])) {
265 row.append_child() << category->categories[static_cast<std::size_t>(value)];
266 } else {
267 row.append_child() << value;
268 }
269 }
270 }
271 if (d.weights) {
272 node["weights"].fill_seq(*d.weights);
273 }
274 },
275 [&](CombinedData const &d) {
276 node["type"] << "combined";
277 writeStrings(node["columns"], d.columns);
278 JSONNode &components = node["components"].set_seq();
279 for (CombinedDataComponent const &component : d.components) {
280 JSONNode &out = components.append_child().set_map();
281 writeStrings(out["key"], component.key);
282 out["dataset"] << component.data.name;
283 }
284 },
285 [&](PointData const &d) {
286 node["type"] << "point";
287 node["value"] << d.value;
288 if (d.uncertainty) {
289 node["uncertainty"] << *d.uncertainty;
290 }
291 if (!d.axes.empty()) {
292 writeAxes(node["axes"], d.axes);
293 }
294 },
295 },
296 data.body);
297}
298
299void writeLikelihood(JSONNode &likelihoods, Likelihood const &likelihood)
300{
301 JSONNode &node = appendNamed(likelihoods, likelihood.name);
302 JSONNode &distributions = node["distributions"].set_seq();
303 JSONNode &data = node["data"].set_seq();
304 for (auto const &term : likelihood.terms) {
305 distributions.append_child() << term.distribution.name;
306 std::visit(overloaded{[&](DataRef const &ref) { data.append_child() << ref.name; },
307 [&](double value) { writeNumber(data.append_child(), value); }},
308 term.data);
309 }
310 if (!likelihood.auxDistributions.empty()) {
311 writeRefs(node["aux_distributions"], likelihood.auxDistributions);
312 }
313}
314
315void writeAnalysis(JSONNode &analyses, Analysis const &analysis)
316{
317 JSONNode &node = appendNamed(analyses, analysis.name);
318 writeRefs(node["domains"], analysis.domains);
319 node["likelihood"] << analysis.likelihood.name;
320 if (!analysis.parametersOfInterest.empty()) {
321 writeRefs(node["parameters_of_interest"], analysis.parametersOfInterest);
322 }
323 if (analysis.init) {
324 node["init"] << analysis.init->name;
325 }
326 if (analysis.prior) {
327 node["prior"] << analysis.prior->name;
328 }
329}
330
331void writeRooFitInternal(JSONNode &root, RooFitInternal const &internal)
332{
333 auto rootInternal = [&]() -> JSONNode & { return root.get("misc", "ROOT_internal"); };
334 if (!internal.attributes.empty()) {
335 JSONNode &attributes = rootInternal()["attributes"].set_map();
336 for (auto const &item : internal.attributes) {
337 JSONNode &node = attributes[item.first].set_map();
338 if (!item.second.dict.empty()) {
339 JSONNode &dict = node["dict"].set_map();
340 for (auto const &entry : item.second.dict) {
341 dict[entry.first] << entry.second;
342 }
343 }
344 if (!item.second.tags.empty()) {
345 writeStrings(node["tags"], item.second.tags);
346 }
347 }
348 }
349 if (!internal.combinedDistributions.empty()) {
350 JSONNode &combined = rootInternal()["combined_distributions"].set_map();
351 for (auto const &item : internal.combinedDistributions) {
352 JSONNode &node = combined[item.first].set_map();
353 node["index_cat"] << item.second.indexCat;
354 writeStrings(node["labels"], item.second.labels);
355 node["indices"].fill_seq(item.second.indices);
356 writeRefs(node["distributions"], item.second.distributions);
357 }
358 }
359 if (!internal.combinedDatasets.empty()) {
360 JSONNode &combined = rootInternal()["combined_datasets"].set_map();
361 for (auto const &item : internal.combinedDatasets) {
362 JSONNode &node = combined[item.first].set_map();
363 node["index_cat"] << item.second.indexCat;
364 writeStrings(node["labels"], item.second.labels);
365 node["indices"].fill_seq(item.second.indices);
366 }
367 }
368 if (!internal.modelConfigs.empty()) {
369 JSONNode &modelConfigs = rootInternal()["ModelConfigs"].set_map();
370 for (auto const &item : internal.modelConfigs) {
371 JSONNode &node = modelConfigs[item.first].set_map();
372 if (item.second.mcName) {
373 node["mcName"] << *item.second.mcName;
374 }
375 if (item.second.pdfName) {
376 node["pdfName"] << *item.second.pdfName;
377 }
378 if (item.second.combinedDataName) {
379 node["combined_data_name"] << *item.second.combinedDataName;
380 }
381 if (item.second.observables) {
382 node["observables"].fill_seq(*item.second.observables);
383 }
384 }
385 }
386 if (!internal.channelConstraints.empty()) {
387 JSONNode &constraints = rootInternal()["histfactory_constraints"].set_map();
388 for (auto const &item : internal.channelConstraints) {
389 writeRefs(constraints[item.first], item.second);
390 }
391 }
392 if (!internal.bookkeepingColumns.empty()) {
393 JSONNode &columns = rootInternal()["bookkeeping_columns"].set_map();
394 for (auto const &item : internal.bookkeepingColumns) {
395 writeStrings(columns[item.first], item.second);
396 }
397 }
398 if (!internal.stepWidths.empty()) {
399 JSONNode &stepWidths = root.get("misc", "minimization")["parameter_stepwidths"].set_seq();
400 for (auto const &stepWidth : internal.stepWidths) {
401 appendNamed(stepWidths, stepWidth.name)["step_width"] << stepWidth.width;
402 }
403 }
404}
405
406} // namespace
407
408void writeNumber(JSONNode &node, double value)
409{
410 // To make sure there are no unnecessary floating points in the JSON
411 if (static_cast<int>(value) == value) {
412 node << static_cast<int>(value);
413 } else {
414 node << value;
415 }
416}
417
418void writeContents(JSONNode &node, std::vector<double> const &values)
419{
420 node.set_seq();
421 for (double value : values) {
423 }
424}
425
426void writeInterpolation(JSONNode &node, Interpolation const &interpolation)
427{
428 node.set_map();
429 node["type"] << toString(interpolation.type);
430 node["in"] << toString(interpolation.in);
431 if (interpolation.out) {
432 node["out"] << toString(*interpolation.out);
433 } else {
434 node["out"].set_null();
435 }
436}
437
438namespace {
439
440void writeValue(JSONNode &node, NameOrNumber const &value)
441{
442 std::visit([&](auto const &v) { node << v; }, value);
443}
444
445template <class T>
446void writeValue(JSONNode &node, T const &value)
447{
448 node << value;
449}
450
451template <class T>
452void writeList(JSONNode &node, std::vector<T> const &values)
453{
454 node.set_seq();
455 for (auto const &value : values) {
456 writeValue(node.append_child(), value);
457 }
458}
459
460template <class Function_t>
461void writeInterpolated(JSONNode &node, Function_t const &function, char const *type)
462{
463 node["type"] << type;
464 auto const &interpolations = function.interpolations;
465 const bool allEqual = std::all_of(interpolations.begin(), interpolations.end(),
466 [&](Interpolation const &i) { return i == interpolations.front(); });
467 JSONNode &interpolationsNode = node["interpolations"].set_seq();
468 for (std::size_t i = 0; i < (allEqual ? std::min<std::size_t>(interpolations.size(), 1) : interpolations.size());
469 ++i) {
470 writeInterpolation(interpolationsNode.append_child(), interpolations[i]);
471 }
472 writeList(node["vars"], function.parameters);
473 node["nom"] << function.nominal;
474 writeList(node["high"], function.high);
475 writeList(node["low"], function.low);
476}
477
478} // namespace
479
481{
482 writeInterpolated(node, function, "interpolation");
483 node["positiveDefinite"] << function.positiveDefinite;
484}
485
487{
488 writeInterpolated(node, function, "interpolation0d");
489}
490
492{
493 node.set_map();
494 node["type"] << "histfactory_dist";
495 if (!std::holds_alternative<Unconstrained>(dist.statError.constraint)) {
496 writeGammaConstraint(node["staterror"].set_map(), dist.statError.constraint, "constraint_type");
497 }
498 if (dist.defaultInterpolation) {
499 writeInterpolation(node["default_interpolation"], *dist.defaultInterpolation);
500 }
501 writeAxes(node["axes"], dist.axes);
502 JSONNode &samples = node["samples"].set_seq();
503 for (auto const &sample : dist.samples) {
504 JSONNode &s = appendNamed(samples, sample.name);
505 JSONNode &modifiers = s["modifiers"].set_seq();
506 for (auto const &modifier : sample.modifiers) {
507 writeModifier(modifiers, modifier);
508 }
509 JSONNode &data = s["data"].set_map();
510 writeContents(data["contents"], sample.contents);
511 if (sample.errors) {
512 writeContents(data["errors"], *sample.errors);
513 }
514 }
515}
516
517void writeDocument(JSONNode &root, HS3Document const &doc)
518{
519 root.set_map();
520 JSONNode &metadata = root["metadata"].set_map();
521 metadata["hs3_version"] << doc.metadata.hs3Version;
522 for (auto const &package : doc.metadata.packages) {
523 appendNamed(metadata["packages"], package.name)["version"] << package.version;
524 }
525
526 for (auto const &point : doc.parameterPoints) {
527 writeParameterPoint(root["parameter_points"], point);
528 }
529 for (auto const &domain : doc.domains) {
530 JSONNode &node = appendNamed(root["domains"], domain.name);
531 node["type"] << "product_domain";
532 writeAxes(node["axes"], domain.axes);
533 }
534 for (auto const &data : doc.data) {
535 writeData(root["data"], data);
536 }
537 for (auto const &likelihood : doc.likelihoods) {
538 writeLikelihood(root["likelihoods"], likelihood);
539 }
540 for (auto const &analysis : doc.analyses) {
541 writeAnalysis(root["analyses"], analysis);
542 }
543 writeRooFitInternal(root, doc.rooFit);
544}
545
546} // namespace HS3
547} // namespace RooFit
constexpr const char * fixedValueKey
Definition JSONIOUtils.h:70
#define d(i)
Definition RSha256.hxx:102
#define a(i)
Definition RSha256.hxx:99
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 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 modifier
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
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
JSONNode & get(std::string const &key)
void fill_seq(Collection const &coll)
An HS3 document. It is move-only, because generic objects refer to the JSON tree that the document wa...
std::vector< HS3::Analysis > analyses
std::vector< HS3::Data > data
HS3::Metadata metadata
std::vector< HS3::Likelihood > likelihoods
std::vector< HS3::ParameterPoint > parameterPoints
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
Types that represent an HS3 document.
std::variant< std::string, double > NameOrNumber
A name, or a number for a constant.
void writeNumber(Detail::JSONNode &node, double value)
Writes a number, as an integer if it has no fractional part.
void writeInterpolatedFunction(Detail::JSONNode &node, InterpolatedFunction const &function)
Writes the functions "interpolation" and "interpolation0d", with a single interpolation if they are a...
std::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
std::variant< RegularBinning, EdgesBinning > Binning
Definition HS3Document.h:94
ConstraintType
The constraint types that older documents named, for which RooFit creates the constraint terms.
Ref< struct DataTag > DataRef
Definition HS3Document.h:68
char const * toString(InterpolationType type)
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...
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.
void writeInterpolatedNumber(Detail::JSONNode &node, InterpolatedNumber const &function)
void writeHistFactoryDist(Detail::JSONNode &node, HistFactoryDist const &dist)
Writes a HistFactory distribution, without its name.
Ref< struct ParameterTag > ParameterRef
Definition HS3Document.h:72
void writeInterpolation(Detail::JSONNode &node, Interpolation const &interpolation)
std::variant< Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs > GammaConstraint
overloaded(Ts...) -> overloaded< Ts... >
std::variant< Unconstrained, ConstraintType, ConstraintPdf > Constraint
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
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.
std::string hs3Version
As it was written in the document, empty for fragments of a document.
Definition HS3Document.h:80
std::vector< Package > packages
Definition HS3Document.h:82
TMarker m
Definition textangle.C:8