Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
CliUtils.cpp
Go to the documentation of this file.
1#include "CliUtils.h"
2
3#include <algorithm>
4#include <cctype>
5#include <iostream>
6#include <sstream>
7#include <stdexcept>
8
9namespace {
10
11std::string lower(std::string s) {
12 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
13 return s;
14}
15
16std::vector<std::string> split_csv(const std::string& s) {
17 std::vector<std::string> out;
18 std::stringstream ss(s);
19 std::string item;
20 while (std::getline(ss, item, ',')) {
21 if (!item.empty()) out.push_back(item);
22 }
23 return out;
24}
25
26bool is_option(const std::string& s) {
27 return s.rfind("--", 0) == 0;
28}
29
30} // namespace
31
32CliOptions CliOptions::parse(int argc, char* argv[], int first) {
33 CliOptions out;
34 for (int i = first; i < argc; ++i) {
35 std::string token = argv[i];
36 if (!is_option(token)) {
37 out.positionals.push_back(token);
38 continue;
39 }
40
41 token = token.substr(2);
42 std::string key;
43 std::string value;
44
45 const auto eq = token.find('=');
46 if (eq != std::string::npos) {
47 key = token.substr(0, eq);
48 value = token.substr(eq + 1);
49 } else {
50 key = token;
51 if (i + 1 < argc && !is_option(argv[i + 1])) {
52 value = argv[++i];
53 } else {
54 value = "true";
55 }
56 }
57 out.options[key].push_back(value);
58 }
59 return out;
60}
61
62bool CliOptions::has(const std::string& key) const {
63 return options.contains(key);
64}
65
66std::string CliOptions::get(const std::string& key, const std::string& fallback) const {
67 auto it = options.find(key);
68 if (it == options.end() || it->second.empty()) return fallback;
69 return it->second.back();
70}
71
72int CliOptions::get_int(const std::string& key, int fallback) const {
73 if (!has(key)) return fallback;
74 return std::stoi(get(key));
75}
76
77double CliOptions::get_double(const std::string& key, double fallback) const {
78 if (!has(key)) return fallback;
79 return std::stod(get(key));
80}
81
82bool CliOptions::flag(const std::string& key, bool fallback) const {
83 if (!has(key)) return fallback;
84 const std::string v = lower(get(key, "true"));
85 return v == "true" || v == "1" || v == "yes" || v == "on";
86}
87
88std::vector<std::string> CliOptions::list(const std::string& key, const std::vector<std::string>& fallback) const {
89 auto it = options.find(key);
90 if (it == options.end() || it->second.empty()) return fallback;
91
92 std::vector<std::string> out;
93 for (const auto& raw : it->second) {
94 auto parts = split_csv(raw);
95 out.insert(out.end(), parts.begin(), parts.end());
96 }
97 return out.empty() ? fallback : out;
98}
99
100void print_section(const std::string& title) {
101 std::cout << "\n== " << title << " ==\n";
102}
103
104Model parse_model(const std::string& model) {
105 const std::string m = lower(model);
106 if (m == "sm") return Model::SM;
107 if (m == "thdm" || m == "2hdm") return Model::THDM;
108 if (m == "mssm" || m == "susy" || m == "nmssm") return Model::SUSY;
109 if (m == "marty") return Model::MARTY;
110 throw std::invalid_argument("Unknown model: " + model);
111}
112
113QCDOrder parse_qcd_order(const std::string& order) {
114 const std::string o = lower(order);
115 if (o == "lo") return QCDOrder::LO;
116 if (o == "nlo") return QCDOrder::NLO;
117 if (o == "nnlo") return QCDOrder::NNLO;
118 throw std::invalid_argument("Unknown QCD order: " + order);
119}
120
121
122ContributionType parse_contribution(const std::string& contribution) {
123 const std::string value = lower(contribution);
124 if (value == "sm") return ContributionType::SM;
125 if (value == "bsm") return ContributionType::BSM;
126 if (value == "total") return ContributionType::TOTAL;
127 throw std::invalid_argument("Unknown contribution type: " + contribution);
128}
129
130HyperisoMaster init_hyperiso_from_cli(const CliOptions& opts) {
131 HyperisoConfig cfg;
132 cfg.model = parse_model(opts.get("model", "SM"));
133 cfg.flags[ExternalFlag::HYP_AS_SM_MARTY] = opts.flag("sm-marty", false);
134 cfg.flags[ExternalFlag::IS_LHA_SPECTRUM] = opts.flag("spectrum", false);
135
136 HyperisoMaster hyp;
137 hyp.init(opts.get("lha", "lha/si_input.flha"), cfg);
138 return hyp;
139}
void print_section(const std::string &title)
Definition CliUtils.cpp:100
ContributionType parse_contribution(const std::string &contribution)
Definition CliUtils.cpp:122
HyperisoMaster init_hyperiso_from_cli(const CliOptions &opts)
Definition CliUtils.cpp:130
Model parse_model(const std::string &model)
Definition CliUtils.cpp:104
QCDOrder parse_qcd_order(const std::string &order)
Definition CliUtils.cpp:113
@ IS_LHA_SPECTRUM
Input LHA file already contains a spectrum.
@ HYP_AS_SM_MARTY
If true, use Hyperiso as SM values for Wilson coefficients (up to NNLO).
Model
QCDOrder
ContributionType
High-level interface to initialize and monitor the main framework configuration.
void init(const std::string &lhaFile, HyperisoConfig config)
Initializes Hyperiso using a LHA file and a full Config object.
Small command-line parser for the Hyperiso terminal interface.
Definition CliUtils.h:21
double get_double(const std::string &key, double fallback) const
Definition CliUtils.cpp:77
std::vector< std::string > list(const std::string &key, const std::vector< std::string > &fallback={}) const
Definition CliUtils.cpp:88
bool has(const std::string &key) const
Definition CliUtils.cpp:62
bool flag(const std::string &key, bool fallback=false) const
Definition CliUtils.cpp:82
std::vector< std::string > positionals
Definition CliUtils.h:22
std::map< std::string, std::vector< std::string > > options
Definition CliUtils.h:23
int get_int(const std::string &key, int fallback) const
Definition CliUtils.cpp:72
static CliOptions parse(int argc, char *argv[], int first=1)
Definition CliUtils.cpp:32
std::string get(const std::string &key, const std::string &fallback="") const
Definition CliUtils.cpp:66
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