GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 20 / 0 / 20
Functions: 100.0% 6 / 0 / 6
Branches: 53.1% 17 / 0 / 32

src/iguana/algorithms/example/ExampleAlgorithm/Algorithm.cc
Line Branch Exec Source
1 // ############################################################################
2 // # include the algorithm header
3 // ############################################################################
4 #include "Algorithm.h"
5
6 // ############################################################################
7 // # namespace must match that in the header
8 // ############################################################################
9 namespace iguana::example {
10
11 // ############################################################################
12 // # boilerplate to register an algorithm, so `AlgorithmFactory` knows about it
13 // # - this is a preprocessor macro call (see `../AlgorithmBoilerplate.h`)
14 // # - this must be done here in the source file, not in the header
15 // # - the argument is the class name; do not surround it with quotes
16 // # - usage of a semicolon at the end is optional, but recommended
17 // # - if this algorithm creates NEW banks, they also need to be registered by
18 // # adding additional string arguments for their names; you may add as many
19 // # new banks as you want
20 // ############################################################################
21 REGISTER_IGUANA_ALGORITHM(ExampleAlgorithm);
22 // REGISTER_IGUANA_ALGORITHM(ExampleAlgorithm , "example::newBank1", "example::newBank2"); // if this algorithm creates 2 new banks
23
24 // ############################################################################
25 // # define `ExampleAlgorithm::ConfigHook()`
26 // # - this is called by the main `Algorithm::Start` function, and is typically
27 // # used to load configuration parameters
28 // ############################################################################
29 4 void ExampleAlgorithm::ConfigHook()
30 {
31 // ############################################################################
32 // # get configuration options
33 // # - by default, this will read `ExampleAlgorithm.yaml`, unless the algorithm
34 // # user has specified to use a different configuration file or directory
35 // ############################################################################
36
4/8
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 21 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 19 not taken.
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 10 taken 4 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
8 o_exampleInt = GetOptionScalar<int>({"exampleInt"});
37
4/8
✓ Branch 11 → 12 taken 4 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 26 not taken.
✓ Branch 15 → 16 taken 4 times.
✓ Branch 15 → 18 taken 4 times.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 32 not taken.
8 o_exampleDouble = GetOptionScalar<double>({"exampleDouble"});
38 4 }
39
40 // ############################################################################
41 // # define `ExampleAlgorithm::StartHook()`
42 // # - this is called by the main `Algorithm::Start` function, after
43 // # `ConfigHook()` got called, so the configuration parameters are loaded
44 // # - in `StartHook`, we may do additional things, such as set bank indices
45 // # or create new banks
46 // ############################################################################
47 4 void ExampleAlgorithm::StartHook(hipo::banklist& banks)
48 {
49 // ############################################################################
50 // # get expected bank indices
51 // # - here we make sure that parameter `banks` includes the banks that are
52 // # required to run this algorithm
53 // # - we set the bank index values into the `b_*` members, to
54 // # avoid looking them up in the `Algorithm::Run` method
55 // ############################################################################
56
2/4
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 10 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 7 not taken.
4 b_particle = GetBankIndex(banks, "REC::Particle");
57 // ############################################################################
58 // # if this algorithm creates any new banks, use the `CreateBank` function;
59 // # see API documentation or other algorithms for its usage
60 // ############################################################################
61 // CreateBank(.....); // use this to create a new bank
62 4 }
63
64
65 // ############################################################################
66 // # define `ExampleAlgorithm::RunHook` function
67 // # - this is called by the main `Algorithm::Run` function
68 // # - this function that acts on `hipo::banklist` should just call a
69 // # "specialized" `Run` function that acts on `hipo::bank` objects
70 // ############################################################################
71 1000 bool ExampleAlgorithm::RunHook(hipo::banklist& banks) const
72 {
73 // ############################################################################
74 // # use `GetBank` to get the banks; here we just need `REC::Particle`
75 // ############################################################################
76
2/4
✓ Branch 3 → 4 taken 1000 times.
✗ Branch 3 → 11 not taken.
✓ Branch 4 → 5 taken 1000 times.
✗ Branch 4 → 11 not taken.
2000 return Run(GetBank(banks, b_particle, "REC::Particle"));
77 }
78
79 // ############################################################################
80 // # here is the `Run` function which acts on `hipo::bank` objects
81 // # - note that this method must be _thread safe_, for example, you cannot modify
82 // # class instance objects; therefore it _must_ be `const`
83 // # - try to avoid expensive operations here; instead, put them in the `Start` method
84 // # if it is reasonable to do so
85 // # - the function's `bool` return value can be used as an event-level filter
86 // ############################################################################
87 1000 bool ExampleAlgorithm::Run(hipo::bank& particleBank) const
88 {
89 // ############################################################################
90 // # dump the bank
91 // # - this will only happen if the log level for this algorithm is set low enough
92 // # - this provides a look at the bank _before_ the algorithm runs
93 // # - this is optional
94 // ############################################################################
95
1/2
✓ Branch 5 → 6 taken 1000 times.
✗ Branch 5 → 26 not taken.
2000 ShowBank(particleBank, Logger::Header("INPUT PARTICLES"));
96
97 // ############################################################################
98 // # loop over the bank rows
99 // # - since this example is a filtering algorithm, we call `getMutableRowList().filter`
100 // # - do NOT use `getRows()`, since that will loop over ALL bank rows; instead,
101 // # use `getRowList()`, which will take into consideration upstream filtering algorithms
102 // ############################################################################
103
1/2
✓ Branch 12 → 13 taken 1000 times.
✗ Branch 12 → 32 not taken.
1000 particleBank.getMutableRowList().filter([this, &particleBank](auto bank, auto row) {
104 // ############################################################################
105 // # get the `pid` and feed it to the `Filter` action function; if the row
106 // # is not acceptable, mask it out
107 // ############################################################################
108 6993 auto pid = particleBank.getInt("pid", row);
109 6993 auto accept = Filter(pid);
110 // ############################################################################
111 // # print a useful debugging method (see `Logger.h` for details, or other
112 // # algorithms for examples of how to use the logger)
113 // ############################################################################
114 6993 m_log->Debug("input PID {} -- accept = {}", pid, accept);
115
2/2
✓ Branch 5 → 6 taken 1427 times.
✓ Branch 5 → 7 taken 5566 times.
6993 return accept ? 1 : 0;
116 });
117
118 // ############################################################################
119 // # dump the modified bank (only if the log level is low enough); this is also optional
120 // ############################################################################
121
1/2
✓ Branch 18 → 19 taken 1000 times.
✗ Branch 18 → 35 not taken.
2000 ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES"));
122
123 // ############################################################################
124 // # return true or false, used as an event-level filter; in this case, we
125 // # return false if all particles have been filtered out
126 // ############################################################################
127 1000 return !particleBank.getRowList().empty();
128 }
129
130
131 // ############################################################################
132 // # define the action function
133 // ############################################################################
134 6993 bool ExampleAlgorithm::Filter(int const pid) const
135 {
136 6993 return pid > 0;
137 }
138
139 }
140