Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooJSONFactoryWSTool.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
5 *
6 * Copyright (c) 2022, 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 <RooFitHS3/JSONIO.h>
15
16#include <RooConstVar.h>
17#include <RooRealVar.h>
18#include <RooBinning.h>
19#include <RooRangeBinning.h>
20#include <RooAbsCategory.h>
21#include <RooAbsLValue.h>
22#include <RooCategory.h>
23#include <RooDataSet.h>
24#include <RooGaussian.h>
25#include <RooDataHist.h>
26#include <RooCustomizer.h>
27#include <RooProdPdf.h>
28#include <RooSimultaneous.h>
29#include <RooSuperCategory.h>
30#include <RooFit/ModelConfig.h>
31#include <RooAbsCollection.h>
32
33#include "JSONIOUtils.h"
34#include "HS3Builder.h"
35#include "JSONReadRecorder.h"
36
37#include <TROOT.h>
38
39#include <algorithm>
40#include <cmath>
41#include <limits>
42#include <iterator>
43#include <fstream>
44#include <functional>
45#include <iostream>
46#include <numeric>
47#include <set>
48#include <sstream>
49#include <stdexcept>
50#include <unordered_map>
51
52/** \class RooJSONFactoryWSTool
53\ingroup roofit_dev_docs_hs3
54
55When using \ref Roofitmain, statistical models can be conveniently handled and
56stored as a RooWorkspace. However, for the sake of interoperability
57with other statistical frameworks, and also ease of manipulation, it
58may be useful to store statistical models in text form.
59
60The RooJSONFactoryWSTool is a helper class to achieve exactly this,
61exporting to and importing from JSON.
62
63In order to import a workspace from a JSON file, you can do
64
65~~~ {.py}
66ws = ROOT.RooWorkspace("ws")
67tool = ROOT.RooJSONFactoryWSTool(ws)
68tool.importJSON("myjson.json")
69~~~
70
71Similarly, in order to export a workspace to a JSON file, you can do
72
73~~~ {.py}
74tool = ROOT.RooJSONFactoryWSTool(ws)
75tool.exportJSON("myjson.json")
76~~~
77
78Analogously, in C++, you can do
79
80~~~ {.cxx}
81#include "RooFitHS3/RooJSONFactoryWSTool.h"
82// ...
83RooWorkspace ws("ws");
84RooJSONFactoryWSTool tool(ws);
85tool.importJSON("myjson.json");
86~~~
87
88and
89
90~~~ {.cxx}
91#include "RooFitHS3/RooJSONFactoryWSTool.h"
92// ...
93RooJSONFactoryWSTool tool(ws);
94tool.exportJSON("myjson.json");
95~~~
96
97For more details, consult the tutorial <a href="rf515__hfJSON_8py.html">rf515_hfJSON</a>.
98
99The RooJSONFactoryWSTool only knows about a limited set of classes for
100import and export. If import or export of a class you're interested in
101fails, you might need to add your own importer or exporter. Please
102consult the relevant section in the \ref roofit_dev_docs to learn how to do that (\ref roofit_dev_docs_hs3).
103
104You can always get a list of all the available importers and exporters by calling the following functions:
105~~~ {.py}
106ROOT.RooFit.JSONIO.printImporters()
107ROOT.RooFit.JSONIO.printExporters()
108~~~
109*/
110
111constexpr auto hs3VersionTag = "0.3";
112
116
117namespace HS3 = RooFit::HS3;
118
119/// What the tool needs to know about the document that it is importing.
121 explicit ToolImportState(HS3Document const &doc) : document{doc}
122 {
123 // Without these indices, every cross-reference resolved during import (e.g. dependencies of a
124 // PiecewiseInterpolation, or factory-expression arguments) would need a linear scan over all sibling objects,
125 // which becomes O(N^2) on workspaces with thousands of entries.
126 for (auto const &function : doc.functions) {
127 functions.emplace(function.name, &function);
128 }
129 for (auto const &distribution : doc.distributions) {
130 distributions.emplace(distribution.name, &distribution);
131 }
132 for (auto const &point : doc.parameterPoints) {
133 if (point.name == "default_values") {
134 defaultValues = &point;
135 for (auto const &p : point.parameters) {
136 defaultValuesByName.emplace(p.name, &p);
137 }
138 break;
139 }
140 }
141 }
142
144 /// The components of combined distributions that have data, and are read as such.
146 std::unordered_map<std::string, HS3::Function const *> functions;
147 std::unordered_map<std::string, HS3::Distribution const *> distributions;
149 std::unordered_map<std::string, HS3::ParameterValue const *> defaultValuesByName;
151};
152
153/// The document that the tool is exporting.
156 JSONNode *root = nullptr;
157 /// The ranges and binnings of the exported variables, by domain and variable name.
158 std::map<std::string, std::map<std::string, HS3::DomainAxis>> domains;
159 /// The categories that are declared by categorical axes of the "default_domain", by name.
160 std::map<std::string, HS3::CategoricalAxis> categories;
161 /// Names of the objects written so far, to export each object once.
162 std::set<std::string> exportedObjectNames;
163 /// Array distributions to write at the end of the export: their components, and the array of each component.
164 std::map<std::string, std::vector<RooAbsPdf const *>> arrayComponents;
165 std::map<std::string, std::string> arrayOfComponent;
166 /// The name of the top-level object whose export is in progress, and the depth of the export recursion.
167 std::string rootName;
168 int depth = 0;
169 /// The constraint terms of the exported HistFactory channels, as written, and the data sets written for them.
170 std::map<std::string, std::vector<std::string>> constraintTermsOfPdf;
171 std::set<std::string> writtenConstraintData;
172 /// The labels that the constraint terms that are written as components of a combined distribution add to its
173 /// index category, by category. See exportSingleModelConfig().
174 std::map<std::string, std::vector<std::string>> auxiliaryLabels;
175 /// The observable of a constraint term, by the name of the term, see setConstraintObservable().
176 std::map<std::string, std::string> constraintObservables;
177 /// Set by the exporter of an object that is interpreted by the document.
178 std::optional<HistFactoryDist> typedObject;
179 std::vector<DistributionRef> channelConstraints;
180};
181
183 std::vector<DistributionRef> channelConstraints)
184{
185 tool._export->typedObject = std::move(dist);
186 tool._export->channelConstraints = std::move(channelConstraints);
187}
188
189namespace {
190
191// Append a name to a list that has every name once.
192void addUnique(std::vector<std::string> &names, std::string const &name)
193{
194 if (std::find(names.begin(), names.end(), name) == names.end()) {
195 names.push_back(name);
196 }
197}
198
199template <class Coll_t>
200std::vector<std::string> namesOf(Coll_t const &objects)
201{
202 std::vector<std::string> names;
203 for (auto const *object : objects) {
204 names.push_back(object->GetName());
205 }
206 return names;
207}
208
209// Whether the states of a category have these labels, in any order.
210bool hasLabels(RooCategory const &cat, std::vector<std::string> const &labels)
211{
212 std::set<std::string> states;
213 for (auto const &state : cat) {
214 states.insert(state.first);
215 }
216 return states == std::set<std::string>(labels.begin(), labels.end());
217}
218
219HS3::Binning toBinning(RooAbsBinning const &binning)
220{
221 if (binning.isUniform()) {
222 return HS3::RegularBinning{binning.numBins()};
223 }
224 HS3::EdgesBinning edges;
225 edges.edges.push_back(binning.binLow(0));
226 for (int i = 0; i < binning.numBins(); ++i) {
227 edges.edges.push_back(binning.binHigh(i));
228 }
229 return edges;
230}
231
232constexpr double inf = std::numeric_limits<double>::infinity();
233constexpr int isInfinite(double x)
234{
235 return (x >= +inf) ? +1 : ((x <= -inf) ? -1 : 0);
236}
237
238// The axis of a domain for a range and binning. Infinite bounds are omitted, and only a bounded range has a binning.
239// A RooRangeBinning is a plain named range without bin structure, fully described by its bounds.
240HS3::DomainAxis toDomainAxis(std::string const &name, RooAbsBinning const &binning)
241{
242 HS3::DomainAxis axis;
243 axis.name = name;
244 if (!isInfinite(binning.lowBound())) {
245 axis.min = binning.lowBound();
246 }
247 if (!isInfinite(binning.highBound())) {
248 axis.max = binning.highBound();
249 }
250 const bool hasBins = binning.numBins() > 0 && !dynamic_cast<RooRangeBinning const *>(&binning);
251 if (axis.min && axis.max && hasBins) {
252 axis.binning = toBinning(binning);
253 }
254 return axis;
255}
256
257// Record the range and binning of a variable in the "default_domain", and its named ranges in the domains of their
258// names.
259void recordDomains(std::map<std::string, std::map<std::string, HS3::DomainAxis>> &domains, RooRealVar const &var)
260{
261 domains[HS3::defaultDomainName][var.GetName()] = toDomainAxis(var.GetName(), var.getBinning());
262 for (auto const &name : var.getBinningNames()) {
263 if (!name.empty()) {
264 domains[name][var.GetName()] = toDomainAxis(var.GetName(), var.getBinning(name.c_str()));
265 }
266 }
267}
268
269// Append a domain with the axes in the order of their names, continuous and categorical ones mixed.
270void appendDomain(std::vector<HS3::Domain> &domains, std::string const &name,
271 std::map<std::string, HS3::DomainAxis> axes,
272 std::map<std::string, HS3::CategoricalAxis> categories = {})
273{
274 std::map<std::string, std::variant<HS3::DomainAxis, HS3::CategoricalAxis>> sorted;
275 for (auto &item : axes) {
276 sorted.emplace(item.first, std::move(item.second));
277 }
278 for (auto &item : categories) {
279 sorted.emplace(item.first, std::move(item.second));
280 }
281 HS3::Domain &domain = domains.emplace_back();
282 domain.name = name;
283 for (auto &item : sorted) {
284 domain.axes.emplace_back(std::move(item.second));
285 }
286}
287
288// Set the binning of a variable, or the binning of one of its named ranges.
289void applyBinning(RooRealVar &var, HS3::Binning const &binning, const char *name = nullptr)
290{
291 if (auto const *edges = std::get_if<HS3::EdgesBinning>(&binning)) {
292 if (edges->edges.empty()) {
293 return;
294 }
295 RooBinning bins(edges->edges.front(), edges->edges.back());
296 for (double edge : edges->edges) {
297 bins.addBoundary(edge);
298 }
299 var.setBinning(bins, name);
300 } else if (int nbins = std::get<HS3::RegularBinning>(binning).nbins) {
301 var.setBins(nbins, name);
302 }
303}
304
305void applyRange(RooRealVar &var, HS3::DomainAxis const &axis)
306{
307 if (axis.min) {
308 if (isInfinite(*axis.min)) {
309 var.removeMin();
310 } else {
311 var.setMin(*axis.min);
312 }
313 }
314 if (axis.max) {
315 if (isInfinite(*axis.max)) {
316 var.removeMax();
317 } else {
318 var.setMax(*axis.max);
319 }
320 }
321 if (axis.binning) {
322 applyBinning(var, *axis.binning);
323 }
324}
325
326// Create the variables of the domains that don't exist yet, and the domains other than the "default_domain" as named
327// ranges.
328void createVariables(RooWorkspace &ws, std::vector<HS3::Domain> const &domains, HS3::DomainRanges const &ranges)
329{
330 auto forEachAxis = [&](auto &&fn) {
331 for (HS3::Domain const &domain : domains) {
332 for (auto const &axis : domain.axes) {
333 if (auto const *continuous = std::get_if<HS3::DomainAxis>(&axis)) {
334 fn(domain.name, *continuous);
335 }
336 }
337 }
338 };
339 // Parameters with a single value are RooConstVars, whichever domain gives the value.
340 forEachAxis([&](std::string const &, HS3::DomainAxis const &axis) {
341 if (axis.fixedValue && !ws.arg(axis.name)) {
342 ws.import(RooConstVar{axis.name.c_str(), axis.name.c_str(), *axis.fixedValue}, RooFit::Silence());
343 }
344 });
345 forEachAxis([&](std::string const &, HS3::DomainAxis const &axis) {
346 if (!axis.fixedValue && ranges.find(axis.name) == &axis && !ws.arg(axis.name)) {
347 RooRealVar var{axis.name.c_str(), axis.name.c_str(), axis.min.value_or(-inf), axis.max.value_or(inf)};
348 if (axis.binning) {
349 applyBinning(var, *axis.binning);
350 }
351 ws.import(var, RooFit::Silence());
352 }
353 });
354 // A categorical axis is a RooCategory with one state per category, in the order of the document and with the
355 // position as index, so that the bins of a RooDataHist follow the order of "categories". An existing RooCategory of
356 // that name must have the same labels. That all domains agree on the categories is checked by the validation.
357 for (HS3::Domain const &domain : domains) {
358 for (auto const &axisVariant : domain.axes) {
359 auto const *axis = std::get_if<HS3::CategoricalAxis>(&axisVariant);
360 if (!axis) {
361 continue;
362 }
363 if (RooAbsArg *existing = ws.arg(axis->name)) {
364 auto *cat = dynamic_cast<RooCategory *>(existing);
365 if (!cat) {
366 RooJSONFactoryWSTool::error("the axis '" + axis->name + "' is categorical, but '" + axis->name +
367 "' already exists and is not a category");
368 }
369 if (!hasLabels(*cat, axis->categories)) {
370 RooJSONFactoryWSTool::error("the categories of the axis '" + axis->name +
371 "' differ from the states of the existing category '" + axis->name + "'");
372 }
373 continue;
374 }
375 RooCategory cat{axis->name.c_str(), axis->name.c_str()};
376 for (std::size_t i = 0; i < axis->categories.size(); ++i) {
377 cat.defineType(axis->categories[i], static_cast<int>(i));
378 }
379 ws.import(cat, RooFit::Silence());
380 }
381 }
382 forEachAxis([&](std::string const &domain, HS3::DomainAxis const &axis) {
383 RooRealVar *var = ws.var(axis.name);
384 if (var && domain != HS3::defaultDomainName) {
385 var->setRange(domain.c_str(), axis.min.value_or(-inf), axis.max.value_or(inf));
386 if (axis.binning) {
387 applyBinning(*var, *axis.binning, domain.c_str());
388 }
389 }
390 });
391}
392
393} // namespace
394
395namespace {
396
397// Ends an import or an export: its state must not survive it, not even if there is an exception. The state of an
398// import refers to the document.
399template <class State_t>
400struct StateScope {
401 std::unique_ptr<State_t> &state;
402 ~StateScope() { state.reset(); }
403};
404
405// Keeps track of the objects that are being imported. Objects that depend on each other would otherwise recurse until
406// the stack overflows.
407class ImportGuard {
408public:
409 ImportGuard(std::vector<std::string> &stack, std::string const &name) : _stack{stack}
410 {
411 if (std::find(_stack.begin(), _stack.end(), name) != _stack.end()) {
412 std::string chain;
413 for (std::string const &ancestor : _stack) {
414 chain += ancestor + " -> ";
415 }
416 RooJSONFactoryWSTool::error("circular dependency in the document: " + chain + name);
417 }
418 _stack.push_back(name);
419 }
420 ~ImportGuard() { _stack.pop_back(); }
421
422private:
423 std::vector<std::string> &_stack;
424};
425
426} // namespace
427
428void RooFit::HS3::report(Diagnostics const &diagnostics, std::string const &object)
429{
430 std::string errors;
431 for (Diagnostic const &diagnostic : diagnostics) {
432 // A path starts with the name of a component, or with the index of an entry if the object is an array.
433 const bool needsDot = !object.empty() && !diagnostic.path.empty() && diagnostic.path.front() != '[';
434 const std::string path = object + (needsDot ? "." : "") + diagnostic.path;
435 const std::string text = path.empty() ? diagnostic.message : path + ": " + diagnostic.message;
436 if (diagnostic.severity == Diagnostic::Severity::Error) {
437 errors += (errors.empty() ? "" : "\n") + text;
438 } else {
439 RooJSONFactoryWSTool::warning("RooFitHS3: " + text);
440 }
441 }
442 if (!errors.empty()) {
444 }
445}
446
447namespace {
448
449// Configure `v` (value, error, binning, constness) from the parameter `p`.
450void configureVariable(HS3::DomainRanges const &ranges, HS3::ParameterValue const &p, RooRealVar &v)
451{
452 if (p.value) {
453 if (auto const *value = std::get_if<double>(&*p.value)) {
454 v.setVal(*value);
455 }
456 }
457 if (HS3::DomainAxis const *range = ranges.find(v.GetName())) {
458 applyRange(v, *range);
459 }
460 if (p.nbins)
461 v.setBins(*p.nbins);
462 if (p.relError)
463 v.setError(v.getVal() * *p.relError);
464 if (p.error)
465 v.setError(*p.error);
466 v.setConstant(p.isConst);
467}
468
469std::string genPrefix(const JSONNode &p, bool trailing_underscore)
470{
471 std::string prefix;
472 if (!p.is_map())
473 return prefix;
474 if (auto node = p.find("namespaces")) {
475 for (const auto &ns : node->children()) {
476 if (!prefix.empty())
477 prefix += "_";
478 prefix += ns.val();
479 }
480 }
481 if (trailing_underscore && !prefix.empty())
482 prefix += "_";
483 return prefix;
484}
485
486// Import string attributes ("dict") and boolean tags ("tags") onto `arg`.
487void importAttributes(RooAbsArg *arg, HS3::Attributes const &attributes)
488{
489 for (auto const &attr : attributes.dict) {
490 arg->setStringAttribute(attr.first.c_str(), attr.second.c_str());
491 }
492 for (auto const &tag : attributes.tags) {
493 arg->setAttribute(tag.c_str());
494 }
495}
496
497// The step widths of the minimization are the errors of the parameters of the ModelConfigs, and of the parameters of
498// the top-level pdfs given the data sets. The observables are no parameters.
499void exportParameterStepWidths(RooWorkspace const &workspace, std::vector<RooAbsPdf *> const &pdfs,
500 std::vector<RooAbsData *> const &data, std::vector<HS3::StepWidth> &out)
501{
502 RooArgSet candidates;
503 RooArgSet excluded;
504 auto addIfPresent = [](RooArgSet &set, RooArgSet const *args) {
505 if (args) {
506 set.add(*args, true);
507 }
508 };
509 for (TObject *obj : workspace.allGenericObjects()) {
510 if (auto const *mc = dynamic_cast<RooFit::ModelConfig const *>(obj)) {
511 addIfPresent(candidates, mc->GetParametersOfInterest());
512 addIfPresent(candidates, mc->GetNuisanceParameters());
513 addIfPresent(excluded, mc->GetObservables());
514 addIfPresent(excluded, mc->GetGlobalObservables());
515 addIfPresent(excluded, mc->GetConditionalObservables());
516 }
517 }
518 for (RooAbsPdf const *pdf : pdfs) {
519 RooArgSet observables;
520 for (RooAbsData const *dataset : data) {
521 std::unique_ptr<RooArgSet> pdfObs{pdf->getObservables(*dataset->get())};
522 observables.add(*pdfObs, true);
523 }
524 if (!observables.empty()) {
525 RooArgSet params;
526 pdf->getParameters(&observables, params);
527 candidates.add(params, true);
528 excluded.add(observables, true);
529 }
530 }
531 candidates.sort();
532 for (RooAbsArg *arg : candidates) {
533 auto *var = dynamic_cast<RooRealVar *>(arg);
534 if (var && var->hasError() && !excluded.find(*arg)) {
535 out.push_back({var->GetName(), var->getError()});
536 }
537 }
538}
539
540void importParameterStepWidths(RooWorkspace &workspace, HS3Document const &doc)
541{
542 for (HS3::StepWidth const &stepWidth : doc.rooFit.stepWidths) {
543 auto *var = dynamic_cast<RooRealVar *>(workspace.arg(stepWidth.name));
544 if (!var) {
545 RooJSONFactoryWSTool::warning("RooFitHS3: skipping parameter_stepwidths entry for unknown or non-real "
546 "variable '" +
547 stepWidth.name + "'.");
548 continue;
549 }
550 var->setError(stepWidth.width);
551 }
552}
553
554// Check that the terms of the likelihoods have distinct variates (see HS3::sharedVariates()) and list the constraints
555// of their HistFactory modifiers (see HS3::unlistedConstraints()). Documents of versions before 0.3, which RooFit
556// wrote with shared variates itself, are read with a warning. Returns the bookkeeping columns, which may be shared.
557std::set<std::string> checkVariates(HS3Document const &doc)
558{
559 const bool isLegacy = doc.metadata.version < HS3::Version{0, 3};
560 std::set<std::string> bookkeepingColumns;
561 for (auto const &[dataset, columns] : HS3::bookkeepingColumns(doc)) {
562 for (std::string const &column : columns) {
564 "RooFitHS3: the axis '" + column + "' of the components of the combined data set '" + dataset +
565 "' is declared as a bookkeeping column without statistical meaning (misc.ROOT_internal."
566 "bookkeeping_columns), so it may be shared by the components, although HS3 requires distinct axes.");
567 bookkeepingColumns.insert(column);
568 }
569 }
570 for (std::string const &message : HS3::sharedVariates(doc)) {
571 if (!isLegacy) {
572 RooJSONFactoryWSTool::error("invalid HS3: " + message +
573 ". The terms of a likelihood and the components of a combined distribution or "
574 "data set must have distinct variates.");
575 }
576 RooJSONFactoryWSTool::warning("RooFitHS3: " + message +
577 ". HS3 0.3 requires distinct variates; the document is read anyway, since it "
578 "declares an older version.");
579 }
580 if (!isLegacy) {
581 std::vector<std::string> notes;
582 for (std::string const &message : HS3::unlistedConstraints(doc, &notes)) {
583 RooJSONFactoryWSTool::error("invalid HS3: " + message +
584 ". The \"constraints\" of a modifier add no term to the likelihood, which has "
585 "to list them.");
586 }
587 for (std::string const &note : notes) {
588 RooJSONFactoryWSTool::warning("RooFitHS3: " + note + ".");
589 }
590 }
591 return bookkeepingColumns;
592}
593
594// Declare the bookkeeping columns of the exported document (see HS3::bookkeepingColumns()): the axes that several
595// components of a combined data set share, and whose variable in the workspace `ws` is used by nothing else, i.e. has
596// no clients and is in no set of a ModelConfig. RooAbsData::split() keeps such a column (e.g. an event number) in
597// every sub-data set.
598void declareBookkeepingColumns(HS3Document &doc, RooWorkspace const &ws)
599{
600 std::vector<RooArgSet const *> modelConfigSets;
601 for (TObject *obj : ws.allGenericObjects()) {
602 if (auto *mc = dynamic_cast<RooFit::ModelConfig *>(obj)) {
603 for (RooArgSet const *set : {mc->GetObservables(), mc->GetParametersOfInterest(), mc->GetNuisanceParameters(),
604 mc->GetGlobalObservables(), mc->GetConditionalObservables()}) {
605 if (set) {
606 modelConfigSets.push_back(set);
607 }
608 }
609 }
610 }
611 auto usedByNothing = [&](std::string const &name) {
612 RooAbsArg const *arg = ws.arg(name.c_str());
613 if (!arg || !arg->clients().empty()) {
614 return false;
615 }
616 return std::none_of(modelConfigSets.begin(), modelConfigSets.end(),
617 [&](RooArgSet const *set) { return set->find(name.c_str()) != nullptr; });
618 };
619 for (auto const &[dataset, axes] : HS3::sharedAxes(doc)) {
620 for (std::string const &axis : axes) {
621 if (usedByNothing(axis)) {
622 doc.rooFit.bookkeepingColumns[dataset].push_back(axis);
623 }
624 }
625 }
626}
627
628// Export the string attributes and tags of `arg` into the ROOT-internal attributes.
629void exportAttributes(const RooAbsArg *arg, std::map<std::string, HS3::Attributes> &attributes)
630{
631 // If this RooConst is a literal number, we don't need to export the attributes.
632 if (isLiteralConstVar(*arg) || dynamic_cast<RooConstVar const *>(arg)) {
633 return;
634 }
635
636 HS3::Attributes *out = nullptr;
637 auto initialize = [&]() {
638 if (!out) {
639 out = &attributes[arg->GetName()];
640 }
641 };
642
643 for (const auto &it : arg->stringAttributes()) {
644 // Skip some RooFit internals
645 if (it.first == "factory_tag" || it.first == "PROD_TERM_TYPE")
646 continue;
647 initialize();
648 out->dict[it.first] = it.second;
649 }
650 for (auto const &attr : arg->attributes()) {
651 // Skip some RooFit internals. The array distribution tag is restored from the type on import.
652 if (attr == "SnapShot_ExtRefClone" || attr == "RooRealConstant_Factory_Object" || attr == arrayDistTag ||
654 continue;
655 initialize();
656 out->tags.push_back(attr);
657 }
658}
659
660} // namespace
661
667
669{
670 return {name, binning.lowBound(), binning.highBound(), toBinning(binning)};
671}
672
674{
675 if (isLiteralConstVar(arg)) {
676 return static_cast<RooConstVar const &>(arg).getVal();
677 }
678 return std::string{arg.GetName()};
679}
680
681namespace {
682
683// The axis of unbinned or point data for a variable. Infinite bounds are left out: the axis is unbounded there.
684HS3::ContinuousAxis toContinuousAxis(RooRealVar const &var)
685{
688 axis.name = var.GetName();
689 if (!RooNumber::isInfinite(var.getMin())) {
690 axis.min = var.getMin();
691 }
692 if (!RooNumber::isInfinite(var.getMax())) {
693 axis.max = var.getMax();
694 }
695 return axis;
696}
697
698// A categorical axis has the labels in the order of the states of the category, which is the order of the bins of a
699// RooDataHist.
700HS3::CategoricalAxis toCategoricalAxis(RooAbsCategory const &cat)
701{
703 axis.name = cat.GetName();
704 for (unsigned int i = 0; i < cat.size(); ++i) {
705 axis.categories.push_back(cat.getOrdinal(i).first);
706 }
707 return axis;
708}
709
710// Binned data over variables that are RooRealVars or RooCategories.
711HS3::BinnedData toBinnedData(RooArgSet const &vars, std::size_t n, double const *contents)
712{
714 for (RooAbsArg const *var : vars) {
715 if (auto const *cat = dynamic_cast<RooCategory const *>(var)) {
717 data.axes.emplace_back(toCategoricalAxis(*cat));
718 } else {
719 data.axes.emplace_back(HS3::toBinnedAxis(static_cast<RooRealVar const &>(*var)));
720 }
721 }
722 data.contents.assign(contents, contents + n);
723 return data;
724}
725
726std::unique_ptr<RooRealVar> createObservable(HS3::BinnedAxis const &axis)
727{
728 auto obs = std::make_unique<RooRealVar>(axis.name.c_str(), axis.name.c_str(), axis.min, axis.max);
729 applyBinning(*obs, axis.binning);
730 return obs;
731}
732
733// Create the variables for the axes of binned data. A categorical axis is one bin per category. HS3 identifies
734// categories by name, so the data may list them in another order than the domain that declares the category.
735// RooDataHist looks up the bin of a category by the order of the states of the category it is given, so the category
736// of the data takes over the states and their order from the category of the workspace. `binOrder` is filled with the
737// position in the document of each bin in RooFit's order (last axis fastest), or left empty if the orders agree.
738RooArgSet createBinnedObservables(std::vector<std::variant<HS3::BinnedAxis, HS3::CategoricalAxis>> const &axes,
739 RooWorkspace const &workspace, std::string const &dataName,
740 std::vector<std::size_t> &binOrder)
741{
742 RooArgSet vars;
743 std::vector<std::vector<std::size_t>> axisPositions;
744 bool reordered = false;
745 for (auto const &axisVariant : axes) {
746 if (auto const *axis = std::get_if<HS3::BinnedAxis>(&axisVariant)) {
747 auto obs = createObservable(*axis);
748 std::vector<std::size_t> positions(obs->numBins());
749 std::iota(positions.begin(), positions.end(), 0);
750 axisPositions.push_back(std::move(positions));
751 vars.addOwned(std::move(obs));
752 continue;
753 }
754 auto const &axis = std::get<HS3::CategoricalAxis>(axisVariant);
755 auto cat = std::make_unique<RooCategory>(axis.name.c_str(), axis.name.c_str());
756 if (RooCategory const *wsCat = workspace.cat(axis.name)) {
757 if (!hasLabels(*wsCat, axis.categories)) {
758 RooJSONFactoryWSTool::error("the categories of the axis '" + axis.name + "' of the dataset '" + dataName +
759 "' differ from those of the category '" + axis.name + "'");
760 }
761 for (unsigned int i = 0; i < wsCat->size(); ++i) {
762 auto const &state = wsCat->getOrdinal(i);
763 cat->defineType(state.first, state.second);
764 }
765 } else {
766 for (std::size_t i = 0; i < axis.categories.size(); ++i) {
767 cat->defineType(axis.categories[i], static_cast<int>(i));
768 }
769 }
770 std::vector<std::size_t> positions(axis.categories.size());
771 for (std::size_t i = 0; i < positions.size(); ++i) {
772 auto found = std::find(axis.categories.begin(), axis.categories.end(), cat->getOrdinal(i).first);
773 positions[i] = std::distance(axis.categories.begin(), found);
774 reordered |= positions[i] != i;
775 }
776 axisPositions.push_back(std::move(positions));
777 vars.addOwned(std::move(cat));
778 }
779 binOrder.clear();
780 if (!reordered) {
781 return vars;
782 }
783 std::size_t total = 1;
784 for (auto const &positions : axisPositions) {
785 total *= positions.size();
786 }
787 binOrder.resize(total);
788 for (std::size_t k = 0; k < total; ++k) {
789 // Decompose RooFit's bin index (last axis fastest) and recompose it with the positions in the document.
790 std::size_t rest = k;
791 std::size_t docIndex = 0;
792 std::size_t stride = 1;
793 for (std::size_t j = axisPositions.size(); j-- > 0;) {
794 const std::size_t nBins = axisPositions[j].size();
795 docIndex += axisPositions[j][rest % nBins] * stride;
796 rest /= nBins;
797 stride *= nBins;
798 }
799 binOrder[k] = docIndex;
800 }
801 return vars;
802}
803
804// The values in RooFit's bin order, from the order of the document (see createBinnedObservables()).
805std::vector<double> permute(std::vector<double> const &values, std::vector<std::size_t> const &order)
806{
807 if (order.empty() || values.size() != order.size()) {
808 return values;
809 }
810 std::vector<double> out(values.size());
811 for (std::size_t k = 0; k < order.size(); ++k) {
812 out[k] = values[order[k]];
813 }
814 return out;
815}
816
817// Make the range of an observable of unbinned data consistent with the "min" and "max" of the axis. Variables that
818// didn't get their range from a domain take it from the axis. If `ranges` is null, all ranges are considered explicit.
819void applyUnbinnedAxisRange(HS3::ContinuousAxis const &axis, std::string const &dataName, RooRealVar &var,
820 HS3::DomainRanges const *ranges)
821{
822 // An absent bound means that the axis is unbounded on that side.
823 const double min = axis.min.value_or(-RooNumber::infinity());
824 const double max = axis.max.value_or(RooNumber::infinity());
825 if (ranges && !ranges->contains(axis.name)) {
826 var.setRange(min, max);
827 } else if (var.getMin() != min || var.getMax() != max) {
828 std::stringstream ss;
829 ss << "RooFitHS3: the range [" << min << ", " << max << "] of the axis '" << axis.name << "' of the dataset '"
830 << dataName << "' differs from the range [" << var.getMin() << ", " << var.getMax() << "] of the variable.";
832 }
833}
834
835std::unique_ptr<RooAbsData> createUnbinnedData(std::string const &name, HS3::UnbinnedData const &input,
836 RooWorkspace &workspace, HS3::DomainRanges const *ranges)
837{
838 RooArgList varlist;
839 for (auto const &axisVariant : input.axes) {
840 auto const *axis = std::get_if<HS3::ContinuousAxis>(&axisVariant);
841 if (!axis) {
842 // The category of a categorical axis is declared by a domain.
843 auto const &catAxis = std::get<HS3::CategoricalAxis>(axisVariant);
844 RooCategory *cat = workspace.cat(catAxis.name);
845 if (!cat) {
846 RooJSONFactoryWSTool::error("The categorical observable \"" + catAxis.name +
847 "\" could not be found in the workspace! Categorical observables are "
848 "declared by a categorical axis in a domain.");
849 }
850 if (!hasLabels(*cat, catAxis.categories)) {
851 RooJSONFactoryWSTool::error("The categories of the axis \"" + catAxis.name +
852 "\" differ from the states of the category \"" + catAxis.name + "\"");
853 }
854 varlist.add(*cat);
855 continue;
856 }
857 RooRealVar *var = workspace.var(axis->name);
858 if (!var) {
859 RooJSONFactoryWSTool::error("The observable \"" + axis->name + "\" could not be found in the workspace!");
860 }
861 varlist.add(*var);
862 applyUnbinnedAxisRange(*axis, name, *var, ranges);
863 }
864 RooArgSet vars(varlist);
865 auto data = std::make_unique<RooDataSet>(name, name, vars, RooFit::WeightVar());
866 const std::size_t nAxes = input.axes.size();
867 for (std::size_t i = 0; i < input.numEntries(); ++i) {
868 for (std::size_t j = 0; j < nAxes; ++j) {
869 const double value = input.entries[i * nAxes + j];
870 if (auto const *catAxis = std::get_if<HS3::CategoricalAxis>(&input.axes[j])) {
871 // The entry of a categorical axis is the position of the label in its "categories".
872 static_cast<RooCategory *>(varlist.at(j))->setLabel(catAxis->categories[static_cast<std::size_t>(value)]);
873 } else {
874 static_cast<RooRealVar *>(varlist.at(j))->setVal(value);
875 }
876 }
877 data->add(vars, input.weights ? (*input.weights)[i] : 1.);
878 }
879 return data;
880}
881
882// Create a RooAbsData (binned or unbinned) from its description.
883std::unique_ptr<RooAbsData> loadData(HS3::Data const &input, RooWorkspace &workspace, HS3::DomainRanges const *ranges)
884{
886
887 // What RooFit can't store is reported, instead of being dropped silently.
888 if (auto const *binned = std::get_if<HS3::BinnedData>(&input.body)) {
889 if (binned->uncertainty && !binned->uncertainty->correlation.empty()) {
890 RooJSONFactoryWSTool::warning("RooFitHS3: the correlations of the uncertainties of the dataset '" +
891 input.name + "' are ignored, because RooDataHist can't store them.");
892 }
893 std::vector<std::size_t> binOrder;
894 RooArgSet vars = createBinnedObservables(binned->axes, workspace, input.name, binOrder);
895 std::vector<double> sigma;
896 if (binned->uncertainty) {
897 sigma = permute(binned->uncertainty->sigma, binOrder);
898 }
899 return HS3::createDataHist(input.name, vars, permute(binned->contents, binOrder),
900 binned->uncertainty ? &sigma : nullptr);
901 }
902 if (auto const *unbinned = std::get_if<HS3::UnbinnedData>(&input.body)) {
903 if (unbinned->hasEntriesUncertainties) {
904 RooJSONFactoryWSTool::warning("RooFitHS3: the \"entries_uncertainties\" of the dataset '" + input.name +
905 "' are ignored, because RooDataSet can't store them.");
906 }
907 return createUnbinnedData(input.name, *unbinned, workspace, ranges);
908 }
909 // Point data is the observation of a single variable. With its axis, it is the same as unbinned data with a single
910 // entry, and becomes a RooDataSet with one entry.
911 auto const &point = std::get<HS3::PointData>(input.body);
912 if (point.uncertainty) {
913 RooJSONFactoryWSTool::warning("RooFitHS3: the \"uncertainty\" of the dataset '" + input.name +
914 "' is ignored: HS3 doesn't define how it enters a likelihood.");
915 }
916 // Without an axis, the value is the one of the single variate of the paired distribution, see
917 // HS3::resolvePointData().
918 if (point.axes.empty()) {
919 RooJSONFactoryWSTool::error("the point data set '" + input.name +
920 "' has no axis, and it is not paired with a distribution that has a single variate");
921 } else if (point.axes.size() != 1) {
922 RooJSONFactoryWSTool::error("the point data set '" + input.name + "' must have exactly one axis");
923 }
925 unbinned.axes.emplace_back(point.axes.front());
926 unbinned.entries.push_back(point.value);
927 return createUnbinnedData(input.name, unbinned, workspace, ranges);
928}
929
930// The version of ROOT that writes the document, with dots only such that it can be read automatically.
931std::string rootVersion()
932{
933 std::string versionName = gROOT->GetVersion();
934 std::replace(versionName.begin(), versionName.end(), '/', '.');
935 return versionName;
936}
937
938// The data set `name` among the loaded ones that are not a component of a combined data set by now, or the data of a
939// constraint term, or nullptr.
940std::unique_ptr<RooAbsData> *findDataset(std::vector<std::unique_ptr<RooAbsData>> &datasets, std::string const &name)
941{
942 auto found = std::find_if(datasets.begin(), datasets.end(), [&](auto &d) { return d && d->GetName() == name; });
943 return found != datasets.end() ? &*found : nullptr;
944}
945
946// A distribution of the document can be a function in RooFit, or it can be skipped.
947void requirePdf(RooAbsArg const *arg, std::string const &what)
948{
949 if (!dynamic_cast<RooAbsPdf const *>(arg)) {
951 (arg ? std::string("a ") + arg->ClassName() + ", not a pdf" : "not imported"));
952 }
953}
954
955// Check that a combined data set paired with a RooSimultaneous in a likelihood has a sub-data set for every component.
956// RooFit has no notion of an unobserved component: a key without sub-data set is a channel without entries, which
957// adds the expected number of events of an extended component to the likelihood. That changes the fit, so it is an
958// error (https://github.com/root-project/root/issues/24004). For a non-extended component it changes nothing, and a
959// sub-data set without component is ignored by RooFit; both are reported as warnings.
960void checkCombinedPairing(RooSimultaneous const &simPdf, std::string const &dataName, HS3::CombinedData const &data)
961{
962 std::vector<RooCategory const *> columns = selectionColumns(simPdf.indexCat());
963 const std::vector<std::string> columnNames = namesOf(columns);
964 std::vector<std::string> dataColumns = data.columns;
965 std::sort(dataColumns.begin(), dataColumns.end());
966 if (dataColumns != columnNames) {
967 RooJSONFactoryWSTool::error("the likelihood pairs '" + std::string(simPdf.GetName()) +
968 "' with the combined data set '" + dataName + "', but their columns differ");
969 }
970 // The keys of the data, each as a map from the column to the label.
971 std::set<std::map<std::string, std::string>> dataKeys;
972 for (auto const &component : data.components) {
973 std::map<std::string, std::string> key;
974 for (std::size_t i = 0; i < data.columns.size(); ++i) {
975 key[data.columns[i]] = component.key[i];
976 }
977 dataKeys.insert(key);
978 }
979 auto keyToString = [&](std::map<std::string, std::string> const &key) {
980 std::vector<std::string> labels;
981 for (std::string const &column : data.columns) {
982 labels.push_back(key.at(column));
983 }
984 return selectionKeyToString(labels);
985 };
986 std::set<std::map<std::string, std::string>> pdfKeys;
987 for (std::vector<std::string> const &labels : selectionKeys(columns)) {
988 RooAbsPdf const *component = simPdf.getPdf(selectionLabel(simPdf.indexCat(), columns, labels));
989 if (!component) {
990 continue;
991 }
992 std::map<std::string, std::string> key;
993 for (std::size_t i = 0; i < columns.size(); ++i) {
994 key[columnNames[i]] = labels[i];
995 }
996 pdfKeys.insert(key);
997 if (dataKeys.count(key) > 0) {
998 continue;
999 }
1000 if (component->canBeExtended()) {
1002 "the combined data set '" + dataName + "' has no sub-data set for the key " + keyToString(key) +
1003 " of the extended component '" + component->GetName() + "' of '" + simPdf.GetName() +
1004 "'. RooFit cannot leave a component unobserved: it would add the expected number of events of the " +
1005 "component to the likelihood, see https://github.com/root-project/root/issues/24004");
1006 }
1007 RooJSONFactoryWSTool::warning("RooFitHS3: the combined data set '" + dataName +
1008 "' has no sub-data set for the key " + keyToString(key) + " of the component '" +
1009 component->GetName() + "' of '" + simPdf.GetName() +
1010 "', which RooFit treats as present and empty.");
1011 }
1012 for (auto const &key : dataKeys) {
1013 if (pdfKeys.count(key) == 0) {
1014 RooJSONFactoryWSTool::warning("RooFitHS3: the sub-data set for the key " + keyToString(key) +
1015 " of the combined data set '" + dataName + "' is ignored, because '" +
1016 simPdf.GetName() + "' has no component for it.");
1017 }
1018 }
1019}
1020
1021// The observables of the analysis `analysis` if no data set of its likelihood gives them: the list that RooFit writes
1022// in misc.ROOT_internal.ModelConfigs for a ModelConfig without data, or else the variables that HS3 makes
1023// observables. By the standard, all parameters of the distributions of the likelihood are listed in a domain of the
1024// analysis or are constant, so a variate of the distributions of the likelihood (see HS3::variatesOf(), the columns
1025// of a combined distribution are observables as well) that is neither, is an observable. Requiring all three excludes
1026// the parameters, and the observed values of the constraint terms, which are constant parameters.
1027void observablesWithoutData(HS3Document const &doc, HS3::Analysis const &analysis, HS3::Likelihood const &likelihood,
1028 HS3::ModelConfigInfo const *mcInfo, RooWorkspace &workspace, RooArgSet &observables)
1029{
1030 if (mcInfo && mcInfo->observables) {
1031 for (std::string const &name : *mcInfo->observables) {
1032 if (RooAbsArg *arg = workspace.arg(name)) {
1033 observables.add(*arg);
1034 } else {
1035 RooJSONFactoryWSTool::warning("RooFitHS3: the observable '" + name + "' of the analysis '" + analysis.name +
1036 "' (misc.ROOT_internal.ModelConfigs) does not exist");
1037 }
1038 }
1039 return;
1040 }
1041 std::set<std::string> inDomains;
1042 for (auto const &domainRef : analysis.domains) {
1043 if (HS3::Domain const *domain = HS3::findByName(doc.domains, domainRef.name)) {
1044 for (auto const &axis : domain->axes) {
1045 inDomains.insert(HS3::nameOf(axis));
1046 }
1047 }
1048 }
1049 for (auto const &poi : analysis.parametersOfInterest) {
1050 inDomains.insert(poi.name);
1051 }
1052 std::string list;
1053 for (std::string const &variate : HS3::variatesOf(doc, likelihood)) {
1054 RooAbsArg *arg = workspace.arg(variate);
1055 if (!dynamic_cast<RooAbsRealLValue *>(arg) && !dynamic_cast<RooAbsCategoryLValue *>(arg)) {
1056 continue;
1057 }
1058 if (inDomains.count(variate) || arg->isConstant()) {
1059 continue;
1060 }
1061 observables.add(*arg);
1062 list += (list.empty() ? "'" : ", '") + variate + "'";
1063 }
1064 if (!list.empty()) {
1065 oocoutI(nullptr, IO) << "RooFitHS3: no data set of the likelihood of the analysis '" << analysis.name
1066 << "' gives its observables; they are the variates of the likelihood that are neither in a "
1067 "domain of the analysis nor constant: "
1068 << list << std::endl;
1069 }
1070}
1071
1072// Whether the term `term` of a likelihood is a constraint term of the channel `channel` of a model with the channels
1073// `channels`: it has a global observable (a variable that no channel depends on) and another variable that the
1074// channel depends on. An array of constraints is one term.
1075bool isConstraintOfChannel(RooAbsPdf const &term, RooAbsPdf const &channel, RooArgSet const &channels)
1076{
1077 if (&term == &channel || channels.containsInstance(term)) {
1078 return false;
1079 }
1080 RooArgSet leaves;
1081 term.leafNodeServerList(&leaves);
1082 RooArgSet parameters;
1083 bool hasGlobalObservable = false;
1084 for (RooAbsArg *leaf : leaves) {
1085 auto *var = dynamic_cast<RooRealVar *>(leaf);
1086 if (!var) {
1087 continue;
1088 }
1089 // Whether a global observable is constant yet depends on where the document has its value: as a parameter, or
1090 // in the data set of the constraint, which is read later.
1091 if (std::none_of(channels.begin(), channels.end(),
1092 [&](RooAbsArg const *other) { return other->dependsOn(*var); })) {
1093 hasGlobalObservable = true;
1094 } else {
1095 parameters.add(*var);
1096 }
1097 }
1098 return hasGlobalObservable && !parameters.empty() && channel.dependsOn(parameters);
1099}
1100
1101// The other terms of the likelihoods of the document that have the distribution `dist` as a term: their other
1102// distributions and their auxiliary distributions. These are the candidates for constraint terms of `dist`.
1103std::vector<std::string> otherLikelihoodTerms(HS3Document const &doc, std::string const &dist)
1104{
1105 std::vector<std::string> out;
1106 auto add = [&](std::string const &name) {
1107 if (name != dist) {
1108 addUnique(out, name);
1109 }
1110 };
1111 for (auto const &likelihood : doc.likelihoods) {
1112 if (std::none_of(likelihood.terms.begin(), likelihood.terms.end(),
1113 [&](auto const &term) { return term.distribution.name == dist; })) {
1114 continue;
1115 }
1116 for (auto const &term : likelihood.terms) {
1117 add(term.distribution.name);
1118 }
1119 for (auto const &aux : likelihood.auxDistributions) {
1120 add(aux.name);
1121 }
1122 }
1123 return out;
1124}
1125
1126// In HS3, the constraint terms of a model are terms of the likelihood; in RooFit, as HistFactory builds its models, a
1127// constraint is a factor of every channel whose parameters it constrains, which makes the pdf of the model the
1128// sampling distribution of the auxiliary measurements as well. The export takes the constraints out of the channels
1129// (writeLikelihoodTermsOnce()); this puts them back: a constraint term with a global observable (a constant variable
1130// that the model itself does not depend on) becomes a factor of every channel that is a product and depends on one
1131// of its other parameters. An array of constraints goes to its channels as a whole. The constraints that
1132// found a channel are removed from `constraints`; the others stay external constraints of the ModelConfig, e.g. a
1133// penalty term without an observation, like the regularization of an unfolding, or a constraint of a model whose
1134// channels are not products.
1135void attachConstraintsToChannels(RooAbsPdf &pdf, RooArgSet &constraints)
1136{
1137 RooArgSet channels;
1138 if (auto *simPdf = dynamic_cast<RooSimultaneous *>(&pdf)) {
1139 for (auto const &state : simPdf->indexCat()) {
1140 if (RooAbsPdf *channel = simPdf->getPdf(state.first)) {
1141 channels.add(*channel, /*silent=*/true);
1142 }
1143 }
1144 } else {
1145 channels.add(pdf);
1146 }
1147 RooArgSet attached;
1148 for (auto *constraint : static_range_cast<RooAbsPdf *>(constraints)) {
1149 // Decided for all channels before any is changed: a channel that has the constraint depends on its global
1150 // observable, which then would not look like one anymore.
1151 bool isFactor = false;
1152 std::vector<RooProdPdf *> products;
1153 for (auto *channel : static_range_cast<RooAbsPdf *>(channels)) {
1154 auto *product = dynamic_cast<RooProdPdf *>(channel);
1155 if (!product || channel == constraint) {
1156 continue;
1157 }
1158 if (product->pdfList().find(*constraint)) {
1159 isFactor = true;
1160 } else if (isConstraintOfChannel(*constraint, *channel, channels)) {
1161 products.push_back(product);
1162 }
1163 }
1164 for (RooProdPdf *product : products) {
1165 product->addPdfs(RooArgSet{*constraint});
1166 }
1167 if (isFactor || !products.empty()) {
1168 attached.add(*constraint);
1169 }
1170 }
1171 constraints.remove(attached);
1172}
1173
1174// Import an analysis (likelihood + domains) as one or more ModelConfig objects into the workspace.
1175void importAnalysis(HS3Document const &doc, HS3::Analysis const &analysis, RooWorkspace &workspace,
1176 std::vector<std::unique_ptr<RooAbsData>> &datasets, std::set<std::string> const &bookkeepingColumns,
1177 std::set<std::string> const &auxiliaryMeasurements)
1178{
1179 std::string const &analysisName = analysis.name;
1180 auto foundInfo = doc.rooFit.modelConfigs.find(analysisName);
1181 HS3::ModelConfigInfo const *mcInfo = foundInfo != doc.rooFit.modelConfigs.end() ? &foundInfo->second : nullptr;
1182
1183 std::string mcname = mcInfo && mcInfo->mcName ? *mcInfo->mcName : analysisName;
1184 if (workspace.obj(mcname))
1185 return;
1186
1187 workspace.import(RooFit::ModelConfig{mcname.c_str(), mcname.c_str()});
1188 auto *mc = static_cast<RooFit::ModelConfig *>(workspace.obj(mcname));
1189 mc->SetWS(workspace);
1190
1191 // The validation has checked that the likelihood exists.
1192 HS3::Likelihood const &likelihood = *HS3::findByName(doc.likelihoods, analysis.likelihood.name);
1193
1194 auto checkPdf = [&](RooAbsArg const *arg, std::string const &name, std::string const &what) {
1195 requirePdf(arg, "the " + what + " '" + name + "' of analysis '" + analysisName + "'");
1196 };
1197
1198 for (auto const &term : likelihood.terms) {
1199 checkPdf(workspace.arg(term.distribution.name), term.distribution.name, "distribution");
1200 }
1201
1202 // The constraint terms of the HistFactory distributions of the likelihood enter through those distributions.
1203 // Listed in "aux_distributions", they are not added again; paired with a data set in "distributions", they are
1204 // not channels, and the data set gives the observed values of their observables.
1205 std::vector<std::unique_ptr<RooArgSet>> trees;
1206 for (auto const &term : likelihood.terms) {
1207 RooAbsPdf *listed = workspace.pdf(term.distribution.name);
1208 trees.push_back(listed ? std::unique_ptr<RooArgSet>{listed->getComponents()} : nullptr);
1209 }
1210 auto isConstraintTermOfOthers = [&](RooAbsArg const &term, std::size_t self) {
1211 auto isPartOf = [&term](RooArgSet const &tree) {
1212 if (tree.find(term.GetName())) {
1213 return true;
1214 }
1215 if (term.getAttribute(arrayDistTag)) {
1216 if (auto const *array = dynamic_cast<RooProdPdf const *>(&term)) {
1217 for (RooAbsArg const *component : array->pdfList()) {
1218 if (tree.find(component->GetName())) {
1219 return true;
1220 }
1221 }
1222 }
1223 }
1224 return false;
1225 };
1226 for (std::size_t i = 0; i < trees.size(); ++i) {
1227 if (i != self && trees[i] && isPartOf(*trees[i])) {
1228 return true;
1229 }
1230 }
1231 return false;
1232 };
1233
1234 // RooFit writes the constraint terms of a model as terms of the likelihood, with config().exportConstraintData
1235 // paired with a data set of one entry. If the document names the pdf of the ModelConfig, and it is a term of the
1236 // likelihood, another term that is not part of it and is paired with such a data set is a constraint term: an
1237 // external constraint of the ModelConfig.
1238 std::string const modelPdfName = mcInfo && mcInfo->pdfName ? *mcInfo->pdfName : "";
1239 std::unique_ptr<RooArgSet> modelTree;
1240 if (RooAbsPdf const *modelPdf = workspace.pdf(modelPdfName);
1241 modelPdf && std::any_of(likelihood.terms.begin(), likelihood.terms.end(),
1242 [&](auto const &term) { return term.distribution.name == modelPdfName; })) {
1243 modelTree.reset(modelPdf->getComponents());
1244 }
1245 // So is a component of a combined distribution that is read as a constraint term (`auxiliaryMeasurements`, see
1246 // readAuxiliaryChannelsAsConstraints()), whatever the document says about the ModelConfig.
1247 auto isConstraintTermOfModel = [&](RooAbsPdf const &listed, HS3::LikelihoodTerm const &term) {
1248 auto const *dataRef = std::get_if<HS3::DataRef>(&term.data);
1249 const bool auxiliary = auxiliaryMeasurements.count(listed.GetName()) > 0;
1250 if (!dataRef || (!auxiliary && (!modelTree || modelTree->find(listed.GetName())))) {
1251 return false;
1252 }
1253 auto const *data = findDataset(datasets, dataRef->name);
1254 return data && (*data)->numEntries() == 1;
1255 };
1256
1257 std::vector<HS3::LikelihoodTerm const *> nllTerms;
1258 RooArgSet constraintObservables;
1259 RooArgSet constraintsWithData;
1260 for (std::size_t i = 0; i < likelihood.terms.size(); ++i) {
1261 auto const &term = likelihood.terms[i];
1262 RooAbsPdf *listed = workspace.pdf(term.distribution.name);
1263 if (listed && !isConstraintTermOfOthers(*listed, i) && isConstraintTermOfModel(*listed, term)) {
1264 constraintsWithData.add(*listed);
1265 } else if (!listed || !isConstraintTermOfOthers(*listed, i)) {
1266 nllTerms.push_back(&term);
1267 continue;
1268 }
1269 auto const *dataRef = std::get_if<HS3::DataRef>(&term.data);
1270 auto *data = dataRef ? findDataset(datasets, dataRef->name) : nullptr;
1271 if (!data) {
1272 continue;
1273 }
1274 if ((*data)->numEntries() != 1) {
1275 throw std::runtime_error("the data set '" + dataRef->name + "' paired with the constraint term '" +
1276 term.distribution.name + "' must have exactly one entry");
1277 }
1278 for (RooAbsArg const *obs : *(*data)->get(0)) {
1279 auto *var = workspace.var(obs->GetName());
1280 auto const *value = dynamic_cast<RooAbsReal const *>(obs);
1281 if (!var || !value) {
1282 RooJSONFactoryWSTool::warning("RooFitHS3: cannot set the observed value of '" +
1283 std::string(obs->GetName()) + "' from the data set '" + dataRef->name +
1284 "': it is not a variable of the workspace");
1285 continue;
1286 }
1287 var->setVal(value->getVal());
1288 var->setConstant(true);
1289 constraintObservables.add(*var);
1290 }
1291 // The values live in the variables now; the data set is not imported.
1292 data->reset();
1293 }
1294 // The constraint terms of HistFactory distributions are listed as auxiliary distributions, but they are part of the
1295 // distributions in RooFit.
1296 std::set<std::string> channelConstraints;
1297 for (auto const &item : doc.rooFit.channelConstraints) {
1298 for (auto const &ref : item.second) {
1299 channelConstraints.insert(ref.name);
1300 }
1301 }
1302 RooArgSet extConstraints;
1303 for (auto const &aux : likelihood.auxDistributions) {
1304 RooAbsArg *extConstraint = workspace.arg(aux.name);
1305 if (!extConstraint) {
1306 continue;
1307 }
1308 checkPdf(extConstraint, aux.name, "auxiliary distribution");
1309 // Only if the constraint really ended up in a distribution of the likelihood.
1310 if (channelConstraints.count(aux.name) &&
1311 std::any_of(nllTerms.begin(), nllTerms.end(), [&](HS3::LikelihoodTerm const *term) {
1312 RooAbsArg const *dist = workspace.arg(term->distribution.name);
1313 return dist && dist->dependsOn(*extConstraint);
1314 })) {
1315 continue;
1316 }
1317 if (isConstraintTermOfOthers(*extConstraint, likelihood.terms.size())) {
1318 continue;
1319 }
1320 extConstraints.add(*extConstraint);
1321 }
1322 RooArgSet observables;
1323 for (HS3::LikelihoodTerm const *term : nllTerms) {
1324 if (auto const *dataRef = std::get_if<HS3::DataRef>(&term->data)) {
1325 auto const *data = findDataset(datasets, dataRef->name);
1326 if (!data) {
1327 throw std::runtime_error("dataset '" + dataRef->name + "' cannot be found!");
1328 }
1329 observables.add(*(*data)->get(), true);
1330 }
1331 }
1332 // Bookkeeping columns of the data sets are no observables of the model.
1333 for (std::string const &column : bookkeepingColumns) {
1334 if (RooAbsArg *arg = observables.find(column.c_str())) {
1335 observables.remove(*arg);
1336 }
1337 }
1338 if (observables.empty()) {
1339 observablesWithoutData(doc, analysis, likelihood, mcInfo, workspace, observables);
1340 }
1341
1342 // A combined distribution paired with a combined data set must have a sub-data set for every component.
1343 for (auto const &term : likelihood.terms) {
1344 auto const *dataRef = std::get_if<HS3::DataRef>(&term.data);
1345 auto *simPdf = dynamic_cast<RooSimultaneous const *>(workspace.pdf(term.distribution.name));
1346 if (!dataRef || !simPdf) {
1347 continue;
1348 }
1349 if (auto const *combined = HS3::find<HS3::CombinedData>(doc.data, dataRef->name)) {
1350 checkCombinedPairing(*simPdf, dataRef->name, *combined);
1351 }
1352 }
1353
1354 std::string const pdfName = mcInfo && mcInfo->pdfName ? *mcInfo->pdfName : "simPdf";
1355
1356 RooAbsPdf *pdf = workspace.pdf(pdfName);
1357
1358 if (!pdf) {
1359 // if there is no simultaneous pdf, we can check whether there is only one pdf in the list
1360 if (nllTerms.size() == 1) {
1361 // if so, we can use that one to populate the ModelConfig
1362 pdf = workspace.pdf(nllTerms[0]->distribution.name);
1363 } else {
1364 // otherwise, we have no choice but to build a simPdf by hand
1365 std::string simPdfName = analysisName + "_simPdf";
1366 std::string indexCatName = analysisName + "_categoryIndex";
1367 RooCategory indexCat{indexCatName.c_str(), indexCatName.c_str()};
1368 std::map<std::string, RooAbsPdf *> pdfMap;
1369 for (std::size_t i = 0; i < nllTerms.size(); ++i) {
1370 std::string const &name = nllTerms[i]->distribution.name;
1371 indexCat.defineType(name, i);
1372 pdfMap[name] = workspace.pdf(name);
1373 }
1374 RooSimultaneous simPdf{simPdfName.c_str(), simPdfName.c_str(), pdfMap, indexCat};
1375 workspace.import(simPdf, RooFit::RecycleConflictNodes(true), RooFit::Silence(true));
1376 pdf = workspace.pdf(simPdfName);
1377 }
1378 }
1379
1380 mc->SetPdf(*pdf);
1381
1382 extConstraints.add(constraintsWithData, /*silent=*/true);
1383 attachConstraintsToChannels(*pdf, extConstraints);
1384 if (!extConstraints.empty())
1385 mc->SetExternalConstraints(extConstraints);
1386
1387 if (analysis.prior) {
1388 if (RooAbsArg *prior = workspace.arg(analysis.prior->name)) {
1389 checkPdf(prior, analysis.prior->name, "prior");
1390 mc->SetPriorPdf(static_cast<RooAbsPdf &>(*prior));
1391 }
1392 }
1393
1394 RooArgSet parametersOfInterest;
1395 for (auto const &poi : analysis.parametersOfInterest) {
1396 RooAbsArg *arg = workspace.arg(poi.name);
1397 if (!arg) {
1398 RooJSONFactoryWSTool::error("the object '" + poi.name + "' in \"parameters_of_interest\" of analysis '" +
1399 analysisName + "' does not exist");
1400 }
1401 if (!dynamic_cast<RooRealVar *>(arg)) {
1402 RooJSONFactoryWSTool::error("the parameter of interest '" + poi.name + "' of analysis '" + analysisName +
1403 "' is a " + arg->ClassName() + ", not a variable" +
1404 (dynamic_cast<RooConstVar *>(arg) ? ", because no domain lists it" : ""));
1405 }
1406 parametersOfInterest.add(*arg);
1407 }
1408
1409 mc->SetParametersOfInterest(parametersOfInterest);
1410 mc->SetObservables(observables);
1411 RooArgSet pars;
1412 pdf->getParameters(&observables, pars);
1413
1414 // Figure out the set parameters that appear in the main measurement:
1415 // getAllConstraints() has the side effect to remove all parameters from
1416 // "mainPars" that are not part of any pdf over observables.
1417 RooArgSet mainPars{pars};
1418 pdf->getAllConstraints(observables, mainPars, /*stripDisconnected*/ true);
1419
1420 // Parameters that only the external constraints depend on are parameters of the likelihood as well, e.g. the
1421 // global observable and the nuisance parameter of a constraint that is not part of the pdf. They are not part of
1422 // the main measurement.
1423 for (RooAbsArg const *extConstraint : extConstraints) {
1424 RooArgSet extPars;
1425 extConstraint->getParameters(&observables, extPars);
1426 pars.add(extPars, /*silent=*/true);
1427 }
1428
1429 RooArgSet domainPars;
1430 for (auto const &domainRef : analysis.domains) {
1431 if (HS3::Domain const *domain = HS3::findByName(doc.domains, domainRef.name)) {
1432 for (auto const &axis : domain->axes) {
1433 if (RooRealVar *wsvar = workspace.var(HS3::nameOf(axis))) {
1434 domainPars.add(*wsvar);
1435 }
1436 }
1437 }
1438 }
1439
1440 RooArgSet nps;
1441 RooArgSet globs;
1442 for (const auto &p : pars) {
1443 if (mc->GetParametersOfInterest()->find(*p))
1444 continue;
1445 if (p->isConstant() && !mainPars.find(*p) && domainPars.find(*p)) {
1446 globs.add(*p);
1447 } else if (domainPars.find(*p)) {
1448 nps.add(*p);
1449 }
1450 }
1451
1452 globs.add(constraintObservables, /*silent=*/true);
1453 mc->SetGlobalObservables(globs);
1454 mc->SetNuisanceParameters(nps);
1455
1456 if (mcInfo && mcInfo->combinedDataName) {
1457 pdf->setStringAttribute("combined_data_name", mcInfo->combinedDataName->c_str());
1458 }
1459
1460 // A missing parameter point was reported by HS3::validate().
1461 RooArgSet const *init = analysis.init ? workspace.getSnapshot(analysis.init->name.c_str()) : nullptr;
1462 if (init) {
1463 const std::string &initName = analysis.init->name;
1464 // ModelConfig::SetSnapshot() saves the snapshot as "<mc name>_<set name>_snapshot". If "init" already follows
1465 // this pattern, which is the case for files written by RooFit, pick the set name such that it is preserved.
1466 std::string setName = initName;
1467 const std::string prefix = mcname + "_";
1468 const std::string suffix = "_snapshot";
1469 if (initName.size() >= prefix.size() + suffix.size() && startsWith(initName, prefix) &&
1470 endsWith(initName, suffix)) {
1471 setName = initName.substr(prefix.size(), initName.size() - prefix.size() - suffix.size());
1472 }
1473 // Take an owning copy: SetSnapshot() might replace the workspace snapshot that `init` points to.
1474 RooArgSet initSet;
1475 init->snapshot(initSet);
1476 initSet.setName(setName.c_str());
1477 mc->SetSnapshot(initSet);
1478 }
1479}
1480
1481// HistFactory modifiers pick the components that constrain their parameters out of an array distribution. By the
1482// standard the whole array enters the likelihood, so the components of a partially used array that nothing uses are
1483// reported.
1484void warnAboutUnusedArrayComponents(RooWorkspace const &ws)
1485{
1486 for (RooAbsArg *arg : ws.allPdfs()) {
1487 auto const *array = dynamic_cast<RooProdPdf const *>(arg);
1488 if (!array || !array->getAttribute(arrayDistTag) || array->hasClients()) {
1489 continue;
1490 }
1491 bool used = false;
1492 std::string unused;
1493 for (RooAbsArg const *component : array->pdfList()) {
1494 bool usedElsewhere = false;
1495 for (RooAbsArg const *client : component->clients()) {
1496 if (client != array) {
1497 usedElsewhere = true;
1498 }
1499 }
1500 if (usedElsewhere) {
1501 used = true;
1502 } else {
1503 unused += (unused.empty() ? "'" : ", '") + std::string(component->GetName()) + "'";
1504 }
1505 }
1506 if (used && !unused.empty()) {
1507 RooJSONFactoryWSTool::warning("RooFitHS3: the components " + unused + " of the array distribution '" +
1508 array->GetName() +
1509 "' are not used by any distribution and are not part of any likelihood");
1510 }
1511 }
1512}
1513
1514void combinePdfs(HS3Document const &doc, RooWorkspace &ws)
1515{
1516 for (auto const &item : doc.rooFit.combinedDistributions) {
1517 std::string const &combinedName = item.first;
1518 HS3::CombinedDistribution const &info = item.second;
1519
1520 RooCategory indexCat{info.indexCat.c_str(), info.indexCat.c_str()};
1521 std::map<std::string, RooAbsPdf *> pdfMap;
1522
1523 for (std::size_t iChannel = 0; iChannel < info.labels.size(); ++iChannel) {
1524 indexCat.defineType(info.labels[iChannel], info.indices[iChannel]);
1525 std::string const &name = info.distributions[iChannel].name;
1526 requirePdf(ws.arg(name),
1527 "the distribution '" + name + "' of the combined distribution '" + combinedName + "'");
1528 pdfMap[info.labels[iChannel]] = ws.pdf(name);
1529 }
1530
1531 RooSimultaneous simPdf{combinedName.c_str(), combinedName.c_str(), pdfMap, indexCat};
1532 ws.import(simPdf, RooFit::RecycleConflictNodes(true), RooFit::Silence(true));
1533 }
1534}
1535
1536void combineDatasets(HS3Document const &doc, std::vector<std::unique_ptr<RooAbsData>> &datasets)
1537{
1538 for (auto const &item : doc.rooFit.combinedDatasets) {
1539 std::string const &combinedName = item.first;
1540 HS3::CombinedDataset const &info = item.second;
1541
1542 // Create the combined dataset for RooFit
1543 std::map<std::string, std::unique_ptr<RooAbsData>> dsMap;
1544 RooCategory indexCat{info.indexCat.c_str(), info.indexCat.c_str()};
1545 RooArgSet allVars{indexCat};
1546 for (std::size_t iChannel = 0; iChannel < info.labels.size(); ++iChannel) {
1547 auto componentName = combinedName + "_" + info.labels[iChannel];
1548 // We move the found channel data out of the "datasets" vector, such that
1549 // the data components don't get imported anymore.
1550 auto *found = findDataset(datasets, componentName);
1551 if (!found) {
1552 RooJSONFactoryWSTool::error("unable to obtain component matching component name '" + componentName + "'");
1553 }
1554 allVars.add(*(*found)->get(), true);
1555 dsMap.insert({info.labels[iChannel], std::move(*found)});
1556 indexCat.defineType(info.labels[iChannel], info.indices[iChannel]);
1557 }
1558
1559 auto combined = std::make_unique<RooDataSet>(combinedName, combinedName, allVars, RooFit::Import(dsMap),
1560 RooFit::Index(indexCat));
1561 datasets.emplace_back(std::move(combined));
1562 }
1563}
1564
1565// The categories of the workspace that are the columns of a combined data set or distribution: they are declared by
1566// categorical axes of the domains, or existed before. The validation has checked the columns and the keys against the
1567// domains, but not against the categories that existed before.
1568template <class Ref_t>
1569std::vector<RooCategory *>
1570columnCategories(RooWorkspace const &workspace, HS3::Combined<Ref_t> const &combined, std::string const &context)
1571{
1572 std::vector<RooCategory *> columns;
1573 for (std::string const &column : combined.columns) {
1574 RooCategory *cat = workspace.cat(column);
1575 if (!cat) {
1576 RooJSONFactoryWSTool::error("the column '" + column + "' of " + context +
1577 " is not declared by a categorical axis of a domain");
1578 }
1579 columns.push_back(cat);
1580 }
1581 for (auto const &component : combined.components) {
1582 for (std::size_t i = 0; i < columns.size(); ++i) {
1583 if (!columns[i]->hasLabel(component.key[i])) {
1584 RooJSONFactoryWSTool::error("the label '" + component.key[i] + "' in position " + std::to_string(i) +
1585 " of a key of " + context + " is not one of the categories of '" +
1586 columns[i]->GetName() + "'");
1587 }
1588 }
1589 }
1590 return columns;
1591}
1592
1593// Build the combined data sets of the document: a RooDataSet with one category column per entry of "columns" and the
1594// variables of all components, filled row by row from the components, which are then no longer imported on their own.
1595// A key without a sub-data set is a state without entries: RooFit has no notion of an absent component. The validation
1596// has checked that the components are data sets of the document.
1597void assembleCombinedData(HS3Document const &doc, RooWorkspace const &workspace,
1598 std::vector<std::unique_ptr<RooAbsData>> &datasets)
1599{
1600 for (HS3::Data const &input : doc.data) {
1601 auto const *info = std::get_if<HS3::CombinedData>(&input.body);
1602 if (!info) {
1603 continue;
1604 }
1605 std::string const &name = input.name;
1607 std::string const context = "the combined data set '" + name + "'";
1608 std::vector<RooCategory *> columns = columnCategories(workspace, *info, context);
1609
1610 struct Component {
1611 std::vector<std::string> const *key;
1612 std::unique_ptr<RooAbsData> data;
1613 };
1614 std::vector<Component> components;
1615 for (auto const &c : info->components) {
1616 auto *found = findDataset(datasets, c.ref.name);
1617 if (!found) {
1618 RooJSONFactoryWSTool::error("the component '" + c.ref.name + "' of " + context +
1619 " is not a data set of the document, or it is already a component of " +
1620 "another combined data set");
1621 }
1622 components.push_back({&c.key, std::move(*found)});
1623 }
1624
1625 // The variables: the columns first, then those of the components. A weight variable if any component is
1626 // weighted, with errors if any component is binned, like RooDataSet does when importing a map of data sets.
1627 RooArgSet columnClones; // owns the clones; `vars` must not own anything, it also lists the components' variables
1628 for (RooCategory const *column : columns) {
1629 columnClones.addClone(*column);
1630 }
1631 RooArgSet vars{columnClones};
1632 bool weighted = false;
1633 bool storeErrors = false;
1634 std::string weightName = RooFit::WeightVar().getString(0);
1635 for (auto const &c : components) {
1636 vars.add(*c.data->get(), /*silent=*/true);
1637 weighted |= c.data->isWeighted();
1638 if (auto *ds = dynamic_cast<RooDataSet const *>(c.data.get()); ds && ds->isWeighted()) {
1639 weightName = ds->weightVar()->GetName();
1640 }
1641 storeErrors |= dynamic_cast<RooDataHist const *>(c.data.get()) != nullptr;
1642 }
1643 std::unique_ptr<RooDataSet> combined;
1644 if (weighted) {
1645 RooRealVar weightVar{weightName.c_str(), weightName.c_str(), 1.0};
1646 vars.add(weightVar);
1647 RooArgSet errorSet{weightVar};
1648 combined = std::make_unique<RooDataSet>(name.c_str(), name.c_str(), vars, RooFit::WeightVar(weightVar),
1649 storeErrors ? RooFit::StoreError(errorSet) : RooCmdArg::none());
1650 } else {
1651 combined = std::make_unique<RooDataSet>(name.c_str(), name.c_str(), vars);
1652 }
1653 for (auto const &c : components) {
1654 for (std::size_t i = 0; i < columns.size(); ++i) {
1655 static_cast<RooCategory &>(vars[columns[i]->GetName()]).setLabel((*c.key)[i].c_str());
1656 }
1657 for (int i = 0; i < c.data->numEntries(); ++i) {
1658 vars.assign(*c.data->get(i));
1659 if (weighted) {
1660 combined->add(vars, c.data->weight(), storeErrors ? c.data->weightError(RooAbsData::SumW2) : 0.0);
1661 } else {
1662 combined->add(vars);
1663 }
1664 }
1665 }
1666 datasets.emplace_back(std::move(combined));
1667 }
1668}
1669
1670template <class T>
1671void sortByName(T &coll)
1672{
1673 std::sort(coll.begin(), coll.end(), [](auto &l, auto &r) { return strcmp(l->GetName(), r->GetName()) < 0; });
1674}
1675
1676// Whether no object of the workspace uses `arg`. Only clients that are part of the workspace count: RooFit also
1677// registers evaluation artifacts as clients, like the normalization integral that RooAbsPdf caches after
1678// getVal(normSet), or the integral returned by createIntegral(). These live outside the workspace and are not evidence
1679// that the object is a sub-node of a bigger model (see https://github.com/root-project/root/issues/23221). Helpers that
1680// are not written don't count as users either.
1681bool isTopLevel(RooAbsArg const &arg, RooWorkspace const &ws)
1682{
1683 for (RooAbsArg const *client : arg.clients()) {
1684 if (ws.components().containsInstance(*client) && !isSkippedHelper(*client, ws)) {
1685 return false;
1686 }
1687 }
1688 return true;
1689}
1690
1691// The top-level objects among `args` that are written, sorted by name.
1692template <class Arg_t>
1693std::vector<Arg_t *> topLevelObjects(RooArgSet const &args, RooWorkspace const &ws)
1694{
1695 std::vector<Arg_t *> out;
1696 for (RooAbsArg *arg : args) {
1697 auto *object = dynamic_cast<Arg_t *>(arg);
1698 if (object && isTopLevel(*arg, ws) && !isSkippedHelper(*arg, ws)) {
1699 out.push_back(object);
1700 }
1701 }
1702 sortByName(out);
1703 return out;
1704}
1705
1706} // namespace
1707
1708RooArgSet RooFit::HS3::createObservables(std::vector<BinnedAxis> const &axes)
1709{
1710 RooArgSet vars;
1711 for (BinnedAxis const &axis : axes) {
1712 vars.addOwned(createObservable(axis));
1713 }
1714 return vars;
1715}
1716
1717std::unique_ptr<RooDataHist> RooFit::HS3::createDataHist(std::string const &name, RooArgSet const &vars,
1718 std::vector<double> const &contents,
1719 std::vector<double> const *errors)
1720{
1721 if (errors && errors->size() != contents.size()) {
1722 RooJSONFactoryWSTool::error("inconsistent number of errors and contents in '" + name + "': errors=" +
1723 std::to_string(errors->size()) + ", contents=" + std::to_string(contents.size()));
1724 }
1725 // A continuous variable has the bins of its binning, and a category one bin per state.
1726 std::size_t nBins = 1;
1727 for (RooAbsArg const *var : vars) {
1728 nBins *= std::max(0, dynamic_cast<RooAbsLValue const &>(*var).numBins());
1729 }
1730 if (contents.size() != nBins) {
1731 RooJSONFactoryWSTool::error("inconsistent bin numbers: contents=" + std::to_string(contents.size()) +
1732 ", bins=" + std::to_string(nBins));
1733 }
1734 auto dh = std::make_unique<RooDataHist>(name, name, vars);
1735 // RooDataHist only starts to store squared weights when it sees a positive error, and then initializes them from
1736 // the weights so far. Trigger that before setting any weight, so that errors of zero stay zero.
1737 if (errors && !errors->empty()) {
1738 dh->set(0, 0., 1.);
1739 }
1740 for (std::size_t ibin = 0; ibin < nBins; ++ibin) {
1741 dh->set(ibin, contents[ibin], errors ? (*errors)[ibin] : -1);
1742 }
1743 return dh;
1744}
1745
1746// A combined distribution is a RooSimultaneous. Its index category is the single column, or a RooSuperCategory over the
1747// columns in the order of the document.
1748void RooFit::HS3::buildCombinedDist(RooJSONFactoryWSTool &tool, std::string const &name, CombinedDist const &dist,
1749 std::vector<std::string> const &otherLikelihoodTerms)
1750{
1751 std::vector<RooCategory *> columns = columnCategories(*tool.workspace(), dist, "the combined_dist '" + name + "'");
1752 std::map<std::string, RooAbsPdf *> pdfMap;
1753 for (auto const &component : dist.components) {
1754 pdfMap[selectionLabel(component.key)] = tool.request<RooAbsPdf>(component.ref.name, name);
1755 }
1756
1757 // In RooFit, the constraint terms of a channel are factors of it, which RooFit takes out of the product when it
1758 // builds a likelihood. A channel that is a product gets them when the likelihood is read; the other channels are
1759 // wrapped in a product "<channel>_with_constraints" here, since the components of a RooSimultaneous can't be
1760 // replaced later. The candidates are the other terms of the likelihoods that have this distribution.
1761 std::vector<RooAbsPdf *> terms;
1762 for (std::string const &term : otherLikelihoodTerms) {
1763 if (auto *pdf = tool.tryRequest<RooAbsPdf>(term)) {
1764 terms.push_back(pdf);
1765 }
1766 }
1767 RooArgSet channels;
1768 for (auto const &item : pdfMap) {
1769 channels.add(*item.second, /*silent=*/true);
1770 }
1771 for (auto &item : pdfMap) {
1772 RooAbsPdf &channel = *item.second;
1773 RooArgList factors;
1774 for (RooAbsPdf *term : terms) {
1775 if (!dynamic_cast<RooProdPdf *>(&channel) && isConstraintOfChannel(*term, channel, channels)) {
1776 factors.add(*term);
1777 }
1778 }
1779 if (!factors.empty()) {
1780 factors.add(channel);
1781 item.second = &tool.wsEmplace<RooProdPdf>(std::string(channel.GetName()) + "_with_constraints", factors);
1782 }
1783 }
1784
1785 if (columns.size() == 1) {
1786 tool.wsImport(RooSimultaneous{name.c_str(), name.c_str(), pdfMap, *columns.front()});
1787 return;
1788 }
1789 RooArgSet inputs;
1790 for (RooCategory *column : columns) {
1791 inputs.add(*column);
1792 }
1793 std::string const indexName = name + "_index";
1794 RooSuperCategory indexCat{indexName.c_str(), indexName.c_str(), inputs};
1795 tool.wsImport(RooSimultaneous{name.c_str(), name.c_str(), pdfMap, indexCat});
1796}
1797
1798bool isSkippedHelper(RooAbsArg const &arg, RooWorkspace const &ws)
1799{
1800 if (!arg.getAttribute(helperObjectTag)) {
1801 return false;
1802 }
1803 // The HS3 object that the helper stands for is written: an array distribution.
1805 return false;
1806 }
1807 for (RooAbsArg const *client : arg.clients()) {
1808 if (ws.components().containsInstance(*client) && !isSkippedHelper(*client, ws)) {
1809 return false;
1810 }
1811 }
1812 for (TObject const *obj : ws.allGenericObjects()) {
1813 auto const *mc = dynamic_cast<RooFit::ModelConfig const *>(obj);
1814 if (!mc) {
1815 continue;
1816 }
1817 if (mc->GetPdf() == &arg || mc->GetPriorPdf() == &arg ||
1818 (mc->GetExternalConstraints() && mc->GetExternalConstraints()->containsInstance(arg))) {
1819 return false;
1820 }
1821 }
1822 return true;
1823}
1824
1826
1828{
1829 if (!_import) {
1830 return false;
1831 }
1832 auto const &componentsWithData = _import->componentsWithData;
1833 // Also as a component of an array distribution, which is the product of its components in RooFit.
1834 return componentsWithData.count(pdf.GetName()) ||
1835 std::any_of(pdf.clients().begin(), pdf.clients().end(), [&](RooAbsArg const *client) {
1836 return client->getAttribute(arrayDistTag) && componentsWithData.count(client->GetName());
1837 });
1838}
1839
1840std::vector<HS3::DistributionRef> const *RooJSONFactoryWSTool::channelConstraints(std::string const &dist) const
1841{
1842 if (!_import) {
1843 return nullptr;
1844 }
1845 auto const &all = _import->document.rooFit.channelConstraints;
1846 auto found = all.find(dist);
1847 return found != all.end() ? &found->second : nullptr;
1848}
1849
1851
1853{
1854 return _import ? _import->document.metadata.version : HS3::Version{};
1855}
1856
1857void RooJSONFactoryWSTool::fillSeq(JSONNode &node, RooAbsCollection const &coll, size_t nMax)
1858{
1859 const size_t old_children = node.num_children();
1860 node.set_seq();
1861 size_t n = 0;
1862 for (RooAbsArg const *arg : coll) {
1863 if (n >= nMax)
1864 break;
1865 writeReference(node.append_child(), *arg);
1866 ++n;
1867 }
1868 if (node.num_children() != old_children + coll.size()) {
1869 error("unable to stream collection " + std::string(coll.GetName()) + " to " + node.key());
1870 }
1871}
1872
1874{
1876 child["name"] << name;
1877 return child;
1878}
1879
1880JSONNode const *RooJSONFactoryWSTool::findNamedChild(JSONNode const &node, std::string const &name)
1881{
1882 if (!node.is_seq())
1883 return nullptr;
1884 for (JSONNode const &child : node.children()) {
1885 if (child["name"].val() == name)
1886 return &child;
1887 }
1888
1889 return nullptr;
1890}
1891
1892/// Whether a string is a valid name: a letter or an underscore, followed by letters, digits or underscores. Only
1893/// characters from the ASCII character set are allowed.
1894bool RooJSONFactoryWSTool::isValidName(const std::string &str)
1895{
1896 return !str.empty() && (std::isalpha(str[0]) || str[0] == '_') &&
1897 std::all_of(str.begin(), str.end(), [](char c) { return std::isalnum(c) || c == '_'; });
1898}
1899
1900bool RooJSONFactoryWSTool::testValidName(const std::string &name, bool forceError)
1901{
1903 std::stringstream ss;
1904 ss << "RooJSONFactoryWSTool() name '" << name << "' is not valid!" << std::endl
1905 << "Sanitize names by setting RooJSONFactoryWSTool::config().allowSanitizeNames = true." << std::endl;
1906 if (RooJSONFactoryWSTool::config().allowExportInvalidNames && !forceError) {
1908 return false;
1909 } else {
1911 }
1912 }
1913 return true;
1914}
1915
1917{
1918 return n["name"].val();
1919}
1920
1921// The specializations of requestImpl() look in the workspace first. If the object is not there, they import what the
1922// document has under that name, and look again.
1923template <>
1924RooRealVar *RooJSONFactoryWSTool::requestImpl<RooRealVar>(const std::string &objname)
1925{
1926 if (_import && !_workspace.var(objname)) {
1927 auto found = _import->defaultValuesByName.find(objname);
1928 if (found != _import->defaultValuesByName.end()) {
1929 importVariable(*found->second);
1930 }
1931 }
1932 return _workspace.var(objname);
1933}
1934
1935template <>
1936RooAbsPdf *RooJSONFactoryWSTool::requestImpl<RooAbsPdf>(const std::string &objname)
1937{
1938 if (_import && !_workspace.pdf(objname)) {
1939 auto found = _import->distributions.find(objname);
1940 if (found != _import->distributions.end()) {
1941 importDistribution(*found->second);
1942 }
1943 }
1944 // HS3 lists some resolution models, which are pdfs in RooFit, as functions.
1945 if (_import && !_workspace.pdf(objname)) {
1946 auto found = _import->functions.find(objname);
1947 if (found != _import->functions.end()) {
1948 importFunction(*found->second->body.node, true);
1949 }
1950 }
1951 return _workspace.pdf(objname);
1952}
1953
1954template <>
1955RooAbsReal *RooJSONFactoryWSTool::requestImpl<RooAbsReal>(const std::string &objname)
1956{
1957 if (RooAbsReal *retval = _workspace.function(objname))
1958 return retval;
1959 if (std::optional<double> number = toNumber(objname))
1960 return &RooFit::RooConst(*number);
1961 if (RooAbsPdf *pdf = requestImpl<RooAbsPdf>(objname))
1962 return pdf;
1963 if (RooRealVar *var = requestImpl<RooRealVar>(objname))
1964 return var;
1965 if (_import && !_workspace.function(objname)) {
1966 auto found = _import->functions.find(objname);
1967 if (found != _import->functions.end()) {
1968 importFunction(*found->second->body.node, true);
1969 }
1970 }
1971 return _workspace.function(objname);
1972}
1973
1974// Export a single variable (RooRealVar or RooConstVar) `v` to the parameter point.
1976{
1977 auto *cv = dynamic_cast<const RooConstVar *>(v);
1978 auto *rrv = dynamic_cast<const RooRealVar *>(v);
1979 if (!cv && !rrv)
1980 return;
1981
1982 // for RooConstVar, if name and value are the same, we don't need to do anything
1983 if (cv && strcmp(cv->GetName(), TString::Format("%g", cv->getVal()).Data()) == 0) {
1984 return;
1985 }
1986
1987 // A named RooConstVar only takes its value: it is written as a domain axis with that single value, not in the
1988 // parameter points.
1989 if (cv) {
1990 HS3::DomainAxis axis;
1991 axis.name = cv->GetName();
1992 axis.fixedValue = cv->getVal();
1993 _export->domains[HS3::defaultDomainName][axis.name] = std::move(axis);
1994 return;
1995 }
1996
1998 var.name = v->GetName();
1999 var.value = rrv->getVal();
2000 var.isConst = rrv->isConstant() || rrv->getMin() >= rrv->getMax();
2001 recordDomains(_export->domains, *rrv);
2002 point.parameters.push_back(std::move(var));
2003}
2004
2005std::string RooJSONFactoryWSTool::exportTransformed(const RooAbsReal *original, const std::string &suffix,
2006 const std::string &formula)
2007{
2008 // A literal constant is spelled as an identifier, which a parameter must not be mistaken for when reading.
2009 if (!isLiteralConstVar(*original) && isLiteralIdentifier(original->GetName())) {
2010 error("cannot write a function of '" + std::string(original->GetName()) +
2011 "': names of the form 'const_<number>' are reserved for functions of literal constants");
2012 }
2013 std::string newname = transformedBaseName(*original) + suffix;
2014 // HS3 0.3 (HS3 PR #154) only allows expressions to refer to parameters, variables and functions.
2015 if (dynamic_cast<RooAbsPdf const *>(original)) {
2016 error("the parameter '" + std::string(original->GetName()) +
2017 "' would have to be written as an expression that refers to a pdf, which HS3 doesn't allow");
2018 }
2019 // Another object that shares the parameter might have exported the helper already.
2020 auto &functions = _export->doc.functions;
2021 if (HS3::findByName(functions, newname)) {
2022 return newname;
2023 }
2024 RooFit::Detail::JSONNode &trafo_node = appendNamedChild((*_export->root)["functions"], newname);
2025 trafo_node["type"] << "generic";
2026 trafo_node["expression"] << TString::Format(formula.c_str(), transformedArgument(*original).c_str()).Data();
2027 functions.push_back({newname, {"generic", nullptr}});
2028 this->setAttribute(newname, "roofit_skip"); // this function should not be imported back in
2029 return newname;
2030}
2031
2032namespace {
2033
2034// Turn an arbitrary string into a valid variable name, but refuse to change the
2035// first character (which would silently rename the object).
2036std::string makeValidNameOrError(std::string const &in)
2037{
2038 if (!std::isalpha(in[0])) {
2039 RooJSONFactoryWSTool::error("refusing to change first character of string '" + in + "' to make a valid name!");
2040 }
2041 std::string out = in;
2042 std::transform(out.begin(), out.end(), out.begin(), [](char c) { return std::isalnum(c) ? c : '_'; });
2043 if (out != in) {
2044 oocoutW(nullptr, ObjectHandling) << "RooFitHS3: changed '" << in << "' to '" << out << "' to become a valid name";
2045 }
2046 return out;
2047}
2048
2049// The labels of a selection key, made valid names and joined by "_". Used in the names of sub-data sets and of
2050// renamed observables.
2051std::string selectionKeyName(std::vector<std::string> const &key)
2052{
2053 std::string out;
2054 for (std::string const &label : key) {
2055 out += (out.empty() ? "" : "_") + makeValidNameOrError(label);
2056 }
2057 return out;
2058}
2059
2060// The RooSimultaneous of the workspace (the first by name) whose columns are `columns`, or nullptr.
2061RooSimultaneous const *findMatchingSimultaneous(RooWorkspace const &workspace, std::vector<std::string> const &columns)
2062{
2063 std::vector<RooSimultaneous const *> candidates;
2064 for (RooAbsArg *arg : workspace.allPdfs()) {
2065 if (auto *candidate = dynamic_cast<RooSimultaneous const *>(arg)) {
2066 candidates.push_back(candidate);
2067 }
2068 }
2069 sortByName(candidates);
2070 for (RooSimultaneous const *candidate : candidates) {
2071 if (namesOf(selectionColumns(candidate->indexCat())) == columns) {
2072 return candidate;
2073 }
2074 }
2075 return nullptr;
2076}
2077
2078} // namespace
2079
2080namespace {
2081
2082// Record an object that was written by an exporter in the document. Its node moves when the array that it is in
2083// grows, so the distributions only get their nodes at the end of the export.
2084void recordGenericObject(HS3Document &doc, bool isPdf, std::string const &name, JSONNode const &elem)
2085{
2086 std::string type = elem.has_child("type") ? elem["type"].val() : "";
2087 if (isPdf) {
2088 doc.distributions.push_back({name, HS3::GenericObject{std::move(type), nullptr}});
2089 } else {
2090 doc.functions.push_back({name, {std::move(type), nullptr}});
2091 }
2092}
2093
2094} // namespace
2095
2096// Export a single object `func` (pdf, function, variable or category) to the output JSON, recording its name in
2097// the exported object names to avoid exporting it twice.
2099{
2100 std::string name = func.GetName();
2101 std::set<std::string> &exportedObjectNames = _export->exportedObjectNames;
2102
2103 if (exportedObjectNames.find(name) != exportedObjectNames.end()) {
2104 // A component of an array distribution is not written under its own name, so an object that refers to it by
2105 // that name would have a dangling reference.
2106 auto const &arrayOfComponent = _export->arrayOfComponent;
2107 if (auto found = arrayOfComponent.find(name); found != arrayOfComponent.end() && found->second != name) {
2108 error("'" + name + "' is written as a component of the array distribution '" + found->second +
2109 "', but another object refers to it by its own name. Set "
2110 "RooJSONFactoryWSTool::config().exportArrayDist = false to write it on its own.");
2111 }
2112 return;
2113 }
2114
2115 exportedObjectNames.insert(name);
2116
2117 // Exporters that group objects per top-level pdf (the HistFactory constraints) ask for the root of the recursion.
2118 if (_export->depth == 0) {
2119 _export->rootName = name;
2120 }
2121 struct DepthGuard {
2122 int &depth;
2123 DepthGuard(int &d) : depth{d} { ++depth; }
2124 ~DepthGuard() { --depth; }
2125 } depthGuard{_export->depth};
2126
2127 if (auto simPdf = dynamic_cast<RooSimultaneous const *>(&func)) {
2128 exportSimultaneous(*simPdf);
2129 return;
2130 } else if (dynamic_cast<RooAbsCategory const *>(&func)) {
2131 // Categories are not objects of the document: the index category of a RooSimultaneous is declared by the
2132 // categorical axes of the domain that its "columns" name.
2133 return;
2134 } else if (dynamic_cast<RooRealVar const *>(&func) || dynamic_cast<RooConstVar const *>(&func)) {
2135 exportVariable(&func, _export->doc.parameterPoints.front());
2136 return;
2137 } else if (config().exportArrayDist && func.getAttribute(arrayDistTag)) {
2138 // An array distribution (HS3 `array_dist`): its components are registered here, and the array is written at
2139 // the end of the export, see exportAsArrayComponent().
2140 auto const *prod = dynamic_cast<RooProdPdf const *>(&func);
2141 if (!prod) {
2142 error("'" + name + "' has the attribute '" + arrayDistTag +
2143 "' of an array distribution, but it is not a RooProdPdf");
2144 }
2145 for (RooAbsPdf *pdf : static_range_cast<RooAbsPdf *>(prod->pdfList())) {
2146 const std::string writtenIn = exportAsArrayComponent(*pdf, name);
2147 if (writtenIn != name) {
2148 error("cannot write '" + name + "' as an array distribution: its component '" + pdf->GetName() +
2149 "' is already written as '" + writtenIn + "'");
2150 }
2151 }
2152 return;
2153 }
2154
2155 // HS3 lists some resolution models as functions.
2156 const bool isPdf = dynamic_cast<RooAbsPdf const *>(&func) && !func.InheritsFrom("RooGaussModel") &&
2157 !func.InheritsFrom("RooTruthModel");
2158 auto &collectionNode = (*_export->root)[isPdf ? "distributions" : "functions"];
2159
2160 auto const &exporters = RooFit::JSONIO::exporters();
2161
2162 TClass *cl = func.IsA();
2163
2164 auto &elem = appendNamedChild(collectionNode, name);
2165
2166 auto it = exporters.find(cl);
2167 if (it != exporters.end()) { // check if we have a specific exporter available
2168 for (auto &exp : it->second) {
2169 _serversToExport.clear();
2170 _serversToDelete.clear();
2171 if (!exp->exportObject(this, &func, elem)) {
2172 // The exporter might have messed with the content of the node
2173 // before failing. That's why we clear it and only reset the name.
2174 elem.clear();
2175 elem.set_map();
2176 elem["name"] << name;
2177 continue;
2178 }
2179 // Objects that are interpreted by the document are written here, the others were written by the exporter.
2180 if (_export->typedObject) {
2181 HS3::writeHistFactoryDist(elem, *_export->typedObject);
2182 elem["name"] << name;
2183 _export->doc.distributions.push_back({name, std::move(*_export->typedObject)});
2184 _export->typedObject.reset();
2185 if (!_export->channelConstraints.empty()) {
2186 _export->doc.rooFit.channelConstraints[name] = std::move(_export->channelConstraints);
2187 }
2188 _export->channelConstraints.clear();
2189 } else {
2190 recordGenericObject(_export->doc, isPdf, name, elem);
2191 }
2192 if (exp->autoExportDependants()) {
2193 exportObjects(func.servers());
2194 } else {
2196 }
2197 for (auto &s : _serversToDelete) {
2198 delete s;
2199 }
2200 return;
2201 }
2202 }
2203
2204 std::cerr << "unable to export '" << name << "' of class '" << cl->GetName()
2205 << "': no exporter is registered for this class, or none of them could handle the object. Check "
2206 "https://root.cern/doc/master/group__roofit__dev__docs__hs3.html to see how to write one!\n";
2207}
2208
2210{
2211 return _export->rootName;
2212}
2213
2214std::string RooJSONFactoryWSTool::exportAsArrayComponent(RooAbsPdf const &pdf, std::string const &arrayName)
2215{
2216 const std::string pdfName = pdf.GetName();
2217 auto &arrayOfComponent = _export->arrayOfComponent;
2218 auto &arrayComponents = _export->arrayComponents;
2219 auto &exportedObjectNames = _export->exportedObjectNames;
2220 if (auto it = arrayOfComponent.find(pdfName); it != arrayOfComponent.end()) {
2221 return it->second;
2222 }
2223 // Already written on its own: it is referenced by its name.
2224 if (exportedObjectNames.find(pdfName) != exportedObjectNames.end()) {
2225 return pdfName;
2226 }
2227 if (arrayComponents.find(arrayName) == arrayComponents.end()) {
2228 RooAbsArg const *existing = _workspace.arg(arrayName);
2229 if (existing && !existing->getAttribute(arrayDistTag)) {
2230 error("cannot write the array distribution '" + arrayName +
2231 "': the workspace has another object of that name");
2232 }
2233 exportedObjectNames.insert(arrayName);
2234 }
2235 arrayComponents[arrayName].push_back(&pdf);
2236 arrayOfComponent[pdfName] = arrayName;
2237 exportedObjectNames.insert(pdfName);
2238 return arrayName;
2239}
2240
2241// Write the array distributions registered with exportAsArrayComponent(), in alphabetical order, each followed by the
2242// servers of its components. They are generic objects of the document.
2244{
2245 std::set<std::string> written;
2246 auto &arrayComponents = _export->arrayComponents;
2247 for (;;) {
2248 auto next = std::find_if(arrayComponents.begin(), arrayComponents.end(),
2249 [&](auto const &item) { return written.find(item.first) == written.end(); });
2250 if (next == arrayComponents.end()) {
2251 break;
2252 }
2253 const std::string arrayName = next->first;
2254 written.insert(arrayName);
2255 // Exporting the servers may add to the registry, so the components are copied.
2256 const std::vector<RooAbsPdf const *> components = next->second;
2257 JSONNode &elem = appendNamedChild((*_export->root)["distributions"], arrayName);
2258 writeArrayDist(*this, elem, arrayName, components);
2259 recordGenericObject(_export->doc, true, arrayName, elem);
2260 for (RooAbsPdf const *pdf : components) {
2261 exportObjects(pdf->servers());
2262 }
2263 }
2264}
2265
2266void RooJSONFactoryWSTool::setConstraintObservable(RooAbsPdf const &pdf, std::string const &observable)
2267{
2268 _export->constraintObservables[pdf.GetName()] = observable;
2269}
2270
2271std::string const *RooJSONFactoryWSTool::constraintObservable(std::string const &pdfName) const
2272{
2273 if (!_export) {
2274 return nullptr;
2275 }
2276 auto found = _export->constraintObservables.find(pdfName);
2277 return found == _export->constraintObservables.end() ? nullptr : &found->second;
2278}
2279
2280void RooJSONFactoryWSTool::recordConstraintTerms(std::string const &pdfName, std::vector<std::string> const &terms)
2281{
2282 auto &out = _export->constraintTermsOfPdf[pdfName];
2283 for (auto const &term : terms) {
2284 addUnique(out, term);
2285 }
2286}
2287
2288// Write the data set `dataName` with the observed values of the observables of the constraint term `term` (an
2289// array distribution written in this export, or a single constraint pdf) and return true, or return false if the
2290// observables of the term are not all global observables of the model.
2291bool RooJSONFactoryWSTool::exportConstraintData(std::string const &term, RooArgSet const *globalObservables,
2292 std::string const &dataName)
2293{
2294 std::vector<RooAbsPdf const *> components;
2295 if (auto found = _export->arrayComponents.find(term); found != _export->arrayComponents.end()) {
2296 components = found->second;
2297 } else if (auto const *product = dynamic_cast<RooProdPdf const *>(_workspace.pdf(term))) {
2298 // A product of constraint terms: its factors.
2299 for (RooAbsArg const *factor : product->pdfList()) {
2300 components.push_back(static_cast<RooAbsPdf const *>(factor));
2301 }
2302 } else if (RooAbsPdf *pdf = _workspace.pdf(term)) {
2303 components.push_back(pdf);
2304 }
2305 std::vector<std::string> observables;
2306 for (RooAbsPdf const *pdf : components) {
2307 auto info = arrayComponentInfo(*pdf, constraintObservable(pdf->GetName()));
2308 if (!info || !globalObservables || !globalObservables->find(info->observable.c_str())) {
2309 return false;
2310 }
2311 observables.push_back(info->observable);
2312 }
2313 if (observables.empty()) {
2314 return false;
2315 }
2316 if (!_export->writtenConstraintData.insert(dataName).second) {
2317 return true;
2318 }
2319 if (_workspace.data(dataName)) {
2320 error("cannot write the observed values of the constraint term '" + term + "' as the data set '" + dataName +
2321 "': the workspace has a data set of that name");
2322 }
2324 for (std::string const &name : observables) {
2325 auto const *var = dynamic_cast<RooAbsReal const *>(_workspace.arg(name));
2326 if (!var) {
2327 error("the observable '" + name + "' of the constraint term '" + term + "' is not in the workspace");
2328 }
2329 // A range without a bound leaves the bound out, which means that the axis is unbounded there.
2331 axis.name = name;
2332 if (auto const *rrv = dynamic_cast<RooRealVar const *>(var)) {
2333 axis = toContinuousAxis(*rrv);
2334 }
2335 data.axes.emplace_back(std::move(axis));
2336 data.entries.push_back(var->getVal());
2337 }
2338 // The observed value of a single observable is point data.
2339 if (data.axes.size() == 1) {
2340 HS3::PointData point;
2341 point.value = data.entries.front();
2342 point.axes.push_back(std::get<HS3::ContinuousAxis>(data.axes.front()));
2343 _export->doc.data.push_back({dataName, std::move(point)});
2344 return true;
2345 }
2346 _export->doc.data.push_back({dataName, std::move(data)});
2347 return true;
2348}
2349
2350/**
2351 * @brief Export a RooSimultaneous as a "combined_dist".
2352 *
2353 * The components are selected by a key with one label per column. The columns are the index category of the
2354 * RooSimultaneous or, for a RooSuperCategory index, its input categories, sorted by name. They are declared as
2355 * categorical axes of the default domain. States without a pdf are not written: HS3 does not require a component
2356 * for every key.
2357 */
2359{
2360 RooAbsCategory const &indexCat = simPdf.indexCat();
2361 std::vector<RooCategory const *> columns = selectionColumns(indexCat);
2362
2363 // The distribution takes its place among the distributions here, and is written at the end of the export.
2364 appendNamedChild((*_export->root)["distributions"], simPdf.GetName());
2365 HS3::CombinedDist combined;
2366 for (RooCategory const *column : columns) {
2367 combined.columns.push_back(column->GetName());
2368 recordCategory(*column);
2369 }
2370 // Components with renamed observables replace the original ones (Config::renameCombinedObservables).
2371 auto renamed = _renamedComponents.find(simPdf.GetName());
2372 std::set<RooAbsArg const *> replaced;
2373 std::vector<RooAbsArg const *> replacements;
2374 // In the order of the columns and of their states, which is the order in which the categories are written to
2375 // the domain. This does not depend on the order of the inputs of a RooSuperCategory, so the document is the same
2376 // after a round trip.
2377 for (std::vector<std::string> const &key : selectionKeys(columns)) {
2378 const std::string label = selectionLabel(indexCat, columns, key);
2379 RooAbsPdf const *pdf = simPdf.getPdf(label);
2380 if (!pdf) {
2381 continue;
2382 }
2383 if (renamed != _renamedComponents.end()) {
2384 auto found = renamed->second.find(label);
2385 if (found != renamed->second.end()) {
2386 replaced.insert(pdf);
2387 pdf = found->second;
2388 replacements.push_back(pdf);
2389 }
2390 }
2391 combined.components.push_back({key, {pdf->GetName()}});
2392 }
2393 _export->doc.distributions.push_back({simPdf.GetName(), std::move(combined)});
2394
2395 std::vector<RooAbsArg const *> servers;
2396 for (RooAbsArg const *server : simPdf.servers()) {
2397 if (replaced.count(server) == 0) {
2398 servers.push_back(server);
2399 }
2400 }
2401 servers.insert(servers.end(), replacements.begin(), replacements.end());
2402 exportObjects(servers);
2403}
2404
2405// Declare a category as a categorical axis of the "default_domain". The states are written in their insertion order:
2406// this is the order of the bins of a RooDataHist over the category, and the position becomes the index when the
2407// document is read back.
2409{
2410 HS3::CategoricalAxis axis = toCategoricalAxis(cat);
2411 auto found = _export->categories.find(axis.name);
2412 if (found != _export->categories.end() && found->second.categories != axis.categories) {
2413 RooJSONFactoryWSTool::error("the category '" + axis.name +
2414 "' is written to the domain twice, with different states");
2415 }
2416 _export->categories[axis.name] = std::move(axis);
2417}
2418
2419/// Import the function or distribution `p` into the workspace, with the importers that are registered for its type.
2420/// If `importAllDependants` is false, `p` is a fragment of a document, whose own "functions" and "distributions" are
2421/// imported first.
2422void RooJSONFactoryWSTool::importFunction(const JSONNode &p, bool importAllDependants)
2423{
2424 // To report the components that no importer understands.
2425 const RooFit::Detail::JSONReadRecorder recorder{p};
2426 std::string name(RooJSONFactoryWSTool::name(p));
2427
2428 // If this node if marked to be skipped by RooFit, exit
2429 if (hasAttribute(name, "roofit_skip")) {
2430 return;
2431 }
2432
2433 auto const &importers = RooFit::JSONIO::importers();
2434
2436
2437 if (_workspace.arg(name)) {
2438 return;
2439 }
2440 ImportGuard importGuard{_importStack, name};
2441 if (!p.is_map()) {
2442 RooJSONFactoryWSTool::error("RooJSONFactoryWSTool() function node " + name + " is not a map!");
2443 }
2444 std::string prefix = genPrefix(p, true);
2445 if (!prefix.empty())
2446 name = prefix + name;
2447 if (!p.has_child("type")) {
2448 RooJSONFactoryWSTool::error("RooJSONFactoryWSTool() no type given for function '" + name + "', skipping.");
2449 }
2450
2451 std::string functype(p["type"].val());
2452
2453 // import all dependents if importing a workspace, not for creating new objects
2454 if (!importAllDependants) {
2455 this->importDependants(p);
2456 }
2457
2458 auto it = importers.find(functype);
2459 bool ok = false;
2460 if (it != importers.end()) {
2461 for (auto &imp : it->second) {
2462 ok = imp->importArg(this, p);
2463 if (ok)
2464 break;
2465 }
2466 }
2467 if (!ok) {
2469 "RooJSONFactoryWSTool() no handling for type '" + functype +
2470 "' implemented. Check https://root.cern/doc/master/group__roofit__dev__docs__hs3.html "
2471 "to see how to write an importer for it!");
2472 }
2474 if (!func) {
2475 RooJSONFactoryWSTool::error("something went wrong importing function '" + name + "'.");
2476 }
2477 for (std::string const &component : recorder.unreadComponents()) {
2478 warning("RooFitHS3: " + name + ": unknown component \"" + component + "\" is ignored");
2479 }
2480}
2481
2482// Import a distribution of the document, which is interpreted by the document or by the registered importers.
2484{
2485 if (auto const *generic = std::get_if<HS3::GenericObject>(&distribution.body)) {
2486 this->importFunction(*generic->node, true);
2487 return;
2488 }
2489 std::string const &name = distribution.name;
2490 if (hasAttribute(name, "roofit_skip")) {
2491 return;
2492 }
2494 if (_workspace.arg(name)) {
2495 return;
2496 }
2497 ImportGuard importGuard{_importStack, name};
2498 if (auto const *combined = std::get_if<HS3::CombinedDist>(&distribution.body)) {
2499 HS3::buildCombinedDist(*this, name, *combined, otherLikelihoodTerms(_import->document, name));
2500 return;
2501 }
2502 HS3::buildHistFactoryDist(*this, name, std::get<HS3::HistFactoryDist>(distribution.body));
2503}
2504
2505/// Import a function or distribution that is given as a JSON string, see the overload for a JSONNode.
2506void RooJSONFactoryWSTool::importFunction(const std::string &jsonString, bool importAllDependants)
2507{
2508 this->importFunction((JSONTree::create(jsonString))->rootnode(), importAllDependants);
2509}
2510
2511/// Write the axes and the `n` bin contents of a histogram over the variables `vars`, which are RooRealVars or
2512/// RooCategories, in the order of the variables.
2513void RooJSONFactoryWSTool::exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, JSONNode &output)
2514{
2515 HS3::writeBinnedData(output, toBinnedData(vars, n, contents));
2516}
2517
2518/// Write `n` numbers as an array, each as an integer if it has no fractional part. RooFit doesn't use this function
2519/// itself, but exporters outside of ROOT do.
2520void RooJSONFactoryWSTool::exportArray(std::size_t n, double const *contents, JSONNode &output)
2521{
2522 HS3::writeContents(output, std::vector<double>(contents, contents + n));
2523}
2524
2525// Config::renameCombinedObservables: work out, before anything is exported, the new names of the observables of
2526// every combined data set, and the components of the matching RooSimultaneous with these observables. One
2527// observable per key is named "obs_<key>", several are named "obs_<key>_<observable>". Observables that already
2528// have their new name are left alone, so that a second export of an imported document gives the same result.
2530{
2532 _renamedComponents.clear();
2533 _renamedObjects.clear();
2534 if (!config().renameCombinedObservables) {
2535 return;
2536 }
2537 std::vector<RooAbsData *> datasets;
2538 for (RooAbsData *data : _workspace.allData()) {
2539 datasets.push_back(data);
2540 }
2541 sortByName(datasets);
2542 for (RooAbsData const *data : datasets) {
2543 std::vector<RooCategory const *> columns = selectionColumns(*data->get());
2544 if (columns.empty()) {
2545 continue;
2546 }
2547 RooSimultaneous const *simPdf = findMatchingSimultaneous(_workspace, namesOf(selectionColumns(*data->get())));
2548 for (std::vector<std::string> const &key : selectionKeys(columns)) {
2549 const std::string keyName = selectionKeyName(key);
2550 RooArgSet candidates;
2551 RooAbsPdf const *component = nullptr;
2552 std::string label;
2553 if (simPdf) {
2554 label = selectionLabel(simPdf->indexCat(), columns, key);
2555 component = simPdf->getPdf(label);
2556 if (!component) {
2557 continue;
2558 }
2559 std::unique_ptr<RooArgSet> observables{component->getObservables(*data->get())};
2560 candidates.add(*observables);
2561 } else {
2562 candidates.add(*data->get());
2563 }
2564 std::vector<RooAbsArg *> observables;
2565 for (RooAbsArg *arg : candidates) {
2566 if (!dynamic_cast<RooAbsCategory *>(arg)) {
2567 observables.push_back(arg);
2568 }
2569 }
2570 sortByName(observables);
2571 std::map<std::string, std::string> renames;
2572 for (RooAbsArg const *obs : observables) {
2573 std::string const newName =
2574 "obs_" + keyName + (observables.size() == 1 ? "" : "_" + std::string(obs->GetName()));
2575 if (newName == obs->GetName()) {
2576 continue;
2577 }
2578 if (_workspace.arg(newName.c_str())) {
2579 error("can't rename the observable '" + std::string(obs->GetName()) + "' of the data set '" +
2580 data->GetName() + "' to '" + newName + "': an object of that name already exists");
2581 }
2582 renames[obs->GetName()] = newName;
2583 }
2584 if (renames.empty()) {
2585 continue;
2586 }
2587 _combinedObservableRenames[data->GetName()][keyName] = renames;
2588 if (!simPdf || _renamedComponents[simPdf->GetName()].count(label) > 0) {
2589 continue;
2590 }
2591 // The component with the renamed observables. Nodes that depend on them are cloned with the suffix
2592 // "_<key>", so that nodes shared by several components get one copy per component.
2593 RooCustomizer customizer{*component, keyName.c_str()};
2594 for (auto const &[from, to] : renames) {
2595 RooAbsArg *original = candidates.find(from.c_str());
2596 _renamedObjects.emplace_back(static_cast<RooAbsArg *>(original->clone(to.c_str())));
2597 customizer.replaceArg(*original, *_renamedObjects.back());
2598 }
2599 auto *renamedComponent = static_cast<RooAbsPdf *>(customizer.build());
2600 _renamedObjects.emplace_back(renamedComponent);
2601 _renamedComponents[simPdf->GetName()][label] = renamedComponent;
2602 }
2603 }
2604}
2605
2606/**
2607 * @brief Export a data set with category observables as a "combined" data set.
2608 *
2609 * The categories are the columns, sorted by name, and are declared as categorical axes of the default domain. There
2610 * is one sub-data set per key, i.e. per combination of states, written like any other data set and named after the
2611 * data set and the labels of the key. A key is written also without entries: RooFit treats a channel without entries
2612 * as present and empty. If the workspace has a RooSimultaneous with the same columns, each sub-data set only carries
2613 * the observables of the corresponding component, and keys without entries for which it has no component are left
2614 * out; otherwise, every sub-data set carries all observables.
2615 */
2617{
2618 std::vector<RooCategory const *> columns = selectionColumns(*data.get());
2619 std::string const name = data.GetName();
2620 HS3::CombinedData combined;
2621 for (RooCategory const *column : columns) {
2622 combined.columns.push_back(column->GetName());
2623 recordCategory(*column);
2624 }
2625
2626 // Split the data by the key: by the index category of a matching RooSimultaneous, or by the columns themselves.
2627 RooSimultaneous const *simPdf = findMatchingSimultaneous(_workspace, combined.columns);
2628 std::unique_ptr<RooSuperCategory> superCat;
2629 RooAbsCategory const *indexCat = nullptr;
2630 std::vector<std::unique_ptr<RooAbsData>> parts;
2631 if (simPdf) {
2632 indexCat = &simPdf->indexCat();
2633 parts = data.split(*simPdf, true);
2634 } else if (columns.size() == 1) {
2635 indexCat = columns.front();
2636 parts = data.split(*indexCat, true);
2637 } else {
2638 RooArgSet inputs;
2639 for (RooCategory const *column : columns) {
2640 inputs.add(*column);
2641 }
2642 const std::string superName = name + "_key";
2643 superCat = std::make_unique<RooSuperCategory>(superName.c_str(), superName.c_str(), inputs);
2644 indexCat = superCat.get();
2645 parts = data.split(*indexCat, true);
2646 }
2647
2648 // The combined data set is written before its sub-data sets.
2649 std::vector<RooAbsData *> subDataSets;
2650 for (std::vector<std::string> const &key : selectionKeys(columns)) {
2651 std::string const label = selectionLabel(*indexCat, columns, key);
2652 auto found = std::find_if(parts.begin(), parts.end(), [&](auto &d) { return d->GetName() == label; });
2653 if (found == parts.end() || ((*found)->numEntries() == 0 && simPdf && !simPdf->getPdf(label))) {
2654 continue;
2655 }
2656 const std::string keyName = selectionKeyName(key);
2657 const std::string subName = name + "_" + keyName;
2658 (*found)->SetName(subName.c_str());
2659 // Config::renameCombinedObservables: give the observables of this key their own names.
2660 auto renamesOfData = _combinedObservableRenames.find(name);
2661 if (renamesOfData != _combinedObservableRenames.end()) {
2662 auto renames = renamesOfData->second.find(keyName);
2663 if (renames != renamesOfData->second.end()) {
2664 for (auto const &[from, to] : renames->second) {
2665 (*found)->changeObservableName(from.c_str(), to.c_str());
2666 }
2667 }
2668 }
2669 combined.components.push_back({key, {subName}});
2670 subDataSets.push_back(found->get());
2671 }
2672 _export->doc.data.push_back({name, std::move(combined)});
2673 for (RooAbsData *subData : subDataSets) {
2674 exportData(*subData);
2675 }
2676}
2677
2678// Export a single dataset `data` (binned or unbinned) to the output document.
2680{
2681 for (RooAbsArg const *obs : *data.get()) {
2682 if (dynamic_cast<RooAbsCategory const *>(obs) && !dynamic_cast<RooCategory const *>(obs)) {
2683 error("the data set '" + std::string(data.GetName()) + "' has the categorical observable '" + obs->GetName() +
2684 "', which is not a RooCategory, and can't be written as a combined data set");
2685 }
2686 }
2687
2688 auto write = [&](auto body) { _export->doc.data.push_back({data.GetName(), std::move(body)}); };
2689
2690 // This works around a problem in RooStats/HistFactory that was only fixed
2691 // in ROOT 6.30: until then, the weight variable of the observed dataset,
2692 // called "weightVar", was added to the observables. Therefore, it also got
2693 // added to the Asimov dataset. But the Asimov has its own weight variable,
2694 // called "binWeightAsimov", making "weightVar" an actual observable in the
2695 // Asimov data. But this is only by accident and should be removed.
2696 RooArgSet variables = *data.get();
2697 if (auto weightVar = variables.find("weightVar")) {
2698 variables.remove(*weightVar);
2699 }
2700
2701 // The ranges of the variables are written to the domains. Categories are declared by a categorical axis of the
2702 // default domain.
2703 for (RooAbsArg const *arg : variables) {
2704 if (auto const *cat = dynamic_cast<RooCategory const *>(arg)) {
2705 recordCategory(*cat);
2706 } else {
2707 recordDomains(_export->domains, static_cast<RooRealVar const &>(*arg));
2708 }
2709 }
2710
2711 // this is a regular binned dataset
2712 if (auto dh = dynamic_cast<RooDataHist const *>(&data)) {
2713 return write(toBinnedData(variables, dh->numEntries(), dh->weightArray()));
2714 }
2715
2716 // Check if this actually represents a binned dataset, and then import it
2717 // like a RooDataHist. This happens frequently when people create combined
2718 // RooDataSets from binned data to fit HistFactory models. In this case, it
2719 // doesn't make sense to export them like an unbinned dataset, because the
2720 // coordinates are redundant information with the binning. We only do this
2721 // for 1D data for now. A variable without bins can't be binned data: unbinned data sets leave their axes without
2722 // binning, and would otherwise be written as binned once they have no entries.
2723 if (data.isWeighted() && variables.size() == 1 && dynamic_cast<RooRealVar const *>(variables[0]) &&
2724 static_cast<RooRealVar const &>(*variables[0]).getBins() > 0) {
2725 auto &x = static_cast<RooRealVar const &>(*variables[0]);
2726 std::vector<double> contents;
2727 int i = 0;
2728 for (; i < data.numEntries(); ++i) {
2729 data.get(i);
2730 if (x.getBin() != i)
2731 break;
2732 contents.push_back(data.weight());
2733 }
2734 // Binned data has exactly one entry per bin, in the order of the bins. Further entries, e.g. a second one in
2735 // the last bin, make it unbinned data.
2736 if (i == x.getBins() && data.numEntries() == x.getBins()) {
2737 return write(toBinnedData(variables, contents.size(), contents.data()));
2738 }
2739 }
2740
2741 // A single unweighted entry of a single variable is point data.
2742 if (variables.size() == 1 && data.numEntries() == 1) {
2743 data.get(0);
2744 auto const *var = dynamic_cast<RooRealVar const *>(variables[0]);
2745 if (var && (!data.isWeighted() || data.weight() == 1.)) {
2746 HS3::PointData point;
2747 point.value = var->getVal();
2748 point.axes.push_back(toContinuousAxis(*var));
2749 return write(std::move(point));
2750 }
2751 }
2752
2753 // this really is an unbinned dataset
2754 HS3::UnbinnedData unbinned;
2755 for (RooAbsArg const *arg : variables) {
2756 testValidName(arg->GetName(), false);
2757 if (auto const *cat = dynamic_cast<RooCategory const *>(arg)) {
2758 unbinned.axes.emplace_back(toCategoricalAxis(*cat));
2759 } else {
2760 // HS3 forbids binning information on the axes of unbinned data.
2761 auto const &var = static_cast<RooRealVar const &>(*arg);
2762 unbinned.axes.emplace_back(toContinuousAxis(var));
2763 }
2764 }
2765 std::vector<double> weights;
2766 for (int i = 0; i < data.numEntries(); ++i) {
2767 data.get(i);
2768 // The entries of a categorical axis are the positions of the labels in its "categories".
2769 for (RooAbsArg const *arg : variables) {
2770 if (auto const *cat = dynamic_cast<RooCategory const *>(arg)) {
2771 unbinned.entries.push_back(cat->getCurrentOrdinalNumber());
2772 } else {
2773 unbinned.entries.push_back(static_cast<RooRealVar const *>(arg)->getVal());
2774 }
2775 }
2776 weights.push_back(data.weight());
2777 }
2778 // Weights that are all one are not written.
2779 if (std::any_of(weights.begin(), weights.end(), [](double weight) { return weight != 1.; })) {
2780 unbinned.weights = std::move(weights);
2781 }
2782 write(std::move(unbinned));
2783}
2784
2785/// Create the variables for the "axes" of `topNode`, which are owned by the returned set.
2787{
2788 HS3::Diagnostics diagnostics;
2789 std::vector<HS3::BinnedAxis> axes = HS3::parseBinnedAxes(topNode["axes"], diagnostics);
2790 HS3::report(diagnostics);
2791 return HS3::createObservables(axes);
2792}
2793
2794/// Create a RooDataHist with the name `name` over the variables `vars` from the "contents" of `n`, and its "errors"
2795/// if it has any.
2796std::unique_ptr<RooDataHist>
2797RooJSONFactoryWSTool::readBinnedData(const JSONNode &n, const std::string &name, RooArgSet const &vars)
2798{
2799 if (!n.has_child("contents"))
2800 RooJSONFactoryWSTool::error("no contents given");
2801
2802 JSONNode const &contents = n["contents"];
2803
2804 if (!contents.is_seq())
2805 RooJSONFactoryWSTool::error("contents are not in list form");
2806
2807 std::vector<double> contentVals;
2808 contentVals << contents;
2809
2810 if (!n.has_child("errors")) {
2811 return HS3::createDataHist(name, vars, contentVals, nullptr);
2812 }
2813 if (!n["errors"].is_seq())
2814 RooJSONFactoryWSTool::error("errors are not in list form");
2815 std::vector<double> errorVals;
2816 errorVals << n["errors"];
2817 return HS3::createDataHist(name, vars, contentVals, &errorVals);
2818}
2819
2820// Import a single variable (RooRealVar or RooConstVar) from the parameter `p` into the workspace.
2822{
2824
2825 if (_workspace.arg(p.name))
2826 return;
2827 // A string selects the state of a category, which is done together with the other parameter points.
2828 if (p.value && std::holds_alternative<std::string>(*p.value))
2829 return;
2830 if (!_import->ranges.contains(p.name)) {
2831 // ROOT versions before HS3 0.3 marked named RooConstVars with an attribute.
2832 auto const &attributes = _import->document.rooFit.attributes;
2833 auto found = attributes.find(p.name);
2834 if (found != attributes.end() && found->second.isConstVar) {
2835 if (!p.value) {
2836 RooJSONFactoryWSTool::error("cannot instantiate RooConstVar '" + p.name + "' without \"value\"!");
2837 }
2838 wsEmplace<RooConstVar>(p.name, std::get<double>(*p.value));
2839 return;
2840 }
2841 }
2842 RooRealVar &var = wsEmplace<RooRealVar>(p.name, 1.);
2843 configureVariable(_import->ranges, p, var);
2845}
2846
2847// HS3 requires every parameter to be listed in a domain or constant ("const": true) in the parameter point.
2849{
2850 if (!var.isConstant() && !_import->ranges.contains(var.GetName())) {
2851 warning(std::string("RooFitHS3: the parameter '") + var.GetName() +
2852 "' is not listed in any domain and is not constant: HS3 requires a domain, or \"const\": true. It "
2853 "is read as a free parameter without range.");
2854 }
2855}
2856
2857// Import the objects of a fragment of a document, for which the node `n` plays the role of the document.
2859{
2860 for (const char *section : {"functions", "distributions"}) {
2861 if (auto seq = n.find(section)) {
2862 for (const auto &p : seq->children()) {
2863 this->importFunction(p, true);
2864 }
2865 }
2866 }
2867}
2868
2869// Export a ModelConfig as one analysis for every data set that goes with its pdf, or as one without data.
2871 const std::vector<RooAbsData *> &singleDataSets)
2872{
2873 std::vector<std::string> dataNames;
2874 if (auto simpdf = dynamic_cast<RooSimultaneous const *>(mc.GetPdf())) {
2875 // The combined data sets with the same columns.
2876 const std::vector<std::string> columns = namesOf(selectionColumns(simpdf->indexCat()));
2877 for (HS3::Data const &data : _export->doc.data) {
2878 auto const *combined = std::get_if<HS3::CombinedData>(&data.body);
2879 if (combined && combined->columns == columns) {
2880 dataNames.push_back(data.name);
2881 }
2882 }
2883 } else {
2884 for (RooAbsData const *data : singleDataSets) {
2885 if (mc.GetObservables()->equals(*data->get())) {
2886 dataNames.push_back(data->GetName());
2887 }
2888 }
2889 }
2890 // The analyses are named after the ModelConfig, so that ModelConfigs sharing a pdf and a data set (like an S+B
2891 // and B-only pair) get separate analyses.
2892 for (std::string const &dataName : dataNames) {
2893 exportSingleModelConfig(mc, std::string(mc.GetName()) + "_" + dataName, &dataName);
2894 }
2895 if (dataNames.empty()) {
2896 exportSingleModelConfig(mc, mc.GetName(), nullptr);
2897 }
2898}
2899
2900namespace {
2901
2902// The channels of the pdf of a ModelConfig as they are written: the pdf itself, or the components of a RooSimultaneous,
2903// of which those in `renamed` (by label) replace the original ones (Config::renameCombinedObservables).
2904std::vector<RooAbsPdf const *> channelsOf(RooAbsPdf const &pdf, std::map<std::string, RooAbsPdf const *> const &renamed)
2905{
2906 auto const *simPdf = dynamic_cast<RooSimultaneous const *>(&pdf);
2907 if (!simPdf) {
2908 return {&pdf};
2909 }
2910 std::vector<RooAbsPdf const *> channels;
2911 for (auto const &item : simPdf->indexCat()) {
2912 auto found = renamed.find(item.first);
2913 if (RooAbsPdf const *component = found != renamed.end() ? found->second : simPdf->getPdf(item.first)) {
2914 channels.push_back(component);
2915 }
2916 }
2917 return channels;
2918}
2919
2920// A constraint term of a channel of the pdf of a ModelConfig, see constraintsOfChannels().
2921struct ChannelConstraint {
2922 std::string channel;
2923 RooAbsPdf const *constraint = nullptr;
2924 bool histFactory = false; ///< Whether the channel is written as a histfactory_dist.
2925 /// Whether the channel consists of constraint terms only; `constraint` is then the channel itself.
2926 bool wholeChannel = false;
2927};
2928
2929// The constraint terms of the pdf `pdf` of a ModelConfig with the observables `observables`, which are written as
2930// terms of the likelihood: the constraints that RooFit finds (RooAbsPdf::getAllConstraints()) and that are factors
2931// of a channel, i.e. of `pdf` itself or of a component of the RooSimultaneous `pdf`. In RooFit, constraints are
2932// factors of the channels, and RooFit counts each once, also if several channels share it; in HS3, they are terms
2933// of the likelihood.
2934// - The channels that are written as histfactory_dist (`isHistFactoryChannel`) don't contain their constraints in
2935// the document; their modifiers refer to them. Nested products of their constraint terms are expanded into the
2936// factors, which the HistFactory exporter writes as terms of their own.
2937// - The constraints of a channel that is written as product_dist are taken out of the product by
2938// writeLikelihoodTermsOnce(). This is only done if no other object of the workspace uses the product, so the
2939// constraints of other products are not returned.
2940// Constraints elsewhere, e.g. in a nested product, stay where they are. The channels are taken as they are written,
2941// see channelsOf().
2942std::vector<ChannelConstraint>
2943constraintsOfChannels(RooWorkspace const &ws, RooAbsPdf const &pdf, RooArgSet const &observables,
2944 std::function<bool(std::string const &)> const &isHistFactoryChannel,
2945 std::vector<RooAbsPdf const *> const &channels)
2946{
2947 RooArgSet params;
2948 pdf.getParameters(&observables, params);
2949 std::unique_ptr<RooArgSet> constraints{pdf.getAllConstraints(observables, params, /*stripDisconnected=*/false)};
2950 std::vector<ChannelConstraint> out;
2951 for (RooAbsPdf const *channel : channels) {
2952 auto const *prod = dynamic_cast<RooProdPdf const *>(channel);
2953 if (!prod) {
2954 continue;
2955 }
2956 const bool histFactory = isHistFactoryChannel(prod->GetName());
2957 // A channel that consists of constraint terms only is an auxiliary measurement that is part of the model. It
2958 // is written as that channel, once, and not as constraint terms of the likelihood.
2959 if (!histFactory && !prod->pdfList().empty() &&
2960 std::all_of(prod->pdfList().begin(), prod->pdfList().end(),
2961 [&](RooAbsArg const *factor) { return constraints->find(*factor) != nullptr; })) {
2962 out.push_back({prod->GetName(), prod, false, true});
2963 continue;
2964 }
2965 if (!histFactory) {
2966 // Being a component of a RooSimultaneous, i.e. of a combined distribution, is not a use.
2967 bool usedElsewhere = false;
2968 for (RooAbsArg const *client : prod->clients()) {
2969 if (ws.arg(client->GetName()) == client && !dynamic_cast<RooSimultaneous const *>(client)) {
2970 usedElsewhere = true;
2971 }
2972 }
2973 if (usedElsewhere) {
2974 continue;
2975 }
2976 }
2977 std::function<void(RooAbsPdf const &)> add = [&](RooAbsPdf const &term) {
2978 auto const *nested = dynamic_cast<RooProdPdf const *>(&term);
2979 if (!histFactory || !nested) {
2980 out.push_back({prod->GetName(), &term, histFactory});
2981 return;
2982 }
2983 for (RooAbsArg const *factor : nested->pdfList()) {
2984 add(static_cast<RooAbsPdf const &>(*factor));
2985 }
2986 };
2987 for (RooAbsArg const *factor : prod->pdfList()) {
2988 if (constraints->find(*factor)) {
2989 add(static_cast<RooAbsPdf const &>(*factor));
2990 }
2991 }
2992 }
2993 return out;
2994}
2995
2996} // namespace
2997
2998void RooJSONFactoryWSTool::exportSingleModelConfig(RooFit::ModelConfig const &mc, std::string const &analysisName,
2999 std::string const *dataName)
3000{
3001 HS3Document &doc = _export->doc;
3002 auto pdf = mc.GetPdf();
3003
3004 HS3::Analysis analysis;
3005 analysis.name = analysisName;
3006
3007 // HS3 names must be unique across all top-level components, so the likelihood can't have the analysis name.
3008 HS3::Likelihood likelihood;
3009 likelihood.name = analysisName + "_likelihood";
3010 analysis.likelihood.name = likelihood.name;
3011
3012 if (dataName) {
3013 likelihood.terms.push_back({{pdf->GetName()}, HS3::DataRef{*dataName}});
3014 } else {
3015 // Without data, the likelihood is a pure model, which is written with "data": null.
3016 likelihood.terms.push_back({{pdf->GetName()}, std::monostate{}});
3017 likelihood.pureModel = true;
3018 }
3019
3020 // The additional terms of the likelihood: the external constraints of the ModelConfig, and the constraint terms
3021 // of the channels of the model, which can be the components of a combined distribution (constraintsOfChannels()).
3022 // They are listed in "aux_distributions" with the observed values of their observables as constant parameters, or
3023 // (config().exportConstraintData) in "distributions", each paired with a data set that holds the observed values.
3024 std::vector<RooAbsPdf const *> const channels = channelsOf(*pdf, _renamedComponents[pdf->GetName()]);
3025 // The channels of the model that consist of constraint terms only, see constraintsOfChannels().
3026 std::vector<std::string> auxiliaryChannelsOfModel;
3027 std::vector<std::string> constraintTerms;
3028 if (mc.GetExternalConstraints()) {
3029 for (const auto &constr : *mc.GetExternalConstraints()) {
3030 addUnique(constraintTerms, constr->GetName());
3031 }
3032 }
3033 // The constraint terms that the HistFactory exporter wrote for the channel `channel`, in the order of the document:
3034 // the constraints that the modifiers name, as written (e.g. array distributions), and those that no modifier
3035 // names.
3036 auto writtenByHistFactory = [&](std::string const &channel) {
3037 std::vector<std::string> out;
3038 if (auto found = _export->constraintTermsOfPdf.find(channel); found != _export->constraintTermsOfPdf.end()) {
3039 for (std::string const &term : found->second) {
3040 addUnique(out, term);
3041 }
3042 }
3043 if (auto found = doc.rooFit.channelConstraints.find(channel); found != doc.rooFit.channelConstraints.end()) {
3044 for (auto const &ref : found->second) {
3045 addUnique(out, ref.name);
3046 }
3047 }
3048 return out;
3049 };
3050 RooArgSet const *observables = mc.GetObservables();
3051 if (!observables || observables->empty()) {
3052 // Without observables (e.g. a ModelConfig without data), RooFit can't tell constraints from other pdfs: only the
3053 // constraint terms of the HistFactory channels are listed.
3054 for (RooAbsPdf const *channel : channels) {
3055 for (std::string const &term : writtenByHistFactory(channel->GetName())) {
3056 addUnique(constraintTerms, term);
3057 }
3058 }
3059 } else {
3060 auto isHistFactoryChannel = [&](std::string const &name) {
3061 return _export->constraintTermsOfPdf.count(name) || HS3::find<HS3::HistFactoryDist>(doc.distributions, name);
3062 };
3063 // The constraint terms of the channels as RooFit finds them, under the names under which they are written:
3064 // the array distribution that holds a constraint of a HistFactory channel, or the name of the constraint.
3065 std::map<std::string, std::vector<std::string>> termsOfChannel;
3066 for (ChannelConstraint const &item :
3067 constraintsOfChannels(_workspace, *pdf, *observables, isHistFactoryChannel, channels)) {
3068 if (item.wholeChannel) {
3069 auxiliaryChannelsOfModel.push_back(item.channel);
3070 continue;
3071 }
3072 // A component of an array distribution is never referred to by itself: the array is the term.
3073 std::string term = item.constraint->GetName();
3074 if (auto found = _export->arrayOfComponent.find(term); found != _export->arrayOfComponent.end()) {
3075 term = found->second;
3076 }
3077 addUnique(termsOfChannel[item.channel], term);
3078 }
3079 for (RooAbsPdf const *channelPdf : channels) {
3080 std::string const channel = channelPdf->GetName();
3081 std::vector<std::string> const &found = termsOfChannel[channel];
3082 if (!isHistFactoryChannel(channel)) {
3083 for (std::string const &term : found) {
3084 addUnique(constraintTerms, term);
3085 }
3086 continue;
3087 }
3088 // The constraint terms that the HistFactory exporter wrote must be the ones that RooFit finds, or a
3089 // constraint would be missing from the likelihood, or would be in it without being written. They are listed
3090 // in the order of the document, which doesn't depend on how the workspace was built.
3091 std::vector<std::string> const written = writtenByHistFactory(channel);
3092 std::set<std::string> const writtenSet{written.begin(), written.end()};
3093 std::set<std::string> const foundSet{found.begin(), found.end()};
3094 if (writtenSet != foundSet) {
3095 auto join = [](std::set<std::string> const &names) {
3096 std::string out;
3097 for (std::string const &name : names) {
3098 out += (out.empty() ? "'" : ", '") + name + "'";
3099 }
3100 return out.empty() ? std::string{"none"} : out;
3101 };
3102 error("cannot export the constraint terms of the HistFactory channel '" + channel +
3103 "': the channel writes " + join(writtenSet) + ", but RooFit finds " + join(foundSet));
3104 }
3105 for (std::string const &term : written) {
3106 addUnique(constraintTerms, term);
3107 }
3108 }
3109 }
3110 // An array of constraints with its data set is a measurement like the others: if the model is a combined
3111 // distribution, it is written as a component of it, paired with its data set as a component of the combined data
3112 // set. The combined distribution is then the sampling distribution of all measurements, auxiliary or not. Its
3113 // label is the name of the array. Constraints that are written one by one stay terms of the likelihood: there
3114 // can be hundreds, which is not what an index category is for. So do the constraints of models whose index has
3115 // several columns.
3116 auto const *simPdf = dynamic_cast<RooSimultaneous const *>(pdf);
3117 // Looked up when needed: writing the data set of a constraint term adds to the data sets of the document.
3118 auto findCombinedData = [&]() { return dataName ? HS3::find<HS3::CombinedData>(doc.data, *dataName) : nullptr; };
3119 RooCategory const *indexColumn = nullptr;
3120 if (simPdf && findCombinedData()) {
3121 std::vector<RooCategory const *> columns = selectionColumns(simPdf->indexCat());
3122 if (columns.size() == 1) {
3123 indexColumn = columns.front();
3124 }
3125 }
3126 auto addAuxiliaryChannel = [&](std::string const &term) {
3127 if (indexColumn->hasLabel(term)) {
3128 error("cannot write the constraint term '" + term + "' as a component of the combined distribution '" +
3129 simPdf->GetName() + "': its index category '" + indexColumn->GetName() + "' has a state of that name");
3130 }
3131 addUnique(_export->auxiliaryLabels[indexColumn->GetName()], term);
3132 auto *combinedDist = HS3::find<HS3::CombinedDist>(doc.distributions, simPdf->GetName());
3133 if (!combinedDist) {
3134 error("cannot write the constraint terms of '" + std::string(simPdf->GetName()) +
3135 "' as its components: it was not written as a combined distribution");
3136 }
3137 if (!combinedDist->find({term})) {
3138 combinedDist->components.push_back({{term}, {term}});
3139 doc.rooFit.auxiliaryChannels[simPdf->GetName()].push_back({term});
3140 }
3141 if (HS3::CombinedData *combinedData = findCombinedData(); !combinedData->find({term})) {
3142 combinedData->components.push_back({{term}, HS3::DataRef{*dataName + "_" + term}});
3143 }
3144 };
3145 // A channel of the model that consists of constraint terms is an auxiliary measurement, whose observed values are
3146 // the values of its global observables if the data set has no entries for it. It is written like the arrays
3147 // above: with a data set of the observed values, and marked for RooFit, which can read it as constraint terms.
3148 for (std::string const &channel : auxiliaryChannelsOfModel) {
3149 if (!indexColumn || likelihood.pureModel) {
3150 continue;
3151 }
3152 std::string label;
3153 for (auto const &state : simPdf->indexCat()) {
3154 RooAbsPdf const *component = simPdf->getPdf(state.first);
3155 if (component && channel == component->GetName()) {
3156 label = state.first;
3157 }
3158 }
3159 if (label.empty()) {
3160 continue;
3161 }
3162 auto const *component = findCombinedData()->find({label});
3163 const std::string observed = component ? component->ref.name : *dataName + "_" + label;
3164 // A data set without entries for the channel is replaced by the observed values.
3165 auto written = std::find_if(doc.data.begin(), doc.data.end(), [&](auto const &d) { return d.name == observed; });
3166 if (written == doc.data.end() || written->numEntries() == 0) {
3167 std::optional<HS3::Data> empty;
3168 if (written != doc.data.end()) {
3169 empty = std::move(*written);
3170 doc.data.erase(written);
3171 }
3172 if (exportConstraintData(channel, mc.GetGlobalObservables(), observed)) {
3173 if (HS3::CombinedData *combinedData = findCombinedData(); !combinedData->find({label})) {
3174 combinedData->components.push_back({{label}, HS3::DataRef{observed}});
3175 }
3176 } else {
3177 if (empty) {
3178 doc.data.push_back(std::move(*empty));
3179 }
3180 warning("RooFitHS3: the channel '" + channel + "' of '" + simPdf->GetName() +
3181 "' consists of constraint terms, but has no data, and its observables are not all global "
3182 "observables of the ModelConfig: it is written without an observation.");
3183 continue;
3184 }
3185 }
3186 auto &marked = doc.rooFit.auxiliaryChannels[simPdf->GetName()];
3187 if (!HS3::findByName(marked, channel)) {
3188 marked.push_back({channel});
3189 }
3190 }
3191 for (std::string const &term : constraintTerms) {
3192 // A pure model has no data at all, so the observed values of its constraint terms are not written as data sets
3193 // either: they are the constant parameters of auxiliary distributions.
3194 // A component of a combined data set belongs to that data set alone, and is named after it like the others.
3195 const bool asChannel = indexColumn && _export->arrayComponents.count(term);
3196 const std::string constraintData = asChannel ? *dataName + "_" + term : term + "_data";
3197 if (!likelihood.pureModel && config().exportConstraintData &&
3198 exportConstraintData(term, mc.GetGlobalObservables(), constraintData)) {
3199 if (asChannel) {
3200 addAuxiliaryChannel(term);
3201 } else {
3202 likelihood.terms.push_back({{term}, HS3::DataRef{constraintData}});
3203 }
3204 } else {
3205 likelihood.auxDistributions.push_back({term});
3206 }
3207 }
3208 doc.likelihoods.push_back(std::move(likelihood));
3209
3210 if (RooArgSet const *pois = mc.GetParametersOfInterest()) {
3211 std::vector<std::string> names = namesOf(*pois);
3212 std::sort(names.begin(), names.end());
3213 for (auto const &name : names) {
3214 analysis.parametersOfInterest.push_back({name});
3215 }
3216 }
3217
3218 auto writeProductDomain = [&](const char *suffix, RooArgSet const *args) {
3219 if (!args || args->empty())
3220 return;
3221 HS3::Domain &domain = doc.domains.emplace_back();
3222 domain.name = analysisName + suffix;
3223 analysis.domains.push_back({domain.name});
3224 RooArgList sorted{*args};
3225 sorted.sort();
3226 for (auto *var : static_range_cast<const RooRealVar *>(sorted)) {
3227 domain.axes.emplace_back(toDomainAxis(var->GetName(), var->getBinning()));
3228 }
3229 };
3230
3231 writeProductDomain("_nuisance_parameters", mc.GetNuisanceParameters());
3232 writeProductDomain("_global_observables", mc.GetGlobalObservables());
3233 writeProductDomain("_parameters_of_interest", mc.GetParametersOfInterest());
3234
3235 // The snapshots are exported as parameter points. Reference the one of the ModelConfig as the initial values.
3236 if (_workspace.getSnapshot(mc.GetSnapshotName().c_str())) {
3238 }
3239 // The prior is a top-level pdf of the workspace or a component of one, so it is exported already.
3240 if (RooAbsPdf const *prior = mc.GetPriorPdf()) {
3241 analysis.prior = HS3::DistributionRef{prior->GetName()};
3242 }
3243 doc.analyses.push_back(std::move(analysis));
3244
3245 HS3::ModelConfigInfo &info = doc.rooFit.modelConfigs[analysisName];
3246 info.pdfName = pdf->GetName();
3247 info.mcName = mc.GetName();
3248 // Without a data set, the document doesn't say what the observables are.
3249 if (!dataName && mc.GetObservables() && !mc.GetObservables()->empty()) {
3250 std::vector<std::string> names = namesOf(*mc.GetObservables());
3251 std::sort(names.begin(), names.end());
3252 info.observables = std::move(names);
3253 }
3254}
3255
3256// Export all top-level pdfs, functions, datasets and ModelConfigs of the workspace into `n`.
3258{
3259 _export = std::make_unique<HS3::ToolExportState>();
3260 _export->root = &n;
3261 StateScope<HS3::ToolExportState> exportScope{_export};
3262
3263 HS3Document &doc = _export->doc;
3265 doc.metadata.packages.push_back({"ROOT", rootVersion()});
3266 doc.parameterPoints.push_back({"default_values", {}});
3268
3269 std::vector<RooAbsPdf *> const allpdfs = topLevelObjects<RooAbsPdf>(_workspace.allPdfs(), _workspace);
3270 // A Gaussian constraint is written with its global observable as "x", also if that is its mean in RooFit (see
3271 // setConstraintObservable()). Which one it is must be known before any pdf is written: the pdfs are written in the
3272 // order of their names, and a constraint can come before the model that uses it.
3273 for (TObject *obj : _workspace.allGenericObjects()) {
3274 auto const *mc = dynamic_cast<RooFit::ModelConfig const *>(obj);
3275 RooArgSet const *globs = mc ? mc->GetGlobalObservables() : nullptr;
3276 if (!globs) {
3277 continue;
3278 }
3279 for (RooAbsArg const *arg : _workspace.components()) {
3280 auto const *gauss = dynamic_cast<RooGaussian const *>(arg);
3281 if (gauss && globs->find(gauss->getMean()) && !globs->find(gauss->getX()) &&
3282 !constraintObservable(gauss->GetName())) {
3283 setConstraintObservable(*gauss, gauss->getMean().GetName());
3284 }
3285 }
3286 }
3287 exportObjects(allpdfs);
3288 exportObjects(topLevelObjects<RooAbsReal>(_workspace.allFunctions(), _workspace));
3289
3290 // The array distributions are written after the objects that use them, see exportAsArrayComponent().
3292
3293 for (RooAbsArg *arg : _workspace.components()) {
3294 exportAttributes(arg, doc.rooFit.attributes);
3295 }
3296
3297 std::vector<RooAbsData *> alldata;
3298 for (auto &d : _workspace.allData()) {
3299 alldata.push_back(d);
3300 }
3301 sortByName(alldata);
3302 // Data sets with category observables are combined data sets.
3303 std::vector<RooAbsData *> singleData;
3304 for (auto &d : alldata) {
3305 // Without a matching RooSimultaneous, the categories of a data set are ordinary observables, written as
3306 // categorical axes, unless its observables are renamed per key (Config::renameCombinedObservables).
3307 if (selectionColumns(*d->get()).empty() ||
3309 !findMatchingSimultaneous(_workspace, namesOf(selectionColumns(*d->get()))))) {
3310 singleData.push_back(d);
3311 this->exportData(*d);
3312 } else {
3313 this->exportCombinedData(*d);
3314 }
3315 }
3316
3317 for (TObject *obj : _workspace.allGenericObjects()) {
3318 if (auto mc = dynamic_cast<RooFit::ModelConfig *>(obj)) {
3319 exportModelConfig(*mc, singleData);
3320 }
3321 }
3322
3323 exportParameterStepWidths(_workspace, allpdfs, alldata, doc.rooFit.stepWidths);
3324
3325 for (auto *snsh : static_range_cast<RooArgSet const *>(_workspace.getSnapshots())) {
3326 RooArgSet snapshotSorted;
3327 // We only want to add the variables that actually got exported and skip
3328 // the ones that the pdfs encoded implicitly (like in the case of
3329 // HistFactory).
3330 for (RooAbsArg *arg : *snsh) {
3331 if (std::any_of(allpdfs.begin(), allpdfs.end(), [&](RooAbsPdf *pdf) { return pdf->dependsOn(*arg); })) {
3333 snapshotSorted.add(*arg);
3334 }
3335 }
3336 snapshotSorted.sort();
3337 std::string name(snsh->GetName());
3338 if (name != "default_values") {
3339 doc.parameterPoints.push_back({name, {}});
3340 for (RooAbsArg *arg : snapshotSorted) {
3341 exportVariable(arg, doc.parameterPoints.back());
3342 }
3343 }
3344 }
3345 // The constraint terms that are components of a combined distribution have a label of their own.
3346 for (auto const &item : _export->auxiliaryLabels) {
3347 auto &categories = _export->categories[item.first].categories;
3348 for (std::string const &label : item.second) {
3349 if (std::find(categories.begin(), categories.end(), label) == categories.end()) {
3350 categories.push_back(label);
3351 }
3352 }
3353 }
3354 // The categories are categorical axes of the "default_domain", which must not list them as continuous.
3355 auto &defaultAxes = _export->domains[HS3::defaultDomainName];
3356 for (auto const &item : _export->categories) {
3357 if (defaultAxes.count(item.first)) {
3358 RooJSONFactoryWSTool::error("the variable '" + item.first +
3359 "' is categorical, but the domain already lists it as continuous");
3360 }
3361 }
3362 // The domains of the analyses take precedence.
3363 for (auto &item : _export->domains) {
3364 if (!HS3::findByName(doc.domains, item.first)) {
3365 appendDomain(doc.domains, item.first, std::move(item.second),
3366 item.first == HS3::defaultDomainName ? std::move(_export->categories)
3367 : std::map<std::string, HS3::CategoricalAxis>{});
3368 }
3369 }
3370
3371 // The exporters have written the generic distributions to the JSON tree. Now that the tree has all of them, the
3372 // document refers to their nodes, like a document that was read. The combined distributions are written now, since
3373 // constraint terms can become their components until here, see exportSingleModelConfig().
3374 if (n.has_child("distributions")) {
3375 std::map<std::string, JSONNode *> nodes;
3376 for (JSONNode &node : n["distributions"].children()) {
3377 nodes.emplace(name(node), &node);
3378 }
3379 for (HS3::Distribution &dist : doc.distributions) {
3380 auto found = nodes.find(dist.name);
3381 if (found == nodes.end()) {
3382 continue;
3383 }
3384 if (auto *generic = std::get_if<HS3::GenericObject>(&dist.body)) {
3385 generic->node = found->second;
3386 } else if (auto const *combined = std::get_if<HS3::CombinedDist>(&dist.body)) {
3387 HS3::writeCombinedDist(*found->second, *combined);
3388 }
3389 }
3390 }
3391
3392 // A RooFit model often has a constraint both as a factor of the product of a channel and as a term of the
3393 // likelihood (e.g. a constraint of a HistFactory modifier, an external constraint of the ModelConfig, or any
3394 // constraint of a channel, see constraintsOfChannels()); RooFit counts it once. In HS3 it would enter twice.
3395 for (auto const &[product, factors] : HS3::writeLikelihoodTermsOnce(doc, n)) {
3396 for (std::string const &factor : factors) {
3397 oocoutI(nullptr, IO) << "RooFitHS3: '" << factor << "' is a factor of '" << product
3398 << "' and a term of its likelihood; it is written once, as a term of the likelihood"
3399 << std::endl;
3400 }
3401 }
3402 declareBookkeepingColumns(doc, _workspace);
3403
3404 // The document that RooFit writes has to pass the same checks as the ones that it reads. Until RooFit writes
3405 // documents that follow the current standard, problems are only reported.
3406 HS3::Diagnostics diagnostics;
3407 HS3::ValidateOptions validateOptions;
3408 validateOptions.isExternal = [](std::string const &) { return false; };
3410 HS3::checkDocument(doc, diagnostics, validateOptions);
3411 for (auto &diagnostic : diagnostics) {
3412 diagnostic.severity = HS3::Diagnostic::Severity::Warning;
3413 diagnostic.message = "the exported document is not valid: " + diagnostic.message;
3414 }
3415 HS3::report(diagnostics);
3416 HS3::writeDocument(n, doc);
3417
3418 // Without distinct variates (see HS3::sharedVariates()), the document is only written if explicitly allowed, e.g.
3419 // to rename the variates in the JSON by hand.
3420 if (std::vector<std::string> const shared = HS3::sharedVariates(doc); !shared.empty()) {
3421 if (!config().allowExportSharedVariates) {
3422 std::string text;
3423 for (std::string const &message : shared) {
3424 text += (text.empty() ? "" : "; ") + message;
3425 }
3426 error("cannot export: " + text +
3427 ". HS3 requires the terms of a likelihood and the components of a combined distribution to have "
3428 "distinct variates: give the variates their own names (e.g. 'mass_SR' and 'mass_CR' instead of "
3429 "'mass'; RooJSONFactoryWSTool::config().renameCombinedObservables does this for the observables of "
3430 "combined data sets), or set RooJSONFactoryWSTool::config().allowExportSharedVariates = true to "
3431 "write the document anyway, with a warning, and rename them in the JSON.");
3432 }
3433 for (std::string const &message : shared) {
3434 warning("RooFitHS3: " + message +
3435 ". HS3 requires distinct variates: the document is not valid HS3, and can't be read back before the "
3436 "variates are renamed.");
3437 }
3438 }
3439}
3440
3441/// Import an HS3 document that is given as a JSON string into the workspace. Returns true; errors are exceptions.
3443{
3444 std::stringstream ss(s);
3445 return importJSON(ss);
3446}
3447
3448/// Export the workspace as an HS3 document, and return it as a JSON string.
3450{
3451 std::stringstream ss;
3452 exportJSON(ss);
3453 return ss.str();
3454}
3455
3456/// Export the workspace as an HS3 document, and write it as JSON to the stream. Returns true; errors are exceptions.
3458{
3459 std::unique_ptr<JSONTree> tree = JSONTree::create();
3460 JSONNode &n = tree->rootnode().set_map();
3461 this->exportAllObjects(n);
3462 n.writeJSON(os);
3463 return true;
3464}
3465
3466/// Export the workspace as an HS3 document, and write it to a JSON file. Returns true; errors are exceptions.
3468{
3469 std::ofstream out(filename.c_str());
3470 if (!out.is_open())
3471 RooJSONFactoryWSTool::error("RooJSONFactoryWSTool() invalid output file '" + filename + "'.");
3472 return this->exportJSON(out);
3473}
3474
3475bool RooJSONFactoryWSTool::hasAttribute(const std::string &obj, const std::string &attrib)
3476{
3477 if (!_import)
3478 return false;
3479 auto const &attributes = _import->document.rooFit.attributes;
3480 auto found = attributes.find(obj);
3481 if (found == attributes.end())
3482 return false;
3483 auto const &tags = found->second.tags;
3484 return std::find(tags.begin(), tags.end(), attrib) != tags.end();
3485}
3486void RooJSONFactoryWSTool::setAttribute(const std::string &obj, const std::string &attrib)
3487{
3488 _export->doc.rooFit.attributes[obj].tags.push_back(attrib);
3489}
3490
3491std::string RooJSONFactoryWSTool::getStringAttribute(const std::string &obj, const std::string &attrib)
3492{
3493 if (!_import)
3494 return "";
3495 auto const &attributes = _import->document.rooFit.attributes;
3496 auto found = attributes.find(obj);
3497 if (found == attributes.end())
3498 return "";
3499 auto value = found->second.dict.find(attrib);
3500 return value != found->second.dict.end() ? value->second : "";
3501}
3502void RooJSONFactoryWSTool::setStringAttribute(const std::string &obj, const std::string &attrib,
3503 const std::string &value)
3504{
3505 _export->doc.rooFit.attributes[obj].dict[attrib] = value;
3506}
3507
3508// Import the JSON document rooted at `n` into the workspace.
3510{
3511 HS3::ParseOptions options;
3512 // A user can replace how HistFactory distributions are imported by registering their own importer.
3513 auto const &importers = RooFit::JSONIO::importers();
3514 auto histFactoryImporters = importers.find("histfactory_dist");
3515 options.typedHistFactory = histFactoryImporters != importers.end() && !histFactoryImporters->second.empty() &&
3516 HS3::isBuiltinHistFactoryImporter(*histFactoryImporters->second.front());
3517
3518 HS3::Diagnostics diagnostics;
3519 HS3::upgradeDocument(n, diagnostics, options);
3520 HS3Document parsed = HS3::parseDocument(n, diagnostics, options);
3521 std::optional<RooFit::ValidatedHS3Document> validated;
3522 if (!HS3::hasErrors(diagnostics)) {
3523 HS3::ValidateOptions validateOptions;
3524 validateOptions.strict = !(parsed.metadata.version < HS3::Version{0, 3});
3525 validateOptions.isExternal = [this](std::string const &name) { return _workspace.obj(name) != nullptr; };
3526 validated = HS3::validate(std::move(parsed), diagnostics, validateOptions);
3527 }
3528 HS3::report(diagnostics);
3529 // What HS3 requires of the variates and the constraints is checked on the document as it is, before the components
3530 // that are read as constraint terms are taken out of it.
3531 std::set<std::string> const bookkeepingColumns = checkVariates(validated->doc());
3532 HS3::AuxiliaryConstraints const auxiliary =
3533 HS3::readAuxiliaryChannelsAsConstraints(*validated, config().importConstraintsAsChannels);
3534 for (std::string const &message : auxiliary.warnings) {
3535 warning("RooFitHS3: " + message);
3536 }
3537 for (std::string const &message : HS3::resolvePointData(*validated)) {
3538 error(message);
3539 }
3540 HS3Document const &doc = validated->doc();
3541
3542 StateScope<HS3::ToolImportState> importScope{_import};
3543 _import = std::make_unique<HS3::ToolImportState>(doc);
3544
3545 createVariables(_workspace, _import->document.domains, _import->ranges);
3546
3547 if (_import->defaultValues) {
3548 for (auto const &p : _import->defaultValues->parameters) {
3550 }
3551 }
3552 for (auto const &function : doc.functions) {
3553 this->importFunction(*function.body.node, true);
3554 }
3555 for (auto const &distribution : doc.distributions) {
3556 this->importDistribution(distribution);
3557 }
3558
3559 // This creates the index categories, which the parameter points can refer to.
3560 combinePdfs(doc, _workspace);
3561
3562 for (auto const &point : doc.parameterPoints) {
3563 RooJSONFactoryWSTool::testValidName(point.name, true);
3564
3565 RooArgSet vars;
3566 for (auto const &var : point.parameters) {
3567 std::string const *label = var.value ? std::get_if<std::string>(&*var.value) : nullptr;
3568 if (RooCategory *cat = _workspace.cat(var.name)) {
3569 if (label) {
3570 cat->setLabel(label->c_str());
3571 } else if (var.value) {
3572 cat->setIndex(static_cast<int>(std::get<double>(*var.value)));
3573 }
3574 vars.add(*cat);
3575 continue;
3576 }
3577 if (label) {
3578 warning("RooFitHS3: ignoring the string value of parameter '" + var.name + "' in parameter point '" +
3579 point.name + "', because there is no category with that name.");
3580 continue;
3581 }
3582 RooRealVar *rrv = _workspace.var(var.name);
3583 if (!rrv && !_workspace.arg(var.name)) {
3584 // The parameter is not used by any other component: create it. "const": false is the same as no
3585 // "const" at all.
3586 testValidName(var.name, true);
3587 rrv = &wsEmplace<RooRealVar>(var.name, 1.);
3588 configureVariable(_import->ranges, var, *rrv);
3590 } else if (rrv) {
3591 configureVariable(_import->ranges, var, *rrv);
3592 }
3593 if (rrv) {
3594 vars.add(*rrv);
3595 }
3596 }
3597 _workspace.saveSnapshot(point.name, vars);
3598 }
3599
3600 for (auto const &item : doc.rooFit.attributes) {
3601 if (RooAbsArg *arg = _workspace.arg(item.first))
3602 importAttributes(arg, item.second);
3603 }
3604
3605 // The components of combined data sets are moved out of the "datasets" vector when the combined data set is
3606 // assembled, so they don't get imported on their own. The ROOT-internal form of older files (combineDatasets())
3607 // is handled after the analyses, because their likelihoods refer to the components.
3608 std::vector<std::unique_ptr<RooAbsData>> datasets;
3609 datasets.reserve(doc.data.size());
3610 for (auto const &data : doc.data) {
3611 if (!std::holds_alternative<HS3::CombinedData>(data.body)) {
3612 datasets.push_back(loadData(data, _workspace, &_import->ranges));
3613 }
3614 }
3615 assembleCombinedData(doc, _workspace, datasets);
3616
3617 for (auto const &analysis : doc.analyses) {
3618 importAnalysis(doc, analysis, _workspace, datasets, bookkeepingColumns, auxiliary.terms);
3619 }
3620
3621 combineDatasets(doc, datasets);
3622 // The data sets of the components that are read as constraint terms are not data sets of the workspace: their
3623 // values are in the global observables. Several analyses can share a ModelConfig, of which only the first one
3624 // consumes the data of its constraint terms.
3625 for (auto &d : datasets) {
3626 if (d && auxiliary.data.count(d->GetName())) {
3627 d.reset();
3628 }
3629 }
3630
3631 for (auto const &d : datasets) {
3632 if (d) {
3634 for (auto const &obs : *d->get()) {
3635 if (auto *rrv = dynamic_cast<RooRealVar *>(obs)) {
3636 _workspace.var(rrv->GetName())->setBinning(rrv->getBinning());
3637 }
3638 }
3639 }
3640 }
3641
3642 warnAboutUnusedArrayComponents(_workspace);
3643
3644 if (_workspace.getSnapshot("default_values")) {
3645 _workspace.loadSnapshot("default_values");
3646 }
3647 importParameterStepWidths(_workspace, doc);
3648}
3649
3650/// Import an HS3 document that is read as JSON from the stream into the workspace. Returns true; errors are exceptions.
3652{
3653 std::unique_ptr<JSONTree> tree = JSONTree::create(is);
3654 this->importAllNodes(tree->rootnode());
3655 return true;
3656}
3657
3658/// Import the HS3 document of a JSON file into the workspace. Returns true; errors are exceptions.
3660{
3661 std::ifstream infile(filename.c_str());
3662 if (!infile.is_open())
3663 RooJSONFactoryWSTool::error("RooJSONFactoryWSTool() invalid input file '" + filename + "'.");
3664 return this->importJSON(infile);
3665}
3666
3667void RooJSONFactoryWSTool::importJSONElement(const std::string &name, const std::string &jsonString)
3668{
3669 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(jsonString);
3670 JSONNode &n = tree->rootnode();
3671
3672 // If the objects containts a parameter of interest, import it as a modelConfig
3673 if (n.find("poi")) {
3674
3675 RooStats::ModelConfig modelConfig{"ModelConfig"};
3676 std::string poi = n.find("poi")->val();
3677 std::string pdname = n.find("pdfName")->val();
3678 modelConfig.SetWS(_workspace);
3679 modelConfig.SetPdf(pdname.c_str());
3680 modelConfig.SetParametersOfInterest(_workspace.argSet(poi));
3681 _workspace.import(modelConfig);
3682
3683 return;
3684 }
3685
3686 n["name"] << name;
3687
3688 // Without a type, it must be a variable.
3689 JSONNode const *type = n.find("type");
3690 if (!type) {
3691 this->importVariableElement(n);
3692 } else if (type->val() == "binned" || type->val() == "unbinned") {
3693 HS3::Diagnostics diagnostics;
3694 HS3::Data data = HS3::parseData(n, diagnostics);
3695 HS3::report(diagnostics);
3696 _workspace.import(*loadData(data, _workspace, nullptr), RooFit::Silence());
3697 } else {
3698 this->importFunction(n, false);
3699 }
3700}
3701
3703{
3704 // A variable is given by its "value", by its range "min" and "max", or by both. With only a value, it is constant.
3705 JSONNode const *value = elementNode.find("value");
3706 JSONNode const *min = elementNode.find("min");
3707 JSONNode const *max = elementNode.find("max");
3708 if (!value && !(min && max)) {
3709 throw std::invalid_argument("Invalid Syntax: Please provide either 'value' or 'min' and 'max' or both");
3710 }
3711 HS3::DomainAxis axis;
3712 axis.name = elementNode["name"].val();
3713 HS3::ParameterValue parameter;
3714 parameter.name = axis.name;
3715 if (min && max) {
3716 axis.min = min->val_double();
3717 axis.max = max->val_double();
3718 }
3719 parameter.value = value ? value->val_double() : (*axis.max + *axis.min) / 2;
3720 parameter.isConst = value && !(min && max);
3721 HS3Document doc;
3722 doc.domains.push_back({HS3::defaultDomainName, {std::move(axis)}});
3723 doc.parameterPoints.push_back({"default_values", {parameter}});
3724
3725 StateScope<HS3::ToolImportState> importScope{_import};
3726 _import = std::make_unique<HS3::ToolImportState>(doc);
3727
3728 importVariable(parameter);
3729 if (RooRealVar *rrv = _workspace.var(parameter.name)) {
3730 configureVariable(_import->ranges, parameter, *rrv);
3731 }
3732}
3733
3734/// Write a warning to the RooFit message service, and return its stream.
3735std::ostream &RooJSONFactoryWSTool::warning(std::string const &str)
3736{
3737 return RooMsgService::instance().log(nullptr, RooFit::MsgLevel::WARNING, RooFit::ObjectHandling) << str << std::endl;
3738}
3739
3740/// Write an error to the RooFit message service, and throw it as a std::runtime_error.
3742{
3743 RooMsgService::instance().log(nullptr, RooFit::MsgLevel::ERROR, RooFit::IO) << s << std::endl;
3744 throw std::runtime_error(s);
3745}
3746
3747/// Replace some characters that are common in the names of RooFit objects, but that the HS3 standard doesn't allow in
3748/// names, if config().allowSanitizeNames is set.
3749std::string RooJSONFactoryWSTool::sanitizeName(const std::string str)
3750{
3751 std::string result;
3752 if (RooJSONFactoryWSTool::config().allowSanitizeNames) {
3753 for (char c : str) {
3754 switch (c) {
3755 case '[':
3756 case '|':
3757 case ',':
3758 case '(': result += '_'; break;
3759 case ']':
3760 case ')':
3761 // skip these characters entirely
3762 break;
3763 case '.': result += "_dot_"; break;
3764 case '@': result += "at"; break;
3765 case '-': result += "minus"; break;
3766 case '/': result += "_div_"; break;
3767
3768 default: result += c; break;
3769 }
3770 }
3771 return result;
3772 }
3773 return str;
3774}
3775
3777{
3778 RooWorkspace tmpWS = RooWorkspace();
3779 if (onlyModelConfig) {
3780 for (auto *obj : ws.allGenericObjects()) {
3781 if (auto *mc = dynamic_cast<RooFit::ModelConfig *>(obj)) {
3782 tmpWS.import(*mc->GetPdf(), RooFit::RecycleConflictNodes(true));
3783 }
3784 }
3785
3786 } else {
3787
3788 for (auto *pdf : ws.allPdfs()) {
3789 if (isTopLevel(*pdf, ws)) {
3790 tmpWS.import(*pdf, RooFit::RecycleConflictNodes(true));
3791 }
3792 }
3793
3794 for (auto *func : ws.allFunctions()) {
3795 if (isTopLevel(*func, ws)) {
3796 tmpWS.import(*func, RooFit::RecycleConflictNodes(true));
3797 }
3798 }
3799 }
3800
3801 for (auto *data : ws.allData()) {
3802 tmpWS.import(*data);
3803 }
3804
3805 for (auto *obj : ws.allGenericObjects()) {
3806 tmpWS.import(*obj);
3807 }
3808
3809 for (auto *obj : ws.allResolutionModels()) {
3810 tmpWS.import(*obj);
3811 }
3812
3813 for (auto *snsh : ws.getSnapshots()) {
3814 auto *snshSet = dynamic_cast<RooArgSet *>(snsh);
3815 if (snshSet) {
3816 tmpWS.saveSnapshot(snshSet->GetName(), *snshSet, true);
3817 }
3818 }
3819
3820 return tmpWS;
3821}
3822
3824{
3825 static Config conf;
3826 return conf;
3827}
3828
3829// Sanitize all names in the workspace to be HS3 compliant
3831{
3832 RooWorkspace tmpWS = cleanWS(ws, false);
3833
3834 // Valid names stay as they are.
3835 auto sanitize = [](auto const &objects) {
3836 for (auto *obj : objects) {
3837 obj->SetName(sanitizeName(obj->GetName()).c_str());
3838 }
3839 };
3840 sanitize(tmpWS.allVars());
3841 sanitize(tmpWS.allFunctions());
3842 sanitize(tmpWS.allPdfs());
3843 sanitize(tmpWS.allResolutionModels());
3844 for (auto const &datasets : {tmpWS.allData(), tmpWS.allEmbeddedData()}) {
3845 sanitize(datasets);
3846 for (RooAbsData *data : datasets) {
3847 sanitize(*data->get());
3848 }
3849 }
3850 for (auto *snshObj : tmpWS.getSnapshots()) {
3851 // Snapshots are stored as TObject*, but really they are RooArgSet*
3852 auto *snsh = dynamic_cast<RooArgSet *>(snshObj);
3853 if (!snsh) {
3854 std::cerr << "Warning: found snapshot that is not a RooArgSet, skipping\n";
3855 continue;
3856 }
3857 snsh->setName(sanitizeName(snsh->GetName()).c_str());
3858 sanitize(*snsh);
3859 }
3860 // Generic objects, like the ModelConfigs with their sets
3861 for (auto *obj : tmpWS.allGenericObjects()) {
3862 if (auto *named = dynamic_cast<TNamed *>(obj)) {
3863 named->SetName(sanitizeName(named->GetName()).c_str());
3864 } else if (!isValidName(obj->GetName())) {
3865 std::cerr << "Warning: object " << obj->GetName() << " is not TNamed, cannot rename.\n";
3866 }
3867 if (auto *mc = dynamic_cast<RooFit::ModelConfig *>(obj)) {
3868 sanitize(*mc->GetObservables());
3869 sanitize(*mc->GetParametersOfInterest());
3870 sanitize(*mc->GetNuisanceParameters());
3871 sanitize(*mc->GetGlobalObservables());
3872 }
3873 }
3874 RooWorkspace newWS = cleanWS(tmpWS, false);
3875 newWS.SetName((std::string{ws.GetName()} + "_sanitized").c_str());
3876 return newWS;
3877}
void writeArrayDist(RooJSONFactoryWSTool &tool, JSONNode &elem, std::string const &name, std::vector< RooAbsPdf const * > const &components)
std::optional< ArrayComponentInfo > arrayComponentInfo(RooAbsPdf const &pdf, std::string const *observable, RooJSONFactoryWSTool *tool)
std::optional< double > toNumber(const std::string &str)
std::vector< std::vector< std::string > > selectionKeys(std::vector< RooCategory const * > const &columns)
void writeReference(JSONNode &node, RooAbsArg const &arg)
bool isLiteralIdentifier(std::string_view name)
std::string transformedBaseName(RooAbsReal const &param)
std::vector< RooCategory const * > selectionColumns(RooAbsCategory const &indexCat)
std::string transformedArgument(RooAbsReal const &param)
std::string selectionKeyToString(std::vector< std::string > const &key)
bool startsWith(std::string_view str, std::string_view prefix)
bool endsWith(std::string_view str, std::string_view suffix)
std::string selectionLabel(std::vector< std::string > const &key)
bool isLiteralConstVar(RooAbsArg const &arg)
constexpr const char * helperObjectTag
Definition JSONIOUtils.h:45
constexpr const char * arrayDistTag
Definition JSONIOUtils.h:34
std::string message
#define d(i)
Definition RSha256.hxx:102
#define c(i)
Definition RSha256.hxx:101
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
bool isSkippedHelper(RooAbsArg const &arg, RooWorkspace const &ws)
constexpr auto hs3VersionTag
#define oocoutW(o, a)
#define oocoutI(o, a)
static unsigned int total
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 input
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 filename
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 Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
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
Option_t Option_t TPoint TPoint const char text
char name[80]
Definition TGX11.cxx:142
#define gROOT
Definition TROOT.h:417
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
TClass * IsA() const override
Definition RooAbsArg.h:674
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
bool isConstant() const
Check if the "Constant" attribute is set.
Definition RooAbsArg.h:284
virtual TObject * clone(const char *newname=nullptr) const =0
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
RooFit::OwningPtr< RooArgSet > getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
const RefCountList_t & servers() const
List of all servers of this object.
Definition RooAbsArg.h:146
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
const RefCountList_t & clients() const
List of all clients of this object.
Definition RooAbsArg.h:138
void setAttribute(const Text_t *name, bool value=true)
Set (default) or clear a named boolean attribute of this object.
void leafNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool recurseNonDerived=false) const
Fill supplied list with all leaf nodes of the arg tree, starting with ourself as top node.
Abstract base class for RooRealVar binning definitions.
Int_t numBins() const
Return number of bins.
virtual double binLow(Int_t bin) const =0
virtual bool isUniform() const
virtual double highBound() const =0
virtual double lowBound() const =0
virtual double binHigh(Int_t bin) const =0
Abstract base class for objects that represent a discrete value that can be set from the outside,...
A space to attach TBranches.
unsigned int getCurrentOrdinalNumber() const
Return ordinal number of the current state.
bool hasLabel(const std::string &label) const
Check if a state with name label exists.
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
std::size_t size() const
Number of states defined.
const RooCatType * defineType(const char *label)
Abstract container object that can hold multiple RooAbsArg objects.
bool equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
const_iterator end() const
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
Storage_t::size_type size() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
const_iterator begin() const
void sort(bool reverse=false)
Sort collection using std::sort and name comparison.
void setName(const char *name)
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
Abstract base class for objects that are lvalues, i.e.
virtual Int_t numBins(const char *rangeName=nullptr) const =0
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
std::unique_ptr< RooArgSet > getAllConstraints(const RooArgSet &observables, RooArgSet &constrainedParams, bool stripDisconnected=true) const
This helper function finds and collects all constraints terms of all component p.d....
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:214
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual Int_t getBins(const char *name=nullptr) const
Get number of bins of currently defined range.
void setConstant(bool value=true)
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:159
bool containsInstance(const RooAbsArg &var) const override
Check if this exact instance is in this collection.
Definition RooArgSet.h:132
Implements a RooAbsBinning in terms of an array of boundary values, posing no constraints on the choi...
Definition RooBinning.h:27
Object to represent discrete states.
Definition RooCategory.h:28
bool setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
bool defineType(const std::string &label)
Define a state with given name.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
const char * getString(Int_t idx) const
Return string stored in slot idx.
Definition RooCmdArg.h:96
Represents a constant real-valued object.
Definition RooConstVar.h:23
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
Container class to hold unbinned data.
Definition RooDataSet.h:32
std::string val() const
JSONNode const * find(std::string const &key) const
The component at the end of a path of keys, or nullptr if a component on the path doesn't exist.
bool has_child(std::string const &) const
std::string key() const
Records which components of a JSON map are read while it is alive, through any node of the tree.
static std::unique_ptr< JSONTree > create()
An HS3 document. It is move-only, because generic objects refer to the JSON tree of the document.
std::vector< HS3::Analysis > analyses
std::vector< HS3::Data > data
HS3::Metadata metadata
std::vector< HS3::Likelihood > likelihoods
std::vector< HS3::Function > functions
std::vector< HS3::ParameterPoint > parameterPoints
HS3::RooFitInternal rooFit
std::vector< HS3::Domain > domains
std::vector< HS3::Distribution > distributions
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.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
bool isConstraintReadAsChannel(RooAbsPdf const &pdf) const
Whether the constraint term pdf contributes to the likelihoods of the document that is currently bein...
std::string getStringAttribute(const std::string &obj, const std::string &attrib)
void exportModelConfig(RooStats::ModelConfig const &mc, const std::vector< RooAbsData * > &singleDataSets)
static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1)
bool exportConstraintData(std::string const &term, RooArgSet const *globalObservables, std::string const &dataName)
RooJSONFactoryWSTool(RooWorkspace &ws)
void recordConstraintTerms(std::string const &pdfName, std::vector< std::string > const &terms)
T * request(const std::string &objname, const std::string &requestAuthor)
The object objname from the workspace, imported from the document first if needed.
void exportCombinedData(RooAbsData const &data)
Export a data set with category observables as a "combined" data set.
void exportSimultaneous(RooSimultaneous const &simPdf)
Export a RooSimultaneous as a "combined_dist".
void exportData(RooAbsData const &data)
bool hasAttribute(const std::string &obj, const std::string &attrib)
bool importJSON(std::string const &filename)
Import the HS3 document of a JSON file into the workspace. Returns true; errors are exceptions.
std::string const & exportRootName() const
The name of the top-level object whose export is in progress (the root of the current export recursio...
T * tryRequest(const std::string &objname)
Like request(), but returns nullptr if neither the workspace nor the document has the object.
std::map< std::string, std::map< std::string, std::map< std::string, std::string > > > _combinedObservableRenames
Obj_t & wsImport(Obj_t const &obj)
std::vector< std::string > _importStack
static std::unique_ptr< RooDataHist > readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars)
Create a RooDataHist with the name name over the variables vars from the "contents" of n,...
static RooFit::Detail::JSONNode & appendNamedChild(RooFit::Detail::JSONNode &node, std::string const &name)
void importDistribution(RooFit::HS3::Distribution const &distribution)
static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Write n numbers as an array, each as an integer if it has no fractional part.
void recordCategory(RooAbsCategory const &cat)
void exportObjects(T const &args)
static void exportHisto(RooArgSet const &vars, std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Write the axes and the n bin contents of a histogram over the variables vars, which are RooRealVars o...
std::vector< RooAbsArg const * > _serversToDelete
std::map< std::string, std::map< std::string, RooAbsPdf const * > > _renamedComponents
std::vector< RooFit::HS3::DistributionRef > const * channelConstraints(std::string const &dist) const
The constraint terms that the document that is currently being imported adds to the HistFactory distr...
std::unique_ptr< RooFit::HS3::ToolExportState > _export
std::string exportJSONtoString()
Export the workspace as an HS3 document, and return it as a JSON string.
static RooWorkspace cleanWS(const RooWorkspace &ws, bool onlyModelConfig=false)
std::string exportTransformed(const RooAbsReal *original, const std::string &suffix, const std::string &formula)
static bool isValidName(const std::string &str)
Whether a string is a valid name: a letter or an underscore, followed by letters, digits or underscor...
void importVariable(RooFit::HS3::ParameterValue const &p)
RooFit::HS3::Version hs3Version() const
The HS3 version of the document that is currently being imported.
std::vector< std::unique_ptr< RooAbsArg > > _renamedObjects
void importDependants(const RooFit::Detail::JSONNode &n)
void importJSONElement(const std::string &name, const std::string &jsonString)
static RooWorkspace sanitizeWS(const RooWorkspace &ws)
static void error(const char *s)
Write an error to the RooFit message service, and throw it as a std::runtime_error.
void setConstraintObservable(RooAbsPdf const &pdf, std::string const &observable)
Record that observable is the observable of the constraint term pdf (the side that is not the constra...
void setAttribute(const std::string &obj, const std::string &attrib)
void exportObject(RooAbsArg const &func)
void exportSingleModelConfig(RooStats::ModelConfig const &mc, std::string const &analysisName, std::string const *dataName)
void importFunction(const RooFit::Detail::JSONNode &p, bool importAllDependants)
Import the function or distribution p into the workspace, with the importers that are registered for ...
void importVariableElement(const RooFit::Detail::JSONNode &elementNode)
bool importJSONfromString(const std::string &s)
Import an HS3 document that is given as a JSON string into the workspace. Returns true; errors are ex...
std::unique_ptr< RooFit::HS3::ToolImportState > _import
static bool testValidName(const std::string &name, bool forceError)
void exportVariable(const RooAbsArg *v, RooFit::HS3::ParameterPoint &point)
Obj_t & wsEmplace(RooStringView name, Args_t &&...args)
static std::string sanitizeName(const std::string str)
Replace some characters that are common in the names of RooFit objects, but that the HS3 standard doe...
static std::string name(const RooFit::Detail::JSONNode &n)
void exportAllObjects(RooFit::Detail::JSONNode &n)
bool exportJSON(std::string const &fileName)
Export the workspace as an HS3 document, and write it to a JSON file. Returns true; errors are except...
static RooFit::Detail::JSONNode const * findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name)
void importAllNodes(RooFit::Detail::JSONNode &n)
std::string exportAsArrayComponent(RooAbsPdf const &pdf, std::string const &arrayName)
void warnIfFreeWithoutDomain(RooRealVar const &var)
std::string const * constraintObservable(std::string const &pdfName) const
void setStringAttribute(const std::string &obj, const std::string &attrib, const std::string &value)
std::vector< RooAbsArg const * > _serversToExport
static std::ostream & warning(const std::string &s)
Write a warning to the RooFit message service, and return its stream.
static RooArgSet readAxes(const RooFit::Detail::JSONNode &node)
Create the variables for the "axes" of topNode, which are owned by the returned set.
static RooMsgService & instance()
Return reference to singleton instance.
std::ostream & log(const RooAbsArg *self, RooFit::MsgLevel level, RooFit::MsgTopic topic, bool forceSkipPrefix=false)
Log error message associated with RooAbsArg object self at given level and topic.
static constexpr double infinity()
Return internal infinity representation.
Definition RooNumber.h:25
static constexpr int isInfinite(double x)
Return true if x is infinite by RooNumber internal specification.
Definition RooNumber.h:27
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:35
Binning/range definition that only defines a range but no binning.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void removeMin(const char *name=nullptr)
Remove lower range limit for binning with given name. Empty name means default range.
void setVal(double value) override
Set value of variable to 'value'.
void setError(double value)
Definition RooRealVar.h:61
void setMin(const char *name, double value, bool shared=true)
Set minimum of name range to given value.
double getError() const
Definition RooRealVar.h:59
void setRange(const char *name, double min, double max, bool shared=true)
Set a fit or plotting range.
void setBins(Int_t nBins, const char *name=nullptr, bool shared=true)
Create a uniform binning under name 'name' for this variable.
bool hasError(bool allowZero=true) const
Definition RooRealVar.h:60
void setBinning(const RooAbsBinning &binning, const char *name=nullptr, bool shared=true)
Add given binning under name 'name' with this variable.
void setMax(const char *name, double value, bool shared=true)
Set maximum of name range to given value.
void removeMax(const char *name=nullptr)
Remove upper range limit for binning with given name. Empty name means default range.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false, bool shared=true) const override
Return binning definition with name.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsPdf * getPdf(RooStringView catName) const
Return the p.d.f associated with the given index category name.
const RooAbsCategoryLValue & indexCat() const
< A class that holds configuration information for a model using a workspace as a store
Definition ModelConfig.h:34
const std::string & GetSnapshotName() const
get the name of the workspace snapshot saved by SetSnapshot() (empty if there is none)
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
void SetWS(RooWorkspace &ws) override
Set a workspace that owns all the necessary components for the analysis.
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
const RooArgSet * GetExternalConstraints() const
get RooArgSet for global observables (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return nullptr if not existing)
Joins several RooAbsCategoryLValue objects into a single category.
Persistable container for RooFit projects.
TObject * obj(RooStringView name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
const RooArgSet * getSnapshot(const char *name) const
Return the RooArgSet containing a snapshot of variables contained in the workspace.
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooCategory * cat(RooStringView name) const
Retrieve discrete variable (RooCategory) with given name. A null pointer is returned if not found.
RooArgSet allVars() const
Return set with all variable objects.
RooArgSet argSet(RooStringView nameList) const
Return set of RooAbsArgs matching to given list of names.
RooArgSet allResolutionModels() const
Return set with all resolution model objects.
bool saveSnapshot(RooStringView, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of given parameters.
RooArgSet allPdfs() const
Return set with all probability density function objects.
std::list< RooAbsData * > allData() const
Return list of all dataset in the workspace.
RooLinkedList const & getSnapshots() const
std::list< TObject * > allGenericObjects() const
Return list of all generic objects in the workspace.
RooAbsReal * function(RooStringView name) const
Retrieve function (RooAbsReal) with given name. Note that all RooAbsPdfs are also RooAbsReals....
RooAbsArg * arg(RooStringView name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
const RooArgSet & components() const
RooArgSet allFunctions() const
Return set with all function objects.
RooRealVar * var(RooStringView name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
RooAbsData * data(RooStringView name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
std::list< RooAbsData * > allEmbeddedData() const
Return list of all dataset in the workspace.
bool loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name.
bool import(const RooAbsArg &arg, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
Import a RooAbsArg object, e.g.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:548
const char * Data() const
Definition TString.h:385
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
RooCmdArg RecycleConflictNodes(bool flag=true)
RooConstVar & RooConst(double val)
RooCmdArg Silence(bool flag=true)
RooCmdArg Index(RooCategory &icat)
RooCmdArg StoreError(const RooArgSet &aset)
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
RooCmdArg Import(const char *state, TH1 &histo)
const Double_t sigma
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
double product(DoubleArray factors, std::size_t nFactors)
Definition MathFuncs.h:102
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::variant< std::string, double > NameOrNumber
A name, or a number for a constant.
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 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
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::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
void buildCombinedDist(RooJSONFactoryWSTool &tool, std::string const &name, CombinedDist const &dist, std::vector< std::string > const &otherLikelihoodTerms={})
Create the RooSimultaneous of a combined distribution in the workspace of the tool.
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...
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 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,...
std::set< std::string > componentsWithData(HS3Document const &doc)
The distributions that are components of a combined distribution of a likelihood, with a component of...
void setExportedObject(RooJSONFactoryWSTool &tool, HistFactoryDist dist, std::vector< DistributionRef > channelConstraints)
Called by the exporter of a HistFactory distribution, which is then written by the tool.
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 report(Diagnostics const &diagnostics, std::string const &object="")
Forward the warnings to the message service, and throw if there are errors.
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.
RooArgSet createObservables(std::vector< BinnedAxis > const &axes)
Create the variables for the axes of binned data.
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 writeHistFactoryDist(Detail::JSONNode &node, HistFactoryDist const &dist)
Writes a HistFactory distribution, without its name.
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,...
std::vector< Diagnostic > Diagnostics
NameOrNumber toNameOrNumber(RooAbsArg const &arg)
The name of an object, or the value of a literal constant.
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...
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,...
BinnedAxis toBinnedAxis(RooRealVar const &var)
The axis of binned data for a variable, or for a binning of a variable with the given name.
void writeBinnedData(Detail::JSONNode &node, BinnedData const &data)
Writes the axes, contents and uncertainties of binned data, without its name and type.
std::unique_ptr< RooDataHist > createDataHist(std::string const &name, RooArgSet const &vars, std::vector< double > const &contents, std::vector< double > const *errors)
Create a RooDataHist with the contents and errors. If there are no errors, it doesn't store squared w...
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.
bool isBuiltinHistFactoryImporter(JSONIO::Importer const &importer)
Whether this is the importer that RooFit registers for HistFactory distributions, as opposed to the o...
std::vector< BinnedAxis > parseBinnedAxes(Detail::JSONNode const &axes, Diagnostics &diagnostics)
Reads the axes of binned data that doesn't come with a document.
void buildHistFactoryDist(RooJSONFactoryWSTool &tool, std::string const &name, HistFactoryDist const &dist)
Create the RooFit objects of a HistFactory distribution in the workspace of the tool.
HS3Document parseDocument(Detail::JSONNode const &root, Diagnostics &diagnostics, ParseOptions const &options={})
Reads a document.
ImportMap & importers()
Definition JSONIO.cxx:21
ExportMap & exporters()
Definition JSONIO.cxx:44
@ ObjectHandling
void initialize(typename Architecture_t::Matrix_t &A, EInitialization m)
Definition Functions.h:282
static const char * what
Definition stlLoader.cc:5
std::optional< DistributionRef > prior
std::optional< ParameterPointRef > init
LikelihoodRef likelihood
std::vector< DomainRef > domains
std::vector< ParameterRef > parametersOfInterest
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::string > categories
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 axis of point data or unbinned data, which can't have a binning.
std::optional< double > max
std::optional< double > min
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
A distribution or function that is not interpreted by the document, but by the importers that are reg...
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::string hs3Version
As it was written in the document, empty for fragments of a document.
Version version
The parsed version. Versions that can't be parsed count as the oldest one.
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
std::vector< ParameterValue > parameters
std::optional< std::variant< double, std::string > > value
A string selects the state of a category.
Options for upgradeDocument() and parseDocument().
bool typedHistFactory
If false, HistFactory distributions stay generic objects, because a user registered their own importe...
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
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::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, ModelConfigInfo > modelConfigs
The document that the tool is exporting.
std::map< std::string, std::string > constraintObservables
The observable of a constraint term, by the name of the term, see setConstraintObservable().
std::string rootName
The name of the top-level object whose export is in progress, and the depth of the export recursion.
std::optional< HistFactoryDist > typedObject
Set by the exporter of an object that is interpreted by the document.
std::map< std::string, std::vector< std::string > > constraintTermsOfPdf
The constraint terms of the exported HistFactory channels, as written, and the data sets written for ...
std::set< std::string > writtenConstraintData
std::map< std::string, HS3::CategoricalAxis > categories
The categories that are declared by categorical axes of the "default_domain", by name.
std::map< std::string, std::map< std::string, HS3::DomainAxis > > domains
The ranges and binnings of the exported variables, by domain and variable name.
std::vector< DistributionRef > channelConstraints
std::set< std::string > exportedObjectNames
Names of the objects written so far, to export each object once.
std::map< std::string, std::vector< RooAbsPdf const * > > arrayComponents
Array distributions to write at the end of the export: their components, and the array of each compon...
std::map< std::string, std::string > arrayOfComponent
std::map< std::string, std::vector< std::string > > auxiliaryLabels
The labels that the constraint terms that are written as components of a combined distribution add to...
What the tool needs to know about the document that it is importing.
ToolImportState(HS3Document const &doc)
std::unordered_map< std::string, HS3::Function const * > functions
std::set< std::string > componentsWithData
The components of combined distributions that have data, and are read as such.
HS3::ParameterPoint const * defaultValues
std::unordered_map< std::string, HS3::ParameterValue const * > defaultValuesByName
std::unordered_map< std::string, HS3::Distribution const * > distributions
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....
bool renameCombinedObservables
HS3 requires the components of a combined distribution to have distinct variates, e....
TLine l
Definition textangle.C:4