Mantid
Loading...
Searching...
No Matches
item_struct.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 "item_struct.h"
8#include <cstdlib>
9
10#define FAILURE 1
11#define SUCCESS 0
12
18template <typename T> int item_struct<T>::getItem(const std::string &item_name, T &value) {
19 int n;
20 long spec_no;
21 // handle case of item_spectrum which means we average the array
22 // item[ndet] for that particular spectrum
23 n = item_name.find('_');
24 if (n != std::string::npos) {
25 spec_no = atol(item_name.c_str() + n + 1);
26 return getItem(item_name.substr(0, n), &spec_no, 1, &value);
27 } else {
28 spec_no = 0;
29 return getItem(item_name, &spec_no, 0, &value);
30 }
31}
32
41template <typename T>
42int item_struct<T>::getItem(const std::string &item_name, const long *spec_array, int nspec, T *lVal) {
43 int i, j, n;
44 const T *pVal = NULL;
45 const item_t *item;
46 item = findItem(item_name, false);
47 if (item != NULL) {
48 for (j = 0; j < (nspec == 0 ? 1 : nspec); j++) {
49 lVal[j] = *(item->value);
50 }
51 return SUCCESS;
52 }
53 if (nspec == 0) {
54 return FAILURE;
55 }
56 item = findItem(item_name, true);
57 if (item != NULL) {
58 if (item->dim1 == 0) {
59 n = *(item->dim0);
60 } else {
61 n = *(item->dim0) * *(item->dim1);
62 }
63 if (n == m_ndet) {
64 pVal = item->value;
65 }
66 }
67 if (pVal == NULL) {
68 return FAILURE;
69 }
70 for (j = 0; j < nspec; j++) {
71 lVal[j] = 0;
72 n = 0;
73 for (i = 0; i < m_ndet; i++) {
74 if (m_spec_array[i] == spec_array[j]) {
75 lVal[j] += pVal[i];
76 n++;
77 }
78 }
79 if (n > 0) {
80 lVal[j] = lVal[j] / n;
81 }
82 }
83 return SUCCESS;
84}
85
92template <typename T> int item_struct<T>::getArrayItemSize(const std::string &item_name, int *dims_array, int &ndims) {
93 const item_t *item;
94 item = findItem(item_name, false);
95 if (item == NULL) {
96 item = findItem(item_name, true);
97 }
98 if (item != NULL) {
99 if (item->dim1 == 0) {
100 dims_array[0] = *(item->dim0);
101 ndims = 1;
102 } else {
103 dims_array[0] = *(item->dim0);
104 dims_array[1] = *(item->dim1);
105 ndims = 2;
106 }
107 return SUCCESS;
108 }
109 return FAILURE;
110}
111
118template <typename T> int item_struct<T>::getArrayItem(const std::string &item_name, int nspec, T *larray) {
119 const item_t *item;
120 item = findItem(item_name, false);
121 if (item == NULL) {
122 item = findItem(item_name, true);
123 }
124 if (item != NULL) {
125 int n;
126 if (item->dim1 == 0) {
127 n = *(item->dim0);
128 } else {
129 n = *(item->dim0) * *(item->dim1);
130 }
131 for (int k = 0; k < nspec; k++) {
132 for (int j = 0; j < n; j++) {
133 larray[j + k * n] = item->value[j];
134 }
135 }
136 return SUCCESS;
137 }
138 return FAILURE;
139}
140
146template <typename T> int item_struct<T>::getArrayItem(const std::string &item_name, T *larray) {
147 int n;
148 long spec_no;
149 n = item_name.find('_');
150 if (n != std::string::npos) {
151 spec_no = atol(item_name.c_str() + n + 1);
152 return getIntArrayItem(item_name.substr(0, n), &spec_no, 1, larray);
153 } else {
154 spec_no = 0;
155 return getIntArrayItem(item_name, &spec_no, 1, larray);
156 }
157}
double value
The value of the point.
Definition: FitMW.cpp:51
PyObject * getItem(WorkspaceGroup &self, const int &index)
int getArrayItem(const std::string &item_name, int nspec, T *larray)
Gets an array of items.
int getArrayItemSize(const std::string &item_name, int *dims_array, int &ndims)
Gets the size of an array of items.
Definition: item_struct.cpp:92
int getItem(const std::string &item_name, T &value)
Gets an item.
Definition: item_struct.cpp:18
#define FAILURE
Definition: item_struct.cpp:10
#define SUCCESS
Definition: item_struct.cpp:11
structure to hold a dae item
Definition: item_struct.h:18
const int * dim1
dimension one array
Definition: item_struct.h:22
const T * value
array of type T
Definition: item_struct.h:19
const int * dim0
dimension zero array
Definition: item_struct.h:21