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
15#include <RooAbsBinning.h>
16#include <RooBinning.h>
18#include <RooNumber.h>
19#include <RooRealVar.h>
20#include <RooWorkspace.h>
21
23
24namespace RooFit {
25namespace JSONIO {
26namespace Detail {
27
28constexpr static auto defaultDomainName = "default_domain";
29
30namespace {
31
32double readBound(RooFit::Detail::JSONNode const &node, const char *key, double defaultValue)
33{
34 if (!node.has_child(key)) {
35 return defaultValue;
36 }
37 auto const &bound = node[key];
38 return bound.is_null() ? defaultValue : bound.val_double();
39}
40
42{
44 node.set_null();
45 } else {
46 node << value;
47 }
48}
49
50} // namespace
51
53{
55 if (default_domain != _map.end()) {
56 default_domain->second.populate(ws);
57 }
58 for (const auto &domain : _map) {
59 if (domain.first == defaultDomainName)
60 continue;
61 domain.second.registerBinnings(domain.first.c_str(), ws);
62 }
63}
64
66{
67 _map[defaultDomainName].readVariable(var.GetName(), var.getBinning());
68 for (const auto &bname : var.getBinningNames()) {
69 if (bname.empty())
70 continue;
71 auto &binning = var.getBinning(bname.c_str());
72 _map[bname].readVariable(var.GetName(), binning);
73 }
74}
75
77{
79 if (default_domain != _map.end()) {
80 default_domain->second.writeVariable(var);
81 }
82}
83
85{
88 RooJSONFactoryWSTool::error("\"domains\" do not contain \"" + std::string{defaultDomainName} + "\"");
89 }
90 for (auto &domain : node.children()) {
91 if (!domain.has_child("name")) {
92 RooJSONFactoryWSTool::error("encountered domain without \"name\"");
93 }
94 auto &name = domain["name"];
95 _map[name.val()].readJSON(domain);
96 }
97}
98
100{
101 for (auto const &domain : _map) {
102 // Avoid writing a domain that was already written
103 if (!RooJSONFactoryWSTool::findNamedChild(node, domain.first)) {
104 domain.second.writeJSON(RooJSONFactoryWSTool::appendNamedChild(node, domain.first));
105 }
106 }
107}
108
109bool Domains::hasVariable(const char *name) const
110{
111 for (auto const &domain : _map) {
112 if (domain.second.hasVariable(name)) {
113 return true;
114 }
115 }
116 return false;
117}
118
123
125{
126 elem.hasNBins = false;
127 elem.nBins = 0;
128 elem.edges.clear();
129
130 const int nBins = binning.numBins();
131 if (nBins <= 0) {
132 return;
133 }
134
135 if (binning.isUniform()) {
136 elem.hasNBins = true;
137 elem.nBins = nBins;
138 } else {
139 elem.edges.push_back(binning.binLow(0));
140 for (int i = 0; i < nBins; ++i) {
141 elem.edges.push_back(binning.binHigh(i));
142 }
143 }
144}
145
147{
148 auto &elem = _map[name];
149
150 elem.hasMin = true;
151 elem.min = binning.lowBound();
152 elem.hasMax = true;
153 elem.max = binning.highBound();
154 readBinning(elem, binning);
155}
156
158{
159 if (!elem.edges.empty()) {
160 RooBinning binning(elem.edges.front(), elem.edges.back());
161 for (double edge : elem.edges) {
162 binning.addBoundary(edge);
163 }
164 var.setBinning(binning, name);
165 } else if (elem.hasNBins && elem.nBins != 0) {
166 var.setBins(elem.nBins, name);
167 }
168}
169
171{
172 if (!elem.edges.empty()) {
173 auto &edges = node["edges"].set_seq();
174 for (double edge : elem.edges) {
175 edges.append_child() << edge;
176 }
177 } else if (elem.hasNBins && elem.nBins != 0) {
178 node["nbins"] << elem.nBins;
179 }
180}
182{
183 auto found = _map.find(var.GetName());
184 if (found != _map.end()) {
185 auto const &elem = found->second;
186 if (elem.hasMin) {
187 if (RooNumber::isInfinite(elem.min)) {
188 var.removeMin();
189 } else {
190 var.setMin(elem.min);
191 }
192 }
193 if (elem.hasMax) {
194 if (RooNumber::isInfinite(elem.max)) {
195 var.removeMax();
196 } else {
197 var.setMax(elem.max);
198 }
199 }
200 applyBinning(var, elem);
201 }
202}
203
205{
206 return _map.find(name) != _map.end();
207}
208
210{
211 if (!node.has_child("type") || node["type"].val() != "product_domain") {
212 RooJSONFactoryWSTool::error("only domains of type \"product_domain\" are currently supported!");
213 }
214 for (auto const &varNode : node["axes"].children()) {
216
217 if (varNode.has_child("min")) {
218 elem.min = readBound(varNode, "min", -RooNumber::infinity());
219 elem.hasMin = true;
220 }
221 if (varNode.has_child("max")) {
222 elem.max = readBound(varNode, "max", RooNumber::infinity());
223 elem.hasMax = true;
224 }
225 if (varNode.has_child("edges")) {
226 elem.hasNBins = false;
227 elem.edges.clear();
228 for (auto const &edge : varNode["edges"].children()) {
229 elem.edges.push_back(edge.val_double());
230 }
231 if (!elem.edges.empty()) {
232 if (!elem.hasMin) {
233 elem.min = elem.edges.front();
234 elem.hasMin = true;
235 }
236 if (!elem.hasMax) {
237 elem.max = elem.edges.back();
238 elem.hasMax = true;
239 }
240 }
241 } else if (varNode.has_child("nbins")) {
242 elem.nBins = varNode["nbins"].val_int();
243 elem.hasNBins = elem.nBins != 0;
244 elem.edges.clear();
245 }
246 }
247}
249
250{
251 node.set_map();
252 node["type"] << "product_domain";
253
254 auto &variablesNode = node["axes"];
255
256 for (auto const &item : _map) {
257 auto const &elem = item.second;
259 writeBound(varnode["min"], elem.hasMin ? elem.min : -RooNumber::infinity());
260 writeBound(varnode["max"], elem.hasMax ? elem.max : RooNumber::infinity());
261 writeBinning(varnode, elem);
262 }
263}
265{
266 for (auto const &item : _map) {
267 const auto &name = item.first;
268 if (!ws.arg(name)) {
269 const auto &elem = item.second;
270 const double vMin = elem.hasMin ? elem.min : -RooNumber::infinity();
271 const double vMax = elem.hasMax ? elem.max : RooNumber::infinity();
272 RooRealVar var{name.c_str(), name.c_str(), vMin, vMax};
273 applyBinning(var, elem);
274 ws.import(var);
275 }
276 }
277}
279{
280 for (auto const &item : _map) {
281 auto *var = ws.var(item.first);
282 if (!var)
283 continue;
284 const double vMin = item.second.hasMin ? item.second.min : -RooNumber::infinity();
285 const double vMax = item.second.hasMax ? item.second.max : RooNumber::infinity();
286 var->setRange(name, vMin, vMax);
287 applyBinning(*var, item.second, name);
288 }
289}
290
291} // namespace Detail
292} // namespace JSONIO
293} // namespace RooFit
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:145
const_iterator end() const
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
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.
virtual std::string val() const =0
virtual JSONNode & set_map()=0
virtual JSONNode & set_null()=0
virtual children_view children()
virtual JSONNode & set_seq()=0
virtual bool has_child(std::string const &) const =0
void registerBinnings(const char *name, RooWorkspace &ws) const
Definition Domains.cxx:278
bool hasVariable(const char *name) const
Definition Domains.cxx:204
static void writeBinning(RooFit::Detail::JSONNode &node, ProductDomainElement const &elem)
Definition Domains.cxx:170
static void readBinning(ProductDomainElement &elem, RooAbsBinning const &binning)
Definition Domains.cxx:124
void populate(RooWorkspace &ws) const
Definition Domains.cxx:264
static void applyBinning(RooRealVar &var, ProductDomainElement const &elem, const char *name=nullptr)
Definition Domains.cxx:157
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:248
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:209
void populate(RooWorkspace &ws) const
Definition Domains.cxx:52
std::map< std::string, ProductDomain > _map
Definition Domains.h:79
void writeVariable(RooRealVar &) const
Definition Domains.cxx:76
void readVariable(RooRealVar const &)
Definition Domains.cxx:65
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:99
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:84
bool hasVariable(const char *name) const
Definition Domains.cxx:109
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
static constexpr auto defaultDomainName
Definition Domains.cxx:28
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:72