CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PDFWeightsAnalyzer.py
Go to the documentation of this file.
1 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
2 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
4 
5 
6 class PDFWeightsAnalyzer( Analyzer ):
7  """ """
8  def __init__(self, cfg_ana, cfg_comp, looperName ):
9  super(PDFWeightsAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
10  self.doPDFWeights = hasattr(self.cfg_ana, "PDFWeights") and len(self.cfg_ana.PDFWeights) > 0
11  if self.doPDFWeights:
12  self.pdfWeightInit = False
13  #---------------------------------------------
14  # DECLARATION OF HANDLES OF GEN LEVEL OBJECTS
15  #---------------------------------------------
16 
17 
18  def declareHandles(self):
19  super(PDFWeightsAnalyzer, self).declareHandles()
20 
21  if self.doPDFWeights:
22  self.mchandles['pdfstuff'] = AutoHandle( 'generator', 'GenEventInfoProduct' )
23 
24  def beginLoop(self, setup):
25  super(PDFWeightsAnalyzer,self).beginLoop(setup)
26 
27  def initPDFWeights(self):
28  from ROOT import PdfWeightProducerTool
29  self.pdfWeightInit = True
30  self.pdfWeightTool = PdfWeightProducerTool()
31  for pdf in self.cfg_ana.PDFWeights:
32  self.pdfWeightTool.addPdfSet(pdf+".LHgrid")
33  self.pdfWeightTool.beginJob()
34 
35  def makePDFWeights(self, event):
36  if not self.pdfWeightInit: self.initPDFWeights()
37  self.pdfWeightTool.processEvent(self.mchandles['pdfstuff'].product())
38  event.pdfWeights = {}
39  for pdf in self.cfg_ana.PDFWeights:
40  ws = self.pdfWeightTool.getWeights(pdf+".LHgrid")
41  event.pdfWeights[pdf] = [w for w in ws]
42 
43  def process(self, event):
44  self.readCollections( event.input )
45 
46  # if not MC, nothing to do
47  if not self.cfg_comp.isMC:
48  return True
49 
50  if self.doPDFWeights:
51  self.makePDFWeights(event)
52  return True
53 
54 setattr(PDFWeightsAnalyzer,"defaultConfig",
55  cfg.Analyzer(PDFWeightsAnalyzer,
56  PDFWeights = []
57  )
58 )