00001 #ifndef NPSTAT_RESCANARRAY_HH_ 00002 #define NPSTAT_RESCANARRAY_HH_ 00003 00015 #include "JetMETCorrections/InterpolationTables/interface/ArrayND.h" 00016 00017 namespace npstat { 00028 template<typename Num1, unsigned Len1, unsigned Dim1, 00029 typename Num2, unsigned Len2, unsigned Dim2> 00030 void rescanArray(const ArrayND<Num1,Len1,Dim1>& from, 00031 ArrayND<Num2,Len2,Dim2>* to, 00032 unsigned interpolationDegree=0); 00033 } 00034 00035 #include <cassert> 00036 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h" 00037 00038 #include "JetMETCorrections/InterpolationTables/interface/LinearMapper1d.h" 00039 00040 namespace npstat { 00041 namespace Private { 00042 template<typename Num1, unsigned Len1, unsigned Dim1, 00043 typename Num2, unsigned Len2, unsigned Dim2> 00044 class ArrayMapper 00045 { 00046 public: 00047 ArrayMapper(const ArrayND<Num1,Len1,Dim1>& from, 00048 const ArrayND<Num2,Len2,Dim2>& to, 00049 const unsigned interpolationDegree) 00050 : mapped_(from.rank()), 00051 from_(from), 00052 dim_(from.rank()), 00053 ideg_(interpolationDegree) 00054 { 00055 assert(dim_ == to.rank()); 00056 if (dim_) 00057 { 00058 mappers_.reserve(dim_); 00059 for (unsigned i=0; i<dim_; ++i) 00060 mappers_.push_back(LinearMapper1d(-0.5, -0.5, 00061 to.span(i) - 0.5, 00062 from.span(i) - 0.5)); 00063 } 00064 } 00065 00066 Num1 operator()(const unsigned* index, unsigned indexLen) const 00067 { 00068 if (dim_) 00069 { 00070 const LinearMapper1d* m = &mappers_[0]; 00071 double* coords = &mapped_[0]; 00072 for (unsigned i=0; i<dim_; ++i) 00073 coords[i] = m[i](index[i]); 00074 switch (ideg_) 00075 { 00076 case 0U: 00077 return from_.closest(coords, dim_); 00078 00079 case 1U: 00080 return from_.interpolate1(coords, dim_); 00081 00082 case 3U: 00083 return from_.interpolate3(coords, dim_); 00084 00085 default: 00086 assert(0); 00087 } 00088 } 00089 return from_(); 00090 } 00091 00092 private: 00093 std::vector<LinearMapper1d> mappers_; 00094 mutable std::vector<double> mapped_; 00095 const ArrayND<Num1,Len1,Dim1>& from_; 00096 unsigned dim_; 00097 unsigned ideg_; 00098 }; 00099 } 00100 00101 template<typename Num1, unsigned Len1, unsigned Dim1, 00102 typename Num2, unsigned Len2, unsigned Dim2> 00103 void rescanArray(const ArrayND<Num1,Len1,Dim1>& from, 00104 ArrayND<Num2,Len2,Dim2>* to, 00105 const unsigned interpolationDegree) 00106 { 00107 assert(to); 00108 if (from.rank() != to->rank()) throw npstat::NpstatInvalidArgument( 00109 "In npstat::rescanArray: incompatible dimensionalities " 00110 "of input and output arrays"); 00111 if (!(interpolationDegree == 0U || 00112 interpolationDegree == 1U || 00113 interpolationDegree == 3U)) throw npstat::NpstatInvalidArgument( 00114 "In npstat::rescanArray: unsupported interpolation degree"); 00115 to->functorFill(Private::ArrayMapper<Num1,Len1,Dim1,Num2,Len2,Dim2>( 00116 from, *to, interpolationDegree)); 00117 } 00118 } 00119 00120 00121 #endif // NPSTAT_RESCANARRAY_HH_ 00122