DataProcessorAlgorithm.h 6.4 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
//     NScD Oak Ridge National Laboratory, European Spallation Source
//     & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8
9

#include "MantidAPI/Algorithm.h"
10
#include "MantidAPI/AlgorithmManager.h"
11
#include "MantidAPI/DistributedAlgorithm.h"
12
#include "MantidAPI/IEventWorkspace_fwd.h"
13
14
15
#include "MantidAPI/ITableWorkspace_fwd.h"
#include "MantidAPI/ParallelAlgorithm.h"
#include "MantidAPI/SerialAlgorithm.h"
16
#include "MantidKernel/PropertyManager.h"
LamarMoore's avatar
LamarMoore committed
17
#include "MantidKernel/System.h"
18
19
#include <vector>

20
21
namespace Mantid {
namespace API {
22
23
/**

24
25
   Data processor algorithm to be used as a parent to workflow algorithms.
   This algorithm provides utility methods to load and process data.
26
27
28

   @date 2012-04-04
 */
29
template <class Base>
30
class MANTID_API_DLL GenericDataProcessorAlgorithm : public Base {
31
public:
32
  GenericDataProcessorAlgorithm();
33
  std::string getPropertyValue(const std::string &name) const override;
34
35
  Kernel::PropertyManagerOwner::TypedValue
  getProperty(const std::string &name) const override;
36
37

protected:
38
  boost::shared_ptr<Algorithm> createChildAlgorithm(
39
40
      const std::string &name, const double startProgress = -1.,
      const double endProgress = -1., const bool enableLogging = true,
41
      const int &version = -1) override;
42
43
44
  void setLoadAlg(const std::string &alg);
  void setLoadAlgFileProp(const std::string &filePropName);
  void setAccumAlg(const std::string &alg);
45
  void setPropManagerPropName(const std::string &propName);
46
47
48
  void mapPropertyName(const std::string &nameInProp,
                       const std::string &nameInPropManager);
  void copyProperty(API::Algorithm_sptr alg, const std::string &name);
49
50
  virtual ITableWorkspace_sptr determineChunk(const std::string &filename);
  virtual MatrixWorkspace_sptr loadChunk(const size_t rowIndex);
51
52
53
  Workspace_sptr load(const std::string &inputData,
                      const bool loadQuiet = false);
  std::vector<std::string> splitInput(const std::string &input);
54
  void forwardProperties();
55
56
  boost::shared_ptr<Kernel::PropertyManager> getProcessProperties(
      const std::string &propertyManager = std::string()) const;
57
58
  /// MPI option. If false, we will use one job event if MPI is available
  bool m_useMPI;
59
  Workspace_sptr assemble(Workspace_sptr partialWS);
60
61
62
63
  Workspace_sptr assemble(const std::string &partialWSName,
                          const std::string &outputWSName);
  void saveNexus(const std::string &outputWSName,
                 const std::string &outputFile);
64
65
  bool isMainThread();
  int getNThreads();
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  /// Divide a matrix workspace by another matrix workspace
  MatrixWorkspace_sptr divide(const MatrixWorkspace_sptr lhs,
                              const MatrixWorkspace_sptr rhs);
  /// Divide a matrix workspace by a single value
  MatrixWorkspace_sptr divide(const MatrixWorkspace_sptr lhs,
                              const double &rhsValue);

  /// Multiply a matrix workspace by another matrix workspace
  MatrixWorkspace_sptr multiply(const MatrixWorkspace_sptr lhs,
                                const MatrixWorkspace_sptr rhs);
  /// Multiply a matrix workspace by a single value
  MatrixWorkspace_sptr multiply(const MatrixWorkspace_sptr lhs,
                                const double &rhsValue);

  /// Add a matrix workspace to another matrix workspace
  MatrixWorkspace_sptr plus(const MatrixWorkspace_sptr lhs,
                            const MatrixWorkspace_sptr rhs);
84
  /// Add a single value to a matrix workspace
85
86
87
88
89
90
91
92
93
  MatrixWorkspace_sptr plus(const MatrixWorkspace_sptr lhs,
                            const double &rhsValue);

  /// Subract a matrix workspace by another matrix workspace
  MatrixWorkspace_sptr minus(const MatrixWorkspace_sptr lhs,
                             const MatrixWorkspace_sptr rhs);
  /// Subract a single value from a matrix workspace
  MatrixWorkspace_sptr minus(const MatrixWorkspace_sptr lhs,
                             const double &rhsValue);
94

95
private:
96
97
98
  template <typename LHSType, typename RHSType, typename ResultType>
  ResultType executeBinaryAlgorithm(const std::string &algorithmName,
                                    const LHSType lhs, const RHSType rhs) {
99
100
101
    auto alg = createChildAlgorithm(algorithmName);
    alg->initialize();

102
103
    alg->template setProperty<LHSType>("LHSWorkspace", lhs);
    alg->template setProperty<RHSType>("RHSWorkspace", rhs);
104
105
    alg->execute();

106
    if (alg->isExecuted()) {
107
108
      // Get the output workspace property
      return alg->getProperty("OutputWorkspace");
109
    } else {
110
111
112
113
114
115
      std::string message = "Error while executing operation: " + algorithmName;
      throw std::runtime_error(message);
    }
  }

  /// Create a matrix workspace from a single number
116
  MatrixWorkspace_sptr createWorkspaceSingleValue(const double &rhsValue);
117

118
119
120
121
  /// The name of the algorithm to invoke when loading data
  std::string m_loadAlg;
  /// The name of the algorithm to invoke when accumulating data chunks
  std::string m_accumulateAlg;
122
123
  /// An alternate filename property for the load algorithm
  std::string m_loadAlgFileProp;
124
125
126
  /// The name of the parameter that names the property manager. The default
  /// value is "ReductionProperties".
  std::string m_propertyManagerPropertyName;
127
128
  /// Map property names to names in supplied properties manager
  std::map<std::string, std::string> m_nameToPMName;
129
130
131
132
133
134
135
136

  // This method is a workaround for the C4661 compiler warning in visual
  // studio. This allows the template declaration and definition to be separated
  // in different files. See stack overflow article for a more detailed
  // explanation:
  // https://stackoverflow.com/questions/44160467/warning-c4661no-suitable-definition-provided-for-explicit-template-instantiatio
  // https://stackoverflow.com/questions/33517902/how-to-export-a-class-derived-from-an-explicitly-instantiated-template-in-visual
  void visualStudioC4661Workaround();
137
138
};

139
140
141
142
template <>
MANTID_API_DLL void
GenericDataProcessorAlgorithm<Algorithm>::visualStudioC4661Workaround();

143
144
145
146
147
148
149
using DataProcessorAlgorithm = GenericDataProcessorAlgorithm<Algorithm>;
using SerialDataProcessorAlgorithm =
    GenericDataProcessorAlgorithm<SerialAlgorithm>;
using ParallelDataProcessorAlgorithm =
    GenericDataProcessorAlgorithm<ParallelAlgorithm>;
using DistributedDataProcessorAlgorithm =
    GenericDataProcessorAlgorithm<DistributedAlgorithm>;
150

151
152
} // namespace API
} // namespace Mantid