Belle II Software development
func.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#include <cmath>
11#include <Math/Vector3D.h>
12
13
14namespace Belle2 {
19 namespace TOP {
20
22 namespace func {
23
31 inline long lround(double x)
32 {
33 // 2^52: above it every double is already an integer, and the conversion below could overflow
34 if (std::abs(x) < 4503599627370496.0) {
35 x += 0.5;
36 long n = static_cast<long>(std::abs(x)); // truncation towards zero
37 if (x > 0) return n;
38 return -(n + 1);
39 }
40 return std::lround(x); // also takes care of NaN and infinity
41 }
42
43
51 inline double unfold(double x, int nx, double A)
52 {
53 if (nx % 2 == 0) return (nx * A + x);
54 else return (nx * A - x);
55 }
56
57
64 inline double unfold(double kx, int nx)
65 {
66 if (nx % 2 == 0) return kx;
67 else return -kx;
68 }
69
70
79 inline void fold(double xu, double A, double& x, double& kx, int& nx)
80 {
81 nx = func::lround(xu / A);
82 x = xu - nx * A;
83 if (nx % 2 != 0) {
84 x = -x;
85 kx = -kx;
86 }
87 }
88
98 inline double clip(double x, int Nx, double A, double xmi, double xma)
99 {
100 x = unfold(x, Nx, A);
101 x = std::max(std::min(x, xma), xmi) - Nx * A;
102 if (Nx % 2 != 0) x = -x;
103 return x;
104 }
105
112 inline int getNumOfEven(int j1, int j2)
113 {
114 return (j2 - j1 + 1 - std::abs(j1) % 2) / 2;
115 }
116
122 inline double within2PI(double angle)
123 {
124 angle = fmod(angle, 2 * M_PI);
125 if (angle < 0) angle += 2 * M_PI;
126 return angle;
127 }
128
134 inline void rotateUz(ROOT::Math::XYZVector& vec, const ROOT::Math::XYZVector& z_Axis)
135 {
136 auto zAxis = z_Axis.Unit();
137 double cth = zAxis.Z();
138 double sth = sqrt(1 - cth * cth);
139 if (sth == 0) {
140 if (cth < 0) vec = -vec;
141 return;
142 }
143 double cfi = zAxis.X() / sth;
144 double sfi = zAxis.Y() / sth;
145 // rotation by theta around y then by phi around z
146 double x = cth * vec.X() + sth * vec.Z();
147 double y = vec.Y();
148 double z = -sth * vec.X() + cth * vec.Z();
149 vec.SetX(cfi * x - sfi * y);
150 vec.SetY(sfi * x + cfi * y);
151 vec.SetZ(z);
152 }
153
154
155 } // func
156 } // TOP
158} // Belle2
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
commonly used functions
Definition func.h:22
double unfold(double x, int nx, double A)
unfold a coordinate.
Definition func.h:51
void rotateUz(ROOT::Math::XYZVector &vec, const ROOT::Math::XYZVector &z_Axis)
Replacement for a function TVector3::RotateUz which is not implemented in GenVector classes.
Definition func.h:134
double clip(double x, int Nx, double A, double xmi, double xma)
Performs a clip on x w.r.t xmi and xma.
Definition func.h:98
int getNumOfEven(int j1, int j2)
Returns number of even numbers in the range given by arguments.
Definition func.h:112
void fold(double xu, double A, double &x, double &kx, int &nx)
fold a coordinate (inverse of unfold).
Definition func.h:79
double within2PI(double angle)
Returns angle within 0 and 2PI.
Definition func.h:122
long lround(double x)
Rounds to the nearest integer, halfway cases away from zero.
Definition func.h:31
Abstract base class for different kinds of events.