00001 #ifndef StDetectorDbInterpolator_h
00002 #define StDetectorDbInterpolator_h
00003
00018 template<class T>
00019 class StDetectorDbInterpolator{
00020 public:
00021 StDetectorDbInterpolator(unsigned int numEntries,unsigned int* times,T* array);
00022 T interpolate(unsigned int time);
00023 T getLowerValue(unsigned int time);
00024 private:
00025 unsigned int mNumEntries;
00026 unsigned int* mTimes;
00027 T* mArray;
00028 };
00029
00031 template<class T>
00032 StDetectorDbInterpolator<T>::StDetectorDbInterpolator(unsigned int numEntries,unsigned int* times,T* array){
00033 mNumEntries = numEntries;
00034 mTimes = times;
00035 mArray = array;
00036 };
00037
00039 template<class T>
00040 T StDetectorDbInterpolator<T>::interpolate(unsigned int time){
00041
00042 if( mNumEntries == 0)
00043 return 0;
00044
00045 if(time <= mTimes[0])
00046 return mArray[0];
00047 if(time >= mTimes[mNumEntries-1])
00048 return mArray[mNumEntries-1];
00049 unsigned int i = 0;
00050 while(mTimes[i] < time && i < mNumEntries)
00051 i++;
00052 double relTime = (time - mTimes[i-1]);
00053 double denominator = (mTimes[i]-mTimes[i-1]);
00054
00055 if(denominator == 0)
00056 return mArray[i];
00057 else
00058 return (relTime/denominator)*(mArray[i]-mArray[i-1])+mArray[i-1];
00059
00060 };
00061
00065 template<class T>
00066 T StDetectorDbInterpolator<T>::getLowerValue(unsigned int time){
00067
00068 if( mNumEntries == 0)
00069 return 0;
00070
00071 if(time <= mTimes[0])
00072 return mArray[0];
00073 if(time >= mTimes[mNumEntries-1])
00074 return mArray[mNumEntries-1];
00075
00076 unsigned int i = 0;
00077
00078
00079 while(mTimes[i] < time && i < mNumEntries){
00080 i++;
00081 }
00082
00083 if(i > 0){
00084 i--;
00085 }
00086
00087 return mArray[i];
00088
00089 };
00090
00091 #endif