Belle II Software  release-08-01-10
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 #include <framework/geometry/B2Vector3.h>
12 
13 /* External headers. */
14 #include <TMatrixDSym.h>
15 #include <TMatrixD.h>
16 #include <TVectorD.h>
17 #include <TRotation.h>
18 #include <Math/Vector3D.h>
19 
20 namespace Belle2 {
26  namespace RotationTools {
27 
32  inline TMatrixD toMatrix(TRotation r)
33  {
34  TMatrixD rM(3, 3);
35  for (int i = 0; i < 3; ++i)
36  for (int j = 0; j < 3; ++j)
37  rM(i, j) = r(i, j);
38  return rM;
39  }
40 
41 
47  inline TMatrixD getRotationMatrixZtoZp(ROOT::Math::XYZVector zPrime)
48  {
49  ROOT::Math::XYZVector zAxis(0, 0, 1);
50  zPrime = zPrime.Unit();
51  ROOT::Math::XYZVector rotAxis = zAxis.Cross(zPrime);
52  double angle = asin(sqrt(rotAxis.Mag2()));
53 
54  TRotation r;
55  r.Rotate(angle, TVector3(rotAxis.x(), rotAxis.y(), rotAxis.z()));
56  return toMatrix(r);
57  }
58 
59 
66  inline TMatrixD getRotationMatrixXY(double angleX, double angleY)
67  {
68  TRotation r;
69  r.RotateX(angleX);
70  r.RotateY(angleY);
71  return toMatrix(r);
72  }
73 
74 
75 
82  inline TMatrixD rotateTensor(const B2Vector3D& vTo, const TMatrixD& orgMat)
83  {
84  TMatrixD r = getRotationMatrixZtoZp(vTo);
85  TMatrixD rT = r; rT.T();
86  return r * orgMat * rT;
87  }
88 
95  inline TMatrixD rotateTensorInv(const ROOT::Math::XYZVector& vTo, const TMatrixD& orgMat)
96  {
97  TMatrixD r = getRotationMatrixZtoZp(vTo);
98  TMatrixD rT = r; rT.T();
99  return rT * orgMat * r;
100  }
101 
102 
109  inline TMatrixDSym toSymMatrix(const TMatrixD& m)
110  {
111  TMatrixDSym mS(m.GetNrows());
112  for (int i = 0; i < m.GetNrows(); ++i)
113  for (int j = 0; j < m.GetNcols(); ++j) {
114  mS(i, j) = (m(i, j) + m(j, i)) / 2;
115  }
116  return mS;
117  }
118 
119 
124  inline TVectorD toVec(B2Vector3D v)
125  {
126  return TVectorD(0, 2, v.X(), v.Y(), v.Z(), "END");
127  }
128 
133  B2Vector3D getUnitOrthogonal(B2Vector3D v)
134  {
135  return B2Vector3D(v.Z(), 0, -v.X()).Unit();
136  }
137 
138  }
139 
141 }
B2Vector3< DataType > Unit() const
Unit vector parallel to this.
Definition: B2Vector3.h:269
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:516
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.