ATLAS Offline Software
StoreGate/src/VarHandleKey.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
14 #include "GaudiKernel/DataHandle.h"
15 #include "GaudiKernel/ToStream.h"
16 
17 #include "StoreGate/VarHandleKey.h"
18 #include "StoreGate/exceptions.h"
19 #include "StoreGate/StoreGateSvc.h"
22 #include "AthenaKernel/StoreID.h"
23 #include <boost/tokenizer.hpp>
24 
25 #include <sstream>
26 
27 constexpr char const storeSeparator = '+';
28 
29 namespace SG {
30 
31 
49  const std::string& sgkey,
51  const std::string& storeName /*= "StoreGateSvc"*/,
52  bool isCond /*= false*/)
53  : Gaudi::DataHandle (DataObjID (clid, sgkey), isCond, a),
54  m_storeHandle (storeName, "VarHandleKey")
55 {
56  parseKey (sgkey, storeName);
57  m_isEventStore = (m_storeHandle.name() == StoreID::storeName(StoreID::EVENT_STORE) ||
58  m_storeHandle.name() == StoreID::storeName(StoreID::PILEUP_STORE));
59 }
60 
61 
74 VarHandleKey& VarHandleKey::operator= (const std::string& sgkey)
75 {
76  parseKey (sgkey, m_storeHandle.name());
77  return *this;
78 }
79 
80 
92 StatusCode VarHandleKey::assign (const std::string& sgkey)
93 {
94  try {
95  parseKey (sgkey, m_storeHandle.name());
96  } catch (SG::ExcBadHandleKey &e) {
97  std::cerr << "VarHandleKey::assign failure: " << e.what() << std::endl;
98  return StatusCode::FAILURE;
99  } catch (...) {
100  return StatusCode::FAILURE;
101  }
102  return StatusCode::SUCCESS;
103 }
104 
105 
114 StatusCode VarHandleKey::initialize (bool used /*= true*/)
115 {
116  if (!used) {
117  Gaudi::DataHandle::updateKey ( "" );
118  m_sgKey.clear();
119  m_hashedKey = 0;
120  return StatusCode::SUCCESS;
121  }
122 
123  // if (Gaudi::DataHandle::objKey() == "") {
124  if (key().empty()) {
125  REPORT_ERROR (StatusCode::FAILURE)
126  << "Cannot initialize a Read/Write/Update handle with a null key.";
127  return StatusCode::FAILURE;
128  }
129  // Don't do retrieve() here. That unconditionally fetches the pointer
130  // from the service manager, even if it's still valid, which it might
131  // be if this is a handle that was just created from a key.
132  if (!m_storeHandle.isValid()) {
133  REPORT_ERROR (StatusCode::FAILURE)
134  << "Cannot locate store: " << m_storeHandle.typeAndName();
135  return StatusCode::FAILURE;
136  }
137 
138  CLID this_clid = clid();
139  m_hashedKey = m_storeHandle->stringToKey (m_sgKey, this_clid);
140 
141  // Make sure we also register hashes for base classes at this point,
142  // to prevent collisions with transient keys.
143  const SG::BaseInfoBase* bib = SG::BaseInfoBase::find (this_clid);
144  if (bib) {
145  for (CLID base_clid : bib->get_bases()) {
146  if (base_clid != this_clid) {
147  m_storeHandle->stringToKey (m_sgKey, base_clid);
148  }
149  }
150  }
151 
152  return StatusCode::SUCCESS;
153 }
154 
155 
166 {
167  if (key().empty()) {
168  return StatusCode::SUCCESS;
169  }
170  return initialize (true);
171 }
172 
173 
178 {
179  return Gaudi::DataHandle::fullKey().clid();
180 }
181 
182 
186 void VarHandleKey::setKey(DataObjID /*key*/)
187 {
188  throw SG::ExcForbiddenMethod ("VarHandleKey::setKey");
189 }
190 
191 
195 void VarHandleKey::updateKey(std::string /*key*/)
196 {
197  throw SG::ExcForbiddenMethod ("VarHandleKey::updateKey");
198 }
199 
200 
217 void VarHandleKey::parseKey (const std::string& key,
218  const std::string& storeName)
219 {
220  m_hashedKey = 0;
221 
222  std::string sn;
223  // test if storeName has classname
224  std::string::size_type sp = storeName.find('/');
225  if (sp == std::string::npos) {
226  sn = storeName;
227  } else {
228  sn = storeName.substr(sp+1,storeName.length()-sp+1);
229  }
230 
231  if (key.empty()) {
232  this->updateHandle(sn);
233  Gaudi::DataHandle::updateKey("");
234  m_sgKey.clear();
235  return;
236  }
237 
238  // StoreName separator is "+"
239  sp = key.find(storeSeparator);
240  if(sp == std::string::npos) {
241  m_sgKey = key;
242  } else {
243  sn = key.substr(0,sp);
244  m_sgKey = key.substr(sp+1,key.length()-sp-1);
245  }
246 
247 
248  this->updateHandle(sn);
249 
250  StoreID::type st;
251  // would be nice if we could get the storeID from the storeHandle, but
252  // we can't be sure that the store has been created when the VHK is
253  // constructed.
254  //
255 
256  st = StoreID::findStoreID(sn);
257 
259  if (m_sgKey.find('/') != std::string::npos) {
260  throw SG::ExcBadHandleKey("key \"" + key
261  + "\": keys with \"/\" only allowed for "
263  + " - store is \""
264  + sn + "\"");
265  }
266  } else {
267  sp = m_sgKey.rfind('/');
268  if (sp != std::string::npos) {
269  if (sp == 0
270  && m_sgKey.size() == 1) {
271  // Replace '\' with blank key
272  m_sgKey.clear();
273  } else if ( sp == m_sgKey.length()-1) {
274  throw SG::ExcBadHandleKey("key \"" + key
275  + "\": must not end with a \"/\"");
276  }
277  }
278  }
279 
280  if (m_sgKey.empty()) {
281  Gaudi::DataHandle::updateKey("");
282  } else {
283  Gaudi::DataHandle::updateKey(sn + storeSeparator + m_sgKey);
284  }
285 
286 
287 }
288 
289 
294 void VarHandleKey::updateHandle (const std::string& name)
295 {
296  // Don't invalidate a stored pointer if the handle is already pointing
297  // at the desired service.
298  if (m_storeHandle.name() != name) {
299  m_hashedKey = 0;
300  m_storeHandle = ServiceHandle<IProxyDict>(name, "VarHandleKey");
303  }
304 }
305 
309 std::string VarHandleKey::pythonRepr() const
310 {
311  const std::string& className = fullKey().className().empty() ?
312  Gaudi::DataHandle::default_type : fullKey().className();
313 
314  std::ostringstream ost;
315  ost << "DataHandle(";
317  ost << ",";
318  switch (mode()) {
320  Gaudi::Utils::toStream("W", ost); break;
321  default:
322  Gaudi::Utils::toStream("R", ost); break;
323  }
325  ost << ","; Gaudi::Utils::toStream(isCondition(), ost);
326  ost << ")";
327 
328  return ost.str();
329 }
330 
331 
332 } // namespace SG
333 
334 namespace std {
335  ostream& operator<<(ostream& s, const SG::VarHandleKey& m) {
336  s << "'" << m.objKey() << "'";
337  return s;
338  }
339 }
used
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
common.sgkey
def sgkey(tool)
Definition: common.py:1028
REPORT_ERROR
#define REPORT_ERROR(SC)
Report an error.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:355
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
VarHandleKey.h
A property holding a SG store/key/clid from which a VarHandle is made.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
SG::VarHandleKey::m_isEventStore
bool m_isEventStore
Cache test for whether we're referencing the event store.
Definition: StoreGate/StoreGate/VarHandleKey.h:253
Gaudi::Utils::toStream
std::ostream & toStream(const SG::VarHandleKeyArray &v, std::ostream &o)
Gaudi function used to convert a property to a string.
Definition: StoreGate/src/VarHandleKeyArray.cxx:47
SG::VarHandleKey::m_storeHandle
ServiceHandle< IProxyDict > m_storeHandle
Handle to the referenced store.
Definition: StoreGate/StoreGate/VarHandleKey.h:244
SG::VarHandleKey::updateHandle
void updateHandle(const std::string &name)
Update the name of the store to which we're referring.
Definition: StoreGate/src/VarHandleKey.cxx:294
SG::VarHandleKey::m_hashedKey
SG::sgkey_t m_hashedKey
The hashed StoreGate key. May be 0 if not yet initialized.
Definition: StoreGate/StoreGate/VarHandleKey.h:250
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
SG::BaseInfoBase::get_bases
const std::vector< CLID > & get_bases() const
Return the class IDs of all known bases of T (that have class IDs).
Definition: BaseInfo.cxx:304
storeSeparator
constexpr char const storeSeparator
Definition: StoreGate/src/VarHandleKey.cxx:27
SG::VarHandleKey::VarHandleKey
VarHandleKey(CLID clid, const std::string &sgkey, Gaudi::DataHandle::Mode a, const std::string &storeName=StoreID::storeName(StoreID::EVENT_STORE), bool isCond=false)
Constructor.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:38
JetTagCalibConfig.className
string className
Definition: JetTagCalibConfig.py:36
SG::AllowEmptyEnum
AllowEmptyEnum
Definition: StoreGate/StoreGate/VarHandleKey.h:29
StoreID::CONDITION_STORE
@ CONDITION_STORE
Definition: StoreID.h:28
SG::VarHandleKey::pythonRepr
virtual std::string pythonRepr() const override
Python representation of Handle.
Definition: StoreGate/src/VarHandleKey.cxx:309
SG::VarHandleKey::setKey
virtual void setKey(DataObjID key) override final
Don't allow calling these.
Definition: StoreGate/src/VarHandleKey.cxx:186
StoreID::PILEUP_STORE
@ PILEUP_STORE
Definition: StoreID.h:31
exceptions.h
Exceptions that can be thrown from StoreGate.
SG::VarHandleKey::assign
virtual StatusCode assign(const std::string &sgkey)
Change the key of the object to which we're referring.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:88
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::ExcBadHandleKey
Exception — Bad key format for VarHandleKey.
Definition: Control/StoreGate/StoreGate/exceptions.h:62
Preparation.mode
mode
Definition: Preparation.py:94
StoreID::METADATA_STORE
@ METADATA_STORE
Definition: StoreID.h:29
SG::ExcForbiddenMethod
Exception — Forbidden method called.
Definition: Control/StoreGate/StoreGate/exceptions.h:77
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
SG::VarHandleKey::m_sgKey
std::string m_sgKey
StoreGate key, that doesn't include the storename.
Definition: StoreGate/StoreGate/VarHandleKey.h:247
StoreID.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SG::BaseInfoBase::find
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition: BaseInfo.cxx:569
SG::VarHandleKey::operator=
VarHandleKey & operator=(const std::string &sgkey)
Change the key of the object to which we're referring.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:68
errorcheck.h
Helpers for checking error return status codes and reporting errors.
EventContainers::Mode
Mode
Definition: IdentifiableContainerBase.h:13
operator<<
std::ostream & operator<<(std::ostream &lhs, const TestGaudiProperty &rhs)
Definition: TestGaudiProperty.cxx:69
SG::VarHandleKey::clid
CLID clid() const
Return the class ID for the referenced object.
Definition: StoreGate/src/VarHandleKey.cxx:177
SG::VarHandleKey
A property holding a SG store/key/clid from which a VarHandle is made.
Definition: StoreGate/StoreGate/VarHandleKey.h:62
SG::VarHandleKey::parseKey
void parseKey(const std::string &sgkey, const std::string &storeName)
Handle assignment/construction from a string key.
Definition: StoreGate/src/VarHandleKey.cxx:217
a
TList * a
Definition: liststreamerinfos.cxx:10
StoreID::type
type
Definition: StoreID.h:24
StoreID::findStoreID
static StoreID::type findStoreID(const std::string &storeName)
Definition: StoreID.cxx:21
SG::BaseInfoBase
The non-template portion of the BaseInfo implementation.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:451
LHEF::Writer
Pythia8::Writer Writer
Definition: Prophecy4fMerger.cxx:12
StoreID::EVENT_STORE
@ EVENT_STORE
Definition: StoreID.h:26
SG::VarHandleKey::updateKey
virtual void updateKey(std::string key) override final
Prevent this method from being called.
Definition: StoreGate/src/VarHandleKey.cxx:195
StoreGateSvc.h
StoreID::storeName
static const std::string & storeName(const StoreID::type &s)
Definition: StoreID.cxx:77
ServiceHandle< IProxyDict >
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37