Skip to content

Commit

Permalink
feat: filter a bank with mode: blank
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Nov 15, 2023
1 parent ae28683 commit e0a5546
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 22 deletions.
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ project(
'iguana',
'cpp',
version: '0.0.0',
license: 'LGPLv3'
license: 'LGPLv3',
default_options: [ 'cpp_std=c++20' ],
)

project_inc = include_directories('src')
Expand Down
63 changes: 46 additions & 17 deletions src/algorithms/clas12/event_builder_filter/EventBuilderFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,67 @@
namespace iguana::clas12 {

void EventBuilderFilter::Start() {
m_log->Info("start event builder filter");
m_log->Debug("START {}", m_name);

// set configuration
m_log->SetLevel(Logger::Level::trace);
m_opt.mode = EventBuilderFilterOptions::Modes::blank;
m_opt.pids = {11, 211, -211};
}

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

Algorithm::BankMap EventBuilderFilter::Run(Algorithm::BankMap inBanks) {
m_log->Debug("RUN {}", m_name);

// check the input banks existence
if(MissingInputBanks(inputBanks, {"particles"}))
if(MissingInputBanks(inBanks, {"particles"}))
ThrowRun();

// define the output schemata and banks
BankMap outputBanks = {
{ "particles", hipo::bank(inputBanks.at("particles").getSchema(), inputBanks.at("particles").getRows()) }
BankMap outBanks = {
{ "particles", hipo::bank(inBanks.at("particles").getSchema()) }
};

// filter the input bank for requested PDG code(s)
int j=0;
for(int i=0; i<inputBanks.at("particles").getRows(); i++) {
auto pid = inputBanks.at("particles").get("pid",i);
m_log->Info("INPUT PID = {}", pid);
if(pid==11)
outputBanks.at("particles").put("pid", j++, pid);
// set number of output rows
switch(m_opt.mode) {
case EventBuilderFilterOptions::Modes::blank:
outBanks.at("particles").setRows(inBanks.at("particles").getRows());
break;
case EventBuilderFilterOptions::Modes::compact:
outBanks.at("particles").setRows(inBanks.at("particles").getRows()); // FIXME
break;
}

// print
m_log->Info("SHOW OUTPUT BANKS");
outputBanks.at("particles").show();
// filter the input bank for requested PDG code(s)
int outRow = -1;
for(int inRow=0; inRow<inBanks.at("particles").getRows(); inRow++) {
auto inPid = inBanks.at("particles").get("pid",inRow);

if(m_opt.pids.contains(inPid)) {
m_log->Debug("input PID {} -- accept", inPid);
if(m_opt.mode == EventBuilderFilterOptions::Modes::blank)
outRow = inRow;
else
outRow++;
outBanks.at("particles").put("pid", outRow, inPid);
}

return outputBanks;
else {
m_log->Debug("input PID {} -- reject", inPid);
if(m_opt.mode == EventBuilderFilterOptions::blank)
outBanks.at("particles").put("pid", inRow, 0);
}

}

// dump the banks and return the output
ShowBanks(inBanks, outBanks);
return outBanks;
}


void EventBuilderFilter::Stop() {
m_log->Info("stop event builder filter");
m_log->Debug("STOP {}", m_name);
}

}
15 changes: 14 additions & 1 deletion src/algorithms/clas12/event_builder_filter/EventBuilderFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@

namespace iguana::clas12 {

class EventBuilderFilterOptions {
public:
enum Modes { blank, compact };
Modes mode = blank;
std::set<int> pids = {11, 211};
};


class EventBuilderFilter : public Algorithm {

public:
EventBuilderFilter() : Algorithm("event_builder_filter") {}
~EventBuilderFilter() {}

void Start() override;
Algorithm::BankMap Run(Algorithm::BankMap inputBanks) override;
Algorithm::BankMap Run(Algorithm::BankMap inBanks) override;
void Stop() override;

private:
EventBuilderFilterOptions m_opt;

};

}
15 changes: 15 additions & 0 deletions src/services/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ namespace iguana {
throw std::runtime_error(fmt::format("Algorithm '{}' cannot `Run`", m_name));
}

void Algorithm::ShowBanks(BankMap banks, std::string message, Logger::Level level) {
if(m_log->GetLevel() <= level) {
m_log->Print(level, message);
for(auto [key,bank] : banks) {
m_log->Print(level, "BANK: '{}'", key);
bank.show();
}
}
}

void Algorithm::ShowBanks(BankMap inBanks, BankMap outBanks, Logger::Level level) {
ShowBanks(inBanks, "===== INPUT BANKS =====", level);
ShowBanks(outBanks, "===== OUTPUT BANKS =====", level);
}

}
16 changes: 14 additions & 2 deletions src/services/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ namespace iguana {
virtual void Start() = 0;

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

/// Finalize an algorithm after all events are processed
virtual void Stop() = 0;
Expand All @@ -41,6 +41,18 @@ namespace iguana {
/// Throw a runtime exception when calling `Run`
void ThrowRun();

/// Dump all banks in a BankMap
/// @param banks the banks to show
/// @param message optionally print a header message
/// @param level the log level
void ShowBanks(BankMap banks, std::string message="", Logger::Level level=Logger::trace);

/// Dump all input and output banks
/// @param inBanks the input banks
/// @param outBanks the output banks
/// @param level the log level
void ShowBanks(BankMap inBanks, BankMap outBanks, Logger::Level level=Logger::trace);

/// algorithm name
std::string m_name;

Expand Down
4 changes: 4 additions & 0 deletions src/services/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ namespace iguana {
Debug("Logger '{}' set to '{}'", m_name, m_level_names.at(m_level));
}

Logger::Level Logger::GetLevel() {
return m_level;
}

}
1 change: 1 addition & 0 deletions src/services/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace iguana {
~Logger() {}

void SetLevel(Level lev);
Level GetLevel();

template <typename... VALUES> void Trace(std::string msg, VALUES... vals) { Print(trace, msg, vals...); }
template <typename... VALUES> void Debug(std::string msg, VALUES... vals) { Print(debug, msg, vals...); }
Expand Down
2 changes: 1 addition & 1 deletion src/tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char **argv) {
reader.open(inFile.c_str());
hipo::dictionary factory;
reader.readDictionary(factory);
factory.show();
// factory.show();

hipo::bank particleBank(factory.getSchema("REC::Particle"));
hipo::event event;
Expand Down

0 comments on commit e0a5546

Please sign in to comment.