ATLAS Offline Software
Loading...
Searching...
No Matches
MuctpiXMLHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <charconv>
7#include <string_view>
8#include <iostream>
9
10using namespace std;
11using boost::property_tree::ptree;
12
16
17void
19
20 // initialize attributes ptree
21 ptree tmp_ptree;
22 ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
23
24 // iterate through elements
25 for(const ptree::value_type & a: attributes) {
26 string attrName = a.first;
27 string attrVal = a.second.data();
28 std::cout << attrName << " : " << attrVal << std::endl;
29 }
30}
31
32bool
33MuctpiXMLHelper::hasAttribute(const ptree & tree, const string & attr) {
34
35 // initialize attributes ptree
36 ptree tmp_ptree;
37 ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
38
39 if(attributes.empty()) return false;
40
41 for(const ptree::value_type &a: attributes) {
42 string attrName = a.first;
43 if(attrName == attr) {
44 return true;
45 }
46 }
47 return false;
48}
49
50
51string
52MuctpiXMLHelper::readAttribute(const ptree & tree, const string & attr) {
53
54 // initialize attributes ptree
55 ptree tmp_ptree;
56 ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
57
58 // iterate through children
59 for(const ptree::value_type &a : attributes) {
60 if(a.first != attr) continue;
61 return a.second.data(); //a.second.data is the value!
62 }
63 return "";
64}
65
66
67std::string
68MuctpiXMLHelper::getAttribute(const ptree & tree, const string & attr) {
69 if( ! hasAttribute(tree, attr) ) {
70 TRG_MSG_WARNING("attribute " << attr << " does not exist");
71 return "";
72 }
73 return readAttribute(tree,attr);
74}
75
76std::string
77MuctpiXMLHelper::getAttribute(const ptree & tree, const string & attr, const std::string & defval) {
78 if( ! hasAttribute(tree, attr) )
79 return defval;
80 return readAttribute(tree,attr);
81}
82
83
84int
85MuctpiXMLHelper::getIntAttribute(const ptree & tree, const string & attr) {
86 if( ! hasAttribute(tree, attr) ) {
87 TRG_MSG_WARNING("attribute " << attr << " does not exist");
88 return 0;
89 }
90
91 int ret_value{0};
92 std::string attr_value = readAttribute(tree, attr);
93 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value);
94 if (ec != std::errc()) {
95 TRG_MSG_ERROR("attribute '" << attr << "' is not an int (it is '" << attr_value << "')");
96 }
97 return ret_value;
98}
99
100int
101MuctpiXMLHelper::getIntAttribute(const ptree & tree, const string & attr, int defval) {
102 if( ! hasAttribute(tree, attr) )
103 return defval;
104 int ret_value{0};
105 std::string attr_value = readAttribute(tree, attr);
106 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value);
107 if (ec != std::errc()) {
108 TRG_MSG_ERROR("attribute '" << attr << "' is not an int (it is '" << attr_value << "')");
109 }
110 return ret_value;
111}
112
113
114unsigned int
115MuctpiXMLHelper::getUIntAttribute(const ptree & tree, const string & attr) {
116 if( ! hasAttribute(tree, attr) ) {
117 TRG_MSG_WARNING("attribute " << attr << " does not exist");
118 return 0;
119 }
120 unsigned int ret_value{0};
121 std::string attr_value = readAttribute(tree, attr);
122 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value);
123 if (ec != std::errc()) {
124 TRG_MSG_ERROR("attribute '" << attr << "' is not an unsigned int (it is " << attr_value << ")");
125 }
126 return ret_value;
127}
128
129unsigned int
130MuctpiXMLHelper::getUIntAttribute(const ptree & tree, const string & attr, unsigned int & defval) {
131 if( ! hasAttribute(tree, attr) )
132 return defval;
133 unsigned int ret_value{0};
134 std::string attr_value = readAttribute(tree, attr);
135 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value);
136 if (ec != std::errc()) {
137 TRG_MSG_ERROR("attribute '" << attr << "' is not an unsigned int (it is " << attr_value << ")");
138 }
139 return ret_value;
140}
141
142
143float
144MuctpiXMLHelper::getFloatAttribute(const ptree & tree, const string & attr) {
145 if( ! hasAttribute(tree, attr) ) {
146 TRG_MSG_WARNING("attribute " << attr << " does not exist");
147 return 0;
148 }
149 float ret_value{0};
150 std::string attr_value = readAttribute(tree, attr);
151 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value, std::chars_format::general);
152 if (ec != std::errc()) {
153 TRG_MSG_ERROR("attribute '" << attr << "' is not a float (it is " << attr_value << ")");
155 }
156 return ret_value;
157}
158
159float
160MuctpiXMLHelper::getFloatAttribute(const ptree & tree, const string & attr, float & defval) {
161 if( ! hasAttribute(tree, attr) )
162 return defval;
163 float ret_value{0};
164 std::string attr_value = readAttribute(tree, attr);
165 auto [ptr, ec] = std::from_chars(attr_value.data(), attr_value.data() + attr_value.size(), ret_value, std::chars_format::general);
166 if (ec != std::errc()) {
167 TRG_MSG_ERROR("attribute '" << attr << "' is not a float (it is " << attr_value << ")");
169 }
170 return ret_value;
171}
boost::property_tree::ptree ptree
static Double_t a
std::string readAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
unsigned int getUIntAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
float getFloatAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
void printAttributes(const boost::property_tree::ptree &tree)
int getIntAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
bool hasAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
std::string getAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
TrigConfMessaging(const std::string &name)
Constructor with parameters.
Forward iterator to traverse the main components of the trigger configuration.
Definition Config.h:22
STL namespace.
TChain * tree