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