/** Do the initial copy of the data from the input to the output workspace for * histogram workspaces. * Takes out the bin width if necessary. * @param inputWS The input workspace * @param outputWS The output workspace */ void ConvertUnitsUsingDetectorTable::fillOutputHist( const API::MatrixWorkspace_const_sptr inputWS, const API::MatrixWorkspace_sptr outputWS) { const int size = static_cast<int>(inputWS->blocksize()); // Loop over the histograms (detector spectra) Progress prog(this, 0.0, 0.2, m_numberOfSpectra); int64_t numberOfSpectra_i = static_cast<int64_t>(m_numberOfSpectra); // cast to make openmp happy PARALLEL_FOR2(inputWS, outputWS) for (int64_t i = 0; i < numberOfSpectra_i; ++i) { PARALLEL_START_INTERUPT_REGION // Take the bin width dependency out of the Y & E data if (m_distribution) { for (int j = 0; j < size; ++j) { const double width = std::abs(inputWS->dataX(i)[j + 1] - inputWS->dataX(i)[j]); outputWS->dataY(i)[j] = inputWS->dataY(i)[j] * width; outputWS->dataE(i)[j] = inputWS->dataE(i)[j] * width; } } else { // Just copy over outputWS->dataY(i) = inputWS->readY(i); outputWS->dataE(i) = inputWS->readE(i); } // Copy over the X data outputWS->setX(i, inputWS->refX(i)); prog.report("Convert to " + m_outputUnit->unitID()); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION }
/** * The main method to calculate the ring profile for 2d image based workspace. * * It will iterate over all the spectrum inside the workspace. * For each spectrum, it will use the RingProfile::getBinForPixel method to *identify * where, in the output_bins, the elements of the spectrum should be placed in. * * @param inputWS: pointer to the input workspace * @param output_bins: the reference to the vector to be filled with the *integration values */ void RingProfile::processNumericImageRingProfile( const API::MatrixWorkspace_sptr inputWS, std::vector<double> &output_bins) { // allocate the bin positions vector std::vector<int> bin_n(inputWS->dataY(0).size(), -1); // consider that each spectrum is a row in the image for (int i = 0; i < static_cast<int>(inputWS->getNumberHistograms()); i++) { m_progress->report("Computing ring bins positions for pixels"); // get bin for the pixels inside this spectrum // for each column of the image getBinForPixel(inputWS, i, bin_n); // accumulate the values from the spectrum to the target bin // each column has it correspondend bin_position inside bin_n const MantidVec &refY = inputWS->dataY(i); for (size_t j = 0; j < bin_n.size(); j++) { // is valid bin? No -> skip if (bin_n[j] < 0) continue; // accumulate the values of this spectrum inside this bin output_bins[bin_n[j]] += refY[j]; } } }
/** * Perform a call to nxgetslab, via the NexusClasses wrapped methods for a given blocksize. The xbins are read along with * each call to the data/error loading * @param data :: The NXDataSet object of y values * @param errors :: The NXDataSet object of error values * @param xbins :: The xbin NXDataSet * @param blocksize :: The blocksize to use * @param nchannels :: The number of channels for the block * @param hist :: The workspace index to start reading into * @param wsIndex :: The workspace index to save data into * @param local_workspace :: A pointer to the workspace */ void LoadNexusProcessed::loadBlock(NXDataSetTyped<double> & data, NXDataSetTyped<double> & errors, NXDouble & xbins, int64_t blocksize, int64_t nchannels, int64_t &hist, int64_t& wsIndex, API::MatrixWorkspace_sptr local_workspace) { data.load(static_cast<int>(blocksize),static_cast<int>(hist)); double *data_start = data(); double *data_end = data_start + nchannels; errors.load(static_cast<int>(blocksize),static_cast<int>(hist)); double *err_start = errors(); double *err_end = err_start + nchannels; xbins.load(static_cast<int>(blocksize),static_cast<int>(hist)); const int64_t nxbins(nchannels + 1); double *xbin_start = xbins(); double *xbin_end = xbin_start + nxbins; int64_t final(hist + blocksize); while( hist < final ) { MantidVec& Y = local_workspace->dataY(wsIndex); Y.assign(data_start, data_end); data_start += nchannels; data_end += nchannels; MantidVec& E = local_workspace->dataE(wsIndex); E.assign(err_start, err_end); err_start += nchannels; err_end += nchannels; MantidVec& X = local_workspace->dataX(wsIndex); X.assign(xbin_start, xbin_end); xbin_start += nxbins; xbin_end += nxbins; ++hist; ++wsIndex; } }
/** loadData * Load the counts data from an NXInt into a workspace */ void LoadMuonNexus2::loadData(const Mantid::NeXus::NXInt &counts, const std::vector<double> &timeBins, int wsIndex, int period, int spec, API::MatrixWorkspace_sptr localWorkspace) { MantidVec &X = localWorkspace->dataX(wsIndex); MantidVec &Y = localWorkspace->dataY(wsIndex); MantidVec &E = localWorkspace->dataE(wsIndex); X.assign(timeBins.begin(), timeBins.end()); int nBins = 0; int *data = nullptr; if (counts.rank() == 3) { nBins = counts.dim2(); data = &counts(period, spec, 0); } else if (counts.rank() == 2) { nBins = counts.dim1(); data = &counts(spec, 0); } else { throw std::runtime_error("Data have unsupported dimansionality"); } assert(nBins + 1 == static_cast<int>(timeBins.size())); Y.assign(data, data + nBins); typedef double (*uf)(double); uf dblSqrt = std::sqrt; std::transform(Y.begin(), Y.end(), E.begin(), dblSqrt); }
/** Smoothing by zeroing. * @param n :: The order of truncation * @param unfilteredWS :: workspace for storing the unfiltered Fourier * transform of the input spectrum * @param filteredWS :: workspace for storing the filtered spectrum */ void FFTSmooth2::zero(int n, API::MatrixWorkspace_sptr &unfilteredWS, API::MatrixWorkspace_sptr &filteredWS) { int mx = static_cast<int>(unfilteredWS->readX(0).size()); int my = static_cast<int>(unfilteredWS->readY(0).size()); int ny = my / n; if (ny == 0) ny = 1; filteredWS = API::WorkspaceFactory::Instance().create(unfilteredWS, 2, mx, my); const Mantid::MantidVec &Yr = unfilteredWS->readY(0); const Mantid::MantidVec &Yi = unfilteredWS->readY(1); const Mantid::MantidVec &X = unfilteredWS->readX(0); Mantid::MantidVec &yr = filteredWS->dataY(0); Mantid::MantidVec &yi = filteredWS->dataY(1); Mantid::MantidVec &xr = filteredWS->dataX(0); Mantid::MantidVec &xi = filteredWS->dataX(1); xr.assign(X.begin(), X.end()); xi.assign(X.begin(), X.end()); yr.assign(Yr.size(), 0); yi.assign(Yr.size(), 0); for (int i = 0; i < ny; i++) { yr[i] = Yr[i]; yi[i] = Yi[i]; } }
void RadiusSum::setUpOutputWorkspace(std::vector<double> &values) { g_log.debug() << "Output calculated, setting up the output workspace\n"; API::MatrixWorkspace_sptr outputWS = API::WorkspaceFactory::Instance().create( inputWS, 1, values.size() + 1, values.size()); g_log.debug() << "Set the data\n"; MantidVec &refY = outputWS->dataY(0); std::copy(values.begin(), values.end(), refY.begin()); g_log.debug() << "Set the bins limits\n"; MantidVec &refX = outputWS->dataX(0); double bin_size = (max_radius - min_radius) / num_bins; for (int i = 0; i < (static_cast<int>(refX.size())) - 1; i++) refX[i] = min_radius + i * bin_size; refX.back() = max_radius; // configure the axis: // for numeric images, the axis are the same as the input workspace, and are // copied in the creation. // for instrument related, the axis Y (1) continues to be the same. // it is necessary to change only the axis X. We have to change it to radius. if (inputWorkspaceHasInstrumentAssociated(inputWS)) { API::Axis *const horizontal = new API::NumericAxis(refX.size()); auto labelX = UnitFactory::Instance().create("Label"); boost::dynamic_pointer_cast<Units::Label>(labelX)->setLabel("Radius"); horizontal->unit() = labelX; outputWS->replaceAxis(0, horizontal); } setProperty("OutputWorkspace", outputWS); }
/** Smoothing using Butterworth filter. * @param n :: The cutoff frequency control parameter. * Cutoff frequency = my/n where my is the * number of sample points in the data. * As with the "Zeroing" case, the cutoff * frequency is truncated to an integer value * and set to 1 if the truncated value was zero. * @param order :: The order of the Butterworth filter, 1, 2, etc. * This must be a positive integer. * @param unfilteredWS :: workspace for storing the unfiltered Fourier * transform of the input spectrum * @param filteredWS :: workspace for storing the filtered spectrum */ void FFTSmooth2::Butterworth(int n, int order, API::MatrixWorkspace_sptr &unfilteredWS, API::MatrixWorkspace_sptr &filteredWS) { int mx = static_cast<int>(unfilteredWS->readX(0).size()); int my = static_cast<int>(unfilteredWS->readY(0).size()); int ny = my / n; if (ny == 0) ny = 1; filteredWS = API::WorkspaceFactory::Instance().create(unfilteredWS, 2, mx, my); const Mantid::MantidVec &Yr = unfilteredWS->readY(0); const Mantid::MantidVec &Yi = unfilteredWS->readY(1); const Mantid::MantidVec &X = unfilteredWS->readX(0); Mantid::MantidVec &yr = filteredWS->dataY(0); Mantid::MantidVec &yi = filteredWS->dataY(1); Mantid::MantidVec &xr = filteredWS->dataX(0); Mantid::MantidVec &xi = filteredWS->dataX(1); xr.assign(X.begin(), X.end()); xi.assign(X.begin(), X.end()); yr.assign(Yr.size(), 0); yi.assign(Yr.size(), 0); double cutoff = ny; for (int i = 0; i < my; i++) { double scale = 1.0 / (1.0 + pow(i / cutoff, 2 * order)); yr[i] = scale * Yr[i]; yi[i] = scale * Yi[i]; } }
/** * Read spectra from the DAE * @param period :: Current period index * @param index :: First spectrum index * @param count :: Number of spectra to read * @param workspace :: Workspace to store the data * @param workspaceIndex :: index in workspace to store data */ void ISISHistoDataListener::getData(int period, int index, int count, API::MatrixWorkspace_sptr workspace, size_t workspaceIndex) { const int numberOfBins = m_numberOfBins[m_timeRegime]; const size_t bufferSize = count * (numberOfBins + 1) * sizeof(int); std::vector<int> dataBuffer(bufferSize); // Read in spectra from DAE int ndims = 2, dims[2]; dims[0] = count; dims[1] = numberOfBins + 1; int spectrumIndex = index + period * (m_totalNumberOfSpectra + 1); if (IDCgetdat(m_daeHandle, spectrumIndex, count, dataBuffer.data(), dims, &ndims) != 0) { g_log.error("Unable to read DATA from DAE " + m_daeName); throw Kernel::Exception::FileError("Unable to read DATA from DAE ", m_daeName); } for (size_t i = 0; i < static_cast<size_t>(count); ++i) { size_t wi = workspaceIndex + i; workspace->setX(wi, m_bins[m_timeRegime]); MantidVec &y = workspace->dataY(wi); MantidVec &e = workspace->dataE(wi); workspace->getSpectrum(wi)->setSpectrumNo(index + static_cast<specid_t>(i)); size_t shift = i * (numberOfBins + 1) + 1; y.assign(dataBuffer.begin() + shift, dataBuffer.begin() + shift + y.size()); std::transform(y.begin(), y.end(), e.begin(), dblSqrt); } }
void ConvertEmptyToTof::setTofInWS(const std::vector<double> &tofAxis, API::MatrixWorkspace_sptr outputWS) { const size_t numberOfSpectra = m_inputWS->getNumberHistograms(); int64_t numberOfSpectraInt64 = static_cast<int64_t>(numberOfSpectra); // cast to make openmp happy g_log.debug() << "Setting the TOF X Axis for numberOfSpectra=" << numberOfSpectra << '\n'; Progress prog(this, 0.0, 0.2, numberOfSpectra); PARALLEL_FOR2(m_inputWS, outputWS) for (int64_t i = 0; i < numberOfSpectraInt64; ++i) { PARALLEL_START_INTERUPT_REGION // Just copy over outputWS->dataY(i) = m_inputWS->readY(i); outputWS->dataE(i) = m_inputWS->readE(i); // copy outputWS->setX(i, tofAxis); prog.report(); PARALLEL_END_INTERUPT_REGION } // end for i PARALLEL_CHECK_INTERUPT_REGION outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("TOF"); }
/** Execute the algorithm. */ void DampSq::exec() { // TODO Auto-generated execute stub // 1. Generate new workspace API::MatrixWorkspace_const_sptr isqspace = getProperty("InputWorkspace"); API::MatrixWorkspace_sptr osqspace = WorkspaceFactory::Instance().create(isqspace, 1, isqspace->size(), isqspace->size()); int mode = getProperty("Mode"); double qmax = getProperty("QMax"); if (mode < 1 || mode > 4) { g_log.error("Damp mode can only be 1, 2, 3, or 4"); return; } // 2. Get access to all const MantidVec& iQVec = isqspace->dataX(0); const MantidVec& iSVec = isqspace->dataY(0); const MantidVec& iEVec = isqspace->dataE(0); MantidVec& oQVec = osqspace->dataX(0); MantidVec& oSVec = osqspace->dataY(0); MantidVec& oEVec = osqspace->dataE(0); // 3. Calculation double dqmax = qmax - iQVec[0]; double damp; for (unsigned int i = 0; i < iQVec.size(); i ++) { // a) calculate damp coefficient switch (mode) { case 1: damp = dampcoeff1(iQVec[i], qmax, dqmax); break; case 2: damp = dampcoeff2(iQVec[i], qmax, dqmax);; break; case 3: damp = dampcoeff3(iQVec[i], qmax, dqmax);; break; case 4: damp = dampcoeff4(iQVec[i], qmax, dqmax);; break; default: damp = 0; break; } // b) calculate new S(q) oQVec[i] = iQVec[i]; oSVec[i] = 1 + damp*(iSVec[i]-1); oEVec[i] = damp*iEVec[i]; } // i // 4. Over setProperty("OutputWorkspace", osqspace); return; }
/** Performs the Holtzer transformation: IQ v Q * @param ws The workspace to be transformed */ void IQTransform::holtzer(API::MatrixWorkspace_sptr ws) { MantidVec& X = ws->dataX(0); MantidVec& Y = ws->dataY(0); MantidVec& E = ws->dataE(0); std::transform(Y.begin(),Y.end(),X.begin(),Y.begin(),std::multiplies<double>()); std::transform(E.begin(),E.end(),X.begin(),E.begin(),std::multiplies<double>()); ws->setYUnitLabel("I x Q"); }
/** Performs the Porod transformation: IQ^4 v Q * @param ws The workspace to be transformed */ void IQTransform::porod(API::MatrixWorkspace_sptr ws) { MantidVec& X = ws->dataX(0); MantidVec& Y = ws->dataY(0); MantidVec& E = ws->dataE(0); MantidVec Q4(X.size()); std::transform(X.begin(),X.end(),X.begin(),Q4.begin(),VectorHelper::TimesSquares<double>()); std::transform(Y.begin(),Y.end(),Q4.begin(),Y.begin(),std::multiplies<double>()); std::transform(E.begin(),E.end(),Q4.begin(),E.begin(),std::multiplies<double>()); ws->setYUnitLabel("I x Q^4"); }
/** Performs a log-log transformation: Ln(I) v Ln(Q) * @param ws The workspace to be transformed * @throw std::range_error if an attempt is made to take log of a negative number */ void IQTransform::logLog(API::MatrixWorkspace_sptr ws) { MantidVec& X = ws->dataX(0); MantidVec& Y = ws->dataY(0); MantidVec& E = ws->dataE(0); std::transform(X.begin(),X.end(),X.begin(),VectorHelper::Log<double>()); std::transform(E.begin(),E.end(),Y.begin(),E.begin(),std::divides<double>()); std::transform(Y.begin(),Y.end(),Y.begin(),VectorHelper::LogNoThrow<double>()); ws->setYUnitLabel("Ln(I)"); m_label->setLabel("Ln(Q)"); }
/** Performs the Guinier (sheets) transformation: Ln(IQ^2) v Q^2 * @param ws The workspace to be transformed * @throw std::range_error if an attempt is made to take log of a negative number */ void IQTransform::guinierSheets(API::MatrixWorkspace_sptr ws) { MantidVec& X = ws->dataX(0); MantidVec& Y = ws->dataY(0); MantidVec& E = ws->dataE(0); std::transform(E.begin(),E.end(),Y.begin(),E.begin(),std::divides<double>()); std::transform(X.begin(),X.end(),X.begin(),VectorHelper::Squares<double>()); std::transform(Y.begin(),Y.end(),X.begin(),Y.begin(),std::multiplies<double>()); std::transform(Y.begin(),Y.end(),Y.begin(),VectorHelper::LogNoThrow<double>()); ws->setYUnitLabel("Ln(I x Q^2)"); m_label->setLabel("Q^2"); }
/** Remove background per pixel * @brief ConvertCWSDExpToMomentum::removeBackground * @param dataws */ void ConvertCWSDExpToMomentum::removeBackground( API::MatrixWorkspace_sptr dataws) { if (dataws->getNumberHistograms() != m_backgroundWS->getNumberHistograms()) throw std::runtime_error("Impossible to have this situation"); size_t numhist = dataws->getNumberHistograms(); for (size_t i = 0; i < numhist; ++i) { double bkgd_y = m_backgroundWS->readY(i)[0]; if (fabs(bkgd_y) > 1.E-2) { dataws->dataY(i)[0] -= bkgd_y; dataws->dataE(i)[0] = std::sqrt(dataws->readY(i)[0]); } } }
/** Unwraps the Y & E vectors of a spectrum according to the ranges found in * unwrapX. * @param tempWS :: A pointer to the temporary workspace in which the * results are being stored * @param spectrum :: The workspace index * @param rangeBounds :: The upper and lower ranges for the unwrapping */ void UnwrapMonitor::unwrapYandE(const API::MatrixWorkspace_sptr &tempWS, const int &spectrum, const std::vector<int> &rangeBounds) { // Copy over the relevant ranges of Y & E data MantidVec &Y = tempWS->dataY(spectrum); MantidVec &E = tempWS->dataE(spectrum); // Get references to the input data const MantidVec &YIn = m_inputWS->dataY(spectrum); const MantidVec &EIn = m_inputWS->dataE(spectrum); if (rangeBounds[2] != -1) { // Copy in the upper range Y.assign(YIn.begin() + rangeBounds[2], YIn.end()); E.assign(EIn.begin() + rangeBounds[2], EIn.end()); // Propagate masking, if necessary if (m_inputWS->hasMaskedBins(spectrum)) { const MatrixWorkspace::MaskList &inputMasks = m_inputWS->maskedBins(spectrum); MatrixWorkspace::MaskList::const_iterator it; for (it = inputMasks.begin(); it != inputMasks.end(); ++it) { if (static_cast<int>((*it).first) >= rangeBounds[2]) tempWS->flagMasked(spectrum, (*it).first - rangeBounds[2], (*it).second); } } } else { // Y & E are references to existing vector. Assign above clears them, so // need to explicitly here Y.clear(); E.clear(); } if (rangeBounds[0] != -1 && rangeBounds[1] > 0) { // Now append the lower range MantidVec::const_iterator YStart = YIn.begin(); MantidVec::const_iterator EStart = EIn.begin(); Y.insert(Y.end(), YStart + rangeBounds[0], YStart + rangeBounds[1]); E.insert(E.end(), EStart + rangeBounds[0], EStart + rangeBounds[1]); // Propagate masking, if necessary if (m_inputWS->hasMaskedBins(spectrum)) { const MatrixWorkspace::MaskList &inputMasks = m_inputWS->maskedBins(spectrum); MatrixWorkspace::MaskList::const_iterator it; for (it = inputMasks.begin(); it != inputMasks.end(); ++it) { const int maskIndex = static_cast<int>((*it).first); if (maskIndex >= rangeBounds[0] && maskIndex < rangeBounds[1]) tempWS->flagMasked(spectrum, maskIndex - rangeBounds[0], (*it).second); } } } }
/** Reads in the data corresponding to a single spectrum * @param speFile :: The file handle * @param workspace :: The output workspace * @param index :: The index of the current spectrum */ void LoadSPE::readHistogram(FILE *speFile, API::MatrixWorkspace_sptr workspace, size_t index) { // First, there should be a comment line char comment[100]; fgets(comment, 100, speFile); if (comment[0] != '#') reportFormatError(std::string(comment)); // Then it's the Y values MantidVec &Y = workspace->dataY(index); const size_t nbins = workspace->blocksize(); int retval; for (size_t i = 0; i < nbins; ++i) { retval = fscanf(speFile, "%10le", &Y[i]); // g_log.error() << Y[i] << std::endl; if (retval != 1) { std::stringstream ss; ss << "Reading data value" << i << " of histogram " << index; reportFormatError(ss.str()); } // -10^30 is the flag for not a number used in SPE files (from // www.mantidproject.org/images/3/3d/Spe_file_format.pdf) if (Y[i] == SaveSPE::MASK_FLAG) { Y[i] = std::numeric_limits<double>::quiet_NaN(); } } // Read to EOL fgets(comment, 100, speFile); // Another comment line fgets(comment, 100, speFile); if (comment[0] != '#') reportFormatError(std::string(comment)); // And then the error values MantidVec &E = workspace->dataE(index); for (size_t i = 0; i < nbins; ++i) { retval = fscanf(speFile, "%10le", &E[i]); if (retval != 1) { std::stringstream ss; ss << "Reading error value" << i << " of histogram " << index; reportFormatError(ss.str()); } } // Read to EOL fgets(comment, 100, speFile); return; }
/** Divide each bin by the width of its q bin. * @param outputWS :: The output workspace * @param qAxis :: A vector of the q bin boundaries */ void SofQWCentre::makeDistribution(API::MatrixWorkspace_sptr outputWS, const std::vector<double> qAxis) { std::vector<double> widths(qAxis.size()); std::adjacent_difference(qAxis.begin(), qAxis.end(), widths.begin()); const size_t numQBins = outputWS->getNumberHistograms(); for (size_t i = 0; i < numQBins; ++i) { MantidVec &Y = outputWS->dataY(i); MantidVec &E = outputWS->dataE(i); std::transform(Y.begin(), Y.end(), Y.begin(), std::bind2nd(std::divides<double>(), widths[i + 1])); std::transform(E.begin(), E.end(), E.begin(), std::bind2nd(std::divides<double>(), widths[i + 1])); } }
/** * Here is the main logic to perform the transformation, to calculate the bin *position in degree for each spectrum. * * The first part of the method is to check if the pixel position is inside the *ring defined as minRadio and maxRadio. * * To do this, it deducts the pixel position. This deduction follows the *followin assumption: * * - the spectrum_index == row number * - the position in the 'Y' direction is given by getAxis(1)[spectrum_index] * - the position in the 'X' direction is the central point of the bin *(dataX[column] + dataX[column+1])/2 * * Having the position of the pixel, as defined above, if the distance is *outside the ring defined by minRadio, maxRadio, * it defines the bin position as -1. * * If the pixel is inside the ring, it calculates the angle of the pixel and *calls fromAngleToBin to define the bin * position. * @param ws: pointer to the workspace * @param spectrum_index: index of the spectrum * @param bins_pos: bin positions (for each column inside the spectrum, the *correspondent bin_pos) */ void RingProfile::getBinForPixel(const API::MatrixWorkspace_sptr ws, int spectrum_index, std::vector<int> &bins_pos) { if (bins_pos.size() != ws->dataY(spectrum_index).size()) throw std::runtime_error("Invalid bin positions vector"); API::NumericAxis *oldAxis2 = dynamic_cast<API::NumericAxis *>(ws->getAxis(1)); // assumption y position is the ws->getAxis(1)(spectrum_index) if (!oldAxis2) { throw std::logic_error("Failed to cast workspace axis to NumericAxis"); } // calculate ypos, the difference of y - centre and the square of this // difference double ypos = (*oldAxis2)(spectrum_index); double diffy = ypos - centre_y; double diffy_quad = pow(diffy, 2.0); // the reference to X bins (the limits for each pixel in the horizontal // direction) auto xvec = ws->dataX(spectrum_index); // for each pixel inside this row for (size_t i = 0; i < xvec.size() - 1; i++) { double xpos = (xvec[i] + xvec[i + 1]) / 2.0; // the x position is the centre of the bins boundaries double diffx = xpos - centre_x; // calculate the distance => norm of pixel position - centre double distance = sqrt(pow(diffx, 2.0) + diffy_quad); // check if the distance is inside the ring if (distance < min_radius || distance > max_radius || distance == 0) { bins_pos[i] = -1; continue; } double angle = atan2(diffy, diffx); // call fromAngleToBin (radians) bins_pos[i] = fromAngleToBin(angle, false); } }
/** Performs a transformation of the form: Q^A x I^B x Ln(Q^C x I^D x E) v Q^F x * I^G x Ln(Q^H x I^I x J). * Uses the 'GeneralFunctionConstants' property where A-J are the 10 (ordered) * input constants. * @param ws The workspace to be transformed * @throw std::range_error if an attempt is made to take log of a negative * number */ void IQTransform::general(API::MatrixWorkspace_sptr ws) { MantidVec &X = ws->dataX(0); MantidVec &Y = ws->dataY(0); MantidVec &E = ws->dataE(0); const std::vector<double> C = getProperty("GeneralFunctionConstants"); // Check for the correct number of elements if (C.size() != 10) { std::string mess( "The General transformation requires 10 values to be provided."); g_log.error(mess); throw std::invalid_argument(mess); } for (size_t i = 0; i < Y.size(); ++i) { double tmpX = std::pow(X[i], C[7]) * std::pow(Y[i], C[8]) * C[9]; if (tmpX <= 0.0) throw std::range_error( "Attempt to take log of a zero or negative number."); tmpX = std::pow(X[i], C[5]) * std::pow(Y[i], C[6]) * std::log(tmpX); const double tmpY = std::pow(X[i], C[2]) * std::pow(Y[i], C[3]) * C[4]; if (tmpY <= 0.0) throw std::range_error( "Attempt to take log of a zero or negative number."); const double newY = std::pow(X[i], C[0]) * std::pow(Y[i], C[1]) * std::log(tmpY); E[i] *= std::pow(X[i], C[0]) * (C[1] * std::pow(Y[i], C[1] - 1) * std::log(tmpY) + ((std::pow(Y[i], C[1]) * std::pow(X[i], C[2]) * C[4] * C[3] * std::pow(Y[i], C[3] - 1)) / tmpY)); X[i] = tmpX; Y[i] = newY; } std::stringstream ylabel; ylabel << "Q^" << C[0] << " x I^" << C[1] << " x Ln( Q^" << C[2] << " x I^" << C[3] << " x " << C[4] << ")"; ws->setYUnitLabel(ylabel.str()); std::stringstream xlabel; xlabel << "Q^" << C[5] << " x I^" << C[6] << " x Ln( Q^" << C[7] << " x I^" << C[8] << " x " << C[9] << ")"; m_label->setLabel(xlabel.str()); }
/** Performs the Debye-Bueche transformation: 1/sqrt(I) v Q^2 * The output is set to zero for negative input Y values * @param ws The workspace to be transformed */ void IQTransform::debyeBueche(API::MatrixWorkspace_sptr ws) { MantidVec &X = ws->dataX(0); MantidVec &Y = ws->dataY(0); MantidVec &E = ws->dataE(0); std::transform(X.begin(), X.end(), X.begin(), VectorHelper::Squares<double>()); for (size_t i = 0; i < Y.size(); ++i) { if (Y[i] > 0.0) { Y[i] = 1.0 / std::sqrt(Y[i]); E[i] *= std::pow(Y[i], 3); } else { Y[i] = 0.0; E[i] = 0.0; } } ws->setYUnitLabel("1/sqrt(I)"); m_label->setLabel("Q^2"); }
/** Zeroes all data points outside the X values given * @param outputWorkspace :: The output workspace - data has already been * copied * @param inIndex :: The workspace index of the spectrum in the input * workspace * @param outIndex :: The workspace index of the spectrum in the output * workspace */ void ExtractSpectra::cropRagged(API::MatrixWorkspace_sptr outputWorkspace, int inIndex, int outIndex) { MantidVec &Y = outputWorkspace->dataY(outIndex); MantidVec &E = outputWorkspace->dataE(outIndex); const size_t size = Y.size(); size_t startX = this->getXMin(inIndex); if (startX > size) startX = size; for (size_t i = 0; i < startX; ++i) { Y[i] = 0.0; E[i] = 0.0; } size_t endX = this->getXMax(inIndex); if (endX > 0) endX -= m_histogram; for (size_t i = endX; i < size; ++i) { Y[i] = 0.0; E[i] = 0.0; } }
/** * Perform a call to nxgetslab, via the NexusClasses wrapped methods for a given blocksize. This assumes that the * xbins have alread been cached * @param data :: The NXDataSet object of y values * @param errors :: The NXDataSet object of error values * @param blocksize :: The blocksize to use * @param nchannels :: The number of channels for the block * @param hist :: The workspace index to start reading into * @param local_workspace :: A pointer to the workspace */ void LoadNexusProcessed::loadBlock(NXDataSetTyped<double> & data, NXDataSetTyped<double> & errors, int64_t blocksize, int64_t nchannels, int64_t &hist, API::MatrixWorkspace_sptr local_workspace) { data.load(static_cast<int>(blocksize),static_cast<int>(hist)); errors.load(static_cast<int>(blocksize),static_cast<int>(hist)); double *data_start = data(); double *data_end = data_start + nchannels; double *err_start = errors(); double *err_end = err_start + nchannels; int64_t final(hist + blocksize); while( hist < final ) { MantidVec& Y = local_workspace->dataY(hist); Y.assign(data_start, data_end); data_start += nchannels; data_end += nchannels; MantidVec& E = local_workspace->dataE(hist); E.assign(err_start, err_end); err_start += nchannels; err_end += nchannels; local_workspace->setX(hist, m_xbins); ++hist; } }
/** Carries out the bin-by-bin normalisation * @param inputWorkspace The input workspace * @param outputWorkspace The result workspace */ void NormaliseToMonitor::normaliseBinByBin(API::MatrixWorkspace_sptr inputWorkspace, API::MatrixWorkspace_sptr& outputWorkspace) { EventWorkspace_sptr inputEvent = boost::dynamic_pointer_cast<EventWorkspace>(inputWorkspace); EventWorkspace_sptr outputEvent; // Only create output workspace if different to input one if (outputWorkspace != inputWorkspace ) { if (inputEvent) { //Make a brand new EventWorkspace outputEvent = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create("EventWorkspace", inputEvent->getNumberHistograms(), 2, 1)); //Copy geometry and data API::WorkspaceFactory::Instance().initializeFromParent(inputEvent, outputEvent, false); outputEvent->copyDataFrom( (*inputEvent) ); outputWorkspace = boost::dynamic_pointer_cast<MatrixWorkspace>(outputEvent); } else outputWorkspace = WorkspaceFactory::Instance().create(inputWorkspace); } // Get hold of the monitor spectrum const MantidVec& monX = m_monitor->readX(0); MantidVec& monY = m_monitor->dataY(0); MantidVec& monE = m_monitor->dataE(0); // Calculate the overall normalisation just the once if bins are all matching if (m_commonBins) this->normalisationFactor(m_monitor->readX(0),&monY,&monE); const size_t numHists = inputWorkspace->getNumberHistograms(); MantidVec::size_type specLength = inputWorkspace->blocksize(); Progress prog(this,0.0,1.0,numHists); // Loop over spectra PARALLEL_FOR3(inputWorkspace,outputWorkspace,m_monitor) for (int64_t i = 0; i < int64_t(numHists); ++i) { PARALLEL_START_INTERUPT_REGION prog.report(); const MantidVec& X = inputWorkspace->readX(i); // If not rebinning, just point to our monitor spectra, otherwise create new vectors MantidVec* Y = ( m_commonBins ? &monY : new MantidVec(specLength) ); MantidVec* E = ( m_commonBins ? &monE : new MantidVec(specLength) ); if (!m_commonBins) { // ConvertUnits can give X vectors of all zeroes - skip these, they cause problems if (X.back() == 0.0 && X.front() == 0.0) continue; // Rebin the monitor spectrum to match the binning of the current data spectrum VectorHelper::rebinHistogram(monX,monY,monE,X,*Y,*E,false); // Recalculate the overall normalisation factor this->normalisationFactor(X,Y,E); } if (inputEvent) { // ----------------------------------- EventWorkspace --------------------------------------- EventList & outEL = outputEvent->getEventList(i); outEL.divide(X, *Y, *E); } else { // ----------------------------------- Workspace2D --------------------------------------- const MantidVec& inY = inputWorkspace->readY(i); const MantidVec& inE = inputWorkspace->readE(i); MantidVec& YOut = outputWorkspace->dataY(i); MantidVec& EOut = outputWorkspace->dataE(i); outputWorkspace->dataX(i) = inputWorkspace->readX(i); // The code below comes more or less straight out of Divide.cpp for (MantidVec::size_type k = 0; k < specLength; ++k) { // Get references to the input Y's const double& leftY = inY[k]; const double& rightY = (*Y)[k]; // Calculate result and store in local variable to avoid overwriting original data if // output workspace is same as one of the input ones const double newY = leftY/rightY; if (fabs(rightY)>1.0e-12 && fabs(newY)>1.0e-12) { const double lhsFactor = (inE[k]<1.0e-12|| fabs(leftY)<1.0e-12) ? 0.0 : pow((inE[k]/leftY),2); const double rhsFactor = (*E)[k]<1.0e-12 ? 0.0 : pow(((*E)[k]/rightY),2); EOut[k] = std::abs(newY) * sqrt(lhsFactor+rhsFactor); } // Now store the result YOut[k] = newY; } // end Workspace2D case } // end loop over current spectrum if (!m_commonBins) { delete Y; delete E; } PARALLEL_END_INTERUPT_REGION } // end loop over spectra PARALLEL_CHECK_INTERUPT_REGION }
/** Executes the rebin algorithm * * @throw runtime_error Thrown if */ void Rebunch::exec() { // retrieve the properties int n_bunch=getProperty("NBunch"); // Get the input workspace MatrixWorkspace_const_sptr inputW = getProperty("InputWorkspace"); bool dist = inputW->isDistribution(); // workspace independent determination of length int histnumber = static_cast<int>(inputW->size()/inputW->blocksize()); /* const std::vector<double>& Xold = inputW->readX(0); const std::vector<double>& Yold = inputW->readY(0); int size_x=Xold.size(); int size_y=Yold.size(); */ int size_x = static_cast<int>(inputW->readX(0).size()); int size_y = static_cast<int>(inputW->readY(0).size()); //signal is the same length for histogram and point data int ny=(size_y/n_bunch); if(size_y%n_bunch >0)ny+=1; // default is for hist int nx=ny+1; bool point=false; if (size_x==size_y) { point=true; nx=ny; } // make output Workspace the same type is the input, but with new length of signal array API::MatrixWorkspace_sptr outputW = API::WorkspaceFactory::Instance().create(inputW,histnumber,nx,ny); int progress_step = histnumber / 100; if (progress_step == 0) progress_step = 1; PARALLEL_FOR2(inputW,outputW) for (int hist=0; hist < histnumber;hist++) { PARALLEL_START_INTERUPT_REGION // Ensure that axis information are copied to the output workspace if the axis exists try { outputW->getAxis(1)->spectraNo(hist)=inputW->getAxis(1)->spectraNo(hist); } catch( Exception::IndexError& ) { // Not a Workspace2D } // get const references to input Workspace arrays (no copying) const MantidVec& XValues = inputW->readX(hist); const MantidVec& YValues = inputW->readY(hist); const MantidVec& YErrors = inputW->readE(hist); //get references to output workspace data (no copying) MantidVec& XValues_new=outputW->dataX(hist); MantidVec& YValues_new=outputW->dataY(hist); MantidVec& YErrors_new=outputW->dataE(hist); // output data arrays are implicitly filled by function if(point) { rebunch_point(XValues,YValues,YErrors,XValues_new,YValues_new,YErrors_new,n_bunch); } else { rebunch_hist(XValues,YValues,YErrors,XValues_new,YValues_new,YErrors_new,n_bunch, dist); } if (hist % progress_step == 0) { progress(double(hist)/histnumber); interruption_point(); } PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION outputW->isDistribution(dist); // Copy units if (outputW->getAxis(0)->unit().get()) outputW->getAxis(0)->unit() = inputW->getAxis(0)->unit(); try { if (inputW->getAxis(1)->unit().get()) outputW->getAxis(1)->unit() = inputW->getAxis(1)->unit(); } catch(Exception::IndexError&) { // OK, so this isn't a Workspace2D } // Assign it to the output workspace property setProperty("OutputWorkspace",outputW); return; }
/** * Move the user selected spectra in the input workspace into groups in the output workspace * @param inputWS :: user selected input workspace for the algorithm * @param outputWS :: user selected output workspace for the algorithm * @param prog4Copy :: the amount of algorithm progress to attribute to moving a single spectra * @return number of new grouped spectra */ size_t GroupDetectors2::formGroupsEvent( DataObjects::EventWorkspace_const_sptr inputWS, DataObjects::EventWorkspace_sptr outputWS, const double prog4Copy) { // get "Behaviour" string const std::string behaviour = getProperty("Behaviour"); int bhv = 0; if ( behaviour == "Average" ) bhv = 1; API::MatrixWorkspace_sptr beh = API::WorkspaceFactory::Instance().create( "Workspace2D", static_cast<int>(m_GroupSpecInds.size()), 1, 1); g_log.debug() << name() << ": Preparing to group spectra into " << m_GroupSpecInds.size() << " groups\n"; // where we are copying spectra to, we start copying to the start of the output workspace size_t outIndex = 0; // Only used for averaging behaviour. We may have a 1:1 map where a Divide would be waste as it would be just dividing by 1 bool requireDivide(false); for ( storage_map::const_iterator it = m_GroupSpecInds.begin(); it != m_GroupSpecInds.end() ; ++it ) { // This is the grouped spectrum EventList & outEL = outputWS->getEventList(outIndex); // The spectrum number of the group is the key outEL.setSpectrumNo(it->first); // Start fresh with no detector IDs outEL.clearDetectorIDs(); // the Y values and errors from spectra being grouped are combined in the output spectrum // Keep track of number of detectors required for masking size_t nonMaskedSpectra(0); beh->dataX(outIndex)[0] = 0.0; beh->dataE(outIndex)[0] = 0.0; for( std::vector<size_t>::const_iterator wsIter = it->second.begin(); wsIter != it->second.end(); ++wsIter) { const size_t originalWI = *wsIter; const EventList & fromEL=inputWS->getEventList(originalWI); //Add the event lists with the operator outEL += fromEL; // detectors to add to the output spectrum outEL.addDetectorIDs(fromEL.getDetectorIDs() ); try { Geometry::IDetector_const_sptr det = inputWS->getDetector(originalWI); if( !det->isMasked() ) ++nonMaskedSpectra; } catch(Exception::NotFoundError&) { // If a detector cannot be found, it cannot be masked ++nonMaskedSpectra; } } if( nonMaskedSpectra == 0 ) ++nonMaskedSpectra; // Avoid possible divide by zero if(!requireDivide) requireDivide = (nonMaskedSpectra > 1); beh->dataY(outIndex)[0] = static_cast<double>(nonMaskedSpectra); // make regular progress reports and check for cancelling the algorithm if ( outIndex % INTERVAL == 0 ) { m_FracCompl += INTERVAL*prog4Copy; if ( m_FracCompl > 1.0 ) m_FracCompl = 1.0; progress(m_FracCompl); interruption_point(); } outIndex ++; } // Refresh the spectraDetectorMap outputWS->doneAddingEventLists(); if ( bhv == 1 && requireDivide ) { g_log.debug() << "Running Divide algorithm to perform averaging.\n"; Mantid::API::IAlgorithm_sptr divide = createChildAlgorithm("Divide"); divide->initialize(); divide->setProperty<API::MatrixWorkspace_sptr>("LHSWorkspace", outputWS); divide->setProperty<API::MatrixWorkspace_sptr>("RHSWorkspace", beh); divide->setProperty<API::MatrixWorkspace_sptr>("OutputWorkspace", outputWS); divide->execute(); } g_log.debug() << name() << " created " << outIndex << " new grouped spectra\n"; return outIndex; }
/** * Move the user selected spectra in the input workspace into groups in the output workspace * @param inputWS :: user selected input workspace for the algorithm * @param outputWS :: user selected output workspace for the algorithm * @param prog4Copy :: the amount of algorithm progress to attribute to moving a single spectra * @return number of new grouped spectra */ size_t GroupDetectors2::formGroups( API::MatrixWorkspace_const_sptr inputWS, API::MatrixWorkspace_sptr outputWS, const double prog4Copy) { // get "Behaviour" string const std::string behaviour = getProperty("Behaviour"); int bhv = 0; if ( behaviour == "Average" ) bhv = 1; API::MatrixWorkspace_sptr beh = API::WorkspaceFactory::Instance().create( "Workspace2D", static_cast<int>(m_GroupSpecInds.size()), 1, 1); g_log.debug() << name() << ": Preparing to group spectra into " << m_GroupSpecInds.size() << " groups\n"; // where we are copying spectra to, we start copying to the start of the output workspace size_t outIndex = 0; // Only used for averaging behaviour. We may have a 1:1 map where a Divide would be waste as it would be just dividing by 1 bool requireDivide(false); for ( storage_map::const_iterator it = m_GroupSpecInds.begin(); it != m_GroupSpecInds.end() ; ++it ) { // This is the grouped spectrum ISpectrum * outSpec = outputWS->getSpectrum(outIndex); // The spectrum number of the group is the key outSpec->setSpectrumNo(it->first); // Start fresh with no detector IDs outSpec->clearDetectorIDs(); // Copy over X data from first spectrum, the bin boundaries for all spectra are assumed to be the same here outSpec->dataX() = inputWS->readX(0); // the Y values and errors from spectra being grouped are combined in the output spectrum // Keep track of number of detectors required for masking size_t nonMaskedSpectra(0); beh->dataX(outIndex)[0] = 0.0; beh->dataE(outIndex)[0] = 0.0; for( std::vector<size_t>::const_iterator wsIter = it->second.begin(); wsIter != it->second.end(); ++wsIter) { const size_t originalWI = *wsIter; // detectors to add to firstSpecNum const ISpectrum * fromSpectrum = inputWS->getSpectrum(originalWI); // Add up all the Y spectra and store the result in the first one // Need to keep the next 3 lines inside loop for now until ManagedWorkspace mru-list works properly MantidVec &firstY = outSpec->dataY(); MantidVec::iterator fYit; MantidVec::iterator fEit = outSpec->dataE().begin(); MantidVec::const_iterator Yit = fromSpectrum->dataY().begin(); MantidVec::const_iterator Eit = fromSpectrum->dataE().begin(); for (fYit = firstY.begin(); fYit != firstY.end(); ++fYit, ++fEit, ++Yit, ++Eit) { *fYit += *Yit; // Assume 'normal' (i.e. Gaussian) combination of errors *fEit = std::sqrt( (*fEit)*(*fEit) + (*Eit)*(*Eit) ); } // detectors to add to the output spectrum outSpec->addDetectorIDs(fromSpectrum->getDetectorIDs() ); try { Geometry::IDetector_const_sptr det = inputWS->getDetector(originalWI); if( !det->isMasked() ) ++nonMaskedSpectra; } catch(Exception::NotFoundError&) { // If a detector cannot be found, it cannot be masked ++nonMaskedSpectra; } } if( nonMaskedSpectra == 0 ) ++nonMaskedSpectra; // Avoid possible divide by zero if(!requireDivide) requireDivide = (nonMaskedSpectra > 1); beh->dataY(outIndex)[0] = static_cast<double>(nonMaskedSpectra); // make regular progress reports and check for cancelling the algorithm if ( outIndex % INTERVAL == 0 ) { m_FracCompl += INTERVAL*prog4Copy; if ( m_FracCompl > 1.0 ) m_FracCompl = 1.0; progress(m_FracCompl); interruption_point(); } outIndex ++; } // Refresh the spectraDetectorMap outputWS->generateSpectraMap(); if ( bhv == 1 && requireDivide ) { g_log.debug() << "Running Divide algorithm to perform averaging.\n"; Mantid::API::IAlgorithm_sptr divide = createChildAlgorithm("Divide"); divide->initialize(); divide->setProperty<API::MatrixWorkspace_sptr>("LHSWorkspace", outputWS); divide->setProperty<API::MatrixWorkspace_sptr>("RHSWorkspace", beh); divide->setProperty<API::MatrixWorkspace_sptr>("OutputWorkspace", outputWS); divide->execute(); } g_log.debug() << name() << " created " << outIndex << " new grouped spectra\n"; return outIndex; }
/** * Takes a single valued histogram workspace and assesses which histograms are within the limits. * Those that are not are masked on the input workspace. * @param countsWS :: Input/Output Integrated workspace to diagnose. * @param medianvec The median value calculated from the current counts. * @param indexmap Index map. * @param maskWS :: A mask workspace to apply. * @return The number of detectors that failed the tests, not including those skipped. */ int MedianDetectorTest::doDetectorTests(const API::MatrixWorkspace_sptr countsWS, const std::vector<double> medianvec, std::vector<std::vector<size_t> > indexmap, API::MatrixWorkspace_sptr maskWS) { g_log.debug("Applying the criteria to find failing detectors"); // A spectra can't fail if the statistics show its value is consistent with the mean value, // check the error and how many errorbars we are away const double minSigma = getProperty("SignificanceTest"); // prepare to report progress const int numSpec(m_maxSpec - m_minSpec); const int progStep = static_cast<int>(ceil(numSpec/30.0)); int steps(0); const double deadValue(1.0); int numFailed(0); bool checkForMask = false; Geometry::Instrument_const_sptr instrument = countsWS->getInstrument(); if (instrument != NULL) { checkForMask = ((instrument->getSource() != NULL) && (instrument->getSample() != NULL)); } PARALLEL_FOR2(countsWS, maskWS) for (int j=0;j<static_cast<int>(indexmap.size());++j) { std::vector<size_t> hists=indexmap.at(j); double median=medianvec.at(j); const size_t nhist = hists.size(); g_log.debug() << "new component with " <<nhist <<" spectra.\n"; for (size_t i = 0; i < nhist; ++i) { g_log.debug() << "Counts workspace index=" << i << ", Mask workspace index=" << hists.at(i) << std::endl; PARALLEL_START_INTERUPT_REGION ++steps; // update the progressbar information if (steps % progStep == 0) { progress(advanceProgress(progStep*static_cast<double>(RTMarkDetects)/numSpec)); } if (checkForMask) { const std::set<detid_t>& detids = countsWS->getSpectrum(i)->getDetectorIDs(); if (instrument->isDetectorMasked(detids)) { maskWS->dataY(hists.at(i))[0] = deadValue; continue; } if (instrument->isMonitor(detids)) { // Don't include in calculation but don't mask it continue; } } const double signal = countsWS->dataY(hists.at(i))[0]; // Mask out NaN and infinite if( boost::math::isinf(signal) || boost::math::isnan(signal) ) { maskWS->dataY(hists.at(i))[0] = deadValue; PARALLEL_ATOMIC ++numFailed; continue; } const double error = minSigma*countsWS->readE(hists.at(i))[0]; if( (signal < median*m_loFrac && (signal-median < -error)) || (signal > median*m_hiFrac && (signal-median > error)) ) { maskWS->dataY(hists.at(i))[0] = deadValue; PARALLEL_ATOMIC ++numFailed; } PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // Log finds g_log.information() << numFailed << " spectra failed the median tests.\n"; } return numFailed; }
/** Generate peaks in the given output workspace * @param functionmap :: map to contain the list of functions with key as their spectra * @param dataWS :: output matrix workspace */ void GeneratePeaks::generatePeaks(const std::map<specid_t, std::vector<std::pair<double, API::IFunction_sptr> > >& functionmap, API::MatrixWorkspace_sptr dataWS) { // Calcualte function std::map<specid_t, std::vector<std::pair<double, API::IFunction_sptr> > >::const_iterator mapiter; for (mapiter = functionmap.begin(); mapiter != functionmap.end(); ++mapiter) { // Get spec id and translated to wsindex in the output workspace specid_t specid = mapiter->first; specid_t wsindex; if (m_newWSFromParent) wsindex = specid; else wsindex = m_SpectrumMap[specid]; const std::vector<std::pair<double, API::IFunction_sptr> >& vec_centrefunc = mapiter->second; size_t numpeaksinspec = mapiter->second.size(); for (size_t ipeak = 0; ipeak < numpeaksinspec; ++ipeak) { const std::pair<double, API::IFunction_sptr>& centrefunc = vec_centrefunc[ipeak]; // Determine boundary API::IPeakFunction_sptr thispeak = getPeakFunction(centrefunc.second); double centre = centrefunc.first; double fwhm = thispeak->fwhm(); // const MantidVec& X = dataWS->dataX(wsindex); double leftbound = centre - m_numPeakWidth*fwhm; if (ipeak > 0) { // Not left most peak. API::IPeakFunction_sptr leftPeak = getPeakFunction(vec_centrefunc[ipeak-1].second); double middle = 0.5*(centre + leftPeak->centre()); if (leftbound < middle) leftbound = middle; } std::vector<double>::const_iterator left = std::lower_bound(X.begin(), X.end(), leftbound); if (left == X.end()) left = X.begin(); double rightbound = centre + m_numPeakWidth*fwhm; if (ipeak != numpeaksinspec-1) { // Not the rightmost peak IPeakFunction_sptr rightPeak = getPeakFunction(vec_centrefunc[ipeak+1].second); double middle = 0.5*(centre + rightPeak->centre()); if (rightbound > middle) rightbound = middle; } std::vector<double>::const_iterator right = std::lower_bound(left + 1, X.end(), rightbound); // Build domain & function API::FunctionDomain1DVector domain(left, right); //dataWS->dataX(wsindex)); // Evaluate the function API::FunctionValues values(domain); centrefunc.second->function(domain, values); // Put to output std::size_t offset = (left-X.begin()); std::size_t numY = values.size(); for (std::size_t i = 0; i < numY; i ++) { dataWS->dataY(wsindex)[i + offset] += values[i]; } } // ENDFOR(ipeak) } return; }
/** Executes the algorithm * */ void SplineBackground::exec() { API::MatrixWorkspace_sptr inWS = getProperty("InputWorkspace"); int spec = getProperty("WorkspaceIndex"); if (spec > static_cast<int>(inWS->getNumberHistograms())) throw std::out_of_range("WorkspaceIndex is out of range."); const MantidVec& X = inWS->readX(spec); const MantidVec& Y = inWS->readY(spec); const MantidVec& E = inWS->readE(spec); const bool isHistogram = inWS->isHistogramData(); const int ncoeffs = getProperty("NCoeff"); const int k = 4; // order of the spline + 1 (cubic) const int nbreak = ncoeffs - (k - 2); if (nbreak <= 0) throw std::out_of_range("Too low NCoeff"); gsl_bspline_workspace *bw; gsl_vector *B; gsl_vector *c, *w, *x, *y; gsl_matrix *Z, *cov; gsl_multifit_linear_workspace *mw; double chisq; int n = static_cast<int>(Y.size()); bool isMasked = inWS->hasMaskedBins(spec); std::vector<int> masked(Y.size()); if (isMasked) { for(API::MatrixWorkspace::MaskList::const_iterator it=inWS->maskedBins(spec).begin();it!=inWS->maskedBins(spec).end();++it) masked[it->first] = 1; n -= static_cast<int>(inWS->maskedBins(spec).size()); } if (n < ncoeffs) { g_log.error("Too many basis functions (NCoeff)"); throw std::out_of_range("Too many basis functions (NCoeff)"); } /* allocate a cubic bspline workspace (k = 4) */ bw = gsl_bspline_alloc(k, nbreak); B = gsl_vector_alloc(ncoeffs); x = gsl_vector_alloc(n); y = gsl_vector_alloc(n); Z = gsl_matrix_alloc(n, ncoeffs); c = gsl_vector_alloc(ncoeffs); w = gsl_vector_alloc(n); cov = gsl_matrix_alloc(ncoeffs, ncoeffs); mw = gsl_multifit_linear_alloc(n, ncoeffs); /* this is the data to be fitted */ int j = 0; for (MantidVec::size_type i = 0; i < Y.size(); ++i) { if (isMasked && masked[i]) continue; gsl_vector_set(x, j, (isHistogram ? (0.5*(X[i]+X[i+1])) : X[i])); // Middle of the bins, if a histogram gsl_vector_set(y, j, Y[i]); gsl_vector_set(w, j, E[i]>0.?1./(E[i]*E[i]):0.); ++j; } if (n != j) { gsl_bspline_free(bw); gsl_vector_free(B); gsl_vector_free(x); gsl_vector_free(y); gsl_matrix_free(Z); gsl_vector_free(c); gsl_vector_free(w); gsl_matrix_free(cov); gsl_multifit_linear_free(mw); throw std::runtime_error("Assertion failed: n != j"); } double xStart = X.front(); double xEnd = X.back(); /* use uniform breakpoints */ gsl_bspline_knots_uniform(xStart, xEnd, bw); /* construct the fit matrix X */ for (int i = 0; i < n; ++i) { double xi=gsl_vector_get(x, i); /* compute B_j(xi) for all j */ gsl_bspline_eval(xi, B, bw); /* fill in row i of X */ for (j = 0; j < ncoeffs; ++j) { double Bj = gsl_vector_get(B, j); gsl_matrix_set(Z, i, j, Bj); } } /* do the fit */ gsl_multifit_wlinear(Z, w, y, c, cov, &chisq, mw); /* output the smoothed curve */ API::MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(inWS,1,X.size(),Y.size()); { outWS->getAxis(1)->setValue(0, inWS->getAxis(1)->spectraNo(spec)); double xi, yi, yerr; for (MantidVec::size_type i=0;i<Y.size();i++) { xi = X[i]; gsl_bspline_eval(xi, B, bw); gsl_multifit_linear_est(B, c, cov, &yi, &yerr); outWS->dataY(0)[i] = yi; outWS->dataE(0)[i] = yerr; } outWS->dataX(0) = X; } gsl_bspline_free(bw); gsl_vector_free(B); gsl_vector_free(x); gsl_vector_free(y); gsl_matrix_free(Z); gsl_vector_free(c); gsl_vector_free(w); gsl_matrix_free(cov); gsl_multifit_linear_free(mw); setProperty("OutputWorkspace",outWS); }