Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/6473_muon_seq_fitting'
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Dec 13, 2013
2 parents 0aac32d + 3d5a2b6 commit 953f539
Show file tree
Hide file tree
Showing 11 changed files with 1,100 additions and 63 deletions.
Expand Up @@ -214,6 +214,8 @@ private slots:
/// Saves the value of the widget which called the slot
void saveWidgetValue();

/// Opens a sequential fit dialog
void openSequentialFitDialog();

private:

Expand Down Expand Up @@ -320,6 +322,15 @@ private slots:
/// Return number of groups defined (not including pairs)
int numGroups();

/// Returns custom dead time table file name as set on the interface
std::string deadTimeFilename();

/// Loads dead time table (group of tables) from the file.
Workspace_sptr loadDeadTimes(const std::string& filename);

/// Creates and algorithm with all the properties set according to widget values on the interface
Algorithm_sptr createLoadAlgorithm();

// TODO: wsIndex can be removed from functions below if we put only one group to the workspace
// (as we are doing with pairs)

Expand Down
233 changes: 202 additions & 31 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Expand Up @@ -28,6 +28,7 @@
#include "MantidQtCustomInterfaces/MuonAnalysisResultTableTab.h"
#include "MantidQtMantidWidgets/FitPropertyBrowser.h"
#include "MantidQtMantidWidgets/MuonFitPropertyBrowser.h"
#include "MantidQtMantidWidgets/MuonSequentialFitDialog.h"

#include <Poco/File.h>
#include <Poco/Path.h>
Expand Down Expand Up @@ -148,6 +149,8 @@ void MuonAnalysis::initLayout()
}

m_uiForm.fitBrowser->init();
connect( m_uiForm.fitBrowser, SIGNAL(sequentialFitRequested()),
this, SLOT(openSequentialFitDialog()) );

// alow appending files
m_uiForm.mwRunFiles->allowMultipleFiles(true);
Expand Down Expand Up @@ -367,7 +370,7 @@ void MuonAnalysis::plotItem(ItemType itemType, int tableRow, PlotType plotType)

// Find names for new workspaces
const std::string wsName = getNewAnalysisWSName(groupName, itemType, tableRow, plotType);
const std::string wsRawName = wsName + "; Raw";
const std::string wsRawName = wsName + "_Raw";

// Make sure they end up in the ADS
ads.addOrReplace(wsName, ws);
Expand Down Expand Up @@ -395,7 +398,7 @@ void MuonAnalysis::plotItem(ItemType itemType, int tableRow, PlotType plotType)

setCurrentDataName( wsNameQ );
}
catch(std::exception& e)
catch(...)
{
QMessageBox::critical( this, "MuonAnalysis - Error", "Unable to plot the item. Check log for details." );
}
Expand Down Expand Up @@ -505,10 +508,7 @@ MatrixWorkspace_sptr MuonAnalysis::createAnalysisWorkspace(ItemType itemType, in

alg->initialize();

// TODO: should really be global
const std::string loadedWSName = m_workspace_name + "Grouped";

auto loadedWS = AnalysisDataService::Instance().retrieveWS<Workspace>(loadedWSName);
auto loadedWS = AnalysisDataService::Instance().retrieveWS<Workspace>(m_grouped_name);

if ( auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(loadedWS) )
{
Expand Down Expand Up @@ -573,7 +573,7 @@ MatrixWorkspace_sptr MuonAnalysis::createAnalysisWorkspace(ItemType itemType, in

QTableWidget* t = m_uiForm.pairTable;

double alpha = t->item(m_pairTableRowInFocus,3)->text().toDouble();
double alpha = t->item(tableRow,3)->text().toDouble();
int index1 = static_cast<QComboBox*>( t->cellWidget(tableRow,1) )->currentIndex();
int index2 = static_cast<QComboBox*>( t->cellWidget(tableRow,2) )->currentIndex();

Expand Down Expand Up @@ -1532,38 +1532,28 @@ void MuonAnalysis::inputFileChanged(const QStringList& files)
if (m_uiForm.instrSelector->currentText().toUpper() == "ARGUS")
throw std::runtime_error("Dead times are currently not implemented in ARGUS files.");

IAlgorithm_sptr applyCorrAlg = AlgorithmManager::Instance().create("ApplyDeadTimeCorr");

applyCorrAlg->setPropertyValue("InputWorkspace", m_workspace_name);
applyCorrAlg->setPropertyValue("OutputWorkspace", m_workspace_name);

ScopedWorkspace customDeadTimes;
ScopedWorkspace deadTimes;

if (m_uiForm.deadTimeType->currentIndex() == 1) // From Run Data
{
if( ! loadedDeadTimes )
throw std::runtime_error("Data file doesn't appear to contain dead time values");

applyCorrAlg->setPropertyValue("DeadTimeTable", loadedDeadTimes.name());

Workspace_sptr ws = loadedDeadTimes.retrieve();
loadedDeadTimes.remove();

deadTimes.set(ws);
}
else if (m_uiForm.deadTimeType->currentIndex() == 2) // From Specified File
{
if(!m_uiForm.mwRunDeadTimeFile->isValid())
throw std::runtime_error("Specified Dead Time file is not valid.");

std::string deadTimeFile = m_uiForm.mwRunDeadTimeFile->getFirstFilename().toStdString();

IAlgorithm_sptr loadDeadTimes = AlgorithmManager::Instance().create("LoadNexusProcessed");
loadDeadTimes->setPropertyValue("Filename", deadTimeFile);
loadDeadTimes->setPropertyValue("OutputWorkspace", customDeadTimes.name());
loadDeadTimes->execute();

if ( ! customDeadTimes )
throw std::runtime_error("Unable to load dead times from the spefied file");

applyCorrAlg->setPropertyValue("DeadTimeTable", customDeadTimes.name());
Workspace_sptr ws = loadDeadTimes( deadTimeFilename() );
deadTimes.set(ws);
}

IAlgorithm_sptr applyCorrAlg = AlgorithmManager::Instance().create("ApplyDeadTimeCorr");
applyCorrAlg->setPropertyValue("InputWorkspace", m_workspace_name);
applyCorrAlg->setPropertyValue("OutputWorkspace", m_workspace_name);
applyCorrAlg->setPropertyValue("DeadTimeTable", deadTimes.name());
applyCorrAlg->execute();
}
catch(std::exception& e)
Expand Down Expand Up @@ -1768,10 +1758,10 @@ void MuonAnalysis::inputFileChanged(const QStringList& files)
infoStr += ss.str();
}
else // Show appropriate error message.
infoStr += "Errror - Not set in data file.";
infoStr += "Error - Not set in data file.";
}
else // Show appropriate error message.
infoStr += "Errror - Not found in data file.";
infoStr += "Error - Not found in data file.";

