-
Notifications
You must be signed in to change notification settings - Fork 122
/
ChopData.cpp
176 lines (141 loc) · 5.71 KB
/
ChopData.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
// 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 +
#include "MantidAlgorithms/ChopData.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/SpectraAxisValidator.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidHistogramData/Histogram.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/MultiThreaded.h"
namespace Mantid {
namespace Algorithms {
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
using namespace HistogramData;
DECLARE_ALGORITHM(ChopData)
void ChopData::init() {
auto wsVal = std::make_shared<CompositeValidator>();
wsVal->add<WorkspaceUnitValidator>("TOF");
wsVal->add<HistogramValidator>();
wsVal->add<SpectraAxisValidator>();
declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsVal),
"Name of the input workspace to be split.");
declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>("OutputWorkspace", "", Direction::Output),
"Name for the WorkspaceGroup that will be created.");
declareProperty("Step", 20000.0);
declareProperty("NChops", 5);
declareProperty("IntegrationRangeLower", EMPTY_DBL());
declareProperty("IntegrationRangeUpper", EMPTY_DBL());
declareProperty("MonitorWorkspaceIndex", EMPTY_INT());
}
void ChopData::exec() {
// Get the input workspace
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
const std::string output = getPropertyValue("OutputWorkspace");
const double step = getProperty("Step");
const int chops = getProperty("NChops");
const double rLower = getProperty("IntegrationRangeLower");
const double rUpper = getProperty("IntegrationRangeUpper");
const int monitorWi = getProperty("MonitorWorkspaceIndex");
const auto nHist = static_cast<int>(inputWS->getNumberHistograms());
const size_t nBins = inputWS->blocksize();
const double maxX = inputWS->readX(0)[nBins];
std::map<int, double> intMap;
int prelow = -1;
std::vector<MatrixWorkspace_sptr> workspaces;
std::unique_ptr<Progress> progress;
if (maxX < step) {
throw std::invalid_argument("Step value provided larger than size of workspace.");
}
if (rLower != EMPTY_DBL() && rUpper != EMPTY_DBL() && monitorWi != EMPTY_INT()) {
progress = std::make_unique<Progress>(this, 0.0, 1.0, chops * 2);
// Select the spectrum that is to be used to compare the sections of the
// workspace
// This will generally be the monitor spectrum.
MatrixWorkspace_sptr monitorWS = create<MatrixWorkspace>(*inputWS, 1, inputWS->histogram(monitorWi));
int lowest = 0;
// Get ranges
for (int i = 0; i < chops; i++) {
auto integ = createChildAlgorithm("Integration");
integ->initialize();
integ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", monitorWS);
integ->setProperty<double>("RangeLower", i * step + rLower);
integ->setProperty<double>("RangeUpper", i * step + rUpper);
integ->execute();
MatrixWorkspace_sptr integR = integ->getProperty("OutputWorkspace");
intMap[i] = integR->y(0)[0];
if (intMap[i] < intMap[lowest]) {
lowest = i;
}
progress->report();
}
auto nlow = intMap.find(lowest - 1);
if (nlow != intMap.end() && intMap[lowest] < (0.1 * nlow->second)) {
prelow = nlow->first;
}
} else {
progress = std::make_unique<Progress>(this, 0.0, 1.0, chops);
}
int wsCounter{1};
for (int i = 0; i < chops; i++) {
const double stepDiff = (i * step);
size_t indexLow, indexHigh;
try {
indexLow = inputWS->yIndexOfX(stepDiff);
if (indexLow < (nBins + 1)) {
indexLow++;
}
} catch (std::out_of_range &) {
indexLow = 0;
}
if (i == prelow) {
i++;
}
try {
indexHigh = inputWS->yIndexOfX((i + 1) * step);
} catch (std::out_of_range &) {
indexHigh = nBins;
}
size_t nbins = indexHigh - indexLow;
MatrixWorkspace_sptr workspace = create<MatrixWorkspace>(*inputWS, BinEdges(nbins + 1));
// Copy over X, Y and E data
PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *workspace))
for (int j = 0; j < nHist; j++) {
auto edges = inputWS->binEdges(j);
PARALLEL_START_INTERUPT_REGION;
workspace->mutableX(j).assign(edges.cbegin() + indexLow, edges.cbegin() + indexLow + nbins + 1);
workspace->mutableX(j) += -stepDiff;
workspace->mutableY(j).assign(inputWS->y(j).cbegin() + indexLow, inputWS->y(j).cbegin() + indexLow + nbins);
workspace->mutableE(j).assign(inputWS->e(j).cbegin() + indexLow, inputWS->e(j).cbegin() + indexLow + nbins);
PARALLEL_END_INTERUPT_REGION;
}
PARALLEL_CHECK_INTERUPT_REGION;
// add the workspace to the AnalysisDataService
std::stringstream name;
name << output << "_" << wsCounter;
std::string wsname = name.str();
declareProperty(std::make_unique<WorkspaceProperty<>>(wsname, wsname, Direction::Output));
setProperty(wsname, workspace);
++wsCounter;
workspaces.emplace_back(workspace);
progress->report();
}
// Create workspace group that holds output workspaces
auto wsgroup = std::make_shared<WorkspaceGroup>();
for (auto &workspace : workspaces) {
wsgroup->addWorkspace(workspace);
}
// set the output property
setProperty("OutputWorkspace", wsgroup);
}
} // namespace Algorithms
} // namespace Mantid