-
Notifications
You must be signed in to change notification settings - Fork 121
/
SaveMask.cpp
190 lines (155 loc) · 5.31 KB
/
SaveMask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <fstream>
#include <sstream>
#include <algorithm>
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/ISpectrum.h"
#include "MantidDataHandling/SaveMask.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
#include "MantidKernel/System.h"
#include <boost/shared_ptr.hpp>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/Text.h>
#include <Poco/DOM/AutoPtr.h>
#include <Poco/DOM/DOMWriter.h>
#ifdef _MSC_VER
// Disable a flood of warnings from Poco about inheriting from
// std::basic_istream
// See
// http://connect.microsoft.com/VisualStudio/feedback/details/733720/inheriting-from-std-fstream-produces-c4250-warning
#pragma warning(push)
#pragma warning(disable : 4250)
#endif
#include <Poco/XML/XMLWriter.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Poco::XML;
namespace Mantid {
namespace DataHandling {
DECLARE_ALGORITHM(SaveMask)
/// Define input parameters
void SaveMask::init() {
declareProperty(make_unique<API::WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"Workspace to output masking to XML file");
declareProperty(
make_unique<FileProperty>("OutputFile", "", FileProperty::Save, ".xml"),
"File to save the detectors mask in XML format");
}
/// Main body to execute algorithm
void SaveMask::exec() {
// 1. Get input
API::MatrixWorkspace_sptr userInputWS = this->getProperty("InputWorkspace");
DataObjects::SpecialWorkspace2D_sptr inpWS =
boost::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(userInputWS);
if (!inpWS) {
// extract the masking and use that
Algorithm_sptr emAlg =
this->createChildAlgorithm("ExtractMask", 0.0, 0.5, false);
emAlg->setProperty("InputWorkspace", userInputWS);
emAlg->setPropertyValue("OutputWorkspace", "tmp");
emAlg->execute();
API::MatrixWorkspace_sptr ws = emAlg->getProperty("OutputWorkspace");
inpWS = boost::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(ws);
if (!inpWS) {
throw std::runtime_error(
"Unable to extract masking data using ExtractMask");
}
}
std::string outxmlfilename = this->getPropertyValue("OutputFile");
// 2. Convert Workspace to ...
std::vector<detid_t> detid0s;
for (size_t i = 0; i < inpWS->getNumberHistograms(); i++) {
if (inpWS->dataY(i)[0] > 0.1) {
// It is way from 0 but smaller than 1
for (const auto &det_id : inpWS->getSpectrum(i).getDetectorIDs()) {
detid0s.push_back(det_id);
}
}
}
// d) sort
g_log.debug() << "Number of detectors to be masked = " << detid0s.size()
<< '\n';
// 3. Count workspace to count 1 and 0
std::vector<detid_t> idx0sts; // starting point of the pair
std::vector<detid_t> idx0eds; // ending point of pair
if (!detid0s.empty()) {
std::sort(detid0s.begin(), detid0s.end());
detid_t i0st = detid0s[0];
detid_t i0ed = detid0s[0];
for (size_t i = 1; i < detid0s.size(); i++) {
if (detid0s[i] == detid0s[i - 1] + 1) {
// If it is continuous: record the current one
i0ed = detid0s[i];
} else {
// If skip: restart everything
// i) record previous result
idx0sts.push_back(i0st);
idx0eds.push_back(i0ed);
// ii) reset the register
i0st = detid0s[i];
i0ed = detid0s[i];
}
} // for
// Complete the registration
idx0sts.push_back(i0st);
idx0eds.push_back(i0ed);
for (size_t i = 0; i < idx0sts.size(); i++) {
g_log.information() << "Section " << i << " : " << idx0sts[i] << " , "
<< idx0eds[i] << " to be masked and recorded.\n";
}
} // Only work for detid > 0
// 4. Write out to XML nodes
// a) Create document and root node
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElement("detector-masking");
pDoc->appendChild(pRoot);
// pRoot->setAttribute("default", "use");
// b) Append Group
AutoPtr<Element> pChildGroup = pDoc->createElement("group");
// pChildGroup->setAttribute("type", "notuse");
pRoot->appendChild(pChildGroup);
// c) Append detid
// c1. Generate text value
std::stringstream ss;
for (size_t i = 0; i < idx0sts.size(); i++) {
size_t ist = idx0sts[i];
size_t ied = idx0eds[i];
// a-b or a
bool writedata = true;
if (ist < ied) {
ss << ist << "-" << ied;
} else if (ist == ied) {
ss << ist;
} else {
writedata = false;
}
// add ","
if (writedata && i < idx0sts.size() - 1) {
ss << ",";
}
} // for
std::string textvalue = ss.str();
g_log.debug() << "SaveMask main text: available section = " << idx0sts.size()
<< "\n" << textvalue << '\n';
// c2. Create element
AutoPtr<Element> pDetid = pDoc->createElement("detids");
AutoPtr<Text> pText1 = pDoc->createTextNode(textvalue);
pDetid->appendChild(pText1);
pChildGroup->appendChild(pDetid);
// 4. Write
DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(XMLWriter::PRETTY_PRINT);
std::ofstream ofs;
ofs.open(outxmlfilename.c_str(), std::fstream::out);
ofs << "<?xml version=\"1.0\"?>\n";
writer.writeNode(std::cout, pDoc);
writer.writeNode(ofs, pDoc);
ofs.close();
}
} // namespace DataHandling
} // namespace Mantid