LDMX Software
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ldmx::DetectorIDInterpreter Class Reference

Class provides an "introspection" capability for the 32-bit packed IDs used for uniquely identifying hits and detector components. More...

#include <DetectorIDInterpreter.h>

Classes

struct  IDSignature
 
struct  SubdetectorIDFields
 

Public Types

typedef unsigned FieldValue
 Definition of the field value type.
 
typedef std::vector< FieldValueFieldValueList
 A list of field values.
 

Public Member Functions

 DetectorIDInterpreter (DetectorID detid)
 
DetectorID getId () const
 Get the raw value of the detector ID.
 
void setRawValue (DetectorID rawValue)
 Set the raw value of the detector ID.
 
FieldValue getFieldValue (int i) const
 Decode and return a field's value from the raw ID.
 
void setFieldValue (int i, FieldValue value)
 Set a field value by index in the field value list.
 
void setFieldValue (const std::string &fieldName, FieldValue fieldValue)
 Set a field value by its name.
 
int getFieldCount () const
 Get the list of field information.
 
const IDField::IDFieldListgetFieldList () const
 Get the list of field information.
 
const IDFieldgetField (const std::string &fieldName) const
 Get the information for a field by name.
 
FieldValue getFieldValue (const std::string &fieldName) const
 Decode and return a field's value by name (e.g.
 

Static Public Member Functions

static void registerInterpreter (SubdetectorIDType idtype, const IDField::IDFieldList &fieldList)
 Register a new field interpreter for a given subdetector id.
 
static void registerInterpreter (SubdetectorIDType idtype, unsigned int mask, unsigned int equality, const IDField::IDFieldList &fieldList)
 Register a new field interpreter for a more-complex case where additional bits are needed to determine format.
 

Private Member Functions

void init ()
 Reinitialize the ID in case the field list changed.
 
void unpack ()
 Unpack the current raw value into a list.
 
void pack ()
 Pack the current list of field values into a raw value.
 

Static Private Member Functions

static void loadStandardInterpreters ()
 Load the standard field interpreters if not yet loaded.
 

Private Attributes

DetectorID id_
 The raw, packed value of the ID.
 
FieldValueList fieldValues_
 The unpacked list of field values.
 
const SubdetectorIDFieldsp_fieldInfo_
 Pointer to the appropriate field info for this class.
 

Static Private Attributes

static std::map< IDSignature, const SubdetectorIDFields * > g_rosettaStone
 

Detailed Description

Class provides an "introspection" capability for the 32-bit packed IDs used for uniquely identifying hits and detector components.

Note
Represents an ID in the detector with a raw, 32-bit value which can be unpacked into a list of field values or packed from a list of field values.

Definition at line 25 of file DetectorIDInterpreter.h.

Member Typedef Documentation

◆ FieldValue

Definition of the field value type.

Definition at line 35 of file DetectorIDInterpreter.h.

◆ FieldValueList

A list of field values.

Definition at line 40 of file DetectorIDInterpreter.h.

Constructor & Destructor Documentation

◆ DetectorIDInterpreter() [1/2]

ldmx::DetectorIDInterpreter::DetectorIDInterpreter ( )

Definition at line 17 of file DetectorIDInterpreter.cxx.

17 : id_(), p_fieldInfo_(0) {
18 init();
19}
const SubdetectorIDFields * p_fieldInfo_
Pointer to the appropriate field info for this class.
void init()
Reinitialize the ID in case the field list changed.
DetectorID id_
The raw, packed value of the ID.

◆ DetectorIDInterpreter() [2/2]

ldmx::DetectorIDInterpreter::DetectorIDInterpreter ( DetectorID  detid)

Definition at line 21 of file DetectorIDInterpreter.cxx.

22 : id_(did), p_fieldInfo_(0) {
23 init();
24 unpack();
25}
void unpack()
Unpack the current raw value into a list.

◆ ~DetectorIDInterpreter()

ldmx::DetectorIDInterpreter::~DetectorIDInterpreter ( )

Definition at line 16 of file DetectorIDInterpreter.cxx.

16{}

Member Function Documentation

◆ getField()

const IDField * ldmx::DetectorIDInterpreter::getField ( const std::string &  fieldName) const

Get the information for a field by name.

Returns
The information of the field.

Definition at line 72 of file DetectorIDInterpreter.cxx.

73 {
74 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
75 if (byname != p_fieldInfo_->fieldMap_.end()) return (byname->second);
76 return 0;
77}

◆ getFieldCount()

int ldmx::DetectorIDInterpreter::getFieldCount ( ) const
inline

Get the list of field information.

Returns
The list of field information.

Definition at line 78 of file DetectorIDInterpreter.h.

78{ return int(p_fieldInfo_->fieldList_.size()); }

References p_fieldInfo_.

◆ getFieldList()

const IDField::IDFieldList & ldmx::DetectorIDInterpreter::getFieldList ( ) const
inline

Get the list of field information.

Returns
The list of field information.

Definition at line 84 of file DetectorIDInterpreter.h.

84 {
85 return p_fieldInfo_->fieldList_;
86 }

