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 <boost/lexical_cast.hpp>
7 #include <iostream>
8 
9 using namespace std;
11 
13  TrigConf::TrigConfMessaging("MuctpiXML")
14 {}
15 
16 void
18 
19  // initialize attributes ptree
20  ptree tmp_ptree;
21  ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
22 
23  // iterate through elements
24  for(const ptree::value_type & a: attributes) {
25  string attrName = a.first;
26  string attrVal = a.second.data();
27  std::cout << attrName << " : " << attrVal << std::endl;
28  }
29 }
30 
31 bool
32 MuctpiXMLHelper::hasAttribute(const ptree & tree, const string & attr) {
33 
34  // initialize attributes ptree
35  ptree tmp_ptree;
36  ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
37 
38  if(attributes.empty()) return false;
39 
40  for(const ptree::value_type &a: attributes) {
41  string attrName = a.first;
42  if(attrName == attr) {
43  return true;
44  }
45  }
46  return false;
47 }
48 
49 
50 string
51 MuctpiXMLHelper::readAttribute(const ptree & tree, const string & attr) {
52 
53  // initialize attributes ptree
54  ptree tmp_ptree;
55  ptree attributes = tree.get_child("<xmlattr>", tmp_ptree);
56 
57  // iterate through children
58  for(const ptree::value_type &a : attributes) {
59  if(a.first != attr) continue;
60  return a.second.data(); //a.second.data is the value!
61  }
62  return "";
63 }
64 
65 
66 std::string
67 MuctpiXMLHelper::getAttribute(const ptree & tree, const string & attr) {
68  if( ! hasAttribute(tree, attr) ) {
69  TRG_MSG_WARNING("attribute " << attr << " does not exist");
70  return "";
71  }
72  return readAttribute(tree,attr);
73 }
74 
75 std::string
76 MuctpiXMLHelper::getAttribute(const ptree & tree, const string & attr, const std::string & defval) {
77  if( ! hasAttribute(tree, attr) )
78  return defval;
79  return readAttribute(tree,attr);
80 }
81 
82 
83 int
84 MuctpiXMLHelper::getIntAttribute(const ptree & tree, const string & attr) {
85  if( ! hasAttribute(tree, attr) ) {
86  TRG_MSG_WARNING("attribute " << attr << " does not exist");
87  return 0;
88  }
89 
90  int ret_value{0};
91  try {
92  ret_value = boost::lexical_cast<int, string>(readAttribute(tree,attr));
93  }
94  catch(const boost::bad_lexical_cast & bc) {
95  TRG_MSG_ERROR("attribute '" << attr << "' is not an int (it is '" << readAttribute(tree,attr) << "')");
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  try {
106  ret_value = boost::lexical_cast<int, string>(readAttribute(tree,attr));
107  }
108  catch(const boost::bad_lexical_cast & bc) {
109  TRG_MSG_ERROR("attribute '" << attr << "' is not an int (it is '" << readAttribute(tree,attr) << "')");
110  }
111  return ret_value;
112 }
113 
114 
115 unsigned int
116 MuctpiXMLHelper::getUIntAttribute(const ptree & tree, const string & attr) {
117  if( ! hasAttribute(tree, attr) ) {
118  TRG_MSG_WARNING("attribute " << attr << " does not exist");
119  return 0;
120  }
121  unsigned int ret_value{0};
122  try {
123  ret_value = boost::lexical_cast<unsigned int, string>(readAttribute(tree,attr));
124  }
125  catch(const boost::bad_lexical_cast & bc) {
126  TRG_MSG_ERROR("attribute '" << attr << "' is not an unsigned int (it is " << readAttribute(tree,attr) << ")");
127  }
128  return ret_value;
129 }
130 
131 unsigned int
132 MuctpiXMLHelper::getUIntAttribute(const ptree & tree, const string & attr, unsigned int & defval) {
133  if( ! hasAttribute(tree, attr) )
134  return defval;
135  return boost::lexical_cast<unsigned int, string>(readAttribute(tree,attr));
136 }
137 
138 
139 float
140 MuctpiXMLHelper::getFloatAttribute(const ptree & tree, const string & attr) {
141  if( ! hasAttribute(tree, attr) ) {
142  TRG_MSG_WARNING("attribute " << attr << " does not exist");
143  return 0;
144  }
145  float ret_value{0};
146  try {
147  ret_value = boost::lexical_cast<float, string>(readAttribute(tree,attr));
148  }
149  catch(const boost::bad_lexical_cast & bc) {
150  TRG_MSG_ERROR("attribute '" << attr << "' is not an float (it is " << readAttribute(tree,attr) << ")");
152  }
153  return ret_value;
154 }
155 
156 float
157 MuctpiXMLHelper::getFloatAttribute(const ptree & tree, const string & attr, float & defval) {
158  if( ! hasAttribute(tree, attr) )
159  return defval;
160  return boost::lexical_cast<float, string>(readAttribute(tree,attr));
161 }
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:67
MuctpiXMLHelper::printAttributes
void printAttributes(const boost::property_tree::ptree &tree)
Definition: MuctpiXMLHelper.cxx:17
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:116
MuctpiXMLHelper::readAttribute
std::string readAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:51
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:32
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:84
python.PoolAttributeHelper.attrName
attrName
Definition: PoolAttributeHelper.py:100
a
TList * a
Definition: liststreamerinfos.cxx:10
MuctpiXMLHelper::MuctpiXMLHelper
MuctpiXMLHelper()
Definition: MuctpiXMLHelper.cxx:12
collListGuids.attributes
attributes
Definition: collListGuids.py:46
MuctpiXMLHelper::getFloatAttribute
float getFloatAttribute(const boost::property_tree::ptree &tree, const std::string &attr)
Definition: MuctpiXMLHelper.cxx:140