ATLAS Offline Software
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>
17 #include "StoreGate/WriteHandle.h"
18 
21 {
22  public:
23  using Storage = std::unordered_map<std::string, std::unique_ptr<HitsVectorBase>>;
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(), transform);
117  }
118 
119  private:
120  // Holds the hits container for this event.
122 };
123 
124 #endif
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AthenaHitsVector.h
HitCollectionMap
Small wrapper around hit collection map to facilitate accessing the hit collection.
Definition: HitCollectionMap.h:21
HitCollectionMap::Extract
std::unique_ptr< T > Extract(std::string const &hitCollectionName)
Extract the hit collection for a given SDs downcasted to the template parameter.
Definition: HitCollectionMap.h:66
python.CaloAddPedShiftConfig.args
args
Definition: CaloAddPedShiftConfig.py:47
HitCollectionMap::TransformAndRecord
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...
Definition: HitCollectionMap.h:100
skel.it
it
Definition: skel.GENtoEVGEN.py:407
WriteHandle.h
Handle class for recording to StoreGate.
HitCollectionMap::Record
void Record(std::string const &hitCollectionName)
Overload for Record with the same name for the SG key and hit collection name.
Definition: HitCollectionMap.h:91
HitCollectionMap::Insert
std::pair< StorageIterator, bool > Insert(std::string const &hitCollectionName, std::unique_ptr< HitsVectorBase > hitCollection)
Insert the hit collection for a given SDs.
Definition: HitCollectionMap.h:29
HitCollectionMap::Emplace
std::pair< StorageIterator, bool > Emplace(std::string const &hitCollectionName, CollectionArgs &&... args)
Insert a container in the map with in-place construction.
Definition: HitCollectionMap.h:38
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
HitCollectionMap::TransformAndRecord
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.
Definition: HitCollectionMap.h:115
Record
Definition: HephProf.cxx:56
HitCollectionMap::Storage
std::unordered_map< std::string, std::unique_ptr< HitsVectorBase > > Storage
Definition: HitCollectionMap.h:23
HitCollectionMap::Find
T * Find(std::string const &hitCollectionName)
Get the hit collection for a given SDs.
Definition: HitCollectionMap.h:50
HitCollectionMap::Record
void Record(std::string const &sgKey, std::string const &hitCollectionName, EventContext const &ctx)
Record the hit collection hitCollectionName to the StoreGate sgKey.
Definition: HitCollectionMap.h:82
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
HitCollectionMap::m_outputCollections
Storage m_outputCollections
Definition: HitCollectionMap.h:121
HitCollectionMap::StorageIterator
typename Storage::iterator StorageIterator
Definition: HitCollectionMap.h:24
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35