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 bool includeUnsetGroup) 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 if (!includeUnsetGroup && group == UNSET_GROUP)
104 continue;
105 groupIDs.insert(group);
106 }
107 std::vector<int> output(groupIDs.begin(), groupIDs.end());
108 return output;
109}
110
112 // count distinct group numbers
113 std::vector<int> groups = this->getGroupIDs();
114 return static_cast<int>(groups.size());
115}
116
117std::vector<detid_t> GroupingWorkspace::getDetectorIDsOfGroup(const int groupID) const {
118 std::vector<detid_t> detectorIDs;
119 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
120 // Convert the Y value to a group number
121 const auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
122 if (group == groupID) {
123 // if the instrument isn't set no detector ids exist
124 const auto detIDs = this->getDetectorIDs(wi);
125 std::transform(detIDs.cbegin(), detIDs.cend(), std::back_inserter(detectorIDs),
126 [](const auto &detID) { return static_cast<int>(detID); });
127 }
128 }
129 return detectorIDs;
130}
131
132} // namespace Mantid::DataObjects
133
135
136namespace Mantid::Kernel {
137
138template <>
140IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_sptr>(const std::string &name) const {
141 auto *prop =
142 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
143 if (prop) {
144 return *prop;
145 } else {
146 std::string message =
147 "Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<GroupingWorkspace>.";
148 throw std::runtime_error(message);
149 }
150}
151
152template <>
154IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_const_sptr>(const std::string &name) const {
155 auto const *prop =
156 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
157 if (prop) {
158 return prop->operator()();
159 } else {
160 std::string message =
161 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<GroupingWorkspace>.";
162 throw std::runtime_error(message);
163 }
164}
165
166} // namespace Mantid::Kernel
167
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< detid_t > getDetectorIDsOfGroup(const int groupID) const
std::vector< int > getGroupIDs(const bool includeUnsetGroup=true) 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.