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