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