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