Belle II Software development
RotationTools.h
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#pragma once
10
11/* External headers. */
12#include <TMatrixDSym.h>
13#include <TMatrixD.h>
14#include <TVectorD.h>
15#include <Math/RotationX.h>
16#include <Math/RotationY.h>
17#include <Math/RotationZ.h>
18#include <Math/Vector3D.h>
19#include <Math/VectorUtil.h>
20
21namespace Belle2 {
26
27 namespace RotationTools {
28
33 inline TMatrixD toMatrix(ROOT::Math::Rotation3D r)
34 {
35 TMatrixD rM(3, 3);
36 r.GetRotationMatrix(rM);
37 return rM;
38 }
39
40
46 inline TMatrixD getRotationMatrixZtoZp(ROOT::Math::XYZVector zPrime)
47 {
48 ROOT::Math::XYZVector zAxis(0, 0, 1);
49 double angle = ROOT::Math::VectorUtil::Angle(zAxis, zPrime);
50
51 ROOT::Math::XYZVector rotAxis = zAxis.Cross(zPrime).Unit();
52 double x = rotAxis.x();
53 double y = rotAxis.y();
54
55 double cosA = std::cos(angle);
56 double sinA = std::sin(angle);
57 double oneMinusCosA = 1 - cosA;
58
59 double xx = cosA + x * x * oneMinusCosA;
60 double xy = x * y * oneMinusCosA;
61 double xz = y * sinA;
62
63 double yx = y * x * oneMinusCosA;
64 double yy = cosA + y * y * oneMinusCosA;
65 double yz = - x * sinA;
66
67 double zx = - y * sinA;
68 double zy = x * sinA;
69 double zz = cosA;
70
71 return toMatrix(ROOT::Math::Rotation3D(xx, xy, xz, yx, yy, yz, zx, zy, zz));
72 }
73
74
81 inline TMatrixD getRotationMatrixXY(double angleX, double angleY)
82 {
83 ROOT::Math::RotationX rX(angleX);
84 ROOT::Math::RotationY rY(angleY);
85 ROOT::Math::Rotation3D r = rX * rY;
86 return toMatrix(r);
87 }
88
89
90
97 inline TMatrixD rotateTensor(const ROOT::Math::XYZVector& vTo, const TMatrixD& orgMat)
98 {
99 TMatrixD r = getRotationMatrixZtoZp(vTo);
100 TMatrixD rT = r; rT.T();
101 return r * orgMat * rT;
102 }
103
110 inline TMatrixD rotateTensorInv(const ROOT::Math::XYZVector& vTo, const TMatrixD& orgMat)
111 {
112 TMatrixD r = getRotationMatrixZtoZp(vTo);
113 TMatrixD rT = r; rT.T();
114 return rT * orgMat * r;
115 }
116
117
124 inline TMatrixDSym toSymMatrix(const TMatrixD& m)
125 {
126 TMatrixDSym mS(m.GetNrows());
127 for (int i = 0; i < m.GetNrows(); ++i)
128 for (int j = 0; j < m.GetNcols(); ++j) {
129 mS(i, j) = (m(i, j) + m(j, i)) / 2;
130 }
131 return mS;
132 }
133
134
139 inline TVectorD toVec(ROOT::Math::XYZVector v)
140 {
141 return TVectorD(0, 2, v.X(), v.Y(), v.Z(), "END");
142 }
143
148 ROOT::Math::XYZVector getUnitOrthogonal(ROOT::Math::XYZVector v)
149 {
150 return ROOT::Math::XYZVector(v.Z(), 0, -v.X()).Unit();
151 }
152
153 }
154
156}
Abstract base class for different kinds of events.