Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HS3Document.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN, Nov 2026
5 *
6 * Copyright (c) 2026, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13// Operations on documents that RooFit needs besides reading, upgrading, validating and writing them.
14
15#include "HS3Document.h"
16
17#include <algorithm>
18
19namespace RooFit {
20namespace HS3 {
21
22DomainRanges::DomainRanges(std::vector<Domain> const &domains)
23{
24 std::vector<Domain const *> sorted;
25 sorted.reserve(domains.size());
26 for (Domain const &domain : domains) {
27 sorted.push_back(&domain);
28 }
29 std::stable_sort(sorted.begin(), sorted.end(), [](Domain const *l, Domain const *r) { return l->name < r->name; });
30 const bool hasDefault =
31 std::any_of(domains.begin(), domains.end(), [](Domain const &d) { return d.name == defaultDomainName; });
32
33 for (Domain const *domain : sorted) {
34 // Of several axes of a variable in one domain, the last one counts.
35 std::map<std::string, DomainAxis const *> axes;
36 for (auto const &axis : domain->axes) {
37 if (auto const *continuous = std::get_if<DomainAxis>(&axis)) {
38 axes[continuous->name] = continuous;
39 _names.insert(continuous->name);
40 }
41 }
42 if (!hasDefault || domain->name == defaultDomainName) {
43 _ranges.insert(axes.begin(), axes.end());
44 }
45 }
46}
47
48DomainAxis const *DomainRanges::find(std::string const &name) const
49{
50 auto found = _ranges.find(name);
51 return found != _ranges.end() ? found->second : nullptr;
52}
53
54char const *typeOf(Modifier const &modifier)
55{
56 return std::visit(
57 overloaded{[](NormFactor const &) { return "normfactor"; }, [](NormSys const &) { return "normsys"; },
58 [](HistoSys const &) { return "histosys"; }, [](ShapeSys const &) { return "shapefactor"; },
59 [](StatError const &) { return "staterror"; }, [](CustomModifier const &) { return "custom"; }},
60 modifier);
61}
62
63namespace {
64
65// What duplicates must have in common to be combined.
66template <class Modifier_t>
67char const *differences(Modifier_t const &l, Modifier_t const &r)
68{
69 if (!(l.constraint == r.constraint)) {
70 return "constraint metadata differs";
71 }
72 if (l.parameter.name != r.parameter.name) {
73 return "parameter metadata differs";
74 }
75 if (l.interpolation != r.interpolation) {
76 return "interpolation behaviours differ";
77 }
78 return nullptr;
79}
80
81// Combines `modifier` into `merged`, or returns why that's not possible.
82char const *combine(NormSys &merged, NormSys const &modifier, std::vector<double> const &)
83{
84 if (char const *reason = differences(merged, modifier)) {
85 return reason;
86 }
87 // Multiplicatively combining two normsys is only faithful when the interpolation is done in log-space, so that
88 // f1(alpha) * f2(alpha) is again representable by a single normsys with the multiplied lo/hi factors. This holds
89 // for the piecewise exponential (exact everywhere) and for the polynomial exponential (exact at the +-1 sigma
90 // anchors and in the exponential extrapolation region). The linear-space interpolations would turn the product into
91 // a shape that cannot be represented by a single normsys.
92 if (merged.interpolation != multiplicativePiecewiseExponential &&
93 merged.interpolation != multiplicativePolynomialExponential) {
94 return "multiplicative combination is only valid for log-space interpolation";
95 }
96 merged.lo *= modifier.lo;
97 merged.hi *= modifier.hi;
98 return nullptr;
99}
100
101char const *combine(HistoSys &merged, HistoSys const &modifier, std::vector<double> const &nominal)
102{
103 if (char const *reason = differences(merged, modifier)) {
104 return reason;
105 }
106 if (merged.interpolation != additivePolynomialLinear) {
107 return "this interpolation cannot currently be combined for duplicate histosys modifiers";
108 }
109 const std::size_t nBins = nominal.size();
110 auto fits = [&](HistoSysVariation const &variation) { return variation.contents.size() == nBins; };
111 if (!fits(merged.lo) || !fits(merged.hi) || !fits(modifier.lo) || !fits(modifier.hi)) {
112 return "histogram binning differs";
113 }
114 for (std::size_t bin = 0; bin < nBins; ++bin) {
115 merged.lo.contents[bin] += modifier.lo.contents[bin] - nominal[bin];
116 merged.hi.contents[bin] += modifier.hi.contents[bin] - nominal[bin];
117 }
118 return nullptr;
119}
120
121} // namespace
122
123void mergeDuplicateModifiers(HistFactoryDist &dist, std::string const &channel, Diagnostics &diagnostics)
124{
125 for (Sample &sample : dist.samples) {
126 auto error = [&](Modifier const &modifier, std::string const &reason) {
127 diagnostics.push_back({Diagnostic::Severity::Error, "",
128 "cannot combine duplicate modifier '" + nameOf(modifier) + "' of type '" +
129 typeOf(modifier) + "' in sample '" + sample.name + "' of channel '" + channel +
130 "': " + reason});
131 };
132
133 // The duplicates of a type are next to each other, because the modifiers of a type are sorted by name.
134 std::vector<Modifier> merged;
135 std::size_t count = 0; // of the modifiers that the last merged one combines
136 bool failed = false;
137 auto warnIfCombined = [&]() {
138 if (count > 1 && !failed) {
139 diagnostics.push_back({Diagnostic::Severity::Warning, "",
140 "combined " + std::to_string(count) + " duplicate modifiers named '" +
141 nameOf(merged.back()) + "' of type '" + typeOf(merged.back()) + "' in sample '" +
142 sample.name + "' of channel '" + channel + "'"});
143 }
144 };
145 for (Modifier &modifier : sample.modifiers) {
146 Modifier *last = merged.empty() ? nullptr : &merged.back();
147 const bool combinable =
148 std::holds_alternative<NormSys>(modifier) || std::holds_alternative<HistoSys>(modifier);
149 if (combinable && last && last->index() == modifier.index() && nameOf(*last) == nameOf(modifier)) {
150 char const *reason = nullptr;
151 if (auto *normSys = std::get_if<NormSys>(last)) {
152 reason = combine(*normSys, std::get<NormSys>(modifier), sample.contents);
153 } else {
154 reason = combine(std::get<HistoSys>(*last), std::get<HistoSys>(modifier), sample.contents);
155 }
156 if (reason) {
157 error(modifier, reason);
158 failed = true;
159 }
160 ++count;
161 continue;
162 }
163 warnIfCombined();
164 merged.push_back(std::move(modifier));
165 count = 1;
166 failed = false;
167 }
168 warnIfCombined();
169 sample.modifiers = std::move(merged);
170
171 std::set<std::pair<std::string, std::string>> seen;
172 for (Modifier const &modifier : sample.modifiers) {
173 if (!seen.emplace(typeOf(modifier), nameOf(modifier)).second) {
174 error(modifier, "this modifier type cannot be combined without changing its meaning");
175 }
176 }
177 }
178}
179
181{
182 auto forEachInterpolation = [&](auto &&fn) {
183 for (Sample &sample : dist.samples) {
184 for (Modifier &modifier : sample.modifiers) {
185 if (auto *normSys = std::get_if<NormSys>(&modifier)) {
186 fn(normSys->interpolation);
187 } else if (auto *histoSys = std::get_if<HistoSys>(&modifier)) {
188 fn(histoSys->interpolation);
189 }
190 }
191 }
192 };
193
194 std::map<Interpolation, std::size_t> counts;
195 forEachInterpolation([&](std::optional<Interpolation> &interpolation) {
196 if (interpolation) {
197 ++counts[*interpolation];
198 }
199 });
200 if (counts.empty()) {
201 return;
202 }
203 // A tie is decided in favor of the default of HistFactory.
204 auto best = counts.begin();
205 for (auto current = std::next(counts.begin()); current != counts.end(); ++current) {
206 if (current->second > best->second ||
207 (current->second == best->second && current->first == multiplicativePolynomialExponential &&
208 best->first != multiplicativePolynomialExponential)) {
209 best = current;
210 }
211 }
212 dist.defaultInterpolation = best->first;
213 forEachInterpolation([&](std::optional<Interpolation> &interpolation) {
214 if (interpolation == dist.defaultInterpolation) {
215 interpolation.reset();
216 }
217 });
218}
219
220} // namespace HS3
221} // namespace RooFit
#define d(i)
Definition RSha256.hxx:102
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t modifier
char name[80]
Definition TGX11.cxx:142
DomainAxis const * find(std::string const &name) const
The axis that defines the range of a variable, or null.
DomainRanges(std::vector< Domain > const &domains)
std::map< std::string, DomainAxis const * > _ranges
std::set< std::string > _names
Types that represent an HS3 document.
std::variant< NormFactor, NormSys, HistoSys, ShapeSys, StatError, CustomModifier > Modifier
std::string const & nameOf(std::variant< Ts... > const &v)
The name of the alternative that a variant holds, e.g. of an axis or of a modifier.
Definition HS3Document.h:58
constexpr Interpolation multiplicativePolynomialExponential
constexpr Interpolation additivePolynomialLinear
constexpr char const * defaultDomainName
The domain in which RooFit writes the ranges of all variables. It is a convention of RooFit,...
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...
void factorOutDefaultInterpolation(HistFactoryDist &dist)
Makes the most common interpolation of the modifiers the "default_interpolation", which they then don...
char const * typeOf(Modifier const &modifier)
The type of a modifier, as the standard names it.
std::vector< Diagnostic > Diagnostics
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
Multiplies the sample with a function of the document.
The axis of a domain.
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...
Combines lambdas into one visitor for std::visit(). The compiler complains about the alternatives wit...
Definition HS3Document.h:50
TLine l
Definition textangle.C:4