Skip to content
Snippets Groups Projects
Stitch1DManyTest.h 51.4 KiB
Newer Older
    // Third group
    auto ws5 = createUniformWorkspace(1.6, 0.1, 1.5, 2.5);
    auto ws6 = createUniformWorkspace(1.6, 0.1, 1.6, 3.0);
    WorkspaceGroup_sptr group3 = boost::make_shared<WorkspaceGroup>();
    group3->addWorkspace(ws5);
    group3->addWorkspace(ws6);

    // The algorithm needs the workspaces to be in the ADS
    AnalysisDataService::Instance().addOrReplace("group1", group1);
    AnalysisDataService::Instance().addOrReplace("group2", group2);
    AnalysisDataService::Instance().addOrReplace("group3", group3);

    // ws1 will be stitched with ws3 and ws5
    // ws2 will be stitched with ws4 and ws6

    Stitch1DMany alg;
    alg.setChild(true);
    alg.initialize();
    alg.setProperty("InputWorkspaces", "group1, group2, group3");
    alg.setProperty("Params", "0.1, 0.1, 2.6");
    alg.setProperty("StartOverlaps", "0.8, 1.6");
    alg.setProperty("EndOverlaps", "1.1, 1.9");
    alg.setProperty("UseManualScaleFactors", "1");
    alg.setProperty("ScaleFactorFromPeriod", 2);
    alg.setProperty("OutputWorkspace", "outws");
    // By keeping ManualScaleFactors empty (default value) it allows workspaces
    // in other periods to be scaled by scale factors from a specific period.
    // Periods 0 and 2 workspaces will be scaled by scale factors from period 1.

    // Test output ws
    Workspace_sptr outws = alg.getProperty("OutputWorkspace");
    TS_ASSERT(outws);
    auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(outws);
    TS_ASSERT_EQUALS(group->getNumberOfEntries(), 2);

    // First item in the output group
    auto stitched =
        boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(0));
    TS_ASSERT_EQUALS(stitched->getNumberHistograms(), 2);
    TS_ASSERT_EQUALS(stitched->blocksize(), 25);
    // First spectrum, Y values
    TS_ASSERT_DELTA(stitched->y(0)[0], 1, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[9], 1.01589, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[16], 0.97288, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[24], 0.9375, 0.00001);
    // Second spectrum, Y values
    TS_ASSERT_DELTA(stitched->y(1)[0], 2, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[9], 1.98375, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[16], 1.70307, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[24], 1.56250, 0.00001);
    // First spectrum, E values
    TS_ASSERT_DELTA(stitched->e(0)[0], 1, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[9], 0.70111, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[16], 0.60401, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[24], 0.76547, 0.00001);
    // Second spectrum, E values
    TS_ASSERT_DELTA(stitched->e(1)[0], 1.41421, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[9], 0.97973, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[16], 0.79916, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[24], 0.98821, 0.00001);

    // Second item in the output group
    stitched = boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(1));
    TS_ASSERT_EQUALS(stitched->getNumberHistograms(), 2);
    TS_ASSERT_EQUALS(stitched->blocksize(), 25);
    // First spectrum, Y values
    TS_ASSERT_DELTA(stitched->y(0)[0], 1.5, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[9], 1.5, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[16], 1.15385, 0.00001);
    TS_ASSERT_DELTA(stitched->y(0)[24], 1, 0.00001);
    // Second spectrum, Y values
    TS_ASSERT_DELTA(stitched->y(1)[0], 2.5, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[9], 2.46735, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[16], 2.06568, 0.00001);
    TS_ASSERT_DELTA(stitched->y(1)[24], 1.87500, 0.00001);
    // First spectrum, E values
    TS_ASSERT_DELTA(stitched->e(0)[0], 1.22474, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[9], 0.85194, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[16], 0.65779, 0.00001);
    TS_ASSERT_DELTA(stitched->e(0)[24], 0.79057, 0.00001);
    // Second spectrum, E values
    TS_ASSERT_DELTA(stitched->e(1)[0], 1.58114, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[9], 1.09265, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[16], 0.88013, 0.00001);
    TS_ASSERT_DELTA(stitched->e(1)[24], 1.08253, 0.00001);

    // Test out scale factors
    std::vector<double> scales = alg.getProperty("OutScaleFactors");
    TS_ASSERT_EQUALS(scales.size(), 4);
    TS_ASSERT_DELTA(scales[0], 0.9375, 0.0001);
    TS_ASSERT_DELTA(scales[1], 0.6249, 0.0001);
    TS_ASSERT_DELTA(scales[2], 0.9375, 0.0001);
    TS_ASSERT_DELTA(scales[3], 0.6249, 0.0001);

    // Clear the ADS
    AnalysisDataService::Instance().clear();
  }

  void test_two_workspaces_history() {
    // This test is functionally similar to test_two_workspaces
    // Two matrix workspaces with two spectra each
    createUniformWorkspace(0.1, 0.1, 1., 2., true, "ws1");
    createUniformWorkspace(0.8, 0.1, 1.1, 2.1, true, "ws2");

    Stitch1DMany alg;
    alg.initialize();
    alg.setProperty("InputWorkspaces", "ws1, ws2");
    alg.setProperty("Params", "0.1, 0.1, 1.8");
    alg.setProperty("StartOverlaps", "0.8");
    alg.setProperty("EndOverlaps", "1.1");
    alg.setProperty("OutputWorkspace", "outws");
    TS_ASSERT(alg.isExecuted());
    //Workspace_sptr outws = alg.getProperty("OutputWorkspace");
    //TS_ASSERT(outws);
        AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("outws");
    // Test the algorithm histories
    std::vector<std::string> histNames;
    auto histories = stitched->history().getAlgorithmHistories();
    for (auto &hist : histories) {
      histNames.push_back(hist->name());
    const std::string createWsName = "CreateWorkspace";
    const std::string s1dmName = "Stitch1DMany";

    TS_ASSERT_EQUALS(histNames[0], createWsName);
    TS_ASSERT_EQUALS(histNames[1], createWsName);
    TS_ASSERT_EQUALS(histNames[2], s1dmName);
    AnalysisDataService::Instance().clear();
    // This test is functionally similar to
    // test_two_groups_with_two_workspaces_each

    // Two groups with two matrix workspaces each.
    // Each matrix workspace has two spectra.

    // First group
    createUniformWorkspace(0.1, 0.1, 1., 2., true, "ws1");
    createUniformWorkspace(0.1, 0.1, 1.5, 2.5, true, "ws2");
    doGroupWorkspaces("ws1, ws2", "group1");
    createUniformWorkspace(0.8, 0.1, 1.1, 2.1, true, "ws3");
    createUniformWorkspace(0.8, 0.1, 1.6, 2.6, true, "ws4");
    doGroupWorkspaces("ws3, ws4", "group2");

    Stitch1DMany alg;
    alg.initialize();
    alg.setProperty("InputWorkspaces", "group1, group2");
    alg.setProperty("Params", "0.1");
    alg.setProperty("StartOverlaps", "0.8");
    alg.setProperty("EndOverlaps", "1.1");
    alg.setProperty("OutputWorkspace", "outws");
    TS_ASSERT(alg.isExecuted());
    //Workspace_sptr outws = alg.getProperty("OutputWorkspace");
    //TS_ASSERT(outws);
    auto group =
        AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("outws");
    TS_ASSERT_EQUALS(group->getNumberOfEntries(), 2);

    // First item in the output group
    auto stitched =
        boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(0));
    // Test the algorithm histories
    std::vector<std::string> histNames;
    auto histories = stitched->history().getAlgorithmHistories();
    for (auto &hist : histories) {
      histNames.push_back(hist->name());
    const std::string createWsName = "CreateWorkspace";
    const std::string groupWsName = "GroupWorkspaces";
    const std::string s1dmName = "Stitch1DMany";

    TS_ASSERT_EQUALS(histNames[0], createWsName);
    TS_ASSERT_EQUALS(histNames[1], groupWsName);
    TS_ASSERT_EQUALS(histNames[2], createWsName);
    TS_ASSERT_EQUALS(histNames[3], groupWsName);
    TS_ASSERT_EQUALS(histNames[4], s1dmName);
    AnalysisDataService::Instance().clear();
  }

  void test_two_groups_scale_factor_from_period_history() {
    // This test is functionally similar to
    // test_two_groups_with_three_workspaces_scale_factor_from_period

    // Three groups with two matrix workspaces each.
    // Each matrix workspace has two spectra.

    // First group
    createUniformWorkspace(0.1, 0.1, 1., 2., true, "ws1");
    createUniformWorkspace(0.1, 0.1, 1.5, 2.5, true, "ws2");
    doGroupWorkspaces("ws1, ws2", "group1");
    createUniformWorkspace(0.8, 0.1, 1.1, 2.1, true, "ws3");
    createUniformWorkspace(0.8, 0.1, 1.6, 2.6, true, "ws4");
    doGroupWorkspaces("ws3, ws4", "group2");
    createUniformWorkspace(1.6, 0.1, 1.5, 2.5, true, "ws5");
    createUniformWorkspace(1.6, 0.1, 1.6, 3.0, true, "ws6");
    doGroupWorkspaces("ws5, ws6", "group3");

    Stitch1DMany alg;
    alg.initialize();
    alg.setProperty("InputWorkspaces", "group1, group2, group3");
    alg.setProperty("Params", "0.1, 0.1, 2.6");
    alg.setProperty("StartOverlaps", "0.8, 1.6");
    alg.setProperty("EndOverlaps", "1.1, 1.9");
    alg.setProperty("UseManualScaleFactors", "1");
    alg.setProperty("ScaleFactorFromPeriod", 2);
    alg.setProperty("OutputWorkspace", "outws");
    TS_ASSERT(alg.isExecuted());
    Workspace_sptr outws = alg.getProperty("OutputWorkspace");
    TS_ASSERT(outws);
    auto group =
        AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>("outws");
        boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(0));
    // Test the algorithm histories
    std::vector<std::string> histNames;
    auto histories = stitched->history().getAlgorithmHistories();
    for (auto &hist : histories) {
      histNames.push_back(hist->name());
    const std::string createWsName = "CreateWorkspace";
    const std::string groupWsName = "GroupWorkspaces";

    TS_ASSERT_EQUALS(histNames[0], createWsName);
    TS_ASSERT_EQUALS(histNames[1], groupWsName);
    TS_ASSERT_EQUALS(histNames[2], createWsName);
    TS_ASSERT_EQUALS(histNames[3], groupWsName);
    TS_ASSERT_EQUALS(histNames[4], createWsName);
    TS_ASSERT_EQUALS(histNames[5], groupWsName);
    // Clear the ADS
    AnalysisDataService::Instance().clear();
  }

  void test_two_point_data_workspaces() {
    const auto &x1 = Mantid::HistogramData::Points({0.2, 0.9, 1.6});
    const auto &y1 = Mantid::HistogramData::Counts({56., 77., 48.});
    Mantid::HistogramData::Histogram histogram1(x1, y1);
    auto ws1 = boost::make_shared<Mantid::DataObjects::Workspace2D>();
    ws1->initialize(1, histogram1);
    const auto &x2 = Mantid::HistogramData::Points({0.23, 1.3, 2.6});
    const auto &y2 = Mantid::HistogramData::Counts({1.1, 2., 3.7});
    Mantid::HistogramData::Histogram histogram2(x2, y2);
    auto ws2 = boost::make_shared<Mantid::DataObjects::Workspace2D>();
    ws2->initialize(1, histogram2);
    Mantid::API::AnalysisDataService::Instance().addOrReplace("ws1", ws1);
    Mantid::API::AnalysisDataService::Instance().addOrReplace("ws2", ws2);
    //Stitch1DMany alg;
    //alg.initialize();
    //alg.setProperty("InputWorkspaces", "ws1, ws2");
    //alg.setProperty("UseManualScaleFactors", "1");
    //alg.setProperty("OutputWorkspace", "ws");
    //alg.execute();
    //TS_ASSERT(alg.isExecuted());
    Mantid::API::AnalysisDataService::Instance().remove("ws1");
    Mantid::API::AnalysisDataService::Instance().remove("ws2");
  }
};

#endif /* MANTID_ALGORITHMS_STITCH1DMANYTEST_H_ */