g4tools  5.4.0
carray
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_carray
5 #define tools_carray
6 
7 //fixed array manips
8 
9 namespace tools {
10 
11 template <class T>
12 inline void destroy(T*& a_v){
13  delete [] a_v;
14  a_v = 0;
15 }
16 
17 template <class T,class I>
18 inline bool carray_copy(T*& a_v,I a_n,const T* a_from){
19  delete [] a_v;
20  a_v = 0;
21  if(a_n) {
22  a_v = new T[a_n];
23  if(!a_v) return false;
24  }
25  for(I i=0;i<a_n;i++) a_v[i] = a_from[i];
26  return true;
27 }
28 
29 template <class T,class I>
30 inline bool copy(T*& a_v,I a_n,const T* a_from) {return carray_copy<T,I>(a_v,a_n,a_from);}
31 
32 /*
33 template <class T>
34 inline T value(const T* a_v,
35  unsigned int a_q,
36  unsigned int a_i,unsigned int a_j){
37  // to get value in the case a x[p][q] is stored in a x[p*q].
38  // (used in hep/polyhedron_arb8)
39  // WARNING : no check is done on a_i, a_j
40  return a_v[a_i*a_q+a_j];
41 }
42 
43 template <class I,class T,class I>
44 inline bool normalize(I a_n,T*& a_v,T(*a_sqrt)(T)){
45  T sz = T();
46  I i;
47  for(i=0;i<a_n;i++) sz += a_v[i]*a_v[i];
48  sz = a_sqrt(sz);
49  if(sz==T()) return false;
50  for(i=0;i<a_n;i++) a_v[i] /= sz;
51  return true;
52 }
53 
54 template <class T>
55 inline T value(const T* a_v,
56  unsigned int a_q,
57  unsigned int a_i,unsigned int a_j){
58  // to get value x[i][j] in the case a x[p][q] is stored in a x[p*q].
59  // (used in hep/polyhedron_arb8)
60  // WARNING : no check is done on a_i, a_j
61  return a_v[a_i*a_q+a_j];
62 }
63 */
64 
65 }
66 
67 #include "mnmx"
68 
69 namespace tools {
70 
71 template <class I,class T,class TO>
72 inline bool min_max(I a_n,const void* a_v,TO& a_mn,TO& a_mx){
73  if(a_n<=I(0)) {a_mn = TO(0);a_mx = TO(0);return false;}
74  T* pos = (T*)a_v;
75  a_mn = *pos;
76  a_mx = *pos;
77  for(I i=0;i<a_n;i++,pos++) {
78  a_mn = mn<TO>(a_mn,*pos);
79  a_mx = mx<TO>(a_mx,*pos);
80  }
81  return true;
82 }
83 
84 template <class I,class T,class TO>
85 inline bool min_max_S_S2(I a_n,const void* a_v,TO& a_mn,TO& a_mx,TO& a_S,TO& a_S2){
86  if(a_n<=0) {a_mn = TO(0);a_mx = TO(0);a_S=TO(0);a_S2=TO(0);return false;}
87  T* pos = (T*)a_v;
88  a_mn = *pos;
89  a_mx = *pos;
90  a_S = TO(0);
91  a_S2 = TO(0);
92  for(I i=0;i<a_n;i++,pos++) {
93  a_mn = mn<TO>(a_mn,*pos);
94  a_mx = mx<TO>(a_mx,*pos);
95  a_S += TO(*pos);
96  a_S2 += TO(*pos) * TO(*pos);
97  }
98  return true;
99 }
100 
101 template <class I,class T,class TO>
102 inline bool min_max_mean_rms(I a_n,const void* a_v,TO& a_mn,TO& a_mx,TO& a_mean,TO& a_rms,
103  TO(*a_sqrt)(TO),TO(*a_fabs)(TO)){
104  TO S = TO(0);
105  TO S2 = TO(0);
106  if(!min_max_S_S2<I,T,TO>(a_n,a_v,a_mn,a_mx,S,S2)) {
107  a_mn = TO(0);a_mx = TO(0);a_mean=TO(0);a_rms=TO(0);
108  return false;
109  }
110  a_mean = S/TO(a_n);
111  a_rms = a_sqrt(a_fabs(S2/TO(a_n) - a_mean * a_mean));
112  return true;
113 }
114 
115 template <class I,class T,class HISTO>
116 inline bool h1_fill(I a_n,const void* a_v,HISTO& a_histo) {
117  a_histo.reset();
118  if(a_n<=0) return false;
119  typedef typename HISTO::coordinate_t TC;
120  T* pos = (T*)a_v;
121  for(I i=0;i<a_n;i++,pos++) a_histo.fill(TC(*pos));
122  return true;
123 }
124 
125 template <class T,class I>
126 inline T* _4s_to_3s(const T* a_4s,const I& a_w,const I& a_h) {
127  T* _3s = new T[a_w*a_h*3];
128  if(!_3s) return 0;
129  T* pfrom = (T*)a_4s;
130  T* pto = _3s;
131  {I _sz = a_w*a_h*4;
132  for(I i=0;i<_sz;i+=4) {
133  *(pto+0) = *(pfrom+0);
134  *(pto+1) = *(pfrom+1);
135  *(pto+2) = *(pfrom+2);
136  pfrom += 4;
137  pto += 3;
138  }}
139  return _3s;
140 }
141 
142 }
143 
144 #endif
tools::destroy
void destroy(T *&a_v)
Definition: carray:12
tools::wcsv::pto
bool pto(std::ostream &a_writer, const std::string &a_class, const PROF &a_prof, char a_sep=',', char a_hc='#', bool a_header=true)
Definition: wcsv_histo:115
tools::min_max_S_S2
bool min_max_S_S2(I a_n, const void *a_v, TO &a_mn, TO &a_mx, TO &a_S, TO &a_S2)
Definition: carray:85
tools::_4s_to_3s
T * _4s_to_3s(const T *a_4s, const I &a_w, const I &a_h)
Definition: carray:126
tools::copy
bool copy(T *&a_v, I a_n, const T *a_from)
Definition: carray:30
mnmx
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::min_max_mean_rms
bool min_max_mean_rms(I a_n, const void *a_v, TO &a_mn, TO &a_mx, TO &a_mean, TO &a_rms, TO(*a_sqrt)(TO), TO(*a_fabs)(TO))
Definition: carray:102
tools::carray_copy
bool carray_copy(T *&a_v, I a_n, const T *a_from)
Definition: carray:18
tools::h1_fill
bool h1_fill(I a_n, const void *a_v, HISTO &a_histo)
Definition: carray:116
tools::min_max
bool min_max(I a_n, const void *a_v, TO &a_mn, TO &a_mx)
Definition: carray:72