MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
Macros | Functions
YamlHelper.h File Reference

Utility functions for handling YAML nodes. More...

#include <iostream>
#include <fstream>
#include <string>
#include <cxxabi.h>
#include <regex>
#include "Manager/MaCh3Exception.h"
#include "Manager/Core.h"
#include "TMacro.h"
#include "TList.h"
#include "TObjString.h"
#include "yaml-cpp/yaml.h"
Include dependency graph for YamlHelper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define M3OpenConfig(filename)   LoadYamlConfig((filename), __FILE__, __LINE__)
 Macro to simplify calling LoadYaml with file and line info.
 
#define GetBounds(filename)   ParseBounds((filename), __FILE__, __LINE__)
 
#define Get2DBounds(filename)   Parse2DBounds((filename), __FILE__, __LINE__)
 

Functions

template<typename T >
bool CheckNodeExistsHelper (const T &node)
 Use this like this CheckNodeExists(config, "LikelihoodOptions", "TestStatistic"); KS: Base case for recursion.
 
template<typename T , typename... Args>
bool CheckNodeExistsHelper (const T &node, const std::string &key, Args... args)
 KS: Recursive function to traverse YAML nodes.
 
template<typename... Args>
bool CheckNodeExists (const YAML::Node &node, Args... args)
 KS: Wrapper function to call the recursive helper.
 
template<typename T >
FindFromManagerHelper (const YAML::Node &node)
 
template<typename T , typename... Args>
FindFromManagerHelper (const YAML::Node &node, const std::string &key, Args... args)
 Recursive function to traverse YAML nodes.
 
template<typename T , typename... Args>
FindFromManager (const YAML::Node &node, Args... args)
 Wrapper function to call the recursive helper.
 
YAML::Node STRINGtoYAML (const std::string &yaml_string)
 Function to convert a YAML string to a YAML node.
 
std::string YAMLtoSTRING (const YAML::Node &node)
 KS: Convert a YAML node to a string representation.
 
std::string TMacroToString (const TMacro &macro)
 KS: Convert a ROOT TMacro object to a string representation.
 
YAML::Node TMacroToYAML (const TMacro &macro)
 KS: Convert a ROOT TMacro object to a YAML node.
 
TMacro YAMLtoTMacro (const YAML::Node &yaml_node, const std::string &name)
 Convert a YAML node to a ROOT TMacro object.
 
bool compareYAMLNodes (const YAML::Node &node1, const YAML::Node &node2)
 Compare if yaml nodes are identical.
 
template<typename TValue >
void OverrideConfig (YAML::Node node, std::string const &key, TValue val)
 Overrides the configuration settings based on provided arguments.
 
template<typename... Args>
void OverrideConfig (YAML::Node node, std::string const &key, Args... args)
 Overrides the configuration settings based on provided arguments.
 
std::string DemangleTypeName (const std::string &mangledName)
 Function to demangle type names.
 
template<typename Type >
Type Get (const YAML::Node &node, const std::string File, const int Line)
 Get content of config file.
 
template<typename Type >
Type GetFromManager (const YAML::Node &node, Type defval, const std::string File="", const int Line=1)
 Get content of config file if node is not found take default value specified.
 
YAML::Node LoadYamlConfig (const std::string &filename, const std::string &File, const int Line)
 Open YAML file.
 
const YAML::Node & Cnode (const YAML::Node &n)
 KS: Convenience wrapper to return a YAML node as-is.
 
YAML::Node MergeNodes (YAML::Node a, YAML::Node b)
 KS: Recursively merges two YAML nodes.
 
std::vector< double > ParseBounds (const YAML::Node &node, const std::string &File, const int Line)
 KS: Get bounds from YAML for example for selection cuts.
 
std::vector< std::vector< double > > Parse2DBounds (const YAML::Node &node, const std::string &File, const int Line)
 KS: Get 2D bounds from YAML for example for selection cuts.
 

Detailed Description

Utility functions for handling YAML nodes.

