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