19#include "MantidNexus/NexusFile.h"
20#include <boost/algorithm/string.hpp>
25#define strcasecmp _stricmp
36const std::string POS_PARAM_NAME =
"pos";
37const std::string POSX_PARAM_NAME =
"x";
38const std::string POSY_PARAM_NAME =
"y";
39const std::string POSZ_PARAM_NAME =
"z";
41const std::string ROT_PARAM_NAME =
"rot";
42const std::string ROTX_PARAM_NAME =
"rotx";
43const std::string ROTY_PARAM_NAME =
"roty";
44const std::string ROTZ_PARAM_NAME =
"rotz";
46const std::string DOUBLE_PARAM_NAME =
"double";
47const std::string INT_PARAM_NAME =
"int";
48const std::string BOOL_PARAM_NAME =
"bool";
49const std::string STRING_PARAM_NAME =
"string";
50const std::string V3D_PARAM_NAME =
"V3D";
51const std::string QUAT_PARAM_NAME =
"Quat";
53const std::string SCALE_PARAM_NAME =
"sca";
56Kernel::Logger
g_log(
"ParameterMap");
58void checkIsNotMaskingParameter(
const std::string &
name) {
59 if (
name == std::string(
"masked"))
60 throw std::runtime_error(
"Masking data (\"masked\") cannot be stored in "
61 "ParameterMap. Use DetectorInfo instead");
75 : m_parameterFileNames(other.m_parameterFileNames),
76 m_parameterInfo(
std::make_shared<
ParameterInfo>(*other.m_parameterInfo)), m_idToIndex(other.m_idToIndex),
77 m_indexToId(other.m_indexToId),
80 m_instrument(other.m_instrument) {
108 result = found->second;
120 result = found->second;
136 std::vector<Entry> result;
140 for (
auto const &[parameterName, parameter] : parameters) {
141 static_cast<void>(parameterName);
142 result.emplace_back(
id, parameter);
150std::shared_ptr<ParameterInfo>
ParameterMap::rekey(
const std::unordered_map<IComponent const *, size_t> &idToIndex) {
151 auto rekeyed = std::make_shared<ParameterInfo>();
154 auto const found = idToIndex.find(
id);
155 if (found == idToIndex.end()) {
158 for (
auto const &[parameterName, parameter] : parameters) {
159 static_cast<void>(parameterName);
162 rekeyed->insert(found->second, parameter);
171 for (
auto const &[
id,
index] : idToIndex) {
239 for (
auto const &entry :
entries()) {
240 if (compName == entry.first->getName()) {
241 std::shared_ptr<Parameter>
const param =
get(entry.first,
name);
243 result = param->getDescription();
260 for (
auto const &entry :
entries()) {
261 if (compName == entry.first->getName()) {
262 std::shared_ptr<Parameter>
const param =
get(entry.first,
name);
264 result = param->getShortDescription();
288 double num = std::fabs(x1 - x2);
290 double den = 0.5 * (std::fabs(x1) + std::fabs(x2));
292 return (num > errorVal);
294 return (num / den > errorVal);
312 const double doubleTolerance)
const {
314 return std::string(
"");
318 return std::string(
"Number of parameters does not match: ") +
std::to_string(this->
size()) +
" not equal to " +
326 std::unordered_multimap<std::string, Parameter_sptr> thisMap, rhsMap;
327 for (
auto const &entry :
entries()) {
328 thisMap.emplace(entry.first->getFullName(), entry.second);
330 for (
auto const &entry :
rhs.entries()) {
331 rhsMap.emplace(entry.first->getFullName(), entry.second);
334 std::stringstream strOutput;
335 for (
auto thisIt = thisMap.cbegin(); thisIt != thisMap.cend(); ++thisIt) {
336 const std::string fullName = thisIt->first;
337 const auto ¶m = thisIt->second;
339 for (
auto rhsIt = rhsMap.cbegin(); rhsIt != rhsMap.cend(); ++rhsIt) {
340 const std::string rhsFullName = rhsIt->first;
341 const auto &rhsParam = rhsIt->second;
342 if ((fullName == rhsFullName) && (param->name() == (rhsParam->name()))) {
343 if ((param->type() == rhsParam->type()) && (rhsParam->type() ==
"double")) {
345 if (!
relErr(param->value<
double>(), rhsParam->value<
double>(), doubleTolerance))
347 }
else if (std::abs(param->value<
double>() - rhsParam->value<
double>()) <= doubleTolerance)
349 }
else if (param->asString() == rhsParam->asString()) {
359 strOutput <<
"Parameter mismatch LHS=RHS for LHS parameter in component "
361 << fullName <<
". Parameter name is: " << (*param).name() <<
" and value: " << (*param).asString()
363 bool componentWithSameNameRHS =
false;
364 bool parameterWithSameNameRHS =
false;
365 for (
auto rhsIt = rhsMap.cbegin(); rhsIt != rhsMap.cend(); ++rhsIt) {
366 const std::string rhsFullName = rhsIt->first;
367 if (fullName == rhsFullName) {
368 componentWithSameNameRHS =
true;
369 if ((*param).name() == (*rhsIt->second).
name()) {
370 parameterWithSameNameRHS =
true;
371 strOutput <<
"RHS param with same name has value: " << (*rhsIt->second).
asString() <<
'\n';
375 if (!componentWithSameNameRHS) {
376 strOutput <<
"No matching RHS component name\n";
378 if (componentWithSameNameRHS && !parameterWithSameNameRHS) {
379 strOutput <<
"Found matching RHS component name but not parameter name\n";
382 return strOutput.str();
385 return strOutput.str();
393 checkIsNotMaskingParameter(
name);
407 checkIsNotMaskingParameter(
name);
429 const std::string &
value,
const std::string *
const pDescription,
const std::string &pVisible) {
431 param->fromString(
value);
432 this->
add(comp, param, pDescription);
443 const std::string *
const pDescription) {
450 const std::string *
const pDescription) {
453 checkIsNotMaskingParameter(par->name());
455 par->setDescription(*pDescription);
464 const std::string &
value,
const std::string *
const pDescription,
const std::string &pVisible) {
466 param->fromString(
value);
485 const std::string &fittingFunction,
const std::string &
value,
486 const std::string *
const pDescription,
const std::string &pVisible) {
491 const std::string &fittingFunction,
const std::string &
value,
492 const std::string *
const pDescription,
const std::string &pVisible) {
493 checkIsNotMaskingParameter(
name);
497 param->fromString(
value);
499 param->setDescription(*pDescription);
505 g_log.
debug() <<
"addFittingParameter: [" << (replaced ?
"replace" :
"insert") <<
"] (name='" <<
name
506 <<
"', function='" << fittingFunction <<
"') on component index " <<
componentIndex <<
'\n';
519 const std::string *
const pDescription) {
539 g_log.
warning() <<
"addPositionCoordinate() called with unrecognized "
540 "coordinate symbol: "
544 param->setDescription(*pDescription);
565 const std::string *
const pDescription) {
569 double rotX, rotY, rotZ;
572 rotX = paramRotX->value<
double>();
577 rotY = paramRotY->value<
double>();
582 rotZ = paramRotZ->value<
double>();
598 g_log.
warning() <<
"addRotationParam() called with unrecognized coordinate symbol: " <<
name;
619 const std::string *
const pDescription,
const std::string &pVisible) {
633 const std::string *
const pDescription,
const std::string &pVisible) {
647 const std::string *
const pDescription,
const std::string &pVisible) {
661 const std::string *
const pDescription,
const std::string &pVisible) {
675 const std::string *
const pDescription,
const std::string &pVisible) {
688 const std::string *
const pDescription,
const std::string &pVisible) {
700 const std::string
name(
"masked");
702 auto typedParam = std::dynamic_pointer_cast<ParameterType<bool>>(param);
703 typedParam->setValue(
value);
720 const std::string *
const pDescription,
const std::string &pVisible) {
733 const std::string *
const pDescription) {
747 const std::string *
const pDescription) {
761 const std::string *
const pDescription) {
786 checkIsNotMaskingParameter(
name);
797 checkIsNotMaskingParameter(parameter.
name());
805 static_cast<void>(storedName);
806 if (param && *param == parameter)
819 return get(comp,
name.c_str(), type.c_str());
830 checkIsNotMaskingParameter(
name);
852 std::shared_ptr<const IComponent> compInFocus(comp,
NoDeleting());
853 while (compInFocus !=
nullptr) {
858 compInFocus = compInFocus->getParent();
873 const std::string &type)
const {
886 checkIsNotMaskingParameter(
name);
893 result = this->
get(parent->getComponentID(),
name, type);
896 parent = parent->getParent();
915 const std::string &fittingFunction)
const {
916 checkIsNotMaskingParameter(
name);
924 std::shared_ptr<const IComponent> parent;
925 while (current !=
nullptr) {
929 static_cast<void>(storedName);
930 if (param->type() ==
"fitting" && strcasecmp(param->nameAsCString(),
name.c_str()) == 0) {
942 current = parent.get();
964 return param->asString();
984 std::stringstream out;
985 for (
auto const &entry :
entries()) {
986 const std::shared_ptr<Parameter> &p = entry.second;
987 if (p && entry.first) {
988 const auto *comp =
dynamic_cast<const IComponent *
>(entry.first);
989 const auto *det =
dynamic_cast<const IDetector *
>(comp);
991 out <<
"detID:" << det->
getID();
993 out << comp->getFullName();
996 const auto paramVisible =
"visible:" + std::string(p->visible() == 1 ?
"true" :
"false");
997 out <<
';' << p->type() <<
';' << p->name() <<
';' << p->asString() <<
';' << paramVisible <<
'|';
1051 auto const oldParameterNames = oldPMap->
names(oldComp);
1053 for (
const auto &oldParameterName : oldParameterNames) {
1066 file->makeGroup(
group,
"NXnote",
true);
1067 file->putAttr(
"version", 1);
1068 file->writeData(
"author",
"");
1069 file->writeData(
"date", Types::Core::DateAndTime::getCurrentTime().toISO8601String());
1070 file->writeData(
"description",
"A string representation of the parameter "
1071 "map. The format is either: "
1072 "|detID:id-value;param-type;param-name;param-"
1073 "value| for a detector or "
1074 "|comp-name;param-type;param-name;param-value|"
1075 " for other components.");
1076 file->writeData(
"type",
"text/plain");
1078 file->writeData(
"data", s);
1094 const std::string &visible)
const {
1122 throw std::runtime_error(
"Cannot return reference to NULL DetectorInfo");
1129 throw std::runtime_error(
"Cannot return reference to NULL DetectorInfo");
1136 throw std::runtime_error(
"Cannot return reference to NULL ComponentInfo");
1144 throw std::runtime_error(
"Cannot return reference to NULL ComponentInfo");
1152 throw std::runtime_error(
"Cannot return reference to NULL InstrumentMetadata");
1175 throw std::logic_error(
"ParameterMap::setInstrument: Cannot change "
1176 "instrument once it has been set.");
1178 throw std::logic_error(
"ParameterMap::setInstrument must be called with "
1179 "base instrument, not a parametrized instrument");
1183 auto [newComponentInfo, newDetectorInfo] =
1202 physicalInstrument =
1203 std::make_shared<Instrument>(basePhysicalInstrument, std::shared_ptr<ParameterMap>(
this,
NoDeleting()));
1208 std::move(physicalInstrument));
1220 const size_t translationMem =
m_idToIndex.size() * (
sizeof(std::pair<const ComponentID, size_t>) +
sizeof(
void *)) +
1225 [](
size_t acc,
const auto &s) {
return acc + s.capacity(); });
1228 const size_t cacheLocMem =
m_cacheLocMap ?
sizeof(*m_cacheLocMap) : 0;
1229 const size_t cacheRotMem =
m_cacheRotMap ?
sizeof(*m_cacheRotMap) : 0;
1235 return sizeof(*this) + mapMem + translationMem + fileNamesMem + cacheLocMem + cacheRotMem + detectorInfoMem +
const std::vector< double > & rhs
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
#define PARALLEL_CRITICAL(name)
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
ComponentInfo : Provides a component centric view on to the instrument.
static constexpr size_t invalidIndex
Returned by indexOfOrInvalid() for a component ID that is not part of this instrument.
bool isParametrized() const override
Return true if the Component is, in fact, parametrized (that is - it has a valid parameter map)
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
Store information about a fitting parameter such as its value if it is constrained or tied.
const std::string & getFunction() const
get function
base class for Geometric IComponent
virtual Kernel::V3D getPos() const =0
Get the position of the IComponent. Tree structure is traverse through the.
virtual std::shared_ptr< const IComponent > getParent() const =0
Return a pointer to the current parent.
virtual ComponentID getComponentID() const =0
Returns the ComponentID - a unique identifier of the component.
Interface class for detector objects.
virtual detid_t getID() const =0
Get the detector ID.
std::shared_ptr< const Instrument > getPhysicalInstrument() const
INDIRECT GEOMETRY INSTRUMENTS ONLY: Returns the physical instrument, if one has been specified as dis...
std::pair< std::unique_ptr< ComponentInfo >, std::unique_ptr< DetectorInfo > > makeBeamlineNew(ParameterMap &pmap) const
Return ComponentInfo and DetectorInfo for instrument given by pmap.
std::pair< std::unique_ptr< ComponentInfo >, std::unique_ptr< DetectorInfo > > makeBeamline(ParameterMap &pmap, const ParameterMap *source=nullptr) const
Return ComponentInfo and DetectorInfo for instrument given by pmap.
size_t detectorIndex(const detid_t detID) const
Returns the index for a detector ID. Used for accessing DetectorInfo.
const std::string & getFilename() const
const std::string & getDefaultView() const
Get the default type of the instrument view.
const std::string & getDefaultAxis() const
Retrieves from which side the instrument to be viewed from when the instrument viewer first starts,...
const std::string & getXmlText() const
Types::Core::DateAndTime getValidFromDate() const
Types::Core::DateAndTime getValidToDate() const
static std::shared_ptr< Parameter > create(const std::string &className, const std::string &name, const std::string &visible="true")
Creates an instance of a parameter.
ParameterInfo : the named parameters of an instrument, addressed by component index.
Parameter map iterator typedef.
bool getCachedRotation(const IComponent *comp, Kernel::Quat &rotation) const
Attempts to retrieve a rotation from the rotation cache.
const Geometry::DetectorInfo & detectorInfo() const
Only for use by ExperimentInfo. Returns a reference to the DetectorInfo.
const std::vector< std::string > & getParameterFilenames() const
Returns a list of all the parameter files loaded.
static const std::string & pQuat()
bool getCachedLocation(const IComponent *comp, Kernel::V3D &location) const
Attempts to retrieve a location from the location cache.
void clearPositionSensitiveCaches()
Clears the location, rotation & bounding box caches.
bool contains(const IComponent *comp, const std::string &name, const std::string &type="") const
Does the named parameter exist for the given component and type (std::string version)
void copyFromParameterMap(const IComponent *oldComp, const IComponent *newComp, const ParameterMap *oldPMap)
Copy pairs (oldComp->id,Parameter) to the m_map assigning the new newComp->id.
void rebuildBeamlineFrom(const ParameterMap &source)
Rebuild the 2.0 layers for a map that was copy-constructed from source.
void addInt(const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Adds an int value to the parameter map.
void addPositionCoordinate(const IComponent *comp, const std::string &name, const double value, const std::string *const pDescription=nullptr)
Create or adjust "pos" parameter for a component.
std::shared_ptr< Parameter > getRecursive(const IComponent *comp, const std::string &name, const std::string &type="") const
Use get() recursively to see if can find param in all parents of comp and given type (std::string ver...
std::shared_ptr< ParameterInfo > rekey(const std::unordered_map< Geometry::IComponent const *, size_t > &idToIndex)
Rekey this map's parameters from the synthetic staging indices onto an instrument's real component in...
std::vector< ComponentID > m_indexToId
Reverse of m_idToIndex.
void addParameterFilename(const std::string &filename)
adds a parameter filename that has been loaded
std::shared_ptr< ParameterInfo > m_parameterInfo
The parameter store, owner of the parameter info.
void buildInstrumentMetadata()
Builds m_instrumentMetadata from m_instrument. Requires m_instrument to be set.
static const std::string & scale()
std::shared_ptr< Parameter > getRecursiveByType(const IComponent *comp, const std::string &type) const
Looks recursively upwards in the component tree for the first instance of a parameter with a specifie...
std::unordered_map< Geometry::IComponent const *, size_t > m_idToIndex
Component pointer to index translation.
std::shared_ptr< Geometry::InstrumentMetadata > m_instrumentMetadata
static const std::string & rot()
void setCachedLocation(const IComponent *comp, const Kernel::V3D &location) const
Sets a cached location on the location cache.
const Instrument * m_instrument
Pointer to the owning instrument for translating detector IDs into detector indices when accessing th...
std::string getString(const IComponent *comp, const std::string &name, bool recursive=false) const
Return the value of a parameter as a string.
static const std::string & pBool()
Geometry::InstrumentMetadata const & instrumentMetadata() const
Only for use by ExperimentInfo. Returns a reference to the InstrumentMetadata.
void saveNexus(Nexus::File *file, const std::string &group) const
Persist a representation of the Parameter map to the open Nexus file.
void setCachedRotation(const IComponent *comp, const Kernel::Quat &rotation) const
Sets a cached rotation on the rotation cache.
void clearParametersByName(const std::string &name)
Clear any parameters with the given name.
const std::string getDescription(const std::string &compName, const std::string &name) const
Get the component description by name.
std::shared_ptr< Parameter > getByType(const IComponent *comp, const std::string &type) const
Finds the parameter in the map via the parameter type.
void setInstrument(const Instrument *instrument)
Only for use by Instrument. Sets the pointer to the owning instrument.
Geometry::ComponentInfo & mutableComponentInfo()
Only for use by ExperimentInfo. Returns a reference to the ComponentInfo.
bool operator==(const ParameterMap &rhs) const
Equality comparison operator.
std::shared_ptr< Parameter > create(const std::string &className, const std::string &name, const std::string &visible="true") const
Wrapper for ParameterFactory::create to avoid include in header.
void addString(const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Adds a std::string value to the parameter map.
std::set< std::string > names(const IComponent *comp) const
Returns a set with all parameter names for component.
size_t indexOf(const IComponent *comp) const
Index for a component, or ComponentInfo::invalidIndex if this map holds nothing for it.
std::vector< Entry > entries() const
Every stored parameter with its owning component, in component-index order.
int size() const
Return the size of the map.
std::vector< std::string > m_parameterFileNames
internal list of parameter files loaded
static const std::string & pV3D()
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::V3D > > m_cacheLocMap
internal cache map instance for cached position values
size_t getMemorySize() const
Get the footprint in memory in bytes.
size_t indexForWrite(const IComponent *comp)
As indexOf(), but allocating a new index for a component seen for the first time.
void addDouble(const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Adds a double value to the parameter map.
static const std::string & posz()
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::Quat > > m_cacheRotMap
internal cache map instance for cached rotation values
bool relErr(double x1, double x2, double errorVal) const
calculate relative error for use in diff
const std::string diff(const ParameterMap &rhs, const bool &firstDiffOnly=false, const bool relative=false, const double doubleTolerance=Kernel::Tolerance) const
Output information that helps understanding the mismatch between two parameter maps.
static const std::string & pDouble()
static const std::string & pos()
Return string to be used in the map.
void addRotationParam(const IComponent *comp, const std::string &name, const double deg, const std::string *const pDescription=nullptr)
Create or adjust "rot" parameter for a component.
ParameterMap()
Default constructor.
std::string asString() const
Returns a string with all component names, parameter names and values.
void addFittingParameter(const IComponent *comp, const std::string &name, const std::string &fittingFunction, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add a fitting parameter.
void addQuat(const IComponent *comp, const std::string &name, const Kernel::Quat &value, const std::string *const pDescription=nullptr)
Adds a Kernel::Quat value to the parameter map.
size_t componentIndex(const Geometry::ComponentID componentId) const
void add(const std::string &type, const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &visible="true")
Method for adding a parameter providing its value as a string.
std::shared_ptr< Parameter > getRecursiveFittingParameter(const IComponent *comp, const std::string &name, const std::string &fittingFunction) const
Look for a fitting parameter recursively, picking the one whose embedded FitParameter function name m...
static const std::string & rotz()
ComponentID componentIdAt(const size_t index) const
The component an index refers to. Only valid for an index this map issued.
const std::string getShortDescription(const std::string &compName, const std::string &name) const
Get the component tooltip by name.
size_t detectorIndex(const detid_t detID) const
Only for use by Detector. Returns a detector index for a detector ID.
static const std::string & posy()
static const std::string & posx()
std::shared_ptr< Geometry::DetectorInfo > m_detectorInfo
The 2.0 instrument layers, co-owned with the ExperimentInfo that built them.
static const std::string & pString()
void addV3D(const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr)
Adds a Kernel::V3D value to the parameter map.
static const std::string & pInt()
std::shared_ptr< Geometry::ComponentInfo > m_componentInfo
void addBool(const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Adds a bool value to the parameter map.
bool operator!=(const ParameterMap &rhs) const
Inquality comparison operator.
bool hasComponentInfo(const Instrument *instrument) const
Only for use by ExperimentInfo.
static const std::string & rotx()
bool hasDetectorInfo(const Instrument *instrument) const
Only for use by ExperimentInfo.
std::shared_ptr< Parameter > get(const IComponent *comp, const std::string &name, const std::string &type="") const
Get a parameter with a given name and type (std::string version)
void forceUnsafeSetMasked(const IComponent *comp, bool value)
Force adding masking information.
static const std::string & roty()
const Geometry::ComponentInfo & componentInfo() const
Only for use by ExperimentInfo. Returns a reference to the ComponentInfo.
Geometry::DetectorInfo & mutableDetectorInfo()
Only for use by ExperimentInfo. Returns a reference to the DetectorInfo.
Base class for parameters of an instrument.
const std::string & name() const
Parameter name.
Cache is a generic caching storage class.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
void setX(const double xx) noexcept
Set is x position.
This functor is used as the deleter object of a shared_ptr to effectively erase ownership Raw pointer...
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Mantid::Kernel::Logger g_log("Goniometer")
IComponent * ComponentID
Define a type for a unique component identifier.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
std::string to_string(const wide_integer< Bits, Signed > &n)