CentroidPeaksMD.cpp 6.84 KB
Newer Older
1
2
#include "MantidKernel/System.h"
#include "MantidKernel/ListValidator.h"
3
4
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
5
6
#include "MantidDataObjects/CoordTransformDistance.h"
#include "MantidDataObjects/MDEventFactory.h"
7
8
9
#include "MantidMDAlgorithms/IntegratePeaksMD.h"
#include "MantidMDAlgorithms/CentroidPeaksMD.h"

10
using Mantid::DataObjects::PeaksWorkspace;
11

12
13
14
15
16
17
18
19
20
21
namespace Mantid {
namespace MDAlgorithms {

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CentroidPeaksMD)

using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
22
using namespace Mantid::DataObjects;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

//----------------------------------------------------------------------------------------------
/** Constructor
 */
CentroidPeaksMD::CentroidPeaksMD() {}

//----------------------------------------------------------------------------------------------
/** Destructor
 */
CentroidPeaksMD::~CentroidPeaksMD() {}

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

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
 */
void CentroidPeaksMD::init() {
40
41
  declareProperty(make_unique<WorkspaceProperty<IMDEventWorkspace>>(
                      "InputWorkspace", "", Direction::Input),
42
43
                  "An input MDEventWorkspace.");

44
45
  std::vector<std::string> propOptions{"Q (lab frame)", "Q (sample frame)",
                                       "HKL"};
46
47
48
49
50
  declareProperty("CoordinatesToUse", "HKL",
                  boost::make_shared<StringListValidator>(propOptions),
                  "Ignored:  algorithm uses the InputWorkspace's coordinates.");

  declareProperty(
51
52
      make_unique<PropertyWithValue<double>>("PeakRadius", 1.0,
                                             Direction::Input),
53
54
55
      "Fixed radius around each peak position in which to calculate the "
      "centroid.");

56
57
  declareProperty(make_unique<WorkspaceProperty<PeaksWorkspace>>(
                      "PeaksWorkspace", "", Direction::Input),
58
59
60
                  "A PeaksWorkspace containing the peaks to centroid.");

  declareProperty(
61
62
      make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "",
                                                     Direction::Output),
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
      "The output PeaksWorkspace will be a copy of the input PeaksWorkspace "
      "with the peaks' positions modified by the new found centroids.");
}

//----------------------------------------------------------------------------------------------
/** Integrate the peaks of the workspace using parameters saved in the algorithm
 * class
 * @param ws ::  MDEventWorkspace to integrate
 */
template <typename MDE, size_t nd>
void CentroidPeaksMD::integrate(typename MDEventWorkspace<MDE, nd>::sptr ws) {
  if (nd != 3)
    throw std::invalid_argument("For now, we expect the input MDEventWorkspace "
                                "to have 3 dimensions only.");

  /// Peak workspace to centroid
  Mantid::DataObjects::PeaksWorkspace_sptr inPeakWS =
      getProperty("PeaksWorkspace");

  /// Output peaks workspace, create if needed
  Mantid::DataObjects::PeaksWorkspace_sptr peakWS =
      getProperty("OutputWorkspace");
  if (peakWS != inPeakWS)
86
    peakWS = inPeakWS->clone();
87
88
89
90
91

  std::string CoordinatesToUseStr = getPropertyValue("CoordinatesToUse");
  int CoordinatesToUse = ws->getSpecialCoordinateSystem();
  if (CoordinatesToUse == 1 && CoordinatesToUseStr != "Q (lab frame)")
    g_log.warning() << "Warning: used Q (lab frame) coordinates for MD "
Hahn, Steven's avatar
Hahn, Steven committed
92
                       "workspace, not CoordinatesToUse from input " << '\n';
93
94
  else if (CoordinatesToUse == 2 && CoordinatesToUseStr != "Q (sample frame)")
    g_log.warning() << "Warning: used Q (sample frame) coordinates for MD "
Hahn, Steven's avatar
Hahn, Steven committed
95
                       "workspace, not CoordinatesToUse from input " << '\n';
96
97
  else if (CoordinatesToUse == 3 && CoordinatesToUseStr != "HKL")
    g_log.warning() << "Warning: used HKL coordinates for MD workspace, not "
98
                       "CoordinatesToUse from input \n";
99
100
101
102
103

  /// Radius to use around peaks
  double PeakRadius = getProperty("PeakRadius");

  // cppcheck-suppress syntaxError
104
    PRAGMA_OMP(parallel for schedule(dynamic, 10) )
105
    for (int i = 0; i < int(peakWS->getNumberPeaks()); ++i) {
106
      // Get a direct ref to that peak.
107
      IPeak &p = peakWS->getPeak(i);
108
      double detectorDistance = p.getL2();
109
110
111

      // Get the peak center as a position in the dimensions of the workspace
      V3D pos;
112
      if (CoordinatesToUse == 1) //"Q (lab frame)"
113
        pos = p.getQLabFrame();
114
      else if (CoordinatesToUse == 2) //"Q (sample frame)"
115
        pos = p.getQSampleFrame();
116
      else if (CoordinatesToUse == 3) //"HKL"
117
118
119
120
121
        pos = p.getHKL();

      // Build the sphere transformation
      bool dimensionsUsed[nd];
      coord_t center[nd];
122
      for (size_t d = 0; d < nd; ++d) {
123
        dimensionsUsed[d] = true; // Use all dimensions
124
        center[d] = static_cast<coord_t>(pos[d]);
125
126
127
128
      }
      CoordTransformDistance sphere(nd, center, dimensionsUsed);

      // Initialize the centroid to 0.0
129
      signal_t signal = 0;
130
      coord_t centroid[nd];
131
      for (size_t d = 0; d < nd; d++)
132
133
134
        centroid[d] = 0.0;

      // Perform centroid
135
136
137
      ws->getBox()->centroidSphere(
          sphere, static_cast<coord_t>(PeakRadius * PeakRadius), centroid,
          signal);
138
139

      // Normalize by signal
140
141
      if (signal != 0.0) {
        for (size_t d = 0; d < nd; d++)
142
          centroid[d] /= static_cast<coord_t>(signal);
143

144
145
146
        V3D vecCentroid(centroid[0], centroid[1], centroid[2]);

        // Save it back in the peak object, in the dimension specified.
147
        if (CoordinatesToUse == 1) //"Q (lab frame)"
148
        {
149
          p.setQLabFrame(vecCentroid, detectorDistance);
150
          p.findDetector();
151
        } else if (CoordinatesToUse == 2) //"Q (sample frame)"
152
        {
153
          p.setQSampleFrame(vecCentroid, detectorDistance);
154
          p.findDetector();
155
        } else if (CoordinatesToUse == 3) //"HKL"
156
        {
157
          p.setHKL(vecCentroid);
158
        }
159

160
        g_log.information() << "Peak " << i << " at " << pos << ": signal "
161
                            << signal << ", centroid " << vecCentroid << " in "
162
                            << CoordinatesToUse << '\n';
163
164
165
      } else {
        g_log.information() << "Peak " << i << " at " << pos
                            << " had no signal, and could not be centroided."
166
                            << '\n';
167
168
169
      }
    }

170
171
    // Save the output
    setProperty("OutputWorkspace", peakWS);
172
}
173

174
175
176
177
178
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
 */
void CentroidPeaksMD::exec() {
  inWS = getProperty("InputWorkspace");
179

180
181
  CALL_MDEVENT_FUNCTION3(this->integrate, inWS);
}
182
183

} // namespace Mantid
184
} // namespace DataObjects