Skip to content
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
1 change: 1 addition & 0 deletions Common/SimConfig/test/testSimCutParam.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/test/unit_test.hpp>
#include "SimConfig/SimParams.h"
#include "CommonUtils/ConfigurableParam.h"
#include <boost/property_tree/ptree.hpp>

using namespace o2::conf;

Expand Down
3 changes: 2 additions & 1 deletion Common/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ o2_add_library(CommonUtils
src/RootChain.cxx src/CompStream.cxx src/ShmManager.cxx
src/ValueMonitor.cxx
src/StringUtils.cxx
src/ConfigurableParamHelper.cxx src/ConfigurableParam.cxx src/RootSerializableKeyValueStore.cxx
src/ConfigurableParamReaders.cxx src/ConfigurableParamHelper.cxx src/ConfigurableParam.cxx src/RootSerializableKeyValueStore.cxx
src/KeyValParam.cxx
src/FileSystemUtils.cxx
src/FIFO.cxx
Expand All @@ -39,6 +39,7 @@ o2_target_root_dictionary(CommonUtils
include/CommonUtils/MemFileHelper.h
include/CommonUtils/ConfigurableParam.h
include/CommonUtils/ConfigurableParamHelper.h
include/CommonUtils/ConfigurableParamReaders.h
include/CommonUtils/ConfigurationMacroHelper.h
include/CommonUtils/RootSerializableKeyValueStore.h
include/CommonUtils/KeyValParam.h
Expand Down
62 changes: 21 additions & 41 deletions Common/Utils/include/CommonUtils/ConfigurableParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <cassert>
#include <map>
#include <unordered_map>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <typeinfo>
#include <iostream>
#include <array>
Expand Down Expand Up @@ -176,15 +176,10 @@ class ConfigurableParam
static void printAllRegisteredParamNames();
static void printAllKeyValuePairs(bool useLogger = false);

static const std::string& getInputDir() { return sInputDir; }
static const std::string& getOutputDir() { return sOutputDir; }

static void setInputDir(const std::string& d) { sInputDir = d; }
static void setOutputDir(const std::string& d) { sOutputDir = d; }

static boost::property_tree::ptree readINI(std::string const& filepath);
static boost::property_tree::ptree readJSON(std::string const& filepath);
static boost::property_tree::ptree readConfigFile(std::string const& filepath);
static bool configFileExists(std::string const& filepath);

// writes a human readable JSON file of all parameters
Expand All @@ -196,10 +191,12 @@ class ConfigurableParam
template <typename T>
static T getValueAs(std::string key)
{
if (!sIsFullyInitialized) {
initialize();
}
return sPtree->get<T>(key);
return [](auto* tree, const std::string& key) -> T {
if (!sIsFullyInitialized) {
initialize();
}
return tree->template get<T>(key);
}(sPtree, key);
Comment thread
ktf marked this conversation as resolved.
}

template <typename T>
Expand All @@ -208,19 +205,21 @@ class ConfigurableParam
if (!sIsFullyInitialized) {
initialize();
}
assert(sPtree);
try {
auto key = mainkey + "." + subkey;
if (sPtree->get_optional<std::string>(key).is_initialized()) {
sPtree->put(key, x);
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
if (changed != EParamUpdateStatus::Failed) {
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
return [&subkey, &x, &mainkey](auto* tree) -> void {
assert(tree);
try {
auto key = mainkey + "." + subkey;
if (tree->template get_optional<std::string>(key).is_initialized()) {
tree->put(key, x);
auto changed = updateThroughStorageMap(mainkey, subkey, typeid(T), (void*)&x);
if (changed != EParamUpdateStatus::Failed) {
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
}
}
} catch (std::exception const& e) {
std::cerr << "Error in setValue (T) " << e.what() << "\n";
}
} catch (std::exception const& e) {
std::cerr << "Error in setValue (T) " << e.what() << "\n";
}
}(sPtree);
}

static void setProvenance(std::string const& mainkey, std::string const& subkey, EParamProvenance p)
Expand All @@ -242,25 +241,7 @@ class ConfigurableParam

// specialized for std::string
// which means that the type will be converted internally
static void setValue(std::string const& key, std::string const& valuestring)
{
if (!sIsFullyInitialized) {
initialize();
}
assert(sPtree);
try {
if (sPtree->get_optional<std::string>(key).is_initialized()) {
sPtree->put(key, valuestring);
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
if (changed != EParamUpdateStatus::Failed) {
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
}
}
} catch (std::exception const& e) {
std::cerr << "Error in setValue (string) " << e.what() << "\n";
}
}

static void setValue(std::string const& key, std::string const& valuestring);
static void setEnumValue(const std::string&, const std::string&);
static void setArrayValue(const std::string&, const std::string&);

Expand Down Expand Up @@ -321,7 +302,6 @@ class ConfigurableParam
// (stored as a vector of pairs <enumValueLabel, enumValueInt>)
static EnumRegistry* sEnumRegistry;

static std::string sInputDir;
static std::string sOutputDir;

void setRegisterMode(bool b) { sRegisterMode = b; }
Expand Down
36 changes: 36 additions & 0 deletions Common/Utils/include/CommonUtils/ConfigurableParamReaders.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef O2_COMMON_UTILS_CONFIGURABLEPARAMREADERS_H_
#define O2_COMMON_UTILS_CONFIGURABLEPARAMREADERS_H_

#include <boost/property_tree/ptree.hpp>
#include <string>

namespace o2::conf
{

// Helpers to read ConfigurableParam from different file formats
class ConfigurableParamReaders
{
public:
static void setInputDir(const std::string& d) { sInputDir = d; }
static const std::string& getInputDir() { return sInputDir; }

static boost::property_tree::ptree readINI(std::string const& filepath);
static boost::property_tree::ptree readJSON(std::string const& filepath);
static boost::property_tree::ptree readConfigFile(std::string const& filepath);

private:
static std::string sInputDir;
};

} // namespace o2::conf
#endif // O2_COMMON_UTILS_CONF_CONFIGURABLEPARAMREADERS_H_
70 changes: 19 additions & 51 deletions Common/Utils/src/ConfigurableParam.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

//first version 8/2018, Sandro Wenzel
// first version 8/2018, Sandro Wenzel

#include "CommonUtils/ConfigurableParam.h"
#include "CommonUtils/StringUtils.h"
#include "CommonUtils/KeyValParam.h"
#include "CommonUtils/ConfigurableParamReaders.h"
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/algorithm/string/predicate.hpp>
#include <boost/property_tree/ptree.hpp>
Expand Down Expand Up @@ -47,7 +48,6 @@ std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nu
boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr;
std::map<std::string, std::pair<std::type_info const&, void*>>* ConfigurableParam::sKeyToStorageMap = nullptr;
std::map<std::string, ConfigurableParam::EParamProvenance>* ConfigurableParam::sValueProvenanceMap = nullptr;
std::string ConfigurableParam::sInputDir = "";
std::string ConfigurableParam::sOutputDir = "";
EnumRegistry* ConfigurableParam::sEnumRegistry = nullptr;

Expand Down Expand Up @@ -194,60 +194,28 @@ void ConfigurableParam::writeINI(std::string const& filename, std::string const&

bool ConfigurableParam::configFileExists(std::string const& filepath)
{
return std::filesystem::exists(o2::utils::Str::concat_string(sInputDir, filepath));
return std::filesystem::exists(o2::utils::Str::concat_string(ConfigurableParamReaders::getInputDir(), filepath));
}

// ------------------------------------------------------------------

boost::property_tree::ptree ConfigurableParam::readConfigFile(std::string const& filepath)
void ConfigurableParam::setValue(std::string const& key, std::string const& valuestring)
{
auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
if (!std::filesystem::exists(inpfilename)) {
LOG(fatal) << inpfilename << " : config file does not exist!";
}

boost::property_tree::ptree pt;

if (boost::iends_with(inpfilename, ".ini")) {
pt = readINI(inpfilename);
} else if (boost::iends_with(inpfilename, ".json")) {
pt = readJSON(inpfilename);
} else {
LOG(fatal) << "Configuration file must have either .ini or .json extension";
}

return pt;
}

// ------------------------------------------------------------------

boost::property_tree::ptree ConfigurableParam::readINI(std::string const& filepath)
{
boost::property_tree::ptree pt;
try {
boost::property_tree::read_ini(filepath, pt);
} catch (const boost::property_tree::ptree_error& e) {
LOG(fatal) << "Failed to read INI config file " << filepath << " (" << e.what() << ")";
} catch (...) {
LOG(fatal) << "Unknown error when reading INI config file ";
if (!sIsFullyInitialized) {
initialize();
}

return pt;
}

// ------------------------------------------------------------------

boost::property_tree::ptree ConfigurableParam::readJSON(std::string const& filepath)
{
boost::property_tree::ptree pt;

assert(sPtree);
try {
boost::property_tree::read_json(filepath, pt);
} catch (const boost::property_tree::ptree_error& e) {
LOG(fatal) << "Failed to read JSON config file " << filepath << " (" << e.what() << ")";
if (sPtree->get_optional<std::string>(key).is_initialized()) {
sPtree->put(key, valuestring);
auto changed = updateThroughStorageMapWithConversion(key, valuestring);
if (changed != EParamUpdateStatus::Failed) {
sValueProvenanceMap->find(key)->second = kRT; // set to runtime
}
}
} catch (std::exception const& e) {
std::cerr << "Error in setValue (string) " << e.what() << "\n";
}

return pt;
}

// ------------------------------------------------------------------
Expand All @@ -258,7 +226,7 @@ void ConfigurableParam::writeJSON(std::string const& filename, std::string const
LOG(info) << "ignoring writing of json file " << filename;
return;
}
initPropertyTree(); // update the boost tree before writing
initPropertyTree(); // update the boost tree before writing
auto outfilename = o2::utils::Str::concat_string(sOutputDir, filename);
if (!keyOnly.empty()) { // write ini for selected key only
try {
Expand Down Expand Up @@ -409,7 +377,7 @@ void ConfigurableParam::updateFromFile(std::string const& configFile, std::strin
return;
}

boost::property_tree::ptree pt = readConfigFile(cfgfile);
boost::property_tree::ptree pt = ConfigurableParamReaders::readConfigFile(cfgfile);

std::vector<std::pair<std::string, std::string>> keyValPairs;
auto request = o2::utils::Str::tokenize(paramsList, ',', true);
Expand Down Expand Up @@ -522,7 +490,7 @@ void ConfigurableParam::updateFromString(std::string const& configString)

const auto& kv = o2::conf::KeyValParam::Instance();
if (getProvenance("keyval.input_dir") != kCODE) {
sInputDir = o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.input_dir));
ConfigurableParamReaders::setInputDir(o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(kv.input_dir)));
}
if (getProvenance("keyval.output_dir") != kCODE) {
if (kv.output_dir == "/dev/null") {
Expand Down
76 changes: 76 additions & 0 deletions Common/Utils/src/ConfigurableParamReaders.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#include "CommonUtils/ConfigurableParamReaders.h"
#include "CommonUtils/StringUtils.h"
#include <fairlogger/Logger.h>
#include <filesystem>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace o2::conf
{
// ------------------------------------------------------------------

boost::property_tree::ptree ConfigurableParamReaders::readINI(std::string const& filepath)
{
boost::property_tree::ptree pt;
try {
boost::property_tree::read_ini(filepath, pt);
} catch (const boost::property_tree::ptree_error& e) {
LOG(fatal) << "Failed to read INI config file " << filepath << " (" << e.what() << ")";
} catch (...) {
LOG(fatal) << "Unknown error when reading INI config file ";
}

return pt;
}

// ------------------------------------------------------------------

boost::property_tree::ptree ConfigurableParamReaders::readJSON(std::string const& filepath)
{
boost::property_tree::ptree pt;

try {
boost::property_tree::read_json(filepath, pt);
} catch (const boost::property_tree::ptree_error& e) {
LOG(fatal) << "Failed to read JSON config file " << filepath << " (" << e.what() << ")";
}

return pt;
}

boost::property_tree::ptree ConfigurableParamReaders::readConfigFile(std::string const& filepath)
{
auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
if (!std::filesystem::exists(inpfilename)) {
LOG(fatal) << inpfilename << " : config file does not exist!";
}

boost::property_tree::ptree pt;

if (boost::iends_with(inpfilename, ".ini")) {
pt = ConfigurableParamReaders::readINI(inpfilename);
} else if (boost::iends_with(inpfilename, ".json")) {
pt = ConfigurableParamReaders::readJSON(inpfilename);
} else {
LOG(fatal) << "Configuration file must have either .ini or .json extension";
}

return pt;
}

std::string ConfigurableParamReaders::sInputDir = "";

} // namespace o2::conf
1 change: 1 addition & 0 deletions Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <boost/property_tree/ptree.hpp>
#include "DetectorsRaw/HBFUtils.h"
#include "DetectorsRaw/RawFileWriter.h"
#include "Framework/Logger.h"
Expand Down
1 change: 1 addition & 0 deletions Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "TGeoManager.h"
#include "boost/format.hpp"
#include <boost/test/data/test_case.hpp>
#include <boost/property_tree/ptree.hpp>
#include <algorithm>
#include <unordered_map>

Expand Down
1 change: 1 addition & 0 deletions Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <vector>

#include <boost/test/unit_test.hpp>
#include <boost/property_tree/ptree.hpp>

#include "CommonUtils/ConfigurableParam.h"
#include "SimulationDataFormat/MCCompLabel.h"
Expand Down
Loading