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 //
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 {
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),
145  _resolution_exponent(m),
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),
164  _resolution_exponent(0),
165  _noise_sigma (res),
166  _inverse (inverse)
167 {
168 }
169 
170 
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  if (_inverse)
219  p = 1 / p;
220 
221  return sqrt ((_constant_sigma*_constant_sigma*p +
222  _resolution_sigma*_resolution_sigma)*p +
223  _noise_sigma*_noise_sigma);
224 }
225 
226 
227 double Resolution::pick (double x, double p, CLHEP::HepRandomEngine& engine) const
228 //
229 // Purpose: Given a value X, measured for an object with momentum P,
230 // pick a new value from a Gaussian distribution
231 // described by this resolution --- with mean X and width sigma(P).
232 //
233 // Inputs:
234 // x - The quantity value (distribution mean).
235 // p - The momentum, for calculating the width.
236 // engine - The underlying RNG.
237 //
238 // Returns:
239 // A smeared value of X.
240 //
241 {
242  CLHEP::RandGauss gen (engine);
243  if (_inverse)
244  return 1 / gen.fire (1 / x, sigma (p));
245  else
246  return gen.fire (x, sigma (p));
247 }
248 
249 
258 std::ostream& operator<< (std::ostream& s, const Resolution& r)
259 //
260 // Purpose: Dump this object to S.
261 //
262 // Inputs:
263 // s - The stream to which to write.
264 // r - The object to dump.
265 //
266 // Returns:
267 // The stream S.
268 //
269 {
270  if (r._inverse) s << "-";
271  s << r._constant_sigma << ","
272  << r._resolution_sigma << ","
273  << r._noise_sigma;
274  return s;
275 }
276 
277 
278 } // namespace hitfit
int i
Definition: DBlmapReader.cc:9
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
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:227
uint16_t size_type
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
double _resolution_exponent
Definition: Resolution.h:221
T x() const
Cartesian x coordinate.
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
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:90
double _resolution_sigma
Definition: Resolution.h:216
double N() const
Return the N term (noise term)
Definition: Resolution.cc:201
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.
double C() const
Return the C term (constant term)
Definition: Resolution.cc:183
double _constant_sigma
Definition: Resolution.h:211