examples/iguana_ex_cpp_01_action_functions.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @begin_doc_example{cpp} | ||
| 2 | /// @file iguana_ex_cpp_01_action_functions.cc | ||
| 3 | /// @brief Example using Iguana action functions on data from bank rows. This is useful for | ||
| 4 | /// users who do not have the `hipo::bank` objects, instead only having the numerical data from them. | ||
| 5 | /// @par Usage | ||
| 6 | /// ```bash | ||
| 7 | /// iguana_ex_cpp_01_action_functions [HIPO_FILE] [NUM_EVENTS] | ||
| 8 | /// | ||
| 9 | /// HIPO_FILE the HIPO file to analyze | ||
| 10 | /// | ||
| 11 | /// NUM_EVENTS the number of events to analyze; | ||
| 12 | /// set to zero to analyze all events | ||
| 13 | /// ``` | ||
| 14 | /// | ||
| 15 | /// @note while this example _does_ use `hipo::bank` objects to read HIPO data, it demonstrates | ||
| 16 | /// using action functions called with the data _from_ these banks. We only use `hipo::bank` to | ||
| 17 | /// _obtain_ these data, since it is convenient that we don't have to use another HIPO reader, | ||
| 18 | /// which would introduce another build dependency to this program. | ||
| 19 | /// | ||
| 20 | /// @end_doc_example | ||
| 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 | |||
| 26 | /// main function | ||
| 27 | 1 | int main(int argc, char** argv) | |
| 28 | { | ||
| 29 | |||
| 30 | // parse arguments | ||
| 31 |
1/2✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 12 not taken.
|
1 | char const* inFileName = argc > 1 ? argv[1] : "data.hipo"; |
| 32 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 12 not taken.
|
1 | int const numEvents = argc > 2 ? std::stoi(argv[2]) : 3; |
| 33 | |||
| 34 | // read input file | ||
| 35 |
1/2✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 167 not taken.
|
1 | hipo::reader reader(inFileName, {0}); |
| 36 | |||
| 37 | // set list of banks to be read | ||
| 38 |
1/2✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 247 not taken.
|
2 | hipo::banklist banks = reader.getBanks({"REC::Particle", |
| 39 | "RUN::config", | ||
| 40 | "REC::Track", | ||
| 41 | "REC::Calorimeter", | ||
| 42 |
1/2✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 171 not taken.
|
1 | "REC::Scintillator"}); |
| 43 | |||
| 44 | // get bank index, for each bank we want to use after Iguana algorithms run | ||
| 45 |
2/4✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 245 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 173 not taken.
|
1 | auto b_particle = iguana::tools::GetBankIndex(banks, "REC::Particle"); |
| 46 |
2/4✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 245 not taken.
✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 179 not taken.
|
1 | auto b_config = iguana::tools::GetBankIndex(banks, "RUN::config"); |
| 47 |
2/4✓ Branch 34 → 35 taken 1 time.
✗ Branch 34 → 245 not taken.
✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 185 not taken.
|
1 | auto b_track = iguana::tools::GetBankIndex(banks, "REC::Track"); |
| 48 |
2/4✓ Branch 41 → 42 taken 1 time.
✗ Branch 41 → 245 not taken.
✓ Branch 42 → 43 taken 1 time.
✗ Branch 42 → 191 not taken.
|
1 | auto b_calorimeter = iguana::tools::GetBankIndex(banks, "REC::Calorimeter"); |
| 49 |
2/4✓ Branch 48 → 49 taken 1 time.
✗ Branch 48 → 245 not taken.
✓ Branch 49 → 50 taken 1 time.
✗ Branch 49 → 197 not taken.
|
1 | 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 |
2/4✓ Branch 55 → 56 taken 1 time.
✗ Branch 55 → 245 not taken.
✓ Branch 56 → 57 taken 1 time.
✗ Branch 56 → 203 not taken.
|
1 | iguana::GlobalConcurrencyModel = "single"; |
| 54 | |||
| 55 | // create the algorithms | ||
| 56 |
1/2✓ Branch 62 → 63 taken 1 time.
✗ Branch 62 → 245 not taken.
|
1 | iguana::clas12::EventBuilderFilter algo_eventbuilder_filter; // filter by Event Builder PID (a filter algorithm) |
| 57 |
1/2✓ Branch 63 → 64 taken 1 time.
✗ Branch 63 → 243 not taken.
|
1 | iguana::clas12::SectorFinder algo_sector_finder; // get the sector for each particle (a creator algorithm) |
| 58 |
1/2✓ Branch 64 → 65 taken 1 time.
✗ Branch 64 → 241 not taken.
|
1 | 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 |
1/2✓ Branch 65 → 66 taken 1 time.
✗ Branch 65 → 239 not taken.
|
1 | std::string config_file = "examples/config_for_examples.yaml"; |
| 66 |
1/2✓ Branch 66 → 67 taken 1 time.
✗ Branch 66 → 233 not taken.
|
1 | algo_eventbuilder_filter.SetConfigFile(config_file); |
| 67 |
1/2✓ Branch 67 → 68 taken 1 time.
✗ Branch 67 → 233 not taken.
|
1 | algo_sector_finder.SetConfigFile(config_file); |
| 68 |
1/2✓ Branch 68 → 69 taken 1 time.
✗ Branch 68 → 233 not taken.
|
1 | algo_momentum_correction.SetConfigFile(config_file); |
| 69 | |||
| 70 | // start the algorithms | ||
| 71 |
1/2✓ Branch 69 → 70 taken 1 time.
✗ Branch 69 → 233 not taken.
|
1 | algo_eventbuilder_filter.Start(); |
| 72 |
1/2✓ Branch 70 → 71 taken 1 time.
✗ Branch 70 → 233 not taken.
|
1 | algo_sector_finder.Start(); |
| 73 |
1/2✓ Branch 71 → 142 taken 1 time.
✗ Branch 71 → 233 not taken.
|
1 | algo_momentum_correction.Start(); |
| 74 | |||
| 75 | // run the algorithms on each event | ||
| 76 | int iEvent = 0; | ||
| 77 |
5/8✓ Branch 142 → 143 taken 101 times.
✗ Branch 142 → 233 not taken.
✓ Branch 143 → 144 taken 101 times.
✗ Branch 143 → 147 not taken.
✓ Branch 144 → 145 taken 101 times.
✗ Branch 144 → 146 not taken.
✓ Branch 145 → 146 taken 100 times.
✓ Branch 145 → 147 taken 1 time.
|
101 | while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) { |
| 78 | |||
| 79 | // get the banks for this event | ||
| 80 |
1/2✓ Branch 146 → 72 taken 100 times.
✗ Branch 146 → 233 not taken.
|
100 | auto& particleBank = banks.at(b_particle); |
| 81 |
1/2✓ Branch 72 → 73 taken 100 times.
✗ Branch 72 → 233 not taken.
|
100 | auto& configBank = banks.at(b_config); |
| 82 |
1/2✓ Branch 73 → 74 taken 100 times.
✗ Branch 73 → 233 not taken.
|
100 | auto& trackBank = banks.at(b_track); |
| 83 |
1/2✓ Branch 74 → 75 taken 100 times.
✗ Branch 74 → 233 not taken.
|
100 | auto& calorimeterBank = banks.at(b_calorimeter); |
| 84 |
1/2✓ Branch 75 → 76 taken 100 times.
✗ Branch 75 → 233 not taken.
|
100 | auto& scintillatorBank = banks.at(b_scintillator); |
| 85 | |||
| 86 | // show the particle bank | ||
| 87 | // particleBank.show(); | ||
| 88 | |||
| 89 | // print the event number | ||
| 90 |
1/2✓ Branch 78 → 79 taken 100 times.
✗ Branch 78 → 209 not taken.
|
200 | 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 |
3/4✓ Branch 78 → 79 taken 100 times.
✗ Branch 78 → 209 not taken.
✓ Branch 85 → 80 taken 271 times.
✓ Branch 85 → 86 taken 100 times.
|
371 | for(auto const& r : trackBank.getRowList()) { |
| 103 |
1/2✓ Branch 81 → 82 taken 271 times.
✗ Branch 81 → 209 not taken.
|
271 | trackBank_sectors.push_back(trackBank.getByte("sector", r)); |
| 104 |
1/2✓ Branch 83 → 84 taken 271 times.
✗ Branch 83 → 209 not taken.
|
271 | trackBank_pindices.push_back(trackBank.getShort("pindex", r)); |
| 105 | } | ||
| 106 |
3/4✓ Branch 86 → 87 taken 100 times.
✗ Branch 86 → 209 not taken.
✓ Branch 93 → 88 taken 585 times.
✓ Branch 93 → 94 taken 100 times.
|
685 | for(auto const& r : calorimeterBank.getRowList()) { |
| 107 |
1/2✓ Branch 89 → 90 taken 585 times.
✗ Branch 89 → 209 not taken.
|
585 | calorimeterBank_sectors.push_back(calorimeterBank.getByte("sector", r)); |
| 108 |
1/2✓ Branch 91 → 92 taken 585 times.
✗ Branch 91 → 209 not taken.
|
585 | calorimeterBank_pindices.push_back(calorimeterBank.getShort("pindex", r)); |
| 109 | } | ||
| 110 |
3/4✓ Branch 94 → 95 taken 100 times.
✗ Branch 94 → 209 not taken.
✓ Branch 101 → 96 taken 637 times.
✓ Branch 101 → 102 taken 100 times.
|
737 | for(auto const& r : scintillatorBank.getRowList()) { |
| 111 |
1/2✓ Branch 97 → 98 taken 637 times.
✗ Branch 97 → 209 not taken.
|
637 | scintillatorBank_sectors.push_back(scintillatorBank.getByte("sector", r)); |
| 112 |
1/4✓ Branch 99 → 100 taken 637 times.
✗ Branch 99 → 209 not taken.
✗ Branch 209 → 210 not taken.
✗ Branch 209 → 212 not taken.
|
637 | scintillatorBank_pindices.push_back(scintillatorBank.getShort("pindex", r)); |
| 113 | } | ||
| 114 | |||
| 115 | // loop over bank rows | ||
| 116 |
3/4✓ Branch 102 → 103 taken 100 times.
✗ Branch 102 → 209 not taken.
✓ Branch 122 → 104 taken 683 times.
✓ Branch 122 → 123 taken 100 times.
|
783 | for(auto const& row : particleBank.getRowList()) { |
| 117 | |||
| 118 | // check the PID with EventBuilderFilter | ||
| 119 | 683 | auto pid = particleBank.getInt("pid", row); | |
| 120 |
3/4✓ Branch 105 → 106 taken 683 times.
✗ Branch 105 → 209 not taken.
✓ Branch 106 → 107 taken 379 times.
✓ Branch 106 → 121 taken 304 times.
|
683 | 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 |
1/2✓ Branch 107 → 108 taken 379 times.
✗ Branch 107 → 209 not taken.
|
379 | 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 |
1/2✓ Branch 112 → 113 taken 379 times.
✗ Branch 112 → 209 not taken.
|
758 | auto [px, py, pz] = algo_momentum_correction.Transform( |
| 135 | 379 | particleBank.getFloat("px", row), | |
| 136 | 379 | particleBank.getFloat("py", row), | |
| 137 | 379 | particleBank.getFloat("pz", row), | |
| 138 | sector, | ||
| 139 | pid, | ||
| 140 | configBank.getFloat("torus", 0)); | ||
| 141 | |||
| 142 | // then print the result | ||
| 143 |
1/2✓ Branch 114 → 115 taken 379 times.
✗ Branch 114 → 209 not taken.
|
379 | fmt::print("Analysis Particle PDG = {}\n", pid); |
| 144 | 379 | fmt::print(" sector = {}\n", sector); | |
| 145 |
1/2✓ Branch 119 → 120 taken 379 times.
✗ Branch 119 → 209 not taken.
|
758 | fmt::print(" p_old = ({:11.5f}, {:11.5f}, {:11.5f})\n", particleBank.getFloat("px", row), particleBank.getFloat("py", row), particleBank.getFloat("pz", row)); |
| 146 | 379 | fmt::print(" p_new = ({:11.5f}, {:11.5f}, {:11.5f})\n", px, py, pz); | |
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | // stop the algorithms | ||
| 152 |
1/2✓ Branch 147 → 148 taken 1 time.
✗ Branch 147 → 233 not taken.
|
1 | algo_eventbuilder_filter.Stop(); |
| 153 |
1/2✓ Branch 148 → 149 taken 1 time.
✗ Branch 148 → 233 not taken.
|
1 | algo_sector_finder.Stop(); |
| 154 |
1/2✓ Branch 149 → 150 taken 1 time.
✗ Branch 149 → 233 not taken.
|
1 | algo_momentum_correction.Stop(); |
| 155 | return 0; | ||
| 156 | 1 | } | |
| 157 |