ATLAS Offline Software
DataStructure.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef TRIGCONFDATA_DATASTRUCTURE_H
6 #define TRIGCONFDATA_DATASTRUCTURE_H
7 
12 #include <iostream>
13 #include <vector>
14 #include <memory>
15 #include <type_traits>
16 #include <optional>
17 #include "boost/property_tree/ptree.hpp"
18 
19 namespace TrigConf {
20 
37  class DataStructure {
38  public:
39 
41 
43  DataStructure();
44 
45  DataStructure(const DataStructure&) = default;
47 
50 
54  DataStructure(const ptree & data);
55  DataStructure(const std::string & name, const ptree & data);
57  DataStructure(const std::string & name, ptree && data);
58 
60  virtual ~DataStructure() = default;
61 
63  void setData(const ptree & data);
64  void setData(ptree && data);
65 
67  void setName(const std::string& n);
68 
70  virtual std::string className() const;
71 
72  virtual const std::string & name() const final;
73 
80  virtual void clear();
81 
83  const ptree & data() const {
84  if( ! isInitialized() ) {
85  throw std::runtime_error("Trying to access data of uninitialized object of type " + className());
86  }
87  // Don't use ?: operator here: it confuses cppcheck.
88  if (ownsData()) {
89  return *m_dataSPtr.get();
90  }
91  return *m_dataPtr;
92  }
93 
97  bool isValue() const;
98 
105  std::string getValue() const; // this will be removed in the next MR (TrigSignatureMoniMT still depends on it)
106 
107  template<class T>
108  T getValue() const {
109  return data().get_value<T>();
110  }
111 
115  template<class T>
116  std::optional<T> getValue_optional() const {
117  auto v = data().get_value_optional<T>();
118  return v ? std::optional<T>(std::move(*v)) : std::nullopt;
119  }
120 
125  bool hasAttribute(const std::string & key) const;
126 
134  bool isNull(const std::string & key) const;
135 
140  bool hasChild(const std::string & path) const;
141 
145  std::string operator[](const std::string & key) const;
146 
151  template<class T>
152  T getAttribute(const std::string & key, bool ignoreIfMissing = false, const T & def = T()) const {
153  const auto & obj = data().get_child_optional(key);
154  if( !obj ) {
155  if( ignoreIfMissing ) {
156  return def;
157  } else {
158  throw std::runtime_error(className() + "#" + name() + ": structure '" + key + "' does not exist" );
159  }
160  }
161  return obj.get().get_value<T>();
162  }
163 
164  template<class T>
165  std::optional<T> getAttribute_optional(const std::string & key) const {
166  const auto & obj = data().get_child_optional(key);
167  if( ! obj ) {
168  return std::nullopt;
169  }
170  auto v = obj.get().get_value_optional<T>();
171  return v ? std::optional(std::move(*v)) : std::nullopt;
172  }
173 
174  const std::string & getAttribute(const std::string & key, bool ignoreIfMissing = false, const std::string & def = "") const;
175 
183  std::vector<DataStructure> getList(const std::string & pathToChild, bool ignoreIfMissing = false) const;
184 
185  std::optional<std::vector<DataStructure> > getList_optional(const std::string & pathToChild) const;
186 
200  DataStructure getObject(const std::string & pathToChild, bool ignoreIfMissing = false) const;
201 
202  std::optional<TrigConf::DataStructure>
203  getObject_optional(const std::string & pathToChild) const;
204 
205 
210  std::vector<std::string> getKeys() const;
211 
212 
214  explicit operator bool() const { return m_initialized; }
215  bool isValid() const { return m_initialized; }
216  bool isInitialized() const { return m_initialized; }
217 
219  bool empty() const { return data().empty(); }
220 
221  /* Print this object including children
222  * @param os The output stream
223  */
224  void printRaw(std::ostream & os = std::cout) const;
225 
226  /* Print this object including children
227  * @param os The output stream
228  */
229  virtual void print(std::ostream & os = std::cout) const;
230 
237  static void printElement(const std::string& key,
238  const ptree & data,
239  uint level = 0,
240  std::ostream & os = std::cout);
241 
242  bool ownsData() const {
243  return (bool)m_dataSPtr;
244  }
245 
246  protected:
247 
252  virtual void update() {};
253 
254  bool m_initialized { false };
255 
256  std::shared_ptr<ptree> m_dataSPtr { nullptr }; // used when owning the tree
257  const ptree * m_dataPtr { nullptr }; // used when not owning the tree
258 
259  std::string m_name{""}; // most objects are named
260 
261  };
262 
263 }
264 
265 #ifndef TRIGCONF_STANDALONE
266 #ifndef XAOD_STANDALONE
267 
268 #include "AthenaKernel/CLASS_DEF.h"
269 CLASS_DEF( TrigConf::DataStructure , 98904516 , 1 )
270 
271 #include "AthenaKernel/CondCont.h"
273 
274 #endif
275 #endif
276 
277 
278 #endif
TrigConf::DataStructure::data
const ptree & data() const
Access to the underlying data, if needed.
Definition: DataStructure.h:83
TrigConf::DataStructure::getObject
DataStructure getObject(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to configuration object.
Definition: DataStructure.cxx:207
TrigConf::DataStructure::~DataStructure
virtual ~DataStructure()=default
Destructor.
TrigConf::DataStructure::operator=
DataStructure & operator=(DataStructure &&)=default
TrigConf::DataStructure::isInitialized
bool isInitialized() const
Definition: DataStructure.h:216
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
TrigConf::DataStructure::getObject_optional
std::optional< TrigConf::DataStructure > getObject_optional(const std::string &pathToChild) const
Definition: DataStructure.cxx:230
TrigConf::DataStructure::empty
bool empty() const
Check if children exist.
Definition: DataStructure.h:219
TrigConf::DataStructure::printElement
static void printElement(const std::string &key, const ptree &data, uint level=0, std::ostream &os=std::cout)
Static function to print a ptree object.
Definition: DataStructure.cxx:279
TrigConf::DataStructure::setName
void setName(const std::string &n)
Setting the configuration element name.
Definition: DataStructure.cxx:59
TrigConf::DataStructure::getAttribute
T getAttribute(const std::string &key, bool ignoreIfMissing=false, const T &def=T()) const
Access to simple attribute.
Definition: DataStructure.h:152
TrigConf::DataStructure::getAttribute_optional
std::optional< T > getAttribute_optional(const std::string &key) const
Definition: DataStructure.h:165
TrigConf::DataStructure::m_dataSPtr
std::shared_ptr< ptree > m_dataSPtr
Definition: DataStructure.h:256
TrigConf::DataStructure::getKeys
std::vector< std::string > getKeys() const
Access to the keys of an DataStructure which presents a dictionary.
Definition: DataStructure.cxx:250
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:560
TrigConf::DataStructure::name
virtual const std::string & name() const final
Definition: DataStructure.cxx:109
TrigConf::DataStructure::m_name
std::string m_name
Definition: DataStructure.h:259
CONDCONT_DEF
CONDCONT_DEF(TrigConf::DataStructure, 265887802)
TrigConf::DataStructure::setData
void setData(const ptree &data)
Setting the configuration data.
Definition: DataStructure.cxx:39
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
TrigConf::DataStructure::isValid
bool isValid() const
Definition: DataStructure.h:215
TrigConf
Forward iterator to traverse the main components of the trigger configuration.
Definition: Config.h:22
TrigConf::DataStructure::hasChild
bool hasChild(const std::string &path) const
Check if child exists.
Definition: DataStructure.cxx:114
TrigConf::DataStructure::DataStructure
DataStructure(const DataStructure &)=default
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
TrigConf::DataStructure::operator[]
std::string operator[](const std::string &key) const
Access to simple attribute.
Definition: DataStructure.cxx:121
beamspotman.n
n
Definition: beamspotman.py:731
TrigConf::DataStructure::className
virtual std::string className() const
A string that is the name of the class.
Definition: DataStructure.cxx:104
TrigConf::DataStructure::DataStructure
DataStructure(DataStructure &&)=default
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
TrigConf::name
Definition: HLTChainList.h:35
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
TrigConf::DataStructure::getList_optional
std::optional< std::vector< DataStructure > > getList_optional(const std::string &pathToChild) const
Definition: DataStructure.cxx:197
TrigConf::DataStructure::getList
std::vector< DataStructure > getList(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to array structure.
Definition: DataStructure.cxx:158
TrigConf::DataStructure::m_initialized
bool m_initialized
if initialized, the underlying ptree is has been assigned to (can be empty)
Definition: DataStructure.h:254
TrigConf::DataStructure::printRaw
void printRaw(std::ostream &os=std::cout) const
Definition: DataStructure.cxx:265
TrigConf::DataStructure
Base class for Trigger configuration data and wrapper around underlying representation.
Definition: DataStructure.h:37
TrigConf::DataStructure::operator=
DataStructure & operator=(const DataStructure &)=default
TrigConf::DataStructure::getValue
std::string getValue() const
Access to simple content.
Definition: DataStructure.cxx:80
TrigConf::DataStructure::isNull
bool isNull(const std::string &key) const
Check if an attribute is null.
Definition: DataStructure.cxx:94
python.PyAthena.v
v
Definition: PyAthena.py:154
CLASS_DEF
#define CLASS_DEF(NAME, CID, VERSION)
associate a clid and a version to a type eg
Definition: Control/AthenaKernel/AthenaKernel/CLASS_DEF.h:64
TrigConf::DataStructure::getValue
T getValue() const
Definition: DataStructure.h:108
TrigConf::DataStructure::getValue_optional
std::optional< T > getValue_optional() const
access to content of the note Will return false if the value could not be converted into T
Definition: DataStructure.h:116
TrigConf::DataStructure::clear
virtual void clear()
Clearing the configuration data.
Definition: DataStructure.cxx:65
TrigConf::DataStructure::print
virtual void print(std::ostream &os=std::cout) const
Definition: DataStructure.cxx:272
TrigConf::DataStructure::ownsData
bool ownsData() const
Definition: DataStructure.h:242
python.PyAthena.obj
obj
Definition: PyAthena.py:132
TrigConf::DataStructure::DataStructure
DataStructure()
Default constructor, leading to an uninitialized configuration object.
Definition: DataStructure.cxx:11
TrigConf::DataStructure::isValue
bool isValue() const
Check for attribute.
Definition: DataStructure.cxx:74
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
TrigConf::DataStructure::ptree
boost::property_tree::ptree ptree
Definition: DataStructure.h:40
CLASS_DEF.h
macros to associate a CLID to a type
TrigConf::DataStructure::hasAttribute
bool hasAttribute(const std::string &key) const
Check for attribute.
Definition: DataStructure.cxx:86
TrigConf::DataStructure::m_dataPtr
const ptree * m_dataPtr
Definition: DataStructure.h:257
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
TrigConf::DataStructure::update
virtual void update()
Update the internal data after modification of the data object.
Definition: DataStructure.h:252