Mantid
Loading...
Searching...
No Matches
GroupDetectors.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12#include "MantidHistogramData/HistogramMath.h"
14#include <numeric>
15#include <set>
16
17namespace Mantid::DataHandling {
18// Register the algorithm into the algorithm factory
19DECLARE_ALGORITHM(GroupDetectors)
20
21using namespace Kernel;
22using namespace API;
23
26 std::make_unique<WorkspaceProperty<>>("Workspace", "", Direction::InOut, std::make_shared<CommonBinsValidator>()),
27 "The name of the workspace2D on which to perform the algorithm");
28
29 declareProperty(std::make_unique<ArrayProperty<specnum_t>>("SpectraList"),
30 "An array containing a list of the indexes of the spectra to combine\n"
31 "(DetectorList and WorkspaceIndexList are ignored if this is set)");
32
33 declareProperty(std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
34 "An array of detector ID's (WorkspaceIndexList is ignored if this is\n"
35 "set)");
36
37 declareProperty(std::make_unique<ArrayProperty<size_t>>("WorkspaceIndexList"),
38 "An array of workspace indices to combine");
39
40 declareProperty("ResultIndex", -1, "The workspace index of the summed spectrum (or -1 on error)", Direction::Output);
41}
42
44 // Get the input workspace
45 const MatrixWorkspace_sptr WS = getProperty("Workspace");
46
47 std::vector<size_t> indexList = getProperty("WorkspaceIndexList");
48 std::vector<specnum_t> spectraList = getProperty("SpectraList");
49 const std::vector<detid_t> detectorList = getProperty("DetectorList");
50
51 // Could create a Validator to replace the below
52 if (indexList.empty() && spectraList.empty() && detectorList.empty()) {
53 g_log.information(name() + ": WorkspaceIndexList, SpectraList, and "
54 "DetectorList properties are all empty, no "
55 "grouping done");
56 return;
57 }
58
59 // If the spectraList property has been set, need to loop over the workspace
60 // looking for the appropriate spectra number and adding the indices they
61 // are linked to the list to be processed
62 if (!spectraList.empty()) {
63 indexList = WS->getIndicesFromSpectra(spectraList);
64 } // End dealing with spectraList
65 else if (!detectorList.empty()) {
66 // Dealing with DetectorList
67 // convert from detectors to workspace indices
68 indexList = WS->getIndicesFromDetectorIDs(detectorList);
69 }
70
71 if (indexList.empty()) {
72 g_log.warning("Nothing to group");
73 return;
74 }
75
76 const auto firstIndex = static_cast<specnum_t>(indexList[0]);
77 auto &firstSpectrum = WS->getSpectrum(firstIndex);
78 setProperty("ResultIndex", firstIndex);
79
80 // loop over the spectra to group
81 Progress progress(this, 0.0, 1.0, static_cast<int>(indexList.size() - 1));
82
83 auto outputHisto = firstSpectrum.histogram();
84
85 for (size_t i = 0; i < indexList.size() - 1; ++i) {
86 // The current spectrum
87 const size_t currentIndex = indexList[i + 1];
88 auto &spec = WS->getSpectrum(currentIndex);
89
90 // Add the current detector to belong to the first spectrum
91 firstSpectrum.addDetectorIDs(spec.getDetectorIDs());
92
93 // Add up all the Y spectra and store the result in the first one
94 outputHisto += spec.histogram();
95
96 // Now zero the now redundant spectrum and set its spectraNo to indicate
97 // this (using -1)
98 spec.clearData();
99 spec.setSpectrumNo(-1);
100 spec.clearDetectorIDs();
101 progress.report();
102 }
103
104 firstSpectrum.setHistogram(outputHisto);
105}
106
107} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IntArray detectorList
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Virtual method - must be overridden by concrete algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Output
An output workspace.
Definition: Property.h:54