Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat+build: improve algorithm configuration; colored log output; improve dependency resolution #20

Merged
merged 34 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c14c2e3
feat: streamline configuration options
c-dilks Nov 22, 2023
b90335a
feat: set option from executable
c-dilks Nov 22, 2023
b0659e5
fix: make it work
c-dilks Nov 22, 2023
f03109a
refactor: try to typecast bad options, or fallback to default
c-dilks Nov 27, 2023
2a377e2
fix: don't typecast, just complain
c-dilks Nov 28, 2023
b49a6bd
test: `int` and `double` types
c-dilks Nov 28, 2023
4135753
feat: user set algorithm log level
c-dilks Nov 28, 2023
3121cef
feat: colored errors and warnings
c-dilks Nov 28, 2023
e5663a9
fix(ci): use a newer `fmt`
c-dilks Nov 28, 2023
2808ba2
fix: use `realpath` for `fmt`
c-dilks Nov 28, 2023
7b3751c
fix: `cmake` method for `hipo4`
c-dilks Nov 28, 2023
70e5816
test: syntax
c-dilks Nov 28, 2023
29fa71a
fix: use fixed `hipo` version
c-dilks Nov 29, 2023
ec2b59e
test: `tree`
c-dilks Nov 29, 2023
a82692a
test: find hipo first
c-dilks Nov 29, 2023
fe76bf3
test: combined `cmake` prefix path
c-dilks Nov 29, 2023
6aaa781
fix: var name
c-dilks Nov 29, 2023
879e3eb
fix: another var name
c-dilks Nov 29, 2023
bf8104e
test: build `fmt` shared library setting
c-dilks Nov 29, 2023
e01c249
test: build `fmt` static lib
c-dilks Nov 29, 2023
558712c
test: `TRUE` -> `ON`
c-dilks Nov 29, 2023
a42653e
fix: barking up the wrong tree
c-dilks Nov 29, 2023
e1917be
build: use `pkg-config` for `fmt`
c-dilks Nov 29, 2023
f2df416
fix: full path to `pc` file
c-dilks Nov 29, 2023
cfd3c5c
build: use `cmake_prefix_path` builtin
c-dilks Nov 29, 2023
803fbb7
fix: build `hipo` with `-fPIC`
c-dilks Nov 29, 2023
0475a90
fix: include `hipo` in rpath
c-dilks Nov 29, 2023
8522ef1
deleted: meson.options
c-dilks Nov 29, 2023
6960e15
refactor: generate native `ini` file
c-dilks Nov 29, 2023
55254d5
fix: use HIPO `lib` subdir in rpath
c-dilks Nov 29, 2023
2dce809
fix: pkgconfig subdir
c-dilks Nov 29, 2023
f0b4719
refactor: add prefix to native `ini` file
c-dilks Nov 29, 2023
19b7f5e
fix: use native libdir instead of forcing `prefix/lib`
c-dilks Nov 29, 2023
b5d112f
fix: avoid prefix in rpath
c-dilks Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
namespace iguana::clas12 {

EventBuilderFilter::EventBuilderFilter() : Algorithm("event_builder_filter") {

// define required banks
m_requiredBanks = {
"REC::Particle",
"REC::Calorimeter"
};

// set default configuration options
SetOption("pids", std::set<int>{11, 211});
}

void EventBuilderFilter::Start(bank_index_cache_t &index_cache) {

// set configuration
// set logger
// TODO: should be done by configuration
m_log->SetLevel(Logger::Level::trace);
m_log->Debug("START {}", m_name);
m_opt.pids = {11, 211, -211};

// cache options
CacheOption("pids", o_pids);
PrintOptions();

// cache expected bank indices
CacheBankIndex(index_cache, b_particle, "REC::Particle");
Expand Down Expand Up @@ -48,7 +57,7 @@ namespace iguana::clas12 {


bool EventBuilderFilter::Filter(int pid) {
return m_opt.pids.find(pid) != m_opt.pids.end();
return o_pids.find(pid) != o_pids.end();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

namespace iguana::clas12 {

class EventBuilderFilterOptions {
public:
std::set<int> pids = {11, 211};
};


class EventBuilderFilter : public Algorithm {

public:
Expand All @@ -25,9 +19,13 @@ namespace iguana::clas12 {
bool Filter(int pid);

private:
EventBuilderFilterOptions m_opt;

/// `bank_vec_t` indices
int b_particle, b_calo;

/// configuration options
std::set<int> o_pids;

};

}
20 changes: 20 additions & 0 deletions src/services/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ namespace iguana {
Start(index_cache);
}

void Algorithm::SetOption(std::string key, option_value_t val) {
m_opt[key] = val;
}

void Algorithm::PrintOptions(Logger::Level level) {
if(m_log->GetLevel() <= level) {
m_log->Print(level, Logger::Header("CONFIGURATION OPTIONS"));
std::string format_str = "{:>20} = {} [{}]";
for(auto [key, val] : m_opt) {
if (const auto valPtr(std::get_if<int>(&val)); valPtr) m_log->Print(level, format_str, key, *valPtr, "int");
else if (const auto valPtr(std::get_if<double>(&val)); valPtr) m_log->Print(level, format_str, key, *valPtr, "double");
else if (const auto valPtr(std::get_if<std::string>(&val)); valPtr) m_log->Print(level, format_str, key, *valPtr, "string");
else if (const auto valPtr(std::get_if<std::set<int>>(&val)); valPtr) m_log->Print(level, format_str, key, fmt::join(*valPtr,", "), "set<int>");
else
m_log->Error("option '{}' type has no printer defined in PrintOptions", key);
}
m_log->Print(level, Logger::Header(""));
}
}

void Algorithm::CacheBankIndex(bank_index_cache_t index_cache, int &idx, std::string bankName) {
try {
idx = index_cache.at(bankName);
Expand Down
23 changes: 23 additions & 0 deletions src/services/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ namespace iguana {
/// Finalize an algorithm after all events are processed
virtual void Stop() = 0;

/// Set an option specified by the user
/// @param key the name of the option
/// @param val the value to set
void SetOption(std::string key, option_value_t val);

/// Print all the options and their values for this algorithm
/// @param level the log level
void PrintOptions(Logger::Level level=Logger::debug);

protected:

/// Cache the index of a bank in a `bank_vec_t`; throws an exception if the bank is not found
Expand All @@ -39,6 +48,17 @@ namespace iguana {
/// @param bankName the name of the bank
void CacheBankIndex(bank_index_cache_t index_cache, int &idx, std::string bankName);

/// Cache an option specified by the user
/// @param key the name of the option
/// @param val reference to the value of the option
template <typename VALUE>
void CacheOption(std::string key, VALUE &val) {
if(auto it{m_opt.find(key)}; it != m_opt.end())
val = std::get<VALUE>(it->second);
c-dilks marked this conversation as resolved.
Show resolved Hide resolved
else
m_log->Warn("unknown option '{}' in CacheOption", key);
}

/// Get the pointer to a bank from a `bank_vec_t`; optionally checks if the bank name matches the expectation
/// @param banks the `bank_vec_t` from which to get the specified bank
/// @param idx the index of `banks` of the specified bank
Expand Down Expand Up @@ -81,5 +101,8 @@ namespace iguana {

/// Logger
std::shared_ptr<Logger> m_log;

/// Configuration options
options_t m_opt;
};
}
2 changes: 1 addition & 1 deletion src/services/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace iguana {
}

std::string Logger::Header(std::string message, int width) {
return fmt::format("{:=^{}}", message, width);
return fmt::format("{:=^{}}", " " + message + " ", width);
}

}
13 changes: 13 additions & 0 deletions src/services/TypeDefs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <memory>
#include <vector>
#include <set>
#include <unordered_map>
#include <variant>

#include <hipo4/bank.h>

Expand All @@ -15,4 +17,15 @@ namespace iguana {
/// association between HIPO bank name and its index in a `bank_vec_t`
using bank_index_cache_t = std::unordered_map<std::string, int>;

/// option value variant type
using option_value_t = std::variant<
int,
double,
std::string,
std::set<int>
>;

/// data structure to hold configuration options
using options_t = std::unordered_map<std::string, option_value_t>;

}
2 changes: 1 addition & 1 deletion src/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ foreach test_exe : [ 'run_banks', 'run_rows' ]
test_exe + '.cc',
include_directories: project_inc,
dependencies: [ fmt_dep, hipo_dep ],
link_with: [ iguana_lib, algo_lib ],
link_with: [ iguana_lib, algo_lib, services_lib ],
install: true,
install_dir: project_bin_install_dir,
install_rpath: project_bin_rpath,
Expand Down
1 change: 1 addition & 0 deletions src/tests/run_banks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int main(int argc, char **argv) {
*/
iguana::Iguana I;
auto algo = I.algo_map.at(iguana::Iguana::clas12_EventBuilderFilter);
algo->SetOption("pids", std::set<int>{11, 211, -211});
algo->Start();

/////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/tests/run_rows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ int main(int argc, char **argv) {

// start the algorithm
auto algo = std::make_shared<iguana::clas12::EventBuilderFilter>();
algo->SetOption("pids", std::set<int>{11, 211, -211});
algo->Start();

/////////////////////////////////////////////////////
Expand Down