00001 #ifndef CondFormats_JetMETObjects_Utilities_h
00002 #define CondFormats_JetMETObjects_Utilities_h
00003
00004 #if 1
00005 #include <stdexcept>
00006 #else
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #endif
00009
00010 #include <cstdlib>
00011 #include <sstream>
00012 #include <string>
00013 #include <vector>
00014 #include <cmath>
00015
00016 namespace
00017 {
00018 void handleError(const std::string& fClass, const std::string& fMessage)
00019 {
00020 #if 1
00021 std::stringstream sserr;
00022 sserr<<fClass<<" ERROR: "<<fMessage;
00023 throw std::runtime_error(sserr.str());
00024 #else
00025 edm::LogError(fClass) << fMessage;
00026 #endif
00027 }
00028
00029 inline float getFloat(const std::string& token)
00030 {
00031 char* endptr;
00032 float result = strtod (token.c_str(), &endptr);
00033 if (endptr == token.c_str())
00034 {
00035 std::stringstream sserr;
00036 sserr<<"can't convert token "<<token<<" to float value";
00037 handleError("getFloat",sserr.str());
00038 }
00039 return result;
00040 }
00041
00042 inline unsigned getUnsigned(const std::string& token)
00043 {
00044 char* endptr;
00045 unsigned result = strtoul (token.c_str(), &endptr, 0);
00046 if (endptr == token.c_str())
00047 {
00048 std::stringstream sserr;
00049 sserr<<"can't convert token "<<token<<" to unsigned value";
00050 handleError("getUnsigned",sserr.str());
00051 }
00052 return result;
00053 }
00054 inline long int getSigned(const std::string& token)
00055 {
00056 char* endptr;
00057 unsigned result = strtol (token.c_str(), &endptr, 0);
00058 if (endptr == token.c_str())
00059 {
00060 std::stringstream sserr;
00061 sserr<<"can't convert token "<<token<<" to signed value";
00062 handleError("getSigned",sserr.str());
00063 }
00064 return result;
00065 }
00066
00067 inline std::string getSection(const std::string& token)
00068 {
00069 size_t iFirst = token.find ('[');
00070 size_t iLast = token.find (']');
00071 if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
00072 return std::string (token, iFirst+1, iLast-iFirst-1);
00073 return "";
00074 }
00075
00076 inline std::vector<std::string> getTokens(const std::string& fLine)
00077 {
00078 std::vector<std::string> tokens;
00079 std::string currentToken;
00080 for (unsigned ipos = 0; ipos < fLine.length (); ++ipos)
00081 {
00082 char c = fLine[ipos];
00083 if (c == '#') break;
00084 else if (c == ' ')
00085 {
00086 if (!currentToken.empty())
00087 {
00088 tokens.push_back(currentToken);
00089 currentToken.clear();
00090 }
00091 }
00092 else
00093 currentToken += c;
00094 }
00095 if (!currentToken.empty()) tokens.push_back(currentToken);
00096 return tokens;
00097 }
00098
00099 inline std::string getDefinitions(const std::string& token)
00100 {
00101 size_t iFirst = token.find ('{');
00102 size_t iLast = token.find ('}');
00103 if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
00104 return std::string (token, iFirst+1, iLast-iFirst-1);
00105 return "";
00106 }
00107
00108 inline float quadraticInterpolation(float fZ, const float fX[3], const float fY[3])
00109 {
00110
00111
00112 float D[4],a[3];
00113 D[0] = fX[0]*fX[1]*(fX[0]-fX[1])+fX[1]*fX[2]*(fX[1]-fX[2])+fX[2]*fX[0]*(fX[2]-fX[0]);
00114 D[3] = fY[0]*(fX[1]-fX[2])+fY[1]*(fX[2]-fX[0])+fY[2]*(fX[0]-fX[1]);
00115 D[2] = fY[0]*(pow(fX[2],2)-pow(fX[1],2))+fY[1]*(pow(fX[0],2)-pow(fX[2],2))+fY[2]*(pow(fX[1],2)-pow(fX[0],2));
00116 D[1] = fY[0]*fX[1]*fX[2]*(fX[1]-fX[2])+fY[1]*fX[0]*fX[2]*(fX[2]-fX[0])+fY[2]*fX[0]*fX[1]*(fX[0]-fX[1]);
00117 if (D[0] != 0)
00118 {
00119 a[0] = D[1]/D[0];
00120 a[1] = D[2]/D[0];
00121 a[2] = D[3]/D[0];
00122 }
00123 else
00124 {
00125 a[0] = 0.0;
00126 a[1] = 0.0;
00127 a[2] = 0.0;
00128 }
00129 float r = a[0]+fZ*(a[1]+fZ*a[2]);
00130 return r;
00131 }
00132 }
00133 #endif