Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:16:30

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Errors.h>
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/InstanceCount.h>
0019 #include <DD4hep/detail/Handle.inl>
0020 #include <DD4hep/PluginCreators.h>
0021 
0022 #include <DD4hep/ConditionsListener.h>
0023 #include <DDCond/ConditionsManager.h>
0024 #include <DDCond/ConditionsManagerObject.h>
0025 
0026 using namespace dd4hep::cond;
0027 
0028 DD4HEP_INSTANTIATE_HANDLE_NAMED(ConditionsManagerObject);
0029 
0030 /// Namespace for the AIDA detector description toolkit
0031 namespace dd4hep {
0032   
0033   /// Namespace for implementation details of the AIDA detector description toolkit
0034   namespace cond {
0035 
0036     /// Access specialization
0037     template <> ConditionsManager ConditionsManager::from<Detector>(Detector& host)  {
0038       Object* obj = host.extension<Object>();
0039       if ( obj ) return ConditionsManager(obj);
0040       except("ConditionsManager","+++ Failed to access installed manager from Detector.");
0041       return ConditionsManager();
0042     }
0043   }
0044 }
0045 
0046 /// Default constructor
0047 ConditionsManagerObject::ConditionsManagerObject(Detector& ref_description)
0048   : NamedObject(), m_detDesc(ref_description)
0049 {
0050   InstanceCount::increment(this);
0051   declareProperty("LoadConditions",           m_doLoad);
0052   declareProperty("OutputUnloadedConditions", m_doOutputUnloaded);
0053 }
0054 
0055 /// Default destructor
0056 ConditionsManagerObject::~ConditionsManagerObject()   {
0057   m_onRegister.clear();
0058   m_onRemove.clear();
0059   InstanceCount::decrement(this);
0060 }
0061 
0062 void ConditionsManagerObject::registerCallee(Listeners& listeners, const Listener& callee, bool add)  {
0063   if ( add )  {
0064     listeners.insert(callee);
0065     return;
0066   }
0067   Listeners::iterator i=listeners.find(callee);
0068   if ( i != listeners.end() ) listeners.erase(i);  
0069 }
0070 
0071 /// (Un)Registration of conditions listeners with callback when a new condition is registered
0072 void ConditionsManagerObject::callOnRegister(const Listener& callee, bool add)  {
0073   registerCallee(m_onRegister, callee, add);
0074 }
0075 
0076 /// (Un)Registration of conditions listeners with callback when a condition is de-registered
0077 void ConditionsManagerObject::callOnRemove(const Listener& callee, bool add)  {
0078   registerCallee(m_onRemove, callee, add);
0079 }
0080 
0081 /// Call this when a condition is registered to the cache
0082 void ConditionsManagerObject::onRegister(Condition condition)    {
0083   for(const auto& listener : m_onRegister )
0084     listener.first->onRegisterCondition(condition, listener.second);
0085 }
0086 
0087 /// Call this when a condition is deregistered from the cache
0088 void ConditionsManagerObject::onRemove(Condition condition)   {
0089   for(const auto& listener : m_onRemove )
0090     listener.first->onRegisterCondition(condition, listener.second);
0091 }
0092 
0093 /// Access the used/registered IOV types
0094 const std::vector<const dd4hep::IOVType*> ConditionsManagerObject::iovTypesUsed() const   {
0095   std::vector<const IOVType*> result;
0096   const auto& types = this->iovTypes();
0097   for ( const auto& i : types )  {
0098     if ( i.type != IOVType::UNKNOWN_IOV ) result.emplace_back(&i);
0099   }
0100   return result;
0101 }
0102 
0103 /// Create IOV from string
0104 void ConditionsManagerObject::fromString(const std::string& data, IOV& iov)   {
0105   size_t id1 = data.find(',');
0106   size_t id2 = data.find('#');
0107   if ( id2 == std::string::npos )  {
0108     except("ConditionsManager","+++ Unknown IOV string representation: %s",data.c_str());
0109   }
0110   std::string iov_name = data.substr(id2+1);
0111   IOV::Key key;
0112   int nents = 0;
0113   /// Need assignment from long (k1,k2) for compatibility with Apple MAC
0114   long k1 = 0, k2 = 0;
0115   if ( id1 != std::string::npos )   {
0116     nents = ::sscanf(data.c_str(),"%ld,%ld#",&k1,&k2) == 2 ? 2 : 0;
0117     key.second = k2;
0118     key.first = k1;
0119   }
0120   else  {
0121     nents = ::sscanf(data.c_str(),"%ld#",&k1) == 1 ? 1 : 0;
0122     key.second = key.first = k1;
0123   }
0124   if ( nents == 0 )   {
0125     except("ConditionsManager",
0126            "+++ Failed to read keys from IOV string representation: %s",data.c_str());
0127   }
0128 
0129   // Check if this IOV type is known
0130   const IOVType* typ = iovType(iov_name);
0131   if ( !typ )  {
0132     // Severe: We have an unknown IOV type. This is not allowed, 
0133     // because we do not known hot to handle it.....
0134     except("ConditionsManager","+++ Unknown IOV type requested from data: %s. [%s]",
0135            data.c_str(),Errors::invalidArg().c_str());
0136   }
0137   iov.type    = typ->type;
0138   iov.iovType = typ;
0139   iov.set(key);
0140 }
0141 
0142 /// Register IOV using new string data
0143 ConditionsPool* ConditionsManagerObject::registerIOV(const std::string& data)   {
0144   IOV iov(0);
0145   // Convert string to IOV
0146   fromString(data, iov);
0147   // IOV read and checked. Now register it.
0148   // The validity of iov->iovType is already ensured in 'fromString'
0149   return registerIOV(*iov.iovType, iov.keyData);
0150 }
0151 
0152 /// Initializing constructor to create a named manager
0153 ConditionsManager::ConditionsManager(Detector& description, const std::string& factory)   {
0154   ConditionsManagerObject* obj = createPlugin<ConditionsManagerObject>(factory,description);
0155   if ( !obj )  {
0156     except("ConditionsManagerInstaller","Failed to create manager object of type %s",
0157            factory.c_str());
0158   }
0159   assign(obj, "ConditionsManager",factory);
0160 }
0161 
0162 /// Default constructor
0163 ConditionsManager::ConditionsManager(Detector& description)  {
0164   assign(ConditionsManager::from(description).ptr(), "ConditionsManager","");
0165 }
0166 
0167 ConditionsManager& ConditionsManager::initialize()   {
0168   access()->initialize();
0169   return *this;
0170 }
0171 
0172 /// Access the detector description
0173 dd4hep::Detector& ConditionsManager::detectorDescription()  const   {
0174   return access()->detectorDescription();
0175 }
0176 
0177 /// Access to the property manager
0178 dd4hep::PropertyManager& ConditionsManager::properties()  const   {
0179   return access()->properties();
0180 }
0181 
0182 /// Access to properties
0183 dd4hep::Property& ConditionsManager::operator[](const std::string& property_name) const    {
0184   return access()->properties().property(property_name);
0185 }
0186 
0187 /// Access the conditions loader
0188 ConditionsDataLoader& ConditionsManager::loader()  const    {
0189   return *(access()->loader());
0190 }
0191 
0192 /// Register new IOV type if it does not (yet) exist.
0193 std::pair<bool, const dd4hep::IOVType*> 
0194 ConditionsManager::registerIOVType(size_t iov_type, const std::string& iov_name)  const  {
0195   return access()->registerIOVType(iov_type, iov_name);
0196 }
0197 
0198 /// Access IOV by its name
0199 const dd4hep::IOVType* ConditionsManager::iovType (const std::string& iov_name) const   {
0200   return access()->iovType(iov_name);
0201 }
0202 
0203 /// Access conditions multi IOV pool by iov type
0204 ConditionsIOVPool* ConditionsManager::iovPool(const IOVType& iov_type)  const {
0205   return access()->iovPool(iov_type);
0206 }
0207 
0208 /// Access the used/registered IOV types
0209 const std::vector<const dd4hep::IOVType*> ConditionsManager::iovTypesUsed() const  {
0210   Object* obj = access();
0211   std::vector<const IOVType*> result;
0212   const auto& types = obj->iovTypes();
0213   result.reserve(types.size());
0214   for(const auto& i : types )
0215     if ( i.type != IOVType::UNKNOWN_IOV ) result.emplace_back(&i);
0216   return result;
0217 }
0218 
0219 /// Register IOV with type and key
0220 ConditionsPool* ConditionsManager::registerIOV(const std::string& iov_rep)  const   {
0221   IOV iov(0);
0222   Object* o = access();
0223   o->fromString(iov_rep, iov);
0224   if ( iov.iovType )
0225     return o->registerIOV(*iov.iovType, iov.key());
0226   except("ConditionsManager","Invalid IOV type registration requested by IOV:%s",iov_rep.c_str());
0227   return 0;
0228 }
0229       
0230 /// Register IOV with type and key
0231 ConditionsPool* ConditionsManager::registerIOV(const IOV& iov) const   {
0232   if ( iov.iovType )
0233     return access()->registerIOV(*iov.iovType, iov.key());
0234   except("ConditionsManager","+++ Attempt to register invalid IOV [FAILED]");
0235   return 0;
0236 }
0237 
0238 /// Register IOV with type and key
0239 ConditionsPool* ConditionsManager::registerIOV(const IOVType& typ, IOV::Key key)   const {
0240   return access()->registerIOV(typ, key);
0241 }
0242 
0243 /// Create IOV from string
0244 void ConditionsManager::fromString(const std::string& iov_str, IOV& iov)  const  {
0245   access()->fromString(iov_str, iov);
0246 }
0247       
0248 /// Register a whole block of conditions with identical IOV.
0249 std::size_t ConditionsManager::blockRegister(ConditionsPool& pool, const std::vector<Condition>& cond) const   {
0250   return access()->blockRegister(pool, cond);
0251 }
0252 
0253 /// Register new condition with the conditions store. Unlocked version, not multi-threaded
0254 bool ConditionsManager::registerUnlocked(ConditionsPool& pool, Condition cond)  const  {
0255   return access()->registerUnlocked(pool, cond);
0256 }
0257 
0258 /// Push all pending updates to the conditions store. 
0259 void ConditionsManager::pushUpdates()  const {
0260 }
0261 
0262 /// Adopt cleanup handler. If a handler is registered, it is invoked at every "prepare" step
0263 void ConditionsManager::adoptCleanup(ConditionsCleanup* cleaner)  const   {
0264   access()->adoptCleanup(cleaner);
0265 }
0266 
0267 /// Clean conditions, which are above the age limit.
0268 void ConditionsManager::clean(const IOVType* typ, int max_age)  const  {
0269   access()->clean(typ, max_age);
0270 }
0271 
0272 /// Invoke cache cleanup with user defined policy
0273 void ConditionsManager::clean(const ConditionsCleanup& cleaner)  const   {
0274   access()->clean(cleaner);  
0275 }
0276 
0277 /// Full cleanup of all managed conditions.
0278 void ConditionsManager::clear()  const  {
0279   access()->clear();
0280 }
0281 
0282 /// Create empty user pool object
0283 std::unique_ptr<UserPool> ConditionsManager::createUserPool(const IOVType* iovT)  const   {
0284   return access()->createUserPool(iovT);
0285 }
0286 
0287 /// Prepare all updates to the clients with the defined IOV
0288 ConditionsManager::Result
0289 ConditionsManager::prepare(const IOV& req_iov, ConditionsSlice& slice, ConditionUpdateUserContext* ctx)  const  {
0290   return access()->prepare(req_iov, slice, ctx);
0291 }
0292 
0293 /// Load all updates to the clients with the defined IOV (1rst step of prepare)
0294 ConditionsManager::Result
0295 ConditionsManager::load(const IOV& req_iov, ConditionsSlice& slice, ConditionUpdateUserContext* ctx)  const  {
0296   return access()->load(req_iov, slice, ctx);
0297 }
0298 
0299 /// Compute all derived conditions with the defined IOV (2nd step of prepare)
0300 ConditionsManager::Result
0301 ConditionsManager::compute(const IOV& req_iov, ConditionsSlice& slice, ConditionUpdateUserContext* ctx)  const  {
0302   return access()->compute(req_iov, slice, ctx);
0303 }