CMS 3D CMS Logo

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