ATLAS Offline Software
Loading...
Searching...
No Matches
PFTrackSelector.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
5#include "PFTrackSelector.h"
8#include "GaudiKernel/SystemOfUnits.h"
9
10constexpr float invGeV = 1./CLHEP::GeV;
11
12PFTrackSelector::PFTrackSelector(const std::string& name, ISvcLocator* pSvcLocator):
13 AthReentrantAlgorithm(name, pSvcLocator)
14{
15}
16
18
21
22 ATH_CHECK(m_tracksReadHandleKey.initialize());
23 ATH_CHECK(m_vertexKey.initialize());
24
25 // Optional readhandlekeys for electrons and muons
26 if(!m_electronsReadHandleKey.key().empty()) {
28 }
29 if(!m_muonsReadHandleKey.key().empty()) {
30 ATH_CHECK(m_muonsReadHandleKey.initialize());
31 }
32
34
35 if (!m_monTool.empty()) ATH_CHECK(m_monTool.retrieve());
36
37 return StatusCode::SUCCESS;
38
39}
40
41StatusCode PFTrackSelector::execute(const EventContext& ctx) const{
42 // Monitor the time taken to execute the alg
43 auto t_exec = Monitored::Timer<std::chrono::milliseconds>( "TIME_execute" );
44 Monitored::ScopedTimer execution_time(t_exec);
45 auto N_tracks = Monitored::Scalar( "N_tracks", 0 );
46 auto eta_track = Monitored::Scalar( "eta_track", 0. );
47 auto pt_track = Monitored::Scalar( "pt_track", 0. );
48
50 ATH_CHECK(eflowRecTracksWriteHandle.record(std::make_unique<eflowRecTrackContainer>()));
51
52 /* Verify the read handle has a valid pointer, and if not return */
54 if (!tracksReadHandle.isValid()) {
55 ATH_MSG_WARNING("Can not retrieve xAOD::TrackParticleContainer with name: " << tracksReadHandle.key());
56 return StatusCode::FAILURE;
57 }
58
59 /* Do the track selection for tracks to be used in all of the following steps: */
60
61 for (const auto *thisTrack : *tracksReadHandle){
62
63 if (!thisTrack){
64 ATH_MSG_WARNING("Have invalid pointer to xAOD::TrackParticle");
65 continue;
66 }
67
68 ATH_MSG_DEBUG("Have track with E, pt, eta and phi of " << thisTrack->e() << ", " << thisTrack->pt() << ", "
69 << thisTrack->eta() << " and " << thisTrack->phi());
70
71
72 bool rejectTrack(!selectTrack(*thisTrack));
73
74 bool isElectron = this->isElectron(thisTrack);
75 bool isMuon = this->isMuon(thisTrack);
76 ATH_MSG_DEBUG("isElectron is " << isElectron << " and isMuon is " << isMuon);
77 if (isElectron || isMuon) rejectTrack = true;
78
79 ATH_MSG_DEBUG("rejectTrack is " << rejectTrack);
80
81 const xAOD::TrackParticleContainer* trkcont{nullptr};
82 if (!rejectTrack) {
83 // Monitor the time per selected track
84 auto t_track = Monitored::Timer<std::chrono::microseconds>( "TIME_track" );
85 eta_track = thisTrack->eta();
86 pt_track = thisTrack->pt() * invGeV;
87 if(trkcont==nullptr) {
88 trkcont = static_cast<const xAOD::TrackParticleContainer*>(thisTrack->container());
89 }
90
91 /* Create the eflowRecCluster and put it in the container */
92 unsigned int trackIndex = thisTrack->index();
93 std::unique_ptr<eflowRecTrack> thisEFRecTrack = std::make_unique<eflowRecTrack>(ElementLink<xAOD::TrackParticleContainer>(trkcont, trackIndex), m_theTrackExtrapolatorTool);
94 thisEFRecTrack->setTrackId(trackIndex);
95 eflowRecTracksWriteHandle->push_back(std::move(thisEFRecTrack));
96
97 // Fill histogram
98 auto mon_trk = Monitored::Group(m_monTool, t_track, eta_track, pt_track);
99 }
100 }
101
102 std::sort(eflowRecTracksWriteHandle->begin(), eflowRecTracksWriteHandle->end(), eflowRecTrack::SortDescendingPt());
103
104 N_tracks = eflowRecTracksWriteHandle->size();
105 auto mon_exectime = Monitored::Group(m_monTool, t_exec, N_tracks);
106 return StatusCode::SUCCESS;
107}
108
109StatusCode PFTrackSelector::finalize(){return StatusCode::SUCCESS;}
110
111bool
113{
114 // We pass a null xAOD::Vertex pointer to accept here, the vertex is only needed if z0 cuts are applied (which they
115 // are not)
116 if (track.pt() * 0.001 < m_upperTrackPtCut){
117 return static_cast<bool>(m_trackSelectorTool->accept(track, nullptr));
118 }
119 else
120 return false;
121}
122
124
125 if (m_electronsReadHandleKey.key().empty())
126 return false;
127
129 if (electronsReadHandle.isValid()) {
130
131 for (const auto* thisElectron : *electronsReadHandle) {
132
133 if (thisElectron) {
134 unsigned int nTrack = thisElectron->nTrackParticles();
135
136 if (0 != nTrack) {
138 if (origTrack) {
139 if (track == origTrack) {
140 return true;
141 }
142 } // if valid track pointer
143 else
144 ATH_MSG_WARNING("Electron object map has NULL pointer to original TrackParticle");
145 } // if has a track
146 else
147 ATH_MSG_WARNING("Electron object has " << nTrack << " tracks");
148 } // if valid pointer
149 else
150 ATH_MSG_WARNING("Electron is a NULL pointer");
151 } // electron loop
152 } else
153 ATH_MSG_WARNING("Invalid ReadHandle for electrons with key: " << electronsReadHandle.key());
154
155 return false;
156}
157
158bool
160{
161
162 if (m_muonsReadHandleKey.key().empty())
163 return false;
164
166 if (muonsReadHandle.isValid()) {
167
168 for (const auto* theMuon : *muonsReadHandle) {
169 if (theMuon) {
170 ATH_MSG_DEBUG("Considering muon in isMuon with e,pt, eta and phi of "
171 << theMuon->e() << ", " << theMuon->pt() << ", " << theMuon->eta() << " and " << theMuon->phi());
172 const ElementLink<xAOD::TrackParticleContainer>& theLink = theMuon->inDetTrackParticleLink();
173 if (theLink.isValid()) {
174 const xAOD::TrackParticle* ID_track = *theLink;
175 if (ID_track) {
176 if (track == ID_track){
177 return true;
178 }
179 } else
180 ATH_MSG_WARNING("This muon has a NULL pointer to the track");
181 } else
182 ATH_MSG_WARNING("This muon has an invalid link to the track");
183 } // if muon pointer is valid
184 else
185 ATH_MSG_WARNING("This muon is a NULL pointer");
186 } // muon loop
187 } else
188 ATH_MSG_WARNING("Invalid ReadHandle for muons with key: " << muonsReadHandle.key());
189
190 return false;
191}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
constexpr float invGeV
An algorithm that can be simultaneously executed in multiple threads.
Group of local monitoring quantities and retain correlation when filling histograms
Declare a monitored scalar variable.
Helper class to create a scoped timer.
A monitored timer.
bool selectTrack(const xAOD::TrackParticle &track) const
This applys a selection criteria to the track using the tracking CP track selection tool.
SG::WriteHandleKey< eflowRecTrackContainer > m_eflowRecTracksWriteHandleKey
WriteHandleKey for the eflowRecTrackContainer to write out.
bool isMuon(const xAOD::TrackParticle *track) const
check if track belongs to an muon
bool isElectron(const xAOD::TrackParticle *track) const
check if track belongs to an electron
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexKey
PFTrackSelector(const std::string &name, ISvcLocator *pSvcLocator)
Default constructor.
StatusCode initialize()
Gaudi AthAlgorithm hooks.
ToolHandle< eflowTrackExtrapolatorBaseAlgTool > m_theTrackExtrapolatorTool
ToolHandle for track extrapolation to calorimeter tool.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_tracksReadHandleKey
ReadHandleKey for the TrackParticleContainer to be used as input.
StatusCode execute(const EventContext &ctx) const
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackSelectorTool
ToolHandle to track selection tool provided by tracking CP.
SG::ReadHandleKey< xAOD::MuonContainer > m_muonsReadHandleKey
ReadHandleKey for the MuonContainer to be used as input.
ToolHandle< GenericMonitoringTool > m_monTool
Online monitoring tool for recording histograms of the alg in action.
StatusCode finalize()
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronsReadHandleKey
ReadHandleKey for the ElectronContainer to be used as input.
Gaudi::Property< float > m_upperTrackPtCut
Upper limit on track Pt for input tracks.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
const xAOD::TrackParticle * getOriginalTrackParticle(const xAOD::Electron *el)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the electron.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".