Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:36

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