Loading [MathJax]/extensions/tex2jax.js
Iguana 0.9.0
Implementation Guardian of Analysis Algorithms
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/MomentumCorrection/Algorithm.h>
25
27int main(int argc, char** argv)
28{
29
30 // parse arguments
31 int argi = 1;
32 char const* inFileName = argc > argi ? argv[argi++] : "data.hipo";
33 int const numEvents = argc > argi ? std::stoi(argv[argi++]) : 1;
34
35 // read input file
36 hipo::reader reader(inFileName,{0});
37
38 // set list of banks to be read
39 hipo::banklist banks = reader.getBanks({
40 "REC::Particle",
41 "RUN::config",
42 "REC::Track",
43 "REC::Calorimeter",
44 "REC::Scintillator"
45 });
46
47 // get bank index, for each bank we want to use after Iguana algorithms run
48 auto b_particle = hipo::getBanklistIndex(banks, "REC::Particle");
49 auto b_config = hipo::getBanklistIndex(banks, "RUN::config");
50 auto b_track = hipo::getBanklistIndex(banks, "REC::Track");
51 auto b_calorimeter = hipo::getBanklistIndex(banks, "REC::Calorimeter");
52 auto b_scintillator = hipo::getBanklistIndex(banks, "REC::Scintillator");
53
54 // set the concurrency model to single-threaded, since this example is single-threaded;
55 // not doing this will use the thread-safe model, `"memoize"`
57
58 // create the algorithms
59 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
60 iguana::clas12::SectorFinder algo_sector_finder;
61 iguana::clas12::MomentumCorrection algo_momentum_correction;
62
63 // set log levels
64 algo_eventbuilder_filter.SetOption("log", "info");
65 algo_sector_finder.SetOption("log", "info");
66 algo_momentum_correction.SetOption("log", "info");
67
68 // set algorithm options
69 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
70
71 // start the algorithms
72 algo_eventbuilder_filter.Start();
73 algo_sector_finder.Start();
74 algo_momentum_correction.Start();
75
76 // run the algorithms on each event
77 int iEvent = 0;
78 while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
79
80 // get the banks for this event
81 auto& particleBank = banks.at(b_particle);
82 auto& configBank = banks.at(b_config);
83 auto& trackBank = banks.at(b_track);
84 auto& calorimeterBank = banks.at(b_calorimeter);
85 auto& scintillatorBank = banks.at(b_scintillator);
86
87 // show the particle bank
88 // particleBank.show();
89
90 // print the event number
91 fmt::print("evnum = {}\n", configBank.getInt("event", 0));
92
93 // we'll need information from all the rows of REC::Track,Calorimeter,Scintilator,
94 // in order to get the sector information for each particle
95 // FIXME: there are vectorized accessors, but we cannot use them yet; see https://github.com/gavalian/hipo/issues/72
96 // until then, we fill `std::vector`s manually
97 std::vector<int> trackBank_sectors;
98 std::vector<int> trackBank_pindices;
99 std::vector<int> calorimeterBank_sectors;
100 std::vector<int> calorimeterBank_pindices;
101 std::vector<int> scintillatorBank_sectors;
102 std::vector<int> scintillatorBank_pindices;
103 for(auto const& r : trackBank.getRowList()) {
104 trackBank_sectors.push_back(trackBank.getByte("sector", r));
105 trackBank_pindices.push_back(trackBank.getShort("pindex", r));
106 }
107 for(auto const& r : calorimeterBank.getRowList()) {
108 calorimeterBank_sectors.push_back(calorimeterBank.getByte("sector", r));
109 calorimeterBank_pindices.push_back(calorimeterBank.getShort("pindex", r));
110 }
111 for(auto const& r : scintillatorBank.getRowList()) {
112 scintillatorBank_sectors.push_back(scintillatorBank.getByte("sector", r));
113 scintillatorBank_pindices.push_back(scintillatorBank.getShort("pindex", r));
114 }
115
116 // loop over bank rows
117 for(auto const& row : particleBank.getRowList()) {
118
119 // check the PID with EventBuilderFilter
120 auto pid = particleBank.getInt("pid", row);
121 if(algo_eventbuilder_filter.Filter(pid)) {
122
123 // get the sector for this particle; this is using a vector action function, so
124 // many of its arguments are of type `std::vector`
125 auto sector = algo_sector_finder.GetStandardSector(
126 trackBank_sectors,
127 trackBank_pindices,
128 calorimeterBank_sectors,
129 calorimeterBank_pindices,
130 scintillatorBank_sectors,
131 scintillatorBank_pindices,
132 row);
133
134 // correct the particle momentum
135 auto [px, py, pz] = algo_momentum_correction.Transform(
136 particleBank.getFloat("px", row),
137 particleBank.getFloat("py", row),
138 particleBank.getFloat("pz", row),
139 sector,
140 pid,
141 configBank.getFloat("torus", 0));
142
143 // then print the result
144 fmt::print("Particle PDG = {}\n", pid);
145 fmt::print(" sector = {}\n", sector);
146 fmt::print(" p_old = ({}, {}, {})\n", particleBank.getFloat("px", row), particleBank.getFloat("py", row), particleBank.getFloat("pz", row));
147 fmt::print(" p_new = ({}, {}, {})\n", px, py, pz);
148 }
149 }
150 }
151
152 // stop the algorithms
153 algo_eventbuilder_filter.Stop();
154 algo_momentum_correction.Stop();
155 return 0;
156}
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Definition Algorithm.h:80
Algorithm: Filter the REC::Particle (or similar) bank by PID from the Event Builder
Definition Algorithm.h:18
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: Momentum Corrections
Definition Algorithm.h:17
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.
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
Algorithm: Find the sector for all rows in REC::Particle
Definition Algorithm.h:34
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...
int main(int argc, char **argv)
main function
GlobalParam< std::string > GlobalConcurrencyModel
The concurrency model, for running certain algorithms in a thread-safe way.