Iguana 1.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
iguana_ex_cpp_01_action_functions.cc
Go to the documentation of this file.
1
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::banklist banks = reader.getBanks({"REC::Particle",
39 "RUN::config",
40 "REC::Track",
41 "REC::Calorimeter",
42 "REC::Scintillator"});
43
44 // get bank index, for each bank we want to use after Iguana algorithms run
45 auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
46 auto b_config = hipo::getBanklistIndex(banks, "RUN::config");
47 auto b_track = hipo::getBanklistIndex(banks, "REC::Track");
48 auto b_calorimeter = hipo::getBanklistIndex(banks, "REC::Calorimeter");
49 auto b_scintillator = hipo::getBanklistIndex(banks, "REC::Scintillator");
50
51 // set the concurrency model to single-threaded, since this example is single-threaded;
52 // not doing this will use the thread-safe model, `"memoize"`
53 iguana::GlobalConcurrencyModel = "single";
54
55 // create the algorithms
56 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter; // filter by Event Builder PID (a filter algorithm)
57 iguana::clas12::SectorFinder algo_sector_finder; // get the sector for each particle (a creator algorithm)
58 iguana::clas12::rga::MomentumCorrection algo_momentum_correction; // momentum corrections (a transformer algorithm)
59
60 // set log levels
61 algo_eventbuilder_filter.SetOption("log", "info");
62 algo_sector_finder.SetOption("log", "info");
63 algo_momentum_correction.SetOption("log", "info");
64
65 // set algorithm options
66 // NOTE: this can also be done in a config file
67 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
68
69 // start the algorithms
70 algo_eventbuilder_filter.Start();
71 algo_sector_finder.Start();
72 algo_momentum_correction.Start();
73
74 // run the algorithms on each event
75 int iEvent = 0;
76 while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
77
78 // get the banks for this event
79 auto& particleBank = banks.at(b_particle);
80 auto& configBank = banks.at(b_config);
81 auto& trackBank = banks.at(b_track);
82 auto& calorimeterBank = banks.at(b_calorimeter);
83 auto& scintillatorBank = banks.at(b_scintillator);
84
85 // show the particle bank
86 // particleBank.show();
87
88 // print the event number
89 fmt::print("evnum = {}\n", configBank.getInt("event", 0));
90
91 // we'll need information from all the rows of REC::Track,Calorimeter,Scintilator,
92 // in order to get the sector information for each particle
93 // FIXME: there are vectorized accessors, but we cannot use them yet; see https://github.com/gavalian/hipo/issues/72
94 // until then, we fill `std::vector`s manually
95 std::vector<int> trackBank_sectors;
96 std::vector<int> trackBank_pindices;
97 std::vector<int> calorimeterBank_sectors;
98 std::vector<int> calorimeterBank_pindices;
99 std::vector<int> scintillatorBank_sectors;
100 std::vector<int> scintillatorBank_pindices;
101 for(auto const& r : trackBank.getRowList()) {
102 trackBank_sectors.push_back(trackBank.getByte("sector", r));
103 trackBank_pindices.push_back(trackBank.getShort("pindex", r));
104 }
105 for(auto const& r : calorimeterBank.getRowList()) {
106 calorimeterBank_sectors.push_back(calorimeterBank.getByte("sector", r));
107 calorimeterBank_pindices.push_back(calorimeterBank.getShort("pindex", r));
108 }
109 for(auto const& r : scintillatorBank.getRowList()) {
110 scintillatorBank_sectors.push_back(scintillatorBank.getByte("sector", r));
111 scintillatorBank_pindices.push_back(scintillatorBank.getShort("pindex", r));
112 }
113
114 // loop over bank rows
115 for(auto const& row : particleBank.getRowList()) {
116
117 // check the PID with EventBuilderFilter
118 auto pid = particleBank.getInt("pid", row);
119 if(algo_eventbuilder_filter.Filter(pid)) {
120
121 // get the sector for this particle; this is using a vector action function, so
122 // many of its arguments are of type `std::vector`
123 auto sector = algo_sector_finder.GetStandardSector(
124 trackBank_sectors,
125 trackBank_pindices,
126 calorimeterBank_sectors,
127 calorimeterBank_pindices,
128 scintillatorBank_sectors,
129 scintillatorBank_pindices,
130 row);
131
132 // correct the particle momentum
133 auto [px, py, pz] = algo_momentum_correction.Transform(
134 particleBank.getFloat("px", row),
135 particleBank.getFloat("py", row),
136 particleBank.getFloat("pz", row),
137 sector,
138 pid,
139 configBank.getFloat("torus", 0));
140
141 // then print the result
142 fmt::print("Analysis Particle PDG = {}\n", pid);
143 fmt::print(" sector = {}\n", sector);
144 fmt::print(" p_old = ({:11.5f}, {:11.5f}, {:11.5f})\n", particleBank.getFloat("px", row), particleBank.getFloat("py", row), particleBank.getFloat("pz", row));
145 fmt::print(" p_new = ({:11.5f}, {:11.5f}, {:11.5f})\n", px, py, pz);
146 }
147 }
148 }
149
150 // stop the algorithms
151 algo_eventbuilder_filter.Stop();
152 algo_sector_finder.Stop();
153 algo_momentum_correction.Stop();
154 return 0;
155}
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Definition Algorithm.h:84
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 Filter(int const pid) const
Action Function: checks if the PDG pid is a part of the list of user-specified PDGs
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.
int GetStandardSector(std::vector< int > const &sectors_track, std::vector< int > const &pindices_track, std::vector< int > const &sectors_cal, std::vector< int > const &pindices_cal, std::vector< int > const &sectors_scint, std::vector< int > const &pindices_scint, int const &pindex_particle) const
Action Function: for a given particle with index pindex_particle, get its sector from using the stand...
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.
Momentum3 Transform(vector_element_t const px, vector_element_t const py, vector_element_t const pz, int const sec, int const pid, float const torus) const
Action Function: Apply the momentum correction
void Stop() override
Finalize this algorithm after all events are processed.
int main(int argc, char **argv)
main function