-
Notifications
You must be signed in to change notification settings - Fork 122
/
QueryMDWorkspace.cpp
300 lines (255 loc) · 10.2 KB
/
QueryMDWorkspace.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*WIKI*
This algorithm outputs a table workspace containing summary data about each box within an IMDWorkspace.
The table workspace can be used as a basis for plotting within MantidPlot.
== Format ==
* Column 1: Signal (double)
* Column 2: Error (double)
* Column 3: Number of Events (integer)
* Column 4: Coords of box center (string)
*WIKI*/
#include "MantidMDEvents/QueryMDWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/TableRow.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "../inc/MantidMDEvents/MDEventWorkspace.h"
#include "../inc/MantidMDEvents/MDEventFactory.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace Mantid
{
namespace MDEvents
{
// Register the class into the algorithm factory
DECLARE_ALGORITHM(QueryMDWorkspace)
/**
Non-member defining the no normalisation option.
@return no normalisation option.
*/
std::string noNormalisationOption()
{
return "none";
}
/**
Non-member defining the volume normalisation option.
@return volume normalisation option.
*/
std::string volumeNormalisationOption()
{
return "volume";
}
/**
Non-member defining the number of events normalisation option.
@return number of events normalisation.
*/
std::string numberOfEventsNormalisationOption()
{
return "number of events";
}
/**
Non-member method to interpret an normalisation option.
@param strNormalisation: string normalisation property value.
@return MDNormalisation option.
*/
Mantid::API::MDNormalization whichNormalisation(const std::string& strNormalisation)
{
Mantid::API::MDNormalization requestedNormalisation = Mantid::API::NoNormalization;
if(strNormalisation == noNormalisationOption())
{
requestedNormalisation = Mantid::API::NoNormalization;;
}
else if(strNormalisation == volumeNormalisationOption())
{
requestedNormalisation = Mantid::API::VolumeNormalization;
}
else
{
requestedNormalisation = Mantid::API::NumEventsNormalization;
}
return requestedNormalisation;
}
//----------------------------------------------------------------------------------------------
/** Constructor
*/
QueryMDWorkspace::QueryMDWorkspace()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
QueryMDWorkspace::~QueryMDWorkspace()
{
}
/// Documentation initalisation
void QueryMDWorkspace::initDocs()
{
this->setWikiSummary("Query the IMDWorkspace in order to extract summary information.");
}
/// Initialise the properties
void QueryMDWorkspace::init()
{
declareProperty(new WorkspaceProperty<IMDWorkspace>("InputWorkspace","",Direction::Input), "An input MDWorkspace.");
declareProperty(new WorkspaceProperty<ITableWorkspace>("OutputWorkspace","",Direction::Output),
"The output Tableworkspace "
"with columns containing key summary information about the MDWorkspace.");
declareProperty("LimitRows", true, "Limit the report output to a maximum number of rows");
declareProperty(new PropertyWithValue<int>("MaximumRows", 100000, boost::make_shared<BoundedValidator<int> >(), Direction::Input), "The number of neighbours to utilise. Defaults to 100000.");
setPropertySettings("MaximumRows", new EnabledWhenProperty("LimitRows", IS_DEFAULT));
std::vector<std::string> propOptions;
propOptions.push_back(noNormalisationOption());
propOptions.push_back(volumeNormalisationOption());
propOptions.push_back(numberOfEventsNormalisationOption());
declareProperty("Normalisation", "none", boost::make_shared<StringListValidator>(propOptions),
"What normalisation do you wish to apply"
" none: No normalisation.\n"
" volume: Normalise by the volume.\n"
" number of events: Normalise by the number of events."
);
declareProperty(new WorkspaceProperty<ITableWorkspace>("BoxDataTable","",Direction::Output, Mantid::API::PropertyMode::Optional),
"Optional output data table with MDEventWorkspace-specific box data.");
}
//----------------------------------------------------------------------------------------------
/** Make a table of box data
* @param ws :: MDEventWorkspace being added to
*/
template<typename MDE, size_t nd>
void QueryMDWorkspace::getBoxData(typename Mantid::MDEvents::MDEventWorkspace<MDE, nd>::sptr ws)
{
if (this->getPropertyValue("BoxDataTable").empty())
return;
ITableWorkspace_sptr output = WorkspaceFactory::Instance().createTable();
output->addColumn("int", "RecursionDepth");
output->addColumn("int", "NumBoxes");
output->addColumn("int", "NumWithEvents");
output->addColumn("double", "PctWithEvents");
output->addColumn("int", "TotalEvents");
output->addColumn("double", "AvgEventsPer");
output->addColumn("double", "TotalWeight");
output->addColumn("double", "TotalSignal");
output->addColumn("double", "TotalErrorSquared");
for (size_t d=0; d<nd; d++)
output->addColumn("double", "Dim" + Strings::toString(d));
size_t depth=ws->getBoxController()->getMaxDepth()+1;
std::vector<int> NumBoxes(depth, 0);
std::vector<int> NumWithEvents(depth, 0);
std::vector<int> TotalEvents(depth, 0);
std::vector<double> TotalWeight(depth, 0);
std::vector<double> TotalSignal(depth, 0);
std::vector<double> TotalErrorSquared(depth, 0);
std::vector<std::vector<double> > Dims(depth, std::vector<double>(nd,0.0) );
std::vector<API::IMDNode *> boxes;
ws->getBox()->getBoxes(boxes, depth, true);
for (size_t i=0; i<boxes.size(); i++)
{
MDBoxBase<MDE,nd> * box =dynamic_cast<MDBoxBase<MDE,nd> * >( boxes[i]);
if(!box)
throw(std::runtime_error("Can not cast IMDNode to any type of boxes"));
size_t d = box->getDepth();
NumBoxes[d] += 1;
if (box->getNPoints() > 0)
NumWithEvents[d] += 1;
TotalEvents[d] += static_cast<int>(box->getNPoints());
TotalWeight[d] += box->getTotalWeight();
TotalSignal[d] += box->getSignal();
TotalErrorSquared[d] += box->getErrorSquared();
for (size_t dim=0; dim<nd; dim++)
Dims[d][dim] = double(box->getExtents(dim).getSize());
}
int rowCounter = 0;
for (size_t d=0; d<depth; d++)
{
int col = 0;
output->appendRow();
output->cell<int>(rowCounter, col++) = int(d);
output->cell<int>(rowCounter, col++) = NumBoxes[d];
output->cell<int>(rowCounter, col++) = NumWithEvents[d];
output->cell<double>(rowCounter, col++) = 100.0 * double(NumWithEvents[d]) / double(NumBoxes[d]);
output->cell<int>(rowCounter, col++) = TotalEvents[d];
output->cell<double>(rowCounter, col++) = double(TotalEvents[d]) / double(NumBoxes[d]);
output->cell<double>(rowCounter, col++) = TotalWeight[d];
output->cell<double>(rowCounter, col++) = TotalSignal[d];
output->cell<double>(rowCounter, col++) = TotalErrorSquared[d];
for (size_t dim=0; dim<nd; dim++)
output->cell<double>(rowCounter, col++) = Dims[d][dim];
rowCounter++;
}
setProperty("BoxDataTable", output);
}
//----------------------------------------------------------------------------------------------
/// Run the algorithm
void QueryMDWorkspace::exec()
{
//Extract the required normalisation.
std::string strNormalisation = getPropertyValue("Normalisation");
MDNormalization requestedNormalisation = whichNormalisation(strNormalisation);
IMDWorkspace_sptr input = getProperty("InputWorkspace");
// Define a table workspace with a specific column schema.
ITableWorkspace_sptr output = WorkspaceFactory::Instance().createTable();
const std::string signalColumnName = "Signal/" + strNormalisation;
const std::string errorColumnName = "Error/" + strNormalisation;
output->addColumn("double", signalColumnName);
output->addColumn("double", errorColumnName);
output->addColumn("int", "Number of Events");
const size_t ndims = input->getNumDims();
for(size_t index = 0; index < ndims; ++index)
{
Mantid::Geometry::IMDDimension_const_sptr dim = input->getDimension(index);
std::string dimInUnit = dim->getName()+"/"+dim->getUnits();
output->addColumn("double",dimInUnit);
//Magic numbers required to configure the X axis.
output->getColumn(dimInUnit)->setPlotType(1);
}
//Magic numbers required to configure the Y axis.
output->getColumn(signalColumnName)->setPlotType(2);
output->getColumn(errorColumnName)->setPlotType(5);
IMDIterator* it = input->createIterator();
it->setNormalization(requestedNormalisation);
bool bLimitRows = getProperty("LimitRows");
int maxRows = 0;
if(bLimitRows)
{
maxRows = getProperty("MaximumRows");
}
// Use the iterator to loop through each MDBoxBase and create a row for each entry.
int rowCounter = 0;
Progress progress(this, 0, 1, int64_t(input->getNPoints()));
while(true)
{
size_t cellIndex = 0;
output->appendRow();
output->cell<double>(rowCounter, cellIndex++) = it->getNormalizedSignal();
output->cell<double>(rowCounter, cellIndex++) = it->getNormalizedError();
output->cell<int>(rowCounter, cellIndex++) = int(it->getNumEvents());
VMD center = it->getCenter();
const size_t numberOriginal = input->getNumberTransformsToOriginal();
if(numberOriginal > 0)
{
const size_t index = numberOriginal -1;
CoordTransform * transform = input->getTransformToOriginal(index);
VMD temp = transform->applyVMD(center);
center = temp;
}
for(size_t index = 0; index < ndims; ++index)
{
output->cell<double>(rowCounter, cellIndex++) = center[index];
}
progress.report();
if(!it->next() || (bLimitRows && ((rowCounter+1) >= maxRows)))
{
break;
}
rowCounter++;
}
setProperty("OutputWorkspace", output);
delete it;
//
IMDEventWorkspace_sptr mdew;
CALL_MDEVENT_FUNCTION(this->getBoxData, input);
}
} // namespace Mantid
} // namespace MDEvents