Belle II Software development
TrackFitter.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#include <tracking/trackFitting/fitter/base/TrackFitter.h>
9
10#include <tracking/dataobjects/RecoTrack.h>
11
12#include <genfit/AbsTrackRep.h>
13#include <genfit/FitStatus.h>
14#include <genfit/AbsFitter.h>
15#include <genfit/DAF.h>
16#include <genfit/KalmanFitterInfo.h>
17
18using namespace Belle2;
19
21constexpr double TrackFitter::s_defaultProbCut;
22constexpr unsigned int TrackFitter::s_defaultMaxFailedHits;
23
25{
26 int currentPdgCode = particleType.getPDGCode();
27
28 const auto& pdgParticleCharge = particleType.getParticlePDG()->Charge();
29 const auto& recoTrackCharge = recoTrack.getChargeSeed();
30
31 // Copy from GenfitterModule
32 B2ASSERT("Charge of candidate and PDG particle don't match. (Code assumes |q| = 1).",
33 fabs(pdgParticleCharge) == fabs(recoTrackCharge * 3.0));
34
35 /*
36 * Because the charged stable particles do describe a positive as well as a negative particle,
37 * we have to correct the charge if needed.
38 */
39 if (std::signbit(pdgParticleCharge) != std::signbit(recoTrackCharge))
40 currentPdgCode *= -1;
41
42 return currentPdgCode;
43}
44
45bool TrackFitter::fit(RecoTrack& recoTrack, bool resortHits) const
46{
47 if (not recoTrack.getRepresentations().empty() and recoTrack.getCardinalRepresentation()) {
48 return fit(recoTrack, recoTrack.getCardinalRepresentation(), resortHits);
49 } else {
50 return fit(recoTrack, Const::pion, resortHits);
51 }
52}
53
54bool TrackFitter::fit(RecoTrack& recoTrack, const Const::ChargedStable& particleType, bool resortHits) const
55{
56 const int currentPdgCode = TrackFitter::createCorrectPDGCodeForChargedStable(particleType, recoTrack);
57 genfit::AbsTrackRep* trackRepresentation = RecoTrackGenfitAccess::createOrReturnRKTrackRep(recoTrack,
58 currentPdgCode);
59
60 return fit(recoTrack, trackRepresentation, resortHits);
61}
62
63bool TrackFitter::fit(RecoTrack& recoTrack, const int pdgCode, bool resortHits) const
64{
65 genfit::AbsTrackRep* trackRepresentation = RecoTrackGenfitAccess::createOrReturnRKTrackRep(recoTrack,
66 pdgCode);
67
68 return fit(recoTrack, trackRepresentation, resortHits);
69}
70
71bool TrackFitter::fitWithoutCheck(RecoTrack& recoTrack, const genfit::AbsTrackRep& trackRepresentation, bool resortHits) const
72{
73 // Fit the track
74 try {
75 // Delete the old information to start from scratch
76 recoTrack.deleteFittedInformationForRepresentation(&trackRepresentation);
77 B2DEBUG(28, "resortHits is set to " << resortHits << " when fitting the tracks");
78 m_fitter->processTrackWithRep(&RecoTrackGenfitAccess::getGenfitTrack(recoTrack), &trackRepresentation, resortHits);
79 } catch (genfit::Exception& e) {
80 B2WARNING(e.getExcString());
81 }
82
83 recoTrack.setDirtyFlag(false);
84
85 // Do the hits synchronisation
86 const std::vector<RecoHitInformation*>& relatedRecoHitInformation = recoTrack.getRecoHitInformations();
87
88 for (RecoHitInformation* recoHitInformation : relatedRecoHitInformation) {
89 const genfit::TrackPoint* trackPoint = recoTrack.getCreatedTrackPoint(recoHitInformation);
90 if (trackPoint) {
91 genfit::KalmanFitterInfo* kalmanFitterInfo = trackPoint->getKalmanFitterInfo(&trackRepresentation);
92 if (not kalmanFitterInfo) {
93 recoHitInformation->setFlag(RecoHitInformation::RecoHitFlag::c_dismissedByFit);
94 } else {
95 std::vector<double> weights = kalmanFitterInfo->getWeights();
96 for (const double weight : weights) {
97 if (weight < 1.e-9) {
98 recoHitInformation->setFlag(RecoHitInformation::RecoHitFlag::c_dismissedByFit);
99 }
100 }
101 }
102 }
103 }
104
105 return recoTrack.wasFitSuccessful(&trackRepresentation);
106}
107
108bool TrackFitter::fit(RecoTrack& recoTrack, genfit::AbsTrackRep* trackRepresentation, bool resortHits) const
109{
110 B2ASSERT("No fitter was loaded! Have you reset the fitter to an invalid one?", m_fitter);
111
112 const bool measurementAdderNeedsTrackRefit = m_measurementAdder.addMeasurements(recoTrack);
113
114 if (RecoTrackGenfitAccess::getGenfitTrack(recoTrack).getNumPoints() == 0) {
115 B2WARNING("No track points (measurements) were added to this reco track. Have you used an invalid measurement adder?");
116 return false;
117 }
118
119 const std::vector<genfit::AbsTrackRep*>& trackRepresentations = recoTrack.getRepresentations();
120 if (std::find(trackRepresentations.begin(), trackRepresentations.end(), trackRepresentation) == trackRepresentations.end()) {
121 B2FATAL("The TrackRepresentation provided is not part of the Reco Track.");
122 }
123
124 if (not recoTrack.getDirtyFlag() and not m_skipDirtyCheck and not measurementAdderNeedsTrackRefit
125 and recoTrack.hasTrackFitStatus(trackRepresentation) and recoTrack.getTrackFitStatus(trackRepresentation)->isFitted()) {
126 B2DEBUG(100, "Hit content did not change, track representation is already present and you used only default parameters." <<
127 "I will not fit the track again. If you still want to do so, set the dirty flag of the track.");
128 return recoTrack.wasFitSuccessful(trackRepresentation);
129 }
130
131 const auto previousSetting = gErrorIgnoreLevel; // Save current log level
132 gErrorIgnoreLevel = m_gErrorIgnoreLevel; // Set the log level defined in the TrackFitter
133 auto fitWithoutCheckResult = fitWithoutCheck(recoTrack, *trackRepresentation, resortHits);
134 gErrorIgnoreLevel = previousSetting; // Restore previous setting
135 return fitWithoutCheckResult;
136}
137
139{
140 if (!m_DAFparameters.isValid())
141 B2FATAL("DAF parameters are not available.");
142 genfit::DAF* dafFitter = new genfit::DAF(m_DAFparameters->getAnnealingScheme(),
143 m_DAFparameters->getMinimumIterations(),
144 m_DAFparameters->getMaximumIterations(),
145 m_DAFparameters->getMinimumIterationsForPVal(),
146 true,
147 m_DAFparameters->getDeltaPValue(),
148 m_DAFparameters->getDeltaWeight(),
149 m_DAFparameters->getProbabilityCut());
150 dafFitter->setMaxFailedHits(m_DAFparameters->getMaximumFailedHits());
151 m_fitter.reset(dafFitter);
152 m_skipDirtyCheck = false;
153}
154
156{
157 if (DAFparams == nullptr)
158 B2FATAL("DAF parameters are not available.");
159 genfit::DAF* dafFitter = new genfit::DAF(DAFparams->getAnnealingScheme(),
160 DAFparams->getMinimumIterations(),
161 DAFparams->getMaximumIterations(),
162 DAFparams->getMinimumIterationsForPVal(),
163 true,
164 DAFparams->getDeltaPValue(),
165 DAFparams->getDeltaWeight(),
166 DAFparams->getProbabilityCut());
167 dafFitter->setMaxFailedHits(DAFparams->getMaximumFailedHits());
168 m_fitter.reset(dafFitter);
169 m_skipDirtyCheck = false;
170}
171
173{
174 // The cosmics parameters are the ones from the DAFparameters constructor
175 DAFparameters* DAFparams = new DAFparameters();
176
177 resetFitterToUserSettings(DAFparams);
178}
179
180void TrackFitter::resetFitter(const std::shared_ptr<genfit::AbsFitter>& fitter)
181{
182 m_fitter = fitter;
183 m_skipDirtyCheck = true;
184}
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:589
int getPDGCode() const
PDG code.
Definition: Const.h:473
const TParticlePDG * getParticlePDG() const
Accessor for ROOT TParticlePDG object.
Definition: UnitConst.cc:351
static const ChargedStable pion
charged pion particle
Definition: Const.h:661
The payload containing the DAF parameters.
Definition: DAFparameters.h:21
int getMaximumIterations() const
Get the maximum number of iterations of annealing scheme.
std::tuple< float, float, int > getAnnealingScheme() const
Get the start and end temperatures and number of iterations for the annealing scheme returns a tuple ...
int getMaximumFailedHits() const
Get the maximum number of failed hits after which the fit should be cancelled.
int getMinimumIterationsForPVal() const
Get the minimum number of iterations for pValue check.
float getDeltaPValue() const
Get the DeltaPValue for p-value convergence criterion.
Definition: DAFparameters.h:98
int getMinimumIterations() const
Get the minimum number of iterations of annealing scheme.
float getProbabilityCut() const
Get the probability cut for the weight calculation for the hits.
float getDeltaWeight() const
Get the DeltaWeight for weight convergence criterion.
bool addMeasurements(RecoTrack &recoTrack) const
After you have filled the internal storage with measurement creators (either by providing your own or...
This class stores additional information to every CDC/SVD/PXD hit stored in a RecoTrack.
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition: RecoTrack.cc:404
static genfit::AbsTrackRep * createOrReturnRKTrackRep(RecoTrack &recoTrack, int PDGcode)
Checks if a TrackRap for the PDG id of the RecoTrack (and its charge conjugate) does already exit and...
Definition: RecoTrack.cc:409
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:79
const std::vector< genfit::AbsTrackRep * > & getRepresentations() const
Return a list of track representations. You are not allowed to modify or delete them!
Definition: RecoTrack.h:638
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
Definition: RecoTrack.cc:336
genfit::AbsTrackRep * getCardinalRepresentation() const
Get a pointer to the cardinal track representation. You are not allowed to modify or delete it!
Definition: RecoTrack.h:631
void setDirtyFlag(const bool &dirtyFlag=true)
Set to true, if you want to rebuild the measurements and do the fit independent on changes of the hit...
Definition: RecoTrack.h:722
bool hasTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Check, if there is a fit status for the given representation or for the cardinal one.
Definition: RecoTrack.cc:543
const genfit::TrackPoint * getCreatedTrackPoint(const RecoHitInformation *recoHitInformation) const
Get a pointer to the TrackPoint that was created from this hit.
Definition: RecoTrack.cc:230
std::vector< RecoHitInformation * > getRecoHitInformations(bool getSorted=false) const
Return a list of all RecoHitInformations associated with the RecoTrack.
Definition: RecoTrack.cc:557
bool getDirtyFlag() const
This returns true, if a hit was added after the last fit and measurement creation and a refit should ...
Definition: RecoTrack.h:715
void deleteFittedInformationForRepresentation(const genfit::AbsTrackRep *rep)
Delete all fitted information for the given representations.
Definition: RecoTrack.cc:470
short int getChargeSeed() const
Return the charge seed stored in the reco track. ATTENTION: This is not the fitted charge.
Definition: RecoTrack.h:508
const genfit::FitStatus * getTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Return the track fit status for the given representation or for the cardinal one. You are not allowed...
Definition: RecoTrack.h:621
DBObjPtr< DAFparameters > m_DAFparameters
DAF parameters Database OjbPtr.
Definition: TrackFitter.h:328
static int createCorrectPDGCodeForChargedStable(const Const::ChargedStable &particleType, const RecoTrack &recoTrack)
Helper function to multiply the PDG code of a charged stable with the charge of the reco track (if ne...
Definition: TrackFitter.cc:24
static constexpr double s_defaultProbCut
Default probCut for the default DAF fitter.
Definition: TrackFitter.h:126
void resetFitterToUserSettings(DAFparameters *DAFparams)
Use the user settings of the fitter to fit the reco tracks.
Definition: TrackFitter.cc:155
bool fitWithoutCheck(RecoTrack &recoTrack, const genfit::AbsTrackRep &trackRepresentation, bool resortHits=false) const
Helper function to do the fit.
Definition: TrackFitter.cc:71
static constexpr double s_defaultDeltaPValue
Default deltaPValue for the default DAF fitter.
Definition: TrackFitter.h:124
void resetFitterToDBSettings()
Use the DB settings of the fitter to fit the reco tracks.
Definition: TrackFitter.cc:138
bool fit(RecoTrack &recoTrack, genfit::AbsTrackRep *trackRepresentation, bool resortHits=false) const
Fit a reco track with a given non-default track representation.
Definition: TrackFitter.cc:108
void resetFitter(const std::shared_ptr< genfit::AbsFitter > &fitter)
Set the internal storage of the fitter to a provided one, if you want to use non-default settings.
Definition: TrackFitter.cc:180
MeasurementAdder m_measurementAdder
The measurement adder algorithm class.
Definition: TrackFitter.h:322
Int_t m_gErrorIgnoreLevel
Control the output level of the ROOT functions used by the GenFit fitter. Default is increased from k...
Definition: TrackFitter.h:325
static constexpr unsigned int s_defaultMaxFailedHits
Default maxFailedHits for the default DAF fitter.
Definition: TrackFitter.h:128
std::shared_ptr< genfit::AbsFitter > m_fitter
The internal storage of the used fitting algorithms.
Definition: TrackFitter.h:316
bool m_skipDirtyCheck
Flag to skip the dirty flag check which is needed when using non-default fitters.
Definition: TrackFitter.h:319
void resetFitterToCosmicsSettings()
Use the settings of the fitter to fit the reco tracks for cosmics data.
Definition: TrackFitter.cc:172
Abstract base class for different kinds of events.