Iguana LATEST
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
iguana_ex_cpp_00_run_functions.cc
Go to the documentation of this file.
1
20
21#include <hipo4/reader.h>
22#include <iguana/algorithms/AlgorithmSequence.h>
23
25int main(int argc, char** argv)
26{
27
28 // parse arguments
29 char const* inFileName = argc > 1 ? argv[1] : "data.hipo";
30 int const numEvents = argc > 2 ? std::stoi(argv[2]) : 3;
31
32 // read input file
33 hipo::reader reader(inFileName, {0});
34
35 // set list of banks to be read
36 hipo::banklist banks = reader.getBanks({"RUN::config",
37 "REC::Particle",
38 "REC::Calorimeter",
39 "REC::Track",
40 "REC::Scintillator"});
41
42 // iguana algorithm sequence
43 // NOTE: the order that they are added to the sequence here will be the same order in which they will be run
45 seq.Add("clas12::EventBuilderFilter"); // filter by Event Builder PID (a filter algorithm)
46 seq.Add("clas12::SectorFinder"); // get the sector for each particle (a creator algorithm)
47 seq.Add("clas12::rga::MomentumCorrection"); // momentum corrections (a transformer algorithm)
48 // seq.PrintSequence();
49
50 // set log levels
51 // NOTE: this can also be done in a config file
52 seq.SetOption("clas12::EventBuilderFilter", "log", "info");
53 seq.SetOption("clas12::SectorFinder", "log", "info");
54 seq.SetOption("clas12::rga::MomentumCorrection", "log", "info");
55
56 // set algorithm options
57 // NOTE: this can also be done in a config file, but setting options here OVERRIDES config file settings
58 seq.SetOption<std::vector<int>>("clas12::EventBuilderFilter", "pids", {11, 211, -211});
59
60 // start the algorithms
61 seq.Start(banks);
62
63 // get bank index, for each bank we want to use after Iguana algorithms run
64 // NOTE: new banks from creator algorithms are initialized by `Start`
65 auto b_config = iguana::tools::GetBankIndex(banks, "RUN::config");
66 auto b_particle = iguana::tools::GetBankIndex(banks, "REC::Particle");
67 auto b_sector = seq.GetCreatedBankIndex(banks, "clas12::SectorFinder"); // newly created bank; string parameter is algorithm name, not bank name
68
69 // run the algorithm sequence on each event
70 int iEvent = 0;
71 while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
72
73 // references to this event's banks
74 auto& bank_config = banks.at(b_config);
75 auto& bank_particle = banks.at(b_particle);
76 auto& bank_sector = banks.at(b_sector);
77
78 // print the event number
79 fmt::println("===== EVENT {} =====", bank_config.getInt("event", 0));
80
81 // print the particle bank before Iguana algorithms
82 fmt::println("----- BEFORE IGUANA -----");
83 bank_particle.show(); // the original particle bank
84
85 // run the sequence of Iguana algorithms
86 seq.Run(banks);
87
88 // print the banks after Iguana algorithms
89 fmt::println("----- AFTER IGUANA -----");
90 bank_particle.show(); // the filtered particle bank, with corrected momenta
91 bank_sector.show(); // the new sector bank
92
93 // print a table; first the header
94 fmt::print("----- Analysis Particles -----\n");
95 fmt::print(" {:<20} {:<20} {:<20} {:<20}\n", "row == pindex", "PDG", "|p|", "sector");
96 // then print a row for each particle
97 // - use the `hipo::bank::getRowList()` method to loop over the bank rows that PASS the filter
98 // - if you'd rather loop over ALL bank rows, iterate from `i=0` up to `i < hipo::bank::getRows()` instead
99 for(auto const& row : bank_particle.getRowList()) {
100 auto p = std::hypot(
101 bank_particle.getFloat("px", row),
102 bank_particle.getFloat("py", row),
103 bank_particle.getFloat("pz", row));
104 auto pdg = bank_particle.getInt("pid", row);
105 auto sector = bank_sector.getInt("sector", row);
106 fmt::print(" {:<20} {:<20} {:<20.3f} {:<20}\n", row, pdg, p, sector);
107 }
108 fmt::print("\n");
109 }
110
111 // stop algorithms
112 seq.Stop();
113 return 0;
114}
Algorithm: An algorithm that can run a sequence of algorithms
hipo::banklist::size_type GetCreatedBankIndex(hipo::banklist &banks, std::string const &algo_instance_name) const noexcept(false)
void Stop() override
Finalize this algorithm after all events are processed.
void Add(std::string const &algo_class_name, std::string const &algo_instance_name="")
void SetOption(std::string const &algo_instance_name, std::string const &key, const OPTION_TYPE val)
void Start(hipo::banklist &banks) override
Initialize this algorithm before any events are processed, with the intent to process banks.
bool Run(hipo::banklist &banks) const override
Run Function: Process an event's hipo::banklist
int main(int argc, char **argv)
main function
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name, unsigned int const &variant=0) noexcept(false)