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 <filesystem>
40#include <memory>
41
42namespace Mantid::DataObjects {
43
44using namespace Mantid::API;
50using Mantid::Types::Core::DateAndTime;
52
58namespace MDEventsTestHelper {
59
60//-------------------------------------------------------------------------------------
68 double binDelta = 10.0;
69
70 auto retVal = std::make_shared<EventWorkspace>();
71 retVal->initialize(numPixels + 2, 1, 1);
72
73 // --------- Load the instrument -----------
74 const std::string filename = FileFinder::Instance().getFullPath("unit_testing/MINITOPAZ_Definition.xml");
75 InstrumentDefinitionParser parser(filename, "MINITOPAZ", Strings::loadFile(filename));
76 auto instrument = parser.parseXML(nullptr);
77 retVal->populateInstrumentParameters();
78 retVal->setInstrument(instrument);
79
80 DateAndTime run_start("2010-01-01T00:00:00");
81
82 // create spectra for monitors
83 retVal->getSpectrum(0).addDetectorID(-1);
84 retVal->getSpectrum(1).addDetectorID(-2);
85
86 for (int pix = 0; pix < numPixels; pix++) {
87 for (int i = 0; i < numEvents; i++) {
88 retVal->getSpectrum(pix + 2) += Types::Event::TofEvent((i + 0.5) * binDelta, run_start + double(i));
89 }
90 retVal->getSpectrum(pix + 2).addDetectorID(pix);
91 }
92
93 // Create the x-axis for histogramming.
94 HistogramData::BinEdges x1(numBins);
95 auto &xRef = x1.mutableData();
96 for (int i = 0; i < numBins; ++i) {
97 xRef[i] = i * binDelta;
98 }
99
100 // Set all the histograms at once.
101 retVal->setAllX(x1);
102 // Default unit: TOF.
103 retVal->getAxis(0)->setUnit("TOF");
104
105 // Give it a crystal and goniometer
106 WorkspaceCreationHelper::setGoniometer(retVal, 0., 0., 0.);
108
109 // Some sanity checks
110 if (retVal->getInstrument()->getName() != "MINITOPAZ")
111 throw std::runtime_error("MDEventsTestHelper::"
112 "createDiffractionEventWorkspace(): Wrong "
113 "instrument loaded.");
115 retVal->getInstrument()->getDetectors(dets);
116 if (dets.size() != 100 * 100 + 2)
117 throw std::runtime_error("MDEventsTestHelper::"
118 "createDiffractionEventWorkspace(): Wrong "
119 "instrument size.");
120
121 return retVal;
122}
123
124//=====================================================================================
138 // ---------- Make a file-backed MDEventWorkspace -----------------------
139 MDEventWorkspace3Lean::sptr ws1 = MDEventsTestHelper::makeMDEW<3>(10, 0.0, 10.0, 0);
140 ws1->setCoordinateSystem(coord);
141 ws1->getBoxController()->setSplitThreshold(100);
142 API::AnalysisDataService::Instance().addOrReplace(wsName,
143 std::dynamic_pointer_cast<Mantid::API::IMDEventWorkspace>(ws1));
144 FakeMD dataFaker(std::vector<double>(1, static_cast<double>(numEvents)), std::vector<double>(), std::vector<double>(),
145 0, true);
146 dataFaker.fill(ws1);
147 return std::dynamic_pointer_cast<MDEventWorkspace3Lean>(API::AnalysisDataService::Instance().retrieve(wsName));
148}
149
150//-------------------------------------------------------------------------------------
154MDBox<MDLeanEvent<1>, 1> *makeMDBox1(size_t splitInto, BoxController *splitter) {
155 if (!splitter) {
156 splitter = (new BoxController(1));
157 }
158 // Split at 5 events
159 splitter->setSplitThreshold(5);
160 // Splits into 10 boxes
161 splitter->setSplitInto(splitInto);
162 // Set the size
163 auto *out = new MDBox<MDLeanEvent<1>, 1>(splitter);
164 out->setExtents(0, 0.0, 10.0);
165 out->calcVolume();
166 return out;
167}
168
169//-------------------------------------------------------------------------------------
174 // Split at 5 events
175 auto splitter = new BoxController(3);
176 splitter->setSplitThreshold(5);
177 // Splits into 10x5x2 boxes
178 splitter->setSplitInto(10);
179 splitter->setSplitInto(1, 5);
180 splitter->setSplitInto(2, 2);
181 // Set the size to 10.0 in all directions
182 auto out = new MDBox<MDLeanEvent<3>, 3>(splitter);
183 for (size_t d = 0; d < 3; ++d) {
184 out->setExtents(d, 0.0, 10.0);
185 }
186 out->calcVolume();
187 return out;
188}
189
190//-------------------------------------------------------------------------------------
193std::vector<MDLeanEvent<1>> makeMDEvents1(size_t num) {
194 std::vector<MDLeanEvent<1>> out;
195 out.reserve(num);
196 for (std::size_t i = 0; i < num; ++i) {
197 float coords[1] = {static_cast<float>(i) + 0.5f};
198 out.emplace_back(1.0f, 1.0f, coords);
199 }
200 return out;
201}
202
203//-------------------------------------------------------------------------------------
216Mantid::DataObjects::MDHistoWorkspace_sptr makeFakeMDHistoWorkspace(double signal, size_t numDims, size_t numBins,
217 coord_t max, double errorSquared,
218 const std::string &name, double numEvents) {
219 // Create MDFrame of General Frame type
221 return makeFakeMDHistoWorkspaceWithMDFrame(signal, numDims, frame, numBins, max, errorSquared, name, numEvents);
222}
223
224//-------------------------------------------------------------------------------------
237MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(size_t numDims, double signal, double errorSquared,
238 size_t *numBins, coord_t *min, coord_t *max,
239 const std::string &name) {
240 std::vector<std::string> names{"x", "y", "z", "t"};
241 // Create MDFrame of General Frame type
243
244 std::vector<Mantid::Geometry::MDHistoDimension_sptr> dimensions;
245 for (size_t d = 0; d < numDims; d++)
246 dimensions.emplace_back(
247 MDHistoDimension_sptr(new MDHistoDimension(names[d], names[d], frame, min[d], max[d], numBins[d])));
248
249 MDHistoWorkspace_sptr ws_sptr = std::make_shared<MDHistoWorkspace>(dimensions);
250 ws_sptr->setTo(signal, errorSquared, 1.0 /* num events */);
251 if (!name.empty())
252 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
253 return ws_sptr;
254}
255
256//-------------------------------------------------------------------------------------
270MDHistoWorkspace_sptr makeFakeMDHistoWorkspaceGeneral(size_t numDims, double signal, double errorSquared,
271 size_t *numBins, coord_t *min, coord_t *max,
272 std::vector<std::string> names, const std::string &name) {
273 std::vector<Mantid::Geometry::MDHistoDimension_sptr> dimensions;
274 // Create MDFrame of General Frame type
276 for (size_t d = 0; d < numDims; d++)
277 dimensions.emplace_back(
278 MDHistoDimension_sptr(new MDHistoDimension(names[d], names[d], frame, min[d], max[d], numBins[d])));
279
280 MDHistoWorkspace_sptr ws_sptr = std::make_shared<MDHistoWorkspace>(dimensions);
281 ws_sptr->setTo(signal, errorSquared, 1.0 /* num events */);
282 if (!name.empty())
283 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
284 return ws_sptr;
285}
286
287//-------------------------------------------------------------------------------------
302makeFakeMDHistoWorkspaceWithMDFrame(double signal, size_t numDims, const Mantid::Geometry::MDFrame &frame,
303 size_t numBins, coord_t max, double errorSquared, const std::string &name,
304 double numEvents) {
305 // MDHistoWorkspace *ws = nullptr;
306 MDHistoWorkspace_sptr ws_sptr;
307 if (numDims == 1) {
308 ws_sptr =
309 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins));
310 } else if (numDims == 2) {
311 ws_sptr =
312 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
313 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins));
314 } else if (numDims == 3) {
315 ws_sptr =
316 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
317 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins),
318 std::make_shared<MDHistoDimension>("z", "z", frame, 0.0f, max, numBins));
319 } else if (numDims == 4) {
320 ws_sptr =
321 std::make_shared<MDHistoWorkspace>(std::make_shared<MDHistoDimension>("x", "x", frame, 0.0f, max, numBins),
322 std::make_shared<MDHistoDimension>("y", "y", frame, 0.0f, max, numBins),
323 std::make_shared<MDHistoDimension>("z", "z", frame, 0.0f, max, numBins),
324 std::make_shared<MDHistoDimension>("t", "t", frame, 0.0f, max, numBins));
325 }
326
327 if (!ws_sptr)
328 throw std::runtime_error(" invalid or unsupported number of dimensions given");
329
330 ws_sptr->setTo(signal, errorSquared, numEvents);
331 ws_sptr->addExperimentInfo(std::make_shared<ExperimentInfo>());
332 if (!name.empty())
333 AnalysisDataService::Instance().addOrReplace(name, ws_sptr);
334 return ws_sptr;
335}
336
341void checkAndDeleteFile(const std::string &filename) {
342 if (!filename.empty()) {
343 if (std::filesystem::exists(filename)) {
344 std::filesystem::remove(filename);
345 }
346 }
347}
348
349} // namespace MDEventsTestHelper
350} // namespace Mantid::DataObjects
std::string name
Definition Run.cpp:60
This class is used by MDBox and MDGridBox in order to intelligently determine optimal behavior.
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:24
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.
static const std::string GeneralFrameDistance
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
std::size_t numEvents(Nexus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix)
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.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
Definition Strings.cpp:26
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.