Belle II Software  release-08-01-10
DistanceTools.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 
10 #include <analysis/utility/DistanceTools.h>
11 #include <framework/logging/Logger.h>
12 #include "TMath.h"
13 
14 using namespace Belle2;
15 
16 
17 ROOT::Math::XYZVector DistanceTools::poca(ROOT::Math::XYZVector const& trackPos, ROOT::Math::XYZVector const& trackP,
18  ROOT::Math::XYZVector const& vtxPos)
19 {
20  ROOT::Math::XYZVector trackDir(trackP.Unit());
21  ROOT::Math::XYZVector r(vtxPos - trackPos);
22  return trackPos + r.Dot(trackDir) * trackDir;
23 }
24 
25 B2Vector3D DistanceTools::trackToVtxVec(B2Vector3D const& trackPos, B2Vector3D const& trackP, B2Vector3D const& vtxPos)
26 {
27  B2Vector3D trackDir(trackP.Unit());
28  B2Vector3D r(vtxPos - trackPos);
29  return r - (r.Dot(trackDir)) * trackDir;
30 }
31 
32 double DistanceTools::trackToVtxDist(B2Vector3D const& trackPos, B2Vector3D const& trackP, B2Vector3D const& vtxPos)
33 {
34  return trackToVtxVec(trackPos, trackP, vtxPos).Mag();
35 }
36 
37 TMatrixDSym DistanceTools::trackToVtxCovmat(B2Vector3D const& trackP,
38  TMatrixDSym const& trackPosCovMat, TMatrixDSym const& vtxPosCovMat)
39 {
40  if (trackPosCovMat.GetNcols() != 3 || vtxPosCovMat.GetNcols() != 3) {
41  B2ERROR("in DistanceTools::trackToVtxCovmat, matrices must be of size 3");
42  return TMatrixDSym(3);
43  }
44 
45  TMatrixDSym rCovMat(trackPosCovMat + vtxPosCovMat);
46  B2Vector3D trackDir(trackP.Unit());
47  //d_j = r_j - v_j * v_k r_k
48  //Jij = del_i d_j = delta_ij - v_i * v_j
49  //Since the vector of closest approach is a linear function of r, its
50  //propagation of errors is exact
51  TMatrixDSym Jacobian(3);
52  // Jacobian_ij = delta_ij -v(i)v(j)
53  for (int i(0); i < 3; ++i)
54  for (int j(0); j < 3; ++j)
55  Jacobian(i, j) = -trackDir(i) * trackDir(j);
56  for (int i(0); i < 3; ++i)
57  Jacobian(i, i) += 1;
58 
59  return rCovMat.Similarity(Jacobian); //calculates J * rCovMat * J^T, and returns it
60 
61 }
62 
63 double DistanceTools::trackToVtxDistErr(B2Vector3D const& trackPos, B2Vector3D const& trackP, B2Vector3D const& vtxPos,
64  TMatrixDSym const& trackPosCovMat, TMatrixDSym const& vtxPosCovMat)
65 {
66  TMatrixDSym covMat(trackToVtxCovmat(trackP, trackPosCovMat, vtxPosCovMat));
67  B2Vector3D dVec(trackToVtxVec(trackPos, trackP, vtxPos));
68  // n is the normalise vector in the direction of the POCA between the track and the vtx
69  B2Vector3D n((1. / dVec.Mag()) * dVec);
70 
71  double ret(0);
72 
73  //error on the distance computed as d^T * covMat * d
74  for (int i(0); i < 3; ++i)
75  for (int j(0); j < 3; ++j)
76  ret += n(i) * covMat(i, j) * n(j);
77 
78  return TMath::Sqrt(ret);
79 }
80 
81 
83 {
84  return vtx2Pos - vtx1Pos;
85 }
86 
87 double DistanceTools::vtxToVtxDist(B2Vector3D const& vtx1Pos, B2Vector3D const& vtx2Pos)
88 {
89  return vtxToVtxVec(vtx1Pos, vtx2Pos).Mag();
90 }
91 
92 TMatrixDSym DistanceTools::vtxToVtxCovMat(TMatrixDSym const& vtx1CovMat, TMatrixDSym const& vtx2CovMat)
93 {
94  if (vtx1CovMat.GetNcols() != 3 || vtx2CovMat.GetNcols() != 3) {
95  B2ERROR("in DistanceTools::vtxToVtxCovMat, matrices must be of size 3");
96  return TMatrixDSym(3);
97  }
98 
99  return vtx1CovMat + vtx2CovMat;
100 }
101 
102 double DistanceTools::vtxToVtxDistErr(B2Vector3D const& vtx1Pos, B2Vector3D const& vtx2Pos,
103  TMatrixDSym const& vtx1CovMat, TMatrixDSym const& vtx2CovMat)
104 {
105  TMatrixDSym covMat(vtxToVtxCovMat(vtx1CovMat, vtx2CovMat));
106  B2Vector3D dVec(vtxToVtxVec(vtx1Pos, vtx2Pos));
107  B2Vector3D n((1. / dVec.Mag()) * dVec);
108 
109  double ret(0);
110 
111  //error on the distance computed as d^T * covMat * d
112  for (int i(0); i < 3; ++i)
113  for (int j(0); j < 3; ++j)
114  ret += n(i) * covMat(i, j) * n(j);
115 
116  return TMath::Sqrt(ret);
117 }
118 
DataType Mag() const
The magnitude (rho in spherical coordinate system).
Definition: B2Vector3.h:159
B2Vector3< DataType > Unit() const
Unit vector parallel to this.
Definition: B2Vector3.h:269
TMatrixDSym vtxToVtxCovMat(TMatrixDSym const &vtx1CovMat, TMatrixDSym const &vtx2CovMat)
Returns the covariance (error) matrix related to the vector linking two vertices.
ROOT::Math::XYZVector poca(ROOT::Math::XYZVector const &trackPos, ROOT::Math::XYZVector const &trackP, ROOT::Math::XYZVector const &vtxPos)
Returns the Point Of Closest Approach of a track to a vertex.
double trackToVtxDistErr(B2Vector3D const &trackPos, B2Vector3D const &trackP, B2Vector3D const &vtxPos, TMatrixDSym const &trackPosCovMat, TMatrixDSym const &vtxPosCovMat)
Returns the estimated uncertainty between a vertex and a track's point of closest approach to that ve...
double trackToVtxDist(B2Vector3D const &trackPos, B2Vector3D const &trackP, B2Vector3D const &vtxPos)
Returns the distance between a vertex and a track's point of closest approach to that vertex.
TMatrixDSym trackToVtxCovmat(B2Vector3D const &trackP, TMatrixDSym const &trackPosCovMat, TMatrixDSym const &vtxPosCovMat)
Returns the covariance (error) matrix of the 3D vector between a vertex and a track's point of closes...
double vtxToVtxDist(B2Vector3D const &vtx1Pos, B2Vector3D const &vtx2Pos)
Returns the distance between two vertices.
B2Vector3D trackToVtxVec(B2Vector3D const &trackPos, B2Vector3D const &trackP, B2Vector3D const &vtxPos)
Returns the 3D vector between a vertex and a track's point of closest approach to that vertex.
B2Vector3D vtxToVtxVec(B2Vector3D const &vtx1Pos, B2Vector3D const &vtx2Pos)
Returns the 3D vector between two vertices, ie vtxPos2 - vtxPos1.
double vtxToVtxDistErr(B2Vector3D const &vtx1Pos, B2Vector3D const &vtx2Pos, TMatrixDSym const &vtx1CovMat, TMatrixDSym const &vtx2CovMat)
Returns the estimated uncertainty on the distance between two vertices.
Abstract base class for different kinds of events.