-
Notifications
You must be signed in to change notification settings - Fork 1
/
PileupWeightTool.py
60 lines (50 loc) · 2.37 KB
/
PileupWeightTool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/usr/env python
# Author: Izaak Neutelings (November 2018)
from ROOT import TFile
import os
path = 'pileup/'
def ensureTFile(filename,option='READ'):
"""Open TFile, checking if the file in the given path exists."""
if not os.path.isfile(filename):
print '>>> ERROR! ensureTFile: File in path "%s" does not exist!!'%(filename)
exit(1)
file = TFile(filename,option)
if not file or file.IsZombie():
print '>>> ERROR! ensureTFile Could not open file by name "%s"'%(filename)
exit(1)
return file
class PileupWeightTool:
def __init__( self, year=2017, sigma='central' ):
"""Load data and MC pilup profiles."""
assert( year in [2016,2017,2018] ), "You must choose a year from: 2016, 2017, or 2018."
assert( sigma in ['central','up','down'] ), "You must choose a s.d. variation from: 'central', 'up', or 'down'."
minbias = '69p2'
if sigma=='down':
minbias = '66p0168' # -4.6%
elif sigma=='up':
minbias = '72p3832' # +4.6%
if year==2016:
self.datafile = ensureTFile( path+'Data_PileUp_2016_%s.root'%(minbias), 'READ')
self.mcfile = ensureTFile( path+'MC_PileUp_2016.root', 'READ')
elif year==2017:
self.datafile = ensureTFile( path+'Data_PileUp_2017_%s.root'%(minbias), 'READ')
self.mcfile = ensureTFile( path+'MC_PileUp_2017.root', 'READ')
else:
self.datafile = ensureTFile( path+'Data_PileUp_2018_%s.root'%(minbias), 'READ')
self.mcfile = ensureTFile( path+'MC_PileUp_2018.root', 'READ')
self.datahist = self.datafile.Get('pileup')
self.mchist = self.mcfile.Get('pileup')
self.datahist.SetDirectory(0)
self.mchist.SetDirectory(0)
self.datahist.Scale(1./self.datahist.Integral())
self.mchist.Scale(1./self.mchist.Integral())
self.datafile.Close()
self.mcfile.Close()
def getWeight(self,npu):
"""Get pileup weight for a given number of pileup interactions."""
data = self.datahist.GetBinContent(self.datahist.GetXaxis().FindBin(npu))
mc = self.mchist.GetBinContent(self.mchist.GetXaxis().FindBin(npu))
if mc>0.:
return data/mc
print ">>> Warning! PileupWeightTools.getWeight: Could not make pileup weight for npu=%s data=%s, mc=%s"%(npu,data,mc)
return 1.