ATLAS Offline Software
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 
10 using namespace std;
12 
14  TrigConf::TrigConfMessaging("MuctpiXML")
15 {}
16 
17 void
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 
32 bool
33 MuctpiXMLHelper::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 
51 string
52 MuctpiXMLHelper::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 
67 std::string
68 MuctpiXMLHelper::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 
76 std::string
77 MuctpiXMLHelper::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 
84 int
85 MuctpiXMLHelper::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 
100 int
101 MuctpiXMLHelper::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 
114 unsigned int
115 MuctpiXMLHelper::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 
129 unsigned int
130 MuctpiXMLHelper::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 
143 float
144 MuctpiXMLHelper::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 
159 float
160 MuctpiXMLHelper::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 }
TRG_MSG_ERROR
#define TRG_MSG_ERROR(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:29
MuctpiXMLHelper::getAttribute
std::string getAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:68
MuctpiXMLHelper::printAttributes
void printAttributes(const boost::property_tree::ptree &tree)
Definition: MuctpiXMLHelper.cxx:18
tree
TChain * tree
Definition: tile_monitor.h:30
MuctpiXMLHelper::getUIntAttribute
unsigned int getUIntAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:115
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
MuctpiXMLHelper::readAttribute
std::string readAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:52
TrigConf
Forward iterator to traverse the main components of the trigger configuration.
Definition: Config.h:22
MuctpiXMLHelper.h
MuctpiXMLHelper::hasAttribute
bool hasAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:33
TRG_MSG_WARNING
#define TRG_MSG_WARNING(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:28
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
MuctpiXMLHelper::getIntAttribute
int getIntAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:85
python.PoolAttributeHelper.attrName
attrName
Definition: PoolAttributeHelper.py:100
a
TList * a
Definition: liststreamerinfos.cxx:10
MuctpiXMLHelper::MuctpiXMLHelper
MuctpiXMLHelper()
Definition: MuctpiXMLHelper.cxx:13
MuctpiXMLHelper::getFloatAttribute
float getFloatAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:144
python.TransformConfig.attributes
def attributes(self)
Definition: TransformConfig.py:383