ATLAS Offline Software
Loading...
Searching...
No Matches
METRemappingAlg.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5*/
6
7#include "METRemappingAlg.h"
8
9#include <memory>
10
11namespace DerivationFramework {
12
13 METRemappingAlg::METRemappingAlg(const std::string& name, ISvcLocator* pSvcLocator) :
14 AthAlgorithm(name, pSvcLocator)
15 {
16
17 }
18
20 {
21 ATH_MSG_VERBOSE("METRemappingAlg::initialize()");
22
23 ATH_CHECK( m_jetContKey.initialize() );
24 ATH_CHECK( m_photonContKey.initialize() );
25 ATH_CHECK( m_electronContKey.initialize() );
26 ATH_CHECK( m_muonContKey.initialize() );
27 ATH_CHECK( m_tauContKey.initialize() );
28
29 ATH_CHECK( m_inputMapKey.initialize() );
30 ATH_CHECK( m_inputCoreKey.initialize() );
31 ATH_CHECK( m_outputMapKey.initialize() );
32 ATH_CHECK( m_outputCoreKey.initialize() );
33
34 return StatusCode::SUCCESS;
35 }
36
37 StatusCode METRemappingAlg::execute(const EventContext& ctx)
38 {
39 ATH_MSG_VERBOSE("METRemappingAlg::execute()");
40
41
43 if( !jetContHandle.isValid() ) {
44 ATH_MSG_ERROR("Unable to retrieve input jet container " << m_jetContKey.key());
45 return StatusCode::FAILURE;
46 }
47
48 // first iterate through the AnalysisJets container and populate a map
49 // that links original Jet objects to their calibrated counterparts
50 std::map<const xAOD::Jet*, ElementLink<xAOD::JetContainer> > jetLinkMap;
51 for( const xAOD::IParticle *j : *jetContHandle ) {
52 if( !m_accOriginalObject.isAvailable(*j) ) {
53 ATH_MSG_ERROR("originalObjectLink not available!");
54 return StatusCode::FAILURE;
55 }
56 const xAOD::IParticle *orig = *m_accOriginalObject(*j);
57 ElementLink<xAOD::JetContainer> link(*jetContHandle, j->index());
58 jetLinkMap.try_emplace(
59 static_cast<const xAOD::Jet*>(orig),
60 link
61 );
62 }
63
64 // repeat for Photon/Electron/Muon/Tau containers
65 linkMap_t objectLinkMap;
67 ATH_CHECK( fillLinkMap(objectLinkMap, photonContHandle) );
68
70 ATH_CHECK( fillLinkMap(objectLinkMap, electronContHandle) );
71
73 ATH_CHECK( fillLinkMap(objectLinkMap, muonContHandle) );
74
76 ATH_CHECK( fillLinkMap(objectLinkMap, tauContHandle) );
77
78 // now retrieve and iterate through the METmap from PHYS and
79 // use its contents as a baseline to populate our own
81 if( !inputMapHandle.isValid() ) {
82 ATH_MSG_ERROR("Unable to retrieve input MissingETAssociationMap " << m_inputMapKey.key());
83 return StatusCode::FAILURE;
84 }
85
87 ATH_CHECK( outputMapHandle.record(
88 std::make_unique<xAOD::MissingETAssociationMap>(),
89 std::make_unique<xAOD::MissingETAuxAssociationMap>()
90 ));
91 ATH_CHECK( outputMapHandle.isValid() );
92
94 for( const xAOD::MissingETAssociation *el : *inputMapHandle ) {
95 // copy constructor creates a deep copy
96 auto assoc = outputMapHandle->push_back(new xAOD::MissingETAssociation(*el));
97
98 if( !assoc->isMisc() ) {
99 // check if the reference jet has a calibrated equivalent that should be linked to instead
100 std::map<const xAOD::Jet*, ElementLink<xAOD::JetContainer> >::const_iterator jet_it = jetLinkMap.find(assoc->refJet());
101 if( jet_it != jetLinkMap.end() ) {
102 // relink to calibrated jet
103 assoc->setJetLink(jet_it->second);
104
105 // update objectLinks for this association
107 for( const ElementLink<xAOD::IParticleContainer> &link : assoc->objectLinks() ) {
108 if( !link.isValid() ) {
109 objectLinks.push_back(invalidLink);
110 continue;
111 }
112
113 linkMap_t::const_iterator obj_it = objectLinkMap.find(*link);
114 if( obj_it != objectLinkMap.end() ) {
115 objectLinks.emplace_back(obj_it->second);
116 } else {
117 // objects that aren't found in the map were selected away,
118 // but we should leave an invalid link to maintain index order
119 objectLinks.push_back(invalidLink);
120 }
121 }
122 assoc->setObjectLinks(objectLinks);
123
124 } else { // jet_it == jetLinkMap.end()
125 // jet was selected away - this case should not happen, just give an error for now
126 ATH_MSG_ERROR("Jet not found!");
127 return StatusCode::FAILURE;
128 }
129 } else { // assoc->isMisc() == true
130 // update links in the misc association
132 for( const ElementLink<xAOD::IParticleContainer> &link : assoc->objectLinks() ) {
133 if( !link.isValid() ) {
134 miscObjectLinks.push_back(invalidLink);
135 continue;
136 }
137
138 linkMap_t::const_iterator obj_it = objectLinkMap.find(*link);
139 if( obj_it != objectLinkMap.end() ) {
140 miscObjectLinks.emplace_back(obj_it->second);
141 } else {
142 miscObjectLinks.push_back(invalidLink);
143 }
144 }
145 assoc->setObjectLinks(miscObjectLinks);
146 }
147 } //> end loop over METmap
148
149 // copy over the MET core container
151 if( !inputCoreHandle.isValid() ) {
152 ATH_MSG_ERROR("Unable to retrieve input MET core container " << m_inputCoreKey.key());
153 return StatusCode::FAILURE;
154 }
155
157 ATH_CHECK( outputCoreHandle.record(
158 std::make_unique<xAOD::MissingETContainer>(),
159 std::make_unique<xAOD::MissingETAuxContainer>()
160 ));
161 ATH_CHECK( outputCoreHandle.isValid() );
162
163 for( const xAOD::MissingET *el : *inputCoreHandle ) {
164 outputCoreHandle->push_back(new xAOD::MissingET(*el));
165 }
166
167 return StatusCode::SUCCESS;
168 }
169
170 template<typename handle_t>
171 StatusCode METRemappingAlg::fillLinkMap(linkMap_t &map, handle_t &handle)
172 {
173 if( !handle.isValid() ) {
174 ATH_MSG_ERROR("Unable to retrieve " << handle.key());
175 return StatusCode::FAILURE;
176 }
177
178 for( const xAOD::IParticle *obj : *handle ) {
179 if( !m_accOriginalObject.isAvailable(*obj) ) {
180 ATH_MSG_ERROR("originalObjectLink not available!");
181 return StatusCode::FAILURE;
182 }
183 const xAOD::IParticle *orig = *m_accOriginalObject(*obj);
184 ElementLink<xAOD::IParticleContainer> link(*handle,obj->index());
185 map.try_emplace(orig, link);
186 }
187 return StatusCode::SUCCESS;
188 }
189
190} //> end namespace DerivationFramework
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
const SG::AuxElement::ConstAccessor< ElementLink< xAOD::IParticleContainer > > m_accOriginalObject
SG::WriteHandleKey< xAOD::MissingETAssociationMap > m_outputMapKey
METRemappingAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< xAOD::MissingETContainer > m_inputCoreKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetContKey
SG::WriteHandleKey< xAOD::MissingETContainer > m_outputCoreKey
StatusCode fillLinkMap(linkMap_t &map, handle_t &handle)
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauContKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronContKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonContKey
std::map< const xAOD::IParticle *, ElementLink< xAOD::IParticleContainer > > linkMap_t
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
SG::ReadHandleKey< xAOD::MissingETAssociationMap > m_inputMapKey
SG::ReadHandleKey< xAOD::PhotonContainer > m_photonContKey
virtual StatusCode initialize() override
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
STL class.
Class providing the definition of the 4-vector interface.
THE reconstruction tool.
std::vector< objlink_t > objlink_vector_t
Vector of object links type.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Jet_v1 Jet
Definition of the current "jet version".
MissingETAssociation_v1 MissingETAssociation
Version control by type definition.
MissingET_v1 MissingET
Version control by type defintion.