Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Validate.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 "HS3Document.h"
14
15#include <algorithm>
16#include <cmath>
17#include <limits>
18#include <map>
19#include <optional>
20#include <set>
21#include <string>
22#include <vector>
23
24namespace RooFit {
25namespace HS3 {
26namespace {
27
28std::string at(const char *section, std::size_t index, std::string const &name)
29{
30 return std::string(section) + "[" + std::to_string(index) + "](" + name + ")";
31}
32
33class Validator {
34public:
35 Validator(HS3Document const &doc, Diagnostics &diagnostics, ValidateOptions const &options)
36 : _doc{doc}, _diagnostics{diagnostics}, _options{options}
37 {
38 }
39
40 void run()
41 {
42 collectNames();
43 checkFixedAxes();
44 checkCategoricalAxes();
45 checkLikelihoods();
46 checkAnalyses();
47 checkData();
48 checkCombinedData();
49 for (std::size_t i = 0; i < _doc.distributions.size(); ++i) {
50 auto const &dist = _doc.distributions[i];
51 if (auto const *histFactory = std::get_if<HistFactoryDist>(&dist.body)) {
52 checkHistFactory(*histFactory, dist.name, at("distributions", i, dist.name));
53 }
54 }
55 for (auto const &item : _doc.rooFit.combinedDistributions) {
56 for (auto const &ref : item.second.distributions) {
57 requireDistribution(ref.name, "misc.ROOT_internal.combined_distributions." + item.first, "distribution");
58 }
59 }
60 for (auto const &item : _doc.rooFit.channelConstraints) {
61 requireDistribution(item.first, "misc.ROOT_internal.histfactory_constraints", "distribution");
62 for (auto const &ref : item.second) {
63 requireDistribution(ref.name, "misc.ROOT_internal.histfactory_constraints." + item.first, "constraint");
64 }
65 }
66 }
67
68 void error(std::string const &path, std::string const &message)
69 {
71 }
72
73 void warning(std::string const &path, std::string const &message)
74 {
76 }
77
78 // An error in documents that follow the current standard, which older versions of RooFit didn't care about.
79 void strictError(std::string const &path, std::string const &message)
80 {
81 _diagnostics.push_back(
83 }
84
85 void checkHistFactory(HistFactoryDist const &dist, std::string const &name, std::string const &path);
86
87private:
88 bool isExternal(std::string const &name) const { return _options.isExternal && _options.isExternal(name); }
89
90 char const *sectionOf(std::string const &name) const
91 {
92 auto found = _sections.find(name);
93 return found != _sections.end() ? found->second.front() : nullptr;
94 }
95
96 bool isDefinedIn(std::string const &name, char const *section) const
97 {
98 auto found = _sections.find(name);
99 if (found == _sections.end()) {
100 return false;
101 }
102 return std::any_of(found->second.begin(), found->second.end(),
103 [&](char const *s) { return std::string(s) == section; });
104 }
105
106 void requireDistribution(std::string const &name, std::string const &path, std::string const &what)
107 {
108 // RooFit refers to a RooSimultaneous by name, like the likelihood of a ModelConfig without data does.
109 if (!isDefinedIn(name, "distributions") && !_doc.rooFit.combinedDistributions.count(name) && !isExternal(name)) {
110 error(path, "the " + what + " '" + name + "' does not exist");
111 }
112 }
113
114 void addParameter(std::string const &name) { _parameters.insert(name); }
115
116 // The number of bins of an axis, after reporting an invalid binning.
117 std::size_t checkAxis(BinnedAxis const &axis, std::string const &path);
118 std::size_t checkAxis(CategoricalAxis const &axis, std::string const &path);
119 template <class... Axis>
120 std::size_t checkAxis(std::variant<Axis...> const &axis, std::string const &path)
121 {
122 return std::visit([&](auto const &a) { return checkAxis(a, path); }, axis);
123 }
124 // The number of bins of all axes, or nothing if it can't be stored.
125 template <class Axes>
126 std::optional<std::size_t> numBins(Axes const &axes, std::string const &path);
127
128 void collectNames();
129 void checkFixedAxes();
130 void checkCategoricalAxes();
131 void checkLikelihoods();
132 void checkAnalyses();
133 void checkData();
134 void checkCombinedData();
135
136 HS3Document const &_doc;
138 ValidateOptions const &_options;
139 std::map<std::string, std::vector<char const *>> _sections; ///< The sections where a name is defined.
140 std::set<std::string> _parameters;
141};
142
143// HS3 requires the names of all objects to be unique within a document, including the parameters. RooFit writes such
144// documents, e.g. for a range and a pdf of one name, so only ambiguous clashes in current documents are errors.
145void Validator::collectNames()
146{
147 auto check = [&](char const *section, std::string const &name) {
148 auto &sections = _sections[name];
149 sections.push_back(section);
150 if (sections.size() == 1) {
151 return;
152 }
153 std::string const firstSection = sections.front();
154 // RooFit used to give analyses and likelihoods the name of the pdf or the analysis. Accept this for backwards
155 // compatibility: analyses are not referenced by name, and likelihoods only from analyses, so there is no
156 // ambiguity.
157 auto isLegacyNamed = [](std::string const &s) { return s == "likelihoods" || s == "analyses"; };
158 // Within one section, and between distributions and functions (both can be referenced by name), a clash is
159 // ambiguous. Other clashes are produced by RooFit itself (a range, snapshot or data set named like a pdf).
160 auto isReferable = [](std::string const &s) { return s == "distributions" || s == "functions"; };
161 const bool ambiguous = firstSection == section || (isReferable(firstSection) && isReferable(section));
162 if (!_options.strict || !ambiguous || isLegacyNamed(firstSection) || isLegacyNamed(section)) {
163 warning("", "the name '" + name + "' is used in \"" + firstSection + "\" and in \"" + section +
164 "\", which is not allowed by the HS3 standard.");
165 return;
166 }
167 error("", "duplicate name '" + name + "': it is defined in \"" + firstSection + "\" and in \"" + section +
168 "\", but names must be unique within the document");
169 };
170 auto checkSection = [&](char const *section, auto const &objects) {
171 for (auto const &object : objects) {
172 check(section, object.name);
173 }
174 };
175 checkSection("distributions", _doc.distributions);
176 checkSection("functions", _doc.functions);
177 checkSection("data", _doc.data);
178 checkSection("domains", _doc.domains);
179 checkSection("parameter_points", _doc.parameterPoints);
180 checkSection("likelihoods", _doc.likelihoods);
181 checkSection("analyses", _doc.analyses);
182
183 // The parameters are the axes of the domains, the entries of the parameter points, and the parameters of
184 // HistFactory modifiers, which are created on the fly.
185 for (auto const &domain : _doc.domains) {
186 for (auto const &axis : domain.axes) {
187 addParameter(std::visit([](auto const &a) -> std::string const & { return a.name; }, axis));
188 }
189 }
190 for (auto const &point : _doc.parameterPoints) {
191 for (auto const &parameter : point.parameters) {
192 addParameter(parameter.name);
193 }
194 }
195 for (auto const &item : _doc.rooFit.combinedDistributions) {
196 addParameter(item.second.indexCat);
197 }
198 for (auto const &dist : _doc.distributions) {
199 auto const *histFactory = std::get_if<HistFactoryDist>(&dist.body);
200 if (!histFactory) {
201 continue;
202 }
203 for (auto const &sample : histFactory->samples) {
204 for (auto const &modifier : sample.modifiers) {
205 std::visit(overloaded{[&](NormFactor const &m) { addParameter(m.parameter.name); },
206 [&](NormSys const &m) { addParameter(m.parameter.name); },
207 [&](HistoSys const &m) { addParameter(m.parameter.name); },
208 [&](ShapeSys const &m) {
209 for (auto const &p : m.parameters) {
210 addParameter(p.name);
211 }
212 },
213 [&](StatError const &m) {
214 for (auto const &p : m.parameters) {
215 addParameter(p.name);
216 }
217 },
218 [](CustomModifier const &) {}},
219 modifier);
220 }
221 }
222 }
223 for (std::string const &parameter : _parameters) {
224 if (char const *section = sectionOf(parameter)) {
225 strictError("", "the name '" + parameter + "' is used in \"" + section +
226 "\" and for a parameter, which is not allowed by the HS3 standard.");
227 }
228 }
229}
230
231// A fixed axis says that the parameter only takes a single value. No other domain can give it a range or another
232// value, and no parameter point another value.
233void Validator::checkFixedAxes()
234{
235 struct FixedAxis {
236 double value = 0.0;
237 std::string const *domain = nullptr;
238 };
239 std::map<std::string, FixedAxis> fixedAxes;
240 auto forEachAxis = [&](auto &&fn) {
241 for (std::size_t i = 0; i < _doc.domains.size(); ++i) {
242 for (auto const &axis : _doc.domains[i].axes) {
243 // A categorical axis is passed as null.
244 fn(i, std::visit([](auto const &a) -> std::string const & { return a.name; }, axis),
245 std::get_if<DomainAxis>(&axis));
246 }
247 }
248 };
249 forEachAxis([&](std::size_t i, std::string const &, DomainAxis const *continuous) {
250 if (!continuous || !continuous->fixedValue) {
251 return;
252 }
253 DomainAxis const &axis = *continuous;
254 Domain const &domain = _doc.domains[i];
255 if (!std::isfinite(*axis.fixedValue)) {
256 error(at("domains", i, domain.name),
257 "the single value of the parameter '" + axis.name + "' is not finite, which JSON can't represent");
258 }
259 auto inserted = fixedAxes.insert({axis.name, FixedAxis{*axis.fixedValue, &domain.name}});
260 if (!inserted.second && inserted.first->second.value != *axis.fixedValue) {
261 error(at("domains", i, domain.name),
262 "the parameter '" + axis.name + "' has different single values in the domains '" +
263 *inserted.first->second.domain + "' and '" + domain.name + "'");
264 }
265 });
266 if (fixedAxes.empty()) {
267 return;
268 }
269 forEachAxis([&](std::size_t i, std::string const &name, DomainAxis const *continuous) {
270 auto found = fixedAxes.find(name);
271 if (found != fixedAxes.end() && !(continuous && continuous->fixedValue)) {
272 Domain const &domain = _doc.domains[i];
273 error(at("domains", i, domain.name),
274 "the parameter '" + name + "' has the single value " + std::to_string(found->second.value) +
275 " in the domain '" + *found->second.domain + "', but " + (continuous ? "a range" : "categories") +
276 " in the domain '" + domain.name + "'");
277 }
278 });
279 for (std::size_t i = 0; i < _doc.parameterPoints.size(); ++i) {
280 ParameterPoint const &point = _doc.parameterPoints[i];
281 for (ParameterValue const &parameter : point.parameters) {
282 auto found = fixedAxes.find(parameter.name);
283 double const *value = parameter.value ? std::get_if<double>(&*parameter.value) : nullptr;
284 if (value && found != fixedAxes.end() && *value != found->second.value) {
285 error(at("parameter_points", i, point.name),
286 "the parameter point '" + point.name + "' gives the parameter '" + parameter.name + "' the value " +
287 std::to_string(*value) + ", but its domain only has the value " +
288 std::to_string(found->second.value));
289 }
290 }
291 }
292}
293
294// A categorical axis (HS3 #108) declares a category. All domains that list it must give it the same categories, and
295// none may list it as continuous.
296void Validator::checkCategoricalAxes()
297{
298 struct Declaration {
299 std::vector<std::string> const *categories = nullptr;
300 std::string const *domain = nullptr;
301 };
302 std::map<std::string, Declaration> categorical;
303 for (std::size_t i = 0; i < _doc.domains.size(); ++i) {
304 Domain const &domain = _doc.domains[i];
305 for (auto const &axis : domain.axes) {
306 auto const *cat = std::get_if<CategoricalAxis>(&axis);
307 if (!cat) {
308 continue;
309 }
310 auto inserted = categorical.insert({cat->name, Declaration{&cat->categories, &domain.name}});
311 if (!inserted.second && *inserted.first->second.categories != cat->categories) {
312 error(at("domains", i, domain.name), "the categorical axis '" + cat->name + "' is listed in the domains '" +
313 *inserted.first->second.domain + "' and '" + domain.name +
314 "' with different categories");
315 }
316 }
317 }
318 for (std::size_t i = 0; i < _doc.domains.size(); ++i) {
319 Domain const &domain = _doc.domains[i];
320 for (auto const &axis : domain.axes) {
321 auto const *continuous = std::get_if<DomainAxis>(&axis);
322 if (!continuous || continuous->fixedValue) {
323 continue; // fixed axes are checked by checkFixedAxes()
324 }
325 auto found = categorical.find(continuous->name);
326 if (found != categorical.end()) {
327 error(at("domains", i, domain.name),
328 "the axis '" + continuous->name + "' is listed in a domain as categorical and as continuous");
329 }
330 }
331 }
332}
333
334// The columns of combined data sets are categorical axes of the domains, or categories that exist already, and the keys
335// consist of their categories. Every sub-data set is a data set of the document that belongs to one combined data set.
336void Validator::checkCombinedData()
337{
338 std::map<std::string, std::vector<std::string> const *> categorical;
339 for (Domain const &domain : _doc.domains) {
340 for (auto const &axis : domain.axes) {
341 if (auto const *cat = std::get_if<CategoricalAxis>(&axis)) {
342 categorical.emplace(cat->name, &cat->categories);
343 }
344 }
345 }
346 std::set<std::string> combinedNames;
347 for (auto const &data : _doc.data) {
348 if (std::holds_alternative<CombinedData>(data.body)) {
349 combinedNames.insert(data.name);
350 }
351 }
352 std::set<std::string> usedComponents;
353 for (std::size_t i = 0; i < _doc.data.size(); ++i) {
354 auto const *combined = std::get_if<CombinedData>(&_doc.data[i].body);
355 if (!combined) {
356 continue;
357 }
358 std::string const path = at("data", i, _doc.data[i].name);
359 std::string const context = "the combined data set '" + _doc.data[i].name + "'";
360 std::vector<std::vector<std::string> const *> columnCategories;
361 std::set<std::string> columns;
362 for (std::string const &column : combined->columns) {
363 auto found = categorical.find(column);
364 if (found == categorical.end() && !isExternal(column)) {
365 error(path,
366 "the column '" + column + "' of " + context + " is not declared by a categorical axis of a domain");
367 return;
368 }
369 if (!columns.insert(column).second) {
370 error(path, "the column '" + column + "' of " + context + " is listed twice");
371 return;
372 }
373 columnCategories.push_back(found != categorical.end() ? found->second : nullptr);
374 }
375 std::set<std::vector<std::string>> keys;
376 for (CombinedDataComponent const &component : combined->components) {
377 for (std::size_t k = 0; k < component.key.size(); ++k) {
378 auto const *categories = columnCategories[k];
379 if (categories &&
380 std::find(categories->begin(), categories->end(), component.key[k]) == categories->end()) {
381 error(path, "the label '" + component.key[k] + "' in position " + std::to_string(k) + " of a key of " +
382 context + " is not one of the categories of '" + combined->columns[k] + "'");
383 }
384 }
385 if (!keys.insert(component.key).second) {
386 std::string key = "[";
387 for (std::size_t k = 0; k < component.key.size(); ++k) {
388 key += (k == 0 ? "\"" : ", \"") + component.key[k] + "\"";
389 }
390 error(path, "the key " + key + "] is used by two components of " + context);
391 }
392 std::string const &name = component.data.name;
393 if (!isDefinedIn(name, "data") || combinedNames.count(name) || !usedComponents.insert(name).second) {
394 error(path, "the component '" + name + "' of " + context +
395 " is not a data set of the document, or it is already a component of another combined "
396 "data set");
397 }
398 }
399 }
400}
401
402void Validator::checkLikelihoods()
403{
404 for (std::size_t i = 0; i < _doc.likelihoods.size(); ++i) {
405 auto const &likelihood = _doc.likelihoods[i];
406 const std::string path = at("likelihoods", i, likelihood.name);
407 for (auto const &term : likelihood.terms) {
408 requireDistribution(term.distribution.name, path + ".distributions", "distribution");
409 if (auto const *data = std::get_if<DataRef>(&term.data)) {
410 if (!isDefinedIn(data->name, "data")) {
411 error(path + ".data", "dataset '" + data->name + "' cannot be found!");
412 }
413 }
414 }
415 for (auto const &aux : likelihood.auxDistributions) {
416 if (!isDefinedIn(aux.name, "distributions") && !isExternal(aux.name)) {
417 strictError(path + ".aux_distributions", "the distribution '" + aux.name + "' does not exist");
418 }
419 }
420 }
421}
422
423void Validator::checkAnalyses()
424{
425 for (std::size_t i = 0; i < _doc.analyses.size(); ++i) {
426 auto const &analysis = _doc.analyses[i];
427 const std::string path = at("analyses", i, analysis.name);
428 if (!isDefinedIn(analysis.likelihood.name, "likelihoods")) {
429 error(path + ".likelihood", "likelihood node not found!");
430 }
431 for (auto const &poi : analysis.parametersOfInterest) {
432 if (char const *section = sectionOf(poi.name)) {
433 error(path, "the parameter of interest '" + poi.name + "' of analysis '" + analysis.name +
434 "' is defined in \"" + section + "\", but it must be a parameter");
435 } else if (!_parameters.count(poi.name) && !isExternal(poi.name)) {
436 error(path, "the object '" + poi.name + "' in \"parameters_of_interest\" of analysis '" + analysis.name +
437 "' does not exist");
438 }
439 }
440 for (auto const &domain : analysis.domains) {
441 if (!isDefinedIn(domain.name, "domains")) {
442 strictError(path + ".domains", "the domain '" + domain.name + "' does not exist");
443 }
444 }
445 if (analysis.init && !isDefinedIn(analysis.init->name, "parameter_points")) {
446 warning(path, "the parameter point '" + analysis.init->name + "' used as \"init\" of analysis '" +
447 analysis.name + "' does not exist.");
448 }
449 if (analysis.prior && !isDefinedIn(analysis.prior->name, "distributions") && !isExternal(analysis.prior->name)) {
450 strictError(path + ".prior", "the distribution '" + analysis.prior->name + "' does not exist");
451 }
452 }
453}
454
455std::size_t Validator::checkAxis(BinnedAxis const &axis, std::string const &path)
456{
457 if (auto const *regular = std::get_if<RegularBinning>(&axis.binning)) {
458 if (regular->nbins <= 0) {
459 error(path, "the axis '" + axis.name + "' needs a positive number of bins");
460 return 0;
461 }
462 if (!(axis.min < axis.max)) {
463 error(path, "the axis '" + axis.name + "' needs \"min\" smaller than \"max\"");
464 }
465 return regular->nbins;
466 }
467 auto const &edges = std::get<EdgesBinning>(axis.binning).edges;
468 return edges.empty() ? 0 : edges.size() - 1;
469}
470
471std::size_t Validator::checkAxis(CategoricalAxis const &axis, std::string const &path)
472{
473 if (axis.categories.empty()) {
474 error(path, "the axis '" + axis.name + "' needs at least one category");
475 }
476 return axis.categories.size();
477}
478
479template <class Axes>
480std::optional<std::size_t> Validator::numBins(Axes const &axes, std::string const &path)
481{
482 std::size_t nBins = 1;
483 bool valid = true;
484 for (std::size_t i = 0; i < axes.size(); ++i) {
485 const std::size_t n = checkAxis(axes[i], path + ".axes[" + std::to_string(i) + "]");
486 if (n == 0) {
487 valid = false; // reported already
488 } else if (nBins > std::numeric_limits<std::size_t>::max() / n) {
489 error(path + ".axes", "the axes have more bins than can be stored");
490 return std::nullopt;
491 }
492 nBins *= n;
493 }
494 return valid ? std::optional<std::size_t>{nBins} : std::nullopt;
495}
496
497void Validator::checkData()
498{
499 for (std::size_t i = 0; i < _doc.data.size(); ++i) {
500 auto const &data = _doc.data[i];
501 auto const *binned = std::get_if<BinnedData>(&data.body);
502 if (!binned) {
503 continue;
504 }
505 const std::string path = at("data", i, data.name);
506 std::optional<std::size_t> nBinsOrNone = numBins(binned->axes, path);
507 if (!nBinsOrNone) {
508 continue;
509 }
510 const std::size_t nBins = *nBinsOrNone;
511 if (binned->contents.size() != nBins) {
512 error(path, "inconsistent bin numbers: contents=" + std::to_string(binned->contents.size()) +
513 ", bins=" + std::to_string(nBins));
514 }
515 if (binned->uncertainty) {
516 auto const &sigma = binned->uncertainty->sigma;
517 if (sigma.size() != binned->contents.size()) {
518 error(path, "inconsistent number of errors and contents in '" + data.name +
519 "': errors=" + std::to_string(sigma.size()) +
520 ", contents=" + std::to_string(binned->contents.size()));
521 }
522 auto const &correlation = binned->uncertainty->correlation;
523 if (!correlation.empty()) {
524 bool square = correlation.size() == binned->contents.size();
525 for (auto const &row : correlation) {
526 square = square && row.size() == binned->contents.size();
527 }
528 if (!square) {
529 error(path + ".uncertainty.correlation", "must be a square matrix of the size of \"contents\"");
530 }
531 }
532 }
533 }
534}
535
536void Validator::checkHistFactory(HistFactoryDist const &dist, std::string const &name, std::string const &path)
537{
538 if (dist.axes.empty()) {
539 error(path + ".axes", "a HistFactory distribution needs at least one axis");
540 return;
541 }
542 std::optional<std::size_t> nBinsOrNone = numBins(dist.axes, path);
543 if (!nBinsOrNone) {
544 return;
545 }
546 const std::size_t nBins = *nBinsOrNone;
547
548 std::string channelName = name;
549 if (channelName.rfind("model_", 0) == 0) {
550 channelName.erase(0, 6);
551 }
552
553 auto checkSize = [&](std::string const &where, std::size_t size, char const *what) {
554 if (size != nBins) {
555 error(where, std::string(what) + " has " + std::to_string(size) + " entries, but the channel has " +
556 std::to_string(nBins) + " bins");
557 }
558 };
559
560 // Two modifiers with the same parameter must have the same constraint.
561 std::map<std::string, std::pair<Constraint, std::string>> constraintOfParameter;
562 auto checkCorrelated = [&](std::string const &where, std::string const &parameter, Constraint const &constraint) {
563 auto inserted = constraintOfParameter.emplace(parameter, std::make_pair(constraint, where));
564 if (inserted.second) {
565 return;
566 }
567 if (!(inserted.first->second.first == constraint)) {
568 warning(where, "the parameter '" + parameter + "' has a different constraint than in " +
569 inserted.first->second.second +
570 ", but modifiers with the same parameter must share the constraint");
571 }
572 };
573
574 auto checkConstraint = [&](std::string const &where, Constraint const &constraint) {
575 if (auto const *pdf = std::get_if<ConstraintPdf>(&constraint)) {
576 requireDistribution(pdf->pdf.name, where, "constraint");
577 }
578 };
579
580 // The constraint distributions of the gamma parameters of shape modifiers.
581 auto checkGammaConstraint = [&](std::string const &where, GammaConstraint const &constraint) {
582 auto const *pdfs = std::get_if<GammaConstraintPdfs>(&constraint);
583 if (!pdfs) {
584 return;
585 }
586 checkSize(where + ".constraints", pdfs->pdfs.size(), "\"constraints\"");
587 for (auto const &pdf : pdfs->pdfs) {
588 if (pdf) {
589 requireDistribution(pdf->name, where + ".constraints", "constraint");
590 }
591 }
592 };
593
594 auto checkParameter = [&](std::string const &where, std::string const &parameter, std::string const &modifier) {
595 if (char const *section = sectionOf(parameter)) {
596 error(where, "the parameter '" + parameter + "' of the modifier '" + modifier + "' is defined in \"" +
597 section + "\", but it must be a parameter");
598 }
599 };
600
601 checkGammaConstraint(path + ".staterror", dist.statError.constraint);
602
603 // The staterror modifiers of all samples share the gamma parameters, so the ones that name constraints must name
604 // the same.
605 GammaConstraintPdfs const *statErrorConstraints = nullptr;
606 auto checkStatErrorConstraints = [&](std::string const &where, GammaConstraint const &constraint) {
607 auto const *pdfs = std::get_if<GammaConstraintPdfs>(&constraint);
608 if (!pdfs) {
609 return;
610 }
611 if (!statErrorConstraints) {
612 statErrorConstraints = pdfs;
613 return;
614 }
615 auto const &first = statErrorConstraints->pdfs;
616 const bool same = std::equal(first.begin(), first.end(), pdfs->pdfs.begin(), pdfs->pdfs.end(),
617 [](auto const &l, auto const &r) { return l && r ? l->name == r->name : !l && !r; });
618 if (!same) {
619 error(where,
620 "the staterror modifiers of the samples of channel '" + channelName + "' name different constraints");
621 }
622 };
623
624 std::set<std::string> sampleNames;
625 for (std::size_t iSample = 0; iSample < dist.samples.size(); ++iSample) {
626 auto const &sample = dist.samples[iSample];
627 const std::string samplePath = path + ".samples[" + std::to_string(iSample) + "](" + sample.name + ")";
628 if (!sampleNames.insert(sample.name).second) {
629 error(samplePath, "the name of the sample is not unique in the channel");
630 }
631 checkSize(samplePath + ".data.contents", sample.contents.size(), "\"contents\"");
632 // These modifiers create objects that are named after them.
633 std::set<std::string> objectNames;
634 auto checkUniqueName = [&](std::string const &where, char const *type, std::string const &modifierName) {
635 if (!objectNames.insert(std::string(type) + " " + modifierName).second) {
636 error(where,
637 std::string("there is another ") + type + " modifier named '" + modifierName + "' in the sample");
638 }
639 };
640 if (sample.errors && sample.errors->size() != sample.contents.size()) {
641 error(samplePath + ".data", "inconsistent number of errors and contents in '" + sample.name +
642 "': errors=" + std::to_string(sample.errors->size()) +
643 ", contents=" + std::to_string(sample.contents.size()));
644 }
645 for (std::size_t iModifier = 0; iModifier < sample.modifiers.size(); ++iModifier) {
646 std::string modifierPath = samplePath + ".modifiers[" + std::to_string(iModifier) + "]";
647 auto checkInterpolation = [&](std::optional<Interpolation> const &interpolation, char const *type,
648 std::string const &modifierName) {
649 if (!interpolation && !dist.defaultInterpolation) {
650 error(modifierPath, std::string("interpolation for ") + type + " modifier '" + modifierName +
651 "' in sample '" + sample.name + "' of channel '" + channelName +
652 "' is not given, and the channel has no \"default_interpolation\"");
653 }
654 };
655 std::visit(overloaded{
656 [&](NormFactor const &m) {
657 modifierPath += "(" + m.name + ")";
658 checkParameter(modifierPath, m.parameter.name, m.name);
659 checkConstraint(modifierPath, m.constraint);
660 checkCorrelated(modifierPath, m.parameter.name, m.constraint);
661 },
662 [&](NormSys const &m) {
663 modifierPath += "(" + m.name + ")";
664 checkParameter(modifierPath, m.parameter.name, m.name);
665 checkConstraint(modifierPath, m.constraint);
666 checkCorrelated(modifierPath, m.parameter.name, m.constraint);
667 checkInterpolation(m.interpolation, "normsys", m.name);
668 },
669 [&](HistoSys const &m) {
670 modifierPath += "(" + m.name + ")";
671 checkUniqueName(modifierPath, "histosys", m.name);
672 checkParameter(modifierPath, m.parameter.name, m.name);
673 checkConstraint(modifierPath, m.constraint);
674 checkCorrelated(modifierPath, m.parameter.name, m.constraint);
675 checkInterpolation(m.interpolation, "histosys", m.name);
676 checkSize(modifierPath + ".data.hi.contents", m.hi.contents.size(), "\"contents\"");
677 checkSize(modifierPath + ".data.lo.contents", m.lo.contents.size(), "\"contents\"");
678 },
679 [&](ShapeSys const &m) {
680 modifierPath += "(" + m.name + ")";
681 checkUniqueName(modifierPath, "shapesys", m.name);
682 if (!m.vals.empty()) {
683 checkSize(modifierPath + ".data.vals", m.vals.size(), "\"vals\"");
684 }
685 if (!m.parameters.empty()) {
686 checkSize(modifierPath + ".parameters", m.parameters.size(), "\"parameters\"");
687 }
688 for (auto const &p : m.parameters) {
689 checkParameter(modifierPath, p.name, m.name);
690 }
691 checkGammaConstraint(modifierPath, m.constraint);
692 },
693 [&](StatError const &m) {
694 modifierPath += "(" + m.name + ")";
695 if (!m.parameters.empty()) {
696 checkSize(modifierPath + ".parameters", m.parameters.size(), "\"parameters\"");
697 }
698 checkGammaConstraint(modifierPath, m.constraint);
699 checkStatErrorConstraints(modifierPath, m.constraint);
700 },
701 [&](CustomModifier const &m) {
702 modifierPath += "(" + m.name + ")";
703 if (!isDefinedIn(m.name, "functions") && !isDefinedIn(m.name, "distributions") &&
704 !isExternal(m.name)) {
705 error(modifierPath, "unable to find custom modifier '" + m.name + "'");
706 }
707 },
708 },
709 sample.modifiers[iModifier]);
710 }
711 }
712}
713
714} // namespace
715
716void checkDocument(HS3Document const &doc, Diagnostics &diagnostics, ValidateOptions const &options)
717{
718 Validator{doc, diagnostics, options}.run();
719}
720
721std::optional<ValidatedHS3Document> validate(HS3Document doc, Diagnostics &diagnostics, ValidateOptions const &options)
722{
723 checkDocument(doc, diagnostics, options);
724 if (hasErrors(diagnostics)) {
725 return std::nullopt;
726 }
727 return ValidatedHS3Document{std::move(doc)};
728}
729
730void validateHistFactoryDist(HistFactoryDist const &dist, std::string const &name, Diagnostics &diagnostics)
731{
732 // Without a document, all names are resolved later in the workspace.
733 HS3Document empty;
734 ValidateOptions options;
735 options.isExternal = [](std::string const &) { return true; };
736 Validator{empty, diagnostics, options}.checkHistFactory(dist, name, "");
737}
738
739} // namespace HS3
740} // namespace RooFit
std::string channelName
ValidateOptions const & _options
Diagnostics & _diagnostics
std::map< std::string, std::vector< char const * > > _sections
The sections where a name is defined.
HS3Document const & _doc
std::set< std::string > _parameters
std::string message
#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 r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void 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 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 that the document wa...
A document that passed validate().
const Double_t sigma
const Int_t n
Definition legend1.C:16
double dist(Rotation3D const &r1, Rotation3D const &r2)
Types that represent an HS3 document.
void validateHistFactoryDist(HistFactoryDist const &dist, std::string const &name, Diagnostics &diagnostics)
Same as validate(), but for a single HistFactory distribution that doesn't come with a document.
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.
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,...
bool hasErrors(Diagnostics const &diagnostics)
Whether there is a diagnostic with error severity.
Definition HS3Parser.cxx:94
std::vector< Diagnostic > Diagnostics
std::variant< Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs > GammaConstraint
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
static const char * what
Definition stlLoader.cc:5
Options for validate().
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....
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4