ATLAS Offline Software
CoolDataReader.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #
4 # CoolDataReader
5 #
6 # Eric Torrence - October 2010
7 #
8 # Contents:
9 # CoolDataReader - utility object to handle reading of COOL DB folders.
10 # The benefit over just using AtlCoolLib directly is that each DB connection is
11 # cached, so multiple connections to the same DB will not be made.
12 #
13 # CoolDataReader.readData() returns a list the full IObject for maximal flexibility
14 #
15 # General usage example
16 # myReader = CoolDataReader('COOLONL_TRIGGER/COMP200', '/TRIGGER/LUMI/LBLESTONL')
17 # myReader.setIOVRange(startIOV, endIOV)
18 # myReader.readData()
19 # for obj in myReader.data:
20 # ...
21 #
22 # One can specify specific channels or IOV ranges if desired, but by default all data will be loaded
23 #
24 # The CoolDataReader uses the LumiDBHandler internally to cache multiple CoolConnections
25 #
26 
27 from PyCool import cool
28 
29 # Get our global DB handler object
30 from CoolLumiUtilities.LumiDBHandler import LumiDBHandler
31 
32 
34 
35  def __init__(self, dbstr=None, folderstr=None):
36 
37  self.verbose = False
38 
39  # Defined variables
40  self.dbstr = None
41  self.folderstr = None
42  self.channelIdList = []
43  self.tag = ''
44  self.iovstart = None
45  self.iovend = None
46 
47  self.folder = None
48  self.data = []
49 
50  # Initialize to default values
51  self.setChannel()
52  self.setTag()
53  self.setFolder(dbstr, folderstr)
54  self.setIOVRange()
55 
56  def setFolder(self, dbstr, folderstr):
57  # Force re-opening connection if these are different
58  if (dbstr != self.dbstr) or (folderstr != self.folderstr):
59  self.folder = None
60 
61  self.dbstr = dbstr
62  self.folderstr = folderstr
63 
64  def setTag(self, tagstr=''):
65  self.tag = tagstr
66 
67  def setChannel(self, channelIdList=[]):
68  self.channelIdList = channelIdList
69 
70  def setChannelAll(self):
71  self.setChannel()
72 
73  def setChannelId(self, channelId):
74  self.setChannel([channelId])
75 
76  def setIOVRange(self, iovstart=cool.ValidityKeyMin, iovend=cool.ValidityKeyMax):
77  self.iovstart = iovstart
78  self.iovend = iovend
79 
80  def setIOVRangeFromRun(self, runnum):
81  self.iovstart = runnum << 32
82  self.iovend = ((runnum+1) << 32) - 1
83 
84  # Call to get data after all other parameters are properly set
85  # Data is returned as a list of IObject values, one per DB entry.
86  # This gives maximal flexibility to manipulate the items
87  def readData(self):
88 
89  self.data = []
90 
91  # Open the DB connection here if needed
92  if self.folder is None:
93  dbHandler = LumiDBHandler()
94  self.folder = dbHandler.getFolder(self.dbstr, self.folderstr)
95 
96  if self.folder is None:
97  print("Can't access DB", self.dbstr, 'folder', self.folderstr, '!')
98  return self.data
99 
100  # Create the channel list
101  if len(self.channelIdList) == 0:
102  channels = cool.ChannelSelection.all()
103  self.readChannelList(channels)
104 
105  else:
106  # Build the channel list here
107  self.channelIdList.sort() # Must be sorted!
108 
109  # Must read channels 50 at a time due to COOL limit...
110  ichan = 0
111  while (ichan < len(self.channelIdList)) :
112 
113  jchan = 0
114  channels = None
115  firstChan = True
116 
117  for channelId in self.channelIdList[ichan:]:
118  jchan += 1
119  if firstChan:
120  firstChan = False
121  channels = cool.ChannelSelection(channelId)
122  else:
123  channels.addChannel(channelId)
124  if jchan == 50: break
125 
126  # Remeber how many we have read for next time
127  if self.verbose:
128  print('CoolDataReader.readData() - loaded %d channels from %d' % (jchan, ichan))
129  ichan += jchan
130 
131  if self.verbose:
132  print('CoolDataReader.readData() - browsing', self.iovstart, self.iovend, 'with channel', channels, 'and tag', self.tag)
133 
134  self.readChannelList(channels)
135 
136  # End of loop building channel list and reading
137 
138  # End of if statement reading data
139  return self.data
140 
141  def readChannelList(self, channels):
142 
143  # Open iterator over our defined IOV range
144  try:
145  itr = self.folder.browseObjects(self.iovstart, self.iovend, channels, self.tag)
146  except Exception as e:
147  print('CoolDataReader.readData() - exception reading folder:', self.folderstr)
148  print(e)
149  print('CoolDataReader.readData() - will try to reconnect (once)')
150 
151  # Force re-opening connection
152  dbHandler = LumiDBHandler()
153  dbHandler.verbose = True
154  self.folder = dbHandler.getFolder(self.dbstr, self.folderstr, force=True)
155 
156  if self.folder is None:
157  print('CoolDataReader.readData() - forced re-opening failed!')
158  return self.data
159 
160  # OK, lets try reading this again
161  print('CoolDataReader.readData() - trying to re-read re-opened folder!')
162  try:
163  itr = self.folder.browseObjects(self.iovstart, self.iovend, channels, self.tag)
164  except Exception as e:
165  print('CoolDataReader.readData() - exception reading folder:', self.folderstr)
166  print(e)
167  return self.data
168 
169  while itr.goToNext():
170  obj = itr.currentRef()
171  # print obj.payload()
172  self.data.append(obj.clone())
173 
174  itr.close()
175 
176 
177 
python.CoolDataReader.CoolDataReader.dbstr
dbstr
Definition: CoolDataReader.py:40
python.CoolDataReader.CoolDataReader.readChannelList
def readChannelList(self, channels)
Definition: CoolDataReader.py:141
python.CoolDataReader.CoolDataReader.iovstart
iovstart
Definition: CoolDataReader.py:44
python.CoolDataReader.CoolDataReader.setChannel
def setChannel(self, channelIdList=[])
Definition: CoolDataReader.py:67
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.CoolDataReader.CoolDataReader.folder
folder
Definition: CoolDataReader.py:47
python.CoolDataReader.CoolDataReader.setFolder
def setFolder(self, dbstr, folderstr)
Definition: CoolDataReader.py:56
python.CoolDataReader.CoolDataReader.setIOVRangeFromRun
def setIOVRangeFromRun(self, runnum)
Definition: CoolDataReader.py:80
python.CoolDataReader.CoolDataReader
Definition: CoolDataReader.py:33
python.CoolDataReader.CoolDataReader.data
data
Definition: CoolDataReader.py:48
python.CoolDataReader.CoolDataReader.folderstr
folderstr
Definition: CoolDataReader.py:41
python.CoolDataReader.CoolDataReader.setChannelAll
def setChannelAll(self)
Definition: CoolDataReader.py:70
python.CoolDataReader.CoolDataReader.iovend
iovend
Definition: CoolDataReader.py:45
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.CoolDataReader.CoolDataReader.setTag
def setTag(self, tagstr='')
Definition: CoolDataReader.py:64
python.CoolDataReader.CoolDataReader.setIOVRange
def setIOVRange(self, iovstart=cool.ValidityKeyMin, iovend=cool.ValidityKeyMax)
Definition: CoolDataReader.py:76
python.CoolDataReader.CoolDataReader.readData
def readData(self)
Definition: CoolDataReader.py:87
python.CoolDataReader.CoolDataReader.__init__
def __init__(self, dbstr=None, folderstr=None)
Definition: CoolDataReader.py:35
python.CoolDataReader.CoolDataReader.verbose
verbose
Definition: CoolDataReader.py:37
python.CoolDataReader.CoolDataReader.tag
tag
Definition: CoolDataReader.py:43
python.CoolDataReader.CoolDataReader.setChannelId
def setChannelId(self, channelId)
Definition: CoolDataReader.py:73
python.CoolDataReader.CoolDataReader.channelIdList
channelIdList
Definition: CoolDataReader.py:42