-
Notifications
You must be signed in to change notification settings - Fork 122
/
MDEventsTestHelper.h
334 lines (297 loc) · 12.3 KB
/
MDEventsTestHelper.h
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*********************************************************************************
* PLEASE READ THIS!!!!!!!
*
* This header MAY NOT be included in any test from a package below the level
*of
* MDEvents (e.g. Kernel, Geometry, API, DataObjects).
*********************************************************************************/
#ifndef MDEVENTSTEST_HELPER_H
#define MDEVENTSTEST_HELPER_H
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Utils.h"
#include "MantidAPI/BoxController.h"
#include "MantidMDEvents/MDEventFactory.h"
#include "MantidMDEvents/MDEventWorkspace.h"
#include "MantidMDEvents/MDLeanEvent.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidKernel/SingletonHolder.h"
namespace Mantid {
namespace MDEvents {
/** Set of helper methods for testing MDEventWorkspace things
*
* @author Janik Zikovsky
* @date March 29, 2011
* */
namespace MDEventsTestHelper {
/** Create an EventWorkspace containing fake data
* of single-crystal diffraction.
* Instrument is MINITOPAZ
*
* @return EventWorkspace_sptr
*/
Mantid::DataObjects::EventWorkspace_sptr
createDiffractionEventWorkspace(int numEvents, int numPixels = 400,
int numBins = 160);
/** Make a (optionally) file backed MDEventWorkspace with 10000 fake random data
*points
*
* @param wsName :: name of the workspace in ADS
* @param fileBacked :: true for file-backed
* @return MDEW sptr
*/
Mantid::MDEvents::MDEventWorkspace3Lean::sptr
makeFileBackedMDEW(std::string wsName, bool fileBacked, long numEvents = 10000,
Kernel::SpecialCoordinateSystem coord = Kernel::None);
/// Make a fake n-dimensional MDHistoWorkspace
Mantid::MDEvents::MDHistoWorkspace_sptr
makeFakeMDHistoWorkspace(double signal, size_t numDims, size_t numBins = 10,
coord_t max = 10.0, double errorSquared = 1.0,
std::string name = "", double numEvents = 1.0);
/// More general fake n-dimensionsal MDHistoWorkspace
Mantid::MDEvents::MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(
size_t numDims, double signal, double errorSquared, size_t *numBins,
coord_t *min, coord_t *max, std::string name = "");
/// More general fake n-dimensionsal MDHistoWorkspace
Mantid::MDEvents::MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(
size_t numDims, double signal, double errorSquared, size_t *numBins,
coord_t *min, coord_t *max, std::vector<std::string> names,
std::string name = "");
//-------------------------------------------------------------------------------------
/** Create a test MDEventWorkspace<nd> . Dimensions are names Axis0, Axis1, etc.
*
* @param splitInto :: each dimension will split into this many subgrids
* @param min :: extent of each dimension (min)
* @param max :: extent of each dimension (max)
* @param numEventsPerBox :: will create one MDLeanEvent in the center of each
*sub-box.
* 0 = don't split box, don't add events
* @param wsName :: if specified, then add the workspace to the analysis data
*service
* @param axisNameFormat :: string for the axis name, processed via sprintf()
* @param axisIdFormat :: string for the axis ID, processed via sprintf()
* @return shared ptr to the created workspace
*/
template <typename MDE, size_t nd>
boost::shared_ptr<Mantid::MDEvents::MDEventWorkspace<MDE, nd>>
makeAnyMDEW(size_t splitInto, coord_t min, coord_t max,
size_t numEventsPerBox = 0, std::string wsName = "",
std::string axisNameFormat = "Axis%d",
std::string axisIdFormat = "Axis%d") {
boost::shared_ptr<Mantid::MDEvents::MDEventWorkspace<MDE, nd>> out(
new Mantid::MDEvents::MDEventWorkspace<MDE, nd>());
Mantid::API::BoxController_sptr bc = out->getBoxController();
bc->setSplitThreshold(100);
bc->setSplitInto(splitInto);
for (size_t d = 0; d < nd; d++) {
char name[200];
sprintf(name, axisNameFormat.c_str(), d);
char id[200];
sprintf(id, axisIdFormat.c_str(), d);
Mantid::Geometry::MDHistoDimension_sptr dim(
new Mantid::Geometry::MDHistoDimension(
std::string(name), std::string(id), "m", min, max, 10));
out->addDimension(dim);
}
out->initialize();
if (numEventsPerBox > 0) {
out->splitBox();
size_t index[nd];
Mantid::Kernel::Utils::NestedForLoop::SetUp(nd, index);
size_t index_max[nd];
Mantid::Kernel::Utils::NestedForLoop::SetUp(nd, index_max, splitInto);
bool allDone = false;
while (!allDone) {
for (size_t i = 0; i < numEventsPerBox; i++) {
// Put an event in the middle of each box
Mantid::coord_t centers[nd];
for (size_t d = 0; d < nd; d++)
centers[d] = min +
(static_cast<coord_t>(index[d]) + 0.5f) * (max - min) /
static_cast<coord_t>(splitInto);
out->addEvent(MDE(1.0, 1.0, centers));
}
allDone =
Mantid::Kernel::Utils::NestedForLoop::Increment(nd, index, index_max);
}
out->refreshCache();
}
auto ei = API::ExperimentInfo_sptr(new API::ExperimentInfo());
out->addExperimentInfo(ei);
// Add to ADS on option
if (!wsName.empty())
Mantid::API::AnalysisDataService::Instance().addOrReplace(wsName, out);
return out;
}
/** Make a MDEventWorkspace with MDLeanEvents */
template <size_t nd>
boost::shared_ptr<
Mantid::MDEvents::MDEventWorkspace<Mantid::MDEvents::MDLeanEvent<nd>, nd>>
makeMDEW(size_t splitInto, coord_t min, coord_t max,
size_t numEventsPerBox = 0) {
return makeAnyMDEW<Mantid::MDEvents::MDLeanEvent<nd>, nd>(splitInto, min, max,
numEventsPerBox);
}
/** Make a MDEventWorkspace with MDEvents - updated to split dims by splitInto,
* not 10 */
template <size_t nd>
boost::shared_ptr<
Mantid::MDEvents::MDEventWorkspace<Mantid::MDEvents::MDEvent<nd>, nd>>
makeMDEWFull(size_t splitInto, coord_t min, coord_t max,
size_t numEventsPerBox = 0) {
return makeAnyMDEW<Mantid::MDEvents::MDEvent<nd>, nd>(splitInto, min, max,
numEventsPerBox);
}
//=====================================================================================
//=============================== MDGRIDBOX HELPER METHODS
//============================
//=====================================================================================
/** Generate an empty MDBox */
MDBox<MDLeanEvent<1>, 1> *makeMDBox1(size_t splitInto = 10,
API::BoxController *splitter = NULL);
/** Generate an empty MDBox with 3 dimensions, split 10x5x2 */
MDBox<MDLeanEvent<3>, 3> *makeMDBox3();
/** Return a vector with this many MDEvents, spaced evenly from 0.5, 1.5, etc.
*/
std::vector<MDLeanEvent<1>> makeMDEvents1(size_t num);
//-------------------------------------------------------------------------------------
/** Generate an empty MDBox with 2 dimensions, splitting in (default) 10x10
*boxes.
* Box size is 10x10.
*
* @param split0 :: for uneven splitting
* @param split1 :: for uneven splitting
* @param dimensionMin :: minimum dimesion extent
* @param dimensionMax :: maximum dimesion extent
*/
template <size_t nd>
static MDGridBox<MDLeanEvent<nd>, nd> *
makeMDGridBox(size_t split0 = 10, size_t split1 = 10,
coord_t dimensionMin = 0.0, coord_t dimensionMax = 10.0) {
// Split at 5 events
Mantid::API::BoxController *splitter = new Mantid::API::BoxController(nd);
splitter->setSplitThreshold(5);
// Splits into 10x10x.. boxes
splitter->setSplitInto(split0);
splitter->setSplitInto(0, split0);
if (nd > 1)
splitter->setSplitInto(1, split1);
// Set the size to 10.0 in all directions
MDBox<MDLeanEvent<nd>, nd> *box = new MDBox<MDLeanEvent<nd>, nd>(splitter);
for (size_t d = 0; d < nd; d++)
// carefull! function with the side effects!
box->setExtents(d, dimensionMin, dimensionMax);
// calc volume necessary
box->calcVolume();
// Split
MDGridBox<MDLeanEvent<nd>, nd> *out = new MDGridBox<MDLeanEvent<nd>, nd>(box);
return out;
}
//-------------------------------------------------------------------------------------
/** Feed a MDGridBox with evenly-spaced events
*
* @param box :: MDGridBox pointer
* @param repeat :: how many events to stick in the same place
* @param numPerSide :: e.g. if 10, and 3 dimensions, there will be 10x10x10
*events
* @param start :: x-coordinate starts at this for event 0
* @param step :: x-coordinate increases by this much.
*/
template <size_t nd>
static void feedMDBox(MDBoxBase<MDLeanEvent<nd>, nd> *box, size_t repeat = 1,
size_t numPerSide = 10, coord_t start = 0.5,
coord_t step = 1.0) {
size_t counters[nd];
Mantid::Kernel::Utils::NestedForLoop::SetUp(nd, counters, 0);
size_t index_max[nd];
Mantid::Kernel::Utils::NestedForLoop::SetUp(nd, index_max, numPerSide);
// Recursive for loop
bool allDone = false;
while (!allDone) {
// Generate the position from the counter
coord_t centers[nd];
for (size_t d = 0; d < nd; d++)
centers[d] = static_cast<coord_t>(counters[d]) * step + start;
// Add that event 'repeat' times
for (size_t i = 0; i < repeat; ++i)
box->addEvent(MDLeanEvent<nd>(1.0, 1.0, centers));
// Increment the nested for loop
allDone = Mantid::Kernel::Utils::NestedForLoop::Increment(nd, counters,
index_max);
}
box->refreshCache(NULL);
}
//-------------------------------------------------------------------------------------
/** Recursively split an existing MDGridBox
*
* @param box :: box to split
* @param atRecurseLevel :: This is the recursion level at which we are
* @param recurseLimit :: this is where to spot
*/
template <size_t nd>
static void recurseSplit(MDGridBox<MDLeanEvent<nd>, nd> *box,
size_t atRecurseLevel, size_t recurseLimit) {
typedef std::vector<MDBoxBase<MDLeanEvent<nd>, nd> *> boxVector;
if (atRecurseLevel >= recurseLimit)
return;
// Split all the contents
boxVector boxes;
boxes = box->getBoxes();
for (size_t i = 0; i < boxes.size(); i++)
box->splitContents(i);
// Retrieve the contained MDGridBoxes
boxes = box->getBoxes();
// Go through them and split them
for (size_t i = 0; i < boxes.size(); i++) {
MDGridBox<MDLeanEvent<nd>, nd> *containedbox =
dynamic_cast<MDGridBox<MDLeanEvent<nd>, nd> *>(boxes[i]);
if (containedbox)
recurseSplit(containedbox, atRecurseLevel + 1, recurseLimit);
}
}
//-------------------------------------------------------------------------------------
/** Generate a recursively gridded MDGridBox
*
* @param splitInto :: boxes split into this many boxes/side
* @param levels :: levels of splitting recursion (0=just the top level is
*split)
* @return
*/
template <size_t nd>
static MDGridBox<MDLeanEvent<nd>, nd> *makeRecursiveMDGridBox(size_t splitInto,
size_t levels) {
// Split at 5 events
Mantid::API::BoxController *splitter(new Mantid::API::BoxController(nd));
splitter->setSplitThreshold(5);
splitter->resetNumBoxes();
splitter->setMaxDepth(levels + 1);
// Splits into splitInto x splitInto x ... boxes
splitter->setSplitInto(splitInto);
// Set the size to splitInto*1.0 in all directions
MDBox<MDLeanEvent<nd>, nd> *box = new MDBox<MDLeanEvent<nd>, nd>(splitter);
for (size_t d = 0; d < nd; d++)
box->setExtents(d, 0.0, static_cast<coord_t>(splitInto));
// Split into the gridbox.
MDGridBox<MDLeanEvent<nd>, nd> *gridbox =
new MDGridBox<MDLeanEvent<nd>, nd>(box);
// Now recursively split more
recurseSplit(gridbox, 0, levels);
return gridbox;
}
//-------------------------------------------------------------------------------------
/** Helper function compares the extents of the given box */
template <typename MDBOX>
static void extents_match(MDBOX box, size_t dim, double min, double max) {
TSM_ASSERT_DELTA(dim, box->getExtents(dim).getMin(), min, 1e-6);
TSM_ASSERT_DELTA(dim, box->getExtents(dim).getMax(), max, 1e-6);
}
void checkAndDeleteFile(std::string filename);
//=====================================================================================
//===================================== TEST METHODS
//==================================
//=====================================================================================
} // namespace
}
}
#endif