22#include <boost/algorithm/string/join.hpp>
29using namespace Kernel;
31using namespace FlipperConfigurations;
42static const std::string FILE_EXTENSION =
".nxs";
52 std::map<std::string, std::string> &errorList) {
54 errorList[propertyName] =
"All input workspaces must be of type MatrixWorkspace.";
57 if (ws->getNumberHistograms() != 1) {
58 errorList[propertyName] =
"All input workspaces must contain a single histogram.";
61 if (ws->getAxis(0)->unit()->unitID() !=
"Wavelength") {
62 errorList[propertyName] =
"All input workspaces must be in units of Wavelength.";
65 if (!ws->isHistogramData() && ws->isDistribution()) {
66 errorList[propertyName] =
"All input workspaces must be using distributed histogram data.";
78 std::make_shared<Mantid::API::PolSANSWorkspaceValidator>(
true,
false, std::unordered_set<int>{2, 3, 4})),
79 "Input group workspace to use for polarization calculation");
80 const auto &wavelengthValidator = std::make_shared<WorkspaceUnitValidator>(
"Wavelength");
83 "Analyser efficiency as a function of wavelength");
86 "Polarizer efficiency as a function of wavelength");
88 const auto &spinValidator = std::make_shared<SpinStateValidator>(std::unordered_set<int>{2, 3, 4});
90 "Order of individual spin states in the input group workspace, e.g. \"01,11,00,10\"");
93 "File name or path for the output to be saved to.");
102 std::map<std::string, std::string> errorList;
110 auto const &inputWsCount = inputWorkspace->size();
112 if (spinStates.size() != inputWsCount) {
114 "The number of workspaces in the input WorkspaceGroup (" +
std::to_string(inputWsCount) +
115 ") does not match the number of spin states provided (" +
std::to_string(spinStates.size()) +
").";
120 if (!t01WsIndex.has_value() || !t00WsIndex.has_value()) {
122 "The required spin configurations (00, 01) could not be found in the given SpinStates.";
125 std::dynamic_pointer_cast<MatrixWorkspace>(inputWorkspace->getItem(t00WsIndex.value()));
129 "workspace do not match.";
136 if (outputWs.empty() && outputFile.empty()) {
158 auto &&effPolarizer = (t00Ws - t01Ws) / (2 * (2 * effCell - 1) * (t00Ws + t01Ws)) + 0.5;
163 if (!filename.empty()) {
168 if (!outputWsName.empty()) {
177 auto &effPolarizerE = effPolarizerWs->mutableE(0);
178 const auto &t00E = t00Ws->e(0);
179 const auto &t01E = t01Ws->e(0);
180 const auto &effCellE = effCellWs->e(0);
181 const auto &t00Y = t00Ws->y(0);
182 const auto &t01Y = t01Ws->y(0);
183 const auto &effCellY = effCellWs->y(0);
185 for (
size_t i = 0; i < effPolarizerE.size(); ++i) {
186 const auto &twoCellEffMinusOne = 2 * effCellY[i] - 1;
187 const auto &t00PlusT01 = t00Y[i] + t01Y[i];
189 const auto &delta00 = (t01Y[i]) / ((twoCellEffMinusOne)*pow(t00PlusT01, 2));
190 const auto &delta01 = (-1 * t00Y[i]) / ((twoCellEffMinusOne)*pow(t00PlusT01, 2));
191 const auto &deltaEffCell = (t01Y[i] - t00Y[i]) / (pow(twoCellEffMinusOne, 2) * (t00PlusT01));
192 effPolarizerE[i] = sqrt(pow(delta00 * t00E[i], 2) + pow(delta01 * t01E[i], 2) + pow(deltaEffCell * effCellE[i], 2));
197 std::filesystem::path filePath = filePathStr;
199 if (filePath.extension() != FILE_EXTENSION) {
200 filePath.replace_extension(FILE_EXTENSION);
203 saveAlg->initialize();
204 saveAlg->setProperty(
"Filename", filePath.string());
205 saveAlg->setProperty(
"InputWorkspace",
workspace);
#define DECLARE_ALGORITHM(classname)
IPeaksWorkspace_sptr workspace
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
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.
@ OptionalSave
to specify a file to write to but an empty string is
A property class for workspaces.
void calculatePolarizerEfficiency()
std::map< std::string, std::string > validateInputs() override
Check that input params are valid.
void saveToFile(MatrixWorkspace_sptr const &workspace, std::string const &filePathStr)
void init() override
Virtual method - must be overridden by concrete algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
void calculateErrors(const MatrixWorkspace_sptr &t00Ws, const MatrixWorkspace_sptr &t01Ws, const MatrixWorkspace_sptr &effCellWs, const MatrixWorkspace_sptr &effPolarizerWs)
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static const std::string OFF_ON
static const std::string OFF_OFF
MANTID_ALGORITHMS_DLL API::MatrixWorkspace_sptr workspaceForSpinState(const API::WorkspaceGroup_sptr &group, const std::string &spinStateOrder, const std::string &targetSpinState)
Returns the workspace in the group associated with the given targetSpinState according to the order d...
static const std::string OUTPUT_WORKSPACE
static const std::string ANALYSER_EFFICIENCY
static const std::string INPUT_WORKSPACE
constexpr auto SPIN_STATES
static const std::string OUTPUT_FILE_PATH
MANTID_KERNEL_DLL std::vector< std::string > splitSpinStateString(const std::string &spinStates)
MANTID_KERNEL_DLL std::optional< size_t > indexOfWorkspaceForSpinState(const std::vector< std::string > &spinStateOrder, std::string targetSpinState)
std::string to_string(const wide_integer< Bits, Signed > &n)
static bool matchingBins(const std::shared_ptr< const MatrixWorkspace > &ws1, const std::shared_ptr< const MatrixWorkspace > &ws2, const bool firstOnly=false)
Checks whether the bins (X values) of two workspace are the same.
@ Input
An input workspace.
@ Output
An output workspace.