Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
SourceView.cpp
Go to the documentation of this file.
1#include "SourcesView.h"
2
3std::string join(const std::vector<std::string>& v) {
4 std::ostringstream oss;
5 for (size_t i = 0; i < v.size(); ++i) { if (i) oss << ", "; oss << v[i]; }
6 return oss.str();
7}
8
9std::string to_string(const LhaID& id) {
10 return id.to_string();
11}
12
13std::string to_string(const ParamId& id) {
14 std::ostringstream oss;
15 oss << ParameterTypeMapper::str(id.type.value()) << "::" << id.block << "::" << id.code.to_string();
16 return oss.str();
17}
18
19BlockSrc::BlockSrc(const std::unordered_map<std::string, std::shared_ptr<Block>>& m,
20 std::string ctx)
21 : m_(&m), ctx_(std::move(ctx)) {}
22
23bool BlockSrc::has_block(std::string_view name) const {
24 return m_->find(std::string(name)) != m_->end();
25}
26
27const std::shared_ptr<Block>& BlockSrc::block(std::string_view name) const {
28 auto it = m_->find(std::string(name));
29 if (it == m_->end()) {
30 std::vector<std::string> avail;
31 avail.reserve(m_->size());
32 for (auto& [k,_] : *m_) avail.push_back(k);
33 std::ostringstream oss;
34 oss << "Missing source Block '" << name << "'";
35 if (!ctx_.empty()) oss << " in " << ctx_;
36 oss << ". Available blocks: [" << join(avail) << "].";
37 throw std::invalid_argument(oss.str());
38 }
39 return it->second;
40}
41
42std::shared_ptr<Parameter> BlockSrc::get_param(std::string_view blk, const LhaID& code) const {
43 auto& b = block(blk);
44 return b->retrieve(code);
45}
46
47std::shared_ptr<Parameter> BlockSrc::get_param(std::string_view blk, int code) const {
48 return get_param(blk, LhaID(code));
49}
50std::shared_ptr<Parameter> BlockSrc::get_param(std::string_view blk, std::pair<int,int> code) const {
51 return get_param(blk, LhaID(code.first, code.second));
52}
53
54scalar_t BlockSrc::get_val(std::string_view blk, std::initializer_list<long> code) const {
55 return get_param(blk, LhaID{std::vector<long>(code)})->get_val();
56}
57
58scalar_t BlockSrc::get_val(std::string_view blk,
59 std::initializer_list<std::size_t> code) const {
60 std::vector<long> v;
61 v.reserve(code.size());
62 for (auto x : code) v.push_back(static_cast<long>(x));
63 return get_param(blk, LhaID{std::move(v)})->get_val();
64}
65
66scalar_t BlockSrc::get_val(std::string_view blk, std::initializer_list<int> code) const {
67 std::vector<long> v;
68 v.reserve(code.size());
69 for (int x : code) v.push_back(static_cast<long>(x));
70 return get_val(blk, LhaID{std::move(v)});
71}
72
73scalar_t BlockSrc::get_val(std::string_view blk, const LhaID& code) const {
74 return get_param(blk, code)->get_val();
75}
76
77scalar_t BlockSrc::get_val(std::string_view blk, int code) const {
78 return get_param(blk, LhaID(code))->get_val();
79}
80
81scalar_t BlockSrc::get_val(std::string_view blk, std::pair<int,int> code) const {
82 return get_param(blk, LhaID(code.first, code.second))->get_val();
83}
84
85const std::unordered_map<std::string, std::shared_ptr<Block>>& BlockSrc::raw() const { return *m_; }
86
87
88
89ParamSrc::ParamSrc(const std::unordered_map<ParamId, std::shared_ptr<Parameter>>& m,
90 std::string ctx)
91 : m_(&m), ctx_(std::move(ctx)) {}
92
93bool ParamSrc::has(const ParamId& id) const { return m_->find(id) != m_->end(); }
94
95const std::shared_ptr<Parameter>& ParamSrc::require(const ParamId& id) const {
96 auto it = m_->find(id);
97 if (it == m_->end()) {
98 std::vector<std::string> avail;
99 avail.reserve(m_->size());
100 for (auto& [k,_] : *m_) avail.push_back(to_string(k));
101 std::ostringstream oss;
102 oss << "Missing source Parameter '" << to_string(id) << "'";
103 if (!ctx_.empty()) oss << " in " << ctx_;
104 oss << ". Available: [" << join(avail) << "].";
105 throw std::invalid_argument(oss.str());
106 }
107 return it->second;
108}
109
110std::shared_ptr<Parameter> ParamSrc::get_param(const ParamId& id) const { return require(id); }
111scalar_t ParamSrc::get_val(const ParamId& id) const { return require(id)->get_val(); }
112
113scalar_t ParamSrc::get_val(ParameterType t, std::string_view block, std::initializer_list<long> code) const {
114 LhaID id{std::vector<long>(code)};
115 return get_val(ParamId{t, std::string(block), id});
116}
117scalar_t ParamSrc::get_val(ParameterType t, std::string_view block, const LhaID& code) const {
118 return get_val(ParamId{t, std::string(block), code});
119}
120scalar_t ParamSrc::get_val(ParameterType t, std::string_view block, int code) const {
121 return get_val(ParamId{t, std::string(block), LhaID(code)});
122}
123scalar_t ParamSrc::get_val(ParameterType t, std::string_view block, std::pair<int,int> code) const {
124 return get_val(ParamId{t, std::string(block), LhaID(code.first, code.second)});
125}
126
127const std::unordered_map<ParamId, std::shared_ptr<Parameter>>& ParamSrc::raw() const { return *m_; }
128
129BlockSrc as_block_src(const std::unordered_map<std::string, std::shared_ptr<Block>>& m, std::string ctx) {
130 return BlockSrc(m, std::move(ctx));
131}
132ParamSrc as_param_src(const std::unordered_map<ParamId, std::shared_ptr<Parameter>>& m, std::string ctx) {
133 return ParamSrc(m, std::move(ctx));
134}
ParameterType
ParamSrc as_param_src(const std::unordered_map< ParamId, std::shared_ptr< Parameter > > &m, std::string ctx)
Helper function to build a ParamSrc view from a parameter map.
std::string to_string(const LhaID &id)
Convenience stringification for LhaID.
Definition SourceView.cpp:9
std::string join(const std::vector< std::string > &v)
Joins a list of strings with ", ".
Definition SourceView.cpp:3
BlockSrc as_block_src(const std::unordered_map< std::string, std::shared_ptr< Block > > &m, std::string ctx)
Helper function to build a BlockSrc view from a block map.
std::string to_string(const LhaID &id)
Convenience stringification for LhaID.
Definition SourceView.cpp:9
std::string join(const std::vector< std::string > &v)
Joins a list of strings with ", ".
Definition SourceView.cpp:3
Lightweight view over a set of source blocks.
Definition SourcesView.h:71
const std::unordered_map< std::string, std::shared_ptr< Block > > & raw() const
Returns the underlying map reference.
scalar_t get_val(std::string_view blk, std::initializer_list< long > code) const
Retrieves the value of a parameter given as an initializer_list.
BlockSrc(const std::unordered_map< std::string, std::shared_ptr< Block > > &m, std::string ctx={})
Constructs a BlockSrc view from an existing map.
const std::shared_ptr< Block > & block(std::string_view name) const
Returns a reference to a block by name.
std::shared_ptr< Parameter > get_param(std::string_view blk, const LhaID &code) const
Retrieves a parameter from a given block by LhaID.
bool has_block(std::string_view name) const
Checks whether a block with a given name is present.
static std::string str(const IdOf< ParamTypeTag > &id)
Returns the string representation associated with an identifier.
Lightweight view over a set of source parameters keyed by ParamId.
const std::shared_ptr< Parameter > & require(const ParamId &id) const
Returns a parameter reference or throws if missing.
ParamSrc()=default
Default constructor: creates an empty, unbound view.
std::shared_ptr< Parameter > get_param(const ParamId &id) const
Convenience wrapper for require().
const std::unordered_map< ParamId, std::shared_ptr< Parameter > > & raw() const
Access to the underlying parameter map.
scalar_t get_val(const ParamId &id) const
Retrieves the current value of a parameter.
bool has(const ParamId &id) const
Checks whether a parameter with the given ID exists.
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56
Composite identifier for a single parameter.
Definition ParamID.h:57