ATLAS Offline Software
PFTrackSelector.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
8 #include "GaudiKernel/SystemOfUnits.h"
9 
10 constexpr float invGeV = 1./CLHEP::GeV;
11 
12 PFTrackSelector::PFTrackSelector(const std::string& name, ISvcLocator* pSvcLocator):
13  AthReentrantAlgorithm(name, pSvcLocator)
14 {
15 }
16 
18 
20  ATH_CHECK(m_trackSelectorTool.retrieve());
21 
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 
41 StatusCode 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  if (!rejectTrack) {
81  // Monitor the time per selected track
82  auto t_track = Monitored::Timer<std::chrono::microseconds>( "TIME_track" );
83  eta_track = thisTrack->eta();
84  pt_track = thisTrack->pt() * invGeV;
85 
86  /* Create the eflowRecCluster and put it in the container */
87  std::unique_ptr<eflowRecTrack> thisEFRecTrack = std::make_unique<eflowRecTrack>(ElementLink<xAOD::TrackParticleContainer>(*tracksReadHandle, trackIndex), m_theTrackExtrapolatorTool);
88  thisEFRecTrack->setTrackId(trackIndex);
89  eflowRecTracksWriteHandle->push_back(std::move(thisEFRecTrack));
90 
91  // Fill histogram
92  auto mon_trk = Monitored::Group(m_monTool, t_track, eta_track, pt_track);
93  }
94  trackIndex++;
95  }
96 
97  std::sort(eflowRecTracksWriteHandle->begin(), eflowRecTracksWriteHandle->end(), eflowRecTrack::SortDescendingPt());
98 
99  N_tracks = eflowRecTracksWriteHandle->size();
100  auto mon_exectime = Monitored::Group(m_monTool, t_exec, N_tracks);
101  return StatusCode::SUCCESS;
102 }
103 
104 StatusCode PFTrackSelector::finalize(){return StatusCode::SUCCESS;}
105 
106 bool
108 {
109  // We pass a null xAOD::Vertex pointer to accept here, the vertex is only needed if z0 cuts are applied (which they
110  // are not)
111  if (track.pt() * 0.001 < m_upperTrackPtCut){
112  return static_cast<bool>(m_trackSelectorTool->accept(track, nullptr));
113  }
114  else
115  return false;
116 }
117 
119 
120  if (m_electronsReadHandleKey.key().empty())
121  return false;
122 
124  if (electronsReadHandle.isValid()) {
125 
126  for (const auto* thisElectron : *electronsReadHandle) {
127 
128  if (thisElectron) {
129  unsigned int nTrack = thisElectron->nTrackParticles();
130 
131  if (0 != nTrack) {
133  if (origTrack) {
134  if (track == origTrack) {
135  return true;
136  }
137  } // if valid track pointer
138  else
139  ATH_MSG_WARNING("Electron object map has NULL pointer to original TrackParticle");
140  } // if has a track
141  else
142  ATH_MSG_WARNING("Electron object has " << nTrack << " tracks");
143  } // if valid pointer
144  else
145  ATH_MSG_WARNING("Electron is a NULL pointer");
146  } // electron loop
147  } else
148  ATH_MSG_WARNING("Invalid ReadHandle for electrons with key: " << electronsReadHandle.key());
149 
150  return false;
151 }
152 
153 bool
155 {
156 
157  if (m_muonsReadHandleKey.key().empty())
158  return false;
159 
161  if (muonsReadHandle.isValid()) {
162 
163  for (const auto* theMuon : *muonsReadHandle) {
164  if (theMuon) {
165  ATH_MSG_DEBUG("Considering muon in isMuon with e,pt, eta and phi of "
166  << theMuon->e() << ", " << theMuon->pt() << ", " << theMuon->eta() << " and " << theMuon->phi());
167  const ElementLink<xAOD::TrackParticleContainer>& theLink = theMuon->inDetTrackParticleLink();
168  if (theLink.isValid()) {
169  const xAOD::TrackParticle* ID_track = *theLink;
170  if (ID_track) {
171  if (track == ID_track){
172  return true;
173  }
174  } else
175  ATH_MSG_WARNING("This muon has a NULL pointer to the track");
176  } else
177  ATH_MSG_WARNING("This muon has an invalid link to the track");
178  } // if muon pointer is valid
179  else
180  ATH_MSG_WARNING("This muon is a NULL pointer");
181  } // muon loop
182  } else
183  ATH_MSG_WARNING("Invalid ReadHandle for muons with key: " << muonsReadHandle.key());
184 
185  return false;
186 }
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
PFTrackSelector::m_electronsReadHandleKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronsReadHandleKey
ReadHandleKey for the ElectronContainer to be used as input.
Definition: PFTrackSelector.h:60
ElectronxAODHelpers.h
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
PFTrackSelector::selectTrack
bool selectTrack(const xAOD::TrackParticle &track) const
This applys a selection criteria to the track using the tracking CP track selection tool.
Definition: PFTrackSelector.cxx:107
eflowRecTrack::setTrackId
void setTrackId(int trackId)
Definition: eflowRecTrack.h:100
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
PFTrackSelector::m_trackSelectorTool
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackSelectorTool
ToolHandle to track selection tool provided by tracking CP.
Definition: PFTrackSelector.h:98
PFTrackSelector::execute
StatusCode execute(const EventContext &ctx) const
Definition: PFTrackSelector.cxx:41
PFTrackSelector::isMuon
bool isMuon(const xAOD::TrackParticle *track) const
check if track belongs to an muon
Definition: PFTrackSelector.cxx:154
PFTrackSelector::finalize
StatusCode finalize()
Definition: PFTrackSelector.cxx:104
PFTrackSelector::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Online monitoring tool for recording histograms of the alg in action.
Definition: PFTrackSelector.h:107
PFTrackSelector::isElectron
bool isElectron(const xAOD::TrackParticle *track) const
check if track belongs to an electron
Definition: PFTrackSelector.cxx:118
PFTrackSelector.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
PFTrackSelector::initialize
StatusCode initialize()
Gaudi AthAlgorithm hooks.
Definition: PFTrackSelector.cxx:17
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
PFTrackSelector::m_eflowRecTracksWriteHandleKey
SG::WriteHandleKey< eflowRecTrackContainer > m_eflowRecTracksWriteHandleKey
WriteHandleKey for the eflowRecTrackContainer to write out.
Definition: PFTrackSelector.h:76
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
PFTrackSelector::m_tracksReadHandleKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_tracksReadHandleKey
ReadHandleKey for the TrackParticleContainer to be used as input.
Definition: PFTrackSelector.h:52
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
eflowRecTrack::SortDescendingPt
Definition: eflowRecTrack.h:160
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ReadCondHandleKey.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
PFTrackSelector::m_vertexKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexKey
Definition: PFTrackSelector.h:83
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
PFTrackSelector::PFTrackSelector
PFTrackSelector(const std::string &name, ISvcLocator *pSvcLocator)
Default constructor.
Definition: PFTrackSelector.cxx:12
SG::VarHandleBase::key
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:64
PFTrackSelector::m_muonsReadHandleKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonsReadHandleKey
ReadHandleKey for the MuonContainer to be used as input.
Definition: PFTrackSelector.h:68
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
PFTrackSelector::m_theTrackExtrapolatorTool
ToolHandle< eflowTrackExtrapolatorBaseAlgTool > m_theTrackExtrapolatorTool
ToolHandle for track extrapolation to calorimeter tool.
Definition: PFTrackSelector.h:89
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::EgammaHelpers::getOriginalTrackParticle
const xAOD::TrackParticle * getOriginalTrackParticle(const xAOD::Electron *el)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the electron.
Definition: ElectronxAODHelpers.cxx:11
eflowTrackExtrapolatorBaseAlgTool.h
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
PFTrackSelector::m_upperTrackPtCut
Gaudi::Property< float > m_upperTrackPtCut
Upper limit on track Pt for input tracks.
Definition: PFTrackSelector.h:101
Monitored::ScopedTimer
Helper class to create a scoped timer.
Definition: MonitoredTimer.h:95
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Monitored::Timer
A monitored timer.
Definition: MonitoredTimer.h:32
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
invGeV
constexpr float invGeV
Definition: PFTrackSelector.cxx:10