Mantid
Loading...
Searching...
No Matches
CreateEPP.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
11#include "MantidAPI/Run.h"
18
19namespace Mantid::Algorithms {
20
23
24namespace {
27namespace PropertyNames {
28const static std::string INPUT_WORKSPACE("InputWorkspace");
29const static std::string OUTPUT_WORKSPACE("OutputWorkspace");
30const static std::string SIGMA("Sigma");
31} // namespace PropertyNames
32
35namespace ColumnNames {
36const static std::string WS_INDEX("WorkspaceIndex");
37const static std::string PEAK_CENTRE("PeakCentre");
38const static std::string PEAK_CENTRE_ERR("PeakCentreError");
39const static std::string SIGMA("Sigma");
40const static std::string SIGMA_ERR("SigmaError");
41const static std::string HEIGHT("Height");
42const static std::string HEIGHT_ERR("HeightError");
43const static std::string CHI_SQUARED("chiSq");
44const static std::string STATUS("FitStatus");
45} // namespace ColumnNames
46
51void addEPPColumns(const API::ITableWorkspace_sptr &ws) {
52 ws->addColumn("int", ColumnNames::WS_INDEX);
53 ws->addColumn("double", ColumnNames::PEAK_CENTRE);
54 ws->addColumn("double", ColumnNames::PEAK_CENTRE_ERR);
55 ws->addColumn("double", ColumnNames::SIGMA);
56 ws->addColumn("double", ColumnNames::SIGMA_ERR);
57 ws->addColumn("double", ColumnNames::HEIGHT);
58 ws->addColumn("double", ColumnNames::HEIGHT_ERR);
59 ws->addColumn("double", ColumnNames::CHI_SQUARED);
60 ws->addColumn("str", ColumnNames::STATUS);
61}
62
63} // anonymous namespace
64
65// Register the algorithm into the AlgorithmFactory
66DECLARE_ALGORITHM(CreateEPP)
67
68//----------------------------------------------------------------------------------------------
69
70
71const std::string CreateEPP::name() const { return "CreateEPP"; }
72
74int CreateEPP::version() const { return 1; }
75
77const std::string CreateEPP::category() const { return "Utility"; }
78
80const std::string CreateEPP::summary() const {
81 return "Creates a nominal EPP table compatible with what is returned by the "
82 "FindEPP algorithm.";
83}
84
85//----------------------------------------------------------------------------------------------
89 auto inputWSValidator = std::make_shared<Kernel::CompositeValidator>();
90 inputWSValidator->add(std::make_shared<API::InstrumentValidator>());
91 inputWSValidator->add(std::make_shared<API::WorkspaceUnitValidator>("TOF"));
92 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input,
93 inputWSValidator),
94 "An input workspace.");
95 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Direction::Output),
96 "The calculated output EPP table.");
97 auto mustBePositive = std::make_shared<Kernel::BoundedValidator<double>>();
98 mustBePositive->setLower(0);
99 declareProperty(PropertyNames::SIGMA, 0.0, mustBePositive, "The value to fill the Sigma column with.");
100}
101
102//----------------------------------------------------------------------------------------------
107 const auto &spectrumInfo = inputWS->spectrumInfo();
108 API::ITableWorkspace_sptr outputWS = std::make_shared<DataObjects::TableWorkspace>();
109 addEPPColumns(outputWS);
110 const double sigma = getProperty(PropertyNames::SIGMA);
111 const size_t spectraCount = spectrumInfo.size();
112 outputWS->setRowCount(spectraCount);
113 const auto l1 = spectrumInfo.l1();
114 const double EFixed = inputWS->run().getPropertyAsSingleValue("Ei");
115 for (size_t i = 0; i < spectraCount; ++i) {
116 const auto l2 = spectrumInfo.l2(i);
117 const auto elasticTOF =
118 Kernel::UnitConversion::run("Energy", "TOF", EFixed, l1, l2, 0, Kernel::DeltaEMode::Direct, EFixed);
119 outputWS->getRef<int>(ColumnNames::WS_INDEX, i) = static_cast<int>(i);
120 outputWS->getRef<double>(ColumnNames::PEAK_CENTRE, i) = elasticTOF;
121 outputWS->getRef<double>(ColumnNames::PEAK_CENTRE_ERR, i) = 0;
122 outputWS->getRef<double>(ColumnNames::SIGMA, i) = sigma;
123 outputWS->getRef<double>(ColumnNames::SIGMA_ERR, i) = 0;
124 double height = 0;
125 try {
126 const auto elasticIndex = inputWS->yIndexOfX(elasticTOF, i);
127 height = inputWS->y(i)[elasticIndex];
128 } catch (std::out_of_range &) {
129 std::ostringstream sout;
130 sout << "EPP out of TOF range for workspace index " << i << ". Peak height set to zero.";
131 g_log.warning() << sout.str();
132 }
133 outputWS->getRef<double>(ColumnNames::HEIGHT, i) = height;
134 outputWS->getRef<double>(ColumnNames::CHI_SQUARED, i) = 1;
135 outputWS->getRef<std::string>(ColumnNames::STATUS, i) = "success";
136 }
138}
139
140//----------------------------------------------------------------------------------------------
145std::map<std::string, std::string> CreateEPP::validateInputs(void) {
146 std::map<std::string, std::string> issues;
148 if (!inputWS) {
149 issues[PropertyNames::INPUT_WORKSPACE] = "The InputWorkspace must be a MatrixWorkspace.";
150 } else if (!inputWS->run().hasProperty("Ei")) {
151 issues[PropertyNames::INPUT_WORKSPACE] = "Workspace is missing the 'Ei' sample log.";
152 }
153 return issues;
154}
155
156} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double sigma
Definition: GetAllEi.cpp:156
double height
Definition: GetAllEi.cpp:155
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
CreateEPP : Calculates the nominal elastic time-of flights for each spectrum and creates an elastic p...
Definition: CreateEPP.h:19
std::map< std::string, std::string > validateInputs(void) override
Validate the algorithm's properties.
Definition: CreateEPP.cpp:145
int version() const override
Algorithm's version for identification.
Definition: CreateEPP.cpp:74
void exec() override
Execute the algorithm.
Definition: CreateEPP.cpp:105
const std::string category() const override
Algorithm's category for identification.
Definition: CreateEPP.cpp:77
void init() override
Initialize the algorithm's properties.
Definition: CreateEPP.cpp:88
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
Definition: CreateEPP.cpp:80
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
static double run(const std::string &src, const std::string &dest, const double srcValue, const double l1, const double l2, const double theta, const DeltaEMode::Type emode, const double efixed)
Convert a single value between the given units (as strings)
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static const std::string OUTPUT_WORKSPACE
static const std::string INPUT_WORKSPACE
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54