DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsManager.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Detector.h>
16 #include <DD4hep/Errors.h>
17 #include <DD4hep/Printout.h>
18 #include <DD4hep/InstanceCount.h>
19 #include <DD4hep/detail/Handle.inl>
20 #include <DD4hep/PluginCreators.h>
21 
25 
26 using namespace dd4hep::cond;
27 
29 
31 namespace dd4hep {
32 
34  namespace cond {
35 
37  template <> ConditionsManager ConditionsManager::from<Detector>(Detector& host) {
38  Object* obj = host.extension<Object>();
39  if ( obj ) return ConditionsManager(obj);
40  except("ConditionsManager","+++ Failed to access installed manager from Detector.");
41  return ConditionsManager();
42  }
43  }
44 }
45 
48  : NamedObject(), m_detDesc(ref_description)
49 {
51  declareProperty("LoadConditions", m_doLoad);
52  declareProperty("OutputUnloadedConditions", m_doOutputUnloaded);
53 }
54 
57  m_onRegister.clear();
58  m_onRemove.clear();
60 }
61 
62 void ConditionsManagerObject::registerCallee(Listeners& listeners, const Listener& callee, bool add) {
63  if ( add ) {
64  listeners.insert(callee);
65  return;
66  }
67  Listeners::iterator i=listeners.find(callee);
68  if ( i != listeners.end() ) listeners.erase(i);
69 }
70 
72 void ConditionsManagerObject::callOnRegister(const Listener& callee, bool add) {
73  registerCallee(m_onRegister, callee, add);
74 }
75 
77 void ConditionsManagerObject::callOnRemove(const Listener& callee, bool add) {
78  registerCallee(m_onRemove, callee, add);
79 }
80 
83  for(const auto& listener : m_onRegister )
84  listener.first->onRegisterCondition(condition, listener.second);
85 }
86 
89  for(const auto& listener : m_onRemove )
90  listener.first->onRegisterCondition(condition, listener.second);
91 }
92 
94 const std::vector<const dd4hep::IOVType*> ConditionsManagerObject::iovTypesUsed() const {
95  std::vector<const IOVType*> result;
96  const auto& types = this->iovTypes();
97  for ( const auto& i : types ) {
98  if ( i.type != IOVType::UNKNOWN_IOV ) result.emplace_back(&i);
99  }
100  return result;
101 }
102 
104 void ConditionsManagerObject::fromString(const std::string& data, IOV& iov) {
105  size_t id1 = data.find(',');
106  size_t id2 = data.find('#');
107  if ( id2 == std::string::npos ) {
108  except("ConditionsManager","+++ Unknown IOV string representation: %s",data.c_str());
109  }
110  std::string iov_name = data.substr(id2+1);
111  IOV::Key key;
112  int nents = 0;
114  long k1 = 0, k2 = 0;
115  if ( id1 != std::string::npos ) {
116  nents = ::sscanf(data.c_str(),"%ld,%ld#",&k1,&k2) == 2 ? 2 : 0;
117  key.second = k2;
118  key.first = k1;
119  }
120  else {
121  nents = ::sscanf(data.c_str(),"%ld#",&k1) == 1 ? 1 : 0;
122  key.second = key.first = k1;
123  }
124  if ( nents == 0 ) {
125  except("ConditionsManager",
126  "+++ Failed to read keys from IOV string representation: %s",data.c_str());
127  }
128 
129  // Check if this IOV type is known
130  const IOVType* typ = iovType(iov_name);
131  if ( !typ ) {
132  // Severe: We have an unknown IOV type. This is not allowed,
133  // because we do not known hot to handle it.....
134  except("ConditionsManager","+++ Unknown IOV type requested from data: %s. [%s]",
135  data.c_str(),Errors::invalidArg().c_str());
136  }
137  iov.type = typ->type;
138  iov.iovType = typ;
139  iov.set(key);
140 }
141 
144  IOV iov(0);
145  // Convert string to IOV
146  fromString(data, iov);
147  // IOV read and checked. Now register it.
148  // The validity of iov->iovType is already ensured in 'fromString'
149  return registerIOV(*iov.iovType, iov.keyData);
150 }
151 
153 ConditionsManager::ConditionsManager(Detector& description, const std::string& factory) {
154  ConditionsManagerObject* obj = createPlugin<ConditionsManagerObject>(factory,description);
155  if ( !obj ) {
156  except("ConditionsManagerInstaller","Failed to create manager object of type %s",
157  factory.c_str());
158  }
159  assign(obj, "ConditionsManager",factory);
160 }
161 
164  assign(ConditionsManager::from(description).ptr(), "ConditionsManager","");
165 }
166 
168  access()->initialize();
169  return *this;
170 }
171 
174  return access()->detectorDescription();
175 }
176 
179  return access()->properties();
180 }
181 
183 dd4hep::Property& ConditionsManager::operator[](const std::string& property_name) const {
184  return access()->properties().property(property_name);
185 }
186 
189  return *(access()->loader());
190 }
191 
193 std::pair<bool, const dd4hep::IOVType*>
194 ConditionsManager::registerIOVType(size_t iov_type, const std::string& iov_name) const {
195  return access()->registerIOVType(iov_type, iov_name);
196 }
197 
199 const dd4hep::IOVType* ConditionsManager::iovType (const std::string& iov_name) const {
200  return access()->iovType(iov_name);
201 }
202 
205  return access()->iovPool(iov_type);
206 }
207 
209 const std::vector<const dd4hep::IOVType*> ConditionsManager::iovTypesUsed() const {
210  Object* obj = access();
211  std::vector<const IOVType*> result;
212  const auto& types = obj->iovTypes();
213  result.reserve(types.size());
214  for(const auto& i : types )
215  if ( i.type != IOVType::UNKNOWN_IOV ) result.emplace_back(&i);
216  return result;
217 }
218 
220 ConditionsPool* ConditionsManager::registerIOV(const std::string& iov_rep) const {
221  IOV iov(0);
222  Object* o = access();
223  o->fromString(iov_rep, iov);
224  if ( iov.iovType )
225  return o->registerIOV(*iov.iovType, iov.key());
226  except("ConditionsManager","Invalid IOV type registration requested by IOV:%s",iov_rep.c_str());
227  return 0;
228 }
229 
232  if ( iov.iovType )
233  return access()->registerIOV(*iov.iovType, iov.key());
234  except("ConditionsManager","+++ Attempt to register invalid IOV [FAILED]");
235  return 0;
236 }
237 
240  return access()->registerIOV(typ, key);
241 }
242 
244 void ConditionsManager::fromString(const std::string& iov_str, IOV& iov) const {
245  access()->fromString(iov_str, iov);
246 }
247 
249 std::size_t ConditionsManager::blockRegister(ConditionsPool& pool, const std::vector<Condition>& cond) const {
250  return access()->blockRegister(pool, cond);
251 }
252 
255  return access()->registerUnlocked(pool, cond);
256 }
257 
260 }
261 
264  access()->adoptCleanup(cleaner);
265 }
266 
268 void ConditionsManager::clean(const IOVType* typ, int max_age) const {
269  access()->clean(typ, max_age);
270 }
271 
273 void ConditionsManager::clean(const ConditionsCleanup& cleaner) const {
274  access()->clean(cleaner);
275 }
276 
279  access()->clear();
280 }
281 
283 std::unique_ptr<UserPool> ConditionsManager::createUserPool(const IOVType* iovT) const {
284  return access()->createUserPool(iovT);
285 }
286 
290  return access()->prepare(req_iov, slice, ctx);
291 }
292 
296  return access()->load(req_iov, slice, ctx);
297 }
298 
302  return access()->compute(req_iov, slice, ctx);
303 }
dd4hep::cond::ConditionsManager::iovType
const IOVType * iovType(const std::string &iov_name) const
Access IOV by its name.
Definition: ConditionsManager.cpp:199
dd4hep::cond::ConditionsManager::loader
ConditionsDataLoader & loader() const
Access the conditions loader.
Definition: ConditionsManager.cpp:188
dd4hep::cond::ConditionsManager::registerIOV
ConditionsPool * registerIOV(const IOV &iov) const
Register IOV with type and key.
Definition: ConditionsManager.cpp:231
dd4hep::cond::ConditionsManager::adoptCleanup
void adoptCleanup(ConditionsCleanup *cleaner) const
Adopt cleanup handler. If a handler is registered, it is invoked at every "prepare" step.
Definition: ConditionsManager.cpp:263
cond
AlignmentCondition::Object * cond
Definition: AlignmentsCalculator.cpp:68
dd4hep::PropertyConfigurable::declareProperty
void declareProperty(const std::string &nam, T &val)
Declare property.
Definition: ComponentProperties.h:288
dd4hep::cond::ConditionsManagerObject::iovPool
virtual ConditionsIOVPool * iovPool(const IOVType &type) const =0
Access conditions multi IOV pool by iov type.
dd4hep::Property
The property class to assign options to actions.
Definition: ComponentProperties.h:48
dd4hep::cond::ConditionsManagerObject::registerUnlocked
virtual bool registerUnlocked(ConditionsPool &pool, Condition cond)=0
Register new condition with the conditions store. Unlocked version, not multi-threaded.
dd4hep::cond::ConditionsManagerObject::load
virtual Result load(const IOV &required_validity, ConditionsSlice &slice, ConditionUpdateUserContext *ctxt=0)=0
Load all updates to the clients with the defined IOV (1rst step of prepare)
Detector.h
dd4hep::cond::ConditionsManagerObject::~ConditionsManagerObject
virtual ~ConditionsManagerObject()
Default destructor.
Definition: ConditionsManager.cpp:56
dd4hep::cond::ConditionsManagerObject::createUserPool
virtual std::unique_ptr< UserPool > createUserPool(const IOVType *iovT) const =0
Create empty user pool object.
dd4hep::cond::ConditionsManager::clear
void clear() const
Full cleanup of all managed conditions.
Definition: ConditionsManager.cpp:278
dd4hep::cond::ConditionsManager::detectorDescription
Detector & detectorDescription() const
Access the detector description.
Definition: ConditionsManager.cpp:173
dd4hep::cond::ConditionsManagerObject::iovTypes
virtual const std::vector< IOVType > & iovTypes() const =0
Access IOV by its type.
dd4hep::IOVType::UNKNOWN_IOV
static constexpr unsigned int UNKNOWN_IOV
Definition: IOV.h:39
dd4hep::cond::ConditionsManagerObject::iovType
virtual const IOVType * iovType(size_t iov_type) const =0
Access IOV by its type.
dd4hep::cond::ConditionsManager::operator[]
Property & operator[](const std::string &property_name) const
Access to properties.
Definition: ConditionsManager.cpp:183
dd4hep::IOV::Key
std::pair< Key_value_type, Key_value_type > Key
Definition: IOV.h:74
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
ConditionsManager.h
dd4hep::cond::ConditionsManagerObject::registerIOVType
virtual std::pair< bool, const IOVType * > registerIOVType(size_t iov_type, const std::string &iov_name)=0
Register new IOV type if it does not (yet) exist.
dd4hep::cond::ConditionUpdateUserContext
ConditionUpdateUserContext class used by the derived conditions calculation mechanism.
Definition: ConditionDerived.h:49
dd4hep::cond::ConditionsManagerObject::m_onRegister
Listeners m_onRegister
Conditions listeners on registration of new conditions.
Definition: ConditionsManagerObject.h:66
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:37
dd4hep::cond::ConditionsManagerObject::adoptCleanup
virtual void adoptCleanup(ConditionsCleanup *cleaner)=0
Adopt cleanup handler. If a handler is registered, it is invoked at every "prepare" step.
dd4hep::cond::ConditionsManager::iovTypesUsed
const std::vector< const IOVType * > iovTypesUsed() const
Access the used/registered IOV types.
Definition: ConditionsManager.cpp:209
DD4HEP_INSTANTIATE_HANDLE_NAMED
DD4HEP_INSTANTIATE_HANDLE_NAMED(ConditionsManagerObject)
dd4hep::cond::ConditionsManagerObject::fromString
void fromString(const std::string &iov_str, IOV &iov)
Create IOV from string.
Definition: ConditionsManager.cpp:104
dd4hep::Errors::invalidArg
std::string invalidArg()
System error string for EINVAL. Sets errno accordingly.
dd4hep::cond::ConditionsManagerObject
Basic conditions manager implementation.
Definition: ConditionsManagerObject.h:54
dd4hep::cond::ConditionsManager::iovPool
ConditionsIOVPool * iovPool(const IOVType &type) const
Access conditions multi IOV pool by iov type.
Definition: ConditionsManager.cpp:204
ConditionsManagerObject.h
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::cond::ConditionsManager::registerIOVType
std::pair< bool, const IOVType * > registerIOVType(size_t iov_type, const std::string &iov_name) const
Register new IOV type if it does not (yet) exist.
Definition: ConditionsManager.cpp:194
dd4hep::cond::ConditionsManager::from
static ConditionsManager from(T &host)
Static accessor if installed as an extension.
dd4hep::cond
Namespace for implementation details of the AIDA detector description toolkit.
Definition: ConditionsCleanup.h:23
dd4hep::IOV
Class describing the interval of validty.
Definition: IOV.h:67
dd4hep::cond::ConditionsManagerObject::detectorDescription
Detector & detectorDescription() const
Access to the detector description instance.
Definition: ConditionsManagerObject.h:88
dd4hep::cond::ConditionsManager::prepare
Result prepare(const IOV &required_validity, ConditionsSlice &slice, ConditionUpdateUserContext *ctxt=0) const
Prepare all updates to the clients with the defined IOV.
Definition: ConditionsManager.cpp:289
dd4hep::cond::ConditionsManager::pushUpdates
void pushUpdates() const
Push all pending updates to the conditions store.
Definition: ConditionsManager.cpp:259
dd4hep::cond::ConditionsManagerObject::blockRegister
virtual size_t blockRegister(ConditionsPool &pool, const std::vector< Condition > &cond) const =0
Register a whole block of conditions with identical IOV.
dd4hep::cond::ConditionsManagerObject::m_onRemove
Listeners m_onRemove
Conditions listeners on de-registration of new conditions.
Definition: ConditionsManagerObject.h:68
dd4hep::IOV::set
void set(const Key &value)
Set discrete IOV value.
Definition: IOV.cpp:69
dd4hep::cond::ConditionsManagerObject::iovTypesUsed
const std::vector< const IOVType * > iovTypesUsed() const
Access the used/registered IOV types.
Definition: ConditionsManager.cpp:94
dd4hep::cond::ConditionsManagerObject::initialize
virtual void initialize()=0
Initialize the object after having set the properties.
dd4hep::Handle< ConditionsManagerObject >::assign
void assign(Object *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
dd4hep::cond::ConditionsManagerObject::registerCallee
void registerCallee(Listeners &listeners, const Listener &callee, bool add)
Register callback listener object.
Definition: ConditionsManager.cpp:62
dd4hep::PropertyManager::property
const Property & property(const std::string &name) const
Access property by name (CONST)
Definition: ComponentProperties.cpp:139
dd4hep::cond::ConditionsPool
Class implementing the conditions collection for a given IOV type.
Definition: ConditionsPool.h:54
dd4hep::cond::ConditionsManager::registerUnlocked
bool registerUnlocked(ConditionsPool &pool, Condition cond) const
Register new condition with the conditions store. Unlocked version, not multi-threaded.
Definition: ConditionsManager.cpp:254
dd4hep::cond::ConditionsManagerObject::onRegister
void onRegister(Condition condition)
Listener invocation when a condition is registered to the cache.
Definition: ConditionsManager.cpp:82
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::cond::ConditionsManager::properties
PropertyManager & properties() const
Access to the property manager.
Definition: ConditionsManager.cpp:178
dd4hep::IOV::keyData
Key keyData
IOV key (if second==first, discrete, otherwise range)
Definition: IOV.h:82
dd4hep::cond::ConditionsManager::load
Result load(const IOV &required_validity, ConditionsSlice &slice, ConditionUpdateUserContext *ctxt=0) const
Load all updates to the clients with the defined IOV (1rst step of prepare)
Definition: ConditionsManager.cpp:295
dd4hep::cond::ConditionsManagerObject::prepare
virtual Result prepare(const IOV &req_iov, ConditionsSlice &slice, ConditionUpdateUserContext *ctx=0)=0
Prepare all updates to the clients with the defined IOV.
Errors.h
dd4hep::cond::ConditionsManagerObject::Listener
std::pair< ConditionsListener *, void * > Listener
Definition: ConditionsManagerObject.h:57
dd4hep::cond::ConditionsManagerObject::compute
virtual Result compute(const IOV &required_validity, ConditionsSlice &slice, ConditionUpdateUserContext *ctxt=0)=0
Compute all derived conditions with the defined IOV (2nd step of prepare)
ConditionsListener.h
dd4hep::IOV::key
Key key() const
Get the local key of the IOV.
Definition: IOV.h:115
dd4hep::cond::ConditionsManagerObject::registerIOV
ConditionsPool * registerIOV(const std::string &data)
Register IOV using new string data.
Definition: ConditionsManager.cpp:143
dd4hep::cond::ConditionsManager::compute
Result compute(const IOV &required_validity, ConditionsSlice &slice, ConditionUpdateUserContext *ctxt=0) const
Compute all derived conditions with the defined IOV (2nd step of prepare)
Definition: ConditionsManager.cpp:301
dd4hep::cond::ConditionsManager
Manager class for condition handles.
Definition: ConditionsManager.h:46
dd4hep::cond::ConditionsManagerObject::callOnRegister
void callOnRegister(const Listener &callee, bool add)
(Un)Registration of conditions listeners with callback when a new condition is registered
Definition: ConditionsManager.cpp:72
dd4hep::IOV::type
unsigned int type
IOV buffer type: Must be a bitmap!
Definition: IOV.h:86
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::cond::ConditionsManager::Result
Result of a prepare call to the conditions manager.
Definition: ConditionsManager.h:59
dd4hep::PropertyConfigurable::properties
virtual PropertyManager & properties() override
Access to the properties of the object.
Definition: ComponentProperties.h:269
dd4hep::cond::ConditionsDataLoader
Interface for a generic conditions loader.
Definition: ConditionsDataLoader.h:46
dd4hep::Handle< ConditionsManagerObject >::access
ConditionsManagerObject * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
dd4hep::cond::ConditionsCleanup
Base class to handle conditions cleanups.
Definition: ConditionsCleanup.h:32
dd4hep::PropertyManager
Manager to ease the handling of groups of properties.
Definition: ComponentProperties.h:174
dd4hep::Handle< ConditionsManagerObject >::ptr
ConditionsManagerObject * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::cond::ConditionsManager::initialize
ConditionsManager & initialize()
Initialize the object after having set the properties.
Definition: ConditionsManager.cpp:167
dd4hep::cond::ConditionsManagerObject::ConditionsManagerObject
ConditionsManagerObject(Detector &description)
Default constructor.
Definition: ConditionsManager.cpp:47
dd4hep::IOV::iovType
const IOVType * iovType
Reference to IOV type.
Definition: IOV.h:80
dd4hep::cond::ConditionsManager::clean
void clean(const ConditionsCleanup &cleaner) const
Invoke cache cleanup with user defined policy.
Definition: ConditionsManager.cpp:273
dd4hep::Detector::extension
IFACE * extension(bool alert=true) const
Access extension element by the type.
Definition: Detector.h:342
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::NamedObject
Implementation of a named object.
Definition: NamedObject.h:30
dd4hep::cond::ConditionsManagerObject::onRemove
void onRemove(Condition condition)
Listener invocation when a condition is deregistered from the cache.
Definition: ConditionsManager.cpp:88
dd4hep::cond::ConditionsManagerObject::Listeners
std::set< Listener > Listeners
Definition: ConditionsManagerObject.h:58
dd4hep::cond::ConditionsManagerObject::loader
ConditionsDataLoader * loader() const
Access to the data loader.
Definition: ConditionsManagerObject.h:91
dd4hep::cond::ConditionsManager::ConditionsManager
ConditionsManager()=default
Default constructor.
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::cond::ConditionsManager::createUserPool
std::unique_ptr< UserPool > createUserPool(const IOVType *iovT) const
Create empty user pool object.
Definition: ConditionsManager.cpp:283
dd4hep::cond::ConditionsManager::blockRegister
size_t blockRegister(ConditionsPool &pool, const std::vector< Condition > &cond) const
Register a whole block of conditions with identical IOV.
Definition: ConditionsManager.cpp:249
dd4hep::cond::ConditionsManager::fromString
void fromString(const std::string &iov_str, IOV &iov) const
Create IOV from string.
Definition: ConditionsManager.cpp:244
dd4hep::cond::ConditionsManagerObject::callOnRemove
void callOnRemove(const Listener &callee, bool add)
(Un)Registration of conditions listeners with callback when a condition is unregistered
Definition: ConditionsManager.cpp:77
InstanceCount.h
dd4hep::IOVType::type
unsigned int type
integer identifier used internally
Definition: IOV.h:41
dd4hep::cond::ConditionsIOVPool
Pool of conditions satisfying one IOV type (epoch, run, fill, etc)
Definition: ConditionsIOVPool.h:38
dd4hep::cond::ConditionsManagerObject::clean
virtual int clean(const IOVType *typ, int max_age)=0
Clean conditions, which are above the age limit.
Printout.h
PluginCreators.h
dd4hep::cond::ConditionsManagerObject::m_doLoad
bool m_doLoad
Property: Flag to indicate if items should be loaded (or not)
Definition: ConditionsManagerObject.h:72
dd4hep::cond::ConditionsSlice
Conditions slice object. Defines which conditions should be loaded by the ConditionsManager.
Definition: ConditionsSlice.h:53
dd4hep::cond::ConditionsManagerObject::clear
virtual std::pair< int, int > clear()=0
Full cleanup of all managed conditions.
dd4hep::cond::ConditionsManagerObject::m_doOutputUnloaded
bool m_doOutputUnloaded
Property: Flag to indicate if unloaded items should be saved to the slice (or not)
Definition: ConditionsManagerObject.h:74