37#include <Math/QuantFuncMathCore.h>
43DAF::DAF(
const std::tuple<double, double, int>& annealingScheme,
int minIter,
int maxIter,
int minIterForPval,
bool useRefKalman,
double deltaPval,
double deltaWeight,
double probCut)
57 std::get<1>(annealingScheme),
58 std::get<2>(annealingScheme),
63DAF::DAF(
bool useRefKalman,
double deltaPval,
double deltaWeight)
88 static_cast<KalmanFitterRefTrack*>(kalman_.get())->setRefitAll();
99 debugOut<<
"DAF::processTrack //////////////////////////////////////////////////////////////// \n";
103 bool oneLastIter =
false;
105 double lastPval = -1;
107 for(
unsigned int iBeta = 0; ; ++iBeta) {
110 debugOut<<
"DAF::processTrack, trackRep " << rep <<
", iteration " << iBeta+1 <<
", beta = " <<
betas_.at(iBeta) <<
"\n";
113 kalman_->processTrackWithRep(tr, rep, resortHits);
124 debugOut <<
"DAF::Kalman could not fit!\n";
130 if( oneLastIter ==
true){
132 debugOut <<
"DAF::break after one last iteration\n";
143 debugOut <<
"DAF::number of max iterations reached!\n";
150 bool converged(
false);
153 if (!converged && iBeta >=
static_cast<unsigned int>(
minIterForPval_ - 1) &&
156 debugOut <<
"converged by Pval = " << status->
getBackwardPVal() <<
" even though weights changed at iBeta = " << iBeta << std::endl;
175 debugOut <<
"DAF::convergence reached in iteration " << iBeta+1 <<
" -> Do one last iteration with updated weights.\n";
195 for (
int i = 1; i < 7; ++i){
201 if ( prob_cut > 1.0 || prob_cut < 0.0){
202 Exception exc(
"DAF::addProbCut prob_cut is not between 0 and 1",__LINE__,__FILE__);
206 if ( measDim < 1 || measDim > 6 ){
207 Exception exc(
"DAF::addProbCut measDim must be in the range [1,6]",__LINE__,__FILE__);
211 chi2Cuts_[measDim] = ROOT::Math::chisquared_quantile_c( prob_cut, measDim);
217 assert(bStart > bFinal);
218 assert(bFinal > 1.E-10);
227 for (
unsigned int i=0; i<nSteps; ++i) {
228 betas_.push_back(bStart * pow(bFinal / bStart, i / (nSteps - 1.)));
238 assert(bStart > bFinal);
239 assert(bFinal > 1.E-10);
247 for (
unsigned int i=0; i<nSteps; ++i) {
248 betas_.push_back(bStart * pow(bFinal / bStart, i / (nSteps - 1.)));
262 bool converged(
true);
263 double maxAbsChange(0);
266 for (std::vector< TrackPoint* >::const_iterator tp = trackPoints.begin(); tp != trackPoints.end(); ++tp) {
267 if (! (*tp)->hasFitterInfo(rep)) {
272 Exception exc(
"DAF::getWeights ==> can only use KalmanFitterInfos",__LINE__,__FILE__);
279 debugOut<<
"weights are fixed, continue \n";
286 std::vector<double> phi(nMeas, 0.);
289 for(
unsigned int j=0; j<nMeas; j++) {
293 const TVectorD& resid(residual.
getState());
294 TMatrixDSym Vinv(residual.
getCov());
297 int hitDim = resid.GetNrows();
301 double twoPiN = 2.*M_PI;
305 twoPiN = pow(twoPiN, hitDim);
307 double chi2 = Vinv.Similarity(resid);
309 debugOut<<
"chi2 = " << chi2 <<
"\n";
313 double norm = 1./sqrt(twoPiN * detV);
315 phi[j] = norm*exp(-0.5*chi2/beta);
319 assert(cutVal>1.E-6);
321 phi_cut += norm*exp(-0.5*cutVal/beta);
329 for(
unsigned int j=0; j<nMeas; j++) {
330 double weight = phi[j]/(phi_sum+phi_cut);
337 if (absChange > maxAbsChange)
338 maxAbsChange = absChange;
344 debugOut<<
"\t new weight: " << weight;
354 debugOut <<
"max abs weight change = " << maxAbsChange <<
"\n";
362void DAF::Streamer(TBuffer &R__b)
369 if (R__b.IsReading()) {
370 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
if (R__v) { }
373 baseClass0::Streamer(R__b);
377 std::vector<double> &R__stl =
betas_;
381 R__stl.reserve(R__n);
382 for (R__i = 0; R__i < R__n; R__i++) {
385 R__stl.push_back(R__t);
393 for (R__i = 0; R__i < R__n; R__i++) {
399 if (R__t >= 1 && R__t <= 6)
405 assert(n_chi2Cuts == 6);
407 R__b.ReadFastArray(&
chi2Cuts_[1], n_chi2Cuts);
412 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
414 R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
416 typedef genfit::AbsKalmanFitter baseClass0;
417 baseClass0::Streamer(R__b);
420 std::vector<double> &R__stl =
betas_;
421 int R__n=int(R__stl.size());
424 std::vector<double>::iterator R__k;
425 for (R__k = R__stl.begin(); R__k != R__stl.end(); ++R__k) {
435 R__b.SetByteCount(R__c, kTRUE);
This class collects all information needed and produced by a specific AbsFitter and is specific to on...
Abstract base class for Kalman fitter and derived fitting algorithms.
unsigned int maxIterations_
Maximum number of iterations to attempt. Forward and backward are counted as one iteration.
unsigned int minIterations_
Minimum number of iterations to attempt. Forward and backward are counted as one iteration.
AbsKalmanFitter(unsigned int maxIterations=4, double deltaPval=1e-3, double blowUpFactor=1e3)
Abstract base class for a track representation.
Determinstic Annealing Filter (DAF) implementation.
std::vector< double > betas_
bool calcWeights(Track *trk, const AbsTrackRep *rep, double beta)
Calculate and set the weights for the next fitting pass. Return if convergence is met....
void setAnnealingScheme(double bStart, double bFinal, unsigned int nSteps)
Configure the annealing scheme.
void processTrackWithRep(Track *tr, const AbsTrackRep *rep, bool resortHits=false) override
Process a track using the DAF.
void addProbCut(const double prob_cut, const int measDim)
Set the probability cut for the weight calculation for the hits for a specific measurement dimensiona...
std::unique_ptr< AbsKalmanFitter > kalman_
void setProbCut(const double prob_cut)
Set the probability cut for the weight calculation for the hits.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
void setFatal(bool b=true)
Set fatal flag.
virtual const char * what() const noexcept
Standard error message handling for exceptions. use like "std::cerr << e.what();".
void info()
Print information in the exception object.
int getNFailedPoints() const
void setIsFitConvergedPartially(bool fitConverged=true)
bool isFitted() const
Has the track been fitted?
void setIsFitted(bool fitted=true)
void setIsFitConvergedFully(bool fitConverged=true)
FitStatus for use with AbsKalmanFitter implementations.
double getForwardPVal() const
void setNumIterations(unsigned int numIterations)
double getBackwardPVal() const
void setIsFittedWithDaf(bool fittedWithDaf=true)
Simple Kalman filter implementation.
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
unsigned int getNumMeasurements() const
MeasurementOnPlane getResidual(unsigned int iMeasurement=0, bool biased=false, bool onlyMeasurementErrors=true) const override
Get unbiased (default) or biased residual from ith measurement.
bool areWeightsFixed() const
Are the weights fixed?
MeasurementOnPlane * getMeasurementOnPlane(int i=0) const
Kalman filter implementation with linearization around a reference track.
void setRefitAll(bool refit=true)
If true always refit all points, otherwise fit points only if reference states have changed.
const TMatrixDSym & getCov() const
Measured coordinates on a plane.
void setWeight(double weight)
const TVectorD & getState() const
Collection of TrackPoint objects, AbsTrackRep objects and FitStatus objects.
FitStatus * getFitStatus(const AbsTrackRep *rep=nullptr) const
Get FitStatus for a AbsTrackRep. Per default, return FitStatus for cardinalRep.
const std::vector< genfit::TrackPoint * > & getPointsWithMeasurement() const
Defines for I/O streams used for error and debug printing.