1 #ifndef ALIYAMLCONFIGURATION_H
2 #define ALIYAMLCONFIGURATION_H
5 #if !(defined(__CINT__) || defined(__MAKECINT__))
6 #include <yaml-cpp/yaml.h>
113 namespace PWG {
namespace Tools {
class AliYAMLConfiguration; } }
121 AliYAMLConfiguration(
const std::string prefixString =
"AliEmcalCorrection",
const std::string delimiterCharacter =
":");
136 int AddConfiguration(std::string configurationFilename, std::string configurationName =
"");
137 #if !(defined(__CINT__) || defined(__MAKECINT__))
138 int AddConfiguration(
const YAML::Node node, std::string configurationName =
"");
171 bool GetProperty(std::vector<std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const;
173 bool GetProperty(
const std::vector<std::string> propertyPath,
T & property,
const bool requiredProperty)
const;
176 bool GetProperty(std::string propertyName,
T & property,
const bool requiredProperty =
true)
const;
184 bool WriteProperty(std::string propertyName,
T & property, std::string configurationName =
"");
207 std::string
toString(
const int index = -1)
const;
208 std::ostream &
Print(std::ostream &in,
const int index = -1)
const;
209 std::ostream &
Print(std::ostream &in,
const std::string & configurationName)
const;
221 #if !(defined(__CINT__) || defined(__MAKECINT__))
223 void PrintConfiguration(std::ostream & stream,
const std::pair<std::string, YAML::Node> & configPair)
const;
230 #if !(defined(__CINT__) || defined(__MAKECINT__))
234 auto
PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage, const std::vector <
T> & property,
int) const -> decltype(property.begin(), property.end(), tempMessage << std::declval<
T>(),
void());
241 bool GetPropertyFromNode(const YAML::Node & node, std::
string propertyName, T & property) const;
243 bool GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode, const std::
string & configurationName, std::
string propertyName, T & property) const;
246 void WriteValue(YAML::Node & node, std::
string propertyName, T & proeprty);
261 #if !(defined(__CINT__) || defined(__MAKECINT__))
275 tempMessage <<
" with value \"" <<
property <<
"\"";
289 tempMessage <<
" with value(s):";
290 for (
auto it = property.begin(); it !=
property.end(); it++) {
291 tempMessage <<
"\n\t- " << *it;
310 tempMessage <<
" with a value that cannot be trivially printed";
343 if (node[propertyName])
345 property = node[propertyName].as<
T>();
365 propertyPath.push_back(propertyName);
366 return GetProperty(propertyPath, property, requiredProperty);
383 std::string requestedName =
"";
384 for (
auto & str : propertyPath)
386 if (requestedName.length() > 0) {
387 requestedName +=
":" + str;
395 return GetProperty(requestedName, property, requiredProperty);
413 std::size_t prefixStringLocation = propertyName.find(
fPrefixString);
414 if (prefixStringLocation != std::string::npos)
417 propertyName.erase(prefixStringLocation, prefixStringLocation +
fPrefixString.length());
420 bool setProperty =
false;
423 if (setProperty ==
true) {
424 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Property \"" << propertyName <<
"\" found!\n";
430 if (configPair.second.IsNull() !=
true)
432 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Looking for parameter \"" << propertyName <<
"\" in \"" << configPair.first <<
"\" configuration\n";
434 YAML::Node sharedParameters = configPair.second[
"sharedParameters"];
435 setProperty =
GetProperty(configPair.second, sharedParameters, configPair.first, propertyName, property);
439 if (setProperty !=
true && requiredProperty ==
true)
441 std::stringstream message;
442 message <<
"Failed to retrieve required property \""
443 << propertyName <<
"\" from available configurations!" << std::endl;
444 AliFatalGeneral(
"AliYAMLConfiguration", message.str().c_str());
477 std::stringstream tempMessage;
479 bool returnValue =
false;
481 const std::string specializationDelimiter =
"_";
482 size_t delimiterPosition = 0;
483 std::string tempPropertyName = propertyName;
485 if ((delimiterPosition = tempPropertyName.find(
fDelimiter)) != std::string::npos)
487 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
488 tempPropertyName.erase(0, delimiterPosition +
fDelimiter.length());
491 if (node[nodeName].IsDefined() ==
true)
494 YAML::Node tempNode = node[nodeName];
495 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper with node \"" << nodeName <<
"\".\n";
496 returnValue =
GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
502 if (node[nodeName].IsDefined() ==
false || returnValue ==
false)
505 if ((delimiterPosition = nodeName.find(specializationDelimiter)) != std::string::npos)
507 std::string specializationNodeName = nodeName.substr(0, delimiterPosition);
508 YAML::Node tempNode = node[specializationNodeName];
509 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper through dropping the specializtion and using node \"" << specializationNodeName <<
"\".\n";
510 returnValue =
GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
520 if (node[propertyName])
523 bool isShared =
false;
526 std::string sharedValueName =
"";
528 if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
531 sharedValueName = node[propertyName].as<std::string>();
538 tempMessage <<
"property \""
540 <<
"\" using " << ( isShared ?
"\"sharedParameters:" + sharedValueName +
"\" in " :
"" )
541 <<
"values from the " << configurationName <<
" configuration";
543 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Retrieveing " << tempMessage.str() <<
"\n";
546 bool retrievalResult =
false;
547 if (isShared ==
true) {
556 if (retrievalResult ==
true) {
559 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Succeeded in retrieveing " << tempMessage.str() <<
"\n";
584 unsigned int configurationIndex = 0;
585 if (configurationName !=
"")
591 AliErrorStream() <<
"No configurations available! Property will not be written!\n";
595 std::pair<std::string, YAML::Node> & configPair =
fConfigurations.at(configurationIndex);
597 WriteValue(configPair.second, propertyName, property);
598 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Final Node:\n" << configPair.second <<
"\n";
615 const std::string delimiter =
":";
616 size_t delimiterPosition = 0;
617 std::string tempPropertyName = propertyName;
619 if ((delimiterPosition = tempPropertyName.find(delimiter)) != std::string::npos)
621 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
622 tempPropertyName.erase(0, delimiterPosition + delimiter.length());
623 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"nodeName: " << nodeName <<
"\n";
624 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node Before:\n" << node <<
"\n";
625 if (node[nodeName].IsDefined()) {
626 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Using existing node\n";
627 YAML::Node tempNode = node[nodeName];
628 WriteValue(tempNode, tempPropertyName, property);
631 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Creating new node\n";
633 node[nodeName] = tempNode;
634 WriteValue(tempNode, tempPropertyName, property);
636 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node After:\n" << node <<
"\n";
640 node[propertyName] = property;
659 for (
const auto & configPair : configurations)
661 if (configPair.first == name) {
664 index = std::addressof(configPair) - std::addressof(configurations[0]);
UShort_t T(UShort_t m, UShort_t t)
std::ostream & operator<<(std::ostream &in, const PWG::Tools::AliYAMLConfiguration &myTask)