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 <cctype>
6#include <string>
7#include <utility>
8
10
11bool startsWith(std::string_view str, std::string_view prefix)
12{
13 return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
14}
15
16bool endsWith(std::string_view str, std::string_view suffix)
17{
18 return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
19}
20
21std::string removeSuffix(std::string_view str, std::string_view suffix)
22{
23 std::string out;
24 out += str;
25 out = out.substr(0, out.length() - suffix.length());
26 return out;
27}
28
29void writeAxisBinning(JSONNode &node, const RooAbsBinning &binning)
30{
31 node["min"] << binning.lowBound();
32 node["max"] << binning.highBound();
33 if (binning.isUniform()) {
34 node["nbins"] << binning.numBins();
35 return;
36 }
37
38 auto &edges = node["edges"].set_seq();
39 edges.append_child() << binning.binLow(0);
40 for (int i = 0; i < binning.numBins(); ++i) {
41 edges.append_child() << binning.binHigh(i);
42 }
43}
44
45// Parse the leading "MAJOR.MINOR" of an HS3 version string like "0.2", "0.2.9" or "0.3".
46bool parseHS3Version(std::string const &version, int &major, int &minor)
47{
48 std::size_t pos = 0;
49 auto readNumber = [&](int &out) {
50 const std::size_t begin = pos;
51 while (pos < version.size() && std::isdigit(static_cast<unsigned char>(version[pos]))) {
52 ++pos;
53 }
54 // Reject empty and absurdly long numbers, which std::stoi could not represent.
55 if (pos == begin || pos - begin > 9) {
56 return false;
57 }
58 out = std::stoi(version.substr(begin, pos - begin));
59 return true;
60 };
61 if (!readNumber(major) || pos >= version.size() || version[pos] != '.') {
62 return false;
63 }
64 ++pos;
65 return readNumber(minor);
66}
67
68// Whether an HS3 version predates `major.minor`. Versions that can't be parsed count as old. That includes the empty
69// version of objects that are imported without a document.
70bool isHS3VersionBefore(std::string const &version, int major, int minor)
71{
72 int versionMajor = 0;
73 int versionMinor = 0;
74 if (!parseHS3Version(version, versionMajor, versionMinor)) {
75 return true;
76 }
77 return std::make_pair(versionMajor, versionMinor) < std::make_pair(major, minor);
78}
bool isHS3VersionBefore(std::string const &version, int major, int minor)
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)
bool parseHS3Version(std::string const &version, int &major, int &minor)
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