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 int trackIndex = 0;
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 bool rejectTrack(!selectTrack(*thisTrack));
72
73 bool isElectron = this->isElectron(thisTrack);
74 bool isMuon = this->isMuon(thisTrack);
75 ATH_MSG_DEBUG("isElectron is " << isElectron << " and isMuon is " << isMuon);
76 if (isElectron || isMuon) rejectTrack = true;
77
78 ATH_MSG_DEBUG("rejectTrack is " << rejectTrack);
79
80 const xAOD::TrackParticleContainer* trkcont{nullptr};
81 if (!rejectTrack) {
82 // Monitor the time per selected track
83 auto t_track = Monitored::Timer<std::chrono::microseconds>( "TIME_track" );
84 eta_track = thisTrack->eta();
85 pt_track = thisTrack->pt() * invGeV;
86 if(trkcont==nullptr) {
87 trkcont = static_cast<const xAOD::TrackParticleContainer*>(thisTrack->container());
88 }
89
90 /* Create the eflowRecCluster and put it in the container */
91 std::unique_ptr<eflowRecTrack> thisEFRecTrack = std::make_unique<eflowRecTrack>(ElementLink<xAOD::TrackParticleContainer>(trkcont, thisTrack->index()), m_theTrackExtrapolatorTool);
92 thisEFRecTrack->setTrackId(trackIndex);
93 eflowRecTracksWriteHandle->push_back(std::move(thisEFRecTrack));
94
95 // Fill histogram
96 auto mon_trk = Monitored::Group(m_monTool, t_track, eta_track, pt_track);
97 }
98 trackIndex++;
99 }
100
101 std::sort(eflowRecTracksWriteHandle->begin(), eflowRecTracksWriteHandle->end(), eflowRecTrack::SortDescendingPt());
102
103 N_tracks = eflowRecTracksWriteHandle->size();
104 auto mon_exectime = Monitored::Group(m_monTool, t_exec, N_tracks);
105 return StatusCode::SUCCESS;
106}
107
108StatusCode PFTrackSelector::finalize(){return StatusCode::SUCCESS;}
109
110bool
112{
113 // We pass a null xAOD::Vertex pointer to accept here, the vertex is only needed if z0 cuts are applied (which they
114 // are not)
115 if (track.pt() * 0.001 < m_upperTrackPtCut){
116 return static_cast<bool>(m_trackSelectorTool->accept(track, nullptr));
117 }
118 else
119 return false;
120}
121
123
124 if (m_electronsReadHandleKey.key().empty())
125 return false;
126
128 if (electronsReadHandle.isValid()) {
129
130 for (const auto* thisElectron : *electronsReadHandle) {
131
132 if (thisElectron) {
133 unsigned int nTrack = thisElectron->nTrackParticles();
134
135 if (0 != nTrack) {
137 if (origTrack) {
138 if (track == origTrack) {
139 return true;
140 }
141 } // if valid track pointer
142 else
143 ATH_MSG_WARNING("Electron object map has NULL pointer to original TrackParticle");
144 } // if has a track
145 else
146 ATH_MSG_WARNING("Electron object has " << nTrack << " tracks");
147 } // if valid pointer
148 else
149 ATH_MSG_WARNING("Electron is a NULL pointer");
150 } // electron loop
151 } else
152 ATH_MSG_WARNING("Invalid ReadHandle for electrons with key: " << electronsReadHandle.key());
153
154 return false;
155}
156
157bool
159{
160
161 if (m_muonsReadHandleKey.key().empty())
162 return false;
163
165 if (muonsReadHandle.isValid()) {
166
167 for (const auto* theMuon : *muonsReadHandle) {
168 if (theMuon) {
169 ATH_MSG_DEBUG("Considering muon in isMuon with e,pt, eta and phi of "
170 << theMuon->e() << ", " << theMuon->pt() << ", " << theMuon->eta() << " and " << theMuon->phi());
171 const ElementLink<xAOD::TrackParticleContainer>& theLink = theMuon->inDetTrackParticleLink();
172 if (theLink.isValid()) {
173 const xAOD::TrackParticle* ID_track = *theLink;
174 if (ID_track) {
175 if (track == ID_track){
176 return true;
177 }
178 } else
179 ATH_MSG_WARNING("This muon has a NULL pointer to the track");
180 } else
181 ATH_MSG_WARNING("This muon has an invalid link to the track");
182 } // if muon pointer is valid
183 else
184 ATH_MSG_WARNING("This muon is a NULL pointer");
185 } // muon loop
186 } else
187 ATH_MSG_WARNING("Invalid ReadHandle for muons with key: " << muonsReadHandle.key());
188
189 return false;
190}
#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".