CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AutoHandle.py
Go to the documentation of this file.
1 #ROOTTOOLS
2 from DataFormats.FWLite import Events, Handle
3 
4 
5 class AutoHandle( Handle, object ):
6  '''Handle + label.'''
7 
8  handles = {}
9 
10  def __init__(self, label, type, mayFail=False, fallbackLabel=None):
11  '''Note: label can be a tuple : (module_label, collection_label, process)'''
12  self.label = label
13  self.fallbackLabel = fallbackLabel
14  self.type = type
15  self.mayFail = mayFail
16  Handle.__init__(self, self.type)
17 
18  def Load(self, event):
19  '''Load self from a given event.
20 
21  Call this function, and then just call self.product() to get the collection'''
22  try:
23  event.getByLabel( self.label, self)
24  if not self.isValid(): raise RuntimeError
25  except RuntimeError:
26  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
27  errstr = '''
28  Cannot find collection with:
29  type = {type}
30  label = {label}
31  '''.format(type = self.type, label = self.label)
32  if not self.mayFail and self.fallbackLabel == None:
33  raise Exception(errstr)
34  if self.fallbackLabel != None:
35  try:
36  event.getByLabel( self.fallbackLabel, self)
37  if not self.isValid(): raise RuntimeError
38  ## if I succeeded, swap default and fallback assuming that the next event will be like this one
39  self.fallbackLabel, self.label = self.label, self.fallbackLabel
40  except RuntimeError:
41  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
42  errstr = '''
43  Cannot find collection with:
44  type = {type}
45  label = {label} or {lab2}
46  '''.format(type = self.type, label = self.label, lab2 = self.fallbackLabel)
47  if not self.mayFail:
48  raise Exception(errstr)
49 
50 
label
if I succeeded, swap default and fallback assuming that the next event will be like this one ...
Definition: AutoHandle.py:12
list object
Definition: dbtoconf.py:77