PandaRoot
PndTrkVectors.h
Go to the documentation of this file.
1 #ifndef PndTrkVectors_H
2 #define PndTrkVectors_H 1
3 // Root includes
4 #include "TROOT.h"
5 
6 #include <stdlib.h>
7 #include <iostream>
8 
9 using namespace std;
10 
11 // this class is useful to mimic the [] operator
12 // of an array or the at() operator of the std::vector
13 // but it contains also the boundary check. If the boundary
14 // are violated an error message is printed and a call
15 // to the exit function with value -1 is performed.
16 // The argument for the constructor are :
17 // p --> pointer to an EXISTING array(of ANY type) to be mimiced;
18 // dim --> dimension of the array to be mimiced;
19 // nam --> string containing the name of the Vec class
20 // that mimics the array.
21 
22 // The typical statements for producing the Vec mimicing
23 // an existing array of type MYTYPE is :
24 
25 /*
26  MYTYPE array[100]; the array must physically exist in memory;
27  Vec <MYTYPE> myvec(array, 100, "myvec");
28 */
29 
30 template <class T>
31 class Vec {
32 
33  public:
34  int dimension;
35  TString name;
36  T *array;
37 
38  // constructor;
39  Vec(T *p, int dim, TString nam)
40  {
41  if (dim < 0 || dim > 1000000) {
42  cout << "PndTrkVectors::Vec the dimension of the array " << nam << " is " << dim << " and not acceptable; exit(-2) the program.\n";
43  exit(-2);
44  }
45  dimension = dim;
46  array = p;
47  name = nam;
48  };
49 
50  ~Vec() { ; };
51 
52  // function at();
53  T &at(int index)
54  {
55 
56  if (index >= 0 && index < dimension) {
57  T &alias = array[index];
58  return alias;
59  }
60  cout << "PndTrkVectors:: array " << name << ": index = " << index << " and it is out of bounds [from 0 to " << dimension - 1 << " included]; exiting the process.\n";
61  exit(-1);
62  };
63 
64  // overloaded operator [] ; the functionality is identical to the at() function;
65  T &operator[](int index)
66  {
67 
68  T &alias = at(index);
69  return alias;
70  };
71 };
72 
73 #endif
T & operator[](int index)
Definition: PndTrkVectors.h:65
STL namespace.
TString name
Definition: PndTrkVectors.h:35
Vec(T *p, int dim, TString nam)
Definition: PndTrkVectors.h:39
~Vec()
Definition: PndTrkVectors.h:50
T * array
Definition: PndTrkVectors.h:36
int dimension
Definition: PndTrkVectors.h:34
T & at(int index)
Definition: PndTrkVectors.h:53