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