Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
YamlInputReader.cpp
Go to the documentation of this file.
1#include "YamlInputReader.h"
2
3YamlInputReader::YamlInputReader(const std::string& filename) {
4 root_ = yaml_p.readFromFile(filename);
5 if (!root_) {
6 throw std::runtime_error("YamlInputReader: root DBNode is null after parsing file: " + filename);
7 }
8}
9
10std::string YamlInputReader::unquote(std::string s) {
11 if (s.size() >= 2) {
12 char a = s.front();
13 char b = s.back();
14 if ((a == '"' && b == '"') || (a == '\'' && b == '\'')) {
15 return s.substr(1, s.size() - 2);
16 }
17 }
18 return s;
19}
20
21std::string YamlInputReader::as_string(const DBNode::Value& v, const std::string& ctx) {
22 if (std::holds_alternative<BlockName>(v)) {
23 return std::get<BlockName>(v).to_string();
24 }
25
26 throw std::runtime_error("YamlInputReader: expected string(BlockName) for " + ctx);
27}
28
29double YamlInputReader::as_double(const DBNode::Value& v, const std::string& ctx) {
30 if (std::holds_alternative<double>(v)) return std::get<double>(v);
31 if (std::holds_alternative<int>(v)) return static_cast<double>(std::get<int>(v));
32 throw std::runtime_error("YamlInputReader: expected number(int/double) for " + ctx);
33}
34
35std::shared_ptr<DBNode> YamlInputReader::as_node(const DBNode::Value& v, const std::string& ctx) {
36 if (std::holds_alternative<std::shared_ptr<DBNode>>(v)) {
37 return std::get<std::shared_ptr<DBNode>>(v);
38 }
39 throw std::runtime_error("YamlInputReader: expected DBNode for " + ctx);
40}
41
42std::vector<YamlInputParam> YamlInputReader::get_input_params() const {
43 std::vector<YamlInputParam> out;
44
45 auto pSpecsNode = as_node(root_->get("p_specs"), "p_specs");
46 auto items = pSpecsNode->getGroup({});
47
48 out.reserve(items.size());
49
50 for (const auto& [idxKey, itemVal] : items) {
51 (void)idxKey;
52
53 auto itemNode = as_node(itemVal, "p_specs item");
54
55 auto block_raw = as_string(itemNode->get("block"), "p_specs.block");
56 auto code_raw = as_string(itemNode->get("code"), "p_specs.code");
57
58 std::string block = unquote(block_raw);
59 std::string code = unquote(code_raw);
60
61 out.push_back(YamlInputParam{
62 .block_name = block,
63 .pdg_code = LhaID(code)
64 });
65 }
66
67 return out;
68}
69
70std::vector<YamlScanParam> YamlInputReader::get_scan_params() const {
71 std::vector<YamlScanParam> out;
72
73 auto scanNode = as_node(root_->get("scan_params"), "scan_params");
74 auto items = scanNode->getGroup({});
75
76 out.reserve(items.size());
77
78 for (const auto& [idxKey, itemVal] : items) {
79 (void)idxKey;
80
81 auto itemNode = as_node(itemVal, "scan_params item");
82
83 auto block_raw = as_string(itemNode->get("block"), "scan_params.block");
84 auto code_raw = as_string(itemNode->get("code"), "scan_params.code");
85
86 double minv = as_double(itemNode->get("min_val"), "scan_params.min_val");
87 double maxv = as_double(itemNode->get("max_val"), "scan_params.max_val");
88 double step = as_double(itemNode->get("step"), "scan_params.step");
89
90 std::string block = unquote(block_raw);
91 std::string code = unquote(code_raw);
92
93 out.push_back(YamlScanParam{
94 .block_name = block,
95 .pdg_code = LhaID(code),
96 .min_val = minv,
97 .max_val = maxv,
98 .step_val = step
99 });
100 }
101
102 return out;
103}
std::variant< BlockName, int, double, bool, std::shared_ptr< DBNode >, std::vector< std::shared_ptr< DBNode > > > Value
Variant type used to store values in the tree.
Definition DBNode.h:45
std::shared_ptr< DBNode > readFromFile(const std::string &filename) const override
Reads and parses YAML data from a file.
YamlInputReader(const std::string &filename)
std::vector< YamlInputParam > get_input_params() const
std::vector< YamlScanParam > get_scan_params() const
csl::Expr v
Definition sm.h:110
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56
std::string block_name
std::string block_name