32#include <boost/algorithm/string.hpp>
33#include <boost/algorithm/string/split.hpp>
39#include <gsl/gsl_sf_erf.h>
60 : m_BestGSLChi2(0.0), m_MinSigma(0.0), m_MinNumFittedPeaks(0), m_MaxNumberStoredParameters(0) {
61 this->useAlgorithm(
"RefinePowderInstrumentParameters", 3);
71 "TableWorkspace containg all peaks' parameters.");
76 "TableWorkspace containg instrument's parameters.");
81 "Output Workspace2D for the d-TOF curves. ");
86 "Output TableWorkspace for the fitted peak parameters for each peak.");
91 "Output TableWorkspace for the N best MC fitting results. ");
94 declareProperty(
"MinNumberFittedPeaks", 5,
"Minimum number of fitted peaks for refining instrument parameters.");
97 vector<string> algoptions{
"DirectFit",
"MonteCarlo"};
98 auto validator = std::make_shared<Kernel::StringListValidator>(algoptions);
99 declareProperty(
"RefinementAlgorithm",
"MonteCarlo", validator,
"Algorithm to refine the instrument parameters.");
101 declareProperty(
"RandomWalkSteps", 10000,
"Number of Monte Carlo random walk steps. ");
105 "Names of the parameters to fit. ");
109 declareProperty(
"MinSigma", 1.0,
"Minimum allowed value for Sigma of a peak.");
112 vector<string> stdoptions{
"ConstantValue",
"InvertedPeakHeight"};
113 auto listvalidator = std::make_shared<Kernel::StringListValidator>(stdoptions);
115 "Algorithm to calculate the standard error of peak positions.");
117 declareProperty(
"NumberBestFitRecorded", 1,
"Number of best fits (Monte Carlo) recorded and output. ");
119 declareProperty(
"MonteCarloRandomSeed", 0,
"Random seed for Monte Carlo simulation. ");
126 enum { DirectFit, MonteCarlo } refinealgorithm;
136 g_log.
error() <<
"Input MinNumberFittedPeaks = " << tempint <<
" is too small. \n";
137 throw std::invalid_argument(
"Input MinNumberFittedPeaks is too small.");
143 throw runtime_error(
"Input NumberBestFitRecorded cannot be less and equal to 0. ");
146 string algoption =
getProperty(
"RefinementAlgorithm");
147 if (algoption ==
"DirectFit")
148 refinealgorithm = DirectFit;
149 else if (algoption ==
"MonteCarlo")
150 refinealgorithm = MonteCarlo;
152 throw runtime_error(
"RefinementAlgorithm other than DirectFit and "
153 "MonteCarlo are not supported.");
162 if (refinealgorithm == MonteCarlo)
170 switch (refinealgorithm) {
180 setProperty(
"OutputBestResultsWorkspace", mcresultws);
185 errss <<
"Refinement algorithm " << algoption <<
" is not supported. Quit!";
187 throw invalid_argument(errss.str());
196 this->
setProperty(
"OutputInstrumentParameterWorkspace", fitparamws);
204 g_log.
debug() <<
"=========== Method [FitInstrumentParameters] ===============\n";
207 m_Function = std::make_shared<ThermalNeutronDtoTOFFunction>();
216 std::vector<std::string> funparamnames =
m_Function->getParameterNames();
218 std::vector<std::string> paramtofit =
getProperty(
"ParametersToFit");
219 std::sort(paramtofit.begin(), paramtofit.end());
222 msgss <<
"Set Instrument Function Parameter : \n";
223 std::map<std::string, double>::iterator paramiter;
224 for (
const auto &parname : funparamnames) {
231 double parvalue = paramiter->second;
233 msgss << setw(10) << parname <<
" = " << parvalue <<
'\n';
240 g_log.
debug() <<
"Fit Starting Value: Chi^2 (GSL) = " << gslchi2 <<
", Chi2^2 (Home) = " << homchi2 <<
'\n';
243 size_t numparams = funparamnames.size();
244 for (
size_t i = 0; i < numparams; ++i) {
245 string parname = funparamnames[i];
246 vector<string>::iterator vsiter;
247 vsiter = std::find(paramtofit.begin(), paramtofit.end(), parname);
249 if (vsiter == paramtofit.end())
257 string minimizer(
"Levenberg-MarquardtMD");
258 if (paramtofit.size() > 1) {
259 minimizer =
"Simplex";
267 for (
size_t i = 0; i <
m_dataWS->x(0).size(); ++i)
269 g_log.
debug() <<
"Input Peak Position Workspace To Fit: \n" << outss.str() <<
'\n';
272 fitalg->initialize();
274 fitalg->setProperty(
"Function", std::dynamic_pointer_cast<API::IFunction>(
m_Function));
275 fitalg->setProperty(
"InputWorkspace",
m_dataWS);
276 fitalg->setProperty(
"WorkspaceIndex", 0);
277 fitalg->setProperty(
"Minimizer", minimizer);
278 fitalg->setProperty(
"CostFunction",
"Least squares");
279 fitalg->setProperty(
"MaxIterations", 1000);
281 bool successfulfit = fitalg->execute();
282 if (!fitalg->isExecuted() || !successfulfit) {
284 g_log.
error() <<
"Fitting to instrument geometry function failed. \n";
285 throw std::runtime_error(
"Fitting failed.");
288 double chi2 = fitalg->getProperty(
"OutputChi2overDoF");
289 std::string fitstatus = fitalg->getProperty(
"OutputStatus");
291 g_log.
debug() <<
"Fit Result (GSL): Chi^2 = " << chi2 <<
"; Fit Status = " << fitstatus <<
'\n';
298 for (
size_t i = 0; i < domain.
size(); ++i) {
299 m_dataWS->mutableY(1)[i] = values[i];
304 g_log.
debug() <<
"Homemade Chi^2 = " << selfchi2 <<
'\n';
307 for (
const auto &parname : funparamnames) {
308 double parvalue = fitfunc->getParameter(parname);
314 dbss <<
"************ Fit Parameter Result *************\n";
316 std::string parname = paramiter->first;
318 double parvalue = paramiter->second;
319 dbss << setw(20) << parname <<
" = " << setw(15) << setprecision(6) << parvalue <<
"\t\tFrom " << setw(15)
320 << setprecision(6) << inpparvalue <<
"\t\tDiff = " << inpparvalue - parvalue <<
'\n';
322 dbss <<
"*********************************************\n";
332 zss << setw(20) <<
"d_h" << setw(20) <<
"Z DataY" << setw(20) <<
"Z ModelY" << setw(20) <<
"Z DiffY" << setw(20)
336 for (
size_t i = 0; i < z0.size(); ++i) {
338 double zdatay = z0[i];
339 double zmodely = z1[i];
340 double zdiffy = z2[i];
342 zss << setw(20) << d_h << setw(20) << zdatay << setw(20) << zmodely << setw(20) << zdiffy << setw(20) << diffy
345 g_log.
debug() <<
"Zscore Survey: \b" << zss.str();
352 fitalg->initialize();
354 fitalg->setProperty(
"Function", std::dynamic_pointer_cast<API::IFunction>(func));
355 fitalg->setProperty(
"InputWorkspace",
m_dataWS);
356 fitalg->setProperty(
"WorkspaceIndex", 0);
357 fitalg->setProperty(
"Minimizer",
"Simplex");
358 fitalg->setProperty(
"CostFunction",
"Least squares");
359 fitalg->setProperty(
"MaxIterations", 1000);
361 bool successfulfit = fitalg->execute();
362 if (!fitalg->isExecuted() || !successfulfit) {
364 g_log.
error() <<
"Fitting to instrument geometry function failed. \n";
365 throw std::runtime_error(
"Fitting failed.");
368 gslchi2 = fitalg->getProperty(
"OutputChi2overDoF");
369 std::string fitstatus = fitalg->getProperty(
"OutputStatus");
371 g_log.
debug() <<
"Function Fit: Chi^2 = " << gslchi2 <<
"; Fit Status = " << fitstatus <<
'\n';
373 bool fitgood = (fitstatus ==
"success");
382 size_t workspaceindex) {
384 vector<string> funcparameters = func->getParameterNames();
385 size_t numparams = funcparameters.size();
386 for (
size_t i = 0; i < numparams; ++i) {
392 fitalg->initialize();
394 fitalg->setProperty(
"Function", std::dynamic_pointer_cast<API::IFunction>(func));
395 fitalg->setProperty(
"InputWorkspace", dataws);
396 fitalg->setProperty(
"WorkspaceIndex",
static_cast<int>(workspaceindex));
397 fitalg->setProperty(
"Minimizer",
"Levenberg-MarquardtMD");
398 fitalg->setProperty(
"CostFunction",
"Least squares");
399 fitalg->setProperty(
"MaxIterations", 2);
401 bool successfulfit = fitalg->execute();
402 if (!fitalg->isExecuted() || !successfulfit) {
404 g_log.
error() <<
"Fitting to instrument geometry function failed. \n";
405 throw std::runtime_error(
"Fitting failed.");
408 double chi2 = fitalg->getProperty(
"OutputChi2overDoF");
409 std::string fitstatus = fitalg->getProperty(
"OutputStatus");
411 g_log.
debug() <<
"Function calculation [L.M]: Chi^2 = " << chi2 <<
"; Fit Status = " << fitstatus <<
'\n';
423 vector<double> stepsizes, lowerbounds, upperbounds;
429 <<
", Max = " << setw(15) << setprecision(6) << upperbounds[i] <<
", Step Size = " << setw(15)
430 << setprecision(6) << stepsizes[i] <<
'\n';
432 g_log.
notice() <<
"Monte Carlo Parameters: \n" << dbss.str();
438 maxsteps =
static_cast<size_t>(tempint);
440 throw runtime_error(
"RandomwWalkSteps cannot be less than or equal to 0. ");
443 int randomseed =
getProperty(
"MonteCarloRandomSeed");
446 double stepsizescalefactor = 1.1;
450 stepsizescalefactor, fit2);
467 vector<double> vec_n;
471 auto &newY =
m_dataWS->mutableY(3 * i + 1);
472 auto &newD =
m_dataWS->mutableY(3 * i + 2);
473 auto &newN =
m_dataWS->mutableY(3 * i + 3);
474 for (
size_t j = 0; j < newY.size(); ++j) {
476 newD[j] =
Y[j] - values[j];
487 const vector<double> &lowerbounds,
488 const vector<double> &upperbounds,
489 vector<double> &stepsizes,
size_t maxsteps,
490 double stepsizescalefactor,
bool fit2) {
493 size_t numparameters = parnames.
size();
494 vector<double> paramvalues;
495 for (
size_t i = 0; i < numparameters; ++i) {
496 string parname = parnames[i];
498 paramvalues.emplace_back(parvalue);
513 vector<string> paramstofit =
getProperty(
"ParametersToFit");
514 set<string> paramstofitset;
515 bool refineallparams;
516 if (paramstofit.empty()) {
518 refineallparams =
true;
521 refineallparams =
false;
522 vector<string>::iterator vsiter;
523 for (vsiter = paramstofit.begin(); vsiter != paramstofit.end(); ++vsiter) {
524 paramstofitset.insert(*vsiter);
529 set<string>::iterator setiter;
530 for (setiter = paramstofitset.begin(); setiter != paramstofitset.end(); ++setiter) {
531 string paramName = *setiter;
532 dbss << setw(20) << paramName;
534 g_log.
notice() <<
"Parameters to refine: " << dbss.str() <<
'\n';
539 func4fit->initialize();
540 for (
size_t i = 0; i < numparameters; ++i) {
541 string parname = parnames[i];
543 if (paramstofitset.count(parname) > 0)
548 double lowerb = lowerbounds[i];
549 double upperb = upperbounds[i];
550 auto newconstraint = std::make_unique<BoundaryConstraint>(func4fit.get(), parname, lowerb, upperb);
551 func4fit->addConstraint(std::move(newconstraint));
553 g_log.
debug() <<
"Function for fitting in MC: " << func4fit->asString() <<
'\n';
558 g_log.
notice() <<
"Monte Carlo Random Walk Starting Chi^2 = " << curchi2 <<
'\n';
560 size_t paramindex = 0;
561 size_t numacceptance = 0;
562 for (
size_t istep = 0; istep < maxsteps; ++istep) {
564 if (!refineallparams) {
565 if (paramstofitset.count(parnames[paramindex]) == 0) {
567 if (paramindex >= parnames.size())
574 double randomnumber =
static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
575 double newvalue = paramvalues[paramindex] + (randomnumber - 1.0) * stepsizes[paramindex];
576 if (newvalue > upperbounds[paramindex]) {
577 newvalue = lowerbounds[paramindex] + (newvalue - upperbounds[paramindex]);
578 }
else if (newvalue < lowerbounds[paramindex]) {
579 newvalue = upperbounds[paramindex] - (lowerbounds[paramindex] - newvalue);
583 m_Function->setParameter(parnames[paramindex], newvalue);
584 }
catch (runtime_error &) {
586 errss <<
"New Value = " << newvalue <<
", Random Number = " << randomnumber
587 <<
"Step Size = " << stepsizes[paramindex] <<
", Step size rescale factor = " << stepsizescalefactor;
598 for (
size_t i = 0; i < numparameters; ++i) {
599 double parvalue =
m_Function->getParameter(i);
600 func4fit->setParameter(i, parvalue);
615 vector<double> newparvalues;
616 newparvalues.reserve(numparameters);
617 for (
size_t i = 0; i < numparameters; ++i) {
618 double parvalue = func4fit->getParameter(i);
619 newparvalues.emplace_back(parvalue);
636 double prob = exp(-(newchi2 - curchi2) / curchi2);
637 double randnumber =
static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
638 accept = randnumber < prob;
642 if (newchi2 < curchi2) {
644 stepsizes[paramindex] = stepsizes[paramindex] / stepsizescalefactor;
647 double newstepsize = stepsizes[paramindex] * stepsizescalefactor;
648 double maxstepsize = upperbounds[paramindex] - lowerbounds[paramindex];
649 if (newstepsize >= maxstepsize) {
650 newstepsize = maxstepsize;
652 stepsizes[paramindex] = newstepsize;
659 paramvalues[paramindex] = newvalue;
676 if (paramindex >= numparameters)
682 stringstream mcresult;
683 mcresult <<
"Monte Carlo Result for " <<
m_BestMCParameters.size() <<
" Best Results\n";
684 mcresult <<
"Number of acceptance = " << numacceptance <<
", out of " << maxsteps <<
" MC steps."
685 <<
"Accept ratio = " <<
static_cast<double>(numacceptance) /
static_cast<double>(maxsteps) <<
'\n';
686 mcresult <<
"Best " <<
m_BestMCParameters.size() <<
" Monte Carlo (no fit) results: \n";
705 m_Function = std::make_shared<ThermalNeutronDtoTOFFunction>();
706 std::vector<std::string> funparamnames =
m_Function->getParameterNames();
709 parnames = funparamnames;
717 const Mantid::HistogramData::HistogramY &rawY,
718 const Mantid::HistogramData::HistogramE &rawE) {
721 throw std::runtime_error(
"m_Function has not been initialized!");
724 if (domain.
size() != values.
size() || domain.
size() != rawY.size() || rawY.size() != rawE.size()) {
725 throw std::runtime_error(
"Input domain, values and raw data have different sizes.");
729 func->function(domain, values);
733 for (
size_t i = 0; i < domain.
size(); ++i) {
734 double temp = (values[i] - rawY[i]) / rawE[i];
751 g_log.
error() <<
"Input tableworkspace for peak parameters is invalid!\n";
752 throw std::invalid_argument(
"Invalid input table workspace for peak parameters");
758 vector<string> colnames = peakparamws->getColumnNames();
759 size_t numrows = peakparamws->rowCount();
761 for (
size_t ir = 0; ir < numrows; ++ir) {
764 newpeakptr->initialize();
768 double alpha, beta, tof_h,
sigma, sigma2, chi2,
height, dbtemp;
773 for (
const auto &colname : colnames) {
776 else if (colname ==
"K")
778 else if (colname ==
"L")
780 else if (colname ==
"Alpha")
782 else if (colname ==
"Beta")
784 else if (colname ==
"Sigma2")
786 else if (colname ==
"Sigma")
788 else if (colname ==
"Chi2")
790 else if (colname ==
"Height")
792 else if (colname ==
"TOF_h")
797 }
catch (runtime_error &) {
804 sigma = sqrt(sigma2);
807 newpeakptr->setParameter(
"A", alpha);
808 newpeakptr->setParameter(
"B", beta);
809 newpeakptr->setParameter(
"S",
sigma);
810 newpeakptr->setParameter(
"X0", tof_h);
811 newpeakptr->setParameter(
"I",
height);
813 std::vector<int> hkl;
818 m_Peaks.emplace(hkl, newpeakptr);
822 g_log.
information() <<
"[Generatem_Peaks] Peak " << ir <<
" HKL = [" << hkl[0] <<
", " << hkl[1] <<
", " << hkl[2]
823 <<
"], Input Center = " << setw(10) << setprecision(6) << newpeakptr->centre() <<
'\n';
832 std::map<std::string, double> ¶meters) {
834 std::vector<std::string> colnames = parameterWS->getColumnNames();
835 if (colnames.size() < 2) {
836 g_log.
error() <<
"Input parameter table workspace does not have enough "
837 "number of columns. "
838 <<
" Number of columns = " << colnames.size() <<
" < 3 as required. \n";
839 throw std::runtime_error(
"Input parameter workspace is wrong. ");
842 if (colnames[0] !=
"Name" || colnames[1] !=
"Value") {
843 g_log.
error() <<
"Input parameter table workspace does not have the "
845 <<
" It must be Name, Value, FitOrTie.\n";
846 throw std::runtime_error(
"Input parameter workspace is wrong. ");
853 size_t numrows = parameterWS->rowCount();
855 for (
size_t ir = 0; ir < numrows; ++ir) {
858 trow >> parname >>
value;
859 parameters.emplace(parname,
value);
860 }
catch (runtime_error &) {
861 g_log.
error() <<
"Import table workspace " << parameterWS->getName() <<
" error in line " << ir <<
". "
862 <<
" Requires [string, double] in the first 2 columns.\n";
872 const vector<string> ¶meternames,
873 vector<double> &stepsizes,
874 vector<double> &lowerbounds,
875 vector<double> &upperbounds) {
877 vector<string> colnames = tablews->getColumnNames();
878 size_t imax, imin, istep;
879 imax = colnames.size() + 1;
883 for (
size_t i = 0; i < colnames.size(); ++i) {
884 if (colnames[i] ==
"Max")
886 else if (colnames[i] ==
"Min")
888 else if (colnames[i] ==
"StepSize")
892 if (imax > colnames.size() || imin > colnames.size() || istep > colnames.size()) {
894 errss <<
"Input parameter workspace misses information for Monte Carlo "
896 <<
"One or more of the following columns are missing (Max, Min, "
899 throw runtime_error(errss.str());
907 map<string, vector<double>> mcparameters;
908 size_t numrows = tablews->rowCount();
909 for (
size_t ir = 0; ir < numrows; ++ir) {
912 double tmax = 0, tmin = 0, tstepsize = 0;
914 for (
size_t ic = 1; ic < colnames.size(); ++ic) {
915 double tmpdbl = std::numeric_limits<float>::quiet_NaN();
918 }
catch (runtime_error &) {
919 g_log.
error() <<
"Import MC parameter " << colnames[ic] <<
" error in row " << ir <<
" of workspace "
920 << tablews->getName() <<
'\n';
923 g_log.
error() <<
"Should be " << tmpstr <<
'\n';
930 else if (ic == istep)
933 vector<double> tmpvec;
934 tmpvec.emplace_back(tmin);
935 tmpvec.emplace_back(tmax);
936 tmpvec.emplace_back(tstepsize);
937 mcparameters.emplace(parname, tmpvec);
941 for (
const auto &parname : parameternames) {
943 auto mit = mcparameters.find(parname);
944 if (mit == mcparameters.end()) {
947 errss <<
"Input instrument parameter workspace does not have parameter " << parname
948 <<
". Information is incomplete for Monte Carlo simulation.\n";
950 throw runtime_error(errss.str());
952 vector<double> mcparvalues = mit->second;
955 lowerbounds.emplace_back(mcparvalues[0]);
956 upperbounds.emplace_back(mcparvalues[1]);
957 stepsizes.emplace_back(mcparvalues[2]);
979 const HistogramX &xVals, vector<double> &vec_n) {
980 if (
m_Function->name() !=
"ThermalNeutronDtoTOFFunction") {
982 <<
" is not ThermalNeutronDtoTOFFunction. And it is not "
983 "required to calculate n.\n";
984 for (
size_t i = 0; i < xVals.size(); ++i)
985 vec_n.emplace_back(0);
988 double width =
m_Function->getParameter(
"Width");
989 double tcross =
m_Function->getParameter(
"Tcross");
991 for (
double dh : xVals) {
992 double n = 0.5 * gsl_sf_erfc(width * (tcross - 1 / dh));
993 vec_n.emplace_back(
n);
1007 if (lattice < 1.0E-5) {
1008 std::stringstream errmsg;
1009 errmsg <<
"Input Lattice constant = " << lattice <<
" is wrong or not set up right. ";
1010 throw std::invalid_argument(errmsg.str());
1014 enum { ConstantValue, InvertedPeakHeight } stderroroption;
1015 if (stdoption ==
"ConstantValue") {
1016 stderroroption = ConstantValue;
1017 }
else if (stdoption ==
"InvertedPeakHeight") {
1018 stderroroption = InvertedPeakHeight;
1021 errss <<
"Input StandardError (" << stdoption <<
") is not supported. ";
1023 throw runtime_error(errss.str());
1027 std::vector<std::pair<double, std::pair<double, double>>> peakcenters;
1031 for (peakiter =
m_Peaks.begin(); peakiter !=
m_Peaks.end(); ++peakiter) {
1032 vector<int> hkl = peakiter->first;
1035 double sigma = peak->getParameter(
"S");
1037 g_log.
information() <<
"Peak (" << hkl[0] <<
", " << hkl[1] <<
", " << hkl[2]
1038 <<
") has unphysically small Sigma = " <<
sigma <<
". "
1039 <<
"It is thus excluded. \n";
1046 double dh = unitcell.
d(hkl[0], hkl[1], hkl[2]);
1047 double center = peak->centre();
1048 double height = peak->height();
1050 if (stderroroption == ConstantValue) {
1052 }
else if (stderroroption == InvertedPeakHeight) {
1053 chi2 = sqrt(1.0 /
height);
1055 throw runtime_error(
"Standard error option is not supported. ");
1058 peakcenters.emplace_back(dh, make_pair(center, chi2));
1062 std::sort(peakcenters.begin(), peakcenters.end());
1065 size_t size = peakcenters.size();
1069 nspec = 1 + 3 * numbestfit;
1075 m_dataWS = std::dynamic_pointer_cast<DataObjects::Workspace2D>(
1077 m_dataWS->getAxis(0)->setUnit(
"dSpacing");
1080 for (
size_t i = 0; i < peakcenters.size(); ++i) {
1081 for (
size_t j = 0; j < nspec; ++j) {
1082 m_dataWS->mutableX(j)[i] = peakcenters[i].first;
1084 m_dataWS->mutableY(0)[i] = peakcenters[i].second.first;
1085 m_dataWS->mutableE(0)[i] = peakcenters[i].second.second;
1096 tablews->addColumn(
"double",
"Chi2");
1097 tablews->addColumn(
"double",
"GSLChi2");
1099 tablews->addColumn(
"double", peakFunctionParameterName);
1104 TableRow newrow = tablews->appendRow();
1107 newrow << chi2 << gslchi2;
1127 newtablews->addColumn(
"str",
"Name");
1128 newtablews->addColumn(
"double",
"Value");
1129 newtablews->addColumn(
"str",
"FitOrTie");
1130 newtablews->addColumn(
"double",
"Min");
1131 newtablews->addColumn(
"double",
"Max");
1132 newtablews->addColumn(
"double",
"StepSize");
1134 std::map<std::string, double>::iterator pariter;
1138 std::string parname = pariter->first;
1139 double parvalue = pariter->second;
1140 newrow << parname << parvalue;
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
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.
Implements FunctionDomain1D with its own storage in form of a std::vector.
size_t size() const override
Return the number of arguments in the domain.
A class to store values calculated by a function.
size_t size() const
Return the number of values.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
RefinePowderInstrumentParameters : Algorithm to refine instrument geometry parameters only.
void genPeaksFromTable(const DataObjects::TableWorkspace_sptr &peakparamws)
Generate peaks from table (workspace)
void importMonteCarloParametersFromTable(const DataObjects::TableWorkspace_sptr &tablews, const std::vector< std::string > ¶meternames, std::vector< double > &stepsizes, std::vector< double > &lowerbounds, std::vector< double > &upperbounds)
Import the Monte Carlo related parameters from table.
std::map< std::string, double > m_OrigParameters
Map to store the original (input) parameters.
bool fitFunction(const API::IFunction_sptr &func, double &gslchi2)
Fit function to data.
std::vector< std::string > m_PeakFunctionParameterNames
Peak function parameter names.
double calculateD2TOFFunction(const API::IFunction_sptr &func, const API::FunctionDomain1DVector &domain, API::FunctionValues &values, const Mantid::HistogramData::HistogramY &rawY, const Mantid::HistogramData::HistogramE &rawE)
Calculate the value and chi2.
size_t m_MinNumFittedPeaks
Minimum number of fitted peaks for refinement.
void importParametersFromTable(const DataObjects::TableWorkspace_sptr ¶meterWS, std::map< std::string, double > ¶meters)
Import instrument parameter from table (workspace)
void init() override
Parameter declaration.
void fitInstrumentParameters()
Fit instrument geometry parameters by ThermalNeutronDtoTOFFunction.
Functions::ThermalNeutronDtoTOFFunction_sptr m_Function
Modelling function.
std::map< std::string, double > m_FuncParameters
Map for function (instrument parameter)
std::vector< std::pair< double, std::vector< double > > > m_BestFitParameters
N sets of the peak parameter values for the best N chi2 for MC.
std::map< std::vector< int >, double > m_PeakErrors
Map for all peaks' error (fitted vs. experimental): [HKL]: Chi^2.
std::map< std::vector< int >, Functions::BackToBackExponential_sptr > m_Peaks
Map for all peaks to fit individually.
double calculateFunctionStatistic(const API::IFunction_sptr &func, const API::MatrixWorkspace_sptr &dataws, size_t workspaceindex)
Calculate function's statistic.
DataObjects::Workspace2D_sptr m_dataWS
Output Workspace containing the dspacing ~ TOF peak positions.
DataObjects::TableWorkspace_sptr genMCResultTable()
Generate an output table workspace for N best fitting result.
void doParameterSpaceRandomWalk(const std::vector< std::string > &parnames, const std::vector< double > &lowerbounds, const std::vector< double > &upperbounds, std::vector< double > &stepsizes, size_t maxsteps, double stepsizescalefactor, bool fit2)
Core Monte Carlo random walk on parameter-space.
double m_BestGSLChi2
Best Chi2 ever.
void calculateThermalNeutronSpecial(const API::IFunction_sptr &m_Function, const Mantid::HistogramData::HistogramX &xVals, std::vector< double > &vec_n)
Calculate d-space value from peak's miller index for thermal neutron.
void getD2TOFFuncParamNames(std::vector< std::string > &parnames)
Get the names of the parameters of D-TOF conversion function.
std::vector< std::pair< double, std::vector< double > > > m_BestMCParameters
N sets of the peak parameter values for the best N chi2 for MC.
void genPeakCentersWorkspace(bool montecarlo, size_t numbestfit)
Generate (output) workspace of peak centers.
std::vector< std::pair< double, double > > m_BestFitChi2s
N sets of the homemade chi2 and gsl chi2.
DataObjects::TableWorkspace_sptr genOutputInstrumentParameterTable()
Generate (output) table worksspace for instrument parameters.
void refineInstrumentParametersMC(const DataObjects::TableWorkspace_sptr ¶meterWS, bool fit2=false)
Set up and run a monte carlo simulation to refine the peak parameters.
double m_MinSigma
Minimum allowed sigma of a peak.
void exec() override
Main execution.
size_t m_MaxNumberStoredParameters
Maximum number of data stored.
ThermalNeutronDtoTOFFunction : TODO: DESCRIPTION.
TableWorkspace is an implementation of Workspace in which the data are organised in columns of same s...
Class to implement unit cell of crystals.
double d(double h, double k, double l) const
Return d-spacing ( ) for a given h,k,l coordinate.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< ThermalNeutronDtoTOFFunction > ThermalNeutronDtoTOFFunction_sptr
std::shared_ptr< BackToBackExponential > BackToBackExponential_sptr
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
std::vector< double > getZscore(const std::vector< TYPE > &data)
Return the Z score values for a dataset.
Helper class which provides the Collimation Length for SANS instruments.
@ InOut
Both an input & output workspace.
@ Input
An input workspace.
@ Output
An output workspace.