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 Geometry {
25class ComponentInfo;
26class DetectorInfo;
27class Instrument;
28class InstrumentMetadata;
29
40
41class MANTID_GEOMETRY_DLL ParameterMap {
42public:
44 using Entry = std::pair<size_t, std::shared_ptr<Parameter>>;
45
49 ParameterMap(const ParameterMap &other);
52 inline bool empty() const { return m_parameterInfo->empty(); }
54 inline int size() const { return static_cast<int>(m_parameterInfo->size()); }
56 size_t getMemorySize() const;
58 static const std::string &pos();
59 static const std::string &posx();
60 static const std::string &posy();
61 static const std::string &posz();
62 static const std::string &rot();
63 static const std::string &rotx();
64 static const std::string &roty();
65 static const std::string &rotz();
66 static const std::string &pDouble(); // p prefix to avoid name clash
67 static const std::string &pInt();
68 static const std::string &pBool();
69 static const std::string &pString();
70 static const std::string &pV3D();
71 static const std::string &pQuat();
72 static const std::string &scale();
73
74 const std::string diff(const ParameterMap &rhs, const bool &firstDiffOnly = false, const bool relative = false,
75 const double doubleTolerance = Kernel::Tolerance) const;
76
78 bool operator!=(const ParameterMap &rhs) const;
80 bool operator==(const ParameterMap &rhs) const;
81
83 inline void clear() {
84 // The translation tables are kept: they describe the instrument, not the parameters, and an
85 // attached map shares its index domain with ComponentInfo, which may still write by index.
86 m_parameterInfo->clear();
87 }
89 void swap(ParameterMap &other) {
90 // The index domain and staging tables must travel with the store: an index is meaningless without them.
91 m_parameterInfo.swap(other.m_parameterInfo);
92 m_domainIds.swap(other.m_domainIds);
93 m_domainIndices.swap(other.m_domainIndices);
94 m_stagedIndices.swap(other.m_stagedIndices);
95 m_stagedIds.swap(other.m_stagedIds);
96 }
98 void clearParametersByName(const std::string &name);
99
101 void clearParametersByName(const std::string &name, const IComponent *comp);
102
104 void add(const std::string &type, const IComponent *comp, const std::string &name, const std::string &value,
105 const std::string *const pDescription = nullptr, const std::string &visible = "true");
106
121 template <class T>
122 void add(const std::string &type, const IComponent *comp, const std::string &name, const T &value,
123 const std::string *const pDescription = nullptr, const std::string &pVisible = "true") {
124 auto param = create(type, name, pVisible);
125 auto typedParam = std::dynamic_pointer_cast<ParameterType<T>>(param);
126 assert(typedParam); // If not true the factory has created the wrong type
127 typedParam->setValue(value);
128 this->add(comp, param, pDescription);
129 }
132 void add(const IComponent *comp, const std::shared_ptr<Parameter> &par,
133 const std::string *const pDescription = nullptr);
134
138 void addFittingParameter(const IComponent *comp, const std::string &name, const std::string &fittingFunction,
139 const std::string &value, const std::string *const pDescription = nullptr,
140 const std::string &pVisible = "true");
141
149 void add(const std::string &type, const size_t componentIndex, const std::string &name, const std::string &value,
150 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
151 void add(const size_t componentIndex, const std::shared_ptr<Parameter> &par,
152 const std::string *const pDescription = nullptr);
153 void addFittingParameter(const size_t componentIndex, const std::string &name, const std::string &fittingFunction,
154 const std::string &value, const std::string *const pDescription = nullptr,
155 const std::string &pVisible = "true");
157
160 void addPositionCoordinate(const IComponent *comp, const std::string &name, const double value,
161 const std::string *const pDescription = nullptr);
163 void addRotationParam(const IComponent *comp, const std::string &name, const double deg,
164 const std::string *const pDescription = nullptr);
166 void addDouble(const IComponent *comp, const std::string &name, const std::string &value,
167 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
169 void addDouble(const IComponent *comp, const std::string &name, double value,
170 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
172 void addInt(const IComponent *comp, const std::string &name, const std::string &value,
173 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
175 void addInt(const IComponent *comp, const std::string &name, int value,
176 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
178 void addBool(const IComponent *comp, const std::string &name, const std::string &value,
179 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
181 void addBool(const IComponent *comp, const std::string &name, bool value,
182 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
184 void addString(const IComponent *comp, const std::string &name, const std::string &value,
185 const std::string *const pDescription = nullptr, const std::string &pVisible = "true");
187 void addV3D(const IComponent *comp, const std::string &name, const std::string &value,
188 const std::string *const pDescription = nullptr);
190 void addV3D(const IComponent *comp, const std::string &name, const Kernel::V3D &value,
191 const std::string *const pDescription = nullptr);
193 void addQuat(const IComponent *comp, const std::string &name, const Kernel::Quat &value,
194 const std::string *const pDescription = nullptr);
195 void forceUnsafeSetMasked(const IComponent *comp, bool value);
197
200 bool contains(const IComponent *comp, const std::string &name, const std::string &type = "") const;
202 bool contains(const IComponent *comp, const char *name, const char *type = "") const;
204 bool contains(const IComponent *comp, const Parameter &parameter) const;
206 std::shared_ptr<Parameter> get(const IComponent *comp, const std::string &name, const std::string &type = "") const;
208 std::shared_ptr<Parameter> get(const IComponent *comp, const char *name, const char *type = "") const;
210 std::shared_ptr<Parameter> getByType(const IComponent *comp, const std::string &type) const;
213 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const std::string &name,
214 const std::string &type = "") const;
217 std::shared_ptr<Parameter> getRecursive(const IComponent *comp, const char *name, const char *type = "") const;
220 std::shared_ptr<Parameter> getRecursiveByType(const IComponent *comp, const std::string &type) const;
221
225 std::shared_ptr<Parameter> getRecursiveFittingParameter(const IComponent *comp, const std::string &name,
226 const std::string &fittingFunction) const;
227
235 template <class T> std::vector<T> getType(const std::string &compName, const std::string &name) const {
236 std::vector<T> retval;
237 // Iterate every component with parameters because the match is on component name rather than key
238 for (auto const &[index, parameters] : *m_parameterInfo) {
239 static_cast<void>(parameters);
240 if (compName == componentName(index)) {
241 std::shared_ptr<Parameter> const param = m_parameterInfo->get(index, name);
242 if (param) {
243 retval.emplace_back(param->value<T>());
244 }
245 }
246 }
247 return retval;
248 }
250 const std::string getDescription(const std::string &compName, const std::string &name) const;
252 const std::string getShortDescription(const std::string &compName, const std::string &name) const;
253
255 std::string getString(const IComponent *comp, const std::string &name, bool recursive = false) const;
258 std::vector<std::string> getString(const std::string &compName, const std::string &name) const {
259 return getType<std::string>(compName, name);
260 }
268 std::vector<double> getDouble(const std::string &compName, const std::string &name) const {
269 return getType<double>(compName, name);
270 }
278 std::vector<Kernel::V3D> getV3D(const std::string &compName, const std::string &name) const {
279 return getType<Kernel::V3D>(compName, name);
280 }
281
283 std::set<std::string> names(const IComponent *comp) const;
285 std::string asString() const;
287 static std::string asString(const std::vector<Entry> &entries, const ComponentInfo &componentInfo,
288 const DetectorInfo &detectorInfo);
290 static std::string asStringEntry(const std::string &component, const Parameter &parameter);
291
293 static void saveNexus(Nexus::File *file, const std::string &group, const std::string &contents);
296 void copyFromParameterMap(const IComponent *oldComp, const IComponent *newComp, const ParameterMap *oldPMap);
297
299 const std::vector<std::string> &getParameterFilenames() const;
301 void addParameterFilename(const std::string &filename);
302
305 std::vector<Entry> entries() const;
306
307 bool hasDetectorInfo(const Instrument *instrument) const;
308 bool hasComponentInfo(const Instrument *instrument) const;
309 const Geometry::DetectorInfo &detectorInfo() const;
310 Geometry::DetectorInfo &mutableDetectorInfo();
311 const Geometry::ComponentInfo &componentInfo() const;
312 Geometry::ComponentInfo &mutableComponentInfo();
313 Geometry::InstrumentMetadata const &instrumentMetadata() const;
314 size_t detectorIndex(const detid_t detID) const;
315 size_t componentIndex(const Geometry::ComponentID componentId) const;
316 const std::vector<Geometry::ComponentID> &componentIds() const;
317 void setInstrument(const Instrument *instrument);
318
324 void rebuildBeamlineFrom(const ParameterMap &source);
325
335 const std::shared_ptr<ParameterInfo> &sharedParameterInfo() const { return m_parameterInfo; }
336 const std::shared_ptr<Geometry::ComponentInfo> &sharedComponentInfo() const { return m_componentInfo; }
337 const std::shared_ptr<Geometry::DetectorInfo> &sharedDetectorInfo() const { return m_detectorInfo; }
338 const std::shared_ptr<Geometry::InstrumentMetadata> &sharedInstrumentMetadata() const { return m_instrumentMetadata; }
340
342 using ComponentIDs = std::vector<Geometry::IComponent *>;
344 using ComponentIndices = std::unordered_map<Geometry::IComponent const *, size_t>;
345
349 std::shared_ptr<ParameterInfo> rekey(std::shared_ptr<const ComponentIDs> componentIds,
350 std::shared_ptr<const ComponentIndices> componentIndices);
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 void seedLogfileParameters();
363 bool relErr(double x1, double x2, double errorVal) const;
364
366 size_t domainSize() const { return m_domainIds ? m_domainIds->size() : 0; }
368 size_t indexOf(const IComponent *comp) const;
371 size_t indexForWrite(const IComponent *comp);
373 std::string componentName(const size_t index) const;
375 std::string componentFullName(const size_t index) const;
376
378 std::vector<std::string> m_parameterFileNames;
379
381 std::shared_ptr<ParameterInfo> m_parameterInfo;
382
386 std::shared_ptr<const ComponentIDs> m_domainIds;
387 std::shared_ptr<const ComponentIndices> m_domainIndices;
388
391 std::unordered_map<Geometry::IComponent const *, size_t> m_stagedIndices;
392 std::vector<ComponentID> m_stagedIds;
393
404 std::shared_ptr<Geometry::DetectorInfo> m_detectorInfo;
405 std::shared_ptr<Geometry::ComponentInfo> m_componentInfo;
406 std::shared_ptr<Geometry::InstrumentMetadata> m_instrumentMetadata;
407
412 const Instrument *m_instrument{nullptr};
413};
414
416using ParameterMap_sptr = std::shared_ptr<ParameterMap>;
418using ParameterMap_const_sptr = std::shared_ptr<const ParameterMap>;
419
420} // Namespace Geometry
421
422} // 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
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:50
Parameter map iterator typedef.
std::pair< size_t, std::shared_ptr< Parameter > > Entry
A stored parameter together with the component index of the component it belongs to.
void clear()
Clears the map.
ParameterMap & operator=(ParameterMap *rhs)
Assignment operator.
std::vector< ComponentID > m_stagedIds
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::shared_ptr< ParameterInfo > m_parameterInfo
The parameter store, owner of the parameter info.
size_t domainSize() const
The number of components in the index domain.
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.
int size() const
Return the size of the map.
std::vector< std::string > m_parameterFileNames
internal list of parameter files loaded
std::vector< Geometry::IComponent * > ComponentIDs
The components of one instrument, by index, as held by its ComponentInfo.
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::unordered_map< Geometry::IComponent const *, size_t > m_stagedIndices
Staging indices, for the components of a detached map, which has no component indices yet.
std::shared_ptr< const ComponentIndices > m_domainIndices
std::unordered_map< Geometry::IComponent const *, size_t > ComponentIndices
The reverse of ComponentIDs.
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
std::shared_ptr< const ComponentIDs > m_domainIds
The index domain: the components of the instrument this map is attached to, shared with that instrume...
void swap(ParameterMap &other)
method swaps two parameter maps contents each other
Base class for parameters of an instrument.
Definition Parameter.h:42
Class for quaternions.
Definition Quat.h:38
Class for 3D vectors.
Definition V3D.h:35
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)