MatrixWorkspace.h 25.7 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
10
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/ExperimentInfo.h"
11
#include "MantidAPI/IMDWorkspace.h"
12
#include "MantidAPI/ISpectrum.h"
13
#include "MantidAPI/MatrixWorkspace_fwd.h"
14
#include "MantidKernel/EmptyValues.h"
15

16
#include <atomic>
17
#include <mutex>
18

19
namespace Mantid {
20
21

namespace Indexing {
22
class IndexInfo;
23
24
}

25
namespace Types {
26
namespace Core {
27
class DateAndTime;
28
}
LamarMoore's avatar
LamarMoore committed
29
} // namespace Types
30

31
32
33
namespace Geometry {
class ParameterMap;
}
34

35
namespace API {
36
class Axis;
37
38
39
class SpectrumDetectorMapping;

/// typedef for the image type
40
using MantidImage = std::vector<std::vector<double>>;
41
/// shared pointer to MantidImage
42
using MantidImage_sptr = std::shared_ptr<MantidImage>;
43
/// shared pointer to const MantidImage
44
using MantidImage_const_sptr = std::shared_ptr<const MantidImage>;
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//----------------------------------------------------------------------
/** Base MatrixWorkspace Abstract Class.

@author Laurent C Chapon, ISIS, RAL
@date 26/09/2007
*/
class MANTID_API_DLL MatrixWorkspace : public IMDWorkspace,
                                       public ExperimentInfo {

private:
  using ExperimentInfo::toString;

public:
  // The Workspace Factory create-from-parent method needs direct access to the
  // axes.
  friend class WorkspaceFactoryImpl;

  void initialize(const std::size_t &NVectors, const std::size_t &XLength,
                  const std::size_t &YLength);
65
66
  void initialize(const std::size_t &NVectors,
                  const HistogramData::Histogram &histogram);
67
68
  void initialize(const Indexing::IndexInfo &indexInfo,
                  const HistogramData::Histogram &histogram);
69
70

  MatrixWorkspace &operator=(const MatrixWorkspace &other) = delete;
71
  /// Delete
72
  ~MatrixWorkspace() override;
73

74
  /// Returns a clone of the workspace
Martyn Gigg's avatar
Martyn Gigg committed
75
  MatrixWorkspace_uptr clone() const { return MatrixWorkspace_uptr(doClone()); }
76

77
78
79
80
81
  /// Returns a default-initialized clone of the workspace
  MatrixWorkspace_uptr cloneEmpty() const {
    return MatrixWorkspace_uptr(doCloneEmpty());
  }

82
  const Indexing::IndexInfo &indexInfo() const;
83
  void setIndexInfo(const Indexing::IndexInfo &indexInfo);
84

85
86
  using IMDWorkspace::toString;
  /// String description of state
87
  const std::string toString() const override;
88
89
90

  /**@name Instrument queries */
  //@{
91
  std::shared_ptr<const Geometry::IDetector>
92
  getDetector(const size_t workspaceIndex) const;
93
94
  double detectorTwoTheta(const Geometry::IDetector &det) const;
  double detectorSignedTwoTheta(const Geometry::IDetector &det) const;
95

96
97
  //@}

Owen Arnold's avatar
Owen Arnold committed
98
  virtual void updateSpectraUsing(const SpectrumDetectorMapping &map);
99
100
  /// Build the default spectra mapping, most likely wanted after an instrument
  /// update
101
102
  void rebuildSpectraMapping(const bool includeMonitors = true,
                             const specnum_t specNumOffset = 1);
103
104
105
106
107

  // More mapping
  spec2index_map getSpectrumToWorkspaceIndexMap() const;
  detid2index_map
  getDetectorIDToWorkspaceIndexMap(bool throwIfMultipleDets = false) const;
108
109
  virtual std::vector<size_t>
  getDetectorIDToWorkspaceIndexVector(detid_t &offset,
110
                                      bool throwIfMultipleDets = false) const;
Nick Draper's avatar
Nick Draper committed
111

112
  virtual std::vector<size_t>
Nick Draper's avatar
Nick Draper committed
113
  getSpectrumToWorkspaceIndexVector(specnum_t &offset) const;
114
  std::vector<size_t>
Nick Draper's avatar
Nick Draper committed
115
116
  getIndicesFromSpectra(const std::vector<specnum_t> &spectraList) const;
  size_t getIndexFromSpectrumNumber(const specnum_t specNo) const;
117
118
  std::vector<size_t>
  getIndicesFromDetectorIDs(const std::vector<detid_t> &detIdList) const;
Nick Draper's avatar
Nick Draper committed
119
  std::vector<specnum_t>
120
  getSpectraFromDetectorIDs(const std::vector<detid_t> &detIdList) const;
121
122
123

  bool hasGroupedDetectors() const;

124
125
126
  /// Returns true if the workspace is ragged (has differently sized spectra).
  virtual bool isRaggedWorkspace() const = 0;

127
  /// Get the footprint in memory in bytes.
128
  size_t getMemorySize() const override;
129
130
131
132
133
  virtual size_t getMemorySizeForXAxes() const;

  // Section required for iteration
  /// Returns the number of single indexable items in the workspace
  virtual std::size_t size() const = 0;
Peterson, Peter's avatar
Peterson, Peter committed
134
135
136
  /// Returns the size of each block of data returned by the dataY accessors.
  /// This throws an exception if the lengths are not identical across the
  /// spectra.
137
  virtual std::size_t blocksize() const = 0;
138
139
140
141
  /// Returns the number of bins for a given histogram index.
  virtual std::size_t getNumberBins(const std::size_t &index) const = 0;
  /// Returns the maximum number of bins in a workspace (works on ragged data).
  virtual std::size_t getMaxNumberBins() const = 0;
142
143
144
145
  /// Returns the number of histograms in the workspace
  virtual std::size_t getNumberHistograms() const = 0;

  /// Sets MatrixWorkspace title
146
  void setTitle(const std::string &) override;
147
  /// Gets MatrixWorkspace title (same as Run object run_title property)
148
  const std::string getTitle() const override;
149

150
151
  virtual Types::Core::DateAndTime getFirstPulseTime() const;
  Types::Core::DateAndTime getLastPulseTime() const;
152

Robert Applin's avatar
Robert Applin committed
153
  /// Returns the y index which corresponds to the X Value provided
Robert Applin's avatar
Robert Applin committed
154
155
  std::size_t yIndexOfX(const double xValue, const std::size_t &index = 0,
                        const double tolerance = 0.0) const;
156
157
158
159
160
161

  //----------------------------------------------------------------------
  // DATA ACCESSORS
  //----------------------------------------------------------------------

  /// Return the underlying ISpectrum ptr at the given workspace index.
162
  virtual ISpectrum &getSpectrum(const size_t index) = 0;
163
164
  /// Return the underlying ISpectrum ptr (const version) at the given workspace
  /// index.
165
  virtual const ISpectrum &getSpectrum(const size_t index) const = 0;
166

167
168
  /// Returns the Histogram at the given workspace index.
  HistogramData::Histogram histogram(const size_t index) const {
169
    return getSpectrum(index).histogram();
170
  }
171
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
172
  void setHistogram(const size_t index, T &&... data) & {
Moore's avatar
Moore committed
173
    getSpectrum(index).setHistogram(std::forward<T>(data)...);
174
  }
175
  void convertToCounts(const size_t index) {
176
    getSpectrumWithoutInvalidation(index).convertToCounts();
177
178
  }
  void convertToFrequencies(const size_t index) {
179
    getSpectrumWithoutInvalidation(index).convertToFrequencies();
180
  }
181
  HistogramData::BinEdges binEdges(const size_t index) const {
182
    return getSpectrum(index).binEdges();
183
184
  }
  HistogramData::Points points(const size_t index) const {
185
    return getSpectrum(index).points();
186
  }
187
188
  HistogramData::PointStandardDeviations
  pointStandardDeviations(const size_t index) const {
189
    return getSpectrum(index).pointStandardDeviations();
190
  }
Robert Applin's avatar
Robert Applin committed
191
192
  template <typename... T>
  void setBinEdges(const size_t index, T &&... data) & {
193
    getSpectrum(index).setBinEdges(std::forward<T>(data)...);
194
  }
Robert Applin's avatar
Robert Applin committed
195
  template <typename... T> void setPoints(const size_t index, T &&... data) & {
196
    getSpectrum(index).setPoints(std::forward<T>(data)...);
197
  }
198
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
199
  void setPointVariances(const size_t index, T &&... data) & {
200
201
    getSpectrumWithoutInvalidation(index).setPointVariances(
        std::forward<T>(data)...);
202
203
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
204
  void setPointStandardDeviations(const size_t index, T &&... data) & {
205
206
    getSpectrumWithoutInvalidation(index).setPointStandardDeviations(
        std::forward<T>(data)...);
207
  }
208
  HistogramData::Counts counts(const size_t index) const {
209
    return getSpectrum(index).counts();
210
211
  }
  HistogramData::CountVariances countVariances(const size_t index) const {
212
    return getSpectrum(index).countVariances();
213
214
215
  }
  HistogramData::CountStandardDeviations
  countStandardDeviations(const size_t index) const {
216
    return getSpectrum(index).countStandardDeviations();
217
218
  }
  HistogramData::Frequencies frequencies(const size_t index) const {
219
    return getSpectrum(index).frequencies();
220
221
222
  }
  HistogramData::FrequencyVariances
  frequencyVariances(const size_t index) const {
223
    return getSpectrum(index).frequencyVariances();
224
225
226
  }
  HistogramData::FrequencyStandardDeviations
  frequencyStandardDeviations(const size_t index) const {
227
    return getSpectrum(index).frequencyStandardDeviations();
228
  }
Robert Applin's avatar
Robert Applin committed
229
  template <typename... T> void setCounts(const size_t index, T &&... data) & {
230
    getSpectrumWithoutInvalidation(index).setCounts(std::forward<T>(data)...);
231
232
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
233
  void setCountVariances(const size_t index, T &&... data) & {
234
235
    getSpectrumWithoutInvalidation(index).setCountVariances(
        std::forward<T>(data)...);
236
237
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
238
  void setCountStandardDeviations(const size_t index, T &&... data) & {
239
240
    getSpectrumWithoutInvalidation(index).setCountStandardDeviations(
        std::forward<T>(data)...);
241
242
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
243
  void setFrequencies(const size_t index, T &&... data) & {
244
245
    getSpectrumWithoutInvalidation(index).setFrequencies(
        std::forward<T>(data)...);
246
247
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
248
  void setFrequencyVariances(const size_t index, T &&... data) & {
249
250
    getSpectrumWithoutInvalidation(index).setFrequencyVariances(
        std::forward<T>(data)...);
251
252
  }
  template <typename... T>
Robert Applin's avatar
Robert Applin committed
253
  void setFrequencyStandardDeviations(const size_t index, T &&... data) & {
254
255
    getSpectrumWithoutInvalidation(index).setFrequencyStandardDeviations(
        std::forward<T>(data)...);
256
  }
257
  const HistogramData::HistogramX &x(const size_t index) const {
258
    return getSpectrum(index).x();
259
  }
260
  const HistogramData::HistogramY &y(const size_t index) const {
261
    return getSpectrum(index).y();
262
263
  }
  const HistogramData::HistogramE &e(const size_t index) const {
264
    return getSpectrum(index).e();
265
  }
266
  const HistogramData::HistogramDx &dx(const size_t index) const {
267
    return getSpectrum(index).dx();
268
  }
269
  HistogramData::HistogramX &mutableX(const size_t index) & {
270
    return getSpectrum(index).mutableX();
271
  }
272
  HistogramData::HistogramDx &mutableDx(const size_t index) & {
273
    return getSpectrumWithoutInvalidation(index).mutableDx();
274
  }
275
  HistogramData::HistogramY &mutableY(const size_t index) & {
276
    return getSpectrumWithoutInvalidation(index).mutableY();
277
  }
278
  HistogramData::HistogramE &mutableE(const size_t index) & {
279
    return getSpectrumWithoutInvalidation(index).mutableE();
280
  }
281
  Kernel::cow_ptr<HistogramData::HistogramX> sharedX(const size_t index) const {
282
    return getSpectrum(index).sharedX();
283
  }
284
  Kernel::cow_ptr<HistogramData::HistogramY> sharedY(const size_t index) const {
285
    return getSpectrum(index).sharedY();
286
287
  }
  Kernel::cow_ptr<HistogramData::HistogramE> sharedE(const size_t index) const {
288
    return getSpectrum(index).sharedE();
289
  }
290
291
  Kernel::cow_ptr<HistogramData::HistogramDx>
  sharedDx(const size_t index) const {
292
    return getSpectrum(index).sharedDx();
293
  }
294
  void setSharedX(const size_t index,
295
                  const Kernel::cow_ptr<HistogramData::HistogramX> &x) & {
296
    getSpectrum(index).setSharedX(x);
297
  }
298
  void setSharedDx(const size_t index,
299
                   const Kernel::cow_ptr<HistogramData::HistogramDx> &dx) & {
300
    getSpectrumWithoutInvalidation(index).setSharedDx(dx);
301
  }
302
  void setSharedY(const size_t index,
303
                  const Kernel::cow_ptr<HistogramData::HistogramY> &y) & {
304
    getSpectrumWithoutInvalidation(index).setSharedY(y);
305
306
  }
  void setSharedE(const size_t index,
307
                  const Kernel::cow_ptr<HistogramData::HistogramE> &e) & {
308
    getSpectrumWithoutInvalidation(index).setSharedE(e);
309
  }
310
311
  // Methods for getting read-only access to the data.
  // Just passes through to the virtual dataX/Y/E function (const version)
312
313
  /// Deprecated, use x() instead. Returns a read-only (i.e. const) reference to
  /// the specified X array
314
315
  /// @param index :: workspace index to retrieve.
  const MantidVec &readX(std::size_t const index) const {
316
    return getSpectrum(index).dataX();
317
  }
318
319
  /// Deprecated, use y() instead. Returns a read-only (i.e. const) reference to
  /// the specified Y array
320
321
  /// @param index :: workspace index to retrieve.
  const MantidVec &readY(std::size_t const index) const {
322
    return getSpectrum(index).dataY();
323
  }
324
325
  /// Deprecated, use e() instead. Returns a read-only (i.e. const) reference to
  /// the specified E array
326
327
  /// @param index :: workspace index to retrieve.
  const MantidVec &readE(std::size_t const index) const {
328
    return getSpectrum(index).dataE();
329
  }
330
331
  /// Deprecated, use dx() instead. Returns a read-only (i.e. const) reference
  /// to the specified X error array
332
333
  /// @param index :: workspace index to retrieve.
  const MantidVec &readDx(size_t const index) const {
334
    return getSpectrum(index).dataDx();
335
336
  }

337
  /// Deprecated, use mutableX() instead. Returns the x data
338
  virtual MantidVec &dataX(const std::size_t index) {
339
    return getSpectrum(index).dataX();
340
  }
341
  /// Deprecated, use mutableY() instead. Returns the y data
342
  virtual MantidVec &dataY(const std::size_t index) {
343
    return getSpectrumWithoutInvalidation(index).dataY();
344
  }
345
  /// Deprecated, use mutableE() instead. Returns the error data
346
  virtual MantidVec &dataE(const std::size_t index) {
347
    return getSpectrumWithoutInvalidation(index).dataE();
348
  }
349
  /// Deprecated, use mutableDx() instead. Returns the x error data
350
  virtual MantidVec &dataDx(const std::size_t index) {
351
    return getSpectrumWithoutInvalidation(index).dataDx();
352
353
  }

354
  /// Deprecated, use x() instead. Returns the x data const
355
  virtual const MantidVec &dataX(const std::size_t index) const {
356
    return getSpectrum(index).dataX();
357
  }
358
  /// Deprecated, use y() instead. Returns the y data const
359
  virtual const MantidVec &dataY(const std::size_t index) const {
360
    return getSpectrum(index).dataY();
361
  }
362
  /// Deprecated, use e() instead. Returns the error const
363
  virtual const MantidVec &dataE(const std::size_t index) const {
364
    return getSpectrum(index).dataE();
365
  }
366
  /// Deprecated, use dx() instead. Returns the error const
367
  virtual const MantidVec &dataDx(const std::size_t index) const {
368
    return getSpectrum(index).dataDx();
369
  }
370

371
372
373
  virtual double getXMin() const;
  virtual double getXMax() const;
  virtual void getXMinMax(double &xmin, double &xmax) const;
374

375
  /// Deprecated, use sharedX() instead. Returns a pointer to the x data
376
377
  virtual Kernel::cow_ptr<HistogramData::HistogramX>
  refX(const std::size_t index) const {
378
    return getSpectrum(index).ptrX();
379
380
  }

381
382
  /// Deprecated, use setSharedX() instead. Set the specified X array to point
  /// to the given existing array
383
384
  virtual void setX(const std::size_t index,
                    const Kernel::cow_ptr<HistogramData::HistogramX> &X) {
385
    getSpectrum(index).setX(X);
386
387
  }

388
389
  /// Deprecated, use setSharedX() instead. Set the specified X array to point
  /// to the given existing array
390
  virtual void setX(const std::size_t index,
391
                    const std::shared_ptr<HistogramData::HistogramX> &X) {
392
    getSpectrum(index).setX(X);
393
394
  }

395
396
  /**
   * Probes if DX (X Error) values were set on a particular spectrum
Nick Draper's avatar
Nick Draper committed
397
   * @param index: the workspace index
398
   */
399
  virtual bool hasDx(const std::size_t index) const {
400
    return getSpectrum(index).hasDx();
401
402
  }

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  /// Generate the histogram or rebin the existing histogram.
  virtual void generateHistogram(const std::size_t index, const MantidVec &X,
                                 MantidVec &Y, MantidVec &E,
                                 bool skipError = false) const = 0;

  /// Return a vector with the integrated counts for all spectra withing the
  /// given range
  virtual void getIntegratedSpectra(std::vector<double> &out, const double minX,
                                    const double maxX,
                                    const bool entireRange) const;

  /// Return an index in the X vector for an x-value close to a given value
  std::pair<size_t, double> getXIndex(size_t i, double x, bool isLeft = true,
                                      size_t start = 0) const;

  //----------------------------------------------------------------------

  int axes() const;
Owen Arnold's avatar
Owen Arnold committed
421
  virtual Axis *getAxis(const std::size_t &axisIndex) const;
422
  void replaceAxis(const std::size_t &axisIndex, std::unique_ptr<Axis> newAxis);
423

424
425
426
427
  /// Will return the number of Axis currently stored in the workspace it is not
  /// always safe to assume it is just 2
  size_t numberOfAxis() const;

Hahn, Steven's avatar
Hahn, Steven committed
428
429
430
  /// Returns true if the workspace contains common X bins with log spacing
  virtual bool isCommonLogBins() const;

431
432
  /// Returns true if the workspace contains data in histogram form (as
  /// opposed to point-like)
433
  virtual bool isHistogramData() const;
434

435
  /// Returns true if the workspace contains common X bins
436
437
438
439
  virtual bool isCommonBins() const;

  std::string YUnit() const;
  void setYUnit(const std::string &newUnit);
440
441
  std::string YUnitLabel(bool useLatex = false,
                         bool plotAsDistribution = false) const;
442
443
444
  void setYUnitLabel(const std::string &newLabel);

  /// Are the Y-values dimensioned?
445
  bool isDistribution() const;
446
  void setDistribution(bool newValue);
447
448
449
450

  // Methods to set and access masked bins
  void maskBin(const size_t &workspaceIndex, const size_t &binIndex,
               const double &weight = 1.0);
451
  void flagMasked(const size_t &index, const size_t &binIndex,
452
                  const double &weight = 1.0);
453
  bool hasMaskedBins(const size_t &workspaceIndex) const;
454
  bool hasAnyMaskedBins() const;
455
456
  /// Masked bins for each spectrum are stored as a set of pairs containing <bin
  /// index, weight>
457
  using MaskList = std::map<size_t, double>;
458
  const MaskList &maskedBins(const size_t &workspaceIndex) const;
459
  std::vector<size_t> maskedBinsIndices(const size_t &workspaceIndex) const;
460
  void setMaskedBins(const size_t workspaceIndex, const MaskList &maskedBins);
461
462

  // Methods handling the internal monitor workspace
463
  virtual void
464
465
  setMonitorWorkspace(const std::shared_ptr<MatrixWorkspace> &monitorWS);
  std::shared_ptr<MatrixWorkspace> monitorWorkspace() const;
466
467
468
469
470
471

  void loadInstrumentNexus(::NeXus::File *file);

  //=====================================================================================
  // MD Geometry methods
  //=====================================================================================
472
  size_t getNumDims() const override;
473
  std::shared_ptr<const Mantid::Geometry::IMDDimension>
474
  getDimension(size_t index) const override;
475
  std::shared_ptr<const Mantid::Geometry::IMDDimension>
476
  getDimensionWithId(std::string id) const override;
477
478
479
480
481
482
483
484
485
  //=====================================================================================
  // End MD Geometry methods
  //=====================================================================================

  //=====================================================================================
  // IMDWorkspace methods
  //=====================================================================================

  /// Gets the number of points available on the workspace.
486
  uint64_t getNPoints() const override;
487
  /// Get the number of points available on the workspace.
488
  uint64_t getNEvents() const override { return this->getNPoints(); }
489
490
491
492
493
  /// Dimension id for x-dimension.
  static const std::string xDimensionId;
  /// Dimensin id for y-dimension.
  static const std::string yDimensionId;
  /// Generate a line plot through the matrix workspace.
494
495
496
  LinePlot getLinePlot(const Mantid::Kernel::VMD &start,
                       const Mantid::Kernel::VMD &end,
                       Mantid::API::MDNormalization normalize) const override;
497
  /// Get the signal at a coordinate in the workspace.
498
499
500
  signal_t getSignalAtCoord(
      const coord_t *coords,
      const Mantid::API::MDNormalization &normalization) const override;
501
  /// Get the signal at a coordinate in the workspace
502
  signal_t getSignalWithMaskAtCoord(
Matthew D Jones's avatar
Matthew D Jones committed
503
      const coord_t *coords,
504
      const Mantid::API::MDNormalization &normalization) const override;
505
506
  /// Create iterators. Partitions the iterators according to the number of
  /// cores.
507
  std::vector<std::unique_ptr<IMDIterator>> createIterators(
508
      size_t suggestedNumCores = 1,
509
      Mantid::Geometry::MDImplicitFunction *function = nullptr) const override;
510
511

  /// Apply masking.
512
513
  void setMDMasking(std::unique_ptr<Mantid::Geometry::MDImplicitFunction>
                        maskingRegion) override;
514
  /// Clear exsting masking.
515
  void clearMDMasking() override;
516
517

  /// @return the special coordinate system used if any.
518
519
  Mantid::Kernel::SpecialCoordinateSystem
  getSpecialCoordinateSystem() const override;
520

521
522
523
  // Check if this class has an oriented lattice on a sample object
  virtual bool hasOrientedLattice() const override;

524
  /// Find first index in Y equal to value.
525
526
  /// @param value Number to find in Y
  /// @param idx starting indices for search
527
  /// @return Tuple with the ihistogram and bin indices.
528
529
  virtual std::pair<int64_t, int64_t>
  findY(double value, const std::pair<int64_t, int64_t> &idx = {0, 0}) const;
530

Hahn, Steven's avatar
Hahn, Steven committed
531
532
533
  //=====================================================================================
  // End IMDWorkspace methods
  //=====================================================================================
Hahn, Steven's avatar
Hahn, Steven committed
534

Hahn, Steven's avatar
Hahn, Steven committed
535
536
537
  //=====================================================================================
  // Image methods
  //=====================================================================================
538

Hahn, Steven's avatar
Hahn, Steven committed
539
540
541
  /// Get start and end x indices for images
  std::pair<size_t, size_t> getImageStartEndXIndices(size_t i, double startX,
                                                     double endX) const;
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
  /// Create an image of Ys.
  MantidImage_sptr getImageY(size_t start = 0, size_t stop = 0,
                             size_t width = 0, double startX = EMPTY_DBL(),
                             double endX = EMPTY_DBL()) const;
  /// Create an image of Es.
  MantidImage_sptr getImageE(size_t start = 0, size_t stop = 0,
                             size_t width = 0, double startX = EMPTY_DBL(),
                             double endX = EMPTY_DBL()) const;
  /// Copy the data (Y's) from an image to this workspace.
  virtual void setImageY(const MantidImage &image, size_t start = 0,
                         bool parallelExecution = true);
  /// Copy the data from an image to this workspace's errors.
  virtual void setImageE(const MantidImage &image, size_t start = 0,
                         bool parallelExecution = true);

  //=====================================================================================
  // End image methods
  //=====================================================================================

561
562
  void invalidateCachedSpectrumNumbers();

563
564
565
566
  /// Invalidates the commons bins flag.  This is generally called when a method
  /// could allow the X values to be changed.
  void invalidateCommonBinsFlag() { m_isCommonBinsFlagValid.store(false); }

567
protected:
568
569
570
  /// Protected copy constructor. May be used by childs for cloning.
  MatrixWorkspace(const MatrixWorkspace &other);

571
572
  MatrixWorkspace(
      const Parallel::StorageMode storageMode = Parallel::StorageMode::Cloned);
573
574
575
576
577

  /// Initialises the workspace. Sets the size and lengths of the arrays. Must
  /// be overloaded.
  virtual void init(const std::size_t &NVectors, const std::size_t &XLength,
                    const std::size_t &YLength) = 0;
578
  virtual void init(const HistogramData::Histogram &histogram) = 0;
579

580
581
  virtual ISpectrum &getSpectrumWithoutInvalidation(const size_t index) = 0;

582
  void updateCachedDetectorGrouping(const size_t index) const override;
583

584
  /// A vector of pointers to the axes for this workspace
585
  std::vector<std::unique_ptr<Axis>> m_axes;
586
587

private:
Robert Applin's avatar
Robert Applin committed
588
  std::size_t binIndexOfValue(Mantid::HistogramData::HistogramX const &xValues,
Robert Applin's avatar
Robert Applin committed
589
590
                              const double xValue,
                              const bool ascendingOrder) const;
Robert Applin's avatar
Robert Applin committed
591
592
  std::size_t xIndexOfValue(const Mantid::HistogramData::HistogramX &xValues,
                            const double xValue, const double tolerance) const;
Robert Applin's avatar
Robert Applin committed
593

594
  MatrixWorkspace *doClone() const override = 0;
595
  MatrixWorkspace *doCloneEmpty() const override = 0;
596

597
598
599
600
601
602
603
604
605
  /// Create an MantidImage instance.
  MantidImage_sptr
  getImage(const MantidVec &(MatrixWorkspace::*read)(std::size_t const) const,
           size_t start, size_t stop, size_t width, size_t indexStart,
           size_t indexEnd) const;
  /// Copy data from an image.
  void setImage(MantidVec &(MatrixWorkspace::*dataVec)(const std::size_t),
                const MantidImage &image, size_t start, bool parallelExecution);

606
  void setIndexInfoWithoutISpectrumUpdate(const Indexing::IndexInfo &indexInfo);
607
  void buildDefaultSpectrumDefinitions();
608
609
  void rebuildDetectorIDGroupings();

610
611
  virtual bool isHistogramDataByIndex(std::size_t index = 0) const;

612
  std::unique_ptr<Indexing::IndexInfo> m_indexInfo;
613

614
  /// Has this workspace been initialised?
615
  bool m_isInitialized{false};
616
617
618
619
620
621

  /// The unit for the data values (e.g. Counts)
  std::string m_YUnit;
  /// A text label for use when plotting spectra
  std::string m_YUnitLabel;

622
623
624
625
  /// Flag indicating if the common bins flag is in a valid state
  mutable std::atomic<bool> m_isCommonBinsFlagValid{false};
  /// Flag indicating whether the data has common bins
  mutable bool m_isCommonBinsFlag{false};
626
627
  /// A mutex protecting the update of m_isCommonBinsFlag.
  mutable std::mutex m_isCommonBinsMutex;
628

629
  /// The set of masked bins in a map keyed on workspace index
630
631
632
633
  std::map<int64_t, MaskList> m_masks;

  /// A workspace holding monitor data relating to the main data in the
  /// containing workspace (null if none).
634
  std::shared_ptr<MatrixWorkspace> m_monitorWorkspace;
635

636
637
  mutable std::atomic<bool> m_indexInfoNeedsUpdate{true};
  mutable std::mutex m_indexInfoMutex;
638

639
640
641
642
643
644
protected:
  /// Getter for the dimension id based on the axis.
  std::string getDimensionIdFromAxis(const int &axisIndex) const;
};

/// shared pointer to the matrix workspace base class
645
using MatrixWorkspace_sptr = std::shared_ptr<MatrixWorkspace>;
646
/// shared pointer to the matrix workspace base class (const version)
647
using MatrixWorkspace_const_sptr = std::shared_ptr<const MatrixWorkspace>;
648
649
650

} // namespace API
} // namespace Mantid