Belle II Software  release-05-02-19
Manager.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014-2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Keck, Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <analysis/DecayDescriptor/DecayDescriptor.h>
14 
15 #include <string>
16 #include <map>
17 #include <vector>
18 #include <functional>
19 #include <memory>
20 
21 namespace Belle2 {
26  class Particle;
27 
28  namespace Variable {
100  class Manager {
108  public:
110  typedef std::function<double(const Particle*)> FunctionPtr;
112  typedef std::function<double(const Particle*, const std::vector<double>&)> ParameterFunctionPtr;
114  typedef std::function<FunctionPtr(const std::vector<std::string>&)> MetaFunctionPtr;
116  typedef Particle Object;
117 
119  struct VarBase {
120  std::string name;
121  std::string description;
122  std::string group;
124  VarBase(const std::string& n, const std::string& d, const std::string& g)
125  : name(n), description(d), group(g) { }
126  };
127 
129  struct Var : public VarBase {
130  FunctionPtr function;
132  Var(const std::string& n, FunctionPtr f, const std::string& d, const std::string& g = "")
133  : VarBase(n, d, g), function(f) { }
134  };
135 
137  struct ParameterVar : public VarBase {
140  ParameterVar(const std::string& n, ParameterFunctionPtr f, const std::string& d, const std::string& g = "")
141  : VarBase(n, d, g), function(f) { }
142  };
143 
145  struct MetaVar : public VarBase {
146  MetaFunctionPtr function;
148  explicit MetaVar(const std::string& n, MetaFunctionPtr f, const std::string& d, const std::string& g = "")
149  : VarBase(n, d, g), function(f) { }
150  };
151 
153  static Manager& Instance();
154 
159  const Var* getVariable(std::string name);
160 
164  std::vector<const Belle2::Variable::Manager::Var*> getVariables(const std::vector<std::string>& variables);
165 
169  bool addAlias(const std::string& alias, const std::string& variable);
170 
174  void printAliases();
175 
179  bool addCollection(const std::string& collection, const std::vector<std::string>& variables);
180 
184  std::vector<std::string> getCollection(const std::string& collection);
185 
189  std::vector<std::string> resolveCollections(const std::vector<std::string>& variables);
190 
192  std::vector<const Belle2::Variable::Manager::VarBase*> getVariables() const { return m_variablesInRegistrationOrder; }
193 
194 
196  void setVariableGroup(const std::string& groupName);
197 
199  void registerVariable(const std::string& name, const Manager::FunctionPtr& f, const std::string& description);
201  void registerVariable(const std::string& name, const Manager::ParameterFunctionPtr& f, const std::string& description);
203  void registerVariable(const std::string& name, const Manager::MetaFunctionPtr& f, const std::string& description);
204 
211  double evaluate(const std::string& varName, const Particle* p);
212 
214  std::vector<std::string> getNames() const;
215 
217  std::vector<std::string> getAliasNames() const;
218 
220  void assertValidName(const std::string& name);
221 
222  private:
223  Manager() {};
225  Manager(const Manager&);
226  ~Manager();
227 
229  bool createVariable(const std::string& name);
230 
232  std::string m_currentGroup;
233 
235  std::vector<const VarBase*> m_variablesInRegistrationOrder;
236 
238  std::map<std::string, std::string> m_alias;
240  std::map<std::string, std::vector<std::string>> m_collection;
242  std::map<std::string, std::shared_ptr<Var>> m_variables;
244  std::map<std::string, std::shared_ptr<ParameterVar>> m_parameter_variables;
246  std::map<std::string, std::shared_ptr<MetaVar>> m_meta_variables;
247  };
248 
250  class Proxy {
251  public:
253  Proxy(const std::string& name, Manager::FunctionPtr f, const std::string& description)
254  {
255  Manager::Instance().registerVariable(name, f, description);
256  }
258  Proxy(const std::string& name, Manager::ParameterFunctionPtr f, const std::string& description)
259  {
260  Manager::Instance().registerVariable(name, f, description);
261  }
263  Proxy(const std::string& name, Manager::MetaFunctionPtr f, const std::string& description)
264  {
265  Manager::Instance().registerVariable(name, f, description);
266  }
267  };
268 
270  class GroupProxy {
271  public:
273  explicit GroupProxy(const std::string& groupName)
274  {
275  Manager::Instance().setVariableGroup(groupName);
276  }
277  };
278 
279  template<typename T>
280  std::function<T> make_function(T* t)
281  {
282  return { t };
283  }
284 
285 
289 #define VARMANAGER_CONCATENATE_DETAIL(x, y) x##y
290 
293 #define VARMANAGER_CONCATENATE(x, y) VARMANAGER_CONCATENATE_DETAIL(x, y)
294 
297 #define VARMANAGER_MAKE_UNIQUE(x) VARMANAGER_CONCATENATE(x, __LINE__)
298 
304 #define REGISTER_VARIABLE(name, function, description) \
305  static Proxy VARMANAGER_MAKE_UNIQUE(_variableproxy)(std::string(name), Belle2::Variable::make_function(function), std::string(description));
306 
312 #define VARIABLE_GROUP(groupName) \
313  static GroupProxy VARMANAGER_MAKE_UNIQUE(_variablegroupproxy)(groupName);
314 
315  }
317 }
Belle2::Variable::Manager::registerVariable
void registerVariable(const std::string &name, const Manager::FunctionPtr &f, const std::string &description)
Register a variable.
Definition: Manager.cc:228
Belle2::Variable::Manager::Var
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:137
Belle2::Variable::Manager::createVariable
bool createVariable(const std::string &name)
Creates and registers a concrete variable (Var) from a MetaVar, ParameterVar or numeric constant.
Definition: Manager.cc:173
Belle2::Variable::Manager::getNames
std::vector< std::string > getNames() const
Return list of all variable names (in order registered).
Definition: Manager.cc:289
Belle2::Variable::Manager::Var::Var
Var(const std::string &n, FunctionPtr f, const std::string &d, const std::string &g="")
ctor
Definition: Manager.h:140
Belle2::Variable::Manager::getVariable
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:33
Belle2::Variable::Manager::m_variablesInRegistrationOrder
std::vector< const VarBase * > m_variablesInRegistrationOrder
List of variables in registration order.
Definition: Manager.h:243
Belle2::Variable::Manager::m_meta_variables
std::map< std::string, std::shared_ptr< MetaVar > > m_meta_variables
List of registered meta variables.
Definition: Manager.h:254
Belle2::Variable::Proxy
Internal class that registers a variable with Manager when constructed.
Definition: Manager.h:258
Belle2::Variable::Manager::m_alias
std::map< std::string, std::string > m_alias
List of registered aliases.
Definition: Manager.h:246
Belle2::Variable::Manager::m_currentGroup
std::string m_currentGroup
Group last set via VARIABLE_GROUP().
Definition: Manager.h:240
Belle2::Variable::Manager::getVariables
std::vector< const Belle2::Variable::Manager::VarBase * > getVariables() const
Return list of all variables (in order registered).
Definition: Manager.h:200
Belle2::Variable::Manager::VarBase::VarBase
VarBase(const std::string &n, const std::string &d, const std::string &g)
ctor
Definition: Manager.h:132
Belle2::Variable::Manager::MetaVar::MetaVar
MetaVar(const std::string &n, MetaFunctionPtr f, const std::string &d, const std::string &g="")
ctor
Definition: Manager.h:156
Belle2::Variable::Manager::addAlias
bool addAlias(const std::string &alias, const std::string &variable)
Add alias Return true if the alias was successfully added.
Definition: Manager.cc:71
Belle2::Variable::Manager::m_collection
std::map< std::string, std::vector< std::string > > m_collection
List of registered collections.
Definition: Manager.h:248
Belle2::Variable::Manager::Object
Particle Object
Typedef for the cut, that we use Particles as outr base objects.
Definition: Manager.h:124
Belle2::Variable::Manager::Var::function
FunctionPtr function
Pointer to function.
Definition: Manager.h:138
Belle2::Variable::Manager::printAliases
void printAliases()
Print existing aliases.
Definition: Manager.cc:94
Belle2::Variable::Manager::evaluate
double evaluate(const std::string &varName, const Particle *p)
evaluate variable 'varName' on given Particle.
Definition: Manager.cc:305
Belle2::Variable::Manager::ParameterFunctionPtr
std::function< double(const Particle *, const std::vector< double > &)> ParameterFunctionPtr
parameter functions stored take a const Particle*, const std::vector<double>& and return double.
Definition: Manager.h:120
Belle2::Variable::Manager::ParameterVar::ParameterVar
ParameterVar(const std::string &n, ParameterFunctionPtr f, const std::string &d, const std::string &g="")
ctor
Definition: Manager.h:148
Belle2::Variable::Manager::VarBase::name
std::string name
Unique identifier of the function, used as key.
Definition: Manager.h:128
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Variable::Manager::FunctionPtr
std::function< double(const Particle *)> FunctionPtr
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:118
Belle2::Variable::Manager::VarBase::group
std::string group
Associated group.
Definition: Manager.h:130
Belle2::Variable::Manager::addCollection
bool addCollection(const std::string &collection, const std::vector< std::string > &variables)
Add collection Return true if the collection was successfully added.
Definition: Manager.cc:111
Belle2::Variable::Manager::MetaVar
A variable taking string arguments returning a variable.
Definition: Manager.h:153
Belle2::Variable::Manager::MetaVar::function
MetaFunctionPtr function
Pointer to function.
Definition: Manager.h:154
Belle2::Variable::Manager::m_parameter_variables
std::map< std::string, std::shared_ptr< ParameterVar > > m_parameter_variables
List of registered parameter variables.
Definition: Manager.h:252
Belle2::Variable::Manager::setVariableGroup
void setVariableGroup(const std::string &groupName)
All variables registered after VARIABLE_GROUP(groupName) will be added to this group.
Definition: Manager.cc:168
Belle2::Variable::Manager::MetaFunctionPtr
std::function< FunctionPtr(const std::vector< std::string > &)> MetaFunctionPtr
meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.
Definition: Manager.h:122
Belle2::Variable::Manager::m_variables
std::map< std::string, std::shared_ptr< Var > > m_variables
List of registered variables.
Definition: Manager.h:250
Belle2::Variable::Manager::getAliasNames
std::vector< std::string > getAliasNames() const
Return a list of all variable alias names (in reverse order added).
Definition: Manager.cc:298
Belle2::Variable::Manager::resolveCollections
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
Definition: Manager.cc:139
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::Variable::Manager::VarBase
Base class for information common to all types of variables.
Definition: Manager.h:127
Belle2::Variable::Manager::assertValidName
void assertValidName(const std::string &name)
Abort with B2FATAL if name is not a valid name for a variable.
Definition: Manager.cc:157
Belle2::Variable::Manager::ParameterVar::function
ParameterFunctionPtr function
Pointer to function.
Definition: Manager.h:146
Belle2::Variable::Proxy::Proxy
Proxy(const std::string &name, Manager::FunctionPtr f, const std::string &description)
constructor.
Definition: Manager.h:261
Belle2::Variable::Manager::ParameterVar
A variable taking additional floating-point arguments to influence the behaviour.
Definition: Manager.h:145
Belle2::Variable::GroupProxy::GroupProxy
GroupProxy(const std::string &groupName)
constructor.
Definition: Manager.h:281
Belle2::Variable::Manager::VarBase::description
std::string description
Description of what this function does.
Definition: Manager.h:129
Belle2::Variable::Manager::getCollection
std::vector< std::string > getCollection(const std::string &collection)
Get Collection Returns variable names corresponding to the given collection.
Definition: Manager.cc:132
Belle2::Variable::Manager
Global list of available variables.
Definition: Manager.h:108
Belle2::Variable::Manager::Instance
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:27