FindDetectorsOutsideLimitsTest.h 6.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef FINDDETECTORSOUTSIDELIMITSTEST_H_
#define FINDDETECTORSOUTSIDELIMITSTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAlgorithms/FindDetectorsOutsideLimits.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
10
#include "MantidDataObjects/EventWorkspace.h"
11
12
#include "MantidGeometry/Instrument.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
13
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
14

Campbell, Stuart's avatar
Campbell, Stuart committed
15
#include <Poco/File.h>
16
#include <fstream>
17

18
19

using namespace Mantid::Algorithms;
20
using namespace Mantid::API;
21
using namespace Mantid::DataObjects;
22
23
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
24

25
class FindDetectorsOutsideLimitsTest : public CxxTest::TestSuite {
26
public:
27
  void testInit() {
28
29
30
    FindDetectorsOutsideLimits alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
31
    TS_ASSERT(alg.isInitialized());
32
33
  }

34
  void testExec() {
35
    const int sizex = 10, sizey = 20;
36
37
    // Register the workspace in the data service and initialise it with abitary
    // data
38
    Workspace2D_sptr work_in =
39
40
        // the x values look like this -1, 2, 5, 8, 11, 14, 17, 20, 23, 26
        WorkspaceCreationHelper::Create2DWorkspaceBinned(sizey, sizex, -1, 3.0);
41

42
43
44
    Instrument_sptr instr(new Instrument);
    work_in->setInstrument(instr);

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    // yVeryDead is a detector with low counts
    boost::shared_ptr<Mantid::MantidVec> yVeryDead(
        new Mantid::MantidVec(sizex, 0.1));
    // yTooDead gives some counts at the start but has a whole region full of
    // zeros
    double TD[sizex] = {2, 4, 5, 10, 0, 0, 0, 0, 0, 0};
    boost::shared_ptr<Mantid::MantidVec> yTooDead(
        new Mantid::MantidVec(TD, TD + 10));
    // yStrange dies after giving some counts but then comes back
    double S[sizex] = {0.2, 4, 50, 0.001, 0, 0, 0, 0, 1, 0};
    boost::shared_ptr<Mantid::MantidVec> yStrange(
        new Mantid::MantidVec(S, S + 10));
    for (int i = 0; i < sizey; i++) {
      if (i % 3 == 0) { // the last column is set arbitrarily to have the same
                        // values as the second because the errors shouldn't
                        // make any difference
61
62
        work_in->setData(i, yTooDead, yTooDead);
      }
63
      if (i % 2 == 0) {
64
65
        work_in->setData(i, yVeryDead, yVeryDead);
      }
66
      if (i == 19) {
67
68
        work_in->setData(i, yStrange, yTooDead);
      }
69
70
      work_in->getSpectrum(i)->setSpectrumNo(i);

71
72
      Mantid::Geometry::Detector *det =
          new Mantid::Geometry::Detector("", i, NULL);
73
74
      instr->add(det);
      instr->markAsDetector(det);
75
      work_in->getSpectrum(i)->setDetectorID(i);
76
77
78
79
80
81
    }

    FindDetectorsOutsideLimits alg;

    AnalysisDataService::Instance().add("testdead_in", work_in);
    alg.initialize();
82
83
84
85
    alg.setPropertyValue("InputWorkspace", "testdead_in");
    alg.setPropertyValue("OutputWorkspace", "testdead_out");
    alg.setPropertyValue("LowThreshold", "1");
    alg.setPropertyValue("HighThreshold", "21.01");
86
87
88
89
    alg.setPropertyValue("RangeLower", "-1");

    // Testing behavour with Range_lower or Range_upper not set
    TS_ASSERT_THROWS_NOTHING(alg.execute());
90
    TS_ASSERT(alg.isExecuted());
91
92
93

    // Get back the output workspace
    MatrixWorkspace_sptr work_out;
94
95
96
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "testdead_out"));
97

98
99
100
    const int numFailed = alg.getProperty("NumberOfFailures");
    TS_ASSERT_EQUALS(numFailed, 11);

