RefMatrixWSImageView::RefMatrixWSImageView( QString wps_name, int peak_min, int peak_max, int back_min, int back_max, int tof_min, int tof_max) { IEventWorkspace_sptr ws; ws = AnalysisDataService::Instance().retrieveWS<IEventWorkspace>(wps_name.toStdString()); const double total_ymin = 0.0; const double total_ymax = 255.0; const size_t total_rows = 256; // const double total_ymax = 303; // const size_t total_rows = 304; std::vector<double> xaxis = ws->readX(0); const size_t sz = xaxis.size()-1; const size_t total_cols = sz; double total_xmin = xaxis[0]; double total_xmax = xaxis[sz]; float *data = new float[static_cast<size_t>(total_ymax) * sz]; // std::cout << "Starting the for loop " << std::endl; // std::cout << "total_xmax: " << total_xmax << std::endl; // std::cout << "sz is : " << sz << std::endl; std::vector<double> yaxis; for (size_t px=0; px<total_ymax; px++) { //retrieve data now yaxis = ws->readY(px); for (size_t tof=0; tof<sz; tof++) { data[px*sz + tof] = static_cast<float>(yaxis[tof]); } } SpectrumView::ArrayDataSource* source = new SpectrumView::ArrayDataSource(total_xmin, total_xmax, total_ymin, total_ymax, total_rows, total_cols, data); // std::cout << "ws->readX(0).size(): " << ws->readX(0).size() << std::endl; image_view = new RefImageView( source, peak_min, peak_max, back_min, back_max, tof_min, tof_max); }
IEventWorkspace_sptr RefReduction::loadData(const std::string dataRun, const std::string polarization) { const std::string instrument = getProperty("Instrument"); // Check whether dataRun refers to an existing workspace // Create a good name for the raw workspace std::string ws_name = "__ref_"+dataRun+"-"+polarization+"_raw"; IEventWorkspace_sptr rawWS; if (AnalysisDataService::Instance().doesExist(dataRun)) { rawWS = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(dataRun); g_log.notice() << "Found workspace: " << dataRun << std::endl; m_output_message += " |Input data run is a workspace: " + dataRun + "\n"; } else if (AnalysisDataService::Instance().doesExist(ws_name)) { rawWS = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(ws_name); g_log.notice() << "Using existing workspace: " << ws_name << std::endl; m_output_message += " |Found workspace from previous reduction: " + ws_name + "\n"; } else { // If we can't find a workspace, find a file to load std::string path = FileFinder::Instance().getFullPath(dataRun); if (path.size()==0 || !Poco::File(path).exists()) { try { std::vector<std::string> paths = FileFinder::Instance().findRuns(instrument+dataRun); path = paths[0]; } catch(Exception::NotFoundError&) { /* Pass. We report the missing file later. */ } } if (path.size()==0 || !Poco::File(path).exists()) { try { std::vector<std::string> paths = FileFinder::Instance().findRuns(dataRun); path = paths[0]; } catch(Exception::NotFoundError&) { /* Pass. We report the missing file later. */ } } if (Poco::File(path).exists()) { g_log.notice() << "Found: " << path << std::endl; m_output_message += " |Loading from " + path + "\n"; IAlgorithm_sptr loadAlg = createChildAlgorithm("LoadEventNexus", 0, 0.2); loadAlg->setProperty("Filename", path); if (polarization.compare(PolStateNone)!=0) loadAlg->setProperty("NXentryName", polarization); loadAlg->executeAsChildAlg(); rawWS = loadAlg->getProperty("OutputWorkspace"); if (rawWS->getNumberEvents()==0) { g_log.notice() << "No data in " << polarization << std::endl; m_output_message += " |No data for " + polarization + "\n"; return rawWS; } // Move the detector to the right position if (instrument.compare("REF_M")==0) { double det_distance = rawWS->getInstrument()->getDetector(0)->getPos().Z(); Mantid::Kernel::Property* prop = rawWS->run().getProperty("SampleDetDis"); Mantid::Kernel::TimeSeriesProperty<double>* dp = dynamic_cast<Mantid::Kernel::TimeSeriesProperty<double>* >(prop); double sdd = dp->getStatistics().mean/1000.0; IAlgorithm_sptr mvAlg = createChildAlgorithm("MoveInstrumentComponent", 0.2, 0.25); mvAlg->setProperty<MatrixWorkspace_sptr>("Workspace", rawWS); mvAlg->setProperty("ComponentName", "detector1"); mvAlg->setProperty("Z", sdd-det_distance); mvAlg->setProperty("RelativePosition", true); mvAlg->executeAsChildAlg(); g_log.notice() << "Ensuring correct Z position: Correction = " << Poco::NumberFormatter::format(sdd-det_distance) << " m" << std::endl; } AnalysisDataService::Instance().addOrReplace(ws_name, rawWS); } else { g_log.error() << "Could not find a data file for " << dataRun << std::endl; throw std::invalid_argument("Could not find a data file for the given input"); } } // Crop TOF as needed and set binning double tofMin = getProperty("TOFMin"); double tofMax = getProperty("TOFMax"); if (isEmpty(tofMin) || isEmpty(tofMax)) { const MantidVec& x = rawWS->readX(0); if (isEmpty(tofMin)) tofMin = *std::min_element(x.begin(), x.end()); if (isEmpty(tofMax)) tofMax = *std::max_element(x.begin(), x.end()); } int nBins = getProperty("NBins"); double tofStep = getProperty("TOFStep"); if (!isEmpty(nBins)) tofStep = (tofMax-tofMin)/nBins; else nBins = (int)floor( (tofMax-tofMin)/tofStep ); std::vector<double> params; params.push_back(tofMin); params.push_back(tofStep); params.push_back(tofMax); IAlgorithm_sptr rebinAlg = createChildAlgorithm("Rebin", 0.25, 0.3); rebinAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", rawWS); rebinAlg->setProperty("Params", params); rebinAlg->setProperty("PreserveEvents", true); rebinAlg->executeAsChildAlg(); MatrixWorkspace_sptr outputWS = rebinAlg->getProperty("OutputWorkspace"); m_output_message += " |TOF binning: " + Poco::NumberFormatter::format(tofMin) + " to " + Poco::NumberFormatter::format(tofMax) + " in steps of " + Poco::NumberFormatter::format(tofStep) + " microsecs\n"; // Normalise by current IAlgorithm_sptr normAlg = createChildAlgorithm("NormaliseByCurrent", 0.3, 0.35); normAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS); //normAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS); normAlg->executeAsChildAlg(); outputWS = normAlg->getProperty("OutputWorkspace"); // Convert to wavelength IAlgorithm_sptr convAlg = createChildAlgorithm("ConvertUnits", 0.35, 0.4); convAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS); convAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS); convAlg->setProperty("Target", "Wavelength"); convAlg->executeAsChildAlg(); // Rebin in wavelength const MantidVec& x = outputWS->readX(0); double wlMin = *std::min_element(x.begin(), x.end()); double wlMax = *std::max_element(x.begin(), x.end()); std::vector<double> wl_params; wl_params.push_back(wlMin); wl_params.push_back((wlMax-wlMin)/nBins); wl_params.push_back(wlMax); IAlgorithm_sptr rebinAlg2 = createChildAlgorithm("Rebin", 0.25, 0.3); rebinAlg2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS); rebinAlg2->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS); rebinAlg2->setProperty("Params", wl_params); rebinAlg2->setProperty("PreserveEvents", true); rebinAlg2->executeAsChildAlg(); IEventWorkspace_sptr outputEvtWS = boost::dynamic_pointer_cast<IEventWorkspace>(outputWS); return outputEvtWS; }