Iguana LATEST
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/DataFileReader.h"
12#include "iguana/services/Deprecated.h"
13#include "iguana/services/GlobalParam.h"
14#include "iguana/services/RCDBReader.h"
15#include "iguana/services/YAMLReader.h"
16
17namespace iguana {
18
20 // ALGORITHM TOOLS
22
24 namespace tools {
34 hipo::banklist::size_type GetBankIndex(
35 hipo::banklist& banks,
36 std::string const& bank_name,
37 unsigned int const& variant = 0) noexcept(false);
38 }
39
41 // BASE CLASS ALGORITHM
43
45 /* NOTE: if you modify this, you also must modify:
46 * - [ ] `PrintOptionValue`
47 * - [ ] Template specializations in this class
48 * - [ ] Template specializations in `YAMLReader` or `ConfigFileReader`, and `ConcurrentParam`
49 * - [ ] Add new tests, if you added new types
50 * - FIXME: adding `bool` type may be tricky, see https://code.jlab.org/hallb/clas12/iguana/-/issues/347
51 */
52 using option_t = std::variant<
53 int,
54 double,
55 std::string,
56 std::vector<int>,
57 std::vector<double>,
58 std::vector<std::string>>;
59
68 class Algorithm : public Object
69 {
70
71 public:
72
74 Algorithm(std::string_view name)
75 : Object(name)
76 , m_rows_only(false)
80 {}
81 virtual ~Algorithm() {}
82
91 virtual void Start(hipo::banklist& banks) final;
92
99 virtual void Start() final;
100
107 virtual bool Run(hipo::banklist& banks) const final;
108
112 virtual void Stop() final;
113
133 template <typename OPTION_TYPE>
134 OPTION_TYPE SetOption(std::string const& key, const OPTION_TYPE val)
135 {
136 // FIXME: this template is not specialized, to be friendlier to python `cppyy` bindings
137 if(key == "log") {
138 if constexpr(std::disjunction<
139 std::is_same<OPTION_TYPE, std::string>,
140 std::is_same<OPTION_TYPE, char const*>,
141 std::is_same<OPTION_TYPE, Logger::Level>>::value)
142 m_log->SetLevel(val);
143 else
144 m_log->Error("Option '{}' must be a string or a Logger::Level", key);
145 }
146 // make sure the key hasn't been renamed or deprecated
147 iguana::deprecated::CheckSetOptionKey(m_class_name, key);
148 // add it to the cache
149 m_option_cache[key] = val;
150 return val;
151 }
152
156 template <typename OPTION_TYPE>
157 OPTION_TYPE GetOptionScalar(YAMLReader::node_path_t node_path = {}) const;
158
162 template <typename OPTION_TYPE>
163 std::vector<OPTION_TYPE> GetOptionVector(YAMLReader::node_path_t node_path = {}) const;
164
168 template <typename OPTION_TYPE>
169 std::set<OPTION_TYPE> GetOptionSet(YAMLReader::node_path_t node_path = {}) const;
170
174 YAML::Node GetOptionNode(YAMLReader::node_path_t node_path) const;
175
178 void SetName(std::string_view name);
179
182 std::string GetAlgorithmDir() const;
183
186 std::unique_ptr<YAMLReader> const& GetConfig() const;
187
190 void SetConfig(std::unique_ptr<YAMLReader>&& yaml_config);
191
195 void SetConfigFile(std::string const& name);
196
200 void SetConfigDirectory(std::string const& name);
201
207 std::string GetModelFile(std::string const& name);
208
215 hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false);
216
222 hipo::banklist::size_type GetCreatedBankIndex(hipo::banklist& banks) const noexcept(false);
223
227 std::vector<std::string> GetCreatedBankNames() const noexcept(false);
228
232 std::string GetCreatedBankName() const noexcept(false);
233
239 hipo::bank GetCreatedBank(std::string const& bank_name = "") const noexcept(false);
240
245 hipo::schema GetCreatedBankSchema(std::string const& bank_name = "") const noexcept(false);
246
249 unsigned int GetCreatedBankVariant() const;
250
252 std::unique_ptr<RCDBReader>& GetRCDBReader();
253
254 protected: // methods
255
258
264 hipo::bank& GetBank(hipo::banklist& banks, hipo::banklist::size_type const idx, std::string const& expected_bank_name = "") const noexcept(false);
265
271 hipo::schema CreateBank(
272 hipo::banklist& banks,
273 hipo::banklist::size_type& bank_idx,
274 std::string const& bank_name) noexcept(false);
275
280 void ShowBanks(hipo::banklist const& banks, std::string_view message = "", Logger::Level const level = Logger::trace) const;
281
286 void ShowBank(hipo::bank const& bank, std::string_view message = "", Logger::Level const level = Logger::trace) const;
287
292 void ThrowSinceRenamed(std::string const& new_name, std::string const& version) const noexcept(false);
293
294 private: // methods
295
299 virtual void ConfigHook() {}
300
304 virtual void StartHook(hipo::banklist& banks) {}
305
308 virtual bool RunHook(hipo::banklist& banks) const { return true; }
309
312 virtual void StopHook() {}
313
315 void ParseYAMLConfig();
316
320 template <typename OPTION_TYPE>
321 std::optional<OPTION_TYPE> GetCachedOption(std::string const& key) const;
322
323 // PrintOptionValue: overloaded for different value types
324 void PrintOptionValue(std::string const& key, int const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
325 void PrintOptionValue(std::string const& key, double const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
326 void PrintOptionValue(std::string const& key, std::string const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
327 void PrintOptionValue(std::string const& key, std::vector<int> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
328 void PrintOptionValue(std::string const& key, std::vector<double> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
329 void PrintOptionValue(std::string const& key, std::vector<std::string> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
330
331 protected: // members
332
334 std::string m_class_name;
335
338
341
345
348 std::string o_user_config_dir;
349
351 mutable std::mutex m_mutex;
352
355 unsigned int m_created_bank_variant{0};
356
358 std::unique_ptr<RCDBReader> m_rcdb;
359
360 private: // members
361
363 std::unique_ptr<YAMLReader> m_yaml_config;
364
366 std::unordered_map<std::string, option_t> m_option_cache;
367
369 std::unique_ptr<DataFileReader> m_datafile_reader;
370 };
371
373 // ALGORITHM FACTORY
375
377 using algo_t = std::unique_ptr<Algorithm>;
378
380 class AlgorithmFactory
381 {
382
383 public:
384
386 using algo_creator_t = std::function<algo_t()>;
387
388 AlgorithmFactory() = delete;
389
395 static bool Register(std::string const& algo_name, algo_creator_t creator, std::vector<std::string> const new_banks = {}) noexcept;
396
400 static algo_t Create(std::string const& algo_name) noexcept(false);
401
405 static std::optional<std::vector<std::string>> GetCreatorAlgorithms(std::string const& bank_name) noexcept;
406
410 static std::optional<std::vector<std::string>> GetCreatedBanks(std::string const& algo_name) noexcept(false);
411
412 private:
413
415 static std::unordered_map<std::string, algo_creator_t> s_creators;
416
418 static std::unordered_map<std::string, std::vector<std::string>> s_bank_to_algos;
419
421 static std::unordered_map<std::string, std::vector<std::string>> s_algo_to_banks;
422 };
423}
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:386
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:69
void SetName(std::string_view name)
std::string GetCreatedBankName() const noexcept(false)
void SetConfigDirectory(std::string const &name)
std::string GetAlgorithmDir() const
virtual bool Run(hipo::banklist &banks) const final
Run Function: Process an event's hipo::banklist
std::unique_ptr< YAMLReader > const & GetConfig() const
void SetConfig(std::unique_ptr< YAMLReader > &&yaml_config)
std::unique_ptr< RCDBReader > m_rcdb
RCDB reader.
Definition Algorithm.h:358
std::string m_class_name
Class name of this algorithm.
Definition Algorithm.h:334
virtual void Stop() final
Stop Function: Finalize this algorithm after all events are processed.
virtual void Start(hipo::banklist &banks) final
Start Function: Initialize this algorithm before any events are processed, with the intent to process...
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
hipo::bank & GetBank(hipo::banklist &banks, hipo::banklist::size_type const idx, std::string const &expected_bank_name="") const noexcept(false)
unsigned int m_created_bank_variant
Definition Algorithm.h:355
std::mutex m_mutex
A mutex for this algorithm.
Definition Algorithm.h:351
hipo::schema GetCreatedBankSchema(std::string const &bank_name="") const noexcept(false)
std::string GetModelFile(std::string const &name)
void SetConfigFile(std::string const &name)
hipo::schema CreateBank(hipo::banklist &banks, hipo::banklist::size_type &bank_idx, std::string const &bank_name) noexcept(false)
std::string o_user_config_dir
Definition Algorithm.h:348
void ShowBank(hipo::bank const &bank, std::string_view message="", Logger::Level const level=Logger::trace) const
unsigned int GetCreatedBankVariant() const
OPTION_TYPE GetOptionScalar(YAMLReader::node_path_t node_path={}) const
void ThrowSinceRenamed(std::string const &new_name, std::string const &version) const noexcept(false)
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Set an option specified by the user.
Definition Algorithm.h:134
std::vector< OPTION_TYPE > GetOptionVector(YAMLReader::node_path_t node_path={}) const
bool m_rows_only
If true, algorithm can only operate on bank rows; Algorithm::GetBank, and therefore Algorithm::Run,...
Definition Algorithm.h:337
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name) const noexcept(false)
std::string o_user_config_file
Definition Algorithm.h:344
std::string m_default_config_file
Default configuration file name.
Definition Algorithm.h:340
std::set< OPTION_TYPE > GetOptionSet(YAMLReader::node_path_t node_path={}) const
hipo::banklist::size_type GetCreatedBankIndex(hipo::banklist &banks) const noexcept(false)
YAML::Node GetOptionNode(YAMLReader::node_path_t node_path) const
virtual void Start() final
Start Function: Initialize this algorithm before any events are processed, with the intent to process...
void StartRCDBReader()
Instantiate the RCDBReader instance for this algorithm.
hipo::bank GetCreatedBank(std::string const &bank_name="") const noexcept(false)
std::unique_ptr< RCDBReader > & GetRCDBReader()
Algorithm(std::string_view name)
Definition Algorithm.h:74
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)
RCDB reader.
Definition RCDBReader.h:32
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
general algorithm tools
Definition Algorithm.h:24
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name, unsigned int const &variant=0) noexcept(false)