GCC Code Coverage Report


Directory: ./
File: examples/iguana_ex_cpp_01_action_functions.cc
Date: 2025-01-05 09:03:17
Exec Total Coverage
Lines: 32 32 100.0%
Functions: 1 1 100.0%
Branches: 40 76 52.6%

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 /// @end_doc_example
15 #include <hipo4/reader.h>
16 #include <iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h>
17 #include <iguana/algorithms/clas12/MomentumCorrection/Algorithm.h>
18
19 /// main function
20 1 int main(int argc, char** argv)
21 {
22
23 // parse arguments
24 int argi = 1;
25
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 char const* inFileName = argc > argi ? argv[argi++] : "data.hipo";
26
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 int const numEvents = argc > argi ? std::stoi(argv[argi++]) : 1;
27
28 // read input file
29
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 hipo::reader reader(inFileName,{0});
30
31 // set banks
32
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 hipo::banklist banks = reader.getBanks({"REC::Particle", "RUN::config"});
33 enum banks_enum { b_particle,
34 b_config }; // TODO: users shouldn't have to do this
35
36 // set the concurrency model to single-threaded, since this example is single-threaded;
37 // not doing this will use the thread-safe model, `"memoize"`
38
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 iguana::GlobalConcurrencyModel = "single";
39
40 // create the algorithms
41
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
42
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 iguana::clas12::MomentumCorrection algo_momentum_correction;
43
44 // set log levels
45
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 algo_eventbuilder_filter.SetOption("log", "debug");
46
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 algo_momentum_correction.SetOption("log", "debug");
47
48 // set algorithm options
49
4/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
2 algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
50
51 // start the algorithms
52
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 algo_eventbuilder_filter.Start();
53
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 algo_momentum_correction.Start();
54
55 // run the algorithm sequence on each event
56 int iEvent = 0;
57
5/8
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 100 times.
✓ Branch 7 taken 1 times.
101 while(reader.next(banks) && (numEvents == 0 || iEvent++ < numEvents)) {
58
59 // show the particle bank
60
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 auto& particleBank = banks.at(b_particle);
61
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 auto& configBank = banks.at(b_config);
62
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 particleBank.show();
63
64 // loop over bank rows
65
3/4
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 702 times.
✓ Branch 3 taken 100 times.
802 for(auto const& row : particleBank.getRowList()) {
66
67 // check the PID with EventBuilderFilter
68 702 auto pid = particleBank.getInt("pid", row);
69
3/4
✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 193 times.
✓ Branch 3 taken 509 times.
702 if(algo_eventbuilder_filter.Filter(pid)) {
70
71 int sector = 1; // FIXME: get the sector number. The algorithm `clas12::SectorFinder` can do this, however
72 // it requires reading full `hipo::bank` objects, whereas this example is meant to demonstrate
73 // `iguana` usage operating _only_ on bank row elements
74
75 // if accepted PID, correct its momentum
76
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
386 auto [px, py, pz] = algo_momentum_correction.Transform(
77 193 particleBank.getFloat("px", row),
78 193 particleBank.getFloat("py", row),
79 193 particleBank.getFloat("pz", row),
80 sector,
81 pid,
82 configBank.getFloat("torus", 0));
83
84 // then print the result
85
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
193 fmt::print("Accepted PID {}:\n", pid);
86 auto printMomentum = [](auto v1, auto v2)
87
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
386 { fmt::print(" {:>20} {:>20}\n", v1, v2); };
88 printMomentum("p_old", "p_new");
89 printMomentum("--------", "--------");
90 193 printMomentum(particleBank.getFloat("px", row), px);
91 193 printMomentum(particleBank.getFloat("py", row), py);
92 193 printMomentum(particleBank.getFloat("pz", row), pz);
93 }
94 }
95 }
96
97 // stop the algorithms
98
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 algo_eventbuilder_filter.Stop();
99
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 algo_momentum_correction.Stop();
100 return 0;
101 1 }
102