ATLAS Offline Software
ReadMetaHandle.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 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 
41 
44 
47 
48  bool isValid();
49 
50  private:
51 
52  bool initMetaHandle();
53 
55  const MetaCont<T>* m_cont {nullptr};
56  T* m_ent {nullptr};
57 
59  };
60 
61 
62  //------------------------------------------------------------------------
63 
64  template <typename T>
66  : ReadMetaHandle(key, Gaudi::Hive::currentContext())
67  {
68  }
69 
70  //------------------------------------------------------------------------
71 
72  template <typename T>
74  , const EventContext& ctx)
75  : m_sid( Atlas::getExtendedEventContext(ctx).proxy()->sourceID() )
76  , m_cont( key.getContainer() )
77  , m_hkey(key)
78  {
79  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
80  if (ATH_UNLIKELY(!m_hkey.isInit())) {
81  msg << MSG::ERROR
82  << "ReadMetaHandleKey " << key.objKey() << " was not initialized"
83  << endmsg;
84  throw std::runtime_error("ReadMetaHandle: ReadMetaHandleKey was not initialized");
85  }
86 
87  if (ATH_UNLIKELY(m_cont == 0)) {
88  // Try to retrieve it
89  StoreGateSvc* ms = m_hkey.getStore();
90  MetaContBase* cb{nullptr};
91  if(ms->retrieve(cb, m_hkey.key()).isFailure()) {
92  msg << MSG::ERROR
93  << "can't retrieve " << m_hkey.fullKey()
94  << " via base class" << endmsg;
95  throw std::runtime_error("ReadCondHandle: ptr to CondCont<T> is zero");
96  }
97  else {
98  m_cont = dynamic_cast< MetaCont<T>* > (cb);
99  if(!m_cont) {
100  msg << MSG::ERROR
101  << "can't dcast MetaContBase to " << m_hkey.fullKey()
102  << endmsg;
103  throw std::runtime_error("ReadMetaHandle: ptr to MetaCont<T> is zero");
104  }
105  }
106  }
107  }
108 
109  //------------------------------------------------------------------------
110 
111  template <typename T>
112  bool
114  {
115 
116  if (m_ent) return true;
117 
118  // Initialize sid from dbKey found at ReadMetaHandleKey initialize
119 // m_sid = m_hkey.dbKey(); ???? Do I need this ????
120 
121  if ( ATH_UNLIKELY(! m_cont->find(m_sid, m_ent)) ) {
122  std::ostringstream ost;
123  m_cont->list(ost);
124  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
125  msg << MSG::ERROR
126  << "ReadMetaHandle:: could not find current SourceID "
127  << m_sid << " for key " << m_hkey.objKey() << "\n"
128  << ost.str() << endmsg;
129  m_ent = nullptr;
130  return false;
131  }
132 
133  return true;
134  }
135 
136  //------------------------------------------------------------------------
137 
138  template <typename T>
139  const T*
141  {
142 
143  if (m_ent == 0) {
144  if (!initMetaHandle()) {
145  return nullptr;
146  }
147  }
148 
149  return m_ent;
150  }
151 
152  //------------------------------------------------------------------------
153 
154  template <typename T>
155  const T*
157  {
158  if (sid == m_sid) {
159  return retrieve();
160  }
161 
162  pointer_type obj(0);
163  if (! (m_cont->find(sid, obj) ) ) {
164  std::ostringstream ost;
165  m_cont->list(ost);
166  MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
167  msg << MSG::ERROR
168  << "ReadMetaHandle::retrieve() could not find SourceID "
169  << sid << " for key " << m_hkey.objKey() << "\n"
170  << ost.str()
171  << endmsg;
172  return nullptr;
173  }
174 
175  return obj;
176  }
177 
178  //---------------------------------------------------------------------------
179 
180  template <typename T>
181  bool
183 
184  return initMetaHandle();
185  }
186 
187 }
188 
189 #endif
190 
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:392
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:45
ExtendedEventContext.h
SG::ReadMetaHandle::m_sid
SG::SourceID m_sid
Definition: ReadMetaHandle.h:54
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ReadMetaHandleKey.h
python.SystemOfUnits.ms
int ms
Definition: SystemOfUnits.py:132
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
SG::ReadMetaHandle::initMetaHandle
bool initMetaHandle()
Definition: ReadMetaHandle.h:113
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:125
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:182
SG::ReadMetaHandle::~ReadMetaHandle
~ReadMetaHandle()
Definition: ReadMetaHandle.h:40
SG::ReadMetaHandle::m_cont
const MetaCont< T > * m_cont
Definition: ReadMetaHandle.h:55
SG::ReadMetaHandle::retrieve
const_pointer_type retrieve()
Definition: ReadMetaHandle.h:140
SG::ReadMetaHandle
Definition: MetaCont.h:21
SG::ReadMetaHandle::ReadMetaHandle
ReadMetaHandle(const SG::ReadMetaHandleKey< T > &key)
Definition: ReadMetaHandle.h:65
SG::SourceID
std::string SourceID
Definition: AthenaKernel/AthenaKernel/SourceID.h:25
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
ReadHandle.h
Handle class for reading from StoreGate.
SG::ReadMetaHandle::m_hkey
const SG::ReadMetaHandleKey< T > & m_hkey
Definition: ReadMetaHandle.h:58
SG::ReadMetaHandle::operator*
const_pointer_type operator*()
Definition: ReadMetaHandle.h:46
python.PyAthena.obj
obj
Definition: PyAthena.py:132
SG::ReadMetaHandle::m_ent
T * m_ent
Definition: ReadMetaHandle.h:56
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
MetaCont.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Atlas
Definition: ExtendedEventContext.h:21