File indexing completed on 2026-12-02 10:27:22
0001
0002
0003
0004
0005 #pragma once
0006
0007 #include <limits>
0008 #include <string>
0009 #include <algorithm>
0010 #include <vector>
0011 #include <array>
0012 #include <map>
0013 #include <cmath>
0014 #include <iomanip>
0015 #include <cstring>
0016
0017 #include <JANA/JLogger.h>
0018 #include <JANA/JException.h>
0019 #include <JANA/Services/JServiceLocator.h>
0020 #include <JANA/Utils/JEventLevel.h>
0021
0022 class JParameter {
0023
0024 std::string m_name;
0025 std::string m_value;
0026 std::string m_default_value;
0027 std::string m_description;
0028
0029 bool m_has_default = false;
0030
0031 bool m_is_default = false;
0032
0033 bool m_is_deprecated = false;
0034
0035 bool m_is_advanced = false;
0036
0037 bool m_is_used = false;
0038
0039 bool m_is_conflicted = false;
0040
0041 public:
0042
0043 JParameter(std::string key, std::string value, std::string defaultValue, std::string description, bool hasDefault, bool isDefault)
0044 : m_name(std::move(key)),
0045 m_value(std::move(value)),
0046 m_default_value(std::move(defaultValue)),
0047 m_description(std::move(description)),
0048 m_has_default(hasDefault),
0049 m_is_default(isDefault) {}
0050
0051 inline const std::string& GetKey() const { return m_name; }
0052 inline const std::string& GetValue() const { return m_value; }
0053 inline const std::string& GetDefault() const { return m_default_value; }
0054 inline const std::string& GetDescription() const { return m_description; }
0055
0056 inline bool HasDefault() const { return m_has_default; }
0057 inline bool IsDefault() const { return m_is_default; }
0058 inline bool IsAdvanced() const { return m_is_advanced; }
0059 inline bool IsUsed() const { return m_is_used; }
0060 inline bool IsDeprecated() const { return m_is_deprecated; }
0061 inline bool IsConflicted() const { return m_is_conflicted; }
0062
0063 inline void SetKey(std::string key) { m_name = std::move(key); }
0064 inline void SetValue(std::string val) { m_value = std::move(val); }
0065 inline void SetDefault(std::string defaultValue) { m_default_value = std::move(defaultValue); }
0066 inline void SetDescription(std::string desc) { m_description = std::move(desc); }
0067 inline void SetHasDefault(bool hasDefault) { m_has_default = hasDefault; }
0068 inline void SetIsDefault(bool isDefault) { m_is_default = isDefault; }
0069 inline void SetIsAdvanced(bool isHidden) { m_is_advanced = isHidden; }
0070 inline void SetIsUsed(bool isUsed) { m_is_used = isUsed; }
0071 inline void SetIsDeprecated(bool isDeprecated) { m_is_deprecated = isDeprecated; }
0072 inline void SetIsConflicted(bool isConflicted) { m_is_conflicted = isConflicted; }
0073
0074 };
0075
0076 class JParameterManager : public JService {
0077 public:
0078
0079 JParameterManager();
0080
0081 JParameterManager(const JParameterManager&);
0082
0083 ~JParameterManager() override;
0084
0085 inline void SetLogger(JLogger logger) { m_logger = logger; }
0086
0087 bool Exists(std::string name);
0088
0089 JParameter* FindParameter(std::string);
0090
0091 void PrintParameters();
0092
0093 void PrintParameters(int verbosity, int strictness);
0094
0095 std::map<std::string, JParameter*> GetAllParameters();
0096
0097 template<typename T>
0098 JParameter* GetParameter(std::string name, T& val);
0099
0100 template<typename T>
0101 T GetParameterValue(std::string name);
0102
0103 template<typename T>
0104 JParameter* SetParameter(std::string name, T val);
0105
0106 template<typename T>
0107 JParameter* SetDefaultParameter(std::string name, T& val, std::string description = "");
0108
0109 template <typename T>
0110 T RegisterParameter(std::string name, const T default_val, std::string description = "");
0111
0112 void FilterParameters(std::map<std::string,std::string> &parms, std::string filter="");
0113
0114 void ReadConfigFile(std::string name);
0115
0116 void WriteConfigFile(std::string name);
0117
0118 template<typename T>
0119 static inline void Parse(const std::string& value, T& out);
0120
0121 template<typename T>
0122 static inline void Parse(const std::string& value, std::vector<T>& out);
0123
0124 template<typename T, size_t arrSize>
0125 static inline void Parse(const std::string& value, std::array<T,arrSize>& out);
0126
0127 template<size_t arrSize>
0128 static inline void Parse(const std::string& value, std::array<std::string,arrSize>& out);
0129
0130
0131
0132
0133
0134
0135 template<typename T>
0136 static std::string Stringify(const T& value);
0137
0138 template<typename T>
0139 static std::string Stringify(const std::vector<T>& values);
0140
0141 template<typename T,size_t N>
0142 static std::string Stringify(const std::array<T,N>& values);
0143
0144 template<size_t N>
0145 static std::string Stringify(const std::array<std::string,N>& values);
0146
0147 template<typename T>
0148 static bool Equals(const T& lhs, const T& rhs);
0149
0150
0151 static std::string ToLower(const std::string& name);
0152
0153 JLogger GetLogger(const std::string& prefix);
0154
0155 private:
0156
0157 std::map<std::string, JParameter*> m_parameters;
0158
0159 int m_strictness = 1;
0160 int m_verbosity = 1;
0161
0162 std::mutex m_mutex;
0163 };
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 template<typename T>
0175 JParameter* JParameterManager::GetParameter(std::string name, T& val) {
0176
0177 auto result = m_parameters.find(ToLower(name));
0178 if (result == m_parameters.end()) {
0179 return nullptr;
0180 }
0181 Parse(result->second->GetValue(),val);
0182 result->second->SetIsUsed(true);
0183 return result->second;
0184 }
0185
0186
0187
0188
0189
0190
0191
0192
0193 template<typename T>
0194 T JParameterManager::GetParameterValue(std::string name) {
0195 T t;
0196 auto result = m_parameters.find(ToLower(name));
0197 if (result == m_parameters.end()) {
0198 throw JException("Unknown parameter \"%s\"", name.c_str());
0199 }
0200 result->second->SetIsUsed(true);
0201 Parse(result->second->GetValue(),t);
0202 return t;
0203 }
0204
0205
0206
0207
0208
0209
0210
0211 template<typename T>
0212 JParameter* JParameterManager::SetParameter(std::string name, T val) {
0213
0214 auto result = m_parameters.find(ToLower(name));
0215
0216 if (result == m_parameters.end()) {
0217 auto* param = new JParameter {name, Stringify(val), "", "", false, false};
0218 m_parameters[ToLower(name)] = param;
0219 return param;
0220 }
0221 result->second->SetValue(Stringify(val));
0222 result->second->SetIsDefault(false);
0223 return result->second;
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253 template<typename T>
0254 JParameter* JParameterManager::SetDefaultParameter(std::string name, T& val, std::string description) {
0255
0256 std::lock_guard<std::mutex> lock(m_mutex);
0257 JParameter* param = nullptr;
0258 T t;
0259 auto result = m_parameters.find(ToLower(name));
0260 if (result != m_parameters.end()) {
0261
0262 param = result->second;
0263
0264 if (!param->HasDefault()) {
0265
0266 param->SetHasDefault(true);
0267 param->SetDefault(Stringify(val));
0268 param->SetDescription(std::move(description));
0269 }
0270 else {
0271
0272
0273
0274 Parse(param->GetDefault(),t);
0275 if (!Equals(val, t)) {
0276 if (!param->IsConflicted()) {
0277
0278 LOG_WARN(m_logger) << "Parameter '" << name << "' has conflicting defaults: '"
0279 << Stringify(val) << "' vs '" << param->GetDefault() << "'"
0280 << LOG_END;
0281 param->SetIsConflicted(true);
0282 }
0283
0284
0285
0286 param->SetDefault(Stringify(val));
0287 }
0288 if (param->IsDefault()) {
0289 param->SetValue(Stringify(val));
0290 }
0291 }
0292 }
0293 else {
0294
0295 auto valstr = Stringify(val);
0296 param = new JParameter {name, valstr, valstr, std::move(description), true, true};
0297
0298
0299
0300
0301 Parse(valstr,t);
0302 if (!Equals(val, t)) {
0303 LOG_WARN(m_logger) << "Parameter '" << name << "' with value '" << valstr
0304 << "' loses equality with itself after stringification" << LOG_END;
0305 }
0306 m_parameters[ToLower(name)] = param;
0307 }
0308
0309
0310
0311 Parse(param->GetValue(),t);
0312 val = t;
0313 param->SetIsUsed(true);
0314 return param;
0315 }
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 template <typename T>
0331 inline T JParameterManager::RegisterParameter(std::string name, const T default_val, std::string description){
0332 T val = default_val;
0333 SetDefaultParameter(name.c_str(), val, description);
0334 return val;
0335 }
0336
0337
0338 #if __cplusplus >= 201703L
0339
0340 template <typename T>
0341 void JParameterManager::Parse(const std::string& s, T& out) {
0342 constexpr bool parseable = JTypeInfo::is_parseable<T>::value;
0343 static_assert(parseable, "Type is not automatically parseable by JParameterManager. To use, provide a template specialization for JParameterManager::Parse(std::string in, T& out).");
0344 if constexpr (parseable) {
0345 std::stringstream ss(s);
0346 ss >> out;
0347 }
0348 }
0349 #else
0350
0351 template <typename T>
0352 void JParameterManager::Parse(const std::string& s, T& out) {
0353 std::stringstream ss(s);
0354 ss >> out;
0355 }
0356 #endif
0357
0358
0359
0360
0361 template <>
0362 inline void JParameterManager::Parse(const std::string& value, std::string& out) {
0363 out = value;
0364 }
0365
0366
0367
0368 template <>
0369 inline void JParameterManager::Parse(const std::string& value, bool& val) {
0370 if (value == "0" || value =="false" || value == "off") val = false;
0371 else if (value == "1" || value == "true" || value == "on") val = true;
0372 else throw JException("'%s' not parseable as bool", value.c_str());
0373 }
0374
0375
0376 template<typename T, size_t N>
0377 inline void JParameterManager::Parse(const std::string& value,std::array<T,N> &val) {
0378 std::string s;
0379 std::stringstream ss(value);
0380 int indx = 0;
0381 while (getline(ss, s, ',')) {
0382 T t;
0383 Parse(s, t);
0384 val[indx++]= t;
0385 }
0386 }
0387
0388
0389 template<size_t N>
0390 inline void JParameterManager::Parse(const std::string& value,std::array<std::string,N> &val) {
0391 std::string s;
0392 std::stringstream ss(value);
0393 std::string temp;
0394 int indx = 0;
0395 while (getline(ss, s, ',')) {
0396 std::string t;
0397 if (s.back() == '\\') {
0398 s.pop_back();
0399 temp += s + ',';
0400 continue;
0401 }
0402 if (!temp.empty()) {
0403 Parse(temp + s, t);
0404 val[indx++]= t;
0405 temp.clear();
0406 }
0407 else {
0408 Parse(s, t);
0409 val[indx++]= t;
0410 }
0411 }
0412 }
0413
0414
0415 template<typename T>
0416 inline void JParameterManager::Parse(const std::string& value, std::vector<T> &val) {
0417 std::stringstream ss(value);
0418 std::string s;
0419 val.clear();
0420 while (getline(ss, s, ',')) {
0421 T t;
0422 Parse(s, t);
0423 val.push_back(t);
0424 }
0425 }
0426
0427
0428 template<>
0429 inline void JParameterManager::Parse(const std::string& value, std::vector<std::string> &val) {
0430 std::stringstream ss(value);
0431 std::string s;
0432 val.clear();
0433 std::string temp;
0434 while (getline(ss, s, ',')) {
0435 std::string t;
0436 if (s.back() == '\\') {
0437 s.pop_back();
0438 temp += s + ',';
0439 continue;
0440 }
0441 if (!temp.empty()) {
0442 Parse(temp + s, t);
0443 val.push_back(t);
0444 temp.clear();
0445 }
0446 else {
0447 Parse(s, t);
0448 val.push_back(t);
0449 }
0450 }
0451 }
0452
0453
0454 template <>
0455 inline void JParameterManager::Parse(const std::string& in, JLogger::Level& out) {
0456 std::string token(in);
0457 std::transform(in.begin(), in.end(), token.begin(), ::tolower);
0458 if (std::strcmp(token.c_str(), "trace") == 0) {
0459 out = JLogger::Level::TRACE;
0460 }
0461 else if (std::strcmp(token.c_str(), "debug") == 0) {
0462 out = JLogger::Level::DEBUG;
0463 }
0464 else if (std::strcmp(token.c_str(), "info") == 0) {
0465 out = JLogger::Level::INFO;
0466 }
0467 else if (std::strcmp(token.c_str(), "warn") == 0) {
0468 out = JLogger::Level::WARN;
0469 }
0470 else if (std::strcmp(token.c_str(), "warning") == 0) {
0471 out = JLogger::Level::WARN;
0472 }
0473 else if (std::strcmp(token.c_str(), "error") == 0) {
0474 out = JLogger::Level::ERROR;
0475 }
0476 else if (std::strcmp(token.c_str(), "err") == 0) {
0477 out = JLogger::Level::ERROR;
0478 }
0479 else if (std::strcmp(token.c_str(), "fatal") == 0) {
0480 out = JLogger::Level::FATAL;
0481 }
0482 else if (std::strcmp(token.c_str(), "critical") == 0) {
0483 out = JLogger::Level::FATAL;
0484 }
0485 else if (std::strcmp(token.c_str(), "off") == 0) {
0486 out = JLogger::Level::OFF;
0487 }
0488 else {
0489 throw JException("Unable to parse log level: '%s'. Options are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF", in.c_str());
0490 }
0491 }
0492
0493 template<>
0494 inline void JParameterManager::Parse(const std::string& in, JEventLevel& out) {
0495 std::string token(in);
0496 std::transform(in.begin(), in.end(), token.begin(), ::tolower);
0497 if (std::strcmp(token.c_str(), "run") == 0) {
0498 out = JEventLevel::Run;
0499 }
0500 else if (std::strcmp(token.c_str(), "subrun") == 0) {
0501 out = JEventLevel::Subrun;
0502 }
0503 else if (std::strcmp(token.c_str(), "timeframe") == 0) {
0504 out = JEventLevel::Timeframe;
0505 }
0506 else if (std::strcmp(token.c_str(), "timeslice") == 0) {
0507 out = JEventLevel::Timeslice;
0508 }
0509 else if (std::strcmp(token.c_str(), "block") == 0) {
0510 out = JEventLevel::Block;
0511 }
0512 else if (std::strcmp(token.c_str(), "slowcontrols") == 0) {
0513 out = JEventLevel::SlowControls;
0514 }
0515 else if (std::strcmp(token.c_str(), "physicsevent") == 0) {
0516 out = JEventLevel::PhysicsEvent;
0517 }
0518 else if (std::strcmp(token.c_str(), "subevent") == 0) {
0519 out = JEventLevel::Subevent;
0520 }
0521 else if (std::strcmp(token.c_str(), "task") == 0) {
0522 out = JEventLevel::Task;
0523 }
0524 else {
0525 throw JException("Unable to parse event level: '%s'. Options are: Run, Subrun, Timeframe, Timeslice, Block, SlowControls, PhysicsEvent, Subevent, Task", in.c_str());
0526 }
0527 }
0528
0529 #if __cplusplus >= 201703L
0530
0531 template <typename T>
0532 inline std::string JParameterManager::Stringify(const T& value) {
0533 constexpr bool serializable = JTypeInfo::is_serializable<T>::value;
0534 static_assert(serializable, "Type is not automatically serializable by JParameterManager. To use, provide a template specialization for JParameterManager::Stringify(const T& val) -> std::string.");
0535 if constexpr (serializable) {
0536 std::stringstream ss;
0537 ss << value;
0538 return ss.str();
0539 }
0540 }
0541 #else
0542
0543 template <typename T>
0544 inline std::string JParameterManager::Stringify(const T& value) {
0545 std::stringstream ss;
0546 ss << value;
0547 return ss.str();
0548 }
0549 #endif
0550
0551
0552 template <>
0553 inline std::string JParameterManager::Stringify(const float& value) {
0554 std::stringstream ss;
0555
0556
0557 ss << std::setprecision(std::numeric_limits<float>::max_digits10) << value;
0558 return ss.str();
0559 }
0560 template <>
0561 inline std::string JParameterManager::Stringify(const double& value) {
0562 std::stringstream ss;
0563
0564
0565 ss << std::setprecision(std::numeric_limits<double>::max_digits10) << value;
0566 return ss.str();
0567 }
0568 template <>
0569 inline std::string JParameterManager::Stringify(const long double& value) {
0570 std::stringstream ss;
0571
0572
0573 ss << std::setprecision(std::numeric_limits<long double>::max_digits10) << value;
0574 return ss.str();
0575 }
0576
0577
0578 template <>
0579 inline std::string JParameterManager::Stringify(const std::string& value) {
0580 return value;
0581 }
0582
0583 template <>
0584 inline std::string JParameterManager::Stringify(const JEventLevel& value) {
0585 return toString(value);
0586 }
0587
0588 template <typename T>
0589 inline std::string JParameterManager::Stringify(const std::vector<T> &values) {
0590 std::stringstream ss;
0591 size_t len = values.size();
0592 for (size_t i = 0; i+1 < len; ++i) {
0593 ss << Stringify(values[i]);
0594 ss << ",";
0595 }
0596 if (len != 0) {
0597 ss << Stringify(values[len-1]);
0598 }
0599 return ss.str();
0600 }
0601
0602
0603 template <>
0604 inline std::string JParameterManager::Stringify(const std::vector<std::string> &values) {
0605 size_t len = values.size();
0606 std::stringstream ssv;
0607 for (size_t i = 0; i < len; ++i) {
0608 if (values[i].find(',') != std::string::npos) {
0609 std::stringstream ss(values[i]);
0610 std::string s;
0611 std::string s_end = values[i].substr(values[i].rfind(',')+1);
0612 while (getline(ss, s, ',')) {
0613 if (s == s_end) {
0614 ssv << s;
0615 if (i != len-1) {
0616 ssv << ',';
0617 }
0618 break;
0619 }
0620 ssv << s << "\\,";
0621 continue;
0622 }
0623 } else {
0624 ssv << values[i];
0625 if (i != len-1) {
0626 ssv << ',';
0627 }
0628 }
0629 }
0630 return ssv.str();
0631 }
0632
0633
0634
0635 template <typename T, size_t N>
0636 inline std::string JParameterManager::Stringify(const std::array<T,N> &values) {
0637 std::stringstream ss;
0638 size_t len = values.size();
0639 for (size_t i = 0; i+1 < N; ++i) {
0640 ss << Stringify(values[i]);
0641 ss << ",";
0642 }
0643 if (len != 0) {
0644 ss << Stringify(values[len-1]);
0645 }
0646 return ss.str();
0647 }
0648
0649
0650 template <size_t N>
0651 inline std::string JParameterManager::Stringify(const std::array<std::string,N> &values) {
0652 size_t len = values.size();
0653 std::stringstream ssv;
0654 for (size_t i = 0; i < len; ++i) {
0655 if (values[i].find(',') != std::string::npos) {
0656 std::stringstream ss(values[i]);
0657 std::string s;
0658 std::string s_end = values[i].substr(values[i].rfind(',')+1);
0659 while (getline(ss, s, ',')) {
0660 if (s == s_end) {
0661 ssv << s;
0662 if (i != len-1) {
0663 ssv << ',';
0664 }
0665 break;
0666 }
0667 ssv << s << "\\,";
0668 continue;
0669 }
0670 } else {
0671 ssv << values[i];
0672 if (i != len-1) {
0673 ssv << ',';
0674 }
0675 }
0676 }
0677 return ssv.str();
0678 }
0679
0680
0681
0682
0683 template <typename T>
0684 inline bool JParameterManager::Equals(const T& lhs, const T& rhs) {
0685 return lhs == rhs;
0686 }
0687
0688
0689
0690