1#include "DetDescr/DetectorIDInterpreter.h"
6#include "DetDescr/SimSpecialID.h"
8#include "DetDescr/TrigScintID.h"
12std::map<DetectorIDInterpreter::IDSignature,
13 const DetectorIDInterpreter::SubdetectorIDFields*>
14 DetectorIDInterpreter::g_rosettaStone;
16DetectorIDInterpreter::~DetectorIDInterpreter() {}
17DetectorIDInterpreter::DetectorIDInterpreter() : id_(), p_fieldInfo_(0) {
21DetectorIDInterpreter::DetectorIDInterpreter(DetectorID did)
22 : id_(did), p_fieldInfo_(0) {
27void DetectorIDInterpreter::setRawValue(
DetectorID rawValue) {
33void DetectorIDInterpreter::unpack() {
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;
42void DetectorIDInterpreter::pack() {
43 DetectorID::RawValue rawValue = 0;
44 for (
auto field : p_fieldInfo_->fieldList_) {
45 unsigned fieldValue = fieldValues_[field->getIndex()];
47 rawValue | ((fieldValue << field->getStartBit()) & field->getBitMask());
49 id_.setRawValue(rawValue);
54 IDField* field = p_fieldInfo_->fieldList_.at(i);
59void DetectorIDInterpreter::setFieldValue(
int i,
FieldValue val) {
60 fieldValues_[i] = val;
64void DetectorIDInterpreter::setFieldValue(
const std::string& fieldName,
66 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
67 if (byname != p_fieldInfo_->fieldMap_.end())
68 fieldValues_[byname->second->getIndex()] = fieldValue;
72const IDField* DetectorIDInterpreter::getField(
73 const std::string& fieldName)
const {
74 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
75 if (byname != p_fieldInfo_->fieldMap_.end())
return (byname->second);
80 const std::string& fieldName)
const {
81 auto byname = p_fieldInfo_->fieldMap_.find(fieldName);
82 return getFieldValue(byname->second->getIndex());
85void DetectorIDInterpreter::init() {
86 if (g_rosettaStone.empty()) loadStandardInterpreters();
90 if (id_.null())
return;
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());
103 sig.mask_ = DetectorID::SUBDETECTORID_MASK << DetectorID::SUBDETECTORID_SHIFT;
105 auto ptr = g_rosettaStone.find(sig);
106 p_fieldInfo_ = (ptr->second);
107 this->fieldValues_.resize(p_fieldInfo_->fieldList_.size());
110void DetectorIDInterpreter::registerInterpreter(
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));
121 fields->fieldList_ = fieldList;
122 for (
auto it : fieldList) fields->fieldMap_[it->getFieldName()] = it;
123 g_rosettaStone[sig] = fields;
126void DetectorIDInterpreter::registerInterpreter(
127 SubdetectorIDType idtype,
unsigned int mask,
unsigned int equality,
130 sig.comparison_ = (idtype << DetectorID::SUBDETECTORID_SHIFT) | equality;
132 (DetectorID::SUBDETECTORID_MASK << DetectorID::SUBDETECTORID_SHIFT) |
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));
142 fields->fieldList_ = fieldList;
143 for (
auto it : fieldList) fields->fieldMap_[it->getFieldName()] = it;
144 g_rosettaStone[sig] = fields;
147void DetectorIDInterpreter::loadStandardInterpreters() {
148 if (!g_rosettaStone.empty())
return;
151 new IDField(
"subdetector", 0, DetectorID::SUBDETECTORID_SHIFT, 31));
153 new IDField(
"payload", 2, 0, DetectorID::SUBDETECTORID_SHIFT - 1));
155 registerInterpreter(SD_NULL, fields);
157 EcalID::createInterpreters();
158 EcalTriggerID::createInterpreters();
159 HcalID::createInterpreters();
160 TrackerID::createInterpreters();
161 TrigScintID::createInterpreters();
162 SimSpecialID::createInterpreters();
Class that defines an ECal detector ID with a cell number.
Class that defines an ECal trigger cell detector ID.
Class that defines an HCal sensitive detector.
Class that defines a Tracker detector ID with a module number.
unsigned FieldValue
Definition of the field value type.
Defines a 32-bit packed ID for uniquely identifying hits and detector components.
Provides information about a field within a DetectorID.
unsigned getStartBit()
Get the start bit of the field.
std::vector< IDField * > IDFieldList
List of fields.
unsigned getBitMask()
Get a bit mask for this field.