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 {
20namespace Prop {
21std::string const INPUT_WORKSPACE("InputWorkspace");
22std::string const FLOOD_WORKSPACE("FloodWorkspace");
23std::string const OUTPUT_WORKSPACE("OutputWorkspace");
24} // namespace Prop
25
29void correctEvents(MatrixWorkspace *ws) {
30 auto eventWS = dynamic_cast<IEventWorkspace *>(ws);
31 if (eventWS) {
32 auto const nSpec = eventWS->getNumberHistograms();
33 for (size_t i = 0; i < nSpec; ++i) {
34 eventWS->getSpectrum(i).switchTo(EventType::WEIGHTED);
35 }
36 }
37}
38
41MatrixWorkspace_sptr makeEqualSizes(const MatrixWorkspace_sptr &input, const MatrixWorkspace_sptr &flood) {
42 auto newFlood = WorkspaceFactory::Instance().create(flood, input->getNumberHistograms());
43 auto const table = BinaryOperation::buildBinaryOperationTable(input, flood);
44 auto const floodBlocksize = flood->blocksize();
45 const ISpectrum *missingSpectrum = nullptr;
46 for (size_t i = 0; i < table->size(); ++i) {
47 auto const j = (*table)[i];
48 if (j < 0) {
49 if (missingSpectrum) {
50 newFlood->getSpectrum(i).copyDataFrom(*missingSpectrum);
51 } else {
52 newFlood->dataY(i).assign(floodBlocksize, 1.0);
53 newFlood->dataE(i).assign(floodBlocksize, 0.0);
54 missingSpectrum = &newFlood->getSpectrum(i);
55 }
56 } else {
57 newFlood->getSpectrum(i).copyDataFrom(flood->getSpectrum(j));
58 }
59 }
60 return newFlood;
61}
62} // namespace
63
64namespace Mantid::Algorithms {
65
66// Register the algorithm into the AlgorithmFactory
68
69const std::string ApplyFloodWorkspace::name() const { return "ApplyFloodWorkspace"; }
70
71const std::string ApplyFloodWorkspace::summary() const {
72 return "Algorithm to apply a flood correction to a workspace.";
73}
74
75int ApplyFloodWorkspace::version() const { return 1; }
76
77const std::vector<std::string> ApplyFloodWorkspace::seeAlso() const {
78 return {"ReflectometryReductionOneAuto", "CreateFloodWorkspace"};
79}
80
81const std::string ApplyFloodWorkspace::category() const { return "Reflectometry\\ISIS"; }
82
84
85 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(Prop::INPUT_WORKSPACE, "", Direction::Input),
86 "The workspace to correct.");
87
88 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(Prop::FLOOD_WORKSPACE, "", Direction::Input),
89 "The flood workspace.");
90
91 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(Prop::OUTPUT_WORKSPACE, "", Direction::Output),
92 "The corrected workspace.");
93}
94
96 MatrixWorkspace_sptr input = getProperty(Prop::INPUT_WORKSPACE);
97 MatrixWorkspace_sptr flood = getProperty(Prop::FLOOD_WORKSPACE);
98
99 if (input->size() != flood->size()) {
100 flood = makeEqualSizes(input, flood);
101 }
102
103 auto const inputXUnitId = input->getAxis(0)->unit()->unitID();
104 bool const doConvertUnits = flood->getAxis(0)->unit()->unitID() != inputXUnitId;
105 bool const doRebin = flood->blocksize() > 1;
106
107 if (doRebin) {
108 if (doConvertUnits) {
109 auto convert = createChildAlgorithm("ConvertUnits", 0, 1);
110 convert->setProperty("InputWorkspace", flood);
111 convert->setProperty("Target", inputXUnitId);
112 convert->setProperty("OutputWorkspace", "dummy");
113 convert->execute();
114 flood = convert->getProperty("OutputWorkspace");
115 }
116 auto rebin = createChildAlgorithm("RebinToWorkspace", 0, 1);
117 rebin->setProperty("WorkspaceToRebin", flood);
118 rebin->setProperty("WorkspaceToMatch", input);
119 rebin->setProperty("OutputWorkspace", "dummy");
120 rebin->execute();
121 flood = rebin->getProperty("OutputWorkspace");
122 }
123
124 auto divide = createChildAlgorithm("Divide", 0, 1);
125 divide->setProperty("LHSWorkspace", input);
126 divide->setProperty("RHSWorkspace", flood);
127 divide->setProperty("OutputWorkspace", "dummy");
128 divide->execute();
129 MatrixWorkspace_sptr output = divide->getProperty("OutputWorkspace");
130 correctEvents(output.get());
131 setProperty(Prop::OUTPUT_WORKSPACE, output);
132}
133
134} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
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
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:39
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.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54