Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
ParamBlockLoader.cpp
Go to the documentation of this file.
1#include "ParamBlockLoader.h"
2#include "LhaDBNodeProvider.h"
3
4
5static double value_to_double(const DBNode::Value& v, double fallback = 0.0)
6{
7 if (std::holds_alternative<double>(v))
8 return std::get<double>(v);
9 if (std::holds_alternative<int>(v))
10 return static_cast<double>(std::get<int>(v));
11 return fallback;
12}
13
14static scalar_t node_to_scalar(const std::shared_ptr<DBNode>& node)
15{
16 const double real_part = value_to_double(node->get("central_value"));
17 const double imaginary_part = node->contains("imaginary_value")
18 ? value_to_double(node->get("imaginary_value"))
19 : 0.0;
20 return scalar_t(real_part, imaginary_part);
21}
22
24 size_t itemCount,
25 size_t valueIdx,
26 int scaleIdx,
27 int rgIdx,
28 int binIdx,
29 bool globalScale)
30{
31 lha_prototypes.push_back({blockName, itemCount, valueIdx, scaleIdx, rgIdx, binIdx, globalScale});
32}
33
34void ParamBlockLoader::add_lha_prototypes(const std::vector<LhaPrototypeSpec>& prototypes)
35{
36 for (const auto& prototype : prototypes) {
37 add_lha_prototype(prototype.blockName,
38 prototype.itemCount,
39 prototype.valueIdx,
40 prototype.scaleIdx,
41 prototype.rgIdx,
42 prototype.binIdx,
43 prototype.globalScale);
44 }
45}
46
47void ParamBlockLoader::apply_lha_prototypes(std::shared_ptr<IDBNodeProvider> provider) const
48{
49 auto lha_provider = std::dynamic_pointer_cast<LhaDBNodeProvider>(provider);
50 if (!lha_provider) {
51 return;
52 }
53
54 for (const auto& prototype : lha_prototypes) {
55 lha_provider->add_lha_prototype(prototype.blockName,
56 prototype.itemCount,
57 prototype.valueIdx,
58 prototype.scaleIdx,
59 prototype.rgIdx,
60 prototype.binIdx,
61 prototype.globalScale);
62 }
63}
64
65void ParamBlockLoader::load(std::shared_ptr<BlockAccessor> dest, fs::path src_file, bool block_in_blocks) {
66 LOG_DEBUG("Loading parameter blocks from", src_file.string());
68 apply_lha_prototypes(np);
69 auto src = np->provide_db_as_node();
70
71 if (block_in_blocks) {
72 for (auto &bk : src->get_keys()) {
73 auto exp = src->getGroup({bk});
74 for (auto &group_pair : exp) {
75 auto group = src->getGroup({bk, group_pair.first});
76
77 auto block = std::make_shared<Block>();
78 block->blockname = bk + "_" + group_pair.first;
79 for (auto &vk : group) {
80 const auto& key = vk.first;
81 const auto& val = vk.second;
82
83 if (key == "scale") {
84 if (!block->has_scale()) {
85 if (std::holds_alternative<double>(val)) {
86 block->set_scale(std::get<double>(val));
87 } else if (std::holds_alternative<int>(val)) {
88 block->set_scale(static_cast<double>(std::get<int>(val)));
89 } else {
90 LOG_WARN("ParamBlockLoader", "Non-numeric block scale under ", bk);
91 }
92 }
93 continue;
94 }
95
96 if (!std::holds_alternative<std::shared_ptr<DBNode>>(val)) {
97 LOG_WARN("ParamBlockLoader", "Unexpected non-node entry under ", bk, " key ", key, " — skipping");
98 continue;
99 }
100
101 auto node = std::get<std::shared_ptr<DBNode>>(val);
102
103 if (!node->contains("central_value")) {
104 LOG_ERROR("ParamBlockLoader", "DBNode doesn't have all necessary keys (central_value).");
105 continue;
106 }
107
108 auto value = node->get("central_value");
109 if (std::holds_alternative<BlockName>(value)) {
110 continue;
111 }
112
113 scalar_t val_central = node_to_scalar(node);
114
115 auto stat = node->contains("stat_error") ? node->get("stat_error") : DBNode::Value{0.0};
116 auto syst = node->contains("syst_error") ? node->get("syst_error") : DBNode::Value{0.0};
117
118 double stat_d = std::holds_alternative<double>(stat) ? std::get<double>(stat)
119 : std::holds_alternative<int>(stat) ? static_cast<double>(std::get<int>(stat))
120 : 0.0;
121 double syst_d = std::holds_alternative<double>(syst) ? std::get<double>(syst)
122 : std::holds_alternative<int>(syst) ? static_cast<double>(std::get<int>(syst))
123 : 0.0;
124
125 block->store(
126 LhaID(vk.first),
127 std::make_shared<Parameter>(Parameter(ParamId(bk, LhaID(vk.first)),
128 val_central, stat_d, syst_d))
129 );
130
131 if (node->contains("scale")) {
132 auto scale = node->get("scale");
133 if (std::holds_alternative<double>(scale))
134 block->retrieve(LhaID(vk.first))->set_scale(std::get<double>(scale));
135 else if (std::holds_alternative<int>(scale))
136 block->retrieve(LhaID(vk.first))->set_scale(static_cast<double>(std::get<int>(scale)));
137 }
138
139 if (node->contains("bin_low") || node->contains("bin_high")) {
140 if (!(node->contains("bin_low") && node->contains("bin_high")))
141 LOG_ERROR("LogicError", "Missing one end of the binning.");
142
143 auto bin_low = node->get("bin_low");
144 auto bin_high = node->get("bin_high");
145
146 double d_bin_low = value_to_double(bin_low);
147 double d_bin_high = value_to_double(bin_high);
148
149 block->retrieve(LhaID(vk.first))->set_bin(std::pair(d_bin_low, d_bin_high));
150 }
151 }
152 dest->emplace(block->blockname, block);
153 }
154 }
155 return;
156 }
157
158 for (auto &bk : src->get_keys()) {
159 auto block = std::make_shared<Block>();
160 block->blockname = bk;
161 LOG_DEBUG("Loading block", bk);
162
163 auto group = src->getGroup({bk});
164
165 for (auto &vk : group) {
166 const auto& key = vk.first;
167 const auto& val = vk.second;
168
169 if (key == "scale") {
170 if (!block->has_scale()) {
171 if (std::holds_alternative<double>(val)) {
172 block->set_scale(std::get<double>(val));
173 } else if (std::holds_alternative<int>(val)) {
174 block->set_scale(static_cast<double>(std::get<int>(val)));
175 } else {
176 LOG_WARN("ParamBlockLoader", "Non-numeric block scale under ", bk);
177 }
178 }
179 continue;
180 }
181
182 if (!std::holds_alternative<std::shared_ptr<DBNode>>(val)) {
183 LOG_WARN("ParamBlockLoader", "Unexpected non-node entry under ", bk, " key ", key, " — skipping");
184 continue;
185 }
186
187 auto node = std::get<std::shared_ptr<DBNode>>(val);
188
189 if (!node->contains("central_value")) {
190 LOG_ERROR("ParamBlockLoader", "DBNode doesn't have all necessary keys (central_value).");
191 continue;
192 }
193
194 auto value = node->get("central_value");
195 if (std::holds_alternative<BlockName>(value)) {
196 continue;
197 }
198
199 scalar_t val_central = node_to_scalar(node);
200
201 auto stat = node->contains("stat_error") ? node->get("stat_error") : DBNode::Value{0.0};
202 auto syst = node->contains("syst_error") ? node->get("syst_error") : DBNode::Value{0.0};
203
204 double stat_d = std::holds_alternative<double>(stat) ? std::get<double>(stat)
205 : std::holds_alternative<int>(stat) ? static_cast<double>(std::get<int>(stat))
206 : 0.0;
207 double syst_d = std::holds_alternative<double>(syst) ? std::get<double>(syst)
208 : std::holds_alternative<int>(syst) ? static_cast<double>(std::get<int>(syst))
209 : 0.0;
210
211 block->store(
212 LhaID(vk.first),
213 std::make_shared<Parameter>(Parameter(ParamId(bk, LhaID(vk.first)),
214 val_central, stat_d, syst_d))
215 );
216
217 if (node->contains("scale")) {
218 auto scale = node->get("scale");
219 if (std::holds_alternative<double>(scale))
220 block->retrieve(LhaID(vk.first))->set_scale(std::get<double>(scale));
221 else if (std::holds_alternative<int>(scale))
222 block->retrieve(LhaID(vk.first))->set_scale(static_cast<double>(std::get<int>(scale)));
223 }
224
225 if (node->contains("bin_low") || node->contains("bin_high")) {
226 if (!(node->contains("bin_low") && node->contains("bin_high")))
227 LOG_ERROR("LogicError", "Missing one end of the binning.");
228
229 auto bin_low = node->get("bin_low");
230 auto bin_high = node->get("bin_high");
231
232 double d_bin_low = value_to_double(bin_low);
233 double d_bin_high = value_to_double(bin_high);
234
235 block->retrieve(LhaID(vk.first))->set_bin(std::pair(d_bin_low, d_bin_high));
236 }
237 }
238
239 dest->emplace(bk, block);
240 }
241
242 LOG_DEBUG("Parameter blocks loaded");
243}
IDBNodeProvider implementation backed by an LHA/SLHA/FLHA file.
#define LOG_ERROR(type,...)
Macro for logging error messages and terminating the application.
Definition Logger.h:41
#define LOG_DEBUG(...)
Macro for logging debug messages.
Definition Logger.h:45
#define LOG_WARN(...)
Macro for logging warning messages.
Definition Logger.h:40
Loads parameter blocks from a file into a BlockAccessor.
Block identifier with alias support.
Definition BlockName.h:59
static std::shared_ptr< IDBNodeProvider > createDBNodeProvider(fs::path src_path)
Creates an IDBNodeProvider for a given file path.
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
void add_lha_prototypes(const std::vector< LhaPrototypeSpec > &prototypes) override
Registers several additional LHA prototypes.
void add_lha_prototype(BlockName blockName, size_t itemCount=2, size_t valueIdx=1, int scaleIdx=-1, int rgIdx=-1, int binIdx=-1, bool globalScale=false) override
Registers an additional LHA prototype to apply before parsing LHA files.
void load(std::shared_ptr< BlockAccessor > dest, fs::path src_file, bool block_in_blocks=false) override
Loads parameter blocks from a file into the given BlockAccessor.
Represents a single parameter with value, uncertainties, and dependency links.
Definition Parameter.h:78
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