Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONIOUtils.cxx
Go to the documentation of this file.
1#include "JSONIOUtils.h"
2
3#include <RooAbsBinning.h>
4
5#include <string>
6
9
10bool startsWith(std::string_view str, std::string_view prefix)
11{
12 return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
13}
14
15bool endsWith(std::string_view str, std::string_view suffix)
16{
17 return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
18}
19
20std::string removeSuffix(std::string_view str, std::string_view suffix)
21{
22 std::string out;
23 out += str;
24 out = out.substr(0, out.length() - suffix.length());
25 return out;
26}
27
28std::unique_ptr<RooFit::Detail::JSONTree> varJSONString(const JSONNode &treeRoot)
29{
30 std::string varName = treeRoot.find("name")->val();
31 double val = 0;
32 double maxVal = 0;
33 double minVal = 0;
34 bool isConstant = false;
35 bool isRange = false;
36
37 if (auto n = treeRoot.find("value")) {
38 val = n->val_double();
39 isConstant = true;
40 }
41
42 auto maxNode = treeRoot.find("max");
43 auto minNode = treeRoot.find("min");
44 if (maxNode && minNode) {
45 maxVal = maxNode->val_double();
46 minVal = minNode->val_double();
47 isRange = true;
48 }
49 if (!isConstant) {
50 val = (maxVal + minVal) / 2;
51 }
52 // Check if variable is at least a range or constant else throw error
53 if (!isConstant && !isRange) {
54 throw std::invalid_argument("Invalid Syntax: Please provide either 'value' or 'min' and 'max' or both");
55 }
56
57 std::unique_ptr<RooFit::Detail::JSONTree> jsonDict = RooFit::Detail::JSONTree::create();
58 JSONNode &n = jsonDict->rootnode().set_map();
59 JSONNode &_domains = n["domains"].set_seq().append_child().set_map();
60 JSONNode &_parameterPoints = n["parameter_points"].set_seq().append_child().set_map();
61
62 _domains["name"] << "default_domain";
63 _domains["type"] << "product_domain";
64 JSONNode &_axes = _domains["axes"].set_seq().append_child().set_map();
65 _axes["name"] << varName;
66
67 _parameterPoints["name"] << "default_values";
68 JSONNode &_parameters = _parameterPoints["parameters"].set_seq().append_child().set_map();
69 _parameters["name"] << varName;
70 _parameters["value"] << val;
71
72 if (isRange) {
73 _axes["max"] << maxVal;
74 _axes["min"] << minVal;
75 }
76
77 if (isConstant && !isRange) {
78 _parameters["const"] << true;
79 JSONNode &_misc = n["misc"].set_map();
80 JSONNode &rootInternal = _misc["ROOT_internal"].set_map();
81 JSONNode &_var = rootInternal[varName].set_map();
82 _var["tags"] << "Constant";
83 }
84
85 return jsonDict;
86}
87
88void writeAxisBinning(JSONNode &node, const RooAbsBinning &binning)
89{
90 if (binning.isUniform()) {
91 node["min"] << binning.lowBound();
92 node["max"] << binning.highBound();
93 node["nbins"] << binning.numBins();
94 return;
95 }
96
97 auto &edges = node["edges"].set_seq();
98 edges.append_child() << binning.binLow(0);
99 for (int i = 0; i < binning.numBins(); ++i) {
100 edges.append_child() << binning.binHigh(i);
101 }
102}
std::unique_ptr< RooFit::Detail::JSONTree > varJSONString(const JSONNode &treeRoot)
bool startsWith(std::string_view str, std::string_view prefix)
bool endsWith(std::string_view str, std::string_view suffix)
std::string removeSuffix(std::string_view str, std::string_view suffix)
void writeAxisBinning(JSONNode &node, const RooAbsBinning &binning)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
virtual JSONNode & set_seq()=0
static std::unique_ptr< JSONTree > create()
const Int_t n
Definition legend1.C:16