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  def product(self):
18  if not self.isLoaded :
19  self.ReallyLoad(self.event)
20  self.isLoaded=True
21  return super(AutoHandle,self).product()
22 
23  def Load(self, event): #is actually a reset state
24  self.event=event
25  self.isLoaded=False
26 
27  def ReallyLoad(self, event):
28  '''Load self from a given event.
29 
30  Call this function, and then just call self.product() to get the collection'''
31  try:
32  event.getByLabel( self.label, self)
33  if not self.isValid(): raise RuntimeError
34  except RuntimeError:
35  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
36  errstr = '''
37  Cannot find collection with:
38  type = {type}
39  label = {label}
40  '''.format(type = self.type, label = self.label)
41  if not self.mayFail and self.fallbackLabel == None:
42  raise Exception(errstr)
43  if self.fallbackLabel != None:
44  try:
45  event.getByLabel( self.fallbackLabel, self)
46  if not self.isValid(): raise RuntimeError
47  ## if I succeeded, swap default and fallback assuming that the next event will be like this one
48  self.fallbackLabel, self.label = self.label, self.fallbackLabel
49  except RuntimeError:
50  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
51  errstr = '''
52  Cannot find collection with:
53  type = {type}
54  label = {label} or {lab2}
55  '''.format(type = self.type, label = self.label, lab2 = self.fallbackLabel)
56  if not self.mayFail:
57  raise Exception(errstr)
58 
59 
list object
Definition: dbtoconf.py:77
label
if I succeeded, swap default and fallback assuming that the next event will be like this one ...
Definition: AutoHandle.py:12