diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MuonAnalysis.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MuonAnalysis.h index c3ce58c0a195..2522e7a2355e 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MuonAnalysis.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MuonAnalysis.h @@ -5,12 +5,16 @@ // Includes //---------------------- #include "ui_MuonAnalysis.h" -#include "MantidQtAPI/UserSubWindow.h" -#include "MantidQtMantidWidgets/pythonCalc.h" -#include "MantidQtMantidWidgets/MWDiag.h" #include "MantidAPI/AnalysisDataService.h" #include "MantidAPI/MatrixWorkspace.h" +#include "MantidAPI/ITableWorkspace.h" + +#include "MantidGeometry/Instrument.h" + +#include "MantidQtAPI/UserSubWindow.h" +#include "MantidQtMantidWidgets/pythonCalc.h" +#include "MantidQtMantidWidgets/MWDiag.h" #include @@ -26,6 +30,9 @@ namespace Muon class MuonAnalysisResultTableTab; } +using namespace Mantid::Kernel; +using namespace Mantid::API; +using namespace Mantid::Geometry; /** This is the main class for the MuonAnalysis interface @@ -327,9 +334,12 @@ private slots: /// The last directory that was viewed QString m_last_dir; - /// name of workspace + /// Name of the loaded workspace std::string m_workspace_name; + /// Name of the loaded AND grouped workspace + std::string m_grouped_name; + /// name of the loaded data QString m_currentDataName; @@ -392,11 +402,6 @@ private slots: /// set grouping in table from information from nexus raw file void setGroupingFromNexus(const QString& nexusFile); - /// - void setDummyGrouping(const int numDetectors); - - /// - void setGroupingFromIDF(const std::string& mainFieldDirection, Mantid::API::MatrixWorkspace_sptr matrix_workspace); /// title of run std::string m_title; @@ -446,6 +451,21 @@ private slots: /// Saves the value of the widget which called the slot void loadWidgetValue(QWidget* target, const QVariant& defaultValue); + // Groups loaded workspace (m_workspace_name) + void groupLoadedWorkspace(ITableWorkspace_sptr detGroupingTable = ITableWorkspace_sptr()); + + /// Parses grouping information from the UI table. + ITableWorkspace_sptr parseGrouping(); + + /// Updated UI table using the grouping information provided. + void setGrouping(ITableWorkspace_sptr detGroupingTable); + + /// Updates UI grouping table - creates dummy grouping + void setDummyGrouping(Instrument_const_sptr instrument); + + /// Updates UI grouping table using default grouping of the instrument + void setGroupingFromIDF(Instrument_const_sptr instrument, const std::string& mainFieldDirection); + /// handles option tab work MantidQt::CustomInterfaces::Muon::MuonAnalysisOptionTab* m_optionTab; /// handles fit data work diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp index 94ef254f3ca6..1ad960d214c6 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp @@ -2893,62 +2893,50 @@ void MuonAnalysis::setGroupingFromNexus(const QString& nexusFile) /** * If nothing else work set dummy grouping and display comment to user */ -void MuonAnalysis::setDummyGrouping(const int numDetectors) +void MuonAnalysis::setDummyGrouping(Instrument_const_sptr instrument) { // if no grouping in nexus then set dummy grouping and display warning to user std::stringstream idstr; - idstr << "1-" << numDetectors; - m_uiForm.groupTable->setItem(0, 0, new QTableWidgetItem("NoGroupingDetected")); - m_uiForm.groupTable->setItem(0, 1, new QTableWidgetItem(idstr.str().c_str())); + idstr << "1-" << instrument->getNumberDetectors(); + m_uiForm.groupTable->setItem( 0, 0, new QTableWidgetItem("NoGroupingDetected") ); + m_uiForm.groupTable->setItem( 0, 1, new QTableWidgetItem( QString::fromStdString(idstr.str()) ) ); updateFrontAndCombo(); - - QMessageBox::warning(this, "MantidPlot - MuonAnalysis", QString("No grouping detected in Nexus file.\n") - + "and no default grouping file specified in IDF\n" - + "therefore dummy grouping created."); } /** * Try to load default grouping file specified in IDF */ -void MuonAnalysis::setGroupingFromIDF(const std::string& mainFieldDirection, MatrixWorkspace_sptr matrix_workspace) +void MuonAnalysis::setGroupingFromIDF(Instrument_const_sptr instrument, const std::string& mainFieldDirection) { - Instrument_const_sptr inst = matrix_workspace->getInstrument(); - - QString instname = m_uiForm.instrSelector->currentText().toUpper(); + std::string parameterName = "Default grouping file"; - QString groupParameter = "Default grouping file"; - // for now hard coded in the special case of MUSR - if (instname == "MUSR") + // Special case for MUSR, because it has two possible groupings + if (instrument->getName() == "MUSR") { - if ( mainFieldDirection == "Transverse" ) - groupParameter += " - Transverse"; - else - groupParameter += " - Longitudinal"; + parameterName.append(" - " + mainFieldDirection); } - std::vector groupFile = inst->getStringParameter(groupParameter.toStdString()); + std::vector groupingFiles = instrument->getStringParameter(parameterName); - // get search directory for XML instrument definition files (IDFs) + // Get search directory for XML instrument definition files (IDFs) std::string directoryName = ConfigService::Instance().getInstrumentDirectory(); - if ( groupFile.size() == 1 ) + if ( groupingFiles.size() == 1 ) { - Grouping loadedGrouping; + const std::string groupingFile = groupingFiles[0]; try { - loadGroupingFromXML(directoryName+groupFile[0], loadedGrouping); + Grouping loadedGrouping; + loadGroupingFromXML(directoryName + groupingFile, loadedGrouping); + fillGroupingTable(loadedGrouping, m_uiForm); } catch (...) { - QMessageBox::warning(this, "MantidPlot - MuonAnalysis", - QString("Can't load default grouping file in IDF. \n With name: ") + groupFile[0].c_str()); - return; + g_log.error("Can't load default grouping file: " + groupingFile); } - - fillGroupingTable(loadedGrouping, m_uiForm); } } @@ -3896,5 +3884,104 @@ void MuonAnalysis::setFirstGoodDataState(int checkBoxState) } } +/** + * Groups loaded workspace (m_workspace_name). Grouped workspace is stored under m_grouped_name. + * @param detGroupingTable :: Grouping information to use. If null - info from table widget is used + */ +void MuonAnalysis::groupLoadedWorkspace(ITableWorkspace_sptr detGroupingTable) +{ + if ( ! detGroupingTable ) + { + auto groupingFromUI = parseGrouping(); + + if ( ! groupingFromUI ) + throw std::invalid_argument("Unable to parse grouping information from the table, or it is empty."); + + detGroupingTable = groupingFromUI; + } + + try + { + IAlgorithm_sptr groupAlg = AlgorithmManager::Instance().createUnmanaged("MuonGroupDetectors"); + groupAlg->initialize(); + groupAlg->setRethrows(true); + groupAlg->setPropertyValue("InputWorkspace", m_workspace_name); + groupAlg->setPropertyValue("OutputWorkspace", m_grouped_name); + + if ( detGroupingTable->name().empty() ) + { + ScopedWorkspace table(detGroupingTable); + groupAlg->setPropertyValue("DetectorGroupingTable", table.name()); + groupAlg->execute(); + } + else + { + groupAlg->setPropertyValue("DetectorGroupingTable", detGroupingTable->name()); + groupAlg->execute(); + } + } + catch(std::exception& e) + { + throw std::runtime_error( "Unable to group loaded workspace:\n\n" + std::string(e.what()) ); + } +} + +/** + * Parses grouping information from the UI table. + * @return ITableWorkspace of the format returned by LoadMuonNexus + */ +ITableWorkspace_sptr MuonAnalysis::parseGrouping() +{ + std::vector groupRows; + whichGroupToWhichRow(m_uiForm, groupRows); + + if ( groupRows.size() == 0 ) + return ITableWorkspace_sptr(); + + auto newTable = boost::dynamic_pointer_cast( + WorkspaceFactory::Instance().createTable("TableWorkspace") ); + + newTable->addColumn("vector_int", "Detectors"); + + for ( auto it = groupRows.begin(); it != groupRows.end(); ++it ) + { + const std::string detectorsString = m_uiForm.groupTable->item(*it,1)->text().toStdString(); + + TableRow newRow = newTable->appendRow(); + newRow << spectrumIDs(detectorsString); + } + + return newTable; +} + +/** + * Updated UI table using the grouping information provided. + * @param detGroupingTable :: Grouping information in the format as returned by LoadMuonNexus + */ +void MuonAnalysis::setGrouping(ITableWorkspace_sptr detGroupingTable) +{ + for ( size_t row = 0; row < detGroupingTable->rowCount(); ++row ) + { + const std::vector& detectors = detGroupingTable->cell< std::vector >(row,0); + + const std::string& detectorRange = Strings::join(detectors.begin(), detectors.end(), ","); + + m_uiForm.groupTable->setItem( static_cast(row), 0, + new QTableWidgetItem( QString::number(row + 1) ) ); + + m_uiForm.groupTable->setItem( static_cast(row), 1, + new QTableWidgetItem( QString::fromStdString(detectorRange) ) ); + } + + if ( numGroups() == 2 && numPairs() <= 0 ) + { + m_uiForm.pairTable->setItem( 0, 0, new QTableWidgetItem("long") ); + m_uiForm.pairTable->setItem( 0, 3, new QTableWidgetItem("1.0") ); + } + + updatePairTable(); + updateFrontAndCombo(); +} + }//namespace MantidQT }//namespace CustomInterfaces