15#include <Poco/DOM/DOMParser.h>
16#include <Poco/DOM/Document.h>
17#include <Poco/DOM/NodeList.h>
36 : m_instrument(
std::move(instrument)), m_mainFieldDirection(
std::move(mainFieldDirection)) {}
48 std::string parameterName =
"Default grouping file";
49 auto loadedGrouping = std::make_shared<Grouping>();
56 std::vector<std::string> groupingFiles =
m_instrument->getStringParameter(parameterName);
58 if (groupingFiles.size() == 1) {
59 const std::string groupingFile = groupingFiles[0];
66 throw std::runtime_error(
"Multiple groupings specified for the instrument");
68 return loadedGrouping;
80 Poco::AutoPtr<Document> pDoc;
82 pDoc = pParser.parse(filename);
88 Element *pRootElem = pDoc->documentElement();
89 if (!pRootElem->hasChildNodes())
93 Poco::AutoPtr<NodeList> groups = pRootElem->getElementsByTagName(
"group");
94 if (groups->length() == 0)
99 grouping.
groups.resize(groups->length());
101 for (
size_t ig = 0; ig < groups->length(); ig++) {
102 auto *pGroupElem =
static_cast<Element *
>(groups->item(
static_cast<long>(ig)));
104 if (!pGroupElem->hasAttribute(
"name"))
107 grouping.
groupNames[ig] = pGroupElem->getAttribute(
"name");
109 Element *idlistElement = pGroupElem->getChildElement(
"ids");
113 grouping.
groups[ig] = idlistElement->getAttribute(
"val");
117 Poco::AutoPtr<NodeList> pairs = pRootElem->getElementsByTagName(
"pair");
120 grouping.
pairNames.resize(pairs->length());
121 grouping.
pairs.resize(pairs->length());
124 for (
size_t ip = 0; ip < pairs->length(); ip++) {
125 auto *pPairElem =
static_cast<Element *
>(pairs->item(
static_cast<long>(ip)));
127 if (!pPairElem->hasAttribute(
"name"))
130 grouping.
pairNames[ip] = pPairElem->getAttribute(
"name");
132 size_t fwdGroupId, bwdGroupId;
135 if (Element *fwdElement = pPairElem->getChildElement(
"forward-group")) {
136 if (!fwdElement->hasAttribute(
"val"))
140 auto it = std::find(grouping.
groupNames.begin(), grouping.
groupNames.end(), fwdElement->getAttribute(
"val"));
146 fwdGroupId = it - grouping.
groupNames.begin();
152 if (Element *bwdElement = pPairElem->getChildElement(
"backward-group")) {
153 if (!bwdElement->hasAttribute(
"val"))
157 auto it = std::find(grouping.
groupNames.begin(), grouping.
groupNames.end(), bwdElement->getAttribute(
"val"));
163 bwdGroupId = it - grouping.
groupNames.begin();
168 grouping.
pairs[ip] = std::make_pair(fwdGroupId, bwdGroupId);
171 if (Element *aElement = pPairElem->getChildElement(
"alpha")) {
172 if (!aElement->hasAttribute(
"val"))
177 grouping.
pairAlphas[ip] = boost::lexical_cast<double>(aElement->getAttribute(
"val"));
179 catch (boost::bad_lexical_cast &) {
190 if (pRootElem->hasAttribute(
"description")) {
191 grouping.
description = pRootElem->getAttribute(
"description");
195 if (Element *defaultElement = pRootElem->getChildElement(
"default")) {
196 if (!defaultElement->hasAttribute(
"name"))
199 grouping.
defaultName = defaultElement->getAttribute(
"name");
209 std::ostringstream all;
212 auto dummyGrouping = std::make_shared<Mantid::API::Grouping>();
213 dummyGrouping->description =
"Dummy grouping";
214 dummyGrouping->groupNames.emplace_back(
"all");
215 dummyGrouping->groups.emplace_back(all.str());
216 return dummyGrouping;
225 for (
size_t row = 0; row < table->rowCount(); ++row) {
226 std::vector<int> detectors = table->cell<std::vector<int>>(row, 0);
229 std::sort(detectors.begin(), detectors.end());
235 this->
groups.emplace_back(detectorRange);
239 if (this->
groups.size() == 2) {
242 this->
pairs.emplace_back(0, 1);
256 newTable->addColumn(
"vector_int",
"Detectors");
258 for (
const auto &group : this->
groups) {
259 TableRow newRow = newTable->appendRow();
static void loadGroupingFromXML(const std::string &filename, Grouping &grouping)
Loads grouping from the XML file specified.
std::shared_ptr< Grouping > getDummyGrouping()
Returns a "dummy" grouping of a single group with all the detectors in it.
std::shared_ptr< Grouping > getGroupingFromIDF() const
Load the grouping from the instrument's IDF.
const std::string m_mainFieldDirection
Orientation of instrument (e.g. for MUSR)
const Geometry::Instrument_const_sptr m_instrument
Instrument to load grouping from.
virtual ~GroupingLoader()
Destructor.
GroupingLoader(Geometry::Instrument_const_sptr instrument)
Constructor without field direction.
Structure to represent grouping information.
Grouping()=default
Default constructor.
std::vector< std::pair< size_t, size_t > > pairs
std::vector< std::string > groupNames
std::vector< double > pairAlphas
std::vector< std::string > pairNames
std::vector< std::string > groups
ITableWorkspace_sptr toTable() const
Convert to grouping table.
TableRow represents a row in a TableWorkspace.
Records the filename and the description of failure.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
MANTID_KERNEL_DLL std::vector< int > parseRange(const std::string &str, const std::string &elemSep=",", const std::string &rangeSep="-")
Parses a number range, e.g.
std::string toString(const T &value)
Convert a number to a string.
std::string to_string(const wide_integer< Bits, Signed > &n)