examples/iguana_ex_cpp_00_run_functions_with_banks.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @begin_doc_example{cpp} | ||
| 2 | /// @file iguana_ex_cpp_00_run_functions_with_banks.cc | ||
| 3 | /// @brief Example using **full HIPO banks** with Iguana algorithms' `Run` functions, using `hipo::bank` | ||
| 4 | /// | ||
| 5 | /// This example requires the user to have the C++ `hipo::bank` objects. | ||
| 6 | /// | ||
| 7 | /// - see `iguana_ex_cpp_00_run_functions.cc` if you prefer `hipo::banklist` | ||
| 8 | /// - see other examples if you do not have `hipo::bank` objects | ||
| 9 | /// | ||
| 10 | /// @par Usage | ||
| 11 | /// ```bash | ||
| 12 | /// iguana_ex_cpp_00_run_functions_with_banks [HIPO_FILE] [NUM_EVENTS] | ||
| 13 | /// | ||
| 14 | /// HIPO_FILE the HIPO file to analyze | ||
| 15 | /// | ||
| 16 | /// NUM_EVENTS the number of events to analyze; | ||
| 17 | /// set to zero to analyze all events | ||
| 18 | /// ``` | ||
| 19 | /// @end_doc_example | ||
| 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 | |||
| 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 |
2/4✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 115 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 151 not taken.
|
2 | hipo::reader reader(inFileName, {0}); |
| 36 | |||
| 37 | // set list of banks to be read | ||
| 38 | hipo::dictionary dict; | ||
| 39 |
1/2✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 151 not taken.
|
1 | reader.readDictionary(dict); |
| 40 |
2/4✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 151 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 151 not taken.
|
1 | hipo::bank bank_config(dict.getSchema("RUN::config")); |
| 41 |
2/4✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 149 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 149 not taken.
|
1 | hipo::bank bank_particle(dict.getSchema("REC::Particle")); |
| 42 |
2/4✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 147 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 147 not taken.
|
1 | hipo::bank bank_calorimeter(dict.getSchema("REC::Calorimeter")); |
| 43 |
2/4✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 145 not taken.
✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 145 not taken.
|
1 | hipo::bank bank_track(dict.getSchema("REC::Track")); |
| 44 |
2/4✓ Branch 26 → 27 taken 1 time.
✗ Branch 26 → 143 not taken.
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 143 not taken.
|
1 | 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 |
1/2✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 141 not taken.
|
1 | iguana::clas12::EventBuilderFilter algo_eventbuilder_filter; // filter by Event Builder PID (a filter algorithm) |
| 51 |
1/2✓ Branch 29 → 30 taken 1 time.
✗ Branch 29 → 139 not taken.
|
1 | iguana::clas12::SectorFinder algo_sector_finder; // get the sector for each particle (a creator algorithm) |
| 52 |
1/2✓ Branch 30 → 31 taken 1 time.
✗ Branch 30 → 137 not taken.
|
1 | iguana::clas12::rga::MomentumCorrection algo_momentum_correction; // momentum corrections (a transformer algorithm) |
| 53 | |||
| 54 | // configure algorithms with a custom YAML file | ||
| 55 | // - in practice, specify the path(s) to your preferred configuration file(s); see documentation | ||
| 56 | // on 'How to Configure Algorithms' for details, and alternative methods for algorithm configuration | ||
| 57 | // - in this example, the file is from the source-code path `./config/examples/`, which was copied to | ||
| 58 | // the installation subdirectory `etc/iguana/`, within the default configuration-file search path | ||
| 59 |
1/2✓ Branch 31 → 32 taken 1 time.
✗ Branch 31 → 135 not taken.
|
1 | std::string config_file = "examples/config_for_examples.yaml"; |
| 60 |
1/2✓ Branch 32 → 33 taken 1 time.
✗ Branch 32 → 129 not taken.
|
1 | algo_eventbuilder_filter.SetConfigFile(config_file); |
| 61 |
1/2✓ Branch 33 → 34 taken 1 time.
✗ Branch 33 → 129 not taken.
|
1 | algo_sector_finder.SetConfigFile(config_file); |
| 62 |
1/2✓ Branch 34 → 35 taken 1 time.
✗ Branch 34 → 129 not taken.
|
1 | algo_momentum_correction.SetConfigFile(config_file); |
| 63 | |||
| 64 | // start the algorithms | ||
| 65 |
1/2✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 129 not taken.
|
1 | algo_eventbuilder_filter.Start(); |
| 66 |
1/2✓ Branch 36 → 37 taken 1 time.
✗ Branch 36 → 129 not taken.
|
1 | algo_sector_finder.Start(); |
| 67 |
1/2✓ Branch 37 → 38 taken 1 time.
✗ Branch 37 → 129 not taken.
|
1 | algo_momentum_correction.Start(); |
| 68 | |||
| 69 | // define newly created bank object | ||
| 70 |
1/2✓ Branch 39 → 40 taken 1 time.
✗ Branch 39 → 119 not taken.
|
1 | hipo::bank bank_sector = algo_sector_finder.GetCreatedBank(); |
| 71 | |||
| 72 | // run the algorithm sequence on each event | ||
| 73 | int iEvent = 0; | ||
| 74 |
1/2✓ Branch 45 → 83 taken 1 time.
✗ Branch 45 → 127 not taken.
|
1 | hipo::event event; |
| 75 |
5/8✓ Branch 83 → 84 taken 101 times.
✗ Branch 83 → 125 not taken.
✓ Branch 84 → 85 taken 101 times.
✗ Branch 84 → 88 not taken.
✓ Branch 85 → 86 taken 101 times.
✗ Branch 85 → 87 not taken.
✓ Branch 86 → 87 taken 100 times.
✓ Branch 86 → 88 taken 1 time.
|
101 | while(reader.next() && (numEvents == 0 || iEvent++ < numEvents)) { |
| 76 | |||
| 77 | // read the event's banks | ||
| 78 |
1/2✓ Branch 87 → 46 taken 100 times.
✗ Branch 87 → 125 not taken.
|
100 | reader.read(event); |
| 79 |
1/2✓ Branch 46 → 47 taken 100 times.
✗ Branch 46 → 125 not taken.
|
100 | event.getStructure(bank_config); |
| 80 |
1/2✓ Branch 47 → 48 taken 100 times.
✗ Branch 47 → 125 not taken.
|
100 | event.getStructure(bank_particle); |
| 81 |
1/2✓ Branch 48 → 49 taken 100 times.
✗ Branch 48 → 125 not taken.
|
100 | event.getStructure(bank_calorimeter); |
| 82 |
1/2✓ Branch 49 → 50 taken 100 times.
✗ Branch 49 → 125 not taken.
|
100 | event.getStructure(bank_track); |
| 83 |
1/2✓ Branch 50 → 51 taken 100 times.
✗ Branch 50 → 125 not taken.
|
100 | event.getStructure(bank_scintillator); |
| 84 | |||
| 85 | // print the event number | ||
| 86 |
2/4✓ Branch 52 → 53 taken 100 times.
✗ Branch 52 → 125 not taken.
✓ Branch 53 → 54 taken 100 times.
✗ Branch 53 → 125 not taken.
|
100 | fmt::println("===== EVENT {} =====", bank_config.getInt("event", 0)); |
| 87 | |||
| 88 | // print the particle bank before Iguana algorithms | ||
| 89 |
1/2✓ Branch 53 → 54 taken 100 times.
✗ Branch 53 → 125 not taken.
|
100 | fmt::println("----- BEFORE IGUANA -----"); |
| 90 |
1/2✓ Branch 54 → 55 taken 100 times.
✗ Branch 54 → 125 not taken.
|
100 | 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 |
3/4✓ Branch 55 → 56 taken 100 times.
✗ Branch 55 → 125 not taken.
✓ Branch 56 → 57 taken 7 times.
✓ Branch 56 → 58 taken 93 times.
|
100 | if(!algo_eventbuilder_filter.Run(bank_particle)) |
| 96 | 7 | continue; | |
| 97 |
1/2✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 93 times.
|
93 | if(!algo_sector_finder.Run(bank_particle, bank_track, bank_calorimeter, bank_scintillator, bank_sector)) |
| 98 | ✗ | continue; | |
| 99 |
2/4✓ Branch 61 → 62 taken 93 times.
✗ Branch 61 → 125 not taken.
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 93 times.
|
93 | if(!algo_momentum_correction.Run(bank_particle, bank_sector, bank_config)) |
| 100 | ✗ | continue; | |
| 101 | |||
| 102 | // print the banks after Iguana algorithms | ||
| 103 |
1/2✓ Branch 64 → 65 taken 93 times.
✗ Branch 64 → 125 not taken.
|
93 | fmt::println("----- AFTER IGUANA -----"); |
| 104 |
1/2✓ Branch 65 → 66 taken 93 times.
✗ Branch 65 → 125 not taken.
|
93 | bank_particle.show(); // the filtered particle bank, with corrected momenta |
| 105 |
1/2✓ Branch 66 → 67 taken 93 times.
✗ Branch 66 → 125 not taken.
|
93 | bank_sector.show(); // the new sector bank |
| 106 | |||
| 107 | // print a table; first the header | ||
| 108 |
1/2✓ Branch 68 → 69 taken 93 times.
✗ Branch 68 → 125 not taken.
|
93 | fmt::print("----- Analysis Particles -----\n"); |
| 109 | 93 | 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 |
3/4✓ Branch 69 → 70 taken 93 times.
✗ Branch 69 → 125 not taken.
✓ Branch 79 → 71 taken 379 times.
✓ Branch 79 → 80 taken 93 times.
|
472 | for(auto const& row : bank_particle.getRowList()) { |
| 114 | 379 | auto p = std::hypot( | |
| 115 | bank_particle.getFloat("px", row), | ||
| 116 | bank_particle.getFloat("py", row), | ||
| 117 | bank_particle.getFloat("pz", row)); | ||
| 118 | 379 | auto pdg = bank_particle.getInt("pid", row); | |
| 119 | 379 | auto sector = bank_sector.getInt("sector", row); | |
| 120 | 379 | fmt::print(" {:<20} {:<20} {:<20.3f} {:<20}\n", row, pdg, p, sector); | |
| 121 | } | ||
| 122 | 93 | fmt::print("\n"); | |
| 123 | } | ||
| 124 | |||
| 125 | // stop algorithms | ||
| 126 |
1/2✓ Branch 88 → 89 taken 1 time.
✗ Branch 88 → 125 not taken.
|
1 | algo_eventbuilder_filter.Stop(); |
| 127 |
1/2✓ Branch 89 → 90 taken 1 time.
✗ Branch 89 → 125 not taken.
|
1 | algo_sector_finder.Stop(); |
| 128 |
1/2✓ Branch 90 → 91 taken 1 time.
✗ Branch 90 → 125 not taken.
|
1 | algo_momentum_correction.Stop(); |
| 129 | return 0; | ||
| 130 | 3 | } | |
| 131 |