Belle II Software development
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/dataobjects/Particle.h>
12#include <mdst/dataobjects/TrackFitResult.h>
13
14#include <rave/VertexFactory.h>
15
16//root
17#include <Math/ProbFunc.h>
18#include <Math/Vector4D.h>
19
20//stl
21using std::string;
22using std::vector;
23
24using namespace Belle2;
25using namespace analysis;
26
27
28
30{
31 //B2WARNING( "RaveVertexFitter::RaveVertexFitter()" );
32 if (RaveSetup::getRawInstance() == nullptr) {
33 B2FATAL("RaveSetup::initialize was not called. It has to be called before RaveSetup or RaveVertexFitter are used");
34 }
35 //B2WARNING "m_useBeamSpot " << m_useBeamSpot );
37 //B2WARNING("m_useBeamSpot " << m_useBeamSpot );
38}
39
41{
42 m_useBeamSpot = false;
43 if (RaveSetup::getRawInstance() == nullptr) {
44 B2FATAL("RaveSetup::initialize was not called. It has to be called before RaveSetup or RaveVertexFitter are used");
45 }
47}
48
49
50
52
53
54void RaveVertexFitter::addTrack(const TrackFitResult* const aTrackPtr)
55{
56 m_raveTracks.push_back(TrackFitResultToRaveTrack(aTrackPtr));
57}
58
59rave::Track RaveVertexFitter::TrackFitResultToRaveTrack(const TrackFitResult* const aTrackPtr) const
60{
61 const int id = m_raveTracks.size();
62
63 ROOT::Math::XYZVector pos = aTrackPtr->getPosition();
64 ROOT::Math::XYZVector mom = aTrackPtr->getMomentum();
65 TMatrixDSym cov(aTrackPtr->getCovariance6());
66
67
68 // state
69 rave::Vector6D ravestate(pos.X(), pos.Y(), pos.Z(),
70 mom.X(), mom.Y(), mom.Z());
71
72 rave::Covariance6D ravecov(cov(0, 0), cov(1, 0), cov(2, 0),
73 cov(1, 1), cov(2, 1), cov(2, 2),
74 cov(3, 0), cov(4, 0), cov(5, 0),
75 cov(3, 1), cov(4, 1), cov(5, 1),
76 cov(3, 2), cov(4, 2), cov(5, 2),
77 cov(3, 3), cov(4, 3), cov(5, 3),
78 cov(4, 4), cov(5, 4), cov(5, 5));
79
80 return rave::Track(id, ravestate, ravecov, rave::Charge(aTrackPtr->getChargeSign()), 1,
81 1); //the two 1s are just dummy values. They are not used by Rave anyway
82
83}
84
85
86void RaveVertexFitter::addTrack(const Particle* aParticlePtr)
87{
88 const int id = m_raveTracks.size();
89 const ROOT::Math::XYZVector& pos = aParticlePtr->getVertex();
90 const ROOT::Math::XYZVector& mom = aParticlePtr->getMomentum();
91 rave::Vector6D ravestate(pos.X(), pos.Y(), pos.Z(), mom.X(), mom.Y(), mom.Z());
92
93 const TMatrixFSym& cov = aParticlePtr->getMomentumVertexErrorMatrix();
94
95 rave::Covariance6D ravecov(cov(4, 4), cov(4, 5), cov(4, 6),
96 cov(5, 5), cov(5, 6), cov(6, 6),
97 cov(0, 4), cov(1, 4), cov(2, 4),
98 cov(0, 5), cov(1, 5), cov(2, 5),
99 cov(0, 6), cov(1, 6), cov(2, 6),
100 cov(0, 0), cov(0, 1), cov(0, 2),
101 cov(1, 1), cov(1, 2), cov(2, 2));
102
103 // 1 and 1 are dummy values for chi2 and ndf. the are not used for the vertex fit
104 m_raveTracks.emplace_back(id, ravestate, ravecov, rave::Charge(aParticlePtr->getCharge() + 0.1), 1, 1);
105
106 m_belleDaughters.push_back(const_cast<Particle*>(aParticlePtr));
107}
108
109void RaveVertexFitter::addMother(const Particle* aMotherParticlePtr)
110{
111 vector<Particle*> daughters = aMotherParticlePtr->getDaughters();
112
113 int nDaughters = daughters.size();
114 for (int i = 0; i not_eq nDaughters; ++i) {
115 addTrack(daughters[i]);
116 }
117
118}
119
120int RaveVertexFitter::fit(string options)
121{
122 //B2WARNING("RaveVertexFitter::fit(string options)" );
123 //B2WARNING("m_useBeamSpot " << m_useBeamSpot );
124 if (options == "default") {
125 options = "kalman";
126 }
127 int ndf = 0;
128
129 ndf = 2 * m_raveTracks.size();
130
131 if (m_useBeamSpot == true) {
132 ndf += 3;
133 }
134 if (ndf < 4) {
135 return -1;
136 }
137 int nOfVertices = -100;
138
139 if (m_useBeamSpot == true) {
140 const ROOT::Math::XYZVector& bsPos = RaveSetup::getRawInstance()->m_beamSpot;
141 const TMatrixDSym& bsCov = RaveSetup::getRawInstance()->m_beamSpotCov;
142 const rave::Covariance3D bsCovRave(bsCov(0, 0), bsCov(0, 1), bsCov(0, 2), bsCov(1, 1), bsCov(1, 2), bsCov(2, 2));
143 RaveSetup::getRawInstance()->m_raveVertexFactory->setBeamSpot(rave::Ellipsoid3D(rave::Point3D(bsPos.X(), bsPos.Y(), bsPos.Z()),
144 bsCovRave));
145 }
146 //B2WARNING( "now fitting with m_raveVertexFactory" );
147 RaveSetup::getRawInstance()->m_raveVertexFactory->setDefaultMethod(options);
149 nOfVertices = m_raveVertices.size();
150
151 return nOfVertices;
152}
153
155{
156
157 if (m_raveVertices.empty()) {
158 B2ERROR("There is no fitted Vertex. Maybe you did not call fit() or maybe the fit was not successful");
159 throw;
160 }
161 if (vertexId >= m_raveVertices.size()) {
162 B2ERROR("The Vertex id " << vertexId << " does not correspond to any fitted vertex");
163 throw;
164 }
165
166}
167
168ROOT::Math::XYZVector RaveVertexFitter::getPos(VecSize vertexId) const
169{
170 isVertexIdValid(vertexId);
171
172 return ROOT::Math::XYZVector(m_raveVertices[vertexId].position().x(), m_raveVertices[vertexId].position().y(),
173 m_raveVertices[vertexId].position().z());
174
175}
176
177double RaveVertexFitter::getWeight(int trackId, VecSize vertexId)const
178{
179 isVertexIdValid(vertexId);
180
181
182 const std::vector < std::pair < float, rave::Track > >& weightedTracks = m_raveVertices[vertexId].weightedTracks();
183 for (unsigned int i = 0; i not_eq weightedTracks.size(); ++i) {
184 if (weightedTracks[i].second.id() == trackId) {
185// B2WARNING( "returning weight for track with x coord: " <<weightedTracks[i].second.state().x() );
186 return weightedTracks[i].first;
187 }
188 }
189 return -1;
190
191}
192
194{
195
196 const std::vector < std::pair < float, rave::Track > >& weightedTracks = m_raveVertices[vertexId].weightedTracks();
197 const int n = weightedTracks.size();
198 vector<int> trackIds(n);
199 for (int i = 0; i not_eq n; ++i) {
200 trackIds[i] = weightedTracks[i].second.id();
201 }
202 return trackIds;
203}
204
206{
207 isVertexIdValid(vertexId);
208
209 return ROOT::Math::chisquared_cdf_c(m_raveVertices[vertexId].chiSquared(), m_raveVertices[vertexId].ndf());
210
211}
212
213double RaveVertexFitter::getNdf(VecSize vertexId) const
214{
215 isVertexIdValid(vertexId);
216
217 return m_raveVertices[vertexId].ndf();
218
219}
220
221double RaveVertexFitter::getChi2(VecSize vertexId) const
222{
223 isVertexIdValid(vertexId);
224
225 return m_raveVertices[vertexId].chiSquared();
226
227}
228
229TMatrixDSym RaveVertexFitter::getCov(VecSize vertexId) const
230{
231 isVertexIdValid(vertexId);
232
233 TMatrixDSym Cov(3);
234 Cov(0, 0) = m_raveVertices[vertexId].error().dxx();
235 Cov(0, 1) = m_raveVertices[vertexId].error().dxy();
236 Cov(0, 2) = m_raveVertices[vertexId].error().dxz();
237 Cov(1, 1) = m_raveVertices[vertexId].error().dyy();
238 Cov(1, 2) = m_raveVertices[vertexId].error().dyz();
239 Cov(2, 2) = m_raveVertices[vertexId].error().dzz();
240 Cov(1, 0) = Cov(0, 1);
241 Cov(2, 1) = Cov(1, 2);
242 Cov(2, 0) = Cov(0, 2);
243
244 return Cov;
245
246}
247
248
250{
251 if (m_raveVertices.size() != 1) {
252 B2ERROR("RaveVertexFitter: Daughters update works only with a single vertex");
253 return;
254 }
255
256 if (!m_raveVertices[0].hasRefittedTracks()) {
257 B2WARNING("RaveVertexFitter: Fitted vertex has no refitted tracks");
258 return;
259 }
260
261 std::vector < rave::Track > rTracks = m_raveVertices[0].tracks(); //< the original tracks
262
263 for (unsigned int i = 0; i < rTracks.size(); i++) {
264 rave::Track rtrk = m_raveVertices[0].refittedTrack(rTracks[i]);
265 const rave::Point3D fittedV = rtrk.position();
266 const rave::Vector3D fittedP = rtrk.momentum();
267 const rave::Covariance6D& fittedCov = rtrk.error();
268
269 ROOT::Math::XYZVector x3(fittedV.x(), fittedV.y(), fittedV.z());
270 double fittedE = sqrt(fittedP.mag2() + (m_belleDaughters[i]->getMass() * (m_belleDaughters[i]->getMass())));
271 ROOT::Math::PxPyPzEVector p4(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:76
ROOT::Math::XYZVector getVertex() const
Returns vertex position (POCA for charged, IP for neutral FS particles)
Definition Particle.h:651
double getCharge(void) const
Returns particle charge.
Definition Particle.cc:653
std::vector< Particle * > getDaughters() const
Returns a vector of pointers to daughter particles.
Definition Particle.cc:668
ROOT::Math::XYZVector getMomentum() const
Returns momentum vector.
Definition Particle.h:580
TMatrixFSym getMomentumVertexErrorMatrix() const
Returns 7x7 error matrix.
Definition Particle.cc:451
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.
ROOT::Math::XYZVector getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
ROOT::Math::XYZVector 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:71
bool m_useBeamSpot
flag determines if beam spot information should be used for vertex fit.
Definition RaveSetup.h:69
static RaveSetup * getRawInstance()
Same as getInstance(), but no check if the instance is initialised.
Definition RaveSetup.cc:29
ROOT::Math::XYZVector m_beamSpot
beam spot position.
Definition RaveSetup.h:70
rave::VertexFactory * m_raveVertexFactory
The RAVE vertex factory is the principal interface offered by the RAVE vertex fitting library.
Definition RaveSetup.h:74
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 ....
ROOT::Math::XYZVector getPos(VecSize vertexId=0) const
get the position of the fitted vertex.
void addTrack(const Particle *const aParticlePtr)
add a track (in the format of a 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 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()
The default constructor checks if RaveSetup was initialized and will set the attributes of RaveVertex...
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.
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.