DAS  3.0
Das Analysis System
UserInfo

Description

Generic meta-information for n-tuple (can be used out of Darwin).

Generic class to read the meta-information contained in a list. The original goal is to read and write information to store in the list TTree::GetUserInfo(), but it can be used in general.

The elements of the list may simply be reached with UserInfo::Get() and written with UserInfo::Set(). The list may contain simple types such as arithmetic types or literals as well as other lists. To reach a list, the Get and Set function simply need the list of keys to navigate through the tree structure, provided with additional arguments. Examples:

ui.Set<bool>("one", "two", "myBool", true);
cout << ui.Get<bool>("one", "two", "myBool") << endl; // will print true
cout << ui.Get<string>("this", "will", "fail") << endl;

The whole information may be extracted in JSON, INFO, or XML format with Darwin::Tools::UserInfo::Write().

This class is as generic as can be. In certain cases, one may want to use inherit from this class to enforce a more rigid structure. One example is Darwin::Tools::MetaInfo.

#include <UserInfo.h>

+ Inheritance diagram for UserInfo:

Public Types

enum  Format {
  INFO, JSON, XML, INFO,
  JSON, XML
}
 
enum  Format {
  INFO, JSON, XML, INFO,
  JSON, XML
}
 

Public Member Functions

template<typename... Args>
TList * List (const char *key, Args... args) const
 
TList * List () const
 
template<class T , typename... Args>
Get (const char *key, Args... args) const
 
template<typename... Args>
bool Find (const char *key, Args... args) const
 
template<class T , typename... Args>
void Set (const char *key, Args... args) const
 
 UserInfo (const char *="UserInfo")
 
 UserInfo (const boost::property_tree::ptree &)
 
 UserInfo (TList *)
 
 UserInfo (TTree *)
 
 UserInfo (TTree *, const boost::property_tree::ptree &)
 
 ~UserInfo ()
 
void Write () const
 
bool Empty () const
 
boost::property_tree::ptree Write (std::ostream &, Format=INFO) const
 
boost::property_tree::ptree Write (const std::filesystem::path &) const
 
boost::property_tree::ptree MkPtree (Format format=INFO) const
 
void ls () const
 
template<typename... Args>
TList * List (const char *key, Args... args) const
 
TList * List () const
 
template<class T , typename... Args>
Get (const char *key, Args... args) const
 
template<typename... Args>
bool Find (const char *key, Args... args) const
 
template<class T , typename... Args>
void Set (const char *key, Args... args) const
 
 UserInfo (const char *="UserInfo")
 
 UserInfo (const boost::property_tree::ptree &)
 
 UserInfo (TList *)
 
 UserInfo (TTree *)
 
 UserInfo (TTree *, const boost::property_tree::ptree &)
 
 ~UserInfo ()
 
void Write () const
 
bool Empty () const
 
boost::property_tree::ptree Write (std::ostream &, Format=INFO) const
 
boost::property_tree::ptree Write (const std::filesystem::path &) const
 
boost::property_tree::ptree MkPtree (Format format=INFO) const
 
void ls () const
 

Protected Member Functions

void ConvertPtree (const boost::property_tree::ptree &, const std::string &, TList *)
 
boost::property_tree::ptree MkPtree (TList *, Format=INFO) const
 
boost::property_tree::ptree Write (const boost::property_tree::ptree &, std::ostream &, Format=INFO) const
 
void ConvertPtree (const boost::property_tree::ptree &, const std::string &, TList *)
 
boost::property_tree::ptree MkPtree (TList *, Format=INFO) const
 
boost::property_tree::ptree Write (const boost::property_tree::ptree &, std::ostream &, Format=INFO) const
 

Private Member Functions

TList * List (TList *mother, const char *key) const
 
template<class T >
Get (TList *mother, const char *key) const
 
template<class T , typename... Args>
Get (TList *mother, const char *key, Args... args) const
 
bool Find (TList *mother, const char *key) const
 
template<typename... Args>
bool Find (TList *mother, const char *key, Args... args) const
 
void CheckKey (const char *key) const
 
template<class T >
void Set (TList *mother, const char *key, T value) const
 
template<class T , typename... Args>
void Set (TList *mother, const char *key, Args... args) const
 
template<typename... Args>
TList * List (TList *mother, const char *key, Args... args) const
 
