Mantid
Loading...
Searching...
No Matches
MDEventsTestHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7/*********************************************************************************
8 * PLEASE READ THIS!!!!!!!
9 *
10 * This collection of functions MAY NOT be used in any test from a package
11 *below
12 * the level of MDEvents (e.g. Kernel, Geometry, API, DataObjects).
13 *********************************************************************************/
14#include "MantidAPI/Axis.h"
20
24
30
33#include "MantidKernel/Utils.h"
34
38
39#include <memory>
40
41#include <Poco/File.h>
42
43namespace Mantid::DataObjects {
44
45using namespace Mantid::API;
51using Mantid::Types::Core::DateAndTime;
53
59namespace MDEventsTestHelper {
60
61//-------------------------------------------------------------------------------------
69 double binDelta = 10.0;
70
71 auto retVal = std::make_shared<EventWorkspace>();
72 retVal->initialize(numPixels + 2, 1, 1);
73
74 // --------- Load the instrument -----------
75 const std::string filename = FileFinder::Instance().getFullPath("unit_testing/MINITOPAZ_Definition.xml");
76 InstrumentDefinitionParser parser(filename, "MINITOPAZ", Strings::loadFile(filename));
77 auto instrument = parser.parseXML(nullptr);
78 retVal->populateInstrumentParameters();
79 retVal->setInstrument(instrument);
80
81 DateAndTime run_start("2010-01-01T00:00:00");
82
83 // create spectra for monitors
84 retVal->getSpectrum(0).addDetectorID(-1);
85 retVal->getSpectrum(1).addDetectorID(-2);
86
87 for (int pix = 0; pix < numPixels; pix++) {
88 for (int i = 0; i < numEvents; i++) {
89 retVal->getSpectrum(pix + 2) += Types::Event::TofEvent((i + 0.5) * binDelta, run_start + double(i));
90 }
91 retVal->getSpectrum(pix + 2).addDetectorID(pix);
92 }
93
94 // Create the x-axis for histogramming.
95 HistogramData::BinEdges x1(numBins);
96 auto &xRef = x1.mutableData();
97 for (int i = 0; i < numBins; ++i) {
98 xRef[i] = i * binDelta;
99 }
100
101 // Set all the histograms at once.
102 retVal->setAllX(x1);
103 // Default unit: TOF.
104 retVal->getAxis(0)->setUnit("TOF");
105
106 // Give it a crystal and goniometer
107 WorkspaceCreationHelper::setGoniometer(retVal, 0., 0., 0.);
109
110 // Some sanity checks
111 if (retVal->getInstrument()->getName() != "MINITOPAZ")
112 throw std::runtime_error("MDEventsTestHelper::"
113 "createDiffractionEventWorkspace(): Wrong "
114 "instrument loaded.");
116 retVal->getInstrument()->getDetectors(dets);
117 if (dets.size() != 100 * 100 + 2)
118 throw std::runtime_error("MDEventsTestHelper::"
119 "createDiffractionEventWorkspace(): Wrong "
120 "instrument size.");
121
122 return retVal;
123}
124
125//=====================================================================================
139 // ---------- Make a file-backed MDEventWorkspace -----------------------
140 MDEventWorkspace3Lean::sptr ws1 = MDEventsTestHelper::makeMDEW<3>(10, 0.0, 10.0, 0);
141 ws1->setCoordinateSystem(coord);
142 ws1->getBoxController()->setSplitThreshold(100);
143 API::AnalysisDataService::Instance().addOrReplace(wsName,
144 std::dynamic_pointer_cast<Mantid::API::IMDEventWorkspace>(ws1));
145 FakeMD dataFaker(std::vector<double>(1, static_cast<double>(numEvents)), std::vector<double>(), std::vector<double>(),
146 0, true);
147 dataFaker.fill(ws1);
148 return std::dynamic_pointer_cast<MDEventWorkspace3Lean>(API::AnalysisDataService::Instance().retrieve(wsName));
149}
150
151//-------------------------------------------------------------------------------------
155MDBox<MDLeanEvent<1>, 1> *makeMDBox1(size_t splitInto, BoxController *splitter) {
156 if (!splitter) {
157 splitter = (new BoxController(1));
158 }
159 // Split at 5 events
160 splitter->setSplitThreshold(5);
161 // Splits into 10 boxes
162 splitter->setSplitInto(splitInto);
163 // Set the size
164 auto *out = new MDBox<MDLeanEvent<1>, 1>(splitter);
165 out->setExtents(0, 0.0, 10.0);
166 out->calcVolume();
167 return out;
168}
169
170//-------------------------------------------------------------------------------------
175 // Split at 5 events
176 auto splitter = new BoxController(3);
177 splitter->setSplitThreshold(5);
178 // Splits into 10x5x2 boxes
179 splitter->setSplitInto(10);
180 splitter->setSplitInto(1, 5);
181 splitter->setSplitInto(2, 2);
182 // Set the size to 10.0 in all directions
183 auto out = new MDBox<MDLeanEvent<3>, 3>(splitter);
184 for (size_t d = 0; d < 3; ++d) {
185 out->setExtents(d, 0.0, 10.0);
186 }
187 out->calcVolume();
188 return out;
189}
190
191//-------------------------------------------------------------------------------------
194std::vector<MDLeanEvent<1>> makeMDEvents1(size_t num) {
195 std::vector<MDLeanEvent<1>> out;
196 out.reserve(num);
197 for (std::size_t i = 0; i < num; ++i) {
198 float coords[1] = {static_cast<float>(i) + 0.5f};
199 out.emplace_back(1.0f, 1.0f, coords);
200 }
201 return out;
202}
203
204//-------------------------------------------------------------------------------------
217Mantid::DataObjects::MDHistoWorkspace_sptr makeFakeMDHistoWorkspace(double signal, size_t numDims, size_t numBins,
218 coord_t max, double errorSquared,
219 const std::string &name, double numEvents) {
220 // Create MDFrame of General Frame type
222 return makeFakeMDHistoWorkspaceWithMDFrame(signal, numDims, frame, numBins, max, errorSquared, name, numEvents);
223}
224
225//-------------------------------------------------------------------------------------
238MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(size_t numDims, double signal, double errorSquared,
239 size_t *numBins, coord_t *min, coord_t *max,
240 const std::string &name) {
241 std::vector<std::string> names{"x", "y", "z", "t"};
242 // Create MDFrame of General Frame type
244
245 std::vector<Mantid::Geometry::MDHistoDimension_sptr> dimensions;
246 for (size_t d = 0; d < numDims; d++)
247 dimensions.emplace_back(
248 MDHistoDimension_sptr(new MDHistoDimension(names[d], names[d], frame, min[d], max[d], numBins[d])));
249
250 MDHistoWorkspace_sptr ws_sptr = std::make_shared<MDHistoWorkspace>(dimensions);
251 ws_sptr->setTo(signal, errorSquared, 1.0 /* num events */);
252 if (!name.empty())
253 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
254 return ws_sptr;
255}
256
257//-------------------------------------------------------------------------------------
271MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(size_t numDims, double signal, double errorSquared,
272 size_t *numBins, coord_t *min, coord_t *max,
273 std::vector<std::string> names, const std::string &name) {
274 std::vector<Mantid::Geometry::MDHistoDimension_sptr> dimensions;
275 // Create MDFrame of General Frame type
277 for (size_t d = 0; d < numDims; d++)
278 dimensions.emplace_back(
279 MDHistoDimension_sptr(new MDHistoDimension(names[d], names[d], frame, min[d], max[d], numBins[d])));
280
281 MDHistoWorkspace_sptr ws_sptr = std::make_shared<MDHistoWorkspace>(dimensions);
282 ws_sptr->setTo(signal, errorSquared, 1.0 /* num events */);
283 if (!name.empty())
284 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
285 return ws_sptr;
286}
287
288//-------------------------------------------------------------------------------------
303makeFakeMDHistoWorkspaceWithMDFrame(double signal, size_t numDims, const Mantid::Geometry::MDFrame &frame,
304 size_t numBins, coord_t max, double errorSquared, const std::string &name,
305 double numEvents) {
306 // MDHistoWorkspace *ws = nullptr;
307 MDHistoWorkspace_sptr ws_sptr;
308 if (numDims == 1) {
309 ws_sptr =
310 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins));
311 } else if (numDims == 2) {
312 ws_sptr =
313 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
314 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins));
315 } else if (numDims == 3) {
316 ws_sptr =
317 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
318 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins),
319 std::make_shared<MDHistoDimension>("z", "z", frame, 0.0f, max, numBins));
320 } else if (numDims == 4) {
321 ws_sptr =
322 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
323 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins),
324 std::make_shared<MDHistoDimension>("z", "z", frame, 0.0f, max, numBins),
325 std::make_shared<MDHistoDimension>("t", "t", frame, 0.0f, max, numBins));
326 }
327
328 if (!ws_sptr)
329 throw std::runtime_error(" invalid or unsupported number of dimensions given");
330
331 ws_sptr->setTo(signal, errorSquared, numEvents);
332 ws_sptr->addExperimentInfo(std::make_shared<ExperimentInfo>());
333 if (!name.empty())
334 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
335 return ws_sptr;
336}
337
342void checkAndDeleteFile(const std::string &filename) {
343 if (!filename.empty()) {
344 if (Poco::File(filename).exists()) {
345 Poco::File(filename).remove();
346 }
347 }
348}
349
350} // namespace MDEventsTestHelper
351} // namespace Mantid::DataObjects
This class is used by MDBox and MDGridBox in order to intelligently determine optimal behavior.
Definition: BoxController.h:33
void setSplitInto(size_t num)
Set the way splitting will be done.
void setSplitThreshold(size_t threshold)
Set the splitting threshold.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
Provides a helper class to add fake data to an MD workspace.
Definition: FakeMD.h:22
void fill(const API::IMDEventWorkspace_sptr &workspace)
Add the fake data to the given workspace.
Definition: FakeMD.cpp:53
void setExtents(size_t dim, double min, double max)
Set the extents of this box.
Definition: MDBoxBase.h:163
Templated class for a multi-dimensional event "box".
Definition: MDBox.h:45
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
GeneralFrame : Any MDFrame that isn't related to momemtum transfer.
Definition: GeneralFrame.h:21
static const std::string GeneralFrameDistance
Definition: GeneralFrame.h:23
Creates an instrument data from a XML instrument description file.
std::shared_ptr< Instrument > parseXML(Kernel::ProgressBase *progressReporter)
Parse XML contents.
MDFrame : The coordinate frame for a dimension, or set of dimensions in a multidimensional workspace.
Definition: MDFrame.h:22
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
bool exists(::NeXus::File &file, const std::string &name)
Based on the current group in the file, does the named sub-entry exist?
std::size_t numEvents(::NeXus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix, const NexusHDF5Descriptor &descriptor)
Get the number of events in the currently opened group.
MDBox< MDLeanEvent< 1 >, 1 > * makeMDBox1(size_t splitInto=10, API::BoxController *splitter=nullptr)
Generate an empty MDBox.
Mantid::DataObjects::EventWorkspace_sptr createDiffractionEventWorkspace(int numEvents, int numPixels=400, int numBins=160)
Create an EventWorkspace containing fake data of single-crystal diffraction.
MDHistoWorkspace_sptr makeFakeMDHistoWorkspace(double signal, size_t numDims, size_t numBins=10, coord_t max=10.0, double errorSquared=1.0, const std::string &name="", double numEvents=1.0)
Make a fake n-dimensional MDHistoWorkspace.
std::vector< MDLeanEvent< 1 > > makeMDEvents1(size_t num)
Return a vector with this many MDEvents, spaced evenly from 0.5, 1.5, etc.
Mantid::DataObjects::MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(size_t numDims, double signal, double errorSquared, size_t *numBins, coord_t *min, coord_t *max, const std::string &name="")
More general fake n-dimensionsal MDHistoWorkspace.
MDBox< MDLeanEvent< 3 >, 3 > * makeMDBox3()
Generate an empty MDBox with 3 dimensions, split 10x5x2.
void checkAndDeleteFile(const std::string &filename)
Delete a file from disk.
MDEventWorkspace3Lean::sptr makeFakeMDEventWorkspace(const std::string &wsName, long numEvents=10000, Kernel::SpecialCoordinateSystem coord=Kernel::None)
Make a (optionally) file backed MDEventWorkspace with 10000 fake random data points.
Mantid::DataObjects::MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceWithMDFrame(double signal, size_t numDims, const Mantid::Geometry::MDFrame &frame, size_t numBins=10, coord_t max=10.0, double errorSquared=1.0, const std::string &name="", double numEvents=1.0)
Creates a fake MDHistoWorkspace with MDFrame selection.
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
std::shared_ptr< MDHistoDimension > MDHistoDimension_sptr
Shared pointer to a MDHistoDimension.
Holds support functions for strings.
Definition: RegexStrings.h:16
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
Definition: Strings.cpp:28
SpecialCoordinateSystem
Special coordinate systems for Q3D.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
std::map< detid_t, Geometry::IDetector_const_sptr > detid2det_map
Typedef of a map from detector ID to detector shared pointer.
Definition: Instrument.h:27
void setOrientedLattice(const Mantid::API::MatrixWorkspace_sptr &ws, double a, double b, double c)
Sets the OrientedLattice in the crystal as an crystal with given lattice lengths, angles of 90 deg.
void setGoniometer(const Mantid::API::MatrixWorkspace_sptr &ws, double phi, double chi, double omega)
Create a default universal goniometer and set its angles.