WorkspaceGroupTest.h 23.4 KB
Newer Older
1
2
#ifndef WORKSPACEGROUP_H_
#define WORKSPACEGROUP_H_
3
4
5
6

#include <cxxtest/TestSuite.h>
#include <cmath>

7
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
8
#include "MantidAlgorithms/FindDeadDetectors.h"
9
#include "MantidAlgorithms/Plus.h"
10
#include "MantidAlgorithms/PolynomialCorrection.h"
11
12
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceGroup.h"
13
14
#include "MantidGeometry/Instrument.h"

15
#include <fstream>
16
#include <Poco/File.h>
17
18
19
20
21

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

24
class WorkspaceGroupTest : public CxxTest::TestSuite {
25
private:
26
  void checkData(MatrixWorkspace_sptr work_in1, MatrixWorkspace_sptr work_in2,
27
28
                 MatrixWorkspace_sptr work_out1) {
    // default to a horizontal loop orientation
29
    checkData(work_in1, work_in2, work_out1, 0);
30
31
32
  }

  // loopOrientation 0=Horizontal, 1=Vertical
33
  void checkData(MatrixWorkspace_sptr work_in1, MatrixWorkspace_sptr work_in2,
34
35
36
                 MatrixWorkspace_sptr work_out1, int loopOrientation) {
    if (!work_in1 || !work_in2 || !work_out1) {
      TSM_ASSERT("One or more empty workspace pointers.", 0);
37
38
      return;
    }
39
    size_t ws2LoopCount = 0;
40
    if (work_in2->size() > 0) {
41
      ws2LoopCount = work_in1->size() / work_in2->size();
42
    }
43
    ws2LoopCount = (ws2LoopCount == 0) ? 1 : ws2LoopCount;
44

45
    for (size_t i = 0; i < work_out1->size(); i++) {
46
      size_t ws2Index = i;
47

48
49
      if (ws2LoopCount > 1) {
        if (loopOrientation == 0) {
50
          ws2Index = i % ws2LoopCount;
51
        } else {
52
          ws2Index = i / ws2LoopCount;
53
54
        }
      }
55
      checkDataItem(work_in1, work_in2, work_out1, i, ws2Index);
56
57
58
    }
  }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
  void checkDataItem(MatrixWorkspace_sptr work_in1,
                     MatrixWorkspace_sptr work_in2,
                     MatrixWorkspace_sptr work_out1, size_t i,
                     size_t ws2Index) {
    double sig1 =
        work_in1->dataY(i / work_in1->blocksize())[i % work_in1->blocksize()];
    double sig2 = work_in2->dataY(
        ws2Index / work_in1->blocksize())[ws2Index % work_in2->blocksize()];
    double sig3 =
        work_out1->dataY(i / work_in1->blocksize())[i % work_in1->blocksize()];
    TS_ASSERT_DELTA(
        work_in1->dataX(i / work_in1->blocksize())[i % work_in1->blocksize()],
        work_out1->dataX(i / work_in1->blocksize())[i % work_in1->blocksize()],
        0.0001);
73
74

    TS_ASSERT_DELTA(sig1 + sig2, sig3, 0.0001);
75
76
77
78
    double err1 =
        work_in1->dataE(i / work_in1->blocksize())[i % work_in1->blocksize()];
    double err2 = work_in2->dataE(
        ws2Index / work_in2->blocksize())[ws2Index % work_in2->blocksize()];
79
    double err3(sqrt((err1 * err1) + (err2 * err2)));
80
81
82
83
    TS_ASSERT_DELTA(
        err3,
        work_out1->dataE(i / work_in1->blocksize())[i % work_in1->blocksize()],
        0.0001);
84
  }
85

86
public:
87
88
  void testExecwithOneGroupandOne2DWorkspace() {

89
    int nHist = 20, nBins = 10;
90
    // Register the workspace in the data service
91
92
93
94
95
96
97
98
    MatrixWorkspace_sptr work_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr work_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
    MatrixWorkspace_sptr work_in3 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr work_in4 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
99
100

    WorkspaceGroup_sptr wsSptr = WorkspaceGroup_sptr(new WorkspaceGroup);
101
    if (wsSptr) {
102
103
104
105
106
107
108
109
110
111
      AnalysisDataService::Instance().add("test_in", wsSptr);
      AnalysisDataService::Instance().add("test_in_1", work_in1);
      wsSptr->add("test_in_1");
      AnalysisDataService::Instance().add("test_in_2", work_in2);
      wsSptr->add("test_in_2");
      AnalysisDataService::Instance().add("test_in_3", work_in3);
      wsSptr->add("test_in_3");
      AnalysisDataService::Instance().add("test_in_4", work_in4);
      wsSptr->add("test_in_4");
    }
112
    WorkspaceGroup_sptr work_in;
113
114
    work_in =
        AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("test_in");
115
    TS_ASSERT_EQUALS(work_in, wsSptr);
116
    if (work_in) {
117
      std::vector<std::string> GroupNames = work_in->getNames();
118
      size_t nSize = GroupNames.size();
119
      TS_ASSERT_EQUALS(nSize, 4);
120
121
    }
    Plus alg;
122
    alg.initialize();
123
124
125
    alg.setPropertyValue("LHSWorkspace", "test_in");
    alg.setPropertyValue("RHSWorkspace", "test_in_1");
    alg.setPropertyValue("OutputWorkspace", "test_out");
126
    TS_ASSERT_THROWS_NOTHING(alg.execute());
127
    TS_ASSERT(alg.isExecuted());
128
129

    WorkspaceGroup_sptr work_out;
130
131
132
    TS_ASSERT_THROWS_NOTHING(
        work_out = boost::dynamic_pointer_cast<WorkspaceGroup>(
            AnalysisDataService::Instance().retrieve("test_out")));
133
    MatrixWorkspace_sptr work_out1;
134
135
136
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("test_out_1")));
137
    MatrixWorkspace_sptr work_out2;
