-
Notifications
You must be signed in to change notification settings - Fork 121
/
NormaliseToUnity.cpp
103 lines (87 loc) · 4.34 KB
/
NormaliseToUnity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/NormaliseToUnity.h"
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
namespace Mantid::Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(NormaliseToUnity)
using namespace Kernel;
using namespace API;
/** Initialisation method.
*
*/
void NormaliseToUnity::init() {
auto wsValidator = std::make_shared<CompositeValidator>();
wsValidator->add<HistogramValidator>();
wsValidator->add<CommonBinsValidator>();
declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator),
"The name of the input workspace.");
declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
"The name with which to store the output workspace in the [[Analysis "
"Data Service]]");
declareProperty("RangeLower", EMPTY_DBL(), "The X (frame) value to integrate from");
declareProperty("RangeUpper", EMPTY_DBL(), "The X (frame) value to integrate to");
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(0);
declareProperty("StartWorkspaceIndex", 0, mustBePositive,
"The lowest workspace index of the specta that will be integrated");
// As the property takes ownership of the validator pointer, have to take care
// to pass in a unique
// pointer to each property.
declareProperty("EndWorkspaceIndex", EMPTY_INT(), mustBePositive,
"The highest workspace index of the spectra that will be integrated");
declareProperty("IncludePartialBins", false,
"If true then partial bins from the beginning and end of the "
"input range are also included in the integration.");
declareProperty("IncludeMonitors", true, "Whether to include monitor spectra in the sum (default: yes)");
}
/** Executes the algorithm
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void NormaliseToUnity::exec() {
// Try and retrieve the optional properties
double m_MinRange = getProperty("RangeLower");
double m_MaxRange = getProperty("RangeUpper");
int m_MinSpec = getProperty("StartWorkspaceIndex");
int m_MaxSpec = getProperty("EndWorkspaceIndex");
const bool keepMonitors = getProperty("IncludeMonitors");
const bool incPartBins = getProperty("IncludePartialBins");
Progress progress(this, 0.0, 1.0, 3);
// Get the input workspace
MatrixWorkspace_sptr localworkspace = getProperty("InputWorkspace");
// Sum up all the wavelength bins
auto integrateAlg = createChildAlgorithm("Integration");
integrateAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", localworkspace);
integrateAlg->setProperty<double>("RangeLower", m_MinRange);
integrateAlg->setProperty<double>("RangeUpper", m_MaxRange);
integrateAlg->setProperty<int>("StartWorkspaceIndex", m_MinSpec);
integrateAlg->setProperty<int>("EndWorkspaceIndex", m_MaxSpec);
integrateAlg->setProperty<bool>("IncludePartialBins", incPartBins);
integrateAlg->executeAsChildAlg();
progress.report("Normalising to unity");
MatrixWorkspace_sptr integrated = integrateAlg->getProperty("OutputWorkspace");
// Sum all the spectra of the integrated workspace
auto sumAlg = createChildAlgorithm("SumSpectra");
sumAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", integrated);
sumAlg->setProperty<bool>("IncludeMonitors", keepMonitors);
sumAlg->executeAsChildAlg();
progress.report("Normalising to unity");
MatrixWorkspace_sptr summed = sumAlg->getProperty("OutputWorkspace");
// Divide by the sum
MatrixWorkspace_sptr result = localworkspace / summed;
progress.report("Normalising to unity");
// Assign it to the output workspace property
setProperty("OutputWorkspace", result);
}
} // namespace Mantid::Algorithms