Commit c8136c0e authored by Oliver Majersky's avatar Oliver Majersky
Browse files

Implement possibility to restrict config option to a set of values.

parent f5f4b37b
......@@ -126,6 +126,9 @@ int main(int argc, char** argv) {
settings->loadFromFile(settingsFilename);
ATH_MSG_INFO("Configuration:\n" << *settings << "\n");
// only after printing full configuration check if there are issues and inform user and crash if necessary
settings->checkSettings();
const std::string libraryNames = settings->value("LibraryNames");
top::loadLibraries(libraryNames);
......
......@@ -12,6 +12,11 @@
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim_all.hpp>
#include <boost/algorithm/string/join.hpp>
#include "TopConfiguration/MsgCategory.h"
using namespace TopConfiguration;
namespace top {
ConfigurationSettings* ConfigurationSettings::m_instance = 0;
......@@ -463,7 +468,8 @@ namespace top {
registerParameter("ApplyElectronInJetSubtraction",
"Subtract electrons close to jets for boosted analysis : True or False(top default)", "False");
registerParameter("TopPartonHistory", "ttbar, tb, Wtb, ttz, ttgamma, tHq, False (default)", "False");
registerParameter("TopPartonHistory", "Topology to be assumed when reconstructing parton-level history.", "False",
{"ttbar", "tb", "Wtb", "ttz", "ttgamma", "tHq", "False"});
registerParameter("TopPartonLevel", "Perform parton level analysis (stored in truth tree)? True or False", "True");
registerParameter("TopParticleLevel",
......@@ -928,12 +934,30 @@ namespace top {
m_configured = true;
}
void ConfigurationSettings::checkSettings() {
for (const std::pair<std::string, StringData>& entry : strings_) {
const StringData& data = entry.second;
// if the config option restricts allowed values to some limited set,
// check that the configured value is valid
if (!data.m_allowed_values.empty()) {
if (std::find(data.m_allowed_values.begin(), data.m_allowed_values.end(),data.m_data) == data.m_allowed_values.end()) {
ATH_MSG_ERROR("Unsupported value specified for config option " << entry.first << ": " << data.m_data
<< "\nAllowed values: " << boost::algorithm::join(data.m_allowed_values, ", "));
throw std::runtime_error("Unsupported value for config option");
}
}
}
}
void ConfigurationSettings::registerParameter(const std::string& name, const std::string& message,
const std::string& default_val) {
const std::string& default_val,
const std::vector<std::string> allowed_values) {
StringData data;
data.m_data = default_val;
data.m_human_explanation = message;
data.m_default_val = default_val;
data.m_allowed_values = allowed_values;
data.m_set = (default_val.empty() ? false : true);
strings_[name] = data;
}
......@@ -1026,13 +1050,14 @@ namespace top {
std::stringstream s2;
s2 << "\"" << its->second.m_data << "\"";
std::string s_values;
if (!its->second.m_allowed_values.empty()) {
s_values = " Allowed values: " + boost::algorithm::join(its->second.m_allowed_values, ", ");
}
os << std::setw(40) << std::left << s.str() << " : " << std::setw(35) << s2.str() << " - " << std::right <<
its->second.m_human_explanation << "\n";
its->second.m_human_explanation << " Default: \"" << its->second.m_default_val << "\"." << s_values << "\n";
}
//for (const auto& selection : settings.selections())
// os << selection << "\n";
return os;
}
}
......@@ -889,7 +889,7 @@ namespace top {
try {
m_nominalWeightIndex = std::stoi(settings->value("NominalWeightFallbackIndex"));
} catch (std::invalid_argument &e) {
std::cout << "Failed to parse NominalWeightFallbackIndex value: " << settings->value("NominalWeightFallbackIndex") << std::endl;
ATH_MSG_ERROR("Failed to parse NominalWeightFallbackIndex value: " << settings->value("NominalWeightFallbackIndex"));
throw;
}
......
......@@ -63,6 +63,11 @@ namespace top {
*/
void loadFromFile(const std::string& filename);
/**
* @brief Validate that configured options make sense. This involves checking if unsupported options are not specified.
*/
void checkSettings();
/**
* @brief All the user input that is stored as strings.
*
......@@ -83,7 +88,8 @@ namespace top {
* @param name The key used to refer to this variable.
* @param message A human readable explanation
*/
void registerParameter(const std::string& name, const std::string& message, const std::string& default_val = "");
void registerParameter(const std::string& name, const std::string& message,
const std::string& default_val = "", const std::vector<std::string> allowed_values = {});
/**
* @brief test whether an (experimental) feature should be enabled
......
......@@ -6,6 +6,7 @@
#define STRINGDATA_H_
#include <string>
#include <vector>
namespace top {
/**
......@@ -19,6 +20,12 @@ namespace top {
///A helpful message that tells the user what's expected here
std::string m_human_explanation;
///If option can be only one of several values, store all possible options here
std::vector<std::string> m_allowed_values;
///Default value for the config option
std::string m_default_val;
///A flag that lets us know if the user has set something or not.
bool m_set;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment