CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ResolutionHelper.cc
Go to the documentation of this file.
3 #include <cmath>
4 #include <iostream>
5 
6 using namespace std; // faster sqrt, sqrt, pow(x,2)....
7 
8 void
10  AlgebraicSymMatrix44 &covariance,
11  const math::XYZTLorentzVector &initialP4)
12 {
13  double inv;
14  switch (parametrization) {
17  // for us parameter[3] = mass, for KinFitter parameter[3] = mass/initialP4.mass();
18  inv = 1.0/initialP4.mass();
19  for (int i = 0; i < 4; i++) {
20  covariance(3,i) *= inv;
21  }
22  covariance(3,3) *= inv;
23  break;
25  // for us parameter[3] = energy, for KinFitter parameter[3] = energy/initialP4.energy();
26  inv = 1.0/initialP4.energy();
27  for (int i = 0; i < 4; i++) {
28  covariance(3,i) *= inv;
29  }
30  covariance(3,3) *= inv;
31  break;
32  default:
33  ;// nothing to do
34  }
35 }
36 
39 {
40  switch (parametrization) {
41  // ==== CARTESIAN ====
45  {
46  // p2 = px^2 + py^2 + pz^2
47  // dp/dp_i = p_i/p ==> it is a unit vector!
48  AlgebraicVector3 derivs(p4.X(), p4.Y(), p4.Z());
49  derivs.Unit();
50  return sqrt(ROOT::Math::Similarity(derivs, covariance.Sub<AlgebraicSymMatrix33>(0,0)));
51  }
52  // ==== SPHERICAL (P) ====
56  return sqrt(covariance(0,0));
57  // ==== SPHERICAL (1/P) ====
59  return sqrt(covariance(0,0)) * (p4.P2());
60  // ==== HEP CYLINDRICAL (with Pt = Et, P = E) ====
63  return getResolE(parametrization,covariance,p4);
64  // ==== OTHER ====
66  throw cms::Exception("Invalid parametrization") << parametrization;
67  default:
68  throw cms::Exception("Not Implemented") << "getResolP not yet implemented for parametrization " << parametrization ;
69  }
70 }
73 {
74  switch (parametrization) {
75  // ==== CARTESIAN ====
79  {
80  double pti2 = 1.0/(p4.Perp2());
81  return sqrt( ( covariance(0,0) * p4.Px()*p4.Px() +
82  covariance(1,1) * p4.Py()*p4.Py() +
83  2*covariance(0,1) * p4.Px()*p4.Py()) * pti2 );
84  }
85  // ==== SPHERICAL (P, Theta) ====
89  {
90  // pt = p * sin(theta)
91  double a = sin(p4.Theta());
92  double b = p4.P() * cos(p4.Theta());
93  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
94  }
95  // ==== SPHERICAL (1/P) ====
97  {
98  // pt = (1/pi) * sin(theta)
99  double p = p4.P();
100  double a = - (p*p) * sin(p4.Theta());
101  double b = p * cos(p4.Theta());
102  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
103  }
104  // ==== HEP CYLINDRICAL (with Pt = Et) ====
107  return sqrt(covariance(0,0));
109  throw cms::Exception("Invalid parametrization") << parametrization;
110  default:
111  throw cms::Exception("Not Implemented") << "getResolPt not yet implemented for parametrization " << parametrization ;
112  }
113 }
114 
117 {
118  switch (parametrization) {
119  // ==== SPHERICAL (P) ====
123  return 1.0/p4.P2() * sqrt(covariance(0,0));
124  // ==== SPHERICAL (1/P) ====
126  return sqrt(covariance(0,0));
127  // ==== OTHER ====
133  return 1.0/p4.P2() * getResolP(parametrization, covariance, p4);
135  throw cms::Exception("Invalid parametrization") << parametrization;
136  default:
137  throw cms::Exception("Not Implemented") << "getResolPInv not yet implemented for parametrization " << parametrization ;
138  }
139 }
140 
143 {
144  switch (parametrization) {
145  // ==== CARTESIAN ====
149  return sqrt(covariance(0,0));
150  // ==== SPHERICAL (P) ====
155  {
156  // Px = P * sin(theta) * cos(phi)
157  double p = p4.P();
158  AlgebraicVector3 derivs;
159  derivs[0] = sin(p4.Theta()) * cos(p4.Phi()); // now let's hope gcc does common subexpr optimiz.
160  if (parametrization == pat::CandKinResolution::MCPInvSpher) {
161  derivs[0] *= -(p*p);
162  }
163  derivs[1] = p * cos(p4.Theta()) * cos(p4.Phi());
164  derivs[2] = p * sin(p4.Theta()) * -sin(p4.Phi());
165  return sqrt(ROOT::Math::Similarity(derivs, covariance.Sub<AlgebraicSymMatrix33>(0,0)));
166  }
167  // ==== HEP CYLINDRICAL (with Pt = Et) ====
170  {
171  // Px = Pt * cos(phi)
172  double a = cos(p4.Phi());
173  double b = - p4.Pt() * sin(p4.Phi());
174  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(2,0) + b*b*covariance(2,2));
175  }
176  // ==== OTHERS ====
178  throw cms::Exception("Invalid parametrization") << parametrization;
179  default:
180  throw cms::Exception("Not Implemented") << "getResolPx not yet implemented for parametrization " << parametrization ;
181  }
182 }
185 {
186  switch (parametrization) {
187  // ==== CARTESIAN ====
191  return sqrt(covariance(1,1));
192  // ==== SPHERICAL (P) ====
197  {
198  // Py = P * sin(theta) * sin(phi)
199  double p = p4.P();
200  AlgebraicVector3 derivs;
201  derivs[0] = sin(p4.Theta()) * sin(p4.Phi()); // now let's hope gcc does common subexpr optimiz.
202  if (parametrization == pat::CandKinResolution::MCPInvSpher) {
203  derivs[0] *= -(p*p);
204  }
205  derivs[1] = p * cos(p4.Theta()) * sin(p4.Phi());
206  derivs[2] = p * sin(p4.Theta()) * cos(p4.Phi());
207  return sqrt(ROOT::Math::Similarity(derivs, covariance.Sub<AlgebraicSymMatrix33>(0,0)));
208  }
209  // ==== HEP CYLINDRICAL (with Pt = Et) ====
212  {
213  // Py = Pt * sin(phi)
214  double a = sin(p4.Phi());
215  double b = p4.Pt() * cos(p4.Phi());
216  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(2,0) + b*b*covariance(2,2));
217  }
218  // ==== OTHERS ====
220  throw cms::Exception("Invalid parametrization") << parametrization;
221  default:
222  throw cms::Exception("Not Implemented") << "getResolPy not yet implemented for parametrization " << parametrization ;
223  }
224 }
227 {
228  switch (parametrization) {
229  // ==== CARTESIAN ====
233  return sqrt(covariance(2,2));
234  // ==== SPHERICAL (P) ====
238  {
239  // Pz = P * cos(theta)
240  double a = cos(p4.Theta());
241  double b = -p4.P() * sin(p4.Theta());
242  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(1,0) + b*b*covariance(1,1));
243  }
245  {
246  // Pz = P * cos(theta)
247  double p = p4.P();
248  double a = -p*p*cos(p4.Theta());
249  double b = -p * sin(p4.Theta());
250  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(1,0) + b*b*covariance(1,1));
251  }
252  // ==== HEP CYLINDRICAL (with Pt = Et) ====
254  {
255  // Pz = Pt * ctg(theta) d ctg(x) = -1/sin^2(x)
256  double s = sin(p4.Theta()), c = cos(p4.Theta());
257  double a = c/s;
258  double b = - p4.Pt() / (s*s);
259  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(1,0) + b*b*covariance(1,1));
260  }
262  {
263  // Pz = Pt * sinh(eta)
264  double a = sinh(p4.Eta());
265  double b = p4.Et() * cosh(p4.Eta());
266  return sqrt(a*a*covariance(0,0) + 2*a*b*covariance(1,0) + b*b*covariance(1,1));
267  }
268  // ==== OTHERS ====
270  throw cms::Exception("Invalid parametrization") << parametrization;
271  default:
272  throw cms::Exception("Not Implemented") << "getResolPz not yet implemented for parametrization " << parametrization ;
273  }
274 }
275 
278 {
279  switch (parametrization) {
280  // ======= ENERGY BASED ==========
283  return sqrt(covariance(3,3));
284  // ======= ET BASED ==========
286  {
287  // E = Et/Sin(theta)
288  double a = 1.0/sin(p4.Theta()); // dE/dEt
289  double b = - a*a* p4.Et() * cos(p4.Theta()); // dE/dTh
290  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
291  }
293  {
294  // E = Et/Sin(Theta(eta))
295  double th = p4.Theta();
296  double a = 1.0/sin(th); // dE/dEt
297  double b = a* p4.Et() * cos(th); // dE/dEta: dTh/dEta = - 1.0/sin(theta) = - dE/dEt
298  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
299  }
300  // ======= MASS BASED ==========
302  {
303  AlgebraicVector4 xoE(p4.X(), p4.Y(), p4.Z(), p4.M());
304  xoE *= 1/p4.E();
305  return sqrt(ROOT::Math::Similarity(xoE, covariance));
306  }
308  {
309  AlgebraicVector4 xoE(p4.X(), p4.Y(), p4.Z(), 0);
310  xoE *= 1/p4.E();
311  return sqrt(ROOT::Math::Similarity(xoE, covariance));
312  }
314  {
315  // E = sqrt(P^2 + m^2)
316  double einv = 1.0/p4.E();
317  double a = p4.P()*einv; // dE/dP
318  double b = p4.M()*einv; // dE/dm
319  return sqrt(a*a*covariance(0,0) + b*b*covariance(3,3) + 2*a*b*covariance(0,3));
320  }
322  {
323  // E = sqrt(P^2 + m^2); |dE/dP| = |P/E| = P/E
324  return p4.P()/p4.E() * sqrt(covariance(0,0));
325  }
327  {
328  // E = sqrt(P^2 + m^2); |dE/d(1/P)| = P^2 |dE/dP| = P^3/E
329  double p = p4.P();
330  return p*p*p/p4.E()*sqrt(covariance(0,0));
331  }
332  // ======= OTHER ==========
334  throw cms::Exception("Invalid parametrization") << parametrization;
335  default:
336  throw cms::Exception("Not Implemented") << "getResolE not yet implemented for parametrization " << parametrization ;
337  }
338 }
339 
342 {
343  switch (parametrization) {
344  // ======= ENERGY BASED ==========
346  {
347  // Et^2 = E^2 * (Pt^2/P^2)
348  double pt2 = p4.Perp2();
349  double pz2 = ROOT::Math::Square(p4.Pz()), p2 = pt2 + pz2;
350  double e2OverP4 = ROOT::Math::Square(p4.E()/p2);
351  AlgebraicVector4 derivs(p4.Px(), p4.Py(), p4.Pz(), p4.E());
352  derivs *= (1.0/p4.Et());
353  derivs[0] *= pz2 * e2OverP4;
354  derivs[1] *= pz2 * e2OverP4;
355  derivs[2] *= -pt2 * e2OverP4;
356  derivs[3] *= pt2/p2;
357  return sqrt(ROOT::Math::Similarity(derivs, covariance));
358  }
360  {
361  // Et = E * Sin(Theta)
362  double st = sin(p4.Theta()), ct = cos(p4.Theta());
363  return sqrt( st*st * covariance(3,3) +
364  ROOT::Math::Square(ct*p4.E()) * covariance(1,1) +
365  2*st*ct*p4.E() * covariance(1,3)
366  );
367  }
368  // ======= ET BASED ==========
371  return sqrt(covariance(0,0));
372  // ======= MASS BASED ==========
375  {
376  // Et^2 = E^2 Sin^2(th) = (p^2 + m^2) * (pt^2) / p^2
377  double pt2 = p4.Perp2();
378  double p2 = pt2 + ROOT::Math::Square(p4.Pz());
379  double e2 = p2 + p4.M2();
380  double s2 = pt2/p2, pi2 = 1.0/p2;
381  double et = sqrt(e2 * s2);
382  AlgebraicVector4 derivs(p4.Px(), p4.Py(), p4.Pz(), p4.M());
383  derivs *= 1.0/et;
384  derivs[0] *= (s2 + e2*pi2*(1.0 - pt2*pi2) );
385  derivs[1] *= (s2 + e2*pi2*(1.0 - pt2*pi2) );
386  derivs[2] *= (s2 - e2*pt2*pi2*pi2);
387  if (parametrization == pat::CandKinResolution::Cart) {
388  derivs[3] *= s2;
389  return sqrt(ROOT::Math::Similarity(derivs, covariance));
390  } else {
391  derivs[3] = 0;
392  return sqrt(ROOT::Math::Similarity(derivs, covariance)); // test if Sub<33> is faster
393  }
394  }
396  {
397  // Et = E sin(theta); dE/dM = M/E, dE/dP = P/E
398  double s = sin(p4.Theta()), c = cos(p4.Theta());
399  double e = p4.E();
400  AlgebraicVector4 derivs( p4.P()/e * s, e * c, 0, p4.M()/e * s);
401  return sqrt(ROOT::Math::Similarity(derivs, covariance));
402  }
404  {
405  // Et = E sin(theta); dE/dP = P/E
406  double s = sin(p4.Theta()), c = cos(p4.Theta());
407  double e = p4.E();
408  double a = p4.P()* s / e;
409  double b = e * c;
410  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
411  }
413  {
414  // Et = E sin(theta); dE/dP = P/E -> dE/d(1/P) = - P^2 dE/dP = - P^3 / E
415  double s = sin(p4.Theta()), c = cos(p4.Theta());
416  double p = p4.P(), e = p4.E();
417  double a = ( - p * p * p / e ) * s;
418  double b = e * c;
419  return sqrt(a*a*covariance(0,0) + b*b*covariance(1,1) + 2*a*b*covariance(0,1));
420  }
421  // ======= OTHER ==========
423  throw cms::Exception("Invalid parametrization") << parametrization;
424  default:
425  throw cms::Exception("Not Implemented") << "getResolEt not yet implemented for parametrization " << parametrization ;
426  }
427 }
428 
429 
432 {
433  switch (parametrization) {
434  // ====== MASS CONSTRAINED =====
440  return 0;
441  // ======= MASS BASED ==========
444  return sqrt(covariance(3,3));
445  // ======= ENERGY BASED ==========
447  { // M^2 = E^2 - P^2
448  double dMdE = p4.E()/p4.M(), dMdP = - p4.P()/p4.M();
449  return sqrt( dMdP*dMdP*covariance(0,0) +
450  2*dMdP*dMdE*covariance(0,3) +
451  dMdE*dMdE*covariance(3,3));
452  }
454  { // M^2 = E^2 - sum_i P_i^2
455  AlgebraicVector4 derivs(-p4.Px(), -p4.Py(), -p4.Pz(), p4.E());
456  derivs *= 1.0/p4.M();
457  return sqrt(ROOT::Math::Similarity(derivs, covariance));
458  }
459  throw cms::Exception("Not Implemented") << "getResolM not yet implemented for parametrization " << parametrization ;
461  throw cms::Exception("Invalid parametrization") << parametrization;
462  default:
463  throw cms::Exception("Not Implemented") << "getResolM not yet implemented for parametrization " << parametrization ;
464  }
465 }
466 
467 inline double DetaDtheta(double theta) {
468  // y = -ln(tg(x/2)) =>
469  // y' = - 1/tg(x/2) * 1/(cos(x/2))^2 * 1/2 = - 1 / (2 * sin(x/2) * cos(x/2)) = -1/sin(x)
470  return -1.0/sin(theta);
471 }
472 inline double DthetaDeta(double eta) {
473  // y = 2 atan(exp(-x))
474  // y' = 2 * 1/(1+exp^2) * exp(-x) * (-1) = - 2 * exp/(1+exp^2) = - 2 / (exp + 1/exp)
475  double e = exp(-eta);
476  return -2.0/(e + 1.0/e);
477 }
478 
481 {
482  switch (parametrization) {
486  // dEta = dTheta * dEta/dTheta
487  return abs(DetaDtheta(p4.Theta())) * getResolTheta(parametrization, covariance, p4);
488  case pat::CandKinResolution::ESpher: // all the ones which have
489  case pat::CandKinResolution::MCPInvSpher: // theta as parameter 1
493  return sqrt(covariance(1,1))*abs(DetaDtheta(p4.Theta()));
494  case pat::CandKinResolution::EtEtaPhi: // as simple as that
495  return sqrt(covariance(1,1));
497  throw cms::Exception("Invalid parametrization") << parametrization;
498  default:
499  throw cms::Exception("Not Implemented") << "getResolEta not yet implemented for parametrization " << parametrization ;
500  }
501 }
504 {
505  switch (parametrization) {
509  {
510  // theta = acos( pz / p ) ; d acos(x) = - 1 / sqrt( 1 - x*x) dx = - p/pt dx
511  double pt2 = p4.Perp2();
512  double p = p4.P(), pi = 1.0/p, pi3 = pi*pi*pi;
513  double dacos = - p/sqrt(pt2);
514  AlgebraicVector3 derivs;
515  derivs[0] = -p4.Px() * p4.Pz() * dacos * pi3;
516  derivs[1] = -p4.Py() * p4.Pz() * dacos * pi3;
517  derivs[2] = pt2 * dacos * pi3;
518  return sqrt(ROOT::Math::Similarity(derivs, covariance.Sub<AlgebraicSymMatrix33>(0,0)));
519  }
520  case pat::CandKinResolution::ESpher: // all the ones which have
521  case pat::CandKinResolution::MCPInvSpher: // theta as parameter 1
525  return sqrt(covariance(1,1));
527  return sqrt(covariance(1,1))*abs(DthetaDeta(p4.Eta()));
529  throw cms::Exception("Invalid parametrization") << parametrization;
530  default:
531  throw cms::Exception("Not Implemented") << "getResolTheta not yet implemented for parametrization " << parametrization ;
532  }
533 }
536 {
537  double pt2 = p4.Perp2();
538  switch (parametrization) {
542  return sqrt( ROOT::Math::Square(p4.Px()) * covariance(1,1) +
543  ROOT::Math::Square(p4.Py()) * covariance(0,0) +
544  -2*p4.Px()*p4.Py() * covariance(0,1)
545  )/pt2;
546  case pat::CandKinResolution::ESpher: // all the ones which have
547  case pat::CandKinResolution::MCPInvSpher: // phi as parameter 2
552  return sqrt(covariance(2,2));
554  throw cms::Exception("Invalid parametrization") << parametrization;
555  default:
556  throw cms::Exception("Not Implemented") << "getResolPhi not yet implemented for parametrization " << parametrization ;
557  }
558 }
double getResolEt(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
int i
Definition: DBlmapReader.cc:9
double DetaDtheta(double theta)
double getResolPz(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolPy(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolP(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolPx(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolPt(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolPhi(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double getResolM(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
void rescaleForKinFitter(const pat::CandKinResolution::Parametrization parametrization, AlgebraicSymMatrix44 &covariance, const math::XYZTLorentzVector &initialP4)
Geom::Theta< T > theta() const
const double pi2
Definition: Thrust.cc:5
double getResolPInv(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
#define abs(x)
Definition: mlp_lapack.h:159
double getResolE(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
T eta() const
tuple s2
Definition: indexGen.py:106
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
ROOT::Math::SMatrix< double, 4, 4, ROOT::Math::MatRepSym< double, 4 > > AlgebraicSymMatrix44
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
T sqrt(T t)
Definition: SSEVec.h:48
double p4[4]
Definition: TauolaWrapper.h:92
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
ROOT::Math::SVector< double, 3 > AlgebraicVector3
double DthetaDeta(double eta)
double p2[4]
Definition: TauolaWrapper.h:90
double b
Definition: hdecay.h:120
math::XYZTLorentzVector LorentzVector
Float e2
Definition: deltaR.h:23
double a
Definition: hdecay.h:121
double getResolEta(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double getResolTheta(pat::CandKinResolution::Parametrization parametrization, const AlgebraicSymMatrix44 &covariance, const pat::CandKinResolution::LorentzVector &p4)
double pi
ROOT::Math::SVector< double, 4 > AlgebraicVector4