Mantid
Loading...
Searching...
No Matches
SANSBeamFluxCorrection.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 +
13#include "Poco/Path.h"
14
15#include <filesystem>
16
18
19using namespace Kernel;
20using namespace API;
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(SANSBeamFluxCorrection)
24
26 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
27 "Workspace to be corrected");
28 declareProperty(std::make_unique<WorkspaceProperty<>>("InputMonitorWorkspace", "", Direction::Input),
29 "Workspace containing the monitor counts for the sample data");
30
31 std::vector<std::string> exts{"_event.nxs", ".nxs", ".nxs.h5"};
32 declareProperty(std::make_unique<API::FileProperty>("ReferenceFluxFilename", "", API::FileProperty::Load, exts),
33 "File containing the reference flux spectrum.");
34
35 declareProperty("ReductionProperties", "__sans_reduction_properties", Direction::Input);
36 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
37 "Corrected workspace.");
38 declareProperty("OutputMessage", "", Direction::Output);
39}
40
42 Progress progress(this, 0.0, 1.0, 10);
43 progress.report("Setting up beam flux correction");
44
45 // Reduction property manager
47
48 // If the beam flux correction algorithm isn't in the reduction properties,
49 // add it
50 if (!m_reductionManager->existsProperty("BeamFluxAlgorithm")) {
51 auto algProp = std::make_unique<AlgorithmProperty>("BeamFluxAlgorithm");
52 algProp->setValue(toString());
53 m_reductionManager->declareProperty(std::move(algProp));
54 }
55
56 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
57 MatrixWorkspace_sptr monitorWS = getProperty("InputMonitorWorkspace");
58
59 // Load reference
60 progress.report("Loading reference data");
62
63 // Rebin the reference and monitor data to the sample data workspace
64 progress.report("Rebinning reference data");
65 auto convAlg = createChildAlgorithm("ConvertToHistogram");
66 convAlg->setProperty("InputWorkspace", fluxRefWS);
67 convAlg->executeAsChildAlg();
68 fluxRefWS = convAlg->getProperty("OutputWorkspace");
69
70 auto rebinAlg = createChildAlgorithm("RebinToWorkspace");
71 rebinAlg->setProperty("WorkspaceToRebin", fluxRefWS);
72 rebinAlg->setProperty("WorkspaceToMatch", inputWS);
73 rebinAlg->executeAsChildAlg();
74 MatrixWorkspace_sptr scaledfluxRefWS = rebinAlg->getProperty("OutputWorkspace");
75
76 progress.report("Rebinning monitor data");
77 rebinAlg = createChildAlgorithm("RebinToWorkspace");
78 rebinAlg->setProperty("WorkspaceToRebin", monitorWS);
79 rebinAlg->setProperty("WorkspaceToMatch", inputWS);
80 rebinAlg->executeAsChildAlg();
81 monitorWS = rebinAlg->getProperty("OutputWorkspace");
82
83 progress.report("Correcting input data");
84 // I = I_0 / Phi_sample
85 // Phi_sample = M_sample * [Phi_ref/M_ref]
86 // where [Phi_ref/M_ref] is the fluxRefWS workspace
87 auto divideAlg = createChildAlgorithm("Divide");
88 divideAlg->setProperty("LHSWorkspace", inputWS);
89 divideAlg->setProperty("RHSWorkspace", monitorWS);
90 divideAlg->executeAsChildAlg();
91 MatrixWorkspace_sptr outputWS = divideAlg->getProperty("OutputWorkspace");
92
93 divideAlg = createChildAlgorithm("Divide");
94 divideAlg->setProperty("LHSWorkspace", outputWS);
95 divideAlg->setProperty("RHSWorkspace", scaledfluxRefWS);
96 divideAlg->executeAsChildAlg();
97 outputWS = divideAlg->getProperty("OutputWorkspace");
98 setProperty("OutputWorkspace", outputWS);
99 setProperty("OutputMessage", "Flux correction applied\n" + m_output_message);
100}
101
108 const std::string referenceFluxFile = getPropertyValue("ReferenceFluxFilename");
109 std::filesystem::path path(referenceFluxFile);
110 const std::string entryName = "SANSBeamFluxCorrection_" + path.stem().string();
111 std::string fluxRefWSName = "__beam_flux_reference_" + path.stem().string();
112
113 // Load reference flux as needed
114 MatrixWorkspace_sptr fluxRefWS;
115 if (m_reductionManager->existsProperty(entryName)) {
116 fluxRefWS = m_reductionManager->getProperty(entryName);
117 m_output_message += " | Using flux reference " + referenceFluxFile + "\n";
118 } else {
119 auto loadAlg = createChildAlgorithm("Load");
120 loadAlg->setProperty("Filename", referenceFluxFile);
121 loadAlg->executeAsChildAlg();
122 Workspace_sptr tmpWS = loadAlg->getProperty("OutputWorkspace");
123 fluxRefWS = std::dynamic_pointer_cast<MatrixWorkspace>(tmpWS);
124 m_output_message += " | Loaded flux reference " + referenceFluxFile + "\n";
125
126 // Keep the reference data for later use
127 AnalysisDataService::Instance().addOrReplace(fluxRefWSName, fluxRefWS);
128 m_reductionManager->declareProperty(
129 std::make_unique<WorkspaceProperty<>>(entryName, fluxRefWSName, Direction::InOut));
130 m_reductionManager->setPropertyValue(entryName, fluxRefWSName);
131 m_reductionManager->setProperty(entryName, fluxRefWS);
132 }
133
134 return fluxRefWS;
135}
136
137} // namespace Mantid::WorkflowAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
@ Load
allowed here which will be passed to the algorithm
std::shared_ptr< Kernel::PropertyManager > getProcessProperties(const std::string &propertyManager=std::string()) const
Get the property manager object of a given name from the property manager data service,...
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) override
Create a Child Algorithm.
Kernel::IPropertyManager::TypedValue getProperty(const std::string &name) const override
Get the property held by this object.
std::string getPropertyValue(const std::string &name) const override
Get the property held by this object.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Performs beam flux correction on TOF SANS data.
std::shared_ptr< Kernel::PropertyManager > m_reductionManager
API::MatrixWorkspace_sptr loadReference()
It's assumed that both the flux reference files are simple Nexus files since they have to produced by...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string toString(const T &value)
Convert values to strings.
@ InOut
Both an input & output workspace.
Definition Property.h:55
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54