ATLAS Offline Software
BacklinkAlg.icc
Go to the documentation of this file.
1 // In case the extension is confusing, this is a -*- C++ -*- file
2 /*
3  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
4 */
5 
6 #include "xAODBTagging/BTaggingContainer.h"
7 #include "xAODJet/JetContainer.h"
8 
9 #include "FlavorTagDiscriminants/BacklinkAlg.h"
10 #include "StoreGate/WriteDecorHandle.h"
11 #include "StoreGate/ReadDecorHandle.h"
12 
13 
14 namespace FlavorTagDiscriminants {
15 
16  template <typename OLD, typename NEW>
17  BacklinkAlg<OLD,NEW>::BacklinkAlg(
18  const std::string& name, ISvcLocator* svcloc):
19  AthReentrantAlgorithm(name, svcloc)
20  {
21  }
22 
23 
24  template <typename OLD, typename NEW>
25  StatusCode BacklinkAlg<OLD,NEW>::initialize() {
26  ATH_CHECK(m_oldLink.initialize());
27  ATH_CHECK(m_newLink.initialize());
28  return StatusCode::SUCCESS;
29  }
30 
31 
32  template <typename OLD, typename NEW>
33  StatusCode BacklinkAlg<OLD,NEW>::execute(const EventContext& cxt) const {
34  // Note: NEW = BTagging container, OLD = JetContainer, see the
35  // typedefs in the header file
36  SG::ReadDecorHandle<OLD,ElementLink<NEW>> oldLinks(m_oldLink, cxt);
37  if (!oldLinks.isValid()) {
38  ATH_MSG_ERROR("no old container " << oldLinks.key());
39  return StatusCode::FAILURE;
40  }
41  SG::WriteDecorHandle<NEW,ElementLink<OLD>> newLinks(m_newLink, cxt);
42 
43  for (const SG::AuxElement* old_element: *oldLinks) {
44 
45  const auto& old_link = oldLinks(*old_element);
46 
47  // let's check to make sure the links are valid and that they
48  // point where we think they do
49  if (!old_link.isValid()) {
50  ATH_MSG_ERROR("invalid old link!");
51  return StatusCode::FAILURE;
52  }
53  SG::sgkey_t hashFromContainer = m_newLink.contHandleKey().hashedKey();
54  SG::sgkey_t hashFromLink = old_link.key();
55  if (hashFromLink != hashFromContainer) {
56  ATH_MSG_ERROR(
57  "Your link points to '" << old_link.dataID()
58  << "' but you're trying to add this link to '"
59  << m_newLink.contHandleKey().key() << "'");
60  return StatusCode::FAILURE;
61  }
62 
63  const SG::AuxElement* new_element = *old_link;
64  // we're linking _to_ the container where the OLD link exists
65  // already, so the ElementLink type is OLD.
66  ElementLink<OLD> newLink(m_oldLink.contHandleKey().hashedKey(),
67  old_element->index(), cxt);
68  newLinks(*new_element) = newLink;
69 
70  }
71  return StatusCode::SUCCESS;
72  }
73 
74 
75  template <typename OLD, typename NEW>
76  StatusCode BacklinkAlg<OLD,NEW>::finalize() {
77  return StatusCode::SUCCESS;
78  }
79 
80 }