Looper.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/config.h>
24 #include <momemta/ParameterSet.h>
25 #include <momemta/Solution.h>
26 
27 #include <Path.h>
28 
29 #ifdef DEBUG_TIMING
30 #include <chrono>
31 using namespace std::chrono;
32 #endif
33 
34 #define CALL(X) { for (auto& m: path.modules()) \
35  m->X(); \
36  }
37 
113 class Looper: public Module {
114  public:
115 
116  Looper(PoolPtr pool, const ParameterSet& parameters): Module(pool, parameters.getModuleName()) {
117  solutions = pool->get<SolutionCollection>(parameters.get<InputTag>("solutions"));
118 
119  path = parameters.get<Path>("path");
120  };
121 
122  virtual void configure() override {
123  CALL(configure);
124  }
125 
126  virtual void beginIntegration() override {
127  CALL(beginIntegration);
128  }
129 
130  virtual void endIntegration() override {
131  CALL(endIntegration);
132 
133 #ifdef DEBUG_TIMING
134  LOG(info) << "Time spent evaluating modules of looper " << name() << ":";
135  for (auto it: m_timings) {
136  LOG(info) << " " << it.first->name() << ": " << duration_cast<duration<double>>(it.second).count() << "s";
137  }
138 #endif
139  }
140 
141  virtual void beginPoint() override {
142  CALL(beginPoint);
143  }
144 
145  virtual void endPoint() override {
146  CALL(endPoint);
147  }
148 
149  virtual Status work() override {
150  particles->clear();
151 
152  CALL(beginLoop);
153 
154  auto status = Status::OK;
155 
156  // For each solution, loop over all the modules
157  for (const auto& s: *solutions) {
158  if (!s.valid)
159  continue;
160 
161  *particles = s.values;
162  *jacobian = s.jacobian;
163 
164  for (auto& m: path.modules()) {
165 #ifdef DEBUG_TIMING
166  auto start = high_resolution_clock::now();
167 #endif
168  auto module_status = m->work();
169 #ifdef DEBUG_TIMING
170  m_timings[m.get()] += high_resolution_clock::now() - start;
171 #endif
172 
173  if (module_status == Status::OK)
174  continue;
175  else if (module_status == Status::NEXT)
176  break;
177  else {
178  status = module_status;
179  break;
180  }
181  }
182 
183  if (status != Status::OK)
184  break;
185  }
186 
187  CALL(endLoop);
188 
189  return status;
190  }
191 
192  private:
193  Path path;
194 
195  // Inputs
196  Value<SolutionCollection> solutions;
197 
198  // Outputs
199  std::shared_ptr<std::vector<LorentzVector>> particles = produce<std::vector<LorentzVector>>("particles");
200  std::shared_ptr<double> jacobian = produce<double>("jacobian");
201 
202 #ifdef DEBUG_TIMING
203  std::unordered_map<Module*, std::chrono::high_resolution_clock::duration> m_timings;
204 #endif
205 
206 };
207 
208 REGISTER_MODULE(Looper)
209  .Input("solutions")
210  .Output("particles")
211  .Output("jacobian")
212  .Attr("path:path");
A module looping over a set of solutions.
Definition: Looper.cc:113
virtual void beginIntegration() override
Called once at the beginning of the integration.
Definition: Looper.cc:126
virtual void beginPoint() override
Called once when a new PS point is started.
Definition: Looper.cc:141
virtual Status work() override
Main function.
Definition: Looper.cc:149
An execution path.
Definition: Path.h:37
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 void endIntegration() override
Called once at the end of the integration.
Definition: Looper.cc:130
virtual void endPoint() override
Called once when a PS point is finished.
Definition: Looper.cc:145
virtual void configure() override
Called once at the beginning of the job.
Definition: Looper.cc:122