Belle II Software  release-05-02-19
ModuleParamList.cc
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 #include <framework/core/ModuleParamList.templateDetails.h>
11 
12 #include <framework/core/ModuleParamInfoPython.h>
13 #include <framework/core/FrameworkExceptions.h>
14 
15 #include <boost/python/object.hpp>
16 #include <boost/python/list.hpp>
17 
18 #include <utility>
19 #include <memory>
20 
21 using namespace Belle2;
22 
24 BELLE2_DEFINE_EXCEPTION(ModuleParameterNotFoundError,
25  "Could not find the parameter with the "
26  "name '%1%'! The value of the parameter "
27  "could NOT be set.");
28 
30 BELLE2_DEFINE_EXCEPTION(ModuleParameterTypeError,
31  "The type of the module parameter '%1%' "
32  "(%2%) is different from the type of the "
33  "value it should be set to (%3%)!");
34 
35 void ModuleParamList::throwNotFoundError(const std::string& name)
36 {
37  throw (ModuleParameterNotFoundError() << name);
38 }
39 
40 void ModuleParamList::throwTypeError(const std::string& name,
41  const std::string& expectedTypeInfo,
42  const std::string& typeInfo)
43 {
44  throw (ModuleParameterTypeError() << name << expectedTypeInfo << typeInfo);
45 }
46 
48 
49 
51 {
52  m_paramMap.clear();
53 }
54 
55 
56 std::vector<std::string> ModuleParamList::getUnsetForcedParams() const
57 {
58  std::vector<std::string> missingParam;
59  for (const auto& mapEntry : m_paramMap) {
60  if (mapEntry.second->isForcedInSteering() && !mapEntry.second->isSetInSteering())
61  missingParam.push_back(mapEntry.first);
62  }
63  return missingParam;
64 }
65 
66 std::shared_ptr<boost::python::list> ModuleParamList::getParamInfoListPython() const
67 {
68  std::shared_ptr<boost::python::list> returnList(new boost::python::list);
69  std::map<std::string, ModuleParamPtr>::const_iterator mapIter;
70 
71  for (mapIter = m_paramMap.begin(); mapIter != m_paramMap.end(); ++mapIter) {
72  ModuleParamInfoPython newParamInfo;
73  ModuleParamPtr currParam = mapIter->second;
74 
75  newParamInfo.m_name = mapIter->first;
76  newParamInfo.m_description = currParam->getDescription();
77  newParamInfo.m_typeName = currParam->getTypeInfo();
78  newParamInfo.m_setInSteering = currParam->isSetInSteering();
79  newParamInfo.m_forceInSteering = currParam->isForcedInSteering();
80  getParamValuesPython(mapIter->first, newParamInfo.m_defaultValues, true);
81  getParamValuesPython(mapIter->first, newParamInfo.m_values, false);
82 
83  returnList->append(boost::python::object(newParamInfo));
84  }
85  return returnList;
86 }
87 
89 {
90  for (const auto& param : params.m_paramMap) {
91  auto& myParam = m_paramMap.at(param.first);
92  myParam->setValueFromParam(*param.second.get());
93  }
94 }
95 
96 
97 std::vector<std::string> ModuleParamList::getParameterNames() const
98 {
99  std::vector<std::string> names;
100  names.reserve(m_paramMap.size());
101  for (const auto& nameAndParam : m_paramMap) {
102  names.push_back(nameAndParam.first);
103  }
104  return names;
105 }
106 
107 std::string ModuleParamList::getParameterDescription(const std::string& name) const
108 {
109  return getParameterPtr(name)->getDescription();
110 }
111 
112 std::string ModuleParamList::getParameterTypeInfo(const std::string& name) const
113 {
114  return getParameterPtr(name)->getTypeInfo();
115 }
116 
117 
118 
119 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 
121 
122 //==================================================================================
123 // Private methods
124 //==================================================================================
125 
126 ModuleParamPtr ModuleParamList::getParameterPtr(const std::string& name) const
127 {
128  //Check if a parameter with the given name exists
129  std::map<std::string, ModuleParamPtr>::const_iterator mapIter;
130  mapIter = m_paramMap.find(name);
131 
132  if (mapIter != m_paramMap.end()) {
133  return mapIter->second;
134  } else throw (ModuleParameterNotFoundError() << name);
135 }
136 
137 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138 
139 
140 
141 //==================================================================================
142 // Explicit template instantiations
143 //==================================================================================
144 
145 template void ModuleParamList::addParameter(const std::string&, bool&, const std::string&, const bool&);
146 template void ModuleParamList::addParameter(const std::string&, double&, const std::string&, const double&);
147 template void ModuleParamList::addParameter(const std::string&, float&, const std::string&, const float&);
148 template void ModuleParamList::addParameter(const std::string&, int&, const std::string&, const int&);
149 template void ModuleParamList::addParameter(const std::string&, std::string&, const std::string&, const std::string&);
150 
151 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Belle2::ModuleParamList::getParameterPtr
ModuleParamPtr getParameterPtr(const std::string &name) const
Returns a ModuleParamPtr to a parameter.
Definition: ModuleParamList.cc:126
Belle2::ModuleParamInfoPython::m_description
std::string m_description
The description of the parameter.
Definition: ModuleParamInfoPython.h:42
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::ModuleParamInfoPython::m_name
std::string m_name
The name of the parameter.
Definition: ModuleParamInfoPython.h:38
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::ModuleParamInfoPython::m_typeName
std::string m_typeName
The name of the type of the parameter.
Definition: ModuleParamInfoPython.h:39
Belle2::ModuleParamInfoPython::m_setInSteering
bool m_setInSteering
True if the parameter was set in the steering file.
Definition: ModuleParamInfoPython.h:44
Belle2::ModuleParamInfoPython
Class to store basic information about a parameter.
Definition: ModuleParamInfoPython.h:34
Belle2::ModuleParamList::getUnsetForcedParams
std::vector< std::string > getUnsetForcedParams() const
Returns list of unset parameters (if they are required to have a value.
Definition: ModuleParamList.cc:56
Belle2::ModuleParamList::setParameters
void setParameters(const ModuleParamList &params)
Set values for parameters from other parameter list.
Definition: ModuleParamList.cc:88
Belle2::ModuleParamList::ModuleParamList
ModuleParamList()
Constructor.
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::ModuleParamList::~ModuleParamList
~ModuleParamList()
Destructor.
Definition: ModuleParamList.cc:50
Belle2::ModuleParamInfoPython::m_forceInSteering
bool m_forceInSteering
If true the parameter has to be set by the user in the steering file.
Definition: ModuleParamInfoPython.h:43
Belle2::ModuleParamList::getParameterTypeInfo
std::string getParameterTypeInfo(const std::string &name) const
Returns the type info of a parameter given by its name.
Definition: ModuleParamList.cc:112
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_DEFINE_EXCEPTION
#define BELLE2_DEFINE_EXCEPTION(ClassName, Message)
Macro that defines an exception with the given message template.
Definition: FrameworkExceptions.h:46
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::ModuleParamList::getParameterDescription
std::string getParameterDescription(const std::string &name) const
Returns the description of a parameter given by its name.
Definition: ModuleParamList.cc:107
Belle2::ModuleParamPtr
std::shared_ptr< ModuleParamBase > ModuleParamPtr
Defines a pointer to a module parameter as a boost shared pointer. *‍/.
Definition: ModuleParamList.h:40
Belle2::ModuleParamInfoPython::m_values
boost::python::list m_values
The values of the parameter as a python list.
Definition: ModuleParamInfoPython.h:41
Belle2::ModuleParamInfoPython::m_defaultValues
boost::python::list m_defaultValues
The default values of the parameter as a python list.
Definition: ModuleParamInfoPython.h:40
Belle2::ModuleParamList::getParamInfoListPython
std::shared_ptr< boost::python::list > getParamInfoListPython() const
Returns a python list of all parameters.
Definition: ModuleParamList.cc:66
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
Belle2::ModuleParamList::getParameterNames
std::vector< std::string > getParameterNames() const
Returns the names of all parameters in this parameter list.
Definition: ModuleParamList.cc:97