ATLAS Offline Software
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 
28  m_decorDRTracks = m_jetContainerName + ".DRTrack";
29 
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: "
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 
86 StatusCode 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;}
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 }
JetDRTrackAssocTool::m_decorDRTracks
SG::WriteDecorHandleKey< xAOD::JetContainer > m_decorDRTracks
Definition: JetDRTrackAssocTool.h:52
JetDRTrackAssocTool::decorate
virtual StatusCode decorate(const xAOD::JetContainer &jets) const override
Decorate a jet collection without otherwise modifying it.
Definition: JetDRTrackAssocTool.cxx:39
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAODP4Helpers.h
JetDRTrackAssocTool::m_dRcut
Gaudi::Property< float > m_dRcut
Definition: JetDRTrackAssocTool.h:49
asg
Definition: DataHandleTestTool.h:28
AthCommonMsg< AlgTool >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
xAOD::P4Helpers::deltaR2
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
Definition: xAODP4Helpers.h:111
JetDRTrackAssocTool::JetDRTrackAssocTool
JetDRTrackAssocTool(const std::string &name)
Definition: JetDRTrackAssocTool.cxx:11
JetDRTrackAssocTool.h
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
JetDRTrackAssocTool::m_jetContainerName
Gaudi::Property< std::string > m_jetContainerName
Definition: JetDRTrackAssocTool.h:48
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
JetDRTrackAssocTool::m_trackContainer_key
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackContainer_key
Definition: JetDRTrackAssocTool.h:51
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
JetDRTrackAssocTool::fillTrackToJetMap
StatusCode fillTrackToJetMap(std::map< const xAOD::TrackParticle *, const xAOD::Jet * > &, const xAOD::TrackParticleContainer &, const xAOD::JetContainer &, float) const
Definition: JetDRTrackAssocTool.cxx:86
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
JetDRTrackAssocTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetDRTrackAssocTool.cxx:19