Back to home page

EIC code displayed by LXR

 
 

    


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

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/Printout.h>
0016 #include <DD4hep/ConditionsMap.h>
0017 #include <DD4hep/detail/ConditionsInterna.h>
0018 
0019 using namespace dd4hep;
0020 
0021 /// Interface to partially scan data content of the conditions mapping
0022 void ConditionsMap::scan(DetElement   detector,
0023                          Condition::itemkey_type lower,
0024                          Condition::itemkey_type upper,
0025                          const Condition::Processor&   processor) const
0026 {
0027   /// Helper to implement partial scans.
0028   /*
0029    *  \author  M.Frank
0030    *  \version 1.0
0031    *  \ingroup DD4HEP_CONDITIONS
0032    */
0033   struct Scanner : public Condition::Processor   {
0034     Condition::key_type lower, upper;
0035     const Condition::Processor& processor;
0036     /// Constructor
0037     Scanner(Condition::key_type low, Condition::key_type up, const Condition::Processor& p)
0038       : lower(low), upper(up), processor(p) {}
0039     /// Conditions callback for object processing
0040     virtual int process(Condition c)  const override  {
0041       Condition::key_type h = c->hash;
0042       if ( h >= lower && h <= upper )
0043         return processor(c);
0044       return 0;
0045     }
0046   };
0047   if ( detector.isValid() )   {
0048     Condition::detkey_type det_key = detector.key();
0049     Condition::key_type low = ConditionKey::KeyMaker(det_key,lower).hash;
0050     Condition::key_type up  = ConditionKey::KeyMaker(det_key,upper).hash;
0051     Scanner  scn(low,up,processor);
0052     this->scan(scn);
0053     return;
0054   }
0055   dd4hep::except("ConditionsMap","Cannot scan conditions map for conditions of an invalid detector element!");
0056 }
0057 
0058 /// No ConditionsMap overload: Access all conditions within a key range in the interval [lower,upper]
0059 std::vector<Condition> ConditionsMap::get(DetElement detector,
0060                                           Condition::itemkey_type lower,
0061                                           Condition::itemkey_type upper)  const   {
0062   /// Helper to implement partial scans.
0063   /*
0064    *  \author  M.Frank
0065    *  \version 1.0
0066    *  \ingroup DD4HEP_CONDITIONS
0067    */
0068   struct Scanner : public Condition::Processor   {
0069     Condition::key_type lower, upper;
0070     std::vector<Condition>& result;
0071     /// Constructor
0072     Scanner(Condition::key_type low, Condition::key_type up, std::vector<Condition>& r) : lower(low), upper(up), result(r) {}
0073     /// Conditions callback for object processing
0074     virtual int process(Condition c)  const override  {
0075       Condition::key_type h = c->hash;
0076       if ( h >= lower && h <= upper )   {
0077         result.emplace_back(c);
0078         return 1;
0079       }
0080       return 0;
0081     }
0082   };
0083   std::vector<Condition> result;
0084   if ( detector.isValid() )   {
0085     Condition::detkey_type det_key = detector.key();
0086     Condition::key_type low = ConditionKey::KeyMaker(det_key,lower).hash;
0087     Condition::key_type up  = ConditionKey::KeyMaker(det_key,upper).hash;
0088     Scanner  scn(low,up,result);
0089     this->scan(scn);
0090     return result;
0091   }
0092   dd4hep::except("ConditionsMap","Cannot scan conditions map for conditions of an invalid detector element!");
0093   return result;
0094 }
0095 
0096 /// Insert a new entry to the map
0097 template <typename T>
0098 bool ConditionsMapping<T>::insert(DetElement detector, Condition::itemkey_type key, Condition condition)   {
0099   auto res = data.emplace(ConditionKey(detector,key).hash,condition);
0100   return res.second;
0101 }
0102 
0103 /// Interface to access conditions by hash value
0104 template <typename T>
0105 Condition ConditionsMapping<T>::get(DetElement detector, Condition::itemkey_type key) const   {
0106   auto res = data.find(ConditionKey(detector,key).hash);
0107   return (res == data.end()) ? Condition() : res->second;
0108 }
0109 
0110 /// Interface to scan data content of the conditions mapping
0111 template <typename T>
0112 void ConditionsMapping<T>::scan(const Condition::Processor& processor) const  {
0113   for( const auto& i : data )
0114     processor(i);
0115 }
0116 
0117 /// Interface to partially scan data content of the conditions mapping
0118 template <typename T>
0119 void ConditionsMapping<T>::scan(DetElement   detector,
0120                                 Condition::itemkey_type lower,
0121                                 Condition::itemkey_type upper,
0122                                 const Condition::Processor&   processor) const   {
0123   if ( detector.isValid() )   {
0124     Condition::detkey_type det_key = detector.key();
0125     Condition::key_type low = ConditionKey::KeyMaker(det_key,lower).hash;
0126     Condition::key_type up  = ConditionKey::KeyMaker(det_key,upper).hash;
0127     typename T::const_iterator first = data.lower_bound(low);
0128     for(; first != data.end() && (*first).first <= up; ++first )
0129       processor((*first).second);
0130     return;
0131   }
0132   dd4hep::except("ConditionsMap","Cannot scan conditions map for conditions of an invalid detector element!");
0133 }
0134 
0135 
0136 /// Namespace for the AIDA detector description toolkit
0137 namespace dd4hep {
0138 
0139   /// Specialization: Insert a new entry to the map
0140   template <>
0141   bool ConditionsMapping<std::multimap<Condition::key_type,Condition> >
0142   ::insert(DetElement detector, Condition::itemkey_type key, Condition condition)   {
0143     data.emplace(ConditionKey(detector,key).hash,condition);
0144     return true;
0145   }
0146 
0147   /// Specialization: Interface to partially scan data content of the conditions mapping
0148   template <>
0149   void ConditionsMapping<std::unordered_map<Condition::key_type,Condition> >
0150   ::scan(DetElement detector, Condition::itemkey_type lower,
0151          Condition::itemkey_type upper,
0152          const Condition::Processor& proc) const
0153   {
0154     this->ConditionsMap::scan(detector, lower, upper, proc);
0155   }
0156 
0157 
0158   template class ConditionsMapping<std::map<Condition::key_type,Condition> >;
0159   template class ConditionsMapping<std::multimap<Condition::key_type,Condition> >;
0160   template class ConditionsMapping<std::unordered_map<Condition::key_type,Condition> >;
0161 }         /* End namespace dd4hep                   */