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,disableAtFirstFail=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  self.isLoaded = False
18  self.autoDisable = disableAtFirstFail;
19  self.disabled= False
20  Handle.__init__(self, self.type)
21  def product(self):
22  if not self.isLoaded :
23  self.ReallyLoad(self.event)
24  self.isLoaded=True
25  return super(AutoHandle,self).product()
26 
27  def Load(self, event): #is actually a reset state
28  self.event=event
29  self.isLoaded=False
30  if self.lazy==False: self.ReallyLoad(self.event)
31 
32  def ReallyLoad(self, event):
33  '''Load self from a given event.
34 
35  Call this function, and then just call self.product() to get the collection'''
36  if self.disabled : #if autodisable kicked in, we do not even try getbylabel
37  return
38  try:
39  event.getByLabel( self.label, self)
40  if not self.isValid(): raise RuntimeError
41  except RuntimeError:
42  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
43  errstr = '''
44  Cannot find collection with:
45  type = {type}
46  label = {label}
47  '''.format(type = self.type, label = self.label)
48  if not self.mayFail and self.fallbackLabel == None:
49  if self.autoDisable : # if auto disable we disable at first failure
50  self.disabled=True
51  print "Disabling as there is no fallback ",self.label,self.type,"at first failure"
52  raise Exception(errstr)
53  if self.fallbackLabel != None:
54  try:
55  event.getByLabel( self.fallbackLabel, self)
56  if not self.isValid(): raise RuntimeError
57  ## if I succeeded, swap default and fallback assuming that the next event will be like this one
58  self.fallbackLabel, self.label = self.label, self.fallbackLabel
59  except RuntimeError:
60  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
61  errstr = '''
62  Cannot find collection with:
63  type = {type}
64  label = {label} or {lab2}
65  '''.format(type = self.type, label = self.label, lab2 = self.fallbackLabel)
66  if not self.mayFail:
67  if self.autoDisable : # if auto disable we disable at first failure
68  self.disabled=True
69  print "Disabling after fallback ",self.label,self.type,"at first failure"
70  raise Exception(errstr)
71  if not self.isValid() :
72  if self.autoDisable : # if auto disable we disable at first failure
73  self.disabled=True
74  print "Disabling ",self.label,self.type,"at first failure"
75  return
76 
77 
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