-
Notifications
You must be signed in to change notification settings - Fork 7
/
heppi-make-plotcard
executable file
·131 lines (119 loc) · 4.67 KB
/
heppi-make-plotcard
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
#!/usr/bin/env python
import sys, os, glob, json, re
print("version python : ", sys.version)
import ROOT
from optparse import OptionParser
from heppi import heppi
# --------------------
# read arguments
# --------------------
def create_json(rootfile='file.root', treename=None, jout=''):
"""
Create a json template file for
plotng all the branches in a given
tree
"""
root = ROOT.TFile.Open(rootfile)
treenm = []
def getall(d, basepath=""):
try:
for key in d.GetListOfKeys():
kname = key.GetName()
if key.IsFolder():
for i in getall(d.Get(kname), os.path.join(basepath,kname)):
yield i
else:
yield basepath+kname, d.Get(kname)
except :
yield basepath, d
trees = []
for k, o in getall(root):
if type(o) == type(ROOT.TTree()) and o.GetEntries() > 0:
if o.GetName() not in treename:
continue
trees.append(k.split(o.GetName())[0] + o.GetName())
if len(trees) > 0:
heppi.logger.info(' The following trees have been found on this root file :')
heppi.logger.info( trees )
heppi.logger.info(' Only "' + trees[0] + '" will be used to build the plotcard')
else:
heppi.logger.error( 'No TTree found on this root file or no empty TTree' )
tree = ROOT.gDirectory.Get(trees[0])
varlist={}
for var in tree.GetListOfLeaves():
L = tree.GetBranch(str(var))
if '[' in var.GetTitle() and ']' in var.GetTitle():
dim = [int(s) for s in re.findall('[-+]?\d*\.\d+|\d+', var.GetTitle().split('[')[1])]
for idim in range(0, dim[0]):
_var_ = var.GetTitle().replace('[%i]'%dim[0], '[%i]'%idim)
tree.Project("_h_"+_var_.split('[')[0],_var_)
_h_ = ROOT.gDirectory.Get('_h_' + _var_.split('[')[0])
nbin = _h_.GetNbinsX()
xmin = _h_.GetXaxis().GetXmin()
xmax = _h_.GetXaxis().GetXmax()
hist = "(%i,%1.3f,%1.3f)" % (nbin,xmin,xmax)
varlist[_var_]={
'cut' :'',
"hist" : hist,
"cut" : "",
"blind": "",
"log" : True ,
"norm" : False,
"title": var.GetTitle().replace('[%i]'%dim[0], '_%i'%idim)
}
print ('variable: %25s' % var.GetTitle().replace('[%i]'%dim[0], '[%i]'%idim))
else:
tree.Project("_h_"+var.GetTitle(),var.GetTitle())
_h_ = ROOT.gDirectory.Get('_h_'+var.GetTitle())
nbin = _h_.GetNbinsX()
xmin = _h_.GetXaxis().GetXmin()
xmax = _h_.GetXaxis().GetXmax()
hist = "(%i,%1.1f,%1.1f)" % (nbin,xmin,xmax)
varlist[var.GetTitle()]={
'cut' :'',
"hist" : hist,
"cut" : "",
"blind": "",
"log" : True ,
"norm" : False,
"title": var.GetTitle()
}
print ('variable: %25s' % var.GetTitle())
samples_new = {}
sample_tree = {'name':treename}
selection = {'cutflow':''}
plot_options = heppi.options()
with open(jout, "w") as file:
json.dump({'variables':varlist,
'processes':{
"SMprocess": {
"color": 138,
"order": 0,
"files" : rootfile,
"title" : 'SMprocess',
"tree" : trees[0],
"cut" : "",
"label" : "background"
},
},
'option':plot_options.__dict__ } ,
file, indent=2)
file.close()
# --------------------
# read arguments
# --------------------
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-r", "--load", dest="rootfile",default='rootfile.root',
help="create a plot card from tree", metavar="FILE")
parser.add_option("-t", "--tree", dest="treename",default=None,
help="Dumper tree that you wnat to plot", metavar="FILE")
parser.add_option("-o", "--out", dest="jsonfile",default="plotcard.json",
help="specify output json file")
(options, args) = parser.parse_args()
if os.path.exists(options.rootfile) :
create_json( options.rootfile,
options.treename,
options.jsonfile)
else:
parser.print_help()