const std::string IEventWorkspace::toString() const { std::ostringstream os; os << MatrixWorkspace::toString() << "\n"; os << "Events: " + boost::lexical_cast<std::string>(getNumberEvents()); switch (getEventType()) { case WEIGHTED: os << " (weighted)\n"; break; case WEIGHTED_NOTIME: os << " (weighted, no times)\n"; break; case TOF: os << "\n"; break; } return os.str(); }
/** 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(); }
void CorrectKiKf::execEvent() { g_log.information("Processing event workspace"); const MatrixWorkspace_const_sptr matrixInputWS = getProperty("InputWorkspace"); auto inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer API::MatrixWorkspace_sptr matrixOutputWS = getProperty("OutputWorkspace"); if (matrixOutputWS != matrixInputWS) { matrixOutputWS = matrixInputWS->clone(); setProperty("OutputWorkspace", matrixOutputWS); } auto outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); const std::string emodeStr = getProperty("EMode"); double efixedProp = getProperty("EFixed"), efixed; if (efixedProp == EMPTY_DBL()) { if (emodeStr == "Direct") { // Check if it has been store on the run object for this workspace if (inputWS->run().hasProperty("Ei")) { Kernel::Property *eiprop = inputWS->run().getProperty("Ei"); efixedProp = boost::lexical_cast<double>(eiprop->value()); g_log.debug() << "Using stored Ei value " << efixedProp << "\n"; } else { throw std::invalid_argument( "No Ei value has been set or stored within the run information."); } } else { // If not specified, will try to get Ef from the parameter file for // indirect geometry, // but it will be done for each spectrum separately, in case of different // analyzer crystals } } // Get the parameter map const ParameterMap &pmap = outputWS->constInstrumentParameters(); int64_t numHistograms = static_cast<int64_t>(inputWS->getNumberHistograms()); API::Progress prog = API::Progress(this, 0.0, 1.0, numHistograms); PARALLEL_FOR_IF(Kernel::threadSafe(*outputWS)) for (int64_t i = 0; i < numHistograms; ++i) { PARALLEL_START_INTERUPT_REGION double Efi = 0; // Now get the detector object for this histogram to check if monitor // or to get Ef for indirect geometry if (emodeStr == "Indirect") { if (efixedProp != EMPTY_DBL()) Efi = efixedProp; else try { IDetector_const_sptr det = inputWS->getDetector(i); if (!det->isMonitor()) { try { Parameter_sptr par = pmap.getRecursive(det.get(), "Efixed"); if (par) { Efi = par->value<double>(); g_log.debug() << "Detector: " << det->getID() << " EFixed: " << Efi << "\n"; } } catch (std::runtime_error &) { /* Throws if a DetectorGroup, use single provided value */ } } } catch (std::runtime_error &) { g_log.information() << "Workspace Index " << i << ": cannot find detector" << "\n"; } } if (emodeStr == "Indirect") efixed = Efi; else efixed = efixedProp; // Do the correction auto &evlist = outputWS->getSpectrum(i); switch (evlist.getEventType()) { case TOF: // Switch to weights if needed. evlist.switchTo(WEIGHTED); /* no break */ // Fall through case WEIGHTED: correctKiKfEventHelper(evlist.getWeightedEvents(), efixed, emodeStr); break; case WEIGHTED_NOTIME: correctKiKfEventHelper(evlist.getWeightedEventsNoTime(), efixed, emodeStr); break; } prog.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION outputWS->clearMRU(); if (inputWS->getNumberEvents() != outputWS->getNumberEvents()) { g_log.information() << "Ef <= 0 or Ei <= 0 for " << inputWS->getNumberEvents() - outputWS->getNumberEvents() << " events, out of " << inputWS->getNumberEvents() << '\n'; if (efixedProp == EMPTY_DBL()) g_log.information() << "Try to set fixed energy\n"; } }