Mantid
Loading...
Searching...
No Matches
XDataConverter.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// Includes
9//------------------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
15
16namespace Mantid::Algorithms {
17
20using API::Progress;
22using API::WorkspaceProperty;
23using DataObjects::RebinnedOutput;
27
28//------------------------------------------------------------------------------
29// Public member functions
30//------------------------------------------------------------------------------
34XDataConverter::XDataConverter() : m_sharedX(false) {}
35
36//------------------------------------------------------------------------------
37// Private member functions
38//------------------------------------------------------------------------------
39
43 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
44 "Name of the input workspace.");
45 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
46 "Name of the output workspace, can be the same as the input.");
47}
48
51 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
52 if (!isProcessingRequired(inputWS)) {
53 setProperty("OutputWorkspace", inputWS);
54 return;
55 }
56
57 const auto numSpectra = static_cast<int>(inputWS->getNumberHistograms());
58 const size_t numYValues = getNewYSize(inputWS);
59 const size_t numXValues = getNewXSize(numYValues);
61 // Create the new workspace
62 MatrixWorkspace_sptr outputWS = WorkspaceFactory::Instance().create(inputWS, numSpectra, numXValues, numYValues);
63
64 // Copy over the 'vertical' axis
65 if (inputWS->axes() > 1)
66 outputWS->replaceAxis(1, std::unique_ptr<API::Axis>(inputWS->getAxis(1)->clone(outputWS.get())));
67
68 bool isRebinnedWorkspace = inputWS->id() == "RebinnedOutput";
69 RebinnedOutput_sptr outRB = std::dynamic_pointer_cast<RebinnedOutput>(outputWS);
70 // Force fractions to unity (converting from histo to point discards bin info).
71
72 Progress prog(this, 0.0, 1.0, numSpectra);
73 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
74 for (int i = 0; i < int(numSpectra); ++i) {
76
77 // Copy over the Y and E data
78 outputWS->setSharedY(i, inputWS->sharedY(i));
79 outputWS->setSharedE(i, inputWS->sharedE(i));
80 if (isRebinnedWorkspace && outRB) {
81 MantidVecPtr outF;
82 outF.access().resize(inputWS->getNumberBins(i), 1.0);
83 outRB->setF(i, outF);
84 }
85 setXData(outputWS, inputWS, i);
86 if (inputWS->hasDx(i)) {
87 outputWS->setSharedDx(i, inputWS->sharedDx(i));
88 }
89 prog.report();
90
92 }
94
95 // Store the output
96 setProperty("OutputWorkspace", outputWS);
97}
98
100 // this is the old behavior of MatrixWorkspace::blocksize()
101 return inputWS->y(0).size();
102}
103
111 const int index) {
112 if (m_sharedX) {
113 PARALLEL_CRITICAL(XDataConverter_para) {
114 if (!m_cachedX) {
115 PARALLEL_CRITICAL(XDataConverter_parb) { m_cachedX = calculateXPoints(inputWS->sharedX(index)); }
116 }
117 }
118 outputWS->setSharedX(index, m_cachedX);
119 } else {
120 outputWS->setSharedX(index, calculateXPoints(inputWS->sharedX(index)));
121 }
122}
123} // namespace Mantid::Algorithms
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_CRITICAL(name)
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
virtual Kernel::cow_ptr< HistogramData::HistogramX > calculateXPoints(const Kernel::cow_ptr< HistogramData::HistogramX > inputX) const =0
Calculate the X point values. Implement in an inheriting class.
bool m_sharedX
Flag if the X data is shared.
virtual std::size_t getNewXSize(const std::size_t ySize) const =0
Returns the size of the new X vector.
void setXData(const API::MatrixWorkspace_sptr &outputWS, const API::MatrixWorkspace_sptr &inputWS, const int index)
Set the X data on given spectra.
virtual bool isProcessingRequired(const API::MatrixWorkspace_sptr inputWS) const =0
Returns true if the algorithm needs to be run.
void exec() override
Override exec.
XDataConverter()
Default constructor.
void init() override
Override init.
Kernel::cow_ptr< HistogramData::HistogramX > m_cachedX
Cached data for shared X values.
std::size_t getNewYSize(const API::MatrixWorkspace_sptr &inputWS)
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Implements a copy on write data template.
Definition: cow_ptr.h:41
DataType & access()
Access function.
Definition: cow_ptr.h:147
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
Mantid::Kernel::SingletonHolder< WorkspaceFactoryImpl > WorkspaceFactory
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< RebinnedOutput > RebinnedOutput_sptr
shared pointer to the RebinnedOutput class
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
static bool sharedXData(const MatrixWorkspace &WS)
Checks whether all the X vectors in a workspace are the same one underneath.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54