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.
#ifndef WORKSPACECREATIONHELPER_H_
#define WORKSPACECREATIONHELPER_H_
#include <cmath>
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/Workspace1D.h"
FibSeries() : x1(1),x2(1) {}
T operator()() { const T out(x1+x2); x1=x2; x2=out; return out; }
Russell Taylor
committed
};
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);
Russell Taylor
committed
retVal->initialize(1,size,size);
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);
Russell Taylor
committed
retVal->initialize(1,size,size);
Peterson, Peter
committed
}
Peterson, Peter
committed
{
return Create2DWorkspace123(xlen, ylen);
}
static Workspace2D_sptr Create2DWorkspace123(int xlen, int ylen,bool isHist=0)
Histogram1D::RCtype x1,y1,e1;
x1.access().resize(isHist?xlen+1:xlen,1);
y1.access().resize(xlen,2);
e1.access().resize(xlen,3);
retVal->initialize(ylen,isHist?xlen+1:xlen,xlen);
Russell Taylor
committed
retVal->setX(i,x1);
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);
retVal->initialize(ylen,isHist?xlen+1:xlen,xlen);
Russell Taylor
committed
retVal->setX(i,x1);
retVal->setData(i,y1,e1);
}
return retVal;
Russell Taylor
committed
}
Russell Taylor
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));
Russell Taylor
committed
{
x.access()[i] = x0+i*deltax;
Russell Taylor
committed
}
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;
}
static WorkspaceSingleValue_sptr CreateWorkspaceSingleValue(double value)
{
WorkspaceSingleValue_sptr retVal(new WorkspaceSingleValue(value,sqrt(value)));
return retVal;
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** 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;
}
//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;
}
}