Mantid
Loading...
Searching...
No Matches
MuonGroupingXMLHelper.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 +
8
10#include "MantidMuon/MuonAlgorithmHelper.h"
11
12#include <Poco/DOM/DOMParser.h>
13#include <Poco/DOM/DOMWriter.h>
14#include <Poco/DOM/Document.h>
15#include <Poco/DOM/NodeList.h>
16#include <Poco/DOM/Text.h>
17#include <Poco/XML/XMLWriter.h>
18
19using namespace Mantid;
20// using namespace Mantid::Kernel;
21using namespace Mantid::API;
22using namespace ScopedFileHelper;
23
24namespace MuonGroupingXMLHelper {
25
32ScopedFileHelper::ScopedFile createGroupingXMLSingleGroup(const std::string &groupName, const std::string &group) {
33 std::string fileContents("");
34 fileContents += "<detector-grouping description=\"test XML file\"> \n";
35 fileContents += "\t<group name=\"" + groupName + "\"> \n";
36 fileContents += "\t\t<ids val=\"" + group + "\"/>\n";
37 fileContents += "\t</group>\n";
38 fileContents += "\t<default name=\"" + groupName + "\"/>\n";
39 fileContents += "</detector-grouping>";
40
41 ScopedFileHelper::ScopedFile file(fileContents, "testXML_1.xml");
42 return file;
43}
44
52ScopedFileHelper::ScopedFile createGroupingXMLSinglePair(const std::string &pairName, const std::string &groupName) {
53
54 std::string fileContents("");
55
56 fileContents += "<detector-grouping description=\"test XML file\"> \n";
57 fileContents += "\t<group name=\"group1\"> \n";
58 fileContents += "\t\t<ids val=\"1\"/>\n";
59 fileContents += "\t</group>\n";
60
61 fileContents += "<detector-grouping description=\"test XML file\"> \n";
62 fileContents += "\t<group name=\"group2\"> \n";
63 fileContents += "\t\t<ids val=\"2\"/>\n";
64 fileContents += "\t</group>\n";
65
66 fileContents += "\t<pair name=\"" + pairName + "\"> \n";
67 fileContents += "\t\t<forward-group val=\"group1\"/>\n";
68 fileContents += "\t\t<backward-group val=\"" + groupName + "\"/>\n";
69 fileContents += "\t\t<alpha val=\"1\"/>\n";
70 fileContents += "\t</pair>\n";
71
72 fileContents += "\t<default name=\"" + groupName + "\"/>\n";
73 fileContents += "</detector-grouping>";
74
75 ScopedFileHelper::ScopedFile file(fileContents, "testXML_1.xml");
76
77 return file;
78}
79
88ScopedFileHelper::ScopedFile createXMLwithPairsAndGroups(const int &nGroups, const int &nDetectorsPerGroup) {
89
90 API::Grouping grouping;
91 std::string groupIDs;
92 // groups
93 for (auto group = 1; group <= nGroups; group++) {
94 std::string groupName = "group" + std::to_string(group);
95 if (nGroups == 1) {
96 groupIDs = "1";
97 } else {
98 groupIDs =
99 std::to_string((group - 1) * nDetectorsPerGroup + 1) + "-" + std::to_string(group * nDetectorsPerGroup);
100 }
101 grouping.groupNames.emplace_back(groupName);
102 grouping.groups.emplace_back(groupIDs);
103 }
104 // pairs
105 for (auto pair = 1; pair < nGroups; pair++) {
106 std::string pairName = "pair" + std::to_string(pair);
107 std::pair<size_t, size_t> pairIndices;
108 pairIndices.first = 0;
109 pairIndices.second = pair;
110 grouping.pairNames.emplace_back(pairName);
111 grouping.pairAlphas.emplace_back(1.0);
112 grouping.pairs.emplace_back(pairIndices);
113 }
114
115 auto fileContents = groupingToXML(grouping);
116 ScopedFileHelper::ScopedFile file(fileContents, "testXML_1.xml");
117 return file;
118}
119
124std::string groupingToXML(const Mantid::API::Grouping &grouping) {
125
126 Poco::XML::DOMWriter writer;
127 writer.setNewLine("\n");
128 writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
129
130 Poco::AutoPtr<Poco::XML::Document> mDoc = new Poco::XML::Document();
131
132 // Create root element with a description
133 Poco::AutoPtr<Poco::XML::Element> rootElem = mDoc->createElement("detector-grouping");
134 rootElem->setAttribute("description", grouping.description);
135 mDoc->appendChild(rootElem);
136
137 // Create group elements
138 for (size_t gi = 0; gi < grouping.groups.size(); gi++) {
139 Poco::AutoPtr<Poco::XML::Element> gElem = mDoc->createElement("group");
140 gElem->setAttribute("name", grouping.groupNames[gi]);
141 rootElem->appendChild(gElem);
142
143 Poco::AutoPtr<Poco::XML::Element> idsElem = mDoc->createElement("ids");
144 idsElem->setAttribute("val", grouping.groups[gi]);
145 gElem->appendChild(idsElem);
146 }
147
148 // Create pair elements
149 for (size_t pi = 0; pi < grouping.pairs.size(); pi++) {
150 Poco::AutoPtr<Poco::XML::Element> gElem = mDoc->createElement("pair");
151 gElem->setAttribute("name", grouping.pairNames[pi]);
152 rootElem->appendChild(gElem);
153
154 Poco::AutoPtr<Poco::XML::Element> fwElem = mDoc->createElement("forward-group");
155 fwElem->setAttribute("val", grouping.groupNames[grouping.pairs[pi].first]);
156 gElem->appendChild(fwElem);
157
158 Poco::AutoPtr<Poco::XML::Element> bwElem = mDoc->createElement("backward-group");
159 bwElem->setAttribute("val", grouping.groupNames[grouping.pairs[pi].second]);
160 gElem->appendChild(bwElem);
161
162 Poco::AutoPtr<Poco::XML::Element> alphaElem = mDoc->createElement("alpha");
163 alphaElem->setAttribute("val", boost::lexical_cast<std::string>(grouping.pairAlphas[pi]));
164 gElem->appendChild(alphaElem);
165 }
166
167 // Create default group/pair name element
168 Poco::AutoPtr<Poco::XML::Element> gElem = mDoc->createElement("default");
169 gElem->setAttribute("name", grouping.defaultName);
170 rootElem->appendChild(gElem);
171
172 std::stringstream stream;
173 writer.writeNode(stream, mDoc);
174 return stream.str();
175}
176
177} // namespace MuonGroupingXMLHelper
Structure to represent grouping information.
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
Helper class which provides the Collimation Length for SANS instruments.
ScopedFileHelper::ScopedFile createGroupingXMLSinglePair(const std::string &pairName, const std::string &groupName)
Create an XML with two simple groups and a pair made from them.
DLLExport std::string groupingToXML(const Mantid::API::Grouping &grouping)
Create an XML file (as a string) containing muon grouping information.
ScopedFileHelper::ScopedFile createGroupingXMLSingleGroup(const std::string &groupName, const std::string &group)
Simplest possible grouping file, with only a single group.
ScopedFileHelper::ScopedFile createXMLwithPairsAndGroups(const int &nGroups=1, const int &nDetectorsPerGroup=1)
Create an XML file with grouping/pairing information.
std::string to_string(const wide_integer< Bits, Signed > &n)