CMS 3D CMS Logo

Vector_Resolution.cc
Go to the documentation of this file.
1 //
2 //
3 // File: src/Vector_Resolution.cc
4 // Purpose: Calculate resolutions in p, phi, eta.
5 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
6 //
7 // CMSSW File : src/Vector_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 
39 #include <cmath>
40 #include <ostream>
41 #include <cctype>
42 
43 
44 using std::ostream;
45 using std::string;
46 using std::isspace;
47 
48 
49 namespace {
50 
59 string field (string s, int i)
60 //
61 // Purpose: Extract the Ith slash-separated field from S.
62 //
63 // Inputs:
64 // s - The string to analyze.
65 // i - Field number (starting with 0).
66 //
67 // Returns:
68 // Field I of S.
69 //
70 {
72  while (i > 0) {
73  pos = s.find ('/', pos);
74  if (pos == string::npos)
75  return "";
76  ++pos;
77  --i;
78  }
79 
80  string::size_type pos2 = s.find ('/', pos+1);
81  if (pos2 != string::npos)
82  pos2 = pos2 - pos;
83 
84  while (pos < pos2 && isspace (s[pos]))
85  ++pos;
86 
87  return s.substr (pos, pos2);
88 }
89 
90 
91 } // unnamed namespace
92 
93 
94 namespace hitfit {
95 
96 
98 //
99 // Purpose: Default constructor. Create a vector resolution object with
100 // infinite precision.v
101 //
102  : _p_res ("0,0,0"),
103  _eta_res ("0,0,0"),
104  _phi_res ("0,0,0"),
105  _use_et (false)
106 
107 {
108 }
109 
110 
112 //
113 // Purpose: Constructor.
114 //
115 // Inputs:
116 // s - String encoding the resolution parameters, as described
117 // in the comments in the header.
118 //
119  : _p_res (field (s, 0)),
120  _eta_res (field (s, 1)),
121  _phi_res (field (s, 2)),
122  _use_et (field (s, 3) == "et")
123 {
124 }
125 
126 
128  const Resolution& eta_res,
129  const Resolution& phi_res,
130  bool use_et /*= false*/)
131 //
132 // Purpose: Constructor from individual resolution objects.
133 //
134 // Inputs:
135 // p_res - Momentum resolution object.
136 // eta_res - Eta resolution object.
137 // phi_res - Phi resolution object.
138 // use_et - If true, momentum resolution is based on pt, not p.
139 //
140  : _p_res (p_res),
141  _eta_res (eta_res),
142  _phi_res (phi_res),
143  _use_et (use_et)
144 {
145 }
146 
147 
149 //
150 // Purpose: Return the momentum resolution object.
151 //
152 // Returns:
153 // The momentum resolution object.
154 //
155 {
156  return _p_res;
157 }
158 
159 
161 //
162 // Purpose: Return the eta resolution object.
163 //
164 // Returns:
165 // The eta resolution object.
166 //
167 {
168  return _eta_res;
169 }
170 
171 
173 //
174 // Purpose: Return the phi resolution object.
175 //
176 // Returns:
177 // The phi resolution object.
178 //
179 {
180  return _phi_res;
181 }
182 
183 
185 //
186 // Purpose: Return the use_et flag.
187 //
188 // Returns:
189 // The use_et flag.
190 //
191 {
192  return _use_et;
193 }
194 
195 
196 namespace {
197 
198 
210 double find_sigma (const Fourvec& v, const Resolution& res, bool use_et)
211 //
212 // Purpose: Helper for *_sigma functions below.
213 //
214 // Inputs:
215 // v - The 4-vector.
216 // res - The resolution object.
217 // use_et - Use_et flag.
218 //
219 // Returns:
220 // The result of res.sigma() (not corrected for e/et difference).
221 //
222 {
223  double ee = use_et ? v.perp() : v.e(); // ??? is perp() correct here?
224  return res.sigma (ee);
225 }
226 
227 
228 } // unnamed namespace
229 
230 
231 double Vector_Resolution::p_sigma (const Fourvec& v) const
232 //
233 // Purpose: Calculate momentum resolution for 4-vector V.
234 //
235 // Inputs:
236 // v - The 4-vector.
237 //
238 // Returns:
239 // The momentum resolution for 4-vector V.
240 //
241 {
242  double sig = find_sigma (v, _p_res, _use_et);
243  if (_use_et) {
244  if(_p_res.inverse()){
245  sig *= v.perp() / v.e();
246  }else{
247  sig *= v.e() / v.perp();
248  }
249  }
250  return sig;
251 }
252 
253 
255 //
256 // Purpose: Calculate eta resolution for 4-vector V.
257 //
258 // Inputs:
259 // v - The 4-vector.
260 //
261 // Returns:
262 // The eta resolution for 4-vector V.
263 //
264 {
265  return find_sigma (v, _eta_res, _use_et);
266 }
267 
268 
270 //
271 // Purpose: Calculate phi resolution for 4-vector V.
272 //
273 // Inputs:
274 // v - The 4-vector.
275 //
276 // Returns:
277 // The phi resolution for 4-vector V.
278 //
279 {
280  return find_sigma (v, _phi_res, _use_et);
281 }
282 
283 
284 
285 namespace {
286 
287 
299 void smear_eta (Fourvec& v, double ee,
300  const Resolution& res, CLHEP::HepRandomEngine& engine)
301 //
302 // Purpose: Smear the eta direction of V.
303 //
304 // Inputs:
305 // v - The 4-vector to smear.
306 // ee - Energy for sigma calculation.
307 // res - The resolution object, giving the amount of smearing.
308 // engine - The underlying RNG.
309 //
310 // Outputs:
311 // v - The smeared 4-vector.
312 //
313 {
314  double rot = res.pick (0, ee, engine);
315  roteta (v, rot);
316 }
317 
318 
330 void smear_phi (Fourvec& v, double ee,
331  const Resolution& res, CLHEP::HepRandomEngine& engine)
332 //
333 // Purpose: Smear the phi direction of V.
334 //
335 // Inputs:
336 // v - The 4-vector to smear.
337 // ee - Energy for sigma calculation.
338 // res - The resolution object, giving the amount of smearing.
339 // engine - The underlying RNG.
340 //
341 // Outputs:
342 // v - The smeared 4-vector.
343 //
344 {
345  double rot = res.pick (0, ee, engine);
346  v.rotateZ (rot);
347 }
348 
349 
350 } // unnamed namespace
351 
352 
354  CLHEP::HepRandomEngine& engine,
355  bool do_smear_dir /*= false*/) const
356 //
357 // Purpose: Smear a 4-vector according to the resolutions.
358 //
359 // Inputs:
360 // v - The 4-vector to smear.
361 // engine - The underlying RNG.
362 // do_smear_dir- If false, only smear the energy.
363 //
364 // Outputs:
365 // v - The smeared 4-vector.
366 //
367 {
368  double ee = _use_et ? v.perp() : v.e(); // ??? is perp() correct?
369  v *= _p_res.pick (ee, ee, engine) / ee;
370 
371  if (do_smear_dir) {
372  smear_eta (v, ee, _eta_res, engine);
373  smear_phi (v, ee, _phi_res, engine);
374  }
375 }
376 
377 
386 std::ostream& operator<< (std::ostream& s, const Vector_Resolution& r)
387 //
388 // Purpose: Dump this object to S.
389 //
390 // Inputs:
391 // s - The stream to which to write.
392 // r - The object to dump.
393 //
394 // Returns:
395 // The stream S.
396 //
397 {
398  s << r._p_res << "/ " << r._eta_res << "/ " << r._phi_res;
399  if (r._use_et)
400  s << "/et";
401  s << "\n";
402  return s;
403 }
404 
405 
406 } // namespace hitfit
Calculate and represent resolution for a vector of momentum , pseudorapidity , and azimuthal angle ...
Define three-vector and four-vector classes for the HitFit package, and supply a few additional opera...
Calculate and represent resolution for a physical quantity.
Definition: Resolution.h:102
bool use_et() const
Return the use_et flag.
bool inverse() const
Return the setting of the inverse flag.
Definition: Resolution.cc:171
double phi_sigma(const Fourvec &v) const
Calculate the azimuthal angle resolution of a four-momentum.
void roteta(Fourvec &v, double eta)
Rotate four-vector v through a polar angle such that the four-vector pseudorapidity changes by a desi...
Definition: fourvec.cc:162
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
Definition: Electron.h:4
Vector_Resolution()
Constructor, instantiate an instance of Vector_Resolution with infinite precision.
double p_sigma(const Fourvec &v) const
Calculate the momentum resolution of a four-momentum.
const Resolution & eta_res() const
Return a constant reference to the pseudorapidity resolution.
double eta_sigma(const Fourvec &v) const
Calculate the pseudorapidity resolution of a four-momentum.
CLHEP::HepLorentzVector Fourvec
Typedef for a HepLorentzVector.
Definition: fourvec.h:57
double sigma(double p) const
Return the uncertainty for a variable with magnitude p.
Definition: Resolution.cc:207
void smear(Fourvec &v, CLHEP::HepRandomEngine &engine, bool do_smear_dir=false) const
Smear a four-momentum according to the resolutions.
const Resolution & p_res() const
Return a constant reference to the momentum resolution.
const Resolution & phi_res() const
Return a constant reference to the azimuthal angle resolution.
friend std::ostream & operator<<(std::ostream &s, const Vector_Resolution &r)
Output stream operator, print the content of this Vector_Resolution object to an output stream...
Calculate and represent resolution for a vector of , pseudorapidity , and azimuthal angle ...