ATLAS Offline Software
Loading...
Searching...
No Matches
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
9
10using std::string;
11using fastjet::PseudoJet;
13using xAOD::Jet;
15
16//**********************************************************************
17
18namespace {
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
77copy(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
152copy(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
163int JetPseudojetCopier::process(const JetContainer& srcjets) const {
164 return copy(srcjets, m_dstname, m_label);
165}
166
167//**********************************************************************
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
int copy(const xAOD::JetContainer &srcjets, const xAOD::JetContainer &dstjets, const std::string &label="PseudojetMap") const
Copy pseudojets from srcjets to dstjets.
JetPseudojetCopier(const std::string &myname)
Ctor from tool name.
StatusCode initialize()
Intialization.
std::string m_dstname
Properties.
ToolHandle< IJetPseudojetRetriever > m_hpjr
int process(const xAOD::JetContainer &srcjets) const
Copy the pseudojets from srcjets to dstjets.
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Class providing the definition of the 4-vector interface.
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
const IParticle * rawConstituent(size_t i) const
Direct access to constituents. WARNING expert use only.
Definition Jet_v1.cxx:158
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
std::map< const xAOD::Jet *, const fastjet::PseudoJet * > PseudoJetMap
Jet_v1 Jet
Definition of the current "jet version".
JetContainer_v1 JetContainer
Definition of the current "jet container version".