1 #ifndef ALIYAMLCONFIGURATION_H 2 #define ALIYAMLCONFIGURATION_H 5 #if !(defined(__CINT__) || defined(__MAKECINT__)) 6 #include <yaml-cpp/yaml.h> 136 #if !(defined(__CINT__) || defined(__MAKECINT__)) 141 return Node(str.Data());
144 if (!node.IsScalar()) {
164 template <
typename It>
172 It
end()
const {
return e; }
175 template <
typename ORange,
typename OIt = decltype(std::begin(std::declval<ORange>())),
176 typename It = std::reverse_iterator<OIt>>
179 return Range<It>(It(std::end(originalRange)), It(std::begin(originalRange)));
184 namespace PWG {
namespace Tools {
class AliYAMLConfiguration; } }
192 AliYAMLConfiguration(
const std::string prefixString =
"AliEmcalCorrection",
const std::string delimiterCharacter =
":");
205 int AddEmptyConfiguration(
const std::string & configurationName);
207 int AddConfiguration(std::string configurationFilename, std::string configurationName =
"");
208 #if !(defined(__CINT__) || defined(__MAKECINT__)) 209 int AddConfiguration(
const YAML::Node node, std::string configurationName =
"");
215 bool DoesConfigurationExist(
const int i)
const {
return i >= 0 &&
static_cast<const unsigned int>(i) < fConfigurations.size(); }
217 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i)
const {
return fConfigurations.at(i); }
218 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const std::string & name)
const {
return GetConfiguration(GetConfigurationIndexFromName(name, fConfigurations)); }
219 std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i) {
return fConfigurations.at(i); }
233 bool RemoveConfiguration(
const unsigned int i);
242 bool GetProperty(std::vector<std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const;
244 bool GetProperty(
const std::vector<std::string> propertyPath,
T & property,
const bool requiredProperty)
const;
247 bool GetProperty(std::string propertyName,
T & property,
const bool requiredProperty =
true)
const;
255 bool WriteProperty(std::string propertyName,
T & property, std::string configurationName =
"");
262 bool WriteConfiguration(
const std::string &
filename,
const unsigned int i)
const;
263 bool WriteConfiguration(
const std::string & filename,
const std::string & configurationName)
const;
269 bool CompareConfigurations(
const int config1,
const int config2)
const;
271 bool CompareConfigurations(
const std::string & config1,
const std::string & config2)
const {
return CompareConfigurations(GetConfigurationIndexFromName(config1, fConfigurations), GetConfigurationIndexFromName(config2, fConfigurations)); }
278 std::string toString(
const int index = -1)
const;
279 std::ostream &
Print(std::ostream &in,
const int index = -1)
const;
280 std::ostream &
Print(std::ostream &in,
const std::string & configurationName)
const;
289 inline bool DoesFileExist(
const std::string & filename)
const;
290 void SetupReadingConfigurationFilePath(std::string & filename,
const std::string & fileIdentifier)
const;
291 void WriteConfigurationToFilePath(
const std::string & localFilename, std::string filename)
const;
292 #if !(defined(__CINT__) || defined(__MAKECINT__)) 294 void PrintConfiguration(std::ostream & stream,
const std::pair<std::string, YAML::Node> & configPair)
const;
298 unsigned int GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const;
300 bool IsSharedValue(std::string & value)
const;
301 #if !(defined(__CINT__) || defined(__MAKECINT__)) 303 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int)
const -> decltype(tempMessage << property,
void());
305 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int)
const -> decltype(property.begin(),
property.end(), tempMessage << std::declval<T>(),
void());
307 void PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const;
309 void PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const;
312 bool GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const;
314 bool GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const;
317 void WriteValue(YAML::Node & node, std::string propertyName,
T & proeprty);
332 #if !(defined(__CINT__) || defined(__MAKECINT__)) 344 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int) const -> decltype(tempMessage << property,
void())
346 tempMessage <<
" with value \"" <<
property <<
"\"";
358 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int) const -> decltype(property.begin(), property.end(), tempMessage <<
std::declval<
T>(),
void())
360 tempMessage <<
" with value(s):";
361 for (
auto it = property.begin(); it !=
property.end(); it++) {
362 tempMessage <<
"\n\t- " << *it;
378 void AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const 381 tempMessage <<
" with a value that cannot be trivially printed";
396 void AliYAMLConfiguration::PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const 399 AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(tempMessage, property, 0);
412 bool AliYAMLConfiguration::GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const 414 if (node[propertyName])
416 property = node[propertyName].as<
T>();
434 bool AliYAMLConfiguration::GetProperty(std::vector <std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const 436 propertyPath.push_back(propertyName);
437 return GetProperty(propertyPath, property, requiredProperty);
451 bool AliYAMLConfiguration::GetProperty(
const std::vector <std::string> propertyPath,
T & property,
const bool requiredProperty)
const 454 std::string requestedName =
"";
455 for (
auto & str : propertyPath)
457 if (requestedName.length() > 0) {
458 requestedName +=
":" + str;
466 return GetProperty(requestedName, property, requiredProperty);
481 bool AliYAMLConfiguration::GetProperty(std::string propertyName,
T & property,
const bool requiredProperty)
const 484 std::size_t prefixStringLocation = propertyName.find(fPrefixString);
485 if (prefixStringLocation != std::string::npos)
488 propertyName.erase(prefixStringLocation, prefixStringLocation + fPrefixString.length());
491 bool setProperty =
false;
493 for (
auto configPair :
reverse(fConfigurations))
495 if (setProperty ==
true) {
496 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Property \"" << propertyName <<
"\" found!\n";
502 if (configPair.second.IsNull() !=
true)
504 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Looking for parameter \"" << propertyName <<
"\" in \"" << configPair.first <<
"\" configuration\n";
506 YAML::Node sharedParameters = configPair.second[
"sharedParameters"];
507 setProperty = GetProperty(configPair.second, sharedParameters, configPair.first, propertyName, property);
511 if (setProperty !=
true && requiredProperty ==
true)
513 std::stringstream message;
514 message <<
"Failed to retrieve required property \"" 515 << propertyName <<
"\" from available configurations!" << std::endl;
516 AliFatalGeneral(
"AliYAMLConfiguration", message.str().c_str());
546 bool AliYAMLConfiguration::GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const 549 std::stringstream tempMessage;
551 bool returnValue =
false;
553 const std::string specializationDelimiter =
"_";
554 size_t delimiterPosition = 0;
555 std::string tempPropertyName = propertyName;
557 if ((delimiterPosition = tempPropertyName.find(fDelimiter)) != std::string::npos)
559 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
560 tempPropertyName.erase(0, delimiterPosition + fDelimiter.length());
563 if (node[nodeName].IsDefined() ==
true)
566 YAML::Node tempNode = node[nodeName];
567 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper with node \"" << nodeName <<
"\".\n";
568 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
574 if (node[nodeName].IsDefined() ==
false || returnValue ==
false)
577 if ((delimiterPosition = nodeName.find(specializationDelimiter)) != std::string::npos)
579 std::string specializationNodeName = nodeName.substr(0, delimiterPosition);
580 YAML::Node tempNode = node[specializationNodeName];
581 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper through dropping the specializtion and using node \"" << specializationNodeName <<
"\".\n";
582 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
592 if (node[propertyName])
595 bool isShared =
false;
598 std::string sharedValueName =
"";
600 if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
603 sharedValueName = node[propertyName].as<std::string>();
605 isShared = IsSharedValue(sharedValueName);
610 tempMessage <<
"property \"" 612 <<
"\" using " << ( isShared ?
"\"sharedParameters:" + sharedValueName +
"\" in " :
"" )
613 <<
"values from the " << configurationName <<
" configuration";
615 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Retrieveing " << tempMessage.str() <<
"\n";
618 bool retrievalResult =
false;
619 if (isShared ==
true) {
621 retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
624 retrievalResult = GetPropertyFromNode(node, propertyName, property);
628 if (retrievalResult ==
true) {
630 AliYAMLConfiguration::PrintRetrievedPropertyValue(tempMessage, property);
631 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Succeeded in retrieveing " << tempMessage.str() <<
"\n";
654 bool AliYAMLConfiguration::WriteProperty(std::string propertyName,
T & property, std::string configurationName)
656 unsigned int configurationIndex = 0;
657 if (configurationName !=
"")
659 configurationIndex = GetConfigurationIndexFromName(configurationName, fConfigurations);
662 if (fConfigurations.size() == 0) {
663 AliErrorStream() <<
"No configurations available! Property will not be written!\n";
667 std::pair<std::string, YAML::Node> & configPair = fConfigurations.at(configurationIndex);
669 WriteValue(configPair.second, propertyName, property);
670 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Final Node:\n" << configPair.second <<
"\n";
683 void AliYAMLConfiguration::WriteValue(YAML::Node & node, std::string propertyName,
T & property)
687 const std::string delimiter =
":";
688 size_t delimiterPosition = 0;
689 std::string tempPropertyName = propertyName;
691 if ((delimiterPosition = tempPropertyName.find(delimiter)) != std::string::npos)
693 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
694 tempPropertyName.erase(0, delimiterPosition + delimiter.length());
695 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"nodeName: " << nodeName <<
"\n";
696 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node Before:\n" << node <<
"\n";
697 if (node[nodeName].IsDefined()) {
698 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Using existing node\n";
699 YAML::Node tempNode = node[nodeName];
700 WriteValue(tempNode, tempPropertyName, property);
703 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Creating new node\n";
705 node[nodeName] = tempNode;
706 WriteValue(tempNode, tempPropertyName, property);
708 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node After:\n" << node <<
"\n";
712 node[propertyName] = property;
728 unsigned int AliYAMLConfiguration::GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const 731 for (
const auto & configPair : configurations)
733 if (configPair.first == name) {
736 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.
Range< It > reverse(ORange &&originalRange)
static Node encode(const TString &str)
static bool decode(const Node &node, TString &str)