ATLAS Offline Software
JetExternalAssocTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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  AthAlgTool(t,n,p),
16  m_momentPrefix(""),
17  m_containerName(""),
18  m_ExternalJetCollectionName(""),
19  m_VectorOfOldLinkNames({}),
20  m_VectorOfNewLinkNames({}),
21  m_dRMatch(false),
22  m_dRCut(0.01)
23 {
24  declareInterface<DerivationFramework::IAugmentationTool>(this);
25 
26  declareProperty("MomentPrefix" , m_momentPrefix);
27  declareProperty("InputJets" , m_containerName);
28 
29  declareProperty("ExternalJetCollectionName", m_ExternalJetCollectionName);
30  declareProperty("ListOfOldLinkNames" , m_VectorOfOldLinkNames);
31  declareProperty("ListOfNewLinkNames" , m_VectorOfNewLinkNames);
32 
33  declareProperty("DeltaRMatch" , m_dRMatch);
34  declareProperty("DeltaRCut" , m_dRCut);
35 }
36 
37 //**********************************************************************
38 
40 
41  // get link names
43 
44  // sanity check
46  ATH_MSG_ERROR("Vector size between old and new link names does not agree!");
47  return StatusCode::FAILURE;
48  }
49 
50  // setup vector of decorator
51  for(const std::string& NewLinkName : m_VectorOfNewLinkNames){
52  m_dec_keys.emplace_back( m_containerName + "." + m_momentPrefix + NewLinkName);
53  }
54 
55  ATH_CHECK(m_dec_keys.initialize());
56 
57  return StatusCode::SUCCESS;
58 }
59 
61 
62  return StatusCode::SUCCESS;
63 }
64 
65 //**********************************************************************
66 
68 
69  // get jet collection to be decorated
70  const xAOD::JetContainer* jets = 0;
71  if(evtStore()->retrieve(jets, m_containerName).isFailure()){
72  ATH_MSG_ERROR("Unable to retrieve jet collection: " << m_containerName << "!");
73  return StatusCode::FAILURE;
74  }
75 
76  // get external jet collection
77  const xAOD::JetContainer* ExternalJetCollection = 0;
78  if(evtStore()->retrieve(ExternalJetCollection, m_ExternalJetCollectionName).isFailure()){
79  ATH_MSG_ERROR("Unable to find external jet collection: " << m_ExternalJetCollectionName << "!");
80  return StatusCode::FAILURE;
81  }
82 
83  if(!m_dRMatch){
84  // 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
85 
86  // basic sanity check
87  if(jets->size() != ExternalJetCollection->size()){
88  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");
89  return StatusCode::FAILURE;
90  }
91 
92  for(unsigned int index = 0; index < jets->size(); index++){
93  auto jet = jets->at(index);
94  auto jet_external = ExternalJetCollection->at(index);
95 
96  if( std::abs(jet->pt() - jet_external->pt()) > 0.1 ){ // 0.1 is hard-coded in
97  ATH_MSG_WARNING("Potential inconsistency between two jet collections: " << jet->pt() << " v.s. " << jet_external->pt());
98  }
99 
100  if(!TransferLink(*jet, *jet_external)){
101  ATH_MSG_ERROR("Failure when transferring link from external jet to current jet!");
102  return StatusCode::FAILURE;
103  }
104  }
105  }
106  else{
107  // simple dR matching
108 
109  // initialize list of un-assigned external jets
110  std::vector<const xAOD::Jet*> UnAssignedExternalJets;
111  for(auto jet_external : *ExternalJetCollection){
112  UnAssignedExternalJets.push_back(jet_external);
113  }
114 
115  // loop
116  for(auto jet : *jets){
117  // get associated jet
118  double mindR = 9e9;
119  const xAOD::Jet* associated_jet = 0;
120  std::vector<const xAOD::Jet*>::iterator associated_iter = UnAssignedExternalJets.begin(); // random assignment
121 
122  for(auto external_iter = UnAssignedExternalJets.begin(); external_iter != UnAssignedExternalJets.end(); external_iter++){
123  auto jet_external = (*external_iter);
124 
125  double dR = jet->p4().DeltaR(jet_external->p4());
126  if(dR > m_dRCut) continue;
127  if(dR < mindR){
128  mindR = dR;
129  associated_jet = jet_external;
130  associated_iter = external_iter;
131  }
132  }
133 
134  if(associated_jet == 0){
135  ATH_MSG_WARNING("Unable to find associated external jet! This jet will be skipped");
136  continue;
137  }
138  else{
139  // sanity check
140  if(associated_jet != (*associated_iter)){
141  ATH_MSG_ERROR("Sanity check after association fails!");
142  return StatusCode::FAILURE;
143  }
144 
145  // remove associated jet from unasigned jet list
146  UnAssignedExternalJets.erase(associated_iter);
147  }
148 
149  // transfer the link
150  if(!TransferLink(*jet, *associated_jet)){
151  ATH_MSG_ERROR("Failure when transferring link from external jet to current jet!");
152  return StatusCode::FAILURE;
153  }
154  }
155  }
156 
157  return StatusCode::SUCCESS;
158 }
159 
160 bool JetExternalAssocTool::TransferLink(const xAOD::Jet& jet, const xAOD::Jet& jet_external) const{
161 
162  for(unsigned int index_link = 0; index_link < m_VectorOfOldLinkNames.size(); index_link++){
163  auto OldLinkName = m_VectorOfOldLinkNames[index_link];
164  auto NewLinkName = m_VectorOfNewLinkNames[index_link];
165 
166  // assume we are always dealing a list of IParticles
167  std::vector<const xAOD::IParticle*> targetObjs;
168  if(!jet_external.getAssociatedObjects<xAOD::IParticle>(OldLinkName, targetObjs)){
169  ATH_MSG_WARNING("Unable to fetch link " << OldLinkName << " under associated external jet");
170  continue;
171  }
172 
173  // put it under my jet
174  type_ghostlink targetLinks;
175  for(auto obj : targetObjs){
176  type_el el_obj;
177  el_obj.toIndexedElement(*(static_cast<const xAOD::IParticleContainer*>(obj->container())), obj->index());
178 
179  targetLinks.push_back(el_obj);
180  }
181 
183  *dec_handle(jet) = targetLinks;
184 
185  }
186 
187  return true;
188 }
189 
190 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::JetExternalAssocTool::m_momentPrefix
std::string m_momentPrefix
Properties.
Definition: JetExternalAssocTool.h:44
DerivationFramework::JetExternalAssocTool::m_ExternalJetCollectionName
std::string m_ExternalJetCollectionName
Definition: JetExternalAssocTool.h:47
index
Definition: index.py:1
DerivationFramework::JetExternalAssocTool::m_dec_keys
SG::WriteDecorHandleKeyArray< xAOD::JetContainer > m_dec_keys
Definition: JetExternalAssocTool.h:58
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::JetExternalAssocTool::type_ghostlink
std::vector< type_el > type_ghostlink
Definition: JetExternalAssocTool.h:56
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::TransferLink
bool TransferLink(const xAOD::Jet &jet, const xAOD::Jet &jet_external) const
Definition: JetExternalAssocTool.cxx:160
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...
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DerivationFramework::JetExternalAssocTool::m_VectorOfNewLinkNames
std::vector< std::string > m_VectorOfNewLinkNames
Definition: JetExternalAssocTool.h:49
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
DerivationFramework::JetExternalAssocTool::m_dRCut
double m_dRCut
Definition: JetExternalAssocTool.h:52
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
DerivationFramework::JetExternalAssocTool::m_dRMatch
bool m_dRMatch
Definition: JetExternalAssocTool.h:51
DerivationFramework::JetExternalAssocTool::finalize
StatusCode finalize()
Definition: JetExternalAssocTool.cxx:60
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::addBranches
virtual StatusCode addBranches() const
Pass the thinning service
Definition: JetExternalAssocTool.cxx:67
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
DerivationFramework::JetExternalAssocTool::m_VectorOfOldLinkNames
std::vector< std::string > m_VectorOfOldLinkNames
Definition: JetExternalAssocTool.h:48
DerivationFramework::JetExternalAssocTool::m_containerName
std::string m_containerName
Definition: JetExternalAssocTool.h:45
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
JetExternalAssocTool.h
AthAlgTool
Definition: AthAlgTool.h:26
python.PyAthena.obj
obj
Definition: PyAthena.py:132
DerivationFramework::JetExternalAssocTool::initialize
StatusCode initialize()
Definition: JetExternalAssocTool.cxx:39
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.