/** * Creates a single-spectrum workspace filled with function values for given X values * @param func :: Function to calculate values * @param xValues :: X values to use * @return Single-spectrum workspace with calculated function values */ MatrixWorkspace_sptr createWsFromFunction(IFunction_const_sptr func, const std::vector<double>& xValues) { auto inputWs = boost::dynamic_pointer_cast<MatrixWorkspace>( WorkspaceFactory::Instance().create("Workspace2D", 1, xValues.size(), xValues.size())); inputWs->dataX(0) = xValues; IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit"); fit->setChild(true); // Don't want workspace in the ADS fit->setProperty("Function", func->asString()); fit->setProperty("InputWorkspace", inputWs); fit->setProperty("MaxIterations", 0); // Don't want to fit, just calculate output workspace fit->setProperty("CreateOutput", true); fit->execute(); MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace"); IAlgorithm_sptr extract = AlgorithmManager::Instance().create("ExtractSingleSpectrum"); extract->setChild(true); // Don't want workspace in the ADS extract->setProperty("InputWorkspace", fitOutput); extract->setProperty("WorkspaceIndex", 1); // "Calc" extract->setPropertyValue("OutputWorkspace", "__NotUsed"); extract->execute(); return extract->getProperty("OutputWorkspace"); }
void ALCBaselineModellingModel::fit(IFunction_const_sptr function, const std::vector<Section> §ions) { // Create a copy of the data IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace"); clone->setChild(true); clone->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data)); clone->setProperty("OutputWorkspace", "__NotUsed__"); clone->execute(); Workspace_sptr cloned = clone->getProperty("OutputWorkspace"); MatrixWorkspace_sptr dataToFit = boost::dynamic_pointer_cast<MatrixWorkspace>(cloned); assert(dataToFit); // CloneWorkspace should take care of that disableUnwantedPoints(dataToFit, sections); IFunction_sptr funcToFit = FunctionFactory::Instance().createInitialized(function->asString()); IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit"); fit->setChild(true); fit->setProperty("Function", funcToFit); fit->setProperty("InputWorkspace", dataToFit); fit->setProperty("CreateOutput", true); // Run async so that progress can be shown Poco::ActiveResult<bool> result(fit->executeAsync()); while (!result.available()) { QCoreApplication::processEvents(); } if (!result.error().empty()) { throw std::runtime_error(result.error()); } MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace"); m_parameterTable = fit->getProperty("OutputParameters"); enableDisabledPoints(fitOutput, m_data); setErrorsAfterFit(fitOutput); setCorrectedData(fitOutput); setFittedFunction(funcToFit); m_sections = sections; }
void ALCPeakFittingModel::fitPeaks(IFunction_const_sptr peaks) { IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit"); fit->setChild(true); fit->setProperty("Function", peaks->asString()); fit->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data)); fit->setProperty("CreateOutput", true); fit->setProperty("OutputCompositeMembers", true); // Execute async so we can show progress bar Poco::ActiveResult<bool> result(fit->executeAsync()); while (!result.available()) { QCoreApplication::processEvents(); } if (!result.error().empty()) { throw std::runtime_error(result.error()); } m_data = fit->getProperty("OutputWorkspace"); m_parameterTable = fit->getProperty("OutputParameters"); setFittedPeaks(static_cast<IFunction_sptr>(fit->getProperty("Function"))); }
void ALCBaselineModellingModel::fit(IFunction_const_sptr function, const std::vector<Section>& sections) { // Create a copy of the data IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace"); clone->setChild(true); clone->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data)); clone->setProperty("OutputWorkspace", "__NotUsed__"); clone->execute(); Workspace_sptr cloned = clone->getProperty("OutputWorkspace"); MatrixWorkspace_sptr dataToFit = boost::dynamic_pointer_cast<MatrixWorkspace>(cloned); assert(dataToFit); // CloneWorkspace should take care of that disableUnwantedPoints(dataToFit, sections); IFunction_sptr funcToFit = FunctionFactory::Instance().createInitialized(function->asString()); IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit"); fit->setChild(true); fit->setProperty("Function", funcToFit); fit->setProperty("InputWorkspace", dataToFit); fit->setProperty("CreateOutput", true); fit->execute(); MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace"); m_parameterTable = fit->getProperty("OutputParameters"); enableDisabledPoints(fitOutput,m_data); setErrorsAfterFit(fitOutput); setCorrectedData(fitOutput); setFittedFunction(funcToFit); m_sections = sections; }