ATLAS Offline Software
CondHandleKey.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "AthenaKernel/StoreID.h"
6 #include "GaudiKernel/System.h"
7 #include <typeinfo>
8 
9 namespace SG {
10 
11  template <class T>
12  CondHandleKey<T>::CondHandleKey(const std::string& key,
13  const std::string& dbKey,
14  Gaudi::DataHandle::Mode mode ) :
15  VarHandleKey(ClassID_traits<T>::ID(), key, mode,
16  StoreID::storeName(StoreID::CONDITION_STORE), true),
17  m_cs(StoreID::storeName(StoreID::CONDITION_STORE),"CondHandleKey"),
18  m_dbKey(dbKey)
19  {
20  CondCont<T>::registerBaseInit();
21  }
22 
23  //---------------------------------------------------------------------------
24 
25  template <class T>
26  StatusCode
27  CondHandleKey<T>::initialize(bool used /*= true*/) {
28  if (m_isInit) return StatusCode::SUCCESS;
29 
30 
31  if (VarHandleKey::initialize(used) != StatusCode::SUCCESS) {
32  return StatusCode::FAILURE;
33  }
34  if (empty()) return StatusCode::SUCCESS;
35 
36  if (!m_cs.isValid()) {
37  MsgStream msg(Athena::getMessageSvc(), "CondHandleKey");
38  msg << MSG::ERROR
39  << "CondHandleKey::initialize() :Unable to locate ConditionStore "
40  << m_cs.name()
41  << endmsg;
42  return StatusCode::FAILURE;
43  }
44 
45 
46  if (mode() == DataHandle::Writer) {
47  if (m_cs->contains< CondCont<T> > (SG::VarHandleKey::key()) ) {
48  MsgStream msg(Athena::getMessageSvc(), "CondHandleKey");
49  msg << MSG::ERROR
50  << StoreID::storeName( StoreID::CONDITION_STORE )
51  << " already contains a CondCont of type "
52  << Gaudi::DataHandle::fullKey()
53  << endmsg;
54  return StatusCode::FAILURE;
55  } else {
56  ServiceHandle<Athena::IRCUSvc> rcusvc ("Athena::RCUSvc", "CondHandleKey");
57  m_cc = new CondCont<T>(*rcusvc, Gaudi::DataHandle::fullKey());
58  if (m_cs->record(m_cc, SG::VarHandleKey::key()).isFailure()) {
59  MsgStream msg(Athena::getMessageSvc(), "CondHandleKey");
60  msg << MSG::ERROR
61  << "CondHandleKey::init(): unable to record empty CondCont of "
62  << Gaudi::DataHandle::fullKey() << " in "
63  << StoreID::storeName( StoreID::CONDITION_STORE )
64  << " with key " << SG::VarHandleKey::key() << endmsg;
65  delete m_cc;
66  m_cc = 0;
67  return StatusCode::FAILURE;
68  }
69  // std::cout << "recorded " << Gaudi::DataHandle::fullKey()
70  // << " with key "
71  // << SG::VarHandleKey::key() << std::endl;
72  }
73  } else {
74  // lets see if we get lucky and the Write alg already created the
75  // container we want
76  if (m_cs->contains< CondCont<T> > (SG::VarHandleKey::key()) ) {
77  if (m_cs->retrieve(m_cc, SG::VarHandleKey::key()).isFailure()) {
78  MsgStream msg(Athena::getMessageSvc(), "CondHandleKey");
79  msg << MSG::ERROR
80  << "CondHandleKey::init(): unable to retrieve CondCont of "
81  << Gaudi::DataHandle::fullKey() << " from "
82  << StoreID::storeName(StoreID::CONDITION_STORE)
83  << " with key " << SG::VarHandleKey::key()
84  << endmsg;
85  m_cc = 0;
86  return StatusCode::FAILURE;
87  }
88  }
89  }
90 
91 
92  m_isInit = true;
93 
94  return StatusCode::SUCCESS;
95 
96  }
97 
98  template <class T>
99  StatusCode
100  CondHandleKey<T>::initialize(AllowEmptyEnum) {
101  if (key().empty()) {
102  return StatusCode::SUCCESS;
103  }
104  return initialize (true);
105  }
106 //---------------------------------------------------------------------------
107 
108  template <class T>
109  StoreGateSvc*
110  CondHandleKey<T>::getCS() const {
111  if (!m_cs.isValid()) {
112  MsgStream msg(Athena::getMessageSvc(), "CondHandleKey");
113  msg << MSG::ERROR
114  << "CondHandleKey::getCS() : Unable to locate ConditionStore"
115  << endmsg;
116  return 0;
117  }
118 
119  return m_cs.get();
120  }
121 
122 
123 //---------------------------------------------------------------------------
124 
125 
126  /**
127  * @brief Called by the owning algorithm during the START transition.
128  *
129  * AthAlgorithm and friends will call this during START. This allows
130  * for extra initialization that we can't do during initialize(), such
131  * as retrieving a conditions container from the store.
132  */
133  template <class T>
134  StatusCode CondHandleKey<T>::start()
135  {
136  if (m_isInit && m_cc == nullptr && mode() == DataHandle::Reader) {
137  // Try again to retrieve the conditions container from the store.
138  // If this is a conditions object that is read by CondInputLoader,
139  // then we will not have found this during initialize(),
140  // as CondInputLoader only records the object during start().
141  m_cc = m_cs->tryRetrieve< CondCont<T> > (SG::VarHandleKey::key());
142  }
143  return StatusCode::SUCCESS;
144  }
145 
146 
147 }