forked from easilar/LFVTOPLimits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertPostfitShapesForPlotIt.py
More file actions
148 lines (108 loc) · 4.66 KB
/
convertPostfitShapesForPlotIt.py
File metadata and controls
148 lines (108 loc) · 4.66 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env python
import os, sys, argparse, math
# to prevent pyroot to hijack argparse we need to go around
tmpargv = sys.argv[:]
sys.argv = []
# ROOT imports
from ROOT import gROOT, gSystem, PyConfig, TFile, TColor, TCanvas
gROOT.Reset()
gROOT.SetBatch()
PyConfig.IgnoreCommandLineOptions = True
sys.argv = tmpargv
def shift_hist(hist, by):
for b in range(1, hist.GetNbinsX() + 1):
hist.SetBinContent(b, hist.GetBinContent(b) + by * hist.GetBinError(b))
hist.SetBinError(b, 0)
def remove_errors(hist):
for b in range(1, hist.GetNbinsX() + 1):
hist.SetBinError(b, 0)
parser = argparse.ArgumentParser(description='Compute data/MC scale factors from a MaxLikelihoodFit')
parser.add_argument('-i', '--input', action='store', type=str, dest='input', help='Path to the ROOT file created by combine harvester', required=True)
parser.add_argument('-s', '--signals', action='store', type=str, dest='signals', help='Path to the ROOT file containing all the signal shapes')
options = parser.parse_args()
# Compute scale factors
file = TFile.Open(options.input)
channels = set()
for k in file.GetListOfKeys():
name = k.GetName().split('_')
name.pop()
channels.add('_'.join(name))
channels = list(channels)
print "Detected channels: ", channels
# Construct the list of processs
# Naming is 'category/bkg_name'
processs = set()
total_unc = ['tt_postfit_histos__totalup','tt_postfit_histos__totaldown']
for proc in file.Get('%s_prefit' % channels[0]).GetListOfKeys():
processs.add(proc.GetName())
print 'Detected processes: ', processs
output_dir = 'postfit_shapes_TOP_LFV_forPlotIt'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("")
print("Creating ROOT files suitable for plotIt...")
# Prepare shapes for plotIt
for process in processs:
print "Process: ", process
output_filename = "%s_postfit_histos.root" % (process)
plot_file = TFile.Open(os.path.join(output_dir, output_filename), 'recreate')
for channel in channels:
# print " Channel : ", channel
# Nominal post-fit shape
nominal_postfit = file.Get('%s_postfit/%s' % (channel, process))
try:
nominal_postfit.SetName(channel)
nominal_postfit.Write()
print " Channel : ", channel
if process != 'data_obs':# and not process.startswith('Hct') and not process.startswith('Hut'):
nominal_postfit_up = nominal_postfit.Clone()
nominal_postfit_up.SetName(channel + '__postfitup')
shift_hist(nominal_postfit_up, 1)
nominal_postfit_down = nominal_postfit.Clone()
nominal_postfit_down.SetName(channel + '__postfitdown')
shift_hist(nominal_postfit_down, -1)
remove_errors(nominal_postfit)
nominal_postfit_up.Write()
nominal_postfit_down.Write()
except: pass
plot_file.Close()
if 'TotalBkg' in process:
for t in total_unc:
print "Process: ", str(t.split('__')[1])
output_filename = "%s.root" % (t)
plot_file = TFile.Open(os.path.join(output_dir, output_filename), 'recreate')
for channel in channels:
print " Channel : ", channel
if 'up' in t:
tot = file.Get('%s_postfit/%s' % (channel, process))
nom = file.Get('%s_postfit/%s' % (channel, 'tt'))
var = tot.Clone()
var.SetName(channel)
shift_hist(var, 1)
var.Add(tot, -1)
var.Add(nom, 1)
var.Write()
elif 'down' in t:
tot = file.Get('%s_postfit/%s' % (channel, process))
nom = file.Get('%s_postfit/%s' % (channel, 'tt'))
var = tot.Clone()
var.SetName(channel)
shift_hist(var, -1)
var.Add(tot, -1)
var.Add(nom, 1)
var.Write()
plot_file.Close()
#not used
if options.signals:
f = TFile.Open(options.signals)
signals = [k.GetName() for k in f.Get(channels[0]).GetListOfKeys() if not '__' in k.GetName() and ('Hct' in k.GetName() or 'Hut' in k.GetName())]
for signal in signals:
output_filename = "%s_postfit_histos.root" % (signal)
plot_file = TFile.Open(os.path.join(output_dir, output_filename), 'recreate')
for channel in channels:
shape = f.Get('%s/%s' % (channel, signal))
shape.SetName(channel)
shape.Write()
plot_file.Close()
f.Close()
print("All done. Files saved in %r" % output_dir)