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 |
10 |
|
import warnings |
11 |
|
warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' ) |
12 |
|
from optparse import OptionParser |
13 |
– |
from BetterConfigParser import BetterConfigParser |
14 |
– |
from samplesclass import sample |
15 |
– |
from mvainfos import mvainfo |
13 |
|
import pickle |
17 |
– |
from progbar import progbar |
18 |
– |
from printcolor import printc |
14 |
|
|
20 |
– |
#CONFIGURE |
15 |
|
|
16 |
+ |
#CONFIGURE |
17 |
+ |
ROOT.gROOT.SetBatch(True) |
18 |
+ |
print('hello') |
19 |
|
#load config |
23 |
– |
#os.mkdir(path+'/sys') |
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") |
30 |
– |
parser.add_option("-P", "--path", dest="path", default="", |
31 |
– |
help="path to samples") |
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 |
< |
MVAdir=config.get('Directories','MVAdir') |
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 |
|
|
51 |
– |
#TreeVar Array |
52 |
– |
#MVA_Vars={} |
53 |
– |
#for systematic in systematics: |
54 |
– |
# MVA_Vars[systematic]=config.get('treeVars',systematic) |
55 |
– |
# MVA_Vars[systematic]=MVA_Vars[systematic].split(' ') |
56 |
– |
|
57 |
– |
###################### |
58 |
– |
#Evaluate multi: Must Have same treeVars!!! |
59 |
– |
|
60 |
– |
Apath=opts.path |
61 |
– |
infofile = open(Apath+'/samples.info','r') |
62 |
– |
info = pickle.load(infofile) |
63 |
– |
infofile.close() |
52 |
|
arglist=opts.discr #RTight_blavla,bsbsb |
53 |
|
|
54 |
|
namelistIN=opts.names |
55 |
|
namelist=namelistIN.split(',') |
56 |
|
|
57 |
< |
doinfo=bool(int(opts.update)) |
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 |
77 |
– |
#MVAnames=[] |
78 |
– |
#for MVA in MVAlist: |
79 |
– |
# print MVA |
80 |
– |
# MVAnames.append(config.get(MVA,'MVAname')) |
81 |
– |
#print Wdir+'/weights/'+factoryname+'_'+MVAname+'.info' |
82 |
– |
#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 |
|
|
89 |
– |
treeVarSet=MVAinfos[0].varset |
90 |
– |
#variables |
91 |
– |
#TreeVar Array |
92 |
– |
MVA_Vars={} |
93 |
– |
for systematic in systematics: |
94 |
– |
MVA_Vars[systematic]=config.get(treeVarSet,systematic) |
95 |
– |
MVA_Vars[systematic]=MVA_Vars[systematic].split(' ') |
96 |
– |
#Spectators: |
97 |
– |
#spectators=config.get(treeVarSet,'spectators') |
98 |
– |
#spectators=spectators.split(' ') |
99 |
– |
#progbar quatsch |
77 |
|
longe=40 |
78 |
|
#Workdir |
79 |
|
workdir=ROOT.gDirectory.GetPath() |
103 |
– |
#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 |
111 |
< |
MVA_var_buffer = [] |
112 |
< |
MVA_var_buffer4 = [] |
113 |
< |
for i in range(len( MVA_Vars['Nominal'])): |
114 |
< |
MVA_var_buffer.append(array( 'f', [ 0 ] )) |
115 |
< |
for reader in readers: |
116 |
< |
reader.AddVariable( MVA_Vars['Nominal'][i],MVA_var_buffer[i]) |
117 |
< |
#MVA_spectator_buffer = [] |
118 |
< |
#for i in range(len(spectators)): |
119 |
< |
# MVA_spectator_buffer.append(array( 'f', [ 0 ] )) |
120 |
< |
# for reader in readers: |
121 |
< |
# reader.AddSpectator(spectators[i],MVA_spectator_buffer[i]) |
122 |
< |
#Load raeder |
123 |
< |
for i in range(0,len(readers)): |
124 |
< |
readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile()) |
125 |
< |
#--> Now the MVA is booked |
126 |
< |
|
127 |
< |
#Apply samples |
128 |
< |
infofile = open(Apath+'/samples.info','r') |
129 |
< |
Ainfo = pickle.load(infofile) |
130 |
< |
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: |
90 |
< |
if eval(job.active): |
91 |
< |
if job.name in namelist: |
92 |
< |
#get trees: |
93 |
< |
input = TFile.Open(Apath+'/'+job.getpath(),'read') |
94 |
< |
outfile = TFile.Open(job.path+'/'+MVAdir+job.prefix+job.identifier+'.root','recreate') |
95 |
< |
input.cd() |
96 |
< |
obj = ROOT.TObject |
97 |
< |
for key in ROOT.gDirectory.GetListOfKeys(): |
98 |
< |
input.cd() |
99 |
< |
obj = key.ReadObj() |
100 |
< |
#print obj.GetName() |
101 |
< |
if obj.GetName() == job.tree: |
102 |
< |
continue |
103 |
< |
outfile.cd() |
104 |
< |
#print key.GetName() |
105 |
< |
obj.Write(key.GetName()) |
106 |
< |
tree = input.Get(job.tree) |
107 |
< |
nEntries = tree.GetEntries() |
108 |
< |
outfile.cd() |
109 |
< |
newtree = tree.CloneTree(0) |
110 |
< |
|
111 |
< |
#MCs: |
112 |
< |
if job.type != 'DATA': |
113 |
< |
MVA_formulas={} |
114 |
< |
MVA_formulas4={} |
115 |
< |
for systematic in systematics: |
116 |
< |
#print '\t\t - ' + systematic |
117 |
< |
MVA_formulas[systematic]=[] |
118 |
< |
MVA_formulas4[systematic]=[] |
119 |
< |
#create TTreeFormulas |
120 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
121 |
< |
MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree)) |
122 |
< |
MVA_formulas4[systematic].append(ROOT.TTreeFormula("MVA_formula4%s_%s"%(j,systematic),MVA_Vars['Nominal'][j]+'+('+MVA_Vars[systematic][j]+'-'+MVA_Vars['Nominal'][j]+')*4',tree))#HERE change |
123 |
< |
outfile.cd() |
124 |
< |
#Setup Branches |
125 |
< |
MVAbranches=[] |
126 |
< |
MVAbranches4=[] |
127 |
< |
for i in range(0,len(readers)): |
128 |
< |
MVAbranches.append(array('f',[0]*9)) |
129 |
< |
MVAbranches4.append(array('f',[0]*9)) |
130 |
< |
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') |
131 |
< |
newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches4[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down/F') |
132 |
< |
print '\n--> ' + job.name +':' |
177 |
< |
#progbar setup |
178 |
< |
if nEntries >= longe: |
179 |
< |
step=int(nEntries/longe) |
180 |
< |
long=longe |
181 |
< |
else: |
182 |
< |
long=nEntries |
183 |
< |
step = 1 |
184 |
< |
bar=progbar(long) |
185 |
< |
#Fill event by event: |
186 |
< |
for entry in range(0,nEntries): |
187 |
< |
if entry % step == 0: |
188 |
< |
bar.move() |
189 |
< |
#load entry |
190 |
< |
tree.GetEntry(entry) |
191 |
< |
for systematic in systematics: |
192 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
193 |
< |
MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance() |
194 |
< |
|
195 |
< |
for j in range(0,len(readers)): |
196 |
< |
MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
197 |
< |
|
198 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
199 |
< |
MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance() |
89 |
> |
|
90 |
> |
samples = info.get_samples(namelist) |
91 |
> |
print(samples) |
92 |
> |
for job in samples: |
93 |
> |
#get trees: |
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() |
104 |
> |
if obj.GetName() == job.tree: |
105 |
> |
continue |
106 |
> |
outfile.cd() |
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.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 |
> |
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 |
> |
print('\n--> ' + job.name +':') |
130 |
> |
#Fill event by event: |
131 |
> |
for entry in range(0,nEntries): |
132 |
> |
newtree.GetEntry(entry) |
133 |
|
|
134 |
< |
for j in range(0,len(readers)): |
135 |
< |
MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
136 |
< |
#Fill: |
137 |
< |
newtree.Fill() |
138 |
< |
newtree.AutoSave() |
139 |
< |
outfile.Close() |
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 |
< |
#DATA: |
209 |
< |
if job.type == 'DATA': |
210 |
< |
#MVA Formulas |
211 |
< |
MVA_formulas_Nominal = [] |
212 |
< |
#create TTreeFormulas |
213 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
214 |
< |
MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree)) |
215 |
< |
outfile.cd() |
216 |
< |
MVAbranches=[] |
217 |
< |
for i in range(0,len(readers)): |
218 |
< |
MVAbranches.append(array('f',[0])) |
219 |
< |
newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F') |
220 |
< |
newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches[i],'nominal/F') |
221 |
< |
#progbar |
222 |
< |
print '\n--> ' + job.name +':' |
223 |
< |
if nEntries >= longe: |
224 |
< |
step=int(nEntries/longe) |
225 |
< |
long=longe |
226 |
< |
else: |
227 |
< |
long=nEntries |
228 |
< |
step = 1 |
229 |
< |
bar=progbar(long) |
230 |
< |
#Fill event by event: |
231 |
< |
for entry in range(0,nEntries): |
232 |
< |
if entry % step == 0: |
233 |
< |
bar.move() |
234 |
< |
#load entry |
235 |
< |
tree.GetEntry(entry) |
236 |
< |
#nominal: |
237 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
238 |
< |
MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance() |
239 |
< |
|
240 |
< |
for j in range(0,len(readers)): |
241 |
< |
MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
242 |
< |
newtree.Fill() |
243 |
< |
newtree.AutoSave() |
244 |
< |
outfile.Close() |
245 |
< |
|
246 |
< |
print '\n' |
247 |
< |
|
248 |
< |
#Update Info: |
249 |
< |
if doinfo: |
250 |
< |
for job in Ainfo: |
251 |
< |
for MVAinfo in MVAinfos: |
252 |
< |
job.addcomment('Added MVA %s'%MVAinfo.MVAname) |
253 |
< |
job.addpath(MVAdir) |
254 |
< |
infofile = open(Apath+MVAdir+'/samples.info','w') |
255 |
< |
pickle.dump(Ainfo,infofile) |
256 |
< |
infofile.close() |
257 |
< |
|
258 |
< |
|
141 |
> |
print('\n') |