ATLAS Offline Software
ReadCondHandle.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 
58 
59  const std::string& key() const { return m_hkey.key(); }
60  const DataObjID& fullKey() const { return m_hkey.fullKey(); }
61 
63  const_pointer_type retrieve( const EventIDBase& t);
64 
68 
69 
70  bool isValid();
71  bool isValid(const EventIDBase& t) const ;
72 
73  bool range(EventIDRange& r);
74  bool range(const EventIDBase& t, EventIDRange& r) const;
75  const EventIDRange& getRange();
76 
77  CondCont<T>* getCC() { return m_cc; }
78 
79  private:
80 
82 
83  EventIDBase m_eid;
84  CondCont<T>* m_cc {nullptr};
85  const T* m_obj { nullptr };
86  const EventIDRange* m_range { nullptr };
87 
89  };
90 
91 
92  //---------------------------------------------------------------------------
93 
94  template <typename T>
96  ReadCondHandle(key, Gaudi::Hive::currentContext())
97  {
98  // cppcheck-suppress missingReturn; false positive
99  }
100 
101  //---------------------------------------------------------------------------
102 
103  template <typename T>
105  const EventContext& ctx):
106  m_eid( ctx.eventID() ),
107  m_cc( key.getCC() ),
108  m_hkey(key)
109  {
110  try {
111  EventIDBase::number_type conditionsRun =
113  if (conditionsRun != EventIDBase::UNDEFNUM) {
114  m_eid.set_run_number (conditionsRun);
115  }
116  }
117  catch (const std::bad_any_cast& e) {
118  throw SG::ExcBadContext (ctx, key.objKey());
119  }
120 
121  if (ATH_UNLIKELY(!key.isInit())) {
122  throw SG::ExcUninitKey (key.clid(), key.key(), key.storeHandle().name(),
123  "", "ReadCond");
124  }
125 
126  if (ATH_UNLIKELY(m_cc == 0)) {
127  // try to retrieve it
128  StoreGateSvc* cs = m_hkey.getCS();
129  CondContBase *cb(nullptr);
130  if (cs->retrieve(cb, m_hkey.key()).isFailure()) {
131  throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't retrieve.");
132  } else {
133  m_cc = dynamic_cast< CondCont<T>* > (cb);
134  if (m_cc == 0) {
135  throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't dcast CondContBase.");
136  }
137  }
138  }
139  }
140 
141  //---------------------------------------------------------------------------
142 
143  template <typename T>
144  bool
146 
147  if (m_obj != 0) return true;
148 
149  if ( ATH_UNLIKELY(!m_cc->find(m_eid, m_obj, &m_range)) ) {
150  ReadCondHandleNotFound (*m_cc, m_eid, m_hkey.objKey());
151  m_obj = nullptr;
152  return false;
153  }
154 
155  return true;
156  }
157 
158  //---------------------------------------------------------------------------
159 
160  template <typename T>
161  const T*
163 
164  if (m_obj == 0) {
165  if (!initCondHandle()) {
166  // std::ostringstream ost;
167  // m_cc->list(ost);
168  // MsgStream msg(Athena::getMessageSvc(), "ReadCondHandle");
169  // msg << MSG::ERROR
170  // << "ReadCondHandle::retrieve() could not find EventTime "
171  // << m_eid << " for key " << objKey() << "\n"
172  // << ost.str()
173  // << endmsg;
174  return nullptr;
175  }
176  }
177 
178  return m_obj;
179  }
180 
181  //---------------------------------------------------------------------------
182 
183  template <typename T>
184  const T*
185  ReadCondHandle<T>::retrieve(const EventIDBase& eid) {
186  if (eid == m_eid) {
187  return retrieve();
188  }
189 
190  // pointer_type obj(0);
191  const_pointer_type cobj(0);
192  if (! (m_cc->find(eid, cobj) ) ) {
193  ReadCondHandleNotFound (*m_cc, eid, m_hkey.objKey());
194  return nullptr;
195  }
196 
197  // const_pointer_type cobj = const_cast<const_pointer_type>( obj );
198 
199  return cobj;
200  }
201 
202  //---------------------------------------------------------------------------
203 
204  template <typename T>
205  bool
207 
208  return initCondHandle();
209  }
210 
211  //---------------------------------------------------------------------------
212 
213  template <typename T>
214  bool
215  ReadCondHandle<T>::isValid(const EventIDBase& t) const {
216 
217  return (m_cc->valid(t));
218  }
219 
220  //---------------------------------------------------------------------------
221 
222  template <typename T>
223  bool
224  ReadCondHandle<T>::range(EventIDRange& r) {
225 
226  if (m_obj == 0) {
227  if (!initCondHandle()) {
228  return false;
229  }
230  }
231 
232  if (m_range) {
233  r = *m_range;
234  return true;
235  }
236 
237  return false;
238  }
239 
240  template <typename T>
241  const EventIDRange&
243 
244  if (m_obj == 0) {
245  if (!initCondHandle()) {
247  }
248  }
249 
250  if (!m_range) {
251  throw SG::ExcNoRange();
252  }
253  return *m_range;
254 
255  }
256 
257 
258  //---------------------------------------------------------------------------
259 
260  template <typename T>
261  bool
262  ReadCondHandle<T>::range(const EventIDBase& eid, EventIDRange& r) const {
263 
264  return ( m_cc->range(eid, r) );
265  }
266 
267 
268  // helper methods to create a read cond handle from the corresponding key.
269  template <class T>
271  const EventContext& ctx = Gaudi::Hive::currentContext()) {
272  return SG::ReadCondHandle<T>(key, ctx);
273  }
274 
275 }
276 
277 #endif
278 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SG::ReadCondHandle::m_range
const EventIDRange * m_range
Definition: ReadCondHandle.h:86
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
SG::ReadCondHandle::range
bool range(const EventIDBase &t, EventIDRange &r) const
Definition: ReadCondHandle.h:262
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:65
SG::ReadCondHandle::getRange
const EventIDRange & getRange()
Definition: ReadCondHandle.h:242
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::ReadCondHandle::fullKey
const DataObjID & fullKey() const
Definition: ReadCondHandle.h:60
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:77
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:57
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:206
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:224
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
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:270
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:88
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:125
SG::ReadCondHandle::retrieve
const_pointer_type retrieve()
Definition: ReadCondHandle.h:162
SG::ReadCondHandle::m_obj
const T * m_obj
Definition: ReadCondHandle.h:85
exceptions.h
Exceptions that can be thrown from StoreGate.
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(const SG::ReadCondHandleKey< T > &key)
Definition: ReadCondHandle.h:95
CondContBase
Base class for all conditions containers.
Definition: CondCont.h:140
SG::ReadCondHandle::operator*
const_pointer_type operator*()
Definition: ReadCondHandle.h:66
PixelCablingCondData
Definition: PixelCablingCondData.h:26
ReadCondHandleKey.h
SG::ReadCondHandle::isValid
bool isValid(const EventIDBase &t) const
Definition: ReadCondHandle.h:215
SG::ReadCondHandle::ReadCondHandle
ReadCondHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx)
Definition: ReadCondHandle.h:104
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:145
CondCont
Hold mapping of ranges to condition objects.
Definition: CondCont.h:811
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:83
SG::ReadCondHandle::m_cc
CondCont< T > * m_cc
Definition: ReadCondHandle.h:84
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
ReadHandle.h
Handle class for reading from StoreGate.
SG::ReadCondHandle::key
const std::string & key() const
Definition: ReadCondHandle.h:59
SG::ReadCondHandle::retrieve
const_pointer_type retrieve(const EventIDBase &t)
Definition: ReadCondHandle.h:185
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:67