Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SettingsHandler.hh
Go to the documentation of this file.
1 ///
2 /// @file SettingsHandler.hh
3 /// @brief Hammer settings manager class
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_SettingsHandler_HH
13 #define HAMMER_SettingsHandler_HH
14 
15 #include <complex>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 #include <type_traits>
22 
23 #include <boost/variant.hpp>
24 
25 #include "Hammer/IndexTypes.hh"
26 #include "Hammer/Tools/Setting.hh"
28 #include "Hammer/Tools/Logging.hh"
29 
30 namespace YAML {
31 
32  class Node;
33  class Emitter;
34 
35 }
36 
37 namespace Hammer {
38 
39  class Log;
40 
41  /// @brief Hammer settings manager class
42  ///
43  /// Stores Hammer options, provides facilities for saving and reading option files,
44  /// provides option query interface. It also provide a repository of other run-wide
45  /// information, such as the bibliography associated to the specific run, the list
46  /// of files being processed
47  ///
48  /// @ingroup Handlers
50 
51  public:
52  /// @name Constructors
53  //@{
54 
55  /// default constructor
57  }
58 
59  SettingsHandler(const SettingsHandler& other) = delete;
60  SettingsHandler& operator=(const SettingsHandler& other) = delete;
61  SettingsHandler(SettingsHandler&& other) = delete;
62  SettingsHandler& operator=(SettingsHandler&& other) = delete;
63 
64  ~SettingsHandler() noexcept = default;
65 
66  //@}
67 
68  protected:
69  /// @brief logging facility
70  /// @return stream to be used for logging
71  Log& getLog() const;
72 
73  public:
74  /// @brief reset all the contents of `SettingsHandler`
75  void reset();
76 
77  /// @name Hammer options
78  //@{
79 
80  /// @brief access the list of registered settings
81  /// @param[in] path the class owning the settings. An empty string returns all the registered settings
82  /// @param[in] group whether it applies to the numerator, denominator or both
83  /// @return a list of setting names
84  std::set<std::string> getSettings(const std::string& path = "", WTerm group = WTerm::COMMON) const;
85 
86  /// @brief access a specific setting value
87  /// @param[in] path the class owning the settings
88  /// @param[in] name the name of the setting
89  /// @param[in] group whether it applies to the numerator, denominator or both
90  /// @return a pointer to the value, `nullptr` if the setting is not found or the type does not match
91  template <typename T>
92  T* getNamedSettingValue(const std::string& path, const std::string& name, WTerm group = WTerm::COMMON);
93 
94  /// @brief access a specific setting value
95  /// @param[in] fullName the full name of the setting in the "<path>:<name>" format
96  /// @param[in] group whether it applies to the numerator, denominator or both
97  /// @return a pointer to the value, `nullptr` if the setting is not found or the type does not match
98  template <typename T>
99  T* getNamedSettingValue(const std::string& fullName, WTerm group = WTerm::COMMON);
100 
101  /// @brief add a setting to the store
102  /// @param[in] path the class owning the settings
103  /// @param[in] name the name of the setting
104  /// @param[in] value the value (and default value) of the setting. If setting is already present only
105  /// @param[in] group whether it applies to the numerator, denominator or both
106  /// default value is changed
107  /// @return the added setting
108  template <typename T>
109  Setting* addSetting(const std::string& path, const std::string& name, const T& value, WTerm group = WTerm::COMMON);
110 
111  /// @brief add a setting to the store
112  /// @param[in] fullName the full name of the setting in the "<path>:<name>" format
113  /// @param[in] value the value (and default value) of the setting
114  /// @param[in] group whether it applies to the numerator, denominator or both
115  /// @return the added setting
116  template <typename T>
117  Setting* addSetting(const std::string& fullName, const T& value, WTerm group = WTerm::COMMON);
118 
119  Setting* cloneSetting(const std::string& path, const std::string& name, const Setting& value, WTerm group = WTerm::COMMON);
120 
121  Setting* cloneSetting(const std::string& fullName, const Setting& value, WTerm group = WTerm::COMMON);
122 
123  Setting* resetSetting(const std::string& path, const std::string& name, WTerm group = WTerm::COMMON);
124 
125  Setting* resetSetting(const std::string& fullName, WTerm group = WTerm::COMMON);
126 
127  Setting* getEntry(const std::string& path, const std::string& name, WTerm group);
128 
129  /// @brief
130  /// @param[in] path
131  /// @param[in] name
132  /// @param[in] group whether it applies to the numerator, denominator or both
133  void removeSetting(const std::string& path, const std::string& name, WTerm group = WTerm::COMMON);
134 
135  /// @brief
136  /// @param[in] fullName
137  /// @param[in] group whether it applies to the numerator, denominator or both
138  void removeSetting(const std::string& fullName, WTerm group = WTerm::COMMON);
139 
140  /// @brief change a setting value
141  /// @param[in] path the class owning the settings
142  /// @param[in] name the name of the setting
143  /// @param[in] value the new value of the setting
144  /// @param[in] group whether it applies to the numerator, denominator or both
145  /// @return the changed setting, `nullptr` if the setting was not found
146  template <typename T>
147  Setting* changeSetting(const std::string& path, const std::string& name, const T& value, WTerm group = WTerm::COMMON);
148 
149  /// @brief change a setting value
150  /// @param[in] fullName the full name of the setting in the "<path>:<name>" format
151  /// @param[in] value the new value of the setting
152  /// @param[in] group whether it applies to the numerator, denominator or both
153  /// @return the changed setting, `nullptr` if the setting was not found
154  template <typename T>
155  Setting* changeSetting(const std::string& fullName, const T& value, WTerm group = WTerm::COMMON);
156 
157  /// @brief read Hammer settings from a file
158  /// @param[in] fileName the file name
159  void readSettings(const std::string& fileName);
160 
161  /// @brief read Hammer settings from a string
162  /// @param[in] data the settings
163  void parseSettings(const std::string& data);
164 
165  /// @brief write current Hammer settings to a file
166  /// @param[in] name the file name
167  /// @param[in] useDefault the file name
168  void saveSettings(const std::string& name, bool useDefault = true) const;
169 
170  //@}
171 
172  void write(flatbuffers::FlatBufferBuilder* msgwriter, std::vector<flatbuffers::Offset<Serial::FBSetting>>* msgs) const;
173 
174  bool read(const Serial::FBHeader* msgreader, bool merge);
175 
176  public:
177 
178  /// @name Bibliography
179  //@{
180 
181  /// writes a BibTeX file containing the bibliography associated to current run
182  /// @param[in] filename the name of the file
183  void saveReferences(const std::string& filename) const;
184 
185  /// check a bibliographic reference to the current run
186  /// @param[in] bibkey the BibTeX key
187  bool checkReference(const std::string& bibkey);
188 
189  /// add a bibliographic reference to the current run
190  /// @param[in] bibkey the BibTeX key
191  /// @param[in] bibtex the body of the citation in BibTeX format
192  void addReference(const std::string& bibkey, const std::string& bibtex);
193 
194  /// empties current run bibliography
195  void clearReferences();
196 
197  //@}
198 
199  private:
200  /// @brief
201  /// @param[in] setting
202  /// @param[in] path
203  /// @param[in] name
204  /// @param[in] group whether it applies to the numerator, denominator or both
205  void processSetting(const YAML::Node& setting, const std::string& path, const std::string& name, WTerm group = WTerm::COMMON);
206 
207  /// @brief
208  /// @param[in] input
209  /// @param[in] group whether it applies to the numerator, denominator or both
210  void processSettings(const YAML::Node& input, WTerm group = WTerm::COMMON);
211 
212  std::string groupToPrefix(WTerm option) const;
213 
214  WTerm prefixToGroup(const std::string& option) const;
215 
216  void writeSetting(YAML::Emitter& emitter, const Setting& val, bool useDefault) const;
217 
218  bool isMassWidth(const std::string& name) const;
219  void processMassWidth(const std::string& path, const std::string& name, double value) const;
220 
221  std::map<std::string, std::map<std::string, const Setting*>> getEntriesByGroup(WTerm group) const;
222 
223  std::string buildName(const std::string& path, const std::string& name, WTerm group) const;
224 
225  std::tuple<std::string, std::string, WTerm> parseName(const std::string& fullName) const;
226 
227  private:
228 
229  template<typename T>
230  using NamedDict = std::map<std::string, T>;
231 
232  /// the Hammer options dictionary
234 
235  std::map<std::string, std::string> _references;
236 
237  };
238 
239 } // namespace Hammer
240 
241 #include "Hammer/SettingsHandlerDefs.hh"
242 
243 #endif
std::map< std::string, std::string > _references
std::map< std::string, std::map< std::string, const Setting * > > getEntriesByGroup(WTerm group) const
Forward declaration of serialization related typedefs and includes.
void saveSettings(const std::string &name, bool useDefault=true) const
write current Hammer settings to a file
Setting * changeSetting(const std::string &path, const std::string &name, const T &value, WTerm group=WTerm::COMMON)
change a setting value
Hammer data types declarations.
void parseSettings(const std::string &data)
read Hammer settings from a string
Log & getLog() const
logging facility
T * getNamedSettingValue(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
access a specific setting value
Setting * cloneSetting(const std::string &path, const std::string &name, const Setting &value, WTerm group=WTerm::COMMON)
void processSetting(const YAML::Node &setting, const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
Setting * resetSetting(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
void write(flatbuffers::FlatBufferBuilder *msgwriter, std::vector< flatbuffers::Offset< Serial::FBSetting >> *msgs) const
SettingsHandler & operator=(const SettingsHandler &other)=delete
Message logging routines.
SettingsHandler()
default constructor
void writeSetting(YAML::Emitter &emitter, const Setting &val, bool useDefault) const
void reset()
reset all the contents of SettingsHandler
bool checkReference(const std::string &bibkey)
check a bibliographic reference to the current run
void processSettings(const YAML::Node &input, WTerm group=WTerm::COMMON)
void addReference(const std::string &bibkey, const std::string &bibtex)
add a bibliographic reference to the current run
bool read(const Serial::FBHeader *msgreader, bool merge)
Setting * getEntry(const std::string &path, const std::string &name, WTerm group)
std::map< WTerm, NamedDict< NamedDict< Setting > > > _settings
the Hammer options dictionary
void clearReferences()
empties current run bibliography
~SettingsHandler() noexcept=default
Logging class.
Definition: Logging.hh:33
container for an Hammer run option
Definition: Setting.hh:39
std::tuple< std::string, std::string, WTerm > parseName(const std::string &fullName) const
void processMassWidth(const std::string &path, const std::string &name, double value) const
bool isMassWidth(const std::string &name) const
std::map< std::string, T > NamedDict
void readSettings(const std::string &fileName)
read Hammer settings from a file
void removeSetting(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
Hammer setting class.
Hammer settings manager class.
void saveReferences(const std::string &filename) const
writes a BibTeX file containing the bibliography associated to current run
Setting * addSetting(const std::string &path, const std::string &name, const T &value, WTerm group=WTerm::COMMON)
add a setting to the store
WTerm prefixToGroup(const std::string &option) const
std::string groupToPrefix(WTerm option) const
std::set< std::string > getSettings(const std::string &path="", WTerm group=WTerm::COMMON) const
access the list of registered settings
std::string buildName(const std::string &path, const std::string &name, WTerm group) const