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 double unfold(double x, int nx, double A)
32 {
33 if (nx % 2 == 0) return (nx * A + x);
34 else return (nx * A - x);
35 }
36
37
44 inline double unfold(double kx, int nx)
45 {
46 if (nx % 2 == 0) return kx;
47 else return -kx;
48 }
49
50
59 inline void fold(double xu, double A, double& x, double& kx, int& nx)
60 {
61 nx = lround(xu / A);
62 x = xu - nx * A;
63 if (nx % 2 != 0) {
64 x = -x;
65 kx = -kx;
66 }
67 }
68
78 inline double clip(double x, int Nx, double A, double xmi, double xma)
79 {
80 x = unfold(x, Nx, A);
81 x = std::max(std::min(x, xma), xmi) - Nx * A;
82 if (Nx % 2 != 0) x = -x;
83 return x;
84 }
85
92 inline int getNumOfEven(int j1, int j2)
93 {
94 return (j2 - j1 + 1 - std::abs(j1) % 2) / 2;
95 }
96
102 inline double within2PI(double angle)
103 {
104 angle = fmod(angle, 2 * M_PI);
105 if (angle < 0) angle += 2 * M_PI;
106 return angle;
107 }
108
114 inline void rotateUz(ROOT::Math::XYZVector& vec, const ROOT::Math::XYZVector& z_Axis)
115 {
116 auto zAxis = z_Axis.Unit();
117 double cth = zAxis.Z();
118 double sth = sqrt(1 - cth * cth);
119 if (sth == 0) {
120 if (cth < 0) vec = -vec;
121 return;
122 }
123 double cfi = zAxis.X() / sth;
124 double sfi = zAxis.Y() / sth;
125 // rotation by theta around y then by phi around z
126 double x = cth * vec.X() + sth * vec.Z();
127 double y = vec.Y();
128 double z = -sth * vec.X() + cth * vec.Z();
129 vec.SetX(cfi * x - sfi * y);
130 vec.SetY(sfi * x + cfi * y);
131 vec.SetZ(z);
132 }
133
134
135 } // func
136 } // TOP
138} // Belle2
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
double unfold(double x, int nx, double A)
unfold a coordinate.
Definition: func.h:31
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:114
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:78
int getNumOfEven(int j1, int j2)
Returns number of even numbers in the range given by arguments.
Definition: func.h:92
void fold(double xu, double A, double &x, double &kx, int &nx)
fold a coordinate (inverse of unfold).
Definition: func.h:59
double within2PI(double angle)
Returns angle within 0 and 2PI.
Definition: func.h:102
Abstract base class for different kinds of events.