/** * Converts the input workspaces to spectrum axis to ElasticQ and adds it to the * ADS to be used by PlotPeakBylogValue * @param inputWs - The MatrixWorkspace to be converted * @param wsName - The desired name of the output workspace */ void ConvolutionFitSequential::convertInputToElasticQ( API::MatrixWorkspace_sptr &inputWs, const std::string &wsName) { auto axis = inputWs->getAxis(1); if (axis->isSpectra()) { auto convSpec = createChildAlgorithm("ConvertSpectrumAxis"); // Store in ADS to allow use by PlotPeakByLogValue convSpec->setAlwaysStoreInADS(true); convSpec->setProperty("InputWorkSpace", inputWs); convSpec->setProperty("OutputWorkSpace", wsName); convSpec->setProperty("Target", "ElasticQ"); convSpec->setProperty("EMode", "Indirect"); convSpec->executeAsChildAlg(); } else if (axis->isNumeric()) { // Check that units are Momentum Transfer if (axis->unit()->unitID() != "MomentumTransfer") { throw std::runtime_error("Input must have axis values of Q"); } auto cloneWs = createChildAlgorithm("CloneWorkspace"); // Store in ADS to allow use by PlotPeakByLogValue cloneWs->setAlwaysStoreInADS(true); cloneWs->setProperty("InputWorkspace", inputWs); cloneWs->setProperty("OutputWorkspace", wsName); cloneWs->executeAsChildAlg(); } else { throw std::runtime_error( "Input workspace must have either spectra or numeric axis."); } }
/** * Process a chunk in-place * * @param filterBadPulses * @param wksp */ API::MatrixWorkspace_sptr LoadEventAndCompress::processChunk(API::MatrixWorkspace_sptr &wksp, double filterBadPulses) { EventWorkspace_sptr eventWS = boost::dynamic_pointer_cast<EventWorkspace>(wksp); if (filterBadPulses > 0.) { auto filterBadPulsesAlgo = createChildAlgorithm("FilterBadPulses"); filterBadPulsesAlgo->setProperty("InputWorkspace", eventWS); filterBadPulsesAlgo->setProperty("OutputWorkspace", eventWS); filterBadPulsesAlgo->setProperty("LowerCutoff", filterBadPulses); filterBadPulsesAlgo->executeAsChildAlg(); eventWS = filterBadPulsesAlgo->getProperty("OutputWorkspace"); } auto compressEvents = createChildAlgorithm("CompressEvents"); compressEvents->setProperty("InputWorkspace", eventWS); compressEvents->setProperty("OutputWorkspace", eventWS); compressEvents->setProperty<double>("Tolerance", getProperty("CompressTOFTolerance")); compressEvents->executeAsChildAlg(); eventWS = compressEvents->getProperty("OutputWorkspace"); return eventWS; }
/** * This function gets the input workspace. In the case for a RebinnedOutput * workspace, it must be cleaned before proceeding. Other workspaces are * untouched. * @return the input workspace, cleaned if necessary */ MatrixWorkspace_sptr Integration::getInputWorkspace() { MatrixWorkspace_sptr temp = getProperty("InputWorkspace"); if (temp->id() == "RebinnedOutput") { // Clean the input workspace in the RebinnedOutput case for nan's and // inf's in order to treat the data correctly later. IAlgorithm_sptr alg = this->createChildAlgorithm("ReplaceSpecialValues"); alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", temp); std::string outName = "_" + temp->getName() + "_clean"; alg->setProperty("OutputWorkspace", outName); alg->setProperty("NaNValue", 0.0); alg->setProperty("NaNError", 0.0); alg->setProperty("InfinityValue", 0.0); alg->setProperty("InfinityError", 0.0); alg->executeAsChildAlg(); temp = alg->getProperty("OutputWorkspace"); } // To integrate point data it will be converted to histograms if (!temp->isHistogramData()) { auto alg = this->createChildAlgorithm("ConvertToHistogram"); alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", temp); std::string outName = "_" + temp->getName() + "_histogram"; alg->setProperty("OutputWorkspace", outName); alg->executeAsChildAlg(); temp = alg->getProperty("OutputWorkspace"); temp->setDistribution(true); } return temp; }
/** * Appends the spectra of a specified workspace to another specified input * workspace. * * @param inputWS The input workspace to append to. * @param spectraWorkspace The workspace whose spectra to append. */ MatrixWorkspace_sptr ExtractQENSMembers::appendSpectra(MatrixWorkspace_sptr inputWS, MatrixWorkspace_sptr spectraWorkspace) { auto appendAlg = createChildAlgorithm("AppendSpectra", -1.0, -1.0, false); appendAlg->setProperty("InputWorkspace1", inputWS); appendAlg->setProperty("InputWorkspace2", spectraWorkspace); appendAlg->setProperty("OutputWorkspace", inputWS); appendAlg->executeAsChildAlg(); return appendAlg->getProperty("OutputWorkspace"); }
/// Call child algorithm ConvertUnits for conversion from TOF to wavelength void LoadILLReflectometry::convertTofToWavelength() { if (m_acqMode && (getPropertyValue("XUnit") == "Wavelength")) { auto convertToWavelength = createChildAlgorithm("ConvertUnits", -1, -1, true); convertToWavelength->initialize(); convertToWavelength->setProperty<MatrixWorkspace_sptr>("InputWorkspace", m_localWorkspace); convertToWavelength->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", m_localWorkspace); convertToWavelength->setPropertyValue("Target", "Wavelength"); convertToWavelength->executeAsChildAlg(); } }
/** * Uses rebin to reduce event workspaces to a single bin histogram */ API::MatrixWorkspace_sptr Integration::rangeFilterEventWorkspace(API::MatrixWorkspace_sptr workspace, double minRange, double maxRange) { bool childLog = g_log.is(Logger::Priority::PRIO_DEBUG); auto childAlg = createChildAlgorithm("Rebin", 0, 0.5, childLog); childAlg->setProperty("InputWorkspace", workspace); std::ostringstream binParams; binParams << minRange << "," << maxRange - minRange << "," << maxRange; childAlg->setPropertyValue("Params", binParams.str()); childAlg->setProperty("PreserveEvents", false); childAlg->executeAsChildAlg(); return childAlg->getProperty("OutputWorkspace"); }
/** * Extracts the specified spectrum from the specified spectrum. * * @param inputWS The workspace whose spectrum to extract. * @param spectrum The spectrum to extract. * @return A workspace containing the extracted spectrum. */ MatrixWorkspace_sptr ExtractQENSMembers::extractSpectrum(MatrixWorkspace_sptr inputWS, size_t spectrum) { auto extractAlg = createChildAlgorithm("ExtractSpectra", -1.0, -1.0, false); extractAlg->setProperty("InputWorkspace", inputWS); extractAlg->setProperty("OutputWorkspace", "__extracted"); extractAlg->setProperty("StartWorkspaceIndex", boost::numeric_cast<int>(spectrum)); extractAlg->setProperty("EndWorkspaceIndex", boost::numeric_cast<int>(spectrum)); extractAlg->executeAsChildAlg(); return extractAlg->getProperty("OutputWorkspace"); }
/** * Extracts the Q-Values from the specified workspace. * * @param workspaces The workspaces whose Q-Values to extract. * @return The extracted Q-Values. */ std::vector<double> ExtractQENSMembers::getQValues( const std::vector<MatrixWorkspace_sptr> &workspaces) { std::vector<double> qValues; for (const auto &workspace : workspaces) { auto getQs = createChildAlgorithm("GetQsInQENSData", -1.0, -1.0, false); getQs->setProperty("InputWorkspace", workspace); getQs->executeAsChildAlg(); const std::vector<double> values = getQs->getProperty("Qvalues"); qValues.insert(std::end(qValues), std::begin(values), std::end(values)); } return qValues; }
/** * Calculate asymmetry for the given workspace * @param ws :: [input] Workspace to calculate asymmetry from * @param forward :: [input] Forward group spectra numbers * @param backward :: [input] Backward group spectra numbers * @param alpha :: [input] Detector efficiency * @return :: Asymmetry for workspace */ API::MatrixWorkspace_sptr CalMuonDetectorPhases::getAsymmetry( const API::MatrixWorkspace_sptr &ws, const std::vector<int> &forward, const std::vector<int> &backward, const double alpha) { auto alg = createChildAlgorithm("AsymmetryCalc"); alg->setProperty("InputWorkspace", ws); alg->setProperty("OutputWorkspace", "__NotUsed"); alg->setProperty("ForwardSpectra", forward); alg->setProperty("BackwardSpectra", backward); alg->setProperty("Alpha", alpha); alg->executeAsChildAlg(); API::MatrixWorkspace_sptr wsAsym = alg->getProperty("OutputWorkspace"); return wsAsym; }
/** * Calculate alpha (detector efficiency) from the given workspace * If calculation fails, returns default 1.0 * @param ws :: [input] Workspace to calculate alpha from * @param forward :: [input] Forward group spectra numbers * @param backward :: [input] Backward group spectra numbers * @return :: Alpha, or 1.0 if calculation failed */ double CalMuonDetectorPhases::getAlpha(const API::MatrixWorkspace_sptr &ws, const std::vector<int> &forward, const std::vector<int> &backward) { double alpha = 1.0; try { auto alphaAlg = createChildAlgorithm("AlphaCalc"); alphaAlg->setProperty("InputWorkspace", ws); alphaAlg->setProperty("ForwardSpectra", forward); alphaAlg->setProperty("BackwardSpectra", backward); alphaAlg->executeAsChildAlg(); alpha = alphaAlg->getProperty("Alpha"); } catch (const std::exception &e) { // Eat the error and return default 1.0 so algorithm can continue. // Warn the user that calculating alpha failed std::ostringstream message; message << "Calculating alpha failed, default to 1.0: " << e.what(); g_log.error(message.str()); } return alpha; }
/** * Fit the asymmetry and return the frequency found. * Starting value for the frequency is taken from the hint. * If the fit fails, return the initial hint. * @param wsAsym :: [input] Workspace with asymmetry to fit * @return :: Frequency found from fit */ double CalMuonDetectorPhases::fitFrequencyFromAsymmetry( const API::MatrixWorkspace_sptr &wsAsym) { // Starting value for frequency is hint double hint = getFrequencyHint(); std::string funcStr = createFittingFunction(hint, false); double frequency = hint; std::string fitStatus = "success"; try { auto func = API::FunctionFactory::Instance().createInitialized(funcStr); auto fit = createChildAlgorithm("Fit"); fit->setProperty("Function", func); fit->setProperty("InputWorkspace", wsAsym); fit->setProperty("WorkspaceIndex", 0); fit->setProperty("CreateOutput", true); fit->setProperty("OutputParametersOnly", true); fit->setProperty("Output", "__Invisible"); fit->executeAsChildAlg(); fitStatus = fit->getPropertyValue("OutputStatus"); if (fitStatus == "success") { API::ITableWorkspace_sptr params = fit->getProperty("OutputParameters"); const size_t rows = params->rowCount(); static size_t colName(0), colValue(1); for (size_t iRow = 0; iRow < rows; iRow++) { if (params->cell<std::string>(iRow, colName) == "w") { frequency = params->cell<double>(iRow, colValue); break; } } } } catch (const std::exception &e) { // Report fit failure to user fitStatus = e.what(); } if (fitStatus != "success") { // Either failed, or threw an exception std::ostringstream message; message << "Fit failed (" << fitStatus << "), using omega hint = " << hint; g_log.error(message.str()); } return frequency; }
/** Execute the algorithm. */ void LoadEventAndCompress::exec() { std::string filename = getPropertyValue("Filename"); double filterBadPulses = getProperty("FilterBadPulses"); m_chunkingTable = determineChunk(filename); Progress progress(this, 0, 1, 2); // first run is free progress.report("Loading Chunk"); MatrixWorkspace_sptr resultWS = loadChunk(0); progress.report("Process Chunk"); resultWS = processChunk(resultWS, filterBadPulses); // load the other chunks const size_t numRows = m_chunkingTable->rowCount(); progress.resetNumSteps(numRows, 0, 1); for (size_t i = 1; i < numRows; ++i) { MatrixWorkspace_sptr temp = loadChunk(i); temp = processChunk(temp, filterBadPulses); auto plusAlg = createChildAlgorithm("Plus"); plusAlg->setProperty("LHSWorkspace", resultWS); plusAlg->setProperty("RHSWorkspace", temp); plusAlg->setProperty("OutputWorkspace", resultWS); plusAlg->setProperty("ClearRHSWorkspace", true); plusAlg->executeAsChildAlg(); resultWS = plusAlg->getProperty("OutputWorkspace"); progress.report(); } Workspace_sptr total = assemble(resultWS); // Don't bother compressing combined workspace. DetermineChunking is designed // to prefer loading full banks so no further savings should be available. setProperty("OutputWorkspace", total); }
/// @see DataProcessorAlgorithm::loadChunk(const size_t) MatrixWorkspace_sptr LoadEventAndCompress::loadChunk(const size_t rowIndex) { g_log.debug() << "loadChunk(" << rowIndex << ")\n"; double rowCount = static_cast<double>(m_chunkingTable->rowCount()); double progStart = static_cast<double>(rowIndex) / rowCount; double progStop = static_cast<double>(rowIndex + 1) / rowCount; auto alg = createChildAlgorithm("LoadEventNexus", progStart, progStop, true); alg->setProperty<string>("Filename", getProperty("Filename")); alg->setProperty<double>("FilterByTofMin", getProperty("FilterByTofMin")); alg->setProperty<double>("FilterByTofMax", getProperty("FilterByTofMax")); alg->setProperty<double>("FilterByTimeStart", getProperty("FilterByTimeStart")); alg->setProperty<double>("FilterByTimeStop", getProperty("FilterByTimeStop")); alg->setProperty<string>("NXentryName", getProperty("NXentryName")); alg->setProperty<bool>("LoadMonitors", getProperty("LoadMonitors")); alg->setProperty<bool>("MonitorsAsEvents", getProperty("MonitorsAsEvents")); alg->setProperty<double>("FilterMonByTofMin", getProperty("FilterMonByTofMin")); alg->setProperty<double>("FilterMonByTofMax", getProperty("FilterMonByTofMax")); alg->setProperty<double>("FilterMonByTimeStart", getProperty("FilterMonByTimeStart")); alg->setProperty<double>("FilterMonByTimeStop", getProperty("FilterMonByTimeStop")); // set chunking information if (rowCount > 0.) { const std::vector<string> COL_NAMES = m_chunkingTable->getColumnNames(); for (auto name = COL_NAMES.begin(); name != COL_NAMES.end(); ++name) { alg->setProperty(*name, m_chunkingTable->getRef<int>(*name, rowIndex)); } } alg->executeAsChildAlg(); Workspace_sptr wksp = alg->getProperty("OutputWorkspace"); return boost::dynamic_pointer_cast<MatrixWorkspace>(wksp); }