template<class T >
Get (TList *mother, std::string key) const
 
bool Find (TList *mother, std::string key) const
 
TList * List (TList *mother, std::string key) const
 
TList * List (TList *mother, const char *key) const
 
template<class T >
Get (TList *mother, const char *key) const
 
template<class T , typename... Args>
Get (TList *mother, const char *key, Args... args) const
 
bool Find (TList *mother, const char *key) const
 
template<typename... Args>
bool Find (TList *mother, const char *key, Args... args) const
 
void CheckKey (const char *key) const
 
template<class T >
void Set (TList *mother, const char *key, T value) const
 
template<class T , typename... Args>
void Set (TList *mother, const char *key, Args... args) const
 
template<typename... Args>
TList * List (TList *mother, const char *key, Args... args) const
 
template<class T >
Get (TList *mother, std::string key) const
 
bool Find (TList *mother, std::string key) const
 
TList * List (TList *mother, std::string key) const
 

Private Attributes

bool own
 
TList * l
 

Member Enumeration Documentation

◆ Format [1/2]

enum Format

Supported serialization formats.

Enumerator
INFO 

INFO format, proper to Boost Property Trees (default)

JSON 

JSON format, rather common and nearly as powerful.

XML 

XML format, another universal format.

INFO 

INFO format, proper to Boost Property Trees (default)

JSON 

JSON format, rather common and nearly as powerful.

XML 

XML format, another universal format.

58  {
59  INFO,
60  JSON,
61  XML
62  };

◆ Format [2/2]

enum Format

Supported serialization formats.

Enumerator
INFO 

INFO format, proper to Boost Property Trees (default)

JSON 

JSON format, rather common and nearly as powerful.

XML 

XML format, another universal format.

INFO 

INFO format, proper to Boost Property Trees (default)

JSON 

JSON format, rather common and nearly as powerful.

XML 

XML format, another universal format.

58  {
59  INFO,
60  JSON,
61  XML
62  };

Constructor & Destructor Documentation

◆ UserInfo() [1/10]

UserInfo ( const char *  name = "UserInfo")

Constructor (starting from scratch)

168  : own(true), l(new TList)
169 {
170  l->SetName(name);
171 }

◆ UserInfo() [2/10]

UserInfo ( const boost::property_tree::ptree &  ptree)

Constructor (starting from existing ptree)

231  : own(true), l(new TList)
232 {
233  for (const auto& [name, pt]: ptree)
234  ConvertPtree(pt, name, l);
235 }

◆ UserInfo() [3/10]

UserInfo ( TList *  L)

Constructor (starting from existing list)

238  : own(false), l(L)
239 {
240  if (L == nullptr)
241  BOOST_THROW_EXCEPTION(invalid_argument("Invalid list given in argument."));
242 }

◆ UserInfo() [4/10]

UserInfo ( TTree *  t)

Constructor (starting from TTree)

245  : own(false), l(t->GetUserInfo())
246 {
247 }

◆ UserInfo() [5/10]

UserInfo ( TTree *  tree,
const boost::property_tree::ptree &  ptree 
)

constructor (inserting existing ptree direcrly in a ttree)

224  : own(false), l(tree->GetUserInfo())
225 {
226  for (const auto& [name, pt]: ptree)
227  ConvertPtree(pt, name, l);
228 }

◆ ~UserInfo() [1/2]

~UserInfo ( )

Destructor.

250 {
251  if (!own) return;
252  delete l;
253 }

◆ UserInfo() [6/10]

UserInfo ( const char *  = "UserInfo")

Constructor (starting from scratch)

◆ UserInfo() [7/10]

UserInfo ( const boost::property_tree::ptree &  )

Constructor (starting from existing ptree)

◆ UserInfo() [8/10]

UserInfo ( TList *  )

Constructor (starting from existing list)

◆ UserInfo() [9/10]

UserInfo ( TTree *  )

Constructor (starting from TTree)

◆ UserInfo() [10/10]

UserInfo ( TTree *  ,
const boost::property_tree::ptree &   
)

constructor (inserting existing ptree direcrly in a ttree)

◆ ~UserInfo() [2/2]

~UserInfo ( )

Destructor.

Member Function Documentation

◆ CheckKey() [1/2]

void CheckKey ( const char *  key) const
inlineprivate

Check that the key is not empty.

