-
Notifications
You must be signed in to change notification settings - Fork 122
/
SumNeighbours.cpp
112 lines (94 loc) · 3.9 KB
/
SumNeighbours.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/SumNeighbours.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/ICompAssembly.h"
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/BoundedValidator.h"
#include <boost/algorithm/string.hpp>
namespace Mantid {
namespace Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(SumNeighbours)
using namespace Kernel;
using namespace Geometry;
using namespace API;
using namespace DataObjects;
/** Initialisation method.
*
*/
void SumNeighbours::init() {
declareProperty(
std::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input,
std::make_shared<InstrumentValidator>()),
"A workspace containing one or more rectangular area "
"detectors. Each spectrum needs to correspond to only one "
"pixelID (e.g. no grouping or previous calls to "
"SumNeighbours).");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The name of the workspace to be created as the output of "
"the algorithm.");
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty("SumX", 4, mustBePositive,
"The number of X (horizontal) pixels to sum together. This "
"must evenly divide the number of X pixels in a detector.");
declareProperty("SumY", 4, mustBePositive,
"The number of Y (vertical) pixels to sum together. This "
"must evenly divide the number of Y pixels in a detector");
}
/** Executes the algorithm
*
*/
void SumNeighbours::exec() {
// Try and retrieve the optional properties
int SumX = getProperty("SumX");
int SumY = getProperty("SumY");
// Get the input workspace
Mantid::API::MatrixWorkspace_sptr inWS = getProperty("InputWorkspace");
const auto &spectrumInfo = inWS->spectrumInfo();
const auto &det = spectrumInfo.detector(0);
// Check if grandparent is rectangular detector
std::shared_ptr<const Geometry::IComponent> parent = det.getParent();
std::shared_ptr<const RectangularDetector> rect;
if (parent) {
rect = std::dynamic_pointer_cast<const RectangularDetector>(
parent->getParent());
}
Mantid::API::MatrixWorkspace_sptr outWS;
Progress progress(this, 0.0, 1.0, 2);
progress.report("Smoothing Neighbours...");
IAlgorithm_sptr smooth = createChildAlgorithm("SmoothNeighbours");
smooth->setProperty("InputWorkspace", inWS);
if (rect) {
smooth->setProperty("SumPixelsX", SumX);
smooth->setProperty("SumPixelsY", SumY);
} else {
smooth->setProperty<std::string>("RadiusUnits", "NumberOfPixels");
smooth->setProperty("Radius",
static_cast<double>(SumX * SumY * SumX * SumY));
smooth->setProperty("NumberOfNeighbours", SumX * SumY * SumX * SumY * 4);
smooth->setProperty("SumNumberOfNeighbours", SumX * SumY);
}
smooth->executeAsChildAlg();
progress.report();
// Get back the result
outWS = smooth->getProperty("OutputWorkspace");
// Cast to the matrixOutputWS and save it
this->setProperty("OutputWorkspace", outWS);
}
} // namespace Algorithms
} // namespace Mantid