Mantid
Loading...
Searching...
No Matches
GroupingWorkspace.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#include <utility>
8
12
14
15using std::size_t;
16using namespace Mantid::API;
17
18namespace Mantid::DataObjects {
19namespace {
20const int UNSET_GROUP{-1};
21}
22// Register the workspace
24
25
29GroupingWorkspace::GroupingWorkspace(size_t numvectors) { this->init(numvectors, 1, 1); }
30
31//----------------------------------------------------------------------------------------------
37
38//----------------------------------------------------------------------------------------------
39
41 if (n == 0)
42 return UNSET_GROUP;
43 else
44 return n;
45}
46
55void GroupingWorkspace::makeDetectorIDToGroupMap(std::map<detid_t, int> &detIDToGroup, int64_t &ngroups) const {
56 ngroups = 0;
57 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
58 // Convert the Y value to a group number
59 auto group = this->translateToGroupID(static_cast<int>(this->readY(wi)[0]));
60 auto detIDs = this->getDetectorIDs(wi);
61 for (auto detID : detIDs) {
62 detIDToGroup[detID] = group;
63 if (group > ngroups)
64 ngroups = group;
65 }
66 }
67}
68
78void GroupingWorkspace::makeDetectorIDToGroupVector(std::vector<int> &detIDToGroup, int64_t &ngroups) const {
79 ngroups = 0;
80 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
81 // Convert the Y value to a group number
82 auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
83 auto detIDs = this->getDetectorIDs(wi);
84 for (auto detID : detIDs) {
85 // if you need negative detector ids, use the other function
86 if (detID < 0)
87 continue;
88 if (detIDToGroup.size() < static_cast<size_t>(detID + 1))
89 detIDToGroup.resize(detID + 1);
90 detIDToGroup[detID] = group;
91 if (group > ngroups)
92 ngroups = group;
93 }
94 }
95}
96
97std::vector<int> GroupingWorkspace::getGroupIDs() const {
98 // collect all the group numbers
99 std::set<int> groupIDs;
100 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
101 // Convert the Y value to a group number
102 auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
103 groupIDs.insert(group);
104 }
105 std::vector<int> output(groupIDs.begin(), groupIDs.end());
106 return output;
107}
108
110 // count distinct group numbers
111 std::vector<int> groups = this->getGroupIDs();
112 return static_cast<int>(groups.size());
113}
114
115std::vector<int> GroupingWorkspace::getDetectorIDsOfGroup(const int groupID) const {
116 std::vector<int> detectorIDs;
117 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
118 // Convert the Y value to a group number
119 const auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
120 if (group == groupID) {
121 // if the instrument isn't set no detector ids exist
122 const auto detIDs = this->getDetectorIDs(wi);
123 std::transform(detIDs.cbegin(), detIDs.cend(), std::back_inserter(detectorIDs),
124 [](const auto &detID) { return static_cast<int>(detID); });
125 }
126 }
127 return detectorIDs;
128}
129
130} // namespace Mantid::DataObjects
131
133
134namespace Mantid::Kernel {
135
136template <>
138IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_sptr>(const std::string &name) const {
139 auto *prop =
140 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
141 if (prop) {
142 return *prop;
143 } else {
144 std::string message =
145 "Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<GroupingWorkspace>.";
146 throw std::runtime_error(message);
147 }
148}
149
150template <>
152IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_const_sptr>(const std::string &name) const {
153 auto const *prop =
154 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
155 if (prop) {
156 return prop->operator()();
157 } else {
158 std::string message =
159 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<GroupingWorkspace>.";
160 throw std::runtime_error(message);
161 }
162}
163
164} // namespace Mantid::Kernel
165
std::string name
Definition Run.cpp:60
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:37
#define DECLARE_WORKSPACE(classname)
const MantidVec & readY(std::size_t const index) const
Deprecated, use y() instead.
A GroupingWorkspace is a subclass of Workspace2D where each spectrum has a single number entry,...
void makeDetectorIDToGroupVector(std::vector< int > &detIDToGroup, int64_t &ngroups) const
Fill a map where the index is detector ID and the value is the group number by using the values in Y.
void makeDetectorIDToGroupMap(std::map< detid_t, int > &detIDToGroup, int64_t &ngroups) const
Fill a map with key = detector ID, value = group number by using the values in Y.
std::vector< int > getDetectorIDsOfGroup(const int groupID) const
std::set< detid_t > getDetectorIDs(const std::size_t workspaceIndex) const
Return the detector ID at the given workspace index (i.e., spectrum/histogram index)
std::size_t getNumberHistograms() const override
Returns the histogram number.
The concrete, templated class for properties.
std::shared_ptr< const GroupingWorkspace > GroupingWorkspace_const_sptr
shared pointer to a const GroupingWorkspace
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.