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