GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TUserSettings.h
Go to the documentation of this file.
1#ifndef TUSERSETTINGS_H
2#define TUSERSETTINGS_H
3
4#include <iostream>
5#include <map>
6#include <string>
7#include <vector>
8#include <stdexcept>
9
10#include "TNamed.h"
11#include "TCollection.h"
12
13/** \addtogroup Sorting
14 * @{
15 */
16
17/////////////////////////////////////////////////////////////
18///
19/// \class TUserSettings
20///
21/// The TUserSettings class defines user settings that can be
22/// read from text or root files. It stores them in separate
23/// maps for booleans, integers, floats, and strings as well
24/// as vectors of those types.
25///
26/// For helpers the reading of the user settings is done in
27/// the TGRSIHelper class and made available as the member
28/// fUserSettings. Code to read the user setting "MyDouble"
29/// into the member variable `fMyMemberVariable` in the helper's
30/// constructor could be:
31/// ```
32/// if(fUserSettings != nullptr) {
33/// fMyMemberVariable = fUserSettings->GetDouble("MyDouble", 1.);
34/// }
35/// ```
36/// See the AngularCorrelationHelper's header file for more.
37///
38/// In general the user settings can be accessed via the
39/// static function `TUserSettings* TGRSIOptions::UserSettings()`.
40///
41/// If TGRSIOptions are not available, the user settings can
42/// be read using the constructor:
43/// ```
44/// userSettings = new TUserSettings(filename);
45/// ```
46/// or using the ReadSettings function:
47/// ```
48/// userSettings->ReadSettings(filename);
49/// ```
50/// where userSettings is of type TUserSettings* and filename is
51/// either a std::string or char*.
52///
53/// The settings file is expected to have the format
54/// parameter name: value
55/// where parameter name is a string w/o whitespace and value is the value of the parameter
56/// the type of the value (bool, int, double, std::string, or vectors of these types) will be determined automatically.
57/// Vectors are comma separated values of one type.
58/// The default type is std::string.
59/// Any lines starting with '#' or '//' or without a colon will be ignored.
60/// See "examples/AngularCorrelationSettings.par" for an example of a settings file.
61///
62/////////////////////////////////////////////////////////////
63
64class TUserSettings : public TNamed {
65public:
66 TUserSettings() = default;
67 explicit TUserSettings(const std::string& settingsFile)
68 {
69 if(!ReadSettings(settingsFile)) { throw std::runtime_error("Failed to read user settings file!"); }
70 }
71
72 TUserSettings(const TUserSettings&) = default;
73 TUserSettings(TUserSettings&&) noexcept = default;
74 TUserSettings& operator=(const TUserSettings&) = default;
75 TUserSettings& operator=(TUserSettings&&) noexcept = default;
76 ~TUserSettings() = default;
77
78 bool ReadSettings(const std::string& settingsFile);
79
80 bool empty() { return fBool.empty() && fInt.empty() && fDouble.empty() && fString.empty() && fBoolVector.empty() && fIntVector.empty() && fDoubleVector.empty() && fStringVector.empty(); }
81
82 // getter functions
83 template <typename T>
84 T Get(const std::string& parameter) const
85 {
86 if(std::is_same<T, bool>::value) { return GetBool(parameter); }
87 if(std::is_same<T, int>::value) { return GetInt(parameter); }
88 if(std::is_same<T, double>::value) { return GetDouble(parameter); }
89 if(std::is_same<T, std::string>::value) { return GetString(parameter); }
90 if(std::is_same<T, std::vector<bool>>::value) { return GetBoolVector(parameter); }
91 if(std::is_same<T, std::vector<int>>::value) { return GetIntVector(parameter); }
92 if(std::is_same<T, std::vector<double>>::value) { return GetDoubleVector(parameter); }
93 if(std::is_same<T, std::vector<std::string>>::value) { return GetStringVector(parameter); }
94 throw std::runtime_error("Unknown type, only bool, int, double, std::string or vectors of those types allowed");
95 }
96
97 bool GetBool(const std::string& parameter, bool quiet = false) const;
98 int GetInt(const std::string& parameter, bool quiet = false) const;
99 double GetDouble(const std::string& parameter, bool quiet = false) const;
100 std::string GetString(const std::string& parameter, bool quiet = false) const;
101 std::vector<bool> GetBoolVector(const std::string& parameter, bool quiet = false) const;
102 std::vector<int> GetIntVector(const std::string& parameter, bool quiet = false) const;
103 std::vector<double> GetDoubleVector(const std::string& parameter, bool quiet = false) const;
104 std::vector<std::string> GetStringVector(const std::string& parameter, bool quiet = false) const;
105
106 // getter functions with default value
107 // can't do this for GetBool as the default bool would clash with the signature with the "quiet" bool
108 int GetInt(const std::string& parameter, int def) const
109 {
110 try {
111 return fInt.at(parameter);
112 } catch(std::out_of_range& e) {
113 return def;
114 }
115 }
116 double GetDouble(const std::string& parameter, double def) const
117 {
118 try {
119 return fDouble.at(parameter);
120 } catch(std::out_of_range& e) {
121 return def;
122 }
123 }
124 std::string GetString(const std::string& parameter, std::string def) const
125 {
126 try {
127 return fString.at(parameter);
128 } catch(std::out_of_range& e) {
129 return def;
130 }
131 }
132
133 // setter functions
134 void SetBool(const std::string& parameter, bool value) { fBool[parameter] = value; }
135 void SetInt(const std::string& parameter, int value) { fInt[parameter] = value; }
136 void SetDouble(const std::string& parameter, double value) { fDouble[parameter] = value; }
137 void SetString(const std::string& parameter, const std::string& value) { fString[parameter] = value; }
138 void SetBoolVector(const std::string& parameter, const std::vector<bool>& value) { fBoolVector[parameter] = value; }
139 void SetIntVector(const std::string& parameter, const std::vector<int>& value) { fIntVector[parameter] = value; }
140 void SetDoubleVector(const std::string& parameter, const std::vector<double>& value) { fDoubleVector[parameter] = value; }
141 void SetStringVector(const std::string& parameter, const std::vector<std::string>& value) { fStringVector[parameter] = value; }
142
143 void Print(Option_t* opt = "") const override;
144 void Clear(Option_t* = "") override
145 {
146 fBool.clear();
147 fInt.clear();
148 fDouble.clear();
149 fString.clear();
150 fBoolVector.clear();
151 fIntVector.clear();
152 fDoubleVector.clear();
153 fStringVector.clear();
154 SetName("");
155 }
156
157 Long64_t Merge(TCollection* list, Option_t* = "");
158
159private:
160 using TObject::Compare;
161 bool Compare(const TUserSettings* settings) const;
162
163 void ParseValue(const std::string& name, const std::string& value, bool vector);
164
165 std::map<std::string, bool> fBool;
166 std::map<std::string, int> fInt;
167 std::map<std::string, double> fDouble;
168 std::map<std::string, std::string> fString;
169 std::map<std::string, std::vector<bool>> fBoolVector;
170 std::map<std::string, std::vector<int>> fIntVector;
171 std::map<std::string, std::vector<double>> fDoubleVector;
172 std::map<std::string, std::vector<std::string>> fStringVector;
173
174 std::vector<std::string> fSettingsFiles;
175
176 /// \cond CLASSIMP
177 ClassDefOverride(TUserSettings, 5) // NOLINT(readability-else-after-return)
178 /// \endcond
179};
180/*! @} */
181#endif
void Print(Option_t *opt="") const override
TUserSettings(TUserSettings &&) noexcept=default
std::map< std::string, double > fDouble
void SetString(const std::string &parameter, const std::string &value)
void Clear(Option_t *="") override
bool Compare(const TUserSettings *settings) const
std::string GetString(const std::string &parameter, std::string def) const
std::map< std::string, std::vector< bool > > fBoolVector
std::vector< double > GetDoubleVector(const std::string &parameter, bool quiet=false) const
std::string GetString(const std::string &parameter, bool quiet=false) const
std::vector< std::string > GetStringVector(const std::string &parameter, bool quiet=false) const
std::map< std::string, int > fInt
bool GetBool(const std::string &parameter, bool quiet=false) const
std::vector< bool > GetBoolVector(const std::string &parameter, bool quiet=false) const
void SetInt(const std::string &parameter, int value)
void SetStringVector(const std::string &parameter, const std::vector< std::string > &value)
void SetDouble(const std::string &parameter, double value)
std::vector< int > GetIntVector(const std::string &parameter, bool quiet=false) const
std::map< std::string, std::vector< std::string > > fStringVector
std::map< std::string, bool > fBool
std::vector< std::string > fSettingsFiles
void SetDoubleVector(const std::string &parameter, const std::vector< double > &value)
void SetBool(const std::string &parameter, bool value)
std::map< std::string, std::vector< int > > fIntVector
TUserSettings(const std::string &settingsFile)
void SetIntVector(const std::string &parameter, const std::vector< int > &value)
int GetInt(const std::string &parameter, int def) const
std::map< std::string, std::vector< double > > fDoubleVector
TUserSettings(const TUserSettings &)=default
int GetInt(const std::string &parameter, bool quiet=false) const
std::map< std::string, std::string > fString
double GetDouble(const std::string &parameter, bool quiet=false) const
TUserSettings()=default
bool ReadSettings(const std::string &settingsFile)
Long64_t Merge(TCollection *list, Option_t *="")
T Get(const std::string &parameter) const
double GetDouble(const std::string &parameter, double def) const
void ParseValue(const std::string &name, const std::string &value, bool vector)
void SetBoolVector(const std::string &parameter, const std::vector< bool > &value)