diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataReductionTab.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataReductionTab.h index 8d7dcf61945e..3e9c62a69ea6 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataReductionTab.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataReductionTab.h @@ -84,6 +84,8 @@ namespace CustomInterfaces /// Function to get details about the instrument configuration defined on C2E tab std::map getInstrumentDetails(); + std::map getRangesFromInstrument(QString instName = "", QString analyser = "", QString reflection = ""); + protected: Ui::IndirectDataReduction m_uiForm; diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/IndirectDataReductionTab.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/IndirectDataReductionTab.cpp index 7473b4e80836..f80f6f62971d 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/IndirectDataReductionTab.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/IndirectDataReductionTab.cpp @@ -172,5 +172,113 @@ namespace CustomInterfaces return instDetails; } + /** + * Gets default peak and background ranges for an instrument in time of flight. + * + * @param instName Name of instrument + * @param analyser Analyser component + * @param reflection Reflection used + * + * @returns A map of range ID to value + */ + std::map IndirectDataReductionTab::getRangesFromInstrument( + QString instName, QString analyser, QString reflection) + { + // Get any unset parameters + if(instName.isEmpty()) + instName = m_uiForm.cbInst->currentText(); + if(analyser.isEmpty()) + analyser = m_uiForm.cbAnalyser->currentText(); + if(reflection.isEmpty()) + reflection = m_uiForm.cbReflection->currentText(); + + std::map ranges; + + // Get the instrument + auto instWs = loadInstrumentIfNotExist(instName.toStdString(), analyser.toStdString(), reflection.toStdString()); + auto inst = instWs->getInstrument(); + + // Get the analyser component + auto comp = inst->getComponentByName(analyser.toStdString()); + if(!comp) + return ranges; + + // Get the resolution of the analyser + auto resParams = comp->getNumberParameter("resolution", true); + if(resParams.size() < 1) + return ranges; + double resolution = resParams[0]; + + std::vector x; + x.push_back(-6 * resolution); + x.push_back(-5 * resolution); + x.push_back(-2 * resolution); + x.push_back(0); + x.push_back(2 * resolution); + std::vector y; + y.push_back(1); + y.push_back(2); + y.push_back(3); + y.push_back(4); + std::vector e(4, 0); + + IAlgorithm_sptr createWsAlg = AlgorithmManager::Instance().create("CreateWorkspace"); + createWsAlg->initialize(); + createWsAlg->setProperty("OutputWorkspace", "__energy"); + createWsAlg->setProperty("DataX", x); + createWsAlg->setProperty("DataY", y); + createWsAlg->setProperty("DataE", e); + createWsAlg->setProperty("Nspec", 1); + createWsAlg->setProperty("UnitX", "DeltaE"); + createWsAlg->execute(); + + IAlgorithm_sptr convertHistAlg = AlgorithmManager::Instance().create("ConvertToHistogram"); + convertHistAlg->initialize(); + convertHistAlg->setProperty("InputWorkspace", "__energy"); + convertHistAlg->setProperty("OutputWorkspace", "__energy"); + convertHistAlg->execute(); + + IAlgorithm_sptr loadInstAlg = AlgorithmManager::Instance().create("LoadInstrument"); + loadInstAlg->initialize(); + loadInstAlg->setProperty("Workspace", "__energy"); + loadInstAlg->setProperty("InstrumentName", instName.toStdString()); + loadInstAlg->execute(); + + QString ipfFilename = instName + "_" + analyser + "_" + reflection + "_Parameters.xml"; + + IAlgorithm_sptr loadParamAlg = AlgorithmManager::Instance().create("LoadParameterFile"); + loadParamAlg->initialize(); + loadParamAlg->setProperty("Workspace", "__energy"); + loadParamAlg->setProperty("Filename", ipfFilename.toStdString()); + loadParamAlg->execute(); + + auto energyWs = AnalysisDataService::Instance().retrieveWS("__energy"); + double efixed = energyWs->getInstrument()->getNumberParameter("efixed-val")[0]; + + auto spectrum = energyWs->getSpectrum(0); + spectrum->setSpectrumNo(3); + spectrum->clearDetectorIDs(); + spectrum->addDetectorID(3); + + IAlgorithm_sptr convUnitsAlg = AlgorithmManager::Instance().create("ConvertUnits"); + convUnitsAlg->initialize(); + convUnitsAlg->setProperty("InputWorkspace", "__energy"); + convUnitsAlg->setProperty("OutputWorkspace", "__tof"); + convUnitsAlg->setProperty("Target", "TOF"); + convUnitsAlg->setProperty("EMode", "Indirect"); + convUnitsAlg->setProperty("EFixed", efixed); + convUnitsAlg->execute(); + + auto tofWs = AnalysisDataService::Instance().retrieveWS("__tof"); + + std::vector tofData = tofWs->readX(0); + ranges["peak-start-tof"] = tofData[0]; + ranges["peak-end-tof"] = tofData[2]; + ranges["back-start-tof"] = tofData[3]; + ranges["back-end-tof"] = tofData[4]; + + return ranges; + } + } // namespace CustomInterfaces } // namespace Mantid