// Include all the run information.
m_uiForm.infoBrowser->setText(infoStr.c_str());
Expand Down Expand Up @@ -3678,5 +3668,186 @@ void MuonAnalysis::setGrouping(ITableWorkspace_sptr detGroupingTable)
updateFrontAndCombo();
}

/**
* Opens a sequential fit dialog.
*/
void MuonAnalysis::openSequentialFitDialog()
{
Algorithm_sptr loadAlg;

try
{
loadAlg = createLoadAlgorithm();
}
catch(...)
{
QMessageBox::critical(this, "Unable to open dialog", "Error while setting load properties");
return;
}

m_uiForm.fitBrowser->blockSignals(true);

MuonSequentialFitDialog* dialog = new MuonSequentialFitDialog(m_uiForm.fitBrowser, loadAlg);
dialog->exec();

m_uiForm.fitBrowser->blockSignals(false);
}

/**
* Returns custom dead time table file name as set on the interface.
* @return The filename
*/
std::string MuonAnalysis::deadTimeFilename()
{
if(!m_uiForm.mwRunDeadTimeFile->isValid())
throw std::runtime_error("Specified Dead Time file is not valid.");

return m_uiForm.mwRunDeadTimeFile->getFirstFilename().toStdString();
}

/**
* Loads dead time table (group of tables) from the file.
* @param filename :: File to load dead times from
* @return Table (group of tables) with dead times
*/
Workspace_sptr MuonAnalysis::loadDeadTimes(const std::string& filename)
{
try
{
IAlgorithm_sptr loadDeadTimes = AlgorithmManager::Instance().create("LoadNexusProcessed");
loadDeadTimes->setChild(true);
loadDeadTimes->setPropertyValue("Filename", filename);
loadDeadTimes->setPropertyValue("OutputWorkspace", "__NotUsed");
loadDeadTimes->execute();

return loadDeadTimes->getProperty("OutputWorkspace");
}
catch(...)
{
throw std::runtime_error("Unable to load dead times from the spefied file");
}
}

/**
* Creates and algorithm with all the properties set according to widget values on the interface.
* @return The algorithm with properties set
*/
Algorithm_sptr MuonAnalysis::createLoadAlgorithm()
{
Algorithm_sptr loadAlg = AlgorithmManager::Instance().createUnmanaged("MuonLoad");
loadAlg->initialize();

// -- Dead Time Correction --------------------------------------------------

if (m_uiForm.deadTimeType->currentIndex() != 0)
{
loadAlg->setProperty("ApplyDeadTimeCorrection", true);

if (m_uiForm.deadTimeType->currentIndex() == 2) // From Specified File
{

Workspace_sptr deadTimes = loadDeadTimes( deadTimeFilename() );

loadAlg->setProperty("CustomDeadTimeTable", deadTimes);
}
}

// -- Grouping --------------------------------------------------------------

ITableWorkspace_sptr grouping = parseGrouping();
loadAlg->setProperty("DetectorGroupingTable", grouping);

// -- X axis options --------------------------------------------------------

double Xmin = m_uiForm.timeAxisStartAtInput->text().toDouble();
loadAlg->setProperty("Xmin", Xmin);

double Xmax = m_uiForm.timeAxisFinishAtInput->text().toDouble();
loadAlg->setProperty("Xmax", Xmax);

double timeZero = m_uiForm.timeZeroFront->text().toDouble();
loadAlg->setProperty("TimeZero", timeZero);

// -- Rebin options ---------------------------------------------------------

if ( m_uiForm.rebinComboBox->currentIndex() != 0)
{
std::string rebinParams;

if(m_uiForm.rebinComboBox->currentIndex() == 1) // Fixed
{
auto loadedWS = AnalysisDataService::Instance().retrieveWS<Workspace>(m_grouped_name);
MatrixWorkspace_sptr ws;

if ( ! ( ws = boost::dynamic_pointer_cast<MatrixWorkspace>(loadedWS) ) )
{
auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(loadedWS);
ws = boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(0));
}

double binSize = ws->dataX(0)[1] - ws->dataX(0)[0];

double bunchedBinSize = binSize * m_uiForm.optionStepSizeText->text().toDouble();

rebinParams = boost::lexical_cast<std::string>(bunchedBinSize);
}
else // Variable
{
rebinParams = m_uiForm.binBoundaries->text().toStdString();
}

loadAlg->setPropertyValue("RebinParams", rebinParams);
}

