CMS 3D CMS Logo

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