1 |
|
#!/usr/bin/env python |
2 |
+ |
from __future__ import print_function |
3 |
|
import sys |
4 |
|
import os |
5 |
|
import ROOT |
5 |
– |
from ROOT import TFile |
6 |
|
from array import array |
7 |
|
from math import sqrt |
8 |
|
from copy import copy |
9 |
|
#suppres the EvalInstace conversion warning bug |
10 |
|
import warnings |
11 |
|
warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' ) |
12 |
< |
from ConfigParser import SafeConfigParser |
13 |
< |
from samplesinfo import sample |
14 |
< |
from mvainfos import mvainfo |
12 |
> |
from optparse import OptionParser |
13 |
|
import pickle |
16 |
– |
from progbar import progbar |
17 |
– |
from printcolor import printc |
14 |
|
|
19 |
– |
#CONFIGURE |
15 |
|
|
16 |
+ |
#CONFIGURE |
17 |
+ |
ROOT.gROOT.SetBatch(True) |
18 |
+ |
print('hello') |
19 |
|
#load config |
20 |
< |
config = SafeConfigParser() |
21 |
< |
config.read('./config') |
20 |
> |
argv = sys.argv |
21 |
> |
parser = OptionParser() |
22 |
> |
parser.add_option("-U", "--update", dest="update", default=0, |
23 |
> |
help="update infofile") |
24 |
> |
parser.add_option("-D", "--discr", dest="discr", default="", |
25 |
> |
help="discriminators to be added") |
26 |
> |
parser.add_option("-S", "--samples", dest="names", default="", |
27 |
> |
help="samples you want to run on") |
28 |
> |
parser.add_option("-C", "--config", dest="config", default=[], action="append", |
29 |
> |
help="configuration file") |
30 |
> |
(opts, args) = parser.parse_args(argv) |
31 |
> |
|
32 |
> |
if opts.config =="": |
33 |
> |
opts.config = "config" |
34 |
> |
|
35 |
> |
#Import after configure to get help message |
36 |
> |
from myutils import BetterConfigParser, progbar, printc, ParseInfo, MvaEvaluator |
37 |
> |
|
38 |
> |
config = BetterConfigParser() |
39 |
> |
config.read(opts.config) |
40 |
> |
anaTag = config.get("Analysis","tag") |
41 |
|
|
42 |
|
#get locations: |
43 |
|
Wdir=config.get('Directories','Wdir') |
44 |
< |
|
44 |
> |
samplesinfo=config.get('Directories','samplesinfo') |
45 |
|
|
46 |
|
#systematics |
47 |
< |
systematics=config.get('systematics','systematics') |
48 |
< |
systematics=systematics.split(' ') |
47 |
> |
INpath = config.get('Directories','MVAin') |
48 |
> |
OUTpath = config.get('Directories','MVAout') |
49 |
> |
|
50 |
> |
info = ParseInfo(samplesinfo,INpath) |
51 |
> |
|
52 |
> |
arglist=opts.discr #RTight_blavla,bsbsb |
53 |
> |
|
54 |
> |
namelistIN=opts.names |
55 |
> |
namelist=namelistIN.split(',') |
56 |
|
|
57 |
< |
#TreeVar Array |
34 |
< |
MVA_Vars={} |
35 |
< |
for systematic in systematics: |
36 |
< |
MVA_Vars[systematic]=config.get('treeVars',systematic) |
37 |
< |
MVA_Vars[systematic]=MVA_Vars[systematic].split(' ') |
38 |
< |
|
39 |
< |
###################### |
40 |
< |
#Evaluate multi: Must Have same treeVars!!! |
41 |
< |
|
42 |
< |
Apath=sys.argv[1] |
43 |
< |
arglist=sys.argv[2] #RTight_blavla,bsbsb |
44 |
< |
|
45 |
< |
#for axample |
46 |
< |
#0 5 0 |
47 |
< |
#and |
48 |
< |
#5 -1 1 |
49 |
< |
|
50 |
< |
start=int(sys.argv[3]) |
51 |
< |
stop=int(sys.argv[4]) |
52 |
< |
doinfo=bool(int(sys.argv[5])) |
57 |
> |
#doinfo=bool(int(opts.update)) |
58 |
|
|
59 |
|
MVAlist=arglist.split(',') |
60 |
|
|
61 |
|
#CONFIG |
62 |
|
#factory |
63 |
|
factoryname=config.get('factory','factoryname') |
64 |
+ |
|
65 |
+ |
#load the namespace |
66 |
+ |
VHbbNameSpace=config.get('VHbbNameSpace','library') |
67 |
+ |
ROOT.gSystem.Load(VHbbNameSpace) |
68 |
+ |
|
69 |
|
#MVA |
60 |
– |
#MVAnames=[] |
61 |
– |
#for MVA in MVAlist: |
62 |
– |
# print MVA |
63 |
– |
# MVAnames.append(config.get(MVA,'MVAname')) |
64 |
– |
#print Wdir+'/weights/'+factoryname+'_'+MVAname+'.info' |
65 |
– |
#MVAinfofiles=[] |
70 |
|
MVAinfos=[] |
71 |
+ |
MVAdir=config.get('Directories','vhbbpath') |
72 |
|
for MVAname in MVAlist: |
73 |
< |
MVAinfofile = open(Wdir+'/weights/'+factoryname+'_'+MVAname+'.info','r') |
73 |
> |
MVAinfofile = open(MVAdir+'/data/'+factoryname+'_'+MVAname+'.info','r') |
74 |
|
MVAinfos.append(pickle.load(MVAinfofile)) |
75 |
|
MVAinfofile.close() |
76 |
|
|
72 |
– |
treeVarSet=MVAinfos[0].varset |
73 |
– |
#variables |
74 |
– |
#TreeVar Array |
75 |
– |
MVA_Vars={} |
76 |
– |
for systematic in systematics: |
77 |
– |
MVA_Vars[systematic]=config.get(treeVarSet,systematic) |
78 |
– |
MVA_Vars[systematic]=MVA_Vars[systematic].split(' ') |
79 |
– |
#Spectators: |
80 |
– |
#spectators=config.get(treeVarSet,'spectators') |
81 |
– |
#spectators=spectators.split(' ') |
82 |
– |
#progbar quatsch |
77 |
|
longe=40 |
78 |
|
#Workdir |
79 |
|
workdir=ROOT.gDirectory.GetPath() |
86 |
– |
#os.mkdir(Apath+'/MVAout') |
80 |
|
|
81 |
< |
#Book TMVA readers: MVAlist=["MMCC_bla","CC5050_bla"] |
82 |
< |
readers=[] |
83 |
< |
for MVA in MVAlist: |
84 |
< |
readers.append(ROOT.TMVA.Reader("!Color:!Silent")) |
85 |
< |
|
86 |
< |
#define variables and specatators |
94 |
< |
MVA_var_buffer = [] |
95 |
< |
for i in range(len( MVA_Vars['Nominal'])): |
96 |
< |
MVA_var_buffer.append(array( 'f', [ 0 ] )) |
97 |
< |
for reader in readers: |
98 |
< |
reader.AddVariable( MVA_Vars['Nominal'][i],MVA_var_buffer[i]) |
99 |
< |
#MVA_spectator_buffer = [] |
100 |
< |
#for i in range(len(spectators)): |
101 |
< |
# MVA_spectator_buffer.append(array( 'f', [ 0 ] )) |
102 |
< |
# for reader in readers: |
103 |
< |
# reader.AddSpectator(spectators[i],MVA_spectator_buffer[i]) |
104 |
< |
#Load raeder |
105 |
< |
for i in range(0,len(readers)): |
106 |
< |
readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile()) |
107 |
< |
#--> Now the MVA is booked |
108 |
< |
|
109 |
< |
#Apply samples |
110 |
< |
infofile = open(Apath+'/samples.info','r') |
111 |
< |
Ainfo = pickle.load(infofile) |
112 |
< |
infofile.close() |
81 |
> |
|
82 |
> |
|
83 |
> |
theMVAs = [] |
84 |
> |
for mva in MVAinfos: |
85 |
> |
theMVAs.append(MvaEvaluator(config,mva)) |
86 |
> |
|
87 |
|
|
88 |
|
#eval |
89 |
< |
for job in Ainfo[start:stop]: |
89 |
> |
|
90 |
> |
samples = info.get_samples(namelist) |
91 |
> |
print(samples) |
92 |
> |
for job in samples: |
93 |
|
#get trees: |
94 |
< |
input = TFile.Open(job.getpath(),'read') |
95 |
< |
outfile = TFile.Open(job.path+'/MVAout2/'+job.prefix+job.identifier+'.root','recreate') |
94 |
> |
print(INpath+'/'+job.prefix+job.identifier+'.root') |
95 |
> |
input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read') |
96 |
> |
print(OUTpath+'/'+job.prefix+job.identifier+'.root') |
97 |
> |
outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate') |
98 |
|
input.cd() |
99 |
|
obj = ROOT.TObject |
100 |
|
for key in ROOT.gDirectory.GetListOfKeys(): |
101 |
|
input.cd() |
102 |
|
obj = key.ReadObj() |
103 |
< |
print obj.GetName() |
103 |
> |
#print obj.GetName() |
104 |
|
if obj.GetName() == job.tree: |
105 |
|
continue |
106 |
|
outfile.cd() |
107 |
< |
print key.GetName() |
107 |
> |
#print key.GetName() |
108 |
|
obj.Write(key.GetName()) |
109 |
|
tree = input.Get(job.tree) |
110 |
|
nEntries = tree.GetEntries() |
111 |
|
outfile.cd() |
112 |
< |
newtree = tree.CloneTree(0) |
113 |
< |
|
114 |
< |
#MCs: |
115 |
< |
if job.type != 'DATA': |
116 |
< |
MVA_formulas={} |
117 |
< |
for systematic in systematics: |
118 |
< |
#print '\t\t - ' + systematic |
119 |
< |
MVA_formulas[systematic]=[] |
120 |
< |
#create TTreeFormulas |
121 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
122 |
< |
MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree)) |
123 |
< |
outfile.cd() |
124 |
< |
#Setup Branches |
146 |
< |
MVAbranches=[] |
147 |
< |
for i in range(0,len(readers)): |
148 |
< |
MVAbranches.append(array('f',[0]*9)) |
149 |
< |
newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down/F') |
150 |
< |
print '\n--> ' + job.name +':' |
151 |
< |
#progbar setup |
152 |
< |
if nEntries >= longe: |
153 |
< |
step=int(nEntries/longe) |
154 |
< |
long=longe |
112 |
> |
newtree = tree.CopyTree() #hard skim to get faster |
113 |
> |
input.Close() |
114 |
> |
|
115 |
> |
#Set branch adress for all vars |
116 |
> |
for i in range(0,len(theMVAs)): |
117 |
> |
theMVAs[i].setVariables(newtree,job) |
118 |
> |
outfile.cd() |
119 |
> |
#Setup Branches |
120 |
> |
mvaVals=[] |
121 |
> |
for i in range(0,len(theMVAs)): |
122 |
> |
if job.type == 'Data': |
123 |
> |
mvaVals.append(array('f',[0])) |
124 |
> |
newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F') |
125 |
|
else: |
126 |
< |
long=nEntries |
127 |
< |
step = 1 |
158 |
< |
bar=progbar(long) |
159 |
< |
#Fill event by event: |
160 |
< |
for entry in range(0,nEntries): |
161 |
< |
if entry % step == 0: |
162 |
< |
bar.move() |
163 |
< |
#load entry |
164 |
< |
tree.GetEntry(entry) |
165 |
< |
for systematic in systematics: |
166 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
167 |
< |
MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance() |
168 |
< |
|
169 |
< |
for j in range(0,len(readers)): |
170 |
< |
MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
171 |
< |
#Fill: |
172 |
< |
newtree.Fill() |
173 |
< |
newtree.AutoSave() |
174 |
< |
outfile.Close() |
175 |
< |
|
176 |
< |
#DATA: |
177 |
< |
if job.type == 'DATA': |
178 |
< |
#MVA Formulas |
126 |
> |
mvaVals.append(array('f',[0]*11)) |
127 |
> |
newtree.Branch(theMVAs[i].MVAname,mvaVals[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F') |
128 |
|
MVA_formulas_Nominal = [] |
129 |
< |
#create TTreeFormulas |
130 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
131 |
< |
MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree)) |
132 |
< |
outfile.cd() |
133 |
< |
MVAbranches=[] |
134 |
< |
for i in range(0,len(readers)): |
135 |
< |
MVAbranches.append(array('f',[0])) |
136 |
< |
newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F') |
137 |
< |
#progbar |
138 |
< |
print '\n--> ' + job.name +':' |
139 |
< |
if nEntries >= longe: |
140 |
< |
step=int(nEntries/longe) |
141 |
< |
long=longe |
193 |
< |
else: |
194 |
< |
long=nEntries |
195 |
< |
step = 1 |
196 |
< |
bar=progbar(long) |
197 |
< |
#Fill event by event: |
198 |
< |
for entry in range(0,nEntries): |
199 |
< |
if entry % step == 0: |
200 |
< |
bar.move() |
201 |
< |
#load entry |
202 |
< |
tree.GetEntry(entry) |
203 |
< |
#nominal: |
204 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
205 |
< |
MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance() |
206 |
< |
|
207 |
< |
for j in range(0,len(readers)): |
208 |
< |
MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
209 |
< |
newtree.Fill() |
210 |
< |
newtree.AutoSave() |
211 |
< |
outfile.Close() |
212 |
< |
|
213 |
< |
|
214 |
< |
|
215 |
< |
|
216 |
< |
|
217 |
< |
#Update Info: |
218 |
< |
if doinfo: |
219 |
< |
for job in Ainfo: |
220 |
< |
for MVAinfo in MVAinfos: |
221 |
< |
job.addcomment('Added MVA %s'%MVAinfo.MVAname) |
222 |
< |
job.addpath('/MVAout') |
223 |
< |
infofile = open(Apath+'/MVAout/samples.info','w') |
224 |
< |
pickle.dump(Ainfo,infofile) |
225 |
< |
infofile.close() |
226 |
< |
|
227 |
< |
|
129 |
> |
print('\n--> ' + job.name +':') |
130 |
> |
#Fill event by event: |
131 |
> |
for entry in range(0,nEntries): |
132 |
> |
newtree.GetEntry(entry) |
133 |
> |
|
134 |
> |
for i in range(0,len(theMVAs)): |
135 |
> |
theMVAs[i].evaluate(mvaVals[i],job) |
136 |
> |
#Fill: |
137 |
> |
newtree.Fill() |
138 |
> |
newtree.AutoSave() |
139 |
> |
outfile.Close() |
140 |
> |
|
141 |
> |
print('\n') |