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))
119  _resolution_sigma = 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  if (_inverse)
175  p = 1 / p;
176 
177  return sqrt((_constant_sigma * _constant_sigma * p + _resolution_sigma * _resolution_sigma) * p +
178  _noise_sigma * _noise_sigma);
179  }
180 
181  double Resolution::pick(double x, double p, CLHEP::HepRandomEngine& engine) const
182  //
183  // Purpose: Given a value X, measured for an object with momentum P,
184  // pick a new value from a Gaussian distribution
185  // described by this resolution --- with mean X and width sigma(P).
186  //
187  // Inputs:
188  // x - The quantity value (distribution mean).
189  // p - The momentum, for calculating the width.
190  // engine - The underlying RNG.
191  //
192  // Returns:
193  // A smeared value of X.
194  //
195  {
196  CLHEP::RandGauss gen(engine);
197  if (_inverse)
198  return 1 / gen.fire(1 / x, sigma(p));
199  else
200  return gen.fire(x, sigma(p));
201  }
202 
211  std::ostream& operator<<(std::ostream& s, const Resolution& r)
212  //
213  // Purpose: Dump this object to S.
214  //
215  // Inputs:
216  // s - The stream to which to write.
217  // r - The object to dump.
218  //
219  // Returns:
220  // The stream S.
221  //
222  {
223  if (r._inverse)
224  s << "-";
225  s << r._constant_sigma << "," << r._resolution_sigma << "," << r._noise_sigma;
226  return s;
227  }
228 
229 } // namespace hitfit
mps_fire.i
i
Definition: mps_fire.py:428
hitfit
Definition: Base_Constrainer.h:43
hitfit::Resolution::_noise_sigma
double _noise_sigma
Definition: Resolution.h:217
hitfit::Resolution::R
double R() const
Return the R term (resolution term)
Definition: Resolution.cc:157
Resolution.h
Calculate and represent resolution for a physical quantity.
hitfit::Resolution::N
double N() const
Return the N term (noise term)
Definition: Resolution.cc:161
watchdog.const
const
Definition: watchdog.py:83
hitfit::Resolution::_inverse
bool _inverse
Definition: Resolution.h:222
hitfit::Resolution::Resolution
Resolution(std::string s="")
Constructor, initialize from a string.
Definition: Resolution.cc:82
alignCSCRings.s
s
Definition: alignCSCRings.py:92
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:78
hitfit::Resolution::C
double C() const
Return the C term (constant term)
Definition: Resolution.cc:155
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
N
#define N
Definition: blowfish.cc:9
hitfit::Resolution::_resolution_sigma
double _resolution_sigma
Definition: Resolution.h:207
gen
Definition: PythiaDecays.h:13
hitfit::operator<<
std::ostream & operator<<(std::ostream &s, const Constraint_Intermed &ci)
Output stream operator, print the content of this Constraint_Intermed to an output stream.
Definition: Constraint_Intermed.cc:268
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
hitfit::Resolution::inverse
bool inverse() const
Return the setting of the inverse flag.
Definition: Resolution.cc:144
res
Definition: Electron.h:6
hitfit::Resolution
Definition: Resolution.h:98
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
alignCSCRings.r
r
Definition: alignCSCRings.py:93
hitfit::Resolution::_constant_sigma
double _constant_sigma
Definition: Resolution.h:202
gen::C
C
Definition: PomwigHadronizer.cc:78
hitfit::Resolution::_resolution_exponent
double _resolution_exponent
Definition: Resolution.h:212
genVertex_cff.x
x
Definition: genVertex_cff.py:13
hitfit::Resolution::pick
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:181
relval_steps.gen
def gen(fragment, howMuch)
Production test section ####.
Definition: relval_steps.py:506
hitfit::Resolution::m
double m() const
Return the exponent factor in the resolution term.
Definition: Resolution.cc:159
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
dttmaxenums::R
Definition: DTTMax.h:29
hitfit::Resolution::sigma
double sigma(double p) const
Return the uncertainty for a variable with magnitude p.
Definition: Resolution.cc:163