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 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | o_exampleInt = GetOptionScalar<int>("exampleInt"); |
37 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | 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 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 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()` | ||
56 | // # - this overrides the virtual function `Algorithm::Run` | ||
57 | // # - note that this method must be _thread safe_, for example, you cannot modify | ||
58 | // # class instance objects | ||
59 | // # - try to avoid expensive operations here; instead, put them in the `Start` method | ||
60 | // # if it is reasonable to do so | ||
61 | // ############################################################################ | ||
62 | 1000 | void ExampleAlgorithm::Run(hipo::banklist& banks) const | |
63 | { | ||
64 | // ############################################################################ | ||
65 | // # get the banks; here we just need `REC::Particle` | ||
66 | // ############################################################################ | ||
67 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
2000 | auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); |
68 | // ############################################################################ | ||
69 | // # dump the bank | ||
70 | // # - this will only happen if the log level for this algorithm is set low enough | ||
71 | // # - this provides a look at the bank _before_ the algorithm runs | ||
72 | // # - this is optional | ||
73 | // ############################################################################ | ||
74 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); |
75 | |||
76 | // ############################################################################ | ||
77 | // # loop over the bank rows | ||
78 | // # - since this example is a filtering algorithm, we call `getMutableRowList().filter` | ||
79 | // # - do NOT use `getRows()`, since that will loop over ALL bank rows; instead, | ||
80 | // # use `getRowList()`, which will take into consideration upstream filtering algorithms | ||
81 | // ############################################################################ | ||
82 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
2000 | particleBank.getMutableRowList().filter([this, &particleBank](auto bank, auto row) { |
83 | // ############################################################################ | ||
84 | // # get the `pid` and feed it to the `Filter` action function; if the row | ||
85 | // # is not acceptable, mask it out | ||
86 | // ############################################################################ | ||
87 | 7143 | auto pid = particleBank.getInt("pid", row); | |
88 | 7143 | auto accept = Filter(pid); | |
89 | // ############################################################################ | ||
90 | // # print a useful debugging method (see `Logger.h` for details, or other | ||
91 | // # algorithms for examples of how to use the logger) | ||
92 | // ############################################################################ | ||
93 | 7143 | m_log->Debug("input PID {} -- accept = {}", pid, accept); | |
94 |
2/2✓ Branch 0 taken 1469 times.
✓ Branch 1 taken 5674 times.
|
7143 | return accept ? 1 : 0; |
95 | }); | ||
96 | |||
97 | // ############################################################################ | ||
98 | // # dump the modified bank (only if the log level is low enough); this is also optional | ||
99 | // ############################################################################ | ||
100 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); |
101 | 1000 | } | |
102 | |||
103 | |||
104 | // ############################################################################ | ||
105 | // # define the action function | ||
106 | // ############################################################################ | ||
107 | 7143 | bool ExampleAlgorithm::Filter(int const pid) const | |
108 | { | ||
109 | 7143 | return pid > 0; | |
110 | } | ||
111 | |||
112 | |||
113 | // ############################################################################ | ||
114 | // # define `ExampleAlgorithm::Stop()` | ||
115 | // # - this overrides the virtual function `Algorithm::Stop` | ||
116 | // # - in this example, there is nothing to do | ||
117 | // ############################################################################ | ||
118 | 1 | void ExampleAlgorithm::Stop() | |
119 | { | ||
120 | 1 | } | |
121 | |||
122 | } | ||
123 |