ATLAS Offline Software
Loading...
Searching...
No Matches
ContainerDecorator.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef XAODMUONVIEWS_CONTAINERDECORATOR_H
5#define XAODMUONVIEWS_CONTAINERDECORATOR_H
11#include <unordered_set>
12
13namespace xAOD{
17 template<detail::PrimaryContainerConcept Cont_t, typename dType>
19 public:
22 explicit ContainerDecorator() = default;
31 const EventContext& ctx,
32 dType defValue = {}) {
33 initialize(key, ctx, std::move(defValue));
34 }
35
39
43
49 const EventContext& ctx,
50 dType defValue = {}) {
51 if (key.empty()) {
52 m_decorHandle.reset();
53 return;
54 }
55 if (m_decorHandle) {
56 THROW_EXCEPTION("The Container decorator is already initialized to "<<m_decorHandle->fullKey()<<"."
57 <<m_decorHandle->decorKey());
58 }
59 m_decorHandle = std::make_unique<Handle_t>(key, ctx);
60 m_defValue = std::move(defValue);
62 }
63
64 dType& operator()(const SG::AuxElement& auxElem) {
66 THROW_EXCEPTION("No decorator has been defined. Please use initialize() beforhand");
67 }
68 m_seenAux.insert(&auxElem);
69 return (*m_decorHandle)(auxElem);
70 }
71
72 constexpr bool isValid() const { return m_decorHandle != nullptr; }
74 constexpr operator bool () const {return isValid(); }
76 const Cont_t* container() const { return isValid() ? m_decorHandle->cptr() : nullptr; }
78 const Cont_t* operator->() const { return container(); }
79 private:
81 void initDefault() {
82 if (!m_decorHandle){
83 return;
84 }
85 if(!m_decorHandle->isPresent() ) {
86 return;
87 }
88 for (const SG::AuxElement* auxElem : **m_decorHandle) {
89 if (m_seenAux.insert(auxElem).second) {
90 (*m_decorHandle)(*auxElem) = m_defValue;
91 }
92 }
93 }
94 std::unique_ptr<Handle_t> m_decorHandle{};
95 std::unordered_set<const SG::AuxElement*> m_seenAux{};
96 dType m_defValue{};
97 };
98
104 template<detail::PrimaryContainerConcept Cont_t, typename dType>
105 std::vector<ContainerDecorator<Cont_t, dType>> createDecorators(const SG::WriteDecorHandleKeyArray<Cont_t>& keyArray,
106 const EventContext& ctx,
107 dType defValue = {}) {
108 std::vector<ContainerDecorator<Cont_t, dType>> retMe{};
109 for (const SG::WriteDecorHandleKey<Cont_t>& key : keyArray) {
110 retMe.emplace_back(key, ctx, defValue);
111 }
112 return retMe;
113 }
114}
115
116#endif
#define ATH_UNLIKELY(x)
Base class for elements of a container that can have aux data.
Property holding a SG store/key/clid/attr name from which a WriteDecorHandle is made.
Handle class for adding a decoration to an object.
Property holding a SG store/key/clid/attr name from which a WriteDecorHandle is made.
Handle class for adding a decoration to an object.
void initDefault()
Decorate the default value to all elements in the container.
const Cont_t * container() const
Returns the underlying container.
dType & operator()(const SG::AuxElement &auxElem)
Access the decoration value.
constexpr bool isValid() const
Returns whether the handle is instantiated.
const Cont_t * operator->() const
Returns the pointer to the underlying container.
ContainerDecorator()=default
Empty default constructor.
ContainerDecorator(ContainerDecorator &&other)=default
Default move constructor.
SG::WriteDecorHandle< Cont_t, dType > Handle_t
ContainerDecorator & operator=(ContainerDecorator &&other)=default
Default move assignment operator.
std::unique_ptr< Handle_t > m_decorHandle
ContainerDecorator(const SG::WriteDecorHandleKey< Cont_t > &key, const EventContext &ctx, dType defValue={})
Construct the decorator with an initialized decorHandleKey, and the current EventContext.
void initialize(const SG::WriteDecorHandleKey< Cont_t > &key, const EventContext &ctx, dType defValue={})
Instantiate the handle.
AuxElement(SG::AuxVectorData *container, size_t index)
Base class for elements of a container that can have aux data.
DecorHandleKeyArray< WriteDecorHandle< T, S >, WriteDecorHandleKey< T >, Gaudi::DataHandle::Writer > WriteDecorHandleKeyArray
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
std::vector< ContainerDecorator< Cont_t, dType > > createDecorators(const SG::WriteDecorHandleKeyArray< Cont_t > &keyArray, const EventContext &ctx, dType defValue={})
Factory method for a vector of ContainerDecorators instantiated from a WriteDecorHandleKeyArray.
setBGCode setTAP setLVL2ErrorBits bool
void initialize()
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10