CMS 3D CMS Logo

Resolution.cc
Go to the documentation of this file.
1 //
2 //
3 // File: src/Resolution.cc
4 // Purpose: Calculate resolutions for a quantity.
5 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
6 //
7 // CMSSW File : src/Resolution.cc
8 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
9 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
10 //
11 
34 #include "CLHEP/Random/RandGauss.h"
35 #include <cmath>
36 #include <iostream>
37 #include <cctype>
38 #include <cstdlib>
39 
40 using std::isdigit;
41 using std::isspace;
42 using std::ostream;
43 using std::sqrt;
44 using std::string;
45 #ifndef __GNUC__
46 using std::atof;
47 #endif
48 
49 namespace {
50 
51  bool get_field(string s, string::size_type i, double& x)
52  //
53  // Purpose: Scan string S starting at position I.
54  // Find the value of the first floating-point number we
55  // find there.
56  //
57  // Inputs:
58  // s - The string to scan.
59  // i - Starting character position in the string.
60  //
61  // Outputs:
62  // x - The value of the number we found.
63  //
64  // Returns:
65  // True if we found something that looks like a number, false otherwise.
66  //
67  {
69  while (j < s.size() && s[j] != ',' && !isdigit(s[j]) && s[j] != '.')
70  ++j;
71  if (j < s.size() && (isdigit(s[j]) || s[j] == '.')) {
72  x = atof(s.c_str() + j);
73  return true;
74  }
75  return false;
76  }
77 
78 } // unnamed namespace
79 
80 namespace hitfit {
81 
83  //
84  // Purpose: Constructor.
85  //
86  // Inputs:
87  // s - String encoding the resolution parameters, as described
88  // in the comments in the header.
89  //
90  : _resolution_exponent(0) {
91  _inverse = false;
92  _constant_sigma = 0;
94  _noise_sigma = 0;
95 
96  // Skip spaces.
97  double x;
98  string::size_type i = 0;
99  while (i < s.size() && isspace(s[i]))
100  ++i;
101 
102  // Check for the inverse flag.
103  if (s[i] == '-') {
104  _inverse = true;
105  ++i;
106  } else if (s[i] == '+') {
107  ++i;
108  }
109 
110  // Get the constant term.
111  if (get_field(s, i, x))
112  _constant_sigma = x;
113  i = s.find(',', i);
114 
115  // Look for a resolution term.
116  if (i != string::npos) {
117  ++i;
118  if (get_field(s, i, x))
120 
121  // Look for a noise term.
122  i = s.find(',', i);
123  if (i != string::npos) {
124  if (get_field(s, i + 1, x))
125  _noise_sigma = x;
126  }
127  }
128  }
129 
130  Resolution::Resolution(double C, double R, double m, double N, bool inverse /*= false*/)
131  : _constant_sigma(C), _resolution_sigma(R), _resolution_exponent(m), _noise_sigma(N), _inverse(inverse) {}
132 
133  Resolution::Resolution(double res, bool inverse /*= false*/)
134  //
135  // Purpose: Constructor, to give a constant resolution.
136  // I.e., sigma() will always return RES.
137  //
138  // Inputs:
139  // res - The resolution value.
140  // inverse - The inverse flag.
141  //
142  : _constant_sigma(0), _resolution_sigma(0), _resolution_exponent(0), _noise_sigma(res), _inverse(inverse) {}
143 
145  //
146  // Purpose: Return the inverse flag.
147  //
148  // Returns:
149  // The inverse flag.
150  //
151  {
152  return _inverse;
153  }
154 
155  double Resolution::C() const { return _constant_sigma; }
156 
157  double Resolution::R() const { return _resolution_sigma; }
158 
159  double Resolution::m() const { return _resolution_exponent; }
160 
161  double Resolution::N() const { return _noise_sigma; }
162 
163  double Resolution::sigma(double p) const
164  //
165  // Purpose: Return the uncertainty for a momentum P.
166  //
167  // Inputs:
168  // p - The momentum
169  //
170  // Returns:
171  // The uncertainty for a momentum P.
172  //
173  {
174  p = std::fabs(p);
175  if (_inverse)
176  p = 1 / p;
177 
178  return sqrt((_constant_sigma * _constant_sigma * p + _resolution_sigma * _resolution_sigma) * p +
179  _noise_sigma * _noise_sigma);
180  }
181 
182  double Resolution::pick(double x, double p, CLHEP::HepRandomEngine& engine) const
183  //
184  // Purpose: Given a value X, measured for an object with momentum P,
185  // pick a new value from a Gaussian distribution
186  // described by this resolution --- with mean X and width sigma(P).
187  //
188  // Inputs:
189  // x - The quantity value (distribution mean).
190  // p - The momentum, for calculating the width.
191  // engine - The underlying RNG.
192  //
193  // Returns:
194  // A smeared value of X.
195  //
196  {
197  CLHEP::RandGauss gen(engine);
198  if (_inverse)
199  return 1 / gen.fire(1 / x, sigma(p));
200  else
201  return gen.fire(x, sigma(p));
202  }
203 
212  std::ostream& operator<<(std::ostream& s, const Resolution& r)
213  //
214  // Purpose: Dump this object to S.
215  //
216  // Inputs:
217  // s - The stream to which to write.
218  // r - The object to dump.
219  //
220  // Returns:
221  // The stream S.
222  //
223  {
224  if (r._inverse)
225  s << "-";
226  s << r._constant_sigma << "," << r._resolution_sigma << "," << r._noise_sigma;
227  return s;
228  }
229 
230 } // namespace hitfit
double pick(double x, double p, CLHEP::HepRandomEngine &engine) const
Generate random value from a Gaussian distribution described by this resolution. Given a value ...
Definition: Resolution.cc:182
Calculate and represent resolution for a physical quantity.
Definition: Resolution.h:98
double m() const
Return the exponent factor in the resolution term.
Definition: Resolution.cc:159
uint16_t size_type
Definition: Electron.h:6
double sigma(double p) const
Return the uncertainty for a variable with magnitude p.
Definition: Resolution.cc:163
double _resolution_exponent
Definition: Resolution.h:212
bool inverse() const
Return the setting of the inverse flag.
Definition: Resolution.cc:144
double N() const
Return the N term (noise term)
Definition: Resolution.cc:161
T sqrt(T t)
Definition: SSEVec.h:23
def gen(fragment, howMuch)
Production test section ####.
#define N
Definition: blowfish.cc:9
Resolution(std::string s="")
Constructor, initialize from a string.
Definition: Resolution.cc:82
double _resolution_sigma
Definition: Resolution.h:207
std::ostream & operator<<(std::ostream &s, const Constraint_Intermed &ci)
Output stream operator, print the content of this Constraint_Intermed to an output stream...
float x
Calculate and represent resolution for a physical quantity.
double R() const
Return the R term (resolution term)
Definition: Resolution.cc:157
double C() const
Return the C term (constant term)
Definition: Resolution.cc:155
double _constant_sigma
Definition: Resolution.h:202