Belle II Software light-2604-jellyfish
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 inline double CosPhi(const ROOT::Math::XYVector& v1, const ROOT::Math::XYVector& v2)
112 {
113 const double v1_r2 = v1.Mag2();
114 const double v2_r2 = v2.Mag2();
115 const double ptot2 = v1_r2 * v2_r2;
116 if (ptot2 == 0) {
117 return 0.0;
118 }
119 const double pdot = v1.Dot(v2);
120 double arg = pdot / std::sqrt(ptot2);
121 if (arg > 1.0)
122 arg = 1.0;
123 if (arg < -1.0)
124 arg = -1.0;
125 return arg;
126 }
127
129 inline double Cross(const ROOT::Math::XYVector& lhs, const ROOT::Math::XYVector& rhs)
130 {
131 return lhs.X() * rhs.Y() - lhs.Y() * rhs.X();
132 }
133
135 inline ROOT::Math::XYVector Orthogonal(const ROOT::Math::XYVector& a)
136 {
137 return ROOT::Math::XYVector(-a.Y(), a.X());
138 }
139
152 inline ROOT::Math::XYVector orthogonalVector(const ROOT::Math::XYVector& v1, const ROOT::Math::XYVector& relativeTo)
153 {
154 const double cross = Cross(relativeTo, v1); // = relativeTo.cross(*this)
155 const ROOT::Math::XYVector tmp = relativeTo * (cross / relativeTo.Mag2()); // = relativeTo.scaled(cross / relativeTo.normSquared())
156 return Orthogonal(tmp); // = .orthogonal()
157 }
158
174 template<class aVector>
175 inline aVector parallelVector(const aVector& v1, const aVector& v2)
176 {
177 const double v2Mag2 = v2.Mag2();
178 if (v2Mag2 == 0)
179 return aVector();
180 const double dotp = v1.Dot(v2); // = relativeTo.dot(*this)
181 const aVector tmp = v2 * (dotp / v2Mag2); // = relativeTo.scaled(dotp / relativeTo.normSquared())
182 return tmp;
183 }
184
186 inline ROOT::Math::XYVector flippedOver(const ROOT::Math::XYVector& inVec, const ROOT::Math::XYVector& reflectionLine)
187 {
188 return inVec - orthogonalVector(inVec, reflectionLine) * 2;
189 }
190
191 }
192
194}
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.