101
102
    const double liveValue(0.0);
    const double maskValue(1.0);
103
    for (int i = 0; i < sizey; i++) {
104
      const double val = work_out->readY(i)[0];
105
106
      double valExpected = liveValue;
      // Check masking
107
      IDetector_const_sptr det;
108
109
      TS_ASSERT_THROWS_NOTHING(det = work_out->getDetector(i));
      // Spectra set up with yVeryDead fail low counts or yStrange fail on high
110
      if (i % 2 == 0 || i == 19) {
111
        valExpected = maskValue;
112
      }
113

114
      TS_ASSERT_DELTA(val, valExpected, 1e-9);
115
    }
116

117
118
    // Set cut off much of the range and yTooDead will stop failing on high
    // counts
119
120
121
    alg.setPropertyValue("RangeUpper", "4.9");
    alg.initialize();
    TS_ASSERT_THROWS_NOTHING(alg.execute());
122
123
124
125
126
    TS_ASSERT(alg.isExecuted());
    // retrieve the output workspace
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "testdead_out"));
127
128

    const int numFailed2 = alg.getProperty("NumberOfFailures");
129
    TS_ASSERT_EQUALS(numFailed2, 10);
130

131
132
    // Check the dead detectors found agrees with what was setup above
    for (int i = 0; i < sizey; i++) {
133
      const double val = work_out->readY(i)[0];
134
135
      double valExpected = liveValue;
      // Check masking
136
      IDetector_const_sptr det;
137
138
      TS_ASSERT_THROWS_NOTHING(det = work_out->getDetector(i));
      // Spectra set up with yVeryDead fail low counts or yStrange fail on high
139
      if (i % 2 == 0) {
140
        valExpected = maskValue;
141
      }
142

143
      TS_ASSERT_DELTA(val, valExpected, 1e-9);
144
    }
145

146
    AnalysisDataService::Instance().remove("testdead_in");
147
148
149
    AnalysisDataService::Instance().remove("testdead_out");
  }

150
  void testExec_Event() {
151
    // Make a workspace with 50 pixels, 200 events per pixel.
152
153
154
155
    EventWorkspace_sptr work_in = WorkspaceCreationHelper::CreateEventWorkspace(
        50, 100, 100, 0.0, 1.0, 2, 1);
    Instrument_sptr inst =
        ComponentCreationHelper::createTestInstrumentCylindrical(10);
156
    work_in->setInstrument(inst);
157
    DateAndTime run_start("2010-01-01T00:00:00");
158
    // Add ten more at #10 so that it fails
159
160
161
    for (int i = 0; i < 10; i++)
      work_in->getEventList(10)
          .addEventQuickly(TofEvent((i + 0.5), run_start + double(i)));
162
163
164
165
166

    AnalysisDataService::Instance().add("testdead_in", work_in);

    FindDetectorsOutsideLimits alg;
    alg.initialize();
167
168
169
170
    alg.setPropertyValue("InputWorkspace", "testdead_in");
    alg.setPropertyValue("OutputWorkspace", "testdead_out");
    alg.setPropertyValue("LowThreshold", "1");
    alg.setPropertyValue("HighThreshold", "201");
171
172
173
    alg.setPropertyValue("RangeLower", "-1");
    alg.setPropertyValue("RangeUpper", "1000");
    alg.execute();
174
    TS_ASSERT(alg.isExecuted());
175
176

    MatrixWorkspace_sptr work_out;
177
178
179
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "testdead_out"));
180

181
182
183
184
    TS_ASSERT_EQUALS(work_out->dataY(0)[0], 0.0);
    TS_ASSERT_EQUALS(work_out->dataY(9)[0], 0.0);
    TS_ASSERT_EQUALS(work_out->dataY(10)[0], 1.0);
    TS_ASSERT_EQUALS(work_out->dataY(11)[0], 0.0);
185
186
187
188

    AnalysisDataService::Instance().remove("testdead_in");
    AnalysisDataService::Instance().remove("testdead_out");
  }
189
190
191
};

#endif /*FINDDETECTORSOUTSIDELIMITSTEST_H_*/