1 // In case the extension is confusing, this is a -*- C++ -*- file
3 Copyright (C) 2002-2024 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 StatusCode BacklinkAlg<OLD,NEW>::initialize() {
18 ATH_CHECK(m_oldLink.initialize());
19 ATH_CHECK(m_newLink.initialize());
20 return StatusCode::SUCCESS;
24 template <typename OLD, typename NEW>
25 StatusCode BacklinkAlg<OLD,NEW>::execute(const EventContext& cxt) const {
26 // Note: NEW = BTagging container, OLD = JetContainer, see the
27 // typedefs in the header file
28 SG::ReadDecorHandle<OLD,ElementLink<NEW>> oldLinks(m_oldLink, cxt);
29 if (!oldLinks.isValid()) {
30 ATH_MSG_ERROR("no old container " << oldLinks.key());
31 return StatusCode::FAILURE;
33 SG::WriteDecorHandle<NEW,ElementLink<OLD>> newLinks(m_newLink, cxt);
35 for (const SG::AuxElement* old_element: *oldLinks) {
37 const auto& old_link = oldLinks(*old_element);
39 // let's check to make sure the links are valid and that they
40 // point where we think they do
41 if (!old_link.isValid()) {
42 ATH_MSG_ERROR("invalid old link!");
43 return StatusCode::FAILURE;
45 SG::sgkey_t hashFromContainer = m_newLink.contHandleKey().hashedKey();
46 SG::sgkey_t hashFromLink = old_link.key();
47 if (hashFromLink != hashFromContainer) {
49 "Your link points to '" << old_link.dataID()
50 << "' but you're trying to add this link to '"
51 << m_newLink.contHandleKey().key() << "'");
52 return StatusCode::FAILURE;
55 const SG::AuxElement* new_element = *old_link;
56 // we're linking _to_ the container where the OLD link exists
57 // already, so the ElementLink type is OLD.
58 ElementLink<OLD> newLink(m_oldLink.contHandleKey().hashedKey(),
59 old_element->index(), cxt);
60 newLinks(*new_element) = newLink;
63 return StatusCode::SUCCESS;
67 template <typename OLD, typename NEW>
68 StatusCode BacklinkAlg<OLD,NEW>::finalize() {
69 return StatusCode::SUCCESS;