138
139
140
    TS_ASSERT_THROWS_NOTHING(
        work_out2 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("test_out_2")));
141
    MatrixWorkspace_sptr work_out3;
142
143
144
    TS_ASSERT_THROWS_NOTHING(
        work_out3 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("test_out_3")));
145
    MatrixWorkspace_sptr work_out4;
146
147
148
    TS_ASSERT_THROWS_NOTHING(
        work_out4 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("test_out_4")));
149

150
151
    if (!work_out1)
      return;
152

153
    checkData(work_in1, work_in1, work_out1);
154
155
156
    checkData(work_in2, work_in1, work_out2);
    checkData(work_in3, work_in1, work_out3);
    checkData(work_in4, work_in1, work_out4);
157

158
159
    work_out =
        AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("test_out");
160
    work_out->removeAll();
161
162
163
164

    AnalysisDataService::Instance().remove("test_in");
    AnalysisDataService::Instance().remove("test_in_1");
    AnalysisDataService::Instance().remove("test_in_2");
165
    AnalysisDataService::Instance().remove("test_in_3");
166
167
    AnalysisDataService::Instance().remove("test_in_4");
    AnalysisDataService::Instance().remove("test_out");
168
    AnalysisDataService::Instance().remove("test_out_1");
169
    AnalysisDataService::Instance().remove("test_out_2");
170
171
    AnalysisDataService::Instance().remove("test_out_3");
    AnalysisDataService::Instance().remove("test_out_4");
172
  }
173

174
  void testExecOnlyOneGroupInput() {
175
    int nHist = 20, nBins = 10;
176
    // Register the workspace in the data service
177
178
179
180
    Workspace2D_sptr work_in1 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins, 1);
    Workspace2D_sptr work_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins, 1);
181
182
183
184
    Instrument_sptr instr(new Instrument);
    work_in1->setInstrument(instr);
    work_in2->setInstrument(instr);

185
    // set some dead detectors
186
    boost::shared_ptr<Mantid::MantidVec> yDead(new Mantid::MantidVec(nHist, 0));
187
188
    for (int i = 0; i < nBins; i++) {
      if (i % 2 == 0) {
189
        work_in1->setData(i, yDead, yDead);
190
      }
191
      work_in1->getSpectrum(i)->setSpectrumNo(i);
192
193
      Mantid::Geometry::Detector *det =
          new Mantid::Geometry::Detector("", i, NULL);
194
195
196
      instr->add(det);
      instr->markAsDetector(det);
    }
