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 
38 #include <cmath>
39 #include <ostream>
40 #include <cctype>
41 
42 using std::isspace;
43 using std::ostream;
44 using std::string;
45 
46 namespace {
47 
56  string field(string s, int i)
57  //
58  // Purpose: Extract the Ith slash-separated field from S.
59  //
60  // Inputs:
61  // s - The string to analyze.
62  // i - Field number (starting with 0).
63  //
64  // Returns:
65  // Field I of S.
66  //
67  {
69  while (i > 0) {
70  pos = s.find('/', pos);
71  if (pos == string::npos)
72  return "";
73  ++pos;
74  --i;
75  }
76 
77  string::size_type pos2 = s.find('/', pos + 1);
78  if (pos2 != string::npos)
79  pos2 = pos2 - pos;
80 
81  while (pos < pos2 && isspace(s[pos]))
82  ++pos;
83 
84  return s.substr(pos, pos2);
85  }
86 
87 } // unnamed namespace
88 
89 namespace hitfit {
90 
92  //
93  // Purpose: Default constructor. Create a vector resolution object with
94  // infinite precision.v
95  //
96  : _p_res("0,0,0"),
97  _eta_res("0,0,0"),
98  _phi_res("0,0,0"),
99  _use_et(false)
100 
101  {}
102 
104  //
105  // Purpose: Constructor.
106  //
107  // Inputs:
108  // s - String encoding the resolution parameters, as described
109  // in the comments in the header.
110  //
111  : _p_res(field(s, 0)), _eta_res(field(s, 1)), _phi_res(field(s, 2)), _use_et(field(s, 3) == "et") {}
112 
114  const Resolution& eta_res,
115  const Resolution& phi_res,
116  bool use_et /*= false*/)
117  //
118  // Purpose: Constructor from individual resolution objects.
119  //
120  // Inputs:
121  // p_res - Momentum resolution object.
122  // eta_res - Eta resolution object.
123  // phi_res - Phi resolution object.
124  // use_et - If true, momentum resolution is based on pt, not p.
125  //
126  : _p_res(p_res), _eta_res(eta_res), _phi_res(phi_res), _use_et(use_et) {}
127 
129  //
130  // Purpose: Return the momentum resolution object.
131  //
132  // Returns:
133  // The momentum resolution object.
134  //
135  {
136  return _p_res;
137  }
138 
140  //
141  // Purpose: Return the eta resolution object.
142  //
143  // Returns:
144  // The eta resolution object.
145  //
146  {
147  return _eta_res;
148  }
149 
151  //
152  // Purpose: Return the phi resolution object.
153  //
154  // Returns:
155  // The phi resolution object.
156  //
157  {
158  return _phi_res;
159  }
160 
162  //
163  // Purpose: Return the use_et flag.
164  //
165  // Returns:
166  // The use_et flag.
167  //
168  {
169  return _use_et;
170  }
171 
172  namespace {
173 
185  double find_sigma(const Fourvec& v, const Resolution& res, bool use_et)
186  //
187  // Purpose: Helper for *_sigma functions below.
188  //
189  // Inputs:
190  // v - The 4-vector.
191  // res - The resolution object.
192  // use_et - Use_et flag.
193  //
194  // Returns:
195  // The result of res.sigma() (not corrected for e/et difference).
196  //
197  {
198  double ee = use_et ? v.perp() : v.e(); // ??? is perp() correct here?
199  return res.sigma(ee);
200  }
201 
202  } // unnamed namespace
203 
204  double Vector_Resolution::p_sigma(const Fourvec& v) const
205  //
206  // Purpose: Calculate momentum resolution for 4-vector V.
207  //
208  // Inputs:
209  // v - The 4-vector.
210  //
211  // Returns:
212  // The momentum resolution for 4-vector V.
213  //
214  {
215  double sig = find_sigma(v, _p_res, _use_et);
216  if (_use_et) {
217  if (_p_res.inverse()) {
218  sig *= v.perp() / v.e();
219  } else {
220  sig *= v.e() / v.perp();
221  }
222  }
223  return sig;
224  }
225 
227  //
228  // Purpose: Calculate eta resolution for 4-vector V.
229  //
230  // Inputs:
231  // v - The 4-vector.
232  //
233  // Returns:
234  // The eta resolution for 4-vector V.
235  //
236  {
237  return find_sigma(v, _eta_res, _use_et);
238  }
239 
241  //
242  // Purpose: Calculate phi resolution for 4-vector V.
243  //
244  // Inputs:
245  // v - The 4-vector.
246  //
247  // Returns:
248  // The phi resolution for 4-vector V.
249  //
250  {
251  return find_sigma(v, _phi_res, _use_et);
252  }
253 
254  namespace {
255 
267  void smear_eta(Fourvec& v, double ee, const Resolution& res, CLHEP::HepRandomEngine& engine)
268  //
269  // Purpose: Smear the eta direction of V.
270  //
271  // Inputs:
272  // v - The 4-vector to smear.
273  // ee - Energy for sigma calculation.
274  // res - The resolution object, giving the amount of smearing.
275  // engine - The underlying RNG.
276  //
277  // Outputs:
278  // v - The smeared 4-vector.
279  //
280  {
281  double rot = res.pick(0, ee, engine);
282  roteta(v, rot);
283  }
284 
296  void smear_phi(Fourvec& v, double ee, const Resolution& res, CLHEP::HepRandomEngine& engine)
297  //
298  // Purpose: Smear the phi direction of V.
299  //
300  // Inputs:
301  // v - The 4-vector to smear.
302  // ee - Energy for sigma calculation.
303  // res - The resolution object, giving the amount of smearing.
304  // engine - The underlying RNG.
305  //
306  // Outputs:
307  // v - The smeared 4-vector.
308  //
309  {
310  double rot = res.pick(0, ee, engine);
311  v.rotateZ(rot);
312  }
313 
314  } // unnamed namespace
315 
316  void Vector_Resolution::smear(Fourvec& v, CLHEP::HepRandomEngine& engine, bool do_smear_dir /*= false*/) const
317  //
318  // Purpose: Smear a 4-vector according to the resolutions.
319  //
320  // Inputs:
321  // v - The 4-vector to smear.
322  // engine - The underlying RNG.
323  // do_smear_dir- If false, only smear the energy.
324  //
325  // Outputs:
326  // v - The smeared 4-vector.
327  //
328  {
329  double ee = _use_et ? v.perp() : v.e(); // ??? is perp() correct?
330  v *= _p_res.pick(ee, ee, engine) / ee;
331 
332  if (do_smear_dir) {
333  smear_eta(v, ee, _eta_res, engine);
334  smear_phi(v, ee, _phi_res, engine);
335  }
336  }
337 
346  std::ostream& operator<<(std::ostream& s, const Vector_Resolution& r)
347  //
348  // Purpose: Dump this object to S.
349  //
350  // Inputs:
351  // s - The stream to which to write.
352  // r - The object to dump.
353  //
354  // Returns:
355  // The stream S.
356  //
357  {
358  s << r._p_res << "/ " << r._eta_res << "/ " << r._phi_res;
359  if (r._use_et)
360  s << "/et";
361  s << "\n";
362  return s;
363  }
364 
365 } // 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:98
bool use_et() const
Return the use_et flag.
bool inverse() const
Return the setting of the inverse flag.
Definition: Resolution.cc:144
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:156
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
uint16_t size_type
Definition: Electron.h:6
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:55
double sigma(double p) const
Return the uncertainty for a variable with magnitude p.
Definition: Resolution.cc:163
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 ...