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