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; }
/** * Execute the algorithm. */ void RebinByTimeBase::exec() { using Mantid::DataObjects::EventWorkspace; IEventWorkspace_sptr inWS = getProperty("InputWorkspace"); if (!boost::dynamic_pointer_cast<EventWorkspace>(inWS)) { const std::string algName = this->name(); throw std::invalid_argument( algName + " Algorithm requires an EventWorkspace as an input."); } MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace"); // retrieve the properties const std::vector<double> inParams = getProperty("Params"); std::vector<double> rebinningParams; // workspace independent determination of length const int histnumber = static_cast<int>(inWS->getNumberHistograms()); const uint64_t nanoSecondsInASecond = static_cast<uint64_t>(1e9); const DateAndTime runStartTime = inWS->run().startTime(); // The validator only passes parameters with size 1, or 3xn. double tStep = 0; if (inParams.size() >= 3) { // Use the start of the run to offset the times provided by the user. pulse // time of the events are absolute. const DateAndTime startTime = runStartTime + inParams[0]; const DateAndTime endTime = runStartTime + inParams[2]; // Rebinning params in nanoseconds. rebinningParams.push_back( static_cast<double>(startTime.totalNanoseconds())); tStep = inParams[1] * nanoSecondsInASecond; rebinningParams.push_back(tStep); rebinningParams.push_back(static_cast<double>(endTime.totalNanoseconds())); } else if (inParams.size() == 1) { const uint64_t xmin = getMinX(inWS); const uint64_t xmax = getMaxX(inWS); rebinningParams.push_back(static_cast<double>(xmin)); tStep = inParams[0] * nanoSecondsInASecond; rebinningParams.push_back(tStep); rebinningParams.push_back(static_cast<double>(xmax)); } // Validate the timestep. if (tStep <= 0) { throw std::invalid_argument( "Cannot have a timestep less than or equal to zero."); } // Initialize progress reporting. Progress prog(this, 0.0, 1.0, histnumber); MantidVecPtr XValues_new; // create new X axis, with absolute times in seconds. const int ntcnew = VectorHelper::createAxisFromRebinParams( rebinningParams, XValues_new.access()); ConvertToRelativeTime transformToRelativeT(runStartTime); // Transform the output into relative times in seconds. MantidVec OutXValues_scaled(XValues_new->size()); std::transform(XValues_new->begin(), XValues_new->end(), OutXValues_scaled.begin(), transformToRelativeT); outputWS = WorkspaceFactory::Instance().create("Workspace2D", histnumber, ntcnew, ntcnew - 1); WorkspaceFactory::Instance().initializeFromParent(inWS, outputWS, true); // Copy all the axes for (int i = 1; i < inWS->axes(); i++) { outputWS->replaceAxis(i, inWS->getAxis(i)->clone(outputWS.get())); outputWS->getAxis(i)->unit() = inWS->getAxis(i)->unit(); } // X-unit is relative time since the start of the run. outputWS->getAxis(0)->unit() = boost::make_shared<Units::Time>(); // Copy the units over too. for (int i = 1; i < outputWS->axes(); ++i) { outputWS->getAxis(i)->unit() = inWS->getAxis(i)->unit(); } outputWS->setYUnit(inWS->YUnit()); outputWS->setYUnitLabel(inWS->YUnitLabel()); // Assign it to the output workspace property setProperty("OutputWorkspace", outputWS); // Go through all the histograms and set the data doHistogramming(inWS, outputWS, XValues_new, OutXValues_scaled, prog); }