1 #ifndef ALIYAMLCONFIGURATION_H 2 #define ALIYAMLCONFIGURATION_H 5 #if !(defined(__CINT__) || defined(__MAKECINT__)) 6 #include <yaml-cpp/yaml.h> 123 #if !(defined(__CINT__) || defined(__MAKECINT__)) 128 return Node(str.Data());
131 if (!node.IsScalar()) {
151 template <
typename It>
159 It
end()
const {
return e; }
162 template <
typename ORange,
typename OIt = decltype(std::begin(std::declval<ORange>())),
163 typename It = std::reverse_iterator<OIt>>
166 return Range<It>(It(std::end(originalRange)), It(std::begin(originalRange)));
171 namespace PWG {
namespace Tools {
class AliYAMLConfiguration; } }
179 AliYAMLConfiguration(
const std::string prefixString =
"AliEmcalCorrection",
const std::string delimiterCharacter =
":");
192 int AddEmptyConfiguration(
const std::string & configurationName);
194 int AddConfiguration(std::string configurationFilename, std::string configurationName =
"");
195 #if !(defined(__CINT__) || defined(__MAKECINT__)) 196 int AddConfiguration(
const YAML::Node node, std::string configurationName =
"");
202 bool DoesConfigurationExist(
const int i)
const {
return i >= 0 &&
static_cast<const unsigned int>(i) < fConfigurations.size(); }
204 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i)
const {
return fConfigurations.at(i); }
205 const std::pair<std::string, YAML::Node> &
GetConfiguration(
const std::string & name)
const {
return GetConfiguration(GetConfigurationIndexFromName(name, fConfigurations)); }
206 std::pair<std::string, YAML::Node> &
GetConfiguration(
const int i) {
return fConfigurations.at(i); }
220 bool RemoveConfiguration(
const unsigned int i);
229 bool GetProperty(std::vector<std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const;
231 bool GetProperty(
const std::vector<std::string> propertyPath,
T & property,
const bool requiredProperty)
const;
234 bool GetProperty(std::string propertyName,
T & property,
const bool requiredProperty =
true)
const;
242 bool WriteProperty(std::string propertyName,
T & property, std::string configurationName =
"");
249 bool WriteConfiguration(
const std::string &
filename,
const unsigned int i)
const;
250 bool WriteConfiguration(
const std::string & filename,
const std::string & configurationName)
const;
256 bool CompareConfigurations(
const int config1,
const int config2)
const;
258 bool CompareConfigurations(
const std::string & config1,
const std::string & config2)
const {
return CompareConfigurations(GetConfigurationIndexFromName(config1, fConfigurations), GetConfigurationIndexFromName(config2, fConfigurations)); }
265 std::string toString(
const int index = -1)
const;
266 std::ostream &
Print(std::ostream &in,
const int index = -1)
const;
267 std::ostream &
Print(std::ostream &in,
const std::string & configurationName)
const;
276 inline bool DoesFileExist(
const std::string & filename)
const;
277 void SetupReadingConfigurationFilePath(std::string & filename,
const std::string & fileIdentifier)
const;
278 void WriteConfigurationToFilePath(
const std::string & localFilename, std::string filename)
const;
279 #if !(defined(__CINT__) || defined(__MAKECINT__)) 281 void PrintConfiguration(std::ostream & stream,
const std::pair<std::string, YAML::Node> & configPair)
const;
285 unsigned int GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const;
287 bool IsSharedValue(std::string & value)
const;
288 #if !(defined(__CINT__) || defined(__MAKECINT__)) 290 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int)
const -> decltype(tempMessage << property,
void());
292 auto PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int)
const -> decltype(property.begin(),
property.end(), tempMessage << std::declval<T>(),
void());
294 void PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const;
296 void PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const;
299 bool GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const;
301 bool GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const;
304 void WriteValue(YAML::Node & node, std::string propertyName,
T & proeprty);
319 #if !(defined(__CINT__) || defined(__MAKECINT__)) 331 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
int) const -> decltype(tempMessage << property,
void())
333 tempMessage <<
" with value \"" <<
property <<
"\"";
345 auto AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const std::vector <T> & property,
int) const -> decltype(property.begin(), property.end(), tempMessage <<
std::declval<
T>(),
void())
347 tempMessage <<
" with value(s):";
348 for (
auto it = property.begin(); it !=
property.end(); it++) {
349 tempMessage <<
"\n\t- " << *it;
365 void AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(std::stringstream & tempMessage,
const T & property,
long)
const 368 tempMessage <<
" with a value that cannot be trivially printed";
383 void AliYAMLConfiguration::PrintRetrievedPropertyValue(std::stringstream & tempMessage,
const T & property)
const 386 AliYAMLConfiguration::PrintRetrievedPropertyValueImpl(tempMessage, property, 0);
399 bool AliYAMLConfiguration::GetPropertyFromNode(
const YAML::Node & node, std::string propertyName,
T & property)
const 401 if (node[propertyName])
403 property = node[propertyName].as<
T>();
421 bool AliYAMLConfiguration::GetProperty(std::vector <std::string> propertyPath,
const std::string & propertyName,
T & property,
const bool requiredProperty)
const 423 propertyPath.push_back(propertyName);
424 return GetProperty(propertyPath, property, requiredProperty);
438 bool AliYAMLConfiguration::GetProperty(
const std::vector <std::string> propertyPath,
T & property,
const bool requiredProperty)
const 441 std::string requestedName =
"";
442 for (
auto & str : propertyPath)
444 if (requestedName.length() > 0) {
445 requestedName +=
":" + str;
453 return GetProperty(requestedName, property, requiredProperty);
468 bool AliYAMLConfiguration::GetProperty(std::string propertyName,
T & property,
const bool requiredProperty)
const 471 std::size_t prefixStringLocation = propertyName.find(fPrefixString);
472 if (prefixStringLocation != std::string::npos)
475 propertyName.erase(prefixStringLocation, prefixStringLocation + fPrefixString.length());
478 bool setProperty =
false;
480 for (
auto configPair :
reverse(fConfigurations))
482 if (setProperty ==
true) {
483 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Property \"" << propertyName <<
"\" found!\n";
489 if (configPair.second.IsNull() !=
true)
491 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Looking for parameter \"" << propertyName <<
"\" in \"" << configPair.first <<
"\" configuration\n";
493 YAML::Node sharedParameters = configPair.second[
"sharedParameters"];
494 setProperty = GetProperty(configPair.second, sharedParameters, configPair.first, propertyName, property);
498 if (setProperty !=
true && requiredProperty ==
true)
500 std::stringstream message;
501 message <<
"Failed to retrieve required property \"" 502 << propertyName <<
"\" from available configurations!" << std::endl;
503 AliFatalGeneral(
"AliYAMLConfiguration", message.str().c_str());
533 bool AliYAMLConfiguration::GetProperty(YAML::Node & node, YAML::Node & sharedParametersNode,
const std::string & configurationName, std::string propertyName,
T & property)
const 536 std::stringstream tempMessage;
538 bool returnValue =
false;
540 const std::string specializationDelimiter =
"_";
541 size_t delimiterPosition = 0;
542 std::string tempPropertyName = propertyName;
544 if ((delimiterPosition = tempPropertyName.find(fDelimiter)) != std::string::npos)
546 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
547 tempPropertyName.erase(0, delimiterPosition + fDelimiter.length());
550 if (node[nodeName].IsDefined() ==
true)
553 YAML::Node tempNode = node[nodeName];
554 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper with node \"" << nodeName <<
"\".\n";
555 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
561 if (node[nodeName].IsDefined() ==
false || returnValue ==
false)
564 if ((delimiterPosition = nodeName.find(specializationDelimiter)) != std::string::npos)
566 std::string specializationNodeName = nodeName.substr(0, delimiterPosition);
567 YAML::Node tempNode = node[specializationNodeName];
568 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Attempting to retrieving property \"" << tempPropertyName <<
"\" by going a node deeper through dropping the specializtion and using node \"" << specializationNodeName <<
"\".\n";
569 returnValue = GetProperty(tempNode, sharedParametersNode, configurationName, tempPropertyName, property);
579 if (node[propertyName])
582 bool isShared =
false;
585 std::string sharedValueName =
"";
587 if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
590 sharedValueName = node[propertyName].as<std::string>();
592 isShared = IsSharedValue(sharedValueName);
597 tempMessage <<
"property \"" 599 <<
"\" using " << ( isShared ?
"\"sharedParameters:" + sharedValueName +
"\" in " :
"" )
600 <<
"values from the " << configurationName <<
" configuration";
602 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Retrieveing " << tempMessage.str() <<
"\n";
605 bool retrievalResult =
false;
606 if (isShared ==
true) {
608 retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
611 retrievalResult = GetPropertyFromNode(node, propertyName, property);
615 if (retrievalResult ==
true) {
617 AliYAMLConfiguration::PrintRetrievedPropertyValue(tempMessage, property);
618 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Succeeded in retrieveing " << tempMessage.str() <<
"\n";
641 bool AliYAMLConfiguration::WriteProperty(std::string propertyName,
T & property, std::string configurationName)
643 unsigned int configurationIndex = 0;
644 if (configurationName !=
"")
646 configurationIndex = GetConfigurationIndexFromName(configurationName, fConfigurations);
649 if (fConfigurations.size() == 0) {
650 AliErrorStream() <<
"No configurations available! Property will not be written!\n";
654 std::pair<std::string, YAML::Node> & configPair = fConfigurations.at(configurationIndex);
656 WriteValue(configPair.second, propertyName, property);
657 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Final Node:\n" << configPair.second <<
"\n";
670 void AliYAMLConfiguration::WriteValue(YAML::Node & node, std::string propertyName,
T & property)
674 const std::string delimiter =
":";
675 size_t delimiterPosition = 0;
676 std::string tempPropertyName = propertyName;
678 if ((delimiterPosition = tempPropertyName.find(delimiter)) != std::string::npos)
680 std::string nodeName = tempPropertyName.substr(0, delimiterPosition);
681 tempPropertyName.erase(0, delimiterPosition + delimiter.length());
682 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"nodeName: " << nodeName <<
"\n";
683 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node Before:\n" << node <<
"\n";
684 if (node[nodeName].IsDefined()) {
685 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Using existing node\n";
686 YAML::Node tempNode = node[nodeName];
687 WriteValue(tempNode, tempPropertyName, property);
690 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Creating new node\n";
692 node[nodeName] = tempNode;
693 WriteValue(tempNode, tempPropertyName, property);
695 AliDebugGeneralStream(
"AliYAMLConfiguration", 2) <<
"Node After:\n" << node <<
"\n";
699 node[propertyName] = property;
715 unsigned int AliYAMLConfiguration::GetConfigurationIndexFromName(
const std::string & name,
const std::vector<std::pair<std::string, T>> & configurations)
const 718 for (
const auto & configPair : configurations)
720 if (configPair.first == name) {
723 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)