28using namespace Kernel;
29using namespace Algorithms::PeakParameterHelper;
32using namespace DataObjects;
41 "A 2D workspace with X values of d-spacing");
43 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
44 mustBePositive->setLower(0);
46 declareProperty(
"Step", 0.001, mustBePositive,
"Step size used to bin d-spacing data");
47 declareProperty(
"DReference", 2.0, mustBePositive,
"Center of reference peak in d-space");
48 declareProperty(
"XMin", 0.0,
"Minimum of CrossCorrelation data to search for peak, usually negative");
49 declareProperty(
"XMax", 0.0,
"Maximum of CrossCorrelation data to search for peak, usually positive");
52 "Optional: The name of the output CalFile to save the "
53 "generated OffsetsWorkspace.");
55 "An output workspace containing the offsets.");
57 "An output workspace containing the mask.");
61 "The function type for fitting the peaks.");
63 "Whether to esimate FWHM of peak function when estimating fit parameters");
64 declareProperty(
"MaxOffset", 1.0,
"Maximum absolute value of offsets; default is 1");
67 std::vector<std::string> modes{
"Relative",
"Absolute",
"Signed"};
69 declareProperty(
"OffsetMode",
"Relative", std::make_shared<StringListValidator>(modes),
70 "Whether to calculate a relative, absolute, or signed offset");
72 "The known peak centre value from the NIST standard "
73 "information, this is only used in Absolute OffsetMode.");
77 std::map<std::string, std::string> result;
81 result[
"InputWorkspace"] =
"The InputWorkspace must be a MatrixWorkspace.";
84 const auto unit = inputWS->getAxis(0)->unit()->caption();
85 const auto unitErrorMsg =
86 "GetDetectorOffsets only supports input workspaces with units 'Bins of Shift' or 'd-Spacing', your unit was : " +
88 if (unit !=
"Bins of Shift" && unit !=
"d-Spacing") {
89 result[
"InputWorkspace"] = unitErrorMsg;
105 throw std::runtime_error(
"Must specify m_Xmin<m_Xmax");
112 if (mode_str ==
"Absolute") {
116 else if (mode_str ==
"Relative") {
120 else if (mode_str ==
"Signed") {
126 int64_t nspec =
inputW->getNumberHistograms();
128 auto outputW = std::make_shared<OffsetsWorkspace>(
inputW->getInstrument());
130 auto maskWS = std::make_shared<MaskWorkspace>(
inputW->getInstrument());
132 const detid2index_map pixel_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap(
true);
135 Progress prog(
this, 0.0, 1.0, nspec);
136 auto &spectrumInfo = maskWS->mutableSpectrumInfo();
138 for (int64_t wi = 0; wi < nspec; ++wi) {
149 const auto &dets =
inputW->getSpectrum(wi).getDetectorIDs();
155 for (
const auto &det : dets) {
156 outputW->setValue(det, offset);
157 const auto mapEntry = pixel_to_wi.find(det);
158 if (mapEntry == pixel_to_wi.end())
160 const size_t workspaceIndex = mapEntry->second;
163 maskWS->getSpectrum(workspaceIndex).clearData();
164 spectrumInfo.setMasked(workspaceIndex,
true);
165 maskWS->mutableY(workspaceIndex)[0] = mask;
168 maskWS->mutableY(workspaceIndex)[0] = mask;
182 std::string filename =
getProperty(
"GroupingFileName");
183 if (!filename.empty()) {
186 childAlg->setProperty(
"OffsetsWorkspace",
outputW);
187 childAlg->setProperty(
"MaskWorkspace", maskWS);
188 childAlg->setPropertyValue(
"Filename", filename);
189 childAlg->executeAsChildAlg();
201 const auto &yValues =
inputW->y(s);
202 auto it = std::max_element(yValues.cbegin(), yValues.cend());
205 double peakHeight = *it;
206 const double peakLoc =
inputW->x(s)[it - yValues.begin()];
210 if (std::isnan(peakHeight))
216 const auto &histogram =
inputW->histogram(s);
217 const auto &vector_x = histogram.points();
221 if (start_index != stop_index) {
223 auto bkgdFunction = std::dynamic_pointer_cast<IBackgroundFunction>(fun_ptr->getFunction(0));
224 auto peakFunction = std::dynamic_pointer_cast<IPeakFunction>(fun_ptr->getFunction(1));
225 int result =
estimatePeakParameters(histogram, std::pair<size_t, size_t>(start_index, stop_index), peakFunction,
227 if (result != PeakFitResult::GOOD) {
229 <<
" bad result for observing peak parameters, using default peak height and loc\n";
233 <<
" range size is zero in estimatePeakParameters, using default peak height and loc\n";
245 fit_alg->setProperty(
"Function", fun_ptr);
247 fit_alg->setProperty(
"InputWorkspace",
inputW);
248 fit_alg->setProperty<
int>(
"WorkspaceIndex",
249 static_cast<int>(s));
250 fit_alg->setProperty(
"StartX",
m_Xmin);
251 fit_alg->setProperty(
"EndX",
m_Xmax);
252 fit_alg->setProperty(
"MaxIterations", 100);
254 fit_alg->executeAsChildAlg();
255 std::string fitStatus = fit_alg->getProperty(
"OutputStatus");
257 if (fitStatus !=
"success")
263 double offset = function->getParameter(3);
301 peak->setHeight(peakHeight);
302 peak->setCentre(peakLoc);
303 const double sigma(10.0);
304 peak->setFwhm(2.0 * std::sqrt(2.0 * M_LN2) *
sigma);
307 fitFunc->addFunction(background);
308 fitFunc->addFunction(peak);
310 return std::shared_ptr<IFunction>(fitFunc);
#define DECLARE_ALGORITHM(classname)
#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_CRITICAL(name)
#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 PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
A composite function is a function containing other functions.
@ OptionalSave
to specify a file to write to but an empty string is
The FunctionFactory class is in charge of the creation of concrete instances of fitting functions.
std::shared_ptr< IFunction > createFunction(const std::string &type) const
Creates an instance of a function.
Helper class for reporting progress from algorithms.
A property class for workspaces.
API::MatrixWorkspace_sptr inputW
A pointer to the input workspace.
double m_Xmax
The end of the X range for fitting.
API::IFunction_sptr createFunction(const double peakHeight, const double peakLoc)
Create a function string from the given parameters and the algorithm inputs.
double m_dreference
The expected peak position in d-spacing (?)
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
void init() override
Initialisation method.
bool m_estimateFWHM
Flag to estimate fwhm fit parameter.
void exec() override
Executes the algorithm.
DataObjects::OffsetsWorkspace_sptr outputW
A pointer to the output workspace.
double m_Xmin
The start of the X range for fitting.
double m_dideal
The known peak centre value from the NIST standard.
double m_maxOffset
The maximum absolute value of offsets.
double fitSpectra(const int64_t s)
Call Gaussian as a Child Algorithm to fit the peak in a spectrum.
Exception for when an item is not found in a collection.
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 report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
MANTID_ALGORITHMS_DLL size_t findXIndex(const vector_like &vecx, const double x, const size_t startindex=0)
Get an index of a value in a sorted vector.
MANTID_ALGORITHMS_DLL int estimatePeakParameters(const HistogramData::Histogram &histogram, const std::pair< size_t, size_t > &peak_window, const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunction, bool observe_peak_width, const EstimatePeakWidth peakWidthEstimateApproach, const double peakWidthPercentage, const double minPeakHeight)
Estimate peak parameters by 'observation'.
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
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.
@ Input
An input workspace.
@ Output
An output workspace.