susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
JEC_Utilities.hpp
Go to the documentation of this file.
1 // Adapted for cfA from https://github.com/cms-sw/cmssw/blob/CMSSW_7_2_X/CondFormats/JetMETObjects/interface/Utilities.h
2 
3 #ifndef JEC_Utilities_hpp
4 #define JEC_Utilities_hpp
5 
6 // #ifdef STANDALONE
7 #include <stdexcept>
8 // #else
9 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
10 // #endif
11 
12 #include <cstdlib>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 #include <cmath>
17 
18 namespace
19 {
20  __attribute__((noreturn)) void handleError(const std::string& fClass, const std::string& fMessage)
21  {
22  std::stringstream sserr;
23  sserr<<fClass<<" ERROR: "<<fMessage;
24  throw std::runtime_error(sserr.str());
25  }
26  //----------------------------------------------------------------------
27  inline float getFloat(const std::string& token)
28  {
29  char* endptr;
30  float result = strtod (token.c_str(), &endptr);
31  if (endptr == token.c_str())
32  {
33  std::stringstream sserr;
34  sserr<<"can't convert token "<<token<<" to float value";
35  handleError("getFloat",sserr.str());
36  }
37  return result;
38  }
39  //----------------------------------------------------------------------
40  inline unsigned getUnsigned(const std::string& token)
41  {
42  char* endptr;
43  unsigned result = strtoul (token.c_str(), &endptr, 0);
44  if (endptr == token.c_str())
45  {
46  std::stringstream sserr;
47  sserr<<"can't convert token "<<token<<" to unsigned value";
48  handleError("getUnsigned",sserr.str());
49  }
50  return result;
51  }
52  //----------------------------------------------------------------------
53  inline std::string getSection(const std::string& token)
54  {
55  size_t iFirst = token.find ('[');
56  size_t iLast = token.find (']');
57  if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
58  return std::string (token, iFirst+1, iLast-iFirst-1);
59  return "";
60  }
61  //----------------------------------------------------------------------
62  inline std::vector<std::string> getTokens(const std::string& fLine)
63  {
64  std::vector<std::string> tokens;
65  std::string currentToken;
66  for (unsigned ipos = 0; ipos < fLine.length (); ++ipos)
67  {
68  char c = fLine[ipos];
69  if (c == '#') break; // ignore comments
70  else if (c == ' ')
71  { // flush current token if any
72  if (!currentToken.empty())
73  {
74  tokens.push_back(currentToken);
75  currentToken.clear();
76  }
77  }
78  else
79  currentToken += c;
80  }
81  if (!currentToken.empty()) tokens.push_back(currentToken); // flush end
82  return tokens;
83  }
84  //----------------------------------------------------------------------
85  inline std::string getDefinitions(const std::string& token)
86  {
87  size_t iFirst = token.find ('{');
88  size_t iLast = token.find ('}');
89  if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
90  return std::string (token, iFirst+1, iLast-iFirst-1);
91  return "";
92  }
93  //------------------------------------------------------------------------
94  inline float quadraticInterpolation(float fZ, const float fX[3], const float fY[3])
95  {
96  // Quadratic interpolation through the points (x[i],y[i]). First find the parabola that
97  // is defined by the points and then calculate the y(z).
98  float D[4],a[3];
99  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]);
100  D[3] = fY[0]*(fX[1]-fX[2])+fY[1]*(fX[2]-fX[0])+fY[2]*(fX[0]-fX[1]);
101  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));
102  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]);
103  if (D[0] != 0)
104  {
105  a[0] = D[1]/D[0];
106  a[1] = D[2]/D[0];
107  a[2] = D[3]/D[0];
108  }
109  else
110  {
111  a[0] = 0.0;
112  a[1] = 0.0;
113  a[2] = 0.0;
114  }
115  float r = a[0]+fZ*(a[1]+fZ*a[2]);
116  return r;
117  }
118 }
119 #endif
STL namespace.
void handleError(const std::string &fClass, const std::string &fMessage)
unsigned getUnsigned(const std::string &token)
std::string getSection(const std::string &token)
float getFloat(const std::string &token)
std::vector< std::string > getTokens(const std::string &fLine)
std::string getDefinitions(const std::string &token)
float quadraticInterpolation(float fZ, const float fX[3], const float fY[3])