Counter.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <momemta/Module.h>
20 
21 #include <vector>
22 
23 #include <momemta/ParameterSet.h>
24 #include <momemta/Types.h>
25 
31 template<typename T>
32 class Counter: public Module {
33  public:
34 
35  Counter(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
36  input = pool->get<std::vector<T>>(parameters.get<InputTag>("input"));
37  };
38 
39  virtual void beginIntegration() override {
40  *result = 0;
41  }
42 
43  virtual void beginLoop() override {
44  *result = 0;
45  }
46 
47  virtual Status work() override {
48  *result += input->size();
49 
50  return Status::OK;
51  }
52 
53  private:
54 
55  // Inputs
56  Value<std::vector<T>> input;
57 
58  // Outputs
59  std::shared_ptr<int64_t> result = produce<int64_t>("count");
60 
61 };
62 
63 REGISTER_MODULE_NAME("IntCounter", Counter<int64_t>)
64  .Input("input")
65  .Output("count");
66 REGISTER_MODULE_NAME("DoubleCounter", Counter<double>)
67  .Input("input")
68  .Output("count");
69 REGISTER_MODULE_NAME("LorentzVectorCounter", Counter<LorentzVector>)
70  .Input("input")
71  .Output("count");
72 
78 class SimpleCounter: public Module {
79  public:
80 
81  SimpleCounter(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
82  };
83 
84  virtual void beginIntegration() override {
85  *result = 0;
86  }
87 
88  virtual void beginLoop() override {
89  *result = 0;
90  }
91 
92  virtual Status work() override {
93  *result += 1;
94 
95  return Status::OK;
96  }
97 
98  private:
99 
100  // Outputs
101  std::shared_ptr<int64_t> result = produce<int64_t>("count");
102 
103 };
104 
105 REGISTER_MODULE(SimpleCounter)
106  .Output("count");
virtual void beginLoop() override
Called once at the beginning of a loop.
Definition: Counter.cc:43
A simple module counting the number of time it has been called.
Definition: Counter.cc:78
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
Parent class for all the modules.
Definition: Module.h:37
A class encapsulating a lua table.
Definition: ParameterSet.h:82
virtual Status work() override
Main function.
Definition: Counter.cc:92
A module counting the number of entries in a vector.
Definition: Counter.cc:32
virtual void beginIntegration() override
Called once at the beginning of the integration.
Definition: Counter.cc:39
A class representing a value produced by a module.
Definition: Value.h:30
Module(PoolPtr pool, const std::string &name)
Constructor.
Definition: Module.h:61
virtual Status work() override
Main function.
Definition: Counter.cc:47
virtual void beginIntegration() override
Called once at the beginning of the integration.
Definition: Counter.cc:84
virtual void beginLoop() override
Called once at the beginning of a loop.
Definition: Counter.cc:88