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