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 removeSuffix(std::string_view str, std::string_view suffix)
19{
20 std::string out;
21 out += str;
22 out = out.substr(0, out.length() - suffix.length());
23 return out;
24}
25
26std::unique_ptr<RooFit::Detail::JSONTree> varJSONString(const JSONNode &treeRoot)
27{
28 std::string varName = treeRoot.find("name")->val();
29 double val = 0;
30 double maxVal = 0;
31 double minVal = 0;
32 bool isConstant = false;
33 bool isRange = false;
34
35 if (auto n = treeRoot.find("value")) {
36 val = n->val_double();
37 isConstant = true;
38 }
39
40 auto maxNode = treeRoot.find("max");
41 auto minNode = treeRoot.find("min");
42 if (maxNode && minNode) {
43 maxVal = maxNode->val_double();
44 minVal = minNode->val_double();
45 isRange = true;
46 }
47 if (!isConstant) {
48 val = (maxVal + minVal) / 2;
49 }
50 // Check if variable is at least a range or constant else throw error
51 if (!isConstant && !isRange) {
52 throw std::invalid_argument("Invalid Syntax: Please provide either 'value' or 'min' and 'max' or both");
53 }
54
55 std::unique_ptr<RooFit::Detail::JSONTree> jsonDict = RooFit::Detail::JSONTree::create();
56 JSONNode &n = jsonDict->rootnode().set_map();
57 JSONNode &_domains = n["domains"].set_seq().append_child().set_map();
58 JSONNode &_parameterPoints = n["parameter_points"].set_seq().append_child().set_map();
59
60 _domains["name"] << "default_domain";
61 _domains["type"] << "product_domain";
62 JSONNode &_axes = _domains["axes"].set_seq().append_child().set_map();
63 _axes["name"] << varName;
64
65 _parameterPoints["name"] << "default_values";
66 JSONNode &_parameters = _parameterPoints["parameters"].set_seq().append_child().set_map();
67 _parameters["name"] << varName;
68 _parameters["value"] << val;
69
70 if (isRange) {
71 _axes["max"] << maxVal;
72 _axes["min"] << minVal;
73 }
74
75 if (isConstant && !isRange) {
76 _parameters["const"] << true;
77 JSONNode &_misc = n["misc"].set_map();
78 JSONNode &rootInternal = _misc["ROOT_internal"].set_map();
79 JSONNode &_var = rootInternal[varName].set_map();
80 _var["tags"] << "Constant";
81 }
82
83 return jsonDict;
84}
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)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
virtual JSONNode & set_seq()=0
static std::unique_ptr< JSONTree > create()
const Int_t n
Definition legend1.C:16