Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
observable_ids.cpp
Go to the documentation of this file.
1#include "observable_ids.hpp"
2
4#include "decay_ids.hpp"
5#include "ObservableTypeId.h"
6
7#include <optional>
8#include <stdexcept>
9#include <utility>
10
11namespace {
12
13std::optional<LhaID> canonicalize_legacy_polarization_id(const LhaID& ext) {
14 auto parts = ext.get_parts();
15 if (parts.size() < 2) {
16 return std::nullopt;
17 }
18
19 if (parts[1] == kLegacyFermionPolarizationTypeId) {
21 } else if (parts[1] == kLegacyLongitudinalVectorPolarizationTypeId) {
24 1
25 );
26 } else {
27 return std::nullopt;
28 }
29
30 return LhaID(std::move(parts));
31}
32
33} // namespace
34
36 const auto key = nk(s);
37
38 for (const auto& [e, name] : observable_mapping()) {
39 if (nk(name) == key) {
40 return e;
41 }
42 }
43
44 throw std::out_of_range("Unknown builtin observable: " + std::string(s));
45}
46
48 auto maybeId = from_flha(ext);
49 if (!maybeId) {
50 throw std::out_of_range("Unknown observable (FLHA): " + ext.to_string());
51 }
52
53 auto maybeEnum = enum_of(*maybeId);
54 if (!maybeEnum) {
55 throw std::out_of_range("ObservableId has no builtin enum: " + maybeId->str());
56 }
57
58 return *maybeEnum;
59}
60
61std::optional<ObservableId> ObservableMapper::from_flha(const LhaID& ext) {
62 if (auto direct = from_external(ext)) {
63 return direct;
64 }
65
66 const auto canonical = canonicalize_legacy_polarization_id(ext);
67 if (!canonical) {
68 return std::nullopt;
69 }
70 return from_external(*canonical);
71}
72
73std::optional<LhaID> ObservableMapper::flha_of(const ObservableId& id) {
74 return external_of(id);
75}
76
77std::optional<BinnedObservableId> ObservableMapper::from_binned_flha(const LhaID& ext) {
78 try {
80 } catch (const std::exception&) {
81 return std::nullopt;
82 }
83}
84
86 try {
87 return id.flha();
88 } catch (const std::exception&) {
89 return std::nullopt;
90 }
91}
92
94 auto out = from_binned_flha(ext);
95 if (!out) {
96 throw std::runtime_error("Unknown binned observable (FLHA): " + ext.to_string());
97 }
98 return *out;
99}
100
104
105bool ObservableMapper::register_custom(const std::string& canonical,
106 std::vector<std::string> aliases,
107 std::optional<LhaID> ext,
108 const DecayId& parent_decay)
109{
110 // Resolve through DecayMapper so aliases are canonicalized and unknown
111 // parent decays are rejected before observable registration.
112 const DecayId canonical_decay = DecayMapper::id_of(parent_decay.str());
113
114 const bool ok = Base::register_custom(
115 canonical,
116 std::move(aliases),
117 std::move(ext)
118 );
119
120 if (!ok) {
121 return false;
122 }
123
124 const ObservableId obs_id = Base::id_of(canonical);
125 DecayGraph::instance().link(canonical_decay, obs_id);
126 return true;
127}
128
129bool ObservableMapper::register_custom(const std::string& canonical,
130 std::vector<std::string> aliases,
131 std::optional<LhaID> ext,
132 Decays parent_decay_enum)
133{
134 return register_custom(
135 canonical,
136 std::move(aliases),
137 std::move(ext),
138 DecayMapper::to_id(parent_decay_enum)
139 );
140}
141
142bool ObservableMapper::register_custom(const std::string& canonical,
143 std::vector<std::string> aliases,
144 std::optional<LhaID> ext,
145 std::string_view parent_decay_str)
146{
147 return register_custom(
148 canonical,
149 std::move(aliases),
150 std::move(ext),
151 DecayMapper::id_of(parent_decay_str)
152 );
153}
154
156 auto k = flha_of(id);
157 if (!k) {
158 throw std::runtime_error("Observable has no FLHA: " + id.str());
159 }
160 return *k;
161}
162
164 auto k = binned_flha_of(id);
165 if (!k) {
166 throw std::runtime_error("BinnedObservableId has no FLHA: " + id.str());
167 }
168 return *k;
169}
Utilities for identifying observables together with numerical bins.
Observables
Definition GeneralEnum.h:4
Decays
const std::map< Observables, std::string > & observable_mapping()
Returns the mapping between Observables and their string names.
Definition Map.cpp:21
const std::map< Observables, LhaID > & observable_flha_mapping()
Returns the mapping between Observables and their FLHA identifiers.
Definition Map.cpp:566
Helpers for project-defined FLHA observable-type identifiers.
constexpr long kLegacyFermionPolarizationTypeId
Legacy v1.0.0–v1.0.1 type used for charged-lepton polarization.
constexpr long kLegacyLongitudinalVectorPolarizationTypeId
Legacy v1.0.0–v1.0.1 type used for longitudinal vector polarization.
constexpr long make_polarization_type_id(PolarizationKind kind, unsigned int daughter_position)
Build a project-defined FLHA polarization type id using 92ij.
static DecayGraph & instance()
Return the unique global graph instance.
void link(const DecayId &decay, const ObservableId &obs)
Link an observable to a parent decay.
static bool register_custom(std::string canonical, std::vector< std::string > aliases={}, std::optional< LhaID > ext=std::nullopt)
Registers a custom entry with optional external key.
static std::optional< LhaID > external_of(const IdOf< ObservableTag > &id)
Returns the external key associated with a given identifier.
static std::optional< Observables > enum_of(const IdOf< ObservableTag > &id)
Attempts to recover the enum value associated with an identifier.
static std::optional< IdOf< ObservableTag > > from_external(const LhaID &k)
Resolves an external key into an identifier.
static IdOf< DecayTag > to_id(Decays e)
Converts an enum value to an IdOf<Tag>.
static IdOf< DecayTag > id_of(std::string_view s)
Resolves a string into an IdOf<Tag> via the registry.
static std::string str(const IdOf< ObservableTag > &id)
Returns the string representation of an identifier.
static std::optional< ObservableId > from_flha(const LhaID &ext)
Resolve a FLHA id to a dynamic observable id.
static BinnedObservableId binned_from_flha(const LhaID &ext)
Throwing variant of from_binned_flha().
static std::optional< LhaID > flha_of(const ObservableId &id)
Return the FLHA id attached to an observable id, if any.
static std::optional< LhaID > binned_flha_of(const BinnedObservableId &id)
Encode a binned observable id into its FLHA representation.
static LhaID flha(const Observables &id)
Return the FLHA id associated with a builtin enum value.
static Observables enum_elt(const LhaID &ext)
Legacy builtin-only lookup from FLHA id to enum.
static bool register_custom(const std::string &canonical, std::vector< std::string > aliases, std::optional< LhaID > ext, const DecayId &parent_decay)
Register a custom observable and attach it to a parent decay.
static Observables enum_elt_legacy(std::string_view s)
Legacy builtin-only lookup from string to enum.
static std::optional< BinnedObservableId > from_binned_flha(const LhaID &ext)
Decode a binned FLHA id into a binned observable id.
Returns an uppercase copy of the input string.
const std::string & str() const
Returns the underlying string.
Mapper and dynamic identifiers for decay channels.
std::string nk(std::string_view s)
Shortcut helper to normalize a string_view into a canonical key.
Mapper for builtin and runtime observable identifiers.
Identifies an observable together with a numerical bin.
static BinnedObservableId from_flha(LhaID const &id)
Reconstructs a binned observable identifier from an FLHA encoding.
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56
std::vector< long > get_parts() const
Returns the underlying vector of sub-ids.
Definition LhaID.h:148
std::string to_string() const
Returns the canonical string representation of this LhaID.
Definition LhaID.cpp:19