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 
12 
35 #include "CLHEP/Random/RandGauss.h"
36 #include <cmath>
37 #include <iostream>
38 #include <cctype>
39 #include <cstdlib>
40 
41 
42 using std::sqrt;
43 using std::ostream;
44 using std::string;
45 using std::isspace;
46 using std::isdigit;
47 #ifndef __GNUC__
48 using std::atof;
49 #endif
50 
51 
52 
53 namespace {
54 
55 
56 bool get_field (string s, string::size_type i, double& x)
57 //
58 // Purpose: Scan string S starting at position I.
59 // Find the value of the first floating-point number we
60 // find there.
61 //
62 // Inputs:
63 // s - The string to scan.
64 // i - Starting character position in the string.
65 //
66 // Outputs:
67 // x - The value of the number we found.
68 //
69 // Returns:
70 // True if we found something that looks like a number, false otherwise.
71 //
72 {
73  string::size_type j = i;
74  while (j < s.size() && s[j] != ',' && !isdigit (s[j]) && s[j] != '.')
75  ++j;
76  if (j < s.size() && (isdigit (s[j]) || s[j] == '.')) {
77  x = atof (s.c_str() + j);
78  return true;
79  }
80  return false;
81 }
82 
83 
84 } // unnamed namespace
85 
86 
87 namespace hitfit {
88 
89 
91 //
92 // Purpose: Constructor.
93 //
94 // Inputs:
95 // s - String encoding the resolution parameters, as described
96 // in the comments in the header.
97 //
98  :_resolution_exponent(0)
99 {
100  _inverse = false;
101  _constant_sigma = 0;
102  _resolution_sigma = 0;
103  _noise_sigma = 0;
104 
105  // Skip spaces.
106  double x;
107  string::size_type i = 0;
108  while (i < s.size() && isspace (s[i]))
109  ++i;
110 
111  // Check for the inverse flag.
112  if (s[i] == '-') {
113  _inverse = true;
114  ++i;
115  }
116  else if (s[i] == '+') {
117  ++i;
118  }
119 
120  // Get the constant term.
121  if (get_field (s, i, x)) _constant_sigma = x;
122  i = s.find (',', i);
123 
124  // Look for a resolution term.
125  if (i != string::npos) {
126  ++i;
127  if (get_field (s, i, x)) _resolution_sigma = x;
128 
129  // Look for a noise term.
130  i = s.find (',', i);
131  if (i != string::npos) {
132  if (get_field (s, i+1, x)) _noise_sigma = x;
133  }
134  }
135 }
136 
137 
139  double R,
140  double m,
141  double N,
142  bool inverse /*= false*/)
143  : _constant_sigma (C),
144  _resolution_sigma (R),
146  _noise_sigma (N),
147  _inverse (inverse)
148 {
149 }
150 
151 
153  bool inverse /*= false*/)
154 //
155 // Purpose: Constructor, to give a constant resolution.
156 // I.e., sigma() will always return RES.
157 //
158 // Inputs:
159 // res - The resolution value.
160 // inverse - The inverse flag.
161 //
162  : _constant_sigma (0),
163  _resolution_sigma (0),
165  _noise_sigma (res),
166  _inverse (inverse)
167 {
168 }
169 
170 
171 bool Resolution::inverse () const
172 //
173 // Purpose: Return the inverse flag.
174 //
175 // Returns:
176 // The inverse flag.
177 //
178 {
179  return _inverse;
180 }
181 
182 
183 double Resolution::C() const
184 {
185  return _constant_sigma;
186 }
187 
188 
189 double Resolution::R() const
190 {
191  return _resolution_sigma;
192 }
193 
194 
195 double Resolution::m() const
196 {
197  return _resolution_exponent;
198 }
199 
200 
201 double Resolution::N() const
202 {
203  return _noise_sigma;
204 }
205 
206 
207 double Resolution::sigma (double p) const
208 //
209 // Purpose: Return the uncertainty for a momentum P.
210 //
211 // Inputs:
212 // p - The momentum
213 //
214 // Returns:
215 // The uncertainty for a momentum P.
216 //
217 {
218  p = std::fabs(p);
219  if (_inverse)
220  p = 1 / p;
221 
225 }
226 
227 
228 double Resolution::pick (double x, double p, CLHEP::HepRandomEngine& engine) const
229 //
230 // Purpose: Given a value X, measured for an object with momentum P,
231 // pick a new value from a Gaussian distribution
232 // described by this resolution --- with mean X and width sigma(P).
233 //
234 // Inputs:
235 // x - The quantity value (distribution mean).
236 // p - The momentum, for calculating the width.
237 // engine - The underlying RNG.
238 //
239 // Returns:
240 // A smeared value of X.
241 //
242 {
243  CLHEP::RandGauss gen (engine);
244  if (_inverse)
245  return 1 / gen.fire (1 / x, sigma (p));
246  else
247  return gen.fire (x, sigma (p));
248 }
249 
250 
259 std::ostream& operator<< (std::ostream& s, const Resolution& r)
260 //
261 // Purpose: Dump this object to S.
262 //
263 // Inputs:
264 // s - The stream to which to write.
265 // r - The object to dump.
266 //
267 // Returns:
268 // The stream S.
269 //
270 {
271  if (r._inverse) s << "-";
272  s << r._constant_sigma << ","
273  << r._resolution_sigma << ","
274  << r._noise_sigma;
275  return s;
276 }
277 
278 
279 } // namespace hitfit
double R() const
Return the R term (resolution term)
Definition: Resolution.cc:189
Calculate and represent resolution for a physical quantity.
Definition: Resolution.h:102
double m() const
Return the exponent factor in the resolution term.
Definition: Resolution.cc:195
bool inverse() const
Return the setting of the inverse flag.
Definition: Resolution.cc:171
friend std::ostream & operator<<(std::ostream &s, const Resolution &r)
Output stream operator, print the content of this Resolution to an output stream. ...
Definition: Resolution.cc:259
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:228
uint16_t size_type
Definition: Electron.h:6
double _resolution_exponent
Definition: Resolution.h:221
T sqrt(T t)
Definition: SSEVec.h:18
double sigma(double p) const
Return the uncertainty for a variable with magnitude p.
Definition: Resolution.cc:207
def gen(fragment, howMuch)
Production test section ####.
Resolution(std::string s="")
Constructor, initialize from a string.
Definition: Resolution.cc:90
double _resolution_sigma
Definition: Resolution.h:216
double N() const
Return the N term (noise term)
Definition: Resolution.cc:201
Calculate and represent resolution for a physical quantity.
double C() const
Return the C term (constant term)
Definition: Resolution.cc:183
double _constant_sigma
Definition: Resolution.h:211