ATLAS Offline Software
ZdcID.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "GaudiKernel/MsgStream.h"
7 
8 #include "ZdcIdentifier/ZdcID.h"
10 #include "IdDict/IdDictField.h"
11 #include "IdDict/IdDictMgr.h"
12 #include "IdDict/IdDictRegion.h"
15 #include <set>
16 #include <iostream>
17 
18 //______________________________________________________
20  : AtlasDetectorID("ZdcID", "ZDC"),
21  m_dict(nullptr)
22 {
23 }
24 //______________________________________________________
26 
27 }
28 
29 int
31 {
32  MsgStream log(m_msgSvc, "ZdcID");
33  log << MSG::INFO << "Initialize from dictionary" << endmsg;
34 
35  // Check whether this helper should be reinitialized
36  if (!reinitialize(dict_mgr)) {
37  log << MSG::INFO << "Request to reinitialize not satisfied - tags have not changed" << endmsg;
38  return (0);
39  }
40  else {
41  if (m_msgSvc) {
42  log << MSG::DEBUG << "(Re)initialize" << endmsg;
43  }
44  else {
45  std::cout << " DEBUG (Re)initialize" << std::endl;
46  }
47  }
48 
49  // init base object
50  if(AtlasDetectorID::initialize_from_dictionary(dict_mgr)) return (1);
51 
52  // Register version of ForwardDetectors dictionary
53  if (register_dict_tag(dict_mgr, "ForwardDetectors")) return(1);
54 
55  m_dict = dict_mgr.find_dictionary ("ForwardDetectors");
56  if(!m_dict) {
57  log << MSG::ERROR << " ZdcID::initialize_from_dict - cannot access ForwardDetectors dictionary " << endmsg;
58  return 1;
59  }
60 
61  // Initialize the field indices
62  if(initLevelsFromDict()) return (1);
63 
64 
65  //
66  // Build multirange for the valid set of identifiers
67  //
68 
69 
70  // Find value for the field ForwardDetectors
71  const IdDictDictionary* atlasDict = dict_mgr.find_dictionary ("ATLAS");
72  int fwdField = -1;
73  if (atlasDict->get_label_value("subdet", "ForwardDetectors", fwdField)) {
74  log << MSG::ERROR << "Could not get value for label 'ForwardDetectors' of field 'subdet' in dictionary "
75  << atlasDict->name()
76  << endmsg;
77  return (1);
78  }
79 
80  // Find value for the field ZDC
81  int zdcField = -1;
82  if (m_dict->get_label_value("part", "ZDC", zdcField)) {
83  log << MSG::ERROR << "Could not get value for label 'ZDC' of field 'part' in dictionary "
84  << m_dict->name()
85  << endmsg;
86  return (1);
87  }
88  if (m_msgSvc) {
89  log << MSG::DEBUG << " ZdcID::initialize_from_dict "
90  << "Found field values: ZDC "
91  << zdcField
92  << std::endl;
93  }
94  else {
95  std::cout << " DEBUG ZdcID::initialize_from_dict "
96  << "Found field values: ZDC "
97  << zdcField
98  << std::endl;
99  }
100 
101  // Set up id for region and range prefix
103  region_id.add(fwdField);
104  region_id.add(zdcField);
105  Range prefix;
108 
109  if (m_msgSvc) {
110  log << MSG::INFO << " ZdcID::initialize_from_dict " << endmsg;
111  log << MSG::DEBUG
112  << "Module range -> " << (std::string)m_full_module_range
113  << endmsg;
114  log << MSG::DEBUG
115  << "Channel range -> " << (std::string)m_full_channel_range
116  << endmsg;
117  }
118  else {
119  std::cout << " INFO ZdcID::initialize_from_dict " << std::endl;
120  std::cout << " DEBUG module range -> " << (std::string)m_full_module_range
121  << std::endl;
122  std::cout << " DEBUG Channel range -> " << (std::string)m_full_channel_range
123  << std::endl;
124  }
125 
126  // Setup the hash tables
127  if(init_hashes()) return (1);
128 
129  return 0;
130 }
131 
132 
133 
134 int
136 {
137  MsgStream log(m_msgSvc, "ZdcID");
138  if(!m_dict) {
139  log << MSG::ERROR << " ZdcID::initLevelsFromDict - dictionary NOT initialized " << endmsg;
140  return (1);
141  }
142 
143  // Find out which identifier field corresponds to each level. Use
144  // names to find each field/level.
145 
146  m_zdc_region_index = 999;
147  m_FORWARD_INDEX = 999;
148  m_ZDC_INDEX = 999;
149  m_SIDE_INDEX = 999;
150  m_MODULE_INDEX = 999;
151  m_TYPE_INDEX = 999;
152  m_CHANNEL_INDEX = 999;
153 
154  // Save index to a ZDC region for unpacking
158  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find zdc region index: id, reg "
159  << (std::string)id << " " << m_zdc_region_index
160  << endmsg;
161  return (1);
162  }
163 
164  // Find a ZDC region
165  const IdDictField* field = m_dict->find_field("subdet");
166  if (field) {
167  m_FORWARD_INDEX = field->index();
168  }
169  else {
170  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find 'subdet' field " << endmsg;
171  return (1);
172  }
173  field = m_dict->find_field("part");
174  if (field) {
175  m_ZDC_INDEX = field->index();
176  }
177  else {
178  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find 'part' field " << endmsg;
179  return (1);
180  }
181  field = m_dict->find_field("side");
182  if (field) {
183  m_SIDE_INDEX = field->index();
184  }
185  else {
186  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find 'side' field " << endmsg;
187  return (1);
188  }
189  field = m_dict->find_field("module");
190  if (field) {
191  m_MODULE_INDEX = field->index();
192  }
193  else {
194  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find 'module' field " << endmsg;
195  return (1);
196  }
197  field = m_dict->find_field("type");
198  if (field) {
199  m_TYPE_INDEX = field->index();
200  }
201  else {
202  log << MSG::ERROR<< "ZdcID::initLevelsFromDict - unable to find 'type' field " << endmsg;
203  return (1);
204  }
205  field = m_dict->find_field("channel");
206  if (field) {
207  m_CHANNEL_INDEX = field->index();
208  }
209  else {
210  log << MSG::ERROR << "ZdcID::initLevelsFromDict - unable to find 'channel' field " << endmsg;
211  return (1);
212  }
213 
214  // Set the field implementations: for bec, lay/disk, eta/phi mod
215 
216  const IdDictRegion& region = m_dict->region(m_zdc_region_index);
217 
224 
225  if (m_msgSvc) {
226  log << MSG::DEBUG << "decode index and bit fields for each level: " << endmsg;
227  log << MSG::DEBUG << "forward " << m_forward_impl.show_to_string() << endmsg;
228  log << MSG::DEBUG << "zdc " << m_zdc_impl.show_to_string() << endmsg;
229  log << MSG::DEBUG << "side " << m_side_impl.show_to_string() << endmsg;
230  log << MSG::DEBUG << "module " << m_module_impl.show_to_string() << endmsg;
231  log << MSG::DEBUG << "type " << m_type_impl.show_to_string() << endmsg;
232  log << MSG::DEBUG << "channel " << m_channel_impl.show_to_string() << endmsg;
233  }
234  else {
235  std::cout << " DEBUG decode index and bit fields for each level: " << std::endl;
236  std::cout << " DEBUG forward " << m_forward_impl.show_to_string() << std::endl;
237  std::cout << " DEBUG zdc " << m_zdc_impl.show_to_string() << std::endl;
238  std::cout << " DEBUG side " << m_side_impl.show_to_string() << std::endl;
239  std::cout << " DEBUG module " << m_module_impl.show_to_string() << std::endl;
240  std::cout << " DEBUG type " << m_type_impl.show_to_string() << std::endl;
241  std::cout << " DEBUG channel " << m_channel_impl.show_to_string() << std::endl;
242  }
243 
244  return (0);
245 
246 }
247 
248 
249 int
251 {
252  //
253  // create a vector(s) to retrieve the hashes for compact ids.
254  //
255  MsgStream log(m_msgSvc, "ZDC_ID");
256 
257 
258  // module hash
261  unsigned int nids = 0;
262  std::set<Identifier> ids;
263  for (unsigned int i = 0; i < m_full_module_range.size(); ++i) {
264  const Range& range = m_full_module_range[i];
266  auto first = rit.begin();
267  auto last = rit.end();
268  for (; first != last; ++first) {
269  const ExpandedIdentifier& exp_id = (*first);
270  Identifier id = module_id(exp_id[m_SIDE_INDEX],
271  exp_id[m_MODULE_INDEX]);
272  if(!(ids.insert(id)).second) {
273  log << MSG::ERROR << " ZDC_ID::init_hashes "
274  << " Error: duplicated id for module id. nid " << nids
275  << " compact id " << MSG::hex << id.get_compact() << MSG::dec
276  << " id " << (std::string)exp_id << endmsg;
277  return (1);
278  }
279  nids++;
280  }
281  }
282  if(ids.size() != m_module_hash_max) {
283  log << MSG::ERROR << " ZDC_ID::init_hashes "
284  << " Error: set size NOT EQUAL to hash max. size " << ids.size()
285  << " hash max " << m_module_hash_max
286  << endmsg;
287  return (1);
288  }
289 
290  nids = 0;
291  std::set<Identifier>::const_iterator first = ids.begin();
292  std::set<Identifier>::const_iterator last = ids.end();
293  for (; first != last && nids < m_module_vec.size(); ++first) {
294  m_module_vec[nids] = (*first);
295  nids++;
296  }
297 
298  // channel hash
301  nids = 0;
302  ids.clear();
303  for (unsigned int i = 0; i < m_full_channel_range.size(); ++i) {
304  const Range& range = m_full_channel_range[i];
306  auto first = rit.begin();
307  auto last = rit.end();
308  for (; first != last; ++first) {
309  const ExpandedIdentifier& exp_id = (*first);
310  Identifier id = channel_id(exp_id[m_SIDE_INDEX],
311  exp_id[m_MODULE_INDEX],
312  exp_id[m_TYPE_INDEX],
313  exp_id[m_CHANNEL_INDEX]);
314 
315  if(!(ids.insert(id)).second) {
316  log << MSG::ERROR << " ZDC_ID::init_hashes "
317  << " Error: duplicated id for channel id. nid " << nids
318  << " compact id " << MSG::hex << id.get_compact() << MSG::dec
319  << " id " << (std::string)exp_id << endmsg;
320  return (1);
321  }
322  nids++;
323  }
324  }
325  if(ids.size() != m_channel_hash_max) {
326  log << MSG::ERROR << " ZDC_ID::init_hashes "
327  << " Error: set size NOT EQUAL to hash max. size " << ids.size()
328  << " hash max " << m_channel_hash_max
329  << endmsg;
330  return (1);
331  }
332 
333  nids = 0;
334  first = ids.begin();
335  last = ids.end();
336  for (; first != last && nids < m_channel_vec.size(); ++first) {
337  m_channel_vec[nids] = (*first);
338  nids++;
339  }
340  return (0);
341 }
342 
343 void
345 {
346  MsgStream log(m_msgSvc, "ZDC_ID");
347 
348  if (m_dict) {
349 
350  int nids = 0;
351  int nerr = 0;
354  const_id_iterator last = m_module_vec.end();
355  for (; first != last; ++first, ++nids) {
356  Identifier id = (*first);
357  Identifier new_id = module_id(side(id),
358  module(id));
359  if (id != new_id) {
360  log << MSG::ERROR << "ZDC_ID::test_packing: new and old compacts not equal. New/old/expanded ids "
361  << show_to_string(id) << " " << show_to_string(new_id) << endmsg;
362  nerr++;
363  continue;
364  }
365  IdentifierHash idHash = module_hash(id);
366  Identifier new_id_1 = module_id(idHash);
367  if (id != new_id_1) {
368  log << MSG::ERROR << "ZDC_ID::test_packing: new and old compacts from hash not equal. New/old/expanded ids. hash " << idHash << " "
369  << show_to_string(id) << " " << show_to_string(new_id_1) << endmsg;
370  nerr++;
371  continue;
372  }
373 
374  // std::cout << "module ids: " << idHash << " " << std::hex << id.get_compact() << std::dec << " " << new_id_1.get_compact() << " " << show_to_string(id) << " " << show_to_string(new_id) << " " << show_to_string(new_id_1) << std::endl;
375 
376  }
377 
378  if (m_msgSvc) {
379  log << MSG::DEBUG << "ZDC_ID::test_packing: tested module ids. nids, errors "
380  << nids << " " << nerr << endmsg;
381  }
382  else {
383  std::cout << " DEBUG ZDC_ID::test_packing: tested module ids. nids, errors "
384  << nids << " " << nerr << std::endl;
385  }
386 
387  first = m_channel_vec.begin();
388  last = m_channel_vec.end();
389  for (; first != last; ++first, ++nids) {
390  Identifier id = (*first);
391  Identifier new_id = channel_id(side(id),
392  module(id),
393  type(id),
394  channel(id));
395  if (id != new_id) {
396  log << MSG::ERROR << "ZDC_ID::test_packing: new and old compacts not equal. New/old/expanded ids "
397  << show_to_string(id) << " " << show_to_string(new_id) << endmsg;
398  nerr++;
399  continue;
400  }
401  IdentifierHash idHash = channel_hash(id);
402  Identifier new_id_1 = channel_id(idHash);
403  if (id != new_id_1) {
404  log << MSG::ERROR << "ZDC_ID::test_packing: new and old compacts from hash not equal. New/old/expanded ids hash " << idHash << " "
405  << show_to_string(id) << " " << show_to_string(new_id_1) << endmsg;
406  nerr++;
407  continue;
408  }
409 
410  // std::cout << "channel ids: " << idHash << " " << show_to_string(id) << " " << show_to_string(new_id) << " " << show_to_string(new_id_1) << std::endl;
411 
412 
413  }
414 
415  if (m_msgSvc) {
416  log << MSG::DEBUG << "ZDC_ID::test_packing: tested channel ids. nids, errors "
417  << nids << " " << nerr << endmsg;
418  }
419  else {
420  std::cout << " DEBUG ZDC_ID::test_packing: tested channel ids. nids, errors "
421  << nids << " " << nerr << std::endl;
422  }
423 
424  if (m_msgSvc) {
425  log << MSG::DEBUG << "ZDC_ID::test_packing: Successful tested "
426  << nids << " ids. "
427  << endmsg;
428  }
429  else {
430  std::cout << " DEBUG ZDC_ID::test_packing: Successful tested "
431  << nids << " ids. "
432  << std::endl;
433  }
434  }
435  else {
436  log << MSG::ERROR << "ZDC_ID::test_packing: Unable to test module/channel is packing - no dictionary has been defined. "
437  << endmsg;
438  }
439 }
440 
442 {
443  return (m_module_vec.begin());
444 }
445 
447 {
448  return (m_module_vec.end());
449 }
450 
452 {
453  return (m_channel_vec.begin());
454 }
455 
457 {
458  return (m_channel_vec.end());
459 }
460 
461 
462 
IdDictDictionary::find_region
IdDictRegion * find_region(const std::string &region_name) const
Definition: IdDictDictionary.cxx:101
IdDictMgr.h
ConstRangeIterator
Definition: RangeIterator.h:46
ZdcID::m_channel_vec
id_vec m_channel_vec
Definition: ZdcID.h:117
IdDictDictionary::build_multirange
MultiRange build_multirange() const
Get MultiRange for full dictionary.
Definition: IdDictDictionary.cxx:290
AtlasDetectorID::initialize_from_dictionary
virtual int initialize_from_dictionary(const IdDictMgr &dict_mgr) override
Initialization from the identifier dictionary.
Definition: AtlasDetectorID.cxx:257
ZdcID::m_dict
const IdDictDictionary * m_dict
Definition: ZdcID.h:111
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
ZdcID::m_ZDC_INDEX
size_type m_ZDC_INDEX
Definition: ZdcID.h:106
IdDictFieldImplementation::show_to_string
std::string show_to_string(void) const
Definition: IdDictFieldImplementation.cxx:32
ZdcID::module_hash
IdentifierHash module_hash(Identifier module_id) const
module hash from id - optimized
Definition: ZdcID.h:134
IdDictDictionary::region
const IdDictRegion & region(size_t i) const
Region at index i.
Definition: IdDictDictionary.h:350
ZdcID::module
int module(const Identifier &id) const
Definition: ZdcID.h:163
IdDictDictionary.h
ZdcID::module_id
Identifier module_id(int side, int module) const
Definition: ZdcID.h:182
ZdcID::m_SIDE_INDEX
size_type m_SIDE_INDEX
Definition: ZdcID.h:107
AtlasDetectorID::forward_field_value
int forward_field_value() const
Definition: AtlasDetectorID.h:540
ZdcID::m_FORWARD_INDEX
size_type m_FORWARD_INDEX
Definition: ZdcID.h:105
ExpandedIdentifier
Definition: DetectorDescription/Identifier/Identifier/ExpandedIdentifier.h:102
ZdcID::~ZdcID
~ZdcID()
Definition: ZdcID.cxx:25
AtlasDetectorID::m_msgSvc
IMessageSvc * m_msgSvc
pointer to the message service
Definition: AtlasDetectorID.h:369
ZdcID::init_hashes
int init_hashes()
Definition: ZdcID.cxx:250
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
ZdcID::m_full_module_range
MultiRange m_full_module_range
Definition: ZdcID.h:112
IdDictDictionary::name
const std::string & name() const
Dictionary name.
Definition: IdDictDictionary.h:332
ZdcID::channels_end
const_id_iterator channels_end() const
Definition: ZdcID.cxx:456
IdDictDictionary::find_field
IdDictField * find_field(const std::string &name) const
Definition: IdDictDictionary.cxx:47
CalibDbCompareRT.region_id
region_id
Definition: CalibDbCompareRT.py:67
IdDictRegion
Definition: IdDictRegion.h:20
ZdcID::m_type_impl
IdDictFieldImplementation m_type_impl
Definition: ZdcID.h:123
ZdcID::m_channel_hash_max
size_type m_channel_hash_max
Definition: ZdcID.h:115
ZdcID::channel_id
Identifier channel_id(int side, int module, int type, int channel) const
Definition: ZdcID.h:205
ZdcID::m_zdc_region_index
size_type m_zdc_region_index
Definition: ZdcID.h:104
IdDictRegion::implementation
const IdDictFieldImplementation & implementation(size_t i) const
Definition: IdDictRegion.cxx:34
IdDictMgr
Definition: IdDictMgr.h:15
IdDictMgr::find_dictionary
IdDictDictionary * find_dictionary(const std::string &name) const
Access dictionary by name.
Definition: IdDictMgr.cxx:124
ZdcID::modules_begin
const_id_iterator modules_begin() const
Iterators over full set of ids. modules iterator is sorted.
Definition: ZdcID.cxx:441
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
ZdcID::m_module_impl
IdDictFieldImplementation m_module_impl
Definition: ZdcID.h:122
ZdcID::const_id_iterator
std::vector< Identifier >::const_iterator const_id_iterator
Definition: ZdcID.h:30
ZdcID::m_full_channel_range
MultiRange m_full_channel_range
Definition: ZdcID.h:113
ZdcID::test_packing
void test_packing() const
Tests of packing.
Definition: ZdcID.cxx:344
ConstRangeIterator::begin
ConstRangeIterator begin() const
Definition: RangeIterator.cxx:18
ZdcID::m_MODULE_INDEX
size_type m_MODULE_INDEX
Definition: ZdcID.h:108
AtlasDetectorID::register_dict_tag
int register_dict_tag(const IdDictMgr &dict_mgr, const std::string &dict_name)
Register the file and tag names for a particular IdDict dictionary.
Definition: AtlasDetectorID.cxx:203
IdDictDictionary::get_label_value
int get_label_value(const std::string &field, const std::string &label, int &value) const
Definition: IdDictDictionary.cxx:75
ZdcID::channels_begin
const_id_iterator channels_begin() const
Iterators over full set of ids. channels iterator is sorted.
Definition: ZdcID.cxx:451
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:239
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
IdentifierHash.h
RangeIterator.h
Range
A Range describes the possible ranges for the field values of an ExpandedIdentifier.
Definition: DetectorDescription/Identifier/Identifier/Range.h:29
ZdcID::initialize_from_dictionary
virtual int initialize_from_dictionary(const IdDictMgr &dict_mgr)
Initialization from the identifier dictionary.
Definition: ZdcID.cxx:30
IdDictDictionary
Definition: IdDictDictionary.h:30
ZdcID::m_forward_impl
IdDictFieldImplementation m_forward_impl
Definition: ZdcID.h:119
ZdcID.h
AtlasDetectorID::reinitialize
bool reinitialize(const IdDictMgr &dict_mgr)
Test whether an idhelper should be reinitialized based on the change of tags.
Definition: AtlasDetectorID.cxx:223
IdDictField.h
AtlasDetectorID::show_to_string
std::string show_to_string(Identifier id, const IdContext *context=0, char sep='.') const
or provide the printout in string form
Definition: AtlasDetectorID.cxx:367
ZdcID::m_side_impl
IdDictFieldImplementation m_side_impl
Definition: ZdcID.h:121
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:15
ZdcID::m_CHANNEL_INDEX
size_type m_CHANNEL_INDEX
Definition: ZdcID.h:110
ZdcID::modules_end
const_id_iterator modules_end() const
Definition: ZdcID.cxx:446
MultiRange::size
size_type size() const
Definition: MultiRange.cxx:70
DeMoScan.first
bool first
Definition: DeMoScan.py:534
DEBUG
#define DEBUG
Definition: page_access.h:11
ZdcID::side
int side(const Identifier &id) const
Values of different levels (failure returns 0)
Definition: ZdcID.h:157
ZdcID::type
int type(const Identifier &id) const
Definition: ZdcID.h:169
ZdcID::ZdcID
ZdcID()
Definition: ZdcID.cxx:19
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
ZdcID::initLevelsFromDict
int initLevelsFromDict()
Definition: ZdcID.cxx:135
ZdcID::channel_hash
IdentifierHash channel_hash(Identifier channel_id) const
channel hash from id - optimized
Definition: ZdcID.h:143
IdDictRegion.h
AtlasDetectorID::zdc_field_value
int zdc_field_value() const
Definition: AtlasDetectorID.h:548
ConstRangeIterator::end
ConstRangeIterator end() const
Definition: RangeIterator.cxx:32
IdDictField
Definition: IdDictField.h:15
MultiRange::cardinality
size_type cardinality() const
Computes a possible cardinality from all ranges.
Definition: MultiRange.cxx:82
ZdcID::m_zdc_impl
IdDictFieldImplementation m_zdc_impl
Definition: ZdcID.h:120
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
ZdcID::m_channel_impl
IdDictFieldImplementation m_channel_impl
Definition: ZdcID.h:124
ZdcID::m_module_vec
id_vec m_module_vec
Definition: ZdcID.h:116
AtlasDetectorID
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
Definition: AtlasDetectorID.h:52
ZdcID::m_module_hash_max
size_type m_module_hash_max
Definition: ZdcID.h:114
ZdcID::channel
int channel(const Identifier &id) const
Definition: ZdcID.h:175
ZdcID::m_TYPE_INDEX
size_type m_TYPE_INDEX
Definition: ZdcID.h:109
Identifier
Definition: IdentifierFieldParser.cxx:14