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
36#include <regex>
37
38#include "static_execute.h"
39#include "JSONIOUtils.h"
40
42
43using namespace RooStats::HistFactory;
44using namespace RooStats::HistFactory::Detail;
46
47namespace {
48
49inline void writeAxis(JSONNode &axis, RooRealVar const &obs)
50{
51 auto &binning = obs.getBinning();
52 if (binning.isUniform()) {
53 axis["nbins"] << obs.numBins();
54 axis["min"] << obs.getMin();
55 axis["max"] << obs.getMax();
56 } else {
57 auto &edges = axis["edges"];
58 edges.set_seq();
59 double val = binning.binLow(0);
60 edges.append_child() << val;
61 for (int i = 0; i < binning.numBins(); ++i) {
62 val = binning.binHigh(i);
63 edges.append_child() << val;
64 }
65 }
66}
67
68double round_prec(double d, int nSig)
69{
70 if (d == 0.0)
71 return 0.0;
72 int ndigits = std::floor(std::log10(std::abs(d))) + 1 - nSig;
73 double sf = std::pow(10, ndigits);
74 if (std::abs(d / sf) < 2)
75 ndigits--;
76 return sf * std::round(d / sf);
77}
78
79// To avoid repeating the same string literals that can potentially get out of
80// sync.
81namespace Literals {
82constexpr auto staterror = "staterror";
83}
84
85void erasePrefix(std::string &str, std::string_view prefix)
86{
87 if (startsWith(str, prefix)) {
88 str.erase(0, prefix.size());
89 }
90}
91
92bool eraseSuffix(std::string &str, std::string_view suffix)
93{
94 if (endsWith(str, suffix)) {
95 str.erase(str.size() - suffix.size());
96 return true;
97 } else {
98 return false;
99 }
100}
101
102template <class Coll>
103void sortByName(Coll &coll)
104{
105 std::sort(coll.begin(), coll.end(), [](auto &l, auto &r) { return l.name < r.name; });
106}
107
108template <class T>
109T *findClient(RooAbsArg *gamma)
110{
111 for (const auto &client : gamma->clients()) {
112 if (auto casted = dynamic_cast<T *>(client)) {
113 return casted;
114 } else {
115 T *c = findClient<T>(client);
116 if (c)
117 return c;
118 }
119 }
120 return nullptr;
121}
122
124{
125 if (!g)
126 return nullptr;
128 if (constraint_p)
129 return constraint_p;
131 if (constraint_g)
132 return constraint_g;
134 if (constraint_l)
135 return constraint_l;
136 return nullptr;
137}
138
139std::string toString(TClass *c)
140{
141 if (!c) {
142 return "Const";
143 }
144 if (c == RooPoisson::Class()) {
145 return "Poisson";
146 }
147 if (c == RooGaussian::Class()) {
148 return "Gauss";
149 }
150 if (c == RooLognormal::Class()) {
151 return "Lognormal";
152 }
153 return "unknown";
154}
155
156inline std::string defaultGammaName(std::string const &sysname, std::size_t i)
157{
158 return "gamma_" + sysname + "_bin_" + std::to_string(i);
159}
160
161/// Export the names of the gamma parameters to the modifier struct if the
162/// names don't match the default gamma parameter names, which is gamma_<sysname>_bin_<i>
163void optionallyExportGammaParameters(JSONNode &mod, std::string const &sysname, std::vector<RooAbsReal *> const &params,
164 bool forceExport = true)
165{
166 std::vector<std::string> paramNames;
167 bool needExport = forceExport;
168 for (std::size_t i = 0; i < params.size(); ++i) {
169 std::string name(params[i]->GetName());
170 paramNames.push_back(name);
171 if (name != defaultGammaName(sysname, i)) {
172 needExport = true;
173 }
174 }
175 if (needExport) {
176 mod["parameters"].fill_seq(paramNames);
177 }
178}
179
180RooRealVar &createNominal(RooWorkspace &ws, std::string const &parname, double val, double min, double max)
181{
182 RooRealVar &nom = getOrCreate<RooRealVar>(ws, "nom_" + parname, val, min, max);
183 nom.setConstant(true);
184 return nom;
185}
186
187/// Get the conventional name of the constraint pdf for a constrained
188/// parameter.
189std::string constraintName(std::string const &paramName)
190{
191 return paramName + "Constraint";
192}
193
194ParamHistFunc &createPHF(const std::string &phfname, std::string const &sysname,
195 const std::vector<std::string> &parnames, const std::vector<double> &vals,
196 RooJSONFactoryWSTool &tool, RooAbsCollection &constraints, const RooArgSet &observables,
197 const std::string &constraintType, double gammaMin, double gammaMax, double minSigma)
198{
199 RooWorkspace &ws = *tool.workspace();
200
201 size_t n = std::max(vals.size(), parnames.size());
203 for (std::size_t i = 0; i < n; ++i) {
204 const std::string name = parnames.empty() ? defaultGammaName(sysname, i) : parnames[i];
205 auto *e = dynamic_cast<RooAbsReal *>(ws.obj(name.c_str()));
206 if (e)
207 gammas.add(*e);
208 else
210 }
211
212 auto &phf = tool.wsEmplace<ParamHistFunc>(phfname, observables, gammas);
213
214 if (vals.size() > 0) {
215 if (constraintType != "Const") {
217 gammas, vals, minSigma, constraintType == "Poisson" ? Constraint::Poisson : Constraint::Gaussian);
218 for (auto const &term : constraintsInfo.constraints) {
220 constraints.add(*ws.pdf(term->GetName()));
221 }
222 } else {
223 for (auto *gamma : static_range_cast<RooRealVar *>(gammas)) {
224 gamma->setConstant(true);
225 }
226 }
227 }
228
229 return phf;
230}
231
232bool hasStaterror(const JSONNode &comp)
233{
234 if (!comp.has_child("modifiers"))
235 return false;
236 for (const auto &mod : comp["modifiers"].children()) {
237 if (mod["type"].val() == ::Literals::staterror)
238 return true;
239 }
240 return false;
241}
242
243const JSONNode &findStaterror(const JSONNode &comp)
244{
245 if (comp.has_child("modifiers")) {
246 for (const auto &mod : comp["modifiers"].children()) {
247 if (mod["type"].val() == ::Literals::staterror)
248 return mod;
249 }
250 }
251 RooJSONFactoryWSTool::error("sample '" + RooJSONFactoryWSTool::name(comp) + "' does not have a " +
252 ::Literals::staterror + " modifier!");
253}
254
255RooAbsPdf &
256getOrCreateConstraint(RooJSONFactoryWSTool &tool, const JSONNode &mod, RooRealVar &param, const std::string &sample)
257{
258 if (auto constrName = mod.find("constraint_name")) {
259 auto constraint_name = constrName->val();
260 auto constraint = tool.workspace()->pdf(constraint_name);
261 if (!constraint) {
262 constraint = tool.request<RooAbsPdf>(constrName->val(), sample);
263 }
264 if (!constraint) {
265 RooJSONFactoryWSTool::error("unable to find definition of of constraint '" + constraint_name +
266 "' for modifier '" + RooJSONFactoryWSTool::name(mod) + "'");
267 }
268 if (auto gauss = dynamic_cast<RooGaussian *const>(constraint)) {
269 param.setError(gauss->getSigma().getVal());
270 }
271 return *constraint;
272 } else {
273 std::string constraint_type = "Gauss";
274 if (auto constrType = mod.find("constraint_type")) {
276 }
277 if (constraint_type == "Gauss") {
278 param.setError(1.0);
279 return getOrCreate<RooGaussian>(*tool.workspace(), constraintName(param.GetName()), param,
280 *tool.workspace()->var(std::string("nom_") + param.GetName()), 1.);
281 }
282 RooJSONFactoryWSTool::error("unknown or invalid constraint for modifier '" + RooJSONFactoryWSTool::name(mod) +
283 "'");
284 }
285}
286
288 RooAbsArg const *mcStatObject, const std::string &fprefix, const JSONNode &p,
289 RooArgSet &constraints)
290{
291 RooWorkspace &ws = *tool.workspace();
292
294 std::string prefixedName = fprefix + "_" + sampleName;
295
296 std::string channelName = fprefix;
297 erasePrefix(channelName, "model_");
298
299 if (!p.has_child("data")) {
300 RooJSONFactoryWSTool::error("sample '" + sampleName + "' does not define a 'data' key");
301 }
302
303 auto &hf = tool.wsEmplace<RooHistFunc>("hist_" + prefixedName, varlist, dh);
304 hf.SetTitle(RooJSONFactoryWSTool::name(p).c_str());
305
308
309 shapeElems.add(tool.wsEmplace<RooBinWidthFunction>(prefixedName + "_binWidth", hf, true));
310
311 if (hasStaterror(p)) {
313 }
314
315 if (p.has_child("modifiers")) {
317 std::vector<double> overall_low;
318 std::vector<double> overall_high;
319 std::vector<int> overall_interp;
320
324
325 int idx = 0;
326 for (const auto &mod : p["modifiers"].children()) {
327 std::string const &modtype = mod["type"].val();
328 std::string const &sysname =
329 mod.has_child("name")
330 ? mod["name"].val()
331 : (mod.has_child("parameter") ? mod["parameter"].val() : "syst_" + std::to_string(idx));
332 ++idx;
333 if (modtype == "staterror") {
334 // this is dealt with at a different place, ignore it for now
335 } else if (modtype == "normfactor") {
338 if (mod.has_child("constraint_name") || mod.has_child("constraint_type")) {
339 // for norm factors, constraints are optional
341 }
342 } else if (modtype == "normsys") {
343 auto *parameter = mod.find("parameter");
344 std::string parname(parameter ? parameter->val() : "alpha_" + sysname);
345 createNominal(ws, parname, 0.0, -10, 10);
346 auto &par = getOrCreate<RooRealVar>(ws, parname, 0., -5, 5);
347 overall_nps.add(par);
348 auto &data = mod["data"];
349 int interp = 4;
350 if (mod.has_child("interpolation")) {
351 interp = mod["interpolation"].val_int();
352 }
353 double low = data["lo"].val_double();
354 double high = data["hi"].val_double();
355
356 // the below contains a a hack to cut off variations that go below 0
357 // this is needed because with interpolation code 4, which is the default, interpolation is done in
358 // log-space. hence, values <= 0 result in NaN which propagate throughout the model and cause evaluations to
359 // fail if you know a nicer way to solve this, please go ahead and fix the lines below
360 if (interp == 4 && low <= 0)
361 low = std::numeric_limits<double>::epsilon();
362 if (interp == 4 && high <= 0)
363 high = std::numeric_limits<double>::epsilon();
364
365 overall_low.push_back(low);
366 overall_high.push_back(high);
367 overall_interp.push_back(interp);
368
369 constraints.add(getOrCreateConstraint(tool, mod, par, sampleName));
370 } else if (modtype == "histosys") {
371 auto *parameter = mod.find("parameter");
372 std::string parname(parameter ? parameter->val() : "alpha_" + sysname);
373 createNominal(ws, parname, 0.0, -10, 10);
374 auto &par = getOrCreate<RooRealVar>(ws, parname, 0., -5, 5);
375 histNps.add(par);
376 auto &data = mod["data"];
377 histoLo.add(tool.wsEmplace<RooHistFunc>(
378 sysname + "Low_" + prefixedName, varlist,
380 histoHi.add(tool.wsEmplace<RooHistFunc>(
381 sysname + "High_" + prefixedName, varlist,
382 RooJSONFactoryWSTool::readBinnedData(data["hi"], sysname + "High_" + prefixedName, varlist)));
383 constraints.add(getOrCreateConstraint(tool, mod, par, sampleName));
384 } else if (modtype == "shapesys" || modtype == "shapefactor") {
385 std::string funcName = channelName + "_" + sysname + "_ShapeSys";
386 // funcName should be "<channel_name>_<sysname>_ShapeSys"
387 std::vector<double> vals;
388 if (mod["data"].has_child("vals")) {
389 for (const auto &v : mod["data"]["vals"].children()) {
390 vals.push_back(v.val_double());
391 }
392 }
393 std::vector<std::string> parnames;
394 for (const auto &v : mod["parameters"].children()) {
395 parnames.push_back(v.val());
396 }
397 if (vals.empty() && parnames.empty()) {
398 RooJSONFactoryWSTool::error("unable to instantiate shapesys '" + sysname +
399 "' with neither values nor parameters!");
400 }
401 std::string constraint(mod.has_child("constraint_type") ? mod["constraint_type"].val()
402 : mod.has_child("constraint") ? mod["constraint"].val()
403 : "unknown");
404 shapeElems.add(createPHF(funcName, sysname, parnames, vals, tool, constraints, varlist, constraint,
406 } else if (modtype == "custom") {
407 RooAbsReal *obj = ws.function(sysname);
408 if (!obj) {
409 RooJSONFactoryWSTool::error("unable to find custom modifier '" + sysname + "'");
410 }
411 if (obj->dependsOn(varlist)) {
412 shapeElems.add(*obj);
413 } else {
414 normElems.add(*obj);
415 }
416 } else {
417 RooJSONFactoryWSTool::error("modifier '" + sysname + "' of unknown type '" + modtype + "'");
418 }
419 }
420
421 std::string interpName = sampleName + "_" + channelName + "_epsilon";
422 if (!overall_nps.empty()) {
425 normElems.add(v);
426 }
427 if (!histNps.empty()) {
428 auto &v = tool.wsEmplace<PiecewiseInterpolation>("histoSys_" + prefixedName, hf, histoLo, histoHi, histNps);
430 v.setAllInterpCodes(4); // default interpCode for HistFactory
431 shapeElems.add(v);
432 } else {
433 shapeElems.add(hf);
434 }
435 }
436
437 tool.wsEmplace<RooProduct>(prefixedName + "_shapes", shapeElems);
438 if (!normElems.empty()) {
439 tool.wsEmplace<RooProduct>(prefixedName + "_scaleFactors", normElems);
440 } else {
441 ws.factory("RooConstVar::" + prefixedName + "_scaleFactors(1.)");
442 }
443
444 return true;
445}
446
447class HistFactoryImporter : public RooFit::JSONIO::Importer {
448public:
449 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
450 {
451 std::string name = RooJSONFactoryWSTool::name(p);
452 if (!p.has_child("samples")) {
453 RooJSONFactoryWSTool::error("no samples in '" + name + "', skipping.");
454 }
455 double statErrThresh = 0;
456 std::string statErrType = "Poisson";
457 if (p.has_child(::Literals::staterror)) {
458 auto &staterr = p[::Literals::staterror];
459 if (staterr.has_child("relThreshold"))
460 statErrThresh = staterr["relThreshold"].val_double();
461 if (staterr.has_child("constraint_type"))
462 statErrType = staterr["constraint_type"].val();
463 }
464 std::vector<double> sumW;
465 std::vector<double> sumW2;
466 std::vector<std::string> gammaParnames;
468
469 std::string fprefix = name;
470
471 std::vector<std::unique_ptr<RooDataHist>> data;
472 for (const auto &comp : p["samples"].children()) {
473 std::unique_ptr<RooDataHist> dh = RooJSONFactoryWSTool::readBinnedData(
474 comp["data"], fprefix + "_" + RooJSONFactoryWSTool::name(comp) + "_dataHist", observables);
475 size_t nbins = dh->numEntries();
476
477 if (hasStaterror(comp)) {
478 if (sumW.empty()) {
479 sumW.resize(nbins);
480 sumW2.resize(nbins);
481 }
482 for (size_t i = 0; i < nbins; ++i) {
483 sumW[i] += dh->weight(i);
484 sumW2[i] += dh->weightSquared(i);
485 }
486 if (gammaParnames.empty()) {
487 if (auto staterrorParams = findStaterror(comp).find("parameters")) {
488 for (const auto &v : staterrorParams->children()) {
489 gammaParnames.push_back(v.val());
490 }
491 }
492 }
493 }
494 data.emplace_back(std::move(dh));
495 }
496
497 RooAbsArg *mcStatObject = nullptr;
498 RooArgSet constraints;
499 if (!sumW.empty()) {
500 std::string channelName = name;
501 erasePrefix(channelName, "model_");
502
503 std::vector<double> errs(sumW.size());
504 for (size_t i = 0; i < sumW.size(); ++i) {
505 errs[i] = std::sqrt(sumW2[i]) / sumW[i];
506 // avoid negative sigma. This NP will be set constant anyway later
507 errs[i] = std::max(errs[i], 0.);
508 }
509
511 &createPHF("mc_stat_" + channelName, "stat_" + channelName, gammaParnames, errs, *tool, constraints,
513 }
514
515 int idx = 0;
517 RooArgList coefs;
518 for (const auto &comp : p["samples"].children()) {
519 importHistSample(*tool, *data[idx], observables, mcStatObject, fprefix, comp, constraints);
520 ++idx;
521
522 std::string const &compName = RooJSONFactoryWSTool::name(comp);
523 funcs.add(*tool->request<RooAbsReal>(fprefix + "_" + compName + "_shapes", name));
524 coefs.add(*tool->request<RooAbsReal>(fprefix + "_" + compName + "_scaleFactors", name));
525 }
526
527 if (constraints.empty()) {
528 tool->wsEmplace<RooRealSumPdf>(name, funcs, coefs, true);
529 } else {
530 std::string sumName = name + "_model";
531 erasePrefix(sumName, "model_");
532 auto &sum = tool->wsEmplace<RooRealSumPdf>(sumName, funcs, coefs, true);
533 sum.SetTitle(name.c_str());
534 tool->wsEmplace<RooProdPdf>(name, constraints, RooFit::Conditional(sum, observables));
535 }
536 return true;
537 }
538};
539
540class FlexibleInterpVarStreamer : public RooFit::JSONIO::Exporter {
541public:
542 std::string const &key() const override
543 {
544 static const std::string keystring = "interpolation0d";
545 return keystring;
546 }
547 bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override
548 {
549 auto fip = static_cast<const RooStats::HistFactory::FlexibleInterpVar *>(func);
550 elem["type"] << key();
551 elem["interpolationCodes"].fill_seq(fip->interpolationCodes());
552 RooJSONFactoryWSTool::fillSeq(elem["vars"], fip->variables());
553 elem["nom"] << fip->nominal();
554 elem["high"].fill_seq(fip->high(), fip->variables().size());
555 elem["low"].fill_seq(fip->low(), fip->variables().size());
556 return true;
557 }
558};
559
560class PiecewiseInterpolationStreamer : public RooFit::JSONIO::Exporter {
561public:
562 std::string const &key() const override
563 {
564 static const std::string keystring = "interpolation";
565 return keystring;
566 }
567 bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override
568 {
569 const PiecewiseInterpolation *pip = static_cast<const PiecewiseInterpolation *>(func);
570 elem["type"] << key();
571 elem["interpolationCodes"].fill_seq(pip->interpolationCodes());
572 elem["positiveDefinite"] << pip->positiveDefinite();
573 RooJSONFactoryWSTool::fillSeq(elem["vars"], pip->paramList());
574 elem["nom"] << pip->nominalHist()->GetName();
575 RooJSONFactoryWSTool::fillSeq(elem["high"], pip->highList(), pip->paramList().size());
576 RooJSONFactoryWSTool::fillSeq(elem["low"], pip->lowList(), pip->paramList().size());
577 return true;
578 }
579};
580
581class PiecewiseInterpolationFactory : public RooFit::JSONIO::Importer {
582public:
583 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
584 {
585 std::string name(RooJSONFactoryWSTool::name(p));
586
587 RooArgList vars{tool->requestArgList<RooAbsReal>(p, "vars")};
588
589 auto &pip = tool->wsEmplace<PiecewiseInterpolation>(name, *tool->requestArg<RooAbsReal>(p, "nom"),
590 tool->requestArgList<RooAbsReal>(p, "low"),
591 tool->requestArgList<RooAbsReal>(p, "high"), vars);
592
593 pip.setPositiveDefinite(p["positiveDefinite"].val_bool());
594
595 if (p.has_child("interpolationCodes")) {
596 std::size_t i = 0;
597 for (auto const &node : p["interpolationCodes"].children()) {
598 pip.setInterpCode(*static_cast<RooAbsReal *>(vars.at(i)), node.val_int(), true);
599 ++i;
600 }
601 }
602
603 return true;
604 }
605};
606
607class FlexibleInterpVarFactory : public RooFit::JSONIO::Importer {
608public:
609 bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
610 {
611 std::string name(RooJSONFactoryWSTool::name(p));
612 if (!p.has_child("high")) {
613 RooJSONFactoryWSTool::error("no high variations of '" + name + "'");
614 }
615 if (!p.has_child("low")) {
616 RooJSONFactoryWSTool::error("no low variations of '" + name + "'");
617 }
618 if (!p.has_child("nom")) {
619 RooJSONFactoryWSTool::error("no nominal variation of '" + name + "'");
620 }
621
622 double nom(p["nom"].val_double());
623
624 RooArgList vars{tool->requestArgList<RooRealVar>(p, "vars")};
625
626 std::vector<double> high;
627 high << p["high"];
628
629 std::vector<double> low;
630 low << p["low"];
631
632 if (vars.size() != low.size() || vars.size() != high.size()) {
633 RooJSONFactoryWSTool::error("FlexibleInterpVar '" + name +
634 "' has non-matching lengths of 'vars', 'high' and 'low'!");
635 }
636
637 auto &fip = tool->wsEmplace<RooStats::HistFactory::FlexibleInterpVar>(name, vars, nom, low, high);
638
639 if (p.has_child("interpolationCodes")) {
640 size_t i = 0;
641 for (auto const &node : p["interpolationCodes"].children()) {
642 fip.setInterpCode(*static_cast<RooAbsReal *>(vars.at(i)), node.val_int());
643 ++i;
644 }
645 }
646
647 return true;
648 }
649};
650
651struct NormFactor {
652 std::string name;
653 RooAbsReal const *param = nullptr;
654 RooAbsPdf const *constraint = nullptr;
655 TClass *constraintType = RooGaussian::Class();
656 NormFactor(RooAbsReal const &par, const RooAbsPdf *constr = nullptr)
657 : name{par.GetName()}, param{&par}, constraint{constr}
658 {
659 }
660};
661
662struct NormSys {
663 std::string name = "";
664 RooAbsReal const *param = nullptr;
665 double low = 1.;
666 double high = 1.;
667 int interpolationCode = 4;
668 RooAbsPdf const *constraint = nullptr;
669 TClass *constraintType = RooGaussian::Class();
670 NormSys() {};
671 NormSys(const std::string &n, RooAbsReal *const p, double h, double l, int i, const RooAbsPdf *c)
672 : name(n), param(p), low(l), high(h), interpolationCode(i), constraint(c), constraintType(c->IsA())
673 {
674 }
675};
676
677struct HistoSys {
678 std::string name;
679 RooAbsReal const *param = nullptr;
680 std::vector<double> low;
681 std::vector<double> high;
682 RooAbsPdf const *constraint = nullptr;
683 TClass *constraintType = RooGaussian::Class();
684 HistoSys(const std::string &n, RooAbsReal *const p, RooHistFunc *l, RooHistFunc *h, const RooAbsPdf *c)
685 : name(n), param(p), constraint(c), constraintType(c->IsA())
686 {
687 low.assign(l->dataHist().weightArray(), l->dataHist().weightArray() + l->dataHist().numEntries());
688 high.assign(h->dataHist().weightArray(), h->dataHist().weightArray() + h->dataHist().numEntries());
689 }
690};
691struct ShapeSys {
692 std::string name;
693 std::vector<double> constraints;
694 std::vector<RooAbsReal *> parameters;
695 RooAbsPdf const *constraint = nullptr;
696 TClass *constraintType = RooGaussian::Class();
697 ShapeSys(const std::string &n) : name{n} {}
698};
699
700struct GenericElement {
701 std::string name;
702 RooAbsReal *function = nullptr;
703 GenericElement(RooAbsReal *e) : name(e->GetName()), function(e) {};
704};
705
706std::string stripOuterParens(const std::string &s)
707{
708 size_t start = 0;
709 size_t end = s.size();
710
711 while (start < end && s[start] == '(' && s[end - 1] == ')') {
712 int depth = 0;
713 bool balanced = true;
714 for (size_t i = start; i < end - 1; ++i) {
715 if (s[i] == '(')
716 ++depth;
717 else if (s[i] == ')')
718 --depth;
719 if (depth == 0 && i < end - 1) {
720 balanced = false;
721 break;
722 }
723 }
724 if (balanced) {
725 ++start;
726 --end;
727 } else {
728 break;
729 }
730 }
731 return s.substr(start, end - start);
732}
733
734std::vector<std::string> splitTopLevelProduct(const std::string &expr)
735{
736 std::vector<std::string> parts;
737 int depth = 0;
738 size_t start = 0;
739 bool foundTopLevelStar = false;
740
741 for (size_t i = 0; i < expr.size(); ++i) {
742 char c = expr[i];
743 if (c == '(') {
744 ++depth;
745 } else if (c == ')') {
746 --depth;
747 } else if (c == '*' && depth == 0) {
748 foundTopLevelStar = true;
749 std::string sub = expr.substr(start, i - start);
750 parts.push_back(stripOuterParens(sub));
751 start = i + 1;
752 }
753 }
754
755 if (!foundTopLevelStar) {
756 return {}; // Not a top-level product
757 }
758
759 std::string sub = expr.substr(start);
760 parts.push_back(stripOuterParens(sub));
761 return parts;
762}
763
764#include <regex>
765#include <string>
766#include <cctype>
767#include <cstdlib>
768#include <iostream>
769
770NormSys parseOverallModifierFormula(const std::string &s, RooFormulaVar *formula)
771{
772 static const std::regex pattern(
773 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*$)");
774
775 NormSys sys;
776 double sign = 1.0;
777
778 std::smatch match;
779 if (std::regex_match(s, match, pattern)) {
780 if (match[1].str() == "-") {
781 sign = -1.0;
782 }
783
784 std::string token2 = match[2].str();
785 std::string token3 = match[4].str();
786
787 RooAbsReal *p2 = static_cast<RooAbsReal *>(formula->getParameter(token2.c_str()));
788 RooAbsReal *p3 = static_cast<RooAbsReal *>(formula->getParameter(token3.c_str()));
789 RooRealVar *v2 = dynamic_cast<RooRealVar *>(p2);
790 RooRealVar *v3 = dynamic_cast<RooRealVar *>(p3);
791
792 auto *constr2 = findConstraint(v2);
793 auto *constr3 = findConstraint(v3);
794
795 if (constr2 && !p3) {
796 sys.name = p2->GetName();
797 sys.param = p2;
798 sys.high = sign * toDouble(token3);
799 sys.low = -sign * toDouble(token3);
800 } else if (!p2 && constr3) {
801 sys.name = p3->GetName();
802 sys.param = p3;
803 sys.high = sign * toDouble(token2);
804 sys.low = -sign * toDouble(token2);
805 } else if (constr2 && p3 && !constr3) {
806 sys.name = v2->GetName();
807 sys.param = v2;
808 sys.high = sign * p3->getVal();
809 sys.low = -sign * p3->getVal();
810 } else if (p2 && !constr2 && constr3) {
811 sys.name = v3->GetName();
812 sys.param = v3;
813 sys.high = sign * p2->getVal();
814 sys.low = -sign * p2->getVal();
815 }
816
817 // interpolation code 1 means linear, which is what we have here
818 sys.interpolationCode = 1;
819
820 erasePrefix(sys.name, "alpha_");
821 }
822 return sys;
823}
824
825void collectElements(RooArgSet &elems, RooAbsArg *arg)
826{
827 if (auto prod = dynamic_cast<RooProduct *>(arg)) {
828 for (const auto &e : prod->components()) {
829 collectElements(elems, e);
830 }
831 } else {
832 elems.add(*arg);
833 }
834}
835
836bool allRooRealVar(const RooAbsCollection &list)
837{
838 for (auto *var : list) {
839 if (!dynamic_cast<RooRealVar *>(var)) {
840 return false;
841 }
842 }
843 return true;
844}
845
846struct Sample {
847 std::string name;
848 std::vector<double> hist;
849 std::vector<double> histError;
850 std::vector<NormFactor> normfactors;
851 std::vector<NormSys> normsys;
852 std::vector<HistoSys> histosys;
853 std::vector<ShapeSys> shapesys;
854 std::vector<GenericElement> tmpElements;
855 std::vector<GenericElement> otherElements;
856 bool useBarlowBeestonLight = false;
857 std::vector<RooAbsReal *> staterrorParameters;
858 TClass *barlowBeestonLightConstraintType = RooPoisson::Class();
859 Sample(const std::string &n) : name{n} {}
860};
861
862void addNormFactor(RooRealVar const *par, Sample &sample, RooWorkspace *ws)
863{
864 std::string parname = par->GetName();
865 bool isConstrained = false;
866 for (RooAbsArg const *pdf : ws->allPdfs()) {
867 if (auto gauss = dynamic_cast<RooGaussian const *>(pdf)) {
868 if (parname == gauss->getX().GetName()) {
869 sample.normfactors.emplace_back(*par, gauss);
870 isConstrained = true;
871 }
872 }
873 }
874 if (!isConstrained)
875 sample.normfactors.emplace_back(*par);
876}
877
878namespace {
879
880bool verbose = false;
881
882}
883
884struct Channel {
885 std::string name;
886 std::vector<Sample> samples;
887 std::map<int, double> tot_yield;
888 std::map<int, double> tot_yield2;
889 std::map<int, double> rel_errors;
890 RooArgSet const *varSet = nullptr;
891 long unsigned int nBins = 0;
892};
893
895{
896 Channel channel;
897
898 RooWorkspace *ws = tool->workspace();
899
900 channel.name = pdfname;
901 erasePrefix(channel.name, "model_");
902 eraseSuffix(channel.name, "_model");
903
904 for (size_t sampleidx = 0; sampleidx < sumpdf->funcList().size(); ++sampleidx) {
905 PiecewiseInterpolation *pip = nullptr;
906 std::vector<ParamHistFunc *> phfs;
907
908 const auto func = sumpdf->funcList().at(sampleidx);
909 Sample sample(func->GetName());
910 erasePrefix(sample.name, "L_x_");
911 eraseSuffix(sample.name, "_shapes");
912 eraseSuffix(sample.name, "_" + channel.name);
913 erasePrefix(sample.name, pdfname + "_");
914
915 auto updateObservables = [&](RooDataHist const &dataHist) {
916 if (channel.varSet == nullptr) {
917 channel.varSet = dataHist.get();
918 channel.nBins = dataHist.numEntries();
919 }
920 if (sample.hist.empty()) {
921 auto *w = dataHist.weightArray();
922 sample.hist.assign(w, w + dataHist.numEntries());
923 }
924 };
925 auto processElements = [&](const auto &elements, auto &&self) -> void {
926 for (RooAbsArg *e : elements) {
927 if (TString(e->GetName()).Contains("binWidth")) {
928 // The bin width modifiers are handled separately. We can't just
929 // check for the RooBinWidthFunction type here, because prior to
930 // ROOT 6.26, the multiplication with the inverse bin width was
931 // done in a different way (like a normfactor with a RooRealVar,
932 // but it was stored in the dataset).
933 // Fortunately, the name was similar, so we can match the modifier
934 // name.
935 } else if (auto constVar = dynamic_cast<RooConstVar *>(e)) {
936 if (constVar->getVal() != 1.) {
937 sample.normfactors.emplace_back(*constVar);
938 }
939 } else if (auto par = dynamic_cast<RooRealVar *>(e)) {
940 addNormFactor(par, sample, ws);
941 } else if (auto hf = dynamic_cast<const RooHistFunc *>(e)) {
942 updateObservables(hf->dataHist());
943 } else if (ParamHistFunc *phf = dynamic_cast<ParamHistFunc *>(e); phf && allRooRealVar(phf->paramList())) {
944 phfs.push_back(phf);
945 } else if (auto fip = dynamic_cast<RooStats::HistFactory::FlexibleInterpVar *>(e)) {
946 // some (modified) histfactory models have several instances of FlexibleInterpVar
947 // we collect and merge them
948 for (size_t i = 0; i < fip->variables().size(); ++i) {
949 RooAbsReal *var = static_cast<RooAbsReal *>(fip->variables().at(i));
950 std::string sysname(var->GetName());
951 erasePrefix(sysname, "alpha_");
952 const auto *constraint = findConstraint(var);
953 if (!constraint && !var->isConstant()) {
954 RooJSONFactoryWSTool::error("cannot find constraint for " + std::string(var->GetName()));
955 } else {
956 sample.normsys.emplace_back(sysname, var, fip->high()[i], fip->low()[i],
957 fip->interpolationCodes()[i], constraint);
958 }
959 }
960 } else if (!pip && (pip = dynamic_cast<PiecewiseInterpolation *>(e))) {
961 // nothing to do here, already assigned
962 } else if (RooFormulaVar *formula = dynamic_cast<RooFormulaVar *>(e)) {
963 // people do a lot of fancy stuff with RooFormulaVar, like including NormSys via explicit formulae.
964 // let's try to decompose it into building blocks
965 TString expression(formula->expression());
966 for (size_t i = formula->nParameters(); i--;) {
967 const RooAbsArg *p = formula->getParameter(i);
968 expression.ReplaceAll(("x[" + std::to_string(i) + "]").c_str(), p->GetName());
969 expression.ReplaceAll(("@" + std::to_string(i)).c_str(), p->GetName());
970 }
971 auto components = splitTopLevelProduct(expression.Data());
972 if (components.size() == 0) {
973 // it's not a product, let's just treat it as an unknown element
974 sample.otherElements.push_back(formula);
975 } else {
976 // it is a prododuct, we can try to handle the elements separately
977 std::vector<RooAbsArg *> realComponents;
978 int idx = 0;
979 for (auto &comp : components) {
980 // check if this is a trivial element of a product, we can treat it as its own modifier
981 auto *part = formula->getParameter(comp.c_str());
982 if (part) {
983 realComponents.push_back(part);
984 continue;
985 }
986 // check if this is an attempt at explicitly encoding an overallSys
987 auto normsys = parseOverallModifierFormula(comp, formula);
988 if (normsys.param) {
989 sample.normsys.emplace_back(std::move(normsys));
990 continue;
991 }
992
993 // this is something non-trivial, let's deal with it separately
994 std::string name = std::string(formula->GetName()) + "_part" + std::to_string(idx);
995 ++idx;
996 auto *var = new RooFormulaVar(name.c_str(), name.c_str(), comp.c_str(), formula->dependents());
997 sample.tmpElements.push_back({var});
998 }
999 self(realComponents, self);
1000 }
1001 } else if (auto real = dynamic_cast<RooAbsReal *>(e)) {
1002 sample.otherElements.push_back(real);
1003 }
1004 }
1005 };
1006
1007 RooArgSet elems;
1008 collectElements(elems, func);
1009 collectElements(elems, sumpdf->coefList().at(sampleidx));
1011
1012 // see if we can get the observables
1013 if (pip) {
1014 if (auto nh = dynamic_cast<RooHistFunc const *>(pip->nominalHist())) {
1015 updateObservables(nh->dataHist());
1016 }
1017 }
1018
1019 // sort and configure norms
1020 sortByName(sample.normfactors);
1021 sortByName(sample.normsys);
1022
1023 // sort and configure the histosys
1024 if (pip) {
1025 for (size_t i = 0; i < pip->paramList().size(); ++i) {
1026 RooAbsReal *var = static_cast<RooAbsReal *>(pip->paramList().at(i));
1027 std::string sysname(var->GetName());
1028 erasePrefix(sysname, "alpha_");
1029 if (auto lo = dynamic_cast<RooHistFunc *>(pip->lowList().at(i))) {
1030 if (auto hi = dynamic_cast<RooHistFunc *>(pip->highList().at(i))) {
1031 const auto *constraint = findConstraint(var);
1032 if (!constraint && !var->isConstant()) {
1033 RooJSONFactoryWSTool::error("cannot find constraint for " + std::string(var->GetName()));
1034 } else {
1035 sample.histosys.emplace_back(sysname, var, lo, hi, constraint);
1036 }
1037 }
1038 }
1039 }
1040 sortByName(sample.histosys);
1041 }
1042
1043 for (ParamHistFunc *phf : phfs) {
1044 if (startsWith(std::string(phf->GetName()), "mc_stat_")) { // MC stat uncertainty
1045 int idx = 0;
1046 for (const auto &g : phf->paramList()) {
1047 sample.staterrorParameters.push_back(static_cast<RooRealVar *>(g));
1048 ++idx;
1049 RooAbsPdf *constraint = findConstraint(g);
1050 if (channel.tot_yield.find(idx) == channel.tot_yield.end()) {
1051 channel.tot_yield[idx] = 0;
1052 channel.tot_yield2[idx] = 0;
1053 }
1054 channel.tot_yield[idx] += sample.hist[idx - 1];
1055 channel.tot_yield2[idx] += (sample.hist[idx - 1] * sample.hist[idx - 1]);
1056 if (constraint) {
1057 sample.barlowBeestonLightConstraintType = constraint->IsA();
1058 if (RooPoisson *constraint_p = dynamic_cast<RooPoisson *>(constraint)) {
1059 double erel = 1. / std::sqrt(constraint_p->getX().getVal());
1060 channel.rel_errors[idx] = erel;
1061 } else if (RooGaussian *constraint_g = dynamic_cast<RooGaussian *>(constraint)) {
1062 double erel = constraint_g->getSigma().getVal() / constraint_g->getMean().getVal();
1063 channel.rel_errors[idx] = erel;
1064 } else {
1066 "currently, only RooPoisson and RooGaussian are supported as constraint types");
1067 }
1068 }
1069 }
1070 sample.useBarlowBeestonLight = true;
1071 } else { // other ShapeSys
1072 ShapeSys sys(phf->GetName());
1073 erasePrefix(sys.name, channel.name + "_");
1074 bool isshapesys = eraseSuffix(sys.name, "_ShapeSys") || eraseSuffix(sys.name, "_shapeSys");
1075 bool isshapefactor = eraseSuffix(sys.name, "_ShapeFactor") || eraseSuffix(sys.name, "_shapeFactor");
1076
1077 for (const auto &g : phf->paramList()) {
1078 sys.parameters.push_back(static_cast<RooRealVar *>(g));
1079 RooAbsPdf *constraint = nullptr;
1080 if (isshapesys) {
1081 constraint = findConstraint(g);
1082 if (!constraint)
1083 constraint = ws->pdf(constraintName(g->GetName()));
1084 if (!constraint && !g->isConstant()) {
1085 RooJSONFactoryWSTool::error("cannot find constraint for " + std::string(g->GetName()));
1086 }
1087 } else if (!isshapefactor) {
1088 RooJSONFactoryWSTool::error("unknown type of shapesys " + std::string(phf->GetName()));
1089 }
1090 if (!constraint) {
1091 sys.constraints.push_back(0.0);
1092 } else if (auto constraint_p = dynamic_cast<RooPoisson *>(constraint)) {
1093 sys.constraints.push_back(1. / std::sqrt(constraint_p->getX().getVal()));
1094 if (!sys.constraint) {
1095 sys.constraintType = RooPoisson::Class();
1096 }
1097 } else if (auto constraint_g = dynamic_cast<RooGaussian *>(constraint)) {
1098 sys.constraints.push_back(constraint_g->getSigma().getVal() / constraint_g->getMean().getVal());
1099 if (!sys.constraint) {
1100 sys.constraintType = RooGaussian::Class();
1101 }
1102 }
1103 }
1104 sample.shapesys.emplace_back(std::move(sys));
1105 }
1106 }
1107 sortByName(sample.shapesys);
1108
1109 // add the sample
1110 channel.samples.emplace_back(std::move(sample));
1111 }
1112
1113 sortByName(channel.samples);
1114 return channel;
1115}
1116
1117void configureStatError(Channel &channel)
1118{
1119 for (auto &sample : channel.samples) {
1120 if (sample.useBarlowBeestonLight) {
1121 sample.histError.resize(sample.hist.size());
1122 for (auto bin : channel.rel_errors) {
1123 // reverse engineering the correct partial error
1124 // the (arbitrary) convention used here is that all samples should have the same relative error
1125 const int i = bin.first;
1126 const double relerr_tot = bin.second;
1127 const double count = sample.hist[i - 1];
1128 // this reconstruction is inherently imprecise, so we truncate it at some decimal places to make sure that
1129 // we don't carry around too many useless digits
1130 sample.histError[i - 1] =
1131 round_prec(relerr_tot * channel.tot_yield[i] / std::sqrt(channel.tot_yield2[i]) * count, 7);
1132 }
1133 }
1134 }
1135}
1136
1138{
1139 bool observablesWritten = false;
1140 for (const auto &sample : channel.samples) {
1141
1142 elem["type"] << "histfactory_dist";
1143
1144 auto &s = RooJSONFactoryWSTool::appendNamedChild(elem["samples"], sample.name);
1145
1146 auto &modifiers = s["modifiers"];
1147 modifiers.set_seq();
1148
1149 for (const auto &nf : sample.normfactors) {
1150 auto &mod = modifiers.append_child();
1151 mod.set_map();
1152 mod["name"] << nf.name;
1153 mod["parameter"] << nf.param->GetName();
1154 mod["type"] << "normfactor";
1155 if (nf.constraint) {
1156 mod["constraint_name"] << nf.constraint->GetName();
1157 tool->queueExport(*nf.constraint);
1158 }
1159 }
1160
1161 for (const auto &sys : sample.normsys) {
1162 auto &mod = modifiers.append_child();
1163 mod.set_map();
1164 mod["name"] << sys.name;
1165 mod["type"] << "normsys";
1166 mod["parameter"] << sys.param->GetName();
1167 if (sys.interpolationCode != 4) {
1168 mod["interpolation"] << sys.interpolationCode;
1169 }
1170 if (sys.constraint) {
1171 mod["constraint_name"] << sys.constraint->GetName();
1172 } else if (sys.constraintType) {
1173 mod["constraint_type"] << toString(sys.constraintType);
1174 }
1175 auto &data = mod["data"].set_map();
1176 data["lo"] << sys.low;
1177 data["hi"] << sys.high;
1178 }
1179
1180 for (const auto &sys : sample.histosys) {
1181 auto &mod = modifiers.append_child();
1182 mod.set_map();
1183 mod["name"] << sys.name;
1184 mod["type"] << "histosys";
1185 mod["parameter"] << sys.param->GetName();
1186 if (sys.constraint) {
1187 mod["constraint_name"] << sys.constraint->GetName();
1188 } else if (sys.constraintType) {
1189 mod["constraint_type"] << toString(sys.constraintType);
1190 }
1191 auto &data = mod["data"].set_map();
1192 if (channel.nBins != sys.low.size() || channel.nBins != sys.high.size()) {
1193 std::stringstream ss;
1194 ss << "inconsistent binning: " << channel.nBins << " bins expected, but " << sys.low.size() << "/"
1195 << sys.high.size() << " found in nominal histogram errors!";
1196 RooJSONFactoryWSTool::error(ss.str().c_str());
1197 }
1198 RooJSONFactoryWSTool::exportArray(channel.nBins, sys.low.data(), data["lo"].set_map()["contents"]);
1199 RooJSONFactoryWSTool::exportArray(channel.nBins, sys.high.data(), data["hi"].set_map()["contents"]);
1200 }
1201
1202 for (const auto &sys : sample.shapesys) {
1203 auto &mod = modifiers.append_child();
1204 mod.set_map();
1205 mod["name"] << sys.name;
1206 mod["type"] << "shapesys";
1207 optionallyExportGammaParameters(mod, sys.name, sys.parameters);
1208 if (sys.constraint) {
1209 mod["constraint_name"] << sys.constraint->GetName();
1210 } else if (sys.constraintType) {
1211 mod["constraint_type"] << toString(sys.constraintType);
1212 }
1213 if (sys.constraint || sys.constraintType) {
1214 auto &vals = mod["data"].set_map()["vals"];
1215 vals.fill_seq(sys.constraints);
1216 } else {
1217 auto &vals = mod["data"].set_map()["vals"];
1218 vals.set_seq();
1219 for (std::size_t i = 0; i < sys.parameters.size(); ++i) {
1220 vals.append_child() << 0;
1221 }
1222 }
1223 }
1224
1225 for (const auto &other : sample.otherElements) {
1226 auto &mod = modifiers.append_child();
1227 mod.set_map();
1228 mod["name"] << other.name;
1229 mod["type"] << "custom";
1230 }
1231 for (const auto &other : sample.tmpElements) {
1232 auto &mod = modifiers.append_child();
1233 mod.set_map();
1234 mod["name"] << other.name;
1235 mod["type"] << "custom";
1236 }
1237
1238 if (sample.useBarlowBeestonLight) {
1239 auto &mod = modifiers.append_child();
1240 mod.set_map();
1241 mod["name"] << ::Literals::staterror;
1242 mod["type"] << ::Literals::staterror;
1243 optionallyExportGammaParameters(mod, "stat_" + channel.name, sample.staterrorParameters);
1244 mod["constraint_type"] << toString(sample.barlowBeestonLightConstraintType);
1245 }
1246
1247 if (!observablesWritten) {
1248 auto &output = elem["axes"].set_seq();
1249 for (auto *obs : static_range_cast<RooRealVar *>(*channel.varSet)) {
1250 auto &out = output.append_child().set_map();
1251 std::string name = obs->GetName();
1253 out["name"] << name;
1254 writeAxis(out, *obs);
1255 }
1256 observablesWritten = true;
1257 }
1258 auto &dataNode = s["data"].set_map();
1259 if (channel.nBins != sample.hist.size()) {
1260 std::stringstream ss;
1261 ss << "inconsistent binning: " << channel.nBins << " bins expected, but " << sample.hist.size()
1262 << " found in nominal histogram!";
1263 RooJSONFactoryWSTool::error(ss.str().c_str());
1264 }
1265 RooJSONFactoryWSTool::exportArray(channel.nBins, sample.hist.data(), dataNode["contents"]);
1266 if (!sample.histError.empty()) {
1267 if (channel.nBins != sample.histError.size()) {
1268 std::stringstream ss;
1269 ss << "inconsistent binning: " << channel.nBins << " bins expected, but " << sample.histError.size()
1270 << " found in nominal histogram errors!";
1271 RooJSONFactoryWSTool::error(ss.str().c_str());
1272 }
1273 RooJSONFactoryWSTool::exportArray(channel.nBins, sample.histError.data(), dataNode["errors"]);
1274 }
1275 }
1276
1277 return true;
1278}
1279
1280std::vector<RooAbsPdf *> findLostConstraints(const Channel &channel, const std::vector<RooAbsPdf *> &constraints)
1281{
1282 // collect all the vars that are used by the model
1283 std::set<const RooAbsReal *> vars;
1284 for (const auto &sample : channel.samples) {
1285 for (const auto &nf : sample.normfactors) {
1286 vars.insert(nf.param);
1287 }
1288 for (const auto &sys : sample.normsys) {
1289 vars.insert(sys.param);
1290 }
1291
1292 for (const auto &sys : sample.histosys) {
1293 vars.insert(sys.param);
1294 }
1295 for (const auto &sys : sample.shapesys) {
1296 for (const auto &par : sys.parameters) {
1297 vars.insert(par);
1298 }
1299 }
1300 if (sample.useBarlowBeestonLight) {
1301 for (const auto &par : sample.staterrorParameters) {
1302 vars.insert(par);
1303 }
1304 }
1305 }
1306
1307 // check if there is any constraint present that is unrelated to these vars
1308 std::vector<RooAbsPdf *> lostConstraints;
1309 for (auto *pdf : constraints) {
1310 bool related = false;
1311 for (const auto *var : vars) {
1312 if (pdf->dependsOn(*var)) {
1313 related = true;
1314 }
1315 }
1316 if (!related) {
1317 lostConstraints.push_back(pdf);
1318 }
1319 }
1320 // return the constraints that would be "lost" when exporting the model
1321 return lostConstraints;
1322}
1323
1325 std::vector<RooAbsPdf *> constraints, JSONNode &elem)
1326{
1327 // some preliminary checks
1328 if (!sumpdf) {
1329 if (verbose) {
1330 std::cout << pdfname << " is not a sumpdf" << std::endl;
1331 }
1332 return false;
1333 }
1334
1335 for (RooAbsArg *sample : sumpdf->funcList()) {
1336 if (!dynamic_cast<RooProduct *>(sample) && !dynamic_cast<RooRealSumPdf *>(sample)) {
1337 if (verbose)
1338 std::cout << "sample " << sample->GetName() << " is no RooProduct or RooRealSumPdf in " << pdfname
1339 << std::endl;
1340 return false;
1341 }
1342 }
1343
1344 auto channel = readChannel(tool, pdfname, sumpdf);
1345
1346 // sanity checks
1347 if (channel.samples.size() == 0)
1348 return false;
1349 for (auto &sample : channel.samples) {
1350 if (sample.hist.empty()) {
1351 return false;
1352 }
1353 }
1354
1355 // stat error handling
1356 configureStatError(channel);
1357
1358 auto lostConstraints = findLostConstraints(channel, constraints);
1359 // Export all the lost constraints
1360 for (const auto *constraint : lostConstraints) {
1362 "losing constraint term '" + std::string(constraint->GetName()) +
1363 "', implicit constraints are not supported by HS3 yet! The term will appear in the HS3 file, but will not be "
1364 "picked up when creating a likelihood from it! You will have to add it manually as an external constraint.");
1365 tool->queueExport(*constraint);
1366 }
1367
1368 // Export all the regular modifiers
1369 for (const auto &sample : channel.samples) {
1370 for (auto &modifier : sample.normfactors) {
1371 if (modifier.constraint) {
1372 tool->queueExport(*modifier.constraint);
1373 }
1374 }
1375 for (auto &modifier : sample.normsys) {
1376 if (modifier.constraint) {
1377 tool->queueExport(*modifier.constraint);
1378 }
1379 }
1380 for (auto &modifier : sample.histosys) {
1381 if (modifier.constraint) {
1382 tool->queueExport(*modifier.constraint);
1383 }
1384 }
1385 }
1386
1387 // Export all the custom modifiers
1388 for (const auto &sample : channel.samples) {
1389 for (auto &modifier : sample.otherElements) {
1390 tool->queueExport(*modifier.function);
1391 }
1392 for (auto &modifier : sample.tmpElements) {
1393 tool->queueExportTemporary(modifier.function);
1394 }
1395 }
1396
1397 // Export all model parameters
1398 RooArgSet parameters;
1399 sumpdf->getParameters(channel.varSet, parameters);
1400 for (RooAbsArg *param : parameters) {
1401 // This should exclude the global observables
1402 if (!startsWith(std::string{param->GetName()}, "nom_")) {
1403 tool->queueExport(*param);
1404 }
1405 }
1406
1407 return exportChannel(tool, channel, elem);
1408}
1409
1410class HistFactoryStreamer_ProdPdf : public RooFit::JSONIO::Exporter {
1411public:
1412 bool autoExportDependants() const override { return false; }
1414 {
1415 std::vector<RooAbsPdf *> constraints;
1416 RooRealSumPdf *sumpdf = nullptr;
1417 for (RooAbsArg *v : prodpdf->pdfList()) {
1418 RooAbsPdf *pdf = static_cast<RooAbsPdf *>(v);
1419 auto thispdf = dynamic_cast<RooRealSumPdf *>(pdf);
1420 if (thispdf) {
1421 if (!sumpdf)
1422 sumpdf = thispdf;
1423 else
1424 return false;
1425 } else {
1426 constraints.push_back(pdf);
1427 }
1428 }
1429 if (!sumpdf)
1430 return false;
1431
1432 bool ok = tryExportHistFactory(tool, prodpdf->GetName(), sumpdf, constraints, elem);
1433 return ok;
1434 }
1435 std::string const &key() const override
1436 {
1437 static const std::string keystring = "histfactory_dist";
1438 return keystring;
1439 }
1440 bool exportObject(RooJSONFactoryWSTool *tool, const RooAbsArg *p, JSONNode &elem) const override
1441 {
1442 return tryExport(tool, static_cast<const RooProdPdf *>(p), elem);
1443 }
1444};
1445
1446class HistFactoryStreamer_SumPdf : public RooFit::JSONIO::Exporter {
1447public:
1448 bool autoExportDependants() const override { return false; }
1450 {
1451 std::vector<RooAbsPdf *> constraints;
1452 return tryExportHistFactory(tool, sumpdf->GetName(), sumpdf, constraints, elem);
1453 }
1454 std::string const &key() const override
1455 {
1456 static const std::string keystring = "histfactory_dist";
1457 return keystring;
1458 }
1459 bool exportObject(RooJSONFactoryWSTool *tool, const RooAbsArg *p, JSONNode &elem) const override
1460 {
1461 return tryExport(tool, static_cast<const RooRealSumPdf *>(p), elem);
1462 }
1463};
1464
1465STATIC_EXECUTE([]() {
1466 using namespace RooFit::JSONIO;
1467
1468 registerImporter<HistFactoryImporter>("histfactory_dist", true);
1470 registerImporter<FlexibleInterpVarFactory>("interpolation0d", true);
1475});
1476
1477} // namespace
bool startsWith(std::string_view str, std::string_view prefix)
bool endsWith(std::string_view str, std::string_view suffix)
#define d(i)
Definition RSha256.hxx:102
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 funcs
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t modifier
char name[80]
Definition TGX11.cxx:148
#define hi
TClass * IsA() const override
Definition TStringLong.h:20
A class which maps the current values of a RooRealVar (or a set of RooRealVars) to one of a number of...
The PiecewiseInterpolation is a class that can morph distributions into each other,...
static TClass * Class()
void setPositiveDefinite(bool flag=true)
const_iterator begin() const
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
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:283
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
TClass * IsA() const override
Definition RooAbsPdf.h:345
Int_t numBins(const char *rangeName=nullptr) const override
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
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
virtual JSONNode & set_seq()=0
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
static TClass * Class()
A real-valued function sampled from a multidimensional histogram.
Definition RooHistFunc.h:31
When using RooFit, statistical models can be conveniently handled and stored as a RooWorkspace.
static void fillSeq(RooFit::Detail::JSONNode &node, RooAbsCollection const &coll, size_t nMax=-1)
static std::unique_ptr< RooDataHist > readBinnedData(const RooFit::Detail::JSONNode &n, const std::string &namecomp, RooArgSet const &vars)
Read binned data from the JSONNode and create a RooDataHist object.
static RooFit::Detail::JSONNode & appendNamedChild(RooFit::Detail::JSONNode &node, std::string const &name)
static void exportArray(std::size_t n, double const *contents, RooFit::Detail::JSONNode &output)
Export an array of doubles to a JSONNode.
static bool testValidName(const std::string &str, bool forcError)
static void error(const char *s)
Writes an error message to the RooFit message service and throws a runtime_error.
static std::string name(const RooFit::Detail::JSONNode &n)
static std::ostream & warning(const std::string &s)
Writes a warning message to the RooFit message service.
static RooArgSet readAxes(const RooFit::Detail::JSONNode &node)
Read axes from the JSONNode and create a RooArgSet representing them.
RooFit Lognormal PDF.
static TClass * Class()
Poisson pdf.
Definition RooPoisson.h:19
static TClass * Class()
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:36
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:
static TClass * Class()
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setError(double value)
Definition RooRealVar.h:61
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false, bool shared=true) const override
Return binning definition with name.
This class encapsulates all information for the statistical interpretation of one experiment.
Configuration for a constrained, coherent shape variation of affected samples.
Configuration for an un- constrained overall systematic to scale sample normalisations.
Definition Measurement.h:69
Constrained bin-by-bin variation of affected histogram.
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.
RooAbsReal * function(RooStringView name) const
Retrieve function (RooAbsReal) with given name. Note that all RooAbsPdfs are also RooAbsReals....
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.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:138
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:641
RooCmdArg RecycleConflictNodes(bool flag=true)
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)
const Int_t n
Definition legend1.C:16
double gamma(double x)
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition RExports.h:168
CreateGammaConstraintsOutput createGammaConstraints(RooArgList const &paramList, std::span< const double > relSigmas, double minSigma, Constraint::Type type)
#define STATIC_EXECUTE(MY_FUNC)
TLine l
Definition textangle.C:4
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338