15#define BOOST_BIND_GLOBAL_PLACEHOLDERS 1
16#include <boost/property_tree/json_parser.hpp>
24namespace pt = boost::property_tree;
26template<
class Config_t,
class Obj_t>
34 static bool Load(Obj_t& obj,
const std::string& path)
38 if (!
Load(obj, tree))
return false;
42 static bool Load(Obj_t& obj,
const pt::ptree& tree)
45 auto moduleTree {tree.get_child_optional(Config_t::GetModuleTag())};
47 LOG(error) <<
"CbmConfig: module tag not found:" << Config_t::GetModuleTag();
50 if (!
Validate(moduleTree.get()))
return false;
51 if (!Config_t::LoadImpl(obj, moduleTree.get()))
return false;
57 std::string absPath = path;
58 if (absPath.at(0) !=
'/') absPath = gSystem->GetWorkingDirectory() +
"/" + path;
59 LOG(info) <<
"CbmConfig: loading config from file: " << absPath << std::endl;
60 pt::read_json(path, tree);
65 const auto validationSet {Config_t::GetValidationTags()};
69 std::vector<std::string> diff;
70 std::set_difference(treeSet.begin(), treeSet.end(), validationSet.begin(), validationSet.end(),
71 std::inserter(diff, diff.begin()));
74 LOG(error) <<
"CbmConfig: Invalid tags: ";
76 std::cout << s <<
", ";
77 std::cout << std::endl;
90 if (nkey.back() !=
'.') nkey +=
".";
91 LOG(debug) <<
"CbmConfig: key: " << key <<
"\tnkey: " << nkey;
95 if (key.back() ==
'.') key.pop_back();
96 LOG(debug) <<
"CbmConfig: Insert: " << key;
97 if (key.find(
"#") < key.size())
103 for (
auto node : pt) {
104 LOG(debug) <<
"CbmConfig: Try: " << nkey + node.first;
105 ParseTree(node.second, nkey + node.first, treeSet);
111 auto tags = Config_t::GetValidationTags();
112 std::cout <<
"\nAvailable config tags:\n";
113 for (
auto& tag : tags)
114 std::cout << tag << std::endl;
119 std::map<std::string, ECbmModuleId> stringToModuleId = {
127 if (stringToModuleId.find(s) == stringToModuleId.end()) {
128 LOG(error) <<
"CbmConfig: detector subsystem not recognized: " << s;
129 std::cout <<
"list of available detector subsystems:\n";
130 for (
auto& p : stringToModuleId)
131 std::cout << p.first << std::endl;
136 return stringToModuleId.at(s);
141 static std::string
GetStringValue(pt::ptree tree, std::string key, std::string fallback)
143 return ParseString(tree.get<std::string>(key, fallback));
148 std::regex rgx(
"\\$\\{?\\w+\\}?");
150 while (s.find(
"$") < s.size()) {
151 std::regex_search(s, match, rgx);
152 std::string varString = match[0];
153 std::string varName = std::regex_replace(varString, std::regex(
"\\$|\\{|\\}"),
"");
154 const char* varValue = gSystem->Getenv(varName.c_str());
155 if (!varValue) varValue =
"";
156 s.replace(s.find(varString), varString.size(), varValue);
163 auto logScreenLevel = moduleTree.get_optional<std::string>(
"logScreenLevel");
164 if (logScreenLevel) fair::Logger::SetConsoleSeverity(logScreenLevel.get().c_str());
165 auto logVerbosityLevel = moduleTree.get_optional<std::string>(
"logVerbosityLevel");
166 if (logVerbosityLevel) fair::Logger::SetVerbosity(logVerbosityLevel.get().c_str());
@ kMvd
Micro-Vertex Detector.
@ kHodo
Hodoscope (for test beam times)
@ kTrd
Transition Radiation Detector.
@ kShield
Beam pipe shielding in MUCH section.
@ kTof
Time-of-flight Detector.
@ kPsd
Projectile spectator detector.
@ kSts
Silicon Tracking System.
@ kPlatform
RICH rail platform.
@ kMuch
Muon detection system.
@ kFsd
Forward spectator detector.
@ kRich
Ring-Imaging Cherenkov Detector.
static void LoadFromFile(const std::string &path, pt::ptree &tree)
static std::string GetStringValue(pt::ptree tree, std::string key, std::string fallback)
static void SetLogLevel(const pt::ptree &moduleTree)
static void ParseTree(const pt::ptree &pt, std::string key, TagSet_t &treeSet)
static bool Load(Obj_t &obj, const std::string &path)
static std::string ParseString(std::string s)
std::set< std::string > TagSet_t
static void PrintAvailableTags()
static bool Load(Obj_t &obj, const pt::ptree &tree)
virtual ~CbmConfigBase()=default
static bool Validate(const pt::ptree &tree)
static std::string GetStringValue(boost::optional< std::string > opt)
static ECbmModuleId stringToECbmModuleId(std::string s)