59 #include "GblFitter.h"
60 #include "../include/GblFitStatus.h"
61 #include "GblFitterInfo.h"
64 #include "ICalibrationParametersDerivatives.h"
72 #include <FieldManager.h>
75 #include <Math/SMatrix.h>
77 #include <TVectorDfwd.h>
90 GblFitter::~GblFitter() {
91 if (m_segmentController) {
92 delete m_segmentController;
93 m_segmentController =
nullptr;
99 if (m_segmentController) {
100 delete m_segmentController;
101 m_segmentController =
nullptr;
103 m_segmentController = controler;
108 cleanGblInfo(trk, rep);
115 bool fitQoverP =
true;
118 if (!(Bfield > 1.e-16))
125 double lostWeight = 0.;
130 trk->setFitStatus(gblfs, rep);
131 gblfs->setCurvature(fitQoverP);
139 constructGblInfo(trk, rep)
144 gblfs->setIsFittedWithReferenceTrack(
true);
145 gblfs->setNumIterations(0);
146 if (m_externalIterations < 1)
151 unsigned int nFailed = 0;
153 std::vector<std::string> gblIterations;
154 gblIterations.push_back(m_gblInternalIterations);
158 for (
unsigned int iIter = 0; iIter < m_externalIterations; iIter++) {
160 int nscat = 0, nmeas = 0, ndummy = 0;
161 std::vector<gbl::GblPoint> points = collectGblPoints(trk, rep);
162 for(
unsigned int ip = 0;ip<points.size(); ip++) {
164 if (p.hasScatterer())
166 if (p.hasMeasurement())
168 if(!p.hasMeasurement()&&!p.hasScatterer())
173 fitRes = traj.
fit(Chi2, Ndf, lostWeight, (iIter == m_externalIterations - 1) ? m_gblInternalIterations :
"");
176 updateGblInfo(traj, trk, rep);
180 if (m_recalcJacobians > iIter) {
183 for (
unsigned int ip = 0; ip < trk->getNumPoints(); ip++) {
184 if (trk->getPoint(ip)->hasFitterInfo(rep) && (currFitterInfo =
dynamic_cast<GblFitterInfo*
>(trk->getPoint(ip)->
getFitterInfo(rep)))) {
187 prevFitterInfo = currFitterInfo;
192 gblfs->setIsFitted(
true);
193 gblfs->setIsFitConvergedPartially(fitRes == 0);
194 nFailed = trk->getNumPointsWithMeasurement() - nmeas;
195 gblfs->setNFailedPoints(nFailed);
196 gblfs->setIsFitConvergedFully(fitRes == 0 && nFailed == 0);
197 gblfs->setNumIterations(iIter + 1);
198 gblfs->setChi2(Chi2);
203 int npoints_meas = trk->getNumPointsWithMeasurement();
204 int npoints_all = trk->getNumPoints();
206 cout <<
"-------------------------------------------------------" << endl;
207 cout <<
" GBL processed genfit::Track " << endl;
208 cout <<
"-------------------------------------------------------" << endl;
209 cout <<
" # Track Points : " << npoints_all << endl;
210 cout <<
" # Meas. Points : " << npoints_meas << endl;
213 cout <<
" (" << ndummy <<
" dummy) ";
215 cout <<
" # GBL points meas : " << nmeas << endl;
216 cout <<
" # GBL points scat : " << nscat << endl;
217 cout <<
"-------------- GBL Fit Results ----------- Iteration " << iIter+1 <<
" " << ((iIter < gblIterations.size()) ? gblIterations[iIter] :
"") << endl;
218 cout <<
" Fit q/p parameter : " << (gblfs->hasCurvature() ? (
"True") : (
"False")) << endl;
219 cout <<
" Valid trajectory : " << ((traj.
isValid()) ? (
"True") : (
"False")) << endl;
220 cout <<
" Fit result : " << fitRes <<
" (0 for success)" << endl;
221 cout <<
" GBL track NDF : " << Ndf <<
" (-1 for failure)" << endl;
222 cout <<
" GBL track Chi2 : " << Chi2 << endl;
223 cout <<
" GBL track P-value : " << TMath::Prob(Chi2, Ndf) << endl;
224 cout <<
"-------------------------------------------------------" << endl;
234 for (
int ip = trk->getNumPoints() - 1; ip >=0; ip--) {
235 trk->getPoint(ip)->setScatterer(
nullptr);
236 trk->getPoint(ip)->deleteFitterInfo(rep);
238 if (!trk->getPoint(ip)->hasRawMeasurements())
239 trk->deletePoint(ip);
245 int npoints_meas = trk->getNumPointsWithMeasurement();
248 rep->
setTime(reference, trk->getTimeSeed());
249 rep->
setPosMom(reference, trk->getStateSeed());
252 reference.extrapolateToPlane(firstPlane);
254 double arcLenPos = 0;
257 for (
int ipoint_meas = 0; ipoint_meas < npoints_meas - 1; ipoint_meas++) {
259 TrackPoint* point_meas = trk->getPointWithMeasurement(ipoint_meas);
265 point_meas->setSortingParameter(arcLenPos);
266 arcLenPos += reference.extrapolateToPlane(nextPlane);
269 trk->getPointWithMeasurement(npoints_meas - 1)->setSortingParameter(arcLenPos);
275 std::vector<gbl::GblPoint> thePoints;
279 for (
unsigned int ip = 0; ip < trk->getNumPoints(); ip++) {
295 for (
unsigned int ip = 0; ip < trk->getNumPoints(); ip++) {
314 void GblFitter::getScattererFromMatList(
double& length,
315 double& theta,
double& s,
double& ds,
316 const double p,
const double mass,
const double charge,
317 const std::vector<genfit::MatStep>& steps)
const {
318 theta = 0.; s = 0.; ds = 0.; length = 0;
319 if (steps.empty())
return;
333 for (
unsigned int i = 0; i < steps.size(); i++) {
334 const MatStep step = steps.at(i);
336 double rho = 1. / step.material_.radiationLength;
337 len += fabs(step.stepSize_);
339 xmax = xmin + fabs(step.stepSize_);
343 sumxx += rho * (xmax - xmin);
345 sumx2x2 += rho * (xmax * xmax - xmin * xmin) / 2.;
347 sumx3x3 += rho * (xmax * xmax * xmax - xmin * xmin * xmin) / 3.;
350 if (sumxx < 1.0e-10)
return;
354 double beta = p / sqrt(p * p + mass * mass);
355 theta = (0.0136 / p / beta) * fabs(charge) * sqrt(sumxx) * (1. + 0.038 * log(sumxx));
361 double N = 1. / sumxx;
366 double ds_2 = N * (sumx3x3 - 2. * sumx2x2 * s + sumxx * s * s);
377 int npoints_meas = trk->getNumPointsWithMeasurement();
381 TMatrixD jacPointToPoint(dim, dim);
382 jacPointToPoint.UnitMatrix();
387 rep->
setTime(reference, trk->getTimeSeed());
388 rep->
setPosMom(reference, trk->getStateSeed());
391 reference.extrapolateToPlane(firstPlane);
393 double sumTrackLen = 0;
395 TMatrixDSym noise; TVectorD deltaState;
398 for (
int ipoint_meas = 0; ipoint_meas < npoints_meas; ipoint_meas++) {
400 TrackPoint* point_meas = trk->getPointWithMeasurement(ipoint_meas);
404 TVector3 trackDir = rep->
getDir(reference);
406 double trackMomMag = rep->
getMomMag(reference);
408 double particleCharge = rep->
getCharge(reference);
410 double particleMass = rep->
getMass(reference);
412 double trackLen = 0., scatTheta = 0., scatSMean = 0., scatDeltaS = 0.;
414 double theta1 = 0., theta2 = 0., s1 = 0., s2 = 0.;
416 TMatrixD jacMeas2Scat(dim, dim);
417 jacMeas2Scat.UnitMatrix();
421 if (ipoint_meas >= npoints_meas - 1) {
441 TVector3 segmentEntry = refCopy.getPos();
443 TVector3 segmentExit = refCopy.getPos();
445 getScattererFromMatList(trackLen,
455 s1 = 0.; s2 = scatSMean + scatDeltaS * scatDeltaS / (scatSMean - s1);
456 theta1 = sqrt(scatTheta * scatTheta * scatDeltaS * scatDeltaS / (scatDeltaS * scatDeltaS + (scatSMean - s1) * (scatSMean - s1)));
457 theta2 = sqrt(scatTheta * scatTheta * (scatSMean - s1) * (scatSMean - s1) / (scatDeltaS * scatDeltaS + (scatSMean - s1) * (scatSMean - s1)));
460 if (m_segmentController)
461 m_segmentController->controlTrackSegment(segmentEntry, segmentExit, scatTheta,
this);
464 if (m_enableScatterers && !m_enableIntermediateScatterer) {
467 }
else if (!m_enableScatterers) {
475 if (theta1 > scatEpsilon)
485 if (theta2 > scatEpsilon) {
495 scattp->setSortingParameter(point_meas->getSortingParameter() + s2);
500 for (
unsigned int itp = 0; itp < trk->getNumPoints(); itp++) {
501 if (trk->getPoint(itp) == point_meas) {
518 cout <<
" ERROR: The extrapolation to measurement point " << (ipoint_meas + 2) <<
" stepped back by " << nextStep <<
"cm !!! Track will be cut before this point." << endl;
529 cout <<
" ERROR: The extrapolation to measurement point " << (ipoint_meas + 2) <<
" stepped back by " << nextStep <<
"cm !!! Track will be cut before this point." << endl;
535 sumTrackLen += trackLen;