Author
Kamil Skwarczynski
Luke Pickering

Definition in file YamlHelper.h.

Macro Definition Documentation

◆ Get2DBounds

#define Get2DBounds (   filename)    Parse2DBounds((filename), __FILE__, __LINE__)

Definition at line 562 of file YamlHelper.h.

◆ GetBounds

#define GetBounds (   filename)    ParseBounds((filename), __FILE__, __LINE__)

Definition at line 561 of file YamlHelper.h.

◆ M3OpenConfig

#define M3OpenConfig (   filename)    LoadYamlConfig((filename), __FILE__, __LINE__)

Macro to simplify calling LoadYaml with file and line info.

Definition at line 560 of file YamlHelper.h.

Function Documentation

◆ CheckNodeExists()

template<typename... Args>
bool CheckNodeExists ( const YAML::Node &  node,
Args...  args 
)

KS: Wrapper function to call the recursive helper.

Definition at line 54 of file YamlHelper.h.

54 {
55// **********************
56 return CheckNodeExistsHelper(node, args...);
57}
bool CheckNodeExistsHelper(const T &node)
Use this like this CheckNodeExists(config, "LikelihoodOptions", "TestStatistic"); KS: Base case for r...
Definition: YamlHelper.h:33

◆ CheckNodeExistsHelper() [1/2]

template<typename T >
bool CheckNodeExistsHelper ( const T &  node)

Use this like this CheckNodeExists(config, "LikelihoodOptions", "TestStatistic"); KS: Base case for recursion.

Definition at line 33 of file YamlHelper.h.

33 {
34// **********************
35 (void)node;
36 return true; // Node exists, return true
37}

◆ CheckNodeExistsHelper() [2/2]

template<typename T , typename... Args>
bool CheckNodeExistsHelper ( const T &  node,
const std::string &  key,
Args...  args 
)

KS: Recursive function to traverse YAML nodes.

Definition at line 42 of file YamlHelper.h.

42 {\
43// **********************
44 if (!node[key]) {
45 //std::cerr << "Node " << key << " doesn't exist." << std::endl;
46 return false;
47 }
48 return CheckNodeExistsHelper(node[key], args...);
49}

◆ Cnode()

const YAML::Node & Cnode ( const YAML::Node &  n)
inline

KS: Convenience wrapper to return a YAML node as-is.

Parameters
nInput YAML node.
Returns
Reference to the same YAML node.

Definition at line 389 of file YamlHelper.h.

389 {
390// **********************
391 return n;
392}

◆ compareYAMLNodes()

bool compareYAMLNodes ( const YAML::Node &  node1,
const YAML::Node &  node2 
)
inline

Compare if yaml nodes are identical.

Parameters
node1The first YAML node to compare.
node2The second YAML node to compare.
Returns
true If the two nodes are equivalent in type and content.
false If the two nodes differ in structure or content.

Definition at line 179 of file YamlHelper.h.

179 {
180// **********************
181 // Check if the types of the nodes match
182 if (node1.Type() != node2.Type()) {
183 return false;
184 }
185
186 // Compare scalar types (like strings, numbers)
187 if (node1.IsScalar() && node2.IsScalar()) {
188 return node1.as<std::string>() == node2.as<std::string>();
189 }
190
191 // Compare sequences (like YAML lists)
192 if (node1.IsSequence() && node2.IsSequence()) {
193 if (node1.size() != node2.size()) {
194 return false;
195 }
196 for (std::size_t i = 0; i < node1.size(); ++i) {
197 if (!compareYAMLNodes(node1[i], node2[i])) {
198 return false;
199 }
200 }
201 return true;
202 }
203
204 // Compare maps (like YAML dictionaries)
205 if (node1.IsMap() && node2.IsMap()) {
206 if (node1.size() != node2.size()) {
207 return false;
208 }
209 for (auto it1 = node1.begin(); it1 != node1.end(); ++it1) {
210 auto key = it1->first.as<std::string>();
211 if (!node2[key] || !compareYAMLNodes(it1->second, node2[key])) {
212 return false;
213 }
214 }
215 return true;
216 }
217
218 // Default case: if it's neither scalar, sequence, nor map, consider it unequal
219 return false;
220}
bool compareYAMLNodes(const YAML::Node &node1, const YAML::Node &node2)
Compare if yaml nodes are identical.
Definition: YamlHelper.h:179

