DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
BitFieldCoder.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 //==========================================================================
11 
12 #ifndef DDSEGMENTATION_BITFIELDCODER_H
13 #define DDSEGMENTATION_BITFIELDCODER_H 1
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 #include <sstream>
19 #include <cstdint>
20 
21 namespace dd4hep {
22 
23  namespace DDSegmentation {
24 
25  typedef int64_t FieldID;
26  typedef uint64_t CellID;
27  typedef uint64_t VolumeID;
28 
29  class StringTokenizer ;
30 
33 
34  public :
36  BitFieldElement() = default ;
38  BitFieldElement(const BitFieldElement&) = default ;
41 
47  BitFieldElement( const std::string& name, unsigned offset, int signedWidth ) ;
49  ~BitFieldElement() = default ;
50 
53 
55  FieldID value(CellID bitfield) const;
56 
57  // assign the given value to the bit field
58  void set(CellID& bitfield, FieldID value) const ;
59 
61  const std::string& name() const { return _name ; }
62 
64  unsigned offset() const { return _offset ; }
65 
67  unsigned width() const { return _width ; }
68 
70  bool isSigned() const { return _isSigned ; }
71 
73  CellID mask() const { return _mask ; }
74 
76  int minValue() const { return _minVal; }
77 
79  int maxValue() const { return _maxVal; }
80 
81  protected:
82 
84  unsigned _offset {};
85  unsigned _width {};
86  int _minVal {};
87  int _maxVal {};
88  bool _isSigned {};
89  std::string _name;
90  };
91 
92 
93 
95 
115  public :
116  typedef std::map<std::string, unsigned int> IndexMap ;
117 
118  public :
120  BitFieldCoder() = default ;
122  BitFieldCoder(const BitFieldCoder&) = default ;
126  ~BitFieldCoder() = default ;
127 
129  BitFieldCoder& operator=(const BitFieldCoder&) = default ;
130 
145  BitFieldCoder( const std::string& initString ) : _joined(0){
146  init( initString ) ;
147  }
148 
151  static CellID toLong(unsigned low_Word, unsigned high_Word ) {
152  return ( ( low_Word & 0xffffffffULL ) | ( ( high_Word & 0xffffffffULL ) << 32 ) ) ;
153  }
154 
157  static unsigned lowWord(CellID bitfield) { return unsigned( bitfield & 0xffffFFFFUL ); }
158 
161  static unsigned highWord(CellID bitfield) { return unsigned( bitfield >> 32); }
162 
165  FieldID get(CellID bitfield, size_t idx) const {
166  return _fields.at(idx).value( bitfield ) ;
167  }
168 
171  FieldID get(CellID bitfield, const std::string& name) const {
172  return _fields.at( index( name ) ).value( bitfield ) ;
173  }
174 
177  void set(CellID& bitfield, size_t idx, FieldID value) const {
178  _fields.at(idx).set( bitfield , value ) ;
179  }
180 
183  void set(CellID& bitfield, const std::string& name, FieldID value) const {
184  _fields.at( index( name ) ).set( bitfield, value ) ;
185  }
186 
189  unsigned highestBit() const ;
190 
192  size_t size() const { return _fields.size() ; }
193 
196  size_t index( const std::string& name) const ;
197 
200  const BitFieldElement& operator[](const std::string& name) const {
201  return _fields[ index( name ) ] ;
202  }
203 
206  const BitFieldElement& operator[](unsigned idx) const {
207  return _fields[ idx ] ;
208  }
209 
212  std::string fieldDescription() const ;
213 
216  std::string valueString(CellID bitfield) const ;
217 
218  const std::vector<BitFieldElement>& fields() const {
219  return _fields;
220  }
221 
223  CellID mask() const { return _joined ; }
224 
225  protected:
226 
229  void addField( const std::string& name, unsigned offset, int width );
230 
234  void init( const std::string& initString) ;
235 
236  protected:
237  // -------------- data members:--------------
238  std::vector<BitFieldElement> _fields{} ;
241  };
242 
243 
245 
254  std::vector< std::string >& _tokens ;
255  char _del ;
256  char _last ;
257 
258  public:
259 
261  StringTokenizer( std::vector< std::string >& tokens, char del )
262  : _tokens(tokens)
263  , _del(del),
264  _last(del) {
265  }
266 
268  void operator()(const char& c) {
269  if( c != _del ) {
270  if( _last == _del ) {
271  _tokens.emplace_back("") ;
272  }
273  _tokens.back() += c ;
274  }
275  _last = c ;
276  }
277  };
278 
279  } // end namespace
280 } // end namespace
281 #endif
282 
283 
284 
285 
dd4hep::DDSegmentation::BitFieldElement::_minVal
int _minVal
Definition: BitFieldCoder.h:86
dd4hep::DDSegmentation::StringTokenizer::StringTokenizer
StringTokenizer(std::vector< std::string > &tokens, char del)
Definition: BitFieldCoder.h:261
dd4hep::DDSegmentation::VolumeID
uint64_t VolumeID
Definition: BitFieldCoder.h:27
dd4hep::DDSegmentation::BitFieldCoder::BitFieldCoder
BitFieldCoder(BitFieldCoder &&)=default
Move constructor.
dd4hep::DDSegmentation::BitFieldElement::offset
unsigned offset() const
Definition: BitFieldCoder.h:64
dd4hep::DDSegmentation::BitFieldElement::BitFieldElement
BitFieldElement(const BitFieldElement &)=default
Copy constructor.
dd4hep::DDSegmentation::BitFieldCoder::operator[]
const BitFieldElement & operator[](unsigned idx) const
Definition: BitFieldCoder.h:206
dd4hep::DDSegmentation::BitFieldElement
Helper class for BitFieldCoder that corresponds to one field value.
Definition: BitFieldCoder.h:32
dd4hep::DDSegmentation::BitFieldElement::_name
std::string _name
Definition: BitFieldCoder.h:89
dd4hep::DDSegmentation::BitFieldElement::BitFieldElement
BitFieldElement(BitFieldElement &&)=default
Move constructor.
dd4hep::DDSegmentation::BitFieldElement::_mask
CellID _mask
Definition: BitFieldCoder.h:83
dd4hep::DDSegmentation::BitFieldElement::isSigned
bool isSigned() const
Definition: BitFieldCoder.h:70
dd4hep::DDSegmentation::BitFieldElement::maxValue
int maxValue() const
Definition: BitFieldCoder.h:79
dd4hep::DDSegmentation::BitFieldCoder
Helper class for decoding and encoding a bit field of 64bits for convenient declaration.
Definition: BitFieldCoder.h:114
dd4hep::DDSegmentation::BitFieldCoder::~BitFieldCoder
~BitFieldCoder()=default
Default destructor.
dd4hep::DDSegmentation::BitFieldCoder::get
FieldID get(CellID bitfield, size_t idx) const
Definition: BitFieldCoder.h:165
dd4hep::DDSegmentation::StringTokenizer
Helper class for string tokenization.
Definition: BitFieldCoder.h:253
dd4hep::DDSegmentation::BitFieldCoder::highWord
static unsigned highWord(CellID bitfield)
Definition: BitFieldCoder.h:161
dd4hep::DDSegmentation::BitFieldElement::value
FieldID value(CellID bitfield) const
calculate this field's value given an external 64 bit bitmap
Definition: BitFieldCoder.cpp:55
dd4hep::DDSegmentation::BitFieldCoder::IndexMap
std::map< std::string, unsigned int > IndexMap
Definition: BitFieldCoder.h:116
dd4hep::DDSegmentation::BitFieldCoder::toLong
static CellID toLong(unsigned low_Word, unsigned high_Word)
Definition: BitFieldCoder.h:151
dd4hep::DDSegmentation::BitFieldElement::_isSigned
bool _isSigned
Definition: BitFieldCoder.h:88
dd4hep::DDSegmentation::BitFieldCoder::fields
const std::vector< BitFieldElement > & fields() const
Definition: BitFieldCoder.h:218
dd4hep::DDSegmentation::BitFieldCoder::_map
IndexMap _map
Definition: BitFieldCoder.h:239
dd4hep::DDSegmentation::StringTokenizer::_del
char _del
Definition: BitFieldCoder.h:255
dd4hep::DDSegmentation::BitFieldCoder::_fields
std::vector< BitFieldElement > _fields
Definition: BitFieldCoder.h:238
dd4hep::DDSegmentation::StringTokenizer::_last
char _last
Definition: BitFieldCoder.h:256
dd4hep::DDSegmentation::BitFieldElement::operator=
BitFieldElement & operator=(const BitFieldElement &)=default
Assignment operator.
dd4hep::DDSegmentation::BitFieldCoder::highestBit
unsigned highestBit() const
Definition: BitFieldCoder.cpp:96
dd4hep::DDSegmentation::BitFieldCoder::valueString
std::string valueString(CellID bitfield) const
Definition: BitFieldCoder.cpp:109
dd4hep::DDSegmentation::BitFieldCoder::addField
void addField(const std::string &name, unsigned offset, int width)
Definition: BitFieldCoder.cpp:144
dd4hep::DDSegmentation::FieldID
int64_t FieldID
Definition: BitFieldCoder.h:25
dd4hep::DDSegmentation::BitFieldCoder::BitFieldCoder
BitFieldCoder(const std::string &initString)
Definition: BitFieldCoder.h:145
dd4hep::DDSegmentation::StringTokenizer::_tokens
std::vector< std::string > & _tokens
Definition: BitFieldCoder.h:254
dd4hep::DDSegmentation::BitFieldCoder::operator[]
const BitFieldElement & operator[](const std::string &name) const
Definition: BitFieldCoder.h:200
dd4hep::DDSegmentation::BitFieldElement::set
void set(CellID &bitfield, FieldID value) const
Definition: BitFieldCoder.cpp:67
dd4hep::DDSegmentation::BitFieldCoder::operator=
BitFieldCoder & operator=(const BitFieldCoder &)=default
Assignment operator.
dd4hep::DDSegmentation::BitFieldElement::name
const std::string & name() const
Definition: BitFieldCoder.h:61
dd4hep::DDSegmentation::BitFieldCoder::lowWord
static unsigned lowWord(CellID bitfield)
Definition: BitFieldCoder.h:157
dd4hep::DDSegmentation::BitFieldCoder::set
void set(CellID &bitfield, const std::string &name, FieldID value) const
Definition: BitFieldCoder.h:183
dd4hep::DDSegmentation::BitFieldCoder::BitFieldCoder
BitFieldCoder(const BitFieldCoder &)=default
Copy constructor.
dd4hep::DDSegmentation::BitFieldElement::width
unsigned width() const
Definition: BitFieldCoder.h:67
dd4hep::DDSegmentation::StringTokenizer::operator()
void operator()(const char &c)
Definition: BitFieldCoder.h:268
dd4hep::DDSegmentation::BitFieldCoder::set
void set(CellID &bitfield, size_t idx, FieldID value) const
Definition: BitFieldCoder.h:177
dd4hep::DDSegmentation::BitFieldElement::mask
CellID mask() const
Definition: BitFieldCoder.h:73
dd4hep::DDSegmentation::BitFieldCoder::get
FieldID get(CellID bitfield, const std::string &name) const
Definition: BitFieldCoder.h:171
dd4hep::DDSegmentation::BitFieldElement::~BitFieldElement
~BitFieldElement()=default
Default destructor.
dd4hep::DDSegmentation::CellID
uint64_t CellID
Definition: BitFieldCoder.h:26
dd4hep::DDSegmentation::BitFieldCoder::index
size_t index(const std::string &name) const
Definition: BitFieldCoder.cpp:84
dd4hep::DDSegmentation::BitFieldElement::_maxVal
int _maxVal
Definition: BitFieldCoder.h:87
dd4hep::DDSegmentation::BitFieldCoder::BitFieldCoder
BitFieldCoder()=default
Default constructor.
dd4hep::DDSegmentation::BitFieldElement::_width
unsigned _width
Definition: BitFieldCoder.h:85
dd4hep::DDSegmentation::BitFieldCoder::_joined
CellID _joined
Definition: BitFieldCoder.h:240
dd4hep::DDSegmentation::BitFieldElement::minValue
int minValue() const
Definition: BitFieldCoder.h:76
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DDSegmentation::BitFieldCoder::init
void init(const std::string &initString)
Definition: BitFieldCoder.cpp:166
dd4hep::DDSegmentation::BitFieldCoder::fieldDescription
std::string fieldDescription() const
Definition: BitFieldCoder.cpp:123
dd4hep::DDSegmentation::BitFieldElement::BitFieldElement
BitFieldElement()=default
Default constructor.
dd4hep::DDSegmentation::BitFieldCoder::size
size_t size() const
Definition: BitFieldCoder.h:192
dd4hep::DDSegmentation::BitFieldElement::_offset
unsigned _offset
Definition: BitFieldCoder.h:84
dd4hep::DDSegmentation::BitFieldCoder::mask
CellID mask() const
Definition: BitFieldCoder.h:223