17#include <boost/algorithm/string.hpp>
18#include <boost/algorithm/string/split.hpp>
28template <
typename T> T
convert(
const std::string &str) {
29 std::istringstream iss(str);
31 iss >> std::ws >>
obj >> std::ws;
33 throw std::invalid_argument(
"Wrong type destination. Cannot convert " + str);
42using namespace DataObjects;
43using namespace Geometry;
44using namespace Kernel;
89 std::vector<std::string> fileExtensions{
".txt"};
93 "An output workspace.");
101template <
typename MDE,
size_t nd>
106 std::vector<Mantid::coord_t> centers(nd);
108 auto signal = convert<float>(*(++mdEventEntriesIterator));
109 auto error = convert<float>(*(++mdEventEntriesIterator));
111 int32_t detector_no = 0;
113 run_no = convert<uint16_t>(*(++mdEventEntriesIterator));
114 detector_no = convert<int32_t>(*(++mdEventEntriesIterator));
117 centers[j] = convert<Mantid::coord_t>(*(++mdEventEntriesIterator));
139 throw std::invalid_argument(message);
143 throw std::invalid_argument(message);
148 auto posDiffDims =
static_cast<int>(std::distance(posDimStart, posMDEventStart));
149 if (posDiffDims < 1) {
151 throw std::invalid_argument(message);
154 if ((posDiffDims - 1) % 4 != 0) {
155 throw std::invalid_argument(
"Dimensions in the file should be specified id, name, units, nbins");
157 const size_t nDimensions = (posDiffDims - 1) / 4;
159 auto dimEntriesIterator = posDimStart;
160 for (
size_t i = 0; i < nDimensions; ++i) {
161 convert<std::string>(*(++dimEntriesIterator));
162 convert<std::string>(*(++dimEntriesIterator));
163 convert<std::string>(*(++dimEntriesIterator));
164 convert<int>(*(++dimEntriesIterator));
167 int posDiffMDEvent =
static_cast<int>(std::distance(posMDEventStart,
m_file_data.end()));
168 const size_t columnsForFullEvents = nDimensions + 4;
169 const size_t columnsForLeanEvents = nDimensions + 2;
170 if ((posDiffMDEvent - 1) % columnsForFullEvents != 0) {
171 if ((posDiffMDEvent - 1) % columnsForLeanEvents != 0) {
172 std::stringstream stream;
173 stream <<
"With the dimenionality found to be " << nDimensions <<
". Should either have " << columnsForLeanEvents
174 <<
" or " << columnsForFullEvents <<
" in each row";
175 throw std::invalid_argument(stream.str());
188 file.open(filename.c_str(), std::ios::in);
189 }
catch (std::ifstream::failure &e) {
190 g_log.
error() <<
"Cannot open file: " << filename;
196 std::string lastLine;
197 size_t nActualColumns = 0;
198 while (std::getline(file, line)) {
199 boost::algorithm::trim(line);
201 std::stringstream buffer(line);
202 std::copy(std::istream_iterator<std::string>(buffer), std::istream_iterator<std::string>(),
206 std::vector<std::string> strVec;
207 boost::algorithm::split(strVec, line, boost::is_any_of(
"\t "), boost::token_compress_on);
208 nActualColumns = strVec.size();
232 if (0 == nActualColumns) {
234 g_log.
warning() <<
"The number of actual columns found in the file "
235 "(exlcuding comments) is zero\n";
245 mdEventEntriesIterator += 2;
247 mdEventEntriesIterator += 2;
250 auto coord = convert<double>(*(++mdEventEntriesIterator));
251 extentMins[j] = coord < extentMins[j] ? coord : extentMins[j];
252 extentMaxs[j] = coord > extentMaxs[j] ? coord : extentMaxs[j];
264 std::string
id = convert<std::string>(*(++dimEntriesIterator));
265 std::string
name = convert<std::string>(*(++dimEntriesIterator));
266 std::string units = convert<std::string>(*(++dimEntriesIterator));
267 auto nbins = convert<int>(*(++dimEntriesIterator));
269 auto mdUnit = unitFactory->create(units);
272 static_cast<coord_t>(extentMaxs[i]), nbins)));
#define DECLARE_ALGORITHM(classname)
#define CALL_MDEVENT_FUNCTION(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
double obj
the value of the quadratic function
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
@ Load
allowed here which will be passed to the algorithm
A property class for workspaces.
static API::IMDEventWorkspace_sptr CreateMDWorkspace(size_t nd, const std::string &eventType="MDLeanEvent", const Mantid::API::MDNormalization &preferredNormalization=Mantid::API::MDNormalization::VolumeNormalization, const Mantid::API::MDNormalization &preferredNormalizationHisto=Mantid::API::MDNormalization::VolumeNormalization)
Create a MDEventWorkspace of the given type.
MDEventInserter : Helper class that provides a generic interface for adding events to an MDWorkspace ...
void insertMDEvent(float signal, float errorSQ, uint16_t expInfoIndex, uint16_t goniometerIndex, int32_t detectno, Mantid::coord_t *coords)
Creates an mdevent and adds it to the MDEW.
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 GeneralFrameName
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.
void warning(const std::string &msg)
Logs at warning level.
ImportMDEventWorkspace : Loads a file containing dimensionality and data for an MDEventWorkspace.
const std::string category() const override
Algorithm's category for identification.
const std::string name() const override
Algorithm's name for identification.
void exec() override
Execute the algorithm.
void addEventsData(typename DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
call back to add event data
DataCollectionType::iterator m_posDimStart
Iterator for the dimensionality start position.
static const std::string MDEventBlockFlag()
Flag used to indicate the mdevent block in the file.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
DataCollectionType::iterator m_posMDEventStart
Iterator for the mdevent data start position.
void quickFileCheck()
Quick check of the structure, so we can abort if passed junk.
static const std::string DimensionBlockFlag()
Flag used to indicate the dimension block in the file.
bool m_IsFullDataObjects
Flag indicating whether full md events for lean events will be generated.
bool fileDoesContain(const std::string &flag)
Check that the a flag exists in the file.
static const std::string CommentLineStartFlag()
Flag used to indicate a comment line.
DataCollectionType m_file_data
All read-in data.
size_t m_nDimensions
Actual number of dimensions specified.
size_t m_nDataObjects
Actual number of md events provided.
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< MDHistoDimension > MDHistoDimension_sptr
Shared pointer to a MDHistoDimension.
int convert(const std::string &A, T &out)
Convert a string into a number.
MDUnitFactory_uptr MANTID_KERNEL_DLL makeMDUnitFactoryChain()
Convience method. Pre-constructed builder chain.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
@ Output
An output workspace.