Belle II Software  release-06-01-15
ClusterUtils.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 #include <analysis/ClusterUtility/ClusterUtils.h>
10 #include <framework/logging/Logger.h>
11 
12 using namespace Belle2;
13 
14 // -----------------------------------------------------------------------------
15 ClusterUtils::ClusterUtils() = default;
16 
17 // -----------------------------------------------------------------------------
19 {
20  // Use the geometry origin (0,0,0) and *not* the IP position
21  return Get4MomentumFromCluster(cluster, TVector3(0.0, 0.0, 0.0), hypo);
22 }
23 
25 {
26 
27  // Use the default vertex from the beam parameters if none is given.
28  return Get4MomentumFromCluster(cluster, GetIPPosition(), hypo);
29 }
30 
31 const TLorentzVector ClusterUtils::Get4MomentumFromCluster(const ECLCluster* cluster, const TVector3& vertex,
33 {
34 
35  // Get particle direction from vertex and reconstructed cluster position.
36  TVector3 direction = cluster->getClusterPosition() - vertex;
37 
38  // Always ignore mass here (even for neutral hadrons) therefore the magnitude
39  // of the momentum is equal to the cluster energy under this hypo.
40  const double E = cluster->getEnergy(hypo); //must not be changed or clusterE getters will be wrong
41  const double px = E * sin(direction.Theta()) * cos(direction.Phi());
42  const double py = E * sin(direction.Theta()) * sin(direction.Phi());
43  const double pz = E * cos(direction.Theta());
44 
45  const TLorentzVector l(px, py, pz, E);
46  return l;
47 }
48 
49 // -----------------------------------------------------------------------------
50 
52 {
53 
54  return GetJacobiMatrix4x6FromCluster(cluster, GetIPPosition(), hypo);
55 }
56 
57 const TMatrixD ClusterUtils::GetJacobiMatrix4x6FromCluster(const ECLCluster* cluster, const TVector3& vertex,
59 {
60 
61  // Calculate the Jacobi matrix J = dpx/dE dpx/dphi dpx/dtheta dpx/dx dpy/dy dpz/dz
62  // ...
63  // dpx/dE dE/dphi dE/dtheta dpx/dx dE/dy dE/dz
64  TMatrixD jacobian(4, 6);
65 
66  const double R = cluster->getR();
67  const double energy = cluster->getEnergy(hypo);
68  const double theta = cluster->getTheta();
69  const double phi = cluster->getPhi();
70 
71  const double st = sin(theta);
72  const double ct = cos(theta);
73  const double sp = sin(phi);
74  const double cp = cos(phi);
75 
76  const double clx = R * st * cp; // cluster position x
77  const double cly = R * st * sp; // cluster position y
78  const double clz = R * ct; // cluster position z
79 
80  const double vx = vertex.X(); // vertex position x
81  const double vy = vertex.Y(); // vertex position y
82  const double vz = vertex.Z(); // vertex position z
83 
84  const double dx = clx - vx; // direction vector x
85  const double dy = cly - vy; // direction vector y
86  const double dz = clz - vz; // direction vector z
87  const double dx2 = dx * dx; // direction vector x squared
88  const double dy2 = dy * dy; // direction vector y squared
89  const double dz2 = dz * dz; // direction vector z squared
90 
91  const double r2 = (dx * dx + dy * dy + dz * dz);
92  const double r12 = sqrt(r2);
93  const double r32 = pow(r2, 1.5);
94 
95  // px = E * dx/r
96  jacobian(0, 0) = dx / r12; // dpx/dE
97  jacobian(0, 1) = -energy * R * ((dx * dy * cp) + ((dy2 + dz2) * sp)) * st / (r32); // dpx/dphi
98  jacobian(0, 2) = energy * R * (((dy2 + dz2) * cp * ct) - (dx * dy * ct * sp) + (dx * dz * st)) / (r32); // dpx/dtheta
99  jacobian(0, 3) = -energy * (dy2 + dz2) / (r32); // dpx/dvx
100  jacobian(0, 4) = energy * dx * dy / (r32); // dpx/dvy
101  jacobian(0, 5) = energy * dx * dz / (r32); //dpx/dvz
102 
103  // py = E * dy/r
104  jacobian(1, 0) = dy / r12; // dpy/dE
105  jacobian(1, 1) = energy * R * (((dx2 + dz2) * cp) + (dx * dy * sp)) * st / (r32); // dpy/dphi
106  jacobian(1, 2) = energy * R * ((-dx * dy * cp * ct) + ((dx2 + dz2) * ct * sp) + (dy * dz * st)) / (r32); // dpz/dtheta
107  jacobian(1, 3) = energy * dx * dy / (r32); // dpy/dvx
108  jacobian(1, 4) = -energy * (dx2 + dz2) / (r32); // dpy/dvy
109  jacobian(1, 5) = energy * dy * dz / (r32); //dpy/dvz
110 
111  // pz = E * dz/r
112  jacobian(2, 0) = dz / r12; // dpz/dE
113  jacobian(2, 1) = energy * R * dz * (-dy * cp + dx * sp) * st / (r32); // dpz/dphi
114  jacobian(2, 2) = -energy * R * ((dx * dz * cp * ct) + (dy * dz * ct * sp) + (dx2 + dy2) * st) / (r32); // dpz/dtheta
115  jacobian(2, 3) = energy * dx * dz / (r32); // dpz/dvx
116  jacobian(2, 4) = energy * dy * dz / (r32); // dpz/dvy
117  jacobian(2, 5) = -energy * (dx2 + dy2) / (r32); //dpz/ydvz
118 
119  // E
120  jacobian(3, 0) = 1.0; // dE/dE
121  jacobian(3, 1) = 0.0; // dE/dphi
122  jacobian(3, 2) = 0.0; // dE/dtheta
123  jacobian(3, 3) = 0.0; // dE/dvx
124  jacobian(3, 4) = 0.0; // dE/dvy
125  jacobian(3, 5) = 0.0; // dE/dvz
126 
127  //jacobian.Print();
128  return jacobian;
129 
130 }
131 
132 const TMatrixDSym ClusterUtils::GetCovarianceMatrix4x4FromCluster(const ECLCluster* cluster, const TMatrixD& jacobiMatrix)
133 {
134 
135  return GetCovarianceMatrix4x4FromCluster(cluster, GetIPPositionCovarianceMatrix(), jacobiMatrix);
136 }
137 
138 const TMatrixDSym ClusterUtils::GetCovarianceMatrix4x4FromCluster(const ECLCluster* cluster, const TMatrixDSym& covmatvertex,
139  const TMatrixD& jacobiMatrix)
140 {
141 
142  // Get the covariance matrix (theta, phi, energy) from the ECL cluster.
143  TMatrixDSym covmatecl = cluster->getCovarianceMatrix3x3();
144 
145  // Combine into the total covariance matrix from the ECL cluster (0..2) and the IP(3..5) as two 3x3 block matrices.
146  TMatrixDSym covmatcombined(6);
147  for (int i = 0; i < 3; i++) {
148  for (int j = 0; j <= i ; j++) {
149  covmatcombined(i, j) = covmatcombined(j, i) = covmatecl(i, j);
150  covmatcombined(i + 3, j + 3) = covmatcombined(j + 3, i + 3) = covmatvertex(i, j);
151  }
152  }
153 
154  TMatrixDSym covmatCart(4);
155  covmatCart = covmatcombined.Similarity(jacobiMatrix);
156  return covmatCart;
157 }
158 
159 // -----------------------------------------------------------------------------
160 
161 const TMatrixDSym ClusterUtils::GetCovarianceMatrix7x7FromCluster(const ECLCluster* cluster, const TMatrixD& jacobiMatrix)
162 {
163 
164  return GetCovarianceMatrix7x7FromCluster(cluster, GetIPPositionCovarianceMatrix(), jacobiMatrix);
165 }
166 
168  const TMatrixDSym& covmatvertex, const TMatrixD& jacobiMatrix)
169 {
170  TMatrixDSym covmat4x4 = GetCovarianceMatrix4x4FromCluster(cluster, covmatvertex, jacobiMatrix);
171 
172  TMatrixDSym covmatCart(7);
173 
174  // px, py, pz, E
175  for (int i = 0; i < 4; i++) {
176  for (int j = 0; j <= i ; j++) {
177  covmatCart(i, j) = covmatCart(j, i) = covmat4x4(i, j);
178  }
179  }
180 
181  // x, y, z
182  for (int i = 0; i < 3; i++) {
183  for (int j = 0; j <= i ; j++) {
184  covmatCart(i + 4, j + 4) = covmatCart(j + 4, i + 4) = covmatvertex(i, j);
185  }
186  }
187 
188  return covmatCart;
189 }
190 
191 
192 // -----------------------------------------------------------------------------
194 {
195  if (!m_beamSpotDB) {
196  B2WARNING("Beamspot not available, using (0, 0, 0) as IP position instead.");
197  return TVector3(0.0, 0.0, 0.0);
198  } else return m_beamSpotDB->getIPPosition();
199 }
200 
201 // -----------------------------------------------------------------------------
203 {
204  if (!m_beamSpotDB) {
205  B2WARNING("Beam parameters not available, using ((1, 0, 0), (0, 1, 0), (0, 0, 1)) as IP covariance matrix instead.");
206 
207  TMatrixDSym covmat(3);
208  for (int i = 0; i < 3; ++i) {
209  covmat(i, i) = 1.0; // 1.0*1.0 cm^2
210  }
211  return covmat;
212  } else return m_beamSpotDB->getCovVertex();
213 }
const TLorentzVector Get4MomentumFromCluster(const ECLCluster *cluster, ECLCluster::EHypothesisBit hypo)
Returns four momentum vector.
Definition: ClusterUtils.cc:24
const TMatrixDSym GetCovarianceMatrix7x7FromCluster(const ECLCluster *cluster, const TMatrixD &jacobiMatrix)
Returns 7x7 covariance matrix (px, py, pz, E, x, y, z)
const TMatrixD GetJacobiMatrix4x6FromCluster(const ECLCluster *cluster, ECLCluster::EHypothesisBit hypo)
Returns 4x6 Jacobi matrix (px, py, pz, E)
Definition: ClusterUtils.cc:51
const TMatrixDSym GetCovarianceMatrix4x4FromCluster(const ECLCluster *cluster, const TMatrixD &jacobiMatrix)
Returns 4x4 covariance matrix (px, py, pz, E)
const TVector3 GetIPPosition()
Returns default IP position from beam parameters.
DBObjPtr< BeamSpot > m_beamSpotDB
Beam spot database object.
Definition: ClusterUtils.h:115
const TLorentzVector GetCluster4MomentumFromCluster(const ECLCluster *cluster, ECLCluster::EHypothesisBit hypo)
Returns cluster four momentum vector.
Definition: ClusterUtils.cc:18
const TMatrixDSym GetIPPositionCovarianceMatrix()
Returns default IP position covariance matrix from beam parameters.
ClusterUtils()
Constructor.
ECL cluster data.
Definition: ECLCluster.h:27
EHypothesisBit
The hypothesis bits for this ECLCluster (Connected region (CR) is split using this hypothesis.
Definition: ECLCluster.h:31
Abstract base class for different kinds of events.