Belle II Software development
VectorUtil.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/* ROOT headers. */
12#include <Math/Vector3D.h>
13#include <Math/Vector2D.h>
14#include <Math/VectorUtil.h>
15#include <TVector3.h>
16
17namespace Belle2 {
22
26 static constexpr auto XYZToTVector = [](const ROOT::Math::XYZVector& a)
27 {
28 return TVector3(a.X(), a.Y(), a.Z());
29 };
30
31 namespace VectorUtil {
32
40 inline void setMagThetaPhi(ROOT::Math::XYZVector& vector,
41 double mag, double theta, double phi)
42 {
43 const double amag = std::abs(mag);
44 const double sinTheta = std::sin(theta);
45 const double x = amag * sinTheta * std::cos(phi);
46 const double y = amag * sinTheta * std::sin(phi);
47 const double z = amag * std::cos(theta);
48 vector.SetXYZ(x, y, z);
49 }
50
56 inline void setMag(ROOT::Math::XYZVector& vector, double mag)
57 {
58 setMagThetaPhi(vector, mag, vector.Theta(), vector.Phi());
59 }
60
66 inline void setTheta(ROOT::Math::XYZVector& vector, double theta)
67 {
68 setMagThetaPhi(vector, vector.R(), theta, vector.Phi());
69 }
70
76 inline void setPhi(ROOT::Math::XYZVector& vector, double phi)
77 {
78 setMagThetaPhi(vector, vector.R(), vector.Theta(), phi);
79 }
80
88 inline void setPtThetaPhi(ROOT::Math::XYZVector& vector,
89 double pt, double theta, double phi)
90 {
91 const double aPt = std::abs(pt);
92 const double x = aPt * std::cos(phi);
93 const double y = aPt * std::sin(phi);
94 const double tanTheta = std::tan(theta);
95 const double z = tanTheta ? aPt / tanTheta : 0;
96 vector.SetXYZ(x, y, z);
97 }
98
111 template <class Vector>
112 double CosTheta(const Vector& v1, const Vector& v2)
113 {
114 const double v1_r2 = v1.Mag2();
115 const double v2_r2 = v2.Mag2();
116 const double ptot2 = v1_r2 * v2_r2;
117 if (ptot2 == 0) {
118 return 0.0;
119 }
120 const double pdot = v1.Dot(v2);
121 double arg = pdot / std::sqrt(ptot2);
122 if (arg > 1.0)
123 arg = 1.0;
124 if (arg < -1.0)
125 arg = -1.0;
126 return arg;
127 }
128
129
140 template <class Vector>
141 double Angle(const Vector& v1, const Vector& v2)
142 {
143 return std::acos(CosTheta(v1, v2));
144 }
145
160 inline ROOT::Math::XYVector orthogonalVector(const ROOT::Math::XYVector& v1, const ROOT::Math::XYVector& v2)
161 {
162 const double cross = v1.X() * v2.Y() - v1.Y() - v2.X(); // = relativTo.cross(*this)
163 const ROOT::Math::XYVector tmp = v2 * (cross / v2.Mag2()); // = relativTo.scaled(cross / relativTo.normSquared())
164 return ROOT::Math::XYVector(-tmp.Y(), tmp.X()); // = .orthogonal()
165 }
166
182 template<class aVector>
183 inline aVector parallelVector(const aVector& v1, const aVector& v2)
184 {
185 const double v2Mag2 = v2.Mag2();
186 if (v2Mag2 == 0)
187 return aVector();
188 const double dotp = v1.Dot(v2); // = relativTo.dot(*this)
189 const aVector tmp = v2 * (dotp / v2Mag2); // = relativTo.scaled(dotp / relativTo.normSquared())
190 return tmp;
191 }
192
193 }
194
196}
STL class.
static constexpr auto XYZToTVector
Helper function to convert XYZVector to TVector3.
Definition VectorUtil.h:26
Abstract base class for different kinds of events.