Belle II Software development
BaseRecoFitterModule.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/modules/fitter/BaseRecoFitterModule.h>
9
10#include <genfit/KalmanFitStatus.h>
11#include <genfit/FitStatus.h>
12#include <genfit/MaterialEffects.h>
13#include <genfit/FieldManager.h>
14
15#include <tracking/trackFitting/fitter/base/TrackFitter.h>
16#include <tracking/dbobjects/DAFConfiguration.h>
17
18#include <simulation/monopoles/MonopoleConstants.h>
19
20using namespace Belle2;
21
23 Module()
24{
25 setDescription("Fit the given reco tracks with the given fitter parameters.");
27
28 addParam("recoTracksStoreArrayName", m_param_recoTracksStoreArrayName, "StoreArray name of the input and output reco tracks.",
30
31 addParam("pxdHitsStoreArrayName", m_param_pxdHitsStoreArrayName, "StoreArray name of the input PXD hits.",
33 addParam("svdHitsStoreArrayName", m_param_svdHitsStoreArrayName, "StoreArray name of the input SVD hits.",
35 addParam("cdcHitsStoreArrayName", m_param_cdcHitsStoreArrayName, "StoreArray name of the input CDC hits.",
37 addParam("bklmHitsStoreArrayName", m_param_bklmHitsStoreArrayName, "StoreArray name of the input BKLM hits.",
39 addParam("eklmHitsStoreArrayName", m_param_eklmHitsStoreArrayName, "StoreArray name of the input EKLM hits.",
41
42 addParam("pdgCodesToUseForFitting", m_param_pdgCodesToUseForFitting,
43 "Use these particle hypotheses for fitting. Please use positive pdg codes only.",
45
46 addParam("resortHits", m_param_resortHits, "Resort the hits while fitting.",
48
49 addParam("initializeCDCTranslators", m_param_initializeCDCTranslators,
50 "Configures whether the CDC Translators should be initialized by the FitterModule",
52 addParam("monopoleMagCharge", Monopoles::monopoleMagCharge,
53 "Sets monopole magnetic charge hypothesis if it is in the pdgCodesToUseForFitting",
54 Monopoles::monopoleMagCharge);
55
56 addParam("correctSeedCharge", m_correctSeedCharge,
57 "If true changes seed charge of the RecoTrack to the one found by the track fit (if it differs).",
59}
60
62{
64
65 if (!genfit::MaterialEffects::getInstance()->isInitialized()) {
66 B2FATAL("Material effects not set up. Please use SetupGenfitExtrapolationModule.");
67 }
68
69 if (!genfit::FieldManager::getInstance()->isInitialized()) {
70 B2FATAL("Magnetic field not set up. Please use SetupGenfitExtrapolationModule.");
71 }
72
73 genfit::MaterialEffects::getInstance()->setMagCharge(Monopoles::monopoleMagCharge);
74}
75
76
78{
79 // The used fitting algorithm class.
82
83 const std::shared_ptr<genfit::AbsFitter>& genfitFitter = createFitter();
84 if (genfitFitter) {
85 fitter.resetFitter(genfitFitter);
86 }
87
88 B2DEBUG(29, "Number of reco track candidates to process: " << m_recoTracks.getEntries());
89 unsigned int recoTrackCounter = 0;
90
91 for (RecoTrack& recoTrack : m_recoTracks) {
92
93 if (recoTrack.getNumberOfTotalHits() < 3) {
94 B2WARNING("Genfit2Module: only " << recoTrack.getNumberOfTotalHits() << " were assigned to the Track! " <<
95 "This Track will not be fitted!");
96 continue;
97 }
98
99 B2DEBUG(29, "Fitting reco track candidate number " << recoTrackCounter);
100 B2DEBUG(29, "Reco track candidate has start values: ");
101 B2DEBUG(29, "Momentum: " << recoTrack.getMomentumSeed().X() << " " << recoTrack.getMomentumSeed().Y() << " " <<
102 recoTrack.getMomentumSeed().Z());
103 B2DEBUG(29, "Position: " << recoTrack.getPositionSeed().X() << " " << recoTrack.getPositionSeed().Y() << " " <<
104 recoTrack.getPositionSeed().Z());
105 B2DEBUG(29, "Charge: " << recoTrack.getChargeSeed());
106 B2DEBUG(29, "Total number of hits assigned to the track: " << recoTrack.getNumberOfTotalHits());
107
108
109
110 bool flippedCharge = false;
111 for (const unsigned int pdgCodeToUseForFitting : m_param_pdgCodesToUseForFitting) {
112 bool wasFitSuccessful;
113
114 if (pdgCodeToUseForFitting != Monopoles::c_monopolePDGCode) {
115 Const::ChargedStable particleUsedForFitting(pdgCodeToUseForFitting);
116 B2DEBUG(29, "PDG: " << pdgCodeToUseForFitting);
117 B2DEBUG(29, "resortHits: " << m_param_resortHits);
118
119 wasFitSuccessful = fitter.fit(recoTrack, particleUsedForFitting, m_param_resortHits);
120
121 // only flip if the current fit was the cardinal rep. and seed charge differs from fitted charge
122 if (m_correctSeedCharge && wasFitSuccessful
123 && recoTrack.getCardinalRepresentation() == recoTrack.getTrackRepresentationForPDG(pdgCodeToUseForFitting)) { // charge flipping
124 // If the charge after the fit (cardinal rep) is different from the seed charge,
125 // we change the charge seed and refit the track
126 flippedCharge |= recoTrack.getChargeSeed() != recoTrack.getMeasuredStateOnPlaneFromFirstHit().getCharge();
127
128 // debug
129 if (flippedCharge) {
130 B2DEBUG(29, "Refitting with opposite charge PDG: " << pdgCodeToUseForFitting);
131 }
132
133 } // end of charge flipping
134 } else {
135 // Different call signature for monopoles in order not to change Const::ChargedStable types
136 wasFitSuccessful = fitter.fit(recoTrack, pdgCodeToUseForFitting, m_param_resortHits);
137 }
138 const genfit::AbsTrackRep* trackRep = recoTrack.getTrackRepresentationForPDG(pdgCodeToUseForFitting);
139
140 if (!trackRep) {
141 B2FATAL("TrackRepresentation for PDG id " << pdgCodeToUseForFitting << " not present in RecoTrack although it " <<
142 "should have been created.");
143 }
144
145 B2DEBUG(28, "-----> Fit results:");
146 if (wasFitSuccessful) {
147 const genfit::FitStatus* fs = recoTrack.getTrackFitStatus(trackRep);
148 const genfit::KalmanFitStatus* kfs = dynamic_cast<const genfit::KalmanFitStatus*>(fs);
149 B2DEBUG(28, " Chi2 of the fit: " << kfs->getChi2());
150 B2DEBUG(28, " NDF of the fit: " << kfs->getBackwardNdf());
151 //Calculate probability
152 double pValue = recoTrack.getTrackFitStatus(trackRep)->getPVal();
153 B2DEBUG(28, " pValue of the fit: " << pValue);
154 const genfit::MeasuredStateOnPlane& mSoP = recoTrack.getMeasuredStateOnPlaneFromFirstHit(trackRep);
155 B2DEBUG(28, "Charge after fit " << mSoP.getCharge());
156 B2DEBUG(28, "Position after fit " << mSoP.getPos().X() << " " << mSoP.getPos().Y() << " " << mSoP.getPos().Z());
157 B2DEBUG(28, "Momentum after fit " << mSoP.getMom().X() << " " << mSoP.getMom().Y() << " " << mSoP.getMom().Z());
158 } else {
159 B2DEBUG(28, " fit failed!");
160 }
161 } // loop over hypothesis
162
163 // if charge has been flipped reset seed charge and refit all track representations
164 if (flippedCharge) {
165 recoTrack.setChargeSeed(-recoTrack.getChargeSeed());
166 // refit all present track representations
167 for (const auto trackRep : recoTrack.getRepresentations()) {
168 Const::ChargedStable particleUsedForFitting(abs(trackRep->getPDG()));
169 fitter.fit(recoTrack, particleUsedForFitting);
170 }
171 }
172 recoTrackCounter += 1;
173 } // loop tracks
174}
std::string m_param_bklmHitsStoreArrayName
StoreArray name of the BKLM hits.
bool m_param_resortHits
Resort the hits while fitting.
std::vector< unsigned int > m_param_pdgCodesToUseForFitting
Use these particle hypotheses for fitting.
std::string m_param_pxdHitsStoreArrayName
StoreArray name of the PXD hits.
void initialize() override
Initialize the store ararys and check for the material effects.
void event() override
Do the fitting using the created fitter.
virtual std::shared_ptr< genfit::AbsFitter > createFitter() const =0
Method to create the used filter.
std::string m_param_eklmHitsStoreArrayName
StoreArray name of the EKLM hits.
bool m_correctSeedCharge
if true resets the charge seed of the RecoTrack if track fit prefers the other charge
std::string m_param_recoTracksStoreArrayName
StoreArray name of the input and output reco tracks.
std::string m_param_svdHitsStoreArrayName
StoreArray name of the SVD hits.
std::string m_param_cdcHitsStoreArrayName
StoreArray name of the CDC hits.
StoreArray< RecoTrack > m_recoTracks
RecoTracks StoreArray.
bool m_param_initializeCDCTranslators
Configures whether the CDC Translators should be initialized by the FitterModule especially useful fo...
Provides a type-safe way to pass members of the chargedStableSet set.
Definition Const.h:589
@ c_Default
default configuration
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
This is the Reconstruction Event-Data Model Track.
Definition RecoTrack.h:79
Algorithm class to handle the fitting of RecoTrack objects.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
Abstract base class for different kinds of events.