References p_fieldInfo_.

◆ getFieldValue() [1/2]

DetectorIDInterpreter::FieldValue ldmx::DetectorIDInterpreter::getFieldValue ( const std::string &  fieldName) const

Decode and return a field's value by name (e.g.

"layer").

Returns
The value of the field.

Definition at line 79 of file DetectorIDInterpreter.cxx.

80 {
81 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
82 return getFieldValue(byname->second->getIndex());
83}
FieldValue getFieldValue(int i) const
Decode and return a field's value from the raw ID.

◆ getFieldValue() [2/2]

DetectorIDInterpreter::FieldValue ldmx::DetectorIDInterpreter::getFieldValue ( int  i) const

Decode and return a field's value from the raw ID.

Parameters
iThe index of the field value.

Definition at line 52 of file DetectorIDInterpreter.cxx.

53 {
54 IDField* field = p_fieldInfo_->fieldList_.at(i);
55 unsigned result = (field->getBitMask() & id_.raw()) >> field->getStartBit();
56 return result;
57}
RawValue raw() const
Definition DetectorID.h:68

References ldmx::IDField::getBitMask(), and ldmx::IDField::getStartBit().

Referenced by TEST_CASE().

◆ getId()

DetectorID ldmx::DetectorIDInterpreter::getId ( ) const
inline

Get the raw value of the detector ID.

Returns
The raw value.

Definition at line 46 of file DetectorIDInterpreter.h.

46{ return id_; }

References id_.

Referenced by TEST_CASE().

◆ init()

void ldmx::DetectorIDInterpreter::init ( )
private

Reinitialize the ID in case the field list changed.

This is called automatically by setFieldList. Function should be called by a subclass if a new field list is set outside that method or if new fields are added to the existing list.

Definition at line 85 of file DetectorIDInterpreter.cxx.

85 {
86 if (g_rosettaStone.empty()) loadStandardInterpreters();
87
88 p_fieldInfo_ = 0;
89
90 if (id_.null()) return;
91
92 for (auto ptr : g_rosettaStone) {
93 if ((id_.raw() & ptr.first.mask_) == ptr.first.comparison_) {
94 p_fieldInfo_ = (ptr.second);
95 this->fieldValues_.resize(p_fieldInfo_->fieldList_.size());
96 return;
97 }
98 }
99
100 // fell through, no match
101 IDSignature sig;
102 sig.comparison_ = 0;
103 sig.mask_ = DetectorID::SUBDETECTORID_MASK << DetectorID::SUBDETECTORID_SHIFT;
104
105 auto ptr = g_rosettaStone.find(sig);
106 p_fieldInfo_ = (ptr->second);
107 this->fieldValues_.resize(p_fieldInfo_->fieldList_.size());
108}
static void loadStandardInterpreters()
Load the standard field interpreters if not yet loaded.
FieldValueList fieldValues_
The unpacked list of field values.
bool null() const
Definition DetectorID.h:60

◆ loadStandardInterpreters()

void ldmx::DetectorIDInterpreter::loadStandardInterpreters ( )
staticprivate

Load the standard field interpreters if not yet loaded.

@important Developers of new Ids should add construction calls here!

Definition at line 147 of file DetectorIDInterpreter.cxx.

147 {
148 if (!g_rosettaStone.empty()) return;
150 fields.push_back(
151 new IDField("subdetector", 0, DetectorID::SUBDETECTORID_SHIFT, 31));
152 fields.push_back(
153 new IDField("payload", 2, 0, DetectorID::SUBDETECTORID_SHIFT - 1));
154
155 registerInterpreter(SD_NULL, fields);
156
157 EcalID::createInterpreters();
158 EcalTriggerID::createInterpreters();
159 HcalID::createInterpreters();
160 TrackerID::createInterpreters();
161 TrigScintID::createInterpreters();
162 SimSpecialID::createInterpreters();
163}
static void registerInterpreter(SubdetectorIDType idtype, const IDField::IDFieldList &fieldList)
Register a new field interpreter for a given subdetector id.
std::vector< IDField * > IDFieldList
List of fields.
Definition IDField.h:25

◆ pack()

void ldmx::DetectorIDInterpreter::pack ( )
private

Pack the current list of field values into a raw value.

Definition at line 42 of file DetectorIDInterpreter.cxx.

42 {
43 DetectorID::RawValue rawValue = 0;
44 for (auto field : p_fieldInfo_->fieldList_) {
45 unsigned fieldValue = fieldValues_[field->getIndex()];
46 rawValue =
47 rawValue | ((fieldValue << field->getStartBit()) & field->getBitMask());
48 }
49 id_.setRawValue(rawValue);
50}
void setRawValue(RawValue rawValue)
Set the raw value of the detector ID.
Definition DetectorID.h:74

◆ registerInterpreter() [1/2]

void ldmx::DetectorIDInterpreter::registerInterpreter ( SubdetectorIDType  idtype,
const IDField::IDFieldList fieldList 
)
static

Register a new field interpreter for a given subdetector id.

Definition at line 110 of file DetectorIDInterpreter.cxx.

