ATLAS Offline Software
CondSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "CondSvc.h"
6 #include "GaudiKernel/EventIDBase.h"
7 #include "AthenaKernel/StoreID.h"
9 
10 
11 //---------------------------------------------------------------------------
12 
13 CondSvc::CondSvc( const std::string& name, ISvcLocator* svcLoc ):
14  base_class(name,svcLoc),
15  m_sgs("StoreGateSvc/ConditionStore", name)
16 {
17 }
18 
19 //---------------------------------------------------------------------------
20 
22 CondSvc::validRanges( std::vector< EventIDRange >& ranges, const DataObjID& id ) const
23 {
24  // Retrieve all conditions data and search
26  StatusCode sc = m_sgs->retrieve( cib, cie );
27  if ( sc.isSuccess() ) {
28  while ( cib != cie ) {
29  if ( cib->id() == id ) {
30  ranges = cib->ranges();
31  }
32  ++cib;
33  }
34  }
35 
36  return sc;
37 }
38 
39 //---------------------------------------------------------------------------
40 
43 
44  ATH_CHECK( m_sgs.retrieve() );
45 
46  return StatusCode::SUCCESS;
47 
48 }
49 
50 //---------------------------------------------------------------------------
51 
52 void
53 CondSvc::dump(std::ostream& ost) const {
54 
55  std::scoped_lock lock(m_lock);
56 
57  ost << "CondSvc::dump()";
58 
59  ost << "\ndumping id->alg map\n";
60  for (const auto& [id, alg] : m_idMap) {
61  ost << "\n + " << id << " : " << alg->name();
62  }
63 
64  ost << "\n\ndumping ConditionStore:\n\n";
65 
67  if (m_sgs->retrieve(cib,cie).isSuccess()) {
68  while (cib != cie) {
69  ost << " + ";
70  cib->list(ost);
71  ++cib;
72  }
73  }
74 
75  ost << "\n";
76 
77 }
78 
79 //---------------------------------------------------------------------------
80 
83 {
84  // Call this now, in case there is no CondInputLoader.
85  ATH_CHECK( setupDone() );
86  return StatusCode::SUCCESS;
87 }
88 
89 
92 
93  ATH_MSG_DEBUG( "CondSvc::stop()" );
94 
95  if (msgLvl(MSG::DEBUG)) {
96  std::ostringstream ost;
97  dump(ost);
98 
99  ATH_MSG_DEBUG( ost.str() );
100  }
101 
102  return StatusCode::SUCCESS;
103 
104 }
105 
106 //---------------------------------------------------------------------------
107 
108 StatusCode
109 CondSvc::regHandle(IAlgorithm* alg, const Gaudi::DataHandle& dh) {
110 
111  std::scoped_lock lock(m_lock);
112  return regHandle_i(alg, dh);
113 
114 }
115 
116 //---------------------------------------------------------------------------
117 
118 // separate implementation to avoid the use of a recursive mutex
119 StatusCode
120 CondSvc::regHandle_i(IAlgorithm* alg, const Gaudi::DataHandle& dh) {
121 
122  ATH_MSG_DEBUG( "regHandle: alg: " << alg->name() << " id: "
123  << dh.fullKey() );
124 
125  if (dh.mode() != Gaudi::DataHandle::Writer) {
126  ATH_MSG_DEBUG(dh.fullKey() << " is a ReadHandle. No need to register.");
127  return StatusCode::SUCCESS;
128  }
129 
130  const auto [itr, success] = m_idMap.try_emplace(dh.fullKey(), alg);
131  if (!success) {
132  const IAlgorithm *ia = itr->second;
133  if (ia->name() != alg->name()) {
134  ATH_MSG_ERROR("WriteCondHandle " << dh.fullKey()
135  << " is already registered against a different Algorithm "
136  << ia->name()
137  << ". This is not allowed.");
138  return StatusCode::FAILURE;
139  }
140  }
141 
142  m_condAlgs.emplace(alg);
143  m_condIDs.emplace( dh.fullKey() );
144 
145  StatusCode sc{StatusCode::SUCCESS};
146 
147  const CLID clid = dh.fullKey().clid();
148  const SG::BaseInfoBase* bib = SG::BaseInfoBase::find( clid );
149  if ( bib ) {
150  for (CLID clid2 : bib->get_bases()) {
151  if (clid2 != clid) {
152  SG::VarHandleKey vhk(clid2,dh.objKey(),Gaudi::DataHandle::Writer,
154  sc &= regHandle_i(alg, vhk);
155  }
156  }
157  }
158 
159  return sc;
160 }
161 
162 
163 //---------------------------------------------------------------------------
164 
165 bool
166 CondSvc::isValidID(const EventContext& ctx, const DataObjID& id) const {
167  // Don't take out the lock here.
168  // In many-thread jobs, a lock here becomes heavily contended.
169  // The only potential conflict is with setupDone(),
170  // which should only be called during START.
171 
172  auto it = m_condConts.find (id);
173  if (it != m_condConts.end()) {
174  const bool valid = it->second->valid (ctx.eventID());
175  ATH_MSG_VERBOSE("CondSvc::isValidID: now: " << ctx.eventID() << " id : "
176  << id << (valid ? ": T" : ": F") );
177  return valid;
178  }
179  else {
180  ATH_MSG_ERROR( "Cannot find CondCont " << id );
181  }
182 
183  return false;
184 
185 }
186 
187 //---------------------------------------------------------------------------
188 
196 {
197  std::scoped_lock lock(m_lock);
198 
199  // CondHandleKeys always carry the store prefix. Prepend it to the raw key name.
200  const std::string& storePrefix = StoreID::storeName(StoreID::CONDITION_STORE) + "+";
201 
203  if (m_sgs->retrieve(cib,cie).isSuccess()) {
204  while(cib != cie) {
205  // CLID of CondCont payload
206  CLID clid = cib->id().clid();
207  const SG::BaseInfoBase* bib = SG::BaseInfoBase::find(clid);
208  if ( !bib ) {
209  // If no bases, register type
210  m_condConts.try_emplace( DataObjID(clid, storePrefix + cib.key()), &*cib );
211  }
212  else {
213  // Otherwise register all bases (which includes itself)
214  for (CLID clid2 : bib->get_bases()) {
215  m_condConts.try_emplace( DataObjID(clid2, storePrefix + cib.key()), &*cib );
216  }
217  }
218  ++cib;
219  }
220  }
221 
222  return StatusCode::SUCCESS;
223 }
trigbs_pickEvents.ranges
ranges
Definition: trigbs_pickEvents.py:60
CondSvc::initialize
virtual StatusCode initialize() override
Definition: CondSvc.cxx:42
CondSvc::m_sgs
ServiceHandle< StoreGateSvc > m_sgs
Definition: CondSvc.h:70
CondSvc.h
CondSvc::isValidID
virtual bool isValidID(const EventContext &, const DataObjID &) const override
Definition: CondSvc.cxx:166
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
CondCont.h
Hold mappings of ranges to condition objects.
CondSvc::m_condConts
std::unordered_map< DataObjID, const CondContBase *, DataObjID_Hasher > m_condConts
Map from DataObjID to CondContBase (populated in setupDone)
Definition: CondSvc.h:79
SG::detail::IteratorBase::key
const std::string & key() const
Get the key string with which the current object was stored.
Definition: SGIterator.cxx:155
CondSvc::regHandle
virtual StatusCode regHandle(IAlgorithm *alg, const Gaudi::DataHandle &id) override
Definition: CondSvc.cxx:109
skel.it
it
Definition: skel.GENtoEVGEN.py:407
CondSvc::dump
virtual void dump(std::ostream &) const override
Definition: CondSvc.cxx:53
CondSvc::m_lock
std::mutex m_lock
Definition: CondSvc.h:81
python.RatesEmulationExample.lock
lock
Definition: RatesEmulationExample.py:148
PyPoolBrowser.dh
dh
Definition: PyPoolBrowser.py:102
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
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
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
CondSvc::validRanges
virtual StatusCode validRanges(std::vector< EventIDRange > &ranges, const DataObjID &id) const override
Definition: CondSvc.cxx:22
StoreID::CONDITION_STORE
@ CONDITION_STORE
Definition: StoreID.h:28
calibdata.valid
list valid
Definition: calibdata.py:44
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
BaseInfo.h
Provide an interface for finding inheritance information at run time.
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
CondSvc::CondSvc
CondSvc(const std::string &name, ISvcLocator *svc)
Definition: CondSvc.cxx:13
CondSvc::m_idMap
std::unordered_map< DataObjID, IAlgorithm *, DataObjID_Hasher > m_idMap
Map from DataObjID to Algorithm to avoid duplicates.
Definition: CondSvc.h:76
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
CondSvc::m_condIDs
DataObjIDColl m_condIDs
Definition: CondSvc.h:73
StoreID.h
CondSvc::stop
virtual StatusCode stop() override
Definition: CondSvc.cxx:91
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
SG::BaseInfoBase::find
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition: BaseInfo.cxx:570
RegSelToolConfig.alg
alg
Definition: RegSelToolConfig.py:332
CondSvc::m_condAlgs
std::set< IAlgorithm * > m_condAlgs
Definition: CondSvc.h:72
CondSvc::regHandle_i
StatusCode regHandle_i(IAlgorithm *alg, const Gaudi::DataHandle &id)
Definition: CondSvc.cxx:120
SG::VarHandleKey
A property holding a SG store/key/clid from which a VarHandle is made.
Definition: StoreGate/StoreGate/VarHandleKey.h:62
DEBUG
#define DEBUG
Definition: page_access.h:11
SG::BaseInfoBase
The non-template portion of the BaseInfo implementation.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:451
CondSvc::start
virtual StatusCode start() override
Definition: CondSvc.cxx:82
LHEF::Writer
Pythia8::Writer Writer
Definition: Prophecy4fMerger.cxx:12
SG::ConstIterator
Definition: SGIterator.h:164
StoreID::storeName
static const std::string & storeName(const StoreID::type &s)
Definition: StoreID.cxx:77
CondSvc::setupDone
virtual StatusCode setupDone() override
To be called after changes to the set of conditions containers in the conditions store.
Definition: CondSvc.cxx:195