1 #ifndef ALIYAMLCONFIGURATION_H
2 #define ALIYAMLCONFIGURATION_H
5 #if !(defined(__CINT__) || defined(__MAKECINT__))
6 #include <yaml-cpp/yaml.h>
95 AliYAMLConfiguration(
const std::string prefixString =
"AliEmcalCorrection",
const std::string delimiterCharacter =
":");
103 bool AddConfiguration(std::string configurationFilename, std::string configurationName =
"");
104 #if !(defined(__CINT__) || defined(__MAKECINT__))
105 bool AddConfiguration(
const YAML::Node node, std::string configurationName =
"");
114 bool GetProperty(std::vector<std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const;
116 bool GetProperty(
const std::vector<std::string> propertyPath,
T & property,
const bool requiredProperty)
const;
119 bool GetProperty(std::string propertyName,
T & property,
const bool requiredProperty =
true)
const;
123 bool WriteProperty(std::string propertyName,
T & property, std::string configurationName =
"");
142 unsigned int GetConfigurationIndexByName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const;
145 #if !(defined(__CINT__) || defined(__MAKECINT__))
149 auto
PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage, const std::vector <
T> & property,
int) const -> decltype(property.begin(), property.end(), tempMessage << std::declval<
T>(),
void());
156 bool GetPropertyFromNode(const YAML::Node & node, std::
string propertyName, T & property) const;
158 bool GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode, const std::
string & configurationName, std::
string propertyName, T & property) const;
161 void WriteValue(YAML::Node & node, std::
string propertyName, T & proeprty);
176 #if !(defined(__CINT__) || defined(__MAKECINT__))
190 tempMessage <<
" with value \"" <<
property <<
"\"";
204 tempMessage <<
" with value(s):";
205 for (
auto it = property.begin(); it !=
property.end(); it++) {
206 tempMessage <<
"\n\t- " << *it;
225 tempMessage <<
" with a value that cannot be trivially printed";
258 if (node[propertyName])
260 property = node[propertyName].as<
T>();
280 propertyPath.push_back(propertyName);
281 return GetProperty(propertyPath, property, requiredProperty);
298 std::string requestedName =
"";
299 for (
auto & str : propertyPath)
301 if (requestedName.length() > 0) {
302 requestedName +=
":" + str;
310 return GetProperty(requestedName, property, requiredProperty);
329 std::size_t prefixStringLocation = propertyName.find(
fPrefixString);
330 if (prefixStringLocation != std::string::npos)
333 propertyName.erase(prefixStringLocation, prefixStringLocation +
fPrefixString.length());
336 bool setProperty =
false;
339 if (setProperty ==
true) {
340 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Property \"" << propertyName <<
"\" found!\n";
346 if (configPair.second.IsNull() !=
true)
348 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Looking for parameter \"" << propertyName <<
"\" in \"" << configPair.first <<
"\" configuration\n";
350 YAML::Node sharedParameters = configPair.second[
"sharedParameters"];
351 setProperty =
GetProperty(configPair.second, sharedParameters, configPair.first, propertyName, property);
355 if (setProperty !=
true && requiredProperty ==
true)
357 std::stringstream message;
358 message <<
"Failed to retrieve required property \""
359 << propertyName <<
"\" from available configurations!" << std::endl;
360 AliFatalGeneral(
"AliYAMLConfiguration", message.str().c_str());
393 std::stringstream tempMessage;
395 bool returnValue =
false;
397 const std::string specializationDelimiter =
"_";
398 size_t delimiterPosition = 0;
399 std::string tempPropertyName = propertyName;
401 if ((delimiterPosition = tempPropertyName.find(
fDelimiter)) != std::string::npos)
403 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
404 tempPropertyName.erase(0, delimiterPosition +
fDelimiter.length());
407 if (node[nodeName].IsDefined() ==
true)
410 YAML::Node tempNode = node[nodeName];
411 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper with node \"" << nodeName <<
"\".\n";
412 returnValue =
GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
418 if (node[nodeName].IsDefined() ==
false || returnValue ==
false)
421 if ((delimiterPosition = nodeName.find(specializationDelimiter)) != std::string::npos)
423 std::string specializationNodeName = nodeName.substr(0, delimiterPosition);
424 YAML::Node tempNode = node[specializationNodeName];
425 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper through dropping the specializtion and using node \"" << specializationNodeName <<
"\".\n";
426 returnValue =
GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
436 if (node[propertyName])
439 bool isShared =
false;
442 std::string sharedValueName =
"";
444 if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
447 sharedValueName = node[propertyName].as<std::string>();
454 tempMessage <<
"property \""
456 <<
"\" using " << ( isShared ?
"\"sharedParameters:" + sharedValueName +
"\" in " :
"" )
457 <<
"values from the " << configurationName <<
" configuration";
459 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Retrieveing " << tempMessage.str() <<
"\n";
462 bool retrievalResult =
false;
463 if (isShared ==
true) {
472 if (retrievalResult ==
true) {
475 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Succeeded in retrieveing " << tempMessage.str() <<
"\n";
500 unsigned int configurationIndex = 0;
501 if (configurationName !=
"")
507 AliErrorStream() <<
"No configurations available! Property will not be written!\n";
511 std::pair<std::string, YAML::Node> & configPair =
fConfigurations.at(configurationIndex);
513 WriteValue(configPair.second, propertyName, property);
514 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Final Node:\n" << configPair.second <<
"\n";
531 const std::string delimiter =
":";
532 size_t delimiterPosition = 0;
533 std::string tempPropertyName = propertyName;
535 if ((delimiterPosition = tempPropertyName.find(delimiter)) != std::string::npos)
537 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
538 tempPropertyName.erase(0, delimiterPosition + delimiter.length());
539 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"nodeName: " << nodeName <<
"\n";
540 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node Before:\n" << node <<
"\n";
541 if (node[nodeName].IsDefined()) {
542 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Using existing node\n";
543 YAML::Node tempNode = node[nodeName];
544 WriteValue(tempNode, tempPropertyName, property);
547 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Creating new node\n";
549 node[nodeName] = tempNode;
550 WriteValue(tempNode, tempPropertyName, property);
552 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node After:\n" << node <<
"\n";
556 node[propertyName] = property;
574 for (
const auto & configPair : configurations)
576 if (configPair.first == name) {
579 index = std::addressof(configPair) - std::addressof(configurations[0]);
AliYAMLConfiguration(const std::string prefixString="AliEmcalCorrection", const std::string delimiterCharacter=":")
void WriteConfigurationToFilePath(const std::string &localFilename, std::string filename) const
void PrintConfiguration(const unsigned int i=0) const
bool WriteConfiguration(const std::string &filename, const unsigned int i) const
bool GetPropertyFromNode(const YAML::Node &node, std::string propertyName, T &property) const
std::string fDelimiter
Delimiter character to separate each level of the request.
bool AddEmptyConfiguration(const std::string &configurationName)
Add YAML configuration at configurationFilename to available configurations.
bool WriteProperty(std::string propertyName, T &property, std::string configurationName="")
UShort_t T(UShort_t m, UShort_t t)
void WriteValue(YAML::Node &node, std::string propertyName, T &proeprty)
void PrintConfigurations() const
auto PrintRetrievedPropertyValueImpl(std::stringstream &tempMessage, const T &property, int) const -> decltype(tempMessage<< property, void())
bool GetProperty(std::vector< std::string > propertyPath, const std::string &propertyName, T &property, const bool requiredProperty) const
std::vector< std::pair< std::string, std::string > > fConfigurationsStrings
Contains all YAML configurations as strings so that they can be streamed.
const std::pair< std::string, YAML::Node > & GetConfiguration(const std::string &name) const
void PrintRetrievedPropertyValue(std::stringstream &tempMessage, const T &property) const
unsigned int GetConfigurationIndexByName(const std::string &name, const std::vector< std::pair< std::string, T >> &configurations) const
bool AddConfiguration(std::string configurationFilename, std::string configurationName="")
virtual ~AliYAMLConfiguration()
const std::pair< std::string, YAML::Node > & GetConfiguration(const int i) const
bool fInitialized
True if the configurations have been initialized.
YAML configuration class for AliPhysics.
std::pair< std::string, YAML::Node > & GetConfiguration(const std::string &name)
std::string fPrefixString
Contains the prefix of any names base node names which should be removed.
std::pair< std::string, YAML::Node > & GetConfiguration(const int i)
std::vector< std::pair< std::string, YAML::Node > > fConfigurations
! Contains all YAML configurations. The first element has the highest precedence. ...
bool DoesFileExist(const std::string &filename) const
bool IsSharedValue(std::string &value) const
void SetupReadingConfigurationFilePath(std::string &filename, const std::string &fileIdentifier) const