Mantid
Loading...
Searching...
No Matches
ConvertToMatrixWorkspace.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//----------------------------------------------------------------------
14
15namespace Mantid::Algorithms {
16
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(ConvertToMatrixWorkspace)
19
20using namespace Kernel;
21using namespace API;
22using namespace DataObjects;
23using std::size_t;
24
26 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
27 "An input EventWorkspace.");
28 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
29 "An output Workspace2D.");
30}
31
33 MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
34 // Let's see if we have to do anything first. Basically we want to avoid the
35 // data copy if we can
37 std::dynamic_pointer_cast<const DataObjects::EventWorkspace>(inputWorkspace);
38 MatrixWorkspace_sptr outputWorkspace;
39 if (eventW) {
40 g_log.information() << "Converting EventWorkspace to Workspace2D.\n";
41
42 const size_t numHists = inputWorkspace->getNumberHistograms();
43 Progress prog(this, 0.0, 1.0, numHists * 2);
44
45 // Sort the input workspace in-place by TOF. This can be faster if there are
46 // few event lists.
47 eventW->sortAll(TOF_SORT, &prog);
48
49 // Create the output workspace. This will copy many aspects fron the input
50 // one.
51 outputWorkspace = create<Workspace2D>(*inputWorkspace);
52
53 // ...but not the data, so do that here.
54 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWorkspace, *outputWorkspace))
55 for (int64_t i = 0; i < static_cast<int64_t>(numHists); ++i) {
57 const auto &inSpec = inputWorkspace->getSpectrum(i);
58 auto &outSpec = outputWorkspace->getSpectrum(i);
59
60 outSpec.copyInfoFrom(inSpec);
61 outSpec.setHistogram(inSpec.histogram());
62
63 prog.report("Binning");
64
66 }
68 } else {
69 outputWorkspace = getProperty("OutputWorkspace");
70 if (inputWorkspace == outputWorkspace) {
71 g_log.information("InputWorkspace does not need converting. Pointing "
72 "OutputWorkspace property to input.");
73 outputWorkspace = std::const_pointer_cast<MatrixWorkspace>(inputWorkspace);
74 } else {
75 g_log.information("InputWorkspace does not need converting. Cloning InputWorkspace.");
76 outputWorkspace = inputWorkspace->clone();
77 }
78 }
79
80 setProperty("OutputWorkspace", outputWorkspace);
81}
82
83} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
#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_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
Kernel::Logger & g_log
Definition: Algorithm.h:451
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
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
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54