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()) {
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  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 
108 StatusCode PFTrackSelector::finalize(){return StatusCode::SUCCESS;}
109 
110 bool
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 
157 bool
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 }
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
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:111
eflowRecTrack::setTrackId
void setTrackId(int trackId)
Definition: eflowRecTrack.h:87
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
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:158
PFTrackSelector::finalize
StatusCode finalize()
Definition: PFTrackSelector.cxx:108
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:122
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:157
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
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:240
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:73
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:513
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:44
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
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