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: demonstrate in examples how to handle algorithm results #329

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 43 additions & 22 deletions examples/iguana_ex_cpp_00_run_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
#include <hipo4/reader.h>
#include <iguana/algorithms/AlgorithmSequence.h>

/// @brief show a bank along with a header
/// @param header the header to print above the bank
/// @param bank the bank to show
void prettyPrint(std::string header, hipo::bank& bank)
{
fmt::print("{:=^70}\n", " " + header + " ");
bank.show();
}

/// main function
int main(int argc, char** argv)
{
Expand All @@ -44,34 +35,64 @@ int main(int argc, char** argv)
"REC::Track",
"REC::Scintillator"});

// get bank index, for each bank we want to use after Iguana algorithms run
auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
// set the concurrency model to single-threaded, since this example is single-threaded;
// not doing this will use the thread-safe model, `"memoize"`
iguana::GlobalConcurrencyModel = "single";

// iguana algorithm sequence
iguana::AlgorithmSequence seq;
seq.Add("clas12::EventBuilderFilter"); // filter by Event Builder PID
seq.Add("clas12::SectorFinder"); // get the sector for each particle
seq.Add("clas12::MomentumCorrection"); // momentum corrections
// TODO:
// - getRowList for filter
// - add a creator algo
seq.Add("clas12::EventBuilderFilter"); // filter by Event Builder PID (a filter algorithm)
seq.Add("clas12::SectorFinder"); // get the sector for each particle (a creator algorithm)
seq.Add("clas12::MomentumCorrection"); // momentum corrections (a transformer algorithm)

// set log levels
seq.SetOption("clas12::EventBuilderFilter", "log", "debug");
seq.SetOption("clas12::MomentumCorrection", "log", "debug");
// NOTE: this can also be done in a config file
seq.SetOption("clas12::EventBuilderFilter", "log", "info");
seq.SetOption("clas12::SectorFinder", "log", "info");
seq.SetOption("clas12::MomentumCorrection", "log", "info");

// set algorithm options (overrides configuration files)
// set algorithm options
// NOTE: this can also be done in a config file
seq.SetOption<std::vector<int>>("clas12::EventBuilderFilter", "pids", {11, 211, -211});

// start the algorithms
seq.Start(banks);

// get bank index, for each bank we want to use after Iguana algorithms run
// NOTE: new banks from creator algorithms are initialized by `Start`
auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
auto b_sector = hipo::getBanklistIndex(banks, "REC::Particle::Sector"); // new created bank

// run the algorithm sequence on each event
int iEvent = 0;
while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
prettyPrint("BEFORE", banks.at(b_particle));

// print the particle bank before Iguana algorithms
fmt::print("{:=^70}\n", " BEFORE IGUANA ");
banks.at(b_particle).show();

// run the sequence of Iguana algorithms
seq.Run(banks);
prettyPrint("AFTER", banks.at(b_particle));

// print the banks after Iguana algorithms
fmt::print("{:=^70}\n", " AFTER IGUANA ");
banks.at(b_particle).show();
banks.at(b_sector).show();

// print
fmt::print("Analysis Particles:\n");
fmt::print(" {:<20} {:<20} {:<20} {:<20}\n", "row == pindex", "PDG", "|p|", "sector");
for(auto const& row : banks.at(b_particle).getRowList()) {
auto p = std::hypot(
banks.at(b_particle).getFloat("px", row),
banks.at(b_particle).getFloat("py", row),
banks.at(b_particle).getFloat("pz", row));
auto pdg = banks.at(b_particle).getInt("pid", row);
auto sector = banks.at(b_sector).getInt("sector", row);
fmt::print(" {:<20} {:<20} {:<20.3} {:<20}\n", row, pdg, p, sector);
}
fmt::print("\n");

}

// stop algorithms
Expand Down
36 changes: 25 additions & 11 deletions examples/iguana_ex_cpp_01_action_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/// @end_doc_example
#include <hipo4/reader.h>
#include <iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h>
#include <iguana/algorithms/clas12/SectorFinder/Algorithm.h>
#include <iguana/algorithms/clas12/MomentumCorrection/Algorithm.h>

/// main function
Expand All @@ -35,29 +36,34 @@ int main(int argc, char** argv)
// set list of banks to be read
hipo::banklist banks = reader.getBanks({"REC::Particle", "RUN::config"});

// get bank index, for each bank we want to use after Iguana algorithms run
auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
auto b_config = hipo::getBanklistIndex(banks, "RUN::config");

// set the concurrency model to single-threaded, since this example is single-threaded;
// not doing this will use the thread-safe model, `"memoize"`
iguana::GlobalConcurrencyModel = "single";

// create the algorithms
iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
iguana::clas12::MomentumCorrection algo_momentum_correction;
iguana::clas12::EventBuilderFilter algo_eventbuilder_filter; // filter by Event Builder PID (a filter algorithm)
iguana::clas12::SectorFinder algo_sector_finder; // get the sector for each particle (a creator algorithm)
iguana::clas12::MomentumCorrection algo_momentum_correction; // momentum corrections (a transformer algorithm)

// set log levels
algo_eventbuilder_filter.SetOption("log", "debug");
algo_momentum_correction.SetOption("log", "debug");
// set algorithm options
// NOTE: this can also be done in a config file
algo_eventbuilder_filter.SetOption("log", "info");
algo_sector_finder.SetOption("log", "info");
algo_momentum_correction.SetOption("log", "info");

// set algorithm options
// NOTE: this can also be done in a config file
algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});

// start the algorithms
algo_eventbuilder_filter.Start();
algo_momentum_correction.Start();

// get bank index, for each bank we want to use after Iguana algorithms run
auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
auto b_config = hipo::getBanklistIndex(banks, "RUN::config");

// run the algorithm sequence on each event
int iEvent = 0;
while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
Expand All @@ -74,9 +80,17 @@ int main(int argc, char** argv)
auto pid = particleBank.getInt("pid", row);
if(algo_eventbuilder_filter.Filter(pid)) {

int sector = 1; // FIXME: get the sector number. The algorithm `clas12::SectorFinder` can do this, however
// it requires reading full `hipo::bank` objects, whereas this example is meant to demonstrate
// `iguana` usage operating _only_ on bank row elements
int sector = 1; // FIXME: get the sector number from `clas12::SectorFinder`
/* needs additional general vector action function, such as:
int GetSector(
std::vector<int> const& calorimeter_sectors,
std::vector<int> const& calorimeter_pindices,
std::vector<int> const& track_sectors,
std::vector<int> const& track_pindices,
std::vector<int> const& scintillator_sectors,
std::vector<int> const& scintillator_pindices,
int const pindex) const;
*/

// if accepted PID, correct its momentum
auto [px, py, pz] = algo_momentum_correction.Transform(
Expand Down