◆ DemangleTypeName()

std::string DemangleTypeName ( const std::string &  mangledName)
inline

Function to demangle type names.

Definition at line 252 of file YamlHelper.h.

252 {
253// **********************
254 int status = 0;
255 char* demangledName = abi::__cxa_demangle(mangledName.c_str(), nullptr, nullptr, &status);
256 std::string result = (status == 0) ? demangledName : mangledName;
257 free(demangledName);
258 return result;
259}

◆ FindFromManager()

template<typename T , typename... Args>
T FindFromManager ( const YAML::Node &  node,
Args...  args 
)

Wrapper function to call the recursive helper.

Definition at line 83 of file YamlHelper.h.

83 {
84// **********************
85 return FindFromManagerHelper<T>(node, args...);
86}

◆ FindFromManagerHelper() [1/2]

template<typename T >
T FindFromManagerHelper ( const YAML::Node &  node)

Use this like this FindFromManager<std::string>(config, "LikelihoodOptions", "TestStatistic"); Base case for recursion

Definition at line 63 of file YamlHelper.h.

63 {
64// **********************
65 return node.as<T>(); // Convert YAML node to the specified type
66}

◆ FindFromManagerHelper() [2/2]

template<typename T , typename... Args>
T FindFromManagerHelper ( const YAML::Node &  node,
const std::string &  key,
Args...  args 
)

Recursive function to traverse YAML nodes.

Definition at line 71 of file YamlHelper.h.

71 {
72 // **********************
73 if (!node[key]) {
74 MACH3LOG_ERROR("Node {} doesn't exist.", key);
75 throw;
76 return T();
77 }
78 return FindFromManagerHelper<T>(node[key], args...); // Recursive call
79}
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:25

◆ Get()

template<typename Type >
Type Get ( const YAML::Node &  node,
const std::string  File,
const int  Line 
)

Get content of config file.

Parameters
nodeYaml node

Definition at line 265 of file YamlHelper.h.

265 {
266// **********************
267 try {
268 // Attempt to convert the node to the expected type
269 return node.as<Type>();
270 } catch (const YAML::BadConversion& e) {
271 const std::string nodeAsString = YAMLtoSTRING(node);
272 MACH3LOG_ERROR("YAML type mismatch: {}", e.what());
273 MACH3LOG_ERROR("While trying to access variable {}", nodeAsString);
274 throw MaCh3Exception(File , Line );
275 } catch (const YAML::InvalidNode& e) {
276 std::string key = "<unknown>";
277 const std::string msg = e.what();
278 const std::string search = "first invalid key: \"";
279 auto start = msg.find(search);
280 if (start != std::string::npos) {
281 start += search.length();
282 auto end = msg.find("\"", start);
283 if (end != std::string::npos) {
284 key = msg.substr(start, end - start);
285 }
286 }
287 MACH3LOG_ERROR("Invalid YAML node: {}", e.what());
288 MACH3LOG_ERROR("While trying to access key: {}", key);
289 throw MaCh3Exception(File , Line );
290 }
291}
std::string YAMLtoSTRING(const YAML::Node &node)
KS: Convert a YAML node to a string representation.
Definition: YamlHelper.h:106
Custom exception class for MaCh3 errors.

◆ GetFromManager()

template<typename Type >
Type GetFromManager ( const YAML::Node &  node,
Type  defval,
const std::string  File = "",
const int  Line = 1 
)

Get content of config file if node is not found take default value specified.

Parameters
nodeYaml node
defvalDefault value which will be used in case node doesn't exist

Definition at line 298 of file YamlHelper.h.

