27#include "MantidHistogramData/Histogram.h"
40#include <gsl/gsl_multifit_nlin.h>
41#include <gsl/gsl_multimin.h>
70const auto isNonZero = [](
const double value) {
return value != 0.; };
89 const auto &spectrum = wksp->getSpectrum(
wkspIndex);
90 this->
detid = spectrum.getDetectorIDs();
92 const auto &
X = spectrum.x();
93 const auto &
Y = spectrum.y();
99 for (; minIndex <
Y.size(); ++minIndex) {
100 if (isNonZero(
Y[minIndex])) {
107 size_t maxIndex =
Y.size() - 1;
108 for (; maxIndex > minIndex; --maxIndex) {
109 if (isNonZero(
Y[maxIndex])) {
126 void setPositions(
const std::vector<double> &peaksInD,
const std::vector<double> &peaksInDWindows,
const double difa,
134 inDPos.assign(peaksInD.begin(), peaksInD.end());
135 inTofPos.assign(peaksInD.begin(), peaksInD.end());
136 inTofWindows.assign(peaksInDWindows.begin(), peaksInDWindows.end());
140 std::vector<double> yunused;
182 return "Calibrate the detector pixels and create a calibration table";
190 "Input signal workspace");
193 "Min, Step, and Max of time-of-flight bins. "
194 "Logarithmic binning is used if Step is negative.");
196 const std::vector<std::string> exts2{
".h5",
".cal"};
198 "Previous calibration file");
201 "Previous calibration table. This overrides results from previous file.");
204 std::vector<std::string> peaktypes{
"BackToBackExponential",
"Gaussian",
"Lorentzian",
"PseudoVoigt",
206 declareProperty(
"PeakFunction",
"Gaussian", std::make_shared<StringListValidator>(peaktypes));
207 vector<std::string> bkgdtypes{
"Flat",
"Linear",
"Quadratic"};
208 declareProperty(
"BackgroundType",
"Linear", std::make_shared<StringListValidator>(bkgdtypes),
"Type of Background.");
210 auto peaksValidator = std::make_shared<CompositeValidator>();
211 auto mustBePosArr = std::make_shared<Kernel::ArrayBoundedValidator<double>>();
212 mustBePosArr->setLower(0.0);
213 peaksValidator->add(mustBePosArr);
216 "Comma delimited d-space positions of reference peaks.");
218 auto windowValidator = std::make_shared<CompositeValidator>();
219 windowValidator->add(mustBePosArr);
220 auto lengthValidator = std::make_shared<Kernel::ArrayLengthValidator<double>>();
221 lengthValidator->setLengthMin(1);
222 windowValidator->add(lengthValidator);
225 "Window over which to fit a peak in d-spacing (if a single value is supplied it will used as half "
226 "the window width for all peaks, otherwise a comma delimited list of boundaries).");
228 auto min = std::make_shared<BoundedValidator<double>>();
231 "The estimated peak width as a percent of the peak"
232 "center value, in d-spacing or TOF units.");
235 "Minimum peak height such that all the fitted peaks with "
236 "height under this value will be excluded.");
238 declareProperty(
"MaxChiSq", 100.,
"Maximum chisq value for individual peak fit allowed. (Default: 100)");
241 "If true, peak centers will be constrained by estimated positions "
242 "(highest Y value position) and "
243 "the peak width will either be estimated by observation or calculated.");
245 std::vector<std::string> modes{
"DIFC",
"DIFC+TZERO",
"DIFC+TZERO+DIFA"};
246 declareProperty(
"CalibrationParameters",
"DIFC", std::make_shared<StringListValidator>(modes),
247 "Select calibration parameters to fit.");
250 "Range for allowable TZERO from calibration (default is all)");
253 "Range for allowable DIFA from calibration (default "
257 "By default the square of the peak height is used as weights "
258 "in the least-squares fit to find the diffractometer "
259 "constants, if UseChiSq is true then the inverse square of "
260 "the error on the fitted peak centres will be used instead.");
264 "Output table workspace containing the calibration");
268 "Workspaces to promote understanding of calibration results");
271 std::string inputGroup(
"Input Options");
277 std::string funcgroup(
"Function Types");
282 std::string fitPeaksGroup(
"Peak Fitting");
291 std::string calGroup(
"Calibration Type");
299 std::map<std::string, std::string> messages;
301 vector<double> tzeroRange =
getProperty(
"TZEROrange");
302 if (!tzeroRange.empty()) {
303 if (tzeroRange.size() != 2) {
304 messages[
"TZEROrange"] =
"Require two values [min,max]";
305 }
else if (tzeroRange[0] >= tzeroRange[1]) {
306 messages[
"TZEROrange"] =
"min must be less than max";
310 vector<double> difaRange =
getProperty(
"DIFArange");
311 if (!difaRange.empty()) {
312 if (difaRange.size() != 2) {
313 messages[
"DIFArange"] =
"Require two values [min,max]";
314 }
else if (difaRange[0] >= difaRange[1]) {
315 messages[
"DIFArange"] =
"min must be less than max";
319 vector<double> peakWindow =
getProperty(
"PeakWindow");
320 vector<double> peakCentres =
getProperty(
"PeakPositions");
321 if (peakWindow.size() > 1 && peakWindow.size() != 2 * peakCentres.size()) {
322 messages[
"PeakWindow"] =
"PeakWindow must be a vector with either 1 element (interpreted as half the width of "
323 "the window) or twice the number of peak centres provided.";
333 const auto columnNames = table->getColumnNames();
334 return (std::find(columnNames.begin(), columnNames.end(), std::string(
"dasid")) != columnNames.end());
344double getWidthToFWHM(
const std::string &peakshape) {
346 if (peakshape ==
"Gaussian") {
347 return 2 * std::sqrt(2. * std::log(2.));
348 }
else if (peakshape ==
"Lorentzian") {
350 }
else if (peakshape ==
"BackToBackExponential") {
363 vector<double> tofBinningParams =
getProperty(
"TofBinning");
364 m_tofMin = tofBinningParams.front();
367 vector<double> tzeroRange =
getProperty(
"TZEROrange");
368 if (tzeroRange.size() == 2) {
372 std::stringstream msg;
373 msg <<
"Using tzero range of " <<
m_tzeroMin <<
" <= "
379 m_tzeroMin = std::numeric_limits<double>::lowest();
380 m_tzeroMax = std::numeric_limits<double>::max();
383 vector<double> difaRange =
getProperty(
"DIFArange");
384 if (difaRange.size() == 2) {
388 std::stringstream msg;
389 msg <<
"Using difa range of " <<
m_difaMin <<
" <= "
395 m_difaMin = std::numeric_limits<double>::lowest();
396 m_difaMax = std::numeric_limits<double>::max();
403 const std::vector<double> peakWindow =
getProperty(
"PeakWindow");
404 const double minPeakHeight =
getProperty(
"MinimumPeakHeight");
405 const double maxChiSquared =
getProperty(
"MaxChiSq");
408 if (calParams == std::string(
"DIFC"))
410 else if (calParams == std::string(
"DIFC+TZERO"))
412 else if (calParams == std::string(
"DIFC+TZERO+DIFA"))
415 throw std::runtime_error(
"Encountered impossible CalibrationParameters value");
420 auto uncalibratedEWS = std::dynamic_pointer_cast<EventWorkspace>(
m_uncalibratedWS);
421 auto isEvent = bool(uncalibratedEWS);
424 if ((!
static_cast<std::string
>(
getProperty(
"PreviousCalibrationFile")).empty()) ||
434 maskWSName +=
"_mask";
436 "An output workspace containing the mask");
439 for (
size_t i = 0; i < maskWS->getNumberHistograms(); ++i)
440 maskWS->setMaskedIndex(i,
true);
443 const std::string peakFunction =
getProperty(
"PeakFunction");
444 const double WIDTH_TO_FWHM = getWidthToFWHM(peakFunction);
445 if (WIDTH_TO_FWHM == 1.) {
446 g_log.
notice() <<
"Unknown conversion for \"" << peakFunction
447 <<
"\", found peak widths and resolution should not be "
448 "directly compared to delta-d/d";
459 double peak_width_percent =
getProperty(
"PeakWidthPercent");
461 const std::string diagnostic_prefix =
getPropertyValue(
"DiagnosticWorkspaces");
466 algFitPeaks->setLoggingOffset(3);
470 algFitPeaks->setProperty(
"PeakCentersWorkspace", tof_peak_center_ws);
473 algFitPeaks->setProperty<std::string>(
"PeakFunction", peakFunction);
474 algFitPeaks->setProperty<std::string>(
"BackgroundType",
getProperty(
"BackgroundType"));
476 algFitPeaks->setProperty(
"FitPeakWindowWorkspace", tof_peak_window_ws);
477 algFitPeaks->setProperty(
"PeakWidthPercent", peak_width_percent);
478 algFitPeaks->setProperty(
"MinimumPeakHeight", minPeakHeight);
480 algFitPeaks->setProperty(
"FitFromRight",
true);
481 algFitPeaks->setProperty(
"HighBackground",
false);
482 bool constrainPeakPosition =
getProperty(
"ConstrainPeakPositions");
483 algFitPeaks->setProperty(
"ConstrainPeakPositions",
484 constrainPeakPosition);
486 algFitPeaks->setProperty(
"Minimizer",
"Levenberg-Marquardt");
487 algFitPeaks->setProperty(
"CostFunction",
"Least squares");
492 algFitPeaks->setProperty(
"RawPeakParameters", useChiSq);
503 algFitPeaks->setPropertyValue(
"OutputPeakParametersWorkspace", diagnostic_prefix +
"_fitparam");
508 algFitPeaks->setPropertyValue(
"FittedPeaksWorkspace", diagnostic_prefix +
"_fitted");
510 algFitPeaks->setPropertyValue(
"OutputParameterFitErrorsWorkspace", diagnostic_prefix +
"_fiterrors");
514 algFitPeaks->executeAsChildAlg();
522 errorTable = algFitPeaks->getProperty(
"OutputParameterFitErrorsWorkspace");
527 throw std::runtime_error(
"FitPeaks does not have output OutputPeakParametersWorkspace.");
529 throw std::runtime_error(
"The number of rows in OutputPeakParametersWorkspace is not correct!");
548 for (
int wkspIndex = 0; wkspIndex < NUMHIST; ++wkspIndex) {
550 if ((isEvent && uncalibratedEWS->getSpectrum(wkspIndex).empty()) || !spectrumInfo.hasDetectors(wkspIndex) ||
551 spectrumInfo.isMonitor(wkspIndex)) {
559 const auto [dif_c, dif_a,
tzero] =
568 std::vector<double> tof_vec_full(numPeaks, std::nan(
""));
569 std::vector<double> d_vec;
570 std::vector<double> tof_vec;
572 std::vector<double> width_vec_full(numPeaks, std::nan(
""));
574 std::vector<double> height_vec_full(numPeaks, std::nan(
""));
575 std::vector<double> weights;
577 const size_t rowNumInFitTableOffset = wkspIndex * numPeaks;
580 for (
size_t peakIndex = 0; peakIndex < numPeaks; ++peakIndex) {
581 size_t rowIndexInFitTable = rowNumInFitTableOffset + peakIndex;
584 if (fittedTable->getRef<
int>(
"wsindex", rowIndexInFitTable) != wkspIndex)
585 throw std::runtime_error(
"workspace index mismatch!");
586 if (fittedTable->getRef<
int>(
"peakindex", rowIndexInFitTable) !=
static_cast<int>(peakIndex))
587 throw std::runtime_error(
"peak index mismatch but workspace index matched");
589 const double chi2 = fittedTable->getRef<
double>(
"chi2", rowIndexInFitTable);
591 double centre_error = 0.0;
596 centre = fittedTable->getRef<
double>(
"centre", rowIndexInFitTable);
597 width = fittedTable->getRef<
double>(
"width", rowIndexInFitTable);
598 height = fittedTable->getRef<
double>(
"height", rowIndexInFitTable);
602 auto peakfunc = std::dynamic_pointer_cast<API::IPeakFunction>(
605 for (
size_t ipar = 0; ipar < peakfunc->nParams(); ipar++) {
606 peakfunc->setParameter(ipar, fittedTable->getRef<
double>(peakfunc->parameterName(ipar), rowIndexInFitTable));
608 centre = peakfunc->centre();
609 width = peakfunc->fwhm();
610 height = peakfunc->height();
611 centre_error = errorTable->getRef<
double>(peakfunc->getCentreParameterName(), rowIndexInFitTable);
615 if (chi2 > maxChiSquared || chi2 < 0.) {
626 if (
height < minPeakHeight + 1.E-15) {
632 tof_vec.emplace_back(centre);
636 weights.emplace_back(1 / (centre_error * centre_error));
638 tof_vec_full[peakIndex] = centre;
639 width_vec_full[peakIndex] = width;
640 height_vec_full[peakIndex] =
height;
644 maskWS->setMasked(peaks.
detid, d_vec.size() < 2);
646 if (d_vec.size() < 2) {
652 double difc = 0., t0 = 0.,
difa = 0.;
654 for (
auto iter = peaks.
detid.begin(); iter != peaks.
detid.end(); ++iter) {
666 for (std::size_t i = 0; i < numPeaks; ++i) {
667 if (std::isnan(tof_vec_full[i]))
671 const double dspacing = dSpacingUnit.
singleFromTOF(tof_vec_full[i]);
675 chisq += (temp * temp);
678 WIDTH_TO_FWHM * (width_vec_full[i] / (2 *
difa * dspacing +
difc));
679 m_peakHeightTable->cell<
double>(rowIndexOutputPeaks, i + 1) = height_vec_full[i];
683 chisq /
static_cast<double>(numPeaks - 1);
707 auto diagnosticGroup = std::make_shared<API::WorkspaceGroup>();
710 diagnosticGroup->addWorkspace(fittedTable);
712 diagnosticGroup->addWorkspace(calculatedWS);
715 diagnosticGroup->addWorkspace(errorTable);
726 diagnosticGroup->addWorkspace(resolutionWksp);
727 setProperty(
"DiagnosticWorkspaces", diagnosticGroup);
742 const std::vector<double> *peakVec =
reinterpret_cast<std::vector<double> *
>(peaks);
744 const auto numPeaks =
static_cast<size_t>(peakVec->at(0));
746 const auto numParams =
static_cast<size_t>(peakVec->at(1));
749 const std::vector<double> tofObs(peakVec->begin() + 2, peakVec->begin() + 2 + numPeaks);
750 const std::vector<double> dspace(peakVec->begin() + (2 + numPeaks), peakVec->begin() + (2 + 2 * numPeaks));
751 const std::vector<double> weights(peakVec->begin() + (2 + 2 * numPeaks), peakVec->begin() + (2 + 3 * numPeaks));
754 double difc = gsl_vector_get(v, 0);
758 tzero = gsl_vector_get(v, 1);
760 difa = gsl_vector_get(v, 2);
770 for (
size_t i = 0; i < numPeaks; ++i) {
771 const double tofCalib = dSpacingUnit.
singleToTOF(dspace[i]);
772 const double errsum_i = std::pow(tofObs[i] - tofCalib, 2) * weights[i];
797double fitDIFCtZeroDIFA(std::vector<double> &peaks,
double &
difc,
double &t0,
double &
difa) {
798 const auto numParams =
static_cast<size_t>(peaks[1]);
801 gsl_vector *fitParams = gsl_vector_alloc(numParams);
802 gsl_vector_set_all(fitParams, 0.0);
803 gsl_vector_set(fitParams, 0,
difc);
805 gsl_vector_set(fitParams, 1, t0);
807 gsl_vector_set(fitParams, 2,
difa);
812 gsl_vector *stepSizes = gsl_vector_alloc(numParams);
813 gsl_vector_set_all(stepSizes, 0.1);
814 gsl_vector_set(stepSizes, 0, 0.01);
817 gsl_multimin_function minex_func;
818 minex_func.n = numParams;
820 minex_func.params = &peaks;
823 const gsl_multimin_fminimizer_type *minimizerType = gsl_multimin_fminimizer_nmsimplex2;
824 gsl_multimin_fminimizer *minimizer = gsl_multimin_fminimizer_alloc(minimizerType, numParams);
825 gsl_multimin_fminimizer_set(minimizer, &minex_func, fitParams, stepSizes);
829 const size_t MAX_ITER = 75 * numParams;
833 status = gsl_multimin_fminimizer_iterate(minimizer);
837 double size = gsl_multimin_fminimizer_size(minimizer);
838 status = gsl_multimin_test_size(size, 1e-4);
840 }
while (status == GSL_CONTINUE && iter < MAX_ITER);
844 std::string status_msg = gsl_strerror(status);
845 if (status_msg ==
"success") {
846 difc = gsl_vector_get(minimizer->x, 0);
848 t0 = gsl_vector_get(minimizer->x, 1);
850 difa = gsl_vector_get(minimizer->x, 2);
854 errsum = minimizer->fval;
858 gsl_vector_free(fitParams);
859 gsl_vector_free(stepSizes);
860 gsl_multimin_fminimizer_free(minimizer);
881 const std::vector<double> &weights,
double &
difc,
double &t0,
double &
difa) {
882 const size_t numPeaks =
d.size();
895 std::vector<double> peaks(numPeaks * 3 + 2, 0.);
896 peaks[0] =
static_cast<double>(
d.size());
898 for (
size_t i = 0; i < numPeaks; ++i) {
899 peaks[i + 2] = tof[i];
900 peaks[i + 2 + numPeaks] =
d[i];
901 peaks[i + 2 + 2 * numPeaks] = weights[i];
905 double difc_start =
difc;
906 if (difc_start == 0.) {
907 const double d_sum = std::accumulate(
d.begin(),
d.end(), 0.);
908 const double tof_sum = std::accumulate(tof.begin(), tof.end(), 0.);
909 difc_start = tof_sum / d_sum;
913 double best_errsum = std::numeric_limits<double>::max();
914 double best_difc = difc_start;
916 double best_difa = 0.;
923 for (
size_t numParams = 1; numParams <= maxParams; ++numParams) {
924 peaks[1] =
static_cast<double>(numParams);
925 double difc_local = best_difc;
926 double t0_local = best_t0;
927 double difa_local = best_difa;
928 double errsum = fitDIFCtZeroDIFA(peaks, difc_local, t0_local, difa_local);
931 errsum = errsum /
static_cast<double>(numPeaks - numParams);
935 if (errsum < best_errsum) {
940 best_errsum = errsum;
941 best_difc = difc_local;
943 best_difa = difa_local;
950 if (best_difc > 0. && best_errsum < std::numeric_limits<double>::max()) {
966 const std::vector<double> &windows_in) {
968 const std::size_t numPeaks = centres.size();
971 assert(numPeaks >= 2);
973 vector<double> windows_out(2 * numPeaks);
976 for (std::size_t i = 0; i < numPeaks; ++i) {
977 if (windows_in.size() == 1) {
978 left = centres[i] - windows_in[0];
979 right = centres[i] + windows_in[0];
981 left = windows_in[2 * i];
982 right = windows_in[2 * i + 1];
986 left = std::max(
left, centres[i] - 0.5 * (centres[i] - centres[i - 1]));
988 if (i < numPeaks - 1) {
989 right = std::min(
right, centres[i] + 0.5 * (centres[i + 1] - centres[i]));
992 windows_out[2 * i] =
left;
993 windows_out[2 * i + 1] =
right;
1010 for (
auto detId : detIds) {
1016 if (detIds.size() > 1) {
1017 double norm = 1. /
static_cast<double>(detIds.size());
1027 const double tzero) {
1036 size_t hasDasIdsOffset = 0;
1054 vector<double> tofminmax(2);
1064 const double maxChunkSize =
getProperty(
"MaxChunkSize");
1065 const double filterBadPulses =
getProperty(
"FilterBadPulses");
1068 alg->setLoggingOffset(1);
1069 alg->setProperty(
"Filename", filename);
1070 alg->setProperty(
"MaxChunkSize", maxChunkSize);
1071 alg->setProperty(
"FilterByTofMin",
m_tofMin);
1072 alg->setProperty(
"FilterByTofMax",
m_tofMax);
1073 alg->setProperty(
"FilterBadPulses", filterBadPulses);
1074 alg->setProperty(
"LoadMonitors",
false);
1075 alg->executeAsChildAlg();
1078 return std::dynamic_pointer_cast<MatrixWorkspace>(
workspace);
1090 rebin->setLoggingOffset(1);
1091 rebin->setProperty(
"InputWorkspace", wksp);
1092 rebin->setProperty(
"OutputWorkspace", wksp);
1094 rebin->setProperty(
"PreserveEvents",
true);
1095 rebin->executeAsChildAlg();
1096 wksp =
rebin->getProperty(
"OutputWorkspace");
1116 if (calibrationTableOld ==
nullptr) {
1118 std::string filename =
getProperty(
"PreviousCalibrationFile");
1120 alg->setLoggingOffset(1);
1121 alg->setProperty(
"Filename", filename);
1122 alg->setProperty(
"WorkspaceName",
"NOMold");
1123 alg->setProperty(
"MakeGroupingWorkspace",
false);
1124 alg->setProperty(
"MakeMaskWorkspace",
false);
1125 alg->setProperty(
"TofMin",
m_tofMin);
1126 alg->setProperty(
"TofMax",
m_tofMax);
1127 alg->executeAsChildAlg();
1128 calibrationTableOld = alg->getProperty(
"OutputCalWorkspace");
1135 const size_t numDets = detIDs.
size();
1136 for (
size_t i = 0; i < numDets; ++i) {
1153 for (std::size_t rowNum = 0; rowNum < calibrationTableOld->rowCount(); ++rowNum) {
1156 newRow << calibrationTableOld->getRef<
int>(
"detid", rowNum);
1157 newRow << calibrationTableOld->getRef<
double>(
"difc", rowNum);
1158 newRow << calibrationTableOld->getRef<
double>(
"difa", rowNum);
1159 newRow << calibrationTableOld->getRef<
double>(
"tzero", rowNum);
1161 newRow << calibrationTableOld->getRef<
int>(
"dasid", rowNum);
1164 const auto tofMinMax =
getTOFminmax(calibrationTableOld->getRef<
double>(
"difc", rowNum),
1165 calibrationTableOld->getRef<
double>(
"difa", rowNum),
1166 calibrationTableOld->getRef<
double>(
"tzero", rowNum));
1167 newRow << tofMinMax[0];
1168 newRow << tofMinMax[1];
1182 alg->setLoggingOffset(1);
1184 alg->executeAsChildAlg();
1199 const detid2index_map allDetectors = difcWS->getDetectorIDToWorkspaceIndexMap(
false);
1202 auto it = allDetectors.begin();
1204 for (; it != allDetectors.end(); ++it) {
1205 const detid_t detID = it->first;
1207 const size_t wi = it->second;
1210 newRow << difcWS->y(wi)[0];
1234 std::stringstream namess;
1235 namess <<
"@" << std::setprecision(5) << dSpacing;
1247 detIds[it.second] = it.first;
1251 for (
const auto &detId : detIds) {
1257 newWidthRow << detId;
1258 newHeightRow << detId;
1262 newPosRow << std::nan(
"");
1263 newWidthRow << std::nan(
"");
1264 newHeightRow << std::nan(
"");
1271 std::make_shared<DataObjects::SpecialWorkspace2D>(
m_uncalibratedWS->getInstrument());
1272 resolutionWksp->setTitle(
"average width/height");
1280 std::vector<double> resolution;
1281 for (
size_t rowIndex = 0; rowIndex < numRows; ++rowIndex) {
1285 for (
size_t peakIndex = 1; peakIndex < numPeaks + 1; ++peakIndex) {
1287 if (std::isnormal(pos)) {
1288 resolution.emplace_back(
m_peakWidthTable->Double(rowIndex, peakIndex) / pos);
1291 if (resolution.empty()) {
1292 resolutionWksp->setValue(detId, 0., 0.);
1296 std::accumulate(resolution.begin(), resolution.end(), 0.) /
static_cast<double>(resolution.size());
1298 std::for_each(resolution.cbegin(), resolution.cend(),
1299 [&stddev, mean](
const auto value) { stddev += (value - mean) * (value * mean); });
1300 stddev = std::sqrt(stddev /
static_cast<double>(resolution.size() - 1));
1301 resolutionWksp->setValue(detId, mean, stddev);
1305 return resolutionWksp;
1311 alg->setLoggingOffset(1);
1312 alg->setProperty(
"InputWorkspace", table);
1313 alg->setProperty(
"OutputWorkspace", table);
1314 alg->setProperty(
"Columns",
"detid");
1315 alg->executeAsChildAlg();
1316 table = alg->getProperty(
"OutputWorkspace");
1335std::pair<API::MatrixWorkspace_sptr, API::MatrixWorkspace_sptr>
1337 const std::vector<double> &peakWindow) {
1345 << windowsInDSpacing[2 * i + 1] << std::endl;
1349 size_t numspec = dataws->getNumberHistograms();
1354 const auto NUM_HIST =
static_cast<int64_t
>(dataws->getNumberHistograms());
1357 PRAGMA_OMP(parallel
for schedule(dynamic, 1) )
1358 for (int64_t iws = 0; iws < static_cast<int64_t>(NUM_HIST); ++iws) {
1367 peak_pos_ws->setPoints(iws, peaks.inTofPos);
1368 peak_window_ws->setPoints(iws, peaks.inTofWindows);
1375 return std::make_pair(peak_pos_ws, peak_window_ws);
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
IPeaksWorkspace_sptr workspace
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PRAGMA_OMP(expression)
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
ColumnVector gives access to the column elements without alowing its resizing.
size_t size()
Size of the vector.
A specialized class for dealing with file properties.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
Base MatrixWorkspace Abstract Class.
Helper class for reporting progress from algorithms.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
std::set< detid_t > detid
void setPositions(const std::vector< double > &peaksInD, const std::vector< double > &peaksInDWindows, const double difa, const double difc, const double tzero)
Store the positions of the peak centers, as well as the left and right fit ranges for each peak,...
std::vector< double > inTofPos
std::vector< double > inTofWindows
std::vector< double > inDPos
FittedPeaks(const API::MatrixWorkspace_const_sptr &wksp, const std::size_t wkspIndex)
Find the bins with non-zero counts and with minimum and maximum TOF.
API::ITableWorkspace_sptr m_peakWidthTable
std::map< detid_t, size_t > m_detidToRow
API::ITableWorkspace_sptr m_peakHeightTable
void setCalibrationValues(const detid_t detid, const double difc, const double difa, const double tzero)
API::MatrixWorkspace_sptr m_uncalibratedWS
API::MatrixWorkspace_sptr rebin(API::MatrixWorkspace_sptr wksp)
API::ITableWorkspace_sptr sortTableWorkspace(API::ITableWorkspace_sptr &table)
sort the calibration table according increasing values in column "detid"
std::vector< double > dSpacingWindows(const std::vector< double > ¢res, const std::vector< double > &widthMax)
Fitting ranges to the left and right of peak center (the window cannot exceed half the distance to th...
const std::string category() const override
Algorithm's category for identification.
std::pair< API::MatrixWorkspace_sptr, API::MatrixWorkspace_sptr > createTOFPeakCenterFitWindowWorkspaces(const API::MatrixWorkspace_sptr &dataws, const std::vector< double > &peakWindowMaxInDSpacing)
NEW: convert peak positions in dSpacing to peak centers workspace.
API::ITableWorkspace_sptr m_calibrationTable
PDCalibration()
Constructor.
void init() override
Initialize the algorithm's properties.
API::MatrixWorkspace_sptr loadAndBin()
load input workspace and rebin with parameters "TofBinning" provided by User
std::vector< double > getTOFminmax(const double difc, const double difa, const double tzero)
Adjustment of TofMin and TofMax values, to ensure positive values of d-spacing when converting from T...
void createInformationWorkspaces()
Table workspaces where the first column is the detector ID and subsequent columns are termed "@x....
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
const std::string name() const override
Algorithms name for identification.
void fitDIFCtZeroDIFA_LM(const std::vector< double > &d, const std::vector< double > &tof, const std::vector< double > &height2, double &difc, double &t0, double &difa)
Fit the nominal peak center positions, in d-spacing against the fitted peak center positions,...
std::tuple< double, double, double > getDSpacingToTof(const std::set< detid_t > &detIds)
Return a function that converts from d-spacing to TOF units for a particular pixel,...
API::MatrixWorkspace_sptr load(const std::string &filename)
void createCalTableNew()
Initialize the calibration table workspace.
API::MatrixWorkspace_sptr calculateResolutionTable()
int version() const override
Algorithm's version for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
API::ITableWorkspace_sptr m_peakPositionTable
std::vector< double > m_peaksInDspacing
void exec() override
Execute the algorithm.
void createCalTableFromExisting()
Read a calibration table workspace provided by user, or load from a file provided by User.
~PDCalibration()
Destructor.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
Kernel/ArrayBoundedValidator.h.
Support for a property that holds an array of values.
BoundedValidator is a validator that requires the values to be between upper or lower bounds,...
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
void notice(const std::string &msg)
Logs at notice level.
void information(const std::string &msg)
Logs at information level.
Validator to check that a property is not left empty.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Validator to check the format of a vector providing the rebin parameters to an algorithm.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
void toTOF(std::vector< double > &xdata, std::vector< double > &ydata, const double &_l1, const int &_emode, std::initializer_list< std::pair< const UnitParams, double > > params)
Convert from the concrete unit to time-of-flight.
void initialize(const double &_l1, const int &_emode, const UnitParametersMap ¶ms)
Initialize the unit to perform conversion using singleToTof() and singleFromTof()
double calcTofMax(const double difc, const double difa, const double tzero, const double tofmax=0.)
double calcTofMin(const double difc, const double difa, const double tzero, const double tofmin=0.)
double singleFromTOF(const double tof) const override
DIFA * d^2 + DIFC * d + T0 - TOF = 0.
double singleToTOF(const double x) const override
Convert a single X value to TOF.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static double gsl_costFunction(const gsl_vector *v, void *params)
The gsl_costFunction is optimized by GSL simplex.
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
std::shared_ptr< SpecialWorkspace2D > SpecialWorkspace2D_sptr
shared pointer to the SpecialWorkspace2D class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::unordered_map< UnitParams, double > UnitParametersMap
int32_t detid_t
Typedef for a detector ID.
std::unordered_map< detid_t, size_t > detid2index_map
Map with key = detector ID, value = workspace index.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Describes the direction (within an algorithm) of a Property.
@ InOut
Both an input & output workspace.
@ Input
An input workspace.
@ Output
An output workspace.