ATLAS Offline Software
Loading...
Searching...
No Matches
HitCollectionMap.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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/ThreadLocalContext.h>
18
21{
22 public:
23 using Storage = std::unordered_map<std::string, std::unique_ptr<HitsVectorBase>>;
24 using StorageIterator = typename Storage::iterator;
25
29 std::pair<StorageIterator, bool> Insert(std::string const& hitCollectionName, std::unique_ptr<HitsVectorBase> hitCollection) {
30 // Store the hit collection in a map, using the hitCollectionName as key.
31 return m_outputCollections.insert({hitCollectionName, std::move(hitCollection)});
32 }
33
37 template<class HitCollectionT, class... CollectionArgs>
38 std::pair<StorageIterator, bool> Emplace(std::string const& hitCollectionName, CollectionArgs&&... args) {
39 static_assert(
40 std::is_base_of_v<HitsVectorBase, HitCollectionT>,
41 "HitCollectionT must be derived from HitsVectorBase");
42 return m_outputCollections.emplace(
43 hitCollectionName, std::make_unique<HitCollectionT>(std::forward<CollectionArgs>(args)...));
44 }
45
49 template <class T>
50 T* Find(std::string const& hitCollectionName) {
51 static_assert(
52 std::is_base_of_v<HitsVectorBase, T>,
53 "T must be derived from HitsVectorBase");
54 auto it = m_outputCollections.find(hitCollectionName);
55 if (it != m_outputCollections.end()) {
56 return static_cast<T*>(it->second.get());
57 }
58 return nullptr;
59 }
60
65 template <class T>
66 std::unique_ptr<T> Extract(std::string const& hitCollectionName) {
67 static_assert(
68 std::is_base_of_v<HitsVectorBase, T>,
69 "T must be derived from HitsVectorBase");
70 if (auto handle = m_outputCollections.extract(hitCollectionName)) {
71 // we can static cast, the caller must know the type of the hit collection
72 // for a given key
73 return std::unique_ptr<T>(static_cast<T*>(handle.mapped().release()));
74 }
75 return nullptr;
76 }
77
81 template <class T>
82 void Record(std::string const& sgKey, std::string const& hitCollectionName, EventContext const& ctx) {
83 SG::WriteHandle<T> handle(sgKey, ctx);
84 handle = Extract<T>(hitCollectionName);
85 }
86
90 template <class T>
91 void Record(std::string const& hitCollectionName) {
92 Record<T>(hitCollectionName, hitCollectionName, Gaudi::Hive::currentContext());
93 }
94
99 template <class T>
101 std::string const& sgKey,
102 std::string const& hitCollectionName,
103 EventContext const& ctx,
104 std::function<void(T&)> transform) {
105 auto hitColl = Extract<T>(hitCollectionName);
106 transform(*hitColl);
107 SG::WriteHandle<T> handle(sgKey, ctx);
108 handle = std::move(hitColl);
109 }
110
114 template <class T>
115 void TransformAndRecord(std::string const& hitCollectionName, std::function<void(T&)> transform) {
116 TransformAndRecord(hitCollectionName, hitCollectionName, Gaudi::Hive::currentContext(), std::move(transform));
117 }
118
119 private:
120 // Holds the hits container for this event.
122};
123
124#endif
Handle class for recording to StoreGate.
Small wrapper around hit collection map to facilitate accessing the hit collection.
void 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...
void 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.
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
std::pair< StorageIterator, bool > Emplace(std::string const &hitCollectionName, CollectionArgs &&... args)
Insert a container in the map with in-place construction.
void 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.
void Record(std::string const &hitCollectionName)
Overload for Record with the same name for the SG key and hit collection name.