1 |
|
#!/usr/bin/env python |
2 |
+ |
from __future__ import print_function |
3 |
|
import sys |
4 |
< |
import os |
4 |
> |
import os,subprocess |
5 |
|
import ROOT |
6 |
|
from array import array |
7 |
|
from math import sqrt |
11 |
|
warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' ) |
12 |
|
from optparse import OptionParser |
13 |
|
import pickle |
13 |
– |
from myutils import BetterConfigParser, progbar, mvainfo, printc, parse_info |
14 |
|
|
15 |
|
|
16 |
|
#CONFIGURE |
17 |
|
ROOT.gROOT.SetBatch(True) |
18 |
< |
print 'hello' |
18 |
> |
print('hello') |
19 |
|
#load config |
20 |
– |
#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") |
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") |
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 |
|
|
37 |
– |
#from samplesclass import sample |
38 |
– |
#from mvainfos import mvainfo |
39 |
– |
#from progbar import progbar |
40 |
– |
#from printcolor import printc |
41 |
– |
|
42 |
– |
|
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() |
46 |
– |
#config.read('./config7TeV_ZZ') |
39 |
|
config.read(opts.config) |
40 |
|
anaTag = config.get("Analysis","tag") |
41 |
|
|
44 |
|
samplesinfo=config.get('Directories','samplesinfo') |
45 |
|
|
46 |
|
#systematics |
55 |
– |
|
56 |
– |
|
47 |
|
INpath = config.get('Directories','MVAin') |
48 |
|
OUTpath = config.get('Directories','MVAout') |
49 |
|
|
50 |
< |
info = parse_info(samplesinfo,INpath) |
50 |
> |
info = ParseInfo(samplesinfo,INpath) |
51 |
|
|
62 |
– |
#infofile = open(samplesinfo,'r') |
63 |
– |
#info = pickle.load(infofile) |
64 |
– |
#infofile.close() |
52 |
|
arglist=opts.discr #RTight_blavla,bsbsb |
53 |
|
|
54 |
|
namelistIN=opts.names |
58 |
|
|
59 |
|
MVAlist=arglist.split(',') |
60 |
|
|
74 |
– |
|
61 |
|
#CONFIG |
62 |
|
#factory |
63 |
|
factoryname=config.get('factory','factoryname') |
79 |
|
workdir=ROOT.gDirectory.GetPath() |
80 |
|
|
81 |
|
|
96 |
– |
#Apply samples |
97 |
– |
#infofile = open(samplesinfo,'r') |
98 |
– |
#Ainfo = pickle.load(infofile) |
99 |
– |
#infofile.close() |
100 |
– |
|
101 |
– |
|
102 |
– |
class MvaEvaluater: |
103 |
– |
def __init__(self, config, MVAinfo): |
104 |
– |
self.varset = MVAinfo.varset |
105 |
– |
#Define reader |
106 |
– |
self.reader = ROOT.TMVA.Reader("!Color:!Silent") |
107 |
– |
MVAdir=config.get('Directories','vhbbpath') |
108 |
– |
self.systematics=config.get('systematics','systematics').split(' ') |
109 |
– |
self.MVA_Vars={} |
110 |
– |
self.MVAname = MVAinfo.MVAname |
111 |
– |
for systematic in self.systematics: |
112 |
– |
self.MVA_Vars[systematic]=config.get(self.varset,systematic) |
113 |
– |
self.MVA_Vars[systematic]=self.MVA_Vars[systematic].split(' ') |
114 |
– |
#define variables and specatators |
115 |
– |
self.MVA_var_buffer = [] |
116 |
– |
for i in range(len( self.MVA_Vars['Nominal'])): |
117 |
– |
self.MVA_var_buffer.append(array( 'f', [ 0 ] )) |
118 |
– |
self.reader.AddVariable( self.MVA_Vars['Nominal'][i],self.MVA_var_buffer[i]) |
119 |
– |
self.reader.BookMVA(MVAinfo.MVAname,MVAdir+'/data/'+MVAinfo.getweightfile()) |
120 |
– |
#--> Now the MVA is booked |
121 |
– |
|
122 |
– |
def setBranches(self,tree,job): |
123 |
– |
#Set formulas for all vars |
124 |
– |
self.MVA_formulas={} |
125 |
– |
for systematic in self.systematics: |
126 |
– |
if job.type == 'DATA' and not systematic == 'Nominal': continue |
127 |
– |
self.MVA_formulas[systematic]=[] |
128 |
– |
for j in range(len( self.MVA_Vars['Nominal'])): |
129 |
– |
self.MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),self.MVA_Vars[systematic][j],tree)) |
130 |
– |
|
131 |
– |
def evaluate(self,MVAbranches,job): |
132 |
– |
#Evaluate all vars and fill the branches |
133 |
– |
for systematic in self.systematics: |
134 |
– |
for j in range(len( self.MVA_Vars['Nominal'])): |
135 |
– |
if job.type == 'DATA' and not systematic == 'Nominal': continue |
136 |
– |
self.MVA_var_buffer[j][0] = self.MVA_formulas[systematic][j].EvalInstance() |
137 |
– |
MVAbranches[self.systematics.index(systematic)] = self.reader.EvaluateMVA(self.MVAname) |
138 |
– |
|
82 |
|
|
83 |
|
theMVAs = [] |
84 |
|
for mva in MVAinfos: |
85 |
< |
theMVAs.append(MvaEvaluater(config,mva)) |
85 |
> |
theMVAs.append(MvaEvaluator(config,mva)) |
86 |
|
|
87 |
|
|
88 |
|
#eval |
146 |
– |
for job in info: |
147 |
– |
if eval(job.active): |
148 |
– |
if job.name in namelist: |
149 |
– |
#get trees: |
150 |
– |
print INpath+'/'+job.prefix+job.identifier+'.root' |
151 |
– |
input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read') |
152 |
– |
print OUTpath+'/'+job.prefix+job.identifier+'.root' |
153 |
– |
outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate') |
154 |
– |
input.cd() |
155 |
– |
obj = ROOT.TObject |
156 |
– |
for key in ROOT.gDirectory.GetListOfKeys(): |
157 |
– |
input.cd() |
158 |
– |
obj = key.ReadObj() |
159 |
– |
#print obj.GetName() |
160 |
– |
if obj.GetName() == job.tree: |
161 |
– |
continue |
162 |
– |
outfile.cd() |
163 |
– |
#print key.GetName() |
164 |
– |
obj.Write(key.GetName()) |
165 |
– |
tree = input.Get(job.tree) |
166 |
– |
nEntries = tree.GetEntries() |
167 |
– |
outfile.cd() |
168 |
– |
newtree = tree.CloneTree(0) |
169 |
– |
|
89 |
|
|
90 |
< |
#Set branch adress for all vars |
91 |
< |
for i in range(0,len(theMVAs)): |
92 |
< |
theMVAs[i].setBranches(tree,job) |
93 |
< |
outfile.cd() |
94 |
< |
#Setup Branches |
95 |
< |
MVAbranches=[] |
96 |
< |
for i in range(0,len(theMVAs)): |
97 |
< |
if job.type == 'Data': |
98 |
< |
MVAbranches.append(array('f',[0])) |
99 |
< |
newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F') |
100 |
< |
else: |
101 |
< |
MVAbranches.append(array('f',[0]*11)) |
102 |
< |
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') |
103 |
< |
MVA_formulas_Nominal = [] |
104 |
< |
print '\n--> ' + job.name +':' |
105 |
< |
#progbar setup |
106 |
< |
if nEntries >= longe: |
107 |
< |
step=int(nEntries/longe) |
108 |
< |
long=longe |
109 |
< |
else: |
110 |
< |
long=nEntries |
111 |
< |
step = 1 |
112 |
< |
bar=progbar(long) |
113 |
< |
#Fill event by event: |
114 |
< |
for entry in range(0,nEntries): |
115 |
< |
if entry % step == 0: |
116 |
< |
bar.move() |
117 |
< |
#load entry |
118 |
< |
tree.GetEntry(entry) |
90 |
> |
samples = info.get_samples(namelist) |
91 |
> |
print(samples) |
92 |
> |
tmpDir = os.environ["TMPDIR"] |
93 |
> |
for job in samples: |
94 |
> |
#get trees: |
95 |
> |
print(INpath+'/'+job.prefix+job.identifier+'.root') |
96 |
> |
input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read') |
97 |
> |
print(OUTpath+'/'+job.prefix+job.identifier+'.root') |
98 |
> |
outfile = ROOT.TFile.Open(tmpDir+'/'+job.prefix+job.identifier+'.root','recreate') |
99 |
> |
input.cd() |
100 |
> |
obj = ROOT.TObject |
101 |
> |
for key in ROOT.gDirectory.GetListOfKeys(): |
102 |
> |
input.cd() |
103 |
> |
obj = key.ReadObj() |
104 |
> |
#print obj.GetName() |
105 |
> |
if obj.GetName() == job.tree: |
106 |
> |
continue |
107 |
> |
outfile.cd() |
108 |
> |
#print key.GetName() |
109 |
> |
obj.Write(key.GetName()) |
110 |
> |
tree = input.Get(job.tree) |
111 |
> |
nEntries = tree.GetEntries() |
112 |
> |
outfile.cd() |
113 |
> |
newtree = tree.CloneTree(0) |
114 |
> |
|
115 |
> |
#Set branch adress for all vars |
116 |
> |
for i in range(0,len(theMVAs)): |
117 |
> |
theMVAs[i].setVariables(tree,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 |
> |
tree.GetEntry(entry) |
133 |
|
|
134 |
< |
for i in range(0,len(theMVAs)): |
135 |
< |
theMVAs[i].evaluate(MVAbranches[i],job) |
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 |
> |
targetStorage = OUTpath.replace('gsidcap://t3se01.psi.ch:22128/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')+'/'+job.prefix+job.identifier+'.root' |
141 |
> |
command = 'lcg-del -b -D srmv2 -l %s' %(targetStorage) |
142 |
> |
print(command) |
143 |
> |
subprocess.call([command], shell=True) |
144 |
> |
command = 'lcg-cp -b -D srmv2 file:///%s %s' %(tmpDir+'/'+job.prefix+job.identifier+'.root',targetStorage) |
145 |
> |
print(command) |
146 |
> |
subprocess.call([command], shell=True) |
147 |
|
|
148 |
< |
print '\n' |
209 |
< |
|
210 |
< |
#Update Info: |
211 |
< |
#if doinfo: |
212 |
< |
# for job in Ainfo: |
213 |
< |
# for MVAinfo in MVAinfos: |
214 |
< |
# job.addcomment('Added MVA %s'%MVAinfo.MVAname) |
215 |
< |
# job.addpath(MVAdir) |
216 |
< |
# infofile = open(samplesinfo,'w') |
217 |
< |
# pickle.dump(Ainfo,infofile) |
218 |
< |
# infofile.close() |
148 |
> |
print('\n') |