Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONFactories_HistFactory.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
14#include <RooFitHS3/JSONIO.h>
16
21#include <RooConstVar.h>
22#include <RooRealVar.h>
23#include <RooDataHist.h>
24#include <RooHistFunc.h>
25#include <RooRealSumPdf.h>
26#include <RooBinWidthFunction.h>
27#include <RooProdPdf.h>
28#include <RooPoisson.h>
29#include <RooFormulaVar.h>
30#include <RooLognormal.h>
31#include <RooGaussian.h>
32#include <RooProduct.h>
33#include <RooWorkspace.h>
34#include <RooFitImplHelpers.h>
35#include <RooFit/ModelConfig.h>
36
37#include <algorithm>
38#include <cctype>
39#include <charconv>
40#include <cmath>
41#include <iterator>
42#include <limits>
43#include <map>
44#include <optional>
45#include <regex>
46#include <set>
47#include <sstream>
48#include <variant>
49
50#include "static_execute.h"
51#include "HS3Builder.h"
52#include "JSONIOUtils.h"
53
60
61namespace HS3 = RooFit::HS3;
62
63using namespace RooStats::HistFactory;
64using namespace RooStats::HistFactory::Detail;
66
67namespace {
68
69double round_prec(double d, int nSig)
70{
71 if (d == 0.0)
72 return 0.0;
73 int ndigits = std::floor(std::log10(std::abs(d))) + 1 - nSig;
74 double sf = std::pow(10, ndigits);
75 if (std::abs(d / sf) < 2)
76 ndigits--;
77 return sf * std::round(d / sf);
78}
79
80std::string interpolationString(const Interpolation &interpolation)
81{
82 std::stringstream ss;
83 ss << R"({"type":")" << toString(interpolation.type) << R"(","in":")" << toString(interpolation.in) << R"(","out":)";
84 if (interpolation.out) {
85 ss << '"' << toString(*interpolation.out) << '"';
86 } else {
87 ss << "null";
88 }
89 ss << '}';
90 return ss.str();
91}
92
93const char *interpolationClassName(InterpolationClass interpolationClass)
94{
95 return interpolationClass == InterpolationClass::Piecewise ? "PiecewiseInterpolation" : "FlexibleInterpVar";
96}
97
98Interpolation interpolationFromCode(int code, InterpolationClass interpolationClass, const std::string &context)
99{
100 if (auto interpolation = HS3::interpolationFromCode(code, interpolationClass)) {
101 return *interpolation;
102 }
103 RooJSONFactoryWSTool::error(context + " has unsupported " + interpolationClassName(interpolationClass) + " code " +
104 std::to_string(code));
105}
106
107int codeFromInterpolation(const Interpolation &interpolation, InterpolationClass interpolationClass,
108 const std::string &context)
109{
110 if (auto code = HS3::codeFromInterpolation(interpolation, interpolationClass)) {
111 return *code;
112 }
113 RooJSONFactoryWSTool::error(context + " " + interpolationString(interpolation) + " cannot be represented by " +
114 interpolationClassName(interpolationClass));
115}
116
117// The interpolations of the parameters of a PiecewiseInterpolation or FlexibleInterpVar.
118std::vector<Interpolation>
119toInterpolations(std::vector<int> const &codes, InterpolationClass interpolationClass, std::string const &context)
120{
121 std::vector<Interpolation> out;
122 out.reserve(codes.size());
123 for (std::size_t i = 0; i < codes.size(); ++i) {
124 out.push_back(
125 interpolationFromCode(codes[i], interpolationClass, context + " at parameter index " + std::to_string(i)));
126 }
127 return out;
128}
129
130std::vector<int> toCodes(std::vector<Interpolation> const &interpolations, InterpolationClass interpolationClass,
131 std::string const &context)
132{
133 std::vector<int> out;
134 out.reserve(interpolations.size());
135 for (std::size_t i = 0; i < interpolations.size(); ++i) {
136 out.push_back(codeFromInterpolation(interpolations[i], interpolationClass,
137 context + " component 'interpolations' at index " + std::to_string(i)));
138 }
139 return out;
140}
141
142// Reports the problems with an object that doesn't come with a document.
143void report(std::string const &name, HS3::Diagnostics &diagnostics)
144{
145 for (auto &diagnostic : diagnostics) {
146 diagnostic.path = name + (diagnostic.path.empty() ? "" : "." + diagnostic.path);
147 }
148 HS3::report(diagnostics);
149}
150
151int interpolationCode(std::optional<Interpolation> const &interpolation,
152 std::optional<Interpolation> const &defaultInterpolation, InterpolationClass interpolationClass,
153 std::string const &context)
154{
155 if (interpolation) {
156 return codeFromInterpolation(*interpolation, interpolationClass, context);
157 }
158 // HS3 forbids implementation-specific default interpolations.
159 if (!defaultInterpolation) {
160 RooJSONFactoryWSTool::error(context + " is not given, and the channel has no \"default_interpolation\"");
161 }
162 return codeFromInterpolation(*defaultInterpolation, interpolationClass, context);
163}
164
165void erasePrefix(std::string &str, std::string_view prefix)
166{
167 if (startsWith(str, prefix)) {
168 str.erase(0, prefix.size());
169 }
170}
171
172bool eraseSuffix(std::string &str, std::string_view suffix)
173{
174 if (endsWith(str, suffix)) {
175 str.erase(str.size() - suffix.size());
176 return true;
177 } else {
178 return false;
179 }
180}
181
182template <class Coll>
183void sortByName(Coll &coll)
184{
185 std::sort(coll.begin(), coll.end(), [](auto &l, auto &r) { return l.name < r.name; });
186}
187
188template <class T>
189T *findClient(RooAbsArg *gamma)
190{
191 for (const auto &client : gamma->clients()) {
192 if (auto casted = dynamic_cast<T *>(client)) {
193 return casted;
194 } else {
195 T *c = findClient<T>(client);
196 if (c)
197 return c;
198 }
199 }
200 return nullptr;
201}
202
203template <class T>
204T *findAmong(RooAbsArg *g, std::vector<RooAbsPdf *> const &pdfs)
205{
206 for (RooAbsPdf *pdf : pdfs) {
207 if (auto casted = dynamic_cast<T *>(pdf); casted && casted->dependsOn(*g)) {
208 return casted;
209 }
210 }
211 return nullptr;
212}
213
214// Find the constraint of the parameter `g`: preferably among the constraint terms `channelConstraints` of the channel
215// (the other factors of its product), otherwise among the clients of `g`. Looking at the channel first avoids taking
216// another pdf that depends on the parameter, e.g. in another channel, for its constraint.
217RooAbsPdf *findConstraint(RooAbsArg *g, std::vector<RooAbsPdf *> const &channelConstraints)
218{
219 if (!g)
220 return nullptr;
221 if (auto *constraint = findAmong<RooPoisson>(g, channelConstraints))
222 return constraint;
223 if (auto *constraint = findAmong<RooGaussian>(g, channelConstraints))
224 return constraint;
225 if (auto *constraint = findAmong<RooLognormal>(g, channelConstraints))
226 return constraint;
227 if (auto *constraint = findClient<RooPoisson>(g))
228 return constraint;
229 if (auto *constraint = findClient<RooGaussian>(g))
230 return constraint;
231 return findClient<RooLognormal>(g);
232}
233
234inline std::string defaultGammaName(std::string const &sysname, std::size_t i)
235{
236 return "gamma_" + sysname + "_bin_" + std::to_string(i);
237}
238
239RooRealVar &createNominal(RooWorkspace &ws, std::string const &parname, double val, double min, double max)
240{
241 RooRealVar &nom = getOrCreate<RooRealVar>(ws, "nom_" + parname, val, min, max);
242 nom.setConstant(true);
243 return nom;
244}
245
246/// Get the conventional name of the constraint pdf for a constrained
247/// parameter.
248std::string constraintName(std::string const &paramName)
249{
250 return paramName + "Constraint";
251}
252
253// The component of the array distribution `array` that constrains `param`: the one that depends on it.
254RooAbsPdf &arrayComponentFor(RooProdPdf const &array, RooAbsArg const &param, std::string const &context)
255{
256 std::vector<RooAbsPdf *> matches;
257 for (RooAbsPdf *pdf : static_range_cast<RooAbsPdf *>(array.pdfList())) {
258 if (pdf->dependsOn(param)) {
259 matches.push_back(pdf);
260 }
261 }
262 if (matches.size() == 1) {
263 return *matches.front();
264 }
265 const std::string what = "the array distribution '" + std::string(array.GetName()) +
266 "' named as the constraint of '" + param.GetName() + "' (" + context + ") has ";
267 if (matches.empty()) {
268 RooJSONFactoryWSTool::error(what + "no component that depends on this parameter");
269 }
270 RooJSONFactoryWSTool::error(what + std::to_string(matches.size()) +
271 " components that depend on this parameter, but a parameter can only be constrained "
272 "by one component");
273}
274
275// Find the constraint distribution `constraintName` of the parameter `param`. If the name resolves to an array
276// distribution, the component that depends on the parameter is returned: that is the term that enters the likelihood
277// for this parameter, and the whole array enters once through the components of all of its parameters. Returns
278// nullptr if the name cannot be resolved.
279RooAbsPdf *findNamedConstraint(RooJSONFactoryWSTool &tool, std::string const &constraintName, RooAbsArg const &param,
280 std::string const &context)
281{
282 RooAbsPdf *constraint = tool.tryRequest<RooAbsPdf>(constraintName);
283 if (!constraint || !constraint->getAttribute(arrayDistTag)) {
284 return constraint;
285 }
286 auto const *array = dynamic_cast<RooProdPdf const *>(constraint);
287 if (!array) {
288 return constraint;
289 }
290 return &arrayComponentFor(*array, param, context);
291}
292
293// The parameter of a modifier, which is created if the document doesn't define it. It's a variable, or a constant if
294// the document doesn't give it a domain.
295RooAbsReal &getOrCreateParameter(RooJSONFactoryWSTool &tool, std::string const &name, double val, double min,
296 double max, std::string const &modifier)
297{
298 RooWorkspace &ws = *tool.workspace();
299 TObject *existing = ws.obj(name);
300 if (!existing) {
301 return getOrCreate<RooRealVar>(ws, name, val, min, max);
302 }
303 if (dynamic_cast<RooRealVar *>(existing) || dynamic_cast<RooConstVar *>(existing)) {
304 return *static_cast<RooAbsReal *>(existing);
305 }
306 RooJSONFactoryWSTool::error("the parameter '" + name + "' of the modifier '" + modifier +
307 "' is neither a variable nor a constant");
308}
309
310void setErrorIfVariable(RooAbsReal &param, double error)
311{
312 if (auto *var = dynamic_cast<RooRealVar *>(&param)) {
313 var->setError(error);
314 }
315}
316
317// All constraints of parameters that control a whole sample are created here. Following the HS3 standard, the nominal
318// value is defined by the type of the modifier, and the standard deviation is one.
319void addConstraint(RooJSONFactoryWSTool &tool, HS3::Constraint const &constraint, RooAbsReal &param, double nominal,
320 const char *modifierType, std::string const &modifier, std::string const &sample,
321 RooArgSet &constraints)
322{
323 RooWorkspace &ws = *tool.workspace();
324 std::visit(HS3::overloaded{
325 [](HS3::Unconstrained) {},
327 if (type != HS3::ConstraintType::Gauss) {
328 RooJSONFactoryWSTool::error(std::string("legacy constraint value '") + toString(type) +
329 "' for modifier '" + modifier +
330 "' is a known constraint type, but it cannot be resolved in this "
331 "context");
332 }
333 RooRealVar &nom = createNominal(ws, param.GetName(), nominal, -10, 10);
334 setErrorIfVariable(param, 1.0);
335 const std::string name = constraintName(param.GetName());
336 RooAbsPdf *pdf = ws.pdf(name);
337 constraints.add(pdf ? *pdf : tool.wsEmplace<RooGaussian>(name, param, nom, 1.));
338 },
339 [&](HS3::ConstraintPdf const &constraintPdf) {
340 RooAbsPdf *pdf = findNamedConstraint(tool, constraintPdf.pdf.name, param,
341 std::string("the ") + modifierType + " modifier '" + modifier +
342 "' of sample '" + sample + "'");
343 if (!pdf) {
344 RooJSONFactoryWSTool::error("unable to find definition of of constraint '" +
345 constraintPdf.pdf.name + "' for modifier '" + modifier + "'");
346 }
347 if (auto gauss = dynamic_cast<RooGaussian *>(pdf)) {
348 setErrorIfVariable(param, gauss->getSigma().getVal());
349 }
350 constraints.add(*pdf);
351 },
352 },
353 constraint);
354}
355
356// The constraint of the parameter of a normsys or histosys modifier, whose nominal value is zero. Before HS3 named the
357// constraint distributions in "constraints", RooFit created the global observable "nom_<parameter>", which the
358// constraint distribution of older documents may use without defining it.
359void addSysConstraint(RooJSONFactoryWSTool &tool, HS3::Constraint const &constraint, RooAbsReal &param,
360 const char *modifierType, std::string const &modifier, std::string const &sample,
361 RooArgSet &constraints)
362{
363 auto const *pdf = std::get_if<HS3::ConstraintPdf>(&constraint);
364 if (pdf && pdf->legacyName) {
365 createNominal(*tool.workspace(), param.GetName(), 0.0, -10, 10);
366 }
367 addConstraint(tool, constraint, param, 0.0, modifierType, modifier, sample, constraints);
368}
369
370// The tau of a Poisson constraint of a gamma whose mean is the product gamma * tau, like in HistFactory, or
371// std::nullopt if the constraint has another form.
372std::optional<double> findPoissonTau(RooPoisson const &constraint, RooAbsArg const &gamma)
373{
374 auto const *mean = dynamic_cast<RooProduct const *>(&constraint.getMean());
375 if (!mean) {
376 return std::nullopt;
377 }
378
379 for (RooAbsArg *arg : mean->servers()) {
380 if (arg == &gamma) {
381 continue;
382 }
383
384 if (auto const *tau = dynamic_cast<RooConstVar const *>(arg)) {
385 return tau->getVal();
386 }
387
388 // Imported workspaces can sometimes represent
389 // constants as constant RooRealVars.
390 if (auto const *real = dynamic_cast<RooAbsReal const *>(arg)) {
391 if (real->isConstant() || endsWith(std::string(real->GetName()), "_tau")) {
392 return real->getVal();
393 }
394 }
395 }
396
397 return std::nullopt;
398}
399
400// The relative uncertainty that the constraint distribution of a gamma encodes, if it has one of the forms of
401// HistFactory: the sigma of a Gaussian, or 1/sqrt(tau) for a Poisson distribution with mean gamma * tau. The nominal
402// value of a gamma is 1, so the sigma of a Gaussian is relative to it. It is not divided by the mean, which is the
403// gamma itself in the Gaussians that HistFactory creates.
404std::optional<double> gammaRelUncertainty(RooAbsPdf const &constraint, RooAbsArg const &gamma)
405{
406 if (auto gauss = dynamic_cast<RooGaussian const *>(&constraint)) {
407 return gauss->getSigma().getVal();
408 }
409 if (auto poisson = dynamic_cast<RooPoisson const *>(&constraint)) {
410 if (auto tau = findPoissonTau(*poisson, gamma)) {
411 return 1. / std::sqrt(*tau);
412 }
413 }
414 return std::nullopt;
415}
416
417// Configure the gammas that have a constraint distribution like HistFactory does from their relative uncertainties,
418// see configureConstrainedGammas(). The relative uncertainty of a gamma is the one in `relSigmas` if it is positive,
419// and otherwise the one that its constraint distribution encodes, see gammaRelUncertainty(). Without `relSigmas`, the
420// latter is only the initial error of the gamma. The unconstrained gammas are free, and left as they are. So are the
421// gammas that are functions defined elsewhere in the document, since only variables can be configured.
422void configureGammasWithConstraints(RooArgList const &gammas, std::vector<double> const &relSigmas,
423 std::vector<RooAbsPdf *> const &constraintPdfs, double minSigma)
424{
425 RooArgList constrainedGammas;
426 std::vector<double> constrainedSigmas;
427 for (std::size_t i = 0; i < gammas.size(); ++i) {
428 auto *gamma = dynamic_cast<RooRealVar *>(&gammas[i]);
429 if (!constraintPdfs[i] || !gamma) {
430 continue;
431 }
432 std::optional<double> relSigma = !relSigmas.empty() && relSigmas[i] > 0.
433 ? std::make_optional(relSigmas[i])
434 : gammaRelUncertainty(*constraintPdfs[i], *gamma);
435 // Without any known uncertainty, the gamma is left to its constraint distribution.
436 if (!relSigma) {
437 continue;
438 }
439 if (relSigmas.empty()) {
440 gamma->setError(*relSigma);
441 } else {
442 constrainedGammas.add(*gamma);
443 constrainedSigmas.push_back(*relSigma);
444 }
445 }
446 configureConstrainedGammas(constrainedGammas, constrainedSigmas, minSigma);
447}
448
449// Create the ParamHistFunc of the gamma parameters, together with their constraints. With a constraint type, RooFit
450// creates the constraints of the gammas from their relative uncertainties `vals`, like HistFactory.
451ParamHistFunc &createPHF(RooJSONFactoryWSTool &tool, std::string const &phfname, std::string const &sysname,
452 std::vector<HS3::ParameterRef> const &parnames, std::vector<double> const &vals,
453 HS3::GammaConstraint const &constraint, RooArgSet &constraints, RooArgSet const &observables,
454 double gammaMax, double minSigma, std::size_t nBins, std::string const &context)
455{
456 RooWorkspace &ws = *tool.workspace();
457
458 // The validation made sure that there is a value, parameter and constraint for each bin, if they are given.
459 RooArgList gammas;
460 for (std::size_t i = 0; i < nBins; ++i) {
461 const std::string name = parnames.empty() ? defaultGammaName(sysname, i) : parnames[i].name;
462 if (auto *existing = dynamic_cast<RooAbsReal *>(ws.obj(name))) {
463 gammas.add(*existing);
464 } else {
465 gammas.add(getOrCreate<RooRealVar>(ws, name, 1., defaultGammaMin, gammaMax));
466 }
467 }
468
469 // The gammas and `vals` are given in the order of HS3 (last axis fastest), which is also the order in which they
470 // are paired below. ParamHistFunc expects its parameters in TH1 order.
471 checkParamHistFuncOrderIsDefined(tool.hs3VersionInput(), observables, "the modifier '" + sysname + "'");
472 RooArgList gammasInTH1Order = toParamHistFuncOrder(gammas, observables);
473
474 // Shape modifiers of the same name in a channel share the function, so they need the same parameters.
475 if (RooAbsArg *existing = ws.arg(phfname.c_str())) {
476 auto *existingPHF = dynamic_cast<ParamHistFunc *>(existing);
477 bool same = existingPHF && existingPHF->paramList().size() == gammasInTH1Order.size();
478 for (std::size_t i = 0; same && i < gammasInTH1Order.size(); ++i) {
479 same = &existingPHF->paramList()[i] == &gammasInTH1Order[i];
480 }
481 if (!same) {
482 RooJSONFactoryWSTool::error("the modifier '" + sysname + "' has other parameters than another modifier " +
483 "of this name in the channel");
484 }
485 }
486 auto &phf = tool.wsEmplace<ParamHistFunc>(phfname, observables, gammasInTH1Order);
487
488 auto addGammaConstraints = [&](Constraint::Type type) {
489 for (auto const &term : createGammaConstraints(gammas, vals, minSigma, type).constraints) {
491 constraints.add(*ws.pdf(term->GetName()));
492 }
493 };
494
495 if (auto const *pdfs = std::get_if<HS3::GammaConstraintPdfs>(&constraint)) {
496 // The constraints are resolved per gamma (in bin order, like "constraints"), which also picks the right
497 // component out of an array distribution. Unconstrained gammas get a nullptr.
498 std::vector<RooAbsPdf *> constraintPdfs;
499 for (std::size_t i = 0; i < pdfs->pdfs.size(); ++i) {
500 auto const &pdfName = pdfs->pdfs[i];
501 RooAbsPdf *pdf = nullptr;
502 if (pdfName) {
503 pdf = findNamedConstraint(tool, pdfName->name, gammas[i], context);
504 if (!pdf) {
505 RooJSONFactoryWSTool::error("unable to find definition of constraint '" + pdfName->name +
506 "' for modifier '" + sysname + "'");
507 }
508 constraints.add(*pdf);
509 }
510 constraintPdfs.push_back(pdf);
511 }
512 configureGammasWithConstraints(gammas, vals, constraintPdfs, minSigma);
513 }
514
515 if (vals.empty()) {
516 return phf;
517 }
518
519 std::visit(HS3::overloaded{
520 // Free gammas are left as they are, and the ones with named constraints are configured above.
521 [](HS3::Unconstrained) {},
522 [](HS3::GammaConstraintPdfs const &) {},
524 // Only the gammas that are variables can be set constant; the others can be functions.
525 for (RooAbsArg *gamma : gammas) {
526 if (auto *var = dynamic_cast<RooRealVar *>(gamma)) {
527 var->setConstant(true);
528 }
529 }
530 },
532 switch (type) {
533 case HS3::ConstraintType::Gauss: addGammaConstraints(Constraint::Gaussian); break;
534 case HS3::ConstraintType::Poisson: addGammaConstraints(Constraint::Poisson); break;
535 case HS3::ConstraintType::LogNormal:
536 RooJSONFactoryWSTool::error(std::string("unsupported constraint '") + toString(type) +
537 "' for the gamma parameters of '" + sysname +
538 "', supported are \"Poisson\" and \"Gauss\"");
539 }
540 },
541 },
542 constraint);
543
544 return phf;
545}
546
547// The relative uncertainty that the constraint distribution of a gamma encodes, see gammaRelUncertainty(). Constraints
548// of another form raise an error.
549double constraintRelError(RooAbsPdf const &constraint, RooAbsArg const &gamma)
550{
551 if (auto relSigma = gammaRelUncertainty(constraint, gamma)) {
552 return *relSigma;
553 }
554 RooJSONFactoryWSTool::error("unsupported constraint '" + std::string(constraint.GetName()) + "' of '" +
555 gamma.GetName() +
556 "': only RooGaussian and RooPoisson with mean gamma * tau are supported for the "
557 "relative uncertainties of gammas");
558 return std::numeric_limits<double>::quiet_NaN();
559}
560
561struct ChannelContext {
564 RooArgSet const &observables;
565 std::string fprefix;
566 std::string channelName;
567 std::size_t nBins;
568 RooAbsArg const *mcStatObject;
569 RooArgSet &constraints;
570};
571
572// Collects what the modifiers of a sample contribute to the RooFit objects.
573struct SampleElements {
574 RooArgList shapeElems;
575 RooArgList normElems;
576
577 RooArgList overallNps;
578 std::vector<double> overallLow;
579 std::vector<double> overallHigh;
580 std::vector<int> overallInterp;
581
582 RooArgList histNps;
583 RooArgList histoLo;
584 RooArgList histoHi;
585 std::vector<int> histoInterp;
586};
587
588void buildSample(ChannelContext const &ctx, HS3::Sample const &sample, RooDataHist &dh)
589{
590 RooJSONFactoryWSTool &tool = ctx.tool;
591 RooWorkspace &ws = *tool.workspace();
592
593 std::string const &sampleName = sample.name;
594 std::string prefixedName = ctx.fprefix + "_" + sampleName;
595
596 // The objects of a sample are not shared, so an existing object of the same name is a name clash.
597 auto newName = [&](std::string name) {
598 if (ws.arg(name.c_str())) {
599 RooJSONFactoryWSTool::error("the object '" + name + "' for sample '" + sampleName + "' of channel '" +
600 ctx.channelName + "' has the name of an existing object");
601 }
602 return name;
603 };
604
605 auto &hf = tool.wsEmplace<RooHistFunc>(newName("hist_" + prefixedName), ctx.observables, dh);
606 hf.SetTitle(sampleName.c_str());
607
608 SampleElements elems;
609 elems.shapeElems.add(tool.wsEmplace<RooBinWidthFunction>(newName(prefixedName + "_binWidth"), hf, true));
610
611 const bool hasStatError = std::any_of(sample.modifiers.begin(), sample.modifiers.end(),
612 [](auto const &m) { return std::holds_alternative<HS3::StatError>(m); });
613 if (hasStatError) {
614 elems.shapeElems.add(*ctx.mcStatObject);
615 }
616
617 auto context = [&](const char *type, std::string const &name) {
618 return std::string("interpolation for ") + type + " modifier '" + name + "' in sample '" + sampleName +
619 "' of channel '" + ctx.channelName + "'";
620 };
621
622 for (HS3::Modifier const &modifier : sample.modifiers) {
623 std::visit(
625 [&](HS3::StatError const &) {
626 // this is dealt with at a different place, ignore it for now
627 },
628 [&](HS3::NormFactor const &mod) {
629 RooAbsReal &param = getOrCreateParameter(tool, mod.parameter.name, 1., -3, 5, mod.name);
630 // Norm factors get an initial zero error like in HistoToWorkspaceFactoryFast, unless an error was
631 // already set, e.g. by the constraint of another occurrence of this parameter.
632 if (auto *var = dynamic_cast<RooRealVar *>(&param); var && !var->hasError()) {
633 var->setError(0.0);
634 }
635 elems.normElems.add(param);
636 // for norm factors, constraints are optional
637 addConstraint(tool, mod.constraint, param, 1.0, "normfactor", mod.name, sampleName, ctx.constraints);
638 },
639 [&](HS3::NormSys const &mod) {
640 RooAbsReal &param = getOrCreateParameter(tool, mod.parameter.name, 0., -5, 5, mod.name);
641 elems.overallNps.add(param);
642 const int interp = interpolationCode(mod.interpolation, ctx.dist.defaultInterpolation,
643 InterpolationClass::Flexible, context("normsys", mod.name));
644 double low = mod.lo;
645 double high = mod.hi;
646
647 // the below contains a a hack to cut off variations that go below 0
648 // This is needed because FlexibleInterpVar code 4 interpolates in log-space. Hence, values <= 0 result
649 // in NaN, which propagates throughout the model and causes evaluations to fail. If you know a nicer way
650 // to solve this, please go ahead and fix the lines below.
651 if (interp == 4 && low <= 0)
652 low = std::numeric_limits<double>::epsilon();
653 if (interp == 4 && high <= 0)
654 high = std::numeric_limits<double>::epsilon();
655
656 elems.overallLow.push_back(low);
657 elems.overallHigh.push_back(high);
658 elems.overallInterp.push_back(interp);
659
660 addSysConstraint(tool, mod.constraint, param, "normsys", mod.name, sampleName, ctx.constraints);
661 },
662 [&](HS3::HistoSys const &mod) {
663 RooAbsReal &param = getOrCreateParameter(tool, mod.parameter.name, 0., -5, 5, mod.name);
664 elems.histNps.add(param);
665 auto addVariation = [&](RooArgList &list, HS3::HistoSysVariation const &variation, const char *suffix) {
666 const std::string name = newName(mod.name + suffix + prefixedName);
667 list.add(
668 tool.wsEmplace<RooHistFunc>(name, ctx.observables,
669 HS3::createDataHist(name, ctx.observables, variation.contents,
670 variation.errors ? &*variation.errors : nullptr)));
671 };
672 addVariation(elems.histoLo, mod.lo, "Low_");
673 addVariation(elems.histoHi, mod.hi, "High_");
674 elems.histoInterp.push_back(interpolationCode(mod.interpolation, ctx.dist.defaultInterpolation,
675 InterpolationClass::Piecewise,
676 context("histosys", mod.name)));
677 addSysConstraint(tool, mod.constraint, param, "histosys", mod.name, sampleName, ctx.constraints);
678 },
679 [&](HS3::ShapeSys const &mod) {
680 const bool constrained = std::visit(
681 HS3::overloaded{[](HS3::Unconstrained) { return false; }, [](HS3::ConstantGammas) { return false; },
682 [&](HS3::ConstraintType) { return !mod.vals.empty(); },
683 [](HS3::GammaConstraintPdfs const &constraints) {
684 return std::any_of(constraints.pdfs.begin(), constraints.pdfs.end(),
685 [](auto const &pdf) { return pdf.has_value(); });
686 }},
687 mod.constraint);
688 // Follow the naming of HistFactory: "<channel>_<name>_ShapeSys" for constrained and
689 // "<channel>_<name>_shapeFactor" for free parameters.
690 const std::string funcName =
691 ctx.channelName + "_" + mod.name + (constrained ? "_ShapeSys" : "_shapeFactor");
692 elems.shapeElems.add(
693 createPHF(tool, funcName, mod.name, mod.parameters, mod.vals, mod.constraint, ctx.constraints,
694 ctx.observables, defaultShapeSysGammaMax, minShapeUncertainty, ctx.nBins,
695 "the shapesys modifier '" + mod.name + "' of sample '" + sampleName + "'"));
696 },
697 [&](HS3::CustomModifier const &mod) {
698 // The function can come later in the document.
699 RooAbsReal *obj = tool.request<RooAbsReal>(mod.name, ctx.channelName);
700 if (obj->dependsOn(ctx.observables)) {
701 elems.shapeElems.add(*obj);
702 } else {
703 elems.normElems.add(*obj);
704 }
705 },
706 },
707 modifier);
708 }
709
710 if (!elems.overallNps.empty()) {
711 std::string interpName = newName(sampleName + "_" + ctx.channelName + "_epsilon");
713 interpName, elems.overallNps, 1., elems.overallLow, elems.overallHigh, elems.overallInterp));
714 }
715 if (!elems.histNps.empty()) {
716 auto &v = tool.wsEmplace<PiecewiseInterpolation>(newName("histoSys_" + prefixedName), hf, elems.histoLo,
717 elems.histoHi, elems.histNps, elems.histoInterp);
719 elems.shapeElems.add(v);
720 } else {
721 elems.shapeElems.add(hf);
722 }
723
724 tool.wsEmplace<RooProduct>(newName(prefixedName + "_shapes"), elems.shapeElems);
725 if (!elems.normElems.empty()) {
726 tool.wsEmplace<RooProduct>(newName(prefixedName + "_scaleFactors"), elems.normElems);
727 } else {
728 ws.factory("RooConstVar::" + newName(prefixedName + "_scaleFactors") + "(1.)");
729 }
730}
731
732// The constraint of the gamma parameters of the staterror modifiers. Without one, the gamma parameters are free.
733HS3::GammaConstraint statErrorConstraint(HS3::HistFactoryDist const &dist)
734{
735 // The constraint distributions named by a staterror modifier take precedence over a constraint type, and a type
736 // given on a staterror modifier over the channel-level setting.
737 HS3::GammaConstraint const *modifierConstraint = nullptr;
738 for (auto const &sample : dist.samples) {
739 for (auto const &modifier : sample.modifiers) {
740 auto const *statError = std::get_if<HS3::StatError>(&modifier);
741 if (!statError) {
742 continue;
743 }
744 if (std::holds_alternative<HS3::GammaConstraintPdfs>(statError->constraint)) {
745 return statError->constraint;
746 }
747 if (!modifierConstraint && !std::holds_alternative<HS3::Unconstrained>(statError->constraint)) {
748 modifierConstraint = &statError->constraint;
749 }
750 }
751 }
752 return modifierConstraint ? *modifierConstraint : dist.statError.constraint;
753}
754
755// Reject constraints of staterror modifiers that make no sense for gamma parameters.
756void checkStatErrorConstraints(HS3::HistFactoryDist const &dist, std::string const &name)
757{
758 for (auto const &sample : dist.samples) {
759 for (auto const &modifier : sample.modifiers) {
760 auto const *statError = std::get_if<HS3::StatError>(&modifier);
761 auto const *type = statError ? std::get_if<HS3::ConstraintType>(&statError->constraint) : nullptr;
762 if (type && *type == HS3::ConstraintType::LogNormal) {
763 RooJSONFactoryWSTool::error(std::string("unsupported constraint '") + toString(*type) +
764 "' of the staterror modifier in channel '" + name +
765 "', supported are \"Poisson\" and \"Gauss\"");
766 }
767 }
768 }
769}
770
771class HistFactoryImporter : public RooFit::JSONIO::Importer {
772public:
773 // This is only used for HistFactory distributions that don't come with a document.
774 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
775 {
776 RooFit::HS3::importHistFactoryNode(*tool, p, nullptr);
777 return true;
778 }
779};
780
781class FlexibleInterpVarStreamer : public RooFit::JSONIO::Exporter {
782public:
783 std::string const &key() const override
784 {
785 static const std::string keystring = "interpolation0d";
786 return keystring;
787 }
788 bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override
789 {
790 auto fip = static_cast<const RooStats::HistFactory::FlexibleInterpVar *>(func);
791 const std::string context = "FlexibleInterpVar '" + std::string{fip->GetName()} + "'";
792 const std::size_t nParameters = fip->variables().size();
793 if (fip->low().size() != nParameters || fip->high().size() != nParameters ||
794 fip->interpolationCodes().size() != nParameters) {
795 RooJSONFactoryWSTool::error(context + " has non-matching parameter, variation, and interpolation lengths");
796 }
798 for (RooAbsArg const *var : fip->variables()) {
799 out.parameters.push_back(var->GetName());
800 }
801 out.nominal = fip->nominal();
802 out.high = fip->high();
803 out.low = fip->low();
804 out.interpolations = toInterpolations(fip->interpolationCodes(), InterpolationClass::Flexible, context);
806 return true;
807 }
808};
809
810class PiecewiseInterpolationStreamer : public RooFit::JSONIO::Exporter {
811public:
812 std::string const &key() const override
813 {
814 static const std::string keystring = "interpolation";
815 return keystring;
816 }
817 bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override
818 {
819 const PiecewiseInterpolation *pip = static_cast<const PiecewiseInterpolation *>(func);
820 const std::string context = "PiecewiseInterpolation '" + std::string{pip->GetName()} + "'";
821 const std::size_t nParameters = pip->paramList().size();
822 if (pip->lowList().size() != nParameters || pip->highList().size() != nParameters ||
823 pip->interpolationCodes().size() != nParameters) {
824 RooJSONFactoryWSTool::error(context + " has non-matching parameter, variation, and interpolation lengths");
825 }
827 out.nominal = pip->nominalHist()->GetName();
828 for (std::size_t i = 0; i < nParameters; ++i) {
829 out.parameters.push_back(HS3::toNameOrNumber(pip->paramList()[i]));
830 out.high.push_back(HS3::toNameOrNumber(pip->highList()[i]));
831 out.low.push_back(HS3::toNameOrNumber(pip->lowList()[i]));
832 }
833 out.interpolations = toInterpolations(pip->interpolationCodes(), InterpolationClass::Piecewise, context);
836 return true;
837 }
838};
839
840class PiecewiseInterpolationFactory : public RooFit::JSONIO::Importer {
841public:
842 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
843 {
844 std::string name(RooJSONFactoryWSTool::name(p));
845 HS3::Diagnostics diagnostics;
847 report(name, diagnostics);
848
849 auto request = [&](std::vector<HS3::NameOrNumber> const &refs) {
850 RooArgList out;
851 for (auto const &ref : refs) {
852 auto const *value = std::get_if<double>(&ref);
853 out.add(value ? RooFit::RooConst(*value) : *tool->request<RooAbsReal>(std::get<std::string>(ref), name));
854 }
855 return out;
856 };
857 RooArgList vars{request(in.parameters)};
858 RooArgList low{request(in.low)};
859 RooArgList high{request(in.high)};
860 auto &pip = tool->wsEmplace<PiecewiseInterpolation>(
861 name, *tool->request<RooAbsReal>(in.nominal, name), low, high, vars,
862 toCodes(in.interpolations, InterpolationClass::Piecewise, "PiecewiseInterpolation '" + name + "'"));
864 return true;
865 }
866};
867
868class FlexibleInterpVarFactory : public RooFit::JSONIO::Importer {
869public:
870 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
871 {
872 std::string name(RooJSONFactoryWSTool::name(p));
873 HS3::Diagnostics diagnostics;
875 report(name, diagnostics);
876
877 RooArgList vars;
878 for (std::string const &var : in.parameters) {
879 vars.add(*tool->request<RooRealVar>(var, name));
880 }
882 name, vars, in.nominal, in.low, in.high,
883 toCodes(in.interpolations, InterpolationClass::Flexible, "FlexibleInterpVar '" + name + "'"));
884 return true;
885 }
886};
887
888std::string stripOuterParens(const std::string &s)
889{
890 size_t start = 0;
891 size_t end = s.size();
892
893 while (start < end && s[start] == '(' && s[end - 1] == ')') {
894 int depth = 0;
895 bool balanced = true;
896 for (size_t i = start; i < end - 1; ++i) {
897 if (s[i] == '(') {
898 ++depth;
899 } else if (s[i] == ')') {
900 --depth;
901 }
902 if (depth == 0 && i < end - 1) {
903 balanced = false;
904 break;
905 }
906 }
907 if (balanced) {
908 ++start;
909 --end;
910 } else {
911 break;
912 }
913 }
914 return s.substr(start, end - start);
915}
916
917/// Whether the `+` or `-` at position `i` of `expr` is a binary operator, i.e. not a sign and not part of the exponent
918/// of a number like `1e-5`.
919bool isBinaryOperator(const std::string &expr, std::size_t i)
920{
921 std::size_t j = i;
922 while (j > 0 && std::isspace(static_cast<unsigned char>(expr[j - 1]))) {
923 --j;
924 }
925 if (j == 0) {
926 return false;
927 }
928 const char prev = expr[j - 1];
929 if (prev == 'e' || prev == 'E') {
930 // An exponent if the "e" directly follows the digits of a number, which don't start a name.
931 std::size_t k = j - 1;
932 while (k > 0 && (std::isdigit(static_cast<unsigned char>(expr[k - 1])) || expr[k - 1] == '.')) {
933 --k;
934 }
935 const bool startsName = k > 0 && (std::isalpha(static_cast<unsigned char>(expr[k - 1])) || expr[k - 1] == '_');
936 if (k < j - 1 && !startsName && i == j) {
937 return false;
938 }
939 }
940 return std::isalnum(static_cast<unsigned char>(prev)) || prev == '_' || prev == '.' || prev == ')' || prev == ']';
941}
942
943std::vector<std::string> splitTopLevelProduct(const std::string &expr)
944{
945 std::vector<std::string> parts;
946 int depth = 0;
947 size_t start = 0;
948 bool foundTopLevelStar = false;
949
950 for (size_t i = 0; i < expr.size(); ++i) {
951 char c = expr[i];
952 if (c == '(') {
953 ++depth;
954 } else if (c == ')') {
955 --depth;
956 } else if ((c == '+' || c == '-') && depth == 0 && isBinaryOperator(expr, i)) {
957 return {}; // A sum or difference at the top level is not a product, e.g. "1 + 0.2 * k"
958 } else if (c == '*' && depth == 0) {
959 foundTopLevelStar = true;
960 std::string sub = expr.substr(start, i - start);
961 parts.push_back(stripOuterParens(sub));
962 start = i + 1;
963 }
964 }
965
966 if (!foundTopLevelStar) {
967 return {}; // Not a top-level product
968 }
969
970 std::string sub = expr.substr(start);
971 parts.push_back(stripOuterParens(sub));
972 return parts;
973}
974
975// Expand the nested products of the constraint terms of a channel into their factors, which are the constraint terms
976// that the modifier parameters are looked up in, see findConstraint().
977void collectConstraintTerms(RooAbsPdf *pdf, std::vector<RooAbsPdf *> &terms)
978{
979 if (auto prod = dynamic_cast<RooProdPdf *>(pdf)) {
980 for (auto *factor : static_range_cast<RooAbsPdf *>(prod->pdfList())) {
981 collectConstraintTerms(factor, terms);
982 }
983 } else {
984 terms.push_back(pdf);
985 }
986}
987
988// A normsys that is written as the formula "1 +- a * b", where one of a and b is a constrained parameter.
989struct FormulaNormSys {
990 RooAbsReal *param = nullptr;
991 RooAbsPdf *constraint = nullptr;
992 double variation = 0.0;
993};
994
995FormulaNormSys parseOverallModifierFormula(const std::string &s, RooFormulaVar *formula,
996 std::vector<RooAbsPdf *> const &channelConstraints)
997{
998 static const std::regex pattern(
999 R"(^\s*1(?:\.0)?\s*([\+\-])\s*([a-zA-Z_][a-zA-Z0-9_]*|[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*\*\s*([a-zA-Z_][a-zA-Z0-9_]*|[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*$)");
1000
1001 FormulaNormSys sys;
1002 std::smatch match;
1003 if (std::regex_match(s, match, pattern)) {
1004 const double sign = match[1].str() == "-" ? -1.0 : 1.0;
1005
1006 std::string token2 = match[2].str();
1007 std::string token3 = match[4].str();
1008
1009 RooAbsReal *p2 = static_cast<RooAbsReal *>(formula->getParameter(token2.c_str()));
1010 RooAbsReal *p3 = static_cast<RooAbsReal *>(formula->getParameter(token3.c_str()));
1011 RooRealVar *v2 = dynamic_cast<RooRealVar *>(p2);
1012 RooRealVar *v3 = dynamic_cast<RooRealVar *>(p3);
1013
1014 auto *constr2 = findConstraint(v2, channelConstraints);
1015 auto *constr3 = findConstraint(v3, channelConstraints);
1016
1017 if (constr2 && !p3) {
1018 sys.param = p2;
1019 sys.variation = sign * toDouble(token3);
1020 } else if (!p2 && constr3) {
1021 sys.param = p3;
1022 sys.variation = sign * toDouble(token2);
1023 } else if (constr2 && p3 && !constr3) {
1024 sys.param = v2;
1025 sys.variation = sign * p3->getVal();
1026 } else if (p2 && !constr2 && constr3) {
1027 sys.param = v3;
1028 sys.variation = sign * p2->getVal();
1029 }
1030
1031 // A recognized formula has exactly one constrained parameter.
1032 sys.constraint = constr2 ? constr2 : constr3;
1033 }
1034 return sys;
1035}
1036
1037void collectElements(RooArgList &elems, RooAbsArg *arg)
1038{
1039 if (auto prod = dynamic_cast<RooProduct *>(arg)) {
1040 for (const auto &e : prod->components()) {
1041 collectElements(elems, e);
1042 }
1043 } else {
1044 elems.add(*arg);
1045 }
1046}
1047
1048bool allRooRealVar(const RooAbsCollection &list)
1049{
1050 for (auto *var : list) {
1051 if (!dynamic_cast<RooRealVar *>(var)) {
1052 return false;
1053 }
1054 }
1055 return true;
1056}
1057
1058// The parameters of normsys and histosys modifiers must be variables.
1059void checkModifierParameter(RooAbsReal const &param, RooAbsPdf const *constraint)
1060{
1061 if (!constraint && !param.isConstant() && !dynamic_cast<RooRealVar const *>(&param)) {
1062 RooJSONFactoryWSTool::error("cannot find constraint for " + std::string(param.GetName()) +
1063 ": functions are not supported as parameters of this modifier");
1064 }
1065}
1066
1067std::vector<double> contents(RooDataHist const &dataHist)
1068{
1069 return {dataHist.weightArray(), dataHist.weightArray() + dataHist.numEntries()};
1070}
1071
1072// A channel as it is exported, and what the exporter needs to know about its RooFit objects.
1073struct ExportedChannel {
1074 std::string name;
1076 // The variables of the first histogram, named after the observables of the channel.
1077 std::unique_ptr<RooArgSet> observables;
1078 std::size_t nBins = 0;
1079 /// The pdfs that the modifiers name as constraints, by name.
1080 std::map<std::string, RooAbsPdf const *> constraints;
1081 std::vector<RooAbsReal *> customModifiers;
1082 /// Parts of formulas that are exported as custom modifiers. They are created for the export.
1083 std::vector<std::unique_ptr<RooAbsReal>> temporaries;
1084 // To reconstruct the statistical uncertainties, by bin starting at one.
1085 std::map<int, double> totYield;
1086 std::map<int, double> totYield2;
1087 std::map<int, double> relErrors;
1088 // Constant inverse bin widths of HistFactory before ROOT 6.26, which histfactory_dist implies.
1089 std::set<RooAbsArg const *> legacyBinWidthFactors;
1090};
1091
1092/// The volume of the first bin and the average bin volume of every histogram of a channel. HistFactory before ROOT
1093/// 6.26 took the inverse bin width as the number of bins over the range of each observable.
1094std::vector<double> binVolumes(RooRealSumPdf const &sumpdf)
1095{
1096 RooArgSet nodes;
1097 sumpdf.treeNodeServerList(&nodes);
1098 std::vector<double> out;
1099 for (RooAbsArg *node : nodes) {
1100 auto *hf = dynamic_cast<RooHistFunc const *>(node);
1101 if (!hf || hf->dataHist().numEntries() == 0) {
1102 continue;
1103 }
1104 out.push_back(hf->dataHist().binVolume(0));
1105 double average = 1.;
1106 for (RooAbsArg *obs : *hf->dataHist().get()) {
1107 if (auto *real = dynamic_cast<RooRealVar const *>(obs)) {
1108 average *= (real->getMax() - real->getMin()) / real->numBins();
1109 }
1110 }
1111 out.push_back(average);
1112 }
1113 return out;
1114}
1115
1116/// Whether a constant factor of a sample is the inverse bin width that HistFactory before ROOT 6.26 multiplied the
1117/// samples with (a constant `RooRealVar` `binWidth_<observables>_<i>` in the coefficients of the `RooRealSumPdf`).
1118/// `histfactory_dist` implies this factor, so it is not written as a modifier. It is identified by its structure, not
1119/// by its name: a constant that is not floating in any snapshot, used only by the sum or the products of the channel
1120/// (among the objects of the workspace),
1121/// with the inverse of the volume of the first bin, or of the average bin volume, of one of the histograms of the
1122/// channel as value.
1123bool isLegacyBinWidthFactor(RooAbsArg const &factor, RooRealSumPdf const &sumpdf, RooArgSet const &channelNodes,
1124 std::vector<double> const &volumes, RooWorkspace const *ws)
1125{
1126 if (!RooJSONFactoryWSTool::config().identifyLegacyBinWidthFactors) {
1127 return false;
1128 }
1129 auto const *var = dynamic_cast<RooRealVar const *>(&factor);
1130 if (!dynamic_cast<RooConstVar const *>(&factor) && !(var && var->isConstant())) {
1131 return false;
1132 }
1133 if (var && ws) {
1134 for (auto *snapshot : static_range_cast<RooArgSet const *>(ws->getSnapshots())) {
1135 auto const *inSnapshot = dynamic_cast<RooRealVar const *>(snapshot->find(*var));
1136 if (inSnapshot && !inSnapshot->isConstant()) {
1137 return false;
1138 }
1139 }
1140 }
1141 // A channel whose observables are renamed (RooJSONFactoryWSTool::Config::renameCombinedObservables) is a clone
1142 // made by RooCustomizer, which marks the cloned nodes with "ORIGNAME:<original>". The factor doesn't depend on the
1143 // observables, so it is not cloned: the original sum and products of the channel are still its clients.
1144 auto isOriginalOfChannelNode = [&](RooAbsArg const &client) {
1145 const std::string attribute = std::string{"ORIGNAME:"} + client.GetName();
1146 return sumpdf.getAttribute(attribute.c_str()) ||
1147 std::any_of(channelNodes.begin(), channelNodes.end(),
1148 [&](RooAbsArg const *node) { return node->getAttribute(attribute.c_str()); });
1149 };
1150 for (RooAbsArg const *client : factor.clients()) {
1151 const bool isChannelProduct = dynamic_cast<RooProduct const *>(client) && channelNodes.find(*client) == client;
1152 if (client == &sumpdf || isChannelProduct || isOriginalOfChannelNode(*client)) {
1153 continue;
1154 }
1155 // Objects outside the workspace are no part of the model, e.g. the computation graph of a likelihood that was
1156 // created from the model before the export.
1157 if (ws && ws->arg(client->GetName()) != client) {
1158 continue;
1159 }
1160 return false;
1161 }
1162 // The value was stored with 7 significant digits.
1163 const double value = static_cast<RooAbsReal const &>(factor).getVal();
1164 return std::any_of(volumes.begin(), volumes.end(),
1165 [&](double volume) { return volume > 0. && std::abs(value * volume - 1.) < 1e-6; });
1166}
1167
1168/// Whether a ParamHistFunc has the structure of the one of a HistFactory ShapeSys or ShapeFactor: it is defined over
1169/// the observables of the channel, with one parameter per bin of the channel. The import recreates such a function from
1170/// a shapefactor modifier. Its name doesn't matter.
1171bool isShapeModifierFunction(ParamHistFunc const &phf, ExportedChannel const &channel)
1172{
1173 if (!channel.observables || phf.paramList().size() != channel.nBins ||
1174 static_cast<std::size_t>(phf.numBins()) != channel.nBins ||
1175 phf.dataVars().size() != channel.observables->size()) {
1176 return false;
1177 }
1178 for (RooAbsArg const *obs : phf.dataVars()) {
1179 if (!channel.observables->find(obs->GetName())) {
1180 return false;
1181 }
1182 }
1183 return true;
1184}
1185
1186ExportedChannel readChannel(RooJSONFactoryWSTool *tool, const std::string &pdfname, const RooRealSumPdf *sumpdf,
1187 std::vector<RooAbsPdf *> const &channelConstraints)
1188{
1189 ExportedChannel channel;
1190
1191 RooWorkspace *ws = tool->workspace();
1192
1193 channel.name = pdfname;
1194 erasePrefix(channel.name, "model_");
1195 eraseSuffix(channel.name, "_model");
1196
1197 RooArgSet channelNodes;
1198 sumpdf->treeNodeServerList(&channelNodes);
1199 const std::vector<double> volumes = binVolumes(*sumpdf);
1200
1201 auto constraint = [&](RooAbsPdf const *pdf) -> HS3::Constraint {
1202 if (!pdf) {
1203 return HS3::Unconstrained{};
1204 }
1205 channel.constraints.emplace(pdf->GetName(), pdf);
1206 return HS3::ConstraintPdf{{pdf->GetName()}};
1207 };
1208 auto gammaConstraint = [&](RooAbsPdf const *pdf) -> std::optional<HS3::DistributionRef> {
1209 if (!pdf) {
1210 return std::nullopt;
1211 }
1212 channel.constraints.emplace(pdf->GetName(), pdf);
1213 return HS3::DistributionRef{pdf->GetName()};
1214 };
1215
1216 for (size_t sampleidx = 0; sampleidx < sumpdf->funcList().size(); ++sampleidx) {
1217 PiecewiseInterpolation *pip = nullptr;
1218 std::vector<ParamHistFunc *> phfs;
1219
1220 const auto func = sumpdf->funcList().at(sampleidx);
1221 HS3::Sample sample;
1222 sample.name = func->GetName();
1223 erasePrefix(sample.name, "L_x_");
1224 eraseSuffix(sample.name, "_shapes");
1225 eraseSuffix(sample.name, "_" + channel.name);
1226 erasePrefix(sample.name, pdfname + "_");
1227
1228 // The modifiers by type, which are written in this order.
1229 std::vector<HS3::NormFactor> normFactors;
1230 std::vector<HS3::NormSys> normSys;
1231 std::vector<HS3::HistoSys> histoSys;
1232 std::vector<HS3::ShapeSys> shapeSys;
1233 std::vector<HS3::CustomModifier> custom;
1234 std::vector<HS3::CustomModifier> temporaries;
1235 std::optional<HS3::StatError> statError;
1236
1237 auto addNormSys = [&](RooAbsReal &param, double hi, double lo, HS3::Interpolation interpolation,
1238 RooAbsPdf const *pdf) {
1239 checkModifierParameter(param, pdf);
1240 std::string sysname(param.GetName());
1241 erasePrefix(sysname, "alpha_");
1242 normSys.push_back({sysname, {param.GetName()}, hi, lo, constraint(pdf), interpolation});
1243 };
1244
1245 auto updateObservables = [&](RooHistFunc const &histFunc) {
1246 RooDataHist const &dataHist = histFunc.dataHist();
1247 // The axes are named after the observables of the RooHistFunc, which are not necessarily the variables of
1248 // its histogram (e.g. after a RooCustomizer replaced them).
1249 auto varsPtr =
1250 histogramVariablesNamedAfterObservables(dataHist, histFunc.getHistObsList(), histFunc.variables());
1251 if (channel.observables == nullptr) {
1252 channel.observables = std::move(varsPtr);
1253 channel.nBins = dataHist.numEntries();
1254 } else {
1255 // The bin contents of all samples are written in the order of the axes of the channel (the last axis
1256 // varies fastest), which is the order of the observables of the first histogram.
1257 RooArgSet const &vars = *varsPtr;
1258 bool sameOrder = vars.size() == channel.observables->size();
1259 for (std::size_t i = 0; sameOrder && i < vars.size(); ++i) {
1260 sameOrder = std::string(vars[i]->GetName()) == (*channel.observables)[i]->GetName();
1261 }
1262 if (!sameOrder) {
1263 RooJSONFactoryWSTool::error("the histograms of channel '" + channel.name +
1264 "' don't have their observables in the same order, so their bin "
1265 "contents can't be written along the same axes");
1266 }
1267 }
1268 if (sample.contents.empty()) {
1269 sample.contents = contents(dataHist);
1270 }
1271 };
1272 auto processElements = [&](const auto &elements, auto &&self) -> void {
1273 for (RooAbsArg *e : elements) {
1274 if (auto *binWidth = dynamic_cast<RooBinWidthFunction *>(e); binWidth && binWidth->divideByBinWidth()) {
1275 // The division by the bin width is implied by histfactory_dist.
1276 } else if (isLegacyBinWidthFactor(*e, *sumpdf, channelNodes, volumes, ws)) {
1277 // Before ROOT 6.26, HistFactory multiplied the samples with a constant inverse bin width instead.
1278 channel.legacyBinWidthFactors.insert(e);
1279 if (!TString(e->GetName()).Contains("binWidth")) {
1280 oocoutW(nullptr, ObjectHandling)
1281 << "RooFitHS3: the constant factor '" << e->GetName() << "' of sample '" << sample.name
1282 << "' in channel '" << channel.name
1283 << "' is used like the inverse bin width of HistFactory before ROOT 6.26 (its value is the "
1284 "inverse bin volume), so it is not written: histfactory_dist implies it. Set "
1285 "RooJSONFactoryWSTool::config().identifyLegacyBinWidthFactors = false to write such factors "
1286 "as normfactors."
1287 << std::endl;
1288 }
1289 } else if (auto constVar = dynamic_cast<RooConstVar *>(e)) {
1290 if (constVar->getVal() != 1.) {
1291 normFactors.push_back({constVar->GetName(), {constVar->GetName()}, HS3::Unconstrained{}});
1292 }
1293 } else if (auto par = dynamic_cast<RooRealVar *>(e)) {
1294 // The constraint is found among the clients of the parameter, whichever of x and mean of a Gaussian it
1295 // is.
1296 normFactors.push_back(
1297 {par->GetName(), {par->GetName()}, constraint(findConstraint(par, channelConstraints))});
1298 } else if (auto hf = dynamic_cast<const RooHistFunc *>(e)) {
1299 updateObservables(*hf);
1300 } else if (ParamHistFunc *phf = dynamic_cast<ParamHistFunc *>(e); phf && allRooRealVar(phf->paramList())) {
1301 phfs.push_back(phf);
1302 } else if (auto fip = dynamic_cast<RooStats::HistFactory::FlexibleInterpVar *>(e)) {
1303 // some (modified) histfactory models have several instances of FlexibleInterpVar
1304 // we collect and merge them
1305 for (size_t i = 0; i < fip->variables().size(); ++i) {
1306 RooAbsReal *var = static_cast<RooAbsReal *>(fip->variables().at(i));
1307 std::string sysname(var->GetName());
1308 erasePrefix(sysname, "alpha_");
1309 const std::string context = "normsys modifier '" + sysname + "' in sample '" + sample.name +
1310 "' of channel '" + channel.name + "'";
1311 // A parameter without constraint is unconstrained, see checkUnconstrainedModifiers().
1312 addNormSys(*var, fip->high()[i], fip->low()[i],
1313 interpolationFromCode(fip->interpolationCodes()[i], InterpolationClass::Flexible, context),
1314 findConstraint(var, channelConstraints));
1315 }
1316 } else if (!pip && (pip = dynamic_cast<PiecewiseInterpolation *>(e))) {
1317 // nothing to do here, already assigned
1318 } else if (RooFormulaVar *formula = dynamic_cast<RooFormulaVar *>(e)) {
1319 // people do a lot of fancy stuff with RooFormulaVar, like including NormSys via explicit formulae.
1320 // let's try to decompose it into building blocks
1321 TString expression(formula->expression());
1322 for (size_t i = formula->nParameters(); i--;) {
1323 const RooAbsArg *p = formula->getParameter(i);
1324 expression.ReplaceAll(("x[" + std::to_string(i) + "]").c_str(), p->GetName());
1325 expression.ReplaceAll(("@" + std::to_string(i)).c_str(), p->GetName());
1326 }
1327 auto components = splitTopLevelProduct(expression.Data());
1328 if (components.empty()) {
1329 // it's not a product, let's just treat it as an unknown element
1330 custom.push_back({formula->GetName()});
1331 channel.customModifiers.push_back(formula);
1332 } else {
1333 // it is a prododuct, we can try to handle the elements separately
1334 std::vector<RooAbsArg *> realComponents;
1335 int idx = 0;
1336 for (auto &comp : components) {
1337 // check if this is a trivial element of a product, we can treat it as its own modifier
1338 auto *part = formula->getParameter(comp.c_str());
1339 if (part) {
1340 realComponents.push_back(part);
1341 continue;
1342 }
1343 // check if this is an attempt at explicitly encoding an overallSys
1344 FormulaNormSys sys = parseOverallModifierFormula(comp, formula, channelConstraints);
1345 if (sys.param) {
1346 // Preserve the legacy export behaviour for recognized explicit formulae.
1347 addNormSys(*sys.param, 1.0 + sys.variation, 1.0 - sys.variation,
1348 multiplicativePiecewiseExponential, sys.constraint);
1349 continue;
1350 }
1351
1352 // this is something non-trivial, let's deal with it separately
1353 std::string name = std::string(formula->GetName()) + "_part" + std::to_string(idx);
1354 ++idx;
1355 temporaries.push_back({name});
1356 channel.temporaries.push_back(std::make_unique<RooFormulaVar>(
1357 name.c_str(), name.c_str(), comp.c_str(), formula->dependents()));
1358 }
1359 self(realComponents, self);
1360 }
1361 } else if (auto real = dynamic_cast<RooAbsReal *>(e)) {
1362 custom.push_back({real->GetName()});
1363 channel.customModifiers.push_back(real);
1364 }
1365 }
1366 };
1367
1368 RooArgList elems;
1369 collectElements(elems, func);
1370 collectElements(elems, sumpdf->coefList().at(sampleidx));
1371 processElements(elems, processElements);
1372
1373 // see if we can get the observables
1374 if (pip) {
1375 if (auto nh = dynamic_cast<RooHistFunc const *>(pip->nominalHist())) {
1376 updateObservables(*nh);
1377 }
1378 }
1379
1380 if (pip) {
1381 for (size_t i = 0; i < pip->paramList().size(); ++i) {
1382 RooAbsReal *var = static_cast<RooAbsReal *>(pip->paramList().at(i));
1383 std::string sysname(var->GetName());
1384 erasePrefix(sysname, "alpha_");
1385 if (auto lo = dynamic_cast<RooHistFunc *>(pip->lowList().at(i))) {
1386 if (auto hi = dynamic_cast<RooHistFunc *>(pip->highList().at(i))) {
1387 // A parameter without constraint is unconstrained, see checkUnconstrainedModifiers().
1388 const auto *pdf = findConstraint(var, channelConstraints);
1389 checkModifierParameter(*var, pdf);
1390 const std::string context = "histosys modifier '" + sysname + "' in sample '" + sample.name +
1391 "' of channel '" + channel.name + "'";
1392 histoSys.push_back(
1393 {sysname,
1394 {var->GetName()},
1395 {contents(hi->dataHist()), std::nullopt},
1396 {contents(lo->dataHist()), std::nullopt},
1397 constraint(pdf),
1398 interpolationFromCode(pip->interpolationCodes()[i], InterpolationClass::Piecewise, context)});
1399 }
1400 }
1401 }
1402 }
1403
1404 for (ParamHistFunc *phf : phfs) {
1405 if (startsWith(std::string(phf->GetName()), "mc_stat_")) { // MC stat uncertainty
1406 statError.emplace();
1407 statError->name = "staterror";
1409 int idx = 0;
1410 // In the order of the bins (last axis fastest), like the contents of the samples.
1412 statError->parameters.push_back({g->GetName()});
1413 ++idx;
1414 RooAbsPdf *pdf = findConstraint(g, channelConstraints);
1415 pdfs.pdfs.push_back(gammaConstraint(pdf));
1416 channel.totYield[idx] += sample.contents[idx - 1];
1417 channel.totYield2[idx] += (sample.contents[idx - 1] * sample.contents[idx - 1]);
1418 if (pdf) {
1419 channel.relErrors[idx] = constraintRelError(*pdf, *g);
1420 }
1421 }
1422 statError->constraint = std::move(pdfs);
1423 } else { // ShapeSys or ShapeFactor
1424 // Other ParamHistFuncs can't be written as shapefactors: the import would recreate the function over the
1425 // observables and bins of the channel. Over other variables, they are written as custom modifiers, which
1426 // refer to the function by name. Over the observables of the channel with another binning, they can't be
1427 // written at all, since the observables can only have one binning on import.
1428 if (!isShapeModifierFunction(*phf, channel)) {
1429 if (channel.observables && phf->dataVars().overlaps(*channel.observables)) {
1430 RooJSONFactoryWSTool::error("cannot write the ParamHistFunc '" + std::string(phf->GetName()) +
1431 "': it has another binning of the observables of channel '" +
1432 channel.name + "'");
1433 }
1434 custom.push_back({phf->GetName()});
1435 channel.customModifiers.push_back(phf);
1436 continue;
1437 }
1438 HS3::ShapeSys &sys = shapeSys.emplace_back();
1439 sys.name = phf->GetName();
1440 erasePrefix(sys.name, channel.name + "_");
1441 for (std::string_view suffix : {"_ShapeSys", "_shapeSys", "_ShapeFactor", "_shapeFactor"}) {
1442 if (eraseSuffix(sys.name, suffix))
1443 break;
1444 }
1445
1446 // Both are written as "shapefactor": the constrained parameters name their constraint distribution in
1447 // "constraints", the others are free.
1450 sys.parameters.push_back({g->GetName()});
1451 RooAbsPdf *pdf = findConstraint(g, channelConstraints);
1452 if (!pdf)
1453 pdf = ws->pdf(constraintName(g->GetName()));
1454 pdfs.pdfs.push_back(gammaConstraint(pdf));
1455 }
1456 sys.constraint = std::move(pdfs);
1457 }
1458 }
1459
1460 sortByName(normFactors);
1461 sortByName(normSys);
1462 sortByName(histoSys);
1463 sortByName(shapeSys);
1464 auto append = [&](auto &modifiers) {
1465 sample.modifiers.insert(sample.modifiers.end(), std::make_move_iterator(modifiers.begin()),
1466 std::make_move_iterator(modifiers.end()));
1467 };
1468 append(normFactors);
1469 append(normSys);
1470 append(histoSys);
1471 append(shapeSys);
1472 append(custom);
1473 append(temporaries);
1474 if (statError) {
1475 sample.modifiers.push_back(std::move(*statError));
1476 }
1477
1478 channel.dist.samples.push_back(std::move(sample));
1479 }
1480
1481 sortByName(channel.dist.samples);
1482 return channel;
1483}
1484
1485// The statistical uncertainties of the samples are reconstructed from the constraints of the gamma parameters, with
1486// the (arbitrary) convention that all samples have the same relative uncertainty.
1487void configureStatError(ExportedChannel &channel)
1488{
1489 for (HS3::Sample &sample : channel.dist.samples) {
1490 if (std::none_of(sample.modifiers.begin(), sample.modifiers.end(),
1491 [](auto const &m) { return std::holds_alternative<HS3::StatError>(m); })) {
1492 continue;
1493 }
1494 std::vector<double> errors(sample.contents.size());
1495 for (auto const &bin : channel.relErrors) {
1496 const int i = bin.first;
1497 // this reconstruction is inherently imprecise, so we truncate it at some decimal places to make sure that
1498 // we don't carry around too many useless digits
1499 errors[i - 1] =
1500 round_prec(bin.second * channel.totYield[i] / std::sqrt(channel.totYield2[i]) * sample.contents[i - 1], 7);
1501 }
1502 sample.errors = std::move(errors);
1503 }
1504}
1505
1506// Whether `pdf` has the form that the HistFactory exporter writes as a histfactory_dist with its constraint terms:
1507// a product of exactly one RooRealSumPdf and other pdfs.
1508bool isHistFactoryChannelProduct(RooAbsArg const &pdf)
1509{
1510 auto const *prod = dynamic_cast<RooProdPdf const *>(&pdf);
1511 if (!prod) {
1512 return false;
1513 }
1514 int nSums = 0;
1515 for (RooAbsArg const *factor : prod->pdfList()) {
1516 if (dynamic_cast<RooRealSumPdf const *>(factor)) {
1517 ++nSums;
1518 }
1519 }
1520 return nSums == 1;
1521}
1522
1523// Whether the constraint `constraint` can be written as an array component: every object of the workspace that uses
1524// it is a HistFactory channel, which refers to it through its modifiers, or an array distribution. Any other user,
1525// e.g. an ordinary product or a ModelConfig that has it as an external constraint, refers to the constraint by its
1526// own name, which an array component doesn't have. The decision only depends on the workspace, not on the order in
1527// which the export visits the objects.
1528bool usedOnlyByHistFactoryChannels(RooJSONFactoryWSTool &tool, RooAbsPdf const &constraint)
1529{
1530 for (TObject const *obj : tool.workspace()->allGenericObjects()) {
1531 auto const *mc = dynamic_cast<RooFit::ModelConfig const *>(obj);
1532 if (mc && mc->GetExternalConstraints() && mc->GetExternalConstraints()->find(constraint.GetName())) {
1533 return false;
1534 }
1535 }
1536 for (RooAbsArg const *client : constraint.clients()) {
1537 if (tool.workspace()->arg(client->GetName()) != client) {
1538 continue; // not part of what is exported
1539 }
1540 if (!client->getAttribute(arrayDistTag) && !isHistFactoryChannelProduct(*client)) {
1541 return false;
1542 }
1543 }
1544 return true;
1545}
1546
1547// Register the constraint `constraint` of a modifier parameter for the export and return the name under which it is
1548// written. Constraints are collected into one array distribution per type and top-level pdf (or, if the constraint
1549// already belongs to an array distribution, that one), unless the arrays are switched off, the constraint cannot be
1550// a component, or it is used by objects other than HistFactory channels. In those cases it is written on its own.
1551std::string exportConstraint(RooJSONFactoryWSTool &tool, RooAbsPdf const &constraint)
1552{
1553 if (RooJSONFactoryWSTool::config().exportArrayDist && usedOnlyByHistFactoryChannels(tool, constraint)) {
1554 if (auto info = arrayComponentInfo(constraint)) {
1555 std::string arrayName;
1556 for (RooAbsArg const *client : constraint.clients()) {
1557 if (client->getAttribute(arrayDistTag)) {
1558 arrayName = client->GetName();
1559 break;
1560 }
1561 }
1562 if (arrayName.empty()) {
1563 // "poisson_continuous_dist" -> "<root>_poisson_continuous_constraints"
1564 arrayName = tool.exportRootName() + "_" + removeSuffix(info->type, "_dist") + "_constraints";
1565 }
1566 return tool.exportAsArrayComponent(constraint, arrayName);
1567 }
1568 }
1569 tool.queueExport(constraint);
1570 return constraint.GetName();
1571}
1572
1573// Export the constraints that the modifiers name, in the order in which the modifiers are written, and replace their
1574// names by the names under which they are written: an array distribution that holds them, or their own name. Returns
1575// these terms, for the likelihoods of the models that contain this channel.
1576std::vector<std::string> exportNamedConstraints(RooJSONFactoryWSTool &tool, ExportedChannel &channel)
1577{
1578 std::map<std::string, std::string> writtenAs;
1579 std::vector<std::string> terms;
1580 RooWorkspace &ws = *tool.workspace();
1581 auto rename = [&](std::string &name, std::string const &paramName) {
1582 auto found = writtenAs.find(name);
1583 if (found == writtenAs.end()) {
1584 RooAbsPdf const &constraint = *channel.constraints.at(name);
1585 // HistFactory builds the Gaussian constraints with the parameter as x and the global observable as mean.
1586 // The observable is the side that does not depend on the parameter; recording it makes the Gaussian be
1587 // written with the observable as x.
1588 RooAbsArg const *param = ws.arg(paramName);
1589 if (auto const *gauss = dynamic_cast<RooGaussian const *>(&constraint); gauss && param) {
1590 RooAbsReal const &observable = gauss->getX().dependsOn(*param) ? gauss->getMean() : gauss->getX();
1591 if (!isLiteralConstVar(observable)) {
1592 tool.setConstraintObservable(constraint, observable.GetName());
1593 }
1594 }
1595 found = writtenAs.emplace(name, exportConstraint(tool, constraint)).first;
1596 if (std::find(terms.begin(), terms.end(), found->second) == terms.end()) {
1597 terms.push_back(found->second);
1598 }
1599 }
1600 name = found->second;
1601 };
1602 auto renameConstraint = [&](HS3::Constraint &constraint, HS3::ParameterRef const &param) {
1603 if (auto *pdf = std::get_if<HS3::ConstraintPdf>(&constraint)) {
1604 rename(pdf->pdf.name, param.name);
1605 }
1606 };
1607 auto renameGammas = [&](HS3::GammaConstraint &constraint, std::vector<HS3::ParameterRef> const &params) {
1608 if (auto *pdfs = std::get_if<HS3::GammaConstraintPdfs>(&constraint)) {
1609 for (std::size_t i = 0; i < pdfs->pdfs.size(); ++i) {
1610 if (auto &pdf = pdfs->pdfs[i]) {
1611 rename(pdf->name, i < params.size() ? params[i].name : std::string{});
1612 }
1613 }
1614 }
1615 };
1616 for (HS3::Sample &sample : channel.dist.samples) {
1617 for (HS3::Modifier &modifier : sample.modifiers) {
1618 std::visit(HS3::overloaded{[&](HS3::NormFactor &m) { renameConstraint(m.constraint, m.parameter); },
1619 [&](HS3::NormSys &m) { renameConstraint(m.constraint, m.parameter); },
1620 [&](HS3::HistoSys &m) { renameConstraint(m.constraint, m.parameter); },
1621 [&](HS3::ShapeSys &m) { renameGammas(m.constraint, m.parameters); },
1622 [&](HS3::StatError &m) { renameGammas(m.constraint, m.parameters); },
1623 [](HS3::CustomModifier &) {}},
1624 modifier);
1625 }
1626 }
1627 return terms;
1628}
1629
1630// The modifiers name only the constraint terms of the channel. A pdf of a parameter that is not, like an external
1631// constraint or a prior, stays where it is. Returns the named constraint terms.
1632std::set<RooAbsPdf const *>
1633nameChannelConstraints(ExportedChannel &channel, std::vector<RooAbsPdf *> const &constraints)
1634{
1635 std::set<RooAbsPdf const *> named;
1636 auto isNamed = [&](std::string const &name) {
1637 RooAbsPdf const *pdf = channel.constraints.at(name);
1638 if (std::find(constraints.begin(), constraints.end(), pdf) == constraints.end()) {
1639 return false;
1640 }
1641 named.insert(pdf);
1642 return true;
1643 };
1644 auto filter = [&](HS3::Constraint &constraint) {
1645 auto const *pdf = std::get_if<HS3::ConstraintPdf>(&constraint);
1646 if (pdf && !isNamed(pdf->pdf.name)) {
1647 constraint = HS3::Unconstrained{};
1648 }
1649 };
1650 auto filterGammas = [&](HS3::GammaConstraint &constraint) {
1651 auto *pdfs = std::get_if<HS3::GammaConstraintPdfs>(&constraint);
1652 if (!pdfs) {
1653 return;
1654 }
1655 bool any = false;
1656 for (auto &pdf : pdfs->pdfs) {
1657 if (pdf && !isNamed(pdf->name)) {
1658 pdf.reset();
1659 }
1660 any = any || pdf;
1661 }
1662 // Nothing is written if all parameters are unconstrained.
1663 if (!any) {
1664 constraint = HS3::Unconstrained{};
1665 }
1666 };
1667 for (HS3::Sample &sample : channel.dist.samples) {
1668 for (HS3::Modifier &modifier : sample.modifiers) {
1669 std::visit(HS3::overloaded{
1670 [&](HS3::NormFactor &m) { filter(m.constraint); },
1671 [&](HS3::NormSys &m) { filter(m.constraint); }, [&](HS3::HistoSys &m) { filter(m.constraint); },
1672 [&](HS3::ShapeSys &m) { filterGammas(m.constraint); },
1673 [&](HS3::StatError &m) { filterGammas(m.constraint); }, [](HS3::CustomModifier &) {}},
1674 modifier);
1675 }
1676 }
1677 return named;
1678}
1679
1680bool tryExportHistFactory(RooJSONFactoryWSTool *tool, const std::string &pdfname, const RooRealSumPdf *sumpdf,
1681 std::vector<RooAbsPdf *> constraints)
1682{
1683 // some preliminary checks
1684 if (!sumpdf) {
1685 return false;
1686 }
1687
1688 for (RooAbsArg *sample : sumpdf->funcList()) {
1689 if (!dynamic_cast<RooProduct *>(sample) && !dynamic_cast<RooRealSumPdf *>(sample)) {
1690 return false;
1691 }
1692 }
1693
1694 ExportedChannel channel = readChannel(tool, pdfname, sumpdf, constraints);
1695 HS3::HistFactoryDist &dist = channel.dist;
1696
1697 // sanity checks
1698 if (dist.samples.empty())
1699 return false;
1700 for (auto &sample : dist.samples) {
1701 if (sample.contents.empty()) {
1702 return false;
1703 }
1704 }
1705
1706 HS3::Diagnostics diagnostics;
1707 HS3::mergeDuplicateModifiers(dist, channel.name, diagnostics);
1708 HS3::report(diagnostics);
1709 configureStatError(channel);
1711
1712 for (auto *obs : static_range_cast<RooRealVar *>(*channel.observables)) {
1713 dist.axes.push_back(HS3::toBinnedAxis(*obs));
1714 }
1715
1716 std::set<RooAbsPdf const *> namedConstraints = nameChannelConstraints(channel, constraints);
1717 tool->recordConstraintTerms(pdfname, exportNamedConstraints(*tool, channel));
1718
1719 for (RooAbsReal *modifier : channel.customModifiers) {
1720 tool->queueExport(*modifier);
1721 }
1722 for (auto &modifier : channel.temporaries) {
1723 tool->queueExportTemporary(modifier.release());
1724 }
1725
1726 // Export all model parameters
1727 RooArgSet parameters;
1728 sumpdf->getParameters(channel.observables.get(), parameters);
1729 for (RooAbsArg *param : parameters) {
1730 // This should exclude the global observables. The legacy inverse bin widths are not written at all.
1731 if (!startsWith(std::string{param->GetName()}, "nom_") && !channel.legacyBinWidthFactors.count(param)) {
1732 tool->queueExport(*param);
1733 }
1734 }
1735
1736 // All other constraint terms of the channel are written separately.
1737 std::vector<HS3::DistributionRef> channelConstraintNames;
1738 for (RooAbsPdf const *pdf : constraints) {
1739 if (!namedConstraints.count(pdf)) {
1740 tool->queueExport(*pdf);
1741 channelConstraintNames.push_back({pdf->GetName()});
1742 }
1743 }
1744
1745 HS3::setExportedObject(*tool, std::move(dist), std::move(channelConstraintNames));
1746 return true;
1747}
1748
1749class HistFactoryStreamer_ProdPdf : public RooFit::JSONIO::Exporter {
1750public:
1751 bool autoExportDependants() const override { return false; }
1752 bool tryExport(RooJSONFactoryWSTool *tool, const RooProdPdf *prodpdf) const
1753 {
1754 std::vector<RooAbsPdf *> constraints;
1755 RooRealSumPdf *sumpdf = nullptr;
1756 for (auto *pdf : static_range_cast<RooAbsPdf *>(prodpdf->pdfList())) {
1757 auto thispdf = dynamic_cast<RooRealSumPdf *>(pdf);
1758 if (thispdf) {
1759 if (!sumpdf) {
1760 sumpdf = thispdf;
1761 } else {
1762 return false;
1763 }
1764 } else {
1765 // The constraint terms can also be grouped in nested products.
1766 collectConstraintTerms(pdf, constraints);
1767 }
1768 }
1769 if (!sumpdf)
1770 return false;
1771
1772 return tryExportHistFactory(tool, prodpdf->GetName(), sumpdf, constraints);
1773 }
1774 std::string const &key() const override
1775 {
1776 static const std::string keystring = "histfactory_dist";
1777 return keystring;
1778 }
1779 bool exportObject(RooJSONFactoryWSTool *tool, const RooAbsArg *p, JSONNode &) const override
1780 {
1781 return tryExport(tool, static_cast<const RooProdPdf *>(p));
1782 }
1783};
1784
1785class HistFactoryStreamer_SumPdf : public RooFit::JSONIO::Exporter {
1786public:
1787 bool autoExportDependants() const override { return false; }
1788 bool tryExport(RooJSONFactoryWSTool *tool, const RooRealSumPdf *sumpdf) const
1789 {
1790 std::vector<RooAbsPdf *> constraints;
1791 return tryExportHistFactory(tool, sumpdf->GetName(), sumpdf, constraints);
1792 }
1793 std::string const &key() const override
1794 {
1795 static const std::string keystring = "histfactory_dist";
1796 return keystring;
1797 }
1798 bool exportObject(RooJSONFactoryWSTool *tool, const RooAbsArg *p, JSONNode &) const override
1799 {
1800 return tryExport(tool, static_cast<const RooRealSumPdf *>(p));
1801 }
1802};
1803
1804STATIC_EXECUTE([]() {
1805 using namespace RooFit::JSONIO;
1806
1807 registerImporter<HistFactoryImporter>("histfactory_dist", true);
1808 registerImporter<PiecewiseInterpolationFactory>("interpolation", true);
1809 registerImporter<FlexibleInterpVarFactory>("interpolation0d", true);
1810 registerExporter<FlexibleInterpVarStreamer>(RooStats::HistFactory::FlexibleInterpVar::Class(), true);
1811 registerExporter<PiecewiseInterpolationStreamer>(PiecewiseInterpolation::Class(), true);
1812 registerExporter<HistFactoryStreamer_ProdPdf>(RooProdPdf::Class(), true);
1813 registerExporter<HistFactoryStreamer_SumPdf>(RooRealSumPdf::Class(), true);
1814});
1815
1816} // namespace
1817
1819 std::vector<DistributionRef> const *channelConstraints)
1820{
1821 checkStatErrorConstraints(dist, name);
1822
1823 RooArgSet observables = createObservables(dist.axes);
1824
1825 std::vector<std::unique_ptr<RooDataHist>> data;
1826 data.reserve(dist.samples.size());
1827 std::vector<double> sumW;
1828 std::vector<double> sumW2;
1829 std::vector<ParameterRef> gammaParnames;
1830 for (auto const &sample : dist.samples) {
1831 auto dh = createDataHist(name + "_" + sample.name + "_dataHist", observables, sample.contents,
1832 sample.errors ? &*sample.errors : nullptr);
1833 const std::size_t nbins = dh->numEntries();
1834
1835 auto found = std::find_if(sample.modifiers.begin(), sample.modifiers.end(),
1836 [](auto const &m) { return std::holds_alternative<StatError>(m); });
1837 if (found != sample.modifiers.end()) {
1838 if (sumW.empty()) {
1839 sumW.resize(nbins);
1840 sumW2.resize(nbins);
1841 }
1842 // A sample without "errors" contributes no statistical uncertainty, unless Poisson uncertainties are implied.
1843 const bool hasErrors = sample.errors || dist.statError.impliedPoissonErrors;
1844 for (std::size_t i = 0; i < nbins; ++i) {
1845 sumW[i] += dh->weight(i);
1846 sumW2[i] += hasErrors ? dh->weightSquared(i) : 0.;
1847 }
1848 if (gammaParnames.empty()) {
1849 gammaParnames = std::get<StatError>(*found).parameters;
1850 }
1851 }
1852 data.emplace_back(std::move(dh));
1853 }
1854
1855 std::string channelName = name;
1856 erasePrefix(channelName, "model_");
1857
1858 std::size_t nBins = 1;
1859 for (auto const &axis : dist.axes) {
1860 nBins *= std::visit(overloaded{[](RegularBinning const &b) { return static_cast<std::size_t>(b.nbins); },
1861 [](EdgesBinning const &b) { return b.edges.size() - 1; }},
1862 axis.binning);
1863 }
1864
1865 RooAbsArg *mcStatObject = nullptr;
1866 RooArgSet constraints;
1867 if (!sumW.empty()) {
1868 std::vector<double> errs(sumW.size());
1869 for (std::size_t i = 0; i < sumW.size(); ++i) {
1870 if (sumW[i] == 0.) {
1871 errs[i] = 0.;
1872 continue;
1873 }
1874 errs[i] = std::sqrt(sumW2[i]) / sumW[i];
1875 // avoid negative sigma. This NP will be set constant anyway later
1876 errs[i] = std::max(errs[i], 0.);
1877 }
1878
1879 mcStatObject = &createPHF(tool, "mc_stat_" + channelName, "stat_" + channelName, gammaParnames, errs,
1880 statErrorConstraint(dist), constraints, observables, defaultStatErrorGammaMax,
1881 dist.statError.relThreshold, nBins, "the staterror modifier of channel '" + name + "'");
1882 }
1883
1884 ChannelContext ctx{tool, dist, observables, name, channelName, nBins, mcStatObject, constraints};
1885
1887 RooArgList coefs;
1888 for (std::size_t i = 0; i < dist.samples.size(); ++i) {
1889 HS3::Sample const &sample = dist.samples[i];
1890 buildSample(ctx, sample, *data[i]);
1891 funcs.add(*tool.request<RooAbsReal>(name + "_" + sample.name + "_shapes", name));
1892 coefs.add(*tool.request<RooAbsReal>(name + "_" + sample.name + "_scaleFactors", name));
1893 }
1894
1895 if (channelConstraints) {
1896 for (auto const &ref : *channelConstraints) {
1897 RooAbsPdf *pdf = tool.tryRequest<RooAbsPdf>(ref.name);
1898 if (!pdf) {
1899 RooJSONFactoryWSTool::error("unable to find the constraint '" + ref.name + "' of '" + name + "'");
1900 }
1901 constraints.add(*pdf);
1902 }
1903 }
1904
1905 // A histfactory_dist is a product of Poisson distributions of the bin counts. RooFit's binned likelihood
1906 // computes exactly that (it differs from the event-wise likelihood only by a constant), and binned generation
1907 // generates bin counts, so both are enabled, as HistFactory does by default.
1908 auto enableBinned = [](RooRealSumPdf &sum) {
1909 sum.setAttribute("BinnedLikelihood");
1910 sum.setAttribute("GenerateBinned");
1911 };
1912 if (constraints.empty()) {
1913 enableBinned(tool.wsEmplace<RooRealSumPdf>(name, funcs, coefs, true));
1914 } else {
1915 std::string sumName = name + "_model";
1916 erasePrefix(sumName, "model_");
1917 auto &sum = tool.wsEmplace<RooRealSumPdf>(sumName, funcs, coefs, true);
1918 sum.SetTitle(name.c_str());
1919 enableBinned(sum);
1920 tool.wsEmplace<RooProdPdf>(name, constraints, RooFit::Conditional(sum, observables));
1921 }
1922}
1923
1925{
1926 return dynamic_cast<HistFactoryImporter const *>(&importer) != nullptr;
1927}
1928
1929// The upgrade rewrites the JSON, so it has to work on a copy.
1931 std::vector<DistributionRef> const *channelConstraints)
1932{
1933 std::string name = RooJSONFactoryWSTool::name(p);
1934 if (!p.has_child("samples")) {
1935 RooJSONFactoryWSTool::error("no samples in '" + name + "', skipping.");
1936 }
1937 std::stringstream ss;
1938 p.writeJSON(ss);
1939 std::unique_ptr<RooFit::Detail::JSONTree> tree = RooFit::Detail::JSONTree::create(ss.str());
1940
1941 HS3::Version version;
1942 HS3::parseVersion(tool.hs3VersionInput(), version);
1943
1944 HS3::Diagnostics diagnostics;
1945 HS3::upgradeHistFactoryDist(tree->rootnode(), version, diagnostics);
1946 HS3::HistFactoryDist dist = HS3::parseHistFactoryDist(tree->rootnode(), diagnostics);
1947 if (!HS3::hasErrors(diagnostics)) {
1948 HS3::validateHistFactoryDist(dist, name, diagnostics);
1949 }
1950 HS3::report(diagnostics);
1951 HS3::buildHistFactoryDist(tool, name, dist, channelConstraints);
1952}
std::string channelName
std::optional< ArrayComponentInfo > arrayComponentInfo(RooAbsPdf const &pdf, std::string const *observable)
std::vector< RooAbsReal * > paramHistFuncParamsInBinOrder(ParamHistFunc const &func)
RooArgList toParamHistFuncOrder(RooArgList const &paramsInBinOrder, RooAbsCollection const &vars)
bool startsWith(std::string_view str, std::string_view prefix)
bool endsWith(std::string_view str, std::string_view suffix)
bool isLiteralConstVar(RooAbsArg const &arg)
std::string removeSuffix(std::string_view str, std::string_view suffix)
std::unique_ptr< RooArgSet > histogramVariablesNamedAfterObservables(RooDataHist const &dh, RooArgSet const &histObs, RooArgSet const &funcObs)
void checkParamHistFuncOrderIsDefined(std::string const &hs3Version, RooAbsCollection const &vars, std::string const &context)
constexpr const char * arrayDistTag
Definition JSONIOUtils.h:23
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define e(i)
Definition RSha256.hxx:103
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
double toDouble(const char *s)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void funcs
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t modifier
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
#define hi
A class which maps the current values of a RooRealVar (or a set of RooRealVars) to one of a number of...
const RooArgList & paramList() const
Int_t numBins() const
const RooArgList & dataVars() const
The PiecewiseInterpolation is a class that can morph distributions into each other,...
const RooArgList & highList() const
const RooAbsReal * nominalHist() const
Return pointer to the nominal hist function.
static TClass * Class()
const RooArgList & lowList() const
void setPositiveDefinite(bool flag=true)
const RooArgList & paramList() const
const std::vector< int > & interpolationCodes() const
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.
bool isConstant() const
Check if the "Constant" attribute is set.
Definition RooAbsArg.h:284
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...
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
void treeNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool doBranch=true, bool doLeaf=true, bool valueOnly=false, bool recurseNonDerived=false) const
Fill supplied list with nodes of the arg tree, following all server links, starting with ourself as t...
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
bool overlaps(Iterator_t otherCollBegin, Iterator_t otherCollEnd) const
const_iterator end() const
Storage_t::size_type size() const
const_iterator begin() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
void setConstant(bool value=true)
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
Returns the bin width (or volume) given a RooHistFunc.
Represents a constant real-valued object.
Definition RooConstVar.h:23
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
double const * weightArray() const
double weight(std::size_t i) const
Return weight of i-th bin.
double weightSquared(std::size_t i) const
Return squared weight sum of i-th bin.
static std::unique_ptr< JSONTree > create()
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
RooAbsArg * getParameter(const char *name) const
Return pointer to parameter with given name.
const char * expression() const
const RooArgList & dependents() const
size_t nParameters() const
Return the number of parameters.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
A real-valued function sampled from a multidimensional histogram.
Definition RooHistFunc.h:29
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
void recordConstraintTerms(std::string const &pdfName, std::vector< std::string > const &terms)
T * request(const std::string &objname, const std::string &requestAuthor)
The object objname from the workspace, imported from the document first if needed.
std::string const & exportRootName() const
The name of the top-level object whose export is in progress (the root of the current export recursio...
T * tryRequest(const std::string &objname)
Like request(), but returns nullptr if neither the workspace nor the document has the object.
void queueExportTemporary(RooAbsArg *arg)
void queueExport(RooAbsArg const &arg)
std::string const & hs3VersionInput() const
The "hs3_version" of the document that is currently being imported, or an empty string outside of an ...
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...
Obj_t & wsEmplace(RooStringView name, Args_t &&...args)
static std::string name(const RooFit::Detail::JSONNode &n)
std::string exportAsArrayComponent(RooAbsPdf const &pdf, std::string const &arrayName)
Poisson pdf.
Definition RooPoisson.h:18
RooAbsReal const & getMean() const
Get the mean parameter.
Definition RooPoisson.h:47
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:35
static TClass * Class()
Represents the product of a given set of RooAbsReal objects.
Definition RooProduct.h:29
Implements a PDF constructed from a sum of functions:
const RooArgList & funcList() const
static TClass * Class()
const RooArgList & coefList() const
Variable that can be changed from the outside.
Definition RooRealVar.h:37
bool hasError(bool allowZero=true) const
Definition RooRealVar.h:60
< A class that holds configuration information for a model using a workspace as a store
Definition ModelConfig.h:34
Persistable container for RooFit projects.
TObject * obj(RooStringView name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooAbsArg * arg(RooStringView name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
RooFactoryWSTool & factory()
Return instance to factory tool.
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.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:461
Basic string class.
Definition TString.h:137
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
RooCmdArg RecycleConflictNodes(bool flag=true)
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)
RooConstVar & RooConst(double val)
RooCmdArg Silence(bool flag=true)
double gamma(double x)
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
double dist(Rotation3D const &r1, Rotation3D const &r2)
Types that represent an HS3 document.
HistFactoryDist parseHistFactoryDist(Detail::JSONNode const &distribution, Diagnostics &diagnostics)
Reads a HistFactory distribution that doesn't come with a document.
void validateHistFactoryDist(HistFactoryDist const &dist, std::string const &name, Diagnostics &diagnostics)
Same as validate(), but for a single HistFactory distribution that doesn't come with a document.
void 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 writeInterpolatedFunction(Detail::JSONNode &node, InterpolatedFunction const &function)
Writes the functions "interpolation" and "interpolation0d", with a single interpolation if they are a...
std::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
InterpolationClass
The RooFit classes that implement the interpolations, which encode them with integer codes.
constexpr Interpolation multiplicativePolynomialExponential
constexpr Interpolation additivePolynomialLinear
void mergeDuplicateModifiers(HistFactoryDist &dist, std::string const &channel, Diagnostics &diagnostics)
Combines the modifiers of a sample that have the same type and name, which HistFactory models can hav...
ConstraintType
The constraint types that older documents named, for which RooFit creates the constraint terms.
std::optional< Interpolation > interpolationFromCode(int code, InterpolationClass interpolationClass)
The interpolation for an integer code, or nothing if the class doesn't support the code.
Definition HS3Parser.cxx:64
void factorOutDefaultInterpolation(HistFactoryDist &dist)
Makes the most common interpolation of the modifiers the "default_interpolation", which they then don...
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.
char const * toString(InterpolationType type)
InterpolatedNumber parseInterpolatedNumber(Detail::JSONNode const &node, Diagnostics &diagnostics)
void upgradeHistFactoryDist(Detail::JSONNode &distribution, Version const &version, Diagnostics &diagnostics)
Same as upgradeDocument(), but for a single HistFactory distribution that doesn't come with a documen...
InterpolatedFunction parseInterpolatedFunction(Detail::JSONNode const &node, Diagnostics &diagnostics)
Reads the functions "interpolation" and "interpolation0d", which don't come with a document.
RooArgSet createObservables(std::vector< BinnedAxis > const &axes)
Create the variables for the axes of binned data.
std::optional< int > codeFromInterpolation(Interpolation const &interpolation, InterpolationClass interpolationClass)
The integer code of an interpolation, or nothing if the class can't represent the interpolation.
Definition HS3Parser.cxx:83
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 writeInterpolatedNumber(Detail::JSONNode &node, InterpolatedNumber const &function)
constexpr Interpolation multiplicativePiecewiseExponential
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.
std::variant< Unconstrained, ConstraintType, ConstantGammas, GammaConstraintPdfs > GammaConstraint
bool parseVersion(std::string const &version, Version &out)
Reads the leading "MAJOR.MINOR" and the optional ".PATCH" of an HS3 version string.
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...
std::variant< Unconstrained, ConstraintType, ConstraintPdf > Constraint
bool isBuiltinHistFactoryImporter(JSONIO::Importer const &importer)
Whether this is the importer that RooFit registers for HistFactory distributions, as opposed to the o...
void configureConstrainedGammas(RooArgList const &gammas, std::span< const double > relSigmas, double minSigma)
Configure constrained gamma parameters for fitting.
CreateGammaConstraintsOutput createGammaConstraints(RooArgList const &paramList, std::span< const double > relSigmas, double minSigma, Constraint::Type type)
void variables(TString dataset, TString fin="TMVA.root", TString dirName="InputVariables_Id", TString title="TMVA Input Variables", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
std::string GetName(TCppScope_t)
#define STATIC_EXECUTE(MY_FUNC)
static const char * what
Definition stlLoader.cc:5
The gamma parameters are constant, which is a RooFit extension.
The constraint term is a distribution of the document, named in "constraints".
Multiplies the sample with a function of the document.
Every gamma parameter has its own constraint term, or none, as named in "constraints".
std::vector< std::optional< DistributionRef > > pdfs
std::optional< Interpolation > defaultInterpolation
std::vector< Sample > samples
The function "interpolation" of RooFit, implemented by PiecewiseInterpolation: a nominal function plu...
std::vector< Interpolation > interpolations
One per parameter.
std::vector< NameOrNumber > parameters
std::vector< NameOrNumber > low
std::vector< NameOrNumber > high
The function "interpolation0d" of RooFit, implemented by FlexibleInterpVar: a nominal value plus the ...
std::vector< double > high
std::vector< double > low
std::vector< std::string > parameters
std::vector< Interpolation > interpolations
One per parameter.
How a modifier of a HistFactory distribution interpolates and extrapolates its effect.
InterpolationType type
InterpolationFunction in
std::optional< InterpolationFunction > out
If not set, the function inside the bounds is used outside as well.
std::vector< double > contents
std::optional< std::vector< double > > errors
std::vector< Modifier > modifiers
Scales each bin by its own parameter. The modifier type "shapesys" is a deprecated alias of "shapefac...
Scales each bin by a parameter, that is constrained by the statistical uncertainty of all samples tha...
The parameter of the modifier is not constrained.
Combines lambdas into one visitor for std::visit(). The compiler complains about the alternatives wit...
Definition HS3Document.h:55
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335