From de8c022334e36fb0836ed8beb242861b9c515c48 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:17:26 +0100 Subject: [PATCH] Drop need to include ptree in ConfigurableParam.h --- Common/SimConfig/test/testSimCutParam.cxx | 1 + Common/Utils/CMakeLists.txt | 3 +- .../include/CommonUtils/ConfigurableParam.h | 62 +++++---------- .../CommonUtils/ConfigurableParamReaders.h | 36 +++++++++ Common/Utils/src/ConfigurableParam.cxx | 70 +++++------------ Common/Utils/src/ConfigurableParamReaders.cxx | 76 +++++++++++++++++++ .../MUON/MCH/Raw/test/testClosureCoDec.cxx | 1 + .../MCH/Simulation/test/testDigitizer.cxx | 1 + .../MCH/Triggering/test/testEventFinder.cxx | 1 + Detectors/TPC/base/test/testTPCParameters.cxx | 7 +- .../include/TPCCalibration/IDCAverageGroup.h | 1 + .../include/TPCCalibration/IDCFactorization.h | 1 + .../include/TPCCalibration/SACFactorization.h | 1 + .../calibration/src/IDCGroupingParameter.cxx | 2 + .../TPC/reconstruction/macro/makeIonTail.C | 1 + Detectors/TPC/simulation/macro/toyCluster.C | 1 + .../TPC/workflow/src/ApplyCCDBCalibSpec.cxx | 1 + macro/o2sim.C | 1 + 18 files changed, 170 insertions(+), 97 deletions(-) create mode 100644 Common/Utils/include/CommonUtils/ConfigurableParamReaders.h create mode 100644 Common/Utils/src/ConfigurableParamReaders.cxx diff --git a/Common/SimConfig/test/testSimCutParam.cxx b/Common/SimConfig/test/testSimCutParam.cxx index 7a17ae9d1f2cf..468d76429cbba 100644 --- a/Common/SimConfig/test/testSimCutParam.cxx +++ b/Common/SimConfig/test/testSimCutParam.cxx @@ -15,6 +15,7 @@ #include #include "SimConfig/SimParams.h" #include "CommonUtils/ConfigurableParam.h" +#include using namespace o2::conf; diff --git a/Common/Utils/CMakeLists.txt b/Common/Utils/CMakeLists.txt index b0fa605cb6723..7f650b973990b 100644 --- a/Common/Utils/CMakeLists.txt +++ b/Common/Utils/CMakeLists.txt @@ -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 @@ -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 diff --git a/Common/Utils/include/CommonUtils/ConfigurableParam.h b/Common/Utils/include/CommonUtils/ConfigurableParam.h index 7099e37d5bc50..717a4c425fc82 100644 --- a/Common/Utils/include/CommonUtils/ConfigurableParam.h +++ b/Common/Utils/include/CommonUtils/ConfigurableParam.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include @@ -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 @@ -196,10 +191,12 @@ class ConfigurableParam template static T getValueAs(std::string key) { - if (!sIsFullyInitialized) { - initialize(); - } - return sPtree->get(key); + return [](auto* tree, const std::string& key) -> T { + if (!sIsFullyInitialized) { + initialize(); + } + return tree->template get(key); + }(sPtree, key); } template @@ -208,19 +205,21 @@ class ConfigurableParam if (!sIsFullyInitialized) { initialize(); } - assert(sPtree); - try { - auto key = mainkey + "." + subkey; - if (sPtree->get_optional(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(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) @@ -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(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&); @@ -321,7 +302,6 @@ class ConfigurableParam // (stored as a vector of pairs ) static EnumRegistry* sEnumRegistry; - static std::string sInputDir; static std::string sOutputDir; void setRegisterMode(bool b) { sRegisterMode = b; } diff --git a/Common/Utils/include/CommonUtils/ConfigurableParamReaders.h b/Common/Utils/include/CommonUtils/ConfigurableParamReaders.h new file mode 100644 index 0000000000000..d5ecd6cb97f7a --- /dev/null +++ b/Common/Utils/include/CommonUtils/ConfigurableParamReaders.h @@ -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 +#include + +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_ diff --git a/Common/Utils/src/ConfigurableParam.cxx b/Common/Utils/src/ConfigurableParam.cxx index e581970fc81fb..0a3c9fe2d7595 100644 --- a/Common/Utils/src/ConfigurableParam.cxx +++ b/Common/Utils/src/ConfigurableParam.cxx @@ -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 #include @@ -47,7 +48,6 @@ std::vector* ConfigurableParam::sRegisteredParamClasses = nu boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr; std::map>* ConfigurableParam::sKeyToStorageMap = nullptr; std::map* ConfigurableParam::sValueProvenanceMap = nullptr; -std::string ConfigurableParam::sInputDir = ""; std::string ConfigurableParam::sOutputDir = ""; EnumRegistry* ConfigurableParam::sEnumRegistry = nullptr; @@ -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(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; } // ------------------------------------------------------------------ @@ -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 { @@ -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> keyValPairs; auto request = o2::utils::Str::tokenize(paramsList, ',', true); @@ -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") { diff --git a/Common/Utils/src/ConfigurableParamReaders.cxx b/Common/Utils/src/ConfigurableParamReaders.cxx new file mode 100644 index 0000000000000..2f36f8b27f5fa --- /dev/null +++ b/Common/Utils/src/ConfigurableParamReaders.cxx @@ -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 +#include + +#include +#include +#include + +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 diff --git a/Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx b/Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx index 2407796773f57..b18ffd1abee63 100644 --- a/Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx +++ b/Detectors/MUON/MCH/Raw/test/testClosureCoDec.cxx @@ -16,6 +16,7 @@ #define BOOST_TEST_DYN_LINK #include +#include #include "DetectorsRaw/HBFUtils.h" #include "DetectorsRaw/RawFileWriter.h" #include "Framework/Logger.h" diff --git a/Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx b/Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx index 791ab8e7a3909..2a550220bb137 100644 --- a/Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx +++ b/Detectors/MUON/MCH/Simulation/test/testDigitizer.cxx @@ -31,6 +31,7 @@ #include "TGeoManager.h" #include "boost/format.hpp" #include +#include #include #include diff --git a/Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx b/Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx index eb786fc1bb068..60275f2cad2f4 100644 --- a/Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx +++ b/Detectors/MUON/MCH/Triggering/test/testEventFinder.cxx @@ -27,6 +27,7 @@ #include #include +#include #include "CommonUtils/ConfigurableParam.h" #include "SimulationDataFormat/MCCompLabel.h" diff --git a/Detectors/TPC/base/test/testTPCParameters.cxx b/Detectors/TPC/base/test/testTPCParameters.cxx index 015a6abd964ec..b8baa1e2a74ac 100644 --- a/Detectors/TPC/base/test/testTPCParameters.cxx +++ b/Detectors/TPC/base/test/testTPCParameters.cxx @@ -23,9 +23,9 @@ #include "TPCBase/ParameterGas.h" #include #include -namespace o2 -{ -namespace tpc +#include + +namespace o2::tpc { constexpr float NominalTimeBin = 8 * o2::constants::lhc::LHCBunchSpacingNS * 1e-3; @@ -367,5 +367,4 @@ BOOST_AUTO_TEST_CASE(ParameterGEM_test2) BOOST_CHECK_CLOSE(o2::conf::ConfigurableParam::getValueAs("TPCGEMParam.KappaStack"), 32, 1e-12); BOOST_CHECK_CLOSE(o2::conf::ConfigurableParam::getValueAs("TPCGEMParam.EfficiencyStack"), 33, 1e-3); } -} // namespace tpc } // namespace o2 diff --git a/Detectors/TPC/calibration/include/TPCCalibration/IDCAverageGroup.h b/Detectors/TPC/calibration/include/TPCCalibration/IDCAverageGroup.h index fecbe9f14f18b..dd9a5fc257935 100644 --- a/Detectors/TPC/calibration/include/TPCCalibration/IDCAverageGroup.h +++ b/Detectors/TPC/calibration/include/TPCCalibration/IDCAverageGroup.h @@ -20,6 +20,7 @@ #include "TPCCalibration/IDCAverageGroupBase.h" #include "TPCBase/Sector.h" #include "TPCBase/CalDet.h" +#include #include #include "Rtypes.h" diff --git a/Detectors/TPC/calibration/include/TPCCalibration/IDCFactorization.h b/Detectors/TPC/calibration/include/TPCCalibration/IDCFactorization.h index 13c9d0b98412f..4c7f119efcfe0 100644 --- a/Detectors/TPC/calibration/include/TPCCalibration/IDCFactorization.h +++ b/Detectors/TPC/calibration/include/TPCCalibration/IDCFactorization.h @@ -24,6 +24,7 @@ #include "TPCCalibration/IDCContainer.h" #include "TPCCalibration/IDCGroupHelperSector.h" #include "DataFormatsTPC/Defs.h" +#include namespace o2::tpc { diff --git a/Detectors/TPC/calibration/include/TPCCalibration/SACFactorization.h b/Detectors/TPC/calibration/include/TPCCalibration/SACFactorization.h index b1b4430c11a00..7141750b8ce37 100644 --- a/Detectors/TPC/calibration/include/TPCCalibration/SACFactorization.h +++ b/Detectors/TPC/calibration/include/TPCCalibration/SACFactorization.h @@ -21,6 +21,7 @@ #include "Rtypes.h" #include "TPCCalibration/IDCContainer.h" #include "DataFormatsTPC/Defs.h" +#include namespace o2::tpc { diff --git a/Detectors/TPC/calibration/src/IDCGroupingParameter.cxx b/Detectors/TPC/calibration/src/IDCGroupingParameter.cxx index bbecb4df8f8fd..895b6957d8a01 100644 --- a/Detectors/TPC/calibration/src/IDCGroupingParameter.cxx +++ b/Detectors/TPC/calibration/src/IDCGroupingParameter.cxx @@ -12,6 +12,8 @@ #include "TPCCalibration/IDCGroupingParameter.h" #include "Framework/Logger.h" #include "Algorithm/RangeTokenizer.h" +#include +#include #include using namespace o2::tpc; diff --git a/Detectors/TPC/reconstruction/macro/makeIonTail.C b/Detectors/TPC/reconstruction/macro/makeIonTail.C index 98db6540d20f7..d8d1be6830626 100644 --- a/Detectors/TPC/reconstruction/macro/makeIonTail.C +++ b/Detectors/TPC/reconstruction/macro/makeIonTail.C @@ -32,6 +32,7 @@ #include "TPCBase/CRUCalibHelpers.h" #include "TPCBase/CRU.h" #include "CommonUtils/TreeStreamRedirector.h" +#include using namespace o2::tpc; size_t digitsInSaturateion(std::vector& digits, bool correctCharge = false, CalPad* pedestals = nullptr, o2::utils::TreeStreamRedirector* stream = nullptr); diff --git a/Detectors/TPC/simulation/macro/toyCluster.C b/Detectors/TPC/simulation/macro/toyCluster.C index 257adaf5f79fc..2087a7773fe22 100644 --- a/Detectors/TPC/simulation/macro/toyCluster.C +++ b/Detectors/TPC/simulation/macro/toyCluster.C @@ -58,6 +58,7 @@ #include "TPCReconstruction/HwClusterer.h" #include "TPCSimulation/GEMAmplification.h" #endif +#include using namespace o2::tpc; void fillTPCHits(const float theta, const float phi, const float dedx, std::vector& hitGroupSector, std::pair& trackInfo); diff --git a/Detectors/TPC/workflow/src/ApplyCCDBCalibSpec.cxx b/Detectors/TPC/workflow/src/ApplyCCDBCalibSpec.cxx index 37429a04c4b34..9c588ff50a45c 100644 --- a/Detectors/TPC/workflow/src/ApplyCCDBCalibSpec.cxx +++ b/Detectors/TPC/workflow/src/ApplyCCDBCalibSpec.cxx @@ -19,6 +19,7 @@ #include "TPCBase/ParameterGas.h" #include "DataFormatsTPC/LtrCalibData.h" #include "TPCWorkflow/ApplyCCDBCalibSpec.h" +#include using namespace o2::framework; diff --git a/macro/o2sim.C b/macro/o2sim.C index 5257a225d3ed9..f610be730eeb7 100644 --- a/macro/o2sim.C +++ b/macro/o2sim.C @@ -39,6 +39,7 @@ #include #endif #include "migrateSimFiles.C" +#include void check_notransport() {