FairRoot/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 
31 template <class T> class Vec{
32 
33  public:
34 
35  int dimension;
37  T *array;
38 
39  // constructor;
40  Vec(T *p, int dim, TString nam){
41  if(dim < 0 || dim > 1000000){
42  cout<<"PndTrkVectors::Vec the dimension of the array "<<nam<<" is "
43  <<dim<<" and not acceptable; exit(-2) the program.\n";
44  exit(-2);
45  }
46  dimension = dim;
47  array = p;
48  name = nam;
49  };
50 
51  ~Vec(){;};
52 
53  // function at();
54  T& at(int index){
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 "<<
61  dimension-1<<" included]; exiting the process.\n";
62  exit(-1);
63  };
64 
65  // overloaded operator [] ; the functionality is identical to the at() function;
66  T& operator[] (int index){
67 
68  T& alias = at(index);
69  return alias ;
70  };
71 
72 };
73 
74 
75 #endif
Double_t p
Definition: anasim.C:58
exit(0)
TString name
Definition: PndTrkVectors.h:36
Vec(T *p, int dim, TString nam)
Definition: PndTrkVectors.h:40
TTree * T
Definition: anaLmdReco.C:32
float & operator[](int i)
Definition: P4_F32vec4.h:6
~Vec()
Definition: PndTrkVectors.h:51
TString name
T * array
Definition: PndTrkVectors.h:37
int dimension
Definition: PndTrkVectors.h:35
TString nam
Definition: sim_hypGe.C:48
T & at(int index)
Definition: PndTrkVectors.h:54