111 {
112 IDSignature sig;
113 sig.comparison_ = idtype << DetectorID::SUBDETECTORID_SHIFT;
114 sig.mask_ = DetectorID::SUBDETECTORID_MASK << DetectorID::SUBDETECTORID_SHIFT;
115 if (g_rosettaStone.find(sig) != g_rosettaStone.end()) {
116 EXCEPTION_RAISE("DetectorIDException",
117 "Attempted to replace interpreter for subdetector " +
118 std::to_string(idtype));
119 }
120 SubdetectorIDFields* fields = new SubdetectorIDFields();
121 fields->fieldList_ = fieldList;
122 for (auto it : fieldList) fields->fieldMap_[it->getFieldName()] = it;
123 g_rosettaStone[sig] = fields;
124}

◆ registerInterpreter() [2/2]

void ldmx::DetectorIDInterpreter::registerInterpreter ( SubdetectorIDType  idtype,
unsigned int  mask,
unsigned int  equality,
const IDField::IDFieldList fieldList 
)
static

Register a new field interpreter for a more-complex case where additional bits are needed to determine format.

Definition at line 126 of file DetectorIDInterpreter.cxx.

128 {
129 IDSignature sig;
130 sig.comparison_ = (idtype << DetectorID::SUBDETECTORID_SHIFT) | equality;
131 sig.mask_ =
132 (DetectorID::SUBDETECTORID_MASK << DetectorID::SUBDETECTORID_SHIFT) |
133 mask;
134 if (g_rosettaStone.find(sig) != g_rosettaStone.end()) {
135 EXCEPTION_RAISE("DetectorIDException",
136 "Attempted to replace interpreter for subdetector " +
137 std::to_string(idtype) + " mask " +
138 std::to_string(mask) + " equality " +
139 std::to_string(equality));
140 }
141 SubdetectorIDFields* fields = new SubdetectorIDFields();
142 fields->fieldList_ = fieldList;
143 for (auto it : fieldList) fields->fieldMap_[it->getFieldName()] = it;
144 g_rosettaStone[sig] = fields;
145}

◆ setFieldValue() [1/2]

void ldmx::DetectorIDInterpreter::setFieldValue ( const std::string &  fieldName,
FieldValue  fieldValue 
)

Set a field value by its name.

Parameters
fieldNameThe name of the field.
fieldValueThe new value of the field.

Definition at line 64 of file DetectorIDInterpreter.cxx.

65 {
66 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
67 if (byname != p_fieldInfo_->fieldMap_.end())
68 fieldValues_[byname->second->getIndex()] = fieldValue;
69 pack(); // keep packed
70}
void pack()
Pack the current list of field values into a raw value.

◆ setFieldValue() [2/2]

void ldmx::DetectorIDInterpreter::setFieldValue ( int  i,
FieldValue  value 
)

Set a field value by index in the field value list.

Parameters
iThe index of the field value.
valueThe new field value.

Definition at line 59 of file DetectorIDInterpreter.cxx.

59 {
60 fieldValues_[i] = val;
61 pack(); // keep packed
62}

Referenced by TEST_CASE().

◆ setRawValue()

void ldmx::DetectorIDInterpreter::setRawValue ( DetectorID  rawValue)

Set the raw value of the detector ID.

Parameters
rawValueThe raw value of the ID.

Definition at line 27 of file DetectorIDInterpreter.cxx.

27 {
28 id_ = rawValue;
29 init();
30 unpack();
31}

◆ unpack()

void ldmx::DetectorIDInterpreter::unpack ( )
private

Unpack the current raw value into a list.

Definition at line 33 of file DetectorIDInterpreter.cxx.

33 {
34 std::fill(fieldValues_.begin(), fieldValues_.end(), 0);
35 if (!p_fieldInfo_) return;
36 for (auto field : p_fieldInfo_->fieldList_) {
37 unsigned result = (field->getBitMask() & id_.raw()) >> field->getStartBit();
38 this->fieldValues_[field->getIndex()] = result;
39 }
40}

Member Data Documentation

◆ fieldValues_

FieldValueList ldmx::DetectorIDInterpreter::fieldValues_
private

The unpacked list of field values.

Definition at line 149 of file DetectorIDInterpreter.h.

◆ g_rosettaStone

std::map< DetectorIDInterpreter::IDSignature, const DetectorIDInterpreter::SubdetectorIDFields * > ldmx::DetectorIDInterpreter::g_rosettaStone
staticprivate

Definition at line 164 of file DetectorIDInterpreter.h.

◆ id_

DetectorID ldmx::DetectorIDInterpreter::id_
private

The raw, packed value of the ID.

Definition at line 144 of file DetectorIDInterpreter.h.

Referenced by getId().

◆ p_fieldInfo_

const SubdetectorIDFields* ldmx::DetectorIDInterpreter::p_fieldInfo_
private

Pointer to the appropriate field info for this class.

Definition at line 169 of file DetectorIDInterpreter.h.

Referenced by getFieldCount(), and getFieldList().


The documentation for this class was generated from the following files: