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 class TTree;
24 
25 namespace Darwin::Tools {
26 
52 class UserInfo {
53 public:
56  enum Format {
57  INFO,
58  JSON,
59  XML
60  };
61 
62 private:
63  bool own;
64  TList * l;
65 
68  TList * List (TList * mother, const char * key) const;
69 
75  template<class T> T Get (TList * mother, const char * key) const
76  {
77  if constexpr (std::is_arithmetic_v<T>) {
78  TObject * obj = mother->FindObject(key);
79  auto parameter = dynamic_cast<TParameter<T>*>(obj);
80  if (parameter == nullptr)
81  BOOST_THROW_EXCEPTION(std::invalid_argument(
82  Form("No `TParameter` with name '%s' could be found in the `TList` '%s'.", key, mother->GetName())));
83  return parameter->GetVal();
84  }
85  else {
86  TList * daughter = List(mother, key);
87  TIter next(daughter->begin());
88  auto objs = dynamic_cast<TObjString*>(daughter->Last());
89  if (daughter->GetSize() == 0 || objs == nullptr)
90  BOOST_THROW_EXCEPTION(std::invalid_argument(
91  Form("No literal with name '%s' was found in the `TList` '%s'.", key, mother->GetName())));
92  T literal = objs->GetString().Data();
93  return literal;
94  }
95  }
96 
99  template<class T, typename... Args> T Get (TList * mother, const char * key, Args... args) const
100  {
101  TList * daughter = List(mother, key);
102  return Get<T>(daughter, args...);
103  }
104 
107  bool Find (TList * mother, const char * key) const
108  {
109  TObject * obj = mother->FindObject(key);
110  return (obj != nullptr);
111  }
112 
115  template<typename... Args> bool Find (TList * mother, const char * key, Args... args) const
116  {
117  if (!Find(mother, key)) return false;
118  TList * daughter = List(mother, key);
119  if (daughter->GetSize() > 0)
120  return Find(daughter, args...);
121  else return false;
122  }
123 
129  template<class T> void Set (TList * mother, const char * key, T value) const
130  {
131  if constexpr (std::is_arithmetic_v<T>) {
132  TObject * obj = mother->FindObject(key);
133  if (obj) {
134  auto parameter = dynamic_cast<TParameter<T>*>(obj);
135  if (parameter == nullptr)
136  BOOST_THROW_EXCEPTION(std::invalid_argument(
137  Form("No `TParameter` with name '%s' and typeid '%s' was found in the `TList` '%s'.",
138  key, typeid(T).name(), mother->GetName())));
139  parameter->SetVal(value);
140  }
141  else {
142  auto parameter = new TParameter<T>(key, value, 'l');
143  mother->Add(parameter);
144  }
145  }
146  else {
147  TList * daughter = List(mother, key);
148  TString str(value);
149  auto objs = new TObjString(str);
150  daughter->Add(objs);
151  }
152  }
153 
156  template<class T, typename... Args> void Set (TList * mother, const char * key, Args... args) const
157  {
158  TList * daughter = List(mother, key);
159  Set<T>(daughter, args...);
160  }
161 
164  template<typename... Args> TList * List (TList * mother, const char * key, Args... args) const
165  {
166  TList * daughter = List(mother, key);
167  return List(daughter, args...);
168  }
169 
170  template<class T>
171  inline T Get (TList * mother, std::string key) const { return Get<T>(mother, key.c_str()); }
172  inline bool Find (TList * mother, std::string key) const { return Find (mother, key.c_str()); }
173  inline TList * List (TList * mother, std::string key) const { return List (mother, key.c_str()); }
174 
178  void ConvertPtree (const boost::property_tree::ptree&, const std::string&, TList*);
179 
180 protected:
181 
187  boost::property_tree::ptree MkPtree (TList *,
188  Format = INFO
189  ) const;
190 
191 public:
194  template<typename... Args> TList * List (const char * key, Args... args) const
195  {
196  return List(l, key, args...);
197  }
198 
201  inline TList * List () const
202  {
203  return l;
204  }
205 
208  template<class T, typename... Args> T Get (const char * key, Args... args) const
209  {
210  return Get<T>(l, key, args...);
211  }
212 
215  template<typename... Args> bool Find (const char * key, Args... args) const
216  {
217  return Find(l, key, args...);
218  }
219 
224  template<class T, typename... Args> void Set (const char * key, Args... args) const
225  {
226  Set<T>(l, key, args...);
227  }
228 
229  UserInfo (const char * = "UserInfo");
230  UserInfo (const boost::property_tree::ptree&);
231  UserInfo (TList *);
232  UserInfo (TTree *);
233  UserInfo (TTree *, const boost::property_tree::ptree&);
234  ~UserInfo ();
235 
236  void Write () const;
237  bool Empty () const;
238 
243  boost::property_tree::ptree Write (std::ostream&,
244  Format = INFO
245  ) const;
246 
251  boost::property_tree::ptree Write (const std::filesystem::path&
252  ) const;
253 
258  inline boost::property_tree::ptree MkPtree (Format format = INFO
259  ) const { return MkPtree(l, format); }
260 
263  inline void ls () const { if (l != nullptr) l->ls(); }
264 };
265 
266 } // end of namespace
267 
270 template<> struct std::hash<boost::property_tree::ptree> {
271  std::size_t operator() (boost::property_tree::ptree const&) const noexcept;
272 };
273 
276 template<> struct std::hash<Darwin::Tools::UserInfo> {
277  std::size_t operator() (Darwin::Tools::UserInfo const&) const noexcept;
278 };
279 
286 template<> struct std::hash<TTree *> {
287  std::size_t operator() (TTree * const&) const noexcept;
288 };
289 
290 #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:215
Darwin::Tools::UserInfo::List
TList * List() const
Access directly to main list.
Definition: UserInfo.h:201
Darwin::Tools::UserInfo::l
TList * l
main list (typically from TTree::GetUserInfo())
Definition: UserInfo.h:64
Darwin::Tools::UserInfo::XML
@ XML
XML format, another universal format.
Definition: UserInfo.h:59
Ntupliser_cfg.args
args
Definition: Ntupliser_cfg.py:11
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key) const
Definition: UserInfo.h:75
Darwin::Tools::UserInfo::Get
T Get(TList *mother, std::string key) const
value getter with string
Definition: UserInfo.h:171
Darwin::Tools::UserInfo::MkPtree
boost::property_tree::ptree MkPtree(Format format=INFO) const
Definition: UserInfo.h:258
Darwin::Tools::UserInfo::JSON
@ JSON
JSON format, rather common and nearly as powerful.
Definition: UserInfo.h:58
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key, Args... args) const
Recursive getter.
Definition: UserInfo.h:99
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:167
Darwin::Tools::UserInfo::List
TList * List(TList *mother, const char *key, Args... args) const
Access a sublist.
Definition: UserInfo.h:164
Darwin::Tools::UserInfo::Set
void Set(TList *mother, const char *key, Args... args) const
Recursive setter.
Definition: UserInfo.h:156
Darwin::Tools::UserInfo::Get
T Get(const char *key, Args... args) const
Generic getter.
Definition: UserInfo.h:208
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, const char *key, Args... args) const
Recursive finder.
Definition: UserInfo.h:115
Darwin::Tools::UserInfo::Write
void Write() const
Write TList to TFile in ROOT format.
Definition: UserInfo.cc:149
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, const char *key) const
Recursive finder.
Definition: UserInfo.h:107
Darwin::Tools::UserInfo::Set
void Set(const char *key, Args... args) const
Definition: UserInfo.h:224
Darwin::Tools::UserInfo::own
bool own
Definition: UserInfo.h:63
Darwin
Global namespace for libraries and executables for analysis with Darwin.
Definition: forceMetaInfo.cc:28
Darwin::Tools::UserInfo::INFO
@ INFO
INFO format, proper to Boost Property Trees (default)
Definition: UserInfo.h:57
Darwin::Tools::UserInfo::Set
void Set(TList *mother, const char *key, T value) const
Definition: UserInfo.h:129
Darwin::Tools
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
Darwin::Tools::UserInfo::MkPtree
boost::property_tree::ptree MkPtree(TList *, Format=INFO) const
Definition: UserInfo.cc:52
Darwin::Tools::UserInfo::Empty
bool Empty() const
Check if list is empty.
Definition: UserInfo.cc:156
Darwin::Tools::UserInfo::List
TList * List(TList *mother, std::string key) const
list getter with string
Definition: UserInfo.h:173
Darwin::Tools::UserInfo::ls
void ls() const
Prints the content of the TList
Definition: UserInfo.h:263
Darwin::Tools::UserInfo::Format
Format
Supported serialization formats.
Definition: UserInfo.h:56
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, std::string key) const
finder with string
Definition: UserInfo.h:172
Darwin::Tools::UserInfo
Generic meta-information for n-tuple (can be used out of Darwin).
Definition: UserInfo.h:52
Darwin::Tools::UserInfo::List
TList * List(const char *key, Args... args) const
Access directly a sublist.
Definition: UserInfo.h:194
Darwin::Tools::UserInfo::~UserInfo
~UserInfo()
Destructor.
Definition: UserInfo.cc:231
Darwin::Tools::UserInfo::UserInfo
UserInfo(const char *="UserInfo")
Constructor (starting from scratch)
Definition: UserInfo.cc:161