CentroidPeaksMD.cpp 6.4 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

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

32
33
  std::vector<std::string> propOptions{"Q (lab frame)", "Q (sample frame)",
                                       "HKL"};
34
35
36
37
38
  declareProperty("CoordinatesToUse", "HKL",
                  boost::make_shared<StringListValidator>(propOptions),
                  "Ignored:  algorithm uses the InputWorkspace's coordinates.");

  declareProperty(
39
40
      make_unique<PropertyWithValue<double>>("PeakRadius", 1.0,
                                             Direction::Input),
41
42
43
      "Fixed radius around each peak position in which to calculate the "
      "centroid.");

44
45
  declareProperty(make_unique<WorkspaceProperty<PeaksWorkspace>>(
                      "PeaksWorkspace", "", Direction::Input),
46
47
48
                  "A PeaksWorkspace containing the peaks to centroid.");

  declareProperty(
49
50
      make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "",
                                                     Direction::Output),
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      "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)
74
    peakWS = inPeakWS->clone();
75
76
77
78
79

  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
80
                       "workspace, not CoordinatesToUse from input \n";
81
82
  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
83
                       "workspace, not CoordinatesToUse from input \n";
84
85
  else if (CoordinatesToUse == 3 && CoordinatesToUseStr != "HKL")
    g_log.warning() << "Warning: used HKL coordinates for MD workspace, not "
86
                       "CoordinatesToUse from input \n";
87
88
89
90
91

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

  // cppcheck-suppress syntaxError
92
    PRAGMA_OMP(parallel for schedule(dynamic, 10) )
93
    for (int i = 0; i < int(peakWS->getNumberPeaks()); ++i) {
94
      // Get a direct ref to that peak.
95
      IPeak &p = peakWS->getPeak(i);
96
      double detectorDistance = p.getL2();
97
98
99

      // Get the peak center as a position in the dimensions of the workspace
      V3D pos;
100
      if (CoordinatesToUse == 1) //"Q (lab frame)"
101
        pos = p.getQLabFrame();
102
      else if (CoordinatesToUse == 2) //"Q (sample frame)"
103
        pos = p.getQSampleFrame();
104
      else if (CoordinatesToUse == 3) //"HKL"
105
106
107
108
109
        pos = p.getHKL();

      // Build the sphere transformation
      bool dimensionsUsed[nd];
      coord_t center[nd];
110
      for (size_t d = 0; d < nd; ++d) {
111
        dimensionsUsed[d] = true; // Use all dimensions
112
        center[d] = static_cast<coord_t>(pos[d]);
113
114
115
116
      }
      CoordTransformDistance sphere(nd, center, dimensionsUsed);

      // Initialize the centroid to 0.0
117
      signal_t signal = 0;
118
      coord_t centroid[nd];
119
      for (size_t d = 0; d < nd; d++)
120
121
122
        centroid[d] = 0.0;

      // Perform centroid
123
124
125
      ws->getBox()->centroidSphere(
          sphere, static_cast<coord_t>(PeakRadius * PeakRadius), centroid,
          signal);
126
127

      // Normalize by signal
128
129
      if (signal != 0.0) {
        for (size_t d = 0; d < nd; d++)
130
          centroid[d] /= static_cast<coord_t>(signal);
131

132
133
134
        V3D vecCentroid(centroid[0], centroid[1], centroid[2]);

        // Save it back in the peak object, in the dimension specified.
135
        if (CoordinatesToUse == 1) //"Q (lab frame)"
136
        {
137
          p.setQLabFrame(vecCentroid, detectorDistance);
138
          p.findDetector();
139
        } else if (CoordinatesToUse == 2) //"Q (sample frame)"
140
        {
141
          p.setQSampleFrame(vecCentroid, detectorDistance);
142
          p.findDetector();
143
        } else if (CoordinatesToUse == 3) //"HKL"
144
        {
145
          p.setHKL(vecCentroid);
146
        }
147

148
        g_log.information() << "Peak " << i << " at " << pos << ": signal "
149
                            << signal << ", centroid " << vecCentroid << " in "
150
                            << CoordinatesToUse << '\n';
151
152
      } else {
        g_log.information() << "Peak " << i << " at " << pos
Hahn, Steven's avatar
Hahn, Steven committed
153
                            << " had no signal, and could not be centroided.\n";
154
155
156
      }
    }

157
158
    // Save the output
    setProperty("OutputWorkspace", peakWS);
159
}
160

161
162
163
164
165
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
 */
void CentroidPeaksMD::exec() {
  inWS = getProperty("InputWorkspace");
166

167
168
  CALL_MDEVENT_FUNCTION3(this->integrate, inWS);
}
169
170

} // namespace Mantid
171
} // namespace DataObjects