CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/JetMETCorrections/InterpolationTables/interface/interpolate.h

Go to the documentation of this file.
00001 #ifndef NPSTAT_INTERPOLATE_HH_
00002 #define NPSTAT_INTERPOLATE_HH_
00003 
00015 #include "JetMETCorrections/InterpolationTables/interface/ProperDblFromCmpl.h"
00016 
00017 namespace npstat {
00022     template<typename T>
00023     inline T interpolate_linear(const double x, const T& f0, const T& f1)
00024     {
00025         const typename ProperDblFromCmpl<T>::type dx = 1.0 - x;
00026         return f0*dx + f1*static_cast<typename ProperDblFromCmpl<T>::type>(x);
00027     }
00028 
00033     template<typename T>
00034     inline T interpolate_quadratic(const double x, const T& f0,
00035                                    const T& f1, const T& f2)
00036     {
00037         static const typename ProperDblFromCmpl<T>::type two = 2.0;
00038         const typename ProperDblFromCmpl<T>::type dx = x - 1.0;
00039         return f1 + ((f2 - f0)/two + ((f2 - f1) + (f0 - f1))*(dx/two))*dx;
00040     }
00041 
00046     template<typename T>
00047     inline T interpolate_cubic(const double x, const T& f0, const T& f1,
00048                                const T& f2, const T& f3)
00049     {
00050         return interpolate_linear(x*(3.0 - x)/2.0,
00051                                   interpolate_linear(x/3.0, f0, f3),
00052                                   interpolate_linear(x - 1.0, f1, f2));
00053     }
00054 
00056 
00066     template<typename T>
00067     unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
00068                                         const T& f0, const T& f1);
00069     template<typename T>
00070     unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
00071                                         const T& f0, const T& f1, const T& f2);
00072     template<typename T>
00073     unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
00074                                         const T& f0, const T& f1,
00075                                         const T& f2, const T& f3);
00077 }
00078 
00079 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
00080 
00081 namespace npstat {
00082     template<typename T>
00083     unsigned interpolation_coefficients(T* buffer, const unsigned bufLen,
00084                                         const T& f0, const T& f1)
00085     {
00086         if (bufLen <= 1U) throw npstat::NpstatInvalidArgument(
00087             "In npstat::interpolation_coefficients: "
00088             "insufficient length of the output buffer");
00089         buffer[0] = f0;
00090         buffer[1] = f1 - f0;
00091         return 2U;
00092     }
00093 
00094     template<typename T>
00095     unsigned interpolation_coefficients(T* buffer, const unsigned bufLen,
00096                                         const T& f0, const T& f1, const T& f2)
00097     {
00098         if (bufLen <= 2U) throw npstat::NpstatInvalidArgument(
00099             "In npstat::interpolation_coefficients: "
00100             "insufficient length of the output buffer");
00101         buffer[0] = f0;
00102         buffer[1] = static_cast<T>((f1 - f2 + 3*(f1 - f0))/2.0);
00103         buffer[2] = static_cast<T>(((f0 - f1) + (f2 - f1))/2.0);
00104         return 3U;
00105     }
00106 
00107     template<typename T>
00108     unsigned interpolation_coefficients(T* buffer, const unsigned bufLen,
00109                                         const T& f0, const T& f1,
00110                                         const T& f2, const T& f3)
00111     {
00112         if (bufLen <= 3U) throw npstat::NpstatInvalidArgument(
00113             "In npstat::interpolation_coefficients: "
00114             "insufficient length of the output buffer");
00115         buffer[0] = f0;
00116         buffer[1] = static_cast<T>((11*(f1 - f0)+7*(f1 - f2)+2*(f3 - f2))/6.0);
00117         buffer[2] = static_cast<T>((2*(f0 - f1)+3*(f2 - f1)+(f2 - f3))/2.0);
00118         buffer[3] = static_cast<T>(((f3 - f0) + 3*(f1 - f2))/6.0);
00119         return 4U;
00120     }
00121 }
00122 
00123 
00124 #endif // NPSTAT_INTERPOLATE_HH_
00125