Mantid
Loading...
Searching...
No Matches
BoxControllerDummyIO.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 * API (e.g. Kernel, Geometry).
13 * Conversely, this file MAY NOT be modified to use anything from a package
14 *higher
15 * than API (e.g. any algorithm or concrete workspace), even if via the
16 *factory.
17 *********************************************************************************/
20
21#include <string>
22
23namespace MantidTestHelpers {
29 : m_bc(bc), m_CoordSize(4), m_TypeName("MDEvent"), m_ReadOnly(true), m_isOpened(false) {
30 m_EventSize = static_cast<unsigned int>(bc->getNDims() + 4);
31}
32
47void BoxControllerDummyIO::setDataType(const size_t blockSize, const std::string &typeName) {
48 if (blockSize == 4 || blockSize == 8) {
49 m_CoordSize = static_cast<unsigned int>(blockSize);
50 } else
51 throw std::invalid_argument("The class currently supports 4(float) and "
52 "8(double) event coordinates only");
53 m_TypeName = typeName;
54 if (m_TypeName == "MDEvent") {
55 m_EventSize = static_cast<unsigned int>(m_bc->getNDims() + 4);
56 } else if (m_TypeName == "MDLeanEvent") {
57 m_EventSize = static_cast<unsigned int>(m_bc->getNDims() + 2);
58 } else {
59 throw std::invalid_argument("unsupported event type");
60 }
61}
62
74void BoxControllerDummyIO::getDataType(size_t &CoordSize, std::string &typeName) const {
75 CoordSize = m_CoordSize;
76 typeName = m_TypeName;
77}
78
87bool BoxControllerDummyIO::openFile(const std::string &fileName, const std::string &mode) {
88 m_fileName = fileName;
89 // file already opened
90 if (m_isOpened)
91 return false;
92
93 m_ReadOnly = true;
94 ;
95 if (mode.find('w') != std::string::npos || mode.find('W') != std::string::npos) {
96 m_ReadOnly = false;
97 }
98
99 // open file if it exists or crate it if not in the mode requested
100 // bool fileExists(true);
101 if (fileName.find("exist") != std::string::npos) {
102 size_t nEvents = 1000;
103 fileContents.assign(nEvents * m_EventSize, 0);
104 this->setFileLength(nEvents);
105 size_t ic(0);
106 for (size_t i = 0; i < nEvents; i++) {
107 fileContents[ic++] = static_cast<float>(i);
108 fileContents[ic++] = static_cast<float>(i * i);
109 for (size_t j = 2; j < m_EventSize; j++)
110 fileContents[ic++] = static_cast<float>(i + 10 * j);
111 }
112
113 } else
114 this->setFileLength(0);
115
116 m_isOpened = true;
117
118 return true;
119}
125void BoxControllerDummyIO::saveBlock(const std::vector<float> &DataBlock, const uint64_t blockPosition) const {
126 size_t nEvents = DataBlock.size() / m_EventSize;
127 uint64_t position = blockPosition;
128 // uint64_t fileLength = this->getFileLength();
129 std::lock_guard<std::mutex> lock(m_fileMutex);
130
131 if (m_EventSize * (position + nEvents) > fileContents.size()) {
132 fileContents.resize((position + nEvents) * m_EventSize);
133 this->setFileLength(position + nEvents);
134 }
135
136 for (size_t i = 0; i < DataBlock.size(); i++) {
137 fileContents[blockPosition * m_EventSize + i] = DataBlock[i];
138 }
139}
151void BoxControllerDummyIO::loadBlock(std::vector<float> &Block, const uint64_t blockPosition,
152 const size_t nPoints) const {
153 std::lock_guard<std::mutex> _lock(m_fileMutex);
154 if (blockPosition + nPoints > this->getFileLength())
155 throw Mantid::Kernel::Exception::FileError("Attemtp to read behind the file end", m_fileName);
156
157 Block.resize(nPoints * m_EventSize);
158 for (size_t i = 0; i < nPoints * m_EventSize; i++) {
159 Block[i] = fileContents[blockPosition * m_EventSize + i];
160 }
161}
162
164} // namespace MantidTestHelpers
double position
Definition: GetAllEi.cpp:154
bool openFile(const std::string &fileName, const std::string &mode) override
Open the file to use in IO operations with events.
bool m_isOpened
identified of the file state, if it is open or not.
void getDataType(size_t &CoordSize, std::string &typeName) const override
As save/load operations use void data type, these function allow set up/get the type name provided fo...
void saveBlock(const std::vector< float > &, const uint64_t) const override
Save block of data into properly opened and initiated direct access data file.
void closeFile() override
Close the file.
BoxControllerDummyIO(const Mantid::API::BoxController *bc)
Constructor.
bool m_ReadOnly
identifier if the file open only for reading or is in read/write
std::string m_fileName
full file name (with path) of the Nexis file responsible for the IO operations (as NeXus filename has...
unsigned int m_CoordSize
number of bytes in the event coorinates (coord_t length).
void setDataType(const size_t blockSize, const std::string &typeName) override
The optional method to set up the event type and the size of the event coordinate As save/load operat...
void loadBlock(std::vector< float > &, const uint64_t, const size_t) const override
Load a block of data from properly prepared direct access data file.
const Mantid::API::BoxController * m_bc
shared pointer to the box controller, which is repsoponsible for this IO
This class is used by MDBox and MDGridBox in order to intelligently determine optimal behavior.
Definition: BoxController.h:33
size_t getNDims() const
Get # of dimensions.
Definition: BoxController.h:70
uint64_t getFileLength() const
Definition: DiskBuffer.h:106
void setFileLength(const uint64_t length) const
Set the length of the file that this MRU writes to.
Definition: DiskBuffer.h:111
Records the filename and the description of failure.
Definition: Exception.h:98