Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
test_dynamic_registry.cpp
Go to the documentation of this file.
1// main.cpp
2#include <cassert>
3#include <iostream>
4#include <vector>
5
6#include "LhaID.h" // LhaID
7#include "GeneralEnum.h" // enums (WCoef, Observables, Decays, etc.)
8#include "registry_init.hpp" // init_all_builtins()
9#include "mapper_hub.hpp" // unified access helpers
10#include "decay_graph.h" // DecayGraph::instance()
11
12int main() {
13 std::cout << "== Dynamic registry demo ==\n";
14
15 // 1) Initialize all builtin mappers (one-time, idempotent).
17
18 // -----------------------------------------------------------------------------
19 // 2) WCOEF: string -> Id -> canonical, external roundtrip
20 // -----------------------------------------------------------------------------
21 {
22 // Case-insensitive lookup (aliases supported by the runtime registry)
23 auto id = WCoefMapper::id_of("c7");
24 std::cout << "[WCOEF] id_of(\"c7\") = " << WCoefMapper::str(id) << "\n";
25 assert(WCoefMapper::str(id) == "C7");
26
27 // External key roundtrip (FLHA-style pair<int,int>)
28 auto ext = WCoefMapper::external_of(id);
29 assert(ext.has_value());
30 auto back = WCoefMapper::from_external(*ext);
31 assert(back && WCoefMapper::str(*back) == "C7");
32 std::cout << "[WCOEF] FLHA roundtrip ok\n";
33 }
34
35 // Register a custom WCoef with an external key
36 {
37 bool ok = WCoefMapper::register_custom("CNEW", {"c_new"}, std::make_pair(999, 888));
38 assert(ok);
39
40 auto id = WCoefMapper::id_of("CNEW");
41 auto ext = WCoefMapper::external_of(id);
42
43 assert(ext && ext->first == 999 && ext->second == 888);
44 std::cout << "[WCOEF] custom CNEW registered with FLHA (999,888)\n";
45 }
46
47 // -----------------------------------------------------------------------------
48 // 3) OBSERVABLES: enum -> Id -> LhaID -> back (+ legacy enum_elt(LhaID))
49 // -----------------------------------------------------------------------------
50 {
51 // Start from a builtin enum
53 auto id = ObservableMapper::to_id(e);
54
55 // Get the external FLHA-like key
56 auto lha = ObservableMapper::flha_of(id);
57 assert(lha.has_value());
58
59 // External back to runtime Id
60 auto id2 = ObservableMapper::from_flha(*lha);
61 assert(id2 && ObservableMapper::str(*id2) == ObservableMapper::str(id));
62
63 // Legacy: directly to builtin enum from external
64 auto e2 = ObservableMapper::enum_elt(*lha);
65 assert(e2 == e);
66
67 std::cout << "[OBS] LhaID roundtrip ok for " << ObservableMapper::str(id) << "\n";
68 }
69
70 // Register a custom Observable with LhaID and attach it to a decay
71 {
72 LhaID custom_key(777, 1, 2, 13, -13); // arbitrary example
73
74 // Attach to builtin decay B__l_l; this also creates the runtime link in DecayGraph
76 "MY_OBS", // canonical name
77 {"myObs"}, // aliases
78 custom_key, // external key (LhaID)
79 Decays::B__l_l // parent decay (enum overload)
80 );
81 assert(ok);
82
83 // Lookup by alias
84 auto id = ObservableMapper::id_of("myObs");
85 assert(ObservableMapper::str(id) == "MY_OBS");
86
87 // External must match
88 auto k = ObservableMapper::flha_of(id);
89 assert(k && k->to_string() == custom_key.to_string());
90
91 // DecayGraph now contains the link (only our custom one is required here)
94
95 bool found = false;
96 for (const auto& x : os) {
97 if (x.str() == "MY_OBS") { found = true; break; }
98 }
99 assert(found);
100 std::cout << "[OBS] custom MY_OBS linked to decay B__l_l\n";
101 }
102
103 // -----------------------------------------------------------------------------
104 // 4) DECAY: builtin usage (external key optional; none by default)
105 // -----------------------------------------------------------------------------
106 {
108 std::cout << "[DECAY] canonical = " << DecayMapper::str(d) << "\n";
109
110 // If you decide later to bind an external code to a builtin decay:
111 // DecayMapper::set_external(d, LhaID(521)); // example (arbitrary)
112 // auto ext = DecayMapper::external_of(d); // would return that LhaID
113 // assert(ext && static_cast<long>(*ext) == 521); // depending on your LhaID implementation
114 }
115
116 // -----------------------------------------------------------------------------
117 // 5) GROUP: block naming helpers (MATCHING / HADRONIC + basis)
118 // -----------------------------------------------------------------------------
119 {
122 std::cout << "[GROUP] MATCHING=" << m << " HADRONIC(TRAD)=" << h << "\n";
123 }
124
125 // -----------------------------------------------------------------------------
126 // 6) QCD ORDER: string → runtime id → builtin enum
127 // -----------------------------------------------------------------------------
128 {
129 // Generic API: id_of("NLO") gives a runtime id; enum_of(id) recovers the builtin enum
130 auto qid = OrderMapper::id_of("NLO");
131 auto q = OrderMapper::enum_of(qid);
132 assert(q && *q == QCDOrder::NLO);
133 std::cout << "[ORDER] string->id->enum OK (NLO)\n";
134 }
135
136 // -----------------------------------------------------------------------------
137 // 7) Unified access via Hub (MapperKind) for simple tasks
138 // -----------------------------------------------------------------------------
139 {
140 // Canonical name from user input without knowing which concrete mapper at call site
141 std::cout << "[HUB] canonical(WCoef, \"c7\") = "
142 << canonical_of(MapperKind::WCoef, "c7") << "\n";
143
144 // Register a custom WCoef through the hub helper with an external key
145 bool ok = register_custom_wcoef("CNEW2", {"cnew2"}, {1000, 1001});
146 assert(ok);
147 std::cout << "[HUB] registered custom WCoef CNEW2 via hub helper\n";
148
149 // List a few items from a mapper via Hub
150 auto obs_list = list_all(MapperKind::Observable);
151 std::cout << "[HUB] first observable = " << (obs_list.empty() ? "<empty>" : obs_list.front()) << "\n";
152 }
153
154 std::cout << "✅ Demo OK\n";
155 return 0;
156}
Lightweight representation of LHAPDF / SLHA-style identifiers.
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.
static IdOf< QCDOrderTag > id_of(std::string_view s)
Resolves a string into an IdOf<Tag> via the registry.
static std::optional< QCDOrder > enum_of(const IdOf< QCDOrderTag > &id)
Attempts to recover the enum value associated with an identifier.
static bool register_custom(std::string canonical, std::vector< std::string > aliases={}, std::optional< std::pair< int, int > > ext=std::nullopt)
Registers a custom entry with optional external key.
static std::optional< std::pair< int, int > > external_of(const IdOf< WCoefTag > &id)
Returns the external key associated with a given identifier.
static std::optional< IdOf< WCoefTag > > from_external(const std::pair< int, int > &k)
Resolves an external key into an identifier.
static IdOf< ObservableTag > to_id(Observables e)
Converts an enum value to an IdOf<Tag>.
static IdOf< WCoefTag > id_of(std::string_view s)
Resolves a string into an IdOf<Tag> via the registry.
static std::string str(const IdOf< WCoefTag > &id)
Returns the string representation of an identifier.
static std::string str(const WGroupId &gid, ScaleType s, WilsonBasis b=WilsonBasis::B_STANDARD)
Builds a composite block name using a WGroupId, scale and basis.
static std::optional< ObservableId > from_flha(const LhaID &ext)
Resolve a FLHA id to a dynamic observable id.
static std::optional< LhaID > flha_of(const ObservableId &id)
Return the FLHA id attached to an observable id, if any.
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.
Runtime graph linking decay identifiers to observable identifiers.
Non-templated façade over the project mapper families.
std::vector< std::string > list_all(MapperKind k)
List all canonical names known to a mapper kind.
bool register_custom_wcoef(std::string canonical, std::vector< std::string > aliases, std::pair< int, int > flha)
Specialized registration helper for Wilson coefficients.
std::string canonical_of(MapperKind k, std::string_view s)
Resolve a name to its canonical representation for a mapper kind.
void init_all_builtins()
Initializes all builtin mapper registries and the decay graph.
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56
std::string to_string() const
Returns the canonical string representation of this LhaID.
Definition LhaID.cpp:19