130  {
131  if (TString(key).Length() == 0)
132  BOOST_THROW_EXCEPTION( std::invalid_argument("Empty keys are invalid") );
133  }

◆ CheckKey() [2/2]

void CheckKey ( const char *  key) const
inlineprivate

Check that the key is not empty.

130  {
131  if (TString(key).Length() == 0)
132  BOOST_THROW_EXCEPTION( std::invalid_argument("Empty keys are invalid") );
133  }

◆ ConvertPtree() [1/2]

void ConvertPtree ( const boost::property_tree::ptree &  ,
const std::string &  ,
TList *   
)
protected

Helper function to convert a ptree to a TList. Supports making strings, bools, ints, and floats.

◆ ConvertPtree() [2/2]

void ConvertPtree ( const boost::property_tree::ptree &  pt,
const std::string &  name,
TList *  mother 
)
protected

Helper function to convert a ptree to a TList. Supports making strings, bools, ints, and floats.

176 {
177  if (pt.empty()) {
178  // Value - we need to figure out the type
179  auto str = pt.get_value<string>();
180 
181  char * end;
182 
183  // empty value too --> just the element of a list
184  if (str.empty()) {
185  if (mother->Contains(name.c_str()))
186  return;
187 
188  TString s(name);
189  auto objs = new TObjString(s);
190  mother->Add(objs);
191  }
192 
193  // Bools
194  else if (str == "true")
195  Set<bool>(mother, name.data(), true);
196  else if (str == "false")
197  Set<bool>(mother, name.data(), false);
198 
199  // Integers
200  else if (int i = 0; from_chars(str.data(), &*str.end(), i).ptr == &*str.end())
201  Set<int>(mother, name.data(), i);
202 
203  // Floats
204  else if (float f = strtof(str.data(), &end); end == &*str.end())
205  Set<float>(mother, name.data(), f);
206 
207  // Everything else is a string
208  else Set<string>(mother, name.data(), str);
209 
210  } else if (pt.count("") == pt.size() ||
211  pt.count("item") == pt.size()) {
212  // List
213  for (const auto& [_, subtree]: pt)
214  ConvertPtree(subtree, name.data(), mother);
215  } else {
216  // Subtree
217  auto child = List(mother, name);
218  for (const auto& [subname, subtree]: pt)
219  ConvertPtree(subtree, subname.c_str(), child);
220  }
221 }

◆ Empty() [1/2]

bool Empty ( ) const

Check if list is empty.

163 {
164  return l->GetSize() == 0;
165 }

◆ Empty() [2/2]

bool Empty ( ) const

Check if list is empty.

◆ Find() [1/8]

bool Find ( const char *  key,
Args...  args 
) const
inline

Generic finder.

243  {
244  return Find(l, key, args...);
245  }

◆ Find() [2/8]

bool Find ( const char *  key,
Args...  args 
) const
inline

Generic finder.

243  {
244  return Find(l, key, args...);
245  }

◆ Find() [3/8]

bool Find ( TList *  mother,
const char *  key 
) const
inlineprivate

Check if an element exists in a TList.

110  {
111  CheckKey(key);
112  TObject * obj = mother->FindObject(key);
113  return (obj != nullptr);
114  }

◆ Find() [4/8]

bool Find ( TList *  mother,
const char *  key 
) const
inlineprivate

Check if an element exists in a TList.

110  {
111  CheckKey(key);
112  TObject * obj = mother->FindObject(key);
113  return (obj != nullptr);
114  }

◆ Find() [5/8]

bool Find ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive finder.

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  }

◆ Find() [6/8]

bool Find ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive finder.

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  }

◆ Find() [7/8]

bool Find ( TList *  mother,
std::string  key 
) const
inlineprivate

finder with string

◆ Find() [8/8]

bool Find ( TList *  mother,
std::string  key 
) const
inlineprivate

finder with string

◆ Get() [1/8]

T Get ( const char *  key,
Args...  args 
) const
inline

Generic getter.

236  {
237  return Get<T>(l, key, args...);
238  }

◆ Get() [2/8]

T Get ( const char *  key,
Args...  args 
) const
inline

Generic getter.

236  {
237  return Get<T>(l, key, args...);
238  }

◆ Get() [3/8]

T Get ( TList *  mother,
const char *  key 
) const
inlineprivate

