ATLAS Offline Software
JetPseudojetCopier.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 // JetPseudojetCopier.cxx
6 
8 #include "JetEDM/PseudoJetMap.h"
9 
10 using std::string;
11 using fastjet::PseudoJet;
12 using jet::PseudoJetMap;
13 using xAOD::Jet;
14 using xAOD::JetContainer;
15 
16 //**********************************************************************
17 
18 namespace {
19 
21  const SG::AuxElement::Accessor<float> ptAcc("JetConstitScaleMomentum_pt");
22  struct ConstitPtComp {
23  bool operator()(const xAOD::Jet* j1, const xAOD::Jet * j2) {
24  return ptAcc(*j1) > ptAcc(*j2) ;
25  }
26  };
27 
28  // Helpers to check equality according to constituents content.
29  bool hasConstituent(const xAOD::Jet* j, const xAOD::IParticle* p){
30  int i = 0; int nConst = j->numConstituents();
31  while(i<nConst ){
32  if (j->rawConstituent(i)==p) return true;
33  i++;
34  }
35  return false;
36  }
37 
38  int differentJets(const xAOD::Jet* j1, const xAOD::Jet * j2, bool fullComp =true){
39  if ( j1->numConstituents() != j2->numConstituents() ) return 1;
40  if ( j1->numConstituents() == 0 ) return 2;
41  if ( fullComp ) {
42  for(size_t i=0;i<j1->numConstituents();i++){
43  if(!hasConstituent(j2,j1->rawConstituent(i))) return 100+i;
44  }
45  return 0;
46  }
47  // Assume same input and same jet finding alg has been used :
48  if (j1->rawConstituent(0) == j2->rawConstituent(0)) return 0;
49  return 200;
50  }
51 
52 }
53 
54 
55 //**********************************************************************
56 
58  : asg::AsgTool(name), m_hpjr("",this) {
59  declareProperty("DestinationContainer", m_dstname);
60  declareProperty("Label", m_label ="JetPseudojetMap");
61  declareProperty("JetPseudojetRetriever", m_hpjr);
62 }
63 
64 //**********************************************************************
65 
67  if ( ! m_hpjr.retrieve().isSuccess() ) {
68  ATH_MSG_ERROR("Unable to retrieve jet pseudojet retriever.");
69  return StatusCode::FAILURE;
70  }
71  return StatusCode::SUCCESS;
72 }
73 
74 //**********************************************************************
75 
77 copy(const JetContainer& srcjets, const JetContainer& dstjets, const string& pjmapname) const {
78 
79  if ( ! m_hpjr.retrieve().isSuccess() ) {
80  ATH_MSG_WARNING("Unable to retrieve jet pseudojet retriever.");
81  return 1;
82  }
83 
84  // Check map used to store pseudojets.
85  PseudoJetMap* ppjmap;
86  if ( ! evtStore()->contains<PseudoJetMap>(pjmapname) ) {
87  if ( ! evtStore()->record(new PseudoJetMap, pjmapname).isSuccess() ) {
88  ATH_MSG_ERROR("Unable to create pseudojet map.");
89  }
90  }
91  if ( ! evtStore()->retrieve(ppjmap, pjmapname).isSuccess() ) {
92  ATH_MSG_WARNING("Unable to retrieve pseudojet map.");
93  return 1;
94  }
95  PseudoJetMap& pjmap = *ppjmap;
96 
97  ATH_MSG_DEBUG("Copying... source size=" << srcjets.size());
98  ATH_MSG_DEBUG(" destination size=" << dstjets.size());
99 
100  // build sorted list
101  // (sorting by constit scale pt -> almost 1-to-1 matching
102  // between source and target containers. This renders the matching loops
103  // below faster).
104  std::list<const Jet*> sortedTarget(dstjets.begin(), dstjets.end());
105  sortedTarget.sort(ConstitPtComp());
106  std::list<const Jet*> sortedSource(srcjets.begin(), srcjets.end());
107  sortedSource.sort(ConstitPtComp());
108 
109  size_t sourceIndex=0;
110  // Loop over source jets
111  for ( auto sourceIt = sortedSource.begin(); sourceIt!=sortedSource.end(); ++sourceIt ) {
112  const Jet* pjetin = *sourceIt;
113  if ( pjetin == nullptr ) {
114  ATH_MSG_WARNING("Skipping missing input jet.");
115  continue;
116  }
117  ATH_MSG_DEBUG(" Checking jet " << sourceIndex << " pt=" << ptAcc(*pjetin) );
118  // find a matching jet in the target container
119  //first remove null pointers
120  sortedTarget.remove_if([](const Jet * pJetOut){return pJetOut == nullptr;});
121  //
122  for ( auto targetIt=sortedTarget.begin(); targetIt!=sortedTarget.end(); ++targetIt ) {
123  const Jet* pjetout = *targetIt;
124  int jetdiff = differentJets(pjetin, pjetout);
125  bool isSame = jetdiff == 0;
126  ATH_MSG_DEBUG(" vs pt="<<ptAcc(*pjetout) << " jetdiff= " << jetdiff );
127  if ( isSame ){ // found a jet
128  ATH_MSG_DEBUG(" -------> identical to pt="<< ptAcc(*pjetout));
129  const PseudoJet* ppj = m_hpjr->pseudojet(*pjetin);
130  ATH_MSG_VERBOSE("Recording pseudojet " << long(ppj) << " for jet " << long(pjetout)
131  << " in map " << pjmapname);
132  pjmap[pjetout] = ppj;
133  // remove target from list to reduce next search length.
134  sortedTarget.erase(targetIt); //the returned iterator is unused, as the loop is terminated
135  break;
136  }
137  ATH_MSG_VERBOSE("No match found for destination jet " << long(pjetout));
138  } // end target loop
139 
140  auto targetIt = sortedTarget.begin(); // re-start from start
141  ATH_MSG_DEBUG(" Checking jet " << sourceIndex << " done" );
142  if ( targetIt == sortedTarget.end() ) break; // no more target left.
143  ++sourceIndex;
144  }
145 
146  return 0;
147 }
148 
149 //**********************************************************************
150 
152 copy(const JetContainer& srcjets, const std::string& dstname, const std::string& pjmapname) const {
153  const xAOD::JetContainer* pdstjets;
154  if ( ! evtStore()->retrieve(pdstjets, dstname).isSuccess() ) {
155  ATH_MSG_WARNING("Unable to retrieve destination jet container.");
156  return 1;
157  }
158  return copy(srcjets, *pdstjets, pjmapname);
159 }
160 
161 //**********************************************************************
162 
163 int JetPseudojetCopier::process(const JetContainer& srcjets) const {
164  return copy(srcjets, m_dstname, m_label);
165 }
166 
167 //**********************************************************************
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
JetPseudojetCopier::process
int process(const xAOD::JetContainer &srcjets) const
Copy the pseudojets from srcjets to dstjets.
Definition: JetPseudojetCopier.cxx:163
JetPseudojetCopier::JetPseudojetCopier
JetPseudojetCopier(const std::string &myname)
Ctor from tool name.
Definition: JetPseudojetCopier.cxx:57
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::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
JetPseudojetCopier::m_label
std::string m_label
Definition: JetPseudojetCopier.h:62
JetPseudojetCopier.h
asg
Definition: DataHandleTestTool.h:28
JetPseudojetCopier::m_dstname
std::string m_dstname
Properties.
Definition: JetPseudojetCopier.h:61
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:41
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
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
doubleTestComp.j1
j1
Definition: doubleTestComp.py:21
lumiFormat.i
int i
Definition: lumiFormat.py:85
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
JetPseudojetCopier::m_hpjr
ToolHandle< IJetPseudojetRetriever > m_hpjr
Definition: JetPseudojetCopier.h:63
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
jet::PseudoJetMap
std::map< const xAOD::Jet *, const fastjet::PseudoJet * > PseudoJetMap
Definition: PseudoJetMap.h:22
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAOD::Jet_v1::rawConstituent
const IParticle * rawConstituent(size_t i) const
Direct access to constituents. WARNING expert use only.
Definition: Jet_v1.cxx:158
PseudoJetMap.h
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
JetPseudojetCopier::initialize
StatusCode initialize()
Intialization.
Definition: JetPseudojetCopier.cxx:66
JetPseudojetCopier::copy
int copy(const xAOD::JetContainer &srcjets, const xAOD::JetContainer &dstjets, const std::string &label="PseudojetMap") const
Copy pseudojets from srcjets to dstjets.
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
xAOD::Jet_v1::numConstituents
size_t numConstituents() const
Number of constituents in this jets (this is valid even when reading a file where the constituents ha...
Definition: Jet_v1.cxx:153
xAOD::Jet
Jet_v1 Jet
Definition of the current "jet version".
Definition: Event/xAOD/xAODJet/xAODJet/Jet.h:17
doubleTestComp.j2
j2
Definition: doubleTestComp.py:22