17#include "MantidNexus/NexusFile.h"
18#include <boost/algorithm/string.hpp>
22#define strcasecmp _stricmp
33const std::string POS_PARAM_NAME =
"pos";
34const std::string POSX_PARAM_NAME =
"x";
35const std::string POSY_PARAM_NAME =
"y";
36const std::string POSZ_PARAM_NAME =
"z";
38const std::string ROT_PARAM_NAME =
"rot";
39const std::string ROTX_PARAM_NAME =
"rotx";
40const std::string ROTY_PARAM_NAME =
"roty";
41const std::string ROTZ_PARAM_NAME =
"rotz";
43const std::string DOUBLE_PARAM_NAME =
"double";
44const std::string INT_PARAM_NAME =
"int";
45const std::string BOOL_PARAM_NAME =
"bool";
46const std::string STRING_PARAM_NAME =
"string";
47const std::string V3D_PARAM_NAME =
"V3D";
48const std::string QUAT_PARAM_NAME =
"Quat";
50const std::string SCALE_PARAM_NAME =
"sca";
53Kernel::Logger
g_log(
"ParameterMap");
55void checkIsNotMaskingParameter(
const std::string &
name) {
56 if (
name == std::string(
"masked"))
57 throw std::runtime_error(
"Masking data (\"masked\") cannot be stored in "
58 "ParameterMap. Use DetectorInfo instead");
69 : m_parameterFileNames(other.m_parameterFileNames), m_map(other.m_map),
72 m_instrument(other.m_instrument) {
143 for (it =
m_map.begin(); it !=
m_map.end(); ++it) {
144 if (compName == it->first->getName()) {
145 std::shared_ptr<Parameter> param =
get(it->first,
name);
147 result = param->getDescription();
165 for (it =
m_map.begin(); it !=
m_map.end(); ++it) {
166 if (compName == it->first->getName()) {
167 std::shared_ptr<Parameter> param =
get(it->first,
name);
169 result = param->getShortDescription();
193 double num = std::fabs(x1 - x2);
195 double den = 0.5 * (std::fabs(x1) + std::fabs(x2));
197 return (num > errorVal);
199 return (num / den > errorVal);
217 const double doubleTolerance)
const {
219 return std::string(
"");
223 return std::string(
"Number of parameters does not match: ") +
std::to_string(this->
size()) +
" not equal to " +
231 std::unordered_multimap<std::string, Parameter_sptr> thisMap, rhsMap;
232 for (
auto &mappair : this->
m_map) {
233 thisMap.emplace(mappair.first->getFullName(), mappair.second);
235 for (
auto &mappair :
rhs.m_map) {
236 rhsMap.emplace(mappair.first->getFullName(), mappair.second);
239 std::stringstream strOutput;
240 for (
auto thisIt = thisMap.cbegin(); thisIt != thisMap.cend(); ++thisIt) {
241 const std::string fullName = thisIt->first;
242 const auto ¶m = thisIt->second;
244 for (
auto rhsIt = rhsMap.cbegin(); rhsIt != rhsMap.cend(); ++rhsIt) {
245 const std::string rhsFullName = rhsIt->first;
246 const auto &rhsParam = rhsIt->second;
247 if ((fullName == rhsFullName) && (param->name() == (rhsParam->name()))) {
248 if ((param->type() == rhsParam->type()) && (rhsParam->type() ==
"double")) {
250 if (!
relErr(param->value<
double>(), rhsParam->value<
double>(), doubleTolerance))
252 }
else if (std::abs(param->value<
double>() - rhsParam->value<
double>()) <= doubleTolerance)
254 }
else if (param->asString() == rhsParam->asString()) {
264 strOutput <<
"Parameter mismatch LHS=RHS for LHS parameter in component "
266 << fullName <<
". Parameter name is: " << (*param).name() <<
" and value: " << (*param).asString()
268 bool componentWithSameNameRHS =
false;
269 bool parameterWithSameNameRHS =
false;
270 for (
auto rhsIt = rhsMap.cbegin(); rhsIt != rhsMap.cend(); ++rhsIt) {
271 const std::string rhsFullName = rhsIt->first;
272 if (fullName == rhsFullName) {
273 componentWithSameNameRHS =
true;
274 if ((*param).name() == (*rhsIt->second).
name()) {
275 parameterWithSameNameRHS =
true;
276 strOutput <<
"RHS param with same name has value: " << (*rhsIt->second).
asString() <<
'\n';
280 if (!componentWithSameNameRHS) {
281 strOutput <<
"No matching RHS component name\n";
283 if (componentWithSameNameRHS && !parameterWithSameNameRHS) {
284 strOutput <<
"Found matching RHS component name but not parameter name\n";
287 return strOutput.str();
290 return strOutput.str();
298 checkIsNotMaskingParameter(
name);
300 for (
auto itr =
m_map.begin(); itr !=
m_map.end();) {
301 if (itr->second->name() ==
name) {
318 checkIsNotMaskingParameter(
name);
319 if (!
m_map.empty()) {
321 auto itrs =
m_map.equal_range(
id);
322 for (
auto it = itrs.first; it != itrs.second;) {
323 if (it->second->name() ==
name) {
347 const std::string &
value,
const std::string *
const pDescription,
const std::string &pVisible) {
349 param->fromString(
value);
350 this->
add(comp, param, pDescription);
361 const std::string *
const pDescription) {
364 checkIsNotMaskingParameter(par->name());
366 par->setDescription(*pDescription);
368 auto existing_par =
positionOf(comp, par->name().c_str(),
"");
374 if (existing_par !=
m_map.end()) {
375 std::atomic_store(&(existing_par->second), par);
379#if defined(__clang__) && !defined(__APPLE__)
380#define CLANG_ON_LINUX true
382#define CLANG_ON_LINUX false
384#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
402 const std::string *
const pDescription) {
422 g_log.
warning() <<
"addPositionCoordinate() called with unrecognized "
423 "coordinate symbol: "
427 param->setDescription(*pDescription);
448 const std::string *
const pDescription) {
452 double rotX, rotY, rotZ;
455 rotX = paramRotX->value<
double>();
460 rotY = paramRotY->value<
double>();
465 rotZ = paramRotZ->value<
double>();
481 g_log.
warning() <<
"addRotationParam() called with unrecognized coordinate symbol: " <<
name;
502 const std::string *
const pDescription,
const std::string &pVisible) {
516 const std::string *
const pDescription,
const std::string &pVisible) {
530 const std::string *
const pDescription,
const std::string &pVisible) {
544 const std::string *
const pDescription,
const std::string &pVisible) {
558 const std::string *
const pDescription,
const std::string &pVisible) {
571 const std::string *
const pDescription,
const std::string &pVisible) {
583 const std::string
name(
"masked");
585 auto typedParam = std::dynamic_pointer_cast<ParameterType<bool>>(param);
586 typedParam->setValue(
value);
590#if defined(__clang__) && !defined(__APPLE__)
591#define CLANG_ON_LINUX true
593#define CLANG_ON_LINUX false
595#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
612 const std::string *
const pDescription,
const std::string &pVisible) {
625 const std::string *
const pDescription) {
639 const std::string *
const pDescription) {
653 const std::string *
const pDescription) {
678 checkIsNotMaskingParameter(
name);
682 std::pair<pmap_cit, pmap_cit> components =
m_map.equal_range(
id);
683 bool anytype = (strlen(type) == 0);
684 for (
auto itr = components.first; itr != components.second; ++itr) {
685 const auto ¶m = itr->second;
686 if (strcasecmp(param->nameAsCString(),
name) == 0 && (anytype || param->type() == type)) {
699 checkIsNotMaskingParameter(parameter.
name());
700 if (
m_map.empty() || !comp)
704 auto it_found =
m_map.find(
id);
705 if (it_found !=
m_map.end()) {
706 auto itrs =
m_map.equal_range(
id);
707 for (
auto itr = itrs.first; itr != itrs.second; ++itr) {
709 if (*param == parameter)
724 return get(comp,
name.c_str(), type.c_str());
735 checkIsNotMaskingParameter(
name);
741 if (itr !=
m_map.end())
742 result = std::atomic_load(&itr->second);
754 auto result =
m_map.end();
757 const bool anytype = (strlen(type) == 0);
758 if (!
m_map.empty()) {
760 auto it_found =
m_map.find(
id);
761 if (it_found !=
m_map.end()) {
762 auto itrs =
m_map.equal_range(
id);
763 for (
auto itr = itrs.first; itr != itrs.second; ++itr) {
764 const auto ¶m = itr->second;
765 if (strcasecmp(param->nameAsCString(),
name) == 0 && (anytype || param->type() == type)) {
783 auto result =
m_map.end();
786 const bool anytype = (strlen(type) == 0);
787 if (!
m_map.empty()) {
789 auto it_found =
m_map.find(
id);
790 if (it_found !=
m_map.end()) {
791 auto itrs =
m_map.equal_range(
id);
792 for (
auto itr = itrs.first; itr != itrs.second; ++itr) {
793 const auto ¶m = itr->second;
794 if (strcasecmp(param->nameAsCString(),
name) == 0 && (anytype || param->type() == type)) {
811 if (!
m_map.empty()) {
813 auto it_found =
m_map.find(
id);
814 if (it_found !=
m_map.end() && it_found->first) {
815 auto itrs =
m_map.equal_range(
id);
816 for (
auto itr = itrs.first; itr != itrs.second; ++itr) {
817 const auto ¶m = itr->second;
818 if (strcasecmp(param->type().c_str(), type.c_str()) == 0) {
819 result = std::atomic_load(¶m);
835 std::shared_ptr<const IComponent> compInFocus(comp,
NoDeleting());
836 while (compInFocus !=
nullptr) {
841 compInFocus = compInFocus->getParent();
856 const std::string &type)
const {
869 checkIsNotMaskingParameter(
name);
876 result = this->
get(parent->getComponentID(),
name, type);
879 parent = parent->getParent();
901 return param->asString();
910 std::set<std::string> paramNames;
912 auto it_found =
m_map.find(
id);
913 if (it_found ==
m_map.end()) {
917 auto itrs =
m_map.equal_range(
id);
918 for (
auto it = itrs.first; it != itrs.second; ++it) {
919 paramNames.insert(it->second->name());
932 std::stringstream out;
933 for (
const auto &mappair :
m_map) {
934 const std::shared_ptr<Parameter> &p = mappair.second;
935 if (p && mappair.first) {
936 const auto *comp =
dynamic_cast<const IComponent *
>(mappair.first);
937 const auto *det =
dynamic_cast<const IDetector *
>(comp);
939 out <<
"detID:" << det->
getID();
941 out << comp->getFullName();
944 const auto paramVisible =
"visible:" + std::string(p->visible() == 1 ?
"true" :
"false");
945 out <<
';' << p->type() <<
';' << p->name() <<
';' << p->asString() <<
';' << paramVisible <<
'|';
999 auto oldParameterNames = oldPMap->
names(oldComp);
1000 for (
const auto &oldParameterName : oldParameterNames) {
1003#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
1017 file->makeGroup(
group,
"NXnote",
true);
1018 file->putAttr(
"version", 1);
1019 file->writeData(
"author",
"");
1020 file->writeData(
"date", Types::Core::DateAndTime::getCurrentTime().toISO8601String());
1021 file->writeData(
"description",
"A string representation of the parameter "
1022 "map. The format is either: "
1023 "|detID:id-value;param-type;param-name;param-"
1024 "value| for a detector or "
1025 "|comp-name;param-type;param-name;param-value|"
1026 " for other components.");
1027 file->writeData(
"type",
"text/plain");
1029 file->writeData(
"data", s);
1045 const std::string &visible)
const {
1073 throw std::runtime_error(
"Cannot return reference to NULL DetectorInfo");
1080 throw std::runtime_error(
"Cannot return reference to NULL DetectorInfo");
1087 throw std::runtime_error(
"Cannot return reference to NULL ComponentInfo");
1095 throw std::runtime_error(
"Cannot return reference to NULL ComponentInfo");
1117 throw std::logic_error(
"ParameterMap::setInstrument: Cannot change "
1118 "instrument once it has been set.");
1120 throw std::logic_error(
"ParameterMap::setInstrument must be called with "
1121 "base instrument, not a parametrized instrument");
const std::vector< double > & rhs
double value
The value of the point.
#define PARALLEL_CRITICAL(name)
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
ComponentInfo : Provides a component centric view on to the 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....
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::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.
Void deleter for shared pointers.
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.
component_map_it positionOf(const IComponent *comp, const char *name, const char *type)
internal function to get position of the parameter in the parameter map
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.
std::unique_ptr< Geometry::DetectorInfo > m_detectorInfo
Pointer to the DetectorInfo wrapper.
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 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...
void addParameterFilename(const std::string &filename)
adds a parameter filename that has been loaded
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...
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()
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.
int size() const
Return the size of the map.
std::vector< std::string > m_parameterFileNames
internal list of parameter files loaded
pmap m_map
internal parameter map instance
static const std::string & pV3D()
std::unique_ptr< Kernel::Cache< const ComponentID, Kernel::V3D > > m_cacheLocMap
internal cache map instance for cached position values
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 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.
static const std::string & rotz()
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()
static const std::string & pString()
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::const_iterator pmap_cit
Parameter map iterator typedef.
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()
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.
std::unique_ptr< Geometry::ComponentInfo > m_componentInfo
Pointer to the ComponentInfo wrapper.
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 warning(const std::string &msg)
Logs at warning level.
void setX(const double xx) noexcept
Set is x position.
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::const_iterator component_map_cit
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
tbb::concurrent_unordered_multimap< ComponentID, std::shared_ptr< Parameter > >::iterator component_map_it
Parameter map iterator typedef.
Mantid::Kernel::Logger g_log("Goniometer")
int32_t detid_t
Typedef for a detector ID.
std::string to_string(const wide_integer< Bits, Signed > &n)