ATLAS Offline Software
JetExternalAssocTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // JetExternalAssocTool.cxx
6 
7 #include "JetExternalAssocTool.h"
9 
10 namespace DerivationFramework{
11 
12  //**********************************************************************
13 
14  JetExternalAssocTool::JetExternalAssocTool(const std::string& t, const std::string& n, const IInterface* p):
15  base_class(t,n,p)
16  {
17  }
18 
19  //**********************************************************************
20 
24  // get link names
25  if(m_VectorOfNewLinkNames.empty()) m_VectorOfNewLinkNames = m_VectorOfOldLinkNames; // FIXME Properties should be set properly in the configuration layer.
26 
27  // sanity check
29  ATH_MSG_ERROR("Vector size between old and new link names does not agree!");
30  return StatusCode::FAILURE;
31  }
32 
33  // setup vector of decorator
34  for(const std::string& NewLinkName : m_VectorOfNewLinkNames){
35  m_dec_keys.emplace_back( m_containerName.key() + "." + m_momentPrefix.value() + NewLinkName); // FIXME HandleKeys should be set properly in the configuration layer.
36  }
37 
38  ATH_CHECK(m_dec_keys.initialize());
39 
40  return StatusCode::SUCCESS;
41  }
42 
43  //**********************************************************************
44 
45  StatusCode JetExternalAssocTool::addBranches(const EventContext& ctx) const{
46  // get jet collection to be decorated
48  if (!jets.isValid()) {
49  ATH_MSG_ERROR("Unable to retrieve jet collection: " << m_containerName << "!");
50  return StatusCode::FAILURE;
51  }
52 
53  // get external jet collection
55  if (!ExternalJetCollection.isValid()) {
56  ATH_MSG_ERROR("Unable to find external jet collection: " << m_ExternalJetCollectionName << "!");
57  return StatusCode::FAILURE;
58  }
59 
60  if(!m_dRMatch){
61  // Here we are assuming there is a strict one-to-one correspondence between two jet collections and both jet collections have been sorted by pT
62 
63  // basic sanity check
64  if(jets->size() != ExternalJetCollection->size()){
65  ATH_MSG_ERROR("Size not the same between collection " << m_containerName << " and " << m_ExternalJetCollectionName << ". Please either double check your jet collections, or turn on dRMatch option");
66  return StatusCode::FAILURE;
67  }
68 
69  for (unsigned int index = 0; index < jets->size(); index++) {
70  auto jet = jets->at(index);
71  auto jet_external = ExternalJetCollection->at(index);
72 
73  if( std::abs(jet->pt() - jet_external->pt()) > 0.1 ){ // 0.1 is hard-coded in
74  ATH_MSG_WARNING("Potential inconsistency between two jet collections: " << jet->pt() << " v.s. " << jet_external->pt());
75  }
76 
77  if (TransferLink(*jet, *jet_external, ctx).isFailure()) {
78  ATH_MSG_ERROR("Failure when transferring link from external jet to current jet!");
79  return StatusCode::FAILURE;
80  }
81  }
82  }
83  else{
84  // simple dR matching
85 
86  // initialize list of un-assigned external jets
87  std::vector<const xAOD::Jet*> UnAssignedExternalJets;
88  for (auto jet_external : *ExternalJetCollection) {
89  UnAssignedExternalJets.push_back(jet_external);
90  }
91 
92  // loop
93  for (auto jet : *jets) {
94  // get associated jet
95  double mindR = 9e9;
96  const xAOD::Jet* associated_jet = 0;
97  std::vector<const xAOD::Jet*>::iterator associated_iter = UnAssignedExternalJets.begin(); // random assignment
98 
99  for(auto external_iter = UnAssignedExternalJets.begin(); external_iter != UnAssignedExternalJets.end(); external_iter++){
100  auto jet_external = (*external_iter);
101 
102  double dR = jet->p4().DeltaR(jet_external->p4());
103  if(dR > m_dRCut) continue;
104  if(dR < mindR){
105  mindR = dR;
106  associated_jet = jet_external;
107  associated_iter = external_iter;
108  }
109  }
110 
111  if(associated_jet == 0){
112  ATH_MSG_WARNING("Unable to find associated external jet! This jet will be skipped");
113  continue;
114  }
115  else{
116  // sanity check
117  if(associated_jet != (*associated_iter)){
118  ATH_MSG_ERROR("Sanity check after association fails!");
119  return StatusCode::FAILURE;
120  }
121 
122  // remove associated jet from unasigned jet list
123  UnAssignedExternalJets.erase(associated_iter);
124  }
125 
126  // transfer the link
127  if (TransferLink(*jet, *associated_jet, ctx).isFailure()) {
128  ATH_MSG_ERROR("Failure when transferring link from external jet to current jet!");
129  return StatusCode::FAILURE;
130  }
131  }
132  }
133 
134  return StatusCode::SUCCESS;
135  }
136 
137  StatusCode JetExternalAssocTool::TransferLink(const xAOD::Jet& jet, const xAOD::Jet& jet_external, const EventContext& ctx) const{
138 
139  for(unsigned int index_link = 0; index_link < m_VectorOfOldLinkNames.size(); index_link++){
140  const auto & OldLinkName = m_VectorOfOldLinkNames[index_link];
141 
142 
143  // assume we are always dealing a list of IParticles
144  std::vector<const xAOD::IParticle*> targetObjs;
145  if (!jet_external.getAssociatedObjects<xAOD::IParticle>(OldLinkName, targetObjs)){
146  ATH_MSG_ERROR("Unable to fetch link " << OldLinkName << " under associated external jet");
147  return StatusCode::FAILURE;
148  }
149 
150  // put it under my jet
151  type_ghostlink targetLinks;
152  for(auto obj : targetObjs){
153  type_el el_obj;
154  el_obj.toIndexedElement(*(static_cast<const xAOD::IParticleContainer*>(obj->container())), obj->index());
155 
156  targetLinks.push_back(el_obj);
157  }
158 
160  *dec_handle(jet) = std::move(targetLinks);
161 
162  }
163 
164  return StatusCode::SUCCESS;
165  }
166 
167 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::JetExternalAssocTool::m_ExternalJetCollectionName
SG::ReadHandleKey< xAOD::JetContainer > m_ExternalJetCollectionName
Definition: JetExternalAssocTool.h:42
DerivationFramework::JetExternalAssocTool::m_momentPrefix
Gaudi::Property< std::string > m_momentPrefix
Definition: JetExternalAssocTool.h:43
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
index
Definition: index.py:1
DerivationFramework::JetExternalAssocTool::m_containerName
SG::ReadHandleKey< xAOD::JetContainer > m_containerName
Properties.
Definition: JetExternalAssocTool.h:41
DerivationFramework::JetExternalAssocTool::m_dec_keys
SG::WriteDecorHandleKeyArray< xAOD::JetContainer > m_dec_keys
Definition: JetExternalAssocTool.h:46
defineDB.jets
jets
Definition: JetTagCalibration/share/defineDB.py:24
DerivationFramework::JetExternalAssocTool::m_dRMatch
Gaudi::Property< bool > m_dRMatch
Definition: JetExternalAssocTool.h:48
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
DerivationFramework::JetExternalAssocTool::type_ghostlink
std::vector< type_el > type_ghostlink
Definition: JetExternalAssocTool.h:53
DerivationFramework::JetExternalAssocTool::initialize
virtual StatusCode initialize() override final
Definition: JetExternalAssocTool.cxx:21
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
DerivationFramework::JetExternalAssocTool::JetExternalAssocTool
JetExternalAssocTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: JetExternalAssocTool.cxx:14
DerivationFramework::JetExternalAssocTool::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const override final
Definition: JetExternalAssocTool.cxx:45
xAOD::Jet_v1::getAssociatedObjects
std::vector< const T * > getAssociatedObjects(const std::string &name) const
get associated objects as a vector<object> this compact form throws an exception if the object is not...
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DerivationFramework::JetExternalAssocTool::m_VectorOfNewLinkNames
Gaudi::Property< std::vector< std::string > > m_VectorOfNewLinkNames
Definition: JetExternalAssocTool.h:45
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
DerivationFramework::JetExternalAssocTool::TransferLink
StatusCode TransferLink(const xAOD::Jet &jet, const xAOD::Jet &jet_external, const EventContext &ctx) const
Definition: JetExternalAssocTool.cxx:137
WriteDecorHandle.h
Handle class for adding a decoration to an object.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::JetExternalAssocTool::m_VectorOfOldLinkNames
Gaudi::Property< std::vector< std::string > > m_VectorOfOldLinkNames
Definition: JetExternalAssocTool.h:44
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
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:795
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DerivationFramework::JetExternalAssocTool::m_dRCut
Gaudi::Property< double > m_dRCut
Definition: JetExternalAssocTool.h:49
JetExternalAssocTool.h
python.PyAthena.obj
obj
Definition: PyAthena.py:132