CMS 3D CMS Logo

AutoHandle.py
Go to the documentation of this file.
1 from __future__ import print_function
2 #ROOTTOOLS
3 from DataFormats.FWLite import Events, Handle
4 
5 
6 class AutoHandle( Handle, object ):
7  '''Handle + label.'''
8 
9  handles = {}
10 
11  def __init__(self, label, type, mayFail=False, fallbackLabel=None, lazy=True,disableAtFirstFail=True):
12  '''Note: label can be a tuple : (module_label, collection_label, process)'''
13  self.label = label
14  self.fallbackLabel = fallbackLabel
15  self.type = type
16  self.mayFail = mayFail
17  self.lazy = lazy
18  self.isLoaded = False
19  self.autoDisable = disableAtFirstFail;
20  self.disabled= False
21  Handle.__init__(self, self.type)
22  def product(self):
23  if not self.isLoaded :
24  self.ReallyLoad(self.event)
25  self.isLoaded=True
26  return super(AutoHandle,self).product()
27 
28  def Load(self, event): #is actually a reset state
29  self.event=event
30  self.isLoaded=False
31  if self.lazy==False: self.ReallyLoad(self.event)
32 
33  def ReallyLoad(self, event):
34  '''Load self from a given event.
35 
36  Call this function, and then just call self.product() to get the collection'''
37  if self.disabled : #if autodisable kicked in, we do not even try getbylabel
38  return
39  try:
40  event.getByLabel( self.label, self)
41  if not self.isValid(): raise RuntimeError
42  except RuntimeError:
43  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
44  errstr = '''
45  Cannot find collection with:
46  type = {type}
47  label = {label}
48  '''.format(type = self.type, label = self.label)
49  if not self.mayFail and self.fallbackLabel == None:
50  if self.autoDisable : # if auto disable we disable at first failure
51  self.disabled=True
52  print("Disabling as there is no fallback ",self.label,self.type,"at first failure")
53  raise Exception(errstr)
54  if self.fallbackLabel != None:
55  try:
56  event.getByLabel( self.fallbackLabel, self)
57  if not self.isValid(): raise RuntimeError
58  ## if I succeeded, swap default and fallback assuming that the next event will be like this one
59  self.fallbackLabel, self.label = self.label, self.fallbackLabel
60  except RuntimeError:
61  Handle.__init__(self, self.type) # must re-init, since otherwise after a failure it becomes unusable
62  errstr = '''
63  Cannot find collection with:
64  type = {type}
65  label = {label} or {lab2}
66  '''.format(type = self.type, label = self.label, lab2 = self.fallbackLabel)
67  if not self.mayFail:
68  if self.autoDisable : # if auto disable we disable at first failure
69  self.disabled=True
70  print("Disabling after fallback ",self.label,self.type,"at first failure")
71  raise Exception(errstr)
72  if not self.isValid() :
73  if self.autoDisable : # if auto disable we disable at first failure
74  self.disabled=True
75  print("Disabling ",self.label,self.type,"at first failure")
76  return
77 
78 
def __init__(self, label, type, mayFail=False, fallbackLabel=None, lazy=True, disableAtFirstFail=True)
Definition: AutoHandle.py:11
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def Load(self, event)
Definition: AutoHandle.py:28
label
if I succeeded, swap default and fallback assuming that the next event will be like this one ...
Definition: AutoHandle.py:13
def ReallyLoad(self, event)
Definition: AutoHandle.py:33