197

198
199
    for (int i = 0; i < nBins; i++) {
      if (i % 2 == 0) {
200
        work_in2->setData(i, yDead, yDead);
201
      }
202
      work_in2->getSpectrum(i)->setSpectrumNo(i);
203
    }
204
205
206
207
208
209
210
211
212

    WorkspaceGroup_sptr wsSptr = WorkspaceGroup_sptr(new WorkspaceGroup);
    AnalysisDataService::Instance().add("testdead_in", wsSptr);
    AnalysisDataService::Instance().add("testdead_in_1", work_in1);
    wsSptr->add("testdead_in_1");
    AnalysisDataService::Instance().add("testdead_in_2", work_in2);
    wsSptr->add("testdead_in_2");

    WorkspaceGroup_sptr work_in;
213
214
    work_in = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
        "testdead_in");
215
216
217
    TS_ASSERT_EQUALS(work_in, wsSptr);

    FindDeadDetectors alg;
218
    alg.initialize();
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    alg.setPropertyValue("InputWorkspace", "testdead_in");
    alg.setPropertyValue("OutputWorkspace", "testdead_out");
    alg.setPropertyValue("DeadThreshold", "0");
    alg.setPropertyValue("LiveValue", "1");
    alg.setPropertyValue("DeadValue", "2");
    std::string filename = "testFile.txt";
    alg.setPropertyValue("OutputFile", filename);
    alg.execute();

    TS_ASSERT_THROWS_NOTHING(alg.execute());

    TS_ASSERT(alg.isExecuted());
    // Get back the output workspace
    WorkspaceGroup_sptr work_out;
233
234
235
    TS_ASSERT_THROWS_NOTHING(
        work_out = boost::dynamic_pointer_cast<WorkspaceGroup>(
            AnalysisDataService::Instance().retrieve("testdead_out")));
236
237

    MatrixWorkspace_sptr work_out1;
238
239
240
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("testdead_out_1")));
241
242

    MatrixWorkspace_sptr work_out2;
243
244
245
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = boost::dynamic_pointer_cast<MatrixWorkspace>(
            AnalysisDataService::Instance().retrieve("testdead_out_2")));
246
247
248

    // Get back the output property
    std::vector<int> deadDets;
249
250
251
    TS_ASSERT_THROWS_NOTHING(deadDets = alg.getProperty("FoundDead"))
    TS_ASSERT_EQUALS(deadDets.size(), 0)
    for (int i = 0; i < nBins; i++) {
252
253
      const double val = work_out1->readY(i)[0];
      double valExpected = 1;
254
      if (i % 2 == 0) {
255
        valExpected = 2;
256
257
        if (!deadDets.empty())
          TS_ASSERT_EQUALS(deadDets[i / 2], i)
258
      }
259
      TS_ASSERT_DELTA(val, valExpected, 1e-9);
260
261
    }

262
    std::fstream outFile(filename.c_str());
263
    TS_ASSERT(outFile)
264
    outFile.close();
265
    Poco::File(filename).remove();
266

267
    work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
268
        "testdead_out");
269
    work_out->removeAll();
270

271
272
273
274
275
276
277
    AnalysisDataService::Instance().remove("testdead_in");
    AnalysisDataService::Instance().remove("testdead_in_1");
    AnalysisDataService::Instance().remove("testdead_in_2");
    AnalysisDataService::Instance().remove("testdead_out");
    AnalysisDataService::Instance().remove("testdead_out_1");
    AnalysisDataService::Instance().remove("testdead_out_2");
  }
