ATLAS Offline Software
Loading...
Searching...
No Matches
HitCollectionMap.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef HITMANAGEMENT_HITCOLLECTIONMAP_H
6#define HITMANAGEMENT_HITCOLLECTIONMAP_H
7
8#include <functional>
9#include <memory>
10#include <type_traits>
11#include <unordered_map>
12#include <utility>
13
14#include <GaudiKernel/EventContext.h>
15#include <GaudiKernel/StatusCode.h>
16#include <GaudiKernel/ThreadLocalContext.h>
19
20
23{
24 public:
25 using Storage = std::unordered_map<std::string, std::unique_ptr<HitsVectorBase>>;
26 using StorageIterator = typename Storage::iterator;
27
31 std::pair<StorageIterator, bool> Insert(std::string const& hitCollectionName, std::unique_ptr<HitsVectorBase> hitCollection) {
32 // Store the hit collection in a map, using the hitCollectionName as key.
33 return m_outputCollections.insert({hitCollectionName, std::move(hitCollection)});
34 }
35
39 template<AthHitVec::isHitVectorBase HitCollectionT, class... CollectionArgs>
40 std::pair<StorageIterator, bool> Emplace(std::string const& hitCollectionName, CollectionArgs&&... args) {
41 return m_outputCollections.emplace(
42 hitCollectionName, std::make_unique<HitCollectionT>(std::forward<CollectionArgs>(args)...));
43 }
44
48 template <AthHitVec::isHitVectorBase T>
49 T* Find(std::string const& hitCollectionName) {
50 auto it = m_outputCollections.find(hitCollectionName);
51 if (it != m_outputCollections.end()) {
52 return dynamic_cast<T*>(it->second.get());
53 }
54 return nullptr;
55 }
56
61 template <AthHitVec::isHitVectorBase T>
62 std::unique_ptr<T> Extract(std::string const& hitCollectionName) {
63 auto it = m_outputCollections.find(hitCollectionName);
64 if (it == m_outputCollections.end()) {
65 return nullptr;
66 }
67
68 auto* baseCollection = it->second.release();
69 auto* collection = dynamic_cast<T*>(baseCollection);
70 if (!collection) {
71 it->second.reset(baseCollection);
72 return nullptr;
73 }
74
75 m_outputCollections.erase(it);
76 return std::unique_ptr<T>{collection};
77 }
78
82 template <AthHitVec::isHitVectorBase T>
83 StatusCode Record(std::string const& sgKey, std::string const& hitCollectionName, EventContext const& ctx) {
84 auto hitColl = Extract<T>(hitCollectionName);
85 return recordExtracted<T>(sgKey, ctx, std::move(hitColl));
86 }
87
91 template <AthHitVec::isHitVectorBase T>
92 StatusCode Record(std::string const& hitCollectionName) {
93 auto hitColl = Extract<T>(hitCollectionName);
94 return recordExtracted<T>(hitCollectionName, Gaudi::Hive::currentContext(), std::move(hitColl));
95 }
96
101 template <AthHitVec::isHitVectorBase T>
103 std::string const& sgKey,
104 std::string const& hitCollectionName,
105 EventContext const& ctx,
106 std::function<void(T&)> transform) {
107 auto hitColl = Extract<T>(hitCollectionName);
108 if (!hitColl) {
109 return StatusCode::FAILURE;
110 }
111 transform(*hitColl);
112 return recordExtracted<T>(sgKey, ctx, std::move(hitColl));
113 }
114
118 template <AthHitVec::isHitVectorBase T>
119 StatusCode TransformAndRecord(std::string const& hitCollectionName, std::function<void(T&)> transform) {
120 return TransformAndRecord(hitCollectionName, hitCollectionName, Gaudi::Hive::currentContext(), std::move(transform));
121 }
122
123 private:
124 template <AthHitVec::isHitVectorBase T>
125 StatusCode recordExtracted(std::string const& sgKey, EventContext const& ctx, std::unique_ptr<T> hitColl) {
126 if (!hitColl) {
127 return StatusCode::FAILURE;
128 }
129
132 return handle.record(std::move(hitColl->container), std::move(hitColl->auxContainer));
133 } else {
134 SG::WriteHandle<T> handle{sgKey, ctx};
135 return handle.record(std::move(hitColl));
136 }
137 }
138
139 // Holds the hits container for this event.
141};
142
143#endif
Handle class for recording to StoreGate.
Small wrapper around hit collection map to facilitate accessing the hit collection.
StatusCode TransformAndRecord(std::string const &hitCollectionName, std::function< void(T &)> transform)
Overload for TransformAndRecord with the same name for the SG key and hit collection name.
StatusCode Record(std::string const &sgKey, std::string const &hitCollectionName, EventContext const &ctx)
Record the hit collection hitCollectionName to the StoreGate sgKey.
T * Find(std::string const &hitCollectionName)
Get the hit collection for a given SDs.
std::unique_ptr< T > Extract(std::string const &hitCollectionName)
Extract the hit collection for a given SDs downcasted to the template parameter.
StatusCode Record(std::string const &hitCollectionName)
Overload for Record with the same name for the SG key and hit collection name.
std::pair< StorageIterator, bool > Insert(std::string const &hitCollectionName, std::unique_ptr< HitsVectorBase > hitCollection)
Insert the hit collection for a given SDs.
std::unordered_map< std::string, std::unique_ptr< HitsVectorBase > > Storage
typename Storage::iterator StorageIterator
StatusCode recordExtracted(std::string const &sgKey, EventContext const &ctx, std::unique_ptr< T > hitColl)
std::pair< StorageIterator, bool > Emplace(std::string const &hitCollectionName, CollectionArgs &&... args)
Insert a container in the map with in-place construction.
StatusCode TransformAndRecord(std::string const &sgKey, std::string const &hitCollectionName, EventContext const &ctx, std::function< void(T &)> transform)
Record the hit collection hitCollectionName to the StoreGate sgKey, applying a transformation functio...
Define the concept for hit-collection carriers owning an xAOD container and its auxiliary store.
Define the concept that the struct needs to inherit from the HitsVectorBase.