Iguana 1.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
Algorithm.h
1#pragma once
2
3#include <mutex>
4#include <optional>
5#include <set>
6
7#include <hipo4/bank.h>
8
10#include "iguana/bankdefs/BankDefs.h"
11#include "iguana/services/YAMLReader.h"
12#include <iguana/services/GlobalParam.h>
13
14namespace iguana {
15
17 /* NOTE: if you modify this, you also must modify:
18 * - [ ] `PrintOptionValue`
19 * - [ ] Template specializations in this class
20 * - [ ] Template specializations in `YAMLReader` or `ConfigFileReader`, and `ConcurrentParam`
21 * - [ ] Add new tests, if you added new types
22 * - FIXME: adding `bool` type may be tricky, see https://github.com/JeffersonLab/iguana/issues/347
23 */
24 using option_t = std::variant<
25 int,
26 double,
27 std::string,
28 std::vector<int>,
29 std::vector<double>,
30 std::vector<std::string>>;
31
40 class Algorithm : public Object
41 {
42
43 public:
44
46 Algorithm(std::string_view name)
47 : Object(name)
48 , m_rows_only(false)
52 {}
53 virtual ~Algorithm() {}
54
60 virtual void Start(hipo::banklist& banks) = 0;
61
65 void Start();
66
73 virtual bool Run(hipo::banklist& banks) const = 0;
74
76 virtual void Stop() = 0;
77
83 template <typename OPTION_TYPE>
84 OPTION_TYPE SetOption(std::string const& key, const OPTION_TYPE val)
85 {
86 // FIXME: this template is not specialized, to be friendlier to python `cppyy` bindings
87 if(key == "log") {
88 if constexpr(std::disjunction<
89 std::is_same<OPTION_TYPE, std::string>,
90 std::is_same<OPTION_TYPE, char const*>,
91 std::is_same<OPTION_TYPE, Logger::Level>>::value)
92 m_log->SetLevel(val);
93 else
94 m_log->Error("Option '{}' must be a string or a Logger::Level", key);
95 }
96 m_option_cache[key] = val;
97 return val;
98 }
99
104 template <typename OPTION_TYPE>
105 OPTION_TYPE GetOptionScalar(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
106
111 template <typename OPTION_TYPE>
112 std::vector<OPTION_TYPE> GetOptionVector(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
113
118 template <typename OPTION_TYPE>
119 std::set<OPTION_TYPE> GetOptionSet(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
120
123 void SetName(std::string_view name);
124
127 std::unique_ptr<YAMLReader> const& GetConfig() const;
128
131 void SetConfig(std::unique_ptr<YAMLReader>&& yaml_config);
132
136 void SetConfigFile(std::string const& name);
137
141 void SetConfigDirectory(std::string const& name);
142
146 std::vector<std::string> GetCreatedBankNames() const noexcept(false);
147
151 std::string GetCreatedBankName() const noexcept(false);
152
158 hipo::bank GetCreatedBank(std::string const& bank_name = "") const noexcept(false);
159
164 hipo::schema GetCreatedBankSchema(std::string const& bank_name = "") const noexcept(false);
165
166 protected: // methods
167
170
176 hipo::bank& GetBank(hipo::banklist& banks, hipo::banklist::size_type const idx, std::string const& expected_bank_name = "") const noexcept(false);
177
182 hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false);
183
189 hipo::schema CreateBank(
190 hipo::banklist& banks,
191 hipo::banklist::size_type& bank_idx,
192 std::string const& bank_name) const noexcept(false);
193
198 void ShowBanks(hipo::banklist const& banks, std::string_view message = "", Logger::Level const level = Logger::trace) const;
199
204 void ShowBank(hipo::bank const& bank, std::string_view message = "", Logger::Level const level = Logger::trace) const;
205
209 template <typename OPTION_TYPE>
210 std::optional<OPTION_TYPE> GetCachedOption(std::string const& key) const;
211
216 void ThrowSinceRenamed(std::string const& new_name, std::string const& version) const noexcept(false);
217
218 private: // methods
219
223 void CompleteOptionNodePath(std::string const& key, YAMLReader::node_path_t& node_path) const;
224
225 // PrintOptionValue: overloaded for different value types
226 void PrintOptionValue(std::string const& key, int const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
227 void PrintOptionValue(std::string const& key, double const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
228 void PrintOptionValue(std::string const& key, std::string const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
229 void PrintOptionValue(std::string const& key, std::vector<int> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
230 void PrintOptionValue(std::string const& key, std::vector<double> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
231 void PrintOptionValue(std::string const& key, std::vector<std::string> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
232
233 protected: // members
234
236 std::string m_class_name;
237
240
243
247
250 std::string o_user_config_dir;
251
253 mutable std::mutex m_mutex;
254
255 private: // members
256
258 std::unique_ptr<YAMLReader> m_yaml_config;
259
261 std::unordered_map<std::string, option_t> m_option_cache;
262 };
263
265
267 using algo_t = std::unique_ptr<Algorithm>;
268
270 class AlgorithmFactory
271 {
272
273 public:
274
276 using algo_creator_t = std::function<algo_t()>;
277
278 AlgorithmFactory() = delete;
279
285 static bool Register(std::string const& algo_name, algo_creator_t creator, std::vector<std::string> const new_banks = {}) noexcept;
286
290 static algo_t Create(std::string const& algo_name) noexcept(false);
291
295 static std::optional<std::vector<std::string>> GetCreatorAlgorithms(std::string const& bank_name) noexcept;
296
300 static std::optional<std::vector<std::string>> GetCreatedBanks(std::string const& algo_name) noexcept(false);
301
302 private:
303
305 static std::unordered_map<std::string, algo_creator_t> s_creators;
306
308 static std::unordered_map<std::string, std::vector<std::string>> s_bank_to_algos;
309
311 static std::unordered_map<std::string, std::vector<std::string>> s_algo_to_banks;
312 };
313}
Preprocessor macros to generate standardized algorithm boilerplate code.
static std::optional< std::vector< std::string > > GetCreatedBanks(std::string const &algo_name) noexcept(false)
std::function< algo_t()> algo_creator_t
Algorithm creator function type.
Definition Algorithm.h:276
static std::optional< std::vector< std::string > > GetCreatorAlgorithms(std::string const &bank_name) noexcept
static bool Register(std::string const &algo_name, algo_creator_t creator, std::vector< std::string > const new_banks={}) noexcept
static algo_t Create(std::string const &algo_name) noexcept(false)
Base class for all algorithms to inherit from.
Definition Algorithm.h:41
void SetName(std::string_view name)
std::string GetCreatedBankName() const noexcept(false)
void SetConfigDirectory(std::string const &name)
std::optional< OPTION_TYPE > GetCachedOption(std::string const &key) const
virtual void Start(hipo::banklist &banks)=0
Initialize this algorithm before any events are processed, with the intent to process banks.
std::set< OPTION_TYPE > GetOptionSet(std::string const &key, YAMLReader::node_path_t node_path={}) const
std::unique_ptr< YAMLReader > const & GetConfig() const
void SetConfig(std::unique_ptr< YAMLReader > &&yaml_config)
std::string m_class_name
Class name of this algorithm.
Definition Algorithm.h:236
OPTION_TYPE GetOptionScalar(std::string const &key, YAMLReader::node_path_t node_path={}) const
std::vector< std::string > GetCreatedBankNames() const noexcept(false)
void ShowBanks(hipo::banklist const &banks, std::string_view message="", Logger::Level const level=Logger::trace) const
std::vector< OPTION_TYPE > GetOptionVector(std::string const &key, YAMLReader::node_path_t node_path={}) const
hipo::bank & GetBank(hipo::banklist &banks, hipo::banklist::size_type const idx, std::string const &expected_bank_name="") const noexcept(false)
std::mutex m_mutex
A mutex for this algorithm.
Definition Algorithm.h:253
hipo::schema GetCreatedBankSchema(std::string const &bank_name="") const noexcept(false)
void Start()
Initialize this algorithm before any events are processed, with the intent to process bank rows rathe...
void SetConfigFile(std::string const &name)
void ParseYAMLConfig()
Parse YAML configuration files. Sets m_yaml_config.
std::string o_user_config_dir
Definition Algorithm.h:250
void ShowBank(hipo::bank const &bank, std::string_view message="", Logger::Level const level=Logger::trace) const
void ThrowSinceRenamed(std::string const &new_name, std::string const &version) const noexcept(false)
virtual void Stop()=0
Finalize this algorithm after all events are processed.
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Definition Algorithm.h:84
bool m_rows_only
If true, algorithm can only operate on bank rows; Algorithm::GetBank, and therefore Algorithm::Run,...
Definition Algorithm.h:239
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name) const noexcept(false)
hipo::schema CreateBank(hipo::banklist &banks, hipo::banklist::size_type &bank_idx, std::string const &bank_name) const noexcept(false)
std::string o_user_config_file
Definition Algorithm.h:246
std::string m_default_config_file
Default configuration file name.
Definition Algorithm.h:242
hipo::bank GetCreatedBank(std::string const &bank_name="") const noexcept(false)
Algorithm(std::string_view name)
Definition Algorithm.h:46
virtual bool Run(hipo::banklist &banks) const =0
Run Function: Process an event's hipo::banklist
Simple logger service.
Definition Logger.h:18
std::unique_ptr< Logger > m_log
Logger instance for this object
Definition Object.h:52
Object(std::string_view name="", Logger::Level lev=Logger::DEFAULT_LEVEL)
A YAMLReader based on yaml-cpp.
Definition YAMLReader.h:15
std::deque< node_id_t > node_path_t
Representation of a path of YAML::Nodes in a YAML::Node tree, e.g., in a YAML file.
Definition YAMLReader.h:28