// -- Group/pair properties -------------------------------------------------

int index = m_uiForm.frontGroupGroupPairComboBox->currentIndex();

if (index >= numGroups())
{
loadAlg->setProperty("OutputType", "PairAsymmetry");
int tableRow = m_pairToRow[index - numGroups()];

QTableWidget* t = m_uiForm.pairTable;

double alpha = t->item(tableRow,3)->text().toDouble();
int index1 = static_cast<QComboBox*>( t->cellWidget(tableRow,1) )->currentIndex();
int index2 = static_cast<QComboBox*>( t->cellWidget(tableRow,2) )->currentIndex();

loadAlg->setProperty("PairFirstIndex", index1);
loadAlg->setProperty("PairSecondIndex", index2);
loadAlg->setProperty("Alpha", alpha);
}
else
{
if ( parsePlotType(m_uiForm.frontPlotFuncs) == Asymmetry )
loadAlg->setProperty("OutputType", "GroupAsymmetry");
else
loadAlg->setProperty("OutputType", "GroupCounts");

int groupIndex = getGroupNumberFromRow(m_groupToRow[index]);
loadAlg->setProperty("GroupIndex", groupIndex);
}

// -- Period options --------------------------------------------------------

QString periodLabel1 = m_uiForm.homePeriodBox1->currentText();

int periodIndex1 = periodLabel1.toInt() - 1;
loadAlg->setProperty("FirstPeriod", periodIndex1);

QString periodLabel2 = m_uiForm.homePeriodBox2->currentText();
if ( periodLabel2 != "None" )
{
int periodIndex2 = periodLabel2.toInt() - 1;
loadAlg->setProperty("SecondPeriod", periodIndex2);

std::string op = m_uiForm.homePeriodBoxMath->currentText().toStdString();
loadAlg->setProperty("PeriodOperation", op);
}

return loadAlg;
}

}//namespace MantidQT
}//namespace CustomInterfaces
Expand Up @@ -9,6 +9,7 @@
#include "MantidQtAPI/UserSubWindow.h"

#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include <QLineEdit>
#include <QFileDialog>
Expand All @@ -27,8 +28,10 @@ namespace CustomInterfaces
{
namespace Muon
{
using namespace MantidQt::API;
using namespace Mantid::Kernel;
using namespace Mantid::API;

using namespace MantidQt::API;
using namespace MantidQt::MantidWidgets;

const std::string MuonAnalysisResultTableTab::RUN_NO_LOG = "run_number";
Expand Down Expand Up @@ -207,13 +210,47 @@ void MuonAnalysisResultTableTab::populateTables(const QStringList& wsList)
// Clear the previous table values
m_logValues.clear();
QVector<QString> fittedWsList;

// Get all the workspaces from the fitPropertyBrowser and find out whether they have had fitting done to them.
for (int i(0); i<wsList.size(); ++i)
{
if((Mantid::API::AnalysisDataService::Instance().doesExist(wsList[i].toStdString() + "_Parameters"))&&(Mantid::API::AnalysisDataService::Instance().doesExist(wsList[i].toStdString())))
fittedWsList.append(wsList[i]);
}

// Add all the fittings made by seq. fit dialog
std::map<std::string, Workspace_sptr> items =
Mantid::API::AnalysisDataService::Instance().topLevelItems();

for ( auto it = items.begin(); it != items.end(); ++it )
{
if ( ! boost::starts_with(it->first, "MuonSeqFit_") )
continue;

auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(it->second);

if ( ! group )
continue;

for ( size_t i = 0; i < group->size(); ++i )
{
auto ws = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(i) );

if ( ! ws )
continue;

std::string name = ws->name();
size_t pos = name.find("_Workspace");

if ( pos == std::string::npos)
continue;

name.erase(pos);

fittedWsList << QString::fromStdString(name);
}
}

if(fittedWsList.size() > 0)
{
// Make sure all params match.
Expand Down

0 comments on commit 953f539

Please sign in to comment.