ATLAS Offline Software
LArBadChannelBrowserLib.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 
4 from AthenaPython.PyAthena import StatusCode
5 import AthenaPython.PyAthena as PyAthena
6 import sys,os
7 from PyCool import cool
8 import cppyy
9 
10 import LArBadChannelBrowserTools
11 import LArDBFolderBrowser_BadChan
12 import LArDBFolderBrowser_MissingFEBs
13 
14 STATUS_INIT="INIT"
15 STATUS_NEW="NEW"
16 STATUS_REMOVED="DEL"
17 STATUS_MODIFIED="MOD"
18 
19 BAD_CHANNEL=0
20 MISSING_FEB=1
21 
22 STATUS_NOT_MISSING_FEB="0K"
23 STATUS_MISSING_FEB="missing"
24 
26  """My first python algorithm ( and most probably not the last one... )
27  """
28 
29  def __init__(self, name="LArBadChannelBrowserAlg", **kw):
30 
31  kw['name'] = name
32  super(LArBadChannelBrowserAlg,self).__init__(**kw)
33 
34  return
35 
36 
37  def initialize(self):
38 
41 
42  # -----------------------------------------------------------
43  # Store gate service
44  self.msg.info('initializing [%s]...',self.name())
45  self.msg.debug('retrieve StoreGateSvc pointer...')
46  self.sg = PyAthena.py_svc('StoreGateSvc')
47  if self.sg is None:
48  self.msg.error('Problem retrieving StoreGateSvc pointer !')
49  return StatusCode.Failure
50  else:
51  self.msg.info('retrieved [%s]', self.sg.name())
52 
53  # -----------------------------------------------------------
54  # Detector store service
55  self.msg.debug('retrieve DetectorStoreSvc pointer...')
56  self.det = PyAthena.py_svc('DetDescrCnvSvc')
57  if self.det is None:
58  self.msg.error('Problem retrieving DetectorStoreSvc pointer !')
59  return StatusCode.Failure
60  else:
61  self.msg.info('retrieved [%s]', self.det.name())
62 
63  # -----------------------------------------------------------
64  # Message service
65  self.msg.info('initializing [%s]...',self.name())
66  self.msg.debug('retrieve MessageSvc pointer...')
67  self.msgSvc = PyAthena.py_svc('MessageSvc')
68  if self.msgSvc is None:
69  self.msg.error('Problem retrieving MessageSvc pointer !')
70  return StatusCode.Failure
71  else:
72  self.msg.info('retrieved [%s]', self.msgSvc.name())
73 
74  # -----------------------------------------------------------
75  # get database service and open database
76  self.msg.debug('retrieve database service...')
77  self.dbSvc = cool.DatabaseSvcFactory.databaseService()
78 
79  # -----------------------------------------------------------
80  # Read local copy of condb => LarBadChannel folder
81 # dbstring="sqlite://;schema=./myCondLArDB.db;dbname=CONDBR2"
82  dbstring="COOLONL_LAR/CONDBR2"
83  try:
84  self.db = self.dbSvc.openDatabase(dbstring,True)
85  except Exception as e:
86  print ('Problem opening database',e)
87  sys.exit(-1)
88  print ("Opened database",dbstring)
89 
90  # -----------------------------------------------------------
91  # Initialize onlineID from detector store
92  self.msg.info('inlineID initialization...')
93  from StoreGateBindings.Bindings import StoreGate
94  detStore = StoreGate.pointer("DetectorStore")
95  self.onlineID=detStore.retrieve("LArOnlineID","LArOnlineID")
96 
97  # -----------------------------------------------------------
98  # Initialize LArCabling service
99  self.larCablingSvc=PyAthena.py_tool("LArCablingService")
100  if self.larCablingSvc is None:
101  self.msg.error('Problem retrieving LArCablingService pointer !')
102  return StatusCode.Failure
103  else:
104  self.msg.info('retrieved [%s]', self.larCablingSvc.name())
105 
106  return StatusCode.Success
107 
108 
109  def execute(self):
110  self.msg.info('running execute...')
111  self.msg.info('dumping [%s] content', self.sg.name())
112  self.msg.info(self.sg.dump())
113 
114 
115  listOfFolders=["/LAR/BadChannels/BadChannels","/LAR/BadChannels/MissingFEBs"]
116  iSelectedFolder=LArBadChannelBrowserTools.ChooseOptionFromList("Folder list : ","","",listOfFolders,+1,False,{})
117 
118  if iSelectedFolder==1:
119  self.dbFolderName="/LAR/BadChannels/BadChannels"
120  self.folderId=BAD_CHANNEL
121  else:
122  self.dbFolderName="/LAR/BadChannels/MissingFEBs"
123  self.folderId=MISSING_FEB
124 
125 
126  # -----------------------------------------------------------
127  # Browse database a first time to catch tags name and IOV
128  listOfTags, dictIOV, self.iNbCoolChannel=self.GetTagsAndIOVFromDatabase(self.dbFolderName)
129 
130  # -----------------------------------------------------------
131  # Create an objec instance for all interfaced python classes
132 
133  LArBadChannelDBTools=cppyy.makeNamespace('LArBadChannelDBTools')
135 
136  self.class_LArBadChannel=cppyy.makeClass('LArBadChannel')
137  self.class_HWIdentifier=cppyy.makeClass('HWIdentifier')
138  self.class_LArBadChanBitPacking=cppyy.makeClass("LArBadChanBitPacking")
139 
140  if self.folderId==BAD_CHANNEL:
143  self.class_LArBadChannel,
144  self.class_HWIdentifier,
145  self.onlineID, self.larCablingSvc,
146  self.msg
147  )
148 
149  elif self.folderId==MISSING_FEB:
152  self.class_LArBadChannel,
153  self.class_HWIdentifier,
154  self.onlineID, self.larCablingSvc,
155  )
156 
157 
158  # -----------------------------------------------------------
159  # Select the tag
160  iSelectedTag=LArBadChannelBrowserTools.ChooseOptionFromList("Tag list : ","","",listOfTags,+1,False,{})
161  if iSelectedTag<1:
162  return
163  self.selectedTag=listOfTags[iSelectedTag-1]
164 
165  # -----------------------------------------------------------
166  # Construct IOV list based on selected tag
167  self.tagIOVDict={}
168  for iChannel in range(0,self.iNbCoolChannel):
169  self.tagIOVDict[iChannel]=dictIOV[self.selectedTag][iChannel][-1][0]
170 
171  # -----------------------------------------------------------
172  # Show database content
173 
175  if self.folderId==BAD_CHANNEL:
176  self.dbBrowserBadChan.BadChan_SetChannelNameDict(self.channelNameDict)
177 # sMessage="IOV : "+str(self.tagIOVDict)+" / channel : "+str(self.channelNameDict)
178 # LArBadChannelBrowserTools.TypeEnterToContinue(sMessage)
179 
180 
181  # -------------------------------------------------------------------------------------
182  #
183  # LOOP OVER COOL CHANNEL MODIFICATIONS
184  #
185  # -------------------------------------------------------------------------------------
186 
187  bEndOfCoolChannelModification=False
188  while not bEndOfCoolChannelModification:
189 
190  # -----------------------------------------------------------
191  # MISSING_FEB : go to correction menu
192  if self.folderId==MISSING_FEB:#
193 
194  coolChan=0
195  self.dbBrowserMissingFeb.MissingFeb_DisplayMissingFeb(coolChan)
196  endOfModif=self.dbBrowserMissingFeb.MissingFeb_ModifyMissingFeb(coolChan)
197 
198  if endOfModif=='q':
199  sDBName=os.environ["PWD"]+"/MissingFebUpdate.db"
200  if os.path.isfile(sDBName):
201  os.remove(sDBName)
202  dbstring="sqlite://;schema="+sDBName+";dbname=BADCHAN"
203  self.dbBrowserMissingFeb.MissingFeb_SaveMissingFebCorrectionsToDatabase(dbstring,self.dbSvc,self.dbFolderName,self.selectedTag)
204  self.dbBrowserMissingFeb.MissingFeb_CheckSavedDatabaseContent(dbstring,self.dbSvc,self.dbFolderName,self.selectedTag)
205 
206  return
207 
208  # -----------------------------------------------------------
209  # Select Cool channel
210 
211  sCoolChannelMenuDict=[("s","(summary)"),("a","(abort)"),("q","(save and quit)")]
212  listCoolChannel=[]
213  for i in range(0,self.iNbCoolChannel):
214  listCoolChannel.append(self.channelNameDict[i]+" (IOV "+str(self.tagIOVDict[i])+")")
215  repCoolChan=LArBadChannelBrowserTools.ChooseOptionFromList("Cool channel list : ","","",listCoolChannel,
216  0,False,sCoolChannelMenuDict)
217 
218  if repCoolChan=="a":
219  iAbortConfirmation=LArBadChannelBrowserTools.YesNoQuestion("Are you sure you want to quit ? ")
220  if iAbortConfirmation==1:
221  return
222  elif repCoolChan=="q":
223  print (" SAUVEGARDE FINALE")
224  if self.folderId==BAD_CHANNEL:
225  sDBName=os.environ["PWD"]+"/BadChannelUpdate.db"
226  if os.path.isfile(sDBName):
227  os.remove(sDBName)
228  dbstring="sqlite://;schema="+sDBName+";dbname=BADCHAN"
229  self.dbBrowserBadChan.BadChan_SaveBadChannelCorrectionsToDatabase(dbstring,self.dbSvc,self.dbFolderName,self.selectedTag)
230  self.dbBrowserBadChan.BadChan_CheckSavedDatabaseContent(dbstring,self.dbSvc,self.dbFolderName,self.selectedTag)
231 
232  bEndOfCoolChannelModification=True
233  elif repCoolChan=="s":
234  for i in range(0,self.iNbCoolChannel):
235  self.dbBrowserBadChan.BadChan_ShowBadChannelCorrectionsSummary(i)
236  else:
237  # -----------------------------------------------------------
238  # Display selected Cool channel
239  coolChan=int(repCoolChan)
240  if self.folderId==BAD_CHANNEL:
241  self.dbBrowserBadChan.BadChan_DisplayBadChanEntry(coolChan)
242  self.dbBrowserBadChan.BadChan_ModifyLarBadChannel(coolChan)
243 
244 
245  def finalize(self):
246  self.msg.info('finalizing...')
247  return StatusCode.Success
248 
249 
250  def GetTagsAndIOVFromDatabase(self,folderName):
251  """ First database browsing => retrieve tag names, IOVs and number of channels """
252 
253  # Get Folder
254  try:
255  f = self.db.getFolder(folderName)
256  print ("Analysing Folder " + str(folderName))
257  except Exception:
258  print ("Skipping " + str(folderName))
259  return
260 
261  # get tags
262  tags = f.listTags()
263 
264  iMaxChannelNumber=0
265  sIOVBeginEnd={}
266  for tag in tags:
267 
268  f.countObjects( cool.ValidityKeyMin,
269  cool.ValidityKeyMax,
270  cool.ChannelSelection.all(),
271  tag)
272 
273  objs = f.browseObjects( cool.ValidityKeyMin,
274  cool.ValidityKeyMax,
275  cool.ChannelSelection.all(),
276  tag)
277  while objs.hasNext():
278  obj = objs.next()
279 
280  if obj.channelId()>iMaxChannelNumber:
281  iMaxChannelNumber=obj.channelId()
282 
283  keyTag=tag
284  if keyTag not in sIOVBeginEnd:
285  sIOVBeginEnd[keyTag]={}
286 
287  keyChan=obj.channelId()
288  if keyChan not in sIOVBeginEnd[keyTag]:
289  sIOVBeginEnd[keyTag][keyChan]=[]
290 
291  sIOVBeginEnd[keyTag][keyChan].append((obj.since()>>32%0x100000000,obj.until()>>32%0x100000000))
292 # print ("IOV : ",obj.channelId()," : ",obj.since()>>32," ",obj.until()>>32)
293 
294  objs.close()
295 
296 
297  return (tags,sIOVBeginEnd,iMaxChannelNumber+1)
298 
299 
300  def ReadObjectsFromDatabase(self,folderName,tagName,tagIOVNumber):
301  """ Second database browsing => read cool channel corresponding to selected tag """
302 
303  channelNameDict={}
304 
305  # Get Folder
306  try:
307  f = self.db.getFolder(folderName)
308  print ("Analysing Folder " + str(folderName))
309  except Exception:
310  print ("Skipping " + str(folderName))
311  return
312 
313  # get tags
314  tags = f.listTags()
315 
316  for tag in tags:
317 
318  if tag!=tagName:
319  continue
320 
321  print ("-> tag : ",tag)
322 
323  f.countObjects( cool.ValidityKeyMin,
324  cool.ValidityKeyMax,
325  cool.ChannelSelection.all(),
326  tag)
327 
328  objs = f.browseObjects( cool.ValidityKeyMin,
329  cool.ValidityKeyMax,
330  cool.ChannelSelection.all(),
331  tag)
332  iObjet = 0
333  while objs.hasNext():
334  obj = objs.next()
335 
336  # Select cool channel vs IOV
337  if (obj.since()>>32%0x100000000)==tagIOVNumber[obj.channelId()]:
338 
339  print ("Found object", iObjet, end='')
340  print ("since [r,l]: [", obj.since() >> 32,',',obj.since()%0x100000000,']', end='')
341  print ("until [r,l]: [", obj.until() >> 32,',',obj.until()%0x100000000,']', end='')
342  print ("payload", obj.payload(), end='')
343  print ("chan",obj.channelId())
344 
345  payload=obj.payload()
346 
347  if self.folderId==BAD_CHANNEL:
348  sChannelKey=obj.channelId()
349  sChannelName=self.dbBrowserBadChan.BadChan_SetBadChannelDataFromPayload(sChannelKey,payload)
350  elif self.folderId==MISSING_FEB:
351  sChannelKey=obj.channelId()
352  sChannelName=self.dbBrowserMissingFeb.MissingFeb_SetBadChannelDataFromPayload(sChannelKey,payload)
353 
354  print (sChannelKey," ",sChannelName)
355  channelNameDict[sChannelKey]=sChannelName
356  print (str(channelNameDict))
357 
358  iObjet=iObjet+1
359 
360  objs.close()
361 
362  return channelNameDict
363 
364 
365 
366 
367  def DumpDatabasePayloadObjects(self,dbSvc,dbName,ff):
368 
369  payloadDict={}
370 
371  try:
372  dbase = dbSvc.openDatabase(dbName,False)
373  except Exception as e:
374  print ('Problem opening database',e)
375  sys.exit(-1)
376  print ("Opened database",dbName)
377 
378  # Loop over folders
379  folders = dbase.listAllNodes()
380  for ff in folders:
381  # Get Folder
382  try:
383  f = dbase.getFolder(ff)
384  print ("Analysing Folder " + str(ff))
385  except Exception:
386  print ("Skipping " + str(ff))
387  continue
388 
389  # get tags
390  tags = f.listTags()
391 
392  # SES
393  if tags.size()==0:
394  tags.push_back("notag")
395 
396 
397  print ("for tags ",)
398  for tag in tags:
399  print (tag)
400 
401 
402  for tag in tags:
403 
404  nobjs = f.countObjects( cool.ValidityKeyMin,
405  cool.ValidityKeyMax,
406  cool.ChannelSelection.all())
407 
408  print ("number of objects", nobjs)
409 
410  objs = f.browseObjects( cool.ValidityKeyMin,
411  cool.ValidityKeyMax,
412  cool.ChannelSelection.all())
413  i = 0
414  while objs.hasNext():
415  obj = objs.next()
416  print ("Found object", i, end='')
417  print ("since [r,l]: [", obj.since() >> 32,',',obj.since()%0x100000000,']', end='')
418  print ("until [r,l]: [", obj.until() >> 32,',',obj.until()%0x100000000,']', end='')
419  print ("payload", obj.payload(), end='')
420  print ("chan",obj.channelId() )
421  i += 1
422 
423 # payloadDict[obj.channelId()]=obj.payload()
424 
425  objs.close()
426 
427  dbase.closeDatabase()
428 
429  return payloadDict
430 
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.iNbCoolChannel
iNbCoolChannel
Definition: LArBadChannelBrowserLib.py:128
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg
Definition: LArBadChannelBrowserLib.py:25
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.class_HWIdentifier
class_HWIdentifier
Definition: LArBadChannelBrowserLib.py:137
LArBadChannelDBTools
Definition: LArBadChannelDBTools.h:26
python.LArDBFolderBrowser_MissingFEBs.LArDBFolderBrowser_MissingFEBs
Definition: LArDBFolderBrowser_MissingFEBs.py:23
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.dbFolderName
dbFolderName
Definition: LArBadChannelBrowserLib.py:119
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.dbSvc
dbSvc
Definition: LArBadChannelBrowserLib.py:77
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.larCablingSvc
larCablingSvc
Definition: LArBadChannelBrowserLib.py:99
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.tagIOVDict
tagIOVDict
Definition: LArBadChannelBrowserLib.py:167
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.class_LArBadChanBitPacking
class_LArBadChanBitPacking
Definition: LArBadChannelBrowserLib.py:138
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.nspace_LArBadChannelDBTools
nspace_LArBadChannelDBTools
Definition: LArBadChannelBrowserLib.py:134
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
run_Egamma1_LArStrip_Fex.dump
dump
Definition: run_Egamma1_LArStrip_Fex.py:87
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.dbBrowserMissingFeb
dbBrowserMissingFeb
Definition: LArBadChannelBrowserLib.py:150
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.selectedTag
selectedTag
Definition: LArBadChannelBrowserLib.py:163
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.folderId
folderId
Definition: LArBadChannelBrowserLib.py:120
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.det
det
Definition: LArBadChannelBrowserLib.py:56
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.msgSvc
msgSvc
Definition: LArBadChannelBrowserLib.py:67
python.LArDBFolderBrowser_BadChan.LArDBFolderBrowser_BadChan
Definition: LArDBFolderBrowser_BadChan.py:23
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.dbBrowserBadChan
dbBrowserBadChan
Definition: LArBadChannelBrowserLib.py:141
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.db
db
Definition: LArBadChannelBrowserLib.py:84
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.class_LArBadChannel
class_LArBadChannel
Definition: LArBadChannelBrowserLib.py:136
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.onlineID
onlineID
Definition: LArBadChannelBrowserLib.py:95
ReadCoolUPD4.openDatabase
def openDatabase(dbstring)
Definition: ReadCoolUPD4.py:21
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.GetTagsAndIOVFromDatabase
def GetTagsAndIOVFromDatabase(self, folderName)
Definition: LArBadChannelBrowserLib.py:250
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.sg
sg
note that we are using the python logging service and that the PyAthena.Alg base class has already in...
Definition: LArBadChannelBrowserLib.py:46
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.DumpDatabasePayloadObjects
def DumpDatabasePayloadObjects(self, dbSvc, dbName, ff)
Definition: LArBadChannelBrowserLib.py:367
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.__init__
def __init__(self, name="LArBadChannelBrowserAlg", **kw)
Definition: LArBadChannelBrowserLib.py:29
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
str
Definition: BTagTrackIpAccessor.cxx:11
PyAthena::Alg
Definition: PyAthenaAlg.h:33
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.channelNameDict
channelNameDict
Definition: LArBadChannelBrowserLib.py:174
error
Definition: IImpactPoint3dEstimator.h:70
python.ParticleTypeUtil.info
def info
Definition: ParticleTypeUtil.py:87
python.LArBadChannelBrowserLib.LArBadChannelBrowserAlg.ReadObjectsFromDatabase
def ReadObjectsFromDatabase(self, folderName, tagName, tagIOVNumber)
Definition: LArBadChannelBrowserLib.py:300