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