ATLAS Offline Software
EMAPMatrix.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //#include <assert.h>
6 /********************************************************************************************/
7 /// Public Implementations
8 /********************************************************************************************/
9 
10 // helper class
11 
12 inline unsigned int EMAPMatrixAxis::getNumberOfBins() const
13 {
14  return m_vecBins.size()-1;
15 }
16 
17 inline int EMAPMatrixAxis::getBinIndex(double x) const
18 {
19  if ((x<m_vecBins[0]) || (m_vecBins[m_vecBins.size()-1]<x)) return -1;
20  for (unsigned int i=0; i<m_vecBins.size(); i++) {
21  if ((m_vecBins[i]<=x) && (x<m_vecBins[i+1])) {
22  return (int)i;
23  }
24  }
25  return -1;
26 }
27 
28 inline bool EMAPMatrixAxis::isInRange(double x) const
29 {
30  return !((x<m_vecBins[0]) || (m_vecBins[m_vecBins.size()-1]<x));
31 }
32 
33 // double EMAPMatrixAxis::getMeanOfBin(unsigned int i) const
34 // {
35 // if (i>=m_vecBins.size()-1) return -1.0;
36 // return 0.5*(m_vecBins[i+1]-m_vecBins[i])+m_vecBins[i];
37 // }
38 
39 // double EMAPMatrixAxis::getWidthOfBin(unsigned int i) const
40 // {
41 // if (i>=m_vecBins.size()-1) return -1.0;
42 // return (m_vecBins[i+1]-m_vecBins[i]);
43 // }
44 
45 
46 // void EMAPMatrixAxis::setName(std::string name)
47 // {
48 // m_name = name;
49 // }
50 
51 
52 // now the real class
53 
54 template <class T>
55 EMAPMatrix<T>::EMAPMatrix()
56 {
57  // m_emptyObject = 0;
58  clear();
59 }
60 
61 
62 template <class T>
63 EMAPMatrix<T>::EMAPMatrix(const std::vector<EMAPMatrixAxis>& axes, const std::string& textDescription)
64 {
65  // m_emptyObject = 0;
66  StatusCode sc = setupMatrix(axes);
67  if (sc.isFailure()) {
68  throw std::invalid_argument("Could not set up the EMAPMatrix.");
69  }
70  setTextDescription(textDescription);
71 }
72 
73 
74 // template <class T>
75 // EMAPMatrix<T>::EMAPMatrix(std::vector<EMAPMatrixAxis> axes, T emptyObject)
76 // {
77 // // m_emptyObject = 0;
78 // setupMatrix(axes, emptyObject);
79 // }
80 
81 
82 template <class T>
83 StatusCode EMAPMatrix<T>::setupMatrix(std::vector<EMAPMatrixAxis> axes)
84 {
85 
86  m_axis.clear();
87  m_dimensions=axes.size();
88 
89  for (unsigned int i=0;i<m_dimensions;++i)
90  {
91  m_axis.push_back(axes.at(i));
92  }
93  return setupEntries();
94 }
95 
96 
97 template <class T>
98 EMAPMatrix<T>::~EMAPMatrix()
99 {
100  clear();
101 }
102 
103 
104 template <class T>
105 void EMAPMatrix<T>::clear()
106 {
107  m_dimensions = 0;
108  m_axis.clear();
109  m_base.clear();
110  m_matrix.clear();
111  m_textDescription = "";
112 }
113 
114 
115 template <class T>
116 unsigned int EMAPMatrix<T>::Index(const std::vector<unsigned int>& x) const
117 {
118  unsigned int index=0;
119  //assert(x.size()==m_dimensions);
120  if (x.size()!=m_dimensions)
121  {
122  std::cout<<"EMAPMatrix ERROR: Illegal Acces of Matrix "<<std::endl;
123  return index;
124  }
125 
126  for (unsigned int i=0;i<m_dimensions;++i)
127  {
128  //assert(x.at(i)<=m_axis.at(i).getNumberOfBins());
129  if (!(x.at(i)<=m_axis.at(i).getNumberOfBins()))
130  {
131  std::cout<<"EMAPMatrix ERROR: Illegal Acces of Matrix "<<std::endl;
132  return index;
133  }
134  index+=m_base.at(i)*x.at(i);
135  }
136  return index;
137 }
138 
139 template <class T>
140 std::vector<unsigned int> EMAPMatrix<T>::Index(unsigned int id)
141 {
142  std::vector<unsigned int> x;
143  for (int i=(int)(m_dimensions)-1;i>=0;i--) x.push_back(0);
144  //assert(id<m_matrix.size());
145  if (!(id<m_matrix.size()))
146  {
147  std::cout<<"EMAPMatrix ERROR: Illegal Acces of Matrix "<<std::endl;
148  return x;
149  }
150 
151  for (int i=m_dimensions-1;i>0;i--)
152  {
153  x[i]=int((id*1.0)/(m_base.at(i)*1.0));
154  id-=x[i]*m_base.at(i);
155  }
156  x[0]=id;
157  return x;
158 }
159 
160 
161 // template <class T>
162 // T& EMAPMatrix<T>::Access(std::vector<unsigned int> x)
163 // {
164 // return m_matrix[Index(x)];
165 // }
166 
167 
168 template <class T>
169 // cppcheck-suppress passedByValue
170 StatusCode EMAPMatrix<T>::setBinContent(const std::vector<double>& x, T value)
171 {
172  std::vector<unsigned int> id;
173  if (x.size()==m_dimensions) {
174  for (unsigned int i=0;i<m_dimensions;++i) {
175  int bi=m_axis.at(i).getBinIndex(x.at(i));
176  if (bi>=0) {
177  id.push_back(bi);
178  } else {
179  return StatusCode::FAILURE;
180  }
181  }
182  m_matrix[Index(id)]=value;
183  return StatusCode::SUCCESS;
184  } else {
185  return StatusCode::FAILURE;
186  }
187 }
188 
189 template <class T>
190 // cppcheck-suppress passedByValue
191 StatusCode EMAPMatrix<T>::setBinContent(double x, T value)
192 {
193  if (m_dimensions!=1) return StatusCode::FAILURE;;
194  std::vector<double> vec;
195  vec.push_back(x);
196  return setBinContent(vec, value);
197 }
198 
199 template <class T>
200 // cppcheck-suppress passedByValue
201 StatusCode EMAPMatrix<T>::setBinContent(double x, double y, T value)
202 {
203  if (m_dimensions!=2) return StatusCode::FAILURE;;
204  std::vector<double> vec;
205  vec.push_back(x);
206  vec.push_back(y);
207  return setBinContent(vec, value);
208 }
209 
210 template <class T>
211 // cppcheck-suppress passedByValue
212 StatusCode EMAPMatrix<T>::setBinContent(double x, double y, double z, T value)
213 {
214  if (m_dimensions!=3) return StatusCode::FAILURE;;
215  std::vector<double> vec;
216  vec.push_back(x);
217  vec.push_back(y);
218  vec.push_back(z);
219  return setBinContent(vec, value);
220 }
221 
222 template <class T>
223 // cppcheck-suppress passedByValue
224 StatusCode EMAPMatrix<T>::setBinContent(double x, double y, double z, double u, T value)
225 {
226  if (m_dimensions!=4) return StatusCode::FAILURE;;
227  std::vector<double> vec;
228  vec.push_back(x);
229  vec.push_back(y);
230  vec.push_back(z);
231  vec.push_back(u);
232  return setBinContent(vec, value);
233 }
234 
235 template <class T>
236 // cppcheck-suppress passedByValue
237 StatusCode EMAPMatrix<T>::setBinContent(double x, double y, double z, double u, double v, T value)
238 {
239  if (m_dimensions!=5) return StatusCode::FAILURE;
240  std::vector<double> vec;
241  vec.push_back(x);
242  vec.push_back(y);
243  vec.push_back(z);
244  vec.push_back(u);
245  vec.push_back(v);
246  return setBinContent(vec, value);
247 }
248 
249 
250 template <class T>
251 const T* EMAPMatrix<T>::getBinContent(double x) const
252 {
253  if (m_dimensions!=1)
254  {
255  return nullptr;
256  }
257  std::vector<double> vec;
258  vec.push_back(x);
259  return getBinContent(vec);
260 }
261 
262 template <class T>
263 const T* EMAPMatrix<T>::getBinContent(double x, double y) const
264 {
265  if (m_dimensions!=2)
266  {
267  return nullptr;
268  }
269  std::vector<double> vec;
270  vec.push_back(x);
271  vec.push_back(y);
272  return getBinContent(vec);
273 }
274 
275 template <class T>
276 const T* EMAPMatrix<T>::getBinContent(double x, double y, double z) const
277 {
278  if (m_dimensions!=3)
279  {
280  return 0;
281  }
282  std::vector<double> vec;
283  vec.push_back(x);
284  vec.push_back(y);
285  vec.push_back(z);
286  return getBinContent(vec);
287 }
288 
289 template <class T>
290 const T* EMAPMatrix<T>::getBinContent(double x, double y, double z, double u) const
291 {
292  if (m_dimensions!=4)
293  {
294  return 0;
295  }
296  std::vector<double> vec;
297  vec.push_back(x);
298  vec.push_back(y);
299  vec.push_back(z);
300  vec.push_back(u);
301  return getBinContent(vec);
302 
303 }
304 
305 template <class T>
306 const T* EMAPMatrix<T>::getBinContent(double x, double y, double z, double u, double v) const
307 {
308  if (m_dimensions!=5)
309  {
310  return 0;
311  }
312  std::vector<double> vec;
313  vec.push_back(x);
314  vec.push_back(y);
315  vec.push_back(z);
316  vec.push_back(u);
317  vec.push_back(v);
318  return getBinContent(vec);
319 }
320 
321 
322 
323 
324 template <class T>
325 const T* EMAPMatrix<T>::getBinContent(const std::vector<double>& x) const
326 {
327  bool statusCode = true;
328  std::vector<unsigned int> id;
329  if (x.size()==m_dimensions)
330  {
331  for (unsigned int i=0;i<m_dimensions;++i)
332  {
333  int bi=m_axis.at(i).getBinIndex(x.at(i));
334  if (bi>=0)
335  {
336  id.push_back(bi);
337  } else
338  {
339  statusCode=false;
340  }
341  }
342  if (statusCode)
343  {
344  return &(m_matrix.at(Index(id)));
345  }
346  }
347  return nullptr;
348 }
349 
350 
351 
352 
353 template <class T>
354 StatusCode EMAPMatrix<T>::setupEntries()
355 {
356 
357  // just a check
358  m_dimensions = m_axis.size();
359  for (unsigned int i=0;i<m_dimensions;++i) {
360  for (unsigned int j=i+1;j<m_dimensions;++j) {
361  if (m_axis.at(i).getName()==m_axis.at(j).getName()) {
362  return StatusCode::FAILURE;
363  }
364  }
365  }
366 
367  unsigned int entries=1;
368  for (unsigned int i=0;i<m_dimensions;++i) {
369  entries*=m_axis.at(i).getNumberOfBins();
370  }
371  m_matrix.clear();
372 
373  T dummyObject;
374  for (unsigned int i=0;i<entries;++i) {
375  m_matrix.push_back(dummyObject);
376  }
377 
378  unsigned int ib=1;
379  m_base.clear();
380  for (unsigned int i=0;i<m_dimensions;++i)
381  {
382  m_base.push_back(ib);
383  ib*=m_axis.at(i).getNumberOfBins();
384  }
385 
386  return StatusCode::SUCCESS;
387 }
388 
389 template <class T>
390 void EMAPMatrix<T>::clearEntries()
391 {
392  unsigned int entries=1;
393  for (unsigned int i=0;i<m_dimensions;++i)
394  {
395  entries*=m_axis.at(i).getNumberOfBins();
396  }
397 
398  m_matrix.clear();
399 
400  T dummyObject;
401  for (unsigned int i=0;i<entries;++i) {
402  m_matrix.push_back(dummyObject);
403  }
404 }
405 
406 
407 template <class T>
408 bool EMAPMatrix<T>::isInRange(const std::vector<double>& x) const
409 {
410  if (x.size()!=m_dimensions) return false;
411  for (unsigned int i=0;i<m_dimensions;++i) if (m_axis.at(i).isInRange(x.at(i))==false) return false;
412  return true;
413 }
414 
415 template <class T>
416 void EMAPMatrix<T>::setTextDescription(const std::string& text)
417 {
418  m_textDescription = text;
419 }
420 
421 template <class T>
422 std::string EMAPMatrix<T>::getTextDescription() const
423 {
424  return m_textDescription;
425 }