Belle II Software  release-05-01-25
BeamParameters.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/dbobjects/BeamParameters.h>
12 #include <framework/logging/Logger.h>
13 #include <framework/gearbox/Const.h>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 namespace {
24  constexpr int getIndex(unsigned int i, unsigned int j)
25  {
26  //swap indices if i >= j
27  return (i < j) ? ((j + 1) * j / 2 + i) : ((i + 1) * i / 2 + j);
28  }
29 }
30 
31 void BeamParameters::setLER(double energy, double angle, const std::vector<double>& cov)
32 {
33  TLorentzVector vec = getFourVector(energy, angle);
34  setLER(vec);
35  setCovMatrix(m_covLER, cov, false);
36 }
37 
38 void BeamParameters::setHER(double energy, double angle, const std::vector<double>& cov)
39 {
40  TLorentzVector vec = getFourVector(energy, angle);
41  setHER(vec);
42  setCovMatrix(m_covHER, cov, false);
43 }
44 
45 void BeamParameters::setVertex(const TVector3& vertex, const std::vector<double>& cov)
46 {
47  setVertex(vertex);
48  setCovMatrix(m_covVertex, cov, true);
49 }
50 
51 TLorentzVector BeamParameters::getFourVector(double energy, double angle)
52 {
53  double pz = sqrt(energy * energy - Const::electronMass * Const::electronMass);
54  TLorentzVector vec(0, 0, pz, energy);
55  if (angle < 0) angle = M_PI + angle;
56  vec.RotateY(angle);
57  return vec;
58 }
59 
60 TMatrixDSym 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 
71 void 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 
80 void 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 trough 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 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19