Belle II Software development
BeamParameters.cc
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#include <framework/dbobjects/BeamParameters.h>
10#include <framework/logging/Logger.h>
11#include <framework/gearbox/Const.h>
12#include <framework/utilities/MathHelpers.h>
13
14using namespace std;
15using namespace Belle2;
16
17namespace {
23 constexpr int getIndex(unsigned int i, unsigned int j)
24 {
25 //swap indices if i >= j
26 return (i < j) ? ((j + 1) * j / 2 + i) : ((i + 1) * i / 2 + j);
27 }
28}
29
30void Belle2::BeamParameters::setLER(double energy, double angleX, double angleY, const std::vector<double>& cov)
31{
32 ROOT::Math::PxPyPzEVector vec = getFourVector(energy, angleX, angleY, false);
33 setLER(vec);
34 setCovMatrix(m_covLER, cov, false);
35}
36
37void Belle2::BeamParameters::setHER(double energy, double angleX, double angleY, const std::vector<double>& cov)
38{
39 ROOT::Math::PxPyPzEVector vec = getFourVector(energy, angleX, angleY, true);
40 setHER(vec);
41 setCovMatrix(m_covHER, cov, false);
42}
43
44void Belle2::BeamParameters::setVertex(const ROOT::Math::XYZVector& vertex, const std::vector<double>& cov)
45{
46 setVertex(vertex);
47 setCovMatrix(m_covVertex, cov, true);
48}
49
50ROOT::Math::PxPyPzEVector Belle2::BeamParameters::getFourVector(double energy, double angleX, double angleY, bool isHER)
51{
52 double p = sqrt(square(energy) - square(Const::electronMass));
53 double dir = isHER ? 1 : -1;
54
55 double pz = dir * p / sqrt(1 + square(tan(angleX)) + square(tan(angleY)));
56
57 return ROOT::Math::PxPyPzEVector(pz * tan(angleX), pz * tan(angleY), pz, energy);
58}
59
60TMatrixDSym Belle2::BeamParameters::getCovMatrix(const Double32_t* member)
61{
62 TMatrixDSym matrix(3);
63 for (int iRow = 0; iRow < 3; ++iRow) {
64 for (int iCol = iRow; iCol < 3; ++iCol) {
65 matrix(iCol, iRow) = matrix(iRow, iCol) = member[getIndex(iRow, iCol)];
66 }
67 }
68 return matrix;
69}
70
71void Belle2::BeamParameters::setCovMatrix(Double32_t* matrix, const TMatrixDSym& cov)
72{
73 for (int iRow = 0; iRow < 3; ++iRow) {
74 for (int iCol = iRow; iCol < 3; ++iCol) {
75 matrix[getIndex(iRow, iCol)] = cov(iRow, iCol);
76 }
77 }
78}
79
80void Belle2::BeamParameters::setCovMatrix(Double32_t* matrix, const std::vector<double>& cov, bool common)
81{
82 std::fill_n(matrix, 6, 0);
83 // so let's see how many elements we got
84 switch (cov.size()) {
85 case 0: // none, ok, no errors then
86 break;
87 case 1: // either just first value or common value for diagonal elements
88 if (!common) {
89 matrix[0] = cov[0];
90 break;
91 }
92 // not common value, fall through
93 [[fallthrough]];
94 case 3: // diagonal form.
95 // we can do both at once by using cov[i % cov.size()] which will either
96 // loop through 0, 1, 2 if size is 3 or will always be 0
97 for (int i = 0; i < 3; ++i) {
98 matrix[getIndex(i, i)] = cov[i % cov.size()];
99 }
100 break;
101 case 6: // upper triangle, i.e. (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)
102 for (int iRow = 0, n = 0; iRow < 3; ++iRow) {
103 for (int iCol = iRow; iCol < 3; ++iCol) {
104 matrix[getIndex(iRow, iCol)] = cov[n++];
105 }
106 }
107 break;
108 case 9: // all elements
109 for (int iRow = 0; iRow < 3; ++iRow) {
110 for (int iCol = iRow; iCol < 3; ++iCol) {
111 matrix[getIndex(iRow, iCol)] = cov[iRow * 3 + iCol];
112 }
113 }
114 break;
115 default:
116 B2ERROR("Number of elements to set covariance matrix must be either 1, 3, 6 or 9 but "
117 << cov.size() << " given");
118 }
119}
void setHER(double energy, double angleX, double angleY, const std::vector< double > &cov)
Set the HER FourVector and error matrix from beam energy, angle and covariance entries.
Double32_t m_covLER[6]
Covariance matrix of the low energy beam at the IP.
static ROOT::Math::PxPyPzEVector getFourVector(double energy, double angleX, double angleY, bool isHER)
Calculate FourVector of a beam from energy and angles in xz and yz planes.
Double32_t m_covHER[6]
Covariance matrix of the high energy beam at the IP.
void setLER(double energy, double angleX, double angleY, const std::vector< double > &cov)
Set the LER FourVector and error matrix from beam energy, angle and covariance entries.
static void setCovMatrix(Double32_t *member, const std::vector< double > &cov, bool common)
Set covariance matrix from vector of entries.
Double32_t m_covVertex[6]
Covariance matrix of the vertex position.
static TMatrixDSym getCovMatrix(const Double32_t *member)
Obtain covariance matrix from a given float array.
void setVertex(const ROOT::Math::XYZVector &vertex, const std::vector< double > &cov)
Set the vertex position and error matrix.
static const double electronMass
electron mass
Definition Const.h:685
constexpr T square(const T &x)
Calculate the square of the input.
Definition MathHelpers.h:21
double tan(double a)
tan for double
Definition beamHelpers.h:31
GeneralVector< T > getFourVector(T energy, T angleX, T angleY, bool isHER)
get 4-momentum from energy and angles of beam
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.
STL namespace.