ATLAS Offline Software
LArIdHelperStandalone.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 from PyCool import cool
3 from CoolConvUtilities.AtlCoolLib import indirectOpen
4 import ROOT
5 import os,sys
6 from AthenaCommon.Logging import logging
7 
8 def IdToNum(ident):
9  return ident.get_identifier32().get_compact()
10 
11 def NumToId(idnum):
12  return ROOT.Identifier(ROOT.Identifier32(idnum))
13 
14 
16  def __init__(self):
17  self._msg=logging.getLogger("LArIdHelper")
18  #Set up LArOnlineID helper class in standalone mode (from xml file)
19  from ROOT import IdDictParser
20  parser=IdDictParser()
21  #Get xml files:
22  xmlpath=None
23  for dd in os.getenv('XMLPATH').split(os.pathsep):
24  d=dd+"/IdDictParser/ATLAS_IDS.xml"
25  if os.access(d,os.R_OK):
26  xmlpath=dd
27  break
28  if not xmlpath:
29  self._msg.error("unable to locate identifier dictionaries in $XMLPATH")
30  sys.exit(-1)
31 
32  parser.register_external_entity("LArCalorimeter",xmlpath+"/IdDictParser/IdDictLArCalorimeter_DC3-05-Comm-01.xml")
33  parser.register_external_entity("Calorimeter",xmlpath+"/IdDictParser/IdDictCalorimeter_L1Onl.xml")
34  parser.register_external_entity("TileCalorimeter",xmlpath+"/IdDictParser/IdDictTileCalorimeter.xml")
35 
36 
37  idd = parser.parse(xmlpath+"/IdDictParser/ATLAS_IDS.xml")
38  from ROOT import LArOnlineID
40  stat=self._larOnlHelper.initialize_from_dictionary(idd)
41  if stat==1:
42  self._msg.error("failed to init LArOnlineID")
43  sys.exit(-1)
44 
45 
46  from ROOT import LArEM_ID, LArHEC_ID, LArFCAL_ID, LArMiniFCAL_ID, TileID
47 
48  self._allHelpers=[]
49  for subHelper in (LArEM_ID, LArHEC_ID, LArFCAL_ID, LArMiniFCAL_ID, TileID):
50  helper=subHelper()
51  helper.set_do_neighbours(False)
52  stat=helper.initialize_from_dictionary(idd)
53  if stat==1:
54  self._msg.error("failed to init" + str(subHelper))
55  else:
56  self._allHelpers.append(helper)
57  pass
58  from ROOT import CaloCell_ID
60  #Note: It's important that all sub-helpers are part of this class. The CaloCell_ID keeps pointers to the individual sub-helpers,
61  #but python's garbage collector doesn't know about this relationship. So it may delete the sub-helpers at any time.
62 
63  self._cabling=None
64  return
65 
66 
67  def larOnlHelper(self):
68  return self._larOnlHelper
69 
70  def caloHelper(self):
71  return self._caloHelper
72 
73  def getCabling(self):
74  if self._cabling: return self._cabling
75 
76  lardb=indirectOpen("COOLONL_LAR/CONDBR2")
77  f=lardb.getFolder("/LAR/Identifier/OnOffIdMap")
78  ptr=f.findObject(cool.ValidityKeyMax-1,cool.ChannelId(0),"LARIdentifierOnOffIdMap-RUN2-001")
79  payload=ptr.payload()["OnlineHashToOfflineId"]
80  on2off=payload.read()
81  nChans=int(payload.size()/4)
82 
83  self._cabling=ROOT.LArOnOffIdMapping(self._larOnlHelper,self._caloHelper)
84  onlHash2OflIdVec=self._cabling.getOnlHash2OflId()
85  oflHash2OnlIdVec=self._cabling.getOflHash2OnId()
86 
87 
88  i=0
89  nConnected=0
90  iChan=0
91  while (iChan<nChans):
92  idnum=int.from_bytes(on2off[i:i+4],'little')
93  #self._msg.debug("From Blob[%i]=0x%x" % (iChan,idnum))
94  oflId=ROOT.Identifier(ROOT.Identifier32(idnum))
95  if oflId.is_valid():
96  oflHash=self._caloHelper.calo_cell_hash(oflId)
97  onlHash=ROOT.IdentifierHash(iChan)
98  if oflHash.value()>=oflHash2OnlIdVec.size():
99  self._msg.error("invalid offline hash %i", oflHash.value())
100  return
101 
102  if onlHash.value()>=onlHash2OflIdVec.size():
103  self._msg.error("invalid offline hash %i", onlHash.value())
104  return
105  nConnected+=1
106  onlId=self._larOnlHelper.channel_Id(onlHash)
107  onlHash2OflIdVec[iChan]=oflId
108  oflHash2OnlIdVec[oflHash.value()]=onlId
109  pass
110  else:
111  #print ("idnum %i give invalid offline id 0x%x",(idnum,oflId.get_identifier32().get_compact()))
112  pass
113  i+=4
114  iChan+=1
115  pass
116 
117  self._msg.info("Found identifier mapping for %i connected channels", nConnected)
118 
119  return self._cabling
120 
121 
122 if __name__=="__main__":
123 
124  #usage example & self-test
126  cc=c.getCabling()
127 
128  onlId1=NumToId(978347776)
129  oflId1=IdToNum(cc.cnvToIdentifier(onlId1))
130  assert(oflId1==740295168)
131 
132 
133  #cell_id (const int subCalo,
134  # const int barec_or_posneg,
135  # const int sampling_or_fcalmodule,
136  # const int region_or_dummy,
137  # const int eta,
138  # const int phi ) const;
139 
140  oflId2=c.caloHelper().cell_id(0, #LArEM
141  -3, #EMECIW-C
142  1, #layer 0
143  0, #region 0
144  1, #ieta
145  0, #iphi
146  )
147  oflId2Num=IdToNum(oflId2)
148  print (oflId2Num)
149  assert(oflId2Num==740295168)
150 
151  onlId2=cc.createSignalChannelID(oflId2)
152  assert(c.larOnlHelper().channel(onlId2)==103) #Channel on FrontEnd board
153  assert(c.larOnlHelper().feedthrough(onlId2)==10) #Feed-though number
154 
155  onlId2Num=IdToNum(onlId2)
156  assert(onlId2Num==978347776)
157 
grepfile.info
info
Definition: grepfile.py:38
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:24
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.LArIdHelperStandalone.NumToId
def NumToId(idnum)
Definition: LArIdHelperStandalone.py:11
python.LArIdHelperStandalone.LArIdHelper._allHelpers
_allHelpers
Definition: LArIdHelperStandalone.py:48
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.LArIdHelperStandalone.LArIdHelper._caloHelper
_caloHelper
Definition: LArIdHelperStandalone.py:59
python.LArIdHelperStandalone.LArIdHelper.__init__
def __init__(self)
Definition: LArIdHelperStandalone.py:16
python.LArIdHelperStandalone.LArIdHelper._cabling
_cabling
Definition: LArIdHelperStandalone.py:63
python.LArIdHelperStandalone.LArIdHelper
Definition: LArIdHelperStandalone.py:15
IdDictParser
Definition: IdDictParser.h:12
python.LArIdHelperStandalone.IdToNum
def IdToNum(ident)
Definition: LArIdHelperStandalone.py:8
CaloCell_ID
Helper class for offline cell identifiers.
Definition: CaloCell_ID.h:34
python.LArIdHelperStandalone.LArIdHelper.larOnlHelper
def larOnlHelper(self)
Definition: LArIdHelperStandalone.py:67
LArOnlineID
Definition: LArOnlineID.h:20
python.LArIdHelperStandalone.LArIdHelper.getCabling
def getCabling(self)
Definition: LArIdHelperStandalone.py:73
python.LArIdHelperStandalone.LArIdHelper._msg
_msg
Definition: LArIdHelperStandalone.py:17
str
Definition: BTagTrackIpAccessor.cxx:11
python.LArIdHelperStandalone.LArIdHelper._larOnlHelper
_larOnlHelper
Definition: LArIdHelperStandalone.py:39
error
Definition: IImpactPoint3dEstimator.h:70
python.AtlCoolLib.indirectOpen
def indirectOpen(coolstr, readOnly=True, debug=False)
Definition: AtlCoolLib.py:130
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.LArIdHelperStandalone.LArIdHelper.caloHelper
def caloHelper(self)
Definition: LArIdHelperStandalone.py:70