Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ReadCondHandle.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_READCONDHANDLE_H
6 #define STOREGATE_READCONDHANDLE_H 1
7 
10 #include "AthenaKernel/IOVEntryT.h"
12 
13 #include "StoreGate/ReadHandle.h"
15 #include "StoreGate/exceptions.h"
18 
19 #include "GaudiKernel/DataHandle.h"
20 #include "GaudiKernel/DataObjID.h"
21 #include "GaudiKernel/EventIDBase.h"
22 #include "GaudiKernel/EventContext.h"
23 #include "GaudiKernel/ThreadLocalContext.h"
24 
25 #include <string>
26 #include <stdexcept>
27 #include <any>
28 
29 
30 namespace SG {
31 
32 
40  const EventIDBase& eid,
41  const std::string& key);
42 
43  template <typename T>
45 
46  public:
47  typedef T* pointer_type; // FIXME: better handling of
48  typedef const T* const_pointer_type; // qualified T type ?
49  typedef T& reference_type;
50  typedef const T& const_reference_type;
51 
52  public:
55  const EventContext& ctx);
56 
57  ReadCondHandle(SG::ReadCondHandleKey<T>&& key) = delete; // Not allowed from a temporary.
59  const EventContext& ctx) = delete; // Not allowed from a temporary.
60 
62 
63  const std::string& key() const { return m_hkey.key(); }
64  const DataObjID& fullKey() const { return m_hkey.fullKey(); }
65 
67  const_pointer_type retrieve( const EventIDBase& t);
68 
72 
73 
74  bool isValid();
75  bool isValid(const EventIDBase& t) const ;
76 
77  bool range(EventIDRange& r);
78  bool range(const EventIDBase& t, EventIDRange& r) const;
79  const EventIDRange& getRange();
80 
81  CondCont<T>* getCC() { return m_cc; }
82 
83  private:
84 
86 
87  EventIDBase m_eid;
88  CondCont<T>* m_cc {nullptr};
89  const T* m_obj { nullptr };
90  const EventIDRange* m_range { nullptr };
91 
93  };
94 
95 
96  //---------------------------------------------------------------------------
97 
98  template <typename T>
100  ReadCondHandle(key, Gaudi::Hive::currentContext())
101  {
102  // cppcheck-suppress missingReturn; false positive
103  }
104 
105  //---------------------------------------------------------------------------
106 
107  template <typename T>
109  const EventContext& ctx):
110  m_eid( ctx.eventID() ),
111  m_cc( key.getCC() ),
112  m_hkey(key)
113  {
114  try {
115  EventIDBase::number_type conditionsRun =
117  if (conditionsRun != EventIDBase::UNDEFNUM) {
118  m_eid.set_run_number (conditionsRun);
119  }
120  }
121  catch (const std::bad_any_cast& e) {
122  throw SG::ExcBadContext (ctx, key.objKey());
123  }
124 
125  if (ATH_UNLIKELY(!key.isInit())) {
126  throw SG::ExcUninitKey (key.clid(), key.key(), key.storeHandle().name(),
127  "", "ReadCond");
128  }
129 
130  if (ATH_UNLIKELY(m_cc == 0)) {
131  // try to retrieve it
132  StoreGateSvc* cs = m_hkey.getCS();
133  CondContBase *cb(nullptr);
134  if (cs->retrieve(cb, m_hkey.key()).isFailure()) {
135  throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't retrieve.");
136  } else {
137  m_cc = dynamic_cast< CondCont<T>* > (cb);
138  if (m_cc == 0) {
139  throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't dcast CondContBase.");
140  }
141  }
142  }
143  }
144 
145  //---------------------------------------------------------------------------
146 
147  template <typename T>
148  bool
150 
151  if (m_obj != 0) return true;
152 
153  if ( ATH_UNLIKELY(!m_cc->find(m_eid, m_obj, &m_range)) ) {
154  ReadCondHandleNotFound (*m_cc, m_eid, m_hkey.objKey());
155  m_obj = nullptr;
156  return false;
157  }
158 
159  return true;
160  }
161 
162  //---------------------------------------------------------------------------
163 
164  template <typename T>
165  const T*
167 
168  if (m_obj == 0) {
169  if (!initCondHandle()) {
170  // std::ostringstream ost;
171  // m_cc->list(ost);
172  // MsgStream msg(Athena::getMessageSvc(), "ReadCondHandle");
173  // msg << MSG::ERROR
174  // << "ReadCondHandle::retrieve() could not find EventTime "
175  // << m_eid << " for key " << objKey() << "\n"
176  // << ost.str()
177  // << endmsg;
178  return nullptr;
179  }
180  }
181 
182  return m_obj;
183  }
184 
185  //---------------------------------------------------------------------------
186 
187  template <typename T>
188  const T*
189  ReadCondHandle<T>::retrieve(const EventIDBase& eid) {
190  if (eid == m_eid) {
191  return retrieve();
192  }
193 
194  // pointer_type obj(0);
195  const_pointer_type cobj(0);
196  if (! (m_cc->find(eid, cobj) ) ) {
197  ReadCondHandleNotFound (*m_cc, eid, m_hkey.objKey());
198  return nullptr;
199  }
200 
201  // const_pointer_type cobj = const_cast<const_pointer_type>( obj );
202 
203  return cobj;
204  }
205 
206  //---------------------------------------------------------------------------
207 
208  template <typename T>
209  bool
211 
212  return initCondHandle();
213  }
214 
215  //---------------------------------------------------------------------------
216 
217  template <typename T>
218  bool
219  ReadCondHandle<T>::isValid(const EventIDBase& t) const {
220 
221  return (m_cc->valid(t));
222  }
223 
224  //---------------------------------------------------------------------------
225 
226  template <typename T>
227  bool
228  ReadCondHandle<T>::range(EventIDRange& r) {
229 
230  if (m_obj == 0) {
231  if (!initCondHandle()) {
232  return false;
233  }
234  }
235 
236  if (m_range) {
237  r = *m_range;
238  return true;
239  }
240 
241  return false;
242  }
243 
244  template <typename T>
245  const EventIDRange&
247 
248  if (m_obj == 0) {
249  if (!initCondHandle()) {
251  }
252  }
253 
254  if (!m_range) {
255  throw SG::ExcNoRange();
256  }
257  return *m_range;
258 
259  }
260 
261 
262  //---------------------------------------------------------------------------
263 
264  template <typename T>
265  bool
266  ReadCondHandle<T>::range(const EventIDBase& eid, EventIDRange& r) const {
267 
268  return ( m_cc->range(eid, r) );
269  }
270 
271 
272  // helper methods to create a read cond handle from the corresponding key.
273  template <class T>
275  const EventContext& ctx = Gaudi::Hive::currentContext()) {
276  return SG::ReadCondHandle<T>(key, ctx);
277  }
278 
286  template <class T>
287  const T* get (const ReadCondHandleKey<T>& key,
288  const EventContext& ctx)
289  {
290  if (key.key().empty()) return nullptr;
291  try {
292  ReadCondHandle<T> h (key, ctx);
293  return h.cptr();
294  }
295  catch (SG::ExcNoCondCont&) {
296  return nullptr;
297  }
298  }
299 
300 
309  template <class T>
310  StatusCode get (const T*& ptr,
311  const ReadCondHandleKey<T>& key,
312  const EventContext& ctx)
313  {
314  ptr = get(key, ctx);
315  return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
316  }
317 
318 }
319 
320 #endif
321 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SG::ReadCondHandle::m_range
const EventIDRange * m_range
Definition: ReadCondHandle.h:90
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
SG::ReadCondHandle::range
bool range(const EventIDBase &t, EventIDRange &r) const
Definition: ReadCondHandle.h:266
beamspotman.r
def r
Definition: beamspotman.py:676
SG::ExcBadContext
Exception — Bad EventContext extension while building ReadCondHandle.
Definition: Control/StoreGate/StoreGate/exceptions.h:312
SG::ReadCondHandle::operator->
const_pointer_type operator->()
Definition: ReadCondHandle.h:69
SG::ReadCondHandle::getRange
const EventIDRange & getRange()
Definition: ReadCondHandle.h:246
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::ReadCondHandle::fullKey
const DataObjID & fullKey() const
Definition: ReadCondHandle.h:64
CondCont.h
Hold mappings of ranges to condition objects.
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
SG::ReadCondHandle::getCC
CondCont< T > * getCC()
Definition: ReadCondHandle.h:81
SG::ReadCondHandle::pointer_type
T * pointer_type
Definition: ReadCondHandle.h:47
Atlas::ExtendedEventContext::conditionsRun
EventIDBase::number_type conditionsRun() const
Definition: ExtendedEventContext.h:38
ExtendedEventContext.h
SG::ReadCondHandle::~ReadCondHandle
~ReadCondHandle()
Definition: ReadCondHandle.h:61
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:210
AtlasMcWeight::number_type
unsigned int number_type
Definition: AtlasMcWeight.h:20
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
SG::ReadCondHandle::range
bool range(EventIDRange &r)
Definition: ReadCondHandle.h:228
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::ExcBadReadCondHandleInit
Exception — ReadCondHandle didn't initialize in getRange().
Definition: Control/StoreGate/StoreGate/exceptions.h:346
SG::ReadCondHandleNotFound
void ReadCondHandleNotFound(const CondContBase &cc, const EventIDBase &eid, const std::string &key)
Report a conditions container lookup failure.
Definition: ReadCondHandle.cxx:23
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
SG::ExcNoRange
Exception — Range not set in ReadCondHandle::getRange().
Definition: Control/StoreGate/StoreGate/exceptions.h:360
SG::ExcNoCondCont
Exception — Can't retrieve CondCont from ReadCondHandle.
Definition: Control/StoreGate/StoreGate/exceptions.h:330
m_range
float m_range[NbCaloPart][2]
Definition: CellClusterLinkTool.h:55
StoreGateSvc::retrieve
StatusCode retrieve(const T *&ptr) const
Retrieve the default object into a const T*.
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:274
Atlas::getExtendedEventContext
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
Definition: ExtendedEventContext.cxx:32
AthUnlikelyMacros.h
SG::ReadCondHandle::m_hkey
const SG::ReadCondHandleKey< T > & m_hkey
Definition: ReadCondHandle.h:92
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(SG::ReadCondHandleKey< T > &&key)=delete
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:124
SG::ReadCondHandle::retrieve
const_pointer_type retrieve()
Definition: ReadCondHandle.h:166
SG::ReadCondHandle::m_obj
const T * m_obj
Definition: ReadCondHandle.h:89
exceptions.h
Exceptions that can be thrown from StoreGate.
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(const SG::ReadCondHandleKey< T > &key)
Definition: ReadCondHandle.h:99
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(SG::ReadCondHandleKey< T > &&key, const EventContext &ctx)=delete
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
extractSporadic.h
list h
Definition: extractSporadic.py:97
CondContBase
Base class for all conditions containers.
Definition: CondCont.h:140
SG::ReadCondHandle::operator*
const_pointer_type operator*()
Definition: ReadCondHandle.h:70
PixelCablingCondData
Definition: PixelCablingCondData.h:26
ReadCondHandleKey.h
SG::ReadCondHandle::isValid
bool isValid(const EventIDBase &t) const
Definition: ReadCondHandle.h:219
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx)
Definition: ReadCondHandle.h:108
AthenaAttributeList.h
An AttributeList represents a logical row of attributes in a metadata table. The name and type of eac...
SG::ReadCondHandleKey
Definition: ReadCondHandleKey.h:20
SG::ReadCondHandle::initCondHandle
bool initCondHandle()
Definition: ReadCondHandle.h:149
CondCont
Hold mapping of ranges to condition objects.
Definition: CondCont.h:811
h
SG::ExcUninitKey
Exception — Tried to create a handle from an uninitialized key.
Definition: Control/StoreGate/StoreGate/exceptions.h:111
SG::ReadCondHandle::const_reference_type
const T & const_reference_type
Definition: ReadCondHandle.h:50
SG::ReadCondHandle::reference_type
T & reference_type
Definition: ReadCondHandle.h:49
SG::ReadCondHandle::const_pointer_type
const T * const_pointer_type
Definition: ReadCondHandle.h:48
IOVEntryT.h
SG::ReadCondHandle::m_eid
EventIDBase m_eid
Definition: ReadCondHandle.h:87
SG::ReadCondHandle::m_cc
CondCont< T > * m_cc
Definition: ReadCondHandle.h:88
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
ReadHandle.h
Handle class for reading from StoreGate.
SG::ReadCondHandle::key
const std::string & key() const
Definition: ReadCondHandle.h:63
SG::ReadCondHandle::retrieve
const_pointer_type retrieve(const EventIDBase &t)
Definition: ReadCondHandle.h:189
python.handimod.cc
int cc
Definition: handimod.py:523
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
SG::ReadCondHandle::cptr
const_pointer_type cptr()
Definition: ReadCondHandle.h:71