Mantid
Loading...
Searching...
No Matches
CreateFlatEventWorkspace.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 +
8
10
11using namespace Mantid::API;
12using namespace Mantid::DataObjects;
13
14namespace Mantid::Algorithms {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(CreateFlatEventWorkspace)
18
19//----------------------------------------------------------------------------------------------
21const std::string CreateFlatEventWorkspace::name() const { return "CreateFlatEventWorkspace"; }
22
24int CreateFlatEventWorkspace::version() const { return 1; }
25
27const std::string CreateFlatEventWorkspace::category() const { return "CorrectionFunctions\\BackgroundCorrections"; }
28
29//----------------------------------------------------------------------------------------------
30
31//----------------------------------------------------------------------------------------------
36 "InputWorkspace", "", Mantid::Kernel::Direction::Input),
37 "An input event workspace to use as a source for the events.");
38
39 this->declareProperty("RangeStart", EMPTY_DBL(), "Set the lower bound for sampling the background.");
40 this->declareProperty("RangeEnd", EMPTY_DBL(), "Set the upper bound for sampling the background.");
41
42 this->declareProperty(
43 std::make_unique<Mantid::API::WorkspaceProperty<>>("OutputWorkspace", "", Mantid::Kernel::Direction::Output),
44 "Output event workspace containing a flat background.");
45}
46
47//----------------------------------------------------------------------------------------------
51 // Get the workspaces
52 EventWorkspace_sptr inputWS = getProperty("InputWorkspace");
53 MatrixWorkspace_sptr outputWS;
54
55 // Get the background region start/end
56 double start = getProperty("RangeStart");
57 double end = getProperty("RangeEnd");
58
59 double sampleRange = end - start;
60 g_log.debug() << "Total Range = " << sampleRange << '\n';
61
62 // What are the min/max values for the experimental data ?
63 double dataMin, dataMax;
64 inputWS->getEventXMinMax(dataMin, dataMax);
65
66 g_log.debug() << "Data Range (" << dataMin << " < x < " << dataMax << ")\n";
67
68 // How many times do we need to replicate the extracted background region in
69 // order to fill up
70 // the entire tof/x range covered by the data ?
71 auto nRegions = static_cast<int>((dataMax - dataMin) / sampleRange);
72
73 g_log.debug() << "We will need to replicate the selected region " << nRegions << " times.\n";
74
75 // Extract the region we are using for the background
76 auto crop_alg = this->createChildAlgorithm("CropWorkspace");
77 crop_alg->setProperty("InputWorkspace", inputWS);
78 crop_alg->setProperty("XMin", start);
79 crop_alg->setProperty("XMax", end);
80 crop_alg->setPropertyValue("OutputWorkspace", "__extracted_chunk");
81 crop_alg->execute();
82 MatrixWorkspace_sptr chunkws = crop_alg->getProperty("OutputWorkspace");
83
84 // Now lets shift the region to the start of the data.
85 auto shift_alg = this->createChildAlgorithm("ChangeBinOffset");
86 shift_alg->setProperty("InputWorkspace", chunkws);
87 // shift_alg->setPropertyValue("OutputWorkspace", outputWsName);
88 shift_alg->setProperty("Offset", -(start - dataMin));
89 shift_alg->executeAsChildAlg();
90 outputWS = shift_alg->getProperty("OutputWorkspace");
91
92 auto clone = createChildAlgorithm("CloneWorkspace");
93 clone->setProperty("InputWorkspace", outputWS);
94 clone->setPropertyValue("OutputWorkspace", "__background_chunk");
95 clone->executeAsChildAlg();
96 Workspace_sptr tmp = clone->getProperty("OutputWorkspace");
97 MatrixWorkspace_sptr tmpChunkWs = std::dynamic_pointer_cast<MatrixWorkspace>(tmp);
98
99 Progress progress(this, 0.0, 1.0, nRegions);
100
101 for (int i = 0; i < nRegions; ++i) {
102
103 auto shiftchunk = createChildAlgorithm("ChangeBinOffset");
104 shiftchunk->setProperty("InputWorkspace", tmpChunkWs);
105 shiftchunk->setProperty("OutputWorkspace", tmpChunkWs);
106 shiftchunk->setProperty("Offset", sampleRange);
107 shiftchunk->executeAsChildAlg();
108 tmpChunkWs = shiftchunk->getProperty("OutputWorkspace");
109
110 // Now add this chunk onto the output
111 auto plus_alg = createChildAlgorithm("Plus");
112 plus_alg->setProperty("LHSWorkspace", outputWS);
113 plus_alg->setProperty("RHSWorkspace", tmpChunkWs);
114 plus_alg->setProperty("OutputWorkspace", outputWS);
115 plus_alg->executeAsChildAlg();
116 outputWS = plus_alg->getProperty("OutputWorkspace");
117 tmpChunkWs = plus_alg->getProperty("RHSWorkspace");
118
119 progress.report();
120 }
121
122 // Crop the output workspace to be the same range as the input data
123 auto finalcrop_alg = createChildAlgorithm("CropWorkspace");
124 finalcrop_alg->setProperty("InputWorkspace", outputWS);
125 finalcrop_alg->setProperty("XMin", dataMin);
126 finalcrop_alg->setProperty("XMax", dataMax);
127 finalcrop_alg->execute();
128 outputWS = finalcrop_alg->getProperty("OutputWorkspace");
129
130 EventWorkspace_sptr outputEWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
131 outputEWS->clearMRU();
132
133 // Need to reset the matrixworkspace/histogram representation to be the
134 // whole xrange (rather than just the extracted chunk).
135 outputEWS->getEventXMinMax(dataMin, dataMax);
136 outputEWS->setAllX(HistogramData::BinEdges{dataMin, dataMax});
137
138 this->setProperty("OutputWorkspace", outputWS);
139}
140
141} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
gsl_vector * tmp
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
CreateFlatEventWorkspace : Creates a flat event workspace that can be used for background removal.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54