-
Notifications
You must be signed in to change notification settings - Fork 122
/
MaskDetectors.cpp
382 lines (339 loc) · 13.7 KB
/
MaskDetectors.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/MaskDetectors.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/MaskWorkspace.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidKernel/BoundedValidator.h"
#include <set>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(MaskDetectors)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
using Geometry::Instrument_const_sptr;
using Geometry::IDetector_const_sptr;
using namespace DataObjects;
/// (Empty) Constructor
MaskDetectors::MaskDetectors() {}
/// Destructor
MaskDetectors::~MaskDetectors() {}
/*
* Define input arguments
*/
void MaskDetectors::init() {
declareProperty(
new WorkspaceProperty<Workspace>("Workspace", "", Direction::InOut),
"The name of the input and output workspace on which to perform the "
"algorithm.");
declareProperty(new ArrayProperty<specid_t>("SpectraList"),
"An ArrayProperty containing a list of spectra to mask");
declareProperty(
new ArrayProperty<detid_t>("DetectorList"),
"An ArrayProperty containing a list of detector ID's to mask");
declareProperty(new ArrayProperty<size_t>("WorkspaceIndexList"),
"An ArrayProperty containing the workspace indices to mask");
declareProperty(new WorkspaceProperty<>("MaskedWorkspace", "",
Direction::Input,
PropertyMode::Optional),
"If given but not as a SpecialWorkspace2D, the masking from "
"this workspace will be copied. If given as a "
"SpecialWorkspace2D, the masking is read from its Y values.");
auto mustBePosInt = boost::make_shared<BoundedValidator<int>>();
mustBePosInt->setLower(0);
declareProperty("StartWorkspaceIndex", 0, mustBePosInt,
"The index of the first workspace index of input "
"MaskedWorkspace to be included in the calculation. Default "
"is 0.");
declareProperty("EndWorkspaceIndex", EMPTY_INT(), mustBePosInt,
"The index number of the last workspace index of input "
"MaskedWorkspace to be included in the calculation. Default "
"is the last histogram.");
}
/*
* Main exec body
*/
void MaskDetectors::exec() {
// Get the input workspace
Workspace_sptr propWS = getProperty("Workspace");
MatrixWorkspace_sptr WS =
boost::dynamic_pointer_cast<MatrixWorkspace>(propWS);
PeaksWorkspace_sptr peaksWS =
boost::dynamic_pointer_cast<PeaksWorkspace>(propWS);
if (peaksWS) {
execPeaks(peaksWS);
return;
}
// Is it an event workspace?
EventWorkspace_sptr eventWS = boost::dynamic_pointer_cast<EventWorkspace>(WS);
// Is it a Mask Workspace ?
MaskWorkspace_sptr isMaskWS = boost::dynamic_pointer_cast<MaskWorkspace>(WS);
// TODO: Is it an MDEventWorkspace?
MDEventWorkspace_sptr isMDEVentWS = boost::dynamic_pointer_cast<MDEventWorkspace>(WS);
std::vector<size_t> indexList = getProperty("WorkspaceIndexList");
std::vector<specid_t> spectraList = getProperty("SpectraList");
const std::vector<detid_t> detectorList = getProperty("DetectorList");
const MatrixWorkspace_sptr prevMasking = getProperty("MaskedWorkspace");
// each one of these values is optional but the user can't leave all four
// blank
if (indexList.empty() && spectraList.empty() && detectorList.empty() &&
!prevMasking) {
g_log.information(
name() +
": There is nothing to mask, the index, spectra, "
"detector lists and masked workspace properties are all empty");
return;
}
// If the spectraList property has been set, need to loop over the workspace
// looking for the
// appropriate spectra number and adding the indices they are linked to the
// list to be processed
if (!spectraList.empty()) {
fillIndexListFromSpectra(indexList, spectraList, WS);
} // End dealing with spectraList
else if (!detectorList.empty()) {
// Convert from detectors to workspace indexes
WS->getIndicesFromDetectorIDs(detectorList, indexList);
}
// If we have a workspace that could contain masking,copy that in too
if (prevMasking) {
DataObjects::MaskWorkspace_const_sptr maskWS =
boost::dynamic_pointer_cast<DataObjects::MaskWorkspace>(prevMasking);
if (maskWS) {
if (maskWS->getInstrument()->getDetectorIDs().size() !=
WS->getInstrument()->getDetectorIDs().size()) {
throw std::runtime_error(
"Size mismatch between input Workspace and MaskWorkspace");
}
g_log.debug() << "Extracting mask from MaskWorkspace (" << maskWS->name()
<< ")" << std::endl;
appendToIndexListFromMaskWS(indexList, maskWS);
} else {
// Check the provided workspace has the same number of spectra as the
// input
if (prevMasking->getNumberHistograms() > WS->getNumberHistograms()) {
g_log.error() << "Input workspace has " << WS->getNumberHistograms()
<< " histograms vs. "
<< "Input masking workspace has "
<< prevMasking->getNumberHistograms() << " histograms. "
<< std::endl;
throw std::runtime_error("Size mismatch between two input workspaces.");
}
appendToIndexListFromWS(indexList, prevMasking);
}
}
// Need to get hold of the parameter map
Geometry::ParameterMap &pmap = WS->instrumentParameters();
// If explicitly given a list of detectors to mask, just mark those.
// Otherwise, mask all detectors pointing to the requested spectra in
// indexlist loop below
std::vector<detid_t>::const_iterator it;
Instrument_const_sptr instrument = WS->getInstrument();
if (!detectorList.empty()) {
for (it = detectorList.begin(); it != detectorList.end(); ++it) {
try {
if (const Geometry::ComponentID det =
instrument->getDetector(*it)->getComponentID()) {
pmap.addBool(det, "masked", true);
}
} catch (Kernel::Exception::NotFoundError &e) {
g_log.warning() << e.what() << " Found while running MaskDetectors"
<< std::endl;
}
}
}
if (indexList.empty()) {
g_log.warning("No spectra affected.");
return;
}
// Get a reference to the spectra-detector map to get hold of detector ID's
double prog = 0.0;
std::vector<size_t>::const_iterator wit;
for (wit = indexList.begin(); wit != indexList.end(); ++wit) {
WS->maskWorkspaceIndex(*wit);
// Progress
prog += (1.0 / static_cast<int>(indexList.size()));
progress(prog);
}
if (eventWS) {
// Also clear the MRU for event workspaces.
eventWS->clearMRU();
}
if (isMaskWS) {
// If the input was a mask workspace, then extract the mask to ensure
// we are returning the correct thing.
IAlgorithm_sptr alg = createChildAlgorithm("ExtractMask");
alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", WS);
alg->executeAsChildAlg();
MatrixWorkspace_sptr ws = alg->getProperty("OutputWorkspace");
setProperty("Workspace", ws);
}
/*
This rebuild request call, gives the workspace the opportunity to rebuild the
nearest neighbours map
and therfore pick up any detectors newly masked with this algorithm.
*/
WS->rebuildNearestNeighbours();
}
/*
* Peaks exec body
* @param WS :: The input peaks workspace to be masked
*/
void MaskDetectors::execPeaks(PeaksWorkspace_sptr WS) {
std::vector<detid_t> detectorList = getProperty("DetectorList");
const MatrixWorkspace_sptr prevMasking = getProperty("MaskedWorkspace");
// each one of these values is optional but the user can't leave all four
// blank
if (detectorList.empty() && !prevMasking) {
g_log.information(
name() +
": There is nothing to mask, "
"detector lists and masked workspace properties are all empty");
return;
}
// Need to get hold of the parameter map and instrument
Geometry::ParameterMap &pmap = WS->instrumentParameters();
Instrument_const_sptr instrument = WS->getInstrument();
// If we have a workspace that could contain masking,copy that in too
if (prevMasking) {
DataObjects::MaskWorkspace_sptr maskWS =
boost::dynamic_pointer_cast<DataObjects::MaskWorkspace>(prevMasking);
if (maskWS) {
Geometry::ParameterMap &maskPmap = maskWS->instrumentParameters();
Instrument_const_sptr maskInstrument = maskWS->getInstrument();
if (maskInstrument->getDetectorIDs().size() !=
WS->getInstrument()->getDetectorIDs().size()) {
throw std::runtime_error(
"Size mismatch between input Workspace and MaskWorkspace");
}
g_log.debug() << "Extracting mask from MaskWorkspace (" << maskWS->name()
<< ")" << std::endl;
std::vector<detid_t> detectorIDs = maskInstrument->getDetectorIDs();
std::vector<detid_t>::const_iterator it;
for (it = detectorIDs.begin(); it != detectorIDs.end(); ++it) {
try {
if (const Geometry::ComponentID det =
maskInstrument->getDetector(*it)->getComponentID()) {
Geometry::Parameter_sptr maskedParam = maskPmap.get(det, "masked");
int detID =
static_cast<int>(maskInstrument->getDetector(*it)->getID());
if (maskedParam)
detectorList.push_back(detID);
}
} catch (Kernel::Exception::NotFoundError &e) {
g_log.warning() << e.what() << " Found while running MaskDetectors"
<< std::endl;
}
}
}
}
// If explicitly given a list of detectors to mask, just mark those.
// Otherwise, mask all detectors pointing to the requested spectra in
// indexlist loop below
std::vector<detid_t>::const_iterator it;
if (!detectorList.empty()) {
for (it = detectorList.begin(); it != detectorList.end(); ++it) {
try {
if (const Geometry::ComponentID det =
instrument->getDetector(*it)->getComponentID()) {
pmap.addBool(det, "masked", true);
}
} catch (Kernel::Exception::NotFoundError &e) {
g_log.warning() << e.what() << " Found while running MaskDetectors"
<< std::endl;
}
}
}
}
/**
* @brief MaskDetectors::execMDWorkspaces
*/
MaskDetectors::execMDWorkspaces()
{
size_t numruns = WS->numRuns();
for (size_t irun = 0; irun < numruns; ++irun)
{
WS->getExperimetnInfo(irun)->getIntrument();
}
}
/**
* Convert a list of spectra numbers into the corresponding workspace indices
* @param indexList :: An output index list from the given spectra list
* @param spectraList :: A list of spectra numbers
* @param WS :: The input workspace to be masked
*/
void MaskDetectors::fillIndexListFromSpectra(
std::vector<size_t> &indexList, const std::vector<specid_t> &spectraList,
const API::MatrixWorkspace_sptr WS) {
// Convert the vector of properties into a set for easy searching
std::set<int64_t> spectraSet(spectraList.begin(), spectraList.end());
// Next line means that anything in Clear the index list first
indexList.clear();
indexList.reserve(WS->getNumberHistograms());
for (int i = 0; i < static_cast<int>(WS->getNumberHistograms()); ++i) {
const specid_t currentSpec = WS->getSpectrum(i)->getSpectrumNo();
if (spectraSet.find(currentSpec) != spectraSet.end()) {
indexList.push_back(i);
}
}
}
/**
* Append the indices of the masked spectra from the given workspace list to the
* given list
* @param indexList :: An existing list of indices
* @param maskedWorkspace :: An workspace with masked spectra
*/
void MaskDetectors::appendToIndexListFromWS(
std::vector<size_t> &indexList,
const MatrixWorkspace_sptr maskedWorkspace) {
// Convert the vector of properties into a set for easy searching
std::set<int64_t> existingIndices(indexList.begin(), indexList.end());
int endIndex = getProperty("EndWorkspaceIndex");
if (endIndex == EMPTY_INT())
endIndex = static_cast<int>(maskedWorkspace->getNumberHistograms() - 1);
int startIndex = getProperty("StartWorkspaceIndex");
for (int64_t i = startIndex; i <= endIndex; ++i) {
IDetector_const_sptr det;
try {
det = maskedWorkspace->getDetector(i - startIndex);
} catch (Exception::NotFoundError &) {
continue;
}
if (det->isMasked() && existingIndices.count(i) == 0) {
indexList.push_back(i);
}
}
return;
} // appendToIndexListFromWS
/**
* Append the indices of the masked spectra from the given workspace list to the
* given list
* @param indexList :: An existing list of indices
* @param maskedWorkspace :: An workspace with masked spectra
*/
void MaskDetectors::appendToIndexListFromMaskWS(
std::vector<size_t> &indexList,
const DataObjects::MaskWorkspace_const_sptr maskedWorkspace) {
// Convert the vector of properties into a set for easy searching
std::set<int64_t> existingIndices(indexList.begin(), indexList.end());
int endIndex = getProperty("EndWorkspaceIndex");
if (endIndex == EMPTY_INT())
endIndex = static_cast<int>(maskedWorkspace->getNumberHistograms() - 1);
int startIndex = getProperty("StartWorkspaceIndex");
for (int64_t i = startIndex; i <= endIndex; ++i) {
if (maskedWorkspace->dataY(i - startIndex)[0] > 0.5 &&
existingIndices.count(i) == 0) {
g_log.debug() << "Adding WorkspaceIndex " << i << " to mask."
<< std::endl;
indexList.push_back(i);
}
}
return;
} // appendToIndexListFromWS
} // namespace DataHandling
} // namespace Mantid