Iguana 1.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
iguana_ex_cpp_00_run_functions_with_banks.cc
Go to the documentation of this file.
1
20
21#include <hipo4/reader.h>
22#include <iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h>
23#include <iguana/algorithms/clas12/SectorFinder/Algorithm.h>
24#include <iguana/algorithms/clas12/rga/MomentumCorrection/Algorithm.h>
25
27int main(int argc, char** argv)
28{
29
30 // parse arguments
31 char const* inFileName = argc > 1 ? argv[1] : "data.hipo";
32 int const numEvents = argc > 2 ? std::stoi(argv[2]) : 3;
33
34 // read input file
35 hipo::reader reader(inFileName, {0});
36
37 // set list of banks to be read
38 hipo::dictionary dict;
39 reader.readDictionary(dict);
40 hipo::bank bank_config(dict.getSchema("RUN::config"));
41 hipo::bank bank_particle(dict.getSchema("REC::Particle"));
42 hipo::bank bank_calorimeter(dict.getSchema("REC::Calorimeter"));
43 hipo::bank bank_track(dict.getSchema("REC::Track"));
44 hipo::bank bank_scintillator(dict.getSchema("REC::Scintillator"));
45
46 // iguana algorithm sequence
47 // NOTE: unlike `iguana_ex_cpp_00_run_functions`, we do not use `AlgorithmSequence`, since
48 // we'll be calling each algorithm's `Run(hipo::bank& bank1, ...)` functions, which are unique
49 // for each algorithm (unlike `Run(hipo::banklist&)`
50 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter; // filter by Event Builder PID (a filter algorithm)
51 iguana::clas12::SectorFinder algo_sector_finder; // get the sector for each particle (a creator algorithm)
52 iguana::clas12::rga::MomentumCorrection algo_momentum_correction; // momentum corrections (a transformer algorithm)
53
54 // set log levels
55 // NOTE: this can also be done in a config file
56 algo_eventbuilder_filter.SetOption("log", "info");
57 algo_sector_finder.SetOption("log", "info");
58 algo_momentum_correction.SetOption("log", "info");
59
60 // set algorithm options
61 // NOTE: this can also be done in a config file, but setting options here OVERRIDES config file settings
62 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
63
64 // start the algorithms
65 algo_eventbuilder_filter.Start();
66 algo_sector_finder.Start();
67 algo_momentum_correction.Start();
68
69 // define newly created bank object
70 hipo::bank bank_sector = algo_sector_finder.GetCreatedBank();
71
72 // run the algorithm sequence on each event
73 int iEvent = 0;
74 hipo::event event;
75 while(reader.next() && (numEvents == 0 || iEvent++ < numEvents)) {
76
77 // read the event's banks
78 reader.read(event);
79 event.getStructure(bank_config);
80 event.getStructure(bank_particle);
81 event.getStructure(bank_calorimeter);
82 event.getStructure(bank_track);
83 event.getStructure(bank_scintillator);
84
85 // print the event number
86 fmt::println("===== EVENT {} =====", bank_config.getInt("event", 0));
87
88 // print the particle bank before Iguana algorithms
89 fmt::println("----- BEFORE IGUANA -----");
90 bank_particle.show(); // the original particle bank
91
92 // run the sequence of Iguana algorithms, in your preferred order; continue
93 // to the next event if any of the Run functions return `false`, which happens
94 // if, for example, no particles pass a filter
95 if(!algo_eventbuilder_filter.Run(bank_particle))
96 continue;
97 if(!algo_sector_finder.Run(bank_particle, bank_track, bank_calorimeter, bank_scintillator, bank_sector))
98 continue;
99 if(!algo_momentum_correction.Run(bank_particle, bank_sector, bank_config))
100 continue;
101
102 // print the banks after Iguana algorithms
103 fmt::println("----- AFTER IGUANA -----");
104 bank_particle.show(); // the filtered particle bank, with corrected momenta
105 bank_sector.show(); // the new sector bank
106
107 // print a table; first the header
108 fmt::print("----- Analysis Particles -----\n");
109 fmt::print(" {:<20} {:<20} {:<20} {:<20}\n", "row == pindex", "PDG", "|p|", "sector");
110 // then print a row for each particle
111 // - use the `hipo::bank::getRowList()` method to loop over the bank rows that PASS the filter
112 // - if you'd rather loop over ALL bank rows, iterate from `i=0` up to `i < hipo::bank::getRows()` instead
113 for(auto const& row : bank_particle.getRowList()) {
114 auto p = std::hypot(
115 bank_particle.getFloat("px", row),
116 bank_particle.getFloat("py", row),
117 bank_particle.getFloat("pz", row));
118 auto pdg = bank_particle.getInt("pid", row);
119 auto sector = bank_sector.getInt("sector", row);
120 fmt::print(" {:<20} {:<20} {:<20.3f} {:<20}\n", row, pdg, p, sector);
121 }
122 fmt::print("\n");
123 }
124
125 // stop algorithms
126 algo_eventbuilder_filter.Stop();
127 algo_sector_finder.Stop();
128 algo_momentum_correction.Stop();
129 return 0;
130}
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Definition Algorithm.h:84
hipo::bank GetCreatedBank(std::string const &bank_name="") const noexcept(false)
Algorithm: Filter the REC::Particle (or similar) bank by PID from the Event Builder
Definition Algorithm.h:14
void Stop() override
Finalize this algorithm after all events are processed.
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
Algorithm: Find the sector for all rows in REC::Particle
Definition Algorithm.h:30
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
void Stop() override
Finalize this algorithm after all events are processed.
Algorithm: Momentum Corrections
Definition Algorithm.h:12
void Start(hipo::banklist &banks) override
Initialize this algorithm before any events are processed, with the intent to process banks.
void Stop() override
Finalize this algorithm after all events are processed.
bool Run(hipo::banklist &banks) const override
Run Function: Process an event's hipo::banklist
int main(int argc, char **argv)
main function