Iguana LATEST
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 = iguana::tools::GetBankIndex(banks, "REC::Particle");
46 auto b_config = iguana::tools::GetBankIndex(banks, "RUN::config");
47 auto b_track = iguana::tools::GetBankIndex(banks, "REC::Track");
48 auto b_calorimeter = iguana::tools::GetBankIndex(banks, "REC::Calorimeter");
49 auto b_scintillator = iguana::tools::GetBankIndex(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 // configure algorithms with a custom YAML file
61 // - in practice, specify the path(s) to your preferred configuration file(s); see documentation
62 // on 'How to Configure Algorithms' for details, and alternative methods for algorithm configuration
63 // - in this example, the file is from the source-code path `./config/examples/`, which was copied to
64 // the installation subdirectory `etc/iguana/`, within the default configuration-file search path
65 std::string config_file = "examples/config_for_examples.yaml";
66 algo_eventbuilder_filter.SetConfigFile(config_file);
67 algo_sector_finder.SetConfigFile(config_file);
68 algo_momentum_correction.SetConfigFile(config_file);
69
70 // start the algorithms
71 algo_eventbuilder_filter.Start();
72 algo_sector_finder.Start();
73 algo_momentum_correction.Start();
74
75 // run the algorithms on each event
76 int iEvent = 0;
77 while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
78
79 // get the banks for this event
80 auto& particleBank = banks.at(b_particle);
81 auto& configBank = banks.at(b_config);
82 auto& trackBank = banks.at(b_track);
83 auto& calorimeterBank = banks.at(b_calorimeter);
84 auto& scintillatorBank = banks.at(b_scintillator);
85
86 // show the particle bank
87 // particleBank.show();
88
89 // print the event number
90 fmt::print("evnum = {}\n", configBank.getInt("event", 0));
91
92 // we'll need information from all the rows of REC::Track,Calorimeter,Scintilator,
93 // in order to get the sector information for each particle
94 // FIXME: there are vectorized accessors, but we cannot use them yet; see https://github.com/gavalian/hipo/issues/72
95 // until then, we fill `std::vector`s manually
96 std::vector<int> trackBank_sectors;
97 std::vector<int> trackBank_pindices;
98 std::vector<int> calorimeterBank_sectors;
99 std::vector<int> calorimeterBank_pindices;
100 std::vector<int> scintillatorBank_sectors;
101 std::vector<int> scintillatorBank_pindices;
102 for(auto const& r : trackBank.getRowList()) {
103 trackBank_sectors.push_back(trackBank.getByte("sector", r));
104 trackBank_pindices.push_back(trackBank.getShort("pindex", r));
105 }
106 for(auto const& r : calorimeterBank.getRowList()) {
107 calorimeterBank_sectors.push_back(calorimeterBank.getByte("sector", r));
108 calorimeterBank_pindices.push_back(calorimeterBank.getShort("pindex", r));
109 }
110 for(auto const& r : scintillatorBank.getRowList()) {
111 scintillatorBank_sectors.push_back(scintillatorBank.getByte("sector", r));
112 scintillatorBank_pindices.push_back(scintillatorBank.getShort("pindex", r));
113 }
114
115 // loop over bank rows
116 for(auto const& row : particleBank.getRowList()) {
117
118 // check the PID with EventBuilderFilter
119 auto pid = particleBank.getInt("pid", row);
120 if(algo_eventbuilder_filter.Filter(pid)) {
121
122 // get the sector for this particle; this is using a vector action function, so
123 // many of its arguments are of type `std::vector`
124 auto sector = algo_sector_finder.GetStandardSector(
125 trackBank_sectors,
126 trackBank_pindices,
127 calorimeterBank_sectors,
128 calorimeterBank_pindices,
129 scintillatorBank_sectors,
130 scintillatorBank_pindices,
131 row);
132
133 // correct the particle momentum
134 auto [px, py, pz] = algo_momentum_correction.Transform(
135 particleBank.getFloat("px", row),
136 particleBank.getFloat("py", row),
137 particleBank.getFloat("pz", row),
138 sector,
139 pid,
140 configBank.getFloat("torus", 0));
141
142 // then print the result
143 fmt::print("Analysis Particle PDG = {}\n", pid);
144 fmt::print(" sector = {}\n", sector);
145 fmt::print(" p_old = ({:11.5f}, {:11.5f}, {:11.5f})\n", particleBank.getFloat("px", row), particleBank.getFloat("py", row), particleBank.getFloat("pz", row));
146 fmt::print(" p_new = ({:11.5f}, {:11.5f}, {:11.5f})\n", px, py, pz);
147 }
148 }
149 }
150
151 // stop the algorithms
152 algo_eventbuilder_filter.Stop();
153 algo_sector_finder.Stop();
154 algo_momentum_correction.Stop();
155 return 0;
156}
virtual void Stop() final
Stop Function: Finalize this algorithm after all events are processed.
virtual void Start(hipo::banklist &banks) final
Start Function: Initialize this algorithm before any events are processed, with the intent to process...
void SetConfigFile(std::string const &name)
Algorithm: Filter the particle bank (REC::Particle, or similar) bank by PID from the Event Builder
Definition Algorithm.h:12
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:27
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...
Algorithm: Momentum Corrections
Definition Algorithm.h:12
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
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)