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
15
16#include <cstddef>
17#include <memory>
18#include <typeinfo>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
23namespace Mantid {
24namespace Kernel {
25template <class KEYTYPE, class VALUETYPE> class Cache;
26}
27namespace Geometry {
28class ComponentInfo;
29class DetectorInfo;
30class Instrument;
31class InstrumentMetadata;
32
43
44class MANTID_GEOMETRY_DLL ParameterMap {
45public:
47 using Entry = std::pair<ComponentID, std::shared_ptr<Parameter>>;
48
52 ParameterMap(const ParameterMap &other);
55 inline bool empty() const { return m_parameterInfo->empty(); }
57 inline int size() const { return static_cast<int>(m_parameterInfo->size()); }
59 size_t getMemorySize() const;
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_parameterInfo->clear();
88 m_idToIndex.clear();
89 m_indexToId.clear();
90 clearPositionSensitiveCaches();
91 }
94 void swap(ParameterMap &other) {
95 // The translation tables must travel with the store: an index is meaningless without them.
96 m_parameterInfo.swap(other.m_parameterInfo);
97 m_idToIndex.swap(other.m_idToIndex);
98 m_indexToId.swap(other.m_indexToId);
99 clearPositionSensitiveCaches();
100 }
102 void clearParametersByName(const std::string &name);
103
105 void clearParametersByName(const std::string &name, const IComponent *comp);
106
108 void add(const std::string &type, const IComponent *comp, const std::string &name, const std::string &value,
109 const std::string *const pDescription = nullptr, const std::string &visible = "true");
110
125 template <class T>
126 void add(const std::string &type, const IComponent *comp, const std::string &name, const T &value,
127 const std::string *const pDescription = nullptr, const std::string &pVisible = "true") {
128 auto param = create(type, name, pVisible);
129 auto typedParam = std::dynamic_pointer_cast<ParameterType<T>>(param);
130 assert(typedParam); // If not true the factory has created the wrong type
131 typedParam->setValue(value);
132 this->add(comp, param, pDescription);
133 }
136 void add(const IComponent *comp, const std::shared_ptr<Parameter> &par,
137 const std::string *const pDescription = nullptr);
138
142 void addFittingParameter(const IComponent *comp, const std::string &name, const std::string &fittingFunction,
143 const std::string &value, const std::string *const pDescription = nullptr,
144 const std::string &pVisible = "true");
145
153 void add(const std::string &type, const size_t componentIndex, const std::string &name, const std::string &value,
154 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
155 void add(const size_t componentIndex, const std::shared_ptr<Parameter> &par,
156 const std::string *const pDescription = nullptr);
157 void addFittingParameter(const size_t componentIndex, const std::string &name, const std::string &fittingFunction,
158 const std::string &value, const std::string *const pDescription = nullptr,
159 const std::string &pVisible = "true");
161
164 void addPositionCoordinate(const IComponent *comp, const std::string &name, const double value,
165 const std::string *const pDescription = nullptr);
167 void addRotationParam(const IComponent *comp, const std::string &name, const double deg,
168 const std::string *const pDescription = nullptr);
170 void addDouble(const IComponent *comp, const std::string &name, const std::string &value,
171 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
173 void addDouble(const IComponent *comp, const std::string &name, double value,
174 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
176 void addInt(const IComponent *comp, const std::string &name, const std::string &value,
177 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
179 void addInt(const IComponent *comp, const std::string &name, int value,
180 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
182 void addBool(const IComponent *comp, const std::string &name, const std::string &value,
183 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
185 void addBool(const IComponent *comp, const std::string &name, bool value,
186 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
188 void addString(const IComponent *comp, const std::string &name, const std::string &value,
189 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
191 void addV3D(const IComponent *comp, const std::string &name, const std::string &value,
192 const std::string *const pDescription = nullptr);
194 void addV3D(const IComponent *comp, const std::string &name, const Kernel::V3D &value,
195 const std::string *const pDescription = nullptr);
197 void addQuat(const IComponent *comp, const std::string &name, const Kernel::Quat &value,
198 const std::string *const pDescription = nullptr);
199 void forceUnsafeSetMasked(const IComponent *comp, bool value);
201
204 bool contains(const IComponent *comp, const std::string &name, const std::string &type = "") const;
206 bool contains(const IComponent *comp, const char *name, const char *type = "") const;
208 bool contains(const IComponent *comp, const Parameter &parameter) const;
210 std::shared_ptr<Parameter> get(const IComponent *comp, const std::string &name, const std::string &type = "") const;
212 std::shared_ptr<Parameter> get(const IComponent *comp, const char *name, const char *type = "") const;
214 std::shared_ptr<Parameter> getByType(const IComponent *comp, const std::string &type) const;
217 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const std::string &name,
218 const std::string &type = "") const;
221 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const char *name, const char *type = "") const;
224 std::shared_ptr<Parameter> getRecursiveByType(const IComponent *comp, const std::string &type) const;
225
229 std::shared_ptr<Parameter> getRecursiveFittingParameter(const IComponent *comp, const std::string &name,
230 const std::string &fittingFunction) const;
231
239 template <class T> std::vector<T> getType(const std::string &compName, const std::string &name) const {
240 std::vector<T> retval;
241 // Iterate every stored parameter because the match is on component name rather than key
242 for (auto const &entry : entries()) {
243 if (compName == entry.first->getName()) {
244 std::shared_ptr<Parameter> const param = get(entry.first, name);
245 if (param) {
246 retval.emplace_back(param->value<T>());
247 }
248 }
249 }
250 return retval;
251 }
253 const std::string getDescription(const std::string &compName, const std::string &name) const;
255 const std::string getShortDescription(const std::string &compName, const std::string &name) const;
256
258 std::string getString(const IComponent *comp, const std::string &name, bool recursive = false) const;
261 std::vector<std::string> getString(const std::string &compName, const std::string &name) const {
262 return getType<std::string>(compName, name);
263 }
271 std::vector<double> getDouble(const std::string &compName, const std::string &name) const {
272 return getType<double>(compName, name);
273 }
281 std::vector<Kernel::V3D> getV3D(const std::string &compName, const std::string &name) const {
282 return getType<Kernel::V3D>(compName, name);
283 }
284
286 std::set<std::string> names(const IComponent *comp) const;
288 std::string asString() const;
289
291 void clearPositionSensitiveCaches();
293 void setCachedLocation(const IComponent *comp, const Kernel::V3D &location) const;
295 bool getCachedLocation(const IComponent *comp, Kernel::V3D &location) const;
297 void setCachedRotation(const IComponent *comp, const Kernel::Quat &rotation) const;
299 bool getCachedRotation(const IComponent *comp, Kernel::Quat &rotation) const;
301 void saveNexus(Nexus::File *file, const std::string &group) const;
304 void copyFromParameterMap(const IComponent *oldComp, const IComponent *newComp, const ParameterMap *oldPMap);
305
307 const std::vector<std::string> &getParameterFilenames() const;
309 void addParameterFilename(const std::string &filename);
310
312 std::vector<Entry> entries() const;
313
314 bool hasDetectorInfo(const Instrument *instrument) const;
315 bool hasComponentInfo(const Instrument *instrument) const;
316 const Geometry::DetectorInfo &detectorInfo() const;
317 Geometry::DetectorInfo &mutableDetectorInfo();
318 const Geometry::ComponentInfo &componentInfo() const;
319 Geometry::ComponentInfo &mutableComponentInfo();
320 Geometry::InstrumentMetadata const &instrumentMetadata() const;
321 size_t detectorIndex(const detid_t detID) const;
322 size_t componentIndex(const Geometry::ComponentID componentId) const;
323 const std::vector<Geometry::ComponentID> &componentIds() const;
324 void setInstrument(const Instrument *instrument);
325
331 void rebuildBeamlineFrom(const ParameterMap &source);
332
342 const std::shared_ptr<ParameterInfo> &sharedParameterInfo() const { return m_parameterInfo; }
343 const std::shared_ptr<Geometry::ComponentInfo> &sharedComponentInfo() const { return m_componentInfo; }
344 const std::shared_ptr<Geometry::DetectorInfo> &sharedDetectorInfo() const { return m_detectorInfo; }
345 const std::shared_ptr<Geometry::InstrumentMetadata> &sharedInstrumentMetadata() const { return m_instrumentMetadata; }
347
350 std::shared_ptr<ParameterInfo> rekey(const std::unordered_map<Geometry::IComponent const *, size_t> &idToIndex);
351
352private:
353 std::shared_ptr<Parameter> create(const std::string &className, const std::string &name,
354 const std::string &visible = "true") const;
355
359 void buildInstrumentMetadata();
361 bool relErr(double x1, double x2, double errorVal) const;
362
364 size_t indexOf(const IComponent *comp) const;
366 size_t indexForWrite(const IComponent *comp);
368 ComponentID componentIdAt(const size_t index) const;
369
371 std::vector<std::string> m_parameterFileNames;
372
374 std::shared_ptr<ParameterInfo> m_parameterInfo;
375
377 std::unordered_map<Geometry::IComponent const *, size_t> m_idToIndex;
380 std::vector<ComponentID> m_indexToId;
382 std::unique_ptr<Kernel::Cache<const ComponentID, Kernel::V3D>> m_cacheLocMap;
384 std::unique_ptr<Kernel::Cache<const ComponentID, Kernel::Quat>> m_cacheRotMap;
385
396 std::shared_ptr<Geometry::DetectorInfo> m_detectorInfo;
397 std::shared_ptr<Geometry::ComponentInfo> m_componentInfo;
398 std::shared_ptr<Geometry::InstrumentMetadata> m_instrumentMetadata;
399
404 const Instrument *m_instrument{nullptr};
405};
406
408using ParameterMap_sptr = std::shared_ptr<ParameterMap>;
410using ParameterMap_const_sptr = std::shared_ptr<const ParameterMap>;
411
412} // Namespace Geometry
413
414} // Namespace Mantid
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
IntArray detectorIndex
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
ComponentInfo : Provides a component centric view on to the instrument.
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
base class for Geometric IComponent
Definition IComponent.h:53
InstrumentMetadata : metadata related to the entire instrument.
Base Instrument Class.
Definition Instrument.h:49
Parameter map iterator typedef.
void clear()
Clears the map.
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...
bool empty() const
Returns true if the map is empty, false otherwise.
std::vector< ComponentID > m_indexToId
Reverse of m_idToIndex.
std::shared_ptr< ParameterInfo > m_parameterInfo
The parameter store, owner of the parameter info.
std::unordered_map< Geometry::IComponent const *, size_t > m_idToIndex
Component pointer to index translation.
std::shared_ptr< Geometry::InstrumentMetadata > m_instrumentMetadata
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.
const std::shared_ptr< Geometry::InstrumentMetadata > & sharedInstrumentMetadata() const
const std::shared_ptr< ParameterInfo > & sharedParameterInfo() const
The parameter store this map owns.
const std::shared_ptr< Geometry::DetectorInfo > & sharedDetectorInfo() const
const std::shared_ptr< Geometry::ComponentInfo > & sharedComponentInfo() const
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.
std::pair< ComponentID, std::shared_ptr< Parameter > > Entry
A stored parameter together with the component it belongs to.
int size() const
Return the size of the map.
std::vector< std::string > m_parameterFileNames
internal list of parameter files loaded
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::V3D > > m_cacheLocMap
internal cache map instance for cached position values
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.
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::Quat > > m_cacheRotMap
internal cache map instance for cached rotation values
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.
const std::vector< Geometry::ComponentID > & componentIds() const
std::shared_ptr< Geometry::DetectorInfo > m_detectorInfo
The 2.0 instrument layers, co-owned with the ExperimentInfo that built them.
std::shared_ptr< Geometry::ComponentInfo > m_componentInfo
void swap(ParameterMap &other)
method swaps two parameter maps contents each other.
Base class for parameters of an instrument.
Definition Parameter.h:38
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.
std::shared_ptr< ParameterMap > ParameterMap_sptr
ParameterMap shared pointer typedef.
std::shared_ptr< const ParameterMap > ParameterMap_const_sptr
ParameterMap constant shared pointer typedef.
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
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)