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 vector<double> upperbounds, vector<double> stepsizes,
488 size_t maxsteps,
double stepsizescalefactor,
492 size_t numparameters = parnames.size();
493 vector<double> paramvalues;
494 for (
size_t i = 0; i < numparameters; ++i) {
495 string parname = parnames[i];
497 paramvalues.emplace_back(parvalue);
512 vector<string> paramstofit =
getProperty(
"ParametersToFit");
513 set<string> paramstofitset;
514 bool refineallparams;
515 if (paramstofit.empty()) {
517 refineallparams =
true;
520 refineallparams =
false;
521 vector<string>::iterator vsiter;
522 for (vsiter = paramstofit.begin(); vsiter != paramstofit.end(); ++vsiter) {
523 paramstofitset.insert(*vsiter);
528 set<string>::iterator setiter;
529 for (setiter = paramstofitset.begin(); setiter != paramstofitset.end(); ++setiter) {
530 string name = *setiter;
531 dbss << setw(20) <<
name;
533 g_log.
notice() <<
"Parameters to refine: " << dbss.str() <<
'\n';
538 func4fit->initialize();
539 for (
size_t i = 0; i < numparameters; ++i) {
540 string parname = parnames[i];
542 if (paramstofitset.count(parname) > 0)
547 double lowerb = lowerbounds[i];
548 double upperb = upperbounds[i];
549 auto newconstraint = std::make_unique<BoundaryConstraint>(func4fit.get(), parname, lowerb, upperb);
550 func4fit->addConstraint(std::move(newconstraint));
552 g_log.
debug() <<
"Function for fitting in MC: " << func4fit->asString() <<
'\n';
557 g_log.
notice() <<
"Monte Carlo Random Walk Starting Chi^2 = " << curchi2 <<
'\n';
559 size_t paramindex = 0;
560 size_t numacceptance = 0;
561 for (
size_t istep = 0; istep < maxsteps; ++istep) {
563 if (!refineallparams) {
564 if (paramstofitset.count(parnames[paramindex]) == 0) {
566 if (paramindex >= parnames.size())
573 double randomnumber =
static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
574 double newvalue = paramvalues[paramindex] + (randomnumber - 1.0) * stepsizes[paramindex];
575 if (newvalue > upperbounds[paramindex]) {
576 newvalue = lowerbounds[paramindex] + (newvalue - upperbounds[paramindex]);
577 }
else if (newvalue < lowerbounds[paramindex]) {
578 newvalue = upperbounds[paramindex] - (lowerbounds[paramindex] - newvalue);
582 m_Function->setParameter(parnames[paramindex], newvalue);
583 }
catch (runtime_error &) {
585 errss <<
"New Value = " << newvalue <<
", Random Number = " << randomnumber
586 <<
"Step Size = " << stepsizes[paramindex] <<
", Step size rescale factor = " << stepsizescalefactor;
597 for (
size_t i = 0; i < numparameters; ++i) {
598 double parvalue =
m_Function->getParameter(i);
599 func4fit->setParameter(i, parvalue);
614 vector<double> newparvalues;
615 newparvalues.reserve(numparameters);
616 for (
size_t i = 0; i < numparameters; ++i) {
617 double parvalue = func4fit->getParameter(i);
618 newparvalues.emplace_back(parvalue);
635 double prob = exp(-(newchi2 - curchi2) / curchi2);
636 double randnumber =
static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
637 accept = randnumber < prob;
641 if (newchi2 < curchi2) {
643 stepsizes[paramindex] = stepsizes[paramindex] / stepsizescalefactor;
646 double newstepsize = stepsizes[paramindex] * stepsizescalefactor;
647 double maxstepsize = upperbounds[paramindex] - lowerbounds[paramindex];
648 if (newstepsize >= maxstepsize) {
649 newstepsize = maxstepsize;
651 stepsizes[paramindex] = newstepsize;
658 paramvalues[paramindex] = newvalue;
675 if (paramindex >= numparameters)
681 stringstream mcresult;
682 mcresult <<
"Monte Carlo Result for " <<
m_BestMCParameters.size() <<
" Best Results\n";
683 mcresult <<
"Number of acceptance = " << numacceptance <<
", out of " << maxsteps <<
" MC steps."
684 <<
"Accept ratio = " <<
static_cast<double>(numacceptance) /
static_cast<double>(maxsteps) <<
'\n';
685 mcresult <<
"Best " <<
m_BestMCParameters.size() <<
" Monte Carlo (no fit) results: \n";
704 m_Function = std::make_shared<ThermalNeutronDtoTOFFunction>();
705 std::vector<std::string> funparamnames =
m_Function->getParameterNames();
708 parnames = funparamnames;
716 const Mantid::HistogramData::HistogramY &rawY,
717 const Mantid::HistogramData::HistogramE &rawE) {
720 throw std::runtime_error(
"m_Function has not been initialized!");
723 if (domain.
size() != values.
size() || domain.
size() != rawY.size() || rawY.size() != rawE.size()) {
724 throw std::runtime_error(
"Input domain, values and raw data have different sizes.");
728 func->function(domain, values);
732 for (
size_t i = 0; i < domain.
size(); ++i) {
733 double temp = (values[i] - rawY[i]) / rawE[i];
750 g_log.
error() <<
"Input tableworkspace for peak parameters is invalid!\n";
751 throw std::invalid_argument(
"Invalid input table workspace for peak parameters");
757 vector<string> colnames = peakparamws->getColumnNames();
758 size_t numrows = peakparamws->rowCount();
760 for (
size_t ir = 0; ir < numrows; ++ir) {
763 newpeakptr->initialize();
767 double alpha, beta, tof_h,
sigma, sigma2, chi2,
height, dbtemp;
772 for (
auto &colname : colnames) {
775 else if (colname ==
"K")
777 else if (colname ==
"L")
779 else if (colname ==
"Alpha")
781 else if (colname ==
"Beta")
783 else if (colname ==
"Sigma2")
785 else if (colname ==
"Sigma")
787 else if (colname ==
"Chi2")
789 else if (colname ==
"Height")
791 else if (colname ==
"TOF_h")
796 }
catch (runtime_error &) {
803 sigma = sqrt(sigma2);
806 newpeakptr->setParameter(
"A", alpha);
807 newpeakptr->setParameter(
"B", beta);
808 newpeakptr->setParameter(
"S",
sigma);
809 newpeakptr->setParameter(
"X0", tof_h);
810 newpeakptr->setParameter(
"I",
height);
812 std::vector<int> hkl;
817 m_Peaks.emplace(hkl, newpeakptr);
821 g_log.
information() <<
"[Generatem_Peaks] Peak " << ir <<
" HKL = [" << hkl[0] <<
", " << hkl[1] <<
", " << hkl[2]
822 <<
"], Input Center = " << setw(10) << setprecision(6) << newpeakptr->centre() <<
'\n';
831 std::map<std::string, double> ¶meters) {
833 std::vector<std::string> colnames = parameterWS->getColumnNames();
834 if (colnames.size() < 2) {
835 g_log.
error() <<
"Input parameter table workspace does not have enough "
836 "number of columns. "
837 <<
" Number of columns = " << colnames.size() <<
" < 3 as required. \n";
838 throw std::runtime_error(
"Input parameter workspace is wrong. ");
841 if (colnames[0] !=
"Name" || colnames[1] !=
"Value") {
842 g_log.
error() <<
"Input parameter table workspace does not have the "
844 <<
" It must be Name, Value, FitOrTie.\n";
845 throw std::runtime_error(
"Input parameter workspace is wrong. ");
852 size_t numrows = parameterWS->rowCount();
854 for (
size_t ir = 0; ir < numrows; ++ir) {
857 trow >> parname >>
value;
858 parameters.emplace(parname,
value);
859 }
catch (runtime_error &) {
860 g_log.
error() <<
"Import table workspace " << parameterWS->getName() <<
" error in line " << ir <<
". "
861 <<
" Requires [string, double] in the first 2 columns.\n";
871 const vector<string> ¶meternames,
872 vector<double> &stepsizes,
873 vector<double> &lowerbounds,
874 vector<double> &upperbounds) {
876 vector<string> colnames = tablews->getColumnNames();
877 size_t imax, imin, istep;
878 imax = colnames.size() + 1;
882 for (
size_t i = 0; i < colnames.size(); ++i) {
883 if (colnames[i] ==
"Max")
885 else if (colnames[i] ==
"Min")
887 else if (colnames[i] ==
"StepSize")
891 if (imax > colnames.size() || imin > colnames.size() || istep > colnames.size()) {
893 errss <<
"Input parameter workspace misses information for Monte Carlo "
895 <<
"One or more of the following columns are missing (Max, Min, "
898 throw runtime_error(errss.str());
906 map<string, vector<double>> mcparameters;
907 size_t numrows = tablews->rowCount();
908 for (
size_t ir = 0; ir < numrows; ++ir) {
911 double tmax, tmin, tstepsize;
913 for (
size_t ic = 1; ic < colnames.size(); ++ic) {
914 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';
922 g_log.
error() <<
"Should be " << tmpstr <<
'\n';
929 else if (ic == istep)
932 vector<double> tmpvec;
933 tmpvec.emplace_back(tmin);
934 tmpvec.emplace_back(tmax);
935 tmpvec.emplace_back(tstepsize);
936 mcparameters.emplace(parname, tmpvec);
940 for (
const auto &parname : parameternames) {
942 auto mit = mcparameters.find(parname);
943 if (mit == mcparameters.end()) {
946 errss <<
"Input instrument parameter workspace does not have parameter " << parname
947 <<
". Information is incomplete for Monte Carlo simulation.\n";
949 throw runtime_error(errss.str());
951 vector<double> mcparvalues = mit->second;
954 lowerbounds.emplace_back(mcparvalues[0]);
955 upperbounds.emplace_back(mcparvalues[1]);
956 stepsizes.emplace_back(mcparvalues[2]);
978 const HistogramX &xVals, vector<double> &vec_n) {
979 if (
m_Function->name() !=
"ThermalNeutronDtoTOFFunction") {
981 <<
" is not ThermalNeutronDtoTOFFunction. And it is not "
982 "required to calculate n.\n";
983 for (
size_t i = 0; i < xVals.size(); ++i)
984 vec_n.emplace_back(0);
987 double width =
m_Function->getParameter(
"Width");
988 double tcross =
m_Function->getParameter(
"Tcross");
990 for (
double dh : xVals) {
991 double n = 0.5 * gsl_sf_erfc(width * (tcross - 1 / dh));
992 vec_n.emplace_back(
n);
1006 if (lattice < 1.0E-5) {
1007 std::stringstream errmsg;
1008 errmsg <<
"Input Lattice constant = " << lattice <<
" is wrong or not set up right. ";
1009 throw std::invalid_argument(errmsg.str());
1013 enum { ConstantValue, InvertedPeakHeight } stderroroption;
1014 if (stdoption ==
"ConstantValue") {
1015 stderroroption = ConstantValue;
1016 }
else if (stdoption ==
"InvertedPeakHeight") {
1017 stderroroption = InvertedPeakHeight;
1020 errss <<
"Input StandardError (" << stdoption <<
") is not supported. ";
1022 throw runtime_error(errss.str());
1026 std::vector<std::pair<double, std::pair<double, double>>> peakcenters;
1030 for (peakiter =
m_Peaks.begin(); peakiter !=
m_Peaks.end(); ++peakiter) {
1031 vector<int> hkl = peakiter->first;
1034 double sigma = peak->getParameter(
"S");
1036 g_log.
information() <<
"Peak (" << hkl[0] <<
", " << hkl[1] <<
", " << hkl[2]
1037 <<
") has unphysically small Sigma = " <<
sigma <<
". "
1038 <<
"It is thus excluded. \n";
1045 double dh = unitcell.
d(hkl[0], hkl[1], hkl[2]);
1046 double center = peak->centre();
1047 double height = peak->height();
1049 if (stderroroption == ConstantValue) {
1051 }
else if (stderroroption == InvertedPeakHeight) {
1052 chi2 = sqrt(1.0 /
height);
1054 throw runtime_error(
"Standard error option is not supported. ");
1057 peakcenters.emplace_back(dh, make_pair(center, chi2));
1061 std::sort(peakcenters.begin(), peakcenters.end());
1064 size_t size = peakcenters.size();
1068 nspec = 1 + 3 * numbestfit;
1074 m_dataWS = std::dynamic_pointer_cast<DataObjects::Workspace2D>(
1076 m_dataWS->getAxis(0)->setUnit(
"dSpacing");
1079 for (
size_t i = 0; i < peakcenters.size(); ++i) {
1080 for (
size_t j = 0; j < nspec; ++j) {
1081 m_dataWS->mutableX(j)[i] = peakcenters[i].first;
1083 m_dataWS->mutableY(0)[i] = peakcenters[i].second.first;
1084 m_dataWS->mutableE(0)[i] = peakcenters[i].second.second;
1095 tablews->addColumn(
"double",
"Chi2");
1096 tablews->addColumn(
"double",
"GSLChi2");
1098 tablews->addColumn(
"double", peakFunctionParameterName);
1103 TableRow newrow = tablews->appendRow();
1106 newrow << chi2 << gslchi2;
1126 newtablews->addColumn(
"str",
"Name");
1127 newtablews->addColumn(
"double",
"Value");
1128 newtablews->addColumn(
"str",
"FitOrTie");
1129 newtablews->addColumn(
"double",
"Min");
1130 newtablews->addColumn(
"double",
"Max");
1131 newtablews->addColumn(
"double",
"StepSize");
1133 std::map<std::string, double>::iterator pariter;
1137 std::string parname = pariter->first;
1138 double parvalue = pariter->second;
1139 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.
void doParameterSpaceRandomWalk(std::vector< std::string > parnames, std::vector< double > lowerbounds, std::vector< double > upperbounds, std::vector< double > stepsizes, size_t maxsteps, double stepsizescalefactor, bool fit2)
Core Monte Carlo random walk on parameter-space.
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)
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
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.
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.