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
11#include "MantidDataObjects/DllConfig.h"
13
15
16using std::size_t;
17using namespace Mantid::API;
18
19namespace Mantid::DataObjects {
20namespace {
21const int UNSET_GROUP{-1};
22}
23// Register the workspace
25
26
30GroupingWorkspace::GroupingWorkspace(size_t numvectors) { this->init(numvectors, 1, 1); }
31
32//----------------------------------------------------------------------------------------------
38
39//----------------------------------------------------------------------------------------------
40
42 if (n == 0)
43 return UNSET_GROUP;
44 else
45 return n;
46}
47
56void GroupingWorkspace::makeDetectorIDToGroupMap(std::map<detid_t, int> &detIDToGroup, int64_t &ngroups) const {
57 ngroups = 0;
58 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
59 // Convert the Y value to a group number
60 auto group = this->translateToGroupID(static_cast<int>(this->y(wi)[0]));
61 auto detIDs = this->getDetectorIDs(wi);
62 for (auto detID : detIDs) {
63 detIDToGroup[detID] = group;
64 if (group > ngroups)
65 ngroups = group;
66 }
67 }
68}
69
79void GroupingWorkspace::makeDetectorIDToGroupVector(std::vector<int> &detIDToGroup, int64_t &ngroups) const {
80 ngroups = 0;
81 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
82 // Convert the Y value to a group number
83 auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
84 auto detIDs = this->getDetectorIDs(wi);
85 for (auto detID : detIDs) {
86 // if you need negative detector ids, use the other function
87 if (detID < 0)
88 continue;
89 if (detIDToGroup.size() < static_cast<size_t>(detID + 1))
90 detIDToGroup.resize(detID + 1);
91 detIDToGroup[detID] = group;
92 if (group > ngroups)
93 ngroups = group;
94 }
95 }
96}
97
98std::vector<int> GroupingWorkspace::getGroupIDs(const bool includeUnsetGroup) const {
99 // collect all the group numbers
100 std::set<int> groupIDs;
101 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
102 // Convert the Y value to a group number
103 auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
104 if (!includeUnsetGroup && group == UNSET_GROUP)
105 continue;
106 groupIDs.insert(group);
107 }
108 std::vector<int> output(groupIDs.begin(), groupIDs.end());
109 return output;
110}
111
113 // count distinct group numbers
114 std::vector<int> groups = this->getGroupIDs();
115 return static_cast<int>(groups.size());
116}
117
118std::vector<detid_t> GroupingWorkspace::getDetectorIDsOfGroup(const int groupID) const {
119 std::vector<detid_t> detectorIDs;
120 for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
121 // Convert the Y value to a group number
122 const auto group = this->translateToGroupID(static_cast<int>(this->y(wi).front()));
123 if (group == groupID) {
124 // if the instrument isn't set no detector ids exist
125 const auto detIDs = this->getDetectorIDs(wi);
126 std::transform(detIDs.cbegin(), detIDs.cend(), std::back_inserter(detectorIDs),
127 [](const auto &detID) { return static_cast<int>(detID); });
128 }
129 }
130 return detectorIDs;
131}
132
133} // namespace Mantid::DataObjects
134
136
137namespace Mantid::Kernel {
138
139template <>
141IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_sptr>(const std::string &name) const {
142 auto *prop =
143 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
144 if (prop) {
145 return *prop;
146 } else {
147 std::string message =
148 "Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<GroupingWorkspace>.";
149 throw std::runtime_error(message);
150 }
151}
152
153template <>
155IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_const_sptr>(const std::string &name) const {
156 auto const *prop =
157 dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
158 if (prop) {
159 return prop->operator()();
160 } else {
161 std::string message =
162 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<GroupingWorkspace>.";
163 throw std::runtime_error(message);
164 }
165}
166
167} // namespace Mantid::Kernel
168
std::string name
Definition Run.cpp:60
#define DECLARE_WORKSPACE(classname)
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.