298 {
299// **********************
300 if (!node) {
301 return defval;
302 }
303 try {
304 // Attempt to convert the node to the expected type
305 return node.as<Type>();
306 } catch (const YAML::BadConversion& e) {
307 const std::string nodeAsString = YAMLtoSTRING(node);
308 MACH3LOG_ERROR("YAML type mismatch: {}", e.what());
309 MACH3LOG_ERROR("While trying to access variable {}", nodeAsString);
310 //const std::string expectedType = DemangleTypeName(typeid(Type).name());
311 //MACH3LOG_ERROR("Expected argument is {}", expectedType);
312 if(File == "") {
313 throw MaCh3Exception(__FILE__ , __LINE__);
314 } else {
315 throw MaCh3Exception(File , Line );
316 }
317 }
318}

◆ LoadYamlConfig()

YAML::Node LoadYamlConfig ( const std::string &  filename,
const std::string &  File,
const int  Line 
)
inline

Open YAML file.

Parameters
filenamename of filename to open
Filename of file where function is called
Linenumber where function is called

Definition at line 325 of file YamlHelper.h.

325 {
326// **********************
327 // KS: YAML can be dumb and not throw error if you pass toml for example...
328 if (!(filename.length() >= 5 && filename.compare(filename.length() - 5, 5, ".yaml") == 0) &&
329 !(filename.length() >= 4 && filename.compare(filename.length() - 4, 4, ".yml") == 0)) {
330 MACH3LOG_ERROR("Invalid file extension: {}\n", filename);
331 throw MaCh3Exception(File, Line);
332 }
333
334 try {
335 YAML::Node yamlNode = YAML::LoadFile(filename);
336
337 // Convert the YAML file to string
338 std::ifstream fileStream(filename);
339 std::stringstream buffer;
340 buffer << fileStream.rdbuf();
341 std::string fileContent = buffer.str();
342
343 // Use regex to find any line starting with `-` but not followed by space, digit, or dot
344 // This excludes valid numeric negative values like -1760.0
345 std::regex linePattern(R"(^\s*-(?![\d\s\.]).*)");
346
347 std::istringstream contentStream(fileContent);
348 std::string lineconfig;
349 int lineNumber = 1;
350
351 // KS: Instead of boolean, track flow-style list nesting depth
352 int flowListDepth = 0;
353
354 while (std::getline(contentStream, lineconfig)) {
355 // Ignore YAML document separator
356 if (lineconfig.find("---") != std::string::npos) {
357 lineNumber++;
358 continue;
359 }
360
361 // Update flow list depth (increment for each '[' and decrement for each ']')
362 for (char c : lineconfig) {
363 if (c == '[') flowListDepth++;
364 else if (c == ']') flowListDepth = std::max(0, flowListDepth - 1);
365 }
366
367 // Check lines only outside flow-style lists
368 if (flowListDepth == 0 && std::regex_match(lineconfig, linePattern)) {
369 MACH3LOG_ERROR("Warning: Missing space or tab after '-' at line {}: {}\n", lineNumber, lineconfig);
370 throw MaCh3Exception(File, Line);
371 }
372
373 lineNumber++;
374 }
375
376 return yamlNode;
377
378 } catch (const std::exception& e) {
379 MACH3LOG_ERROR("{}\n", e.what());
380 MACH3LOG_ERROR("Can't open file {}\n", filename);
381 throw MaCh3Exception(File, Line);
382 }
383}

◆ MergeNodes()

YAML::Node MergeNodes ( YAML::Node  a,
YAML::Node  b 
)
inline

KS: Recursively merges two YAML nodes.

If both nodes are maps, their keys are merged. Scalar or null values in b will override those in a. If b is null, a is returned unchanged.

Note
Based on https://stackoverflow.com/a/41337824
Parameters
aThe base YAML node.
bThe YAML node to merge into a.
Returns
A new YAML node representing the merged result.

Definition at line 405 of file YamlHelper.h.

