Belle II Software  release-06-01-15
RaveVertexFitter.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 
10 #include <analysis/VertexFitting/RaveInterface/RaveVertexFitter.h>
11 #include <analysis/VertexFitting/RaveInterface/RaveSetup.h>
12 #include <mdst/dataobjects/Track.h>
13 
14 #include <rave/VertexFactory.h>
15 
16 //root
17 #include <Math/ProbFunc.h>
18 //stl
19 using std::string;
20 #include <vector>
21 using std::vector;
22 
23 using namespace Belle2;
24 using namespace analysis;
25 
26 
27 
28 RaveVertexFitter::RaveVertexFitter(): m_useBeamSpot(false)
29 {
30  //B2WARNING( "RaveVertexFitter::RaveVertexFitter()" );
31  if (RaveSetup::getRawInstance() == nullptr) {
32  B2FATAL("RaveSetup::initialize was not called. It has to be called before RaveSetup or RaveVertexFitter are used");
33  }
34  //B2WARNING "m_useBeamSpot " << m_useBeamSpot );
36  //B2WARNING("m_useBeamSpot " << m_useBeamSpot );
37 }
38 
40 {
41  m_useBeamSpot = false;
42  if (RaveSetup::getRawInstance() == nullptr) {
43  B2FATAL("RaveSetup::initialize was not called. It has to be called before RaveSetup or RaveVertexFitter are used");
44  }
46 }
47 
48 
49 
51 
52 
53 void RaveVertexFitter::addTrack(const TrackFitResult* const aTrackPtr)
54 {
55  m_raveTracks.push_back(TrackFitResultToRaveTrack(aTrackPtr));
56 }
57 
58 rave::Track RaveVertexFitter::TrackFitResultToRaveTrack(const TrackFitResult* const aTrackPtr) const
59 {
60  const int id = m_raveTracks.size();
61 
62  TVector3 pos = aTrackPtr->getPosition();
63  TVector3 mom = aTrackPtr->getMomentum();
64  TMatrixF cov(aTrackPtr->getCovariance6());
65 
66 
67  // state
68  rave::Vector6D ravestate(pos.X(), pos.Y(), pos.Z(),
69  mom.X(), mom.Y(), mom.Z());
70 
71  rave::Covariance6D ravecov(cov(0, 0), cov(1, 0), cov(2, 0),
72  cov(1, 1), cov(2, 1), cov(2, 2),
73  cov(3, 0), cov(4, 0), cov(5, 0),
74  cov(3, 1), cov(4, 1), cov(5, 1),
75  cov(3, 2), cov(4, 2), cov(5, 2),
76  cov(3, 3), cov(4, 3), cov(5, 3),
77  cov(4, 4), cov(5, 4), cov(5, 5));
78 
79  return rave::Track(id, ravestate, ravecov, rave::Charge(aTrackPtr->getChargeSign()), 1,
80  1); //the two 1s are just dummy values. They are not used by Rave anyway
81 
82 }
83 
84 
85 void RaveVertexFitter::addTrack(const Particle* aParticlePtr)
86 {
87  const int id = m_raveTracks.size();
88  const TVector3& pos = aParticlePtr->getVertex();
89  const TVector3& mom = aParticlePtr->getMomentum();
90  rave::Vector6D ravestate(pos.X(), pos.Y(), pos.Z(), mom.X(), mom.Y(), mom.Z());
91 
92  const TMatrixFSym& cov = aParticlePtr->getMomentumVertexErrorMatrix();
93 
94  rave::Covariance6D ravecov(cov(4, 4), cov(4, 5), cov(4, 6),
95  cov(5, 5), cov(5, 6), cov(6, 6),
96  cov(0, 4), cov(1, 4), cov(2, 4),
97  cov(0, 5), cov(1, 5), cov(2, 5),
98  cov(0, 6), cov(1, 6), cov(2, 6),
99  cov(0, 0), cov(0, 1), cov(0, 2),
100  cov(1, 1), cov(1, 2), cov(2, 2));
101 
102  // 1 and 1 are dummy values for chi2 and ndf. the are not used for the vertex fit
103  m_raveTracks.emplace_back(id, ravestate, ravecov, rave::Charge(aParticlePtr->getCharge() + 0.1), 1, 1);
104 
105  m_belleDaughters.push_back(const_cast<Particle*>(aParticlePtr));
106 }
107 
108 void RaveVertexFitter::addMother(const Particle* aMotherParticlePtr)
109 {
110  vector<Particle*> daughters = aMotherParticlePtr->getDaughters();
111 
112  int nDaughters = daughters.size();
113  for (int i = 0; i not_eq nDaughters; ++i) {
114  addTrack(daughters[i]);
115  }
116 
117 }
118 
119 int RaveVertexFitter::fit(string options)
120 {
121  //B2WARNING("RaveVertexFitter::fit(string options)" );
122  //B2WARNING("m_useBeamSpot " << m_useBeamSpot );
123  if (options == "default") {
124  options = "kalman";
125  }
126  int ndf = 0;
127 
128  ndf = 2 * m_raveTracks.size();
129 
130  if (m_useBeamSpot == true) {
131  ndf += 3;
132  }
133  if (ndf < 4) {
134  return -1;
135  }
136  int nOfVertices = -100;
137 
138  if (m_useBeamSpot == true) {
139  const TVector3& bsPos = RaveSetup::getRawInstance()->m_beamSpot;
140  const TMatrixDSym& bsCov = RaveSetup::getRawInstance()->m_beamSpotCov;
141  const rave::Covariance3D bsCovRave(bsCov(0, 0), bsCov(0, 1), bsCov(0, 2), bsCov(1, 1), bsCov(1, 2), bsCov(2, 2));
142  RaveSetup::getRawInstance()->m_raveVertexFactory->setBeamSpot(rave::Ellipsoid3D(rave::Point3D(bsPos.X(), bsPos.Y(), bsPos.Z()),
143  bsCovRave));
144  }
145  //B2WARNING( "now fitting with m_raveVertexFactory" );
146  RaveSetup::getRawInstance()->m_raveVertexFactory->setDefaultMethod(options);
148  nOfVertices = m_raveVertices.size();
149 
150  return nOfVertices;
151 }
152 
153 void RaveVertexFitter::isVertexIdValid(const VecSize vertexId) const
154 {
155 
156  if (m_raveVertices.empty()) {
157  B2ERROR("There is no fitted Vertex. Maybe you did not call fit() or maybe the fit was not successful");
158  throw;
159  }
160  if (vertexId >= m_raveVertices.size()) {
161  B2ERROR("The Vertex id " << vertexId << " does not correspond to any fitted vertex");
162  throw;
163  }
164 
165 }
166 
167 TVector3 RaveVertexFitter::getPos(VecSize vertexId) const
168 {
169  isVertexIdValid(vertexId);
170 
171  return TVector3(m_raveVertices[vertexId].position().x(), m_raveVertices[vertexId].position().y(),
172  m_raveVertices[vertexId].position().z());
173 
174 }
175 
176 double RaveVertexFitter::getWeight(int trackId, VecSize vertexId)const
177 {
178  isVertexIdValid(vertexId);
179 
180 
181  const std::vector < std::pair < float, rave::Track > >& weightedTracks = m_raveVertices[vertexId].weightedTracks();
182  for (unsigned int i = 0; i not_eq weightedTracks.size(); ++i) {
183  if (weightedTracks[i].second.id() == trackId) {
184 // B2WARNING( "returning weight for track with x coord: " <<weightedTracks[i].second.state().x() );
185  return weightedTracks[i].first;
186  }
187  }
188  return -1;
189 
190 }
191 
192 std::vector<int> RaveVertexFitter::getTrackIdsForOneVertex(VecSize vertexId) const
193 {
194 
195  const std::vector < std::pair < float, rave::Track > >& weightedTracks = m_raveVertices[vertexId].weightedTracks();
196  const int n = weightedTracks.size();
197  vector<int> trackIds(n);
198  for (int i = 0; i not_eq n; ++i) {
199  trackIds[i] = weightedTracks[i].second.id();
200  }
201  return trackIds;
202 }
203 
204 double RaveVertexFitter::getPValue(VecSize vertexId) const
205 {
206  isVertexIdValid(vertexId);
207 
208  return ROOT::Math::chisquared_cdf_c(m_raveVertices[vertexId].chiSquared(), m_raveVertices[vertexId].ndf());
209 
210 }
211 
212 double RaveVertexFitter::getNdf(VecSize vertexId) const
213 {
214  isVertexIdValid(vertexId);
215 
216  return m_raveVertices[vertexId].ndf();
217 
218 }
219 
220 double RaveVertexFitter::getChi2(VecSize vertexId) const
221 {
222  isVertexIdValid(vertexId);
223 
224  return m_raveVertices[vertexId].chiSquared();
225 
226 }
227 
228 TMatrixDSym RaveVertexFitter::getCov(VecSize vertexId) const
229 {
230  isVertexIdValid(vertexId);
231 
232  TMatrixDSym Cov(3);
233  Cov(0, 0) = m_raveVertices[vertexId].error().dxx();
234  Cov(0, 1) = m_raveVertices[vertexId].error().dxy();
235  Cov(0, 2) = m_raveVertices[vertexId].error().dxz();
236  Cov(1, 1) = m_raveVertices[vertexId].error().dyy();
237  Cov(1, 2) = m_raveVertices[vertexId].error().dyz();
238  Cov(2, 2) = m_raveVertices[vertexId].error().dzz();
239  Cov(1, 0) = Cov(0, 1);
240  Cov(2, 1) = Cov(1, 2);
241  Cov(2, 0) = Cov(0, 2);
242 
243  return Cov;
244 
245 }
246 
247 
249 {
250  if (m_raveVertices.size() != 1) {
251  B2ERROR("RaveVertexFitter: Daughters update works only with a single vertex");
252  return;
253  }
254 
255  if (!m_raveVertices[0].hasRefittedTracks()) {
256  B2WARNING("RaveVertexFitter: Fitted vertex has no refitted tracks");
257  return;
258  }
259 
260  std::vector < rave::Track > rTracks = m_raveVertices[0].tracks(); //< the original tracks
261 
262  for (unsigned int i = 0; i < rTracks.size(); i++) {
263  rave::Track rtrk = m_raveVertices[0].refittedTrack(rTracks[i]);
264  const rave::Point3D fittedV = rtrk.position();
265  const rave::Vector3D fittedP = rtrk.momentum();
266  const rave::Covariance6D& fittedCov = rtrk.error();
267 
268  TVector3 x3(fittedV.x(), fittedV.y(), fittedV.z());
269  TLorentzVector p4;
270  double fittedE = sqrt(fittedP.mag2() + (m_belleDaughters[i]->getMass() * (m_belleDaughters[i]->getMass())));
271  p4.SetXYZT(fittedP.x(), fittedP.y(), fittedP.z(), fittedE);
272 
273  TMatrixDSym fitted7CovPart = m_belleDaughters[i]->getMomentumVertexErrorMatrix() ;
274 
275  //px,py,pz,E,x,y,z
276  TMatrixDSym fitted7CovM(7);
277  fitted7CovM(0, 0) = fittedCov.dpxpx(); fitted7CovM(0, 0) = fitted7CovM(0, 0);
278  fitted7CovM(0, 1) = fittedCov.dpxpy(); fitted7CovM(1, 0) = fitted7CovM(0, 1);
279  fitted7CovM(0, 2) = fittedCov.dpxpz(); fitted7CovM(2, 0) = fitted7CovM(0, 2);
280  fitted7CovM(0, 3) = fitted7CovPart(0, 3); fitted7CovM(3, 0) = fitted7CovM(0, 3);
281  fitted7CovM(0, 4) = fittedCov.dxpx(); fitted7CovM(4, 0) = fitted7CovM(0, 4);
282  fitted7CovM(0, 5) = fittedCov.dypx(); fitted7CovM(5, 0) = fitted7CovM(0, 5);
283  fitted7CovM(0, 6) = fittedCov.dzpx(); fitted7CovM(6, 0) = fitted7CovM(0, 6);
284 
285  fitted7CovM(1, 1) = fittedCov.dpypy(); fitted7CovM(1, 1) = fitted7CovM(1, 1);
286  fitted7CovM(1, 2) = fittedCov.dpypz(); fitted7CovM(2, 1) = fitted7CovM(1, 2);
287  fitted7CovM(1, 3) = fitted7CovPart(1, 3); fitted7CovM(3, 1) = fitted7CovM(1, 3);
288  fitted7CovM(1, 4) = fittedCov.dxpy(); fitted7CovM(4, 1) = fitted7CovM(1, 4);
289  fitted7CovM(1, 5) = fittedCov.dypy(); fitted7CovM(5, 1) = fitted7CovM(1, 5);
290  fitted7CovM(1, 6) = fittedCov.dzpy(); fitted7CovM(6, 1) = fitted7CovM(1, 6);
291 
292  fitted7CovM(2, 2) = fittedCov.dpzpz(); fitted7CovM(2, 2) = fitted7CovM(2, 2);
293  fitted7CovM(2, 3) = fitted7CovPart(2, 3); fitted7CovM(3, 2) = fitted7CovM(2, 3);
294  fitted7CovM(2, 4) = fittedCov.dxpz(); fitted7CovM(4, 2) = fitted7CovM(2, 4);
295  fitted7CovM(2, 5) = fittedCov.dypz(); fitted7CovM(5, 2) = fitted7CovM(2, 5);
296  fitted7CovM(2, 6) = fittedCov.dzpz(); fitted7CovM(6, 2) = fitted7CovM(2, 6);
297 
298  fitted7CovM(3, 3) = fitted7CovPart(3, 3); fitted7CovM(3, 3) = fitted7CovM(3, 3);
299  fitted7CovM(3, 4) = fitted7CovPart(3, 4); fitted7CovM(4, 3) = fitted7CovM(3, 4);
300  fitted7CovM(3, 5) = fitted7CovPart(3, 5); fitted7CovM(5, 3) = fitted7CovM(3, 5);
301  fitted7CovM(3, 6) = fitted7CovPart(3, 6); fitted7CovM(6, 3) = fitted7CovM(3, 6);
302 
303  fitted7CovM(4, 4) = fittedCov.dxx(); fitted7CovM(4, 4) = fitted7CovM(4, 4);
304  fitted7CovM(4, 5) = fittedCov.dxy(); fitted7CovM(5, 4) = fitted7CovM(4, 5);
305  fitted7CovM(4, 6) = fittedCov.dxz(); fitted7CovM(6, 4) = fitted7CovM(4, 6);
306 
307  fitted7CovM(5, 5) = fittedCov.dyy(); fitted7CovM(5, 5) = fitted7CovM(5, 5);
308  fitted7CovM(5, 6) = fittedCov.dyz(); fitted7CovM(6, 5) = fitted7CovM(5, 6);
309 
310  fitted7CovM(6, 6) = fittedCov.dzz(); fitted7CovM(6, 6) = fitted7CovM(6, 6);
311 
312 
313  float pValDau = m_belleDaughters[i]->getPValue();
314 
315  m_belleDaughters[i]->updateMomentum(p4, x3, fitted7CovM, pValDau);
316 
317  }
318 
319 }
Class to store reconstructed particles.
Definition: Particle.h:74
TVector3 getVertex() const
Returns vertex position (POCA for charged, IP for neutral FS particles)
Definition: Particle.h:551
TVector3 getMomentum() const
Returns momentum vector.
Definition: Particle.h:488
float getCharge(void) const
Returns particle charge.
Definition: Particle.cc:630
std::vector< Belle2::Particle * > getDaughters() const
Returns a vector of pointers to daughter particles.
Definition: Particle.cc:645
TMatrixFSym getMomentumVertexErrorMatrix() const
Returns 7x7 error matrix.
Definition: Particle.cc:430
Values of the result of a track fit with a given particle hypothesis.
short getChargeSign() const
Return track charge (1 or -1).
TMatrixDSym getCovariance6() const
Position and Momentum Covariance Matrix.
TVector3 getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
TVector3 getPosition() const
Getter for vector of position at closest approach of track in r/phi projection.
TMatrixDSym m_beamSpotCov
beam spot position covariance matrix.
Definition: RaveSetup.h:74
bool m_useBeamSpot
flag determines if beam spot information should be used for vertex fit.
Definition: RaveSetup.h:72
static RaveSetup * getRawInstance()
Same as getInstance(), but no check if the instance is initialised.
Definition: RaveSetup.cc:27
rave::VertexFactory * m_raveVertexFactory
The RAVE vertex factory is the principal interface offered by the RAVE vertex fitting library.
Definition: RaveSetup.h:77
TVector3 m_beamSpot
beam spot position.
Definition: RaveSetup.h:73
std::vector< Particle * > m_belleDaughters
Belle Particle pointers input.
TMatrixDSym getCov(VecSize vertexId=0) const
get the covariance matrix (3x3) of the of the fitted vertex position.
bool m_useBeamSpot
flag determines if the beam spot will be used or not.
int fit(std::string options="default")
do the vertex fit with all tracks previously added with the addTrack or addMother function.
std::vector< int >::size_type VecSize
I am using std::vector<int>::size_type because it is the official return value of ....
void addTrack(const Particle *const aParticlePtr)
add a track (in the format of a Belle2::Particle) to set of tracks that should be fitted to a vertex
std::vector< int > getTrackIdsForOneVertex(VecSize vertexId=0) const
get the ids of the tracks Rave associated with a specific vertex.
double getNdf(VecSize vertexId=0) const
get the number of degrees of freedom (NDF) of the fitted vertex.
rave::Track TrackFitResultToRaveTrack(const TrackFitResult *const aTrackPtr) const
converts a track from Belle2::TrackFitResult format to rave::Track format
double getChi2(VecSize vertexId=0) const
get the χ² of the fitted vertex.
std::vector< rave::Vertex > m_raveVertices
holds the fitted vertices after fit() was called in the format used by Rave
void isVertexIdValid(const VecSize vertexId) const
checks if the vertex the user requested via one of the getters it actually there
void initBeamSpotMember()
Initialize m_useBeamSpot.
TVector3 getPos(VecSize vertexId=0) const
get the position of the fitted vertex.
void updateDaughters()
update the Daughters particles
double getWeight(int trackId, VecSize vertexId=0) const
get the weight Rave assigned to a specific input track.
double getPValue(VecSize vertexId=0) const
get the p value of the fitted vertex.
std::vector< rave::Track > m_raveTracks
holds the tracks that were added to a RaveVertexFitter object in the format used by Rave
void addMother(const Particle *const aMotherParticlePtr)
All daughters of the argument of this function will be used as input for the vertex fit.
HepGeom::Vector3D< double > Vector3D
3D Vector
Definition: Cell.h:34
HepGeom::Point3D< double > Point3D
3D point
Definition: Cell.h:32
Abstract base class for different kinds of events.