DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
IDDescriptor.cpp
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 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/IDDescriptor.h>
16 #include <DD4hep/detail/Handle.inl>
18 #include <DD4hep/InstanceCount.h>
19 #include <DD4hep/Volumes.h>
20 #include <DD4hep/Printout.h>
21 
22 // C/C++ include files
23 #include <stdexcept>
24 #include <cstdlib>
25 #include <cmath>
26 
27 using namespace dd4hep;
28 
29 namespace {
30  void _construct(IDDescriptor::Object* o, const std::string& dsc) {
31  BitFieldCoder& bf = o->decoder;
32  o->fieldIDs.clear();
33  o->fieldMap.clear();
34  o->description = dsc;
35  for (size_t i = 0; i < bf.size(); ++i) {
36  const BitFieldElement* f = &bf[i];
37  o->fieldIDs.emplace_back(i, f->name());
38  o->fieldMap.emplace_back(f->name(), f);
39  }
40  }
41 }
42 
44 IDDescriptor::IDDescriptor(const std::string& nam, const std::string& description) {
45  Object* obj = new Object(description);
46  assign(obj, nam, "iddescriptor");
47  _construct(obj, description);
48 }
49 
51 void IDDescriptor::rebuild(const std::string& description) {
52  Object* p = ptr();
53  std::string dsc = description;
55  new(&p->decoder) BitFieldCoder(dsc);
56  _construct(p, dsc);
57 }
58 
60 std::string IDDescriptor::toString() const {
61  if ( isValid() ) {
62  return m_element->GetName();
63  }
64  return "----";
65 }
66 
67 std::string IDDescriptor::fieldDescription() const {
68  BitFieldCoder& bf = data<Object>()->decoder;
69  return bf.fieldDescription();
70 }
71 
73 unsigned IDDescriptor::maxBit() const {
74  return data<Object>()->decoder.highestBit();
75 }
76 
79  if ( isValid() ) {
80  return data<Object>()->fieldIDs;
81  }
82  except("IDDescriptor","dd4hep: Attempt to access an invalid IDDescriptor object.");
83  throw std::runtime_error("dd4hep"); // Never called. Simply make the compiler happy!
84 }
85 
88  if ( isValid() ) {
89  return data<Object>()->fieldMap;
90  }
91  except("IDDescriptor","dd4hep: Attempt to access an invalid IDDescriptor object.");
92  throw std::runtime_error("dd4hep"); // Never called. Simply make the compiler happy!
93 }
94 
96 const BitFieldElement* IDDescriptor::field(const std::string& field_name) const {
97  const FieldMap& fm = fields(); // This already checks the object validity
98  for (const auto& i : fm )
99  if (i.first == field_name)
100  return i.second;
101  except("IDDescriptor","dd4hep: %s: This ID descriptor has no field with the name: %s",
102  name(),field_name.c_str());
103  throw std::runtime_error("dd4hep"); // Never called. Simply make the compiler happy!
104 }
105 
107 const BitFieldElement* IDDescriptor::field(size_t identifier) const {
108  const FieldMap& fm = fields(); // This already checks the object validity
109  return fm[identifier].second;
110 }
111 
113 std::size_t IDDescriptor::fieldID(const std::string& field_name) const {
114  const FieldIDs& fm = ids(); // This already checks the object validity
115  for (const auto& i : fm )
116  if (i.second == field_name)
117  return i.first;
118  except("IDDescriptor","dd4hep: %s: This ID descriptor has no field with the name: %s",
119  name(),field_name.c_str());
120  throw std::runtime_error("dd4hep"); // Never called. Simply make the compiler happy!
121 }
122 
124 VolumeID IDDescriptor::get_mask(const std::vector<std::pair<std::string, int> >& id_vector) const {
125  VolumeID mask = 0ULL;
126  for (const auto& i : id_vector ) {
127  const auto* fld = field(i.first);
128  mask |= fld->mask();
129  }
130  return mask;
131 }
132 
134 VolumeID IDDescriptor::encode(const std::vector<std::pair<std::string, int> >& id_vector) const {
135  VolumeID id = 0;
136  //const PlacedVolume::VolIDs* ids = (const PlacedVolume::VolIDs*)&id_vector;
137  //printout(INFO,"IDDescriptor","VolIDs: %s",ids->str().c_str());
138  for (const auto& i : id_vector ) {
139  const BitFieldElement* fld = field(i.first);
140  int off = fld->offset();
141  VolumeID val = i.second;
142  id |= ((fld->value(val << off) << off)&fld->mask());
143  }
144  return id;
145 }
146 
149  if ( fld ) {
150  int off = fld->offset();
151  return ((fld->value(value << off) << off)&fld->mask());
152  }
153  except("IDDescriptor","dd4hep: %s: Cannot encode value with void Field reference.");
154  return 0UL;
155 }
156 
158 VolumeID IDDescriptor::encode_reverse(const std::vector<std::pair<std::string, int> >& id_vector) const
159 {
160  return detail::reverseBits<VolumeID>(encode(id_vector));
161 }
162 
165  std::vector<std::pair<const BitFieldElement*, VolumeID> >& flds) const
166 {
167  const std::vector<BitFieldElement>& v = access()->decoder.fields();
168  flds.clear();
169  for (auto& f : v )
170  flds.emplace_back(&f, f.value(vid));
171 }
172 
174 std::string IDDescriptor::str(VolumeID vid) const {
175  const std::vector<BitFieldElement>& v = access()->decoder.fields();
176  std::stringstream str;
177  for (auto& f : v )
178  str << f.name() << ":" << std::setw(4) << std::setfill('0')
179  << std::hex << std::right << f.value(vid)
180  << std::left << std::dec << " ";
181  return str.str().substr(0, str.str().length()-1);
182 }
183 
185 std::string IDDescriptor::str(VolumeID vid, VolumeID mask) const {
186  const std::vector<BitFieldElement>& v = access()->decoder.fields();
187  std::stringstream str;
188  for (auto& f : v ) {
189  if ( 0 == (mask&f.mask()) ) continue;
190  str << f.name() << ":" << std::setw(4) << std::setfill('0')
191  << std::hex << std::right << f.value(vid)
192  << std::left << std::dec << " ";
193  }
194  return str.str().substr(0, str.str().length()-1);
195 }
196 
199  return &(data<Object>()->decoder);
200 }
dd4hep::IDDescriptor::decodeFields
void decodeFields(VolumeID vid, std::vector< std::pair< const BitFieldElement *, VolumeID > > &fields) const
Decode volume IDs and return filled descriptor with all fields.
Definition: IDDescriptor.cpp:164
dd4hep::IDDescriptor::fieldID
size_t fieldID(const std::string &field_name) const
Get the field identifier of one field by name.
Definition: IDDescriptor.cpp:113
dd4hep::IDDescriptorObject
Concrete object implementation of the IDDescriptorObject Handle.
Definition: ObjectsInterna.h:206
dd4hep::DDSegmentation::BitFieldElement
Helper class for BitFieldCoder that corresponds to one field value.
Definition: BitFieldCoder.h:32
dd4hep::IDDescriptorObject::decoder
BitFieldCoder decoder
Decoder object.
Definition: ObjectsInterna.h:215
Volumes.h
dd4hep::IDDescriptor::FieldMap
std::vector< std::pair< std::string, const Field * > > FieldMap
Definition: IDDescriptor.h:40
v
View * v
Definition: MultiView.cpp:28
dd4hep::Handle< IDDescriptorObject >::Object
IDDescriptorObject Object
Extern accessible definition of the contained element type.
Definition: Handle.h:88
dd4hep::IDDescriptor::field
const BitFieldElement * field(const std::string &field_name) const
Get the field descriptor of one field by name.
Definition: IDDescriptor.cpp:96
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::IDDescriptor::maxBit
unsigned maxBit() const
The total number of encoding bits for this descriptor.
Definition: IDDescriptor.cpp:73
dd4hep::NamedObject::GetName
const char * GetName() const
Access name.
Definition: NamedObject.h:58
dd4hep::IDDescriptor::Field
BitFieldElement Field
Definition: IDDescriptor.h:39
dd4hep::Handle< IDDescriptorObject >::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::DDSegmentation::BitFieldCoder::fields
const std::vector< BitFieldElement > & fields() const
Definition: BitFieldCoder.h:218
dd4hep::Handle< IDDescriptorObject >::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::IDDescriptor::IDDescriptor
IDDescriptor()=default
Default constructor.
dd4hep::DDSegmentation::BitFieldElement::name
const std::string & name() const
Definition: BitFieldCoder.h:61
dd4hep::Handle< IDDescriptorObject >::assign
void assign(Object *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
dd4hep::IDDescriptor::encode
static VolumeID encode(const Field *fld, VolumeID value)
Encode partial volume identifiers to a volumeID.
Definition: IDDescriptor.cpp:148
dd4hep::IDDescriptor::FieldIDs
std::vector< std::pair< size_t, std::string > > FieldIDs
Definition: IDDescriptor.h:41
dd4hep::IDDescriptor::str
std::string str(VolumeID vid) const
Decode volume IDs and return string reprensentation for debugging purposes.
Definition: IDDescriptor.cpp:174
dd4hep::Handle< IDDescriptorObject >::m_element
IDDescriptorObject * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:93
dd4hep::IDDescriptor::get_mask
VolumeID get_mask(const std::vector< std::pair< std::string, int > > &id_vector) const
Compute the submask for a given set of volume IDs.
Definition: IDDescriptor.cpp:124
dd4hep::IDDescriptor::fields
const FieldMap & fields() const
Access the fieldmap container.
Definition: IDDescriptor.cpp:87
VolumeID
dd4hep::DDSegmentation::VolumeID VolumeID
Definition: SegmentationDictionary.h:50
IDDescriptor.h
dd4hep::Handle< IDDescriptorObject >::access
IDDescriptorObject * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
dd4hep::Handle< IDDescriptorObject >::ptr
IDDescriptorObject * ptr() const
Access to the held object.
Definition: Handle.h:153
ObjectsInterna.h
dd4hep::IDDescriptor::fieldDescription
std::string fieldDescription() const
The string description of all fields from the BitField.
Definition: IDDescriptor.cpp:67
dd4hep::IDDescriptor::rebuild
void rebuild(const std::string &description)
Re-build object in place.
Definition: IDDescriptor.cpp:51
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DDSegmentation::BitFieldCoder::fieldDescription
std::string fieldDescription() const
Definition: BitFieldCoder.cpp:123
dd4hep::IDDescriptor::decoder
BitFieldCoder * decoder() const
Access the BitFieldCoder object.
Definition: IDDescriptor.cpp:198
dd4hep::IDDescriptor::encode_reverse
VolumeID encode_reverse(const std::vector< std::pair< std::string, int > > &id_vector) const
Encode a set of volume identifiers to a volumeID with the system ID on the top bits.
Definition: IDDescriptor.cpp:158
dd4hep::DDSegmentation::BitFieldCoder::size
size_t size() const
Definition: BitFieldCoder.h:192
InstanceCount.h
Printout.h
dd4hep::IDDescriptor::toString
std::string toString() const
Access string representation.
Definition: IDDescriptor.cpp:60
dd4hep::IDDescriptor::ids
const FieldIDs & ids() const
Access the field-id container.
Definition: IDDescriptor.cpp:78