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 |
> |
#os.mkdir(path+'/sys') |
21 |
> |
argv = sys.argv |
22 |
> |
parser = OptionParser() |
23 |
> |
parser.add_option("-U", "--update", dest="update", default=0, |
24 |
> |
help="update infofile") |
25 |
> |
parser.add_option("-D", "--discr", dest="discr", default="", |
26 |
> |
help="discriminators to be added") |
27 |
> |
#parser.add_option("-I", "--inpath", dest="inpath", default="", |
28 |
> |
# help="path to samples") |
29 |
> |
#parser.add_option("-O", "--outpath", dest="outpath", default="", |
30 |
> |
# help="path where to store output samples") |
31 |
> |
parser.add_option("-S", "--samples", dest="names", default="", |
32 |
> |
help="samples you want to run on") |
33 |
> |
parser.add_option("-C", "--config", dest="config", default=[], action="append", |
34 |
> |
help="configuration file") |
35 |
> |
(opts, args) = parser.parse_args(argv) |
36 |
> |
|
37 |
> |
if opts.config =="": |
38 |
> |
opts.config = "config" |
39 |
> |
|
40 |
> |
#Import after configure to get help message |
41 |
> |
from myutils import BetterConfigParser, progbar, printc, mvainfo, ParseInfo |
42 |
> |
|
43 |
> |
config = BetterConfigParser() |
44 |
> |
#config.read('./config7TeV_ZZ') |
45 |
> |
config.read(opts.config) |
46 |
> |
anaTag = config.get("Analysis","tag") |
47 |
|
|
48 |
|
#get locations: |
49 |
|
Wdir=config.get('Directories','Wdir') |
50 |
< |
|
50 |
> |
samplesinfo=config.get('Directories','samplesinfo') |
51 |
|
|
52 |
|
#systematics |
53 |
< |
systematics=config.get('systematics','systematics') |
54 |
< |
systematics=systematics.split(' ') |
53 |
> |
INpath = config.get('Directories','MVAin') |
54 |
> |
OUTpath = config.get('Directories','MVAout') |
55 |
> |
|
56 |
> |
info = ParseInfo(samplesinfo,INpath) |
57 |
> |
|
58 |
> |
arglist=opts.discr #RTight_blavla,bsbsb |
59 |
> |
|
60 |
> |
namelistIN=opts.names |
61 |
> |
namelist=namelistIN.split(',') |
62 |
|
|
63 |
< |
#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])) |
63 |
> |
#doinfo=bool(int(opts.update)) |
64 |
|
|
65 |
|
MVAlist=arglist.split(',') |
66 |
|
|
67 |
|
#CONFIG |
68 |
|
#factory |
69 |
|
factoryname=config.get('factory','factoryname') |
70 |
+ |
|
71 |
+ |
#load the namespace |
72 |
+ |
VHbbNameSpace=config.get('VHbbNameSpace','library') |
73 |
+ |
ROOT.gSystem.Load(VHbbNameSpace) |
74 |
+ |
|
75 |
|
#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=[] |
76 |
|
MVAinfos=[] |
77 |
+ |
MVAdir=config.get('Directories','vhbbpath') |
78 |
|
for MVAname in MVAlist: |
79 |
< |
MVAinfofile = open(Wdir+'/weights/'+factoryname+'_'+MVAname+'.info','r') |
79 |
> |
MVAinfofile = open(MVAdir+'/data/'+factoryname+'_'+MVAname+'.info','r') |
80 |
|
MVAinfos.append(pickle.load(MVAinfofile)) |
81 |
|
MVAinfofile.close() |
82 |
|
|
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 |
83 |
|
longe=40 |
84 |
|
#Workdir |
85 |
|
workdir=ROOT.gDirectory.GetPath() |
86 |
– |
#os.mkdir(Apath+'/MVAout') |
86 |
|
|
87 |
< |
#Book TMVA readers: MVAlist=["MMCC_bla","CC5050_bla"] |
88 |
< |
readers=[] |
89 |
< |
for MVA in MVAlist: |
90 |
< |
readers.append(ROOT.TMVA.Reader("!Color:!Silent")) |
91 |
< |
|
92 |
< |
#define variables and specatators |
93 |
< |
MVA_var_buffer = [] |
94 |
< |
for i in range(len( MVA_Vars['Nominal'])): |
95 |
< |
MVA_var_buffer.append(array( 'f', [ 0 ] )) |
96 |
< |
for reader in readers: |
97 |
< |
reader.AddVariable( MVA_Vars['Nominal'][i],MVA_var_buffer[i]) |
98 |
< |
#MVA_spectator_buffer = [] |
99 |
< |
#for i in range(len(spectators)): |
100 |
< |
# MVA_spectator_buffer.append(array( 'f', [ 0 ] )) |
101 |
< |
# for reader in readers: |
102 |
< |
# reader.AddSpectator(spectators[i],MVA_spectator_buffer[i]) |
103 |
< |
#Load raeder |
104 |
< |
for i in range(0,len(readers)): |
105 |
< |
readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile()) |
106 |
< |
#--> Now the MVA is booked |
107 |
< |
|
108 |
< |
#Apply samples |
109 |
< |
infofile = open(Apath+'/samples.info','r') |
110 |
< |
Ainfo = pickle.load(infofile) |
111 |
< |
infofile.close() |
87 |
> |
class MvaEvaluater: |
88 |
> |
def __init__(self, config, MVAinfo): |
89 |
> |
self.varset = MVAinfo.varset |
90 |
> |
#Define reader |
91 |
> |
self.reader = ROOT.TMVA.Reader("!Color:!Silent") |
92 |
> |
MVAdir=config.get('Directories','vhbbpath') |
93 |
> |
self.systematics=config.get('systematics','systematics').split(' ') |
94 |
> |
self.MVA_Vars={} |
95 |
> |
self.MVAname = MVAinfo.MVAname |
96 |
> |
for systematic in self.systematics: |
97 |
> |
self.MVA_Vars[systematic]=config.get(self.varset,systematic) |
98 |
> |
self.MVA_Vars[systematic]=self.MVA_Vars[systematic].split(' ') |
99 |
> |
#define variables and specatators |
100 |
> |
self.MVA_var_buffer = [] |
101 |
> |
for i in range(len( self.MVA_Vars['Nominal'])): |
102 |
> |
self.MVA_var_buffer.append(array( 'f', [ 0 ] )) |
103 |
> |
self.reader.AddVariable( self.MVA_Vars['Nominal'][i],self.MVA_var_buffer[i]) |
104 |
> |
self.reader.BookMVA(MVAinfo.MVAname,MVAdir+'/data/'+MVAinfo.getweightfile()) |
105 |
> |
#--> Now the MVA is booked |
106 |
> |
|
107 |
> |
def setBranches(self,tree,job): |
108 |
> |
#Set formulas for all vars |
109 |
> |
self.MVA_formulas={} |
110 |
> |
for systematic in self.systematics: |
111 |
> |
if job.type == 'DATA' and not systematic == 'Nominal': continue |
112 |
> |
self.MVA_formulas[systematic]=[] |
113 |
> |
for j in range(len( self.MVA_Vars['Nominal'])): |
114 |
> |
self.MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),self.MVA_Vars[systematic][j],tree)) |
115 |
> |
|
116 |
> |
def evaluate(self,MVAbranches,job): |
117 |
> |
#Evaluate all vars and fill the branches |
118 |
> |
for systematic in self.systematics: |
119 |
> |
for j in range(len( self.MVA_Vars['Nominal'])): |
120 |
> |
if job.type == 'DATA' and not systematic == 'Nominal': continue |
121 |
> |
self.MVA_var_buffer[j][0] = self.MVA_formulas[systematic][j].EvalInstance() |
122 |
> |
MVAbranches[self.systematics.index(systematic)] = self.reader.EvaluateMVA(self.MVAname) |
123 |
> |
|
124 |
> |
|
125 |
> |
theMVAs = [] |
126 |
> |
for mva in MVAinfos: |
127 |
> |
theMVAs.append(MvaEvaluater(config,mva)) |
128 |
> |
|
129 |
|
|
130 |
|
#eval |
131 |
< |
for job in Ainfo[start:stop]: |
131 |
> |
|
132 |
> |
samples = info.get_samples(namelist) |
133 |
> |
for job in samples: |
134 |
|
#get trees: |
135 |
< |
input = TFile.Open(job.getpath(),'read') |
136 |
< |
outfile = TFile.Open(job.path+'/MVAout2/'+job.prefix+job.identifier+'.root','recreate') |
135 |
> |
print(INpath+'/'+job.prefix+job.identifier+'.root') |
136 |
> |
input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read') |
137 |
> |
print(OUTpath+'/'+job.prefix+job.identifier+'.root') |
138 |
> |
outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate') |
139 |
|
input.cd() |
140 |
|
obj = ROOT.TObject |
141 |
|
for key in ROOT.gDirectory.GetListOfKeys(): |
142 |
|
input.cd() |
143 |
|
obj = key.ReadObj() |
144 |
< |
print obj.GetName() |
144 |
> |
#print obj.GetName() |
145 |
|
if obj.GetName() == job.tree: |
146 |
|
continue |
147 |
|
outfile.cd() |
148 |
< |
print key.GetName() |
148 |
> |
#print key.GetName() |
149 |
|
obj.Write(key.GetName()) |
150 |
|
tree = input.Get(job.tree) |
151 |
|
nEntries = tree.GetEntries() |
152 |
|
outfile.cd() |
153 |
|
newtree = tree.CloneTree(0) |
154 |
< |
|
155 |
< |
#MCs: |
156 |
< |
if job.type != 'DATA': |
157 |
< |
MVA_formulas={} |
158 |
< |
for systematic in systematics: |
159 |
< |
#print '\t\t - ' + systematic |
160 |
< |
MVA_formulas[systematic]=[] |
161 |
< |
#create TTreeFormulas |
162 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
143 |
< |
MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree)) |
144 |
< |
outfile.cd() |
145 |
< |
#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 |
155 |
< |
else: |
156 |
< |
long=nEntries |
157 |
< |
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 |
179 |
< |
MVA_formulas_Nominal = [] |
180 |
< |
#create TTreeFormulas |
181 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
182 |
< |
MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree)) |
183 |
< |
outfile.cd() |
184 |
< |
MVAbranches=[] |
185 |
< |
for i in range(0,len(readers)): |
154 |
> |
|
155 |
> |
#Set branch adress for all vars |
156 |
> |
for i in range(0,len(theMVAs)): |
157 |
> |
theMVAs[i].setBranches(tree,job) |
158 |
> |
outfile.cd() |
159 |
> |
#Setup Branches |
160 |
> |
MVAbranches=[] |
161 |
> |
for i in range(0,len(theMVAs)): |
162 |
> |
if job.type == 'Data': |
163 |
|
MVAbranches.append(array('f',[0])) |
164 |
|
newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F') |
188 |
– |
#progbar |
189 |
– |
print '\n--> ' + job.name +':' |
190 |
– |
if nEntries >= longe: |
191 |
– |
step=int(nEntries/longe) |
192 |
– |
long=longe |
165 |
|
else: |
166 |
< |
long=nEntries |
167 |
< |
step = 1 |
168 |
< |
bar=progbar(long) |
169 |
< |
#Fill event by event: |
170 |
< |
for entry in range(0,nEntries): |
171 |
< |
if entry % step == 0: |
172 |
< |
bar.move() |
173 |
< |
#load entry |
174 |
< |
tree.GetEntry(entry) |
175 |
< |
#nominal: |
176 |
< |
for j in range(len( MVA_Vars['Nominal'])): |
177 |
< |
MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance() |
178 |
< |
|
179 |
< |
for j in range(0,len(readers)): |
180 |
< |
MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname) |
181 |
< |
newtree.Fill() |
182 |
< |
newtree.AutoSave() |
183 |
< |
outfile.Close() |
184 |
< |
|
185 |
< |
|
186 |
< |
|
187 |
< |
|
188 |
< |
|
189 |
< |
#Update Info: |
190 |
< |
if doinfo: |
191 |
< |
for job in Ainfo: |
192 |
< |
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 |
< |
|
166 |
> |
MVAbranches.append(array('f',[0]*11)) |
167 |
> |
newtree.Branch(theMVAs[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F') |
168 |
> |
MVA_formulas_Nominal = [] |
169 |
> |
print('\n--> ' + job.name +':') |
170 |
> |
#progbar setup |
171 |
> |
if nEntries >= longe: |
172 |
> |
step=long(nEntries/longe) |
173 |
> |
long=longe |
174 |
> |
else: |
175 |
> |
long=nEntries |
176 |
> |
step = 1 |
177 |
> |
bar=progbar(long) |
178 |
> |
#Fill event by event: |
179 |
> |
for entry in range(0,nEntries): |
180 |
> |
if entry % step == 0: |
181 |
> |
bar.move() |
182 |
> |
#load entry |
183 |
> |
tree.GetEntry(entry) |
184 |
> |
|
185 |
> |
for i in range(0,len(theMVAs)): |
186 |
> |
theMVAs[i].evaluate(MVAbranches[i],job) |
187 |
> |
#Fill: |
188 |
> |
newtree.Fill() |
189 |
> |
newtree.AutoSave() |
190 |
> |
outfile.Close() |
191 |
> |
|
192 |
> |
print('\n') |