/
BackgroundHelperTest.h
124 lines (86 loc) · 4.11 KB
/
BackgroundHelperTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef BACKGROUDHELPER_TEST_H_
#define BACKGROUDHELPER_TEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/BackgroundHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidAlgorithms/Rebin.h"
#include "MantidAlgorithms/ConvertUnits.h"
#include "MantidAlgorithms/CalculateFlatBackground.h"
using namespace Mantid;
class BackgroundHelperTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static BackgroundHelperTest *createSuite() { return new BackgroundHelperTest(); }
static void destroySuite( BackgroundHelperTest *suite ) { delete suite; }
BackgroundHelperTest()
{
DataObjects::Workspace2D_sptr theWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(1, 15000);
Algorithms::Rebin rebinner;
std::string wsName = theWS->getName();
if(wsName.empty())
{
wsName = "sourceWS";
}
// Add incident energy necessary for unit conversion
theWS->mutableRun().addProperty("Ei",13.,"meV",true);
API::AnalysisDataService::Instance().addOrReplace(wsName,theWS);
rebinner.initialize();
rebinner.setPropertyValue("InputWorkspace",theWS->getName());
rebinner.setPropertyValue("OutputWorkspace","Background");
rebinner.setPropertyValue("Params","10000,5000,15000");
rebinner.execute();
BgWS = API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>("Background");
Algorithms::ConvertUnits unitsConv;
unitsConv.initialize();
unitsConv.setPropertyValue("InputWorkspace",theWS->getName());
unitsConv.setPropertyValue("OutputWorkspace","sourceWSdE");
unitsConv.setPropertyValue("Target","DeltaE");
unitsConv.setPropertyValue("EMode","Direct");
unitsConv.execute();
Algorithms::CalculateFlatBackground bgRemoval;
bgRemoval.initialize();
bgRemoval.setPropertyValue("InputWorkspace",theWS->getName());
bgRemoval.setPropertyValue("OutputWorkspace",theWS->getName());
bgRemoval.setPropertyValue("StartX","10000");
bgRemoval.setPropertyValue("EndX","15000");
bgRemoval.setPropertyValue("Mode","Mean");
bgRemoval.execute();
unitsConv.setPropertyValue("InputWorkspace",theWS->getName());
unitsConv.setPropertyValue("OutputWorkspace","sampleWSdE");
unitsConv.setPropertyValue("Target","DeltaE");
unitsConv.setPropertyValue("EMode","Direct");
unitsConv.execute();
SourceWS = API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>("sampleWSdE");
}
~BackgroundHelperTest()
{
BgWS.reset();
SourceWS.reset();
}
void testWrongInit()
{
Algorithms::BackgroundHelper bgRemoval;
// create workspace with units of energy transfer
auto bkgWS = WorkspaceCreationHelper::createProcessedInelasticWS(std::vector<double>(1,1.), std::vector<double>(1,20.), std::vector<double>(1,10.));
TSM_ASSERT_THROWS("Should throw if background workspace is not in TOF units",bgRemoval.initialize(bkgWS,SourceWS,0),std::invalid_argument);
bkgWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(2, 15);
TSM_ASSERT_THROWS("Should throw if background is not 1 or equal to source",bgRemoval.initialize(bkgWS,SourceWS,0),std::invalid_argument);
auto sourceWS = WorkspaceCreationHelper::Create2DWorkspace(5,10);
TSM_ASSERT_THROWS("Should throw if source workspace does not have units",bgRemoval.initialize(BgWS,sourceWS,0),std::invalid_argument);
sourceWS ->getAxis(0)->setUnit("TOF");
TSM_ASSERT_THROWS("Should throw if source workspace does not have proper instrument",bgRemoval.initialize(BgWS,sourceWS,0),std::invalid_argument);
}
void testBackgroundHelper()
{
Algorithms::BackgroundHelper bgRemoval;
auto SourceWS=API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>("sourceWSdE");
int emode = static_cast<int>(Kernel::DeltaEMode().fromString("Direct"));
bgRemoval.initialize(BgWS,SourceWS,emode);
}
private:
API::MatrixWorkspace_sptr BgWS;
API::MatrixWorkspace_sptr SourceWS;
};
#endif /*ALGORITHMTEST_H_*/