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
38 {
39 ATH_MSG_VERBOSE("METRemappingAlg::execute()");
40
41 const EventContext& ctx = Gaudi::Hive::currentContext();
42
44 if( !jetContHandle.isValid() ) {
45 ATH_MSG_ERROR("Unable to retrieve input jet container " << m_jetContKey.key());
46 return StatusCode::FAILURE;
47 }
48
49 // first iterate through the AnalysisJets container and populate a map
50 // that links original Jet objects to their calibrated counterparts
51 std::map<const xAOD::Jet*, ElementLink<xAOD::JetContainer> > jetLinkMap;
52 for( const xAOD::IParticle *j : *jetContHandle ) {
53 if( !m_accOriginalObject.isAvailable(*j) ) {
54 ATH_MSG_ERROR("originalObjectLink not available!");
55 return StatusCode::FAILURE;
56 }
57 const xAOD::IParticle *orig = *m_accOriginalObject(*j);
58 ElementLink<xAOD::JetContainer> link(*jetContHandle, j->index());
59 jetLinkMap.try_emplace(
60 static_cast<const xAOD::Jet*>(orig),
61 link
62 );
63 }
64
65 // repeat for Photon/Electron/Muon/Tau containers
66 linkMap_t objectLinkMap;
68 ATH_CHECK( fillLinkMap(objectLinkMap, photonContHandle) );
69
71 ATH_CHECK( fillLinkMap(objectLinkMap, electronContHandle) );
72
74 ATH_CHECK( fillLinkMap(objectLinkMap, muonContHandle) );
75
77 ATH_CHECK( fillLinkMap(objectLinkMap, tauContHandle) );
78
79 // now retrieve and iterate through the METmap from PHYS and
80 // use its contents as a baseline to populate our own
82 if( !inputMapHandle.isValid() ) {
83 ATH_MSG_ERROR("Unable to retrieve input MissingETAssociationMap " << m_inputMapKey.key());
84 return StatusCode::FAILURE;
85 }
86
88 ATH_CHECK( outputMapHandle.record(
89 std::make_unique<xAOD::MissingETAssociationMap>(),
90 std::make_unique<xAOD::MissingETAuxAssociationMap>()
91 ));
92 ATH_CHECK( outputMapHandle.isValid() );
93
95 for( const xAOD::MissingETAssociation *el : *inputMapHandle ) {
96 // copy constructor creates a deep copy
97 auto assoc = outputMapHandle->push_back(new xAOD::MissingETAssociation(*el));
98
99 if( !assoc->isMisc() ) {
100 // check if the reference jet has a calibrated equivalent that should be linked to instead
101 std::map<const xAOD::Jet*, ElementLink<xAOD::JetContainer> >::const_iterator jet_it = jetLinkMap.find(assoc->refJet());
102 if( jet_it != jetLinkMap.end() ) {
103 // relink to calibrated jet
104 assoc->setJetLink(jet_it->second);
105
106 // update objectLinks for this association
108 for( const ElementLink<xAOD::IParticleContainer> &link : assoc->objectLinks() ) {
109 if( !link.isValid() ) {
110 objectLinks.push_back(invalidLink);
111 continue;
112 }
113
114 linkMap_t::const_iterator obj_it = objectLinkMap.find(*link);
115 if( obj_it != objectLinkMap.end() ) {
116 objectLinks.emplace_back(obj_it->second);
117 } else {
118 // objects that aren't found in the map were selected away,
119 // but we should leave an invalid link to maintain index order
120 objectLinks.push_back(invalidLink);
121 }
122 }
123 assoc->setObjectLinks(objectLinks);
124
125 } else { // jet_it == jetLinkMap.end()
126 // jet was selected away - this case should not happen, just give an error for now
127 ATH_MSG_ERROR("Jet not found!");
128 return StatusCode::FAILURE;
129 }
130 } else { // assoc->isMisc() == true
131 // update links in the misc association
133 for( const ElementLink<xAOD::IParticleContainer> &link : assoc->objectLinks() ) {
134 if( !link.isValid() ) {
135 miscObjectLinks.push_back(invalidLink);
136 continue;
137 }
138
139 linkMap_t::const_iterator obj_it = objectLinkMap.find(*link);
140 if( obj_it != objectLinkMap.end() ) {
141 miscObjectLinks.emplace_back(obj_it->second);
142 } else {
143 miscObjectLinks.push_back(invalidLink);
144 }
145 }
146 assoc->setObjectLinks(miscObjectLinks);
147 }
148 } //> end loop over METmap
149
150 // copy over the MET core container
152 if( !inputCoreHandle.isValid() ) {
153 ATH_MSG_ERROR("Unable to retrieve input MET core container " << m_inputCoreKey.key());
154 return StatusCode::FAILURE;
155 }
156
158 ATH_CHECK( outputCoreHandle.record(
159 std::make_unique<xAOD::MissingETContainer>(),
160 std::make_unique<xAOD::MissingETAuxContainer>()
161 ));
162 ATH_CHECK( outputCoreHandle.isValid() );
163
164 for( const xAOD::MissingET *el : *inputCoreHandle ) {
165 outputCoreHandle->push_back(new xAOD::MissingET(*el));
166 }
167
168 return StatusCode::SUCCESS;
169 }
170
171 template<typename handle_t>
172 StatusCode METRemappingAlg::fillLinkMap(linkMap_t &map, handle_t &handle)
173 {
174 if( !handle.isValid() ) {
175 ATH_MSG_ERROR("Unable to retrieve " << handle.key());
176 return StatusCode::FAILURE;
177 }
178
179 for( const xAOD::IParticle *obj : *handle ) {
180 if( !m_accOriginalObject.isAvailable(*obj) ) {
181 ATH_MSG_ERROR("originalObjectLink not available!");
182 return StatusCode::FAILURE;
183 }
184 const xAOD::IParticle *orig = *m_accOriginalObject(*obj);
185 ElementLink<xAOD::IParticleContainer> link(*handle,obj->index());
186 map.try_emplace(orig, link);
187 }
188 return StatusCode::SUCCESS;
189 }
190
191} //> 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 with parameters:
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
virtual StatusCode execute() override
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronContKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonContKey
std::map< const xAOD::IParticle *, ElementLink< xAOD::IParticleContainer > > linkMap_t
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.