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