Belle II Software  release-08-01-10
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  ROOT::Math::PxPyPzEVector vec = getFourVector(energy, angleX, angleY, false);
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  ROOT::Math::PxPyPzEVector vec = getFourVector(energy, angleX, angleY, true);
39  setHER(vec);
40  setCovMatrix(m_covHER, cov, false);
41 }
42 
43 void BeamParameters::setVertex(const ROOT::Math::XYZVector& vertex, const std::vector<double>& cov)
44 {
45  setVertex(vertex);
46  setCovMatrix(m_covVertex, cov, true);
47 }
48 
49 ROOT::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 
59 TMatrixDSym 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 
70 void 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 
79 void 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 }
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
GeneralVector< T > getFourVector(T energy, T angleX, T angleY, bool isHER)
get 4-momentum from energy and angles of beam
Definition: beamHelpers.h:220
double tan(double a)
tan for double
Definition: beamHelpers.h:31
Abstract base class for different kinds of events.