46template <
typename T> T interpretAs(std::vector<char> &Buf,
size_t ind = 0) {
47 return *((
reinterpret_cast<T *
>(&Buf[ind])));
55 : m_fileName(""), m_fileStream(), m_prog(new
Mantid::
API::
Progress(this, 0.05, 0.95, 100)), m_outputFile(""),
56 m_dataPositions(), m_boxSizes(), m_nDataPoints(0), m_mdImageSize(0), m_nDims(0), m_nBins() {}
67 const std::string &extn = descriptor.
extension();
84 std::vector<std::string> fileExtensions{
".sqw"};
86 "File of type SQW format");
89 "Output IMDEventWorkspace reflecting SQW data read-in.");
91 "Load Metadata without events.");
92 std::vector<std::string> fileExtensions2{
".nxs"};
95 "If the input SQW file is too large to fit in memory, specify an output "
97 "The MDEventWorkspace will be create with this file as its back-end.");
112 std::vector<Mantid::Geometry::MDHistoDimensionBuilder> DimVector;
119 for (
size_t i = 0; i < 4; i++) {
120 bc->setSplitInto(i,
m_nBins[i]);
143 <<
" points into memory; this would be "
144 "faster than using a file back-end.\n";
147 saver->setProperty(
"InputWorkspace", ws);
150 saver->setProperty(
"UpdateFileBackEnd",
false);
151 saver->setProperty(
"MakeFileBacked",
false);
152 saver->executeAsChildAlg();
158 pWs->getBox()->setFileBacked();
159 bc->getFileIO()->setWriteBufferSize(1000000);
165 <<
" points into memory. You can cancel and specify "
166 "OutputFilename to load to a file back-end.\n";
169 if (bc->isFileBacked()) {
170 std::cout <<
"File backed? " << bc->isFileBacked() <<
". Cache " << bc->getFileIO()->getMemoryStr() <<
'\n';
173 std::cout <<
"File backed? " << ff <<
". Cache 0\n";
179 throw std::runtime_error(
"Cannot currently handle overwriting of an existing workspace.");
191 g_log.
notice() <<
"Starting SaveMD to update the file back-end.\n";
193 saver->setProperty(
"InputWorkspace", ws);
194 saver->setProperty(
"UpdateFileBackEnd",
true);
195 saver->executeAsChildAlg();
203 size_t maxNPix = ~size_t(0);
205 throw std::runtime_error(
"Not possible to address all datapoints in "
206 "memory using this architecture ");
209 const size_t ncolumns = 9;
210 const size_t column_size = 4;
211 const size_t column_size_2 = column_size * 2;
212 const size_t column_size_3 = column_size * 3;
213 const size_t column_size_4 = column_size * 4;
215 const size_t column_size_6 = column_size * 6;
217 uint16_t goniometerIndex(0);
218 const size_t column_size_7 = column_size * 7;
219 const size_t column_size_8 = column_size * 8;
221 const size_t pixel_width = ncolumns * column_size;
226 size_t blockSize = pixel_width * 1000000;
229 auto numBlocks = int((data_buffer_size + blockSize - 1) / blockSize);
234 size_t eventsAdded = 0;
237 if (bc->isFileBacked())
238 dbuf = bc->getFileIO();
240 for (
int blockNum = 0; blockNum < numBlocks; blockNum++) {
242 size_t inputFileOffset = size_t(blockNum) * blockSize;
245 size_t currentBlockSize = blockSize;
246 if ((inputFileOffset + currentBlockSize) > data_buffer_size)
247 currentBlockSize = data_buffer_size - inputFileOffset;
250 std::vector<char> Buffer = std::vector<char>(currentBlockSize);
255 auto currentNumPixels = int(currentBlockSize / pixel_width);
256 eventsAdded += size_t(currentNumPixels);
260 for (
int i = 0; i < currentNumPixels; i++) {
261 auto current_pix = size_t(i * pixel_width);
262 coord_t centers[4] = {interpretAs<float>(Buffer, current_pix),
263 interpretAs<float>(Buffer, current_pix + column_size),
264 interpretAs<float>(Buffer, current_pix + column_size_2),
265 interpretAs<float>(Buffer, current_pix + column_size_3)};
266 const auto errorSQ = interpretAs<float>(Buffer, current_pix + column_size_8);
268 interpretAs<float>(Buffer, current_pix + column_size_7),
270 static_cast<uint16_t
>(interpretAs<float>(Buffer, current_pix + column_size_6)),
271 goniometerIndex,
static_cast<int32_t
>(interpretAs<float>(Buffer, current_pix + column_size_4)),
292 if (eventsAdded > 19000000) {
293 g_log.
information() <<
"Splitting boxes after " << eventsAdded <<
" events added.\n";
298 ws->splitAllIfNeeded(ts);
348 ws->splitAllIfNeeded(ts);
362 std::vector<char> buf(4 * (3 + 3));
366 auto a =
static_cast<double>(interpretAs<float>(buf, 0));
367 auto b =
static_cast<double>(interpretAs<float>(buf, 4));
368 auto c =
static_cast<double>(interpretAs<float>(buf, 8));
369 auto aa =
static_cast<double>(interpretAs<float>(buf, 12));
370 auto bb =
static_cast<double>(interpretAs<float>(buf, 16));
371 auto cc =
static_cast<double>(interpretAs<float>(buf, 20));
377 info->mutableRun().mutableGoniometer().makeUniversalGoniometer();
378 info->mutableSample().setOrientedLattice(std::make_unique<OrientedLattice>(a, b, c, aa, bb, cc));
379 ws->addExperimentInfo(info);
383 std::vector<std::string> dimID(4,
"qx");
384 std::vector<std::string> dimUnit(4,
"A^-1");
385 std::vector<std::string> dimFrameName(4,
"HKL");
390 dimFrameName[3] =
"meV";
392 for (
size_t i = 0; i < 4; i++) {
393 DimVector[i].setId(dimID[i]);
394 DimVector[i].setUnits(dimUnit[i]);
395 DimVector[i].setName(dimID[i]);
396 DimVector[i].setFrameName(dimFrameName[i]);
402 bool arrangeByMDImage) {
404 std::vector<Mantid::Geometry::MDHistoDimensionBuilder> DimVectorIn;
407 std::vector<char> buf(4 * (3 + 3 + 4 + 16 + 4 + 2));
413 size_t i0 = 4 * (3 + 3);
425 for (
size_t i = 0; i < 4; i++) {
426 for (
size_t j = 0; j < 4; j++) {
434 auto nRows = interpretAs<uint32_t>(buf, i0);
435 auto nCols = interpretAs<uint32_t>(buf, i0 + 4);
438 buf.resize(nRows * nCols);
448 for (
unsigned int i = 0; i < nRows; i++) {
449 for (
unsigned int j = 0; j < nCols; j++) {
450 symb = buf[i + j * nRows];
454 std::string sName(
name);
455 boost::erase_all(sName,
" ");
457 DimVectorIn[i].setName(sName);
461 buf.resize(4 * 4 * 3);
464 auto npax = interpretAs<uint32_t>(buf);
465 unsigned int niax = 4 - npax;
484 std::vector<unsigned int> iax;
486 buf.resize(4 * (niax + 2 * niax));
490 for (
unsigned int i = 0; i < niax; i++) {
491 iax[i] = interpretAs<uint32_t>(buf, i * 4) - 1;
492 auto min = interpretAs<float>(buf, 4 * (niax + i * 2));
493 float max = interpretAs<float>(buf, 4 * (niax + i * 2 + 1)) * (1 + FLT_EPSILON);
495 DimVectorIn[ic].setNumBins(1);
496 DimVectorIn[ic].setMax(max);
497 DimVectorIn[ic].setMin(min);
528 std::vector<unsigned int> pax, dax;
535 for (
unsigned int i = 0; i < npax; i++) {
537 pax[i] = interpretAs<uint32_t>(buf, 4 * i) - 1;
539 std::vector<char> axis_buffer(101 * 4);
541 auto nAxisPoints = interpretAs<uint32_t>(axis_buffer);
542 if (axis_buffer.size() < nAxisPoints * 4)
543 axis_buffer.resize(nAxisPoints * 4);
545 this->
m_fileStream.read(&axis_buffer[0], 4 * nAxisPoints);
547 auto min = interpretAs<float>(axis_buffer, 0);
548 float max = interpretAs<float>(axis_buffer, 4 * (nAxisPoints - 1)) * (1 + FLT_EPSILON);
550 DimVectorIn[ic].setNumBins(nAxisPoints - 1);
551 DimVectorIn[ic].setMax(max);
552 DimVectorIn[ic].setMin(min);
558 for (
unsigned int i = 0; i < npax; i++)
559 dax[i] = interpretAs<uint32_t>(buf, 4 * i) - 1;
561 if (arrangeByMDImage) {
565 DimVectorOut.resize(4);
566 for (
size_t i = 0; i < npax; i++) {
567 DimVectorOut[dimIndex] = DimVectorIn[pax[dax[i]]];
571 for (
size_t i = 0; i < niax; i++) {
572 DimVectorOut[dimIndex] = DimVectorIn[iax[i]];
577 DimVectorOut.assign(DimVectorIn.begin(), DimVectorIn.end());
582 for (
size_t i = 0; i < 4; i++) {
583 m_nBins[i] = DimVectorOut[i].getNumBins();
590 std::vector<Mantid::Geometry::MDHistoDimensionBuilder> &DimVector) {
592 for (
size_t i = 0; i < 4; i++) {
593 ws->addDimension(DimVector[i].
create());
603 std::vector<char> buf(4 * (4 + 4));
608 for (
unsigned int i = 0; i < 4; i++) {
609 auto min = interpretAs<float>(buf, 4 * i * 2);
610 float max = interpretAs<float>(buf, 4 * (i * 2 + 1)) * (1 + FLT_EPSILON);
611 DimVectorOut[i].setNumBins(
m_nBins[i]);
612 DimVectorOut[i].setMax(max);
613 DimVectorOut[i].setMin(min);
634 std::vector<char> data_buffer;
636 data_buffer.resize(3 * 4);
639 this->
m_nDims = interpretAs<uint32_t>(data_buffer);
646 for (
size_t i = 0; i < nFiles; i++) {
664namespace LoadSQWHelper {
673 std::vector<char> data_buffer(4 * 3);
674 dataStream.read(&data_buffer[0], 4);
676 unsigned int file_name_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
678 dataStream.seekg(file_name_length, std::ios_base::cur);
680 dataStream.read(&data_buffer[0], 4);
681 unsigned int file_path_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
684 dataStream.seekg(file_path_length, std::ios_base::cur);
686 dataStream.read(&data_buffer[0], 4);
687 unsigned int file_title = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
690 dataStream.seekg(file_title, std::ios_base::cur);
693 dataStream.read(&data_buffer[0], 4);
694 unsigned int nFiles = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
699 std::streamoff last_location = dataStream.tellg();
700 if (last_location < 0)
701 throw(
"IO error for input file at start of component headers; Can not "
702 "seek to last location");
716 std::streamoff start_location) {
719 std::vector<char> data_buffer(8);
721 std::streamoff shift = start_location - dataStream.tellg();
723 dataStream.seekg(shift, std::ios_base::cur);
725 dataStream.read(&data_buffer[0], 4);
727 unsigned int file_name_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
729 dataStream.seekg(file_name_length, std::ios_base::cur);
731 dataStream.read(&data_buffer[0], 4);
732 unsigned int file_path_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
735 dataStream.seekg(file_path_length, std::ios_base::cur);
738 dataStream.seekg(4 * (7 + 3 * 4), std::ios_base::cur);
741 dataStream.read(&data_buffer[0], 4);
742 unsigned int nEn_bins = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
744 dataStream.seekg(4 * (nEn_bins), std::ios_base::cur);
747 dataStream.seekg(4 * (4 + 4 * 4 + 4), std::ios_base::cur);
750 dataStream.read(&data_buffer[0], 8);
752 unsigned int nRows = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
753 unsigned int nCols = *(
reinterpret_cast<uint32_t *
>(&data_buffer[4]));
756 dataStream.seekg(nRows * nCols, std::ios_base::cur);
758 std::streamoff end_location = dataStream.tellg();
769 std::streamoff start_location) {
770 std::vector<char> data_buffer(8);
772 std::streamoff shift = start_location - dataStream.tellg();
774 dataStream.seekg(shift, std::ios_base::cur);
776 dataStream.read(&data_buffer[0], 4);
777 unsigned int file_name_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
779 dataStream.seekg(file_name_length, std::ios_base::cur);
781 dataStream.read(&data_buffer[0], 4);
782 unsigned int file_path_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
784 dataStream.seekg(file_path_length, std::ios_base::cur);
786 dataStream.read(&data_buffer[0], 4);
787 unsigned int num_detectors = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
789 dataStream.seekg(num_detectors * 6 * 4, std::ios_base::cur);
791 std::streamoff end_location = dataStream.tellg();
801 std::vector<size_t> &nBins, uint64_t &nDataPoints) {
802 std::vector<char> data_buffer(12);
804 std::streamoff shift =
data_start - dataStream.tellg();
806 dataStream.seekg(shift, std::ios_base::cur);
808 dataStream.read(&data_buffer[0], 4);
810 unsigned int file_name_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
812 dataStream.seekg(file_name_length, std::ios_base::cur);
814 dataStream.read(&data_buffer[0], 4);
815 unsigned int file_path_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
818 dataStream.seekg(file_path_length, std::ios_base::cur);
820 dataStream.read(&data_buffer[0], 4);
821 unsigned int data_title_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
824 dataStream.seekg(data_title_length, std::ios_base::cur);
828 dataStream.seekg(4 * (3 + 3 + 4 + 16 + 4), std::ios_base::cur);
831 dataStream.read(&data_buffer[0], 8);
833 unsigned int n_labels = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
834 unsigned int labels_length = *(
reinterpret_cast<uint32_t *
>(&data_buffer[4]));
835 dataStream.seekg(n_labels * labels_length, std::ios_base::cur);
839 dataStream.read(&data_buffer[0], 4);
841 unsigned int npax = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
842 unsigned int niax = 4 - npax;
844 dataStream.seekg(3 * niax * 4, std::ios_base::cur);
850 dataStream.seekg(npax * 4, std::ios_base::cur);
853 for (
unsigned int i = 0; i < npax; i++) {
854 dataStream.read(&data_buffer[0], 4);
856 unsigned int nAxisPoints = *(
reinterpret_cast<uint32_t *
>(&data_buffer[0]));
857 nBins[i] = nAxisPoints - 1;
859 dataStream.seekg(nAxisPoints * 4, std::ios_base::cur);
862 dataStream.seekg(npax * 4, std::ios_base::cur);
865 this->
s_start = dataStream.tellg();
867 dataStream.seekg(
mdImageSize * 4, std::ios_base::cur);
871 dataStream.seekg(
mdImageSize * 4, std::ios_base::cur);
874 if (dataStream.eof()) {
883 dataStream.seekg(
mdImageSize * 8, std::ios_base::cur);
885 if (dataStream.eof()) {
895 dataStream.seekg(8 * 4, std::ios_base::cur);
897 if (dataStream.eof()) {
904 dataStream.read(&data_buffer[0], 12);
906 nDataPoints =
static_cast<size_t>(*(
reinterpret_cast<uint64_t *
>(&data_buffer[4])));
static std::unique_ptr< QThreadPool > tp
#define PARALLEL_FOR_NO_WSP_CHECK()
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
This class is shared by a few Workspace types and holds information related to a particular experimen...
@ OptionalSave
to specify a file to write to but an empty string is
@ Load
allowed here which will be passed to the algorithm
Helper class for reporting progress from algorithms.
A property class for workspaces.
The class responsible for saving events into nexus file using generic box controller interface Expect...
Templated class for the multi-dimensional event workspace.
Templated class holding data about a neutron detection event in N-dimensions (for example,...
MDHistoDimensionBuilder :
Class to implement UB matrix.
CPUTimer : Timer that uses the CPU time, rather than wall-clock time to measure execution time.
Buffer objects that need to be written out to disk so as to optimize writing operations.
void flushCache()
Flush out all the data in the memory; and writes out everything in the to-write cache.
Records the filename and the description of failure.
Defines a wrapper around an open file.
static bool isAscii(const std::string &filename, const size_t nbytes=256)
Returns true if the file is considered ascii.
const std::string & extension() const
Access the file extension.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
This class is responsible for memory statistics.
std::size_t availMem() const
Returns the available memory of the system in kiB.
void resetNumSteps(int64_t nsteps, double start, double end)
Change the number of steps between start/end.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
void setNumSteps(int64_t nsteps)
Change the number of steps between start/end.
void setNotifyStep(double notifyStepPct)
Override the frequency at which notifications are sent out.
The concrete, templated class for properties.
A Thread Pool implementation that keeps a certain number of threads running (normally,...
A First-In-First-Out Thread Scheduler.
The ThreadScheduler object defines how tasks are allocated to threads and in what order.
void parseMetadata(const std::string &fileName)
Parse metadata from file.
std::ifstream m_fileStream
File stream containing binary file data.
std::string m_outputFile
OutputFilename param.
~LoadSQW() override
Destructor.
void readDNDDimensions(std::vector< Mantid::Geometry::MDHistoDimensionBuilder > &DimVectorOut, bool arrangeByMDImage=true)
Read DND dimensions.
LoadSQWHelper::dataPositions m_dataPositions
Instance of helper type, which describes the positions of the data within binary Horace file.
void addDimsToWs(Mantid::DataObjects::MDEventWorkspace< DataObjects::MDEvent< 4 >, 4 > *ws, std::vector< Mantid::Geometry::MDHistoDimensionBuilder > &DimVector)
add range of dimensions to the workspace;
void readSQWDimensions(std::vector< Mantid::Geometry::MDHistoDimensionBuilder > &DimVectorOut)
Read SQW dimensions.
std::vector< uint64_t > m_boxSizes
std::string m_fileName
the name of the file to work with
void init() override
Provide wiki documentation.
std::vector< size_t > m_nBins
number of bins in every non-integrated dimension
const std::string name() const override
Algorithm's name for identification.
virtual void readEvents(Mantid::DataObjects::MDEventWorkspace< DataObjects::MDEvent< 4 >, 4 > *ws)
Read events onto the workspace.
Mantid::API::Progress * m_prog
Progress bar.
void readBoxSizes()
read real box sizes for all Horace Boxes;
void exec() override
Execute the algorithm.
int confidence(Kernel::FileDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
virtual void addLattice(Mantid::DataObjects::MDEventWorkspace< DataObjects::MDEvent< 4 >, 4 > *ws)
Extract lattice information.
void buildMDDimsBase(std::vector< Mantid::Geometry::MDHistoDimensionBuilder > &DimVector)
build an initial range of 4 dimensions
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< ExperimentInfo > ExperimentInfo_sptr
Shared pointer to ExperimentInfo.
std::shared_ptr< BoxController > BoxController_sptr
Shared ptr to BoxController.
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
Helper class which provides the Collimation Length for SANS instruments.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
@ Output
An output workspace.
std::vector< std::streamoff > component_headers_starts
std::streamoff min_max_start
std::streamoff data_start
std::streamoff npax_start
std::streamoff detectors_start
std::streamoff if_sqw_start
std::streamoff n_cell_pix_start
std::streamoff parse_sqw_detpar(std::ifstream &dataStream, std::streamoff start_location)
Block 3: Detpar: parse positions of the contributed detectors.
std::streamoff parse_component_header(std::ifstream &dataStream, std::streamoff start_location)
Block 2: Header: Parse header of single SPE file.
std::streamoff geom_start
void parse_sqw_main_header(std::ifstream &dataStream)
Block 1: Main_header: Parse SQW main data header.
void parse_data_locations(std::ifstream &dataStream, std::streamoff data_start, std::vector< size_t > &nBins, uint64_t &nDataPoints)
Block 4: Data: parse positions of the data fields.