278
279

  void testExecGroupwithNochildWorkspaces() {
280
281
282
283
    WorkspaceGroup_sptr wsSptr = WorkspaceGroup_sptr(new WorkspaceGroup);
    AnalysisDataService::Instance().add("InputWS", wsSptr);
    Mantid::Algorithms::PolynomialCorrection poly3;
    poly3.initialize();
284
285
286
287
288
289
    TS_ASSERT_THROWS_NOTHING(
        poly3.setPropertyValue("InputWorkspace", "InputWS"))
    TS_ASSERT_THROWS_NOTHING(poly3.setPropertyValue("OutputWorkspace", "WSCor"))
    TS_ASSERT_THROWS_NOTHING(
        poly3.setPropertyValue("Coefficients", "3.0,2.0,1.0"))
    TS_ASSERT(!poly3.execute())
290
291
292

    AnalysisDataService::Instance().remove("InputWS");
  }
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  void testTwoGroupWorkspaces() {
    int nHist = 10, nBins = 20;
    // Register the workspace in the data service
    MatrixWorkspace_sptr worklhs_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in3 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in4 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);

    MatrixWorkspace_sptr workrhs_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in3 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in4 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    WorkspaceGroup_sptr wsSptr = WorkspaceGroup_sptr(new WorkspaceGroup);
    if (wsSptr) {
      AnalysisDataService::Instance().add("testlhs_in", wsSptr);
      AnalysisDataService::Instance().add("testlhs_in_1", worklhs_in1);
      wsSptr->add("testlhs_in_1");
      AnalysisDataService::Instance().add("testlhs_in_2", worklhs_in2);
      wsSptr->add("testlhs_in_2");
      AnalysisDataService::Instance().add("testlhs_in_3", worklhs_in3);
      wsSptr->add("testlhs_in_3");
      AnalysisDataService::Instance().add("testlhs_in_4", worklhs_in4);
      wsSptr->add("testlhs_in_4");
    }
    WorkspaceGroup_sptr worklhsgrp_in;
    worklhsgrp_in = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
        "testlhs_in");
    TS_ASSERT_EQUALS(worklhsgrp_in, wsSptr);
    if (worklhsgrp_in) {
      std::vector<std::string> GroupNames = worklhsgrp_in->getNames();
      size_t nSize = GroupNames.size();
      TS_ASSERT_EQUALS(nSize, 4);
    }
336

