Skip to content

Commit

Permalink
Refs #6473. Merge branch 'feature/6473_muon_seq_fitting' into develop
Browse files Browse the repository at this point in the history
Conflicts:
	Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MuonAnalysis.h
	Code/Mantid/MantidQt/MantidWidgets/CMakeLists.txt
  • Loading branch information
arturbekasov committed Dec 13, 2013
2 parents ed1e2ab + cccb358 commit 4ecd5ff
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 62 deletions.
Expand Up @@ -208,6 +208,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 @@ -314,6 +316,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();

/// Plots specific WS spectrum (used by plotPair and plotGroup)
void plotSpectrum(const QString& wsName, const int wsIndex, bool logScale = false);

Expand Down
225 changes: 198 additions & 27 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 @@ -499,10 +502,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 @@ -567,7 +567,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 @@ -1526,38 +1526,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 @@ -3600,5 +3590,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 table) 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 @@ -50,6 +50,9 @@ public slots:
/// Open sequential fit dialog
virtual void sequentialFit();

signals:
/// Emitted when sequential fit is requested by user
void sequentialFitRequested();

protected:
virtual void showEvent(QShowEvent* e);
Expand Down
Expand Up @@ -45,7 +45,7 @@ namespace MantidWidgets
Q_OBJECT

public:
MuonSequentialFitDialog(MuonFitPropertyBrowser* fitPropBrowser);
MuonSequentialFitDialog(MuonFitPropertyBrowser* fitPropBrowser, Algorithm_sptr loadAlg);
virtual ~MuonSequentialFitDialog();

enum DialogState
Expand Down Expand Up @@ -88,6 +88,9 @@ namespace MantidWidgets
/// Whether user requested fitting to be stopped
bool m_stopRequested;

/// Algorithm the dialog should use for loading
Algorithm_sptr m_loadAlg;

// -- STATIC MEMBERS ------------------------------------------------------

/// Checks if specified name is valid as a name for label.
Expand Down
@@ -1,5 +1,4 @@
#include "MantidQtMantidWidgets/MuonFitPropertyBrowser.h"
#include "MantidQtMantidWidgets/MuonSequentialFitDialog.h"
#include "MantidQtMantidWidgets/PropertyHandler.h"
#include "MantidAPI/FunctionFactory.h"

Expand Down Expand Up @@ -316,8 +315,7 @@ void MuonFitPropertyBrowser::fit()
*/
void MuonFitPropertyBrowser::sequentialFit()
{
MuonSequentialFitDialog* dialog = new MuonSequentialFitDialog(this);
dialog->exec();
emit sequentialFitRequested();
}

/**
Expand Down Expand Up @@ -351,6 +349,5 @@ bool MuonFitPropertyBrowser::isWorkspaceValid(Mantid::API::Workspace_sptr ws)con
return false;
}


} // MantidQt
} // API

0 comments on commit 4ecd5ff

Please sign in to comment.