From 0e6598dc310d9d78f1e2102c07707f122cda3f2b Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Thu, 2 Oct 2014 09:59:09 +0100 Subject: [PATCH] Refs #10235 Autofill empty columns in Refl UI * Extract TwoTheta from the log when able. * Calculate DQQ using TwoTheta when able. --- .../ReflMainViewPresenter.h | 2 + .../src/ReflMainViewPresenter.cpp | 78 +++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ReflMainViewPresenter.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ReflMainViewPresenter.h index e6c30fd0670e..6bc58f8f5eb2 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ReflMainViewPresenter.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ReflMainViewPresenter.h @@ -61,6 +61,8 @@ namespace MantidQt Mantid::API::MatrixWorkspace_sptr makeTransWS(const std::string& transString); //Validate a row void validateRow(size_t rowNo) const; + //Autofill a row with sensible values + void autofillRow(size_t rowNo); //Process a row void processRow(size_t rowNo); //Stitch some rows diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/ReflMainViewPresenter.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/ReflMainViewPresenter.cpp index 21ade7821f41..f6b4854facdc 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/ReflMainViewPresenter.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/ReflMainViewPresenter.cpp @@ -1,8 +1,9 @@ #include "MantidQtCustomInterfaces/ReflMainViewPresenter.h" +#include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/ITableWorkspace.h" #include "MantidQtCustomInterfaces/ReflMainView.h" -#include "MantidAPI/AlgorithmManager.h" -#include "MantidKernel/PropertyWithValue.h" +#include "MantidKernel/Strings.h" +#include "MantidKernel/TimeSeriesProperty.h" #include @@ -55,6 +56,7 @@ namespace MantidQt try { validateRow(*it); + autofillRow(*it); const int group = m_model->Int(*it, COL_GROUP); groups[group].push_back(*it); @@ -112,24 +114,24 @@ namespace MantidQt } /** - Validate a row + Validate a row. + If a row passes validation, it is ready to be autofilled, but + not necessarily ready for processing. @param rowNo : The row in the model to validate @throws std::invalid_argument if the row fails validation */ void ReflMainViewPresenter::validateRow(size_t rowNo) const { + if(rowNo >= m_model->rowCount()) + throw std::invalid_argument("Invalid row"); + const std::string runStr = m_model->String(rowNo, COL_RUNS); - const std::string dqqStr = m_model->String(rowNo, COL_DQQ); - const std::string thetaStr = m_model->String(rowNo, COL_ANGLE); const std::string qMinStr = m_model->String(rowNo, COL_QMIN); const std::string qMaxStr = m_model->String(rowNo, COL_QMAX); if(runStr.empty()) throw std::invalid_argument("Run column may not be empty."); - if(dqqStr.empty() && thetaStr.empty()) - throw std::invalid_argument("Theta and dQ/Q columns may not BOTH be empty."); - if(qMinStr.empty()) throw std::invalid_argument("Qmin column may not be empty."); @@ -137,6 +139,66 @@ namespace MantidQt throw std::invalid_argument("Qmax column may not be empty."); } + /** + Autofill a row + @param rowNo : The row in the model to autofill + @throws std::runtime_error if the row could not be auto-filled + */ + void ReflMainViewPresenter::autofillRow(size_t rowNo) + { + if(rowNo >= m_model->rowCount()) + throw std::runtime_error("Invalid row"); + + const std::string runStr = m_model->String(rowNo, COL_RUNS); + MatrixWorkspace_sptr run = boost::dynamic_pointer_cast(loadRun(runStr, m_view->getProcessInstrument())); + + //Fetch two theta from the log if needed + if(m_model->String(rowNo, COL_ANGLE).empty()) + { + Property* logData = NULL; + + //First try TwoTheta + try + { + logData = run->mutableRun().getLogData("Theta"); + } + catch(std::exception&) + { + throw std::runtime_error("Value for two theta could not be found in log."); + } + + auto logPWV = dynamic_cast*>(logData); + auto logTSP = dynamic_cast*>(logData); + + double thetaVal; + if(logPWV) + thetaVal = *logPWV; + else if(logTSP && logTSP->realSize() > 0) + thetaVal = logTSP->lastValue(); + else + throw std::runtime_error("Value for two theta could not be found in log."); + + //Update the model + m_model->String(rowNo, COL_ANGLE) = Strings::toString(thetaVal); + } + + //If we need to calculate the resolution, do. + if(m_model->String(rowNo, COL_DQQ).empty()) + { + IAlgorithm_sptr calcResAlg = AlgorithmManager::Instance().create("CalculateResolution"); + calcResAlg->setProperty("Workspace", run); + calcResAlg->setProperty("TwoTheta", m_model->String(rowNo, COL_ANGLE)); + calcResAlg->execute(); + + //Update the model + double dqqVal = calcResAlg->getProperty("Resolution"); + m_model->String(rowNo, COL_DQQ) = Strings::toString(dqqVal); + } + + //Make sure the view updates + m_view->showTable(m_model); + } + /** Extracts the run number of a workspace @param ws : The workspace to fetch the run number from