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