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/flipAndRefit/FlippedRecoTracksMergerModule.h>
10#include <mdst/dataobjects/Track.h>
11#include <mdst/dataobjects/TrackFitResult.h>
12#include <tracking/dataobjects/RecoTrack.h>
13#include <tracking/dbobjects/TrackFlippingCuts.h>
14
15using namespace Belle2;
16
17REG_MODULE(FlippedRecoTracksMerger);
18
20 Module()
21{
22 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)");
24
25 addParam("inputStoreArrayName", m_inputStoreArrayName,
26 "Name of the input StoreArray");
27 addParam("inputStoreArrayNameFlipped", m_inputStoreArrayNameFlipped,
28 "Name of the input StoreArray for flipped tracks");
29}
30
37
39{
40 if (!m_flipCutsFromDB.isValid())
41 B2FATAL("TRKTrackFlipAndRefit_MVA_cuts payload is not available");
42}
43
45{
46 // check if the flip&refit is switched on (or off)
47 if (!(*m_flipCutsFromDB).getOnOffInfo()) return;
48
49
50 // loop all the recoTracks
51 for (RecoTrack& recoTrack : m_inputRecoTracks) {
52
53 // check if the recoTracks was fitted successfully (WARNING: only the latest fit is checked)
54 if (not recoTrack.wasFitSuccessful()) {
55 continue;
56 }
57
58 // get the related Belle2::Tracks
59 Track* track = recoTrack.getRelatedFrom<Belle2::Track>();
60
61 if (!track) continue;
62
63 // do not change the tracks with Pt > 0.3 GeV
64 auto trackFitResult = track->getTrackFitResultWithClosestMass(Const::pion);
65 if (trackFitResult->getTransverseMomentum() > (*m_flipCutsFromDB).getPtCut()) continue;
66
67 double mvaFlipCut = (*m_flipCutsFromDB).getSecondCut();
68
69 // 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
70 if (std::isnan(recoTrack.get2ndFlipQualityIndicator()) or (recoTrack.get2ndFlipQualityIndicator() < mvaFlipCut)) continue;
71 // get the related flippedRecoTrack
72 RecoTrack* flippedRecoTrack = recoTrack.getRelatedFrom<Belle2::RecoTrack>("RecoTracks_flipped");
73
74 if (!flippedRecoTrack) continue;
75
76 // The algorithm needs to copy the genfit::Track at the end. This can only be done if it passes
77 // the consistency check. So discard those tracks in the first place.
78 try {
79 flippedRecoTrack->getGenfitTrack().checkConsistency();
80 } catch (...) {
81 B2WARNING("Consistency check of genfit track failed. Will skip this track candidate.");
82 continue;
83 }
84
85 // get the tracksflipped
86 Track* trackFlipped = flippedRecoTrack->getRelatedFrom<Belle2::Track>("Tracks_flipped");
87 if (!trackFlipped) continue;
88 std::vector<Track::ChargedStableTrackFitResultPair> fitResultsAfter =
89 trackFlipped->getTrackFitResultsByName("TrackFitResults_flipped");
90 std::vector<Track::ChargedStableTrackFitResultPair> fitResultsBefore = track->getTrackFitResults();
91
92 //set the c_isFlippedAndRefitted bit
93 track->setFlippedAndRefitted();
94
95 // invalidate all TrackFitResults of old Track that dont exist in new Track
96 for (auto fitResult : fitResultsBefore) {
97 auto iterFitResult = std::find_if(fitResultsAfter.begin(),
98 fitResultsAfter.end(), [&fitResult](const Track::ChargedStableTrackFitResultPair & a) { return a.first == fitResult.first;});
99 // did not find this hypothesis in the new FitResults, so invalidate it
100 if (iterFitResult == fitResultsAfter.end()) {
101 track->setTrackFitResultIndex(fitResult.first, -1);
102 fitResult.second->mask();
103 }
104 }
105
106
107 // loop over new TrackFitResults and update or add new FitResults to the old Track
108 for (auto fitResult : fitResultsAfter) {
109
110 auto oldFitResultPairIter = std::find_if(fitResultsBefore.begin(),
111 fitResultsBefore.end(), [&fitResult](const Track::ChargedStableTrackFitResultPair & a) { return a.first == fitResult.first;});
112
113 // if old result exists update it, if not exists add a new TrackFitResult
114 if (oldFitResultPairIter != fitResultsBefore.end()) {
115 oldFitResultPairIter->second->updateTrackFitResult(*fitResult.second);
116 } else {
117 TrackFitResult* newFitResult = m_trackFitResults.appendNew();
118 newFitResult->updateTrackFitResult(*fitResult.second);
119 const int newTrackFitResultArrayIndex = newFitResult->getArrayIndex();
120 track->setTrackFitResultIndex(fitResult.first, newTrackFitResultArrayIndex);
121 }
122 }
123
124 recoTrack.flipTrackDirectionAndCharge();
125
126 // swapping the flipped genfit::Track into the main RecoTrack, to store the new fitted values (useful for V0Finder)
127 genfit::Track flipTrack = RecoTrackGenfitAccess::getGenfitTrack(*flippedRecoTrack);
128 RecoTrackGenfitAccess::swapGenfitTrack(recoTrack, &flipTrack);
129 }
130}
static const ChargedStable pion
charged pion particle
Definition Const.h:661
StoreArray< RecoTrack > m_inputRecoTracks
store array for the input RecoTracks
std::string m_inputStoreArrayNameFlipped
Name of the input StoreArray for flipped tracks.
void initialize() override
Declare required StoreArray.
StoreArray< TrackFitResult > m_trackFitResults
StoreArray of TrackFitResult, only default name should be considered here.
DBObjPtr< TrackFlippingCuts > m_flipCutsFromDB
flipping cuts could be read from the DB
std::string m_inputStoreArrayName
Name of the input StoreArray.
FlippedRecoTracksMergerModule()
Constructor of the module. Setting up parameters and description.
StoreArray< RecoTrack > m_inputRecoTracksFlipped
store array for the input flipped RecoTracks
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
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.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.