ATLAS Offline Software
METRemappingAlg.cxx
Go to the documentation of this file.
1 
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 
11 namespace 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() );
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 
94  const ElementLink<xAOD::IParticleContainer> invalidLink;
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>
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
DerivationFramework::METRemappingAlg::m_accOriginalObject
const SG::AuxElement::ConstAccessor< ElementLink< xAOD::IParticleContainer > > m_accOriginalObject
Definition: METRemappingAlg.h:54
xAOD::MissingETAssociation_v1
MET association descriptor contains object links and corresponding parameters.
Definition: MissingETAssociation_v1.h:29
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
DerivationFramework::METRemappingAlg::linkMap_t
std::map< const xAOD::IParticle *, ElementLink< xAOD::IParticleContainer > > linkMap_t
Definition: METRemappingAlg.h:41
DerivationFramework::METRemappingAlg::m_photonContKey
SG::ReadHandleKey< xAOD::PhotonContainer > m_photonContKey
Definition: METRemappingAlg.h:45
METRemappingAlg.h
DerivationFramework::METRemappingAlg::initialize
virtual StatusCode initialize() override
Definition: METRemappingAlg.cxx:19
DerivationFramework::METRemappingAlg::fillLinkMap
StatusCode fillLinkMap(linkMap_t &map, handle_t &handle)
Definition: METRemappingAlg.cxx:172
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
DerivationFramework::METRemappingAlg::METRemappingAlg
METRemappingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: METRemappingAlg.cxx:13
DerivationFramework::METRemappingAlg::m_inputCoreKey
SG::ReadHandleKey< xAOD::MissingETContainer > m_inputCoreKey
Definition: METRemappingAlg.h:50
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
DerivationFramework::METRemappingAlg::m_muonContKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonContKey
Definition: METRemappingAlg.h:47
DerivationFramework::METRemappingAlg::execute
virtual StatusCode execute() override
Definition: METRemappingAlg.cxx:37
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
MissingETBase::Types::objlink_vector_t
std::vector< objlink_t > objlink_vector_t
Vector of object links type.
Definition: MissingETCompositionBase.h:58
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DerivationFramework::METRemappingAlg::m_inputMapKey
SG::ReadHandleKey< xAOD::MissingETAssociationMap > m_inputMapKey
Definition: METRemappingAlg.h:49
xAOD::MissingET_v1
Principal data object for Missing ET.
Definition: MissingET_v1.h:25
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DerivationFramework::METRemappingAlg::m_jetContKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetContKey
Definition: METRemappingAlg.h:44
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DerivationFramework::METRemappingAlg::m_tauContKey
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauContKey
Definition: METRemappingAlg.h:48
DerivationFramework::METRemappingAlg::m_outputMapKey
SG::WriteHandleKey< xAOD::MissingETAssociationMap > m_outputMapKey
Definition: METRemappingAlg.h:51
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DerivationFramework::METRemappingAlg::m_electronContKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronContKey
Definition: METRemappingAlg.h:46
DerivationFramework::METRemappingAlg::m_outputCoreKey
SG::WriteHandleKey< xAOD::MissingETContainer > m_outputCoreKey
Definition: METRemappingAlg.h:52
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
python.PyAthena.obj
obj
Definition: PyAthena.py:132