Mantid
Loading...
Searching...
No Matches
NormaliseToUnity.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
15
16namespace Mantid::Algorithms {
17
18// Register the class into the algorithm factory
19DECLARE_ALGORITHM(NormaliseToUnity)
20
21using namespace Kernel;
22using namespace API;
23
28 auto wsValidator = std::make_shared<CompositeValidator>();
29 wsValidator->add<HistogramValidator>();
30 wsValidator->add<CommonBinsValidator>();
31
32 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator),
33 "The name of the input workspace.");
34 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
35 "The name with which to store the output workspace in the [[Analysis "
36 "Data Service]]");
37
38 declareProperty("RangeLower", EMPTY_DBL(), "The X (frame) value to integrate from");
39 declareProperty("RangeUpper", EMPTY_DBL(), "The X (frame) value to integrate to");
40 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
41 mustBePositive->setLower(0);
42 declareProperty("StartWorkspaceIndex", 0, mustBePositive,
43 "The lowest workspace index of the specta that will be integrated");
44 // As the property takes ownership of the validator pointer, have to take care
45 // to pass in a unique
46 // pointer to each property.
47 declareProperty("EndWorkspaceIndex", EMPTY_INT(), mustBePositive,
48 "The highest workspace index of the spectra that will be integrated");
49 declareProperty("IncludePartialBins", false,
50 "If true then partial bins from the beginning and end of the "
51 "input range are also included in the integration.");
52 declareProperty("IncludeMonitors", true, "Whether to include monitor spectra in the sum (default: yes)");
53}
54
60 // Try and retrieve the optional properties
61 double m_MinRange = getProperty("RangeLower");
62 double m_MaxRange = getProperty("RangeUpper");
63 int m_MinSpec = getProperty("StartWorkspaceIndex");
64 int m_MaxSpec = getProperty("EndWorkspaceIndex");
65 const bool keepMonitors = getProperty("IncludeMonitors");
66 const bool incPartBins = getProperty("IncludePartialBins");
67
68 Progress progress(this, 0.0, 1.0, 3);
69
70 // Get the input workspace
71 MatrixWorkspace_sptr localworkspace = getProperty("InputWorkspace");
72
73 // Sum up all the wavelength bins
74 auto integrateAlg = createChildAlgorithm("Integration");
75 integrateAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", localworkspace);
76 integrateAlg->setProperty<double>("RangeLower", m_MinRange);
77 integrateAlg->setProperty<double>("RangeUpper", m_MaxRange);
78 integrateAlg->setProperty<int>("StartWorkspaceIndex", m_MinSpec);
79 integrateAlg->setProperty<int>("EndWorkspaceIndex", m_MaxSpec);
80 integrateAlg->setProperty<bool>("IncludePartialBins", incPartBins);
81 integrateAlg->executeAsChildAlg();
82 progress.report("Normalising to unity");
83
84 MatrixWorkspace_sptr integrated = integrateAlg->getProperty("OutputWorkspace");
85
86 // Sum all the spectra of the integrated workspace
87 auto sumAlg = createChildAlgorithm("SumSpectra");
88 sumAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", integrated);
89 sumAlg->setProperty<bool>("IncludeMonitors", keepMonitors);
90 sumAlg->executeAsChildAlg();
91 progress.report("Normalising to unity");
92
93 MatrixWorkspace_sptr summed = sumAlg->getProperty("OutputWorkspace");
94
95 // Divide by the sum
96 MatrixWorkspace_sptr result = localworkspace / summed;
97 progress.report("Normalising to unity");
98
99 // Assign it to the output workspace property
100 setProperty("OutputWorkspace", result);
101}
102
103} // 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
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
A validator which provides a TENTATIVE check that a workspace contains common bins in each spectrum.
A validator which checks that a workspace contains histogram data (the default) or point data as requ...
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void init() override
Initialisation method.
void exec() override
Executes the algorithm.
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
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