Mantid
Loading...
Searching...
No Matches
DataBlockComposite.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2016 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#pragma once
8
10#include "MantidDataHandling/DllConfig.h"
11#include <algorithm>
12#include <vector>
13
14namespace Mantid {
15namespace DataHandling {
16
21class MANTID_DATAHANDLING_DLL DataBlockComposite : public DataBlock {
22public:
23 specnum_t getMinSpectrumID() const override;
24 void setMinSpectrumID(specnum_t) override;
25
26 specnum_t getMaxSpectrumID() const override;
27 void setMaxSpectrumID(specnum_t) override;
28
29 size_t getNumberOfSpectra() const override;
30 size_t getNumberOfChannels() const override;
31 size_t getNumberOfPeriods() const override;
32
33 std::unique_ptr<DataBlockGenerator> getGenerator() const override;
34
35 bool operator==(const DataBlockComposite &other) const;
36
37 // DataBlockComposite only mehtods
38 void addDataBlock(const DataBlock &dataBlock);
39 std::vector<DataBlock> getDataBlocks();
41 void removeSpectra(DataBlockComposite &toRemove);
42 void truncate(specnum_t specMin, specnum_t specMax);
43 std::vector<specnum_t> getAllSpectrumNumbers();
44 bool isEmpty();
45
46private:
47 std::vector<DataBlock> m_dataBlocks;
48};
49
63template <typename T>
64void DLLExport populateDataBlockCompositeWithContainer(DataBlockComposite &dataBlockComposite, T &indexContainer,
65 int64_t nArray, size_t numberOfPeriods, size_t numberOfChannels,
66 std::vector<specnum_t> monitorSpectra) {
67 auto isMonitor = [&monitorSpectra](specnum_t index) {
68 return std::find(std::begin(monitorSpectra), std::end(monitorSpectra), index) != std::end(monitorSpectra);
69 };
70
71 // Handles the case when an element is a monitor. It needs to crate a data
72 // block
73 // for potential specturm numbers before the monitor and a data block for
74 // the
75 // monitor itself
76 struct HandleWhenElementIsMonitor {
77 void operator()(Mantid::DataHandling::DataBlockComposite &dataBlockComposite, size_t numberOfPeriods,
78 size_t numberOfChannels, specnum_t previousValue, specnum_t startValue) {
79 if (previousValue - startValue > 0) {
80 auto numberOfSpectra = previousValue - startValue; /* Should be from [start,
81 previousValue -1]*/
82 DataBlock dataBlock(numberOfPeriods, numberOfSpectra, numberOfChannels);
83 dataBlock.setMinSpectrumID(startValue);
84 dataBlock.setMaxSpectrumID(previousValue - 1);
85 dataBlockComposite.addDataBlock(dataBlock);
86 }
87
88 // Save out the monitor
89 DataBlock dataBlock(numberOfPeriods, 1, numberOfChannels);
90 dataBlock.setMinSpectrumID(previousValue);
91 dataBlock.setMaxSpectrumID(previousValue);
92 dataBlockComposite.addDataBlock(dataBlock);
93 }
94 };
95
96 // Handles the case when the element made a jump, ie there seems to
97 // be a gap between neighbouring spetrum numbers. Then we need to
98 // write out this range as a data block.
99 struct HandleWhenElementMadeAJump {
100 void operator()(Mantid::DataHandling::DataBlockComposite &dataBlockComposite, size_t numberOfPeriods,
101 size_t numberOfChannels, specnum_t previousValue, specnum_t startValue) {
102 auto numberOfSpectra = previousValue - startValue + 1;
103 DataBlock dataBlock(static_cast<int>(numberOfPeriods), numberOfSpectra, numberOfChannels);
104 dataBlock.setMinSpectrumID(startValue);
105 dataBlock.setMaxSpectrumID(previousValue);
106 dataBlockComposite.addDataBlock(dataBlock);
107 }
108 };
109
110 HandleWhenElementIsMonitor handleWhenElementIsMonitor;
111 HandleWhenElementMadeAJump handleWhenElementMadeAJump;
112
113 auto startValue = indexContainer[0];
114 auto previousValue = startValue;
115 for (int64_t arrayIndex = 1; arrayIndex < nArray; ++arrayIndex) {
116 // There are two ways to write data out. Either when we have a jump of
117 // the indices or there is a monitor. In case of a monitor we also need
118 // to clear the data that was potentially before the monitor.
119
120 if (isMonitor(previousValue)) {
121 handleWhenElementIsMonitor(dataBlockComposite, numberOfPeriods, numberOfChannels, previousValue, startValue);
122 startValue = indexContainer[arrayIndex];
123 } else if ((indexContainer[arrayIndex] - previousValue) != 1) {
124 // We must have completed an interval, we create a DataBlock and add
125 // it
126 handleWhenElementMadeAJump(dataBlockComposite, numberOfPeriods, numberOfChannels, previousValue, startValue);
127 startValue = indexContainer[arrayIndex];
128 }
129
130 // Set the previous value to the current value;
131 previousValue = indexContainer[arrayIndex];
132 }
133
134 // The last interval would not have been added.
135 if (isMonitor(previousValue)) {
136 handleWhenElementIsMonitor(dataBlockComposite, numberOfPeriods, numberOfChannels, previousValue, startValue);
137 } else {
138 handleWhenElementMadeAJump(dataBlockComposite, numberOfPeriods, numberOfChannels, previousValue, startValue);
139 }
140}
141} // namespace DataHandling
142} // namespace Mantid
std::map< DeltaEMode::Type, std::string > index
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:37
DataBlockComposite: The DataBlockComposite handles a collection of DataBlocks.
void addDataBlock(const DataBlock &dataBlock)
DataBlock: The DataBlock class holds information about a contiguous block of spectrum numbers.
Definition DataBlock.h:28
virtual void setMaxSpectrumID(specnum_t minSpecID)
Definition DataBlock.cpp:37
virtual void setMinSpectrumID(specnum_t minSpecID)
Definition DataBlock.cpp:33
void DLLExport populateDataBlockCompositeWithContainer(DataBlockComposite &dataBlockComposite, T &indexContainer, int64_t nArray, size_t numberOfPeriods, size_t numberOfChannels, std::vector< specnum_t > monitorSpectra)
Populates a DataBlockComposite with DataBlocks which are extracted from a indexable collection (array...
Helper class which provides the Collimation Length for SANS instruments.
int32_t specnum_t
Typedef for a spectrum Number.
Definition IDTypes.h:14
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr wide_integer< Bits, Signed > operator+(const wide_integer< Bits, Signed > &lhs) noexcept(is_same< Signed, unsigned >::value)