Final getter. The behaviour for arithmetic types and for literal (default) is slightly different, since literals don't really have a key. Literals are always contained in a list, and the last element of the list is returned.

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  }

◆ Get() [4/8]

T Get ( TList *  mother,
const char *  key 
) const
inlineprivate

Final getter. The behaviour for arithmetic types and for literal (default) is slightly different, since literals don't really have a key. Literals are always contained in a list, and the last element of the list is returned.

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  }

◆ Get() [5/8]

T Get ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive getter.

102  {
103  TList * daughter = List(mother, key);
104  return Get<T>(daughter, args...);
105  }

◆ Get() [6/8]

T Get ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive getter.

102  {
103  TList * daughter = List(mother, key);
104  return Get<T>(daughter, args...);
105  }

◆ Get() [7/8]

T Get ( TList *  mother,
std::string  key 
) const
inlineprivate

value getter with string

◆ Get() [8/8]

T Get ( TList *  mother,
std::string  key 
) const
inlineprivate

value getter with string

◆ List() [1/10]

TList* List ( ) const
inline

Access directly to main list.

229  {
230  return l;
231  }

◆ List() [2/10]

TList* List ( ) const
inline

Access directly to main list.

229  {
230  return l;
231  }

◆ List() [3/10]

TList* List ( const char *  key,
Args...  args 
) const
inline

Access directly a sublist.

222  {
223  return List(l, key, args...);
224  }

◆ List() [4/10]

TList* List ( const char *  key,
Args...  args 
) const
inline

Access directly a sublist.

222  {
223  return List(l, key, args...);
224  }

◆ List() [5/10]

TList * List ( TList *  mother,
const char *  key 
) const
private

Accest do daughter list from a mother list.

34 {
35  TObject * obj = mother->FindObject(key);
36  if (obj == nullptr) {
37  auto daughter = static_cast<TList *>(obj);
38  daughter = new TList;
39  daughter->SetName(key);
40  mother->Add(daughter);
41  return daughter;
42  }
43  else {
44  TString classname = obj->ClassName();
45  if (classname != "TList")
46  BOOST_THROW_EXCEPTION(invalid_argument(key));
47  auto daughter = dynamic_cast<TList *>(obj);
48  return daughter;
49  }
50 }

◆ List() [6/10]

TList* List ( TList *  mother,
const char *  key 
) const
private

Accest do daughter list from a mother list.

◆ List() [7/10]

TList* List ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Access a sublist.

181  {
182  TList * daughter = List(mother, key);
183  return List(daughter, args...);
184  }

◆ List() [8/10]

TList* List ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Access a sublist.

181  {
182  TList * daughter = List(mother, key);
183  return List(daughter, args...);
184  }

◆ List() [9/10]

TList* List ( TList *  mother,
std::string  key 
) const
inlineprivate

list getter with string

◆ List() [10/10]

TList* List ( TList *  mother,
std::string  key 
) const
inlineprivate

list getter with string

◆ ls() [1/2]

void ls ( ) const
inline

Prints the content of the TList

290 { if (l != nullptr) l->ls(); }

◆ ls() [2/2]

void ls ( ) const
inline

Prints the content of the TList

290 { if (l != nullptr) l->ls(); }

◆ MkPtree() [1/4]

boost::property_tree::ptree MkPtree ( Format  format = INFO) const
inline

Write main TList to Boost Property Tree.

Returns
content of full TList in Property Tree format
Parameters
formatformat of output file
286  { return MkPtree(l, format); }

◆ MkPtree() [2/4]

boost::property_tree::ptree MkPtree ( Format  format = INFO) const
inline

Write main TList to Boost Property Tree.

Returns
content of full TList in Property Tree format
Parameters
formatformat of output file
286  { return MkPtree(l, format); }

◆ MkPtree() [3/4]

boost::property_tree::ptree MkPtree ( TList *  ,
Format  = INFO 
) const
protected

Write TList to Boost Property Tree. Calls itself recursively. Internally, it uses Boost Preprocessor macros to loop over types.

Returns
content of TList given in argument in Property Tree format

◆ MkPtree() [4/4]

pt::ptree MkPtree ( TList *  list,
Format  format = INFO 
) const
protected

Write TList to Boost Property Tree. Calls itself recursively. Internally, it uses Boost Preprocessor macros to loop over types.

