Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ReadMetaHandle.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 STOREGATE_READMETAHANDLE_H
6 #define STOREGATE_READMETAHANDLE_H
7 
11 
12 #include "StoreGate/ReadHandle.h"
15 
16 #include "GaudiKernel/DataHandle.h"
17 #include "GaudiKernel/DataObjID.h"
18 #include "GaudiKernel/EventContext.h"
19 #include "GaudiKernel/ThreadLocalContext.h"
20 
21 #include <string>
22 #include <stdexcept>
23 
24 namespace SG {
25 
26  template <typename T>
27  class ReadMetaHandle {
28 
29  public:
30  typedef T* pointer_type; // FIXME: better handling of
31  typedef const T* const_pointer_type; // qualified T type ?
32  typedef T& reference_type;
33  typedef const T& const_reference_type;
34 
35  public:
38  , const EventContext& ctx);
39 
40  ReadMetaHandle(SG::ReadMetaHandleKey<T>&& key) = delete; // Not allowed from a temporary.
42  const EventContext& ctx) = delete; // Not allowed from a temporary.
43 
45 
48 
51 
52  bool isValid();
53 
54  private:
55 
56  bool initMetaHandle();
57 
59  const MetaCont<T>* m_cont {nullptr};
60  T* m_ent {nullptr};
61 
63  };
64 
65 
66  //------------------------------------------------------------------------
67 
68  template <typename T>
70  : ReadMetaHandle(key, Gaudi::Hive::currentContext())
71  {
72  }
73 
74  //------------------------------------------------------------------------
75 
76  template <typename T>
78  , const EventContext& ctx)
79  : m_sid( Atlas::getExtendedEventContext(ctx).proxy()->sourceID() )
80  , m_cont( key.getContainer() )
81  , m_hkey(key)
82  {
83  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
84  if (ATH_UNLIKELY(!m_hkey.isInit())) {
85  msg << MSG::ERROR
86  << "ReadMetaHandleKey " << key.objKey() << " was not initialized"
87  << endmsg;
88  throw std::runtime_error("ReadMetaHandle: ReadMetaHandleKey was not initialized");
89  }
90 
91  if (ATH_UNLIKELY(m_cont == 0)) {
92  // Try to retrieve it
93  StoreGateSvc* ms = m_hkey.getStore();
94  MetaContBase* cb{nullptr};
95  if(ms->retrieve(cb, m_hkey.key()).isFailure()) {
96  msg << MSG::ERROR
97  << "can't retrieve " << m_hkey.fullKey()
98  << " via base class" << endmsg;
99  throw std::runtime_error("ReadCondHandle: ptr to CondCont<T> is zero");
100  }
101  else {
102  m_cont = dynamic_cast< MetaCont<T>* > (cb);
103  if(!m_cont) {
104  msg << MSG::ERROR
105  << "can't dcast MetaContBase to " << m_hkey.fullKey()
106  << endmsg;
107  throw std::runtime_error("ReadMetaHandle: ptr to MetaCont<T> is zero");
108  }
109  }
110  }
111  }
112 
113  //------------------------------------------------------------------------
114 
115  template <typename T>
116  bool
118  {
119 
120  if (m_ent) return true;
121 
122  // Initialize sid from dbKey found at ReadMetaHandleKey initialize
123 // m_sid = m_hkey.dbKey(); ???? Do I need this ????
124 
125  if ( ATH_UNLIKELY(! m_cont->find(m_sid, m_ent)) ) {
126  std::ostringstream ost;
127  m_cont->list(ost);
128  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
129  msg << MSG::ERROR
130  << "ReadMetaHandle:: could not find current SourceID "
131  << m_sid << " for key " << m_hkey.objKey() << "\n"
132  << ost.str() << endmsg;
133  m_ent = nullptr;
134  return false;
135  }
136 
137  return true;
138  }
139 
140  //------------------------------------------------------------------------
141 
142  template <typename T>
143  const T*
145  {
146 
147  if (m_ent == 0) {
148  if (!initMetaHandle()) {
149  return nullptr;
150  }
151  }
152 
153  return m_ent;
154  }
155 
156  //------------------------------------------------------------------------
157 
158  template <typename T>
159  const T*
161  {
162  if (sid == m_sid) {
163  return retrieve();
164  }
165 
166  pointer_type obj(0);
167  if (! (m_cont->find(sid, obj) ) ) {
168  std::ostringstream ost;
169  m_cont->list(ost);
170  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
171  msg << MSG::ERROR
172  << "ReadMetaHandle::retrieve() could not find SourceID "
173  << sid << " for key " << m_hkey.objKey() << "\n"
174  << ost.str()
175  << endmsg;
176  return nullptr;
177  }
178 
179  return obj;
180  }
181 
182  //---------------------------------------------------------------------------
183 
184  template <typename T>
185  bool
187 
188  return initMetaHandle();
189  }
190 
191 }
192 
193 #endif
194 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SG::ReadMetaHandle::const_reference_type
const T & const_reference_type
Definition: ReadMetaHandle.h:33
SG::ReadMetaHandle::reference_type
T & reference_type
Definition: ReadMetaHandle.h:32
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:402
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::ReadMetaHandleKey
Definition: ReadMetaHandleKey.h:17
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
SG::ReadMetaHandle::operator->
const_pointer_type operator->()
Definition: ReadMetaHandle.h:49
ExtendedEventContext.h
SG::ReadMetaHandle::m_sid
SG::SourceID m_sid
Definition: ReadMetaHandle.h:58
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ReadMetaHandleKey.h
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
SG::ReadMetaHandle::initMetaHandle
bool initMetaHandle()
Definition: ReadMetaHandle.h:117
MetaContBase
Definition: MetaCont.h:24
Atlas::getExtendedEventContext
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
Definition: ExtendedEventContext.cxx:32
AthUnlikelyMacros.h
SG::ReadMetaHandle::const_pointer_type
const T * const_pointer_type
Definition: ReadMetaHandle.h:31
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:124
MetaCont
Definition: MetaCont.h:48
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
SG::ReadMetaHandle::pointer_type
T * pointer_type
Definition: ReadMetaHandle.h:30
SG::ReadMetaHandle::isValid
bool isValid()
Definition: ReadMetaHandle.h:186
SG::ReadMetaHandle::~ReadMetaHandle
~ReadMetaHandle()
Definition: ReadMetaHandle.h:44
SG::ReadMetaHandle::m_cont
const MetaCont< T > * m_cont
Definition: ReadMetaHandle.h:59
SG::ReadMetaHandle::retrieve
const_pointer_type retrieve()
Definition: ReadMetaHandle.h:144
SG::ReadMetaHandle
Definition: MetaCont.h:21
SG::ReadMetaHandle::ReadMetaHandle
ReadMetaHandle(const SG::ReadMetaHandleKey< T > &key)
Definition: ReadMetaHandle.h:69
SG::SourceID
std::string SourceID
Definition: AthenaKernel/AthenaKernel/SourceID.h:25
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
SG::ReadMetaHandle::ReadMetaHandle
ReadMetaHandle(SG::ReadMetaHandleKey< T > &&key, const EventContext &ctx)=delete
SG::ReadMetaHandle::ReadMetaHandle
ReadMetaHandle(SG::ReadMetaHandleKey< T > &&key)=delete
ReadHandle.h
Handle class for reading from StoreGate.
SG::ReadMetaHandle::m_hkey
const SG::ReadMetaHandleKey< T > & m_hkey
Definition: ReadMetaHandle.h:62
SG::ReadMetaHandle::operator*
const_pointer_type operator*()
Definition: ReadMetaHandle.h:50
python.PyAthena.obj
obj
Definition: PyAthena.py:132
SG::ReadMetaHandle::m_ent
T * m_ent
Definition: ReadMetaHandle.h:60
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
MetaCont.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Atlas
Definition: ExtendedEventContext.h:21
python.SystemOfUnits.ms
float ms
Definition: SystemOfUnits.py:147