3 from __future__
import print_function
5 __author__ =
'Radist Morse radist.morse@gmail.com'
19 inputfile=
open(filename)
22 for line
in inputfile :
23 line = line.expandtabs(1)
24 line = line.rstrip(
"\n")
25 line = line.split(
"#")[0]
27 line[:] = [x
for x
in line
if (len(x) > 0) ]
31 if line[0] ==
"file" :
33 rf.color =
int(line[2])
34 rf.thickness =
int(line[3])
35 rf.markerstyle =
int(line[4])
36 if (rf.markerstyle > 1) :
38 rf.markersize =
float(line[5])
39 rf.legendname =
" ".
join(line[6:])
40 parsedRoots.append(rf)
41 print (
"Found rootfile '"+rf.filename+
"' added as '"+rf.legendname+
"'")
42 if line[0] ==
"restrict" :
45 if line[1].startswith(
"+") :
49 for rang
in line[2:] :
54 if (len(parsedRoots) > 0) :
55 rstrict = parsedRoots[-1].restricts
57 rstrict = parsedRestricts
59 if (rech.var == re.var) :
60 rech.rangeList += re.rangeList
62 if (len(re.rangeList) > 0) :
63 print (
"A restriction found for variable",
str(re))
66 print (
"WARNING: unknown key:", line[0])
68 for pr
in parsedRoots :
69 for re
in parsedRestricts :
70 for rech
in pr.restricts :
71 if (rech.var == re.var) :
72 rech.rangeList += re.rangeList
74 if (len(re.rangeList) > 0) :
75 pr.restricts.append(re)
80 colors = [2,4,7,6,3,1]
81 thickness = [1,1,1,1,1,1]
82 markerstyle = [20,1,0,1,1,1]
83 markersize = [0.6,1,1,1,1,1]
84 for fn,col,thick,mark1,mark2,legname
in zip(args,colors,thickness,markerstyle,markersize,args) :
89 rf.markerstyle = mark1
90 if (rf.markerstyle > 1) :
93 rf.legendname = legname
116 if (value >= rang[0])
and (value <= rang[1]) :
120 outstr = self.
var+
" :"
122 outstr+=
" ["+
str(rang[0])+
".."+
str(rang[1])+
"]"
125 parsed = rang.split(
"..")
127 parsed[0] =
float(parsed[0])
129 parsed[0] = -999999.9
131 parsed[1] =
float(parsed[1])
133 parsed[1] = 9999999.9
136 parsed = rang.split(
"..")
138 parsed[0] =
float(parsed[0])
142 parsed[1] =
float(parsed[1])
144 parsed[1] = 9999999.9
145 if (parsed[0] < 0)
or (parsed[1] <= 0) :
146 print (
"WARNING: The absolute range is not valid: less then zero. Ignore.")
148 rang =
str(parsed[0])+
".."+
str(parsed[1])
149 rang2 =
str(-parsed[1])+
".."+
str(-parsed[0])
154 inputfile=
open(filename)
157 for line
in inputfile :
158 line = line.expandtabs(1)
159 line = line.rstrip(
"\n")
160 line = line.split(
"#")[0]
161 line = line.split(
" ")
162 line[:] = [x
for x
in line
if (len(x) > 0) ]
166 if line[0] ==
"hist" :
167 pe.vars_to_draw.append(line[1])
168 pe.givenmin =
float(line[2])
169 pe.givenmax =
float(line[3])
170 pe.nbins =
int(line[4])
171 pe.logy =
int(line[5])
172 pe.display_name =
" ".
join(line[6:])
173 for var
in pe.vars_to_draw :
174 varcap = varCaption(var.lstrip(
"+"))
176 varcap = var.lstrip(
"+")
177 pe.axis_captions[var.lstrip(
"+")]=varcap
178 if (pe.vars_to_draw[0].startswith(
"+"))
and ((pe.givenmin < 0)
or (pe.givenmax < 0)) :
179 print (
"WARNING: Boundaries are less the zero, while the variable is absolute. Ignore.")
182 print (
"Found 1D histogram:",pe.vars_to_draw[0],[pe.givenmin,pe.givenmax])
183 parsedPlots.append(pe)
184 elif line[0] ==
"prof" :
185 pe.vars_to_draw.extend(line[1:3])
186 pe.givenmin =
float(line[3])
187 pe.givenmax =
float(line[4])
188 pe.nbins =
int(line[5])
189 pe.logy =
int(line[6])
190 pe.display_name =
" ".
join(line[7:])
192 for var
in pe.vars_to_draw :
193 varcap = varCaption(var.lstrip(
"+"))
195 varcap = var.lstrip(
"+")
196 pe.axis_captions[var.lstrip(
"+")]=varcap
197 if (pe.vars_to_draw[0].startswith(
"+"))
and ((pe.givenmin < 0)
or (pe.givenmax < 0)) :
198 print (
"WARNING: Boundaries are less the zero, while the variable is absolute. Ignore.")
201 print (
"Found 1D profile:",pe.vars_to_draw[1],
"vs",pe.vars_to_draw[0],[pe.givenmin,pe.givenmax])
202 parsedPlots.append(pe)
203 elif line[0] ==
"hist2d" :
205 pe.vars_to_draw.extend(line[1:3])
208 pe.nbins = [
int(line[3]),
int(line[4])]
210 pe.display_name =
" ".
join(line[5:])
212 for var
in pe.vars_to_draw :
213 varcap = varCaption(var.lstrip(
"+"))
215 varcap = var.lstrip(
"+")
216 pe.axis_captions[var.lstrip(
"+")]=varcap
217 print (
"Found 2D histogram:",pe.vars_to_draw)
218 parsedPlots.append(pe)
219 elif line[0] ==
"prof2d" :
220 pe.vars_to_draw.extend(line[1:4])
223 pe.nbins = [
int(line[4]),
int(line[5])]
225 pe.display_name =
" ".
join(line[6:])
228 for var
in pe.vars_to_draw :
229 varcap = varCaption(var.lstrip(
"+"))
231 varcap = var.lstrip(
"+")
232 pe.axis_captions[var.lstrip(
"+")]=varcap
233 print (
"Found 2D profile:",pe.vars_to_draw[2],
"vs",pe.vars_to_draw[0:2])
234 parsedPlots.append(pe)
235 elif line[0] ==
"restrict" :
238 if line[1].startswith(
"+") :
240 line[1] = line[1][1:]
242 for rang
in line[2:] :
247 if (len(parsedPlots) > 0) :
248 rstrict = parsedPlots[-1].restricts
250 rstrict = parsedRestricts
251 for rech
in rstrict :
252 if (rech.var == re.var) :
253 rech.rangeList += re.rangeList
255 if (len(re.rangeList) > 0) :
256 print (
"A restriction found for variable",
str(re))
258 elif line[0] ==
"axisname" :
259 if (len(parsedPlots) == 0) :
260 print (
"WARNING: axisname shouldn't be before plots")
262 parsedPlots[-1].axis_captions[line[1].lstrip(
"+")] =
" ".
join(line[2:]).
replace(
"%",
"#")
264 print (
"WARNING: unknown key:", line[0])
266 for pe
in parsedPlots :
267 for re
in parsedRestricts :
268 for rech
in pe.restricts :
269 if (rech.var == re.var) :
270 rech.rangeList += re.rangeList
272 if (len(re.rangeList) > 0) :
273 pe.restricts.append(re)
277 from ROOT
import TH1D, TProfile,TH2D,TProfile2D
280 for plotopt
in plotopts :
282 for rootopt
in rootopts :
283 if (plotopt.profile) :
285 plot = TProfile2D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins[0], 0,0,plotopt.nbins[1], 0,0)
290 plot = TH2D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins[0], 0,0,plotopt.nbins[1], 0,0)
292 plot = TH1D(
str(
hash(plotopt))+
str(
hash(rootopt)),plotopt.display_name,plotopt.nbins, 0,0)
293 plot.SetBuffer(1000000)
294 plots[plotopt][rootopt] = plot
302 for plotopt
in plotopts :
303 listmin[plotopt] = 99999.9
304 listmax[plotopt] = -99999.9
305 listmin2[plotopt] = 99999.9
306 listmax2[plotopt] = -99999.9
307 for rootopt
in rootopts :
308 for event
in rootopt.tree :
309 for plotopt
in plotopts :
312 if (len(plotopt.restricts) > 0) :
313 for rest
in plotopt.restricts :
314 eventval = eventVal(event,rest.var)
315 if (eventval ==
"False") :
317 eventval = event.__getattr__(rest.var)
318 except AttributeError:
319 print (
"ERROR: Non-existent variable in plot restrict:", rest.var)
322 if not rest.checkVar(eventval) :
325 if (len(rootopt.restricts) > 0) :
326 for rest
in rootopt.restricts :
327 eventval = eventVal(event,rest.var)
328 if (eventval ==
"False") :
330 eventval = event.__getattr__(rest.var)
332 print (
"ERROR: Non-existent variable in rootfile restrict:", rest.var)
335 if not rest.checkVar(eventval) :
340 plot = plots[plotopt][rootopt]
341 plotvars = plotopt.vars_to_draw
344 for plotvar
in plotvars :
347 if plotvar.startswith(
"+") :
348 locvar = plotvar.lstrip(
"+")
350 eventval = eventVal(event,locvar)
351 if (eventval ==
"False") :
353 eventval = event.__getattr__(locvar)
354 except AttributeError:
355 print (
"ERROR: Non-existent variable:", locvar)
360 eventvals.append(eventval)
361 if (plotopt.profile) :
363 plot.Fill(eventvals[0],eventvals[1],eventvals[2])
365 plot.Fill(eventvals[0],eventvals[1])
368 plot.Fill(eventvals[0],eventvals[1])
370 plot.Fill(eventvals[0])
372 if (eventvals[0] < plotopt.givenmax)
or (plotopt.givenmin == plotopt.givenmax) :
373 listmax[plotopt] =
max(listmax[plotopt], eventvals[0])
374 if (eventvals[0] > plotopt.givenmin)
or (plotopt.givenmin == plotopt.givenmax) :
375 listmin[plotopt] =
min(listmin[plotopt], eventvals[0])
377 if (eventvals[1] < plotopt.givenmax)
or (plotopt.givenmin == plotopt.givenmax) :
378 listmax2[plotopt] =
max(listmax2[plotopt], eventvals[1])
379 if (eventvals[1] > plotopt.givenmin)
or (plotopt.givenmin == plotopt.givenmax) :
380 listmin2[plotopt] =
min(listmin2[plotopt], eventvals[1])
382 for plotopt
in plotopts :
386 plot = plots[plotopt][rootopt]
387 plot.SetLineColor(rootopt.color)
388 plot.SetLineWidth(rootopt.thickness)
389 if rootopt.markerstyle > 0 :
390 plot.SetMarkerStyle(rootopt.markerstyle)
392 plot.SetMarkerStyle(1)
393 plot.SetFillColor(rootopt.color)
395 plot.SetMarkerSize(rootopt.markersize)
396 plot.SetMarkerColor(rootopt.color)
398 for plotopt
in plotopts :
400 plot.GetXaxis().SetLimits(listmin[plotopt],listmax[plotopt])
402 plot.GetYaxis().SetLimits(listmin2[plotopt],listmax2[plotopt])
406 for singplot
in plots.itervalues() :
407 plot1 = singplot.pop(rootopt1)
409 for plot
in singplot.itervalues() :
415 from ROOT
import TFile
417 rootfile = TFile(output,
"RECREATE")
419 for plotopt, pls
in plots.iteritems() :
420 rootfile.cd(output+
":/")
421 rootfile.mkdir(
"_".
join(plotopt.vars_to_draw)+
"/")
422 rootfile.cd(output+
":/"+
"_".
join(plotopt.vars_to_draw)+
"/")
423 for rootopt, plot
in pls.iteritems() :
424 plot.Write(
"_".
join(plotopt.vars_to_draw))
427 def drawPlots(plots,plotopts,rootopts,output,optzero,optmean,opth,optw) :
428 from ROOT
import TPostScript, TCanvas, TLegend
429 from ROOT
import gROOT, gStyle, gPad
432 gROOT.SetStyle(
"Plain")
436 leg = TLegend(0.54,0.71,0.9,0.9)
446 if output !=
"DISPLAY" :
447 ps = TPostScript(output,111)
449 canv = TCanvas(
'c1',
"Validation Plot Viewer",600,800)
450 canv.Divide(opth, optw)
451 maxperlist = opth * optw
459 for plotopt
in plotopts :
460 print (
"Drawing",plotopt.display_name)
462 if (num > maxperlist
and output !=
"DISPLAY") :
467 gPad.SetLogy(plotopt.logy)
474 for rootopt
in rootopts :
475 entries[rootopt] = plots[plotopt][rootopt].
GetEntries()
476 if (plots[plotopt][rootopt].
GetEntries() > entryZ) :
479 for rootopt
in rootopts :
480 plot = plots[plotopt][rootopt]
481 if (plotopt.profile) :
482 print (rootopt.legendname,
"is a profile: no need to scale")
484 if not (entries[rootopt] == 0) :
485 print (
"scaling",rootopt.legendname,
"to",entryZ/entries[rootopt])
486 plot.Scale(entryZ/entries[rootopt])
488 print (rootopt.legendname,
"is an empty hist, no scale")
490 for rootopt
in rootopts :
491 if (plots[plotopt][rootopt].GetMaximum() > valuemax) :
492 valuemax = plots[plotopt][rootopt].GetMaximum()
497 for rootopt
in rootopts :
498 if (rootopt.markerstyle > 0) :
500 plot = plots[plotopt][rootopt]
501 print (
"Drawing filler from",rootopt.legendname)
503 if (plotopt.logy == 1) :
507 plot.SetMaximum(valuemax * 1.1)
508 plot.GetXaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[0].lstrip(
"+")])
509 if len(plotopt.vars_to_draw) > 1 :
510 plot.GetYaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[1].lstrip(
"+")])
512 plot.Draw(
"CONT4Z "+sameDrawOpt)
514 plot.Draw(rootopt.drawopt+sameDrawOpt)
515 sameDrawOpt =
" SAME"
517 for rootopt
in rootopts :
518 plot = plots[plotopt][rootopt]
519 prname = rootopt.legendname
520 if optmean
and (
not plotopt.profile):
521 prname +=
" (mean: "+(
"%.4f" %plot.GetMean())+
")"
522 leg.AddEntry(plot,prname,
"L")
523 if (rootopt.markerstyle == 0) :
525 print (
"Drawing plot from",rootopt.legendname)
527 if (plotopt.logy == 1) :
531 plot.SetMaximum(valuemax * 1.1)
532 plot.GetXaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[0].lstrip(
"+")])
533 if len(plotopt.vars_to_draw) > 1 :
534 plot.GetYaxis().SetTitle(plotopt.axis_captions[plotopt.vars_to_draw[1].lstrip(
"+")])
536 plot.Draw(
"CONT4Z "+sameDrawOpt)
538 plot.Draw(rootopt.drawopt+sameDrawOpt)
539 sameDrawOpt =
" SAME"
541 legends.append(leg.Clone())
542 legends[len(legends)-1].Draw()
544 if output !=
"DISPLAY" :