-
Notifications
You must be signed in to change notification settings - Fork 122
/
GroupDetectors.cpp
115 lines (94 loc) · 4.05 KB
/
GroupDetectors.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/GroupDetectors.h"
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidHistogramData/HistogramMath.h"
#include "MantidKernel/ArrayProperty.h"
#include <numeric>
#include <set>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(GroupDetectors)
using namespace Kernel;
using namespace API;
void GroupDetectors::init() {
declareProperty(
std::make_unique<WorkspaceProperty<>>(
"Workspace", "", Direction::InOut,
std::make_shared<CommonBinsValidator>()),
"The name of the workspace2D on which to perform the algorithm");
declareProperty(
std::make_unique<ArrayProperty<specnum_t>>("SpectraList"),
"An array containing a list of the indexes of the spectra to combine\n"
"(DetectorList and WorkspaceIndexList are ignored if this is set)");
declareProperty(
std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
"An array of detector ID's (WorkspaceIndexList is ignored if this is\n"
"set)");
declareProperty(std::make_unique<ArrayProperty<size_t>>("WorkspaceIndexList"),
"An array of workspace indices to combine");
declareProperty("ResultIndex", -1,
"The workspace index of the summed spectrum (or -1 on error)",
Direction::Output);
}
void GroupDetectors::exec() {
// Get the input workspace
const MatrixWorkspace_sptr WS = getProperty("Workspace");
std::vector<size_t> indexList = getProperty("WorkspaceIndexList");
std::vector<specnum_t> spectraList = getProperty("SpectraList");
const std::vector<detid_t> detectorList = getProperty("DetectorList");
// Could create a Validator to replace the below
if (indexList.empty() && spectraList.empty() && detectorList.empty()) {
g_log.information(name() + ": WorkspaceIndexList, SpectraList, and "
"DetectorList properties are all empty, no "
"grouping done");
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()) {
indexList = WS->getIndicesFromSpectra(spectraList);
} // End dealing with spectraList
else if (!detectorList.empty()) {
// Dealing with DetectorList
// convert from detectors to workspace indices
indexList = WS->getIndicesFromDetectorIDs(detectorList);
}
if (indexList.empty()) {
g_log.warning("Nothing to group");
return;
}
const auto firstIndex = static_cast<specnum_t>(indexList[0]);
auto &firstSpectrum = WS->getSpectrum(firstIndex);
setProperty("ResultIndex", firstIndex);
// loop over the spectra to group
Progress progress(this, 0.0, 1.0, static_cast<int>(indexList.size() - 1));
auto outputHisto = firstSpectrum.histogram();
for (size_t i = 0; i < indexList.size() - 1; ++i) {
// The current spectrum
const size_t currentIndex = indexList[i + 1];
auto &spec = WS->getSpectrum(currentIndex);
// Add the current detector to belong to the first spectrum
firstSpectrum.addDetectorIDs(spec.getDetectorIDs());
// Add up all the Y spectra and store the result in the first one
outputHisto += spec.histogram();
// Now zero the now redundant spectrum and set its spectraNo to indicate
// this (using -1)
spec.clearData();
spec.setSpectrumNo(-1);
spec.clearDetectorIDs();
progress.report();
}
firstSpectrum.setHistogram(outputHisto);
}
} // namespace DataHandling
} // namespace Mantid