GenerateGroupingPowderTest.h 4.01 KB
Newer Older
1
2
3
4
5
6
#ifndef MANTID_DATAHANDLING_GENERATEGROUPINGPOWDERTEST_H_
#define MANTID_DATAHANDLING_GENERATEGROUPINGPOWDERTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidKernel/System.h"
7
8
#include <fstream>
#include "MantidDataHandling/LoadEmptyInstrument.h"
9
#include "MantidDataHandling/GenerateGroupingPowder.h"
10
#include "MantidDataHandling/LoadDetectorsGroupingFile.h"
11
12
13
14
15

using namespace Mantid;
using namespace Mantid::DataHandling;
using namespace Mantid::API;

16
class GenerateGroupingPowderTest : public CxxTest::TestSuite {
17
18
19
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
20
21
22
23
  static GenerateGroupingPowderTest *createSuite() {
    return new GenerateGroupingPowderTest();
  }
  static void destroySuite(GenerateGroupingPowderTest *suite) { delete suite; }
24

25
  void test_Init() {
26
    GenerateGroupingPowder alg;
27
28
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
29
  }
30
31

  void test_exec() {
32
33
34
35
36
    LoadEmptyInstrument lei;
    lei.initialize();
    lei.setPropertyValue("Filename", "CNCS_Definition.xml");
    std::string wsName = "LoadEmptyInstrumentCNCS";
    lei.setPropertyValue("OutputWorkspace", wsName);
37
38
    TS_ASSERT_THROWS_NOTHING(lei.execute(););
    TS_ASSERT(lei.isExecuted());
39

40
    GenerateGroupingPowder alg;
41
    std::string xmlFile("PowderGrouping.xml");
42
    double step = 10;
43

44
45
46
47
48
49
50
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", wsName));
    TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("GroupingFilename", xmlFile));
    TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("AngleStep", "10"));
    TS_ASSERT_THROWS_NOTHING(alg.execute(););
    TS_ASSERT(alg.isExecuted());
51
52
53
54

    MatrixWorkspace_sptr ws;
    ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName);

55
56
57
    xmlFile = alg.getPropertyValue("GroupingFilename");
    std::string parFile = xmlFile;
    parFile.replace(parFile.end() - 3, parFile.end(), "par");
58
59

    // Check the results
60
    // par file
61
62
    std::ifstream pf(parFile.c_str());
    std::size_t nDet;
63
64
65
66
67
68
69
70
71
72
73
    pf >> nDet;
    TS_ASSERT_EQUALS(nDet, 14);
    for (std::size_t i = 0; i < nDet; ++i) {
      double r, th, phi, dx, dy, tth;
      detid_t detID;
      pf >> r >> th >> phi >> dx >> dy >> detID;
      TS_ASSERT_DELTA(r, 3.5, 0.2);
      TS_ASSERT_DELTA(th, step * (static_cast<double>(i) + 0.5), 0.5 * step);
      TS_ASSERT_EQUALS(phi, 0);
      TS_ASSERT_DELTA(dx, r * step * Geometry::deg2rad, 0.01);
      TS_ASSERT_EQUALS(dy, 0.01);
74
75
      auto det = ws->getInstrument()->getDetector(detID);
      tth = ws->detectorTwoTheta(*det) * Geometry::rad2deg;
76
77
      TS_ASSERT_LESS_THAN(tth, static_cast<double>(i + 1) * step);
      TS_ASSERT_LESS_THAN(static_cast<double>(i) * step, tth);
78
79
80
81
82
83
84
85
86
87
88
89
    }
    pf.close();

    LoadDetectorsGroupingFile load2;
    load2.initialize();

    TS_ASSERT(load2.setProperty("InputFile", xmlFile));
    TS_ASSERT(load2.setProperty("OutputWorkspace", "GroupPowder"));

    load2.execute();
    TS_ASSERT(load2.isExecuted());

90
91
92
    DataObjects::GroupingWorkspace_sptr gws2 =
        boost::dynamic_pointer_cast<DataObjects::GroupingWorkspace>(
            API::AnalysisDataService::Instance().retrieve("GroupPowder"));
93

94
95
96
97
98
99
    TS_ASSERT_DELTA(gws2->dataY(0)[0], 13.0, 1.0E-5);    // 130.6 degrees
    TS_ASSERT_DELTA(gws2->dataY(10000)[0], 9.0, 1.0E-5); // 97.4 degrees
    TS_ASSERT_DELTA(gws2->dataY(20000)[0], 6.0, 1.0E-5); // 62.9 degrees
    TS_ASSERT_DELTA(gws2->dataY(30000)[0], 2.0, 1.0E-5); // 27.8 degrees
    TS_ASSERT_DELTA(gws2->dataY(40000)[0], 1.0, 1.0E-5); // 14.5 degrees
    TS_ASSERT_DELTA(gws2->dataY(50000)[0], 4.0, 1.0E-5); // 49.7 degrees
100

101
    // Remove workspace from the data service.
102
103
104
105
106
    AnalysisDataService::Instance().remove(wsName);
    AnalysisDataService::Instance().remove("GroupPowder");
    // Delete files
    unlink(xmlFile.c_str());
    unlink(parFile.c_str());
107
108
109
110
  }
};

#endif /* MANTID_DATAHANDLING_GENERATEGROUPINGPOWDERTEST_H_ */