Mantid
Loading...
Searching...
No Matches
ImportMDHistoWorkspace.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 +
9
10#include <deque>
11#include <fstream>
12#include <iterator>
13
14namespace Mantid::MDAlgorithms {
15
16using namespace API;
17using namespace DataObjects;
18using namespace Kernel;
19using namespace Geometry;
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(ImportMDHistoWorkspace)
23
24//----------------------------------------------------------------------------------------------
26const std::string ImportMDHistoWorkspace::name() const { return "ImportMDHistoWorkspace"; }
27
29int ImportMDHistoWorkspace::version() const { return 1; }
30
32const std::string ImportMDHistoWorkspace::category() const { return "MDAlgorithms\\DataHandling"; }
33
34//----------------------------------------------------------------------------------------------
35
36//----------------------------------------------------------------------------------------------
40 std::vector<std::string> fileExtensions{".txt"};
41 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load, fileExtensions),
42 "File of type txt");
43
44 // Initialize generic dimension properties on the base class.
46}
47
48//----------------------------------------------------------------------------------------------
52
53 std::string filename = getProperty("Filename");
54
55 /*
56 Base class creates an empty output workspace, with the correct dimensionality
57 according to the algorithm inputs (see base class).
58 */
60
61 // Open the file
62 std::ifstream file;
63 try {
64 file.open(filename.c_str(), std::ios::in);
65 } catch (std::ifstream::failure &e) {
66 g_log.error() << "Cannot open file: " << filename;
67 throw(e);
68 }
69
70 // Copy each string present in the file stream into a deque.
71 using box_collection = std::deque<std::string>;
72 box_collection box_elements;
73 std::copy(std::istream_iterator<std::string>(file), std::istream_iterator<std::string>(),
74 std::back_inserter(box_elements));
75
77 file.close();
78
79 const size_t nElements = this->getBinProduct() * 2;
80
81 // Handle the case that the number of elements is wrong.
82 if (box_elements.size() != nElements) {
83 throw std::invalid_argument("The number of data entries in the file, does "
84 "not match up with the specified "
85 "dimensionality.");
86 }
87
88 // Fetch out raw pointers to workspace arrays.
89 Mantid::signal_t *signals = ws->mutableSignalArray();
90 double *errors = ws->mutableErrorSquaredArray();
91
92 // Write to the signal and error array from the deque.
93 size_t currentBox = 0;
94 for (auto it = box_elements.begin(); it != box_elements.end(); it += 2) {
95 auto temp = it;
96 double signal = std::stod(*(temp));
97 double error = std::stod(*(++temp));
98 signals[currentBox] = signal;
99 errors[currentBox] = error * error;
100 ++currentBox;
101 }
102
103 // Set the output.
104 setProperty("OutputWorkspace", ws);
105}
106
107} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double error
Definition: IndexPeaks.cpp:133
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void initGenericImportProps()
Initialise the properties associated with the generic import (those to do with dimensionality).
DataObjects::MDHistoWorkspace_sptr createEmptyOutputWorkspace()
Creates an empty md histo workspace (with dimensions)
size_t getBinProduct() const
Getter for the number of bins (product accross all dimensions)
ImportMDHistoWorkspace : Takes a text file containing structured signal and error information and imp...
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void exec() override
Execute the algorithm.
const std::string category() const override
Algorithm's category for identification.
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36
STL namespace.