18#include <boost/algorithm/string.hpp>
19#include <boost/algorithm/string/split.hpp>
29template <
typename T> T
convert(
const std::string &str) {
30 std::istringstream iss(str);
32 iss >> std::ws >>
obj >> std::ws;
34 throw std::invalid_argument(
"Wrong type destination. Cannot convert " + str);
43using namespace DataObjects;
44using namespace Geometry;
45using namespace Kernel;
90 std::vector<std::string> fileExtensions{
".txt"};
94 "An output workspace.");
102template <
typename MDE,
size_t nd>
107 std::vector<Mantid::coord_t> centers(nd);
109 auto signal = convert<float>(*(++mdEventEntriesIterator));
110 auto error = convert<float>(*(++mdEventEntriesIterator));
112 int32_t detector_no = 0;
114 run_no = convert<uint16_t>(*(++mdEventEntriesIterator));
115 detector_no = convert<int32_t>(*(++mdEventEntriesIterator));
118 centers[j] = convert<Mantid::coord_t>(*(++mdEventEntriesIterator));
140 throw std::invalid_argument(message);
144 throw std::invalid_argument(message);
149 auto posDiffDims =
static_cast<int>(std::distance(posDimStart, posMDEventStart));
150 if (posDiffDims < 1) {
152 throw std::invalid_argument(message);
155 if ((posDiffDims - 1) % 4 != 0) {
156 throw std::invalid_argument(
"Dimensions in the file should be specified id, name, units, nbins");
158 const size_t nDimensions = (posDiffDims - 1) / 4;
160 auto dimEntriesIterator = posDimStart;
161 for (
size_t i = 0; i < nDimensions; ++i) {
162 convert<std::string>(*(++dimEntriesIterator));
163 convert<std::string>(*(++dimEntriesIterator));
164 convert<std::string>(*(++dimEntriesIterator));
165 convert<int>(*(++dimEntriesIterator));
168 int posDiffMDEvent =
static_cast<int>(std::distance(posMDEventStart,
m_file_data.end()));
169 const size_t columnsForFullEvents = nDimensions + 4;
170 const size_t columnsForLeanEvents = nDimensions + 2;
171 if ((posDiffMDEvent - 1) % columnsForFullEvents != 0) {
172 if ((posDiffMDEvent - 1) % columnsForLeanEvents != 0) {
173 std::stringstream stream;
174 stream <<
"With the dimenionality found to be " << nDimensions <<
". Should either have " << columnsForLeanEvents
175 <<
" or " << columnsForFullEvents <<
" in each row";
176 throw std::invalid_argument(stream.str());
189 file.open(filename.c_str(), std::ios::in);
190 }
catch (std::ifstream::failure &e) {
191 g_log.
error() <<
"Cannot open file: " << filename;
197 std::string lastLine;
198 size_t nActualColumns = 0;
199 while (std::getline(file, line)) {
200 boost::algorithm::trim(line);
202 std::stringstream buffer(line);
203 std::copy(std::istream_iterator<std::string>(buffer), std::istream_iterator<std::string>(),
207 std::vector<std::string> strVec;
208 boost::algorithm::split(strVec, line, boost::is_any_of(
"\t "), boost::token_compress_on);
209 nActualColumns = strVec.size();
233 if (0 == nActualColumns) {
235 g_log.
warning() <<
"The number of actual columns found in the file "
236 "(exlcuding comments) is zero\n";
246 mdEventEntriesIterator += 2;
248 mdEventEntriesIterator += 2;
251 auto coord = convert<double>(*(++mdEventEntriesIterator));
252 extentMins[j] = coord < extentMins[j] ? coord : extentMins[j];
253 extentMaxs[j] = coord > extentMaxs[j] ? coord : extentMaxs[j];
265 std::string
id = convert<std::string>(*(++dimEntriesIterator));
266 std::string
name = convert<std::string>(*(++dimEntriesIterator));
267 std::string units = convert<std::string>(*(++dimEntriesIterator));
268 auto nbins = convert<int>(*(++dimEntriesIterator));
270 auto mdUnit = unitFactory->create(units);
273 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.