337
338
339
340
341
342
343
344
345
346
347
    WorkspaceGroup_sptr wsSptr1 = WorkspaceGroup_sptr(new WorkspaceGroup);
    if (wsSptr1) {
      AnalysisDataService::Instance().add("testrhs_in", wsSptr1);
      AnalysisDataService::Instance().add("testrhs_in_1", workrhs_in1);
      wsSptr1->add("testrhs_in_1");
      AnalysisDataService::Instance().add("testrhs_in_2", workrhs_in2);
      wsSptr1->add("testrhs_in_2");
      AnalysisDataService::Instance().add("testrhs_in_3", workrhs_in3);
      wsSptr1->add("testrhs_in_3");
      AnalysisDataService::Instance().add("testrhs_in_4", workrhs_in4);
      wsSptr1->add("testrhs_in_4");
348
    }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    WorkspaceGroup_sptr workrhsgrp_in;
    workrhsgrp_in = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
        "testrhs_in");
    TS_ASSERT_EQUALS(worklhsgrp_in, wsSptr);
    if (workrhsgrp_in) {
      std::vector<std::string> GroupNames = workrhsgrp_in->getNames();
      size_t nSize = GroupNames.size();
      TS_ASSERT_EQUALS(nSize, 4);
    }

    Plus alg;
    alg.initialize();
    alg.setPropertyValue("LHSWorkspace", "testlhs_in");
    alg.setPropertyValue("RHSWorkspace", "testrhs_in");
    alg.setPropertyValue("OutputWorkspace", "test_out");
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    WorkspaceGroup_sptr work_out;
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
            "test_out"));
    MatrixWorkspace_sptr work_out1;
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_1"));
    MatrixWorkspace_sptr work_out2;
    TS_ASSERT_THROWS_NOTHING(
        work_out2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_2"));
    MatrixWorkspace_sptr work_out3;
    TS_ASSERT_THROWS_NOTHING(
        work_out3 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_3"));
    MatrixWorkspace_sptr work_out4;
    TS_ASSERT_THROWS_NOTHING(
        work_out4 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_4"));
    if (!work_out4)
      return;
389

390
391
392
393
    checkData(worklhs_in1, workrhs_in1, work_out1);
    checkData(worklhs_in2, workrhs_in2, work_out2);
    checkData(worklhs_in3, workrhs_in3, work_out3);
    checkData(worklhs_in4, workrhs_in4, work_out4);
394

395
396
397
398
399
    work_out =
        AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("test_out");
    if (!work_out)
      return;
    work_out->removeAll();
400

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    AnalysisDataService::Instance().remove("testlhs_in");
    AnalysisDataService::Instance().remove("testlhs_in_1");
    AnalysisDataService::Instance().remove("testlhs_in_2");
    AnalysisDataService::Instance().remove("testlhs_in_3");
    AnalysisDataService::Instance().remove("testlhs_in_4");

    AnalysisDataService::Instance().remove("testrhs_in");
    AnalysisDataService::Instance().remove("testrhs_in_1");
    AnalysisDataService::Instance().remove("testrhs_in_2");
    AnalysisDataService::Instance().remove("testrhs_in_3");
    AnalysisDataService::Instance().remove("testrhs_in_4");

    AnalysisDataService::Instance().remove("test_out");
    AnalysisDataService::Instance().remove("test_out_1");
    AnalysisDataService::Instance().remove("test_out_2");
    AnalysisDataService::Instance().remove("test_out_3");
    AnalysisDataService::Instance().remove("test_out_4");
  }

  void testLHS2DWorkspaceandRHSGroupWorkspace() {
    int nHist = 10, nBins = 20;

    MatrixWorkspace_sptr worklhs_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    if (worklhs_in1)
      AnalysisDataService::Instance().add("testlhs_in1", worklhs_in1);

    MatrixWorkspace_sptr workrhs_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in3 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr workrhs_in4 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);

    WorkspaceGroup_sptr wsSptr1 = WorkspaceGroup_sptr(new WorkspaceGroup);
    if (wsSptr1) {
      AnalysisDataService::Instance().add("testrhs_in", wsSptr1);
      AnalysisDataService::Instance().add("testrhs_in_1", workrhs_in1);
      wsSptr1->add("testrhs_in_1");
      AnalysisDataService::Instance().add("testrhs_in_2", workrhs_in2);
      wsSptr1->add("testrhs_in_2");
      AnalysisDataService::Instance().add("testrhs_in_3", workrhs_in3);
      wsSptr1->add("testrhs_in_3");
      AnalysisDataService::Instance().add("testrhs_in_4", workrhs_in4);
      wsSptr1->add("testrhs_in_4");
    }
    WorkspaceGroup_sptr workrhsgrp_in;
    workrhsgrp_in = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
        "testrhs_in");
    TS_ASSERT_EQUALS(workrhsgrp_in, wsSptr1);
    if (workrhsgrp_in) {
      std::vector<std::string> GroupNames = workrhsgrp_in->getNames();
      size_t nSize = GroupNames.size();
      TS_ASSERT_EQUALS(nSize, 4);
457
    }
458

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
    Plus alg;
    alg.initialize();
    alg.setPropertyValue("LHSWorkspace", "testlhs_in1");
    alg.setPropertyValue("RHSWorkspace", "testrhs_in");
    alg.setPropertyValue("OutputWorkspace", "test_out");
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());

    WorkspaceGroup_sptr work_out;
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
            "test_out"));
    MatrixWorkspace_sptr work_out1;
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_1"));
    MatrixWorkspace_sptr work_out2;
    TS_ASSERT_THROWS_NOTHING(
        work_out2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_2"));
    MatrixWorkspace_sptr work_out3;
    TS_ASSERT_THROWS_NOTHING(
        work_out3 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_3"));
    MatrixWorkspace_sptr work_out4;
    TS_ASSERT_THROWS_NOTHING(
        work_out4 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_4"));

    checkData(worklhs_in1, workrhs_in1, work_out1);
    checkData(worklhs_in1, workrhs_in2, work_out2);
    checkData(worklhs_in1, workrhs_in3, work_out3);
    checkData(worklhs_in1, workrhs_in4, work_out4);

    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
            "test_out");)
    if (!work_out)
      return;
    work_out->removeAll();

    AnalysisDataService::Instance().remove("testlhs_in1");

    AnalysisDataService::Instance().remove("testrhs_in");
    AnalysisDataService::Instance().remove("testrhs_in_1");
    AnalysisDataService::Instance().remove("testrhs_in_2");
    AnalysisDataService::Instance().remove("testrhs_in_3");
    AnalysisDataService::Instance().remove("testrhs_in_4");

    AnalysisDataService::Instance().remove("test_out");
    AnalysisDataService::Instance().remove("test_out_1");
    AnalysisDataService::Instance().remove("test_out_2");
    AnalysisDataService::Instance().remove("test_out_3");
    AnalysisDataService::Instance().remove("test_out_4");
  }
