DESY Hbb Analysis Framework
pileupWeights.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # pileupCalcMC.py
4 
5 from argparse import ArgumentParser
6 from ROOT import TH1D
7 from ROOT import TH1F
8 from ROOT import TFile
9 
10 # parsing arguments
11 parser = ArgumentParser()
12 parser.add_argument("--data" , dest="data" , help="file containing data pileup")
13 parser.add_argument("--mc" , dest="mc" , help="file containing mc pileup")
14 parser.add_argument("--up1" , dest="up1" , help="file containing data pileup 1sigma up var")
15 parser.add_argument("--down1" , dest="down1" , help="file containing data pileup 1sigma down var")
16 parser.add_argument("--up2" , dest="up2" , help="file containing data pileup 2sigma up var")
17 parser.add_argument("--down2" , dest="down2" , help="file containing data pileup 2sigma down var")
18 parser.add_argument("--output" , dest="out" , help="name ouf output file", default = "PileupWeight.root")
19 
20 args = parser.parse_args()
21 if not ( args.data and args.mc ):
22  print "nothing to be done"
23  quit()
24 
25 f_data = TFile(args.data,"old")
26 h_data = TH1D(f_data.Get("pileup"))
27 h_data.Scale(1./h_data.Integral())
28 
29 f_mc = TFile(args.mc,"old")
30 try:
31  h_mc = TH1D(f_mc.Get("pileup"))
32 except TypeError as error:
33  h_mc = TH1F(f_mc.Get("pileup"))
34 
35 h_mc.Scale(1./h_mc.Integral())
36 
37 h_weight = TH1D(h_data.Clone("weight"));
38 h_weight.SetTitle("pileup weight")
39 h_weight.Divide(h_mc);
40 
41 if args.up1:
42  f_up1 = TFile(args.up1,"old")
43  h_up1 = TH1D(f_up1.Get("pileup"))
44  h_up1.SetName("weight_1up")
45  h_up1.SetTitle("pileup weight +1#sigma")
46  h_up1.Scale(1./h_up1.Integral())
47  h_up1.Divide(h_mc)
48 
49 if args.down1:
50  f_down1 = TFile(args.down1,"old")
51  h_down1 = TH1D(f_down1.Get("pileup"))
52  h_down1.SetName("weight_1down")
53  h_down1.SetTitle("pileup weight -1#sigma")
54  h_down1.Scale(1./h_down1.Integral())
55  h_down1.Divide(h_mc)
56 
57 if args.up2:
58  f_up2 = TFile(args.up2,"old")
59  h_up2 = TH1D(f_up2.Get("pileup"))
60  h_up2.SetName("weight_2up")
61  h_up2.SetTitle("pileup weight +2#sigma")
62  h_up2.Scale(1./h_up2.Integral())
63  h_up2.Divide(h_mc)
64 
65 if args.down2:
66  f_down2 = TFile(args.down2,"old")
67  h_down2 = TH1D(f_down2.Get("pileup"))
68  h_down2.SetName("weight_2down")
69  h_down2.SetTitle("pileup weight -2#sigma")
70  h_down2.Scale(1./h_down2.Integral())
71  h_down2.Divide(h_mc)
72 
73 
74 fout = TFile(args.out,"recreate")
75 h_weight.Write()
76 if args.up1:
77  h_up1.Write()
78 if args.down1:
79  h_down1.Write()
80 if args.up2:
81  h_up2.Write()
82 if args.down2:
83  h_down2.Write()
84 
85 
86 fout.Close()
87