From f54a855c46ec99a2eef131800f725a0a9d2c50e9 Mon Sep 17 00:00:00 2001
From: Jose Borreguero <borreguero@gmail.com>
Date: Tue, 11 Feb 2020 21:02:50 -0500
Subject: [PATCH] Refs #27876 unit test with RAW file

Signed-off-by: Jose Borreguero <borreguero@gmail.com>
---
 .../Algorithms/test/ApplyCalibrationTest.h    | 71 +++++++++++++++++++
 Testing/Data/UnitTest/HRP39180.RAW.md5        |  1 +
 2 files changed, 72 insertions(+)
 create mode 100644 Testing/Data/UnitTest/HRP39180.RAW.md5

diff --git a/Framework/Algorithms/test/ApplyCalibrationTest.h b/Framework/Algorithms/test/ApplyCalibrationTest.h
index 3535f005c67..d19257d2d2f 100644
--- a/Framework/Algorithms/test/ApplyCalibrationTest.h
+++ b/Framework/Algorithms/test/ApplyCalibrationTest.h
@@ -19,10 +19,12 @@
 #include "MantidAlgorithms/ApplyCalibration.h"
 #include "MantidDataHandling/LoadEmptyInstrument.h"
 #include "MantidDataHandling/LoadInstrument.h"
+#include "MantidDataHandling/LoadRaw3.h"
 #include "MantidDataObjects/Workspace2D.h"
 #include "MantidGeometry/Instrument.h"
 #include "MantidGeometry/Instrument/Component.h"
 #include "MantidGeometry/Instrument/ComponentInfo.h"
+#include "MantidGeometry/Instrument/DetectorInfo.h"
 #include "MantidKernel/V3D.h"
 #include "MantidTestHelpers/WorkspaceCreationHelper.h"
 
@@ -104,6 +106,75 @@ public:
     dataStore.remove(wsName);
   }
 
+  /**
+   * Load a *.raw file and reset the detector position, width, and height for the first two spectra
+   */
+  void testCalibrateRawFile() {
+    // Create a calibration table
+    ITableWorkspace_sptr calTableWs =
+            WorkspaceFactory::Instance().createTable();
+    calTableWs->addColumn("int", "Detector ID");
+    calTableWs->addColumn("V3D", "Detector Position");
+    calTableWs->addColumn("double", "Detector Y Coordinate");
+    calTableWs->addColumn("double", "Detector Width");
+    calTableWs->addColumn("double", "Detector Height");
+
+    // Load the first two spectra from a *.raw data file into a workspace
+    const int nSpectra = 2;
+    const std::string wsName("applyCalibrationToRaw");
+    Mantid::DataHandling::LoadRaw3 loader;
+    loader.initialize();
+    loader.setPropertyValue("Filename", "HRP39180.RAW");
+    loader.setPropertyValue("OutputWorkspace", wsName);
+    loader.setPropertyValue("SpectrumMin", "1");  // Spectrum number, not workspace index
+    loader.setPropertyValue("SpectrumMax", "9");//std::to_string(nSpectra));
+    loader.execute();
+    AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance();
+    MatrixWorkspace_sptr workspace = dataStore.retrieveWS<MatrixWorkspace>(wsName);
+    const auto &detectorInfo = workspace->detectorInfo();
+    const auto &componentInfo = workspace->componentInfo();
+
+    // Populate the calibration table with some final detector positions, widths, and heights
+    const std::vector<V3D> positions{V3D(0.20, 0.0, 0.42), V3D(0.53, 0.0, 0.75)};
+    const std::vector<double> yCoords{0.31, 0.64};
+    const std::vector<double> widths{0.008, 0.007};
+    const std::vector<double> heights{0.041, 0.039};
+    for(size_t i=0; i < nSpectra; i++) {
+      IDetector_const_sptr detector = workspace->getDetector(i);
+      const auto detectorID = detector->getID();
+      TableRow row = calTableWs->appendRow();
+      // insert data in the same order in which table columns were declared
+      // detector-ID  position  Y-coordinate  Width Height
+      row << detectorID << positions[i] << yCoords[i] << widths[i] << heights[i];
+    }
+
+    // Apply the calibration to the workspace
+    ApplyCalibration calibrationAlgorithm;
+    calibrationAlgorithm.initialize();
+    TS_ASSERT(calibrationAlgorithm.isInitialized())
+    TS_ASSERT_THROWS_NOTHING(calibrationAlgorithm.setPropertyValue("Workspace", wsName));
+    TS_ASSERT_THROWS_NOTHING(calibrationAlgorithm.setProperty<ITableWorkspace_sptr>("CalibrationTable", calTableWs));
+    TS_ASSERT_THROWS_NOTHING(calibrationAlgorithm.execute());
+    TS_ASSERT(calibrationAlgorithm.isExecuted());
+
+    // Assert the calibration
+    for(size_t i=0; i < nSpectra; i++) {
+      // assert detector position
+      IDetector_const_sptr detector = workspace->getDetector(i);
+      const auto detectorID = detector->getID();
+      const auto &newPosition = detector->getPos();
+      TS_ASSERT_DELTA(newPosition.X(), positions[i].X(), 0.0001);
+      TS_ASSERT_DELTA(newPosition.Y(), yCoords[i], 0.0001);
+      TS_ASSERT_DELTA(newPosition.Z(), positions[i].Z(), 0.0001);
+      // assert detector width and height
+      const auto detectorIndex = detectorInfo.indexOf(detectorID);
+      const auto &scaleFactor = componentInfo.scaleFactor(detectorIndex);
+      const auto &box = componentInfo.shape(detectorIndex).getBoundingBox().width();
+      TS_ASSERT_DELTA(scaleFactor.X() * box.X(), widths[i], 0.0001);
+      TS_ASSERT_DELTA(scaleFactor.Y() * box.Y(), heights[i], 0.0001);
+    }
+  }
+
   void testComplex() {
     /* The purpse of this test is to test the algorithm when the relative
      * positioning and rotation
diff --git a/Testing/Data/UnitTest/HRP39180.RAW.md5 b/Testing/Data/UnitTest/HRP39180.RAW.md5
new file mode 100644
index 00000000000..f92d2df2e7f
--- /dev/null
+++ b/Testing/Data/UnitTest/HRP39180.RAW.md5
@@ -0,0 +1 @@
+7d19937fbfb68e962c190454a128fde6
-- 
GitLab