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