Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Domains.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN, Jan 2023
5 *
6 * Copyright (c) 2023, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include "Domains.h"
14#include "JSONIOUtils.h"
15
16#include <RooAbsBinning.h>
17#include <RooBinning.h>
18#include <RooConstVar.h>
19#include <RooGlobalFunc.h>
21#include <RooNumber.h>
22#include <RooRealVar.h>
23#include <RooWorkspace.h>
24
26
27namespace RooFit {
28namespace JSONIO {
29namespace Detail {
30
31constexpr static auto defaultDomainName = "default_domain";
32
33namespace {
34
35// JSON can't represent infinity, so an infinite bound is expressed by omitting it. An absent bound is read as
36// infinite by readBound() in JSONIOUtils.
37void writeBound(RooFit::Detail::JSONNode &node, const char *key, double value)
38{
40 node[key] << value;
41 }
42}
43
44} // namespace
45
47{
48 // Parameters with a single value are RooConstVars, whichever domain gives the value.
49 for (const auto &domain : _map) {
50 domain.second.populateFixedValues(ws);
51 }
52 auto default_domain = _map.find(defaultDomainName);
53 if (default_domain != _map.end()) {
54 default_domain->second.populate(ws);
55 } else {
56 // Without the RooFit-specific "default_domain", the variables are created with the range of the domain that
57 // mentions them.
58 for (const auto &domain : _map) {
59 domain.second.populate(ws);
60 }
61 }
62 for (const auto &domain : _map) {
63 if (domain.first == defaultDomainName)
64 continue;
65 domain.second.registerBinnings(domain.first.c_str(), ws);
66 }
67}
68
70{
71 _map[defaultDomainName].readVariable(var.GetName(), var.getBinning());
72 for (const auto &bname : var.getBinningNames()) {
73 if (bname.empty())
74 continue;
75 auto &binning = var.getBinning(bname.c_str());
76 _map[bname].readVariable(var.GetName(), binning);
77 }
78}
79
81{
82 auto default_domain = _map.find(defaultDomainName);
83 if (default_domain != _map.end()) {
84 default_domain->second.writeVariable(var);
85 return;
86 }
87 // Without the RooFit-specific "default_domain", use the first domain that mentions the variable.
88 for (const auto &domain : _map) {
89 if (domain.second.hasVariable(var.GetName())) {
90 domain.second.writeVariable(var);
91 return;
92 }
93 }
94}
95
97{
98 // The "default_domain" is written by RooFit, but it is not required by the HS3 standard.
99 for (auto &domain : node.children()) {
100 if (!domain.has_child("name")) {
101 RooJSONFactoryWSTool::error("encountered domain without \"name\"");
102 }
103 auto &name = domain["name"];
104 _map[name.val()].readJSON(domain);
105 }
106 // A parameter with a single value has no other domain: every domain that lists it must give it the same value.
107 for (auto const &[domainName, domain] : _map) {
108 for (auto const &[varName, elem] : domain._map) {
109 if (!elem.hasFixedValue) {
110 continue;
111 }
112 for (auto const &[otherName, other] : _map) {
113 auto found = other._map.find(varName);
114 if (found == other._map.end()) {
115 continue;
116 }
117 if (!found->second.hasFixedValue) {
118 RooJSONFactoryWSTool::error("the parameter '" + varName + "' has the single value " +
119 std::to_string(elem.fixedValue) + " in the domain '" + domainName +
120 "', but a range in the domain '" + otherName + "'");
121 }
122 if (found->second.fixedValue != elem.fixedValue) {
123 RooJSONFactoryWSTool::error("the parameter '" + varName +
124 "' has different single values in the "
125 "domains '" +
126 domainName + "' and '" + otherName + "'");
127 }
128 }
129 }
130 }
131}
132
133void Domains::readFixedValue(const char *name, double value)
134{
135 _map[defaultDomainName].readFixedValue(name, value);
136}
137
138double const *Domains::fixedValue(const char *name) const
139{
140 for (auto const &domain : _map) {
141 if (double const *value = domain.second.fixedValue(name)) {
142 return value;
143 }
144 }
145 return nullptr;
146}
147
149{
150 for (auto const &domain : _map) {
151 // Avoid writing a domain that was already written
152 if (!RooJSONFactoryWSTool::findNamedChild(node, domain.first)) {
153 domain.second.writeJSON(RooJSONFactoryWSTool::appendNamedChild(node, domain.first));
154 }
155 }
156}
157
158bool Domains::hasVariable(const char *name) const
159{
160 for (auto const &domain : _map) {
161 if (domain.second.hasVariable(name)) {
162 return true;
163 }
164 }
165 return false;
166}
167
172
174{
175 elem.hasNBins = false;
176 elem.nBins = 0;
177 elem.edges.clear();
178
179 const int nBins = binning.numBins();
180 if (nBins <= 0) {
181 return;
182 }
183
184 if (binning.isUniform()) {
185 elem.hasNBins = true;
186 elem.nBins = nBins;
187 } else {
188 elem.edges.push_back(binning.binLow(0));
189 for (int i = 0; i < nBins; ++i) {
190 elem.edges.push_back(binning.binHigh(i));
191 }
192 }
193}
194
196{
197 auto &elem = _map[name];
198
199 elem.hasMin = true;
200 elem.min = binning.lowBound();
201 elem.hasMax = true;
202 elem.max = binning.highBound();
203 readBinning(elem, binning);
204}
205
207{
208 if (!elem.edges.empty()) {
209 RooBinning binning(elem.edges.front(), elem.edges.back());
210 for (double edge : elem.edges) {
211 binning.addBoundary(edge);
212 }
213 var.setBinning(binning, name);
214 } else if (elem.hasNBins && elem.nBins != 0) {
215 var.setBins(elem.nBins, name);
216 }
217}
218
220{
221 if (!elem.edges.empty()) {
222 auto &edges = node["edges"].set_seq();
223 for (double edge : elem.edges) {
224 edges.append_child() << edge;
225 }
226 } else if (elem.hasNBins && elem.nBins != 0) {
227 node["nbins"] << elem.nBins;
228 }
229}
231{
232 auto found = _map.find(var.GetName());
233 if (found != _map.end() && !found->second.hasFixedValue) {
234 auto const &elem = found->second;
235 if (elem.hasMin) {
236 if (RooNumber::isInfinite(elem.min)) {
237 var.removeMin();
238 } else {
239 var.setMin(elem.min);
240 }
241 }
242 if (elem.hasMax) {
243 if (RooNumber::isInfinite(elem.max)) {
244 var.removeMax();
245 } else {
246 var.setMax(elem.max);
247 }
248 }
249 applyBinning(var, elem);
250 }
251}
252
254{
255 return _map.find(name) != _map.end();
256}
257
258double const *Domains::ProductDomain::fixedValue(const char *name) const
259{
260 auto found = _map.find(name);
261 return found != _map.end() && found->second.hasFixedValue ? &found->second.fixedValue : nullptr;
262}
263
265{
266 auto &elem = _map[name];
267 elem = ProductDomainElement{};
268 elem.hasFixedValue = true;
269 elem.fixedValue = value;
270}
271
273{
274 if (!node.has_child("type") || node["type"].val() != "product_domain") {
275 RooJSONFactoryWSTool::error("only domains of type \"product_domain\" are currently supported!");
276 }
277 for (auto const &varNode : node["axes"].children()) {
278 const std::string varName = RooJSONFactoryWSTool::name(varNode);
279 auto &elem = _map[varName];
280
281 if (varNode.has_child(fixedValueKey)) {
282 // The domain of the parameter is the set with this single value: no range, binning or categories.
283 for (const char *key : {"min", "max", "nbins", "edges", "categories"}) {
284 if (varNode.has_child(key)) {
285 RooJSONFactoryWSTool::error("the axis '" + varName + "' of the domain '" +
286 RooJSONFactoryWSTool::name(node) + "' has \"" + fixedValueKey + "\" and \"" +
287 key + "\", which exclude each other");
288 }
289 }
290 auto const &valueNode = varNode[fixedValueKey];
291 if (!valueNode.is_number()) {
292 RooJSONFactoryWSTool::error("the \"" + std::string(fixedValueKey) + "\" of the axis '" + varName +
293 "' must be a number");
294 }
295 elem = ProductDomainElement{};
296 elem.hasFixedValue = true;
297 elem.fixedValue = valueNode.val_double();
298 continue;
299 }
300
301 if (varNode.has_child("min")) {
302 elem.min = readBound(varNode, "min", -RooNumber::infinity());
303 elem.hasMin = true;
304 }
305 if (varNode.has_child("max")) {
306 elem.max = readBound(varNode, "max", RooNumber::infinity());
307 elem.hasMax = true;
308 }
309 if (varNode.has_child("edges")) {
310 elem.hasNBins = false;
311 elem.edges.clear();
312 for (auto const &edge : varNode["edges"].children()) {
313 elem.edges.push_back(edge.val_double());
314 }
315 if (!elem.edges.empty()) {
316 if (!elem.hasMin) {
317 elem.min = elem.edges.front();
318 elem.hasMin = true;
319 }
320 if (!elem.hasMax) {
321 elem.max = elem.edges.back();
322 elem.hasMax = true;
323 }
324 }
325 } else if (varNode.has_child("nbins")) {
326 elem.nBins = varNode["nbins"].val_int();
327 elem.hasNBins = elem.nBins != 0;
328 elem.edges.clear();
329 }
330 }
331}
333
334{
335 node.set_map();
336 node["type"] << "product_domain";
337
338 auto &variablesNode = node["axes"];
339
340 for (auto const &item : _map) {
341 auto const &elem = item.second;
342 RooFit::Detail::JSONNode &varnode = RooJSONFactoryWSTool::appendNamedChild(variablesNode, item.first);
343 if (elem.hasFixedValue) {
344 varnode[fixedValueKey] << elem.fixedValue;
345 continue;
346 }
347 const double min = elem.hasMin ? elem.min : -RooNumber::infinity();
348 const double max = elem.hasMax ? elem.max : RooNumber::infinity();
349 writeBound(varnode, "min", min);
350 writeBound(varnode, "max", max);
351 // A binning requires a bounded range.
352 if (!RooNumber::isInfinite(min) && !RooNumber::isInfinite(max)) {
353 writeBinning(varnode, elem);
354 }
355 }
356}
358{
359 for (auto const &item : _map) {
360 const auto &name = item.first;
361 if (item.second.hasFixedValue) {
362 continue; // see populateFixedValues()
363 }
364 if (!ws.arg(name)) {
365 const auto &elem = item.second;
366 const double vMin = elem.hasMin ? elem.min : -RooNumber::infinity();
367 const double vMax = elem.hasMax ? elem.max : RooNumber::infinity();
368 RooRealVar var{name.c_str(), name.c_str(), vMin, vMax};
369 applyBinning(var, elem);
370 ws.import(var, RooFit::Silence());
371 }
372 }
373}
375{
376 for (auto const &[name, elem] : _map) {
377 if (elem.hasFixedValue && !ws.arg(name)) {
378 ws.import(RooConstVar{name.c_str(), name.c_str(), elem.fixedValue}, RooFit::Silence());
379 }
380 }
381}
382
384{
385 for (auto const &item : _map) {
386 auto *var = ws.var(item.first);
387 if (!var)
388 continue;
389 const double vMin = item.second.hasMin ? item.second.min : -RooNumber::infinity();
390 const double vMax = item.second.hasMax ? item.second.max : RooNumber::infinity();
391 var->setRange(name, vMin, vMax);
392 applyBinning(*var, item.second, name);
393 }
394}
395
396} // namespace Detail
397} // namespace JSONIO
398} // namespace RooFit
double readBound(JSONNode const &node, const char *key, double defaultValue)
constexpr const char * fixedValueKey
Definition JSONIOUtils.h:19
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:142
Abstract base class for RooRealVar binning definitions.
Int_t numBins() const
Return number of bins.
virtual double binLow(Int_t bin) const =0
virtual bool isUniform() const
virtual double highBound() const =0
virtual double lowBound() const =0
virtual double binHigh(Int_t bin) const =0
bool hasMin(const char *name=nullptr) const
Check if variable has a lower bound.
Implements a RooAbsBinning in terms of an array of boundary values, posing no constraints on the choi...
Definition RooBinning.h:27
bool addBoundary(double boundary)
Add bin boundary at given value.
Represents a constant real-valued object.
Definition RooConstVar.h:23
std::string val() const
bool has_child(std::string const &) const
void registerBinnings(const char *name, RooWorkspace &ws) const
Definition Domains.cxx:383
bool hasVariable(const char *name) const
Definition Domains.cxx:253
static void writeBinning(RooFit::Detail::JSONNode &node, ProductDomainElement const &elem)
Definition Domains.cxx:219
void populateFixedValues(RooWorkspace &ws) const
Definition Domains.cxx:374
static void readBinning(ProductDomainElement &elem, RooAbsBinning const &binning)
Definition Domains.cxx:173
double const * fixedValue(const char *name) const
Definition Domains.cxx:258
void populate(RooWorkspace &ws) const
Definition Domains.cxx:357
void readFixedValue(const char *name, double value)
Definition Domains.cxx:264
static void applyBinning(RooRealVar &var, ProductDomainElement const &elem, const char *name=nullptr)
Definition Domains.cxx:206
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:332
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:272
void populate(RooWorkspace &ws) const
Definition Domains.cxx:46
std::map< std::string, ProductDomain > _map
Definition Domains.h:91
void writeVariable(RooRealVar &) const
Definition Domains.cxx:80
void readFixedValue(const char *name, double value)
Definition Domains.cxx:133
void readVariable(RooRealVar const &)
Definition Domains.cxx:69
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:148
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:96
bool hasVariable(const char *name) const
Definition Domains.cxx:158
double const * fixedValue(const char *name) const
Definition Domains.cxx:138
static RooFit::Detail::JSONNode & appendNamedChild(RooFit::Detail::JSONNode &node, std::string const &name)
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 RooFit::Detail::JSONNode const * findNamedChild(RooFit::Detail::JSONNode const &node, std::string const &name)
static constexpr double infinity()
Return internal infinity representation.
Definition RooNumber.h:25
static constexpr int isInfinite(double x)
Return true if x is infinite by RooNumber internal specification.
Definition RooNumber.h:27
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void removeMin(const char *name=nullptr)
Remove lower range limit for binning with given name. Empty name means default range.
void setMin(const char *name, double value, bool shared=true)
Set minimum of name range to given value.
std::list< std::string > getBinningNames() const override
Get a list of all binning names.
void setBins(Int_t nBins, const char *name=nullptr, bool shared=true)
Create a uniform binning under name 'name' for this variable.
void setBinning(const RooAbsBinning &binning, const char *name=nullptr, bool shared=true)
Add given binning under name 'name' with this variable.
void setMax(const char *name, double value, bool shared=true)
Set maximum of name range to given value.
void removeMax(const char *name=nullptr)
Remove upper range limit for binning with given name. Empty name means default range.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false, bool shared=true) const override
Return binning definition with name.
Persistable container for RooFit projects.
RooAbsArg * arg(RooStringView name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
RooRealVar * var(RooStringView name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
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.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
RooCmdArg Silence(bool flag=true)
static constexpr auto defaultDomainName
Definition Domains.cxx:31
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73