405 {
406// **********************
407 if (!b.IsMap()) {
408 // If b is not a map, merge result is b, unless b is null
409 return b.IsNull() ? a : b;
410 }
411 if (!a.IsMap()) {
412 // If a is not a map, merge result is b
413 return b;
414 }
415 if (!b.size()) {
416 // If a is a map, and b is an empty map, return a
417 return a;
418 }
419 // Create a new map 'c' with the same mappings as a, merged with b
420 auto c = YAML::Node(YAML::NodeType::Map);
421 for (auto n : a) {
422 if (n.first.IsScalar()) {
423 const std::string & key = n.first.Scalar();
424 auto t = (Cnode(b)[key]);
425 if (t) {
426 c[n.first] = MergeNodes(n.second, t);
427 continue;
428 }
429 }
430 c[n.first] = n.second;
431 }
432 // Add the mappings from 'b' not already in 'c'
433 for (auto n : b) {
434 if (!n.first.IsScalar() || !Cnode(c)[n.first.Scalar()]) {
435 c[n.first] = n.second;
436 }
437 }
438 return c;
439}
const YAML::Node & Cnode(const YAML::Node &n)
KS: Convenience wrapper to return a YAML node as-is.
Definition: YamlHelper.h:389
YAML::Node MergeNodes(YAML::Node a, YAML::Node b)
KS: Recursively merges two YAML nodes.
Definition: YamlHelper.h:405

◆ OverrideConfig() [1/2]

template<typename... Args>
void OverrideConfig ( YAML::Node  node,
std::string const &  key,
Args...  args 
)

Overrides the configuration settings based on provided arguments.

This function allows you to set configuration options in a nested YAML node.

Parameters
nodeYAML node that will be modified
argsThe arguments to override the configuration. The last argument will be used as the value
Note
Example usage:
OverrideConfig(config, "General", "OutputFile", "Wooimbouttamakeanameformyselfere.root");
OverrideConfig(config, "General", "MyDouble", 5.3);
std::string config
Definition: ProcessMCMC.cpp:30
void OverrideConfig(YAML::Node node, std::string const &key, TValue val)
Overrides the configuration settings based on provided arguments.
Definition: YamlHelper.h:226

Definition at line 245 of file YamlHelper.h.

245 {
246// **********************
247 OverrideConfig(node[key], args...);
248}

◆ OverrideConfig() [2/2]

template<typename TValue >
void OverrideConfig ( YAML::Node  node,
std::string const &  key,
TValue  val 
)

Overrides the configuration settings based on provided arguments.

Parameters
nodeYAML node that will be modified

Definition at line 226 of file YamlHelper.h.

226 {
227// **********************
228 node[key] = val;
229}

◆ Parse2DBounds()

std::vector< std::vector< double > > Parse2DBounds ( const YAML::Node &  node,
const std::string &  File,
const int  Line 
)
inline

KS: Get 2D bounds from YAML for example for selection cuts.

Parses a YAML node representing 2D selection bounds. Each element in the node must be a sequence of 1 or 2 scalar values. If a single value is given, it is interpreted as the center of a bin and expanded to [value - 0.5, value + 0.5]. If two values are given, they are used as-is. If an element is empty or null, default bounds are used.

This function is useful for interpreting kinematic or classification bounds specified in YAML configuration files.

Example YAML input:

bounds2D:
- [1]
- [0.2, 0.8]
- ["", "2.0"]

Resulting vector:

