-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event builder filter algorithm (#9)
- Loading branch information
Showing
14 changed files
with
257 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/algorithms/clas12/event_builder_filter/EventBuilderFilter.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "EventBuilderFilter.h" | ||
|
||
namespace iguana::clas12 { | ||
|
||
void EventBuilderFilter::Start() { | ||
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 inBanks) { | ||
m_log->Debug("RUN {}", m_name); | ||
|
||
// check the input banks existence | ||
if(MissingInputBanks(inBanks, {"particles"})) | ||
Throw("missing input banks"); | ||
|
||
// define the output schemata and banks | ||
BankMap outBanks = { | ||
{ "particles", hipo::bank(inBanks.at("particles").getSchema()) } | ||
}; | ||
|
||
// filter the input bank for requested PDG code(s) | ||
std::set<int> acceptedRows; | ||
for(int row = 0; row < inBanks.at("particles").getRows(); row++) { | ||
auto pid = inBanks.at("particles").get("pid", row); | ||
auto accept = m_opt.pids.contains(pid); | ||
if(accept) acceptedRows.insert(row); | ||
m_log->Debug("input PID {} -- accept = {}", pid, accept); | ||
} | ||
|
||
// fill the output bank | ||
switch(m_opt.mode) { | ||
|
||
case EventBuilderFilterOptions::Modes::blank: | ||
outBanks.at("particles").setRows(inBanks.at("particles").getRows()); | ||
for(int row = 0; row < inBanks.at("particles").getRows(); row++) { | ||
if(acceptedRows.contains(row)) | ||
CopyBankRow(inBanks.at("particles"), row, outBanks.at("particles"), row); | ||
else | ||
BlankRow(outBanks.at("particles"), row); | ||
} | ||
break; | ||
|
||
case EventBuilderFilterOptions::Modes::compact: | ||
outBanks.at("particles").setRows(acceptedRows.size()); | ||
for(int row = 0; auto acceptedRow : acceptedRows) | ||
CopyBankRow(inBanks.at("particles"), acceptedRow, outBanks.at("particles"), row++); | ||
break; | ||
|
||
default: | ||
Throw("unknown 'mode' option"); | ||
|
||
} | ||
|
||
// dump the banks and return the output | ||
ShowBanks(inBanks, outBanks); | ||
return outBanks; | ||
} | ||
|
||
|
||
void EventBuilderFilter::Stop() { | ||
m_log->Debug("STOP {}", m_name); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/algorithms/clas12/event_builder_filter/EventBuilderFilter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "services/Algorithm.h" | ||
|
||
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 inBanks) override; | ||
void Stop() override; | ||
|
||
private: | ||
EventBuilderFilterOptions m_opt; | ||
|
||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Event Builder Filter | ||
|
||
Filters a particle bank for specific Event Builder PDGs |
4 changes: 2 additions & 2 deletions
4
...gorithms/clas12/fiducial_cuts/meson.build → src/algorithms/meson.build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,75 @@ | ||
#pragma once | ||
|
||
#include "Logger.h" | ||
#include <hipo4/bank.h> | ||
#include <set> | ||
|
||
namespace iguana { | ||
|
||
class Algorithm { | ||
|
||
public: | ||
|
||
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; | ||
virtual int Run(int a, int b) = 0; | ||
|
||
/// Run an algorithm | ||
/// @param inBanks the set of input banks | ||
/// @return a set of output banks | ||
virtual BankMap Run(BankMap inBanks) = 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); | ||
|
||
/// Copy a row from one bank to another, assuming their schemata are equivalent | ||
/// @param srcBank the source bank | ||
/// @param srcRow the row in `srcBank` to copy from | ||
/// @param destBank the destination bank | ||
/// @param destRow the row in `destBank` to copy to | ||
void CopyBankRow(hipo::bank srcBank, int srcRow, hipo::bank destBank, int destRow); | ||
|
||
/// Blank a row, setting all items to zero | ||
/// @param bank the bank to modify | ||
/// @param row the row to blank | ||
void BlankRow(hipo::bank bank, int row); | ||
|
||
/// 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); | ||
|
||
/// Stop the algorithm and throw a runtime exception | ||
/// @param message the error message | ||
void Throw(std::string message); | ||
|
||
/// algorithm name | ||
std::string m_name; | ||
|
||
/// Logger | ||
std::shared_ptr<Logger> m_log; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.