Mantid
Loading...
Searching...
No Matches
ChopData.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
14#include "MantidHistogramData/Histogram.h"
17
18namespace Mantid::Algorithms {
19using namespace Kernel;
20using namespace API;
21using namespace Geometry;
22using namespace DataObjects;
23using namespace HistogramData;
24
25DECLARE_ALGORITHM(ChopData)
26
27void ChopData::init() {
28 auto wsVal = std::make_shared<CompositeValidator>();
29 wsVal->add<WorkspaceUnitValidator>("TOF");
30 wsVal->add<HistogramValidator>();
31 wsVal->add<SpectraAxisValidator>();
32 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsVal),
33 "Name of the input workspace to be split.");
34 declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>("OutputWorkspace", "", Direction::Output),
35 "Name for the WorkspaceGroup that will be created.");
36
37 declareProperty("Step", 20000.0);
38 declareProperty("NChops", 5);
39 declareProperty("IntegrationRangeLower", EMPTY_DBL());
40 declareProperty("IntegrationRangeUpper", EMPTY_DBL());
41 declareProperty("MonitorWorkspaceIndex", EMPTY_INT());
42}
43
45 // Get the input workspace
46 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
47 const std::string output = getPropertyValue("OutputWorkspace");
48 const double step = getProperty("Step");
49 const int chops = getProperty("NChops");
50 const double rLower = getProperty("IntegrationRangeLower");
51 const double rUpper = getProperty("IntegrationRangeUpper");
52 const int monitorWi = getProperty("MonitorWorkspaceIndex");
53 const auto nHist = static_cast<int>(inputWS->getNumberHistograms());
54 const size_t nBins = inputWS->blocksize();
55 const double maxX = inputWS->readX(0)[nBins];
56 std::map<int, double> intMap;
57 int prelow = -1;
58 std::vector<MatrixWorkspace_sptr> workspaces;
59 std::unique_ptr<Progress> progress;
60
61 if (maxX < step) {
62 throw std::invalid_argument("Step value provided larger than size of workspace.");
63 }
64
65 if (rLower != EMPTY_DBL() && rUpper != EMPTY_DBL() && monitorWi != EMPTY_INT()) {
66
67 progress = std::make_unique<Progress>(this, 0.0, 1.0, chops * 2);
68
69 // Select the spectrum that is to be used to compare the sections of the
70 // workspace
71 // This will generally be the monitor spectrum.
72 MatrixWorkspace_sptr monitorWS = create<MatrixWorkspace>(*inputWS, 1, inputWS->histogram(monitorWi));
73
74 int lowest = 0;
75
76 // Get ranges
77 for (int i = 0; i < chops; i++) {
78 auto integ = createChildAlgorithm("Integration");
79 integ->initialize();
80 integ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", monitorWS);
81 integ->setProperty<double>("RangeLower", i * step + rLower);
82 integ->setProperty<double>("RangeUpper", i * step + rUpper);
83 integ->execute();
84 MatrixWorkspace_sptr integR = integ->getProperty("OutputWorkspace");
85 intMap[i] = integR->y(0)[0];
86
87 if (intMap[i] < intMap[lowest]) {
88 lowest = i;
89 }
90
91 progress->report();
92 }
93
94 auto nlow = intMap.find(lowest - 1);
95 if (nlow != intMap.end() && intMap[lowest] < (0.1 * nlow->second)) {
96 prelow = nlow->first;
97 }
98 } else {
99 progress = std::make_unique<Progress>(this, 0.0, 1.0, chops);
100 }
101
102 int wsCounter{1};
103
104 for (int i = 0; i < chops; i++) {
105 const double stepDiff = (i * step);
106
107 size_t indexLow, indexHigh;
108
109 try {
110 indexLow = inputWS->yIndexOfX(stepDiff);
111 if (indexLow < (nBins + 1)) {
112 indexLow++;
113 }
114 } catch (std::out_of_range &) {
115 indexLow = 0;
116 }
117
118 if (i == prelow) {
119 i++;
120 }
121
122 try {
123 indexHigh = inputWS->yIndexOfX((i + 1) * step);
124 } catch (std::out_of_range &) {
125 indexHigh = nBins;
126 }
127
128 size_t nbins = indexHigh - indexLow;
129
130 MatrixWorkspace_sptr workspace = create<MatrixWorkspace>(*inputWS, BinEdges(nbins + 1));
131
132 // Copy over X, Y and E data
134 for (int j = 0; j < nHist; j++) {
135
136 auto edges = inputWS->binEdges(j);
137
139
140 workspace->mutableX(j).assign(edges.cbegin() + indexLow, edges.cbegin() + indexLow + nbins + 1);
141
142 workspace->mutableX(j) += -stepDiff;
143
144 workspace->mutableY(j).assign(inputWS->y(j).cbegin() + indexLow, inputWS->y(j).cbegin() + indexLow + nbins);
145
146 workspace->mutableE(j).assign(inputWS->e(j).cbegin() + indexLow, inputWS->e(j).cbegin() + indexLow + nbins);
148 }
150
151 // add the workspace to the AnalysisDataService
152 std::stringstream name;
153 name << output << "_" << wsCounter;
154 std::string wsname = name.str();
155
156 declareProperty(std::make_unique<WorkspaceProperty<>>(wsname, wsname, Direction::Output));
157 setProperty(wsname, workspace);
158 ++wsCounter;
159
160 workspaces.emplace_back(workspace);
161
162 progress->report();
163 }
164
165 // Create workspace group that holds output workspaces
166 auto wsgroup = std::make_shared<WorkspaceGroup>();
167
168 for (auto &workspace : workspaces) {
169 wsgroup->addWorkspace(workspace);
170 }
171 // set the output property
172 setProperty("OutputWorkspace", wsgroup);
173}
174} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
A validator which checks that a workspace contains histogram data (the default) or point data as requ...
A validator which checks whether the input workspace has the Spectra number in the axis.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
For use in TOSCA reduction.
Definition: ChopData.h:23
const std::string name() const override
Definition: ChopData.h:25
void exec() override
Executes the algorithm.
Definition: ChopData.cpp:44
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54