ATLAS Offline Software
WriteCondHandle.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_WRITECONDHANDLE_H
6 #define STOREGATE_WRITECONDHANDLE_H 1
7 
10 
11 #include "StoreGate/StoreGateSvc.h"
14 
15 #include "GaudiKernel/ServiceHandle.h"
16 #include "GaudiKernel/DataHandle.h"
17 #include "GaudiKernel/DataObjID.h"
18 #include "GaudiKernel/EventIDBase.h"
19 
20 #include <string>
21 #include <stdexcept>
22 
23 namespace SG {
24 
25  template <typename T>
27 
28  public:
29  typedef T* pointer_type; // FIXME: better handling of
30  typedef const T* const_pointer_type; // qualified T type ?
31  typedef T& reference_type;
32  typedef const T& const_reference_type;
33 
34  public:
36  WriteCondHandle(const WriteCondHandleKey<T>& key, const EventContext& ctx);
37 
39 
40  const std::string& key() const { return m_hkey.key(); }
41  const DataObjID& fullKey() const { return m_hkey.fullKey(); }
42 
43  bool isValid() const;
44  bool isValid(const EventIDBase& t) const;
45 
46  bool isValid(EventIDRange& range) const;
47  bool isValid(const EventIDBase& t, EventIDRange& range) const;
48 
49  void addDependency(const EventIDRange& range);
50 
51  template <typename R>
53 
54  template <typename R, typename... Args>
56 
57 
58 
64  StatusCode record(const EventIDRange& range, T* t);
65  StatusCode record(const EventIDRange& range, std::unique_ptr<T> t);
66 
71  StatusCode record(std::unique_ptr<T> t);
72  StatusCode record(T* t);
73 
84  StatusCode extendLastRange(const EventIDRange& range,
85  const EventContext& ctx = Gaudi::Hive::currentContext());
86 
87  const std::string& dbKey() const { return m_hkey.dbKey(); }
88 
89  const EventIDRange& getRange() const { return m_range; }
90 
91  private:
92 
93  const EventContext& m_ctx;
94  CondCont<T>* m_cc {nullptr};
95 
97 
98  EventIDRange m_range{};
99  bool m_rangeSet {false};
100  };
101 
102  //---------------------------------------------------------------------------
103 
104  template <typename T>
106  WriteCondHandle( key, Gaudi::Hive::currentContext() ) {}
107 
108  //---------------------------------------------------------------------------
109 
110  template <typename T>
112  const EventContext& ctx) :
113  m_ctx(ctx),
114  m_cc( key.getCC() ),
115  m_hkey(key)
116  {
117  if (m_cc == 0) {
118  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
119  msg << MSG::ERROR
120  << "WriteCondHandle : ptr to CondCont<T> is zero"
121  << endmsg;
122  }
123 
124  if (! m_hkey.isInit()) {
125  throw SG::ExcUninitKey (key.clid(), key.key(), key.storeHandle().name(),
126  "", "WriteCond");
127  }
128 
129  }
130 
131 
132  //---------------------------------------------------------------------------
133 
134  template <typename T>
135  StatusCode
136  WriteCondHandle<T>::record(const EventIDRange& r, std::unique_ptr<T> t)
137  {
138 
139  if (m_rangeSet) {
140  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
141  msg << MSG::ERROR
142  << "WriteCondHandle::record(EventIDRange, T*): for key "
143  << this->fullKey()
144  << " cannot use this method if range has already been set via dependencies"
145  << endmsg;
146  return StatusCode::FAILURE;
147  }
148 
149  m_range = r;
150  m_rangeSet = true;
151 
152  return record( std::move(t) );
153  }
154 
155  template <typename T>
156  StatusCode
157  WriteCondHandle<T>::record(const EventIDRange& r, T* t)
158  {
159  return record (r, std::unique_ptr<T> (t));
160  }
161 
162  template <typename T>
163  StatusCode
165  return record (std::unique_ptr<T> (t));
166  }
167 
168  template <typename T>
169  StatusCode
170  WriteCondHandle<T>::record(std::unique_ptr<T> t) {
171  if (! m_rangeSet) {
172  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
173  msg << MSG::ERROR
174  << "WriteCondHandle::record() : no range defined for key "
175  << this->fullKey()
176  << endmsg;
177  return StatusCode::FAILURE;
178  }
179 
180  #ifndef NDEBUG
181  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
182  if (msg.level() <= MSG::DEBUG) {
183  msg << MSG::DEBUG
184  << "WriteCondHandle::record() : obj at: " << t.get() << " range: "
185  << m_range << endmsg;
186  }
187  #endif
188 
189  StatusCode sc = m_cc->insert(m_range, std::move(t));
190  // Preserve sc for return, since it may be DUPLICATE.
191  if (sc.isFailure()) {
192  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
193  msg << MSG::ERROR
194  << "WriteCondHandle::record() : unable to insert obj in CondCont<T>"
195  << endmsg;
196  return StatusCode::FAILURE;
197  } else if (CondContBase::Category::isOverlap (sc)) {
198 #if 0
199  // Temporarily disable this check until caching issues with IOVDbSvc
200  // are sorted out.
201  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
202  msg << MSG::ERROR
203  << "WriteCondHandle::record() : IOV ranges overlap."
204  << endmsg;
205  return StatusCode::FAILURE;
206 #endif
207  sc = StatusCode::SUCCESS;
208  }
209 
210  return sc;
211  }
212 
213  //---------------------------------------------------------------------------
214 
215 
226  template <typename T>
227  StatusCode
228  WriteCondHandle<T>::extendLastRange (const EventIDRange& r, const EventContext& ctx)
229  {
230  return m_cc->extendLastRange (r, ctx);
231  }
232 
233 
234  //---------------------------------------------------------------------------
235 
236  template <typename T>
237  bool
238  WriteCondHandle<T>::isValid(const EventIDBase& t) const {
239 
240  return (m_cc->valid(t));
241  }
242 
243 
244  //---------------------------------------------------------------------------
245 
246  template <typename T>
247  bool
249 
250  return (m_cc->valid(m_ctx.eventID()));
251  }
252 
253  //---------------------------------------------------------------------------
254 
255  template <typename T>
256  bool
257  WriteCondHandle<T>::isValid(const EventIDBase& t, EventIDRange& range) const {
258 
259  return (m_cc->range(t, range));
260  }
261 
262 
263  //---------------------------------------------------------------------------
264 
265  template <typename T>
266  bool
267  WriteCondHandle<T>::isValid (EventIDRange& range) const {
268 
269  return (m_cc->range(m_ctx.eventID(), range));
270  }
271 
272  //---------------------------------------------------------------------------
273 
274  template <typename T>
275  void WriteCondHandle<T>::addDependency(const EventIDRange& range) {
276  if ( !m_rangeSet ) {
277  m_range = range;
278  } else {
280  }
281  m_rangeSet = true;
282  using KeyType = CondContBase::KeyType;
283  if (m_cc->keyType()==KeyType::RUNLBN && (range.start().isTimeStamp() || range.stop().isTimeStamp())) {
284  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
285  msg << MSG::ERROR << "Adding a time-stamp dependency on a run-lumi indexed CondCont. Consider a mixed ConditionsContainer for type " << fullKey() << endmsg;
286  }
287  if (m_cc->keyType()==KeyType::TIMESTAMP && (range.start().isRunLumi() || range.stop().isRunLumi())) {
288  MsgStream msg(Athena::getMessageSvc(), "WriteCondHandle");
289  msg << MSG::ERROR << "Adding a run-lumi dependency on a timestamp-indexed CondCont. Consider a mixed ConditionsContainer for type " << fullKey() << endmsg;
290  }
291  }
292 
293  // Can't take a const RCH, as RCH.range() can load the ptr.
294  template <typename T>
295  template< typename R>
296  void
298  CondContBase* dep_cc = rch.getCC();
299  dep_cc->addDep (m_cc);
300  return addDependency(rch.getRange());
301  }
302 
303  template< typename T>
304  template <typename R, typename... Args>
305  void
307  addDependency( rch );
308  return addDependency( args... );
309  }
310 
311  // helper methods to create a read cond handle from the corresponding key.
312  template <class T>
314  const EventContext& ctx = Gaudi::Hive::currentContext()) {
315  return SG::WriteCondHandle<T>(key, ctx);
316  }
317 
318 }
319 
320 #endif
321 
beamspotman.r
def r
Definition: beamspotman.py:676
SG::WriteCondHandle::pointer_type
T * pointer_type
Definition: WriteCondHandle.h:29
SG::WriteCondHandle::const_reference_type
const T & const_reference_type
Definition: WriteCondHandle.h:32
SG::ReadCondHandle::getRange
const EventIDRange & getRange()
Definition: ReadCondHandle.h:242
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
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::WriteCondHandle::getRange
const EventIDRange & getRange() const
Definition: WriteCondHandle.h:89
WriteCondHandleKey.h
SG::WriteCondHandle::reference_type
T & reference_type
Definition: WriteCondHandle.h:31
CondContBase::KeyType
KeyType
Type of key used for this container.
Definition: CondCont.h:180
CondContBase::addDep
void addDep(CondContBase *dep)
Declare another conditions container that depends on this one.
Definition: CondCont.cxx:696
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Args
Definition: test_lwtnn_fastgraph.cxx:12
SG::WriteCondHandle::record
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
Definition: WriteCondHandle.h:157
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
m_range
float m_range[NbCaloPart][2]
Definition: CellClusterLinkTool.h:55
ReadCondHandle.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
SG::WriteCondHandle::~WriteCondHandle
~WriteCondHandle()
Definition: WriteCondHandle.h:38
SG::WriteCondHandle::m_range
EventIDRange m_range
Definition: WriteCondHandle.h:98
SG::WriteCondHandle::m_ctx
const EventContext & m_ctx
Definition: WriteCondHandle.h:93
SG::WriteCondHandle::fullKey
const DataObjID & fullKey() const
Definition: WriteCondHandle.h:41
SG::WriteCondHandle::m_cc
CondCont< T > * m_cc
Definition: WriteCondHandle.h:94
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CondContBase
Base class for all conditions containers.
Definition: CondCont.h:140
SG::WriteCondHandle::dbKey
const std::string & dbKey() const
Definition: WriteCondHandle.h:87
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
SG::WriteCondHandle::const_pointer_type
const T * const_pointer_type
Definition: WriteCondHandle.h:30
SG::WriteCondHandle::m_rangeSet
bool m_rangeSet
Definition: WriteCondHandle.h:99
SG::WriteCondHandle::key
const std::string & key() const
Definition: WriteCondHandle.h:40
SG::WriteCondHandle::WriteCondHandle
WriteCondHandle(const WriteCondHandleKey< T > &key)
Definition: WriteCondHandle.h:105
CondContBase::Category::isOverlap
static bool isOverlap(code_t code)
Helper to test whether a code is OVERLAP.
Definition: CondCont.cxx:242
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
Amg::intersect
std::optional< double > intersect(const AmgVector(N)&posA, const AmgVector(N)&dirA, const AmgVector(N)&posB, const AmgVector(N)&dirB)
Calculates the point B' along the line B that's closest to a second line A.
Definition: GeoPrimitivesHelpers.h:347
DEBUG
#define DEBUG
Definition: page_access.h:11
SG::WriteCondHandle::isValid
bool isValid() const
Definition: WriteCondHandle.h:248
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
SG::WriteCondHandle::extendLastRange
StatusCode extendLastRange(const EventIDRange &range, const EventContext &ctx=Gaudi::Hive::currentContext())
Extend the range of the last IOV.
Definition: WriteCondHandle.h:228
SG::WriteCondHandleKey
Definition: WriteCondHandleKey.h:20
SG::WriteCondHandle::m_hkey
const SG::WriteCondHandleKey< T > & m_hkey
Definition: WriteCondHandle.h:96
StoreGateSvc.h
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
python.CaloScaleNoiseConfig.args
args
Definition: CaloScaleNoiseConfig.py:80
SG::WriteCondHandle::addDependency
void addDependency(const EventIDRange &range)
Definition: WriteCondHandle.h:275
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37