DAS  3.0
Das Analysis System
UserInfo.h
Go to the documentation of this file.
1 #ifndef DARWIN_USERINFO_H
2 #define DARWIN_USERINFO_H
3 
4 #include <cassert>
5 
6 #include <type_traits>
7 #include <iostream>
8 #include <filesystem>
9 #include <typeinfo>
10 #include <stdexcept>
11 #include <string>
12 #include <functional>
13 #include <sstream>
14 
15 #include <TList.h>
16 #include <TString.h>
17 #include <TParameter.h>
18 #include <TObjString.h>
19 
20 #include <boost/property_tree/ptree.hpp>
21 #include <boost/exception/all.hpp>
22 
23 #include "colours.h"
24 
25 class TTree;
26 
27 namespace Darwin::Tools {
28 
54 class UserInfo {
55 public:
58  enum Format {
59  INFO,
60  JSON,
61  XML
62  };
63 
64 private:
65  bool own;
66  TList * l;
67 
70  TList * List (TList * mother, const char * key) const;
71 
77  template<class T> T Get (TList * mother, const char * key) const
78  {
79  if constexpr (std::is_arithmetic_v<T>) {
80  TObject * obj = mother->FindObject(key);
81  auto parameter = dynamic_cast<TParameter<T>*>(obj);
82  if (parameter == nullptr)
83  BOOST_THROW_EXCEPTION(std::invalid_argument(
84  Form("No `TParameter` with name '%s' could be found in the `TList` '%s'.", key, mother->GetName())));
85  return parameter->GetVal();
86  }
87  else {
88  TList * daughter = List(mother, key);
89  TIter next(daughter->begin());
90  auto objs = dynamic_cast<TObjString*>(daughter->Last());
91  if (daughter->GetSize() == 0 || objs == nullptr)
92  BOOST_THROW_EXCEPTION(std::invalid_argument(
93  Form("No literal with name '%s' was found in the `TList` '%s'.", key, mother->GetName())));
94  T literal = objs->GetString().Data();
95  return literal;
96  }
97  }
98 
101  template<class T, typename... Args> T Get (TList * mother, const char * key, Args... args) const
102  {
103  TList * daughter = List(mother, key);
104  return Get<T>(daughter, args...);
105  }
106 
109  bool Find (TList * mother, const char * key) const
110  {
111  CheckKey(key);
112  TObject * obj = mother->FindObject(key);
113  return (obj != nullptr);
114  }
115 
118  template<typename... Args> bool Find (TList * mother, const char * key, Args... args) const
119  {
120  if (!Find(mother, key)) return false;
121  TList * daughter = List(mother, key);
122  if (daughter->GetSize() > 0)
123  return Find(daughter, args...);
124  else return false;
125  }
126 
129  void CheckKey (const char * key) const
130  {
131  if (TString(key).Length() == 0)
132  BOOST_THROW_EXCEPTION( std::invalid_argument("Empty keys are invalid") );
133  }
134 
140  template<class T> void Set (TList * mother, const char * key, T value) const
141  {
142  CheckKey(key);
143 
144  if constexpr (std::is_arithmetic_v<T>) {
145  TObject * obj = mother->FindObject(key);
146  if (obj) {
147  auto parameter = dynamic_cast<TParameter<T>*>(obj);
148  if (parameter == nullptr)
149  BOOST_THROW_EXCEPTION(std::invalid_argument(
150  Form("No `TParameter` with name '%s' and typeid '%s' was found in the `TList` '%s'.",
151  key, typeid(T).name(), mother->GetName())));
152  parameter->SetVal(value);
153  }
154  else {
155  auto parameter = new TParameter<T>(key, value, 'l');
156  mother->Add(parameter);
157  }
158  }
159  else {
160  TString str(value);
161  TList * daughter = List(mother, key);
162  if (str.Length() >= 0) {
163  auto objs = new TObjString(str);
164  daughter->Add(objs);
165  }
166  }
167  }
168 
171  template<class T, typename... Args> void Set (TList * mother, const char * key, Args... args) const
172  {
173  CheckKey(key);
174  TList * daughter = List(mother, key);
175  Set<T>(daughter, args...);
176  }
177 
180  template<typename... Args> TList * List (TList * mother, const char * key, Args... args) const
181  {
182  TList * daughter = List(mother, key);
183  return List(daughter, args...);
184  }
185 
186  template<class T>
187  T Get (TList * mother, std::string key) const { return Get<T>(mother, key.c_str()); }
188  bool Find (TList * mother, std::string key) const { return Find (mother, key.c_str()); }
189  TList * List (TList * mother, std::string key) const { return List (mother, key.c_str()); }
190 
191 protected:
192 
196  void ConvertPtree (const boost::property_tree::ptree&, const std::string&, TList*);
197 
203  boost::property_tree::ptree MkPtree (TList *,
204  Format = INFO
205  ) const;
206 
213  boost::property_tree::ptree Write (const boost::property_tree::ptree&,
214  std::ostream&,
215  Format = INFO
216  ) const;
217 
218 public:
221  template<typename... Args> TList * List (const char * key, Args... args) const
222  {
223  return List(l, key, args...);
224  }
225 
228  TList * List () const
229  {
230  return l;
231  }
232 
235  template<class T, typename... Args> T Get (const char * key, Args... args) const
236  {
237  return Get<T>(l, key, args...);
238  }
239 
242  template<typename... Args> bool Find (const char * key, Args... args) const
243  {
244  return Find(l, key, args...);
245  }
246 
251  template<class T, typename... Args> void Set (const char * key, Args... args) const
252  {
253  Set<T>(l, key, args...);
254  }
255 
256  UserInfo (const char * = "UserInfo");
257  UserInfo (const boost::property_tree::ptree&);
258  UserInfo (TList *);
259  UserInfo (TTree *);
260  UserInfo (TTree *, const boost::property_tree::ptree&);
261  ~UserInfo ();
262 
263  void Write () const;
264  bool Empty () const;
265 
270  boost::property_tree::ptree Write (std::ostream&,
271  Format = INFO
272  ) const;
273 
278  boost::property_tree::ptree Write (const std::filesystem::path&
279  ) const;
280 
285  boost::property_tree::ptree MkPtree (Format format = INFO
286  ) const { return MkPtree(l, format); }
287 
290  void ls () const { if (l != nullptr) l->ls(); }
291 };
292 
293 } // end of namespace
294 
297 template<> struct std::hash<boost::property_tree::ptree> {
298  std::size_t operator() (boost::property_tree::ptree const&) const noexcept;
299 };
300 
303 template<> struct std::hash<Darwin::Tools::UserInfo> {
304  std::size_t operator() (Darwin::Tools::UserInfo const&) const noexcept;
305 };
306 
313 template<> struct std::hash<TTree *> {
314  std::size_t operator() (TTree * const&) const noexcept;
315 };
316 
317 #endif
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
Darwin::Tools::UserInfo::Find
bool Find(const char *key, Args... args) const
Generic finder.
Definition: UserInfo.h:242
Darwin::Tools::UserInfo::List
TList * List() const
Access directly to main list.
Definition: UserInfo.h:228
Darwin::Tools::UserInfo::l
TList * l
main list (typically from TTree::GetUserInfo())
Definition: UserInfo.h:66
Darwin::Tools::UserInfo::XML
@ XML
XML format, another universal format.
Definition: UserInfo.h:61
Ntupliser_cfg.args
args
Definition: Ntupliser_cfg.py:11
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key) const
Definition: UserInfo.h:77
Darwin::Tools::UserInfo::Get
T Get(TList *mother, std::string key) const
value getter with string
Definition: UserInfo.h:187
Darwin::Tools::UserInfo::MkPtree
boost::property_tree::ptree MkPtree(Format format=INFO) const
Definition: UserInfo.h:285
Darwin::Tools::UserInfo::JSON
@ JSON
JSON format, rather common and nearly as powerful.
Definition: UserInfo.h:60
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key, Args... args) const
Recursive getter.
Definition: UserInfo.h:101
jercExample.key
string key
Definition: jercExample.py:109
Darwin::Tools::UserInfo::ConvertPtree
void ConvertPtree(const boost::property_tree::ptree &, const std::string &, TList *)
Definition: UserInfo.cc:173
colours.h
Darwin::Tools::UserInfo::List
TList * List(TList *mother, const char *key, Args... args) const
Access a sublist.
Definition: UserInfo.h:180
Darwin::Tools::UserInfo::Set
void Set(TList *mother, const char *key, Args... args) const
Recursive setter.
Definition: UserInfo.h:171
Darwin::Tools::UserInfo::Get
T Get(const char *key, Args... args) const
Generic getter.
Definition: UserInfo.h:235
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, const char *key, Args... args) const
Recursive finder.
Definition: UserInfo.h:118
Darwin::Tools::UserInfo::Write
void Write() const
Write TList to TFile in ROOT format.
Definition: UserInfo.cc:155
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, const char *key) const
Check if an element exists in a TList.
Definition: UserInfo.h:109
Darwin::Tools::UserInfo::Set
void Set(const char *key, Args... args) const
Definition: UserInfo.h:251
Darwin::Tools::UserInfo::own
bool own
Definition: UserInfo.h:65
Darwin
Global namespace for libraries and executables for analysis with Darwin.
Definition: Darwin_dict.cxx:1143
Darwin::Tools::UserInfo::INFO
@ INFO
INFO format, proper to Boost Property Trees (default)
Definition: UserInfo.h:59
Darwin::Tools::UserInfo::Set
void Set(TList *mother, const char *key, T value) const
Definition: UserInfo.h:140
Darwin::Tools
Classes and functions related to the framework.
Definition: Darwin_dict.cxx:1144
Darwin::Tools::UserInfo::MkPtree
boost::property_tree::ptree MkPtree(TList *, Format=INFO) const
Definition: UserInfo.cc:52
Darwin::Tools::UserInfo::CheckKey
void CheckKey(const char *key) const
Check that the key is not empty.
Definition: UserInfo.h:129
Darwin::Tools::UserInfo::Empty
bool Empty() const
Check if list is empty.
Definition: UserInfo.cc:162
Darwin::Tools::UserInfo::List
TList * List(TList *mother, std::string key) const
list getter with string
Definition: UserInfo.h:189
Darwin::Tools::UserInfo::ls
void ls() const
Prints the content of the TList
Definition: UserInfo.h:290
Darwin::Tools::UserInfo::Format
Format
Supported serialization formats.
Definition: UserInfo.h:58
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, std::string key) const
finder with string
Definition: UserInfo.h:188
Darwin::Tools::UserInfo
Generic meta-information for n-tuple (can be used out of Darwin).
Definition: UserInfo.h:54
Darwin::Tools::UserInfo::List
TList * List(const char *key, Args... args) const
Access directly a sublist.
Definition: UserInfo.h:221
Darwin::Tools::UserInfo::~UserInfo
~UserInfo()
Destructor.
Definition: UserInfo.cc:249
Darwin::Tools::UserInfo::UserInfo
UserInfo(const char *="UserInfo")
Constructor (starting from scratch)
Definition: UserInfo.cc:167