ATLAS Offline Software
Loading...
Searching...
No Matches
LArIdHelperStandalone.py
Go to the documentation of this file.
1# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2from PyCool import cool
3from CoolConvUtilities.AtlCoolLib import indirectOpen
4import ROOT
5import os,sys
6from AthenaCommon.Logging import logging
7
8def IdToNum(ident):
9 return ident.get_identifier32().get_compact()
10
11def 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
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
122if __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
Helper class for offline cell identifiers.
Definition CaloCell_ID.h:34
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177