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
16#include <RooNumber.h>
17#include <RooRealVar.h>
18#include <RooWorkspace.h>
19
21
22namespace RooFit {
23namespace JSONIO {
24namespace Detail {
25
26constexpr static auto defaultDomainName = "default_domain";
27
29{
30 auto default_domain = _map.find(defaultDomainName);
31 if (default_domain != _map.end()) {
32 default_domain->second.populate(ws);
33 }
34 for (const auto &domain : _map) {
35 if (domain.first == defaultDomainName)
36 continue;
37 domain.second.registerBinnings(domain.first.c_str(), ws);
38 }
39}
40
41void Domains::readVariable(const char *name, double min, double max)
42{
44}
45void Domains::readVariable(const char *name, double min, double max, const char *domain)
46{
47 _map[domain].readVariable(name, min, max);
48}
49
51{
53 for (const auto &bname : var.getBinningNames()) {
54 if (bname.empty())
55 continue;
56 auto &binning = var.getBinning(bname.c_str());
57 readVariable(var.GetName(), binning.lowBound(), binning.highBound(), bname.c_str());
58 }
59}
60
62{
63 auto default_domain = _map.find(defaultDomainName);
64 if (default_domain != _map.end()) {
65 default_domain->second.writeVariable(var);
66 }
67}
68
70{
71 auto default_domain_element = RooJSONFactoryWSTool::findNamedChild(node, defaultDomainName);
72 if (!default_domain_element) {
73 RooJSONFactoryWSTool::error("\"domains\" do not contain \"" + std::string{defaultDomainName} + "\"");
74 }
75 for (auto &domain : node.children()) {
76 if (!domain.has_child("name")) {
77 RooJSONFactoryWSTool::error("encountered domain without \"name\"");
78 }
79 auto &name = domain["name"];
80 _map[name.val()].readJSON(domain);
81 }
82}
83
85{
86 for (auto const &domain : _map) {
87 // Avoid writing a domain that was already written
88 if (!RooJSONFactoryWSTool::findNamedChild(node, domain.first)) {
89 domain.second.writeJSON(RooJSONFactoryWSTool::appendNamedChild(node, domain.first));
90 }
91 }
92}
93
95{
96 readVariable(var.GetName(), var.getMin(), var.getMax());
97}
98
99void Domains::ProductDomain::readVariable(const char *name, double min, double max)
100{
102 return;
103
104 auto &elem = _map[name];
105
106 if (!RooNumber::isInfinite(min)) {
107 elem.hasMin = true;
108 elem.min = min;
109 }
110 if (!RooNumber::isInfinite(max)) {
111 elem.hasMax = true;
112 elem.max = max;
113 }
114}
116{
117 auto found = _map.find(var.GetName());
118 if (found != _map.end()) {
119 auto const &elem = found->second;
120 if (elem.hasMin)
121 var.setMin(elem.min);
122 if (elem.hasMax)
123 var.setMax(elem.max);
124 }
125}
127{
128 if (!node.has_child("type") || node["type"].val() != "product_domain") {
129 RooJSONFactoryWSTool::error("only domains of type \"product_domain\" are currently supported!");
130 }
131 for (auto const &varNode : node["axes"].children()) {
132 auto &elem = _map[RooJSONFactoryWSTool::name(varNode)];
133
134 if (varNode.has_child("min")) {
135 elem.min = varNode["min"].val_double();
136 elem.hasMin = true;
137 }
138 if (varNode.has_child("max")) {
139 elem.max = varNode["max"].val_double();
140 elem.hasMax = true;
141 }
142 }
143}
145
146{
147 node.set_map();
148 node["type"] << "product_domain";
149
150 auto &variablesNode = node["axes"];
151
152 for (auto const &item : _map) {
153 auto const &elem = item.second;
154 RooFit::Detail::JSONNode &varnode = RooJSONFactoryWSTool::appendNamedChild(variablesNode, item.first);
155 if (elem.hasMin)
156 varnode["min"] << elem.min;
157 if (elem.hasMax)
158 varnode["max"] << elem.max;
159 }
160}
162{
163 for (auto const &item : _map) {
164 const auto &name = item.first;
165 if (!ws.var(name)) {
166 const auto &elem = item.second;
167 const double vMin = elem.hasMin ? elem.min : -RooNumber::infinity();
168 const double vMax = elem.hasMax ? elem.max : RooNumber::infinity();
169 ws.import(RooRealVar{name.c_str(), name.c_str(), vMin, vMax});
170 }
171 }
172}
174{
175 for (auto const &item : _map) {
176 auto *var = ws.var(item.first);
177 if (!var)
178 continue;
179 var->setRange(name, item.second.min, item.second.max);
180 }
181}
182
183} // namespace Detail
184} // namespace JSONIO
185} // namespace RooFit
char name[80]
Definition TGX11.cxx:110
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.
virtual std::string val() const =0
virtual JSONNode & set_map()=0
virtual children_view children()
virtual bool has_child(std::string const &) const =0
void registerBinnings(const char *name, RooWorkspace &ws) const
Definition Domains.cxx:173
void populate(RooWorkspace &ws) const
Definition Domains.cxx:161
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:144
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:126
void populate(RooWorkspace &ws) const
Definition Domains.cxx:28
std::map< std::string, ProductDomain > _map
Definition Domains.h:70
void writeVariable(RooRealVar &) const
Definition Domains.cxx:61
void readVariable(const char *name, double min, double max, const char *domain)
Definition Domains.cxx:45
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:84
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:69
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 setMin(const char *name, double value)
Set minimum of name range to given value.
std::list< std::string > getBinningNames() const override
Get a list of all binning names.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false) const override
Return binning definition with name.
void setRange(const char *name, double min, double max)
Set a fit or plotting range.
void setMax(const char *name, double value)
Set maximum of name range to given value.
Persistable container for RooFit projects.
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.
RooRealVar * var(RooStringView name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
static constexpr auto defaultDomainName
Definition Domains.cxx:26
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26