33 BilloirTrack() : originalPerigee(nullptr),
chi2{} { DtWD.setZero(); DtWDx.setZero(); xpVec.setZero();}
34 virtual ~BilloirTrack() =
default;
44 BilloirVertex() :
chi2{},
ndf{} { DtWD_Sum.setZero(); DtWDx_Sum.setZero();}
45 virtual ~BilloirVertex() =
default;
58 if ( m_extrapolator.retrieve().isFailure() )
61 return StatusCode::FAILURE;
65 msg(MSG::INFO) <<
"Retrieved tool " << m_extrapolator <<
endmsg;
68 if ( m_linFactory.retrieve().isFailure() )
71 return StatusCode::FAILURE;
75 msg(MSG::INFO) <<
"Retrieved tool " << m_linFactory <<
endmsg;
78 return StatusCode::SUCCESS;
82 FastVertexFitter::FastVertexFitter (
const std::string&
t,
const std::string&
n,
const IInterface*
p ) : base_class (
t,
n,
p ),
83 m_maxIterations ( 3 ),
84 m_maxDchi2PerNdf ( 0.000001 )
90 declareInterface<IVertexFitter> (
this );
108 return fit ( originalPerigees, constraint );
116 if ( originalPerigees.empty() )
123 double chi2 = 2000000000000.;
124 unsigned int nRP = originalPerigees.size();
125 int ndf = nRP * ( 5-3 ) - 3;
129 bool constraint =
false;
130 if ( firstStartingPoint.covariancePosition().trace() != 0. )
135 ATH_MSG_VERBOSE(firstStartingPoint.covariancePosition().inverse().eval());
138 double chi2New=0.;
double chi2Old=0.;
142 auto fittedVertex = std::make_unique<xAOD::Vertex>();
143 fittedVertex->makePrivateStore();
145 std::vector<VxTrackAtVertex> tracksAtVertex;
146 std::vector<BilloirTrack> billoirTracks;
156 billoirTracks.clear();
166 BilloirVertex billoirVertex;
168 for (
const auto *originalPerigee : originalPerigees)
171 if ( linTrack==
nullptr )
173 ATH_MSG_DEBUG(
"Could not linearize track! Skipping this track!");
179 locXpVec[0] = locXpVec[0] - linPoint[0];
180 locXpVec[1] = locXpVec[1] - linPoint[1];
181 locXpVec[2] = locXpVec[2] - linPoint[2];
191 AmgMatrix(2,2) billoirCovMat = linTrack->expectedCovarianceAtPCA().block<2,2>(0,0);
194 AmgMatrix(2,2) billoirWeightMat = billoirCovMat.inverse().eval();
198 D = linTrack->positionJacobian().block<2,3>(0,0);
203 BilloirTrack locBilloirTrack;
204 locBilloirTrack.xpVec = locXpVec;
205 locBilloirTrack.DtWD = (D.transpose())*billoirWeightMat*D;
206 locBilloirTrack.chi2 = -1.0;
207 billoirVertex.DtWD_Sum += locBilloirTrack.DtWD;
208 locBilloirTrack.DtWDx = ((D.transpose())*billoirWeightMat*D)*locXpVec;
209 billoirVertex.DtWDx_Sum += locBilloirTrack.DtWDx;
210 locBilloirTrack.originalPerigee = originalPerigee;
211 billoirTracks.push_back ( locBilloirTrack );
213 delete linTrack; linTrack=
nullptr;
215 if ( billoirTracks.empty() )
217 ATH_MSG_DEBUG(
"No linearized tracks left after linearization! Should not happen!");
224 constraintPosInBilloirFrame.setZero();
226 constraintPosInBilloirFrame[0] = firstStartingPoint.
position() [0]-linPoint [0];
227 constraintPosInBilloirFrame[1] = firstStartingPoint.
position() [1]-linPoint [1];
228 constraintPosInBilloirFrame[2] = firstStartingPoint.
position() [2]-linPoint [2];
229 billoirVertex.DtWDx_Sum += firstStartingPoint.covariancePosition().inverse().eval() *constraintPosInBilloirFrame;
230 billoirVertex.DtWD_Sum += firstStartingPoint.covariancePosition().inverse().eval();
233 AmgMatrix(3,3) cov_delta_V_mat = billoirVertex.DtWD_Sum.inverse( ) ;
235 Amg::Vector3D delta_V = cov_delta_V_mat * billoirVertex.DtWDx_Sum;
238 for ( BTIter = billoirTracks.begin(); BTIter != billoirTracks.end() ; ++BTIter )
243 diff[0] = ( *BTIter ).xpVec[0] - delta_V[0];
244 diff[1] = ( *BTIter ).xpVec[1] - delta_V[1];
245 diff[2] = ( *BTIter ).xpVec[2] - delta_V[2];
246 ( *BTIter ).chi2 =
diff.dot(( *BTIter ).DtWD*
diff );
247 if ( ( *BTIter ).chi2 < 0 )
249 std::cout <<
"VxFastFit::calculate: error in chi2_per_track: "<<( *BTIter ).chi2<<
"\n";
252 chi2New += ( *BTIter ).chi2;
261 deltaTrk[0] = delta_V[0] - ( firstStartingPoint.
position() [0] - linPoint [0] );
262 deltaTrk[1] = delta_V[1] - ( firstStartingPoint.
position() [1] - linPoint [1] );
263 deltaTrk[2] = delta_V[2] - ( firstStartingPoint.
position() [2] - linPoint [2] );
264 chi2New +=
Amg::chi2(firstStartingPoint.covariancePosition().inverse().eval(), deltaTrk);
269 tmpPos[0] += delta_V[0]; tmpPos[1] += delta_V[1]; tmpPos[2] += delta_V[2];
276 if ( chi2New <
chi2 )
285 fittedVertex->setPosition( linPoint );
286 fittedVertex->setCovariancePosition( cov_delta_V_mat );
287 fittedVertex->setFitQuality(
chi2,
ndf );
298 tracksAtVertex.clear();
300 Amg::Vector3D pointToExtrapolateTo ( linPoint [0], linPoint [1], linPoint [2] );
302 for ( BTIter = billoirTracks.begin(); BTIter != billoirTracks.end() ; ++BTIter )
307 auto extrapolatedPerigee = std::unique_ptr<const Trk::TrackParameters> (
m_extrapolator->extrapolate (
308 Gaudi::Hive::currentContext(),
309 * ( *BTIter ).originalPerigee,
311 if ( extrapolatedPerigee==
nullptr )
313 extrapolatedPerigee = std::unique_ptr<const Trk::TrackParameters>(((*BTIter).originalPerigee)->clone());
314 ATH_MSG_DEBUG(
"Could not extrapolate these track parameters to final vertex position! Storing original position as final one ...");
318 ( *BTIter ).originalPerigee ) ;
319 tracksAtVertex.push_back ( *tmpVxTrkAtVtx );
321 delete tmpVxTrkAtVtx;
330 fittedVertex->vxTrackAtVertex() = tracksAtVertex;
332 return fittedVertex.release();
338 return fit ( perigeeList, tmpVtx );
350 return fit(vectorTrk, constraint);
356 if(vectorTrk.empty())
358 msg(MSG::INFO)<<
"Empty vector of tracks passed"<<
endmsg;
363 std::vector<const Trk::TrackParameters*> measuredPerigees;
365 for(
const auto *
i : vectorTrk)
369 if(tmpMeasPer!=
nullptr) measuredPerigees.push_back(tmpMeasPer);
370 else msg(MSG::INFO)<<
"Failed to dynamic_cast this track parameters to perigee"<<
endmsg;
376 if (fittedVertex ==
nullptr){
387 for(
unsigned int i = 0;
i <vectorTrk.size(); ++
i)
402 for (
unsigned int i = 0 ;
i < VTAVsize ; ++
i)
407 ATH_MSG_WARNING (
" Trying to set link to xAOD::TrackParticle. The VxTrackAtVertex is not found");
422 ATH_MSG_WARNING (
"Skipping track. Trying to set link to something else than xAOD::TrackParticle. Neutrals not supported.");