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