CMS 3D CMS Logo

weight.py
Go to the documentation of this file.
1 from __future__ import print_function
2 # Copyright (C) 2014 Colin Bernet
3 # https://github.com/cbernet/heppy/blob/master/LICENSE
4 
5 def printWeights( weights ):
6  import six
7  for key, value in six.iteritems(weights):
8  print(key)
9  print(value)
10 
11 class Weight( object ):
12  '''make names uniform wrt Component.
13 
14  COLIN: messy... should I have several types of weight (base, data, mc)?
15  COLIN: need to add other weighting factors'''
16 
17  FBINV = 1000.
18 
19  def __init__(self, genNEvents, xSection, genEff,
20  intLumi = FBINV, addWeight=1):
21  self.genNEvents = int(genNEvents)
22  if xSection is not None:
23  self.xSection = float(xSection)
24  else:
25  self.xSection = None
26  self.genEff = float(genEff)
27  if intLumi is not None:
28  self.intLumi = float(intLumi)
29  else:
30  self.intLumi = Weight.FBINV
31  self.addWeight = float(addWeight)
32 
33  def GetWeight(self):
34  '''Return the weight'''
35  if self.xSection is None:
36  # data
37  return 1
38  else:
39  # MC
40  return self.addWeight * self.xSection * self.intLumi / ( self.genNEvents * self.genEff)
41 
42  def SetIntLumi(self, lumi):
43  '''Set integrated luminosity.'''
44  self.dict['intLumi'] = lumi
45 
46  def __str__(self):
47  if self.xSection is None:
48  return ' intLumi = %5.2f, addWeight = %3.2f' \
49  % ( self.intLumi,
50  self.addWeight )
51  else:
52  return ' genN = %d, xsec = %5.5f pb, genEff = %2.2f, intLumi = %5.2f, addWeight = %3.2f -> weight = %3.5f' \
53  % ( self.genNEvents,
54  self.xSection,
55  self.genEff,
56  self.intLumi,
57  self.addWeight,
58  self.GetWeight() )
59 
60 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def GetWeight(self)
Definition: weight.py:33
def SetIntLumi(self, lumi)
Definition: weight.py:42
def __init__(self, genNEvents, xSection, genEff, intLumi=FBINV, addWeight=1)
Definition: weight.py:20
def __str__(self)
Definition: weight.py:46
def printWeights(weights)
Definition: weight.py:5