Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
HyperisoMaster.cpp
Go to the documentation of this file.
1#include "HyperisoMaster.h"
2#include "HyperisoBanner.h"
3
4#include <algorithm>
5#include <cctype>
6#include <memory>
7#include <stdexcept>
8#include <system_error>
9#include <utility>
10
11namespace {
12
13std::string api_path_name(APIPath path_name)
14{
15 switch (path_name) {
16 case APIPath::LHA_PATH: return "LHA_PATH";
17 case APIPath::ASSETS_ROOT: return "ASSETS_ROOT";
18 case APIPath::DEFAULT_PARAM_VALUES: return "DEFAULT_PARAM_VALUES";
19 case APIPath::DEFAULT_OBS_VALUES: return "DEFAULT_OBS_VALUES";
20 case APIPath::DEFAULT_PARAM_CORR: return "DEFAULT_PARAM_CORR";
21 case APIPath::DEFAULT_OBS_CORR: return "DEFAULT_OBS_CORR";
22 case APIPath::DEFAULT_NUISANCES: return "DEFAULT_NUISANCES";
23 case APIPath::USER_SM_PARAMS: return "USER_SM_PARAMS";
24 case APIPath::USER_FLAVOR_PARAMS: return "USER_FLAVOR_PARAMS";
25 case APIPath::USER_DECAY_PARAMS: return "USER_DECAY_PARAMS";
26 case APIPath::USER_OBS_VALUES: return "USER_OBS_VALUES";
27 case APIPath::USER_PARAM_CORR: return "USER_PARAM_CORR";
28 case APIPath::USER_OBS_CORR: return "USER_OBS_CORR";
29 case APIPath::USER_NUISANCES: return "USER_NUISANCES";
30 case APIPath::PARAM_MAPPING_DIR: return "PARAM_MAPPING_DIR";
31 case APIPath::TEMPLATE_DIR: return "TEMPLATE_DIR";
32 case APIPath::SPECTRUM_DIR: return "SPECTRUM_DIR";
33 case APIPath::MARTY_TEMP_DIR: return "MARTY_TEMP_DIR";
34 }
35 return "UNKNOWN_API_PATH";
36}
37
38bool is_default_input_path(APIPath path_name)
39{
40 switch (path_name) {
46 return true;
47 default:
48 return false;
49 }
50}
51
52bool is_user_input_path(APIPath path_name)
53{
54 switch (path_name) {
62 return true;
63 default:
64 return false;
65 }
66}
67
68bool is_directory_path(APIPath path_name)
69{
70 switch (path_name) {
76 return true;
77 default:
78 return false;
79 }
80}
81
82bool is_writable_cache_directory_path(APIPath path_name)
83{
84 switch (path_name) {
87 return true;
88 default:
89 return false;
90 }
91}
92
93std::string lower_extension(const fs::path& path)
94{
95 std::string extension = path.extension().string();
96 std::transform(extension.begin(), extension.end(), extension.begin(),
97 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
98 return extension;
99}
100
101bool validate_pre_init_path_override(APIPath path_name, const fs::path& path)
102{
103 if (path_name == APIPath::LHA_PATH) {
104 LOG_ERROR("HyperisoMaster", "LHA_PATH is provided through init()/switch_lha() and cannot be overridden through pre_init_set_paths.");
105 return false;
106 }
107
108 std::error_code ec;
109 if (is_writable_cache_directory_path(path_name)) {
110 fs::create_directories(path, ec);
111 if (ec) {
112 LOG_ERROR("HyperisoMaster", "Cannot create cache directory for", api_path_name(path_name), ":", path.string(), ec.message());
113 return false;
114 }
115 }
116
117 if (!fs::exists(path, ec) || ec) {
118 LOG_ERROR("HyperisoMaster", "Path override for", api_path_name(path_name), "does not exist:", path.string());
119 return false;
120 }
121
122 if (is_directory_path(path_name)) {
123 if (!fs::is_directory(path, ec) || ec) {
124 LOG_ERROR("HyperisoMaster", "Path override for", api_path_name(path_name), "must be an existing directory:", path.string());
125 return false;
126 }
127 return true;
128 }
129
130 if (!fs::is_regular_file(path, ec) || ec) {
131 LOG_ERROR("HyperisoMaster", "Path override for", api_path_name(path_name), "must be an existing file:", path.string());
132 return false;
133 }
134
135 const std::string extension = lower_extension(path);
136 if (is_default_input_path(path_name)) {
137 if (extension != ".json") {
138 LOG_ERROR("HyperisoMaster", "Path override for", api_path_name(path_name), "must use the .json extension:", path.string());
139 return false;
140 }
141 LOG_WARN("HyperisoMaster", "Overriding a Hyperiso default path is not the expected runtime behavior. Make sure you know what you are doing:", api_path_name(path_name));
142 return true;
143 }
144
145 if (is_user_input_path(path_name)) {
146 if (extension != ".yaml" && extension != ".yml") {
147 LOG_ERROR("HyperisoMaster", "Path override for", api_path_name(path_name), "must use the .yaml or .yml extension:", path.string());
148 return false;
149 }
150 return true;
151 }
152
153 LOG_ERROR("HyperisoMaster", "Unsupported APIPath override:", api_path_name(path_name));
154 return false;
155}
156
157} // namespace
158
159void HyperisoMaster::ensure_memory_manager_created() {
160 std::shared_ptr<ParamBlockLoader> pbl = std::make_shared<ParamBlockLoader>();
161 std::shared_ptr<CorrelationLoader<ParamId>> cl_param = std::make_shared<CorrelationLoader<ParamId>>();
162 std::shared_ptr<CorrelationLoader<ExperimentObs>> cl_obs = std::make_shared<CorrelationLoader<ExperimentObs>>();
163 std::shared_ptr<SpectrumCalculator> spectrum_c = std::make_shared<SpectrumCalculator>();
164 std::shared_ptr<IPathsProvider> dpp = path_overrides.empty()
165 ? std::static_pointer_cast<IPathsProvider>(std::make_shared<DefaultPathsProvider>())
166 : std::static_pointer_cast<IPathsProvider>(std::make_shared<OverridePathsProvider>(path_overrides));
167 MemoryManager::Create(pbl, cl_param, cl_obs, spectrum_c, dpp, pbl);
168}
169
170bool HyperisoMaster::should_validate_marty_runtime(const HyperisoConfig& config) const {
171 const auto flag_it = config.flags.find(ExternalFlag::HYP_AS_SM_MARTY);
172 const bool hyp_as_sm_marty = flag_it != config.flags.end() && flag_it->second;
173 return hyp_as_sm_marty || config.model == Model::MARTY;
174}
175
176bool HyperisoMaster::validate_marty_runtime_if_needed(const HyperisoConfig& config, const std::string& context) const {
177 if (!should_validate_marty_runtime(config)) {
178 return true;
179 }
180
181 const auto install = MartyRuntimeConfig::require_available(context);
182 return install.valid;
183}
184
185void HyperisoMaster::init(const std::string &lhaFile, HyperisoConfig config) {
186 ensure_memory_manager_created();
187 if (!validate_marty_runtime_if_needed(config, "HyperisoMaster::init")) {
188 return;
189 }
190 HyperisoBanner::print_startup(lhaFile, config);
191 MemoryManager::GetInstance()->init(lhaFile, std::move(config));
192}
193
194
196 size_t itemCount,
197 size_t valueIdx,
198 int scaleIdx,
199 int rgIdx,
200 int binIdx,
201 bool globalScale)
202{
203 ensure_memory_manager_created();
204
205 auto* mm = MemoryManager::GetInstance();
206 if (mm->is_ready()) {
207 LOG_WARN("HyperisoMaster", "pre_init_add_block must be called before init; this prototype will only affect future LHA reloads.");
208 }
209
210 mm->add_lha_prototype(blockName, itemCount, valueIdx, scaleIdx, rgIdx, binIdx, globalScale);
211}
212
213void HyperisoMaster::pre_init_add_blocks(const std::vector<LhaPrototypeSpec>& prototypes)
214{
215 ensure_memory_manager_created();
216
217 auto* mm = MemoryManager::GetInstance();
218 if (mm->is_ready()) {
219 LOG_WARN("HyperisoMaster", "pre_init_add_blocks must be called before init; these prototypes will only affect future LHA reloads.");
220 }
221
222 mm->add_lha_prototypes(prototypes);
223}
224
225void HyperisoMaster::pre_init_set_marty_path(const std::string& martyInstallPath)
226{
227 const auto install = MartyRuntimeConfig::set_external_install_path(martyInstallPath);
228 if (!install.valid) {
229 LOG_ERROR("MartyConfigError", install.error);
230 return;
231 }
232
233 if (install.has_executable) {
234 LOG_DEBUG("MARTY executable detected at", install.marty_executable.string());
235 } else {
236 LOG_WARN("MartyRuntimeConfig", "No bin/marty executable found under", install.prefix.string(), "continuing because libmarty and marty.h are present.");
237 }
238
239 LOG_INFO("MARTY runtime path registered:", install.prefix.string());
240}
241
242
243void HyperisoMaster::pre_init_set_softsusy_path(const std::string& softsusyPath)
244{
245 const auto resolved = SoftsusyRuntimeConfig::set_external_path(softsusyPath);
246 if (!resolved.valid) {
247 throw std::runtime_error(resolved.error);
248 }
249 LOG_INFO("SOFTSUSY runtime path registered:", resolved.executable.string());
250}
251
252void HyperisoMaster::pre_init_set_paths(const std::map<APIPath, std::string>& pathOverrides)
253{
254 ensure_memory_manager_created();
255
256 auto* mm = MemoryManager::GetInstance();
257 if (mm->is_ready()) {
258 LOG_WARN("HyperisoMaster", "pre_init_set_paths must be called before init; these path overrides will only affect future reloads.");
259 }
260
261 if (pathOverrides.empty()) {
262 LOG_WARN("HyperisoMaster", "pre_init_set_paths called with an empty override map; keeping existing paths.");
263 return;
264 }
265
266 bool installed_any_path = false;
267 for (const auto& [path_name, raw_path] : pathOverrides) {
268 const fs::path candidate(raw_path);
269 if (!validate_pre_init_path_override(path_name, candidate)) {
270 continue;
271 }
272
273 std::error_code ec;
274 const fs::path normalized = fs::weakly_canonical(candidate, ec);
275 path_overrides[path_name] = ec ? fs::absolute(candidate) : normalized;
276 installed_any_path = true;
277 }
278
279 if (!installed_any_path) {
280 return;
281 }
282
283 mm->set_paths_provider(std::make_shared<OverridePathsProvider>(path_overrides));
284 LOG_INFO("Hyperiso path overrides registered:", path_overrides.size());
285}
286
287
288
289void HyperisoMaster::pre_init_set_marty_cache_dir(const std::string& cacheDir)
290{
292}
293
294void HyperisoMaster::pre_init_set_spectrum_cache_dir(const std::string& cacheDir)
295{
297}
298
299void HyperisoMaster::init(const std::string &lhaFile) {
300 init(lhaFile, HyperisoConfig());
301}
302
306
310
311void HyperisoMaster::switch_lha(const std::string &lhaFile, HyperisoConfig config) {
312 ensure_memory_manager_created();
313 if (!validate_marty_runtime_if_needed(config, "HyperisoMaster::switch_lha")) {
314 return;
315 }
316 MemoryManager::GetInstance()->switch_lha(lhaFile, std::move(config));
317}
ExternalFlag
Flags describing external input characteristics.
Definition Config.h:13
@ HYP_AS_SM_MARTY
If true, use Hyperiso as SM values for Wilson coefficients (up to NNLO).
Model
High-level helpers for initializing and monitoring the Hyperiso framework.
APIPath
Enumerates the filesystem paths exposed through the public API.
@ USER_OBS_VALUES
YAML/YML file containing user observable overrides.
@ DEFAULT_NUISANCES
JSON file containing default nuisance definitions.
@ USER_OBS_CORR
YAML/YML file containing user observable correlation overrides.
@ USER_NUISANCES
YAML/YML file containing user nuisance overrides.
@ TEMPLATE_DIR
Read-only directory containing generated-code templates.
@ DEFAULT_OBS_CORR
JSON file containing default observable correlations.
@ USER_PARAM_CORR
YAML/YML file containing user parameter correlation overrides.
@ DEFAULT_PARAM_VALUES
JSON file containing default parameter values.
@ USER_SM_PARAMS
YAML/YML file containing user SM parameter overrides.
@ DEFAULT_OBS_VALUES
JSON file containing default observable values.
@ MARTY_TEMP_DIR
Writable cache directory used for generated MARTY files.
@ LHA_PATH
Path to the active LHA file.
@ PARAM_MAPPING_DIR
Read-only directory containing MARTY/Hyperiso parameter mappings.
@ SPECTRUM_DIR
Writable cache directory used for generated spectrum files.
@ USER_FLAVOR_PARAMS
YAML/YML file containing user flavor parameter overrides.
@ ASSETS_ROOT
Root directory for HyperISO read-only assets.
@ DEFAULT_PARAM_CORR
JSON file containing default parameter correlations.
@ USER_DECAY_PARAMS
YAML/YML file containing user decay parameter overrides.
#define LOG_ERROR(type,...)
Macro for logging error messages and terminating the application.
Definition Logger.h:41
#define LOG_INFO(...)
Macro for logging informational messages.
Definition Logger.h:39
#define LOG_DEBUG(...)
Macro for logging debug messages.
Definition Logger.h:45
#define LOG_WARN(...)
Macro for logging warning messages.
Definition Logger.h:40
Block identifier with alias support.
Definition BlockName.h:59
static void print_startup(const std::string &lha_file, const HyperisoConfig &config)
void pre_init_add_block(BlockName blockName, size_t itemCount=2, size_t valueIdx=1, int scaleIdx=-1, int rgIdx=-1, int binIdx=-1, bool globalScale=false)
Registers one additional LHA block prototype before init().
void init(const std::string &lhaFile, HyperisoConfig config)
Initializes Hyperiso using a LHA file and a full Config object.
bool check_flag(ExternalFlag flag)
Checks if a specific external flag is active.
void pre_init_add_blocks(const std::vector< LhaPrototypeSpec > &prototypes)
Registers several additional LHA block prototypes before init().
void pre_init_set_paths(const std::map< APIPath, std::string > &pathOverrides)
Overrides selected HyperISO filesystem paths before init().
void switch_lha(const std::string &lhaFile, HyperisoConfig config)
Switches the LHA file and applies a new configuration.
void pre_init_set_marty_path(const std::string &martyInstallPath)
Registers an existing MARTY installation before init().
void pre_init_set_spectrum_cache_dir(const std::string &cacheDir)
Sets the writable spectrum cache directory before init().
Model get_model()
Retrieves the current model type used by Hyperiso.
void pre_init_set_marty_cache_dir(const std::string &cacheDir)
Sets the writable MARTY generated-code/cache directory before init().
void pre_init_set_softsusy_path(const std::string &softsusyPath)
Registers a SOFTSUSY executable or installation directory before init().
static InstallInfo set_external_install_path(const std::filesystem::path &path)
Register a user-provided MARTY installation path.
static InstallInfo require_available(const std::string &context)
Validate and return the active MARTY installation.
const MemoryCache & getMemoryCache()
Retrieves the current memory cache.
void switch_lha(const std::string &lhaFile, HyperisoConfig config)
Registers several additional LHA block prototypes.
void init(const std::string &lhaFile, HyperisoConfig config)
Initializes the memory manager with the provided LHA file and configuration.
static MemoryManager * GetInstance()
Retrieves the singleton instance of MemoryManager.
static MemoryManager * Create(std::shared_ptr< IDataLoader< BlockAccessor > > loader, std::shared_ptr< IDataLoader< CorrelationMatrixPair< ParamId > > > param_corr, std::shared_ptr< IDataLoader< CorrelationMatrixPair< ExperimentObs > > > obs_corr, std::shared_ptr< ISpectrumCalculator > spectrum_c, std::shared_ptr< IPathsProvider > paths_provider, std::shared_ptr< ILhaPrototypeRegistry > lha_prototype_registry=nullptr)
Retrieves/creates the singleton instance of MemoryManager with injected dependencies.
Resolution set_external_path(const std::string &path)
Definition SoftSusy.cpp:152
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
Configuration object controlling model, input flags and optional MARTY resources.
Definition Config.h:24
std::map< ExternalFlag, bool > flags
External flags describing the nature of the inputs.
Definition Config.h:26
Model model
Current model.
Definition Config.h:33
Interface providing all filesystem paths used by HyperISO.
HyperisoConfig config
Config struct for various flags and runtime information.
Default path provider with validated user overrides.