[ [0.5, 1.5],
[0.2, 0.8],
static constexpr const double KinematicLowBound
When parameter has no bound this serves as it. Lowest possible value the system.
Definition: Core.h:65
Parameters
nodeThe YAML node containing a sequence of bounds (each being a 1- or 2-element sequence).
FileThe name of the file calling this function (for error reporting).
LineThe line number in the file calling this function (for error reporting).
Returns
std::vector<std::vector<double>> A list of bound pairs for each selection region.

Definition at line 538 of file YamlHelper.h.

538 {
539// **********************
540 std::vector<std::vector<double>> bounds;
541 if (!node || !node.IsSequence()) {
542 MACH3LOG_ERROR("Expected a sequence node for 2D bounds");
543 throw MaCh3Exception(File, Line);
544 }
545
546 // Case 1: Single pair like [0.25, 0.5]
547 if (node.size() == 2 && node[0].IsScalar()) {
548 bounds.push_back(ParseBounds(node, File, Line));
549 } else { // Case 2: List of pairs like [[0.25, 0.5], [0.75, 1.0]]
550 for (std::size_t i = 0; i < node.size(); ++i) {
551 const YAML::Node& subnode = node[i];
552 bounds.push_back(ParseBounds(subnode, File, Line));
553 }
554 }
555
556 return bounds;
557}
std::vector< double > ParseBounds(const YAML::Node &node, const std::string &File, const int Line)
KS: Get bounds from YAML for example for selection cuts.
Definition: YamlHelper.h:454

◆ ParseBounds()

std::vector< double > ParseBounds ( const YAML::Node &  node,
const std::string &  File,
const int  Line 
)
inline

KS: Get bounds from YAML for example for selection cuts.

This function expects a YAML node of exactly two elements. If any of the elements is an empty string or null, it is replaced with a default kinematic bound. If the element is a non-empty string and cannot be converted to a double, an exception is thrown.

Parameters
nodeThe YAML node containing the bounds.
FileThe name of the file calling this function (for error context).
LineThe line number in the file calling this function.
Returns
std::vector<double> A vector of size 2 with the parsed bounds.

Definition at line 454 of file YamlHelper.h.

454 {
455// **********************
456 std::vector<double> bounds;
457
458 if (!node || !node.IsSequence() || (node.size() != 1 && node.size() != 2)) {
459 MACH3LOG_ERROR("Bounds must be a sequence of 1 or 2 elements, got size {}", static_cast<int>(node.size()));
460 throw MaCh3Exception(File, Line);
461 }
462
463 if (node.size() == 1) {
464 const YAML::Node& val = node[0];
465
466 if (!val || val.IsNull() || (val.IsScalar() && val.Scalar().empty())) {
467 MACH3LOG_ERROR("Single-element bounds must contain a valid numeric value.");
468 throw MaCh3Exception(File, Line);
469 } else if (val.IsScalar()) {
470 try {
471 const double center = val.as<double>();
472 if (std::floor(center) != center) {
473 MACH3LOG_ERROR("Invalid center value in Bounds[0]: '{}'. Expected an integer (e.g., 0 or 1).", static_cast<std::string>(val.Scalar()));
474 throw MaCh3Exception(File, Line);
475 }
476 bounds = {center - 0.5, center + 0.5};
477 } catch (const YAML::BadConversion& e) {
478 MACH3LOG_ERROR("Invalid value in Bounds[0]: '{}'. Expected a number.", static_cast<std::string>(val.Scalar()));
479 throw MaCh3Exception(File, Line);
480 }
481 } else {
482 MACH3LOG_ERROR("Invalid type in Bounds[0]");
483 throw MaCh3Exception(File, Line);
484 }
485 } else {
486 for (std::size_t i = 0; i < 2; ++i) {
487 const YAML::Node& val = node[i];
488
489 if (!val || val.IsNull() || (val.IsScalar() && val.Scalar().empty())) {
490 bounds.push_back(i == 0 ? M3::KinematicLowBound : M3::KinematicUpBound);
491 } else if (val.IsScalar()) {
492 try {
493 bounds.push_back(val.as<double>());
494 } catch (const YAML::BadConversion& e) {
495 MACH3LOG_ERROR("Invalid value in Bounds[{}]: '{}'. Expected a number or empty string.",
496 static_cast<int>(i), static_cast<std::string>(val.Scalar()));
497 throw MaCh3Exception(File, Line);
498 }
499 } else {
500 MACH3LOG_ERROR("Invalid type in Bounds[{}]", static_cast<int>(i));
501 throw MaCh3Exception(File, Line);
502 }
503 }
504 }
505 return bounds;
506}
static constexpr const double KinematicUpBound
When parameter has no bound this serves as it. Highest possible value the system.
Definition: Core.h:67

◆ STRINGtoYAML()

YAML::Node STRINGtoYAML ( const std::string &  yaml_string)
inline

Function to convert a YAML string to a YAML node.

Parameters
yaml_stringString which will be converted to yaml node

Definition at line 91 of file YamlHelper.h.

91 {
92// **********************
93 try {
94 return YAML::Load(yaml_string);
95 } catch (const YAML::ParserException& e) {
96 MACH3LOG_ERROR("Error parsing YAML string: {}", e.what());
97 return YAML::Node();
98 }
99}

◆ TMacroToString()

std::string TMacroToString ( const TMacro &  macro)
inline

KS: Convert a ROOT TMacro object to a string representation.

Parameters
macroThe ROOT TMacro object to convert.
Returns
std::string The string representation of the TMacro object.

Definition at line 117 of file YamlHelper.h.

117 {
118// **********************
119 std::stringstream ss;
120
121 // Retrieve lines from TMacro
122 TList* linesList = macro.GetListOfLines();
123 if (!linesList) {
124 MACH3LOG_ERROR("Failed to retrieve lines from TMacro.");
125 return "";
126 }
127
128 TIter nextLine(linesList);
129 TObject *obj = nullptr;
130 while ((obj = nextLine())) {
131 TObjString* line = dynamic_cast<TObjString*>(obj);
132 if (!line) {
133 MACH3LOG_ERROR("Failed to cast object to TObjString.");
134 continue;
135 }
136 ss << line->GetString() << std::endl;
137 }
138
139 return ss.str();
140}

◆ TMacroToYAML()

YAML::Node TMacroToYAML ( const TMacro &  macro)
inline

KS: Convert a ROOT TMacro object to a YAML node.

Parameters
macroThe ROOT TMacro object to convert.
Returns
YAML::Node The YAML node representing the TMacro object.

Definition at line 146 of file YamlHelper.h.

146 {
147// **********************
148 std::string yaml_string = TMacroToString(macro);
149
150 // Convert the YAML string to a YAML node
151 YAML::Node yaml_node = STRINGtoYAML(yaml_string);
152
153 return yaml_node;
154}
YAML::Node STRINGtoYAML(const std::string &yaml_string)
Function to convert a YAML string to a YAML node.
Definition: YamlHelper.h:91
std::string TMacroToString(const TMacro &macro)
KS: Convert a ROOT TMacro object to a string representation.
Definition: YamlHelper.h:117

◆ YAMLtoSTRING()

std::string YAMLtoSTRING ( const YAML::Node &  node)
inline

KS: Convert a YAML node to a string representation.

Parameters
nodeThe YAML node to convert to a string.
Returns
std::string The string representation of the YAML node.
Warning
YAML is weird and drops " " for string so output may not be identical to yaml you provided

Definition at line 106 of file YamlHelper.h.

106 {
107// **********************
108 YAML::Emitter emitter;
109 emitter << node;
110 return emitter.c_str();
111}

◆ YAMLtoTMacro()

TMacro YAMLtoTMacro ( const YAML::Node &  yaml_node,
const std::string &  name 
)
inline

Convert a YAML node to a ROOT TMacro object.

Parameters
yaml_nodeThe YAML node to convert to a TMacro.
nameName of TMacro that will be saved
Returns
TMacro The TMacro object constructed from the YAML node.

Definition at line 161 of file YamlHelper.h.

161 {
162// **********************
163 // Convert the YAML node to a string representation
164 std::string macro_string = YAMLtoSTRING(yaml_node);
165
166 // Create a TMacro object with the collected lines
167 TMacro macro(name.c_str(), name.c_str());
168 macro.AddLine(macro_string.c_str());
169
170 return macro;
171}