Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:29:53

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 #ifndef DD4HEP_CONDITIONS_H
0014 #define DD4HEP_CONDITIONS_H
0015 
0016 // Framework include files
0017 #include <DD4hep/IOV.h>
0018 #include <DD4hep/Handle.h>
0019 #include <DD4hep/OpaqueData.h>
0020 
0021 // C/C++ include files
0022 #include <vector>
0023 
0024 /// Namespace for the AIDA detector description toolkit
0025 namespace dd4hep {
0026 
0027   // Forward declarations
0028   class BasicGrammar;
0029   class DetElement;
0030   class Detector;
0031 
0032   /// Conditions internal namespace
0033   namespace detail  {
0034     class ConditionObject;
0035   }
0036 
0037   /// Main condition object handle.
0038   /**
0039    *  This objects allows access to the data block and
0040    *  the interval of validity for a single condition.
0041    *
0042    *  Note:
0043    *  Conditions may be shared between several DetElement objects.
0044    *  Hence, the back-link to the DetElement structure cannot be
0045    *  set - it would be ambiguous.
0046    *
0047    *  \author  M.Frank
0048    *  \version 1.0
0049    *  \ingroup DD4HEP_CONDITIONS
0050    */
0051   class Condition: public Handle<detail::ConditionObject> {
0052   public:
0053     /// Forward definition of the key type
0054     typedef unsigned long long int key_type;
0055     /// High part of the key identifies the detector element
0056     typedef unsigned int           detkey_type;
0057     /// Low part of the key identifies the item identifier
0058     typedef unsigned int           itemkey_type;
0059     /// Forward definition of the object properties
0060     typedef unsigned int           mask_type;
0061 
0062   public:
0063     /// Flags to steer the conditions conversion to string
0064     enum StringFlags  {
0065       WITH_IOV            =  1<<0,
0066       WITH_ADDRESS        =  1<<1,
0067       WITH_TYPE           =  1<<2,
0068       WITH_COMMENT        =  1<<4,
0069       WITH_DATATYPE       =  1<<5,
0070       WITH_DATA           =  1<<6,
0071       NO_NAME             =  1<<20,
0072       NONE
0073     };
0074     /// Flags to indicate the conditions type ans state
0075     enum ConditionState {
0076       INACTIVE            =  0,
0077       ACTIVE              =  1<<0,
0078       CHECKED             =  1<<2,
0079       DERIVED             =  1<<3,
0080       ONSTACK             =  1<<4,
0081       // Flags for specific conditions
0082       TEMPERATURE         =  1<<5,
0083       TEMPERATURE_DERIVED =  1<<6|DERIVED,
0084       PRESSURE            =  1<<7,
0085       PRESSURE_DERIVED    =  1<<8|DERIVED,
0086       ALIGNMENT_DELTA     =  1<<9,
0087       ALIGNMENT_DERIVED   =  1<<10|DERIVED,
0088       // Keep bit 10-15 for other generic types
0089       // Bit 16-31 is reserved for user classifications
0090       USER_FLAGS_FIRST    =  1<<16,
0091       USER_FLAGS_LAST     =  1<<31
0092     };
0093     /// Flags to indicate conditions item ranges (low word of the conditions key)
0094     enum ConditionItemRangeKeys {
0095       FIRST_ITEM_KEY      =  0x0U,
0096       LAST_ITEM_KEY       = ~0x0U
0097     };
0098     /// Flags to indicate conditions detector ranges (high word of the conditions key)
0099     enum ConditionDetectorRangeKeys {
0100       FIRST_DET_KEY       =  0x0U,
0101       LAST_DET_KEY        = ~0x0U
0102     };
0103     /// Flags to indicate global conditions ranges
0104     static constexpr unsigned long long int FIRST_KEY =  0x0ULL, LAST_KEY  = ~0x0ULL;
0105 
0106     /// Abstract base for processing callbacks to conditions objects
0107     /**
0108      *  \author  M.Frank
0109      *  \version 1.0
0110      *  \ingroup DD4HEP_CONDITIONS
0111      */
0112     class Processor {
0113     protected:
0114       /// Move constructor
0115       Processor(Processor&& copy) = default;
0116       /// Copy constructor
0117       Processor(const Processor& copy) = default;
0118       /// Assignment operator
0119       Processor& operator=(const Processor& copy) = delete;
0120     public:
0121       /// Default constructor
0122       Processor() = default;
0123       /// Default destructor
0124       virtual ~Processor() = default;
0125       /// Processing callback
0126       virtual int process(Condition c)  const = 0;
0127       /// Conditions callback for object processing
0128       virtual int operator()(Condition c)  const
0129       {  return this->process(c);                   }
0130       /// Conditions callback for object processing in maps
0131       virtual int operator()(const std::pair<Condition::key_type,Condition>& e)  const
0132       {  return this->process(e.second);            }
0133     };
0134 
0135     /// Default constructor
0136     Condition() = default;
0137     /// Move constructor
0138     Condition(Condition&& c) = default;
0139     /// Copy constructor
0140     Condition(const Condition& c) = default;
0141     /// Initializing constructor
0142     Condition(Object* p);
0143     /// Constructor to be used when reading the already parsed object
0144     template <typename Q> Condition(const Handle<Q>& e);
0145     /// Initializing constructor for a pure, undecorated conditions object
0146     Condition(key_type hash_key);
0147     /// Initializing constructor for a pure, undecorated conditions object
0148     Condition(const std::string& name, const std::string& type);
0149     /// Initializing constructor for a pure, undecorated conditions object with payload buffer
0150     Condition(const std::string& name, const std::string& type, size_t memory);
0151     /// Assignment move operator
0152     Condition& operator=(Condition&& c) = default;
0153     /// Assignment copy operator
0154     Condition& operator=(const Condition& c) = default;
0155 
0156     /// Output method
0157     std::string str(int with_data=WITH_IOV|WITH_ADDRESS|WITH_DATATYPE)  const;
0158 
0159     /** Data block (bound type)         */
0160     /// Access the data type
0161     int dataType()  const;
0162 
0163     /** Interval of validity            */
0164     /// Access the IOV type
0165     const IOVType& iovType()  const;
0166     /// Access the IOV block
0167     const IOV& iov()  const;
0168 
0169     /** Conditions identification using integer keys.   */
0170     /// Hash identifier
0171     key_type key()  const;
0172     /// DetElement part of the identifier
0173     detkey_type  detector_key()  const;
0174     /// Item part of the identifier
0175     itemkey_type item_key()  const;
0176 
0177     /** Direct data items in string form */
0178 #if defined(DD4HEP_CONDITIONS_DEBUG) || !defined(DD4HEP_MINIMAL_CONDITIONS)
0179     /// Access the type field of the condition
0180     const std::string& type()  const;
0181     /// Access the value field of the condition as a string
0182     const std::string& value()  const;
0183     /// Access the comment field of the condition
0184     const std::string& comment()  const;
0185     /// Access the address string [e.g. database identifier]
0186     const std::string& address()  const;
0187 #endif
0188     /// Flag operations: Get condition flags
0189     mask_type flags()  const;
0190     /// Flag operations: Set a conditons flag
0191     void setFlag(mask_type option);
0192     /// Flag operations: UN-Set a conditons flag
0193     void unFlag(mask_type option);
0194     /// Flag operations: Test for a given a conditons flag
0195     bool testFlag(mask_type option) const;
0196 
0197     /** Conditions meta-data and handling of the data binding  */
0198     /// Access the opaque data block
0199     OpaqueDataBlock& data()  const;
0200     /// Access to the type information
0201     const std::type_info& typeInfo() const;
0202     /// Access to the grammar type
0203     const BasicGrammar& descriptor() const;
0204     /// Check if object is already bound....
0205     bool is_bound()  const  {  return isValid() ? data().is_bound() : false;  }
0206     /** Construct conditions object and bind the data
0207      *
0208      *  Note: The type definition is possible exactly once.
0209      *  Any further rebindings MUST match the identical type.
0210      */
0211     template <typename T, typename... Args> T& construct(Args... args);
0212     /** Bind the data of the conditions object to a given format.
0213      *
0214      *  Note: The type definition is possible exactly once.
0215      *  Any further rebindings MUST match the identical type.
0216      */
0217     template <typename T> T& bind();
0218     /** Set and bind the data of the conditions object to a given format.
0219      *
0220      *  Note: The type definition is possible exactly once.
0221      *  Any further rebindings MUST match the identical type.
0222      */
0223     template <typename T> T& bind(const std::string& val);
0224     /// Generic getter. Specify the exact type, not a polymorph type
0225     template <typename T> T& get();
0226     /// Generic getter (const version). Specify the exact type, not a polymorph type
0227     template <typename T> const T& get() const;
0228     /// Generic getter. Resolves polymorph types. It is mandatory that the datatype is polymorph!
0229     template <typename T> T& as();
0230     /// Generic getter (const version). Resolves polymorph types. It is mandatory that the datatype is polymorph!
0231     template <typename T> const T& as() const;
0232 
0233     /// Allow to trace condition names from keys for debugging
0234     static int haveInventory(int value = -1);
0235   };
0236 
0237   /// Initializing constructor
0238   inline Condition::Condition(Condition::Object* p)
0239     : Handle<Condition::Object>(p)  {}
0240 
0241   /// Constructor to be used when reading the already parsed object
0242   template <typename Q> inline Condition::Condition(const Handle<Q>& e)
0243     : Handle<Condition::Object>(e) {}
0244 
0245   /// Construct conditions object and bind the data
0246   template <typename T, typename... Args> T& Condition::construct(Args... args)   {
0247     return data().construct<T,Args...>(args...);
0248   }
0249   /// Bind the data of the conditions object to a given format.
0250   template <typename T> inline T& Condition::bind()   {
0251     return data().bind<T>();
0252   }
0253   /// Bind the data of the conditions object to a given format and fill data from string representation.
0254   template <typename T> inline T& Condition::bind(const std::string& val)   {
0255     return data().bind<T>(val);
0256   }
0257   /// Generic getter. Specify the exact type, not a polymorph type
0258   template <typename T> inline T& Condition::get() {
0259     return data().get<T>();
0260   }
0261   /// Generic getter (const version). Specify the exact type, not a polymorph type
0262   template <typename T> inline const T& Condition::get() const {
0263     return data().get<T>();
0264   }    
0265   /// Generic getter. Specify the exact type, not a polymorph type
0266   template <typename T> inline T& Condition::as() {
0267     return data().as<T>();
0268   }
0269   /// Generic getter (const version). Specify the exact type, not a polymorph type
0270   template <typename T> inline const T& Condition::as() const {
0271     return data().as<T>();
0272   }
0273     
0274   /// Key definition to optimize ans simplyfy the access to conditions entities
0275   /**
0276    *  \author  M.Frank
0277    *  \version 1.0
0278    *  \ingroup DD4HEP_CONDITIONS
0279    */
0280   class ConditionKey  {
0281   public:
0282 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0283     /// Optional string identifier. Helps debugging a lot!
0284     std::string  name;
0285 #endif
0286     /// Hashed key representation
0287     Condition::key_type     hash = 0;
0288 
0289     /// Helper union to interprete conditions keys
0290     /**
0291      *  \author  M.Frank
0292      *  \version 1.0
0293      *  \ingroup DD4HEP_CONDITIONS
0294      */
0295     union KeyMaker  {
0296       Condition::key_type  hash;
0297       /** Note: The memory layout is important here to have properly
0298        *        ordered maps. The detector key MUST be on the high end 
0299        *        of the resulting int64 'hash'.
0300        */
0301       struct {
0302         Condition::itemkey_type item_key;
0303         Condition::detkey_type  det_key;
0304       } values;
0305       KeyMaker()  {
0306         this->hash = 0;
0307       }
0308       KeyMaker(Condition::key_type k)  {
0309         this->hash = k;
0310       }
0311       KeyMaker(Condition::detkey_type det, Condition::itemkey_type item)  {
0312         this->values.det_key  = det;
0313         this->values.item_key = item;
0314       }
0315       /// Constructor from string
0316       KeyMaker(Condition::detkey_type det, const std::string& value);
0317       /// Constructor from string
0318       KeyMaker(DetElement detector, const std::string& value);
0319       /// Constructor from detector element and item sub-key
0320       KeyMaker(DetElement detector, Condition::itemkey_type item_key);
0321     };
0322     
0323     /// Compare keys by the hash value
0324     /**
0325      *  \author  M.Frank
0326      *  \version 1.0
0327      *  \ingroup DD4HEP_CONDITIONS
0328      */
0329     struct HashCompare {
0330       Condition::key_type key;
0331       HashCompare(Condition::key_type k) : key(k) {}
0332       bool operator==(const ConditionKey& k) const { return key==k.hash; }
0333     };
0334   public:
0335     /// Default constructor
0336     ConditionKey() = default;
0337     /// Constructor from fully qualified key
0338     ConditionKey(Condition::key_type key) : hash(key) {}
0339     /// Constructor from string
0340     ConditionKey(DetElement detector, const std::string& identifier);
0341     /// Constructor from detector element key and item sub-key
0342     ConditionKey(Condition::detkey_type det_key, const std::string& identifier);
0343     /// Constructor from detector element and item sub-key
0344     ConditionKey(DetElement detector, Condition::itemkey_type item_key);
0345     /// Constructor from detector element key and item sub-key
0346     ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key);
0347     /// Copy constructor
0348     ConditionKey(const ConditionKey& c) = default;
0349 
0350     /// Access the detector element part of the key
0351     Condition::detkey_type detector_key()  const   {
0352       return KeyMaker(hash).values.det_key;
0353     }
0354     /// Access the detector element part of the key
0355     Condition::itemkey_type item_key()  const   {
0356       return KeyMaker(hash).values.item_key;
0357     }
0358     /// Hash code generation from input string
0359     static Condition::key_type hashCode(DetElement detector, const char* value);
0360     /// Hash code generation from input string
0361     static Condition::key_type hashCode(DetElement detector, const std::string& value);
0362     /// 32 bit hashcode of the item
0363     static Condition::itemkey_type itemCode(const char* value);
0364     /// 32 bit hashcode of the item
0365     static Condition::itemkey_type itemCode(const std::string& value);
0366        
0367     /// Assignment operator from object copy
0368     ConditionKey& operator=(const ConditionKey& key) = default;
0369     /// Equality operator using key object
0370     bool operator==(const ConditionKey& compare)  const;
0371     /// Equality operator using hash value
0372     bool operator==(const Condition::key_type compare)  const;
0373     /// Equality operator using the string representation
0374     //bool operator==(const std::string& compare)  const;
0375 
0376     /// Operator less (for map insertions) using key object
0377     bool operator<(const ConditionKey& compare)  const;
0378     /// Operator less (for map insertions) using hash value
0379     bool operator<(const Condition::key_type compare)  const;
0380     /// Automatic conversion to the hashed representation of the key object
0381     operator Condition::key_type () const   {  return hash;     }
0382     /// Conversion to string
0383     std::string toString()  const;
0384   };
0385 
0386   /// Constructor from detector element key and item sub-key
0387   inline ConditionKey::ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key)  {
0388     hash = KeyMaker(det_key,item_key).hash;
0389   }
0390     
0391   /// Equality operator using key object
0392   inline bool ConditionKey::operator==(const ConditionKey& compare)  const
0393   {  return hash == compare.hash;                            }
0394 
0395   /// Equality operator using hash value
0396   inline bool ConditionKey::operator==(const Condition::key_type compare)  const
0397   {  return hash == compare;                                 }
0398 
0399   /// Operator less (for map insertions) using key object
0400   inline bool ConditionKey::operator<(const ConditionKey& compare)  const
0401   {  return hash < compare.hash;                             }
0402 
0403   /// Operator less (for map insertions) using hash value
0404   inline bool ConditionKey::operator<(const Condition::key_type compare)  const
0405   {  return hash < compare;                                  }
0406 
0407 
0408   /// Conditions selector functor. Default implementation selects everything evaluated.
0409   /**
0410    *  Please note:
0411    *  This class should never be directly instantiated by the user.
0412    *  A typical use-case is to do so in a wrapper class, which contains a refernce
0413    *  to a counter object, which in turn allows to deduce information from the
0414    *  processed objects.
0415    *
0416    *  See class ConditionsSelectWrapper below
0417    *
0418    *  \author  M.Frank
0419    *  \version 1.0
0420    *  \ingroup DD4HEP_CONDITIONS
0421    */
0422   class ConditionsSelect   {
0423   protected:
0424     /// Default constructor
0425     ConditionsSelect() = default;
0426     /// Copy constructor
0427     ConditionsSelect(const ConditionsSelect& copy) = default;
0428     /// Default destructor. 
0429     virtual ~ConditionsSelect();
0430     /// Default assignment operator
0431     ConditionsSelect& operator=(const ConditionsSelect& copy) = default;
0432 
0433   public:
0434     /// Selection callback: return true if the condition should be selected
0435     bool operator()(Condition cond)  const  { return (*this)(cond.ptr()); }
0436     /// Selection callback: return true if the condition should be selected
0437     bool operator()(std::pair<Condition::key_type,Condition::Object*> cond) const
0438     /** Arg is 2 longwords. No need to pass by reference.                      */
0439     { return (*this)(cond.second);            }
0440     /// Selection callback: return true if the condition should be selected
0441     /** Arg is 2 longwords. No need to pass by reference.                      */
0442     bool operator()(std::pair<Condition::key_type,Condition> cond) const
0443     { return (*this)(cond.second.ptr());      }
0444 
0445     /// Overloadable entry: Return number of conditions selected. Default does nothing....
0446     virtual size_t size()  const  { return 0; }
0447     /// Overloadable entry: Selection callback: return true if the condition should be selected
0448     virtual bool operator()(Condition::Object* cond) const = 0;
0449   };
0450 
0451   /// Conditions selector functor. Wraps a user defined object by reference
0452   /**
0453    *  Example usage for the slow ones:
0454    *
0455    *  class MyCounter : public ConditionsSelectWrapper<long> {
0456    *    MyCounter(long& cnt) : ConditionsSelectWrapper<long>(cnt) {}
0457    *    virtual bool operator()(Condition::Object* cond) const { if ( cond != 0 ) ++object; }
0458    *    // Optionally overload: virtual size_t size()  const  { return object; }
0459    *  };
0460    *  
0461    *  long counter = 0;
0462    *  for_each(std::begin(conditons), std::end(conditions), MyCounter(counter));
0463    *
0464    *  \author  M.Frank
0465    *  \version 1.0
0466    *  \ingroup DD4HEP_CONDITIONS
0467    */
0468   template <typename OBJECT> class ConditionsSelectWrapper : public ConditionsSelect {
0469   private:
0470     /// Default constructor
0471     ConditionsSelectWrapper() = delete;
0472     /// Default assignment operator
0473     bool operator==(const ConditionsSelectWrapper& compare) = delete;
0474 
0475   public:
0476     /// Reference to the infomation collector
0477     OBJECT& object;
0478 
0479   public:
0480     /// Default constructor
0481     ConditionsSelectWrapper(OBJECT& o) : ConditionsSelect(), object(o) {}
0482     /// Copy constructor
0483     ConditionsSelectWrapper(const ConditionsSelectWrapper& copy) = default;
0484     /// Default destructor. 
0485     virtual ~ConditionsSelectWrapper() = default;
0486     /// Default assignment operator
0487     ConditionsSelectWrapper& operator=(const ConditionsSelectWrapper& copy) = default;
0488   };
0489 
0490   // Utility type definitions
0491   typedef std::vector<Condition>          RangeConditions;
0492 
0493   /// Conditions internal namespace
0494   namespace detail  {
0495     /// Setup conditions item name inventory for debugging
0496     /** Populate a conditions item name inventory to ease debugging
0497      *  missing dependencies for derived conditions, which otherwise is
0498      *  difficult in optimized mode where all string values are suppressed.
0499      *
0500      *  Note: the inventory gets populated while creating item dependencies etc.
0501      *  Enabling afterwards has no effect.
0502      *
0503      *  value < 0  Return current value
0504      *  value = 0  Disable inventory for performance issues
0505      *  value > 0  Enable inventory population
0506      *
0507      *  \author  M.Frank
0508      *  \version 1.0
0509      *  \ingroup DD4HEP_CONDITIONS
0510      */
0511     int have_condition_item_inventory(int value = -1);
0512 
0513     
0514     /// Resolve key from conditions item name inventory for debugging
0515     /** The functionhave_condition_item_inventory must be called
0516      *  before items get populated to fill the inventory.....
0517      *
0518      *  key: conditions item key
0519      *
0520      *  \author  M.Frank
0521      *  \version 1.0
0522      *  \ingroup DD4HEP_CONDITIONS
0523      */
0524     std::string get_condition_item_name(Condition::itemkey_type key);
0525 
0526     /// Resolve key from conditions item name inventory for debugging
0527     /** The functionhave_condition_item_inventory must be called
0528      *  before items get populated to fill the inventory.....
0529      *
0530      *  key: full condition item hash
0531      *
0532      *  \author  M.Frank
0533      *  \version 1.0
0534      *  \ingroup DD4HEP_CONDITIONS
0535      */
0536     std::string get_condition_item_name(Condition::key_type key);
0537   }
0538   
0539 }          /* End namespace dd4hep                   */
0540 #endif // DD4HEP_CONDITIONS_H