Returns
content of TList given in argument in Property Tree format
Parameters
listlist to write to `ptree`
formatformat of output file (e.g. XML cannot handle empty keys)
53 {
54  pt::ptree tree; // this tree will contain the list
55  for (const auto& obj: *list) {
56  const char * key = obj->GetName();
57  const TString classname = obj->ClassName();
58  if (classname == "TList") {
59  auto sublist = dynamic_cast<TList *>(obj);
60  pt::ptree child = MkPtree(sublist, format);
61  tree.add_child(key, child);
62  }
63  else if (classname == "TObjString") {
64  auto objs = dynamic_cast<TObjString *>(obj);
65  const TString value = objs->GetString();
66  if (value.Length() > 0) {
67  if (list->GetSize() == 1)
68  tree.put<const char *>("", value);
69  else {
70  pt::ptree node;
71  node.put<const char *>("", value);
72  tree.push_back(make_pair("", node));
73  }
74  }
75  }
76 #define ARITHMETIC_TYPES (bool)(char)(signed char)(short)(short int)(int)(long int)(long long int)(unsigned char)(unsigned short)(unsigned short int)(unsigned)(unsigned int)(unsigned long)(unsigned long int)(unsigned long long)(unsigned long long int)(float)(double)(long double)
77 #define ELSE_IF_TPARAM(r, data, TYPE) \
78  else if (classname == "TParameter<" BOOST_PP_STRINGIZE(TYPE) ">") { \
79  auto param = dynamic_cast<TParameter<TYPE>*>(obj); \
80  tree.add<TYPE>(key, param->GetVal()); \
81  }
82  BOOST_PP_SEQ_FOR_EACH(ELSE_IF_TPARAM, _, ARITHMETIC_TYPES)
83 #undef ARITHMETIC_TYPES
84 #undef ELSE_IF_TPARAM
85  else
86  cerr << orange << "Warning: " << classname << " is not handled by " << __func__ << ", therefore `" << key << "` will ignored.\n" << def;
87  }
88  return tree;
89 }

◆ Set() [1/6]

void Set ( const char *  key,
Args...  args 
) const
inline

Generic setter. The key is the name of the first sublist (if any), or to the element directly. An extra argument (implicitly in the variadic argument) contains the value.

252  {
253  Set<T>(l, key, args...);
254  }

◆ Set() [2/6]

void Set ( const char *  key,
Args...  args 
) const
inline

Generic setter. The key is the name of the first sublist (if any), or to the element directly. An extra argument (implicitly in the variadic argument) contains the value.

252  {
253  Set<T>(l, key, args...);
254  }

◆ Set() [3/6]

void Set ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive setter.

172  {
173  CheckKey(key);
174  TList * daughter = List(mother, key);
175  Set<T>(daughter, args...);
176  }

◆ Set() [4/6]

void Set ( TList *  mother,
const char *  key,
Args...  args 
) const
inlineprivate

Recursive setter.

172  {
173  CheckKey(key);
174  TList * daughter = List(mother, key);
175  Set<T>(daughter, args...);
176  }

◆ Set() [5/6]

void Set ( TList *  mother,
const char *  key,
value 
) const
inlineprivate

(Re)sets elements of a given list.

Only arithmetic types have a key and a value. Literals only have identical key and value.

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  }

◆ Set() [6/6]

void Set ( TList *  mother,
const char *  key,
value 
) const
inlineprivate

(Re)sets elements of a given list.

Only arithmetic types have a key and a value. Literals only have identical key and value.

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  }

◆ Write() [1/8]

void Write ( ) const

Write TList to TFile in ROOT format.

156 {
157  TString name = l->GetName();
158  if (name.IsNull()) name = "UserInfo";
159  l->Write(name, TList::kSingleKey);
160 }

◆ Write() [2/8]

void Write ( ) const

Write TList to TFile in ROOT format.

◆ Write() [3/8]

boost::property_tree::ptree Write ( const boost::property_tree::ptree &  ,
std::ostream &  ,
Format  = INFO 
) const
protected

Write a Boost Property Tree to text file format.

It essentially calls pt::write_info, pt::write_json, or pt::write_xml. Minor edits such as the removal of empty strings in INFO format or the the addition of a global tag in XML format are applied.

◆ Write() [4/8]

pt::ptree Write ( const boost::property_tree::ptree &  tree,
std::ostream &  out,
Format  format = INFO 
) const
protected

