GCC Code Coverage Report


Directory: ./
File: examples/iguana_ex_cpp_00_run_functions_with_banks.cc
Date: 2025-11-25 17:57:04
Coverage Exec Excl Total
Lines: 96.2% 51 0 53
Functions: 100.0% 1 0 1
Branches: 51.6% 64 0 124

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 → 141 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 199 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 → 199 not taken.
1 reader.readDictionary(dict);
40
2/4
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 199 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 199 not taken.
1 hipo::bank bank_config(dict.getSchema("RUN::config"));
41
2/4
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 197 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 197 not taken.
1 hipo::bank bank_particle(dict.getSchema("REC::Particle"));
42
2/4
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 195 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 195 not taken.
1 hipo::bank bank_calorimeter(dict.getSchema("REC::Calorimeter"));
43
2/4
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 193 not taken.
✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 193 not taken.
1 hipo::bank bank_track(dict.getSchema("REC::Track"));
44
2/4
✓ Branch 26 → 27 taken 1 time.
✗ Branch 26 → 191 not taken.
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 191 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 → 189 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 → 187 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 → 185 not taken.
1 iguana::clas12::rga::MomentumCorrection algo_momentum_correction; // momentum corrections (a transformer algorithm)
53
54 // set log levels
55 // NOTE: this can also be done in a config file
56
2/4
✓ Branch 31 → 32 taken 1 time.
✗ Branch 31 → 183 not taken.
✓ Branch 32 → 33 taken 1 time.
✗ Branch 32 → 145 not taken.
1 algo_eventbuilder_filter.SetOption("log", "info");
57
2/4
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 183 not taken.
✓ Branch 39 → 40 taken 1 time.
✗ Branch 39 → 151 not taken.
1 algo_sector_finder.SetOption("log", "info");
58
2/4
✓ Branch 45 → 46 taken 1 time.
✗ Branch 45 → 183 not taken.
✓ Branch 46 → 47 taken 1 time.
✗ Branch 46 → 157 not taken.
1 algo_momentum_correction.SetOption("log", "info");
59
60 // set algorithm options
61 // NOTE: this can also be done in a config file, but setting options here OVERRIDES config file settings
62
4/10
✓ Branch 52 → 53 taken 1 time.
✗ Branch 52 → 183 not taken.
✓ Branch 53 → 54 taken 1 time.
✗ Branch 53 → 169 not taken.
✓ Branch 54 → 55 taken 1 time.
✗ Branch 54 → 163 not taken.
✓ Branch 63 → 64 taken 1 time.
✗ Branch 63 → 66 not taken.
✗ Branch 169 → 170 not taken.
✗ Branch 169 → 172 not taken.
2 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
63
64 // start the algorithms
65
1/2
✓ Branch 66 → 67 taken 1 time.
✗ Branch 66 → 183 not taken.
1 algo_eventbuilder_filter.Start();
66
1/2
✓ Branch 67 → 68 taken 1 time.
✗ Branch 67 → 183 not taken.
1 algo_sector_finder.Start();
67
1/2
✓ Branch 68 → 69 taken 1 time.
✗ Branch 68 → 183 not taken.
1 algo_momentum_correction.Start();
68
69 // define newly created bank object
70
1/2
✓ Branch 70 → 71 taken 1 time.
✗ Branch 70 → 173 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 76 → 114 taken 1 time.
✗ Branch 76 → 181 not taken.
1 hipo::event event;
75
5/8
✓ Branch 114 → 115 taken 101 times.
✗ Branch 114 → 179 not taken.
✓ Branch 115 → 116 taken 101 times.
✗ Branch 115 → 119 not taken.
✓ Branch 116 → 117 taken 101 times.
✗ Branch 116 → 118 not taken.
✓ Branch 117 → 118 taken 100 times.
✓ Branch 117 → 119 taken 1 time.
101 while(reader.next() && (numEvents == 0 || iEvent++ < numEvents)) {
76
77 // read the event's banks
78
1/2
✓ Branch 118 → 77 taken 100 times.
✗ Branch 118 → 179 not taken.
100 reader.read(event);
79
1/2
✓ Branch 77 → 78 taken 100 times.
✗ Branch 77 → 179 not taken.
100 event.getStructure(bank_config);
80
1/2
✓ Branch 78 → 79 taken 100 times.
✗ Branch 78 → 179 not taken.
100 event.getStructure(bank_particle);
81
1/2
✓ Branch 79 → 80 taken 100 times.
✗ Branch 79 → 179 not taken.
100 event.getStructure(bank_calorimeter);
82
1/2
✓ Branch 80 → 81 taken 100 times.
✗ Branch 80 → 179 not taken.
100 event.getStructure(bank_track);
83
1/2
✓ Branch 81 → 82 taken 100 times.
✗ Branch 81 → 179 not taken.
100 event.getStructure(bank_scintillator);
84
85 // print the event number
86
2/4
✓ Branch 83 → 84 taken 100 times.
✗ Branch 83 → 179 not taken.
✓ Branch 84 → 85 taken 100 times.
✗ Branch 84 → 179 not taken.
100 fmt::println("===== EVENT {} =====", bank_config.getInt("event", 0));
87
88 // print the particle bank before Iguana algorithms
89
1/2
✓ Branch 84 → 85 taken 100 times.
✗ Branch 84 → 179 not taken.
100 fmt::println("----- BEFORE IGUANA -----");
90
1/2
✓ Branch 85 → 86 taken 100 times.
✗ Branch 85 → 179 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 86 → 87 taken 100 times.
✗ Branch 86 → 179 not taken.
✓ Branch 87 → 88 taken 17 times.
✓ Branch 87 → 89 taken 83 times.
100 if(!algo_eventbuilder_filter.Run(bank_particle))
96 17 continue;
97
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 92 taken 83 times.
83 if(!algo_sector_finder.Run(bank_particle, bank_track, bank_calorimeter, bank_scintillator, bank_sector))
98 continue;
99
2/4
✓ Branch 92 → 93 taken 83 times.
✗ Branch 92 → 179 not taken.
✗ Branch 93 → 94 not taken.
✓ Branch 93 → 95 taken 83 times.
83 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 95 → 96 taken 83 times.
✗ Branch 95 → 179 not taken.
83 fmt::println("----- AFTER IGUANA -----");
104
1/2
✓ Branch 96 → 97 taken 83 times.
✗ Branch 96 → 179 not taken.
83 bank_particle.show(); // the filtered particle bank, with corrected momenta
105
1/2
✓ Branch 97 → 98 taken 83 times.
✗ Branch 97 → 179 not taken.
83 bank_sector.show(); // the new sector bank
106
107 // print a table; first the header
108
1/2
✓ Branch 99 → 100 taken 83 times.
✗ Branch 99 → 179 not taken.
83 fmt::print("----- Analysis Particles -----\n");
109 83 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 100 → 101 taken 83 times.
✗ Branch 100 → 179 not taken.
✓ Branch 110 → 102 taken 201 times.
✓ Branch 110 → 111 taken 83 times.
284 for(auto const& row : bank_particle.getRowList()) {
114 201 auto p = std::hypot(
115 bank_particle.getFloat("px", row),
116 bank_particle.getFloat("py", row),
117 bank_particle.getFloat("pz", row));
118 201 auto pdg = bank_particle.getInt("pid", row);
119 201 auto sector = bank_sector.getInt("sector", row);
120 201 fmt::print(" {:<20} {:<20} {:<20.3f} {:<20}\n", row, pdg, p, sector);
121 }
122 83 fmt::print("\n");
123 }
124
125 // stop algorithms
126
1/2
✓ Branch 119 → 120 taken 1 time.
✗ Branch 119 → 179 not taken.
1 algo_eventbuilder_filter.Stop();
127
1/2
✓ Branch 120 → 121 taken 1 time.
✗ Branch 120 → 179 not taken.
1 algo_sector_finder.Stop();
128
1/2
✓ Branch 121 → 122 taken 1 time.
✗ Branch 121 → 179 not taken.
1 algo_momentum_correction.Stop();
129 return 0;
130 3 }
131