void UnwrapSNS::execEvent() { // set up the output workspace MatrixWorkspace_sptr matrixOutW = this->getProperty("OutputWorkspace"); if (matrixOutW != m_inputWS) { matrixOutW = m_inputWS->clone(); setProperty("OutputWorkspace", matrixOutW); } auto outW = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutW); // set up the progress bar m_progress = new Progress(this, 0.0, 1.0, m_numberOfSpectra * 2); // algorithm assumes the data is sorted so it can jump out early outW->sortAll(Mantid::DataObjects::TOF_SORT, m_progress); this->getTofRangeData(true); // without the primary flight path the algorithm cannot work const auto &spectrumInfo = m_inputWS->spectrumInfo(); const double L1 = spectrumInfo.l1(); // do the actual work for (int workspaceIndex = 0; workspaceIndex < m_numberOfSpectra; workspaceIndex++) { std::size_t numEvents = outW->getSpectrum(workspaceIndex).getNumberEvents(); double Ld = -1.0; if (spectrumInfo.hasDetectors(workspaceIndex)) Ld = L1 + spectrumInfo.l2(workspaceIndex); std::vector<double> time_bins; if (outW->x(0).size() > 2) { this->unwrapX(m_inputWS->x(workspaceIndex), time_bins, Ld); outW->setBinEdges(workspaceIndex, std::move(time_bins)); } else { outW->setSharedX(workspaceIndex, m_inputWS->sharedX(workspaceIndex)); } if (numEvents > 0) { std::vector<double> times(numEvents); outW->getSpectrum(workspaceIndex).getTofs(times); double filterVal = m_Tmin * Ld / m_LRef; for (size_t j = 0; j < numEvents; j++) { if (times[j] < filterVal) times[j] += m_frameWidth; else break; // stop filtering } outW->getSpectrum(workspaceIndex).setTofs(times); } m_progress->report(); } outW->clearMRU(); this->runMaskDetectors(); }
void UnwrapSNS::execEvent() { // set up the output workspace MatrixWorkspace_sptr matrixOutW = this->getProperty("OutputWorkspace"); if (matrixOutW != m_inputWS) { matrixOutW = m_inputWS->clone(); setProperty("OutputWorkspace", matrixOutW); } auto outW = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutW); // set up the progress bar m_progress = new Progress(this, 0.0, 1.0, m_numberOfSpectra * 2); // algorithm assumes the data is sorted so it can jump out early outW->sortAll(Mantid::DataObjects::TOF_SORT, m_progress); this->getTofRangeData(true); // do the actual work // PARALLEL_FOR2(m_inputWS, outW) for (int workspaceIndex = 0; workspaceIndex < m_numberOfSpectra; workspaceIndex++) { // PARALLEL_START_INTERUPT_REGION std::size_t numEvents = outW->getSpectrum(workspaceIndex).getNumberEvents(); bool isMonitor; double Ld = this->calculateFlightpath(workspaceIndex, isMonitor); MantidVec time_bins; if (outW->dataX(0).size() > 2) { this->unwrapX(m_inputWS->dataX(workspaceIndex), time_bins, Ld); outW->setX(workspaceIndex, time_bins); } else { outW->setX(workspaceIndex, m_inputWS->dataX(workspaceIndex)); } if (numEvents > 0) { MantidVec times(numEvents); outW->getSpectrum(workspaceIndex).getTofs(times); double filterVal = m_Tmin * Ld / m_LRef; for (size_t j = 0; j < numEvents; j++) { if (times[j] < filterVal) times[j] += m_frameWidth; else break; // stop filtering } outW->getSpectrum(workspaceIndex).setTofs(times); } m_progress->report(); // PARALLEL_END_INTERUPT_REGION } // PARALLEL_CHECK_INTERUPT_REGION outW->clearMRU(); this->runMaskDetectors(); }
/** Remove low resolution TOF from an EventWorkspace */ void RemoveLowResTOF::execEvent(const SpectrumInfo &spectrumInfo) { // set up the output workspace MatrixWorkspace_sptr matrixOutW = getProperty("OutputWorkspace"); auto outW = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutW); MatrixWorkspace_sptr matrixLowResW = getProperty("LowResTOFWorkspace"); if (m_outputLowResTOF) { matrixLowResW = m_inputWS->clone(); setProperty("LowResTOFWorkspace", matrixLowResW); } auto lowW = boost::dynamic_pointer_cast<EventWorkspace>(matrixLowResW); g_log.debug() << "TOF range was " << m_inputEvWS->getTofMin() << " to " << m_inputEvWS->getTofMax() << " microseconds\n"; std::size_t numEventsOrig = outW->getNumberEvents(); // set up the progress bar m_progress = make_unique<Progress>(this, 0.0, 1.0, m_numberOfSpectra * 2); // algorithm assumes the data is sorted so it can jump out early outW->sortAll(Mantid::DataObjects::TOF_SORT, m_progress.get()); this->getTminData(true); size_t numClearedEventLists = 0; size_t numClearedEvents = 0; // do the actual work for (size_t workspaceIndex = 0; workspaceIndex < m_numberOfSpectra; workspaceIndex++) { if (outW->getSpectrum(workspaceIndex).getNumberEvents() > 0) { double tmin = this->calcTofMin(workspaceIndex, spectrumInfo); if (tmin != tmin) { // Problematic g_log.warning() << "tmin for workspaceIndex " << workspaceIndex << " is nan. Clearing out data. " << "There are " << outW->getSpectrum(workspaceIndex).getNumberEvents() << " of it. \n"; numClearedEventLists += 1; numClearedEvents += outW->getSpectrum(workspaceIndex).getNumberEvents(); outW->getSpectrum(workspaceIndex).clear(false); if (m_outputLowResTOF) lowW->getSpectrum(workspaceIndex).clear(false); } else if (tmin > 0.) { // there might be events between 0 and tmin (i.e., low resolution) outW->getSpectrum(workspaceIndex).maskTof(0., tmin); if (outW->getSpectrum(workspaceIndex).getNumberEvents() == 0) numClearedEventLists += 1; if (m_outputLowResTOF) { double tmax = lowW->getSpectrum(workspaceIndex).getTofMax(); if (tmax != tmax) { g_log.warning() << "tmax for workspaceIndex " << workspaceIndex << " is nan. Clearing out data. \n"; lowW->getSpectrum(workspaceIndex).clear(false); } else { // There is possibility that tmin calculated is larger than TOF-MAX // of the spectrum if (tmax + DBL_MIN > tmin) lowW->getSpectrum(workspaceIndex).maskTof(tmin, tmax + DBL_MIN); } } } else { // do nothing if tmin <= 0. for outW if (m_outputLowResTOF) { // tmin = 0. no event will be in low resolution lowW->getSpectrum(workspaceIndex).clear(false); } } // } } g_log.information() << "Went from " << numEventsOrig << " events to " << outW->getNumberEvents() << " events (" << (static_cast<double>(numEventsOrig - outW->getNumberEvents()) * 100. / static_cast<double>(numEventsOrig)) << "% removed)\n"; if (numClearedEventLists > 0) g_log.warning() << numClearedEventLists << " spectra of " << m_numberOfSpectra << " had all data removed. The number of removed events is " << numClearedEvents << ".\n"; g_log.debug() << "TOF range is now " << outW->getTofMin() << " to " << outW->getTofMax() << " microseconds\n"; outW->clearMRU(); }
/** Executes the algorithm * @throw std::out_of_range If a property is set to an invalid value for the * input workspace */ void ExtractSpectra::execEvent() { double minX_val = getProperty("XMin"); double maxX_val = getProperty("XMax"); if (isEmpty(minX_val)) minX_val = eventW->getTofMin(); if (isEmpty(maxX_val)) maxX_val = eventW->getTofMax(); // Retrieve and validate the input properties this->checkProperties(); HistogramData::BinEdges XValues_new(2); if (m_commonBoundaries) { auto &oldX = m_inputWorkspace->x(m_workspaceIndexList.front()); XValues_new = HistogramData::BinEdges(oldX.begin() + m_minX, oldX.begin() + m_maxX); } if (m_maxX - m_minX < 2) { // create new output X axis std::vector<double> rb_params{minX_val, maxX_val - minX_val, maxX_val}; static_cast<void>(VectorHelper::createAxisFromRebinParams( rb_params, XValues_new.mutableRawData())); } // run inplace branch if appropriate MatrixWorkspace_sptr OutputWorkspace = this->getProperty("OutputWorkspace"); bool inPlace = (OutputWorkspace == m_inputWorkspace); if (inPlace) g_log.debug("Cropping EventWorkspace in-place."); // Create the output workspace eventW->sortAll(TOF_SORT, nullptr); auto outputWorkspace = create<EventWorkspace>( *m_inputWorkspace, Indexing::extract(m_inputWorkspace->indexInfo(), m_workspaceIndexList), XValues_new); outputWorkspace->sortAll(TOF_SORT, nullptr); Progress prog(this, 0.0, 1.0, 2 * m_workspaceIndexList.size()); eventW->sortAll(Mantid::DataObjects::TOF_SORT, &prog); // Loop over the required workspace indices, copying in the desired bins PARALLEL_FOR_IF(Kernel::threadSafe(*m_inputWorkspace, *outputWorkspace)) for (int j = 0; j < static_cast<int>(m_workspaceIndexList.size()); ++j) { PARALLEL_START_INTERUPT_REGION auto i = m_workspaceIndexList[j]; const EventList &el = eventW->getSpectrum(i); // The output event list EventList &outEL = outputWorkspace->getSpectrum(j); switch (el.getEventType()) { case TOF: { std::vector<TofEvent> moreevents; moreevents.reserve(el.getNumberEvents()); // assume all will make it copyEventsHelper(el.getEvents(), moreevents, minX_val, maxX_val); outEL += moreevents; break; } case WEIGHTED: { std::vector<WeightedEvent> moreevents; moreevents.reserve(el.getNumberEvents()); // assume all will make it copyEventsHelper(el.getWeightedEvents(), moreevents, minX_val, maxX_val); outEL += moreevents; break; } case WEIGHTED_NOTIME: { std::vector<WeightedEventNoTime> moreevents; moreevents.reserve(el.getNumberEvents()); // assume all will make it copyEventsHelper(el.getWeightedEventsNoTime(), moreevents, minX_val, maxX_val); outEL += moreevents; break; } } outEL.setSortOrder(el.getSortType()); bool hasDx = eventW->hasDx(i); if (!m_commonBoundaries) { // If the X axis is NOT common, then keep the initial X axis, just clear // the events outEL.setX(el.ptrX()); outEL.setSharedDx(el.sharedDx()); } else { // X is already set in workspace creation, just set Dx if necessary. if (hasDx) { auto &oldDx = m_inputWorkspace->dx(i); outEL.setPointStandardDeviations(oldDx.begin() + m_minX, oldDx.begin() + m_maxX - m_histogram); } } // Propagate bin masking if there is any if (m_inputWorkspace->hasMaskedBins(i)) { const MatrixWorkspace::MaskList &inputMasks = m_inputWorkspace->maskedBins(i); MatrixWorkspace::MaskList::const_iterator it; for (it = inputMasks.begin(); it != inputMasks.end(); ++it) { const size_t maskIndex = (*it).first; if (maskIndex >= m_minX && maskIndex < m_maxX - m_histogram) outputWorkspace->flagMasked(j, maskIndex - m_minX, (*it).second); } } // When cropping in place, you can clear out old memory from the input one! if (inPlace) { eventW->getSpectrum(i).clear(); } prog.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION setProperty("OutputWorkspace", std::move(outputWorkspace)); }