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
16
17using namespace Kernel;
18using namespace API;
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(SANSBeamFluxCorrection)
22
24 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
25 "Workspace to be corrected");
26 declareProperty(std::make_unique<WorkspaceProperty<>>("InputMonitorWorkspace", "", Direction::Input),
27 "Workspace containing the monitor counts for the sample data");
28
29 std::vector<std::string> exts{"_event.nxs", ".nxs", ".nxs.h5"};
30 declareProperty(std::make_unique<API::FileProperty>("ReferenceFluxFilename", "", API::FileProperty::Load, exts),
31 "File containing the reference flux spectrum.");
32
33 declareProperty("ReductionProperties", "__sans_reduction_properties", Direction::Input);
34 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
35 "Corrected workspace.");
36 declareProperty("OutputMessage", "", Direction::Output);
37}
38
40 Progress progress(this, 0.0, 1.0, 10);
41 progress.report("Setting up beam flux correction");
42
43 // Reduction property manager
45
46 // If the beam flux correction algorithm isn't in the reduction properties,
47 // add it
48 if (!m_reductionManager->existsProperty("BeamFluxAlgorithm")) {
49 auto algProp = std::make_unique<AlgorithmProperty>("BeamFluxAlgorithm");
50 algProp->setValue(toString());
51 m_reductionManager->declareProperty(std::move(algProp));
52 }
53
54 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
55 MatrixWorkspace_sptr monitorWS = getProperty("InputMonitorWorkspace");
56
57 // Load reference
58 progress.report("Loading reference data");
60
61 // Rebin the reference and monitor data to the sample data workspace
62 progress.report("Rebinning reference data");
63 auto convAlg = createChildAlgorithm("ConvertToHistogram");
64 convAlg->setProperty("InputWorkspace", fluxRefWS);
65 convAlg->executeAsChildAlg();
66 fluxRefWS = convAlg->getProperty("OutputWorkspace");
67
68 auto rebinAlg = createChildAlgorithm("RebinToWorkspace");
69 rebinAlg->setProperty("WorkspaceToRebin", fluxRefWS);
70 rebinAlg->setProperty("WorkspaceToMatch", inputWS);
71 rebinAlg->executeAsChildAlg();
72 MatrixWorkspace_sptr scaledfluxRefWS = rebinAlg->getProperty("OutputWorkspace");
73
74 progress.report("Rebinning monitor data");
75 rebinAlg = createChildAlgorithm("RebinToWorkspace");
76 rebinAlg->setProperty("WorkspaceToRebin", monitorWS);
77 rebinAlg->setProperty("WorkspaceToMatch", inputWS);
78 rebinAlg->executeAsChildAlg();
79 monitorWS = rebinAlg->getProperty("OutputWorkspace");
80
81 progress.report("Correcting input data");
82 // I = I_0 / Phi_sample
83 // Phi_sample = M_sample * [Phi_ref/M_ref]
84 // where [Phi_ref/M_ref] is the fluxRefWS workspace
85 auto divideAlg = createChildAlgorithm("Divide");
86 divideAlg->setProperty("LHSWorkspace", inputWS);
87 divideAlg->setProperty("RHSWorkspace", monitorWS);
88 divideAlg->executeAsChildAlg();
89 MatrixWorkspace_sptr outputWS = divideAlg->getProperty("OutputWorkspace");
90
91 divideAlg = createChildAlgorithm("Divide");
92 divideAlg->setProperty("LHSWorkspace", outputWS);
93 divideAlg->setProperty("RHSWorkspace", scaledfluxRefWS);
94 divideAlg->executeAsChildAlg();
95 outputWS = divideAlg->getProperty("OutputWorkspace");
96 setProperty("OutputWorkspace", outputWS);
97 setProperty("OutputMessage", "Flux correction applied\n" + m_output_message);
98}
99
106 const std::string referenceFluxFile = getPropertyValue("ReferenceFluxFilename");
107 Poco::Path path(referenceFluxFile);
108 const std::string entryName = "SANSBeamFluxCorrection_" + path.getBaseName();
109 std::string fluxRefWSName = "__beam_flux_reference_" + path.getBaseName();
110
111 // Load reference flux as needed
112 MatrixWorkspace_sptr fluxRefWS;
113 if (m_reductionManager->existsProperty(entryName)) {
114 fluxRefWS = m_reductionManager->getProperty(entryName);
115 m_output_message += " | Using flux reference " + referenceFluxFile + "\n";
116 } else {
117 auto loadAlg = createChildAlgorithm("Load");
118 loadAlg->setProperty("Filename", referenceFluxFile);
119 loadAlg->executeAsChildAlg();
120 Workspace_sptr tmpWS = loadAlg->getProperty("OutputWorkspace");
121 fluxRefWS = std::dynamic_pointer_cast<MatrixWorkspace>(tmpWS);
122 m_output_message += " | Loaded flux reference " + referenceFluxFile + "\n";
123
124 // Keep the reference data for later use
125 AnalysisDataService::Instance().addOrReplace(fluxRefWSName, fluxRefWS);
126 m_reductionManager->declareProperty(
127 std::make_unique<WorkspaceProperty<>>(entryName, fluxRefWSName, Direction::InOut));
128 m_reductionManager->setPropertyValue(entryName, fluxRefWSName);
129 m_reductionManager->setProperty(entryName, fluxRefWS);
130 }
131
132 return fluxRefWS;
133}
134
135} // 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