Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set algorithm log levels from config yaml files #298

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions examples/config/my_combined_config_file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ example::ExampleAlgorithm:

clas12::ZVertexFilter:

# default cuts
- default:
electron_vz: [ -33.0, 11.0 ]
log: debug # NOTE: you may control algorithm log levels here, like so

#RG-A fall2018 inbending
electron:
- default:
vz: [ -33.0, 11.0 ]
- runs: [ 4760, 5419 ]
electron_vz: [ -13.0, 12.0 ]

#RG-A fall2018 outbending
vz: [ -13.0, 12.0 ]
- runs: [ 5420, 5674 ]
electron_vz: [ -18.0, 10.0 ]
vz: [ -18.0, 10.0 ]

another::Algorithm:
#Cuts below are just examples:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Cut values for different run periods
clas12::ZVertexFilter:

# default cuts
- default:
electron_vz: [ -15.0, 15.0 ]
# scattered electron cuts
electron:

# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
electron_vz: [ -5.0, 3.0 ]
# default cuts
- default:
vz: [ -15.0, 15.0 ]

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
electron_vz: [ -8.0, 7.0 ]
# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
vz: [ -5.0, 3.0 ]

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
vz: [ -8.0, 7.0 ]
26 changes: 17 additions & 9 deletions examples/config/my_z_vertex_cuts.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
# Cut values for different run periods
clas12::ZVertexFilter:

# default cuts
- default:
electron_vz: [ -1.5, 1.3 ]
###################################################################################
# NOTE: for convenience, you can also set the log level from the configuration file
log: trace
###################################################################################

# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
electron_vz: [ -0.5, 0.5 ]
# scattered electron cuts
electron:

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
electron_vz: [ -0.8, 0.7 ]
# default cuts
- default:
vz: [ -1.5, 1.3 ]

# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
vz: [ -0.5, 0.5 ]

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
vz: [ -0.8, 0.7 ]
50 changes: 33 additions & 17 deletions src/iguana/algorithms/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace iguana {
template <typename OPTION_TYPE>
OPTION_TYPE Algorithm::GetOptionScalar(std::string const& key, YAMLReader::node_path_t node_path) const
{
try {
CompleteOptionNodePath(key, node_path);
auto opt = GetCachedOption<OPTION_TYPE>(key);
auto val = opt ? opt.value() : m_yaml_config->GetScalar<OPTION_TYPE>(node_path);
PrintOptionValue(key, val);
return val;
CompleteOptionNodePath(key, node_path);
auto opt = GetCachedOption<OPTION_TYPE>(key);
if(!opt.has_value()) {
opt = m_yaml_config->GetScalar<OPTION_TYPE>(node_path);
}
catch(std::runtime_error const& ex) {
m_log->Error("Failed to `GetOptionScalar` for key '{}'", key);
if(!opt.has_value()) {
m_log->Error("Failed to `GetOptionScalar` for key {:?}", key);
throw std::runtime_error("config file parsing issue");
}
PrintOptionValue(key, opt.value());
return opt.value();
}
template int Algorithm::GetOptionScalar(std::string const& key, YAMLReader::node_path_t node_path) const;
template double Algorithm::GetOptionScalar(std::string const& key, YAMLReader::node_path_t node_path) const;
Expand All @@ -37,17 +37,17 @@ namespace iguana {
template <typename OPTION_TYPE>
std::vector<OPTION_TYPE> Algorithm::GetOptionVector(std::string const& key, YAMLReader::node_path_t node_path) const
{
try {
CompleteOptionNodePath(key, node_path);
auto opt = GetCachedOption<std::vector<OPTION_TYPE>>(key);
auto val = opt ? opt.value() : m_yaml_config->GetVector<OPTION_TYPE>(node_path);
PrintOptionValue(key, val);
return val;
CompleteOptionNodePath(key, node_path);
auto opt = GetCachedOption<std::vector<OPTION_TYPE>>(key);
if(!opt.has_value()) {
opt = m_yaml_config->GetVector<OPTION_TYPE>(node_path);
}
catch(std::runtime_error const& ex) {
m_log->Error("Failed to `GetOptionVector` for key '{}'", key);
if(!opt.has_value()) {
m_log->Error("Failed to `GetOptionVector` for key {:?}", key);
throw std::runtime_error("config file parsing issue");
}
PrintOptionValue(key, opt.value());
return opt.value();
}
template std::vector<int> Algorithm::GetOptionVector(std::string const& key, YAMLReader::node_path_t node_path) const;
template std::vector<double> Algorithm::GetOptionVector(std::string const& key, YAMLReader::node_path_t node_path) const;
Expand Down Expand Up @@ -108,19 +108,35 @@ namespace iguana {

void Algorithm::ParseYAMLConfig()
{

// start YAMLReader instance, if not yet started
if(!m_yaml_config) {
// set config files and directories specified by `::SetConfigFile`, `::SetConfigDirectory`, etc.
o_user_config_file = GetCachedOption<std::string>("config_file").value_or("");
o_user_config_dir = GetCachedOption<std::string>("config_dir").value_or("");
m_log->Debug("Instantiating `YAMLReader`");
m_yaml_config = std::make_unique<YAMLReader>("config|" + m_name);
m_yaml_config->SetLogLevel(m_log->GetLevel());
m_yaml_config->SetLogLevel(m_log->GetLevel()); // synchronize log levels
m_yaml_config->AddDirectory(o_user_config_dir);
m_yaml_config->AddFile(m_default_config_file);
m_yaml_config->AddFile(o_user_config_file);
}
else
m_log->Debug("`YAMLReader` already instantiated for this algorithm; using that");

// parse the files
m_yaml_config->LoadFiles();

// if "log" was not set by `SetOption` (i.e., not in `m_option_cache`)
// - NB: not using `GetCachedOption<T>` here, since `T` can be a few different types for key=='log'
if(m_option_cache.find("log") == m_option_cache.end()) {
// check if 'log' is set in the YAML node for this algorithm
auto log_level_from_yaml = m_yaml_config->GetScalar<std::string>({m_class_name, "log"});
if(log_level_from_yaml) {
m_log->SetLevel(log_level_from_yaml.value());
m_yaml_config->SetLogLevel(log_level_from_yaml.value());
}
}
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions src/iguana/algorithms/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ namespace iguana {
else
m_log->Error("Option '{}' must be a string or a Logger::Level", key);
}
else
m_option_cache[key] = val;
m_option_cache[key] = val;
return val;
}

Expand Down
2 changes: 1 addition & 1 deletion src/iguana/algorithms/clas12/ZVertexFilter/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace iguana::clas12 {
std::lock_guard<std::mutex> const lock(m_mutex); // NOTE: be sure to lock successive `ConcurrentParam::Save` calls !!!
m_log->Trace("-> calling Reload({}, {})", runnum, key);
o_runnum->Save(runnum, key);
o_electron_vz_cuts->Save(GetOptionVector<double>("electron_vz", {GetConfig()->InRange("runs", runnum), "electron_vz"}), key);
o_electron_vz_cuts->Save(GetOptionVector<double>("electron_vz", {"electron", GetConfig()->InRange("runs", runnum), "vz"}), key);
}

bool ZVertexFilter::Filter(double const zvertex, int const pid, int const status, concurrent_key_t key) const
Expand Down
22 changes: 12 additions & 10 deletions src/iguana/algorithms/clas12/ZVertexFilter/Config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Cut values for different run periods
clas12::ZVertexFilter:

# default cuts
- default:
electron_vz: [ -20.0, 20.0 ]
# scattered electron cuts
electron:

# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
electron_vz: [ -13.0, 12.0 ]
# default cuts
- default:
vz: [ -20.0, 20.0 ]

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
electron_vz: [ -18.0, 10.0 ]
# RG-A fall2018 inbending
- runs: [ 4760, 5419 ]
vz: [ -13.0, 12.0 ]

# RG-A fall2018 outbending
- runs: [ 5420, 5674 ]
vz: [ -18.0, 10.0 ]
42 changes: 21 additions & 21 deletions src/iguana/services/YAMLReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace iguana {
///////////////////////////////////////////////////////////////////////////////

template <typename SCALAR>
SCALAR YAMLReader::GetScalar(YAML::Node node)
std::optional<SCALAR> YAMLReader::GetScalar(YAML::Node node)
{
if(node.IsDefined() && !node.IsNull()) {
try {
Expand All @@ -35,32 +35,32 @@ namespace iguana {
m_log->Error("YAML Misc. Exception: {}", e.what());
}
}
throw std::runtime_error("Failed `GetScalar`");
return std::nullopt;
}
template int YAMLReader::GetScalar(YAML::Node node);
template double YAMLReader::GetScalar(YAML::Node node);
template std::string YAMLReader::GetScalar(YAML::Node node);
template std::optional<int> YAMLReader::GetScalar(YAML::Node node);
template std::optional<double> YAMLReader::GetScalar(YAML::Node node);
template std::optional<std::string> YAMLReader::GetScalar(YAML::Node node);

///////////////////////////////////////////////////////////////////////////////

template <typename SCALAR>
SCALAR YAMLReader::GetScalar(node_path_t node_path)
std::optional<SCALAR> YAMLReader::GetScalar(node_path_t node_path)
{
for(auto const& [config, filename] : m_configs) {
auto node = FindNode(config, node_path);
if(node.IsDefined() && !node.IsNull())
return GetScalar<SCALAR>(node);
}
throw std::runtime_error("Failed `GetScalar`");
return std::nullopt;
}
template int YAMLReader::GetScalar(node_path_t node_path);
template double YAMLReader::GetScalar(node_path_t node_path);
template std::string YAMLReader::GetScalar(node_path_t node_path);
template std::optional<int> YAMLReader::GetScalar(node_path_t node_path);
template std::optional<double> YAMLReader::GetScalar(node_path_t node_path);
template std::optional<std::string> YAMLReader::GetScalar(node_path_t node_path);

///////////////////////////////////////////////////////////////////////////////

template <typename SCALAR>
std::vector<SCALAR> YAMLReader::GetVector(YAML::Node node)
std::optional<std::vector<SCALAR>> YAMLReader::GetVector(YAML::Node node)
{
if(node.IsDefined() && !node.IsNull() && node.IsSequence()) {
try {
Expand All @@ -76,27 +76,27 @@ namespace iguana {
m_log->Error("YAML Misc. Exception: {}", e.what());
}
}
throw std::runtime_error("Failed `GetVector`");
return std::nullopt;
}
template std::vector<int> YAMLReader::GetVector(YAML::Node node);
template std::vector<double> YAMLReader::GetVector(YAML::Node node);
template std::vector<std::string> YAMLReader::GetVector(YAML::Node node);
template std::optional<std::vector<int>> YAMLReader::GetVector(YAML::Node node);
template std::optional<std::vector<double>> YAMLReader::GetVector(YAML::Node node);
template std::optional<std::vector<std::string>> YAMLReader::GetVector(YAML::Node node);

///////////////////////////////////////////////////////////////////////////////

template <typename SCALAR>
std::vector<SCALAR> YAMLReader::GetVector(node_path_t node_path)
std::optional<std::vector<SCALAR>> YAMLReader::GetVector(node_path_t node_path)
{
for(auto const& [config, filename] : m_configs) {
auto node = FindNode(config, node_path);
if(node.IsDefined() && !node.IsNull())
return GetVector<SCALAR>(node);
}
throw std::runtime_error("Failed `GetVector`");
return std::nullopt;
}
template std::vector<int> YAMLReader::GetVector(node_path_t node_path);
template std::vector<double> YAMLReader::GetVector(node_path_t node_path);
template std::vector<std::string> YAMLReader::GetVector(node_path_t node_path);
template std::optional<std::vector<int>> YAMLReader::GetVector(node_path_t node_path);
template std::optional<std::vector<double>> YAMLReader::GetVector(node_path_t node_path);
template std::optional<std::vector<std::string>> YAMLReader::GetVector(node_path_t node_path);

///////////////////////////////////////////////////////////////////////////////

Expand All @@ -114,7 +114,7 @@ namespace iguana {
auto bounds_node = sub_node[key];
if(bounds_node.IsDefined()) {
auto bounds = GetVector<SCALAR>(bounds_node);
if(bounds.size() == 2 && bounds[0] <= val && bounds[1] >= val)
if(bounds.value().size() == 2 && bounds.value()[0] <= val && bounds.value()[1] >= val)
return sub_node;
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/iguana/services/YAMLReader.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <optional>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -37,27 +38,27 @@ namespace iguana {

/// Read a scalar value from a `YAML::Node`
/// @param node the `YAML::Node` to read
/// @return the scalar
/// @return the scalar, if found
template <typename SCALAR>
SCALAR GetScalar(YAML::Node node);
std::optional<SCALAR> GetScalar(YAML::Node node);

/// Read a scalar value from a `YAML::Node` path; searches all currently loaded config files.
/// @param node_path the `YAML::Node` path
/// @return the scalar
/// @return the scalar, if found
template <typename SCALAR>
SCALAR GetScalar(node_path_t node_path);
std::optional<SCALAR> GetScalar(node_path_t node_path);

/// Read a vector value from a `YAML::Node`
/// @param node the `YAML::Node` to read
/// @return the vector
/// @return the vector, if found
template <typename SCALAR>
std::vector<SCALAR> GetVector(YAML::Node node);
std::optional<std::vector<SCALAR>> GetVector(YAML::Node node);

/// Read a vector value from a `YAML::Node` path; searches all currently loaded config files.
/// @param node_path the `YAML::Node` path
/// @return the vector
/// @return the vector, if found
template <typename SCALAR>
std::vector<SCALAR> GetVector(node_path_t node_path);
std::optional<std::vector<SCALAR>> GetVector(node_path_t node_path);

/// Create a function to search a `YAML::Node` for a sub-`YAML::Node` such that
/// the scalar `val` is within a range specified by `key`
Expand Down
Loading