Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
decay_graph.cpp
Go to the documentation of this file.
1#include "decay_graph.h"
2
3#include <algorithm>
4#include <stdexcept>
5
6namespace {
7
11std::string decay_key(const DecayId& decay) {
12 return normalize_key(decay.str());
13}
14
18std::string observable_key(const ObservableId& obs) {
19 return normalize_key(obs.str());
20}
21
22} // namespace
23
25 static DecayGraph g;
26 return g;
27}
28
29void DecayGraph::link(const DecayId& decay, const ObservableId& obs) {
30 std::lock_guard<std::mutex> lock(m_);
31
32 const std::string dk = decay_key(decay);
33 const std::string ok = observable_key(obs);
34
35 if (auto it = parent_.find(ok); it != parent_.end() && !(it->second == decay)) {
36 throw std::runtime_error(
37 "Observable '" + obs.str() +
38 "' is already linked to decay '" + it->second.str() + "'"
39 );
40 }
41
42 auto& vec = graph_[dk];
43 if (std::find(vec.begin(), vec.end(), obs) == vec.end()) {
44 vec.push_back(obs);
45 }
46
47 parent_[ok] = decay;
48}
49
50std::vector<ObservableId> DecayGraph::observables_of(const DecayId& decay) const {
51 std::lock_guard<std::mutex> lock(m_);
52
53 if (auto it = graph_.find(decay_key(decay)); it != graph_.end()) {
54 return it->second;
55 }
56
57 return {};
58}
59
60std::optional<DecayId> DecayGraph::parent_of(const ObservableId& obs) const {
61 std::lock_guard<std::mutex> lock(m_);
62
63 if (auto it = parent_.find(observable_key(obs)); it != parent_.end()) {
64 return it->second;
65 }
66
67 return std::nullopt;
68}
69
70bool DecayGraph::has_observables(const DecayId& decay) const {
71 std::lock_guard<std::mutex> lock(m_);
72
73 if (auto it = graph_.find(decay_key(decay)); it != graph_.end()) {
74 return !it->second.empty();
75 }
76
77 return false;
78}
Thread-safe singleton storing dynamic decay-observable links.
Definition decay_graph.h:53
std::optional< DecayId > parent_of(const ObservableId &obs) const
Return the dynamic parent decay of an observable, if known.
bool has_observables(const DecayId &decay) const
Check whether a decay has at least one dynamic observable.
static DecayGraph & instance()
Return the unique global graph instance.
std::vector< ObservableId > observables_of(const DecayId &decay) const
Return all dynamically linked observables for a decay.
void link(const DecayId &decay, const ObservableId &obs)
Link an observable to a parent decay.
const std::string & str() const
Returns the underlying string.
Runtime graph linking decay identifiers to observable identifiers.
std::string normalize_key(std::string_view s)
Normalizes a string key in a case-insensitive manner.
constexpr double g