-
Notifications
You must be signed in to change notification settings - Fork 122
/
ModifyData.cpp
98 lines (78 loc) · 3.01 KB
/
ModifyData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "ModifyData.h"
namespace Mantid
{
namespace Algorithms
{
// Algorithm must be declared
DECLARE_ALGORITHM(ModifyData)
using namespace Kernel;
using namespace API;
/** Initialization code
*
* Properties have to be declared here before they can be used
*/
void ModifyData::init()
{
// Declare a 2D input workspace property.
declareProperty(new WorkspaceProperty<>("InputWorkspace","",Direction::Input));
// Declare a 2D output workspace property.
declareProperty(new WorkspaceProperty<>("OutputWorkspace","",Direction::Output));
// Switches between two ways of accessing the data in the input workspace
declareProperty("UseVectors",false);
}
/** Executes the algorithm
*/
void ModifyData::exec()
{
// g_log is a reference to the logger. It is used to print out information,
// warning, and error messages
g_log.information() << "Running algorithm " << name() << " version " << version() << std::endl;
// Get the input workspace
MatrixWorkspace_const_sptr inputW = getProperty("InputWorkspace");
// make output Workspace the same type and size as the input one
MatrixWorkspace_sptr outputW = WorkspaceFactory::Instance().create(inputW);
g_log.information() << "Option 1. Original values:" << std::endl;
// Get the count of histograms in the input workspace
size_t histogramCount = inputW->getNumberHistograms();
// Loop over spectra
for (size_t i = 0; i < histogramCount; ++i)
{
// Retrieve the data into a vector
MantidVec& newX = outputW->dataX(i);
MantidVec& newY = outputW->dataY(i);
MantidVec& newE = outputW->dataE(i);
const MantidVec& XValues = inputW->readX(i);
const MantidVec& YValues = inputW->readY(i);
const MantidVec& EValues = inputW->readE(i);
// Iterate over i-th spectrum and modify the data
for(size_t j=0; j<inputW->blocksize(); j++)
{
g_log.information() << "Spectrum " << i << " Point " << j << " values: "
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
newX[j] = XValues[j] + static_cast<double>(i + j);
newY[j] = YValues[j]*(2. + 0.1*static_cast<double>(j));
newE[j] = EValues[j]+0.1;
}
}
// Assign it to the output workspace property
setProperty("OutputWorkspace",outputW);
// Get the newly set workspace
MatrixWorkspace_const_sptr newW = getProperty("OutputWorkspace");
// Check the new workspace
g_log.information() << "New values:" << std::endl;
int count = 0;
for(size_t i = 0; i < histogramCount; ++i)
{
const MantidVec& XValues = outputW->readX(i);
const MantidVec& YValues = outputW->readY(i);
const MantidVec& EValues = outputW->readE(i);
for(size_t j = 0; j < outputW->blocksize(); ++j)
{
// Get the reference to a data point
g_log.information() << "Point number " << count++ << " values: "
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
}
}
}
}
}