ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/nowaf/StudyXClean/rootScripts/MakeStudyCompare.py
Revision: 1.1
Committed: Thu Feb 25 06:29:24 2010 UTC (15 years, 2 months ago) by nowak
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Log Message:
more stuff

File Contents

# Content
1 import ROOT
2 import ConfigParser, re
3 import sys
4 sys.path.append( '/afs/naf.desy.de/user/n/nowaf/CMSSW_3_3_6/src/LeptonSel/LeptonSel/rootScripts' )
5 import definitions as Def
6 from math import sqrt
7
8 def main(argv):
9 #import sys
10
11 basepath = ''
12
13 try:
14 options, arguments = getopt.getopt(argv, "hp:", ["help","path="])
15
16 except getopt.GetoptError:
17 usage()
18 sys.exit(2)
19
20 for opt, arg in options:
21 if opt in ("-h", "--help"):
22 usage()
23 sys.exit(2)
24 elif opt in ("-p", "--path"):
25 basepath = arg
26
27 if basepath == '':
28 print "Error! Need path to .cfg file to continue."
29 sys.exit(2)
30
31 return basepath
32
33 def usage():
34
35 print "Script to plot all XC study plots for the di lepton analysis"
36 print "Options and arguments:"
37 print "-h, --help\t\t: print this help message and exit"
38 print "-p, --path\t\t: relative path to the .cfg file"
39
40 pass
41
42
43
44 def readTheConfig(name):
45 """
46 read in the config file
47 """
48 config = ConfigParser.ConfigParser()
49 config.read( name )
50
51 return config
52
53 def getStudiedCut( name ):
54 """
55 gets the studied cut out of the section name
56 """
57 studied = ""
58 if re.search( "EJ", name ) or re.search( "PJ", name ):
59 studied = "cut on E_{shared}/E_{Jet}"
60 if re.search( "dR", name ):
61 studied = "max #delta R (non iso elec, jet)"
62 elif re.search( "MJ", name ):
63 studied = "#delta R (muon, jet)"
64
65 return studied
66
67 def MakeDivision( h_1, h_2, option, name ):
68 """
69 divides two histograms, computes errors
70 and returns the result
71 """
72 #h_1.Sumw2()
73 #h_2.Sumw2()
74 hist = h_2.Clone()
75 hist.Divide( h_1, h_2, 1., 1., option )
76 hist.SetName( name )
77
78 return hist
79
80 def Fill( hist, option, weight, section ):
81 """
82 h.SetBinContent sets the content right, but has only
83 one wheighted entry then. This gives problems in dividing the
84 histograms. This fill function takes care of that
85 """
86
87 #print "hist type: ", type( hist )
88
89 ### how to set a bin declaration from a float to a
90 ### description like "no xclean"?
91 if re.search( "EJ_dR", section ):
92 if float( option ) == -1:
93 option = 0.1
94 else:
95 if float( option ) == -1:
96 option = 0
97
98 for i in range( 0, weight ):
99 hist.Fill( float( option ) )
100
101 return hist
102
103 def getCase( section ):
104 """
105 strips the section name off the 'studied cut'
106 """
107 case = section
108 if re.search( "EJ_dR", section ):
109 case = section.strip( "EJ_dR_" )
110 elif re.search( "EJ_", section ):
111 case = section.strip( "EJ_" )
112 elif re.search( "MJ_", section ):
113 case = section.strip( "MJ_" )
114
115 return case
116
117
118 if __name__ == "__main__":
119
120 import getopt
121
122 basepath = main(sys.argv[1:])
123
124
125 Def.SetGlobalStyles( "emr" )
126
127 saves = {}
128
129 config = readTheConfig( basepath )
130
131 hist_list_mu_eff = []
132 hist_list_mu_cont = []
133 hist_list_el_eff = []
134 hist_list_el_cont = []
135 hist_list_ph_eff = []
136 hist_list_ph_cont = []
137 hist_list_je_eff = []
138 hist_list_je_cont = []
139 case_list = []
140 #hist_list_all = []
141
142 hists = {}
143 section_list = []
144 order = {}
145
146 color_list = [1,6,8,50,24,59]
147
148 studied_cut = ''
149
150 fout = ROOT.TFile.Open( 'studyCompare.root', "RECREATE" )
151
152 for section in config.sections():
153
154 print "section: ", section
155
156 section_list.append( section )
157 studied_cut = getStudiedCut( section )
158 case = getCase( section )
159 case_list.append( case )
160
161
162 nbin = 10
163 min = -0.05
164 max = 0.95
165 if( re.search( "EJ_dR", section ) ):
166 min = 0.05
167 max = 1.05
168 pass
169
170 h_mu_m = ROOT.TH1F( "h_mu_m" + section , "h_mu_m" ,nbin,min,max )
171 h_mu_gen = ROOT.TH1F( "h_mu_gen" + section , "h_mu_gen" ,nbin,min,max )
172 h_mu_nm = ROOT.TH1F( "h_mu_nm" + section , "h_mu_nm" ,nbin,min,max )
173 h_mu_all = ROOT.TH1F( "h_mu_all" + section , "h_mu_all" ,nbin,min,max )
174 h_el_m = ROOT.TH1F( "h_el_m" + section , "h_el_m" ,nbin,min,max )
175 h_el_gen = ROOT.TH1F( "h_el_gen" + section , "h_el_gen" ,nbin,min,max )
176 h_el_nm = ROOT.TH1F( "h_el_nm" + section , "h_el_nm" ,nbin,min,max )
177 h_el_all = ROOT.TH1F( "h_el_all" + section , "h_el_all" ,nbin,min,max )
178 h_ph_m = ROOT.TH1F( "h_ph_m" + section , "h_ph_m" ,nbin,min,max )
179 h_ph_gen = ROOT.TH1F( "h_ph_gen" + section , "h_ph_gen" ,nbin,min,max )
180 h_ph_nm = ROOT.TH1F( "h_ph_nm" + section , "h_ph_nm" ,nbin,min,max )
181 h_ph_all = ROOT.TH1F( "h_ph_all" + section , "h_ph_all" ,nbin,min,max )
182 h_je_m = ROOT.TH1F( "h_je_m" + section , "h_je_m" ,nbin,min,max )
183 h_je_gen = ROOT.TH1F( "h_je_gen" + section , "h_je_gen" ,nbin,min,max )
184 h_je_nm = ROOT.TH1F( "h_je_nm" + section , "h_je_nm" ,nbin,min,max )
185 h_je_all = ROOT.TH1F( "h_je_all" + section , "h_je_all" ,nbin,min,max )
186
187 #print "h_mu_gen type: ", type( h_mu_gen )
188
189 h_mu_m.Sumw2()
190 h_mu_gen.Sumw2()
191 h_mu_nm.Sumw2()
192 h_mu_all.Sumw2()
193 h_el_m.Sumw2()
194 h_el_gen.Sumw2()
195 h_el_nm.Sumw2()
196 h_el_all.Sumw2()
197 h_ph_m.Sumw2()
198 h_ph_gen.Sumw2()
199 h_ph_nm.Sumw2()
200 h_ph_all.Sumw2()
201 h_je_m.Sumw2()
202 h_je_gen.Sumw2()
203 h_je_nm.Sumw2()
204 h_je_all.Sumw2()
205
206
207 #print type( h_mu_eff)
208
209 for option in config.options( section ):
210
211 if option == 'number':
212 ## order{ 1 } = LM0, order{ 2 } = LM4, ...
213 order[ float( config.get( section, option ) ) ] = case
214 else:
215
216 print "option: ", option
217 #print type( h_mu_m )
218
219 if re.search( "afs", config.get( section, option ) ) or re.search( "current", config.get( section, option ) ):
220 fin = Def.ReadInRootFile( config.get( section, option ) + "/studyXC.root" )
221 else:
222 fin = Def.ReadInRootFile( "./../test/" + config.get( section, option ) + "/studyXC.root" )
223
224 Tree_ = fin.Get( 'StudyXC' )
225 mu_all = Def.defineArray( 'i', 1 )
226 mu_gen = Def.defineArray( 'i', 1 )
227 mu_m = Def.defineArray( 'i', 1 )
228 mu_nm = Def.defineArray( 'i', 1 )
229 el_all = Def.defineArray( 'i', 1 )
230 el_gen = Def.defineArray( 'i', 1 )
231 el_m = Def.defineArray( 'i', 1 )
232 el_nm = Def.defineArray( 'i', 1 )
233 ph_all = Def.defineArray( 'i', 1 )
234 ph_gen = Def.defineArray( 'i', 1 )
235 ph_m = Def.defineArray( 'i', 1 )
236 ph_nm = Def.defineArray( 'i', 1 )
237 je_all = Def.defineArray( 'i', 1 )
238 je_gen = Def.defineArray( 'i', 1 )
239 je_m = Def.defineArray( 'i', 1 )
240 je_nm = Def.defineArray( 'i', 1 )
241
242 Tree_.SetBranchAddress( "MuonsAll", mu_all )
243 Tree_.SetBranchAddress( "MuonsGen", mu_gen )
244 Tree_.SetBranchAddress( "MuonsNotMatched", mu_nm )
245 Tree_.SetBranchAddress( "MuonsMatched", mu_m )
246 Tree_.SetBranchAddress( "ElectronsAll", el_all )
247 Tree_.SetBranchAddress( "ElectronsGen", el_gen )
248 Tree_.SetBranchAddress( "ElectronsNotMatched", el_nm )
249 Tree_.SetBranchAddress( "ElectronsMatched", el_m )
250 Tree_.SetBranchAddress( "PhotonsAll", ph_all )
251 Tree_.SetBranchAddress( "PhotonsGen", ph_gen )
252 Tree_.SetBranchAddress( "PhotonsNotMatched", ph_nm )
253 Tree_.SetBranchAddress( "PhotonsMatched", ph_m )
254 Tree_.SetBranchAddress( "JetsAll", je_all )
255 Tree_.SetBranchAddress( "JetsGen", je_gen )
256 Tree_.SetBranchAddress( "JetsNotMatched", je_nm )
257 Tree_.SetBranchAddress( "JetsMatched", je_m )
258
259 n_mu_all = 0
260 n_mu_gen = 0
261 n_mu_m = 0
262 n_mu_nm = 0
263 n_el_all = 0
264 n_el_gen = 0
265 n_el_m = 0
266 n_el_nm = 0
267 n_ph_all = 0
268 n_ph_gen = 0
269 n_ph_m = 0
270 n_ph_nm = 0
271 n_je_all = 0
272 n_je_gen = 0
273 n_je_m = 0
274 n_je_nm = 0
275
276 n_bytes = 0
277 for ent in range( 0, Tree_.GetEntries() ):
278
279 n_bytes += Tree_.GetEntry( ent )
280 n_mu_all += mu_all[0]
281 n_mu_gen += mu_gen[0]
282 n_mu_m += mu_m[0]
283 n_mu_nm += mu_nm[0]
284 n_el_all += el_all[0]
285 n_el_gen += el_gen[0]
286 n_el_m += el_m[0]
287 n_el_nm += el_nm[0]
288 n_ph_all += ph_all[0]
289 n_ph_gen += ph_gen[0]
290 n_ph_m += ph_m[0]
291 n_ph_nm += ph_nm[0]
292 n_je_all += je_all[0]
293 n_je_gen += je_gen[0]
294 n_je_m += je_m[0]
295 n_je_nm += je_nm[0]
296 pass
297
298 Fill( h_mu_m, option, n_mu_m, section )
299 Fill( h_mu_gen, option, n_mu_gen, section )
300 Fill( h_mu_nm, option, n_mu_nm, section )
301 Fill( h_mu_all, option, n_mu_all, section )
302 Fill( h_el_m, option, n_el_m, section )
303 Fill( h_el_gen, option, n_el_gen, section )
304 Fill( h_el_nm, option, n_el_nm, section )
305 Fill( h_el_all, option, n_el_all, section )
306 Fill( h_ph_m, option, n_ph_m, section )
307 Fill( h_ph_gen, option, n_ph_gen, section )
308 Fill( h_ph_nm, option, n_ph_nm, section )
309 Fill( h_ph_all, option, n_ph_all, section )
310 Fill( h_je_m, option, n_je_m, section )
311 Fill( h_je_gen, option, n_je_gen, section )
312 Fill( h_je_nm, option, n_je_nm, section )
313 Fill( h_je_all, option, n_je_all, section )
314
315 pass
316
317 h_mu_eff = MakeDivision( h_mu_m, h_mu_gen, "B", case + "_mu_eff" )
318 h_mu_cont = MakeDivision( h_mu_nm, h_mu_all, "B", case + "_mu_cont" )
319 h_el_eff = MakeDivision( h_el_m, h_el_gen, "B", case + "_el_eff" )
320 h_el_cont = MakeDivision( h_el_nm, h_el_all, "B", case + "_el_cont" )
321 h_ph_eff = MakeDivision( h_ph_m, h_ph_gen, "B", case + "_ph_eff" )
322 h_ph_cont = MakeDivision( h_ph_nm, h_ph_all, "B", case + "_ph_cont" )
323 h_je_eff = MakeDivision( h_je_m, h_je_gen, "B", case + "_je_eff" )
324 h_je_cont = MakeDivision( h_je_nm, h_je_all, "B", case + "_je_cont" )
325
326
327 fout.cd()
328
329 h_mu_eff.Write()
330 h_mu_cont.Write()
331 h_el_eff.Write()
332 h_el_cont.Write()
333 h_ph_eff.Write()
334 h_ph_cont.Write()
335 h_je_eff.Write()
336 h_je_cont.Write()
337
338
339 #del fin
340
341 pass
342 pass
343
344 #for section in section_list:
345 # fout.cd()
346 # hist_list_mu_eff.append( fout.Get( section + "_mu_eff" ) )
347 # hist_list_mu_cont.append( fout.Get( section + "_mu_cont" ) )
348 # hist_list_el_eff.append( fout.Get( section + "_el_eff" ) )
349 # hist_list_el_cont.append( fout.Get( section + "_el_cont" ) )
350 # hist_list_ph_eff.append( fout.Get( section + "_ph_eff" ) )
351 # hist_list_ph_cont.append( fout.Get( section + "_ph_cont" ) )
352 # hist_list_je_eff.append( fout.Get( section + "_je_eff" ) )
353 # hist_list_je_cont.append( fout.Get( section + "_je_cont" ) )
354
355 fout.cd()
356 case_list_ordered = []
357 for i in range( 1, len( case_list ) + 1 ):
358 hist_list_mu_eff.append( fout.Get( order[i] + "_mu_eff" ) )
359 hist_list_mu_cont.append( fout.Get( order[i] + "_mu_cont" ) )
360 hist_list_el_eff.append( fout.Get( order[i] + "_el_eff" ) )
361 hist_list_el_cont.append( fout.Get( order[i] + "_el_cont" ) )
362 hist_list_ph_eff.append( fout.Get( order[i] + "_ph_eff" ) )
363 hist_list_ph_cont.append( fout.Get( order[i] + "_ph_cont" ) )
364 hist_list_je_eff.append( fout.Get( order[i] + "_je_eff" ) )
365 hist_list_je_cont.append( fout.Get( order[i] + "_je_cont" ) )
366 case_list_ordered.append( order[i] )
367
368
369
370
371 ## draw the hists
372 c_mu = ROOT.TCanvas( "c_mu", "Muon" ,1,71,300,600 )
373 c_mu.Divide(1,2)
374 c_mu.cd(1)
375 Def.DrawSmall( c_mu, \
376 hist_list_mu_eff, color_list, case_list_ordered, \
377 studied_cut, "efficiency", "", \
378 False, 0, False, "ur", "muons", saves)
379
380 c_mu.cd(2)
381 Def.DrawSmall( c_mu, \
382 hist_list_mu_cont, color_list, case_list_ordered, \
383 studied_cut, "contamination", "", \
384 False, 0, False, "ur", "muons_cont", saves)
385
386 c_el = ROOT.TCanvas( "c_el", "Electron" ,1,71,300,600 )
387 c_el.Divide(1,2)
388 c_el.cd(1)
389 Def.DrawSmall( c_el, \
390 hist_list_el_eff, color_list, case_list_ordered, \
391 studied_cut, "efficiency", "", \
392 False, 0, False, "ur", "elecs", saves)
393
394 c_el.cd(2)
395 Def.DrawSmall( c_el, \
396 hist_list_el_cont, color_list, case_list_ordered, \
397 studied_cut, "contamination", "", \
398 False, 0, False, "ur", "elec_cont", saves)
399
400
401 c_ph = ROOT.TCanvas( "c_ph", "Photon" ,1,71,300,600 )
402 c_ph.Divide(1,2)
403 c_ph.cd(1)
404 Def.DrawSmall( c_ph, \
405 hist_list_ph_eff, color_list, case_list_ordered, \
406 studied_cut, "efficiency", "", \
407 False, 0, False, "ur", "phots", saves)
408
409 c_ph.cd(2)
410 Def.DrawSmall( c_ph, \
411 hist_list_ph_cont, color_list, case_list_ordered, \
412 studied_cut, "contamination", "", \
413 False, 0, False, "ur", "phots_cont", saves)
414
415 c_je = ROOT.TCanvas( "c_je", "Jet" ,1,71,300,600 )
416 c_je.Divide(1,2)
417 c_je.cd(1)
418 Def.DrawSmall( c_je, \
419 hist_list_je_eff, color_list, case_list_ordered, \
420 studied_cut, "efficiency", "", \
421 False, 0, False, "ur", "jets", saves)
422
423 c_je.cd(2)
424 Def.DrawSmall( c_je, \
425 hist_list_je_cont, color_list, case_list_ordered, \
426 studied_cut, "contamination", "", \
427 False, 0, False, "ur", "jets_cont", saves)
428
429
430
431
432 #fout.Close()
433 Def.DontQuit()