Write a Boost Property Tree to text file format.

It essentially calls pt::write_info, pt::write_json, or pt::write_xml. Minor edits such as the removal of empty strings in INFO format or the the addition of a global tag in XML format are applied.

Parameters
outoutput stream
formatformat of output
93 {
94  switch (format) {
95  case JSON:
96  {
97  stringstream ss;
98  write_json(ss, tree);
99  string str = ss.str();
100  // by default, everything is a string
101  // -> we remove quotation marks for numerics and booleans
102  regex numbers("\\\"([0-9]+\\.{0,1}[0-9]*)\\\""), // note: no support for hex
103  booleans("\\\"(true|false)\\\"");
104  str = regex_replace(str, numbers, "$1");
105  str = regex_replace(str, booleans, "$1");
106  al::replace_all(str, "\\/", "/");
107  out << str;
108  break;
109  }
110  case XML :
111  {
112  pt::ptree userinfo;
113  userinfo.put_child("userinfo", tree); // there must be exactly one global key
114  pt::xml_writer_settings<string> settings(' ', 2); // indentation with 2 spaces
115  stringstream ss;
116  write_xml(ss, userinfo, settings);
117  string str = ss.str();
118  al::replace_all(str, "<>", "<item>");
119  al::replace_all(str, "</>", "</item>");
120  out << str;
121  break;
122  }
123  case INFO:
124  {
125  stringstream ss;
126  write_info(ss, tree);
127  string str = ss.str();
128  al::erase_all(str, " \"\"");
129  al::erase_all(str, "\"\" ");
130  out << str;
131  }
132  }
133 
134  return tree;
135 }

◆ Write() [5/8]

boost::property_tree::ptree Write ( const std::filesystem::path &  ) const

Write to file with Boost. Calls MkPtree to write sublists.

Returns
content of full TList in Property Tree format

◆ Write() [6/8]

pt::ptree Write ( const std::filesystem::path &  p) const

Write to file with Boost. Calls MkPtree to write sublists.

Returns
content of full TList in Property Tree format
Parameters
ppath to output
144 {
145  auto type = INFO;
146  if (p.extension() == ".json")
147  type = JSON;
148  else if (p.extension() == ".xml")
149  type = XML;
150 
151  ofstream stream(p);
152  return Write(stream, type);
153 }

◆ Write() [7/8]

pt::ptree Write ( std::ostream &  out,
Format  format = INFO 
) const

Write to stream with Boost. Calls MkPtree to write sublists.

Returns
content of full TList in Property Tree format
Parameters
outoutput stream
formatformat of output
138 {
139  pt::ptree tree = MkPtree(format);
140  return Write(tree, out, format);
141 }

◆ Write() [8/8]

boost::property_tree::ptree Write ( std::ostream &  ,
Format  = INFO 
) const

Write to stream with Boost. Calls MkPtree to write sublists.

Returns
content of full TList in Property Tree format

Member Data Documentation

◆ l

TList * l
private

main list (typically from TTree::GetUserInfo())

◆ own

bool own
private

The documentation for this class was generated from the following files:
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
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:101
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
jmarExample.pt
pt
Definition: jmarExample.py:19
Darwin::Tools::UserInfo::XML
@ XML
XML format, another universal format.
Definition: UserInfo.h:61
Step::def
static const char * def
Definition: Step.h:36
Ntupliser_cfg.args
args
Definition: Ntupliser_cfg.py:11
ELSE_IF_TPARAM
#define ELSE_IF_TPARAM(r, data, TYPE)
Darwin::Tools::UserInfo::JSON
@ JSON
JSON format, rather common and nearly as powerful.
Definition: UserInfo.h:60
Ntupliser_cfg.p
p
Definition: Ntupliser_cfg.py:174
Ntupliser_cfg.f
f
Definition: Ntupliser_cfg.py:318
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
orange
static const char * orange
Definition: colours.h:6
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::own
bool own
Definition: UserInfo.h:65
Darwin::Tools::UserInfo::INFO
@ INFO
INFO format, proper to Boost Property Trees (default)
Definition: UserInfo.h:59
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
ARITHMETIC_TYPES
#define ARITHMETIC_TYPES
Darwin::Tools::UserInfo::UserInfo
UserInfo(const char *="UserInfo")
Constructor (starting from scratch)
Definition: UserInfo.cc:167