ATLAS Offline Software
Loading...
Searching...
No Matches
JetDRTrackAssocTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7#include <sstream>
10
12 : asg::AsgTool(name)
13{
14}
15
16
17//**********************************************************************
18
20
21 ATH_MSG_INFO("Initializing JetDRTrackAssocTool " << name());
22
23 if(m_jetContainerName.empty()){
24 ATH_MSG_ERROR("JetDRTrackAssocTool needs to have its input container name configured!");
25 return StatusCode::FAILURE;
26 }
27
29
30 ATH_CHECK(m_trackContainer_key.initialize());
31 ATH_CHECK(m_decorDRTracks.initialize());
32
33 return StatusCode::SUCCESS;
34}
35
36//**********************************************************************
37
38
40
41 // Get input vertex collection
42 auto handle_trk = SG::makeHandle (m_trackContainer_key);
43 if (!handle_trk.isValid()){
44 ATH_MSG_ERROR("Could not retrieve the TrackParticleContainer: "
45 << m_trackContainer_key.key());
46 return StatusCode::FAILURE;
47 }
48
49 // Generate map of track to closest jet
50 // Later we invert this, to ensure that each track only
51 // gets associated to one jet
52 std::map<const xAOD::TrackParticle*, const xAOD::Jet*> closest_jet_to_track;
53 ATH_CHECK( fillTrackToJetMap(closest_jet_to_track,*handle_trk,jets,m_dRcut) );
54
55 // Prepare a vector of track links for each jet, starts empty
56 std::vector<std::vector<ElementLink<xAOD::IParticleContainer> > > tracks_per_jet(jets.size());
57 for(std::pair<const xAOD::TrackParticle*, const xAOD::Jet*> trkpair : closest_jet_to_track) {
58 // If a jet exists (was in dR range), then add the track for that jet
59 if(trkpair.second!= nullptr) {
60 // Most efficient constructor is with container + index
61 tracks_per_jet[trkpair.second->index()].emplace_back(trkpair.first,*handle_trk);
62 ATH_MSG_VERBOSE("Added track " << trkpair.first->index() << " to association list for jet " << trkpair.second->index());
63 ATH_MSG_VERBOSE("Verify ElementLink -- track pt: " << trkpair.first->pt() << "; EL pt; " << (*tracks_per_jet[trkpair.second->index()].back())->pt());
64 }
65 }
66
68
69 // Now add associated tracks to jet
70 for(const xAOD::Jet* jet : jets) {
71 assocLinkHandle(*jet) = tracks_per_jet[jet->index()];
72 ATH_MSG_DEBUG("Jet " << jet->index() << " has " << tracks_per_jet[jet->index()].size()
73 << " associated tracks with " << m_dRcut);
74 if(msgLvl(MSG::VERBOSE)) {
75 std::vector<const xAOD::TrackParticle*> tracks;
76 bool havetracks = jet->getAssociatedObjects("DRTrack", tracks);
77 if(!havetracks) ATH_MSG_WARNING("Associated tracks not found");
78 msg(MSG::VERBOSE) << "Retrieved " << tracks.size() << " DRTracks" << endmsg;
79 }
80 }
81
82 return StatusCode::SUCCESS;
83}
84
85
86StatusCode JetDRTrackAssocTool::fillTrackToJetMap(std::map<const xAOD::TrackParticle*, const xAOD::Jet*>& closest_jet_to_track,
87 const xAOD::TrackParticleContainer& tracks, const xAOD::JetContainer& jets, float dRcut) const
88{
89 // Find closest jet for every track
90 // Skip jets that are outside the target matching radius -- could configure this from the jet parameters
91 for(const xAOD::TrackParticle* track : tracks) {
92 if(track==nullptr) {return StatusCode::FAILURE;}
93 float mindR2 = 1e6; // large number
94 const xAOD::Jet* p_closestJet = nullptr;
95 for(const xAOD::Jet* jet : jets) {
96 if(jet==nullptr) {return StatusCode::FAILURE;}
97 float deltaR2 = xAOD::P4Helpers::deltaR2(*track,*jet);
98 ATH_MSG_VERBOSE("Track " << track->index() << " / jet " << jet->index() << " has dR = " << sqrt(deltaR2));
99 if(deltaR2<dRcut*dRcut) {
100 if(deltaR2<mindR2*mindR2) {
101 mindR2 = deltaR2;
102 p_closestJet = jet;
103 ATH_MSG_VERBOSE(" This is the closest jet to the track, with mindR = " << sqrt(mindR2));
104 } // found a closer jet
105 } // jet inside dR cone
106 } // loop over jets
107
108 // Add pointer to map, even if null
109 closest_jet_to_track[track] = p_closestJet;
110 }
111
112 return StatusCode::SUCCESS;
113}
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for adding a decoration to an object.
bool msgLvl(const MSG::Level lvl) const
StatusCode initialize() override
Dummy implementation of the initialisation function.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackContainer_key
StatusCode fillTrackToJetMap(std::map< const xAOD::TrackParticle *, const xAOD::Jet * > &, const xAOD::TrackParticleContainer &, const xAOD::JetContainer &, float) const
JetDRTrackAssocTool(const std::string &name)
SG::WriteDecorHandleKey< xAOD::JetContainer > m_decorDRTracks
Gaudi::Property< std::string > m_jetContainerName
virtual StatusCode decorate(const xAOD::JetContainer &jets) const override
Decorate a jet collection without otherwise modifying it.
Gaudi::Property< float > m_dRcut
Handle class for adding a decoration to an object.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
Jet_v1 Jet
Definition of the current "jet version".
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
JetContainer_v1 JetContainer
Definition of the current "jet container version".
MsgStream & msg
Definition testRead.cxx:32