ATLAS Offline Software
Loading...
Searching...
No Matches
TileTBID.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5/***************************************************************************
6 Access to Tile Calorimeter TB data
7 -----------------------------------------
8 ***************************************************************************/
9
10
13#include "IdDict/IdDictField.h"
14#include "IdDict/IdDictMgr.h"
15#include "IdDict/IdDictRegion.h"
18#include "CxxUtils/StrFormat.h"
19
20#include <algorithm>
21#include <cassert>
22#include <cstdio>
23#include <iostream>
24
26
27
29 : AtlasDetectorID("TileTBID", "tile")
31 , m_SYSTEM_INDEX(999)
32 , m_SECTION_INDEX(999)
33 , m_TYPE_INDEX(999)
34 , m_MODULE_INDEX(999)
35 , m_CHANNEL_INDEX(999)
36 , m_dict(nullptr)
40{
41}
42
43TileTBID::~TileTBID() = default;
44
45//
46// TileTBID methods
47//
48std::string
49TileTBID::to_string( const Identifier & id, int level ) const
50{
51 IdContext context;
52 switch (level) {
53 case 1: case -3:
54 case 2: case -2: context = type_context(); break;
55 case 3: case -1: context = module_context(); break;
56 case 4: case 0: context = channel_context(); break;
57 default:
58 return "TileTBID::to_string - unknown context";
59 }
60
61 ExpandedIdentifier exp_id;
62 if ( get_expanded_id(id,exp_id,&context) ) {
63 return "TileTBID::to_string - can not expand ID";
64 } else {
65 return (std::string)(exp_id);
66 }
67}
68
69//
70// get methods
71//
72int
74{
75 int field = 0;
76 if(!m_dict->unpack(id, 0, index, m_tile_region_index, field)) {
77 return field;
78 } else {
79 return 0;
80 }
81}
82
83//
84// check methods
85//
86bool
88{
89 bool result = is_tile(id)
90 && ( section(id) == TileTBID::TILE_TESTBEAM );
91
92 return result;
93}
94
95//
96// TileCal TB ID
97//
100{
101 Identifier compactID(m_base_tile_type);
102 return compactID;
103}
104
105//
106// Build type & module id
107//
109TileTBID::type_id ( int type, bool checks ) const
110{
111 if(checks) {
112
113 // Check that id is within allowed range
114
115 // Fill expanded id
117 expId << TileTBID::TILE_TESTBEAM << type;
118
119 if (!m_full_type_range.match(expId)) {
120 std::string errorMessage = "TileTBID::type_id() result is not OK: ID, range = "
121 + std::string(expId) + " , " + (std::string)m_full_type_range;
122 throw TileID_Exception(errorMessage , 2);
123 }
124 }
125
126 Identifier compactID(m_base_tile_type);
127 m_type_impl.pack (type,compactID);
128
129 return (compactID);
130}
133{
134 return type_id (type, do_checks());
135}
136
137
139TileTBID::module_id ( int type, int module, bool checks ) const
140{
141 if(checks) {
142
143 // Check that id is within allowed range
144
145 // Fill expanded id
147 expId << TileTBID::TILE_TESTBEAM << type << module;
148
149 if (!m_full_module_range.match(expId)) {
150 std::string errorMessage = "TileTBID::module_id() result is not OK: ID, range = "
151 + std::string(expId) + " , " + (std::string)m_full_type_range;
152 throw TileID_Exception(errorMessage , 1);
153 }
154 }
155
156 Identifier compactID(m_base_tile_type);
157 m_type_impl.pack (type,compactID);
158 m_module_impl.pack (module,compactID);
159
160 return (compactID);
161}
163TileTBID::module_id ( int type, int module ) const
164{
165 return module_id (type, module, do_checks());
166}
167
168//
169// Build channel id
170//
172TileTBID::channel_id ( int type, int module, int channel, bool checks ) const
173{
174 if(checks) {
175
176 // Check that id is within allowed range
177
178 // Fill expanded id
180 expId << TileTBID::TILE_TESTBEAM << type << module << channel;
181
182 if (!m_full_channel_range.match(expId)) {
183 std::string errorMessage = "TileTBID::channel_id() result is not OK: ID, range = "
184 + std::string(expId) + " , " + (std::string)m_full_type_range;
185 throw TileID_Exception(errorMessage , 1);
186 }
187 }
188
189 Identifier compactID(m_base_tile_type);
190 m_type_impl.pack (type,compactID);
191 m_module_impl.pack (module,compactID);
192 m_channel_impl.pack (channel,compactID);
193
194 return (compactID);
195}
197TileTBID::channel_id ( int type, int module, int channel ) const
198{
199 return channel_id (type, module, channel, do_checks());
200}
201
203TileTBID::module_id ( const Identifier & any_id ) const
204{
205 Identifier compactId(any_id);
206 m_channel_impl.reset(compactId);
207
208 return (compactId);
209}
210
213 int channel ) const
214{
215 Identifier compactId(module_id);
216 m_channel_impl.pack(channel,compactId);
217
218 if(m_do_checks) {
219
220 // Check that id is within allowed range
221
222 // Fill expanded id
223 ExpandedIdentifier expId;
224
225 IdContext context = module_context();
226 if (get_expanded_id(compactId, expId, &context)) {
227 std::string errorMessage = "TileTBID::channel_id(module_id,channel) result is not OK: ID = "
228 + compactId.getString() ;
229 throw TileID_Exception(errorMessage , 1);
230
231 // region_id.show();
232 }
233
234 expId << channel;
235
236 if (!m_full_channel_range.match(expId)) {
237 std::string errorMessage = "TileTBID::channel_id() result is not OK: ID, range = "
238 + (std::string)expId + " , " + (std::string)m_full_type_range;
239 throw TileID_Exception(errorMessage , 1);
240 }
241 }
242
243 return compactId;
244}
245
247 int module, Identifier& module_id )
248{
250 IdContext context = type_context();
251 get_expanded_id(type_id, id, &context);
252 id << module;
253 if (!m_full_module_range.match(id)) {
254 return false;
255 }
256
257 Identifier compactId(0);
258 if (!m_dict->pack32(id,0, id.fields() - 1,compactId)) {
259 module_id = compactId;
260 }
261 return true;
262}
263
265 int module, int channel, Identifier& channel_id )
266{
268 IdContext context = type_context();
269 get_expanded_id(type_id, id, &context);
270 id << module << channel;
271 if (!m_full_channel_range.match(id)) {
272 return false;
273 }
274
275 Identifier compactId(0);
276 if (!m_dict->pack32(id,0, id.fields() - 1,compactId)) {
277 channel_id = compactId;
278 }
279 return true;
280}
281
284{
286 IdContext context = module_context();
287 get_expanded_id(module_id, id, &context);
288 id << channel;
289 if (!m_full_channel_range.match(id)) {
290 return false;
291 }
292
293 Identifier compactId(0);
294 if (!m_dict->pack32(id,0, id.fields() - 1,compactId)) {
295 channel_id = compactId;
296 }
297 return true;
298}
299
302{
304 return {id, 0, m_TYPE_INDEX};
305}
306
309{
311 return {id, 0, m_MODULE_INDEX};
312}
313
316{
318 return {id, 0, m_CHANNEL_INDEX};
319}
320
321int TileTBID::get_id (const IdentifierHash& hash_id, Identifier& id, const IdContext* context ) const
322{
323 int result = 1;
324 id.clear();
325
326 size_t begin = (context) ? context->begin_index(): 0;
327 // cannot get hash if end is 0:
328 size_t end = (context) ? context->end_index() : 0;
329
330 if (0 == begin) {
331
332 if ( m_CHANNEL_INDEX == end ) {
333 if (hash_id < (unsigned int)(m_channel_vec.end() - m_channel_vec.begin())) {
334 id = m_channel_vec[hash_id];
335 result = 0;
336 } else {
337 ATH_MSG_ERROR("get_id: channel hash_id is out of range " << hash_id
338 << " => " << m_channel_vec.size());
339 }
340 } else if ( m_MODULE_INDEX == end ) {
341 if (hash_id < (unsigned int)(m_module_vec.end() - m_module_vec.begin())) {
342 id = m_module_vec[hash_id];
343 result = 0;
344 } else {
345 ATH_MSG_ERROR("get_id: module hash_id is out of range "
346 << hash_id << " => " << m_module_vec.size());
347 }
348 } else if ( m_TYPE_INDEX == end ) {
349 if (hash_id < (unsigned int)(m_type_vec.end() - m_type_vec.begin())) {
350 id = m_type_vec[hash_id];
351 result = 0;
352 } else {
353 ATH_MSG_ERROR("get_id: TYPE hash_id is out of range " << hash_id
354 << " => " << m_type_vec.size());
355 }
356 }
357 }
358 return(result);
359}
360
361
362int TileTBID::get_hash (const Identifier& id, IdentifierHash& hash_id, const IdContext* context ) const
363{
364
365 hash_id = 0;
366 int result = 1;
367
368 size_t begin = (context) ? context->begin_index(): 0;
369 size_t end = (context) ? context->end_index() : 0;
370
371 if (0 == begin) {
372 if ( m_CHANNEL_INDEX == end ) {
373 std::vector<Identifier>::const_iterator it = std::lower_bound(m_channel_vec.begin(),m_channel_vec.end(),id);
374 if ( it != m_channel_vec.end() ){
375 hash_id = it - m_channel_vec.begin();
376 result = 0;
377 }
378 } else if ( m_MODULE_INDEX == end ) {
379 std::vector<Identifier>::const_iterator it = std::lower_bound(m_module_vec.begin(),m_module_vec.end(),id);
380 if ( it != m_module_vec.end() ){
381 hash_id = it - m_module_vec.begin();
382 result = 0;
383 }
384 } else if ( m_TYPE_INDEX == end ) {
385 std::vector<Identifier>::const_iterator it = std::lower_bound(m_type_vec.begin(),m_type_vec.end(),id);
386 if ( it != m_type_vec.end() ){
387 hash_id = it - m_type_vec.begin();
388 result = 0;
389 }
390 }
391 }
392
393 return (result);
394}
395
397{
398 ATH_MSG_INFO("initialize_from_dictionary ");
399
400 // Check whether this helper should be reinitialized
401 if (!reinitialize(dict_mgr)) {
402 ATH_MSG_DEBUG("Request to reinitialize not satisfied - tags have not changed");
403 return (0);
404 }
405 else {
406 ATH_MSG_DEBUG("(Re)initialize");
407 }
408
409 ATH_MSG_DEBUG("calling base initialize_from_dictionary");
410
411 // init base object
412 if(AtlasDetectorID::initialize_from_dictionary(dict_mgr)) return (1);
413
414 // Register version of the TileCalo dictionary
415 if (register_dict_tag(dict_mgr, "TileCalorimeter")) return(1);
416
417 m_dict = dict_mgr.find_dictionary ("TileCalorimeter");
418 if(!m_dict) {
419 ATH_MSG_ERROR("cannot access TileCalorimeter dictionary ");
420 return 1;
421 }
422
423 // Initialize the field indices
424 if(initLevelsFromDict()) return (1);
425
426
427 // Find value for the field Tile Calorimeter
428 const IdDictDictionary* atlasDict = dict_mgr.find_dictionary ("ATLAS");
429 int tileField = -1;
430 if (atlasDict->get_label_value("subdet", "TileCalorimeter", tileField)) {
431 ATH_MSG_ERROR("Could not get value for label 'TileCalorimeter' of field 'subdet' in dictionary "
432 << atlasDict->name());
433 return (1);
434 }
435
436 // Find value for the field Tile Testbeam
437 int tiletbField = -1;
438 if (m_dict->get_label_value("section", "Testbeam", tiletbField)) {
439 ATH_MSG_ERROR("Could not get value for label 'Testbeam' of field 'section' in dictionary "
440 << m_dict->name());
441 return (1);
442 }
443
444 // Set up id for region and range prefix
445 ExpandedIdentifier reg_id;
446 reg_id.add(tileField);
447 reg_id.add(tiletbField);
448 Range prefix;
449
450 m_full_channel_range= m_dict->build_multirange(reg_id, prefix, "tbchannel");
451 m_full_module_range = m_dict->build_multirange(reg_id, prefix, "tbmodule");
452 m_full_type_range = m_dict->build_multirange(reg_id, prefix, "type");
453
454 ATH_MSG_DEBUG("initialize_from_dict : " );
455 ATH_MSG_DEBUG(" type range -> " << (std::string)m_full_type_range );
456 ATH_MSG_DEBUG(" module range -> " << (std::string)m_full_module_range );
457 ATH_MSG_DEBUG(" channel range -> " << (std::string)m_full_channel_range);
458
459 // Setup the hash tables
460 if(init_hashes()) return (1);
461
462 return 0;
463
464}
465
466int TileTBID::get_expanded_id (const Identifier& id, ExpandedIdentifier& exp_id, const IdContext* context) const
467{
468 int result = 1;
469
470 size_t begin = (context) ? context->begin_index() : 0;
471 size_t end = (context) ? context->end_index() : m_CHANNEL_INDEX;
472 assert (end <= m_CHANNEL_INDEX);
473
474 if (0 == end) {
475 result = 0;
476 }
477 else if ( 0 == begin) {
479 result = m_dict->unpack(group(), id, empty, end, exp_id);
480 }
481 else {
482 result = m_dict->unpack(group(), id, context->prefix_id(), end, exp_id);
483 }
484 return result;
485}
486
488{
489 if(!m_dict) {
490 ATH_MSG_ERROR("initLevelsFromDict - dictionary NOT initialized ");
491 return (1);
492 }
493
494 // Find out which identifier field corresponds to each level.
495
496 m_SYSTEM_INDEX = 999;
497 m_SECTION_INDEX = 999;
498 m_TYPE_INDEX = 999;
499 m_MODULE_INDEX = 999;
500 m_CHANNEL_INDEX = 999;
501
502 // Save index to a Tile region for unpacking
504 if (m_dict->find_region(expId,m_tile_region_index)){
505 ATH_MSG_ERROR("initLevelsFromDict - unable to find tile region index: expId, reg "
506 << (std::string)expId << " " << m_tile_region_index);
507 return (1);
508 }
509
510 // Fing a Tile region
511 const IdDictField* field = m_dict->find_field("subdet") ;
512 if (field) {
513 m_SYSTEM_INDEX = field->index();
514 }
515 else {
516 ATH_MSG_ERROR("initLevelsFromDict - unable to find 'subdet' field ");
517 return (1);
518 }
519
520 field = m_dict->find_field("section") ;
521 if (field) {
522 m_SECTION_INDEX = field->index();
523 }
524 else {
525 ATH_MSG_ERROR("initLevelsFromDict - unable to find 'section' field ");
526 return (1);
527 }
528
529 field = m_dict->find_field("type") ;
530 if (field) {
531 m_TYPE_INDEX = field->index();
532 }
533 else {
534 ATH_MSG_ERROR("initLevelsFromDict - unable to find 'type' field ");
535 return (1);
536 }
537
538 field = m_dict->find_field("tbmodule") ;
539 if (field) {
540 m_MODULE_INDEX = field->index();
541 }
542 else {
543 ATH_MSG_ERROR("initLevelsFromDict - unable to find 'tbmodule' field ");
544 return (1);
545 }
546
547 field = m_dict->find_field("tbchannel") ;
548 if (field) {
549 m_CHANNEL_INDEX = field->index();
550 }
551 else {
552 ATH_MSG_ERROR("initLevelsFromDict - unable to find 'tbchannel' field ");
553 return (1);
554 }
555
556 /* Set the field implementations */
557 const IdDictRegion& region = m_dict->region(m_tile_region_index);
558
564
568
569 ATH_MSG_DEBUG("initLevelsFromDict decode index and bit fields for each level: " );
570 ATH_MSG_DEBUG(" system [" << m_SYSTEM_INDEX << "] " << m_system_impl.show_to_string() );
571 ATH_MSG_DEBUG(" section [" << m_SECTION_INDEX << "] " << m_section_impl.show_to_string() );
572 ATH_MSG_DEBUG(" type [" << m_TYPE_INDEX << "] " << m_type_impl.show_to_string() );
573 ATH_MSG_DEBUG(" module [" << m_MODULE_INDEX << "] " << m_module_impl.show_to_string() );
574 ATH_MSG_DEBUG(" channel [" << m_CHANNEL_INDEX << "] " << m_channel_impl.show_to_string() );
575
576 return(0) ;
577}
578
580{
581 // type hash
582 unsigned int nids = 0;
583 std::set<Identifier> ids;
584 for (unsigned int i = 0; i < m_full_type_range.size(); ++i) {
585 const Range& range = m_full_type_range[i];
586 ConstRangeIterator rit(range);
587 auto first = rit.begin();
588 auto last = rit.end();
589 for (; first != last; ++first) {
590 const ExpandedIdentifier& exp_id = (*first);
591 Identifier id = type_id (exp_id[m_TYPE_INDEX]);
592 if(!(ids.insert(id)).second){
593 ATH_MSG_ERROR("init_hashes "
594 << " Error: duplicated id for type id. nids= " << nids
595 << " compact Id " << show_to_string(id));
596 }
597 nids++;
598 }
599 }
600 m_type_hash_max = m_full_type_range.cardinality();
601 if ( fill_vec(ids, m_type_hash_max, m_type_vec) ) return (1);
602
603 // module hash
604 nids = 0;
605 ids.clear();
606 for (unsigned int i = 0; i < m_full_module_range.size(); ++i) {
607 const Range& range = m_full_module_range[i];
608 ConstRangeIterator rit(range);
609 auto first = rit.begin();
610 auto last = rit.end();
611 for (; first != last; ++first) {
612 const ExpandedIdentifier& exp_id = (*first);
613 Identifier id = module_id (exp_id[m_TYPE_INDEX],
614 exp_id[m_MODULE_INDEX]);
615 if(!(ids.insert(id)).second){
616 ATH_MSG_ERROR("init_hashes "
617 << " Error: duplicated id for module id. nids= " << nids
618 << " compact Id " << show_to_string(id));
619 }
620 nids++;
621 }
622 }
624 if ( fill_vec(ids, m_module_hash_max, m_module_vec) ) return (1);
625
626 // channel hash
627 nids = 0;
628 ids.clear();
629 for (unsigned int i = 0; i < m_full_channel_range.size(); ++i) {
630 const Range& range = m_full_channel_range[i];
631 ConstRangeIterator rit(range);
632 auto first = rit.begin();
633 auto last = rit.end();
634 for (; first != last; ++first) {
635 const ExpandedIdentifier& exp_id = (*first);
636 Identifier id = channel_id (exp_id[m_TYPE_INDEX],
637 exp_id[m_MODULE_INDEX],
638 exp_id[m_CHANNEL_INDEX]);
639 if(!(ids.insert(id)).second){
640 ATH_MSG_ERROR("init_hashes "
641 << " Error: duplicated id for channel id. nids= " << nids
642 << " compact Id " << show_to_string(id));
643 }
644 nids++;
645 }
646 }
648 if ( fill_vec(ids, m_channel_hash_max, m_channel_vec) ) return (1);
649
650 return (0);
651}
652
653int TileTBID::fill_vec (std::set<Identifier> & ids,
654 unsigned int hash_max, std::vector<Identifier> & vec)
655{
656
657 if(ids.size() != hash_max) {
658
659 ATH_MSG_ERROR("fill_vec "
660 << " Error: set size NOT EQUAL to hash max. size " << ids.size()
661 << " hash max " << hash_max);
662
663 return (1);
664 }
665
666 vec.resize(hash_max);
667
668 std::set<Identifier>::const_iterator first = ids.begin();
669 std::set<Identifier>::const_iterator last = ids.end();
670
671 int index = 0;
672 for (;first != last; ++first) {
673 vec[index++] = (*first);
674 }
675
676 return (0);
677}
678
682
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
std::vector< size_t > vec
Provide helper functions to create formatted strings.
static const Attributes_t empty
bool m_do_checks
Flag for subclasses to know whether or not to perform checks.
virtual int initialize_from_dictionary(const IdDictMgr &dict_mgr) override
Initialization from the identifier dictionary.
bool reinitialize(const IdDictMgr &dict_mgr)
Test whether an idhelper should be reinitialized based on the change of tags.
int register_dict_tag(const IdDictMgr &dict_mgr, const std::string &dict_name)
Register the file and tag names for a particular IdDict dictionary.
virtual bool do_checks(void) const override
Checks are performed by default in debug compilation and NOT in optimized compilation.
std::string show_to_string(Identifier id, const IdContext *context=0, char sep='.') const
or provide the printout in string form
bool is_tile(Identifier id) const
ExpandedIdentifier tile_exp(void) const
int tile_field_value() const
const std::string & group() const
Group name for this helper.
AtlasDetectorID(const std::string &name, const std::string &group)
ConstRangeIterator end() const
ConstRangeIterator begin() const
void add(element_type value)
Append a value into a new field.
This class saves the "context" of an expanded identifier (ExpandedIdentifier) for compact or hash ver...
Definition IdContext.h:26
size_type begin_index() const
Definition IdContext.h:45
size_type end_index() const
Definition IdContext.h:46
const ExpandedIdentifier & prefix_id() const
Accessors.
Definition IdContext.h:44
int get_label_value(const std::string &field, const std::string &label, int &value) const
const std::string & name() const
Dictionary name.
const IdDictDictionary * find_dictionary(const std::string &name) const
Access dictionary by name.
const IdDictFieldImplementation & implementation(size_t i) const
This is a "hash" representation of an Identifier.
std::string getString() const
Provide a string form of the identifier - hexadecimal.
A Range describes the possible ranges for the field values of an ExpandedIdentifier.
Exception class for Tile Identifiers.
virtual ~TileTBID()
Default destructor.
virtual int initialize_from_dictionary(const IdDictMgr &dict_mgr)
initialization from the identifier dictionary
Definition TileTBID.cxx:396
Identifier type_id(int type) const
identifer for Tile testbeam detectors of a give type
Definition TileTBID.cxx:132
int fill_vec(std::set< Identifier > &ids, unsigned int hash_max, std::vector< Identifier > &vec)
Definition TileTBID.cxx:653
int initLevelsFromDict()
Definition TileTBID.cxx:487
IdContext channel_context() const
idContext for channels
Definition TileTBID.cxx:315
bool is_tiletb(const Identifier &id) const
Test ID if it is TileTBID.
Definition TileTBID.cxx:87
virtual int get_id(const IdentifierHash &hash_id, Identifier &id, const IdContext *context=0) const
create compact id from hash id (return == 0 for OK)
Definition TileTBID.cxx:321
int get_expanded_id(const Identifier &id, ExpandedIdentifier &exp_id, const IdContext *context) const
create expanded Identifier from Identifier (return == 0 for OK)
Definition TileTBID.cxx:466
Identifier module_id(int type, int module) const
identifer for one module of a Tile testbeam detector
Definition TileTBID.cxx:163
TileTBID()
Default constructor.
Definition TileTBID.cxx:28
int section(const Identifier &id) const
extract section field from TileTB identifier
std::string to_string(const Identifier &id, int level=0) const
extract all fields from TileTB identifier Identifier get_all_fields ( const Identifier & id,...
Definition TileTBID.cxx:49
int init_hashes()
Definition TileTBID.cxx:579
int type(const Identifier &id) const
extract type field from TileTB identifier
Identifier channel_id(int type, int module, int channel) const
identifer for one channel of a Tile testbeam detector
Definition TileTBID.cxx:197
IdContext type_context() const
access to IdContext's which define which levels of fields are contained in the id
Definition TileTBID.cxx:301
int get_field(const Identifier &id, size_type index) const
unpack one field from Identifier
Definition TileTBID.cxx:73
int channel(const Identifier &id) const
extract channel field from TileTB identifier
IdContext module_context() const
idContext for modules
Definition TileTBID.cxx:308
virtual int get_hash(const Identifier &id, IdentifierHash &hash_id, const IdContext *context=0) const
create hash id from compact id (return == 0 for OK)
Definition TileTBID.cxx:362
Identifier tiletb_id() const
identifer for all Tile testbeam detectors
Definition TileTBID.cxx:99
std::string strformat(const char *fmt,...)
return a std::string according to a format fmt and varargs
Definition StrFormat.cxx:49
Definition index.py:1