Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
ObservableExtractor.cpp
Go to the documentation of this file.
2
3#include <limits>
4#include <set>
5
6namespace {
7
8static std::string obs_name(const ObservableId& obs) {
9 return obs.str();
10}
11
12static bool has_specific_bin(const BinnedObservableId& obs) {
13 return obs.p.first != 0.0 || obs.p.second != 0.0;
14}
15
16static void append_schema_for_values(std::vector<std::string>& cols,
17 std::set<std::string>& seen,
18 const ObservableId& obs,
19 QCDOrder ord,
20 const std::vector<ObservableValue>& vals)
21{
22 auto add_col = [&](std::string key) {
23 if (seen.insert(key).second) {
24 cols.push_back(std::move(key));
25 }
26 };
27
28 if (vals.empty()) {
29 add_col(obs_key(obs, ord));
30 return;
31 }
32
33 if (!vals[0].bin.has_value()) {
34 add_col(obs_key(obs, ord));
35 return;
36 }
37
38 for (const auto& v : vals) {
39 if (!v.bin.has_value()) {
40 add_col(obs_key(obs, ord));
41 continue;
42 }
43
44 const auto& br = v.bin.value();
45 add_col(obs_key_bin(obs, ord, br.first, br.second));
46 }
47}
48
49static void write_values(std::unordered_map<std::string, Value>& outY,
50 const ObservableId& obs,
51 QCDOrder ord,
52 const std::vector<ObservableValue>& vals)
53{
54 if (vals.empty()) {
55 outY[obs_key(obs, ord)] = std::numeric_limits<double>::quiet_NaN();
56 return;
57 }
58
59 if (!vals[0].bin.has_value()) {
60 outY[obs_key(obs, ord)] = vals[0].value;
61 return;
62 }
63
64 for (const auto& v : vals) {
65 if (!v.bin.has_value()) {
66 outY[obs_key(obs, ord)] = v.value;
67 continue;
68 }
69
70 const auto& br = v.bin.value();
71 outY[obs_key_bin(obs, ord, br.first, br.second)] = v.value;
72 }
73}
74
75} // namespace
76
77std::string obs_key(const ObservableId& obs, QCDOrder ord) {
78 std::string k = "OBS:";
79 k += obs_name(obs);
80 k += ":";
81 k += OrderMapper::str(ord);
82 return k;
83}
84
85std::string obs_key_bin(const ObservableId& obs,
86 QCDOrder ord,
87 double bmin,
88 double bmax)
89{
90 std::string k = obs_key(obs, ord);
91 k += ":BIN[";
92 k += std::to_string(bmin);
93 k += ",";
94 k += std::to_string(bmax);
95 k += "]";
96 return k;
97}
98
99std::vector<std::string> ObservableExtractor::schema(const OutputSpec& /*spec*/) const {
100 std::vector<std::string> cols;
101 std::set<std::string> seen;
102
103 oi_.enable_obs();
104
105 auto current = oi_.get_current_observables();
106 std::sort(current.begin(), current.end());
107
108 for (const auto& obs : current) {
109 if (has_specific_bin(obs)) {
110 const auto key = obs_key_bin(obs.s, ord_, obs.p.first, obs.p.second);
111 if (seen.insert(key).second) {
112 cols.push_back(key);
113 }
114 continue;
115 }
116
117 auto vals = oi_.compute_observable(obs.s);
118 append_schema_for_values(cols, seen, obs.s, ord_, vals);
119 }
120
121 return cols;
122}
123
124void ObservableExtractor::extract(std::unordered_map<std::string, Value>& outY,
125 const OutputSpec& /*spec*/) const
126{
127 oi_.enable_obs();
128
129 auto current = oi_.get_current_observables();
130 std::sort(current.begin(), current.end());
131
132 for (const auto& obs : current) {
133 if (has_specific_bin(obs)) {
134 auto v = oi_.compute_observable(obs);
135
136 const auto br = v.bin.value_or(obs.p);
137 outY[obs_key_bin(obs.s, ord_, br.first, br.second)] = v.value;
138 continue;
139 }
140
141 auto vals = oi_.compute_observable(obs.s);
142 write_values(outY, obs.s, ord_, vals);
143 }
144}
QCDOrder
std::string obs_key(const ObservableId &obs, QCDOrder ord)
std::string obs_key_bin(const ObservableId &obs, QCDOrder ord, double bmin, double bmax)
std::string obs_key_bin(const ObservableId &obs, QCDOrder ord, double bmin, double bmax)
static std::string str(const IdOf< QCDOrderTag > &id)
Returns the string representation associated with an identifier.
std::vector< std::string > schema(const OutputSpec &spec) const override
void extract(std::unordered_map< std::string, Value > &outY, const OutputSpec &spec) const override
std::vector< BinnedObservableId > get_current_observables()
Return the set of currently registered observables.
std::vector< ObservableValue > compute_observable(Observables obs) const
Compute a theory prediction for an observable (enum API).
void enable_obs()
Force enabling/re-enabling currently registered observables.
const std::string & str() const
Returns the underlying string.
csl::Expr v
Definition sm.h:110
Identifies an observable together with a numerical bin.
std::pair< double, double > p