1 // In case the extension is confusing, this is a -*- C++ -*- file
3 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
6 #include "xAODBTagging/BTaggingContainer.h"
7 #include "xAODJet/JetContainer.h"
9 #include "FlavorTagDiscriminants/BacklinkAlg.h"
10 #include "StoreGate/WriteDecorHandle.h"
11 #include "StoreGate/ReadDecorHandle.h"
14 namespace FlavorTagDiscriminants {
16 template <typename OLD, typename NEW>
17 BacklinkAlg<OLD,NEW>::BacklinkAlg(
18 const std::string& name, ISvcLocator* svcloc):
19 AthReentrantAlgorithm(name, svcloc)
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;
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;
41 SG::WriteDecorHandle<NEW,ElementLink<OLD>> newLinks(m_newLink, cxt);
43 for (const SG::AuxElement* old_element: *oldLinks) {
45 const auto& old_link = oldLinks(*old_element);
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;
53 SG::sgkey_t hashFromContainer = m_newLink.contHandleKey().hashedKey();
54 SG::sgkey_t hashFromLink = old_link.key();
55 if (hashFromLink != hashFromContainer) {
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;
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;
71 return StatusCode::SUCCESS;
75 template <typename OLD, typename NEW>
76 StatusCode BacklinkAlg<OLD,NEW>::finalize() {
77 return StatusCode::SUCCESS;