Iguana 1.2.1
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" // IWYU pragma: export
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
146 template <typename OPTION_TYPE>
147 OPTION_TYPE SetOption(std::string const& key, const OPTION_TYPE val)
148 {
149 // FIXME: this template is not specialized, to be friendlier to python `cppyy` bindings
150 if(key == "log") {
151 if constexpr(std::disjunction<
152 std::is_same<OPTION_TYPE, std::string>,
153 std::is_same<OPTION_TYPE, char const*>,
154 std::is_same<OPTION_TYPE, Logger::Level>>::value)
155 m_log->SetLevel(val);
156 else
157 m_log->Error("Option '{}' must be a string or a Logger::Level", key);
158 }
159 // make sure the key hasn't been renamed or deprecated
160 iguana::deprecated::CheckSetOptionKey(m_class_name, key);
161 // add it to the cache
162 m_option_cache[key] = val;
163 return val;
164 }
165
169 template <typename OPTION_TYPE>
170 OPTION_TYPE GetOptionScalar(YAMLReader::node_path_t node_path = {}) const;
171
175 template <typename OPTION_TYPE>
176 std::vector<OPTION_TYPE> GetOptionVector(YAMLReader::node_path_t node_path = {}) const;
177
181 template <typename OPTION_TYPE>
182 std::set<OPTION_TYPE> GetOptionSet(YAMLReader::node_path_t node_path = {}) const;
183
187 YAML::Node GetOptionNode(YAMLReader::node_path_t node_path) const;
188
191 void SetName(std::string_view name);
192
195 std::string GetAlgorithmDir() const;
196
199 std::unique_ptr<YAMLReader> const& GetConfig() const;
200
203 void SetConfig(std::unique_ptr<YAMLReader>&& yaml_config);
204
208 void SetConfigFile(std::string const& name);
209
213 void SetConfigDirectory(std::string const& name);
214
220 std::string GetModelFile(std::string const& name);
221
228 hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false);
229
235 hipo::banklist::size_type GetCreatedBankIndex(hipo::banklist& banks) const noexcept(false);
236
240 std::vector<std::string> GetCreatedBankNames() const noexcept(false);
241
245 std::string GetCreatedBankName() const noexcept(false);
246
252 hipo::bank GetCreatedBank(std::string const& bank_name = "") const noexcept(false);
253
258 hipo::schema GetCreatedBankSchema(std::string const& bank_name = "") const noexcept(false);
259
262 unsigned int GetCreatedBankVariant() const;
263
265 std::unique_ptr<RCDBReader>& GetRCDBReader();
266
267 protected: // methods
268
271
277 hipo::bank& GetBank(hipo::banklist& banks, hipo::banklist::size_type const idx, std::string const& expected_bank_name = "") const noexcept(false);
278
284 hipo::schema CreateBank(
285 hipo::banklist& banks,
286 hipo::banklist::size_type& bank_idx,
287 std::string const& bank_name) noexcept(false);
288
293 void ShowBanks(hipo::banklist const& banks, std::string_view message = "", Logger::Level const level = Logger::trace) const;
294
299 void ShowBank(hipo::bank const& bank, std::string_view message = "", Logger::Level const level = Logger::trace) const;
300
305 void ThrowSinceRenamed(std::string const& new_name, std::string const& version) const noexcept(false);
306
307 private: // methods
308
312 virtual void ConfigHook() {}
313
317 virtual void StartHook(hipo::banklist& banks) {}
318
321 virtual bool RunHook(hipo::banklist& banks) const { return true; }
322
325 virtual void StopHook() {}
326
328 void ParseYAMLConfig();
329
333 template <typename OPTION_TYPE>
334 std::optional<OPTION_TYPE> GetCachedOption(std::string const& key) const;
335
336 // PrintOptionValue: overloaded for different value types
337 void PrintOptionValue(std::string const& key, int const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
338 void PrintOptionValue(std::string const& key, double const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
339 void PrintOptionValue(std::string const& key, std::string const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
340 void PrintOptionValue(std::string const& key, std::vector<int> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
341 void PrintOptionValue(std::string const& key, std::vector<double> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
342 void PrintOptionValue(std::string const& key, std::vector<std::string> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
343
344 protected: // members
345
347 std::string m_class_name;
348
351
354
358
361 std::string o_user_config_dir;
362
364 mutable std::mutex m_mutex;
365
368 unsigned int m_created_bank_variant{0};
369
371 std::unique_ptr<RCDBReader> m_rcdb;
372
373 private: // members
374
376 std::unique_ptr<YAMLReader> m_yaml_config;
377
379 std::unordered_map<std::string, option_t> m_option_cache;
380
382 std::unique_ptr<DataFileReader> m_datafile_reader;
383 };
384
386 // ALGORITHM FACTORY
388
390 using algo_t = std::unique_ptr<Algorithm>;
391
393 class AlgorithmFactory
394 {
395
396 public:
397
399 using algo_creator_t = std::function<algo_t()>;
400
401 AlgorithmFactory() = delete;
402
408 static bool Register(std::string const& algo_name, algo_creator_t creator, std::vector<std::string> const new_banks = {}) noexcept;
409
413 static algo_t Create(std::string const& algo_name) noexcept(false);
414
418 static std::optional<std::vector<std::string>> GetCreatorAlgorithms(std::string const& bank_name) noexcept;
419
423 static std::optional<std::vector<std::string>> GetCreatedBanks(std::string const& algo_name) noexcept(false);
424
425 private:
426
428 static std::unordered_map<std::string, algo_creator_t> s_creators;
429
431 static std::unordered_map<std::string, std::vector<std::string>> s_bank_to_algos;
432
434 static std::unordered_map<std::string, std::vector<std::string>> s_algo_to_banks;
435 };
436}
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:399
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:371
std::string m_class_name
Class name of this algorithm.
Definition Algorithm.h:347
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:368
std::mutex m_mutex
A mutex for this algorithm.
Definition Algorithm.h:364
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:361
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:147
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:350
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:357
std::string m_default_config_file
Default configuration file name.
Definition Algorithm.h:353
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:19
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)