Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-03 07:54:17

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 //==========================================================================
0011 
0012 #ifndef DDSEGMENTATION_BITFIELD64_H
0013 #define DDSEGMENTATION_BITFIELD64_H 1
0014 
0015 #include <ostream>
0016 
0017 #include <string>
0018 
0019 #include <DDSegmentation/BitFieldCoder.h>
0020 
0021 /// Namespace for the AIDA detector description toolkit
0022 namespace dd4hep {
0023   
0024   /// DDSegmentation namespace declaration
0025   namespace DDSegmentation {
0026     
0027     /// Lightweight helper class for BitField64 that corresponds to one field value.
0028     /**  (Not thread safe - only use directly through BitField64).
0029      *
0030      *    @author F.Gaede, DESY
0031      *    @date  2017-09
0032      */
0033     class BitFieldValue{
0034 
0035       CellID& _value ;
0036       const BitFieldElement& _bv ;
0037     
0038     public :
0039     
0040       BitFieldValue() = delete ;
0041     
0042       /// only c'tor with reference to bitfield and BitFieldElement
0043       BitFieldValue( CellID& bitfield, const BitFieldElement& bv ) :
0044         _value( bitfield ), _bv( bv) {}
0045     
0046       /** Returns the current field value 
0047        */
0048       FieldID value() const { return _bv.value( _value ) ; }
0049   
0050       /// Calculate Field value given an external 64 bit bitmap value
0051       FieldID value(CellID id) const { return _bv.value( id ) ; }
0052 
0053       //// Assignment operator for user convenience 
0054       BitFieldValue& operator=(FieldID in) {
0055         _bv.set( _value, in ) ;
0056         return *this ;
0057       } 
0058     
0059       /** Conversion operator for long64 - allows to write:<br>
0060        *  long64 index = myBitFieldValue ;
0061        */
0062       operator FieldID() const { return value() ; } 
0063     
0064       /** The field's name */
0065       const std::string& name() const { return _bv.name() ; }
0066 
0067       /** The field's offset */
0068       unsigned offset() const { return _bv.offset() ; }
0069 
0070       /** The field's width */
0071       unsigned width() const  { return _bv.width() ; }
0072 
0073       /** True if field is interpreted as signed */
0074       bool isSigned() const   { return _bv.isSigned() ; }
0075 
0076       /** The field's mask */
0077       CellID mask() const     { return _bv.mask() ; }
0078 
0079       /** Minimal value  */
0080       int  minValue()  const  { return _bv.minValue();  }
0081 
0082       /** Maximal value  */
0083       int  maxValue()  const  { return _bv.maxValue();  }
0084 
0085     } ;
0086     
0087   
0088     /// A bit field of 64bits that allows convenient declaration
0089     /**  and manipulation of sub fields of various widths.<br>
0090      *  Example:<br>
0091      *    BitField64 b("layer:7,system:-3,barrel:3,theta:32:11,phi:11" ) ; <br> 
0092      *    b[ "layer"  ]  = 123 ;         <br> 
0093      *    b[ "system" ]  = -4 ;          <br> 
0094      *    b[ "barrel" ]  = 7 ;           <br> 
0095      *    b[ "theta" ]   = 180 ;         <br> 
0096      *    b[ "phi" ]     = 270 ;         <br> 
0097      *    ...                            <br>
0098      *    int theta = b["theta"] ;                    <br>
0099      *    ...                                         <br>
0100      *    unsigned phiIndex = b.index("phi") ;        <br>
0101      *    int phi = b[  phiIndex ] ;                  <br>
0102      *
0103      *    Sep-2017: FG: re-implemented as lightweight wrapper around BitFieldCoder
0104      *    @author F.Gaede, DESY
0105      *    @date  2013-06
0106      */  
0107     class BitField64{
0108       
0109       friend std::ostream& operator<<(std::ostream& os, const BitField64& b) ;
0110 
0111     public :
0112 
0113       virtual ~BitField64() {
0114         if( _owner)
0115           delete _coder ;
0116       }; 
0117 
0118       /** No default c'tor */
0119       BitField64() = delete ;
0120 
0121       /** The c'tor takes an initialization string of the form:<br>
0122        *  \<fieldDesc\>[,\<fieldDesc\>...]<br>
0123        *  fieldDesc = name:[start]:[-]length<br>
0124        *  where:<br>
0125        *  name: The name of the field<br>
0126        *  start: The start bit of the field. If omitted assumed to start 
0127        *  immediately following previous field, or at the least significant 
0128        *  bit if the first field.<br>
0129        *  length: The number of bits in the field. If preceeded by '-' 
0130        *  the field is signed, otherwise unsigned.<br>
0131        *  Bit numbering is from the least significant bit (bit 0) to the most 
0132        *  significant (bit 63). <br>
0133        *  Example: "layer:7,system:-3,barrel:3,theta:32:11,phi:11"
0134        */
0135       BitField64( const std::string& initString ){
0136         _coder = new BitFieldCoder( initString ) ;
0137       }
0138 
0139       /// Initialize from existing BitFieldCoder
0140       BitField64( const BitFieldCoder* coder ) : _owner(false), _coder( coder ) {
0141       }
0142 
0143 
0144       /** Returns the current 64bit value 
0145        */
0146       CellID getValue() const { return _value ; }
0147     
0148       /** Set a new 64bit value  - bits not used in description are set to 0.
0149        */
0150       void  setValue(CellID value ) { _value = ( _coder->mask() & value ) ; }
0151 
0152       /** Set a new 64bit value given as high and low 32bit words.
0153        */
0154       void  setValue(unsigned low_Word, unsigned high_Word ) {
0155         setValue( ( low_Word & 0xffffffffULL ) |  ( ( high_Word & 0xffffffffULL ) << 32 ) ) ; 
0156       }
0157     
0158       /** Operator for setting a new value and accessing the BitField directly */
0159       BitField64& operator()(CellID val) { setValue( val ) ; return *this ; }
0160  
0161       /** Reset - same as setValue(0) - useful if the same encoder is used for many objects.
0162        */
0163       void  reset() { _value = 0 ; }
0164 
0165       /** Acces to field through index 
0166        */
0167       BitFieldValue operator[](size_t idx) {
0168         return BitFieldValue( _value,  _coder->operator[]( idx  ) ) ; 
0169       }
0170     
0171       // /** Const acces to field through index 
0172       //  */
0173       // const BitFieldValue& operator[](size_t idx) const { 
0174       
0175       //   return BitFieldValue( _value,  _coder->operator[]( idx  ) ) ; 
0176       // }
0177 
0178       /** Highest bit used in fields [0-63]
0179        */
0180       unsigned highestBit() const { return _coder->highestBit() ; }
0181     
0182       /** Number of values */
0183       size_t size() const { return _coder->size() ; }
0184 
0185       /** Index for field named 'name' 
0186        */
0187       size_t index( const std::string& name) const { return _coder->index( name ) ; } 
0188 
0189       /** Access to field through name .
0190        */
0191       BitFieldValue operator[](const std::string& name) { 
0192         return BitFieldValue( _value,  _coder->operator[]( name ) ) ; 
0193       }
0194       // /** Const Access to field through name .
0195       //  */
0196       // const BitFieldValue& operator[](const std::string& name) const { 
0197 
0198       //   return BitFieldValue( _value,  _coder->operator[]( name )  ); 
0199       // }
0200 
0201 
0202       /** The low  word, bits 0-31
0203        */
0204       unsigned lowWord() const { return unsigned( _value &  0xffffFFFFUL )  ; } 
0205 
0206       /** The high  word, bits 32-63
0207        */
0208       unsigned highWord() const { return unsigned(_value >> 32) ; } 
0209 
0210 
0211       /** Return a valid description string of all fields
0212        */
0213       std::string fieldDescription() const { return _coder->fieldDescription() ; }
0214 
0215       /** Return a string with a comma separated list of the current sub field values 
0216        */
0217       std::string valueString() const ;
0218 
0219       // const std::vector<BitFieldValue*>& fields()  const  {
0220       //   return _coder->fields() ;
0221       // }
0222     
0223     protected:
0224 
0225       // -------------- data members:--------------
0226       bool      _owner{true} ;
0227       CellID    _value{} ;
0228       const BitFieldCoder* _coder{} ;
0229     
0230     };
0231   
0232   
0233     /** Operator for dumping BitField64 to streams 
0234      */
0235     std::ostream& operator<<(std::ostream& os, const BitField64& b) ;
0236   
0237   
0238   
0239   } // end namespace
0240 
0241   using DDSegmentation::BitField64 ;
0242 } // end namespace
0243 
0244 #endif
0245 
0246 
0247 
0248