Belle II Software  release-05-02-19
ModuleParamList.templateDetails.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/ModuleParamList.h>
14 #include <framework/core/ModuleParam.h>
15 #include <framework/logging/Logger.h>
16 
17 #include <map>
18 #include <string>
19 
20 namespace Belle2 {
25  //======================================================
26  // Implementation of template based methods
27  //======================================================
28 
29  template <typename T>
30  void ModuleParamList::addParameter(const std::string& name,
31  T& paramVariable,
32  const std::string& description,
33  const T& defaultValue)
34  {
35  ModuleParamPtr newParam(new ModuleParam<T>(paramVariable, description, false));
36 
37  // Check if a parameter with the given name already exists
38  std::map<std::string, ModuleParamPtr>::iterator mapIter;
39  mapIter = m_paramMap.find(name);
40 
41  if (mapIter == m_paramMap.end()) {
42  m_paramMap.insert(std::make_pair(name, newParam));
43  ModuleParam<T>* explModParam = static_cast<ModuleParam<T>*>(newParam.get());
44  explModParam->setDefaultValue(defaultValue);
45  } else {
46  B2ERROR("A parameter with the name '" + name +
47  "' already exists! The name of a module parameter must be unique within a module.");
48  }
49  }
50 
51  template <typename T>
52  void ModuleParamList::addParameter(const std::string& name,
53  T& paramVariable,
54  const std::string& description)
55  {
56  ModuleParamPtr newParam(new ModuleParam<T>(paramVariable, description, true));
57 
58  // Check if a parameter with the given name already exists
59  std::map<std::string, ModuleParamPtr>::iterator mapIter;
60  mapIter = m_paramMap.find(name);
61 
62  if (mapIter == m_paramMap.end()) {
63  m_paramMap.insert(std::make_pair(name, newParam));
64  } else {
65  B2ERROR("A parameter with the name '" + name +
66  "' already exists! The name of a module parameter must be unique within a module.");
67  }
68  }
69 
70  template <typename T>
71  void ModuleParamList::setParameter(const std::string& name, const T& value)
72  {
73  try {
74  ModuleParam<T>& explModParam = getParameter<T>(name);
75  explModParam.setValue(value);
76  } catch (std::runtime_error& exc) {
77  B2ERROR(exc.what());
78  }
79  }
80 
81  template <typename T>
82  ModuleParam<T>& ModuleParamList::getParameter(const std::string& name) const
83  {
84  // Check if a parameter with the given name exists
85  std::map<std::string, ModuleParamPtr>::const_iterator mapIter;
86  mapIter = m_paramMap.find(name);
87 
88  if (mapIter != m_paramMap.end()) {
89  ModuleParamPtr moduleParam = mapIter->second;
90 
91  // Check the type of the stored parameter (currently done using the type identifier string)
92  if (moduleParam->getTypeInfo() == ModuleParam<T>::TypeInfo()) {
93  ModuleParam<T>* explModParam = static_cast<ModuleParam<T>*>(moduleParam.get());
94  return *explModParam;
95  } else
96  throwTypeError(name, moduleParam->getTypeInfo(), ModuleParam<T>::TypeInfo());
97  } else
98  throwNotFoundError(name);
99  }
100 
101  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102  // Python API
103  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104 
105  template <typename PythonObject>
106  void ModuleParamList::setParamPython(const std::string& name, const PythonObject& pyObj)
107  {
108 
110  p->setValueFromPythonObject(pyObj);
111  }
112 
113  template <typename PythonObject>
114  void ModuleParamList::getParamValuesPython(const std::string& name,
115  PythonObject& pyOutput,
116  bool defaultValues) const
117  {
118  try {
120  p->setValueToPythonObject(pyOutput, defaultValues);
121  } catch (std::runtime_error& exc) {
122  B2ERROR(exc.what());
123  }
124  }
125 
127 } // end of Belle2 namespace
Belle2::ModuleParamList::getParameterPtr
ModuleParamPtr getParameterPtr(const std::string &name) const
Returns a ModuleParamPtr to a parameter.
Definition: ModuleParamList.cc:126
Belle2::ModuleParamList::m_paramMap
std::map< std::string, ModuleParamPtr > m_paramMap
Stores the module parameters together with a string name as key.
Definition: ModuleParamList.h:216
Belle2::ModuleParam::setDefaultValue
void setDefaultValue(const T &defaultValue)
Sets the default value of a parameter.
Definition: ModuleParam.templateDetails.h:52
Belle2::ModuleParamList::throwTypeError
static void throwTypeError(const std::string &name, const std::string &expectedTypeInfo, const std::string &typeInfo)
Throws an error for a requested parameter that exists but was request with the wrong type.
Definition: ModuleParamList.cc:40
Belle2::ModuleParamList::getParameter
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
Definition: ModuleParamList.templateDetails.h:90
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ModuleParam
A single parameter of the module.
Definition: DisplayUI.h:34
Belle2::ModuleParamList::setParamPython
void setParamPython(const std::string &name, const PythonObject &pyObj)
Implements a method for setting boost::python objects.
Definition: ModuleParamList.templateDetails.h:114
Belle2::ModuleParamList::throwNotFoundError
static void throwNotFoundError(const std::string &name)
Throws an error for a requested parameter that does not exist.
Definition: ModuleParamList.cc:35
Belle2::ModuleParamPtr
std::shared_ptr< ModuleParamBase > ModuleParamPtr
Defines a pointer to a module parameter as a boost shared pointer. *‍/.
Definition: ModuleParamList.h:40
Belle2::ModuleParamList::setParameter
void setParameter(const std::string &name, const T &value)
Sets the value of a parameter given by its name.
Definition: ModuleParamList.templateDetails.h:79
Belle2::ModuleParamList::getParamValuesPython
void getParamValuesPython(const std::string &name, PythonObject &pyOutput, bool defaultValues) const
Returns a python object containing the value or default value of the given parameter.
Definition: ModuleParamList.templateDetails.h:122