Skip to content
Snippets Groups Projects
WorkspaceCreationHelper.cpp 47.7 KiB
Newer Older
/*********************************************************************************
 *  PLEASE READ THIS!!!!!!!
 *
 *  This collection of functions MAY NOT be used in any test from a package
 *below
 *  DataObjects (e.g. Kernel, Geometry, API).
 *  Conversely, this file MAY NOT be modified to use anything from a package
 *higher
 *  than DataObjects (e.g. any algorithm), even if going via the factory.
 *********************************************************************************/
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/InstrumentCreationHelper.h"

#include "MantidAPI/Run.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
Owen Arnold's avatar
Owen Arnold committed
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidGeometry/Instrument/Component.h"
#include "MantidGeometry/Objects/ShapeFactory.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidKernel/MersenneTwister.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidKernel/make_unique.h"
#include <cmath>
namespace WorkspaceCreationHelper {
using namespace Mantid;
using namespace Mantid::DataObjects;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using Mantid::MantidVec;
using Mantid::MantidVecPtr;

MockAlgorithm::MockAlgorithm(size_t nSteps) {
  m_Progress = Mantid::Kernel::make_unique<API::Progress>(this, 0, 1, nSteps);
/**
 * @param name :: The name of the workspace
 * @param ws :: The workspace object
 */
void storeWS(const std::string &name, Mantid::API::Workspace_sptr ws) {
  Mantid::API::AnalysisDataService::Instance().add(name, ws);
}
/**
 * Deletes a workspace
 * @param name :: The name of the workspace
 */
void removeWS(const std::string &name) {
  Mantid::API::AnalysisDataService::Instance().remove(name);
}
Workspace2D_sptr Create1DWorkspaceRand(int size) {
  MantidVecPtr x1, y1, e1;
  x1.access().resize(size, 1);
  y1.access().resize(size);

  MersenneTwister randomGen(DateAndTime::getCurrentTime().nanoseconds(), 0,
                            std::numeric_limits<int>::max());
  auto randFunc = [&randomGen] { return randomGen.nextValue(); };

  std::generate(y1.access().begin(), y1.access().end(), randFunc);
  e1.access().resize(size);
  std::generate(e1.access().begin(), e1.access().end(), randFunc);
  auto retVal = boost::make_shared<Workspace2D>();
  retVal->initialize(1, size, size);
  retVal->setX(0, x1);
  retVal->setData(0, y1, e1);
  return retVal;
}
Workspace2D_sptr Create1DWorkspaceConstant(int size, double value,
                                           double error) {
  MantidVecPtr x1, y1, e1;
  x1.access().resize(size, 1);
  y1.access().resize(size, value);
  e1.access().resize(size, error);
  auto retVal = boost::make_shared<Workspace2D>();
  retVal->initialize(1, size, size);
  retVal->setX(0, x1);
  retVal->setData(0, y1, e1);
  return retVal;
}
Workspace2D_sptr Create1DWorkspaceConstantWithXerror(int size, double value,
                                                     double error,
                                                     double xError) {
  auto ws = Create1DWorkspaceConstant(size, value, error);
  MantidVecPtr dx1;
  dx1.access().resize(size, xError);
  ws->setDx(0, dx1);
  return ws;
}

Workspace2D_sptr Create1DWorkspaceFib(int size) {
  MantidVecPtr x1, y1, e1;
  x1.access().resize(size, 1);
  y1.access().resize(size);
  std::generate(y1.access().begin(), y1.access().end(), FibSeries<double>());
  e1.access().resize(size);
  auto retVal = boost::make_shared<Workspace2D>();
  retVal->initialize(1, size, size);
  retVal->setX(0, x1);
  retVal->setData(0, y1, e1);
  return retVal;
}
Workspace2D_sptr Create2DWorkspace(int nhist, int numBoundaries) {
  return Create2DWorkspaceBinned(nhist, numBoundaries);
}
/** Create a Workspace2D where the Y value at each bin is
 * == to the workspace index
 * @param nhist :: # histograms
 * @param numBoundaries :: # of bins
 * @return Workspace2D
 */
Workspace2D_sptr Create2DWorkspaceWhereYIsWorkspaceIndex(int nhist,
                                                         int numBoundaries) {
  Workspace2D_sptr out = Create2DWorkspaceBinned(nhist, numBoundaries);
  for (int wi = 0; wi < nhist; wi++)
    for (int x = 0; x < numBoundaries; x++)
      out->dataY(wi)[x] = wi * 1.0;
  return out;
}
Workspace2D_sptr create2DWorkspaceThetaVsTOF(int nHist, int nBins) {
  Workspace2D_sptr outputWS = Create2DWorkspaceBinned(nHist, nBins);
  auto const newAxis = new NumericAxis(nHist);
  outputWS->replaceAxis(1, newAxis);
  newAxis->unit() = boost::make_shared<Units::Degrees>();
  for (int i = 0; i < nHist; ++i) {
    newAxis->setValue(i, i + 1);
  return outputWS;
}
Workspace2D_sptr
Create2DWorkspaceWithValues(int64_t nHist, int64_t nBins, bool isHist,
                            const std::set<int64_t> &maskedWorkspaceIndices,
                            double xVal, double yVal, double eVal) {
  MantidVecPtr x1, y1, e1;
  x1.access().resize(isHist ? nBins + 1 : nBins, xVal);
  y1.access().resize(nBins, yVal);
  e1.access().resize(nBins, eVal);
  auto retVal = boost::make_shared<Workspace2D>();
  retVal->initialize(nHist, isHist ? nBins + 1 : nBins, nBins);
  for (int i = 0; i < nHist; i++) {
    retVal->setX(i, x1);
    retVal->setData(i, y1, e1);
    retVal->getSpectrum(i)->setDetectorID(i);
    retVal->getSpectrum(i)->setSpectrumNo(i);
  retVal = maskSpectra(retVal, maskedWorkspaceIndices);
  return retVal;
}
Workspace2D_sptr Create2DWorkspaceWithValuesAndXerror(
    int64_t nHist, int64_t nBins, bool isHist, double xVal, double yVal,
    double eVal, double dxVal,
    const std::set<int64_t> &maskedWorkspaceIndices) {
  auto ws = Create2DWorkspaceWithValues(
      nHist, nBins, isHist, maskedWorkspaceIndices, xVal, yVal, eVal);
  MantidVecPtr dx1;
  dx1.access().resize(isHist ? nBins + 1 : nBins, dxVal);
  for (int i = 0; i < nHist; i++) {
    ws->setDx(i, dx1);
  }
  return ws;
}

Workspace2D_sptr
Create2DWorkspace123(int64_t nHist, int64_t nBins, bool isHist,
                     const std::set<int64_t> &maskedWorkspaceIndices) {
  return Create2DWorkspaceWithValues(nHist, nBins, isHist,
                                     maskedWorkspaceIndices, 1.0, 2.0, 3.0);
}
Workspace2D_sptr
Create2DWorkspace154(int64_t nHist, int64_t nBins, bool isHist,
                     const std::set<int64_t> &maskedWorkspaceIndices) {
  return Create2DWorkspaceWithValues(nHist, nBins, isHist,
                                     maskedWorkspaceIndices, 1.0, 5.0, 4.0);
Loading
Loading full blame...