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 found = _map.find(defaultDomainName);
31 if (found != _map.end()) {
32 found->second.populate(ws);
33 }
34}
35void Domains::readVariable(const char *name, double min, double max)
36{
37 _map[defaultDomainName].readVariable(name, min, max);
38}
40{
41 readVariable(var.GetName(), var.getMin(), var.getMax());
42}
44{
45 _map.at(defaultDomainName).writeVariable(var);
46}
47
49{
51 if (!defaultDomain) {
52 RooJSONFactoryWSTool::error("\"domains\" do not contain \"" + std::string{defaultDomainName} + "\"");
53 }
54 _map[defaultDomainName].readJSON(*defaultDomain);
55}
57{
58 for (auto const &domain : _map) {
59 domain.second.writeJSON(RooJSONFactoryWSTool::appendNamedChild(node, domain.first));
60 }
61}
62void Domains::ProductDomain::readVariable(const char *name, double min, double max)
63{
64 auto &elem = _map[name];
65
66 if (!RooNumber::isInfinite(min)) {
67 elem.hasMin = true;
68 elem.min = min;
69 }
70 if (!RooNumber::isInfinite(max)) {
71 elem.hasMax = true;
72 elem.max = max;
73 }
74}
76{
77 auto found = _map.find(var.GetName());
78 if (found != _map.end()) {
79 auto const &elem = found->second;
80 if (elem.hasMin)
81 var.setMin(elem.min);
82 if (elem.hasMax)
83 var.setMax(elem.max);
84 }
85}
87{
88 if (!node.has_child("type") || node["type"].val() != "product_domain") {
89 RooJSONFactoryWSTool::error("only domains of type \"product_domain\" are currently supported!");
90 }
91 for (auto const &varNode : node["axes"].children()) {
92 auto &elem = _map[RooJSONFactoryWSTool::name(varNode)];
93
94 if (varNode.has_child("min")) {
95 elem.min = varNode["min"].val_double();
96 elem.hasMin = true;
97 }
98 if (varNode.has_child("max")) {
99 elem.max = varNode["max"].val_double();
100 elem.hasMax = true;
101 }
102 }
103}
105{
106 node.set_map();
107 node["type"] << "product_domain";
108
109 auto &variablesNode = node["axes"];
110
111 for (auto const &item : _map) {
112 auto const &elem = item.second;
113 RooFit::Detail::JSONNode &varnode = RooJSONFactoryWSTool::appendNamedChild(variablesNode, item.first);
114 if (elem.hasMin)
115 varnode["min"] << elem.min;
116 if (elem.hasMax)
117 varnode["max"] << elem.max;
118 }
119}
121{
122 for (auto const &item : _map) {
123 const auto &name = item.first;
124 if (!ws.var(name)) {
125 const auto &elem = item.second;
126 const double vMin = elem.hasMin ? elem.min : -RooNumber::infinity();
127 const double vMax = elem.hasMax ? elem.max : RooNumber::infinity();
128 ws.import(RooRealVar{name.c_str(), name.c_str(), vMin, vMax});
129 }
130 }
131}
132
133} // namespace Detail
134} // namespace JSONIO
135} // 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
std::map< std::string, ProductDomainElement > _map
Definition Domains.h:64
void populate(RooWorkspace &ws) const
Definition Domains.cxx:120
void readVariable(const char *name, double min, double max)
Definition Domains.cxx:62
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:104
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:86
void populate(RooWorkspace &ws) const
Definition Domains.cxx:28
std::map< std::string, ProductDomain > _map
Definition Domains.h:67
void writeVariable(RooRealVar &) const
Definition Domains.cxx:43
void readVariable(const char *name, double min, double max)
Definition Domains.cxx:35
void writeJSON(RooFit::Detail::JSONNode &) const
Definition Domains.cxx:56
void readJSON(RooFit::Detail::JSONNode const &)
Definition Domains.cxx:48
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
RooRealVar represents a 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.
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