1 #ifndef ALIYAMLCONFIGURATION_H 2 #define ALIYAMLCONFIGURATION_H 5 #if !(defined(__CINT__) || defined(__MAKECINT__)) 6 #include <yaml-cpp/yaml.h> 117 #if !(defined(__CINT__) || defined(__MAKECINT__)) 122 return Node(str.Data());
125 if (!node.IsScalar()) {
137 namespace PWG {
namespace Tools {
class AliYAMLConfiguration; } }
145 AliYAMLConfiguration(
const std::string prefixString =
"AliEmcalCorrection",
const std::string delimiterCharacter =
":");
158 int AddEmptyConfiguration(
const std::string & configurationName);
160 int AddConfiguration(std::string configurationFilename, std::string configurationName =
"");
161 #if !(defined(__CINT__) || defined(__MAKECINT__)) 162 int AddConfiguration(
const YAML::Node node, std::string configurationName =
"");
168 bool DoesConfigurationExist(
const int i)
const {
return i >= 0 &&
static_cast<const unsigned int>(i) < fConfigurations.size(); }
170 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i)
const {
return fConfigurations.at(i); }
171 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const std::string & name)
const {
return GetConfiguration(GetConfigurationIndexFromName(name, fConfigurations)); }
172 std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i) {
return fConfigurations.at(i); }
186 bool RemoveConfiguration(
const unsigned int i);
195 bool GetProperty(std::vector<std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const;
197 bool GetProperty(
const std::vector<std::string> propertyPath,
T & property,
const bool requiredProperty)
const;
200 bool GetProperty(std::string propertyName,
T & property,
const bool requiredProperty =
true)
const;
208 bool WriteProperty(std::string propertyName,
T & property, std::string configurationName =
"");
215 bool WriteConfiguration(
const std::string &
filename,
const unsigned int i)
const;
216 bool WriteConfiguration(
const std::string & filename,
const std::string & configurationName)
const;
222 bool CompareConfigurations(
const int config1,
const int config2)
const;
224 bool CompareConfigurations(
const std::string & config1,
const std::string & config2)
const {
return CompareConfigurations(GetConfigurationIndexFromName(config1, fConfigurations), GetConfigurationIndexFromName(config2, fConfigurations)); }
231 std::string toString(
const int index = -1)
const;
232 std::ostream &
Print(std::ostream &in,
const int index = -1)
const;
233 std::ostream &
Print(std::ostream &in,
const std::string & configurationName)
const;
242 inline bool DoesFileExist(
const std::string & filename)
const;
243 void SetupReadingConfigurationFilePath(std::string & filename,
const std::string & fileIdentifier)
const;
244 void WriteConfigurationToFilePath(
const std::string & localFilename, std::string filename)
const;
245 #if !(defined(__CINT__) || defined(__MAKECINT__)) 247 void PrintConfiguration(std::ostream & stream,
const std::pair<std::string, YAML::Node> & configPair)
const;
251 unsigned int GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const;
253 bool IsSharedValue(std::string & value)
const;
254 #if !(defined(__CINT__) || defined(__MAKECINT__)) 256 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int)
const -> decltype(tempMessage << property,
void());
258 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int)
const -> decltype(property.begin(),
property.end(), tempMessage << std::declval<T>(),
void());
260 void PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const;
262 void PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const;
265 bool GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const;
267 bool GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const;
270 void WriteValue(YAML::Node & node, std::string propertyName,
T & proeprty);
285 #if !(defined(__CINT__) || defined(__MAKECINT__)) 297 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int) const -> decltype(tempMessage << property,
void())
299 tempMessage <<
" with value \"" <<
property <<
"\"";
311 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int) const -> decltype(property.begin(), property.end(), tempMessage <<
std::declval<
T>(),
void())
313 tempMessage <<
" with value(s):";
314 for (
auto it = property.begin(); it !=
property.end(); it++) {
315 tempMessage <<
"\n\t- " << *it;
331 void AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const 334 tempMessage <<
" with a value that cannot be trivially printed";
349 void AliYAMLConfiguration::PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const 352 AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(tempMessage, property, 0);
365 bool AliYAMLConfiguration::GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const 367 if (node[propertyName])
369 property = node[propertyName].as<
T>();
387 bool AliYAMLConfiguration::GetProperty(std::vector <std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const 389 propertyPath.push_back(propertyName);
390 return GetProperty(propertyPath, property, requiredProperty);
404 bool AliYAMLConfiguration::GetProperty(
const std::vector <std::string> propertyPath,
T & property,
const bool requiredProperty)
const 407 std::string requestedName =
"";
408 for (
auto & str : propertyPath)
410 if (requestedName.length() > 0) {
411 requestedName +=
":" + str;
419 return GetProperty(requestedName, property, requiredProperty);
434 bool AliYAMLConfiguration::GetProperty(std::string propertyName,
T & property,
const bool requiredProperty)
const 437 std::size_t prefixStringLocation = propertyName.find(fPrefixString);
438 if (prefixStringLocation != std::string::npos)
441 propertyName.erase(prefixStringLocation, prefixStringLocation + fPrefixString.length());
444 bool setProperty =
false;
445 for (
auto configPair : fConfigurations)
447 if (setProperty ==
true) {
448 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Property \"" << propertyName <<
"\" found!\n";
454 if (configPair.second.IsNull() !=
true)
456 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Looking for parameter \"" << propertyName <<
"\" in \"" << configPair.first <<
"\" configuration\n";
458 YAML::Node sharedParameters = configPair.second[
"sharedParameters"];
459 setProperty = GetProperty(configPair.second, sharedParameters, configPair.first, propertyName, property);
463 if (setProperty !=
true && requiredProperty ==
true)
465 std::stringstream message;
466 message <<
"Failed to retrieve required property \"" 467 << propertyName <<
"\" from available configurations!" << std::endl;
468 AliFatalGeneral(
"AliYAMLConfiguration", message.str().c_str());
498 bool AliYAMLConfiguration::GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const 501 std::stringstream tempMessage;
503 bool returnValue =
false;
505 const std::string specializationDelimiter =
"_";
506 size_t delimiterPosition = 0;
507 std::string tempPropertyName = propertyName;
509 if ((delimiterPosition = tempPropertyName.find(fDelimiter)) != std::string::npos)
511 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
512 tempPropertyName.erase(0, delimiterPosition + fDelimiter.length());
515 if (node[nodeName].IsDefined() ==
true)
518 YAML::Node tempNode = node[nodeName];
519 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper with node \"" << nodeName <<
"\".\n";
520 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
526 if (node[nodeName].IsDefined() ==
false || returnValue ==
false)
529 if ((delimiterPosition = nodeName.find(specializationDelimiter)) != std::string::npos)
531 std::string specializationNodeName = nodeName.substr(0, delimiterPosition);
532 YAML::Node tempNode = node[specializationNodeName];
533 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper through dropping the specializtion and using node \"" << specializationNodeName <<
"\".\n";
534 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
544 if (node[propertyName])
547 bool isShared =
false;
550 std::string sharedValueName =
"";
552 if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
555 sharedValueName = node[propertyName].as<std::string>();
557 isShared = IsSharedValue(sharedValueName);
562 tempMessage <<
"property \"" 564 <<
"\" using " << ( isShared ?
"\"sharedParameters:" + sharedValueName +
"\" in " :
"" )
565 <<
"values from the " << configurationName <<
" configuration";
567 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Retrieveing " << tempMessage.str() <<
"\n";
570 bool retrievalResult =
false;
571 if (isShared ==
true) {
573 retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
576 retrievalResult = GetPropertyFromNode(node, propertyName, property);
580 if (retrievalResult ==
true) {
582 AliYAMLConfiguration::PrintRetrievedPropertyValue(tempMessage, property);
583 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Succeeded in retrieveing " << tempMessage.str() <<
"\n";
606 bool AliYAMLConfiguration::WriteProperty(std::string propertyName,
T & property, std::string configurationName)
608 unsigned int configurationIndex = 0;
609 if (configurationName !=
"")
611 configurationIndex = GetConfigurationIndexFromName(configurationName, fConfigurations);
614 if (fConfigurations.size() == 0) {
615 AliErrorStream() <<
"No configurations available! Property will not be written!\n";
619 std::pair<std::string, YAML::Node> & configPair = fConfigurations.at(configurationIndex);
621 WriteValue(configPair.second, propertyName, property);
622 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Final Node:\n" << configPair.second <<
"\n";
635 void AliYAMLConfiguration::WriteValue(YAML::Node & node, std::string propertyName,
T & property)
639 const std::string delimiter =
":";
640 size_t delimiterPosition = 0;
641 std::string tempPropertyName = propertyName;
643 if ((delimiterPosition = tempPropertyName.find(delimiter)) != std::string::npos)
645 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
646 tempPropertyName.erase(0, delimiterPosition + delimiter.length());
647 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"nodeName: " << nodeName <<
"\n";
648 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node Before:\n" << node <<
"\n";
649 if (node[nodeName].IsDefined()) {
650 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Using existing node\n";
651 YAML::Node tempNode = node[nodeName];
652 WriteValue(tempNode, tempPropertyName, property);
655 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Creating new node\n";
657 node[nodeName] = tempNode;
658 WriteValue(tempNode, tempPropertyName, property);
660 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node After:\n" << node <<
"\n";
664 node[propertyName] = property;
680 unsigned int AliYAMLConfiguration::GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const 683 for (
const auto & configPair : configurations)
685 if (configPair.first == name) {
688 index = std::addressof(configPair) - std::addressof(configurations[0]);
void Print(std::ostream &o, const char *name, Double_t dT, Double_t dVM, Double_t alldT, Double_t alldVM)
UShort_t T(UShort_t m, UShort_t t)
std::ostream & operator<<(std::ostream &in, const PWG::Tools::AliYAMLConfiguration &myTask)
Namespace for PWG framework classes.
static Node encode(const TString &str)
static bool decode(const Node &node, TString &str)