File indexing completed on 2024-11-25 02:29:52
0001 import ROOT
0002 import string
0003 import random
0004 import sys
0005
0006 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
0007 from PhysicsTools.SelectorUtils.VIDCutFlowResult import VIDCutFlowResult
0008 import DataFormats.FWLite
0009
0010
0011 ROOT.gSystem.Load("libFWCoreFWLite.so");
0012 ROOT.gSystem.Load("libDataFormatsFWLite.so");
0013 ROOT.FWLiteEnabler.enable()
0014
0015
0016 ROOT.gInterpreter.Declare("""
0017 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0018
0019 template <class PhysObj>
0020 struct MakeVersionedSelector {
0021 MakeVersionedSelector() {}
0022
0023 VersionedSelector<edm::Ptr<PhysObj> > operator()(const std::string& pset, const std::string& which_config) {
0024 const edm::ParameterSet& temp = edm::readPSetsFrom(pset)->getParameter<edm::ParameterSet>(which_config);
0025 return VersionedSelector<edm::Ptr<PhysObj> >(temp);
0026 }
0027
0028 VersionedSelector<edm::Ptr<PhysObj> > operator()() { return VersionedSelector<edm::Ptr<PhysObj> >(); }
0029 };
0030
0031 template <class Collection,
0032 class InPhysObj = typename Collection::value_type,
0033 class OutPhysObj = typename Collection::value_type>
0034 struct MakePtrFromCollection {
0035 edm::Ptr<OutPhysObj> operator()(const Collection& coll, unsigned idx) {
0036 edm::Ptr<InPhysObj> temp(&coll, idx);
0037 return edm::Ptr<OutPhysObj>(temp);
0038 }
0039 };
0040 """)
0041
0042
0043 config_template = """
0044 import FWCore.ParameterSet.Config as cms
0045
0046 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
0047
0048 ebCutOff = 1.479
0049
0050 %s = %s
0051 """
0052
0053 def process_pset( builder, pythonpset, suffix ):
0054 """ turn a python cms.PSet into a VID ID """
0055 idname = pythonpset.idName.value().replace('-','_') + suffix
0056 escaped_pset = config_template%(idname, pythonpset)
0057
0058 return builder(escaped_pset,idname)
0059
0060 def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
0061 return ''.join(random.choice(chars) for _ in range(size))
0062
0063 class VIDSelectorBase:
0064 def __init__(self, vidSelectorBuilder, ptrMaker, pythonpset = None):
0065 self.__initialized = False
0066 self.__suffix = id_generator(12)
0067 self.__ptrMaker = ptrMaker
0068 self.__selectorBuilder = vidSelectorBuilder()
0069 self.__instance = None
0070 if pythonpset is not None:
0071 if hasattr(pythonpset,'isPOGApproved'):
0072 approved = pythonpset.isPOGApproved.value()
0073 if not approved:
0074 sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
0075 sys.stderr.write('Please make sure to report your progress with this ID'\
0076 ' at the next relevant POG meeting.\n')
0077 del pythonpset.isPOGApproved
0078 else:
0079 sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
0080 sys.stderr.write('Please make sure to report your progress with this ID'\
0081 ' at the next relevant POG meeting.\n')
0082 self.__instance = process_pset( self.__selectorBuilder, pythonpset, self.__suffix )
0083 expectedmd5 = central_id_registry.getMD5FromName(pythonpset.idName)
0084 if expectedmd5 != self.md5String():
0085 sys.stderr.write("ID: %s\n"%self.name())
0086 sys.stderr.write("The expected md5: %s does not match the md5\n"%expectedmd5)
0087 sys.stderr.write("calculated by the ID: %s please\n"%self.md5String())
0088 sys.stderr.write("update your python configuration or determine the source\n")
0089 sys.stderr.write("of transcription error!\n")
0090 self.__initialized = True
0091 else:
0092 self.__instance = self.__selectorBuilder()
0093
0094 def __call__(self,*args):
0095 if( len(args) == 1 ):
0096 return self.__instance(*args)
0097 if( len(args) == 2 and isinstance(args[1],DataFormats.FWLite.Events) ):
0098 return self.__instance(args[0],args[1].object().event())
0099 elif( len(args) == 2 and isinstance(args[1], int) ):
0100 temp = self.__ptrMaker(args[0],args[1])
0101 newargs = [temp]
0102 return self.__instance(*newargs)
0103 if( len(args) == 3 and isinstance(args[1], int) and isinstance(args[2], DataFormats.FWLite.Events) ):
0104 temp = self.__ptrMaker(args[0],args[1])
0105 newargs = [temp]
0106 newargs += [args[2].object().event()]
0107 return self.__instance(*newargs)
0108 raise ValueError('VIDSelectorBase __call__ with args: "%s" is not a valid call pattern'%(','.join([repr(arg) for arg in args])))
0109
0110
0111 def initialize(self,pythonpset):
0112 if( self.__initialized ):
0113 print('VID Selector is already initialized, doing nothing!')
0114 return
0115 del process.__instance
0116 if hasattr(pythonpset,'isPOGApproved'):
0117 approved = pythonpset.isPOGApproved.value()
0118 if not approved:
0119 sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
0120 sys.stderr.write('Please make sure to report your progress with this ID'\
0121 ' at the next relevant POG meeting.\n')
0122 del pythonpset.isPOGApproved
0123 else:
0124 sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
0125 sys.stderr.write('Please make sure to report your progress with this ID'\
0126 ' at the next relevant POG meeting.\n')
0127 self.__instance = process_pset( self.__selectorBuilder, pythonpset, self.__suffix )
0128 expectedmd5 = central_id_registry.getMD5FromName(pythonpset.idName)
0129 if expectedmd5 != self.md5String():
0130 sys.stderr.write("ID: %s\n"%self.name())
0131 sys.stderr.write("The expected md5: %s does not match the md5\n"%expectedmd5)
0132 sys.stderr.write("calculated by the ID: %s please\n"%self.md5String())
0133 sys.stderr.write("update your python configuration or determine the source\n")
0134 sys.stderr.write("of transcription error!\n")
0135 self.__initialized = True
0136
0137 def cutFlowSize(self):
0138 return self.__instance.cutFlowSize()
0139
0140 def cutFlowResult(self):
0141 return VIDCutFlowResult(self.__instance.cutFlowResult())
0142
0143 def howFarInCutFlow(self):
0144 return self.__instance.howFarInCutFlow()
0145
0146 def name(self):
0147 return self.__instance.name()
0148
0149 def bitMap(self):
0150 return self.__instance.bitMap()
0151
0152 def md5String(self):
0153 return self.__instance.md5String()
0154
0155 def md55Raw(self):
0156 return self.__instance.md55Raw()
0157
0158 def __repr__(self):
0159 out = ROOT.std.stringstream()
0160 self.__instance.print(out)
0161 return out.str();