Mantid
Loading...
Searching...
No Matches
ApplyFloodWorkspace.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#include "MantidAPI/Axis.h"
13#include "MantidKernel/Unit.h"
14
15using namespace Mantid::Kernel;
16using namespace Mantid::Algorithms;
17using namespace Mantid::API;
18
19namespace {
20auto constexpr MISSING_Y_VALUE = 1.0;
21namespace Prop {
22std::string const INPUT_WORKSPACE("InputWorkspace");
23std::string const FLOOD_WORKSPACE("FloodWorkspace");
24std::string const OUTPUT_WORKSPACE("OutputWorkspace");
25} // namespace Prop
26
32void correctEvents(MatrixWorkspace *ws) {
33 auto eventWS = dynamic_cast<IEventWorkspace *>(ws);
34 if (eventWS) {
35 auto const nSpec = eventWS->getNumberHistograms();
36 for (size_t i = 0; i < nSpec; ++i) {
37 eventWS->getSpectrum(i).switchTo(EventType::WEIGHTED);
38 }
39 }
40}
41
46void resetMissingSpectra(MatrixWorkspace_sptr &flood, const std::vector<int64_t> &indexList) {
47 auto const &floodBlockSize = flood->blocksize();
48
49 for (auto i = 0; auto const &specNum : indexList) {
50 if (specNum < 0) {
51 flood->mutableY(i).assign(floodBlockSize, MISSING_Y_VALUE);
52 }
53 i++;
54 }
55}
56
65MatrixWorkspace_sptr makeEqualSizes(const MatrixWorkspace_sptr &input, const MatrixWorkspace_sptr &flood,
66 const std::vector<int64_t> &indexList) {
67 auto newFlood = WorkspaceFactory::Instance().create(flood, input->getNumberHistograms());
68 auto const floodBlocksize = flood->blocksize();
69 const ISpectrum *missingSpectrum = nullptr;
70 for (size_t i = 0; i < indexList.size(); ++i) {
71 auto const j = (indexList)[i];
72 if (j < 0) {
73 if (missingSpectrum) {
74 newFlood->getSpectrum(i).copyDataFrom(*missingSpectrum);
75 } else {
76 newFlood->mutableY(i).assign(floodBlocksize, MISSING_Y_VALUE);
77 newFlood->mutableE(i).assign(floodBlocksize, 0.0);
78 missingSpectrum = &newFlood->getSpectrum(i);
79 }
80 } else {
81 newFlood->getSpectrum(i).copyDataFrom(flood->getSpectrum(j));
82 }
83 }
84 return newFlood;
85}
86} // namespace
87
88namespace Mantid::Algorithms {
89
90// Register the algorithm into the AlgorithmFactory
92
93const std::string ApplyFloodWorkspace::name() const { return "ApplyFloodWorkspace"; }
94
95const std::string ApplyFloodWorkspace::summary() const {
96 return "Algorithm to apply a flood correction to a workspace.";
97}
98
99int ApplyFloodWorkspace::version() const { return 1; }
100
101const std::vector<std::string> ApplyFloodWorkspace::seeAlso() const {
102 return {"ReflectometryReductionOneAuto", "CreateFloodWorkspace"};
103}
104
105const std::string ApplyFloodWorkspace::category() const { return "Reflectometry\\ISIS"; }
106
108
110 "The workspace to correct.");
111
112 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(Prop::FLOOD_WORKSPACE, "", Direction::Input),
113 "The flood workspace.");
114
116 "The corrected workspace.");
117}
118
121 MatrixWorkspace_sptr flood = getProperty(Prop::FLOOD_WORKSPACE);
122
123 auto indexTable = std::make_shared<std::vector<int64_t>>();
124 if (input->size() != flood->size()) {
125 indexTable = BinaryOperation::buildBinaryOperationTable(input, flood);
126 flood = makeEqualSizes(input, flood, *indexTable);
127 }
128
129 auto const inputXUnitId = input->getAxis(0)->unit()->unitID();
130 bool const doConvertUnits = ((flood->getAxis(0)->unit()->unitID() != inputXUnitId) && (inputXUnitId != "Empty"));
131 bool const doRebin = flood->blocksize() > 1;
132
133 if (doRebin) {
134 if (doConvertUnits) {
135 auto convert = createChildAlgorithm("ConvertUnits", 0, 1);
136 convert->setProperty("InputWorkspace", flood);
137 convert->setProperty("Target", inputXUnitId);
138 convert->setProperty("OutputWorkspace", "dummy");
139 convert->execute();
140 flood = convert->getProperty("OutputWorkspace");
141 }
142 auto rebin = createChildAlgorithm("RebinToWorkspace", 0, 1);
143 rebin->setProperty("WorkspaceToRebin", flood);
144 rebin->setProperty("WorkspaceToMatch", input);
145 rebin->setProperty("OutputWorkspace", "dummy");
146 rebin->execute();
147 flood = rebin->getProperty("OutputWorkspace");
148 resetMissingSpectra(flood, *indexTable);
149 }
150
151 auto divide = createChildAlgorithm("Divide", 0, 1);
152 divide->setProperty("LHSWorkspace", input);
153 divide->setProperty("RHSWorkspace", flood);
154 divide->setProperty("OutputWorkspace", "dummy");
155 divide->execute();
156 MatrixWorkspace_sptr output = divide->getProperty("OutputWorkspace");
157 correctEvents(output.get());
159}
160
161} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
This class provides an interface to an EventWorkspace.
A "spectrum" is an object that holds the data for a particular spectrum, in particular:
Definition ISpectrum.h:38
virtual void copyDataFrom(const ISpectrum &source)=0
Copy data from another ISpectrum with double-dynamic dispatch.
Base MatrixWorkspace Abstract Class.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
A property class for workspaces.
Algorithm to apply a flood correction workspace to a reflectometry data workspace.
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso algorithms related to this algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
const std::string category() const override
function to return a category of the algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
const std::string summary() const override
function returns a summary message that will be displayed in the default GUI, and in the help.
int version() const override
function to return a version of the algorithm, must be overridden in all algorithms
static BinaryOperationTable_sptr buildBinaryOperationTable(const API::MatrixWorkspace_const_sptr &lhs, const API::MatrixWorkspace_const_sptr &rhs)
Build up an BinaryOperationTable for performing a binary operation e.g.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
void MANTID_KERNEL_DLL rebin(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool distribution, bool addition=false)
Rebins data according to a new output X array.
String constants for algorithm's properties.
static const std::string INPUT_WORKSPACE
static const std::string OUTPUT_WORKSPACE
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54