File indexing completed on 2024-11-25 02:29:49
0001
0002 from DataFormats.FWLite import Events, Handle
0003
0004
0005 class AutoHandle( Handle, object ):
0006 '''Handle + label.'''
0007
0008 handles = {}
0009
0010 def __init__(self, label, type, mayFail=False, fallbackLabel=None, lazy=True,disableAtFirstFail=True):
0011 '''Note: label can be a tuple : (module_label, collection_label, process)'''
0012 self.label = label
0013 self.fallbackLabel = fallbackLabel
0014 self.type = type
0015 self.mayFail = mayFail
0016 self.lazy = lazy
0017 self.isLoaded = False
0018 self.autoDisable = disableAtFirstFail;
0019 self.disabled= False
0020 Handle.__init__(self, self.type)
0021 def product(self):
0022 if not self.isLoaded :
0023 self.ReallyLoad(self.event)
0024 self.isLoaded=True
0025 return super(AutoHandle,self).product()
0026
0027 def Load(self, event):
0028 self.event=event
0029 self.isLoaded=False
0030 if self.lazy==False: self.ReallyLoad(self.event)
0031
0032 def ReallyLoad(self, event):
0033 '''Load self from a given event.
0034
0035 Call this function, and then just call self.product() to get the collection'''
0036 if self.disabled :
0037 return
0038 try:
0039 event.getByLabel( self.label, self)
0040 if not self.isValid(): raise RuntimeError
0041 except RuntimeError:
0042 Handle.__init__(self, self.type)
0043 errstr = '''
0044 Cannot find collection with:
0045 type = {type}
0046 label = {label}
0047 '''.format(type = self.type, label = self.label)
0048 if not self.mayFail and self.fallbackLabel == None:
0049 if self.autoDisable :
0050 self.disabled=True
0051 print("Disabling as there is no fallback ",self.label,self.type,"at first failure")
0052 raise Exception(errstr)
0053 if self.fallbackLabel != None:
0054 try:
0055 event.getByLabel( self.fallbackLabel, self)
0056 if not self.isValid(): raise RuntimeError
0057
0058 self.fallbackLabel, self.label = self.label, self.fallbackLabel
0059 except RuntimeError:
0060 Handle.__init__(self, self.type)
0061 errstr = '''
0062 Cannot find collection with:
0063 type = {type}
0064 label = {label} or {lab2}
0065 '''.format(type = self.type, label = self.label, lab2 = self.fallbackLabel)
0066 if not self.mayFail:
0067 if self.autoDisable :
0068 self.disabled=True
0069 print("Disabling after fallback ",self.label,self.type,"at first failure")
0070 raise Exception(errstr)
0071 if not self.isValid() :
0072 if self.autoDisable :
0073 self.disabled=True
0074 print("Disabling ",self.label,self.type,"at first failure")
0075 return
0076
0077