Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultiArray.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MultiArray.h
1 
2 #ifndef MULTIARRAY_H
3 #define MULTIARRAY_H
4 
5 #include <cassert>
6 #include <cstdio> // for printf
7 #include <cstdlib> // for malloc
8 
9 template <class T>
10 class MultiArray
11 {
12  // class to hold an up-to-six dimensional array of whatever T is. Any indices not used are flattened. This should probably be replaced with sets of TH3s... but the intention was to take advantage of indices for all elements being the same, to avoid unpacking and re-packing TVectors, etc, and to get rid of any other overhead that might be showing up in the TH3 implementation.
13  // it does make it more annoying to interpolate, though.
14  public:
15  static const int MAX_DIM = 6;
16  int dim;
17  int n[6];
18  long int length;
19  T *field;
20 
21  MultiArray(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0)
22  {
23  int n_[6];
24  for (int i = 0; i < MAX_DIM; i++)
25  n[i] = 0;
26  n_[0] = a;
27  n_[1] = b;
28  n_[2] = c;
29  n_[3] = d;
30  n_[4] = e;
31  n_[5] = f;
32  length = 1;
33  dim = MAX_DIM;
34  for (int i = 0; i < dim; i++)
35  {
36  if (n_[i] < 1)
37  {
38  dim = i;
39  break;
40  }
41  n[i] = n_[i];
42  length *= n[i];
43  }
44  field = static_cast<T *>(malloc(length * sizeof(T)));
45  // note that since we don't know what T is, we can't safely zero it. Someone else will have to do that.
46  }
48  explicit MultiArray(const MultiArray &) = delete;
49  MultiArray &operator=(const MultiArray &) = delete;
50 
52  {
53  free(field);
54  }
55 
56  void Add(int a, int b, int c, T in)
57  {
58  Add(a, b, c, 0, 0, 0, in);
59  return;
60  };
61 
62  void Add(int a, int b, int c, int d, int e, int f, T in)
63  {
64  int n_[6];
65  n_[0] = a;
66  n_[1] = b;
67  n_[2] = c;
68  n_[3] = d;
69  n_[4] = e;
70  n_[5] = f;
71  long int index = n_[0];
72  for (int i = 1; i < dim; i++)
73  {
74  index = (index * n[i]) + n_[i];
75  }
76  field[index] = field[index] + in;
77  return;
78  }
79 
80  T Get(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0)
81  {
82  int n_[6];
83  n_[0] = a;
84  n_[1] = b;
85  n_[2] = c;
86  n_[3] = d;
87  n_[4] = e;
88  n_[5] = f;
89  long int index = 0;
90  for (int i = 0; i < dim; i++)
91  {
92  if (n[i] <= n_[i] || n_[i] < 0)
93  { // check bounds
94  printf("asking for el %d %d %d %d %d %d. %dth element is outside of bounds 0<x<%d\n", n_[0], n_[1], n_[2], n_[3], n_[4], n_[5], n_[i], n[i]);
95  assert(false);
96  }
97  index = (index * n[i]) + n_[i];
98  }
99  return field[index];
100  }
101 
102  T *GetPtr(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0)
103  { // faster for repeated access.
104  int n_[6];
105  n_[0] = a;
106  n_[1] = b;
107  n_[2] = c;
108  n_[3] = d;
109  n_[4] = e;
110  n_[5] = f;
111  long int index = n_[0];
112  for (int i = 1; i < dim; i++)
113  {
114  index = (index * n[i]) + n_[i];
115  }
116  return &(field[index]);
117  }
118 
119  T *GetFlat(int a = 0)
120  { // get the value at position a in the 1D equivalent, assuming the math is done elsewhere, or we're just going straight through the thing.
121  if (a < 0 || a >= length)
122  {
123  printf("tried to seek element %d of multiarray, but bounds are 0<a<%ld\n", a, length);
124  assert(a < 0 || a >= length); // check bounds
125  }
126  return &(field[a]);
127  }
128 
129  int Length()
130  {
131  return (int) length;
132  }
133 
134  void Set(int a, int b, int c, T in)
135  {
136  Set(a, b, c, 0, 0, 0, in);
137  return;
138  };
139 
140  void Set(int a, int b, int c, int d, int e, int f, T in)
141  {
142  int n_[6];
143  n_[0] = a;
144  n_[1] = b;
145  n_[2] = c;
146  n_[3] = d;
147  n_[4] = e;
148  n_[5] = f;
149  long int index = n_[0];
150  for (int i = 1; i < dim; i++)
151  {
152  index = (index * n[i]) + n_[i];
153  }
154  field[index] = in;
155  return;
156  }
157 
158  void SetAll(T in)
159  {
160  // this assumes there's an '=' operator for T, but that's generally true.
161  for (long int i = 0; i < length; i++)
162  {
163  field[i] = in;
164  }
165  return;
166  }
167 };
168 #endif // MULTIARRAY_H