/** * Updates the analyser and reflection names in the UI when an instrument is selected. * * @param instrumentName Nmae of instrument */ void IndirectInstrumentConfig::updateInstrumentConfigurations(const QString & instrumentName) { if(instrumentName.isEmpty()) return; g_log.debug() << "Loading configuration for instrument: " << instrumentName.toStdString() << std::endl; bool analyserPreviousBlocking = m_uiForm.cbAnalyser->signalsBlocked(); m_uiForm.cbAnalyser->blockSignals(true); m_uiForm.cbAnalyser->clear(); IAlgorithm_sptr loadInstAlg = AlgorithmManager::Instance().create("CreateSimulationWorkspace"); loadInstAlg->initialize(); loadInstAlg->setChild(true); loadInstAlg->setProperty("Instrument", instrumentName.toStdString()); loadInstAlg->setProperty("BinParams", "0,0.5,1"); loadInstAlg->setProperty("OutputWorkspace", "__empty_instrument_workspace"); loadInstAlg->execute(); MatrixWorkspace_sptr instWorkspace = loadInstAlg->getProperty("OutputWorkspace"); QList<QPair<QString, QString>> instrumentModes; Instrument_const_sptr instrument = instWorkspace->getInstrument(); std::vector<std::string> ipfAnalysers = instrument->getStringParameter("analysers"); if(ipfAnalysers.size() == 0) return; QStringList analysers = QString::fromStdString(ipfAnalysers[0]).split(","); for(auto it = analysers.begin(); it != analysers.end(); ++it) { QString analyser = *it; std::string ipfReflections = instrument->getStringParameter("refl-" + analyser.toStdString())[0]; QStringList reflections = QString::fromStdString(ipfReflections).split(","); if(m_removeDiffraction && analyser == "diffraction") continue; if(m_forceDiffraction && analyser != "diffraction") continue; if(reflections.size() > 0) { QVariant data = QVariant(reflections); m_uiForm.cbAnalyser->addItem(analyser, data); } else { m_uiForm.cbAnalyser->addItem(analyser); } } int index = m_uiForm.cbAnalyser->currentIndex(); updateReflectionsList(index); m_uiForm.cbAnalyser->blockSignals(analyserPreviousBlocking); }
/** * Handles setting default spectra range when an instrument configuration is *selected. * * @param instrumentName Name of selected instrument * @param analyserName Name of selected analyser (should always be *"diffraction") * @param reflectionName Name of diffraction mode selected */ void IndirectDiffractionReduction::instrumentSelected( const QString &instrumentName, const QString &analyserName, const QString &reflectionName) { UNUSED_ARG(analyserName); // Set the search instrument for runs m_uiForm.rfSampleFiles->setInstrumentOverride(instrumentName); m_uiForm.rfCanFiles->setInstrumentOverride(instrumentName); MatrixWorkspace_sptr instWorkspace = loadInstrument( instrumentName.toStdString(), reflectionName.toStdString()); Instrument_const_sptr instrument = instWorkspace->getInstrument(); // Get default spectra range double specMin = instrument->getNumberParameter("spectra-min")[0]; double specMax = instrument->getNumberParameter("spectra-max")[0]; m_uiForm.spSpecMin->setValue(static_cast<int>(specMin)); m_uiForm.spSpecMax->setValue(static_cast<int>(specMax)); // Determine whether we need vanadium input std::vector<std::string> correctionVector = instrument->getStringParameter("Workflow.Diffraction.Correction"); bool vanadiumNeeded = false; if (correctionVector.size() > 0) vanadiumNeeded = (correctionVector[0] == "Vanadium"); if (vanadiumNeeded) m_uiForm.swVanadium->setCurrentIndex(0); else m_uiForm.swVanadium->setCurrentIndex(1); // Hide options that the current instrument config cannot process if (instrumentName == "OSIRIS" && reflectionName == "diffonly") { // Disable individual grouping m_uiForm.ckIndividualGrouping->setToolTip( "OSIRIS cannot group detectors individually in diffonly mode"); m_uiForm.ckIndividualGrouping->setEnabled(false); m_uiForm.ckIndividualGrouping->setChecked(false); // Disable sum files m_uiForm.ckSumFiles->setToolTip("OSIRIS cannot sum files in diffonly mode"); m_uiForm.ckSumFiles->setEnabled(false); m_uiForm.ckSumFiles->setChecked(false); } else { // Re-enable sum files m_uiForm.ckSumFiles->setToolTip(""); m_uiForm.ckSumFiles->setEnabled(true); m_uiForm.ckSumFiles->setChecked(true); // Re-enable individual grouping m_uiForm.ckIndividualGrouping->setToolTip(""); m_uiForm.ckIndividualGrouping->setEnabled(true); // Re-enable spectra range m_uiForm.spSpecMin->setEnabled(true); m_uiForm.spSpecMax->setEnabled(true); } }
/** * Attempts to load a grouping information referenced by IDF. * @param instrument :: Intrument which we went the grouping for * @param mainFieldDirection :: (MUSR) orientation of the instrument * @return Grouping information */ boost::shared_ptr<Grouping> getGroupingFromIDF(Instrument_const_sptr instrument, const std::string& mainFieldDirection) { std::string parameterName = "Default grouping file"; // Special case for MUSR, because it has two possible groupings if (instrument->getName() == "MUSR") { parameterName.append(" - " + mainFieldDirection); } std::vector<std::string> groupingFiles = instrument->getStringParameter(parameterName); if ( groupingFiles.size() == 1 ) { const std::string groupingFile = groupingFiles[0]; // Get search directory for XML instrument definition files (IDFs) std::string directoryName = ConfigService::Instance().getInstrumentDirectory(); auto loadedGrouping = boost::make_shared<Grouping>(); loadGroupingFromXML(directoryName + groupingFile, *loadedGrouping); return loadedGrouping; } else { throw std::runtime_error("Multiple groupings specified for the instrument"); } }
/** Execute the algorithm. */ void PolarizationCorrection::exec() { WorkspaceGroup_sptr inWS = getProperty("InputWorkspace"); const std::string analysisMode = getProperty("PolarizationAnalysis"); const size_t nWorkspaces = inWS->size(); validateInputWorkspace(inWS); Instrument_const_sptr instrument = fetchInstrument(inWS.get()); // Check if we need to fetch polarization parameters from the instrument's // parameters std::map<std::string, std::string> loadableProperties; loadableProperties[crhoLabel()] = "crho"; loadableProperties[cppLabel()] = "cPp"; // In PA mode, we also require cap and calpha if (analysisMode == pALabel()) { loadableProperties[cApLabel()] = "cAp"; loadableProperties[cAlphaLabel()] = "calpha"; } for (auto propName = loadableProperties.begin(); propName != loadableProperties.end(); ++propName) { Property *prop = getProperty(propName->first); if (!prop) continue; if (prop->isDefault()) { auto vals = instrument->getStringParameter(propName->second); if (vals.empty()) throw std::runtime_error( "Cannot find value for " + propName->first + " in parameter file. Please specify this property manually."); prop->setValue(vals[0]); } } WorkspaceGroup_sptr outWS; if (analysisMode == pALabel()) { if (nWorkspaces != 4) { throw std::invalid_argument( "For PA analysis, input group must have 4 periods."); } g_log.notice("PA polarization correction"); outWS = execPA(inWS); } else if (analysisMode == pNRLabel()) { if (nWorkspaces != 2) { throw std::invalid_argument( "For PNR analysis, input group must have 2 periods."); } outWS = execPNR(inWS); g_log.notice("PNR polarization correction"); } this->setProperty("OutputWorkspace", outWS); }
/** * Update the list of analysers based on an instrument workspace. * * @param ws Instrument workspace * @return If the workspace contained valid analysers */ bool IndirectInstrumentConfig::updateAnalysersList(MatrixWorkspace_sptr ws) { if(!ws) return false; QList<QPair<QString, QString>> instrumentModes; Instrument_const_sptr instrument = ws->getInstrument(); std::vector<std::string> ipfAnalysers = instrument->getStringParameter("analysers"); QStringList analysers; if(ipfAnalysers.size() > 0) analysers = QString::fromStdString(ipfAnalysers[0]).split(","); // Do not try to display analysers if there are none if(analysers.size() == 0) return false; for(auto it = analysers.begin(); it != analysers.end(); ++it) { QString analyser = *it; std::string ipfReflections = instrument->getStringParameter("refl-" + analyser.toStdString())[0]; QStringList reflections = QString::fromStdString(ipfReflections).split(","); if(m_removeDiffraction && analyser == "diffraction") continue; if(m_forceDiffraction && analyser != "diffraction") continue; if(reflections.size() > 0) { QVariant data = QVariant(reflections); m_uiForm.cbAnalyser->addItem(analyser, data); } else { m_uiForm.cbAnalyser->addItem(analyser); } } return true; }
/** Extract a spectrum from the Efficiencies workspace as a 1D workspace. * @param label :: A label of the spectrum to extract. * @return :: A workspace with a single spectrum. */ boost::shared_ptr<Mantid::API::MatrixWorkspace> PolarizationCorrectionFredrikze::getEfficiencyWorkspace( const std::string &label) { MatrixWorkspace_sptr efficiencies = getProperty(efficienciesLabel); auto const &axis = dynamic_cast<TextAxis &>(*efficiencies->getAxis(1)); size_t index = axis.length(); for (size_t i = 0; i < axis.length(); ++i) { if (axis.label(i) == label) { index = i; break; } } if (index == axis.length()) { // Check if we need to fetch polarization parameters from the instrument's // parameters static std::map<std::string, std::string> loadableProperties{ {crhoLabel, "crho"}, {cppLabel, "cPp"}, {cApLabel, "cAp"}, {cAlphaLabel, "calpha"}}; WorkspaceGroup_sptr inWS = getProperty("InputWorkspace"); Instrument_const_sptr instrument = fetchInstrument(inWS.get()); auto vals = instrument->getStringParameter(loadableProperties[label]); if (vals.empty()) { throw std::invalid_argument("Efficiencey property not found: " + label); } auto extract = createChildAlgorithm("CreatePolarizationEfficiencies"); extract->initialize(); extract->setProperty("InputWorkspace", efficiencies); extract->setProperty(label, vals.front()); extract->execute(); MatrixWorkspace_sptr outWS = extract->getProperty("OutputWorkspace"); return outWS; } else { auto extract = createChildAlgorithm("ExtractSingleSpectrum"); extract->initialize(); extract->setProperty("InputWorkspace", efficiencies); extract->setProperty("WorkspaceIndex", static_cast<int>(index)); extract->execute(); MatrixWorkspace_sptr outWS = extract->getProperty("OutputWorkspace"); return outWS; } }
/** Set algorithmic correction properties * * @param alg :: ReflectometryReductionOne algorithm * @param instrument :: The instrument attached to the workspace */ void ReflectometryReductionOneAuto2::populateAlgorithmicCorrectionProperties( IAlgorithm_sptr alg, Instrument_const_sptr instrument) { // With algorithmic corrections, monitors should not be integrated, see below const std::string correctionAlgorithm = getProperty("CorrectionAlgorithm"); if (correctionAlgorithm == "PolynomialCorrection") { alg->setProperty("NormalizeByIntegratedMonitors", false); alg->setProperty("CorrectionAlgorithm", "PolynomialCorrection"); alg->setPropertyValue("Polynomial", getPropertyValue("Polynomial")); } else if (correctionAlgorithm == "ExponentialCorrection") { alg->setProperty("NormalizeByIntegratedMonitors", false); alg->setProperty("CorrectionAlgorithm", "ExponentialCorrection"); alg->setProperty("C0", getPropertyValue("C0")); alg->setProperty("C1", getPropertyValue("C1")); } else if (correctionAlgorithm == "AutoDetect") { // Figure out what to do from the instrument try { const auto corrVec = instrument->getStringParameter("correction"); if (corrVec.empty()) { throw std::runtime_error( "Could not find parameter 'correction' in " "parameter file. Cannot auto detect the type of " "correction."); } const std::string correctionStr = corrVec[0]; if (correctionStr == "polynomial") { const auto polyVec = instrument->getStringParameter("polystring"); if (polyVec.empty()) throw std::runtime_error("Could not find parameter 'polystring' in " "parameter file. Cannot apply polynomial " "correction."); alg->setProperty("CorrectionAlgorithm", "PolynomialCorrection"); alg->setProperty("Polynomial", polyVec[0]); } else if (correctionStr == "exponential") { const auto c0Vec = instrument->getStringParameter("C0"); if (c0Vec.empty()) throw std::runtime_error( "Could not find parameter 'C0' in parameter " "file. Cannot apply exponential correction."); const auto c1Vec = instrument->getStringParameter("C1"); if (c1Vec.empty()) throw std::runtime_error( "Could not find parameter 'C1' in parameter " "file. Cannot apply exponential correction."); alg->setProperty("C0", c0Vec[0]); alg->setProperty("C1", c1Vec[0]); } alg->setProperty("NormalizeByIntegratedMonitors", false); } catch (std::runtime_error &e) { g_log.error() << e.what() << ". Polynomial correction will not be performed."; alg->setProperty("CorrectionAlgorithm", "None"); } } else { alg->setProperty("CorrectionAlgorithm", "None"); } }