DataProcessorAlgorithm.h 6.53 KB
Newer Older
1
2
3
4
5
#ifndef MANTID_API_DATAPROCESSORALGORITHM_H_
#define MANTID_API_DATAPROCESSORALGORITHM_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
6
#include "MantidAPI/AlgorithmManager.h"
7
#include "MantidAPI/DistributedAlgorithm.h"
8
#include "MantidAPI/IEventWorkspace_fwd.h"
9
10
11
#include "MantidAPI/ITableWorkspace_fwd.h"
#include "MantidAPI/ParallelAlgorithm.h"
#include "MantidAPI/SerialAlgorithm.h"
12
#include "MantidKernel/PropertyManager.h"
13
14
#include <vector>

15
16
namespace Mantid {
namespace API {
17
18
/**

19
20
   Data processor algorithm to be used as a parent to workflow algorithms.
   This algorithm provides utility methods to load and process data.
21
22
23

   @date 2012-04-04

24
25
   Copyright &copy; 2012 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
   National Laboratory & European Spallation Source
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

   This file is part of Mantid.

   Mantid is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   Mantid is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

42
   File change history is stored at: <https://github.com/mantidproject/mantid>
43
44
   Code Documentation is available at: <http://doxygen.mantidproject.org>
 */
45
template <class Base> class GenericDataProcessorAlgorithm : public Base {
46
public:
47
  GenericDataProcessorAlgorithm();
48
  std::string getPropertyValue(const std::string &name) const override;
49
50
  Kernel::PropertyManagerOwner::TypedValue
  getProperty(const std::string &name) const override;
51
52

protected:
53
  boost::shared_ptr<Algorithm> createChildAlgorithm(
54
55
      const std::string &name, const double startProgress = -1.,
      const double endProgress = -1., const bool enableLogging = true,
56
      const int &version = -1) override;
57
58
59
  void setLoadAlg(const std::string &alg);
  void setLoadAlgFileProp(const std::string &filePropName);
  void setAccumAlg(const std::string &alg);
60
  void setPropManagerPropName(const std::string &propName);
61
62
63
  void mapPropertyName(const std::string &nameInProp,
                       const std::string &nameInPropManager);
  void copyProperty(API::Algorithm_sptr alg, const std::string &name);
64
65
  virtual ITableWorkspace_sptr determineChunk(const std::string &filename);
  virtual MatrixWorkspace_sptr loadChunk(const size_t rowIndex);
66
67
68
  Workspace_sptr load(const std::string &inputData,
                      const bool loadQuiet = false);
  std::vector<std::string> splitInput(const std::string &input);
69
  void forwardProperties();
70
71
  boost::shared_ptr<Kernel::PropertyManager> getProcessProperties(
      const std::string &propertyManager = std::string()) const;
72
73
  /// MPI option. If false, we will use one job event if MPI is available
  bool m_useMPI;
74
  Workspace_sptr assemble(Workspace_sptr partialWS);
75
76
77
78
  Workspace_sptr assemble(const std::string &partialWSName,
                          const std::string &outputWSName);
  void saveNexus(const std::string &outputWSName,
                 const std::string &outputFile);
79
80
  bool isMainThread();
  int getNThreads();
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  /// 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);
99
  /// Add a single value to a matrix workspace
100
101
102
103
104
105
106
107
108
  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);
109

110
private:
111
112
113
  template <typename LHSType, typename RHSType, typename ResultType>
  ResultType executeBinaryAlgorithm(const std::string &algorithmName,
                                    const LHSType lhs, const RHSType rhs) {
114
115
116
    auto alg = createChildAlgorithm(algorithmName);
    alg->initialize();

117
118
    alg->template setProperty<LHSType>("LHSWorkspace", lhs);
    alg->template setProperty<RHSType>("RHSWorkspace", rhs);
119
120
    alg->execute();

121
    if (alg->isExecuted()) {
122
123
      // Get the output workspace property
      return alg->getProperty("OutputWorkspace");
124
    } else {
125
126
127
128
129
130
      std::string message = "Error while executing operation: " + algorithmName;
      throw std::runtime_error(message);
    }
  }

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

133
134
135
136
  /// 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;
137
138
  /// An alternate filename property for the load algorithm
  std::string m_loadAlgFileProp;
139
140
141
  /// The name of the parameter that names the property manager. The default
  /// value is "ReductionProperties".
  std::string m_propertyManagerPropertyName;
142
143
  /// Map property names to names in supplied properties manager
  std::map<std::string, std::string> m_nameToPMName;
144
145
};

146
147
148
149
150
151
152
typedef GenericDataProcessorAlgorithm<Algorithm> DataProcessorAlgorithm;
typedef GenericDataProcessorAlgorithm<SerialAlgorithm>
    SerialDataProcessorAlgorithm;
typedef GenericDataProcessorAlgorithm<ParallelAlgorithm>
    ParallelDataProcessorAlgorithm;
typedef GenericDataProcessorAlgorithm<DistributedAlgorithm>
    DistributedDataProcessorAlgorithm;
153

154
155
156
} // namespace API
} // namespace Mantid

157
#endif /* MANTID_API_DATAPROCESSORALGORITHM_H_ */