ATLAS Offline Software
JetPtAssociationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // JetPtAssociationTool.cxx
6 
8 
11 
12 using std::string;
13 using xAOD::IParticle;
14 using xAOD::Jet;
15 using xAOD::JetContainer;
18 
19 //**********************************************************************
20 
22  : asg::AsgTool(myname) {
23 
24 }
25 
26 //**********************************************************************
27 
29 
30  if(m_jetContainerName.empty()) {
31  ATH_MSG_ERROR("JetPtAssociationTool needs to have its input jet container configured!");
32  return StatusCode::FAILURE;
33  }
36 
37  ATH_CHECK(m_assocFracKey.initialize());
38  ATH_CHECK(m_assocLinkKey.initialize());
39 
40  ATH_CHECK(m_matchingContainerName.initialize());
41 
42  return StatusCode::SUCCESS;
43 
44 }
45 
46 //**********************************************************************
47 
49 
52 
53  for(const xAOD::Jet* jet : jets) {
54  ATH_MSG_DEBUG("Processing jet " << jet->index());
55  APVector apins;
56 
57  // Retrieve the association vector.
58  if ( ! jet->getAssociatedObjects(m_aname, apins) ) {
59  ATH_MSG_WARNING("Jet does not have association vector " << m_aname);
60  return StatusCode::FAILURE;
61  }
62 
63  // Retrieve the container of jets to be matched.
65  if (!handle.isValid()){
66  ATH_MSG_WARNING("Matching jet container not found: "
68  return StatusCode::FAILURE;
69  }
70 
71  const auto *pjets = handle.cptr();
72 
73  // Match associated particle to jets.
74  FloatVector ptfs;
75  if ( ptfrac(apins, *pjets, ptfs) ) {
76  ATH_MSG_WARNING("Match of association vector to jets failed");
77  return StatusCode::FAILURE;
78  }
79  // Find the best match.
80  float frac = 0.0;
81  unsigned int ijet_matched = ptfs.size();
82  for ( unsigned int ijet=0; ijet<ptfs.size(); ++ijet ) {
83  ATH_MSG_VERBOSE(" Pt fraction[" << ijet << "]: " << ptfs[ijet]);
84  if ( ptfs[ijet] > frac ) {
85  frac = ptfs[ijet];
86  ijet_matched = ijet;
87  }
88  }
90  if ( ijet_matched < ptfs.size() ) {
91  el = ElementLink<JetContainer>(*pjets, ijet_matched);
92  }
93  string sfrac = m_aname + "AssociationFraction";
94  string slink = m_aname + "AssociationLink";
95  ATH_MSG_DEBUG(" Setting " << sfrac << " = " << frac);
96  assocFracHandle(*jet) = frac;
97  ATH_MSG_DEBUG(" Setting " << slink << " = "
98  << el.dataID() << "[" << el.index() << "]");
99  assocLinkHandle(*jet) = el;
100  }
101  return StatusCode::SUCCESS;
102 }
103 
104 //**********************************************************************
105 
107 ptfrac(const APVector& apins, const xAOD::JetContainer& jets, FloatVector& ptfs) const {
108  ptfs.clear();
109  ptfs.resize(jets.size(), 0.0);
110  ATH_MSG_DEBUG("Match jet count: " << jets.size());
111  for ( unsigned int ijet=0; ijet<jets.size(); ++ijet ) {
112  const Jet* pjet = jets[ijet];
113  if ( pjet == nullptr ) return -3;
114  APVector apouts;
115  double ptsum_target = 0.;
116  match(apins, *pjet, apouts, ptsum_target);
117  double ptsum = 0.0;
118  for ( const IParticle* ppar : apouts ) {
119  if ( ppar == nullptr ) return -4;
120  ptsum += ppar->pt();
121  }
122  ptfs[ijet] = ptsum/ptsum_target;
123  }
124  return 0;
125 }
126 
127 //**********************************************************************
128 
130 match(const APVector& aps, const xAOD::Jet& jet, APVector& apvs, double& ptsum_constituents) const {
131  ptsum_constituents = 0.;
132  apvs.clear();
133  const JetConstituentVector cons = jet.getConstituents();
134  for ( const JetConstituent* pcon : cons ) {
135  const IParticle* pparcon = pcon->rawConstituent();
136  ptsum_constituents+=pparcon->pt();
137  for ( const IParticle* pparap : aps ) {
138  if ( pparap == pparcon ) {
139  apvs.push_back(pparcon);
140  break;
141  }
142  }
143  }
144  return 0;
145 }
146 
147 //**********************************************************************
JetPtAssociationTool::m_assocLinkKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_assocLinkKey
Definition: JetPtAssociationTool.h:72
JetPtAssociationTool::m_jetContainerName
Gaudi::Property< std::string > m_jetContainerName
Definition: JetPtAssociationTool.h:68
JetPtAssociationTool::decorate
virtual StatusCode decorate(const xAOD::JetContainer &jets) const override
Inherited method to modify a jet.
Definition: JetPtAssociationTool.cxx:48
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
Jet
Basic data class defines behavior for all Jet objects The Jet class is the principal data class for...
Definition: Reconstruction/Jet/JetEvent/JetEvent/Jet.h:47
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
asg
Definition: DataHandleTestTool.h:28
JetPtAssociationTool::m_aname
Gaudi::Property< std::string > m_aname
Properties.
Definition: JetPtAssociationTool.h:67
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
I4Momentum::pt
virtual double pt() const =0
transverse momentum
JetPtAssociationTool::JetPtAssociationTool
JetPtAssociationTool(const std::string &myname)
Constructor from tool name.
Definition: JetPtAssociationTool.cxx:21
JetPtAssociationTool::initialize
virtual StatusCode initialize() override
Initialization.
Definition: JetPtAssociationTool.cxx:28
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
JetPtAssociationTool::ptfrac
int ptfrac(const APVector &aps, const xAOD::JetContainer &jets, FloatVector &ptsums) const
Return the matched pT sum for each jet in a collection.
Definition: JetPtAssociationTool.cxx:107
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
checkxAOD.frac
frac
Definition: Tools/PyUtils/bin/checkxAOD.py:256
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
JetPtAssociationTool.h
ReadHandle.h
Handle class for reading from StoreGate.
JetPtAssociationTool::m_assocFracKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_assocFracKey
Definition: JetPtAssociationTool.h:71
JetPtAssociationTool::m_matchingContainerName
SG::ReadHandleKey< xAOD::JetContainer > m_matchingContainerName
Definition: JetPtAssociationTool.h:69
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::JetConstituent
4-vector of jet constituent at the scale used during jet finding.
Definition: JetConstituentVector.h:61
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
xAOD::JetConstituentVector
A vector of jet constituents at the scale used during jet finding.
Definition: JetConstituentVector.h:117
JetPtAssociationTool::match
int match(const APVector &aps, const xAOD::Jet &jet, APVector &apvs, double &ptsum_constituents) const
Return the vector of AP's that appear as constituents of the given jet.
Definition: JetPtAssociationTool.cxx:130
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
IParticle
Definition: Event/EventKernel/EventKernel/IParticle.h:43
JetPtAssociationTool::APVector
std::vector< const xAOD::IParticle * > APVector
Definition: JetPtAssociationTool.h:32
xAOD::Jet
Jet_v1 Jet
Definition of the current "jet version".
Definition: Event/xAOD/xAODJet/xAODJet/Jet.h:17
JetPtAssociationTool::FloatVector
std::vector< float > FloatVector
Definition: JetPtAssociationTool.h:34