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