Skip to content

Commit

Permalink
feat: check algorithm input banks
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Nov 14, 2023
1 parent 36fa87a commit a0f4dc2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace iguana::clas12 {
}

Algorithm::BankMap EventBuilderFilter::Run(Algorithm::BankMap inputBanks) {

if(MissingInputBanks(inputBanks, {"particles"})) ThrowRun();

return inputBanks;
}

Expand Down
21 changes: 19 additions & 2 deletions src/services/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

namespace iguana {

Algorithm::Algorithm(std::string name) {
m_log = std::make_shared<Logger>(name);
Algorithm::Algorithm(std::string name) : m_name(name) {
m_log = std::make_shared<Logger>(m_name);
}

bool Algorithm::MissingInputBanks(BankMap banks, std::set<std::string> keys) {
for(auto key : keys) {
if(banks.find(key) == banks.end()) {
m_log->Error("Algorithm '{}' is missing the input bank '{}'", m_name, key);
m_log->Error(" => the following input banks are required by '{}':", m_name);
for(auto k : keys)
m_log->Error(" - {}", k);
return true;
}
}
return false;
}

void Algorithm::ThrowRun() {
throw std::runtime_error(fmt::format("Algorithm '{}' cannot `Run`", m_name));
}

}
28 changes: 27 additions & 1 deletion src/services/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Logger.h"
#include <hipo4/bank.h>
#include <set>

namespace iguana {

Expand All @@ -11,14 +12,39 @@ namespace iguana {

using BankMap = std::unordered_map<std::string, hipo::bank>;

/// Algorithm base class constructor
/// @param name the unique name for a derived class instance
Algorithm(std::string name);

/// Algorithm base class destructor
virtual ~Algorithm() {}

/// Initialize an algorithm before any events are processed
virtual void Start() = 0;

/// Run an algorithm
/// @param inputBanks the set of input banks
/// @return a set of output banks
virtual BankMap Run(BankMap inputBanks) = 0;

/// Finalize an algorithm after all events are processed
virtual void Stop() = 0;
virtual ~Algorithm() {}

protected:

/// Check if `banks` contains all keys `keys`; this is useful for checking algorithm inputs are complete.
/// @param banks the set of (key,bank) pairs to check
/// @keys the required keys
/// @return true if `banks` is missing any keys in `keys`
bool MissingInputBanks(BankMap banks, std::set<std::string> keys);

/// Throw a runtime exception when calling `Run`
void ThrowRun();

/// algorithm name
std::string m_name;

/// Logger
std::shared_ptr<Logger> m_log;
};
}
4 changes: 2 additions & 2 deletions src/tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ int main(int argc, char **argv) {
reader.read(event);
event.getStructure(particleBank);

auto resultBank = algo->Run({{"REC::Particle", particleBank}});
auto resultBank = algo->Run({{"particles", particleBank}});

fmt::print("BEFORE -> AFTER: {} -> {}\n", particleBank.getRows(), resultBank.at("REC::Particle").getRows());
fmt::print("BEFORE -> AFTER: {} -> {}\n", particleBank.getRows(), resultBank.at("particles").getRows());

count++;
}
Expand Down

0 comments on commit a0f4dc2

Please sign in to comment.