forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupingWorkspace.cpp
128 lines (114 loc) · 4.32 KB
/
GroupingWorkspace.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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 +
#include <utility>
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/GroupingWorkspace.h"
#include "MantidKernel/IPropertyManager.h"
#include "MantidKernel/System.h"
using std::size_t;
using namespace Mantid::API;
namespace Mantid::DataObjects {
// Register the workspace
DECLARE_WORKSPACE(GroupingWorkspace)
/** Constructor, buiding with give dimensions
* @param numvectors: input size of the vector/histogram number for this
* workspace
* @return created GroupingWorkspace
*/
GroupingWorkspace::GroupingWorkspace(size_t numvectors) { this->init(numvectors, 1, 1); }
//----------------------------------------------------------------------------------------------
/** Constructor, building from an instrument
*
* @param inst :: input instrument that is the base for this workspace
* @return created GroupingWorkspace
*/
GroupingWorkspace::GroupingWorkspace(const Geometry::Instrument_const_sptr &inst) : SpecialWorkspace2D(inst) {}
//----------------------------------------------------------------------------------------------
/** Fill a map with key = detector ID, value = group number
* by using the values in Y.
* Group values of 0 are converted to -1.
*
* @param detIDToGroup :: ref. to map to fill
* @param[out] ngroups :: the number of groups found (equal to the largest group
*number found)
*/
void GroupingWorkspace::makeDetectorIDToGroupMap(std::map<detid_t, int> &detIDToGroup, int64_t &ngroups) const {
ngroups = 0;
for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
// Convert the Y value to a group number
auto group = static_cast<int>(this->readY(wi)[0]);
if (group == 0)
group = -1;
auto detIDs = this->getDetectorIDs(wi);
for (auto detID : detIDs) {
detIDToGroup[detID] = group;
if (group > ngroups)
ngroups = group;
}
}
}
/**
* Fill a map where the index is detector ID and the value is the
* group number by using the values in Y.
* Group values of 0 are converted to -1.
*
* @param detIDToGroup :: ref. to map to fill
* @param[out] ngroups :: the number of groups found (equal to the largest group
*number found)
*/
void GroupingWorkspace::makeDetectorIDToGroupVector(std::vector<int> &detIDToGroup, int64_t &ngroups) const {
ngroups = 0;
for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
// Convert the Y value to a group number
auto group = static_cast<int>(this->y(wi).front());
if (group == 0)
group = -1;
auto detIDs = this->getDetectorIDs(wi);
for (auto detID : detIDs) {
// if you need negative detector ids, use the other function
if (detID < 0)
continue;
if (detIDToGroup.size() < static_cast<size_t>(detID + 1))
detIDToGroup.resize(detID + 1);
detIDToGroup[detID] = group;
if (group > ngroups)
ngroups = group;
}
}
}
} // namespace Mantid::DataObjects
///\cond TEMPLATE
namespace Mantid::Kernel {
template <>
DLLExport Mantid::DataObjects::GroupingWorkspace_sptr
IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_sptr>(const std::string &name) const {
auto *prop =
dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
if (prop) {
return *prop;
} else {
std::string message =
"Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<GroupingWorkspace>.";
throw std::runtime_error(message);
}
}
template <>
DLLExport Mantid::DataObjects::GroupingWorkspace_const_sptr
IPropertyManager::getValue<Mantid::DataObjects::GroupingWorkspace_const_sptr>(const std::string &name) const {
auto *prop =
dynamic_cast<PropertyWithValue<Mantid::DataObjects::GroupingWorkspace_sptr> *>(getPointerToProperty(name));
if (prop) {
return prop->operator()();
} else {
std::string message =
"Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<GroupingWorkspace>.";
throw std::runtime_error(message);
}
}
} // namespace Mantid::Kernel
///\endcond TEMPLATE