514

515
516
517
518
519
520
521
522
523
524
525
526
  void testLHSandRHSSameGroupWorkspaces() {
    // this is the test for self addition
    int nHist = 10, nBins = 20;
    // Register the workspace in the data service
    MatrixWorkspace_sptr worklhs_in1 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in2 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in3 =
        WorkspaceCreationHelper::Create2DWorkspace123(nHist, nBins);
    MatrixWorkspace_sptr worklhs_in4 =
        WorkspaceCreationHelper::Create2DWorkspace154(nHist, nBins);
527

528
529
530
531
532
533
534
535
536
537
538
    WorkspaceGroup_sptr wsSptr = WorkspaceGroup_sptr(new WorkspaceGroup);
    if (wsSptr) {
      AnalysisDataService::Instance().add("testlhs_in", wsSptr);
      AnalysisDataService::Instance().add("testlhs_in_1", worklhs_in1);
      wsSptr->add("testlhs_in_1");
      AnalysisDataService::Instance().add("testlhs_in_2", worklhs_in2);
      wsSptr->add("testlhs_in_2");
      AnalysisDataService::Instance().add("testlhs_in_3", worklhs_in3);
      wsSptr->add("testlhs_in_3");
      AnalysisDataService::Instance().add("testlhs_in_4", worklhs_in4);
      wsSptr->add("testlhs_in_4");
539
    }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
    WorkspaceGroup_sptr worklhsgrp_in;
    worklhsgrp_in = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
        "testlhs_in");
    TS_ASSERT_EQUALS(worklhsgrp_in, wsSptr);
    if (worklhsgrp_in) {
      std::vector<std::string> GroupNames = worklhsgrp_in->getNames();
      size_t nSize = GroupNames.size();
      TS_ASSERT_EQUALS(nSize, 4);
    }

    Plus alg;
    alg.initialize();
    alg.setPropertyValue("LHSWorkspace", "testlhs_in");
    alg.setPropertyValue("RHSWorkspace", "testlhs_in");
    alg.setPropertyValue("OutputWorkspace", "test_out");
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());

    WorkspaceGroup_sptr work_out;
    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
            "test_out"));
    MatrixWorkspace_sptr work_out1;
    TS_ASSERT_THROWS_NOTHING(
        work_out1 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_1"));
    MatrixWorkspace_sptr work_out2;
    TS_ASSERT_THROWS_NOTHING(
        work_out2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_2"));
    MatrixWorkspace_sptr work_out3;
    TS_ASSERT_THROWS_NOTHING(
        work_out3 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_3"));
    MatrixWorkspace_sptr work_out4;
    TS_ASSERT_THROWS_NOTHING(
        work_out4 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "test_out_4"));

    checkData(worklhs_in1, worklhs_in1, work_out1);
    checkData(worklhs_in2, worklhs_in2, work_out2);
    checkData(worklhs_in3, worklhs_in3, work_out3);
    checkData(worklhs_in4, worklhs_in4, work_out4);

    TS_ASSERT_THROWS_NOTHING(
        work_out = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
            "test_out");)
    if (!work_out)
      return;
    work_out->removeAll();

    AnalysisDataService::Instance().remove("testlhs_in");
    AnalysisDataService::Instance().remove("testlhs_in_1");
    AnalysisDataService::Instance().remove("testlhs_in_2");
    AnalysisDataService::Instance().remove("testlhs_in_3");
    AnalysisDataService::Instance().remove("testlhs_in_4");
596

597
598
599
600
601
602
603
    AnalysisDataService::Instance().remove("test_out");
    AnalysisDataService::Instance().remove("test_out_1");
    AnalysisDataService::Instance().remove("test_out_2");
    AnalysisDataService::Instance().remove("test_out_3");
    AnalysisDataService::Instance().remove("test_out_4");
  }
};
604
605

#endif /*PLUSTEST_H_*/