CombineDiffCal.cpp 8.75 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
#include "MantidAPI/TableRow.h"
11
#include "MantidDataObjects/MaskWorkspace.h"
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
#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(
50
      std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("CalibrationWorkspace", "", Direction::Input),
51
52
53
54
55
      "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");
56
57
58
  declareProperty(std::make_unique<WorkspaceProperty<DataObjects::MaskWorkspace>>("MaskWorkspace", "", Direction::Input,
                                                                                  API::PropertyMode::Optional),
                  "MaskedWorkspace for PixelCalibration");
59
60
}

61
62
63
64
65
66
67
68
69
70
71
72
/// sort the calibration table according increasing values in column "detid"
API::ITableWorkspace_sptr CombineDiffCal::sortTableWorkspace(DataObjects::TableWorkspace_sptr &table) {
  auto alg = createChildAlgorithm("SortTableWorkspace");
  alg->setLoggingOffset(1);
  alg->setProperty("InputWorkspace", table);
  alg->setProperty("OutputWorkspace", table);
  alg->setProperty("Columns", "detid");
  alg->executeAsChildAlg();

  return alg->getProperty("OutputWorkspace");
}

Walsh, Michael's avatar
Walsh, Michael committed
73
bool findColumn(const std::vector<std::string> &columnNames, const std::string &name) {
74
75
76
  return std::find(columnNames.begin(), columnNames.end(), name) != columnNames.end();
}

77
int isMissingDifcColumns(const DataObjects::TableWorkspace_sptr ws) {
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  const std::vector<std::string> columnNames = ws->getColumnNames();

  // 0x0000 is success
  // 0x???1 detid is missing
  // 0x??1? difc is missing
  // 0x?1?? difa is missing
  // 0x1??? tzero is missing
  int result = 0;

  result = result | (findColumn(columnNames, "detid") ? 0x0000 : 0x0001) |
           (findColumn(columnNames, "difc") ? 0x0000 : 0x0010) | (findColumn(columnNames, "difa") ? 0x0000 : 0x0100) |
           (findColumn(columnNames, "tzero") ? 0x0000 : 0x1000);

  return result;
}

std::string getMissingColumnString(const int flags) {
  std::string missingColumns = "[";
  if (flags & 0x0001) {
    missingColumns.append(" detid ");
  }
  if (flags & 0x0010) {
    missingColumns.append(" difc ");
  }
  if (flags & 0x0100) {
    missingColumns.append(" difa ");
  }
  if (flags & 0x1000) {
    missingColumns.append(" tzero ");
  }
  missingColumns.append("]");
  return missingColumns;
}

112
113
114
std::map<std::string, std::string> CombineDiffCal::validateInputs() {
  std::map<std::string, std::string> results;

115
116
117
  const DataObjects::TableWorkspace_sptr groupedCalibrationWS = getProperty("GroupedCalibration");
  const DataObjects::TableWorkspace_sptr pixelCalibrationWS = getProperty("PixelCalibration");

118
119
  const int groupedResult = isMissingDifcColumns(groupedCalibrationWS);
  if (groupedResult)
120
121
122
    results["GroupedCalibration"] =
        "The GroupedCalibration Workspace is missing " + getMissingColumnString(groupedResult);

123
124
  const int pixelResult = isMissingDifcColumns(pixelCalibrationWS);
  if (pixelResult)
125
126
    results["PixelCalibration"] = "The PixelCalibration Workspace is missing " + getMissingColumnString(pixelResult);

127
128
  return results;
}
129

130
std::shared_ptr<Mantid::API::TableRow> binarySearchForRow(API::ITableWorkspace_sptr ws, int detid) {
131
  size_t start = 0;
132
133
134
135
  size_t end = ws->rowCount() - 1;
  while (end >= start) {
    size_t currentPosition = start + ((end - start) / 2);

136
    Mantid::API::TableRow currentRow = ws->getRow(currentPosition);
137
138
139
140
141
142

    currentRow = ws->getRow(currentPosition);
    if (currentRow.Int(0) > detid) {
      end = currentPosition - 1;
    } else if (currentRow.Int(0) < detid) {
      start = currentPosition + 1;
143
144
145
146
147
148
149
    } else {
      return std::make_shared<Mantid::API::TableRow>(currentRow);
    }
  }
  return nullptr;
}

150
151
152
153
154
void addRowFromGroupedCalibration(DataObjects::TableWorkspace_sptr ws, Mantid::API::TableRow row) {
  Mantid::API::TableRow newRow = ws->appendRow();
  newRow << row.Int(0) << row.Double(1) << row.Double(2) << row.Double(3);
}

155
156
157
158
159
160
161
162
// 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
163
164
165
166
167

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
 */
void CombineDiffCal::exec() {
168
  const API::MatrixWorkspace_sptr calibrationWS = getProperty("CalibrationWorkspace");
169
170
171
172
173
174
175

  DataObjects::TableWorkspace_sptr presortedGroupedCalibrationWS = getProperty("GroupedCalibration");
  const API::ITableWorkspace_sptr groupedCalibrationWS = sortTableWorkspace(presortedGroupedCalibrationWS);

  DataObjects::TableWorkspace_sptr presortedPixelCalibrationWS = getProperty("PixelCalibration");
  const API::ITableWorkspace_sptr pixelCalibrationWS = sortTableWorkspace(presortedPixelCalibrationWS);

176
  const DataObjects::MaskWorkspace_sptr maskWorkspace = getProperty("MaskWorkspace");
177
178
179
180

  DataObjects::TableWorkspace_sptr outputWorkspace = std::make_shared<DataObjects::TableWorkspace>();
  outputWorkspace->addColumn("int", "detid");
  outputWorkspace->addColumn("double", "difc");
181
182
  outputWorkspace->addColumn("double", "difa");
  outputWorkspace->addColumn("double", "tzero");
183
184
185

  Mantid::API::TableRow groupedCalibrationRow = groupedCalibrationWS->getFirstRow();
  do {
186
187
188
    int detid = groupedCalibrationRow.Int(0);
    bool prevDifValsExist = false;

189
    if (!(maskWorkspace && maskWorkspace->isMasked(detid))) {
190
191
192
193
194
195
196
197
      std::shared_ptr<Mantid::API::TableRow> pixelCalibrationRow = binarySearchForRow(pixelCalibrationWS, detid);
      if (pixelCalibrationRow) {
        double difcPD = groupedCalibrationRow.Double(1);
        double difcArb = calibrationWS->spectrumInfo().diffractometerConstants(
            calibrationWS->getIndicesFromDetectorIDs({detid})[0])[Kernel::UnitParams::difc];
        double difcPrev = pixelCalibrationRow->Double(1);
        double difaPrev = pixelCalibrationRow->Double(2);

198
199
        double difcNew = (difcPD / difcArb) * difcPrev;
        double difaNew = ((difcPD / difcArb) * (difcPD / difcArb)) * difaPrev;
200

201
        double tzeroNew = pixelCalibrationRow->Double(3);
202
203

        Mantid::API::TableRow newRow = outputWorkspace->appendRow();
204
        newRow << detid << difcNew << difaNew << tzeroNew;
205
206
207
208
209
210
211
        prevDifValsExist = true;
      }
    }

    if (!prevDifValsExist) {
      // copy from group
      addRowFromGroupedCalibration(outputWorkspace, groupedCalibrationRow);
212
213
    }
  } while (groupedCalibrationRow.next());
214
215

  setProperty("OutputWorkspace", outputWorkspace);
216
217
218
219
}

} // namespace Algorithms
} // namespace Mantid