Mantid
Loading...
Searching...
No Matches
ReflectometryHelper.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 +
7#include <utility>
8
10
18#include "MantidHistogramData/BinEdges.h"
19#include "MantidHistogramData/Histogram.h"
20#include "MantidHistogramData/LinearGenerator.h"
22
23namespace Mantid {
24
25using namespace API;
26using namespace DataHandling;
27using namespace DataObjects;
28using namespace HistogramData;
29using namespace Kernel;
30
31namespace FrameworkTestHelpers {
32
33MatrixWorkspace_sptr createHistoWS(size_t nBins, double startX, double endX, std::vector<double> const &values,
34 std::string const &unitX = "TOF") {
35 double const dX = (endX - startX) / double(nBins);
36 BinEdges xVals(nBins + 1, LinearGenerator(startX, dX));
37 size_t nSpec = values.size();
38 std::vector<double> yVals(nSpec * nBins);
39 for (size_t i = 0; i < nSpec; ++i) {
40 std::fill(yVals.begin() + i * nBins, yVals.begin() + (i + 1) * nBins, values[i]);
41 }
42
43 auto creator = AlgorithmManager::Instance().createUnmanaged("CreateWorkspace");
44 creator->setChild(true);
45 creator->initialize();
46 creator->setProperty("DataX", xVals.rawData());
47 creator->setProperty("DataY", yVals);
48 creator->setProperty("NSpec", int(nSpec));
49 creator->setProperty("UnitX", unitX);
50 creator->setPropertyValue("OutputWorkspace", "dummy");
51 creator->execute();
52 MatrixWorkspace_sptr workspace = creator->getProperty("OutputWorkspace");
53 return workspace;
54}
55
56MatrixWorkspace_sptr createREFL_WS(size_t nBins, double startX, double endX, std::vector<double> const &values,
57 std::string const &paramsType, std::string const &instrumentSuffix) {
58 MatrixWorkspace_sptr workspace = createHistoWS(nBins, startX, endX, values);
59
60 auto filePrefix = std::string("unit_testing/REFL") + instrumentSuffix;
61 auto instrumentLoader = AlgorithmManager::Instance().createUnmanaged("LoadInstrument");
62 instrumentLoader->initialize();
63 instrumentLoader->setPropertyValue("Filename", filePrefix + std::string("_Definition.xml"));
64 instrumentLoader->setProperty("Workspace", workspace);
65 instrumentLoader->setProperty("RewriteSpectraMap", OptionalBool(true));
66 instrumentLoader->execute();
67
68 if (!paramsType.empty()) {
69 auto paramLoader = AlgorithmManager::Instance().createUnmanaged("LoadParameterFile");
70 paramLoader->initialize();
71 paramLoader->setPropertyValue("Filename", filePrefix + std::string("_Parameters_") + paramsType + ".xml");
72 paramLoader->setProperty("Workspace", workspace);
73 paramLoader->execute();
74 }
75
76 return workspace;
77}
78
79void prepareInputGroup(std::string const &name, std::string const &paramsType, size_t size, double const startX,
80 double const endX, size_t const nBins) {
81 double monitorValue = 99.0;
82 double detectorValue = 0.9;
83 std::string names;
84
86
87 for (size_t i = 0; i < size; ++i) {
88 std::vector<double> values(257, detectorValue);
89 values[0] = monitorValue;
90 MatrixWorkspace_sptr ws = createREFL_WS(nBins, startX, endX, values, paramsType);
91 std::string const name1 = name + "_" + std::to_string(i + 1);
92 ADS.addOrReplace(name1, ws);
93 monitorValue -= 1.0;
94 detectorValue -= 0.1;
95 if (i > 0)
96 names.append(",");
97 names.append(name1);
98 }
99
100 auto mkGroup = AlgorithmManager::Instance().createUnmanaged("GroupWorkspaces");
101 mkGroup->initialize();
102 mkGroup->setProperty("InputWorkspaces", names);
103 mkGroup->setProperty("OutputWorkspace", name);
104 mkGroup->execute();
105}
106
107std::vector<MatrixWorkspace_sptr> groupToVector(const WorkspaceGroup_sptr &group) {
108 std::vector<MatrixWorkspace_sptr> out;
109 for (size_t i = 0; i < group->size(); ++i) {
110 out.emplace_back(std::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(i)));
111 }
112 return out;
113}
114
115std::vector<MatrixWorkspace_sptr> retrieveOutWS(std::string const &name) {
116 auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(name);
117 return groupToVector(group);
118}
119
121
122 auto wss = groupToVector(ws);
123 auto Rpp = wss[0];
124 auto Rpa = wss[1];
125 auto Rap = wss[2];
126 auto Raa = wss[3];
127
128 auto nBins = Rpp->blocksize();
129 auto startX = Rpp->x(0).front();
130 auto endX = Rpp->x(0).back();
131
132 auto Pp = createHistoWS(nBins, startX, endX, {0.9});
133 auto Ap = createHistoWS(nBins, startX, endX, {0.8});
134 auto Pa = createHistoWS(nBins, startX, endX, {0.7});
135 auto Aa = createHistoWS(nBins, startX, endX, {0.6});
136
137 auto Ipp = (Rpp * (Pp + 1.0) * (Ap + 1.0) + Raa * (Pp * (-1.0) + 1.0) * (Ap * (-1.0) + 1.0) +
138 Rpa * (Pp + 1.0) * (Ap * (-1.0) + 1.0) + Rap * (Pp * (-1.0) + 1.0) * (Ap + 1.0)) /
139 4.;
140 auto Iaa = (Raa * (Pa + 1.0) * (Aa + 1.0) + Rpp * (Pa * (-1.0) + 1.0) * (Aa * (-1.0) + 1.0) +
141 Rap * (Pa + 1.0) * (Aa * (-1.0) + 1.0) + Rpa * (Pa * (-1.0) + 1.0) * (Aa + 1.0)) /
142 4.;
143 auto Ipa = (Rpa * (Pp + 1.0) * (Aa + 1.0) + Rap * (Pp * (-1.0) + 1.0) * (Aa * (-1.0) + 1.0) +
144 Rpp * (Pp + 1.0) * (Aa * (-1.0) + 1.0) + Raa * (Pp * (-1.0) + 1.0) * (Aa + 1.0)) /
145 4.;
146 auto Iap = (Rap * (Pa + 1.0) * (Ap + 1.0) + Rpa * (Pa * (-1.0) + 1.0) * (Ap * (-1.0) + 1.0) +
147 Raa * (Pa + 1.0) * (Ap * (-1.0) + 1.0) + Rpp * (Pa * (-1.0) + 1.0) * (Ap + 1.0)) /
148 4.;
149
150 auto &ADS = AnalysisDataService::Instance();
151 ADS.addOrReplace(Rpp->getName(), Ipp);
152 ADS.addOrReplace(Rpa->getName(), Ipa);
153 ADS.addOrReplace(Rap->getName(), Iap);
154 ADS.addOrReplace(Raa->getName(), Iaa);
155
156 auto instrument = Rpp->getInstrument();
157 Ipp->setInstrument(instrument);
158 Ipa->setInstrument(instrument);
159 Iap->setInstrument(instrument);
160 Iaa->setInstrument(instrument);
161}
162
163void applyPolarizationEfficiencies(std::string const &name) {
164 auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(name);
166}
167
168MatrixWorkspace_sptr createWorkspaceSingle(const double startX, const int nBins, const double deltaX,
169 const std::vector<double> &yValues) {
171 startX, 0.0, Mantid::Kernel::V3D(0, 0, 0), Mantid::Kernel::V3D(0, 0, 1), 0.5, 1.0, Mantid::Kernel::V3D(0, 0, 0),
172 Mantid::Kernel::V3D(14, 0, 0), Mantid::Kernel::V3D(15, 0, 0), Mantid::Kernel::V3D(20, (20 - 15), 0), 2, nBins,
173 deltaX);
174
175 for (auto i = 0u; i < ws->y(0).size(); ++i) {
176 ws->mutableY(0)[i] = yValues[i];
177 }
178
179 ws->mutableRun().addProperty<std::string>("run_number", "1234");
180
181 return ws;
182}
183
184MatrixWorkspace_sptr createWorkspaceSingle(const double startX, const int nBins, const double deltaX) {
186 startX, 0.0, Mantid::Kernel::V3D(0, 0, 0), Mantid::Kernel::V3D(0, 0, 1), 0.5, 1.0, Mantid::Kernel::V3D(0, 0, 0),
187 Mantid::Kernel::V3D(14, 0, 0), Mantid::Kernel::V3D(15, 0, 0), Mantid::Kernel::V3D(20, (20 - 15), 0), 2, nBins,
188 deltaX);
189
190 ws->mutableRun().addProperty<std::string>("run_number", "1234");
191 return ws;
192}
193} // namespace FrameworkTestHelpers
194} // namespace Mantid
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
Class to hold a set of workspaces.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Class for 3D vectors.
Definition: V3D.h:34
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
void prepareInputGroup(std::string const &name, std::string const &paramsType="", size_t size=4, double const startX=5000.0, double const endX=100000.0, size_t const nBins=10)
MatrixWorkspace_sptr createWorkspaceSingle(const double startX, const int nBins, const double deltaX, const std::vector< double > &yValues)
MatrixWorkspace_sptr createREFL_WS(size_t nBins, double startX, double endX, std::vector< double > const &values, std::string const &paramsType="", std::string const &instrumentSuffix="")
MatrixWorkspace_sptr createHistoWS(size_t nBins, double startX, double endX, std::vector< double > const &values, std::string const &unitX="TOF")
void applyPolarizationEfficiencies(std::string const &name)
std::vector< MatrixWorkspace_sptr > retrieveOutWS(std::string const &name)
std::vector< MatrixWorkspace_sptr > groupToVector(const WorkspaceGroup_sptr &group)
Helper class which provides the Collimation Length for SANS instruments.
Mantid::API::MatrixWorkspace_sptr create2DWorkspaceWithReflectometryInstrumentMultiDetector(const double startX=0.0, const double detSize=0.0, const Mantid::Kernel::V3D &slit1Pos=Mantid::Kernel::V3D(0, 0, 0), const Mantid::Kernel::V3D &slit2Pos=Mantid::Kernel::V3D(0, 0, 1), const double vg1=0.5, const double vg2=1.0, const Mantid::Kernel::V3D &sourcePos=Mantid::Kernel::V3D(0, 0, 0), const Mantid::Kernel::V3D &monitorPos=Mantid::Kernel::V3D(14, 0, 0), const Mantid::Kernel::V3D &samplePos=Mantid::Kernel::V3D(15, 0, 0), const Mantid::Kernel::V3D &detectorCenterPos=Mantid::Kernel::V3D(20,(20 - 15), 0), const int nSpectra=4, const int nBins=20, const double deltaX=5000.0)
Create a 2D workspace with one monitor and three detectors based around a virtual reflectometry instr...
std::string to_string(const wide_integer< Bits, Signed > &n)