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