4 __author__ =
'Radist Morse radist.morse@gmail.com'
18 inputfile=
open(filename)
21 for line
in inputfile :
22 line = line.expandtabs(1)
23 line = line.rstrip(
"\n")
24 line = line.split(
"#")[0]
26 line[:] = [x
for x
in line
if (len(x) > 0) ]
30 if line[0] ==
"file" :
32 rf.color =
int(line[2])
33 rf.thickness =
int(line[3])
34 rf.markerstyle =
int(line[4])
35 if (rf.markerstyle > 1) :
37 rf.markersize =
float(line[5])
38 rf.legendname =
" ".
join(line[6:])
39 parsedRoots.append(rf)
40 print (
"Found rootfile '"+rf.filename+
"' added as '"+rf.legendname+
"'")
41 if line[0] ==
"restrict" :
44 if line[1].startswith(
"+") :
48 for rang
in line[2:] :
53 if (len(parsedRoots) > 0) :
54 rstrict = parsedRoots[-1].restricts
56 rstrict = parsedRestricts
58 if (rech.var == re.var) :
59 rech.rangeList += re.rangeList
61 if (len(re.rangeList) > 0) :
62 print (
"A restriction found for variable",
str(re))
65 print (
"WARNING: unknown key:", line[0])
67 for pr
in parsedRoots :
68 for re
in parsedRestricts :
69 for rech
in pr.restricts :
70 if (rech.var == re.var) :
71 rech.rangeList += re.rangeList
73 if (len(re.rangeList) > 0) :
74 pr.restricts.append(re)
79 colors = [2,4,7,6,3,1]
80 thickness = [1,1,1,1,1,1]
81 markerstyle = [20,1,0,1,1,1]
82 markersize = [0.6,1,1,1,1,1]
83 for fn,col,thick,mark1,mark2,legname
in zip(args,colors,thickness,markerstyle,markersize,args) :
88 rf.markerstyle = mark1
89 if (rf.markerstyle > 1) :
92 rf.legendname = legname
115 if (value >= rang[0])
and (value <= rang[1]) :
119 outstr = self.
var+
" :"
121 outstr+=
" ["+
str(rang[0])+
".."+
str(rang[1])+
"]"
124 parsed = rang.split(
"..")
126 parsed[0] =
float(parsed[0])
128 parsed[0] = -999999.9
130 parsed[1] =
float(parsed[1])
132 parsed[1] = 9999999.9
135 parsed = rang.split(
"..")
137 parsed[0] =
float(parsed[0])
141 parsed[1] =
float(parsed[1])
143 parsed[1] = 9999999.9
144 if (parsed[0] < 0)
or (parsed[1] <= 0) :
145 print (
"WARNING: The absolute range is not valid: less then zero. Ignore.")
147 rang =
str(parsed[0])+
".."+
str(parsed[1])
148 rang2 =
str(-parsed[1])+
".."+
str(-parsed[0])
153 inputfile=
open(filename)
156 for line
in inputfile :
157 line = line.expandtabs(1)
158 line = line.rstrip(
"\n")
159 line = line.split(
"#")[0]
160 line = line.split(
" ")
161 line[:] = [x
for x
in line
if (len(x) > 0) ]
165 if line[0] ==
"hist" :
166 pe.vars_to_draw.append(line[1])
167 pe.givenmin =
float(line[2])
168 pe.givenmax =
float(line[3])
169 pe.nbins =
int(line[4])
170 pe.logy =
int(line[5])
171 pe.display_name =
" ".
join(line[6:])
172 for var
in pe.vars_to_draw :
173 varcap = varCaption(var.lstrip(
"+"))
175 varcap = var.lstrip(
"+")
176 pe.axis_captions[var.lstrip(
"+")]=varcap
177 if (pe.vars_to_draw[0].startswith(
"+"))
and ((pe.givenmin < 0)
or (pe.givenmax < 0)) :
178 print (
"WARNING: Boundaries are less the zero, while the variable is absolute. Ignore.")
181 print (
"Found 1D histogram:",pe.vars_to_draw[0],[pe.givenmin,pe.givenmax])
182 parsedPlots.append(pe)
183 elif line[0] ==
"prof" :
184 pe.vars_to_draw.extend(line[1:3])
185 pe.givenmin =
float(line[3])
186 pe.givenmax =
float(line[4])
187 pe.nbins =
int(line[5])
188 pe.logy =
int(line[6])
189 pe.display_name =
" ".
join(line[7:])
191 for var
in pe.vars_to_draw :
192 varcap = varCaption(var.lstrip(
"+"))
194 varcap = var.lstrip(
"+")
195 pe.axis_captions[var.lstrip(
"+")]=varcap
196 if (pe.vars_to_draw[0].startswith(
"+"))
and ((pe.givenmin < 0)
or (pe.givenmax < 0)) :
197 print (
"WARNING: Boundaries are less the zero, while the variable is absolute. Ignore.")
200 print (
"Found 1D profile:",pe.vars_to_draw[1],
"vs",pe.vars_to_draw[0],[pe.givenmin,pe.givenmax])
201 parsedPlots.append(pe)
202 elif line[0] ==
"hist2d" :
204 pe.vars_to_draw.extend(line[1:3])
207 pe.nbins = [
int(line[3]),
int(line[4])]
209 pe.display_name =
" ".
join(line[5:])
211 for var
in pe.vars_to_draw :
212 varcap = varCaption(var.lstrip(
"+"))
214 varcap = var.lstrip(
"+")
215 pe.axis_captions[var.lstrip(
"+")]=varcap
216 print (
"Found 2D histogram:",pe.vars_to_draw)
217 parsedPlots.append(pe)
218 elif line[0] ==
"prof2d" :
219 pe.vars_to_draw.extend(line[1:4])
222 pe.nbins = [
int(line[4]),
int(line[5])]
224 pe.display_name =
" ".
join(line[6:])
227 for var
in pe.vars_to_draw :
228 varcap = varCaption(var.lstrip(
"+"))
230 varcap = var.lstrip(
"+")
231 pe.axis_captions[var.lstrip(
"+")]=varcap
232 print (
"Found 2D profile:",pe.vars_to_draw[2],
"vs",pe.vars_to_draw[0:2])
233 parsedPlots.append(pe)
234 elif line[0] ==
"restrict" :
237 if line[1].startswith(
"+") :
239 line[1] = line[1][1:]
241 for rang
in line[2:] :
246 if (len(parsedPlots) > 0) :
247 rstrict = parsedPlots[-1].restricts
249 rstrict = parsedRestricts
250 for rech
in rstrict :
251 if (rech.var == re.var) :
252 rech.rangeList += re.rangeList
254 if (len(re.rangeList) > 0) :
255 print (
"A restriction found for variable",
str(re))
257 elif line[0] ==
"axisname" :
258 if (len(parsedPlots) == 0) :
259 print (
"WARNING: axisname shouldn't be before plots")
261 parsedPlots[-1].axis_captions[line[1].lstrip(
"+")] =
" ".
join(line[2:]).
replace(
"%",
"#")
263 print (
"WARNING: unknown key:", line[0])
265 for pe
in parsedPlots :
266 for re
in parsedRestricts :
267 for rech
in pe.restricts :
268 if (rech.var == re.var) :
269 rech.rangeList += re.rangeList
271 if (len(re.rangeList) > 0) :
272 pe.restricts.append(re)
276 from ROOT
import TH1D, TProfile,TH2D,TProfile2D
279 for plotopt
in plotopts :
281 for rootopt
in rootopts :
282 if (plotopt.profile) :
284 plot = TProfile2D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins[0], 0,0,plotopt.nbins[1], 0,0)
289 plot = TH2D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins[0], 0,0,plotopt.nbins[1], 0,0)
291 plot = TH1D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins, 0,0)
292 plot.SetBuffer(1000000)
293 plots[plotopt][rootopt] = plot
301 for plotopt
in plotopts :
302 listmin[plotopt] = 99999.9
303 listmax[plotopt] = -99999.9
304 listmin2[plotopt] = 99999.9
305 listmax2[plotopt] = -99999.9
306 for rootopt
in rootopts :
307 for event
in rootopt.tree :
308 for plotopt
in plotopts :
311 if (len(plotopt.restricts) > 0) :
312 for rest
in plotopt.restricts :
313 eventval = eventVal(event,rest.var)
314 if (eventval ==
"False") :
316 eventval = event.__getattr__(rest.var)
317 except AttributeError:
318 print (
"ERROR: Non-existent variable in plot restrict:", rest.var)
321 if not rest.checkVar(eventval) :
324 if (len(rootopt.restricts) > 0) :
325 for rest
in rootopt.restricts :
326 eventval = eventVal(event,rest.var)
327 if (eventval ==
"False") :
329 eventval = event.__getattr__(rest.var)
331 print (
"ERROR: Non-existent variable in rootfile restrict:", rest.var)
334 if not rest.checkVar(eventval) :
339 plot = plots[plotopt][rootopt]
340 plotvars = plotopt.vars_to_draw
343 for plotvar
in plotvars :
346 if plotvar.startswith(
"+") :
347 locvar = plotvar.lstrip(
"+")
349 eventval = eventVal(event,locvar)
350 if (eventval ==
"False") :
352 eventval = event.__getattr__(locvar)
353 except AttributeError:
354 print (
"ERROR: Non-existent variable:", locvar)
359 eventvals.append(eventval)
360 if (plotopt.profile) :
362 plot.Fill(eventvals[0],eventvals[1],eventvals[2])
364 plot.Fill(eventvals[0],eventvals[1])
367 plot.Fill(eventvals[0],eventvals[1])
369 plot.Fill(eventvals[0])
371 if (eventvals[0] < plotopt.givenmax)
or (plotopt.givenmin == plotopt.givenmax) :
372 listmax[plotopt] =
max(listmax[plotopt], eventvals[0])
373 if (eventvals[0] > plotopt.givenmin)
or (plotopt.givenmin == plotopt.givenmax) :
374 listmin[plotopt] =
min(listmin[plotopt], eventvals[0])
376 if (eventvals[1] < plotopt.givenmax)
or (plotopt.givenmin == plotopt.givenmax) :
377 listmax2[plotopt] =
max(listmax2[plotopt], eventvals[1])
378 if (eventvals[1] > plotopt.givenmin)
or (plotopt.givenmin == plotopt.givenmax) :
379 listmin2[plotopt] =
min(listmin2[plotopt], eventvals[1])
381 for plotopt
in plotopts :
385 plot = plots[plotopt][rootopt]
386 plot.SetLineColor(rootopt.color)
387 plot.SetLineWidth(rootopt.thickness)
388 if rootopt.markerstyle > 0 :
389 plot.SetMarkerStyle(rootopt.markerstyle)
391 plot.SetMarkerStyle(1)
392 plot.SetFillColor(rootopt.color)
394 plot.SetMarkerSize(rootopt.markersize)
395 plot.SetMarkerColor(rootopt.color)
397 for plotopt
in plotopts :
399 plot.GetXaxis().SetLimits(listmin[plotopt],listmax[plotopt])
401 plot.GetYaxis().SetLimits(listmin2[plotopt],listmax2[plotopt])
405 for singplot
in plots.itervalues() :
406 plot1 = singplot.pop(rootopt1)
408 for plot
in singplot.itervalues() :
414 from ROOT
import TFile
416 rootfile = TFile(output,
"RECREATE")
418 for plotopt, pls
in plots.iteritems() :
419 rootfile.cd(output+
":/")
420 rootfile.mkdir(
"_".
join(plotopt.vars_to_draw)+
"/")
421 rootfile.cd(output+
":/"+
"_".
join(plotopt.vars_to_draw)+
"/")
422 for rootopt, plot
in pls.iteritems() :
423 plot.Write(
"_".
join(plotopt.vars_to_draw))
426 def drawPlots(plots,plotopts,rootopts,output,optzero,optmean,opth,optw) :
427 from ROOT
import TPostScript, TCanvas, TLegend
428 from ROOT
import gROOT, gStyle, gPad
431 gROOT.SetStyle(
"Plain")
435 leg = TLegend(0.54,0.71,0.9,0.9)
445 if output !=
"DISPLAY" :
446 ps = TPostScript(output,111)
448 canv = TCanvas(
'c1',
"Validation Plot Viewer",600,800)
449 canv.Divide(opth, optw)
450 maxperlist = opth * optw
458 for plotopt
in plotopts :
459 print (
"Drawing",plotopt.display_name)
461 if (num > maxperlist
and output !=
"DISPLAY") :
466 gPad.SetLogy(plotopt.logy)
473 for rootopt
in rootopts :
474 entries[rootopt] = plots[plotopt][rootopt].
GetEntries()
475 if (plots[plotopt][rootopt].
GetEntries() > entryZ) :
478 for rootopt
in rootopts :
479 plot = plots[plotopt][rootopt]
480 if (plotopt.profile) :
481 print (rootopt.legendname,
"is a profile: no need to scale")
483 if not (entries[rootopt] == 0) :
484 print (
"scaling",rootopt.legendname,
"to",entryZ/entries[rootopt])
485 plot.Scale(entryZ/entries[rootopt])
487 print (rootopt.legendname,
"is an empty hist, no scale")
489 for rootopt
in rootopts :
490 if (plots[plotopt][rootopt].GetMaximum() > valuemax) :
491 valuemax = plots[plotopt][rootopt].GetMaximum()
496 for rootopt
in rootopts :
497 if (rootopt.markerstyle > 0) :
499 plot = plots[plotopt][rootopt]
500 print (
"Drawing filler from",rootopt.legendname)
502 if (plotopt.logy == 1) :
506 plot.SetMaximum(valuemax * 1.1)
507 plot.GetXaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[0].lstrip(
"+")])
508 if len(plotopt.vars_to_draw) > 1 :
509 plot.GetYaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[1].lstrip(
"+")])
511 plot.Draw(
"CONT4Z "+sameDrawOpt)
513 plot.Draw(rootopt.drawopt+sameDrawOpt)
514 sameDrawOpt =
" SAME"
516 for rootopt
in rootopts :
517 plot = plots[plotopt][rootopt]
518 prname = rootopt.legendname
519 if optmean
and (
not plotopt.profile):
520 prname +=
" (mean: "+(
"%.4f" %plot.GetMean())+
")"
521 leg.AddEntry(plot,prname,
"L")
522 if (rootopt.markerstyle == 0) :
524 print (
"Drawing plot from",rootopt.legendname)
526 if (plotopt.logy == 1) :
530 plot.SetMaximum(valuemax * 1.1)
531 plot.GetXaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[0].lstrip(
"+")])
532 if len(plotopt.vars_to_draw) > 1 :
533 plot.GetYaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[1].lstrip(
"+")])
535 plot.Draw(
"CONT4Z "+sameDrawOpt)
537 plot.Draw(rootopt.drawopt+sameDrawOpt)
538 sameDrawOpt =
" SAME"
540 legends.append(leg.Clone())
541 legends[len(legends)-1].Draw()
543 if output !=
"DISPLAY" :