Mantid
Loading...
Searching...
No Matches
ParameterMap.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
14#include "MantidKernel/Cache.h"
15#include "MantidKernel/Logger.h"
17#include "MantidNexus/NexusFile.h"
18#include <boost/algorithm/string.hpp>
19#include <cstring>
20
21#ifdef _WIN32
22#define strcasecmp _stricmp
23#else
24#include "strings.h"
25#endif
26
27namespace Mantid::Geometry {
28using Kernel::Quat;
29using Kernel::V3D;
30
31namespace {
32// names of common parameter types
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";
37
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";
42
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";
49
50const std::string SCALE_PARAM_NAME = "sca";
51
52// static logger reference
53Kernel::Logger g_log("ParameterMap");
54
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");
59}
60} // namespace
65 : m_cacheLocMap(std::make_unique<Kernel::Cache<const ComponentID, Kernel::V3D>>()),
66 m_cacheRotMap(std::make_unique<Kernel::Cache<const ComponentID, Kernel::Quat>>()) {}
67
69 : m_parameterFileNames(other.m_parameterFileNames), m_map(other.m_map),
70 m_cacheLocMap(std::make_unique<Kernel::Cache<const ComponentID, Kernel::V3D>>(*other.m_cacheLocMap)),
71 m_cacheRotMap(std::make_unique<Kernel::Cache<const ComponentID, Kernel::Quat>>(*other.m_cacheRotMap)),
72 m_instrument(other.m_instrument) {
73 if (m_instrument)
74 std::tie(m_componentInfo, m_detectorInfo) = m_instrument->makeBeamline(*this, &other);
75}
76
77// Defined as default in source for forward declaration with std::unique_ptr.
79
83// Position
84const std::string &ParameterMap::pos() { return POS_PARAM_NAME; }
85
86const std::string &ParameterMap::posx() { return POSX_PARAM_NAME; }
87
88const std::string &ParameterMap::posy() { return POSY_PARAM_NAME; }
89
90const std::string &ParameterMap::posz() { return POSZ_PARAM_NAME; }
91
92// Rotation
93const std::string &ParameterMap::rot() { return ROT_PARAM_NAME; }
94
95const std::string &ParameterMap::rotx() { return ROTX_PARAM_NAME; }
96
97const std::string &ParameterMap::roty() { return ROTY_PARAM_NAME; }
98
99const std::string &ParameterMap::rotz() { return ROTZ_PARAM_NAME; }
100
101// Other types
102const std::string &ParameterMap::pDouble() { return DOUBLE_PARAM_NAME; }
103
104const std::string &ParameterMap::pInt() { return INT_PARAM_NAME; }
105
106const std::string &ParameterMap::pBool() { return BOOL_PARAM_NAME; }
107
108const std::string &ParameterMap::pString() { return STRING_PARAM_NAME; }
109
110const std::string &ParameterMap::pV3D() { return V3D_PARAM_NAME; }
111
112const std::string &ParameterMap::pQuat() { return QUAT_PARAM_NAME; }
113
114// Scale
115const std::string &ParameterMap::scale() { return SCALE_PARAM_NAME; }
116
123bool ParameterMap::operator!=(const ParameterMap &rhs) const { return !(this->operator==(rhs)); }
124
131bool ParameterMap::operator==(const ParameterMap &rhs) const { return diff(rhs, true, false, 0.).empty(); }
132
140const std::string ParameterMap::getDescription(const std::string &compName, const std::string &name) const {
141 pmap_cit it;
142 std::string result;
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);
146 if (param) {
147 result = param->getDescription();
148 if (!result.empty())
149 return result;
150 }
151 }
152 }
153 return result;
154}
162const std::string ParameterMap::getShortDescription(const std::string &compName, const std::string &name) const {
163 pmap_cit it;
164 std::string result;
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);
168 if (param) {
169 result = param->getShortDescription();
170 if (!result.empty())
171 return result;
172 }
173 }
174 }
175 return result;
176}
177
178//------------------------------------------------------------------------------------------------
192bool ParameterMap::relErr(double x1, double x2, double errorVal) const {
193 double num = std::fabs(x1 - x2);
194 // how to treat x1<0 and x2 > 0 ? probably this way
195 double den = 0.5 * (std::fabs(x1) + std::fabs(x2));
196 if (den < errorVal)
197 return (num > errorVal);
198
199 return (num / den > errorVal);
200}
201
216const std::string ParameterMap::diff(const ParameterMap &rhs, const bool &firstDiffOnly, const bool relative,
217 const double doubleTolerance) const {
218 if (this == &rhs)
219 return std::string(""); // True for the same object
220
221 // Quick size check
222 if (this->size() != rhs.size()) {
223 return std::string("Number of parameters does not match: ") + std::to_string(this->size()) + " not equal to " +
224 std::to_string(rhs.size());
225 }
226
227 // The map is unordered and the key is only valid at runtime. The
228 // asString method turns the ComponentIDs to full-qualified name identifiers
229 // so we will use the same approach to compare them
230
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);
234 }
235 for (auto &mappair : rhs.m_map) {
236 rhsMap.emplace(mappair.first->getFullName(), mappair.second);
237 }
238
239 std::stringstream strOutput;
240 for (auto thisIt = thisMap.cbegin(); thisIt != thisMap.cend(); ++thisIt) {
241 const std::string fullName = thisIt->first;
242 const auto &param = thisIt->second;
243 bool match(false);
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")) {
249 if (relative) {
250 if (!relErr(param->value<double>(), rhsParam->value<double>(), doubleTolerance))
251 match = true;
252 } else if (std::abs(param->value<double>() - rhsParam->value<double>()) <= doubleTolerance)
253 match = true;
254 } else if (param->asString() == rhsParam->asString()) {
255 match = true;
256 }
257 if (match)
258 break;
259 }
260 }
261
262 if (!match) {
263 // output some information that helps with understanding the mismatch
264 strOutput << "Parameter mismatch LHS=RHS for LHS parameter in component "
265 "with name: "
266 << fullName << ". Parameter name is: " << (*param).name() << " and value: " << (*param).asString()
267 << '\n';
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';
277 }
278 }
279 }
280 if (!componentWithSameNameRHS) {
281 strOutput << "No matching RHS component name\n";
282 }
283 if (componentWithSameNameRHS && !parameterWithSameNameRHS) {
284 strOutput << "Found matching RHS component name but not parameter name\n";
285 }
286 if (firstDiffOnly)
287 return strOutput.str();
288 }
289 }
290 return strOutput.str();
291}
292
298 checkIsNotMaskingParameter(name);
299 // Key is component ID so have to search through whole lot
300 for (auto itr = m_map.begin(); itr != m_map.end();) {
301 if (itr->second->name() == name) {
302 PARALLEL_CRITICAL(unsafe_erase) { itr = m_map.unsafe_erase(itr); }
303 } else {
304 ++itr;
305 }
306 }
307 // Check if the caches need invalidating
308 if (name == pos() || name == rot())
310}
311
317void ParameterMap::clearParametersByName(const std::string &name, const IComponent *comp) {
318 checkIsNotMaskingParameter(name);
319 if (!m_map.empty()) {
320 const ComponentID id = comp->getComponentID();
321 auto itrs = m_map.equal_range(id);
322 for (auto it = itrs.first; it != itrs.second;) {
323 if (it->second->name() == name) {
324 PARALLEL_CRITICAL(unsafe_erase) { it = m_map.unsafe_erase(it); }
325 } else {
326 ++it;
327 }
328 }
329
330 // Check if the caches need invalidating
331 if (name == pos() || name == rot())
333 }
334}
335
346void ParameterMap::add(const std::string &type, const IComponent *comp, const std::string &name,
347 const std::string &value, const std::string *const pDescription, const std::string &pVisible) {
348 auto param = ParameterFactory::create(type, name, pVisible);
349 param->fromString(value);
350 this->add(comp, param, pDescription);
351}
352
360void ParameterMap::add(const IComponent *comp, const std::shared_ptr<Parameter> &par,
361 const std::string *const pDescription) {
362 if (!par)
363 return;
364 checkIsNotMaskingParameter(par->name());
365 if (pDescription)
366 par->setDescription(*pDescription);
367
368 auto existing_par = positionOf(comp, par->name().c_str(), "");
369 // As this is only an add method it should really throw if it already
370 // exists.
371 // However, this is old behavior and many things rely on this actually be
372 // an
373 // add/replace-style function
374 if (existing_par != m_map.end()) {
375 std::atomic_store(&(existing_par->second), par);
376 } else {
377// When using Clang & Linux, TBB 4.4 doesn't detect C++11 features.
378// https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/641658
379#if defined(__clang__) && !defined(__APPLE__)
380#define CLANG_ON_LINUX true
381#else
382#define CLANG_ON_LINUX false
383#endif
384#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
385 m_map.emplace(comp->getComponentID(), par);
386#else
387 m_map.insert(std::make_pair(comp->getComponentID(), par));
388#endif
389 }
390}
391
401void ParameterMap::addPositionCoordinate(const IComponent *comp, const std::string &name, const double value,
402 const std::string *const pDescription) {
403 Parameter_sptr param = get(comp, pos());
405 if (param) {
406 // so "pos" already defined
407 position = param->value<V3D>();
408 } else {
409 // so "pos" is not defined - therefore get position from component
410 position = comp->getPos();
411 }
412
413 // adjust position
414
415 if (name == posx())
417 else if (name == posy())
418 position.setY(value);
419 else if (name == posz())
420 position.setZ(value);
421 else {
422 g_log.warning() << "addPositionCoordinate() called with unrecognized "
423 "coordinate symbol: "
424 << name;
425 // set description if one is provided
426 if (pDescription) {
427 param->setDescription(*pDescription);
428 }
429 return;
430 }
431
432 // clear the position cache
434 // finally add or update "pos" parameter
435 addV3D(comp, pos(), position, pDescription);
436}
437
447void ParameterMap::addRotationParam(const IComponent *comp, const std::string &name, const double deg,
448 const std::string *const pDescription) {
449 Parameter_sptr paramRotX = get(comp, rotx());
450 Parameter_sptr paramRotY = get(comp, roty());
451 Parameter_sptr paramRotZ = get(comp, rotz());
452 double rotX, rotY, rotZ;
453
454 if (paramRotX)
455 rotX = paramRotX->value<double>();
456 else
457 rotX = 0.0;
458
459 if (paramRotY)
460 rotY = paramRotY->value<double>();
461 else
462 rotY = 0.0;
463
464 if (paramRotZ)
465 rotZ = paramRotZ->value<double>();
466 else
467 rotZ = 0.0;
468
469 // adjust rotation
470 Quat quat;
471 if (name == rotx()) {
472 addDouble(comp, rotx(), deg);
473 quat = Quat(deg, V3D(1, 0, 0)) * Quat(rotY, V3D(0, 1, 0)) * Quat(rotZ, V3D(0, 0, 1));
474 } else if (name == roty()) {
475 addDouble(comp, roty(), deg);
476 quat = Quat(rotX, V3D(1, 0, 0)) * Quat(deg, V3D(0, 1, 0)) * Quat(rotZ, V3D(0, 0, 1));
477 } else if (name == rotz()) {
478 addDouble(comp, rotz(), deg);
479 quat = Quat(rotX, V3D(1, 0, 0)) * Quat(rotY, V3D(0, 1, 0)) * Quat(deg, V3D(0, 0, 1));
480 } else {
481 g_log.warning() << "addRotationParam() called with unrecognized coordinate symbol: " << name;
482 return;
483 }
484
485 // clear the position cache
487
488 // finally add or update "pos" parameter
489 addQuat(comp, rot(), quat, pDescription);
490}
491
501void ParameterMap::addDouble(const IComponent *comp, const std::string &name, const std::string &value,
502 const std::string *const pDescription, const std::string &pVisible) {
503 add(pDouble(), comp, name, value, pDescription, pVisible);
504}
505
515void ParameterMap::addDouble(const IComponent *comp, const std::string &name, double value,
516 const std::string *const pDescription, const std::string &pVisible) {
517 add(pDouble(), comp, name, value, pDescription, pVisible);
518}
519
529void ParameterMap::addInt(const IComponent *comp, const std::string &name, const std::string &value,
530 const std::string *const pDescription, const std::string &pVisible) {
531 add(pInt(), comp, name, value, pDescription, pVisible);
532}
533
543void ParameterMap::addInt(const IComponent *comp, const std::string &name, int value,
544 const std::string *const pDescription, const std::string &pVisible) {
545 add(pInt(), comp, name, value, pDescription, pVisible);
546}
547
557void ParameterMap::addBool(const IComponent *comp, const std::string &name, const std::string &value,
558 const std::string *const pDescription, const std::string &pVisible) {
559 add(pBool(), comp, name, value, pDescription, pVisible);
560}
570void ParameterMap::addBool(const IComponent *comp, const std::string &name, bool value,
571 const std::string *const pDescription, const std::string &pVisible) {
572 add(pBool(), comp, name, value, pDescription, pVisible);
573}
574
583 const std::string name("masked");
584 auto param = create(pBool(), name);
585 auto typedParam = std::dynamic_pointer_cast<ParameterType<bool>>(param);
586 typedParam->setValue(value);
587
588// When using Clang & Linux, TBB 4.4 doesn't detect C++11 features.
589// https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/641658
590#if defined(__clang__) && !defined(__APPLE__)
591#define CLANG_ON_LINUX true
592#else
593#define CLANG_ON_LINUX false
594#endif
595#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
596 m_map.emplace(comp->getComponentID(), param);
597#else
598 m_map.insert(std::make_pair(comp->getComponentID(), param));
599#endif
600}
601
611void ParameterMap::addString(const IComponent *comp, const std::string &name, const std::string &value,
612 const std::string *const pDescription, const std::string &pVisible) {
613 add<std::string>(pString(), comp, name, value, pDescription, pVisible);
614}
615
624void ParameterMap::addV3D(const IComponent *comp, const std::string &name, const std::string &value,
625 const std::string *const pDescription) {
626 add(pV3D(), comp, name, value, pDescription);
628}
629
638void ParameterMap::addV3D(const IComponent *comp, const std::string &name, const V3D &value,
639 const std::string *const pDescription) {
640 add(pV3D(), comp, name, value, pDescription);
642}
643
652void ParameterMap::addQuat(const IComponent *comp, const std::string &name, const Quat &value,
653 const std::string *const pDescription) {
654 add(pQuat(), comp, name, value, pDescription);
656}
657
665bool ParameterMap::contains(const IComponent *comp, const std::string &name, const std::string &type) const {
666 return contains(comp, name.c_str(), type.c_str());
667}
668
677bool ParameterMap::contains(const IComponent *comp, const char *name, const char *type) const {
678 checkIsNotMaskingParameter(name);
679 if (m_map.empty())
680 return false;
681 const ComponentID id = comp->getComponentID();
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 &param = itr->second;
686 if (strcasecmp(param->nameAsCString(), name) == 0 && (anytype || param->type() == type)) {
687 return true;
688 }
689 }
690 return false;
691}
692
698bool ParameterMap::contains(const IComponent *comp, const Parameter &parameter) const {
699 checkIsNotMaskingParameter(parameter.name());
700 if (m_map.empty() || !comp)
701 return false;
702
703 const ComponentID id = comp->getComponentID();
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) {
708 const Parameter_sptr &param = itr->second;
709 if (*param == parameter)
710 return true;
711 }
712 return false;
713 } else
714 return false;
715}
716
723Parameter_sptr ParameterMap::get(const IComponent *comp, const std::string &name, const std::string &type) const {
724 return get(comp, name.c_str(), type.c_str());
725}
726
734std::shared_ptr<Parameter> ParameterMap::get(const IComponent *comp, const char *name, const char *type) const {
735 checkIsNotMaskingParameter(name);
736 Parameter_sptr result;
737 if (!comp)
738 return result;
739
740 auto itr = positionOf(comp, name, type);
741 if (itr != m_map.end())
742 result = std::atomic_load(&itr->second);
743 return result;
744}
745
753component_map_it ParameterMap::positionOf(const IComponent *comp, const char *name, const char *type) {
754 auto result = m_map.end();
755 if (!comp)
756 return result;
757 const bool anytype = (strlen(type) == 0);
758 if (!m_map.empty()) {
759 const ComponentID id = comp->getComponentID();
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 &param = itr->second;
765 if (strcasecmp(param->nameAsCString(), name) == 0 && (anytype || param->type() == type)) {
766 result = itr;
767 break;
768 }
769 }
770 }
771 }
772 return result;
773}
774
782component_map_cit ParameterMap::positionOf(const IComponent *comp, const char *name, const char *type) const {
783 auto result = m_map.end();
784 if (!comp)
785 return result;
786 const bool anytype = (strlen(type) == 0);
787 if (!m_map.empty()) {
788 const ComponentID id = comp->getComponentID();
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 &param = itr->second;
794 if (strcasecmp(param->nameAsCString(), name) == 0 && (anytype || param->type() == type)) {
795 result = itr;
796 break;
797 }
798 }
799 }
800 }
801 return result;
802}
803
809Parameter_sptr ParameterMap::getByType(const IComponent *comp, const std::string &type) const {
810 Parameter_sptr result;
811 if (!m_map.empty()) {
812 const ComponentID id = comp->getComponentID();
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 &param = itr->second;
818 if (strcasecmp(param->type().c_str(), type.c_str()) == 0) {
819 result = std::atomic_load(&param);
820 break;
821 }
822 } // found->firdst
823 } // it_found != m_map.end()
824 }
825 return result;
826}
827
834Parameter_sptr ParameterMap::getRecursiveByType(const IComponent *comp, const std::string &type) const {
835 std::shared_ptr<const IComponent> compInFocus(comp, NoDeleting());
836 while (compInFocus != nullptr) {
837 Parameter_sptr param = getByType(compInFocus.get(), type);
838 if (param) {
839 return param;
840 }
841 compInFocus = compInFocus->getParent();
842 }
843 // Nothing was found!
844 return Parameter_sptr();
845}
846
856 const std::string &type) const {
857 return getRecursive(comp, name.c_str(), type.c_str());
858}
859
868Parameter_sptr ParameterMap::getRecursive(const IComponent *comp, const char *name, const char *type) const {
869 checkIsNotMaskingParameter(name);
870 Parameter_sptr result = this->get(comp->getComponentID(), name, type);
871 if (result)
872 return result;
873
874 auto parent = comp->getParent();
875 while (parent) {
876 result = this->get(parent->getComponentID(), name, type);
877 if (result)
878 return result;
879 parent = parent->getParent();
880 }
881 return result;
882}
883
892std::string ParameterMap::getString(const IComponent *comp, const std::string &name, bool recursive) const {
893 Parameter_sptr param;
894 if (recursive) {
895 param = getRecursive(comp, name);
896 } else {
897 param = get(comp, name);
898 }
899 if (!param)
900 return "";
901 return param->asString();
902}
903
909std::set<std::string> ParameterMap::names(const IComponent *comp) const {
910 std::set<std::string> paramNames;
911 const ComponentID id = comp->getComponentID();
912 auto it_found = m_map.find(id);
913 if (it_found == m_map.end()) {
914 return paramNames;
915 }
916
917 auto itrs = m_map.equal_range(id);
918 for (auto it = itrs.first; it != itrs.second; ++it) {
919 paramNames.insert(it->second->name());
920 }
921
922 return paramNames;
923}
924
931std::string ParameterMap::asString() const {
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);
938 if (det) {
939 out << "detID:" << det->getID();
940 } else if (comp) {
941 out << comp->getFullName(); // Use full path name to ensure unambiguous
942 // naming
943 }
944 const auto paramVisible = "visible:" + std::string(p->visible() == 1 ? "true" : "false");
945 out << ';' << p->type() << ';' << p->name() << ';' << p->asString() << ';' << paramVisible << '|';
946 }
947 }
948 return out.str();
949}
950
958
962void ParameterMap::setCachedLocation(const IComponent *comp, const V3D &location) const {
963 m_cacheLocMap->setCache(comp->getComponentID(), location);
964}
965
970bool ParameterMap::getCachedLocation(const IComponent *comp, V3D &location) const {
971 return m_cacheLocMap->getCache(comp->getComponentID(), location);
972}
973
978 m_cacheRotMap->setCache(comp->getComponentID(), rotation);
979}
980
986 return m_cacheRotMap->getCache(comp->getComponentID(), rotation);
987}
988
996void ParameterMap::copyFromParameterMap(const IComponent *oldComp, const IComponent *newComp,
997 const ParameterMap *oldPMap) {
998
999 auto oldParameterNames = oldPMap->names(oldComp);
1000 for (const auto &oldParameterName : oldParameterNames) {
1001 Parameter_sptr thisParameter = oldPMap->get(oldComp, oldParameterName);
1002// Insert the fetched parameter in the m_map
1003#if TBB_VERSION_MAJOR >= 4 && TBB_VERSION_MINOR >= 4 && !CLANG_ON_LINUX
1004 m_map.emplace(newComp->getComponentID(), std::move(thisParameter));
1005#else
1006 m_map.insert(std::make_pair(newComp->getComponentID(), std::move(thisParameter)));
1007#endif
1008 }
1009}
1010
1011//--------------------------------------------------------------------------------------------
1016void ParameterMap::saveNexus(Nexus::File *file, const std::string &group) const {
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");
1028 std::string s = this->asString();
1029 file->writeData("data", s);
1030 file->closeGroup();
1031}
1032
1036const std::vector<std::string> &ParameterMap::getParameterFilenames() const { return m_parameterFileNames; }
1038
1041void ParameterMap::addParameterFilename(const std::string &filename) { m_parameterFileNames.emplace_back(filename); }
1042
1044std::shared_ptr<Parameter> ParameterMap::create(const std::string &className, const std::string &name,
1045 const std::string &visible) const {
1046 return ParameterFactory::create(className, name, visible);
1047}
1048
1055bool ParameterMap::hasDetectorInfo(const Instrument *instrument) const {
1056 if (instrument != m_instrument)
1057 return false;
1058 return static_cast<bool>(m_detectorInfo);
1059}
1060
1064bool ParameterMap::hasComponentInfo(const Instrument *instrument) const {
1065 if (instrument != m_instrument)
1066 return false;
1067 return static_cast<bool>(m_componentInfo);
1068}
1069
1073 throw std::runtime_error("Cannot return reference to NULL DetectorInfo");
1074 return *m_detectorInfo;
1075}
1076
1080 throw std::runtime_error("Cannot return reference to NULL DetectorInfo");
1081 return *m_detectorInfo;
1082}
1083
1087 throw std::runtime_error("Cannot return reference to NULL ComponentInfo");
1088 }
1089 return *m_componentInfo;
1090}
1091
1095 throw std::runtime_error("Cannot return reference to NULL ComponentInfo");
1096 }
1097 return *m_componentInfo;
1098}
1099
1101size_t ParameterMap::detectorIndex(const detid_t detID) const { return m_instrument->detectorIndex(detID); }
1102
1103size_t ParameterMap::componentIndex(const ComponentID componentId) const {
1104 return m_componentInfo->indexOf(componentId);
1105}
1106
1109 if (instrument == m_instrument)
1110 return;
1111 if (!instrument) {
1112 m_componentInfo = nullptr;
1113 m_detectorInfo = nullptr;
1114 return;
1115 }
1116 if (m_instrument)
1117 throw std::logic_error("ParameterMap::setInstrument: Cannot change "
1118 "instrument once it has been set.");
1119 if (instrument->isParametrized())
1120 throw std::logic_error("ParameterMap::setInstrument must be called with "
1121 "base instrument, not a parametrized instrument");
1122 m_instrument = instrument;
1124}
1125
1126} // namespace Mantid::Geometry
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
double position
Definition GetAllEi.cpp:154
#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)
Definition Component.cpp:75
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
base class for Geometric IComponent
Definition IComponent.h:53
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.
Definition IDetector.h:43
virtual detid_t getID() const =0
Get the detector ID.
Base Instrument Class.
Definition Instrument.h:47
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.
Definition Parameter.cpp:83
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.
Definition Parameter.h:37
const std::string & name() const
Parameter name.
Definition Parameter.h:45
Cache is a generic caching storage class.
Definition Cache.h:27
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
Class for quaternions.
Definition Quat.h:39
Class for 3D vectors.
Definition V3D.h:34
void setX(const double xx) noexcept
Set is x position.
Definition V3D.h:224
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.
Definition Parameter.h:194
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.
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)