/
CentroidPeaks.cpp
406 lines (357 loc) · 14.4 KB
/
CentroidPeaks.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidCrystal/CentroidPeaks.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
using Mantid::DataObjects::PeaksWorkspace;
namespace Mantid {
namespace Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CentroidPeaks)
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using namespace Mantid::Crystal;
//----------------------------------------------------------------------------------------------
/** Constructor
*/
CentroidPeaks::CentroidPeaks() {}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
CentroidPeaks::~CentroidPeaks() {}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CentroidPeaks::init() {
declareProperty(make_unique<WorkspaceProperty<PeaksWorkspace>>(
"InPeaksWorkspace", "", Direction::Input),
"A PeaksWorkspace containing the peaks to centroid.");
declareProperty(
make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
"An input 2D Workspace.");
declareProperty(
make_unique<PropertyWithValue<int>>("PeakRadius", 10, Direction::Input),
"Fixed radius around each peak position in which to calculate the "
"centroid.");
declareProperty(
make_unique<PropertyWithValue<int>>("EdgePixels", 0, Direction::Input),
"The number of pixels where peaks are removed at edges. Only "
"for instruments with RectangularDetectors. ");
declareProperty(
make_unique<WorkspaceProperty<PeaksWorkspace>>("OutPeaksWorkspace", "",
Direction::Output),
"The output PeaksWorkspace will be a copy of the input PeaksWorkspace "
"with the peaks' positions modified by the new found centroids.");
}
//----------------------------------------------------------------------------------------------
/** Integrate the peaks of the workspace using parameters saved in the algorithm
* class
*/
void CentroidPeaks::integrate() {
/// Peak workspace to centroid
Mantid::DataObjects::PeaksWorkspace_sptr inPeakWS =
getProperty("InPeaksWorkspace");
/// Output peaks workspace, create if needed
Mantid::DataObjects::PeaksWorkspace_sptr peakWS =
getProperty("OutPeaksWorkspace");
if (peakWS != inPeakWS)
peakWS = inPeakWS->clone();
/// Radius to use around peaks
int PeakRadius = getProperty("PeakRadius");
int MinPeaks = -1;
int MaxPeaks = -1;
size_t Numberwi = inWS->getNumberHistograms();
int NumberPeaks = peakWS->getNumberPeaks();
for (int i = 0; i < NumberPeaks; i++) {
Peak &peak = peakWS->getPeaks()[i];
int pixelID = peak.getDetectorID();
// Find the workspace index for this detector ID
if (wi_to_detid_map.find(pixelID) != wi_to_detid_map.end()) {
size_t wi = wi_to_detid_map[pixelID];
if (MinPeaks == -1 && peak.getRunNumber() == inWS->getRunNumber() &&
wi < Numberwi)
MinPeaks = i;
if (peak.getRunNumber() == inWS->getRunNumber() && wi < Numberwi)
MaxPeaks = i;
}
}
int Edge = getProperty("EdgePixels");
Progress prog(this, MinPeaks, 1.0, MaxPeaks);
PARALLEL_FOR2(inWS, peakWS)
for (int i = MinPeaks; i <= MaxPeaks; i++) {
PARALLEL_START_INTERUPT_REGION
// Get a direct ref to that peak.
IPeak &peak = peakWS->getPeak(i);
int col = peak.getCol();
int row = peak.getRow();
int pixelID = peak.getDetectorID();
detid2index_map::const_iterator it = wi_to_detid_map.find(pixelID);
if (it == wi_to_detid_map.end()) {
continue;
}
size_t workspaceIndex = it->second;
double TOFPeakd = peak.getTOF();
const MantidVec &X = inWS->readX(workspaceIndex);
int chan = Kernel::VectorHelper::getBinIndex(X, TOFPeakd);
std::string bankName = peak.getBankName();
double intensity = 0.0;
double chancentroid = 0.0;
int chanstart = std::max(0, chan - PeakRadius);
int chanend = std::min(static_cast<int>(X.size()), chan + PeakRadius);
double rowcentroid = 0.0;
int rowstart = std::max(0, row - PeakRadius);
int rowend = row + PeakRadius;
double colcentroid = 0.0;
int colstart = col - PeakRadius;
int colend = col + PeakRadius;
for (int ichan = chanstart; ichan <= chanend; ++ichan) {
for (int irow = rowstart; irow <= rowend; ++irow) {
for (int icol = colstart; icol <= colend; ++icol) {
if (edgePixel(bankName, icol, irow, Edge))
continue;
detid2index_map::const_iterator it =
wi_to_detid_map.find(findPixelID(bankName, icol, irow));
if (it == wi_to_detid_map.end())
continue;
size_t workspaceIndex = (it->second);
const MantidVec &histogram = inWS->readY(workspaceIndex);
intensity += histogram[ichan];
rowcentroid += irow * histogram[ichan];
colcentroid += icol * histogram[ichan];
chancentroid += ichan * histogram[ichan];
}
}
}
// Set pixelID to change row and col
row = int(rowcentroid / intensity);
row = std::max(0, row);
col = int(colcentroid / intensity);
col = std::max(0, col);
chan = int(chancentroid / intensity);
chan = std::max(0, chan);
chan = std::min(static_cast<int>(inWS->blocksize()), chan);
peak.setDetectorID(findPixelID(bankName, col, row));
// Set wavelength to change tof for peak object
if (!edgePixel(bankName, col, row, Edge)) {
it = wi_to_detid_map.find(findPixelID(bankName, col, row));
workspaceIndex = (it->second);
Mantid::Kernel::Units::Wavelength wl;
std::vector<double> timeflight;
timeflight.push_back(inWS->readX(workspaceIndex)[chan]);
double scattering = peak.getScattering();
double L1 = peak.getL1();
double L2 = peak.getL2();
wl.fromTOF(timeflight, timeflight, L1, L2, scattering, 0, 0, 0);
const double lambda = timeflight[0];
timeflight.clear();
peak.setWavelength(lambda);
peak.setBinCount(inWS->readY(workspaceIndex)[chan]);
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
for (int i = int(peakWS->getNumberPeaks()) - 1; i >= 0; --i) {
// Get a direct ref to that peak.
IPeak &peak = peakWS->getPeak(i);
int col = peak.getCol();
int row = peak.getRow();
std::string bankName = peak.getBankName();
if (edgePixel(bankName, col, row, Edge)) {
peakWS->removePeak(i);
}
}
// Save the output
setProperty("OutPeaksWorkspace", peakWS);
}
//----------------------------------------------------------------------------------------------
/** Integrate the peaks of the workspace using parameters saved in the algorithm
* class
*/
void CentroidPeaks::integrateEvent() {
/// Peak workspace to centroid
Mantid::DataObjects::PeaksWorkspace_sptr inPeakWS =
getProperty("InPeaksWorkspace");
/// Output peaks workspace, create if needed
Mantid::DataObjects::PeaksWorkspace_sptr peakWS =
getProperty("OutPeaksWorkspace");
if (peakWS != inPeakWS)
peakWS = inPeakWS->clone();
/// Radius to use around peaks
int PeakRadius = getProperty("PeakRadius");
int MinPeaks = -1;
int MaxPeaks = -1;
size_t Numberwi = inWS->getNumberHistograms();
int NumberPeaks = peakWS->getNumberPeaks();
for (int i = 0; i < NumberPeaks; i++) {
Peak &peak = peakWS->getPeaks()[i];
int pixelID = peak.getDetectorID();
// Find the workspace index for this detector ID
if (wi_to_detid_map.find(pixelID) != wi_to_detid_map.end()) {
size_t wi = wi_to_detid_map[pixelID];
if (MinPeaks == -1 && peak.getRunNumber() == inWS->getRunNumber() &&
wi < Numberwi)
MinPeaks = i;
if (peak.getRunNumber() == inWS->getRunNumber() && wi < Numberwi)
MaxPeaks = i;
}
}
int Edge = getProperty("EdgePixels");
Progress prog(this, MinPeaks, 1.0, MaxPeaks);
PARALLEL_FOR2(inWS, peakWS)
for (int i = MinPeaks; i <= MaxPeaks; i++) {
PARALLEL_START_INTERUPT_REGION
// Get a direct ref to that peak.
IPeak &peak = peakWS->getPeak(i);
int col = peak.getCol();
int row = peak.getRow();
double TOFPeakd = peak.getTOF();
std::string bankName = peak.getBankName();
double intensity = 0.0;
double tofcentroid = 0.0;
if (edgePixel(bankName, col, row, Edge))
continue;
Mantid::detid2index_map::iterator it;
it = wi_to_detid_map.find(findPixelID(bankName, col, row));
double tofstart = TOFPeakd * std::pow(1.004, -PeakRadius);
double tofend = TOFPeakd * std::pow(1.004, PeakRadius);
double rowcentroid = 0.0;
int rowstart = std::max(0, row - PeakRadius);
int rowend = row + PeakRadius;
double colcentroid = 0.0;
int colstart = std::max(0, col - PeakRadius);
int colend = col + PeakRadius;
for (int irow = rowstart; irow <= rowend; ++irow) {
for (int icol = colstart; icol <= colend; ++icol) {
Mantid::detid2index_map::iterator it;
if (edgePixel(bankName, icol, irow, Edge))
continue;
it = wi_to_detid_map.find(findPixelID(bankName, icol, irow));
size_t workspaceIndex = (it->second);
EventList el = eventW->getEventList(workspaceIndex);
el.switchTo(WEIGHTED_NOTIME);
std::vector<WeightedEventNoTime> events = el.getWeightedEventsNoTime();
std::vector<WeightedEventNoTime>::iterator itev;
auto itev_end = events.end();
// Check for events in tof range
for (itev = events.begin(); itev != itev_end; ++itev) {
double tof = itev->tof();
if (tof > tofstart && tof < tofend) {
double weight = itev->weight();
intensity += weight;
rowcentroid += irow * weight;
colcentroid += icol * weight;
tofcentroid += tof * weight;
}
}
}
}
// Set pixelID to change row and col
row = int(rowcentroid / intensity);
row = std::max(0, row);
col = int(colcentroid / intensity);
col = std::max(0, col);
if (!edgePixel(bankName, col, row, Edge)) {
peak.setDetectorID(findPixelID(bankName, col, row));
// Set wavelength to change tof for peak object
double tof = tofcentroid / intensity;
Mantid::Kernel::Units::Wavelength wl;
std::vector<double> timeflight;
timeflight.push_back(tof);
double scattering = peak.getScattering();
double L1 = peak.getL1();
double L2 = peak.getL2();
wl.fromTOF(timeflight, timeflight, L1, L2, scattering, 0, 0, 0);
const double lambda = timeflight[0];
timeflight.clear();
peak.setWavelength(lambda);
peak.setBinCount(intensity);
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
for (int i = int(peakWS->getNumberPeaks()) - 1; i >= 0; --i) {
// Get a direct ref to that peak.
IPeak &peak = peakWS->getPeak(i);
int col = peak.getCol();
int row = peak.getRow();
std::string bankName = peak.getBankName();
if (edgePixel(bankName, col, row, Edge)) {
peakWS->removePeak(i);
}
}
// Save the output
setProperty("OutPeaksWorkspace", peakWS);
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CentroidPeaks::exec() {
inWS = getProperty("InputWorkspace");
// For quickly looking up workspace index from det id
wi_to_detid_map = inWS->getDetectorIDToWorkspaceIndexMap();
eventW = boost::dynamic_pointer_cast<const EventWorkspace>(inWS);
if (eventW) {
eventW->sortAll(TOF_SORT, nullptr);
this->integrateEvent();
} else {
this->integrate();
}
}
int CentroidPeaks::findPixelID(std::string bankName, int col, int row) {
Geometry::Instrument_const_sptr Iptr = inWS->getInstrument();
boost::shared_ptr<const IComponent> parent =
Iptr->getComponentByName(bankName);
if (parent->type().compare("RectangularDetector") == 0) {
boost::shared_ptr<const RectangularDetector> RDet =
boost::dynamic_pointer_cast<const RectangularDetector>(parent);
boost::shared_ptr<Detector> pixel = RDet->getAtXY(col, row);
return pixel->getID();
} else {
std::string bankName0 = bankName;
// Only works for WISH
bankName0.erase(0, 4);
std::ostringstream pixelString;
pixelString << Iptr->getName() << "/" << bankName0 << "/" << bankName
<< "/tube" << std::setw(3) << std::setfill('0') << col
<< "/pixel" << std::setw(4) << std::setfill('0') << row;
boost::shared_ptr<const Geometry::IComponent> component =
Iptr->getComponentByName(pixelString.str());
boost::shared_ptr<const Detector> pixel =
boost::dynamic_pointer_cast<const Detector>(component);
return pixel->getID();
}
}
bool CentroidPeaks::edgePixel(std::string bankName, int col, int row,
int Edge) {
if (bankName.compare("None") == 0)
return false;
Geometry::Instrument_const_sptr Iptr = inWS->getInstrument();
boost::shared_ptr<const IComponent> parent =
Iptr->getComponentByName(bankName);
if (parent->type().compare("RectangularDetector") == 0) {
boost::shared_ptr<const RectangularDetector> RDet =
boost::dynamic_pointer_cast<const RectangularDetector>(parent);
return col < Edge || col >= (RDet->xpixels() - Edge) || row < Edge ||
row >= (RDet->ypixels() - Edge);
} else {
std::vector<Geometry::IComponent_const_sptr> children;
boost::shared_ptr<const Geometry::ICompAssembly> asmb =
boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(parent);
asmb->getChildren(children, false);
boost::shared_ptr<const Geometry::ICompAssembly> asmb2 =
boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(children[0]);
std::vector<Geometry::IComponent_const_sptr> grandchildren;
asmb2->getChildren(grandchildren, false);
int NROWS = static_cast<int>(grandchildren.size());
int NCOLS = static_cast<int>(children.size());
// Wish pixels and tubes start at 1 not 0
return col - 1 < Edge || col - 1 >= (NCOLS - Edge) || row - 1 < Edge ||
row - 1 >= (NROWS - Edge);
}
return false;
}
} // namespace Mantid
} // namespace Crystal