CMS 3D CMS Logo

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