ATLAS Offline Software
LumiDBHandler.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #
4 # LumiDBHandler
5 #
6 # Eric Torrence - October 2010
7 #
8 # Contents:
9 # LumiDBHandler - utility object to handle opening and closing COOL DB connections within
10 # a large python script. The benefit over just using AtlCoolLib directly
11 # is that each DB connection is cached, so multiple connections to the same
12 # DB will not be made.
13 #
14 # The parent script should call closeAllDB in its __del__ function to close
15 # the DB connections, even if the script crashes.
16 #
17 # General usage example
18 # dbH = LumiDBHandler()
19 # myFolder = dbH.getFolder('COOLONL_TRIGGER/COMP200', '/TRIGGER/LUMI/LBLESTONL')
20 #
21 # One can then browse the folder as usual using browseObjects
22 #
23 # The CoolDataReader uses this class internally to make for more easy access
24 #
25 
26 from __future__ import print_function
27 from CoolConvUtilities.AtlCoolLib import indirectOpen
28 
30 
31  # Define dbDict here at class scope
32  # Then access with self.__class__.dbDict and it will be the same for all instances of the class
33  # This is a pythonish way to create static classes
34 
35  # Dict to store DB connection indexed by text DB connection name
36  dbDict = dict()
37 
38 
39  def __init__(self):
40 
41  # Debug output (can be changed for each instance, slick...)
42  self.verbose = False
43 
44  # Return a folder reference for the dbstring, folder specified
45  # DB will be opened if necessary
46  # Example: getFolder('COOLONL_TRIGGER/COMP200', '/TRIGGER/LUMI/LBLESTONL')
47  def getFolder(self, dbstring, folder, force=False):
48 
49  if self.verbose:
50  print('LumiDBHandler.getFolder(', dbstring, ',', folder, ') called')
51 
52  if not self.openDB(dbstring, force=force):
53  print("LumiDBHandler.getFolder - can't connect to DB!")
54  return None
55 
56  return self.__class__.dbDict[dbstring].getFolder(folder)
57 
58  # Open a COOL DB connection based on a name such as "COOLONL_INDET/OFLP200"
59  # Returns True if successful (or DB already open)
60  def openDB(self, dbstring, oracle=False, debug=False, force=False):
61 
62  if self.verbose:
63  print('LumiDBHandler.openDB(', dbstring, ') called')
64 
65  # Check if already open
66  if dbstring in self.__class__.dbDict:
67 
68  # No force, just return
69  if not force:
70  if self.verbose:
71  print('LumiDBHandler.openDB - Connection already exists')
72  return True # Yes it is
73 
74  # Force specified, close so we can re-open
75  if self.verbose:
76  print('LumiDBHandler.openDB - Connection already exists, closing first due to force=True')
77  self.closeDB(dbstring)
78 
79  # Try to open DB connection
80  if self.verbose:
81  print(('LumiDBHandler.openDB - Connecting to', dbstring))
82 
83  try:
84  db = indirectOpen(dbstring, readOnly=True, debug=debug)
85  except Exception as e:
86  print(e)
87  return False
88 
89  # OK, opened. Save this to our dict for later use
90  self.__class__.dbDict[dbstring] = db
91 
92  return True
93 
94  # Close specific DB
95  def closeDB(self, dbstring):
96 
97  if self.verbose:
98  print('LumiDBHandler.closeDB - Closing connection to', dbstring)
99 
100  if dbstring not in self.__class__.dbDict:
101  print("LumiDBHandler.closeDB - DB doesn't exist:", dbstring)
102  else:
103  try:
104  self.__class__.dbDict[dbstring].closeDatabase()
105  except Exception as e:
106  print(e)
107  self.__class__.dbDict.pop(dbstring)
108 
109  # Called by default in the destructor, but not guaranteed if there are problems
110  def closeAllDB(self):
111  self.closeAll()
112 
113  def closeAll(self):
114 
115  if self.verbose:
116  print('LumiDBHandler.closeAllDB called')
117 
118  # Can't use iterkeys here as we are deleting the elements
119  for dbstring in list(self.__class__.dbDict.keys()):
120  self.closeDB(dbstring)
121 
python.LumiDBHandler.LumiDBHandler
Definition: LumiDBHandler.py:29
python.LumiDBHandler.LumiDBHandler.closeAllDB
def closeAllDB(self)
Definition: LumiDBHandler.py:110
python.LumiDBHandler.LumiDBHandler.closeDB
def closeDB(self, dbstring)
Definition: LumiDBHandler.py:95
python.LumiDBHandler.LumiDBHandler.__init__
def __init__(self)
Definition: LumiDBHandler.py:39
python.LumiDBHandler.LumiDBHandler.openDB
def openDB(self, dbstring, oracle=False, debug=False, force=False)
Definition: LumiDBHandler.py:60
python.LumiDBHandler.LumiDBHandler.getFolder
def getFolder(self, dbstring, folder, force=False)
Definition: LumiDBHandler.py:47
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.LumiDBHandler.LumiDBHandler.closeAll
def closeAll(self)
Definition: LumiDBHandler.py:113
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.AtlCoolLib.indirectOpen
def indirectOpen(coolstr, readOnly=True, debug=False)
Definition: AtlCoolLib.py:130
python.LumiDBHandler.LumiDBHandler.verbose
verbose
Definition: LumiDBHandler.py:42