CMS 3D CMS Logo

VIDSelectorBase.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import ROOT
3 import string
4 import random
5 import sys
6 
7 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
8 from PhysicsTools.SelectorUtils.VIDCutFlowResult import VIDCutFlowResult
9 import DataFormats.FWLite
10 
11 # load FWLite C++ libraries
12 ROOT.gSystem.Load("libFWCoreFWLite.so");
13 ROOT.gSystem.Load("libDataFormatsFWLite.so");
14 ROOT.FWLiteEnabler.enable()
15 
16 # define some C++ helpers that are only used in this VID selector class and deriving classes
17 ROOT.gInterpreter.Declare("""
18 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
19 
20 template <class PhysObj>
21 struct MakeVersionedSelector {
22  MakeVersionedSelector() {}
23 
24  VersionedSelector<edm::Ptr<PhysObj> > operator()(const std::string& pset, const std::string& which_config) {
25  const edm::ParameterSet& temp = edm::readPSetsFrom(pset)->getParameter<edm::ParameterSet>(which_config);
26  return VersionedSelector<edm::Ptr<PhysObj> >(temp);
27  }
28 
29  VersionedSelector<edm::Ptr<PhysObj> > operator()() { return VersionedSelector<edm::Ptr<PhysObj> >(); }
30 };
31 
32 template <class Collection,
33  class InPhysObj = typename Collection::value_type,
34  class OutPhysObj = typename Collection::value_type>
35 struct MakePtrFromCollection {
36  edm::Ptr<OutPhysObj> operator()(const Collection& coll, unsigned idx) {
37  edm::Ptr<InPhysObj> temp(&coll, idx);
38  return edm::Ptr<OutPhysObj>(temp);
39  }
40 };
41 """)
42 
43 
44 config_template = """
45 import FWCore.ParameterSet.Config as cms
46 
47 from PhysicsTools.SelectorUtils.centralIDRegistry import central_id_registry
48 
49 ebCutOff = 1.479
50 
51 %s = %s
52 """
53 
54 def process_pset( builder, pythonpset, suffix ):
55  """ turn a python cms.PSet into a VID ID """
56  idname = pythonpset.idName.value().replace('-','_') + suffix
57  escaped_pset = config_template%(idname, pythonpset)
58 
59  return builder(escaped_pset,idname)
60 
61 def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
62  return ''.join(random.choice(chars) for _ in range(size))
63 
65  def __init__(self, vidSelectorBuilder, ptrMaker, pythonpset = None):
66  self.__initialized = False
68  self.__ptrMaker = ptrMaker
69  self.__selectorBuilder = vidSelectorBuilder()
70  self.__instance = None
71  if pythonpset is not None:
72  if hasattr(pythonpset,'isPOGApproved'):
73  approved = pythonpset.isPOGApproved.value()
74  if not approved:
75  sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
76  sys.stderr.write('Please make sure to report your progress with this ID'\
77  ' at the next relevant POG meeting.\n')
78  del pythonpset.isPOGApproved
79  else:
80  sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
81  sys.stderr.write('Please make sure to report your progress with this ID'\
82  ' at the next relevant POG meeting.\n')
83  self.__instance = process_pset( self.__selectorBuilder, pythonpset, self.__suffix )
84  expectedmd5 = central_id_registry.getMD5FromName(pythonpset.idName)
85  if expectedmd5 != self.md5String():
86  sys.stderr.write("ID: %s\n"%self.name())
87  sys.stderr.write("The expected md5: %s does not match the md5\n"%expectedmd5)
88  sys.stderr.write("calculated by the ID: %s please\n"%self.md5String())
89  sys.stderr.write("update your python configuration or determine the source\n")
90  sys.stderr.write("of transcription error!\n")
91  self.__initialized = True
92  else:
93  self.__instance = self.__selectorBuilder()
94 
95  def __call__(self,*args):
96  if( len(args) == 1 ):
97  return self.__instance(*args)
98  if( len(args) == 2 and isinstance(args[1],DataFormats.FWLite.Events) ):
99  return self.__instance(args[0],args[1].object().event())
100  elif( len(args) == 2 and isinstance(args[1], int) ):
101  temp = self.__ptrMaker(args[0],args[1])
102  newargs = [temp]
103  return self.__instance(*newargs)
104  if( len(args) == 3 and isinstance(args[1], int) and isinstance(args[2], DataFormats.FWLite.Events) ):
105  temp = self.__ptrMaker(args[0],args[1])
106  newargs = [temp]
107  newargs += [args[2].object().event()]
108  return self.__instance(*newargs)
109  raise ValueError('VIDSelectorBase __call__ with args: "%s" is not a valid call pattern'%(','.join([repr(arg) for arg in args])))
110 
111 
112  def initialize(self,pythonpset):
113  if( self.__initialized ):
114  print('VID Selector is already initialized, doing nothing!')
115  return
116  del process.__instance
117  if hasattr(pythonpset,'isPOGApproved'):
118  approved = pythonpset.isPOGApproved.value()
119  if not approved:
120  sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
121  sys.stderr.write('Please make sure to report your progress with this ID'\
122  ' at the next relevant POG meeting.\n')
123  del pythonpset.isPOGApproved
124  else:
125  sys.stderr.write('This ID is not POG approved and likely under development!!!!\n')
126  sys.stderr.write('Please make sure to report your progress with this ID'\
127  ' at the next relevant POG meeting.\n')
128  self.__instance = process_pset( self.__selectorBuilder, pythonpset, self.__suffix )
129  expectedmd5 = central_id_registry.getMD5FromName(pythonpset.idName)
130  if expectedmd5 != self.md5String():
131  sys.stderr.write("ID: %s\n"%self.name())
132  sys.stderr.write("The expected md5: %s does not match the md5\n"%expectedmd5)
133  sys.stderr.write("calculated by the ID: %s please\n"%self.md5String())
134  sys.stderr.write("update your python configuration or determine the source\n")
135  sys.stderr.write("of transcription error!\n")
136  self.__initialized = True
137 
138  def cutFlowSize(self):
139  return self.__instance.cutFlowSize()
140 
141  def cutFlowResult(self):
143 
144  def howFarInCutFlow(self):
145  return self.__instance.howFarInCutFlow()
146 
147  def name(self):
148  return self.__instance.name()
149 
150  def bitMap(self):
151  return self.__instance.bitMap()
152 
153  def md5String(self):
154  return self.__instance.md5String()
155 
156  def md55Raw(self):
157  return self.__instance.md55Raw()
158 
159  def __repr__(self):
160  out = ROOT.std.stringstream()
161  self.__instance.print(out)
162  return out.str();
def replace(string, replacements)
def process_pset(builder, pythonpset, suffix)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def initialize(self, pythonpset)
def __init__(self, vidSelectorBuilder, ptrMaker, pythonpset=None)
def id_generator(size=6, chars=string.ascii_uppercase+string.digits)
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
Definition: event.py:1