Skip to content
Snippets Groups Projects
WorkspaceCreationHelper.hh 5.17 KiB
Newer Older
// The odd line ending on this file is to prevent cxxtest attempting to parse this collection of helper functions as if
// it were a test suite.

Nick Draper's avatar
Nick Draper committed
#ifndef WORKSPACECREATIONHELPER_H_
#define WORKSPACECREATIONHELPER_H_

#include <cmath>

#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidDataObjects/EventWorkspace.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/Workspace1D.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidDataObjects/WorkspaceSingleValue.h"
Nick Draper's avatar
Nick Draper committed

Nick Draper's avatar
Nick Draper committed
using namespace Mantid;
Nick Draper's avatar
Nick Draper committed
using namespace Mantid::DataObjects;
Nick Draper's avatar
Nick Draper committed
using namespace Mantid::Kernel;
using namespace Mantid::API;
Nick Draper's avatar
Nick Draper committed

Nick Draper's avatar
Nick Draper committed
class WorkspaceCreationHelper
Nick Draper's avatar
Nick Draper committed
{
public:

  template<typename T>
  class FibSeries
  {
Nick Draper's avatar
Nick Draper committed
  private:
Nick Draper's avatar
Nick Draper committed
    T x1;  /// Initial value 1;
    T x2;  /// Initial value 2;
Nick Draper's avatar
Nick Draper committed

  public:

Nick Draper's avatar
Nick Draper committed
    FibSeries() : x1(1),x2(1) {}
    T operator()() { const T out(x1+x2); x1=x2; x2=out;  return out; }
Nick Draper's avatar
Nick Draper committed
  static Workspace1D_sptr Create1DWorkspaceRand(int size)
  {
    Histogram1D::RCtype x1,y1,e1;
    x1.access().resize(size,1);
    y1.access().resize(size);
    std::generate(y1.access().begin(),y1.access().end(),rand);
    e1.access().resize(size);
    std::generate(e1.access().begin(),e1.access().end(),rand);
Nick Draper's avatar
Nick Draper committed
    Workspace1D_sptr retVal(new Workspace1D);
Nick Draper's avatar
Nick Draper committed
    retVal->setX(x1);
    retVal->setData(y1,e1);
    return retVal;
  }
Nick Draper's avatar
Nick Draper committed

Nick Draper's avatar
Nick Draper committed
  static Workspace1D_sptr Create1DWorkspaceFib(int size)
Nick Draper's avatar
Nick Draper committed
  {
    Histogram1D::RCtype 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);
Nick Draper's avatar
Nick Draper committed
    Workspace1D_sptr retVal(new Workspace1D);
Nick Draper's avatar
Nick Draper committed
    retVal->setX(x1);
    retVal->setData(y1,e1);
    return retVal;
Nick Draper's avatar
Nick Draper committed
  static Workspace2D_sptr Create2DWorkspace(int xlen, int ylen)
Nick Draper's avatar
Nick Draper committed
  static Workspace2D_sptr Create2DWorkspace123(int xlen, int ylen,bool isHist=0)
Nick Draper's avatar
Nick Draper committed
  {
    Histogram1D::RCtype x1,y1,e1;
    x1.access().resize(isHist?xlen+1:xlen,1);
    y1.access().resize(xlen,2);
    e1.access().resize(xlen,3);
Nick Draper's avatar
Nick Draper committed
    Workspace2D_sptr retVal(new Workspace2D);
    retVal->initialize(ylen,isHist?xlen+1:xlen,xlen);
Nick Draper's avatar
Nick Draper committed
    for (int i=0; i< ylen; i++)
    {
Nick Draper's avatar
Nick Draper committed
      retVal->setData(i,y1,e1);
    }

    return retVal;
  }
Nick Draper's avatar
Nick Draper committed

Nick Draper's avatar
Nick Draper committed
  static Workspace2D_sptr Create2DWorkspace154(int xlen, int ylen,bool isHist=0)
    Histogram1D::RCtype x1,y1,e1;
    x1.access().resize(isHist?xlen+1:xlen,1);
    y1.access().resize(xlen,5);
    e1.access().resize(xlen,4);
Nick Draper's avatar
Nick Draper committed
    Workspace2D_sptr retVal(new Workspace2D);
    retVal->initialize(ylen,isHist?xlen+1:xlen,xlen);
    for (int i=0; i< ylen; i++)
    {
      retVal->setData(i,y1,e1);
    }

    return retVal;
Nick Draper's avatar
Nick Draper committed

  static Workspace2D_sptr Create2DWorkspaceBinned(int nhist, int nbins, double x0, double deltax = 1.0)
  {
    Histogram1D::RCtype x,y,e;
    x.access().resize(nbins+1);
    y.access().resize(nbins,2);
    e.access().resize(nbins,sqrt(2.0));
    for (int i =0; i < nbins+1; ++i)
    }
    Workspace2D_sptr retVal(new Workspace2D);
    retVal->initialize(nhist,nbins+1,nbins);
    for (int i=0; i< nhist; i++)
    {
      retVal->setX(i,x);
      retVal->setData(i,y,e);
    }

    return retVal;
  }

Nick Draper's avatar
Nick Draper committed
  static WorkspaceSingleValue_sptr CreateWorkspaceSingleValue(double value)
  {
    WorkspaceSingleValue_sptr retVal(new WorkspaceSingleValue(value,sqrt(value)));
    return retVal;
  }

Nick Draper's avatar
Nick Draper committed
  /** Create event workspace with:
   * 500 pixels
   * 1000 histogrammed bins.
   */
  static EventWorkspace_sptr CreateEventWorkspace()
  {
    return CreateEventWorkspace(500,1001,100,1000);
  }

  /** Create event workspace
   */
  static EventWorkspace_sptr CreateEventWorkspace(int numPixels,
    int numBins, int numEvents = 100, int binDelta=1000, int initialize_pixels=1, int setX=1)
  {
    //add one to the number of bins as this is histogram
    numBins++;

    EventWorkspace_sptr retVal(new EventWorkspace);
    if (initialize_pixels)
      retVal->initialize(numPixels,1,1);
    else
      retVal->initialize(1,1,1);

    //Make fake events
    for (int pix=0; pix<numPixels; pix++)
    {
      for (int i=0; i<numEvents; i++)
      {
        retVal->getEventList(pix) += TofEvent((pix+i+0.5)*binDelta, 1);
      }
    }
    retVal->doneLoadingData();

    if (setX)
      {
        //Create the x-axis for histogramming.
        cow_ptr<MantidVec> axis;
        MantidVec& xRef = axis.access();
        xRef.resize(numBins);
        for (int i = 0; i < numBins; ++i)
          xRef[i] = i*binDelta;

        //Try setting a single axis
//        retVal->setX(2, axis);

        //Set all the histograms at once.
        retVal->setAllX(axis);
      }

    return retVal;
  }

Nick Draper's avatar
Nick Draper committed
  //not strictly creating a workspace, but really helpfull to see what one contains
  static void DisplayData(const MatrixWorkspace_sptr ws)
  {
      const int numHists = ws->getNumberHistograms();
      for (int i = 0; i < numHists; ++i)
      {
        for (int j = 0; j < ws->blocksize(); ++j)
        {  
          std::cout <<ws->readY(i)[j];
        }
        std::cout<<std::endl;
      }
  }

Nick Draper's avatar
Nick Draper committed
};

#endif /*WORKSPACECREATIONHELPER_H_*/