Belle II Software  release-05-01-25
RecoTrack.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributor: Wouter Hulsbergen, Francesco Tenchini, Jo-Frederik Krohn *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <analysis/VertexFitting/TreeFitter/RecoTrack.h>
12 #include <analysis/VertexFitting/TreeFitter/FitParams.h>
13 #include <analysis/VertexFitting/TreeFitter/HelixUtils.h>
14 
15 #include <framework/gearbox/Const.h>
16 #include <mdst/dataobjects/Track.h>
17 
18 #include <TMath.h>
19 
20 namespace TreeFitter {
21  constexpr double pi = TMath::Pi();
22  constexpr double twoPi = TMath::TwoPi();
23 
24  RecoTrack::RecoTrack(Belle2::Particle* particle, const ParticleBase* mother) :
25  RecoParticle(particle, mother),
26  m_bfield(0),
27  m_trackfit(particle->getTrackFitResult()),
28  m_cached(false),
29  m_flt(0),
30  m_params(5),
31  m_covariance(5, 5)
32  {
33  m_bfield = Belle2::BFieldManager::getField(TVector3(0, 0, 0)).Z() / Belle2::Unit::T; //Bz in Tesla
34  m_covariance = Eigen::Matrix<double, 5, 5>::Zero(5, 5);
35  }
36 
38  {
39  //initPar2
40  if (m_flt == 0) {
41  const_cast<RecoTrack*>(this)->updFltToMother(fitparams);
42  }
43  TVector3 recoP = m_trackfit->getHelix().getMomentumAtArcLength2D(m_flt, m_bfield);
44  const int momindex = momIndex();
45  fitparams.getStateVector()(momindex) = recoP.X();
46  fitparams.getStateVector()(momindex + 1) = recoP.Y();
47  fitparams.getStateVector()(momindex + 2) = recoP.Z();
48  return ErrCode(ErrCode::Status::success);
49  }
50 
51  ErrCode RecoTrack::initMotherlessParticle([[gnu::unused]] FitParams& fitparams)
52  {
53  return ErrCode(ErrCode::Status::success);
54  }
55 
56  ErrCode RecoTrack::initCovariance(FitParams& fitparams) const
57  {
58  // we only need a rough estimate of the covariance
59  TMatrixFSym p4Err = particle()->getMomentumErrorMatrix();
60  const int momindex = momIndex();
61 
62  for (int row = 0; row < 3; ++row) {
63  fitparams.getCovariance()(momindex + row, momindex + row) = 1000 * p4Err[row][row];
64  }
65 
66  return ErrCode(ErrCode::Status::success);
67  }
68 
69 
70  ErrCode RecoTrack::updFltToMother(const FitParams& fitparams)
71  {
72  const int posindexmother = mother()->posIndex();
73  m_flt = m_trackfit->getHelix().getArcLength2DAtXY(
74  fitparams.getStateVector()(posindexmother),
75  fitparams.getStateVector()(posindexmother + 1));
76  return ErrCode(ErrCode::Status::success);
77  } ;
78 
80  {
81  m_flt = flt;
82  std::vector<float> trackParameter = m_trackfit->getTau();
83  for (unsigned int i = 0; i < trackParameter.size(); ++i) {
84  m_params(i) = trackParameter[i];
85  }
86  TMatrixDSym cov = m_trackfit->getCovariance5();
87  for (int row = 0; row < 5; ++row) {
88  for (int col = 0; col <= row; ++col) {
89  m_covariance(row, col) = cov[row][col];
90  }
91  }
92 
93  m_cached = true;
94  return ErrCode(ErrCode::Status::success);
95  }
96 
97  ErrCode RecoTrack::projectRecoConstraint(const FitParams& fitparams, Projection& p) const
98  {
99  ErrCode status;
100  const int posindexmother = mother()->posIndex();
101  const int momindex = momIndex();
102 
103  Eigen::Matrix<double, 1, 6> positionAndMom = Eigen::Matrix<double, 1, 6>::Zero(1, 6);
104  positionAndMom.segment(0, 3) = fitparams.getStateVector().segment(posindexmother, 3);
105  positionAndMom.segment(3, 3) = fitparams.getStateVector().segment(momindex, 3);
106  Eigen::Matrix<double, 5, 6> jacobian = Eigen::Matrix<double, 5, 6>::Zero(5, 6);
107 
109  TVector3(
110  positionAndMom(0),
111  positionAndMom(1),
112  positionAndMom(2)),
113  TVector3(
114  positionAndMom(3),
115  positionAndMom(4),
116  positionAndMom(5)),
117  charge(),
118  m_bfield
119  );
120 
122  positionAndMom(0),
123  positionAndMom(1),
124  positionAndMom(2),
125  positionAndMom(3),
126  positionAndMom(4),
127  positionAndMom(5),
128  m_bfield,
129  charge()
130  );
131  if (!m_cached) {
132  auto* nonconst = const_cast<RecoTrack*>(this);
133  if (m_flt == 0) { nonconst->updFltToMother(fitparams); }
134  nonconst->updateParams(m_flt);
135  }
136 
137  Eigen::Matrix<double, 1, 5> helixpars(5);
138  helixpars(0) = helix.getD0();
139  helixpars(1) = helix.getPhi0();
140  helixpars(2) = helix.getOmega();
141  helixpars(3) = helix.getZ0();
142  helixpars(4) = helix.getTanLambda();
143 
144  p.getResiduals().segment(0, 5) = m_params - helixpars;
145 
146  //account for periodic boundary in phi residual
147  double phiResidual = p.getResiduals().segment(0, 5)(1);
148  phiResidual = std::fmod(phiResidual + pi, twoPi);
149  if (phiResidual < 0) phiResidual += twoPi;
150  phiResidual -= pi;
151  p.getResiduals().segment(0, 5)(1) = phiResidual;
152 
153  p.getV().triangularView<Eigen::Lower>() = m_covariance.triangularView<Eigen::Lower>();
154 
155  p.getH().block<5, 3>(0, posindexmother) = -1.0 * jacobian.block<5, 3>(0, 0);
156  p.getH().block<5, 3>(0, momindex) = -1.0 * jacobian.block<5, 3>(0, 3);
157 
158  return status;
159  }
160 
161 }//end recotrack
TreeFitter::HelixUtils::getJacobianToCartesianFrameworkHelix
static void getJacobianToCartesianFrameworkHelix(Eigen::Matrix< double, 5, 6 > &jacobian, const double x, const double y, const double z, const double px, const double py, const double pz, const double bfield, const double charge)
get the jacobian dh={helix pars}/dx={x,y,z,px,py,pz} for the implementation of the framework helix.
Definition: HelixUtils.cc:390
TreeFitter::RecoTrack::updFltToMother
ErrCode updFltToMother(const FitParams &fitparams)
update flight length to mother
Definition: RecoTrack.cc:78
Belle2::BFieldManager::getField
static void getField(const double *pos, double *field)
return the magnetic field at a given position.
Definition: BFieldManager.h:110
TreeFitter::RecoTrack::initCovariance
ErrCode initCovariance(FitParams &fitparams) const override
init covariance matrix of this particle constraint
Definition: RecoTrack.cc:64
TreeFitter::ErrCode
abstract errorocode be aware that the default is succes
Definition: ErrCode.h:23
TreeFitter::FitParams
Class to store and manage fitparams (statevector)
Definition: FitParams.h:29
TreeFitter::RecoTrack::RecoTrack
RecoTrack(Belle2::Particle *bc, const ParticleBase *mother)
constructor
Definition: RecoTrack.cc:32
TreeFitter::FitParams::getStateVector
Eigen::Matrix< double, -1, 1, 0, MAX_MATRIX_SIZE, 1 > & getStateVector()
getter for the fit parameters/statevector
Definition: FitParams.h:74
TreeFitter::RecoTrack
reprasentation of all charged final states FIXME rename since this name is taken in tracking
Definition: RecoTrack.h:27
TreeFitter::RecoTrack::initParticleWithMother
virtual ErrCode initParticleWithMother(FitParams &fitparams) override
init with mother particle (replacing initPar2)
Definition: RecoTrack.cc:45
TreeFitter::RecoTrack::m_params
Eigen::Matrix< double, 1, 5 > m_params
column vector to store the measurement
Definition: RecoTrack.h:98
Belle2::Unit::T
static const double T
[tesla]
Definition: Unit.h:130
TreeFitter::RecoTrack::projectRecoConstraint
virtual ErrCode projectRecoConstraint(const FitParams &, Projection &) const override
project the constraint (calculate residuals)
Definition: RecoTrack.cc:105
TreeFitter::RecoTrack::m_trackfit
const Belle2::TrackFitResult * m_trackfit
trackfit result from reconstruction
Definition: RecoTrack.h:89
TreeFitter::ParticleBase::charge
int charge() const
get charge
Definition: ParticleBase.h:176
TreeFitter::RecoTrack::initMotherlessParticle
virtual ErrCode initMotherlessParticle(FitParams &fitparams) override
init without mother particle
Definition: RecoTrack.cc:59
TreeFitter::RecoTrack::m_covariance
Eigen::Matrix< double, 5, 5 > m_covariance
only lower triangle filled!
Definition: RecoTrack.h:101
TreeFitter::ParticleBase::posIndex
virtual int posIndex() const
get vertex index (in statevector!)
Definition: ParticleBase.h:142
TreeFitter::RecoTrack::m_bfield
double m_bfield
B field along z
Definition: RecoTrack.h:86
Belle2::CDC::Helix
Helix parameter class.
Definition: Helix.h:51
TreeFitter::RecoParticle::momIndex
virtual int momIndex() const override
get momentum index
Definition: RecoParticle.h:59
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::TrackFitResult::getCovariance5
TMatrixDSym getCovariance5() const
Getter for covariance matrix of perigee parameters in matrix form.
Definition: TrackFitResult.cc:106
TreeFitter::ParticleBase::mother
const ParticleBase * mother() const
getMother() / hasMother()
Definition: ParticleBase.cc:295
TreeFitter::RecoTrack::m_cached
bool m_cached
flag to mark the particle as initialised
Definition: RecoTrack.h:92
Belle2::TrackFitResult::getHelix
Helix getHelix() const
Conversion to framework Helix (without covariance).
Definition: TrackFitResult.h:230
TreeFitter::RecoTrack::updateParams
ErrCode updateParams(double flt)
updated the cahed parameters
Definition: RecoTrack.cc:87
Belle2::TrackFitResult::getTau
std::vector< float > getTau() const
Getter for all perigee parameters.
Definition: TrackFitResult.h:215
TreeFitter::RecoTrack::m_flt
double m_flt
helix arc length at vertex
Definition: RecoTrack.h:95
TreeFitter::ParticleBase::particle
Belle2::Particle * particle() const
get basf2 particle
Definition: ParticleBase.h:109
Belle2::Particle::getMomentumErrorMatrix
TMatrixFSym getMomentumErrorMatrix() const
Returns the 4x4 momentum error matrix.
Definition: Particle.cc:401