Belle II Software  release-08-01-10
MakeMotherKFit.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * External Contributor: J. Tanaka *
5  * *
6  * See git log for contributors and copyright holders. *
7  * This file is licensed under LGPL-3.0, see LICENSE.md. *
8  **************************************************************************/
9 
10 
11 #include <analysis/VertexFitting/KFit/MakeMotherKFit.h>
12 #include <framework/gearbox/Const.h>
13 
14 using namespace std;
15 using namespace Belle2;
16 using namespace Belle2::analysis;
17 using namespace CLHEP;
18 
19 MakeMotherKFit::MakeMotherKFit():
20  m_Vertex(Hep3Vector()),
21  m_VertexError(HepSymMatrix(3, 0))
22 {
24  m_FlagAtDecayPoint = true;
25  m_FlagVertexError = false;
26  m_FlagCorrelation = false;
27  m_FlagTrackVertexError = false;
30  m_TrackCount = 0;
31  m_Charge = 0;
32 }
33 
34 
36 
37 
40  m_Tracks.push_back(p);
41  m_Charge += p.getCharge();
42  m_TrackCount = m_Tracks.size();
43 
45 }
46 
47 
49 MakeMotherKFit::addTrack(const HepLorentzVector& p, const HepPoint3D& x, const HepSymMatrix& e, const double q, const int flag) {
50  if (e.num_row() != (int)KFitConst::kNumber7)
51  {
53  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
54  return m_ErrorCode;
55  }
56 
57  return this->addTrack(KFitTrack(p, x, e, q, flag));
58 }
59 
60 
62 MakeMotherKFit::setMagneticField(const double mf) {
63  m_MagneticField = mf;
64 
66 }
67 
68 
71  m_Vertex = v;
72 
74 }
75 
76 
78 MakeMotherKFit::setVertexError(const HepSymMatrix& e) {
79  if (e.num_row() != 3)
80  {
82  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
83  return m_ErrorCode;
84  }
85 
86  m_VertexError = e;
87  m_FlagVertexError = true;
88 
90 }
91 
92 
94 MakeMotherKFit::setTrackVertexError(const HepMatrix& e) {
95  if (e.num_row() != 3 || e.num_col() != (int)KFitConst::kNumber7)
96  {
98  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
99  return m_ErrorCode;
100  }
101 
102  m_TrackVertexError.push_back(e);
103  m_FlagTrackVertexError = true;
104 
106 }
107 
108 
109 enum KFitError::ECode
111  HepMatrix zero(3, KFitConst::kNumber7, 0);
112 
113  return this->setTrackVertexError(zero);
114 }
115 
116 
117 enum KFitError::ECode
118 MakeMotherKFit::setCorrelation(const HepMatrix& e) {
119  if (e.num_row() != (int)KFitConst::kNumber7 || e.num_col() != (int)KFitConst::kNumber7)
120  {
122  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
123  return m_ErrorCode;
124  }
125 
126  m_Correlation.push_back(e);
127  m_FlagCorrelation = true;
128 
130 }
131 
132 
133 enum KFitError::ECode
135  HepMatrix zero(KFitConst::kNumber7, KFitConst::kNumber7, 0);
136 
137  return this->setCorrelation(zero);
138 }
139 
140 
141 enum KFitError::ECode
142 MakeMotherKFit::setFlagAtDecayPoint(const bool flag) {
143  m_FlagAtDecayPoint = flag;
144 
146 }
147 
148 
149 enum KFitError::ECode
150 MakeMotherKFit::setFlagBeforeAfter(const int flag) {
151  m_FlagBeforeAfter = flag;
152 
154 }
155 
156 
157 enum KFitError::ECode
159  return m_ErrorCode;
160 }
161 
162 const KFitTrack
164 {
165  return m_Mother;
166 }
167 
168 
169 const HepLorentzVector
171 {
173 }
174 
175 
176 const HepPoint3D
178 {
180 }
181 
182 
183 const HepSymMatrix
185 {
187 }
188 
189 
190 enum KFitError::ECode
192  // ...makes matrix.
193  HepMatrix dMdC(KFitConst::kNumber7, KFitConst::kNumber7 * m_TrackCount + 3, 0);
194  HepSymMatrix Ec(KFitConst::kNumber7 * m_TrackCount + 3, 0);
195 
196 
197  // ...sets error matrix
198  if (m_FlagCorrelation && m_Correlation.size() != static_cast<unsigned int>((m_TrackCount * m_TrackCount - m_TrackCount) / 2))
199  {
201  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
202  return m_ErrorCode;
203  }
204  if (m_FlagTrackVertexError && m_TrackVertexError.size() != (unsigned int)m_TrackCount)
205  {
207  KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
208  return m_ErrorCode;
209  }
210  this->calculateError(&Ec);
211 
212 
213  // ...makes delMdelC to calculate error matrix of mother particle.
214  this->calculateDELMDELC(&dMdC);
215 
216 
217  // ...calculates error matrix of mother particle.
218  HepSymMatrix Em(Ec.similarity(dMdC));
219 
220 
221  // ...makes mother particle
222  double px = 0, py = 0, pz = 0, E = 0;
223  for (int i = 0; i < m_TrackCount; i++)
224  {
225  double a = 0, dx = 0, dy = 0;
226  if (m_FlagAtDecayPoint) {
227  const double c = Belle2::Const::speedOfLight * 1e-4;
228  a = -c * m_MagneticField * m_Tracks[i].getCharge();
229  dx = m_Vertex.x() - m_Tracks[i].getPosition(m_FlagBeforeAfter).x();
230  dy = m_Vertex.y() - m_Tracks[i].getPosition(m_FlagBeforeAfter).y();
231  }
232  px += m_Tracks[i].getMomentum(m_FlagBeforeAfter).x() - a * dy;
233  py += m_Tracks[i].getMomentum(m_FlagBeforeAfter).y() + a * dx;
234  pz += m_Tracks[i].getMomentum(m_FlagBeforeAfter).z();
235  E += m_Tracks[i].getMomentum(m_FlagBeforeAfter).t();
236  }
237 
238 
239  // ...momentum
240  m_Mother.setMomentum(HepLorentzVector(px, py, pz, E));
241  // ...position
243  // ...error
244  m_Mother.setError(Em);
246 
247 
249 }
250 
251 
252 void
253 MakeMotherKFit::calculateError(HepSymMatrix* Ec) const
254 {
255  // ...error matrix of tracks
256  {
257  int i = 0;
258  for (const auto& track : m_Tracks) {
259  (*Ec).sub(i * KFitConst::kNumber7 + 1, track.getError(m_FlagBeforeAfter));
260  i++;
261  }
262  }
263 
264 
265  // ...error matrix between tracks
266  if (m_FlagCorrelation) {
267  int i = 0, j = 1;
268  for (const auto& hm : m_Correlation) {
269  for (int k = 0; k < KFitConst::kNumber7; k++) for (int l = 0; l < KFitConst::kNumber7; l++) {
270  (*Ec)[k + i * KFitConst::kNumber7][l + j * KFitConst::kNumber7] = hm[k][l];
271  }
272 
273  if (j != m_TrackCount - 1) {
274  j++;
275  } else if (i != m_TrackCount - 2) {
276  i++;
277  j = i + 1;
278  } else {
279  break;
280  }
281  }
282  }
283 
284 
285  // ...error of vertex
286  if (m_FlagVertexError) {
288  }
289 
290 
291  // ...error matrix between vertex and tracks
293  int i = 0;
294  for (const auto& hm : m_TrackVertexError) {
295  for (int j = 0; j < 3; j++) for (int k = 0; k < KFitConst::kNumber7; k++) {
296  (*Ec)[j + m_TrackCount * KFitConst::kNumber7][k + i * KFitConst::kNumber7] = hm[j][k];
297  }
298  i++;
299  }
300  }
301 }
302 
303 
304 void
305 MakeMotherKFit::calculateDELMDELC(HepMatrix* dMdC) const
306 {
307  // ...local parameters
308  double sum_a = 0;
309 
310  for (int i = 0; i < m_TrackCount; i++) {
311  const double c = Belle2::Const::speedOfLight * 1e-4;
312  double a = -c * m_MagneticField * m_Tracks[i].getCharge();
313  sum_a += a;
314 
315  // ...sets "a" in dMdC.
316  (*dMdC)[0][5 + i * KFitConst::kNumber7] = a;
317  (*dMdC)[1][4 + i * KFitConst::kNumber7] = -a;
318 
319  // ...sets "1" in dMdC.
320  (*dMdC)[0][0 + i * KFitConst::kNumber7] = 1.;
321  (*dMdC)[1][1 + i * KFitConst::kNumber7] = 1.;
322  (*dMdC)[2][2 + i * KFitConst::kNumber7] = 1.;
323  (*dMdC)[3][3 + i * KFitConst::kNumber7] = 1.;
324  }
325 
326  // ...sets "1" in dMdC.
327  (*dMdC)[4][0 + m_TrackCount * KFitConst::kNumber7] = 1.;
328  (*dMdC)[5][1 + m_TrackCount * KFitConst::kNumber7] = 1.;
329  (*dMdC)[6][2 + m_TrackCount * KFitConst::kNumber7] = 1.;
330 
331  // ...sets "sum_a" in dMdC.
332  (*dMdC)[0][1 + m_TrackCount * KFitConst::kNumber7] = - sum_a;
333  (*dMdC)[1][0 + m_TrackCount * KFitConst::kNumber7] = sum_a;
334 }
R E
internal precision of FFTW codelets
static const double speedOfLight
[cm/ns]
Definition: Const.h:686
static void displayError(const char *file, const int line, const char *func, const enum ECode code)
Display a description of error and its location.
Definition: KFitError.h:72
ECode
ECode is a error code enumerate.
Definition: KFitError.h:34
@ kBadTrackSize
Track count too small to perform fit.
Definition: KFitError.h:47
@ kBadMatrixSize
Wrong correlation matrix size.
Definition: KFitError.h:49
KFitTrack is a container of the track information (Lorentz vector, position, and error matrix),...
Definition: KFitTrack.h:38
void setCharge(const double q)
Set a charge of the track.
Definition: KFitTrack.cc:134
const CLHEP::HepLorentzVector getMomentum(const int flag=KFitConst::kAfterFit) const
Get a Lorentz vector of the track.
Definition: KFitTrack.cc:156
void setPosition(const HepPoint3D &x, const int flag=KFitConst::kBeforeFit)
Set a position of the track.
Definition: KFitTrack.cc:109
const CLHEP::HepSymMatrix getError(const int flag=KFitConst::kAfterFit) const
Get an error matrix of the track.
Definition: KFitTrack.cc:172
void setMomentum(const CLHEP::HepLorentzVector &p, const int flag=KFitConst::kBeforeFit)
Set a Lorentz vector of the track.
Definition: KFitTrack.cc:95
void setError(const CLHEP::HepSymMatrix &e, const int flag=KFitConst::kBeforeFit)
Set an error matrix of the track.
Definition: KFitTrack.cc:121
const HepPoint3D getPosition(const int flag=KFitConst::kAfterFit) const
Get a position of the track.
Definition: KFitTrack.cc:164
enum KFitError::ECode setFlagBeforeAfter(const int flag)
Set a flag to control computational parameters for the mother particle property before or after the f...
enum KFitError::ECode setVertex(const HepPoint3D &v)
Set a vertex position of the mother particle.
bool m_FlagTrackVertexError
Flag to indicate if the vertex error matrix of the child particle is preset.
double m_Charge
Charge of the mother particle.
bool m_FlagAtDecayPoint
Flag controlled by setFlagAtDecayPoint().
enum KFitError::ECode addTrack(const KFitTrack &kp)
Add a track to the make-mother object.
enum KFitError::ECode setFlagAtDecayPoint(const bool flag)
Set a flag to control computational point for the mother particle property at the decay point or not.
double m_MagneticField
Magnetic field.
enum KFitError::ECode setTrackZeroVertexError(void)
Indicate no vertex uncertainty in the child particle in the addTrack'ed order.
enum KFitError::ECode doMake(void)
Perform a reconstruction of mother particle.
const CLHEP::HepSymMatrix getMotherError(void) const
Get an error matrix of the mother particle.
enum KFitError::ECode setCorrelation(const CLHEP::HepMatrix &e)
Set a correlation matrix.
const HepPoint3D getMotherPosition(void) const
Get a position of the mother particle.
CLHEP::Hep3Vector m_Vertex
Vertex position of the mother particle.
const KFitTrack getMother(void) const
Get a track object of the mother particle.
enum KFitError::ECode m_ErrorCode
Error code.
CLHEP::HepSymMatrix m_VertexError
Vertex error matrix of the mother particle.
enum KFitError::ECode setZeroCorrelation(void)
Indicate no correlation between tracks.
KFitTrack m_Mother
Track object of the mother particle.
enum KFitError::ECode setVertexError(const CLHEP::HepSymMatrix &e)
Set a vertex error matrix of the mother particle.
enum KFitError::ECode setTrackVertexError(const CLHEP::HepMatrix &e)
Set a vertex error matrix of the child particle in the addTrack'ed order.
const CLHEP::HepLorentzVector getMotherMomentum(void) const
Get a Lorentz vector of the mother particle.
enum KFitError::ECode setMagneticField(const double mf)
Change a magnetic field from the default value KFitConst::kDefaultMagneticField.
bool m_FlagVertexError
Flag to indicate if the vertex error matrix of the mother particle is preset.
std::vector< CLHEP::HepMatrix > m_TrackVertexError
Array of vertex error matrices of the child particles.
bool m_FlagCorrelation
Flag to indicate if the correlation matrix among the child particles is preset.
~MakeMotherKFit(void)
Destruct the object.
void calculateError(CLHEP::HepSymMatrix *Ec) const
Make a matrix to calculate error matrix of the mother particle.
void calculateDELMDELC(CLHEP::HepMatrix *e) const
Make delMdelC to calculate error matrix of the mother particle.
std::vector< KFitTrack > m_Tracks
Array of track objects of the child particles.
int m_TrackCount
Number of tracks.
int m_FlagBeforeAfter
Flag controlled by setFlagBeforeAfter().
std::vector< CLHEP::HepMatrix > m_Correlation
Array of correlation matrices among the child particles.
enum KFitError::ECode getErrorCode(void) const
Get a code of the last error.
Abstract base class for different kinds of events.
static constexpr double kDefaultMagneticField
Default magnetic field when not set externally.
Definition: KFitConst.h:51
static const int kAfterFit
Input parameter to specify after-fit when setting/getting a track attribute.
Definition: KFitConst.h:37
static const int kBeforeFit
Input parameter to specify before-fit when setting/getting a track attribute.
Definition: KFitConst.h:35
static const int kNumber7
Constant 7 to check matrix size (internal use)
Definition: KFitConst.h:32