ATLAS Offline Software
Loading...
Searching...
No Matches
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
26from CoolConvUtilities.AtlCoolLib import indirectOpen
27
29
30 # Define dbDict here at class scope
31 # Then access with self.__class__.dbDict and it will be the same for all instances of the class
32 # This is a pythonish way to create static classes
33
34 # Dict to store DB connection indexed by text DB connection name
35 dbDict = dict()
36
37
38 def __init__(self):
39
40 # Debug output (can be changed for each instance, slick...)
41 self.verbose = False
42
43 # Return a folder reference for the dbstring, folder specified
44 # DB will be opened if necessary
45 # Example: getFolder('COOLONL_TRIGGER/COMP200', '/TRIGGER/LUMI/LBLESTONL')
46 def getFolder(self, dbstring, folder, force=False):
47
48 if self.verbose:
49 print('LumiDBHandler.getFolder(', dbstring, ',', folder, ') called')
50
51 if not self.openDB(dbstring, force=force):
52 print("LumiDBHandler.getFolder - can't connect to DB!")
53 return None
54
55 return self.__class__.dbDict[dbstring].getFolder(folder)
56
57 # Open a COOL DB connection based on a name such as "COOLONL_INDET/OFLP200"
58 # Returns True if successful (or DB already open)
59 def openDB(self, dbstring, oracle=False, debug=False, force=False):
60
61 if self.verbose:
62 print('LumiDBHandler.openDB(', dbstring, ') called')
63
64 # Check if already open
65 if dbstring in self.__class__.dbDict:
66
67 # No force, just return
68 if not force:
69 if self.verbose:
70 print('LumiDBHandler.openDB - Connection already exists')
71 return True # Yes it is
72
73 # Force specified, close so we can re-open
74 if self.verbose:
75 print('LumiDBHandler.openDB - Connection already exists, closing first due to force=True')
76 self.closeDB(dbstring)
77
78 # Try to open DB connection
79 if self.verbose:
80 print(('LumiDBHandler.openDB - Connecting to', dbstring))
81
82 try:
83 db = indirectOpen(dbstring, readOnly=True, debug=debug)
84 except Exception as e:
85 print(e)
86 return False
87
88 # OK, opened. Save this to our dict for later use
89 self.__class__.dbDict[dbstring] = db
90
91 return True
92
93 # Close specific DB
94 def closeDB(self, dbstring):
95
96 if self.verbose:
97 print('LumiDBHandler.closeDB - Closing connection to', dbstring)
98
99 if dbstring not in self.__class__.dbDict:
100 print("LumiDBHandler.closeDB - DB doesn't exist:", dbstring)
101 else:
102 try:
103 self.__class__.dbDict[dbstring].closeDatabase()
104 except Exception as e:
105 print(e)
106 self.__class__.dbDict.pop(dbstring)
107
108 # Called by default in the destructor, but not guaranteed if there are problems
109 def closeAllDB(self):
110 self.closeAll()
111
112 def closeAll(self):
113
114 if self.verbose:
115 print('LumiDBHandler.closeAllDB called')
116
117 # Can't use iterkeys here as we are deleting the elements
118 for dbstring in list(self.__class__.dbDict.keys()):
119 self.closeDB(dbstring)
120
void print(char *figname, TCanvas *c1)
getFolder(self, dbstring, folder, force=False)
openDB(self, dbstring, oracle=False, debug=False, force=False)