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