Mantid
Loading...
Searching...
No Matches
ParameterMap.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "MantidGeometry/DllConfig.h"
11#include "MantidGeometry/IDTypes.h" //For specnum_t
14
15#include "tbb/concurrent_unordered_map.h"
16
17#include <memory>
18#include <typeinfo>
19#include <vector>
20
21namespace Mantid {
22namespace Kernel {
23template <class KEYTYPE, class VALUETYPE> class Cache;
24}
25namespace Geometry {
26class ComponentInfo;
27class DetectorInfo;
28class Instrument;
29
40using component_map_it = tbb::concurrent_unordered_multimap<ComponentID, std::shared_ptr<Parameter>>::iterator;
41using component_map_cit = tbb::concurrent_unordered_multimap<ComponentID, std::shared_ptr<Parameter>>::const_iterator;
42
43class MANTID_GEOMETRY_DLL ParameterMap {
44public:
46 using pmap = tbb::concurrent_unordered_multimap<ComponentID, std::shared_ptr<Parameter>>;
48 using pmap_it = tbb::concurrent_unordered_multimap<ComponentID, std::shared_ptr<Parameter>>::iterator;
50 using pmap_cit = tbb::concurrent_unordered_multimap<ComponentID, std::shared_ptr<Parameter>>::const_iterator;
54 ParameterMap(const ParameterMap &other);
57 inline bool empty() const { return m_map.empty(); }
59 inline int size() const { return static_cast<int>(m_map.size()); }
61 static const std::string &pos();
62 static const std::string &posx();
63 static const std::string &posy();
64 static const std::string &posz();
65 static const std::string &rot();
66 static const std::string &rotx();
67 static const std::string &roty();
68 static const std::string &rotz();
69 static const std::string &pDouble(); // p prefix to avoid name clash
70 static const std::string &pInt();
71 static const std::string &pBool();
72 static const std::string &pString();
73 static const std::string &pV3D();
74 static const std::string &pQuat();
75 static const std::string &scale();
76
77 const std::string diff(const ParameterMap &rhs, const bool &firstDiffOnly = false, const bool relative = false,
78 const double doubleTolerance = Kernel::Tolerance) const;
79
81 bool operator!=(const ParameterMap &rhs) const;
83 bool operator==(const ParameterMap &rhs) const;
84
86 inline void clear() {
87 m_map.clear();
88 clearPositionSensitiveCaches();
89 }
92 void swap(ParameterMap &other) {
93 m_map.swap(other.m_map);
94 clearPositionSensitiveCaches();
95 }
97 void clearParametersByName(const std::string &name);
98
100 void clearParametersByName(const std::string &name, const IComponent *comp);
101
103 void add(const std::string &type, const IComponent *comp, const std::string &name, const std::string &value,
104 const std::string *const pDescription = nullptr, const std::string &visible = "true");
105
120 template <class T>
121 void add(const std::string &type, const IComponent *comp, const std::string &name, const T &value,
122 const std::string *const pDescription = nullptr, const std::string &pVisible = "true") {
123 auto param = create(type, name, pVisible);
124 auto typedParam = std::dynamic_pointer_cast<ParameterType<T>>(param);
125 assert(typedParam); // If not true the factory has created the wrong type
126 typedParam->setValue(value);
127 this->add(comp, param, pDescription);
128 }
131 void add(const IComponent *comp, const std::shared_ptr<Parameter> &par,
132 const std::string *const pDescription = nullptr);
133
136 void addPositionCoordinate(const IComponent *comp, const std::string &name, const double value,
137 const std::string *const pDescription = nullptr);
139 void addRotationParam(const IComponent *comp, const std::string &name, const double deg,
140 const std::string *const pDescription = nullptr);
142 void addDouble(const IComponent *comp, const std::string &name, const std::string &value,
143 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
145 void addDouble(const IComponent *comp, const std::string &name, double value,
146 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
148 void addInt(const IComponent *comp, const std::string &name, const std::string &value,
149 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
151 void addInt(const IComponent *comp, const std::string &name, int value,
152 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
154 void addBool(const IComponent *comp, const std::string &name, const std::string &value,
155 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
157 void addBool(const IComponent *comp, const std::string &name, bool value,
158 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
160 void addString(const IComponent *comp, const std::string &name, const std::string &value,
161 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
163 void addV3D(const IComponent *comp, const std::string &name, const std::string &value,
164 const std::string *const pDescription = nullptr);
166 void addV3D(const IComponent *comp, const std::string &name, const Kernel::V3D &value,
167 const std::string *const pDescription = nullptr);
169 void addQuat(const IComponent *comp, const std::string &name, const Kernel::Quat &value,
170 const std::string *const pDescription = nullptr);
171 void forceUnsafeSetMasked(const IComponent *comp, bool value);
173
176 bool contains(const IComponent *comp, const std::string &name, const std::string &type = "") const;
178 bool contains(const IComponent *comp, const char *name, const char *type = "") const;
180 bool contains(const IComponent *comp, const Parameter &parameter) const;
182 std::shared_ptr<Parameter> get(const IComponent *comp, const std::string &name, const std::string &type = "") const;
184 std::shared_ptr<Parameter> get(const IComponent *comp, const char *name, const char *type = "") const;
186 std::shared_ptr<Parameter> getByType(const IComponent *comp, const std::string &type) const;
189 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const std::string &name,
190 const std::string &type = "") const;
193 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const char *name, const char *type = "") const;
196 std::shared_ptr<Parameter> getRecursiveByType(const IComponent *comp, const std::string &type) const;
197
205 template <class T> std::vector<T> getType(const std::string &compName, const std::string &name) const {
206 std::vector<T> retval;
207
208 pmap_cit it;
209 for (it = m_map.begin(); it != m_map.end(); ++it) {
210 if (compName == it->first->getName()) {
211 std::shared_ptr<Parameter> param = get(it->first, name);
212 if (param)
213 retval.emplace_back(param->value<T>());
214 }
215 }
216 return retval;
217 }
219 const std::string getDescription(const std::string &compName, const std::string &name) const;
221 const std::string getShortDescription(const std::string &compName, const std::string &name) const;
222
224 std::string getString(const IComponent *comp, const std::string &name, bool recursive = false) const;
227 std::vector<std::string> getString(const std::string &compName, const std::string &name) const {
228 return getType<std::string>(compName, name);
229 }
237 std::vector<double> getDouble(const std::string &compName, const std::string &name) const {
238 return getType<double>(compName, name);
239 }
247 std::vector<Kernel::V3D> getV3D(const std::string &compName, const std::string &name) const {
248 return getType<Kernel::V3D>(compName, name);
249 }
250
252 std::set<std::string> names(const IComponent *comp) const;
254 std::string asString() const;
255
257 void clearPositionSensitiveCaches();
259 void setCachedLocation(const IComponent *comp, const Kernel::V3D &location) const;
261 bool getCachedLocation(const IComponent *comp, Kernel::V3D &location) const;
263 void setCachedRotation(const IComponent *comp, const Kernel::Quat &rotation) const;
265 bool getCachedRotation(const IComponent *comp, Kernel::Quat &rotation) const;
267 void saveNexus(::NeXus::File *file, const std::string &group) const;
270 void copyFromParameterMap(const IComponent *oldComp, const IComponent *newComp, const ParameterMap *oldPMap);
271
273 const std::vector<std::string> &getParameterFilenames() const;
275 void addParameterFilename(const std::string &filename);
276
278 pmap_it begin() { return m_map.begin(); }
279 pmap_cit begin() const { return m_map.begin(); }
281 pmap_it end() { return m_map.end(); }
282 pmap_cit end() const { return m_map.end(); }
283
284 bool hasDetectorInfo(const Instrument *instrument) const;
285 bool hasComponentInfo(const Instrument *instrument) const;
286 const Geometry::DetectorInfo &detectorInfo() const;
287 Geometry::DetectorInfo &mutableDetectorInfo();
288 const Geometry::ComponentInfo &componentInfo() const;
289 Geometry::ComponentInfo &mutableComponentInfo();
290 size_t detectorIndex(const detid_t detID) const;
291 size_t componentIndex(const Geometry::ComponentID componentId) const;
292 const std::vector<Geometry::ComponentID> &componentIds() const;
293 void setInstrument(const Instrument *instrument);
294
295private:
296 std::shared_ptr<Parameter> create(const std::string &className, const std::string &name,
297 const std::string &visible = "true") const;
298
302 component_map_it positionOf(const IComponent *comp, const char *name, const char *type);
305 component_map_cit positionOf(const IComponent *comp, const char *name, const char *type) const;
307 bool relErr(double x1, double x2, double errorVal) const;
308
310 std::vector<std::string> m_parameterFileNames;
311
315 std::unique_ptr<Kernel::Cache<const ComponentID, Kernel::V3D>> m_cacheLocMap;
317 std::unique_ptr<Kernel::Cache<const ComponentID, Kernel::Quat>> m_cacheRotMap;
318
321 std::unique_ptr<Geometry::DetectorInfo> m_detectorInfo;
322
325 std::unique_ptr<Geometry::ComponentInfo> m_componentInfo;
326
331 const Instrument *m_instrument{nullptr};
332};
333
335using ParameterMap_sptr = std::shared_ptr<ParameterMap>;
337using ParameterMap_const_sptr = std::shared_ptr<const ParameterMap>;
338
339} // Namespace Geometry
340
341} // Namespace Mantid
const std::vector< double > & rhs
double value
The value of the point.
Definition: FitMW.cpp:51
IntArray detectorIndex
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
ComponentInfo : Provides a component centric view on to the instrument.
Definition: ComponentInfo.h:40
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
Definition: DetectorInfo.h:49
base class for Geometric IComponent
Definition: IComponent.h:51
Base Instrument Class.
Definition: Instrument.h:47
pmap_it begin()
access iterators. begin;
Definition: ParameterMap.h:278
void clear()
Clears the map.
Definition: ParameterMap.h:86
std::unique_ptr< Geometry::DetectorInfo > m_detectorInfo
Pointer to the DetectorInfo wrapper.
Definition: ParameterMap.h:321
ParameterMap & operator=(ParameterMap *rhs)
Assignment operator.
std::vector< Kernel::V3D > getV3D(const std::string &compName, const std::string &name) const
Returns a Kernel::V3D parameter as vector's first element if exists and an empty vector if it doesn't...
Definition: ParameterMap.h:247
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::iterator pmap_it
Parameter map iterator typedef.
Definition: ParameterMap.h:48
bool empty() const
Returns true if the map is empty, false otherwise.
Definition: ParameterMap.h:57
std::vector< T > getType(const std::string &compName, const std::string &name) const
Get the values of a given parameter of all the components that have the name: compName.
Definition: ParameterMap.h:205
std::vector< std::string > getString(const std::string &compName, const std::string &name) const
Returns a string parameter as vector's first element if exists and an empty vector if it doesn't.
Definition: ParameterMap.h:227
int size() const
Return the size of the map.
Definition: ParameterMap.h:59
std::vector< std::string > m_parameterFileNames
internal list of parameter files loaded
Definition: ParameterMap.h:310
pmap_it end()
access iterators. end;
Definition: ParameterMap.h:281
pmap m_map
internal parameter map instance
Definition: ParameterMap.h:313
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::V3D > > m_cacheLocMap
internal cache map instance for cached position values
Definition: ParameterMap.h:315
void add(const std::string &type, const IComponent *comp, const std::string &name, const T &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Method for adding a parameter providing its value of a particular type.
Definition: ParameterMap.h:121
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::Quat > > m_cacheRotMap
internal cache map instance for cached rotation values
Definition: ParameterMap.h:317
std::vector< double > getDouble(const std::string &compName, const std::string &name) const
Returns a double parameter as vector's first element if exists and an empty vector if it doesn't.
Definition: ParameterMap.h:237
const std::vector< Geometry::ComponentID > & componentIds() const
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::const_iterator pmap_cit
Parameter map iterator typedef.
Definition: ParameterMap.h:50
std::unique_ptr< Geometry::ComponentInfo > m_componentInfo
Pointer to the ComponentInfo wrapper.
Definition: ParameterMap.h:325
void swap(ParameterMap &other)
method swaps two parameter maps contents each other.
Definition: ParameterMap.h:92
Base class for parameters of an instrument.
Definition: Parameter.h:37
Class for quaternions.
Definition: Quat.h:39
Class for 3D vectors.
Definition: V3D.h:34
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::const_iterator component_map_cit
Definition: ParameterMap.h:41
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::iterator component_map_it
Parameter map iterator typedef.
Definition: ParameterMap.h:40
std::shared_ptr< ParameterMap > ParameterMap_sptr
ParameterMap shared pointer typedef.
Definition: ParameterMap.h:335
std::shared_ptr< const ParameterMap > ParameterMap_const_sptr
ParameterMap constant shared pointer typedef.
Definition: ParameterMap.h:337
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
Generate a tableworkspace to store the calibration results.
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator!=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)