Belle II Software  release-05-02-19
TrackFitter.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFitting/fitter/base/TrackFitter.h>
11 
12 #include <tracking/dataobjects/RecoTrack.h>
13 
14 #include <genfit/AbsTrackRep.h>
15 #include <genfit/FitStatus.h>
16 #include <genfit/AbsFitter.h>
17 #include <genfit/DAF.h>
18 #include <genfit/KalmanFitterInfo.h>
19 
20 using namespace Belle2;
21 
22 constexpr double TrackFitter::s_defaultDeltaPValue;
23 constexpr double TrackFitter::s_defaultProbCut;
24 constexpr unsigned int TrackFitter::s_defaultMaxFailedHits;
25 
27 {
28  int currentPdgCode = particleType.getPDGCode();
29 
30  const auto& pdgParticleCharge = particleType.getParticlePDG()->Charge();
31  const auto& recoTrackCharge = recoTrack.getChargeSeed();
32 
33  // Copy from GenfitterModule
34  B2ASSERT("Charge of candidate and PDG particle don't match. (Code assumes |q| = 1).",
35  fabs(pdgParticleCharge) == fabs(recoTrackCharge * 3.0));
36 
37  /*
38  * Because the charged stable particles do describe a positive as well as a negative particle,
39  * we have to correct the charge if needed.
40  */
41  if (std::signbit(pdgParticleCharge) != std::signbit(recoTrackCharge))
42  currentPdgCode *= -1;
43 
44  return currentPdgCode;
45 }
46 
47 bool TrackFitter::fit(RecoTrack& recoTrack) const
48 {
49  if (not recoTrack.getRepresentations().empty() and recoTrack.getCardinalRepresentation()) {
50  return fit(recoTrack, recoTrack.getCardinalRepresentation());
51  } else {
52  return fit(recoTrack, Const::pion);
53  }
54 }
55 
56 bool TrackFitter::fit(RecoTrack& recoTrack, const Const::ChargedStable& particleType) const
57 {
58  const int currentPdgCode = TrackFitter::createCorrectPDGCodeForChargedStable(particleType, recoTrack);
60  currentPdgCode);
61 
62  return fit(recoTrack, trackRepresentation);
63 }
64 
65 bool TrackFitter::fit(RecoTrack& recoTrack, const int pdgCode) const
66 {
68  pdgCode);
69 
70  return fit(recoTrack, trackRepresentation);
71 }
72 
73 bool TrackFitter::fitWithoutCheck(RecoTrack& recoTrack, const genfit::AbsTrackRep& trackRepresentation) const
74 {
75  // Fit the track
76  try {
77  // Delete the old information to start from scratch
78  recoTrack.deleteFittedInformationForRepresentation(&trackRepresentation);
79  m_fitter->processTrackWithRep(&RecoTrackGenfitAccess::getGenfitTrack(recoTrack), &trackRepresentation);
80  } catch (genfit::Exception& e) {
81  B2WARNING(e.getExcString());
82  }
83 
84  recoTrack.setDirtyFlag(false);
85 
86  // Do the hits synchronisation
87  const std::vector<RecoHitInformation*>& relatedRecoHitInformation = recoTrack.getRecoHitInformations();
88 
89  for (RecoHitInformation* recoHitInformation : relatedRecoHitInformation) {
90  const genfit::TrackPoint* trackPoint = recoTrack.getCreatedTrackPoint(recoHitInformation);
91  if (trackPoint) {
92  genfit::KalmanFitterInfo* kalmanFitterInfo = trackPoint->getKalmanFitterInfo(&trackRepresentation);
93  if (not kalmanFitterInfo) {
94  recoHitInformation->setFlag(RecoHitInformation::RecoHitFlag::c_dismissedByFit);
95  } else {
96  std::vector<double> weights = kalmanFitterInfo->getWeights();
97  for (const double weight : weights) {
98  if (weight == 0) {
99  recoHitInformation->setFlag(RecoHitInformation::RecoHitFlag::c_dismissedByFit);
100  }
101  }
102  }
103  }
104  }
105 
106  return recoTrack.wasFitSuccessful(&trackRepresentation);
107 }
108 
109 bool TrackFitter::fit(RecoTrack& recoTrack, genfit::AbsTrackRep* trackRepresentation) const
110 {
111  B2ASSERT("No fitter was loaded! Have you reset the fitter to an invalid one?", m_fitter);
112 
113  const bool measurementAdderNeedsTrackRefit = m_measurementAdder.addMeasurements(recoTrack);
114 
115  if (RecoTrackGenfitAccess::getGenfitTrack(recoTrack).getNumPoints() == 0) {
116  B2WARNING("No track points (measurements) were added to this reco track. Have you used an invalid measurement adder?");
117  return false;
118  }
119 
120  const std::vector<genfit::AbsTrackRep*>& trackRepresentations = recoTrack.getRepresentations();
121  if (std::find(trackRepresentations.begin(), trackRepresentations.end(), trackRepresentation) == trackRepresentations.end()) {
122  B2FATAL("The TrackRepresentation provided is not part of the Reco Track.");
123  }
124 
125  if (not recoTrack.getDirtyFlag() and not m_skipDirtyCheck and not measurementAdderNeedsTrackRefit
126  and recoTrack.hasTrackFitStatus(trackRepresentation) and recoTrack.getTrackFitStatus(trackRepresentation)->isFitted()) {
127  B2DEBUG(100, "Hit content did not change, track representation is already present and you used only default parameters." <<
128  "I will not fit the track again. If you still want to do so, set the dirty flag of the track.");
129  return recoTrack.wasFitSuccessful(trackRepresentation);
130  }
131 
132  const auto previousSetting = gErrorIgnoreLevel; // Save current log level
133  gErrorIgnoreLevel = m_gErrorIgnoreLevel; // Set the log level defined in the TrackFitter
134  auto fitWithoutCheckResult = fitWithoutCheck(recoTrack, *trackRepresentation);
135  gErrorIgnoreLevel = previousSetting; // Restore previous setting
136  return fitWithoutCheckResult;
137 }
138 
140 {
141  genfit::DAF* dafFitter = new genfit::DAF(true, s_defaultDeltaPValue);
142  dafFitter->setProbCut(s_defaultProbCut);
143  dafFitter->setMaxFailedHits(s_defaultMaxFailedHits);
144 
145  m_fitter.reset(dafFitter);
146 
147  m_skipDirtyCheck = false;
148 }
149 
150 void TrackFitter::resetFitter(const std::shared_ptr<genfit::AbsFitter>& fitter)
151 {
152  m_fitter = fitter;
153  m_skipDirtyCheck = true;
154 }
genfit::Exception
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
genfit::DAF
Determinstic Annealing Filter (DAF) implementation.
Definition: DAF.h:49
genfit::TrackPoint
Object containing AbsMeasurement and AbsFitterInfo objects.
Definition: TrackPoint.h:46
Belle2::RecoTrack::getTrackFitStatus
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:537
Belle2::TrackFitter::m_fitter
std::shared_ptr< genfit::AbsFitter > m_fitter
The internal storage of the used fitting algorithms.
Definition: TrackFitter.h:280
Belle2::TrackFitter::resetFitterToDefaultSettings
void resetFitterToDefaultSettings()
Use the default settings of the fitter to fit the reco tracks.
Definition: TrackFitter.cc:139
Belle2::RecoTrack::wasFitSuccessful
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
Definition: RecoTrack.cc:326
Belle2::MeasurementAdder::addMeasurements
bool addMeasurements(RecoTrack &recoTrack) const
After you have filled the internal storage with measurement creators (either by providing your own or...
Definition: MeasurementAdder.cc:162
Belle2::RecoTrack::getCardinalRepresentation
genfit::AbsTrackRep * getCardinalRepresentation() const
Get a pointer to the cardinal track representation. You are not allowed to modify or delete it!
Definition: RecoTrack.h:547
Belle2::RecoTrack::hasTrackFitStatus
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:517
Belle2::TrackFitter::fit
bool fit(RecoTrack &recoTrack, genfit::AbsTrackRep *trackRepresentation) const
Fit a reco track with a given non-default track representation.
Definition: TrackFitter.cc:109
Belle2::RecoTrackGenfitAccess::getGenfitTrack
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition: RecoTrack.cc:389
Belle2::TrackFitter::m_measurementAdder
MeasurementAdder m_measurementAdder
The measurement adder algorithm class.
Definition: TrackFitter.h:285
Belle2::RecoTrack::setDirtyFlag
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:621
genfit::TrackPoint::getKalmanFitterInfo
KalmanFitterInfo * getKalmanFitterInfo(const AbsTrackRep *rep=nullptr) const
Helper to avoid casting.
Definition: TrackPoint.cc:180
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
genfit::AbsTrackRep
Abstract base class for a track representation.
Definition: AbsTrackRep.h:66
Belle2::TrackFitter::createCorrectPDGCodeForChargedStable
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:26
Belle2::RecoTrack::getRepresentations
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:554
Belle2::TrackFitter::s_defaultMaxFailedHits
static constexpr unsigned int s_defaultMaxFailedHits
Default maxFailedHits for the default DAF fitter.
Definition: TrackFitter.h:123
Belle2::RecoTrack::getCreatedTrackPoint
const genfit::TrackPoint * getCreatedTrackPoint(const RecoHitInformation *recoHitInformation) const
Get a pointer to the TrackPoint that was created from this hit.
Definition: RecoTrack.cc:220
genfit::DAF::setProbCut
void setProbCut(const double prob_cut)
Set the probability cut for the weight calculation for the hits.
Definition: DAF.cc:174
Belle2::RecoTrack
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:78
Belle2::Const::pion
static const ChargedStable pion
charged pion particle
Definition: Const.h:535
Belle2::TrackFitter::fitWithoutCheck
bool fitWithoutCheck(RecoTrack &recoTrack, const genfit::AbsTrackRep &trackRepresentation) const
Helper function to do the fit.
Definition: TrackFitter.cc:73
genfit::KalmanFitterInfo
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
Definition: KalmanFitterInfo.h:44
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFitter::s_defaultProbCut
static constexpr double s_defaultProbCut
Default probCut for the default DAF fitter.
Definition: TrackFitter.h:121
Belle2::RecoHitInformation
This class stores additional information to every CDC/SVD/PXD hit stored in a RecoTrack.
Definition: RecoHitInformation.h:48
Belle2::RecoTrackGenfitAccess::createOrReturnRKTrackRep
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:394
Belle2::TrackFitter::m_skipDirtyCheck
bool m_skipDirtyCheck
Flag to skip the dirty flag check which is needed when using non-default fitters.
Definition: TrackFitter.h:282
Belle2::RecoTrack::getRecoHitInformations
std::vector< RecoHitInformation * > getRecoHitInformations(bool getSorted=false) const
Return a list of all RecoHitInformations associated with the RecoTrack.
Definition: RecoTrack.cc:531
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::RecoTrack::getDirtyFlag
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:614
Belle2::RecoTrack::getChargeSeed
short int getChargeSeed() const
Return the charge seed stored in the reco track. ATTENTION: This is not the fitted charge.
Definition: RecoTrack.h:497
Belle2::RecoTrack::deleteFittedInformationForRepresentation
void deleteFittedInformationForRepresentation(const genfit::AbsTrackRep *rep)
Delete all fitted information for the given representations.
Definition: RecoTrack.cc:448
genfit::FitStatus::isFitted
bool isFitted() const
Has the track been fitted?
Definition: FitStatus.h:94
Belle2::TrackFitter::resetFitter
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:150
Belle2::TrackFitter::m_gErrorIgnoreLevel
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:288
Belle2::Const::ParticleType::getParticlePDG
const TParticlePDG * getParticlePDG() const
Accessor for ROOT TParticlePDG object.
Definition: UnitConst.cc:305
Belle2::TrackFitter::s_defaultDeltaPValue
static constexpr double s_defaultDeltaPValue
Default deltaPValue for the default DAF fitter.
Definition: TrackFitter.h:119