Belle II Software development
FlippedRecoTracksMergerModule.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
9#include <tracking/modules/FlippedRecoTracksMerger/FlippedRecoTracksMergerModule.h>
10#include <tracking/trackFitting/fitter/base/TrackFitter.h>
11
12using namespace Belle2;
13
14REG_MODULE(FlippedRecoTracksMerger);
15
17 Module()
18{
19 setDescription("This module will check the 2 flipping QIs of one RecoTrack and update the original RecoTrack, Track and TrackFitResults if the flipped one should be taken (according to the 2 flipping QIs)");
21
22 addParam("inputStoreArrayName", m_inputStoreArrayName,
23 "Name of the input StoreArray");
24 addParam("inputStoreArrayNameFlipped", m_inputStoreArrayNameFlipped,
25 "Name of the input StoreArray for flipped tracks");
26
27 addParam("pxdHitsStoreArrayName", m_param_pxdHitsStoreArrayName, "StoreArray name of the input PXD hits.",
29 addParam("svdHitsStoreArrayName", m_param_svdHitsStoreArrayName, "StoreArray name of the input SVD hits.",
31 addParam("cdcHitsStoreArrayName", m_param_cdcHitsStoreArrayName, "StoreArray name of the input CDC hits.",
33 addParam("bklmHitsStoreArrayName", m_param_bklmHitsStoreArrayName, "StoreArray name of the input BKLM hits.",
35 addParam("eklmHitsStoreArrayName", m_param_eklmHitsStoreArrayName, "StoreArray name of the input EKLM hits.",
37}
38
40{
43 m_trackFitResults.isRequired();
44}
45
47{
48 // get the cut from DB
49 if (!m_flipCutsFromDB.isValid()) {
50 B2WARNING("DBobjects : TrackFlippingCuts not found!");
51 return;
52 }
53
54 // check if the flip&refit is switched on (or off)
55 if (!(*m_flipCutsFromDB).getOnOffInfo()) return;
56
57
58 // loop all the recoTracks
59 for (RecoTrack& recoTrack : m_inputRecoTracks) {
60
61 // check if the recoTracks was fitted successfully (WARNING: only the latest fit is checked)
62 if (not recoTrack.wasFitSuccessful()) {
63 continue;
64 }
65
66
67 // get the related Belle2::Tracks
68 Track* track = recoTrack.getRelatedFrom<Belle2::Track>();
69
70 if (!track) continue;
71
72 // do not change the tracks with Pt > 0.3 GeV
73 auto trackFitResult = track->getTrackFitResultWithClosestMass(Const::pion);
74 if (trackFitResult->getTransverseMomentum() > (*m_flipCutsFromDB).getPtCut()) continue;
75
76 double mvaFlipCut = (*m_flipCutsFromDB).getSecondCut();
77
78 // if we should not flip the tracks: the 2nd MVA QI is nan (aka didn't pass the 1st MVA filter) or smaller than the cut
79 if (isnan(recoTrack.get2ndFlipQualityIndicator()) or (recoTrack.get2ndFlipQualityIndicator() < mvaFlipCut)) continue;
80 // get the related flippedRecoTrack
81 RecoTrack* flippedRecoTrack = recoTrack.getRelatedFrom<Belle2::RecoTrack>("RecoTracks_flipped");
82
83 if (!flippedRecoTrack) continue;
84
85 // The algorithm needs to copy the genfit::Track at the end. This can only be done if it passes
86 // the consistency check. So discard those tracks in the first place.
87 try {
88 flippedRecoTrack->getGenfitTrack().checkConsistency();
89 } catch (...) {
90 B2WARNING("Consistency check of genfit track failed. Will skip this track candidate.");
91 continue;
92 }
93
94 // get the tracksflipped
95 Track* trackFlipped = flippedRecoTrack->getRelatedFrom<Belle2::Track>("Tracks_flipped");
96 if (!trackFlipped) continue;
97 std::vector<Track::ChargedStableTrackFitResultPair> fitResultsAfter =
98 trackFlipped->getTrackFitResultsByName("TrackFitResults_flipped");
99 std::vector<Track::ChargedStableTrackFitResultPair> fitResultsBefore = track->getTrackFitResults();
100
101 //set the c_isFlippedAndRefitted bit
102 track->setFlippedAndRefitted();
103
104 // invalidate all TrackFitResults of old Track that dont exist in new Track
105 for (auto fitResult : fitResultsBefore) {
106 auto iterFitResult = std::find_if(fitResultsAfter.begin(),
107 fitResultsAfter.end(), [&fitResult](const Track::ChargedStableTrackFitResultPair & a) { return a.first == fitResult.first;});
108 // did not find this hypothesis in the new FitResults, so invalidate it
109 if (iterFitResult == fitResultsAfter.end()) {
110 track->setTrackFitResultIndex(fitResult.first, -1);
111 fitResult.second->mask();
112 }
113 }
114
115
116 // loop over new TrackFitResults and update or add new FitResults to the old Track
117 for (auto fitResult : fitResultsAfter) {
118
119 auto oldFitResultPairIter = std::find_if(fitResultsBefore.begin(),
120 fitResultsBefore.end(), [&fitResult](const Track::ChargedStableTrackFitResultPair & a) { return a.first == fitResult.first;});
121
122 // if old result exists update it, if not exists add a new TrackFitResult
123 if (oldFitResultPairIter != fitResultsBefore.end()) {
124 oldFitResultPairIter->second->updateTrackFitResult(*fitResult.second);
125 } else {
126 TrackFitResult* newFitResult = m_trackFitResults.appendNew();
127 newFitResult->updateTrackFitResult(*fitResult.second);
128 const int newTrackFitResultArrayIndex = newFitResult->getArrayIndex();
129 track->setTrackFitResultIndex(fitResult.first, newTrackFitResultArrayIndex);
130 }
131 }
132
133 recoTrack.flipTrackDirectionAndCharge();
134
135 // swapping the flipped genfit::Track into the main RecoTrack, to store the new fitted values (useful for V0Finder)
136 genfit::Track flipTrack = RecoTrackGenfitAccess::getGenfitTrack(*flippedRecoTrack);
137 RecoTrackGenfitAccess::swapGenfitTrack(recoTrack, &flipTrack);
138 }
139}
static const ChargedStable pion
charged pion particle
Definition: Const.h:661
std::string m_param_bklmHitsStoreArrayName
StoreArray name of the BKLM hits.
StoreArray< RecoTrack > m_inputRecoTracks
store array for the input RecoTracks
std::string m_inputStoreArrayNameFlipped
Name of the input StoreArray for flipped tracks.
std::string m_param_pxdHitsStoreArrayName
StoreArray name of the PXD hits.
void initialize() override
Declare required StoreArray.
std::string m_param_eklmHitsStoreArrayName
StoreArray name of the EKLM hits.
StoreArray< TrackFitResult > m_trackFitResults
StoreArray of TrackFitResult, only default name should be considered here.
std::string m_param_svdHitsStoreArrayName
StoreArray name of the SVD hits.
std::string m_inputStoreArrayName
Name of the input StoreArray.
std::string m_param_cdcHitsStoreArrayName
StoreArray name of the CDC hits.
FlippedRecoTracksMergerModule()
Constructor of the module. Setting up parameters and description.
OptionalDBObjPtr< TrackFlippingCuts > m_flipCutsFromDB
flipping cuts could be read from the DB
StoreArray< RecoTrack > m_inputRecoTracksFlipped
store array for the input flipped RecoTracks
Base class for Modules.
Definition: Module.h:72
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
@ 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
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition: RecoTrack.cc:404
static void swapGenfitTrack(RecoTrack &recoTrack, const genfit::Track *track)
Set the genfit track of a Recotrack copying the information from another genfit track.
Definition: RecoTrack.cc:399
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:79
const genfit::Track & getGenfitTrack() const
Returns genfit track.
Definition: RecoTrack.h:505
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Values of the result of a track fit with a given particle hypothesis.
void updateTrackFitResult(const TrackFitResult &input)
update the TrackFitResults
Class that bundles various TrackFitResults.
Definition: Track.h:25
std::vector< ChargedStableTrackFitResultPair > getTrackFitResultsByName(const std::string trackFitResultsName) const
Access to all track fit results at the same time (from TrackFitResult with specified name)
Definition: Track.cc:42
std::pair< Const::ChargedStable, TrackFitResult * > ChargedStableTrackFitResultPair
Pair to hold the particle hypothesis used for the fit as first entry and the result of the track fit ...
Definition: Track.h:39
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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.