File indexing completed on 2024-11-25 02:29:52
0001 import md5
0002 import ROOT
0003
0004
0005 ROOT.gSystem.Load("libFWCoreFWLite.so")
0006 ROOT.gSystem.Load("libDataFormatsFWLite.so")
0007 ROOT.FWLiteEnabler.enable()
0008
0009
0010 import FWCore.ParameterSet.Config as cms
0011
0012
0013 from DataFormats.FWLite import Handle, Events
0014
0015
0016
0017
0018
0019
0020
0021
0022 class VIDSelectorValidator:
0023 def __init__(self, selector, collection_type, collection_name):
0024 self.__hasher = md5.new()
0025 self.__selector = selector
0026 self.__colltype = collection_type
0027 self.__collname = collection_name
0028 self.__signalfiles = []
0029 self.__backgroundfiles = []
0030 self.__mixfiles = []
0031
0032 def setSignalFiles(self, files):
0033 if not isinstance(files,list):
0034 raise Exception('BadFileInput','You need to give "setSignalFiles" a list of strings')
0035 self.__signalfiles = files[:]
0036
0037 def setBackgroundFiles(self, files):
0038 if not isinstance(files,list):
0039 raise Exception('BadFileInput','You need to give "setBackgroundFiles" a list of strings')
0040 self.__backgroundfiles = files[:]
0041
0042 def setMixFiles(self, files):
0043 if not isinstance(files,list):
0044 raise Exception('BadFileInput','You need to give "setMixFiles" a list of strings')
0045 self.__mixfiles = files[:]
0046
0047 def runValidation(self):
0048 samples = {}
0049 samples['signal'] = self.__signalfiles
0050 samples['background'] = self.__backgroundfiles
0051 samples['mix'] = self.__mixfiles
0052
0053 select = self.__selector
0054
0055 print('running validation for: %s'%(select.name()))
0056
0057
0058 if not len(samples['signal'] + samples['background'] + samples['mix']):
0059 raise Exception('NoInputFiles','There were no input files given, cannot validate!')
0060
0061 for key in sorted(samples.keys()):
0062 self.processInputList(samples[key],key)
0063
0064 print('input files checksum: %s'%(self.__hasher.hexdigest()))
0065
0066 for key in sorted(samples.keys()):
0067 if len(samples[key]):
0068 local_hash = md5.new()
0069 self.processEvents(samples[key],key,local_hash)
0070 self.__hasher.update(local_hash.hexdigest())
0071
0072 print('event processing checksum: %s'%(self.__hasher.hexdigest()))
0073
0074 self.__hasher.update(select.md5String())
0075
0076 print('total checksum: %s'%(self.__hasher.hexdigest()))
0077
0078 def processInputList(self,the_list,name):
0079 for item in the_list:
0080 self.__hasher.update(item)
0081 print('Input %s file: %s'%(name,item))
0082
0083 def processEvents(self,the_list,name,hasher):
0084
0085 handle, productLabel = Handle(self.__colltype), self.__collname
0086
0087
0088 events = Events(the_list)
0089 n_pass, n_fail = 0,0
0090
0091 sub_cutnames = []
0092 sub_hashes = []
0093 for idstring in repr(self.__selector).split('\n'):
0094 if idstring == '': continue
0095 sub_cutnames.append(idstring.split()[2])
0096 sub_hashes.append(md5.new(idstring))
0097
0098 for event in events:
0099 event.getByLabel(productLabel,handle)
0100 for i,obj in enumerate(handle.product()):
0101 if self.__selector(handle.product(),i,event):
0102 n_pass += 1
0103 else:
0104 n_fail += 1
0105 icut = 0
0106 for idstring in repr(self.__selector).split('\n'):
0107 if idstring == '': continue
0108 sub_hashes[icut].update(idstring)
0109 icut += 1
0110
0111 for sub_hash in sub_hashes:
0112 hasher.update(sub_hash.hexdigest())
0113
0114 hasher.update(str(n_pass))
0115 hasher.update(str(n_fail))
0116 print('%s sample pass : fail : hash -> %d : %d : %s'%(name,n_pass,n_fail,hasher.hexdigest()))
0117 print('%s sample cut breakdown:'%(name))
0118 for i,sub_hash in enumerate(sub_hashes):
0119 print('\t%s hash -> %s'%(sub_cutnames[i],sub_hash.hexdigest()))