diff --git a/Code/Mantid/Framework/WorkflowAlgorithms/inc/MantidWorkflowAlgorithms/MuonLoad.h b/Code/Mantid/Framework/WorkflowAlgorithms/inc/MantidWorkflowAlgorithms/MuonLoad.h index 4a33a215abe1..4daa9498cb64 100644 --- a/Code/Mantid/Framework/WorkflowAlgorithms/inc/MantidWorkflowAlgorithms/MuonLoad.h +++ b/Code/Mantid/Framework/WorkflowAlgorithms/inc/MantidWorkflowAlgorithms/MuonLoad.h @@ -8,6 +8,8 @@ namespace Mantid { namespace WorkflowAlgorithms { + using namespace Kernel; + using namespace API; /** MuonLoad : loads Muon workspace ready for analysis. @@ -46,7 +48,11 @@ namespace WorkflowAlgorithms void init(); void exec(); + /// Returns a workspace for the first period as specified using FirstPeriod property. + MatrixWorkspace_sptr getFirstPeriodWS(WorkspaceGroup_sptr ws); + /// Returns a workspace for the second period as specified using SecondPeriod property. + MatrixWorkspace_sptr getSecondPeriodWS(WorkspaceGroup_sptr ws); }; diff --git a/Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoad.cpp b/Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoad.cpp index b845cfedf0ff..d7a61c3273e1 100644 --- a/Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoad.cpp +++ b/Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoad.cpp @@ -65,7 +65,7 @@ namespace WorkflowAlgorithms declareProperty(new FileProperty("Filename", "", FileProperty::Load, ".nxs"), "The name of the Nexus file to load" ); - declareProperty("FirstPeriod", EMPTY_INT(), "Group index of the first period workspace to use"); + declareProperty("FirstPeriod", 0, "Group index of the first period workspace to use"); declareProperty("SecondPeriod", EMPTY_INT(), "Group index of the first period workspace to use"); std::vector allowedOperations; @@ -78,8 +78,9 @@ namespace WorkflowAlgorithms declareProperty(new FileProperty("CustomDeadTimeFile", "", FileProperty::OptionalLoad, ".nxs"), "Nexus file with custom dead time table. See LoadMuonNexus for format expected."); - declareProperty(new WorkspaceProperty("DetectorGroupingTable","",Direction::Input), - "Table with detector grouping information. See LoadMuonNexus for format expected."); + declareProperty(new WorkspaceProperty("DetectorGroupingTable","",Direction::Input, + PropertyMode::Optional), "Table with detector grouping information." + " See LoadMuonNexus for format expected."); declareProperty("TimeZero", EMPTY_DBL(), "Value used for Time Zero correction."); declareProperty(new ArrayProperty("RebinParams"), @@ -96,7 +97,7 @@ namespace WorkflowAlgorithms declareProperty("PairFirstIndex", EMPTY_INT(), "Workspace index of the first pair group"); declareProperty("PairSecondIndex", EMPTY_INT(), "Workspace index of the second pair group"); - declareProperty("PairAlpha", 1.0, "Alpha value of the pair"); + declareProperty("Alpha", 1.0, "Alpha value of the pair"); declareProperty("GroupIndex", EMPTY_INT(), "Workspace index of the group"); @@ -110,10 +111,112 @@ namespace WorkflowAlgorithms */ void MuonLoad::exec() { - // TODO Auto-generated execute stub + const std::string filename = getProperty("Filename"); + + // Load the file + IAlgorithm_sptr load = createChildAlgorithm("LoadMuonNexus"); + load->setProperty("Filename", filename); + load->execute(); + + Workspace_sptr loadedWS = load->getProperty("OutputWorkspace"); + + MatrixWorkspace_sptr firstPeriodWS, secondPeriodWS; + + // Deal with single-period workspace + if ( auto ws = boost::dynamic_pointer_cast(loadedWS) ) + { + if ( static_cast( getProperty("FirstPeriod") ) != 0 ) + throw std::invalid_argument("Single period data but first period is not 0."); + + if ( static_cast( getProperty("SecondPeriod") ) != EMPTY_INT() ) + throw std::invalid_argument("Single period data but second period specified"); + + firstPeriodWS = ws; + } + // Deal with multi-period workspace + else if ( auto group = boost::dynamic_pointer_cast(loadedWS) ) + { + firstPeriodWS = getFirstPeriodWS(group); + secondPeriodWS = getSecondPeriodWS(group); + } + // Unexpected workspace type + else + { + throw std::runtime_error("Loaded workspace is of invalid type"); + } + + IAlgorithm_sptr calcAssym = createChildAlgorithm("MuonCalculateAsymmetry"); + + // Set period workspaces + calcAssym->setProperty("FirstPeriodWorkspace", firstPeriodWS); + calcAssym->setProperty("SecondPeriodWorkspace", secondPeriodWS); + + // Copy similar properties over + calcAssym->setProperty("PeriodOperation", + static_cast( getProperty("PeriodOperation") ) ); + calcAssym->setProperty("OutputType", + static_cast( getProperty("OutputType") ) ); + calcAssym->setProperty("PairFirstIndex", + static_cast( getProperty("PairFirstIndex") ) ); + calcAssym->setProperty("PairSecondIndex", + static_cast( getProperty("PairSecondIndex") ) ); + calcAssym->setProperty("Alpha", + static_cast( getProperty("Alpha") ) ); + calcAssym->setProperty("GroupIndex", + static_cast( getProperty("GroupIndex") ) ); + + calcAssym->execute(); + + MatrixWorkspace_sptr outWS = calcAssym->getProperty("OutputWorkspace"); + setProperty("OutputWorkspace", outWS); } + /** + * Returns a workspace for the first period as specified using FirstPeriod property. + * @param group :: Loaded group of workspaces to use + * @return Workspace for the period + */ + MatrixWorkspace_sptr MuonLoad::getFirstPeriodWS(WorkspaceGroup_sptr group) + { + int firstPeriod = getProperty("FirstPeriod"); + + MatrixWorkspace_sptr resultWS; + + if ( firstPeriod < 0 || firstPeriod >= static_cast( group->size() ) ) + throw std::invalid_argument("Workspace doesn't contain specified first period"); + + resultWS = boost::dynamic_pointer_cast( group->getItem(firstPeriod) ); + + if ( ! resultWS ) + throw std::invalid_argument("First period workspace is not a MatrixWorkspace"); + + return resultWS; + } + /** + * Returns a workspace for the second period as specified using SecondPeriod property. + * @param group :: Loaded group of workspaces to use + * @return Workspace for the period + */ + MatrixWorkspace_sptr MuonLoad::getSecondPeriodWS(WorkspaceGroup_sptr group) + { + int secondPeriod = getProperty("SecondPeriod"); + + MatrixWorkspace_sptr resultWS; + + if ( secondPeriod != EMPTY_INT() ) + { + if ( secondPeriod < 0 || secondPeriod >= static_cast( group->size() ) ) + throw std::invalid_argument("Workspace doesn't contain specified second period"); + + resultWS = boost::dynamic_pointer_cast( group->getItem(secondPeriod) ); + + if ( ! resultWS ) + throw std::invalid_argument("Second period workspace is not a MatrixWorkspace"); + } + + return resultWS; + } } // namespace WorkflowAlgorithms } // namespace Mantid