Module.h
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 
20 #pragma once
21 
22 #include <momemta/impl/Pool.h>
23 #include <momemta/InputTag.h>
24 #include <momemta/ModuleRegistry.h>
25 
37 class Module {
38  public:
39  // FIXME: Naming
40  enum class Status: std::int8_t {
41  OK,
42  NEXT,
43  ABORT
44  };
45 
46  class invalid_configuration: public std::runtime_error {
47  using std::runtime_error::runtime_error;
48  };
49 
50  static std::string statusToString(const Status& status);
51 
61  Module(PoolPtr pool, const std::string& name):
62  m_name(name), m_pool(pool) {
63  // Empty
64  }
65 
69  virtual void configure() { };
70 
74  virtual void beginIntegration() {};
75 
81  virtual void beginPoint() {};
82 
88  virtual void beginLoop() {};
89 
97  virtual Status work() { return Status::OK; };
98 
104  virtual void endLoop() {};
105 
111  virtual void endPoint() {};
112 
116  virtual void endIntegration() {};
117 
121  virtual void finish() { };
122 
123  virtual std::string name() const final {
124  return m_name;
125  }
126 
135  inline static bool is_virtual_module(const std::string& name) {
136  return (name == "momemta") || (name == "input") || (name == "cuba");
137  }
138 
139  protected:
161  template<typename T, typename... Args> std::shared_ptr<T> produce(const std::string& name, Args... args) {
162  return m_pool->put<T>({m_name, name}, std::forward<Args>(args)...);
163  }
164 
165  template<typename T> Value<T> get(const std::string& module, const std::string& name) {
166  return m_pool->get<T>({module, name});
167  }
168 
169  template<typename T> Value<T> get(const InputTag& tag) {
170  return m_pool->get<T>(tag);
171  }
172 
173  private:
174 
175  const std::string m_name;
176 
177  protected:
178 
179  PoolPtr m_pool;
180 };
181 
182 using ModulePtr = std::shared_ptr<Module>;
virtual void endIntegration()
Called once at the end of the integration.
Definition: Module.h:116
virtual void configure()
Called once at the beginning of the job.
Definition: Module.h:69
virtual void finish()
Called once at the end of the job.
Definition: Module.h:121
virtual Status work()
Main function.
Definition: Module.h:97
static bool is_virtual_module(const std::string &name)
Test if a given name correspond to a virtual module.
Definition: Module.h:135
virtual void beginLoop()
Called once at the beginning of a loop.
Definition: Module.h:88
virtual void beginPoint()
Called once when a new PS point is started.
Definition: Module.h:81
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
Parent class for all the modules.
Definition: Module.h:37
std::shared_ptr< T > produce(const std::string &name, Args... args)
Add a new output to the module.
Definition: Module.h:161
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 void endPoint()
Called once when a PS point is finished.
Definition: Module.h:111
virtual void endLoop()
Called once at the end of a loop.
Definition: Module.h:104
virtual void beginIntegration()
Called once at the beginning of the integration.
Definition: Module.h:74