Skip to content

Commit

Permalink
Refs #8020. Add methods for loading files and applying DTC.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Feb 20, 2014
1 parent 1a58e26 commit 619d1d2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Expand Up @@ -34,6 +34,15 @@ namespace Muon
class MuonAnalysisOptionTab;
class MuonAnalysisFitDataTab;
class MuonAnalysisResultTableTab;

struct LoadResult {
Workspace_sptr loadedWorkspace;
Workspace_sptr loadedGrouping;
Workspace_sptr loadedDeadTimes;
std::string mainFieldDirection;
double timeZero;
double firstGoodData;
};
}

using namespace Muon;
Expand Down Expand Up @@ -248,6 +257,9 @@ private slots:
/// Input file changed - update GUI accordingly
void inputFileChanged(const QStringList& filenames);

/// Loads the given list of files
boost::shared_ptr<LoadResult> load(const QStringList& files) const;

/// Set whether the loading buttons and MWRunFiles widget are enabled.
void allowLoading(bool enabled);

Expand Down Expand Up @@ -324,6 +336,9 @@ private slots:
/// Loads dead time table (group of tables) from the file.
Workspace_sptr loadDeadTimes(const std::string& filename) const;

/// Applies dead time correction to the loaded workspace
void applyDeadTimeCorrection(boost::shared_ptr<LoadResult> loadResult) const;

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

Expand Down
111 changes: 111 additions & 0 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Expand Up @@ -1347,6 +1347,75 @@ void MuonAnalysis::handleInputFileChanges()
}
}

/**
* Loads the given list of files
* @param files :: A list of files to load
* @return Struct with various loaded parameters
*/
boost::shared_ptr<LoadResult> MuonAnalysis::load(const QStringList& files) const
{
if ( files.empty() )
throw std::invalid_argument("Supplied list of files is empty");

auto result = boost::make_shared<LoadResult>();

std::vector<Workspace_sptr> loadedWorkspaces;

// TODO: get instrument and check if is a valid Muon instrument

// Go through all the files and try to load them
for ( auto f = files.constBegin(); f != files.constEnd(); ++f )
{
std::string file = (*f).toStdString();

// Setup Load Nexus Algorithm
IAlgorithm_sptr load = AlgorithmManager::Instance().createUnmanaged("LoadMuonNexus");
load->initialize();
load->setChild(true);
load->setPropertyValue("Filename", file );

// Just to pass validation
load->setPropertyValue("OutputWorkspace", "__NotUsed");

if ( f == files.constBegin() )
{
// These are only needed for the first file
load->setPropertyValue("DeadTimeTable", "__NotUsed");
load->setPropertyValue("DetectorGroupingTable", "__NotUsed");
}

load->execute();

loadedWorkspaces.push_back( load->getProperty("OutputWorkspace") );

if ( f == files.constBegin() )
{
result->loadedDeadTimes = load->getProperty("DeadTimeTable");
result->loadedGrouping = load->getProperty("DetectorGroupingTable");
result->mainFieldDirection = static_cast<std::string>(load->getProperty("MainFieldDirection"));
result->timeZero = load->getProperty("TimeZero");
result->firstGoodData = load->getProperty("FirstGoodData");
}
}

/* TODO:
if (instrument == "ARGUS")
{
// Some of the ARGUS data files contain wrong information about the instrument main field
// direction. It is alway longitudinal.
result->setProperty("MainFieldDirection", "longitudinal");
}
*/

// Acquire a single loaded worksace to work with. If multiple loaded - sum them to get one
if ( loadedWorkspaces.size() == 1 )
result->loadedWorkspace = loadedWorkspaces.front();
else
result->loadedWorkspace = sumWorkspaces(loadedWorkspaces);

return result;
}

/**
* Input file changed. Update GUI accordingly. Note this method does no check of input filename assumed
* done elsewhere depending on e.g. whether filename came from MWRunFiles or 'get current run' button.
Expand Down Expand Up @@ -3627,6 +3696,48 @@ Workspace_sptr MuonAnalysis::loadDeadTimes(const std::string& filename) const
}
}

/**
* Applies dead time correction to the loaded workspace. Updates loadedWorkspace in loadResult.
* @param loadResult :: Struct with loaded parameters
*/
void MuonAnalysis::applyDeadTimeCorrection(boost::shared_ptr<LoadResult> loadResult) const
{
if (m_uiForm.deadTimeType->currentText() != "None")
{
// Dead time table which will be used
Workspace_sptr deadTimes;

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

deadTimes = loadResult->loadedDeadTimes;
}
else if (m_uiForm.deadTimeType->currentText() == "From Disk")
{
deadTimes = loadDeadTimes( deadTimeFilename() );
}

// Add workspaces to ADS so that they can be processed correctly in case they are groups
ScopedWorkspace loadedWsEntry(loadResult->loadedWorkspace);
ScopedWorkspace deadTimesEntry(deadTimes);

ScopedWorkspace correctedWsEntry;

IAlgorithm_sptr applyCorrAlg = AlgorithmManager::Instance().createUnmanaged("ApplyDeadTimeCorr");
applyCorrAlg->initialize();
applyCorrAlg->setRethrows(true);
applyCorrAlg->setLogging(false);
applyCorrAlg->setPropertyValue("InputWorkspace", loadedWsEntry.name());
applyCorrAlg->setPropertyValue("DeadTimeTable", deadTimesEntry.name());
applyCorrAlg->setPropertyValue("OutputWorkspace", correctedWsEntry.name());
applyCorrAlg->execute();

loadResult->loadedWorkspace = correctedWsEntry.retrieve();
}
}

/**
* Creates and algorithm with all the properties set according to widget values on the interface.
* @return The algorithm with properties set
Expand Down

0 comments on commit 619d1d2

Please sign in to comment.