CombineDiffCal.cpp 7.11 KB
Newer Older
1
2
3
4
5
6
7
8
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +

#include "MantidAlgorithms/CombineDiffCal.h"
9
#include "MantidAPI/SpectrumInfo.h"
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "MantidAPI/TableRow.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"

namespace Mantid {
namespace Algorithms {
using Mantid::API::WorkspaceProperty;
using Mantid::Kernel::Direction;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CombineDiffCal)

//----------------------------------------------------------------------------------------------

/// Algorithms name for identification. @see Algorithm::name
const std::string CombineDiffCal::name() const { return "CombineDiffCal"; }

/// Algorithm's version for identification. @see Algorithm::version
int CombineDiffCal::version() const { return 1; }

/// Algorithm's category for identification. @see Algorithm::category
const std::string CombineDiffCal::category() const { return "Diffraction\\Utility"; }

/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string CombineDiffCal::summary() const {
  return "Combine a per-pixel calibration with a grouped spectrum calibration";
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
 */
void CombineDiffCal::init() {
  declareProperty(
      std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("PixelCalibration", "", Direction::Input),
      "OffsetsWorkspace generated from cross-correlation. This is the source of DIFCpixel.");
  declareProperty(
      std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("GroupedCalibration", "", Direction::Input),
      "DiffCal table generated from calibrating grouped spectra. This is the source of DIFCgroup.");
  declareProperty(
      std::make_unique<WorkspaceProperty<DataObjects::Workspace2D>>("CalibrationWorkspace", "", Direction::Input),
      "Workspace where conversion from d-spacing to time-of-flight for each spectrum is determined from. This is the "
      "source of DIFCarb.");
  declareProperty(
      std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("OutputWorkspace", "", Direction::Output),
      "DiffCal table generated from calibrating grouped spectra");
}

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
int getTableWorspaceSortDirection(DataObjects::TableWorkspace_sptr ws) {
  Mantid::API::TableRow row = ws->getFirstRow();
  int sortDirection = 1;
  if (ws->rowCount() > 2) {
    Mantid::API::TableRow lastRow = ws->getRow(ws->rowCount() - 1);
    if (row.Int(0) > lastRow.Int(0)) {
      sortDirection = -1;
    }
  }
  return sortDirection;
}

bool isTableWorkspaceSortedById(DataObjects::TableWorkspace_sptr ws) {
  Mantid::API::TableRow row = ws->getFirstRow();
  int sortDirection = getTableWorspaceSortDirection(ws);
  if (ws->rowCount() > 2) {
    row.next();
    Mantid::API::TableRow nextRow = row;
    row = ws->getFirstRow();
    do {
      if (row.Int(0) * sortDirection > nextRow.Int(0) * sortDirection) {
        return false;
      }
    } while (row.next() && nextRow.next());
  }
  return true;
}

85
86
87
std::map<std::string, std::string> CombineDiffCal::validateInputs() {
  std::map<std::string, std::string> results;

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  const DataObjects::TableWorkspace_sptr groupedCalibrationWS = getProperty("GroupedCalibration");
  const DataObjects::TableWorkspace_sptr pixelCalibrationWS = getProperty("PixelCalibration");

  if (!isTableWorkspaceSortedById(groupedCalibrationWS)) {
    results["GroupedCalibration"] = "Please run SortTableWorkspace on the table submited as 'GroupCalibration'. ";
  }

  if (!isTableWorkspaceSortedById(pixelCalibrationWS)) {
    results["PixelCalibration"] = "Please run SortTableWorkspace on the table submited as 'PixelCalibration'. ";
  }

  if (getTableWorspaceSortDirection(groupedCalibrationWS) != getTableWorspaceSortDirection(pixelCalibrationWS)) {
    results["GroupedCalibration"] = "'GroupedCalibration's sort direction does not match 'PixelCalibration's sort "
                                    "direction, flip one of their orders.";
    results["PixelCalibration"] = "'PixelCalibration's sort direction does not match 'GroupedCalibration's sort "
                                  "direction, flip one of their orders.";
  }

106
107
  return results;
}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

std::shared_ptr<Mantid::API::TableRow> binarySearchForRow(DataObjects::TableWorkspace_sptr ws, int detid) {
  size_t start = 0;
  size_t end = ws->rowCount();
  size_t currentPosition = end / 2;
  int sortDirection = getTableWorspaceSortDirection(ws);
  while (end - start > 0) {
    Mantid::API::TableRow currentRow = ws->getRow(currentPosition);
    if (currentRow.Int(0) * sortDirection > detid * sortDirection) {
      end = currentPosition;
    } else if (currentRow.Int(0) * sortDirection < detid * sortDirection) {
      start = currentPosition;
    } else {
      return std::make_shared<Mantid::API::TableRow>(currentRow);
    }
    currentPosition = start + ((end - start) / 2);
  }
  return nullptr;
}

128
129
130
131
132
133
134
135
// Per Pixel:
//
// DIFC{eff} = (DIFC{pd}/DIFC{arb}) * DIFC{prev}
//
// DIFC{eff} = Output of this Alg, the combined DIFC
// DIFC{pd} = The DIFC produced by PDCalibration, found in the "GroupedCalibration"
// DIFC{arb} = found in the "CalibrationWorkspace" param
// DIFC{prev} = The previous DIFCs, found in "PixelCalibration", as per description this was the set generated by CC
136
137
138
139
140

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
 */
void CombineDiffCal::exec() {
141
142
143
144
145
146
147
  const DataObjects::Workspace2D_sptr calibrationWS = getProperty("CalibrationWorkspace");
  const DataObjects::TableWorkspace_sptr groupedCalibrationWS = getProperty("GroupedCalibration");
  const DataObjects::TableWorkspace_sptr pixelCalibrationWS = getProperty("PixelCalibration");

  DataObjects::TableWorkspace_sptr outputWorkspace = std::make_shared<DataObjects::TableWorkspace>();
  outputWorkspace->addColumn("int", "detid");
  outputWorkspace->addColumn("double", "difc");
148
149
  outputWorkspace->addColumn("double", "difa");
  outputWorkspace->addColumn("double", "tzero");
150
151
152

  Mantid::API::TableRow groupedCalibrationRow = groupedCalibrationWS->getFirstRow();
  do {
153
154
155
156
157
158
159
160
161
162
163
164
    std::shared_ptr<Mantid::API::TableRow> pixelCalibrationRow =
        binarySearchForRow(pixelCalibrationWS, groupedCalibrationRow.Int(0));
    if (pixelCalibrationRow) {
      double value = (groupedCalibrationRow.Double(1) /
                      calibrationWS->spectrumInfo().diffractometerConstants(calibrationWS->getIndicesFromDetectorIDs(
                          {pixelCalibrationRow->Int(0)})[0])[Kernel::UnitParams::difc]) *
                     pixelCalibrationRow->Double(1);

      Mantid::API::TableRow newRow = outputWorkspace->appendRow();
      newRow << pixelCalibrationRow->Int(0) << value << 0.0 << 0.0;
    }
  } while (groupedCalibrationRow.next());
165
166

  setProperty("OutputWorkspace", outputWorkspace);
167
168
169
170
}

} // namespace Algorithms
} // namespace Mantid