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