00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 """
00020 cuy
00021
00022 A very simple way to make plots with ROOT via an XML file.
00023
00024 usage: %prog -x <XML configuration file>
00025 -b, --batch : run in batch mode without graphics.
00026 -c, --create = CREATE: create XML configuration file from a ROOT file.
00027 -e, --example = EXAMPLE: generate an example xml file.
00028 -f, --flag = FLAG: create a baneer
00029 -l, --list = LIST: list of objects in the ROOT file.
00030 -p, --prt = PRT: print canvas in the format specified png, ps, eps, pdf, etc.
00031 -t, --tag = TAG: tag name for XML configuration file.
00032 -v, --verbose : verbose output.
00033 -w, --wait : Pause script after plotting a new superposition of histograms.
00034 -x, --xml = XML: xml configuration file.
00035
00036 Francisco Yumiceva (yumiceva@fnal.gov)
00037 Fermilab 2008
00038
00039 """
00040
00041
00042 import os, string, re, sys, math
00043
00044 try:
00045 import ROOT
00046 except:
00047 print "\nCannot load PYROOT, make sure you have setup ROOT in the path"
00048 print "and pyroot library is also defined in the variable PYTHONPATH, try:\n"
00049 if (os.getenv("PYTHONPATH")):
00050 print " setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n"
00051 else:
00052 print " setenv PYTHONPATH $ROOTSYS/lib\n"
00053 sys.exit()
00054
00055 from ROOT import TFile
00056 from ROOT import TCanvas
00057 from ROOT import TLegend
00058 from ROOT import SetOwnership
00059 from ROOT import THStack
00060 from ROOT import TLatex
00061 from ROOT import TH1
00062 from ROOT import TH1F
00063 from ROOT import TGraphErrors
00064 from ROOT import TVectorD
00065 from ROOT import std
00066
00067 from xml.sax import saxutils, make_parser, handler
00068 from xml.sax.handler import feature_namespaces
00069
00070 import Inspector
00071 import Style
00072
00073
00074 import optparse
00075
00076 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
00077
00078 def nonzero(self):
00079 "True if options were given"
00080 for v in self.__dict__.itervalues():
00081 if v is not None: return True
00082 return False
00083
00084 optparse.Values.__nonzero__ = nonzero
00085
00086 class ParsingError(Exception): pass
00087
00088 optionstring=""
00089
00090 def exit(msg=""):
00091 raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
00092
00093 def parse(docstring, arglist=None):
00094 global optionstring
00095 optionstring = docstring
00096 match = USAGE.search(optionstring)
00097 if not match: raise ParsingError("Cannot find the option string")
00098 optlines = match.group(1).splitlines()
00099 try:
00100 p = optparse.OptionParser(optlines[0])
00101 for line in optlines[1:]:
00102 opt, help=line.split(':')[:2]
00103 short,long=opt.split(',')[:2]
00104 if '=' in opt:
00105 action='store'
00106 long=long.split('=')[0]
00107 else:
00108 action='store_true'
00109 p.add_option(short.strip(),long.strip(),
00110 action = action, help = help.strip())
00111 except (IndexError,ValueError):
00112 raise ParsingError("Cannot parse the option string correctly")
00113 return p.parse_args(arglist)
00114
00115
00116
00117 class ValElement:
00118 def __init__(self):
00119 self.type = ""
00120 self.filename = ""
00121 self.release = ""
00122 self.histos = {}
00123 self.TH1s = {}
00124 self.weight = None
00125
00126 class divideElement:
00127 def __init__(self):
00128 self.name = ""
00129 self.numerator = None
00130 self.denominator = None
00131
00132 class plotElement:
00133 def __init__(self):
00134 self.name = ""
00135 self.title = ""
00136 self.color = ""
00137
00138 class additionElement:
00139 def __init__(self):
00140 self.name = ""
00141 self.title = ""
00142 self.SetLogy = ""
00143 self.SetGrid = ""
00144 self.histos = []
00145 self.weight = []
00146
00147 class superimposeElement:
00148 def __init__(self):
00149 self.name = ""
00150 self.title = ""
00151 self.SetLogy = ""
00152 self.SetGrid = ""
00153 self.histos = []
00154 self.color = []
00155 self.marker = []
00156 self.legend = []
00157 self.weight = []
00158
00159
00160 class graphElement:
00161 def __init__(self):
00162 self.name = ""
00163 self.title = ""
00164 self.SetLogy = ""
00165 self.SetGrid = ""
00166 self.histos = []
00167 self.color = []
00168 self.marker = []
00169 self.legend = []
00170 self.weight = []
00171 self.flavour = []
00172
00173
00174 class FindIssue(handler.ContentHandler):
00175 def __init__(self):
00176 self.data = {}
00177 self.divide = {}
00178 self.addition = {}
00179 self.superimpose = {}
00180 self.graph = {}
00181 self.tmpaddname = ""
00182 self.plot = {}
00183 self.size = 0
00184 self.atype = ""
00185 self.tmpsupername = ""
00186 self.tmpgraphname = ""
00187
00188 def startElement(self, name, attrs):
00189 if name == 'validation':
00190 self.size = self.size + 1
00191 self.atype = attrs.get('type',None)
00192 self.data[self.atype] = ValElement()
00193 self.data[self.atype].type = attrs.get('type',None)
00194 self.data[self.atype].filename = attrs.get('file',None)
00195 self.data[self.atype].release = attrs.get('release',None)
00196 self.data[self.atype].weight = attrs.get('weight','')
00197 if name == 'TH1':
00198 self.data[self.atype].histos[attrs.get('name',None)] = attrs.get('source',None)
00199
00200
00201 if name == 'divide':
00202 aname = attrs.get('name',None)
00203 self.divide[aname] = divideElement()
00204 self.divide[aname].name = aname
00205 self.divide[aname].numerator = attrs.get('numerator',None)
00206 self.divide[aname].denominator = attrs.get('denominator',None)
00207 self.divide[aname].DivideOption = attrs.get('DivideOption',None)
00208 self.divide[aname].Option = attrs.get('Option',None)
00209 if name == 'addition':
00210 aname = attrs.get('name',None)
00211 self.addition[aname] = additionElement()
00212 self.tmpaddname = aname
00213 self.addition[aname].name = aname
00214 self.addition[aname].title = attrs.get('title',None)
00215 self.addition[aname].YTitle = attrs.get('YTitle',None)
00216 self.addition[aname].XTitle = attrs.get('XTitle',None)
00217 self.addition[aname].Option = attrs.get('Option',None)
00218 self.addition[aname].Weight = attrs.get('Wight',None)
00219 self.addition[aname].Normalize = attrs.get('Normalize',None)
00220 self.addition[aname].SetGrid = attrs.get('SetGrid',None)
00221 if name == 'additionItem':
00222
00223 self.addition[self.tmpaddname].histos.append(attrs.get('name',None))
00224 self.addition[self.tmpaddname].weight.append(attrs.get('weight',None))
00225 if name == 'superimpose':
00226 aname = attrs.get('name',None)
00227 self.superimpose[aname] = superimposeElement()
00228 self.superimpose[aname].name = aname
00229 self.superimpose[aname].title = attrs.get('title',None)
00230 self.superimpose[aname].SetLogy = attrs.get('SetLogy',None)
00231 self.superimpose[aname].SetGrid = attrs.get('SetGrid',None)
00232 self.superimpose[aname].Normalize = attrs.get('Normalize',None)
00233 self.superimpose[aname].Stack = attrs.get('Stack',None)
00234 self.superimpose[aname].YTitle = attrs.get('YTitle',None)
00235 self.superimpose[aname].XTitle = attrs.get('XTitle',None)
00236 self.superimpose[aname].projection = attrs.get('Projection',None)
00237 self.superimpose[aname].bin = attrs.get('bin',None)
00238 self.superimpose[aname].profile = attrs.get('Profile',None)
00239 self.superimpose[aname].Fill = attrs.get('Fill',None)
00240 self.superimpose[aname].Option = attrs.get('Option',None)
00241 self.superimpose[aname].Weight = attrs.get('Weight',None)
00242 self.superimpose[aname].Maximum = attrs.get('Maximum',None)
00243 self.superimpose[aname].Minimum = attrs.get('Minimum',None)
00244 self.superimpose[aname].Labels = attrs.get('Labels',None)
00245 self.superimpose[aname].Rebin = attrs.get('Rebin',None)
00246 self.tmpsupername = aname
00247 if name == 'graph':
00248 aname = attrs.get('name',None)
00249 self.graph[aname] = graphElement()
00250 self.graph[aname].name = aname
00251 self.graph[aname].title = attrs.get('title',None)
00252 self.graph[aname].SetLogy = attrs.get('SetLogy',None)
00253 self.graph[aname].SetGrid = attrs.get('SetGrid',None)
00254 self.graph[aname].Normalize = attrs.get('Normalize',None)
00255 self.graph[aname].Stack = attrs.get('Stack',None)
00256 self.graph[aname].YTitle = attrs.get('YTitle',None)
00257 self.graph[aname].XTitle = attrs.get('XTitle',None)
00258 self.graph[aname].projection = attrs.get('Projection',None)
00259 self.graph[aname].bin = attrs.get('bin',None)
00260 self.graph[aname].profile = attrs.get('Profile',None)
00261 self.graph[aname].Fill = attrs.get('Fill',None)
00262 self.graph[aname].Option = attrs.get('Option',None)
00263 self.graph[aname].Weight = attrs.get('Weight',None)
00264 self.graph[aname].Maximum = attrs.get('Maximum',None)
00265 self.graph[aname].Minimum = attrs.get('Minimum',None)
00266 self.graph[aname].Labels = attrs.get('Labels',None)
00267 self.tmpgraphname = aname
00268 if name == 'superimposeItem':
00269
00270 self.superimpose[self.tmpsupername].histos.append(attrs.get('name',None))
00271 self.superimpose[self.tmpsupername].color.append(attrs.get('color',None))
00272 self.superimpose[self.tmpsupername].marker.append(attrs.get('MarkerStyle',None))
00273 self.superimpose[self.tmpsupername].legend.append(attrs.get('legend',None))
00274
00275
00276 if name == 'graphItem':
00277
00278 self.graph[self.tmpgraphname].histos.append(attrs.get('name',None))
00279 self.graph[self.tmpgraphname].color.append(attrs.get('color',None))
00280 self.graph[self.tmpgraphname].marker.append(attrs.get('MarkerStyle',None))
00281 self.graph[self.tmpgraphname].legend.append(attrs.get('legend',None))
00282 self.graph[self.tmpgraphname].flavour.append(attrs.get('flavour',None))
00283
00284
00285
00286
00287 if __name__ == '__main__':
00288
00289
00290
00291
00292 thestyle = Style.Style()
00293 thestyle.SetStyle()
00294
00295 printCanvas = False
00296 printFormat = "png"
00297 printBanner = False
00298 Banner = "CMS Preliminary"
00299 verbose = False
00300
00301
00302 option,args = parse(__doc__)
00303 if not args and not option: exit()
00304
00305 if option.batch:
00306 ROOT.gROOT.SetBatch()
00307
00308 if option.verbose:
00309 verbose = True
00310
00311 if option.list:
00312 ins = Inspector.Inspector()
00313 ins.Verbose(True)
00314 ins.createXML(False)
00315 ins.SetFilename(option.list)
00316 ins.GetListObjects()
00317 sys.exit()
00318
00319 if option.create:
00320 createXML = Inspector.Inspector()
00321 createXML.Verbose(False)
00322 createXML.createXML(True)
00323 if option.tag:
00324 createXML.SetTag(option.tag)
00325 createXML.SetFilename(option.create)
00326 createXML.GetListObjects()
00327 sys.exit()
00328
00329 if not option.xml: exit()
00330 if option.prt:
00331 printCanvas = True
00332 printFormat = option.prt
00333
00334 if option.flag:
00335 printBanner = True
00336 Banner = option.flag
00337
00338
00339 try:
00340 xmlfile = open(option.xml)
00341 xmlfile.close()
00342 except:
00343 print " ERROR: xml file \"" + option.xml + "\" does not exist"
00344 sys.exit()
00345
00346
00347 parser = make_parser()
00348
00349
00350 parser.setFeature(feature_namespaces, 0)
00351
00352
00353 dh = FindIssue()
00354
00355
00356 parser.setContentHandler(dh)
00357
00358
00359 parser.parse(option.xml)
00360
00361
00362 cv = {}
00363 afilelist = {}
00364 stacklist = {}
00365
00366
00367 outputroot = TFile("cuy.root","RECREATE")
00368
00369
00370 newTH1list = []
00371
00372
00373 thedata = dh.data
00374
00375 firstFilename = ''
00376
00377 for ikey in thedata:
00378 if verbose : print "= Processing set called: " + ikey
00379 afilename = thedata[ikey].filename
00380 if firstFilename == '':
00381 firstFilename = afilename
00382 arelease = ""
00383 if thedata[ikey].release != None:
00384 arelease = thedata[ikey].release
00385 if verbose : print "== filename: " + afilename
00386 if verbose : print "== release: " + arelease
00387 if verbose : print "== weight: " + thedata[ikey].weight
00388 thehistos = thedata[ikey].histos
00389 afilelist[afilename] = TFile(afilename)
00390 if verbose : print "== get histograms: "
00391 histonamekeys = thehistos.keys()
00392 for ihname in histonamekeys:
00393 if verbose : print "=== Histogram name: \""+ ihname + "\" source: \""+thehistos[ihname]+"\""
00394 thedata[ikey].TH1s[ihname] = ROOT.gDirectory.Get(thehistos[ihname])
00395
00396
00397 print thedata[ikey].TH1s[ihname].GetName()
00398
00399
00400
00401
00402 afilelist[firstFilename].cd()
00403
00404
00405
00406 theaddition = dh.addition
00407 if verbose : print "= Create addition histograms:"
00408
00409 for ikey in theaddition:
00410 if verbose : print "== plot name: \""+theaddition[ikey].name+"\" title: \""+theaddition[ikey].title+"\""
00411 listname = theaddition[ikey].histos
00412 listweight = theaddition[ikey].weight
00413
00414
00415 cv[theaddition[ikey].name] = TCanvas(theaddition[ikey].name,theaddition[ikey].name,700,700)
00416
00417 isFirst = True
00418 ihnameIt = 0
00419 for ihname in listname:
00420 aweight = 1
00421 if listweight[ihnameIt]:
00422
00423 aweight = float(listweight[ihnameIt])
00424
00425 for jkey in thedata:
00426 tmpkeys = thedata[jkey].histos.keys()
00427 for tmpname in tmpkeys:
00428 if tmpname == ihname:
00429 ath = thedata[jkey].TH1s[tmpname]
00430 if ath is None:
00431 print "ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename
00432 exit(0)
00433 if verbose : print "=== add histogram: "+ath.GetName() + " from " + thedata[jkey].filename + " mean = " + "%.2f" % round(ath.GetMean(),2) + " weight= " + str(aweight)
00434
00435 if isFirst:
00436 newth = ath.Clone(theaddition[ikey].name)
00437 newth.Sumw2()
00438 if theaddition[ikey].Normalize == "true":
00439 newth.Scale(1/newth.Integral())
00440 newth.Scale(aweight)
00441 isFirst = False
00442 else:
00443 atmpth = ath.Clone()
00444 atmpth.Sumw2()
00445 if theaddition[ikey].Normalize == "true":
00446 atmpth.Scale(1/atmpth.Integral())
00447 atmpth.Scale(aweight)
00448 newth.Add( atmpth )
00449 ihnameIt = ihnameIt + 1
00450
00451 if theaddition[ikey].XTitle != None:
00452 newth.SetXTitle(theaddition[ikey].XTitle)
00453 if theaddition[ikey].YTitle != None:
00454 newth.SetYTitle(theaddition[ikey].YTitle)
00455
00456 if theaddition[ikey].Option:
00457 newth.Draw(theaddition[ikey].Option)
00458 else:
00459 newth.Draw()
00460
00461 if theaddition[ikey].SetGrid == "true":
00462 cv[theaddition[ikey].name].SetGrid()
00463
00464 cv[theaddition[ikey].name].Update()
00465
00466
00467 newth.SetName(theaddition[ikey].name)
00468 newTH1list.append(newth.GetName())
00469 thedata[newth.GetName()] = ValElement()
00470 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
00471 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
00472
00473
00474 outputroot.cd()
00475 newth.Write()
00476
00477
00478 if verbose : print "= Create ratio histograms:"
00479
00480 thedivition = dh.divide
00481 for ikey in thedivition:
00482 if verbose : print "== plot name: \""+thedivition[ikey].name+"\" title: \""+"\""
00483 numerator = thedivition[ikey].numerator
00484 denominator = thedivition[ikey].denominator
00485
00486
00487 cv[thedivition[ikey].name] = TCanvas(thedivition[ikey].name,thedivition[ikey].name,700,700)
00488
00489 for jkey in thedata:
00490 tmpkeys = thedata[jkey].histos.keys()
00491 for tmpname in tmpkeys:
00492 if tmpname == numerator:
00493 numeratorth = thedata[jkey].TH1s[tmpname]
00494 if numeratorth is None:
00495 print "ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename
00496 exit(0)
00497
00498
00499 if tmpname == denominator:
00500 denominatorth = thedata[jkey].TH1s[tmpname]
00501 if denominatorth is None:
00502 print "ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename
00503 exit(0)
00504
00505
00506
00507
00508 numeratorth.Sumw2()
00509 denominatorth.Sumw2()
00510 newth = numeratorth.Clone()
00511 newth.Clear()
00512 if thedivition[ikey].DivideOption is None:
00513 newth.Divide(numeratorth,denominatorth)
00514 else:
00515 newth.Divide(numeratorth,denominatorth,1.,1.,thedivition[ikey].DivideOption)
00516
00517
00518
00519
00520
00521 if thedivition[ikey].Option:
00522 newth.Draw(thedivition[ikey].Option)
00523 else:
00524 newth.Draw()
00525
00526 cv[thedivition[ikey].name].Update()
00527
00528
00529
00530 if option.wait:
00531 raw_input( 'Press ENTER to continue\n ' )
00532
00533
00534 newth.SetName(thedivition[ikey].name)
00535 newTH1list.append(newth.GetName())
00536 thedata[newth.GetName()] = ValElement()
00537 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
00538 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
00539
00540
00541 outputroot.cd()
00542 newth.Write()
00543
00544
00545 thesuper = dh.superimpose
00546 if verbose : print "= Create superimpose histograms:"
00547 for ikey in thesuper:
00548 if verbose : print "== plot name: \""+thesuper[ikey].name+"\" title: \""+thesuper[ikey].title+"\""
00549 listname = thesuper[ikey].histos
00550 listcolor = thesuper[ikey].color
00551 listmarker = thesuper[ikey].marker
00552 listlegend = thesuper[ikey].legend
00553
00554 dolegend = False
00555 for il in listlegend:
00556 if il==None: dolegend = False
00557 if verbose : print "dolegend = " +str(dolegend)
00558 doNormalize = False
00559 doRebin=thesuper[ikey].Rebin
00560 if doRebin is not None :
00561 doRebin=int(doRebin)
00562 if verbose : print "Rebin is ", doRebin
00563 if thesuper[ikey].Normalize == "true":
00564 doNormalize = True
00565 if verbose : print "normalize = " +str(doNormalize)
00566 projectAxis = "no"
00567 projectBin = -1
00568 if thesuper[ikey].projection == "x": projectAxis = "x"
00569 if thesuper[ikey].projection == "y": projectAxis = "y"
00570 if thesuper[ikey].bin != None: projectBin = thesuper[ikey].bin
00571 profileAxis = "no"
00572 if thesuper[ikey].profile == "x": profileAxis = "x"
00573 if thesuper[ikey].profile == "y": profileAxis = "y"
00574 doFill = False
00575 if thesuper[ikey].Fill == "true": doFill = True
00576 if verbose : print "fill option:"+ doFill
00577
00578 cv[thesuper[ikey].name] = TCanvas(thesuper[ikey].name,thesuper[ikey].title,700,700)
00579
00580 aleg = TLegend(0.6,0.4,0.8,0.6)
00581 SetOwnership( aleg, 0 )
00582 aleg.SetMargin(0.12)
00583 aleg.SetTextSize(0.035)
00584 aleg.SetFillColor(10)
00585 aleg.SetBorderSize(0)
00586
00587 isFirst = 1
00588 ii = 0
00589
00590 stacklist[thesuper[ikey].name] = THStack("astack"+thesuper[ikey].name,thesuper[ikey].title)
00591 astack = stacklist[thesuper[ikey].name]
00592 for ihname in listname:
00593
00594 for jkey in thedata:
00595 tmpkeys = thedata[jkey].histos.keys()
00596
00597 for tmpname in tmpkeys:
00598
00599 if tmpname == ihname:
00600 ath = thedata[jkey].TH1s[tmpname]
00601 if ath is None:
00602 print "ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename
00603 exit(0)
00604 if verbose : print "=== superimpose histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2)
00605
00606 if projectAxis == "x":
00607 if projectBin == -1:
00608 newthpx = ath.ProjectionX(ath.GetName()+"_px",0,-1,"e")
00609 else:
00610 newthpx = ath.ProjectionX(ath.GetName()+"_px",int(projectBin),int(projectBin),"e")
00611 newth = newthpx.Clone()
00612 if projectAxis == "y":
00613 if projectBin == -1:
00614 newthpy = ath.ProjectionY(ath.GetName()+"_py",0,-1,"e")
00615 else:
00616 newthpx = ath.ProjectionY(ath.GetName()+"_py",int(projectBin),int(projectBin),"e")
00617 newth = newthpy.Clone()
00618 if profileAxis == "x":
00619 newthpx = ath.ProfileX(ath.GetName()+"_px",0,-1,"e")
00620 newth = newthpx.Clone()
00621 if profileAxis == "y":
00622 newthpy = ath.ProfileY(ath.GetName()+"_py",0,-1,"e")
00623 newth = newthpy.Clone()
00624
00625
00626 aweight = 1
00627 if thedata[jkey].weight != None and thesuper[ikey].Weight=="true":
00628 aweight = float( thedata[jkey].weight )
00629 if verbose: print " with weight = " + str(aweight)
00630
00631
00632
00633
00634 if projectAxis == "no" and profileAxis == "no" :newth = ath.Clone()
00635
00636 if doRebin is not None and doRebin>0 :
00637 newth.Rebin(doRebin)
00638
00639 newth.Sumw2()
00640 newth.Scale(aweight)
00641
00642
00643 if not listcolor[ii]:
00644 listcolor[ii] = 1
00645
00646 newth.SetLineColor(int(listcolor[ii]))
00647 newth.SetMarkerColor(int(listcolor[ii]))
00648
00649 if doFill: newth.SetFillColor(int(listcolor[ii]))
00650
00651 if listmarker[ii] != None:
00652 newth.SetMarkerStyle(int(listmarker[ii]))
00653
00654 if doNormalize:
00655 newth.Scale(1./newth.Integral())
00656
00657
00658 if thesuper[ikey].Labels != None:
00659 thelabels = thesuper[ikey].Labels.split(',')
00660 ib = 1
00661
00662
00663 for ilabel in thelabels:
00664 newth.GetXaxis().SetBinLabel(ib,ilabel)
00665
00666
00667
00668 ib += 1
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 if doFill:
00687 if thesuper[ikey].XTitle != None:
00688 newth.SetXTitle("")
00689 astack.Add(newth,"HIST")
00690 elif thesuper[ikey].Option:
00691 astack.Add(newth,thesuper[ikey].Option)
00692 else:
00693
00694 astack.Add(newth)
00695
00696 astack.SetTitle(thesuper[ikey].title)
00697
00698 if isFirst==1:
00699 newth.GetPainter().PaintStat(ROOT.gStyle.GetOptStat(),0);
00700 isFirst=0
00701 tmpsumth = newth.Clone()
00702 else:
00703 tmpsumth.Add(newth)
00704
00705
00706
00707
00708
00709
00710
00711 if dolegend and doFill:
00712 aleg.AddEntry(newth,listlegend[ii],"F")
00713 elif dolegend:
00714 aleg.AddEntry(newth,listlegend[ii],"P")
00715
00716 newth.SetName(tmpname)
00717 outputroot.cd()
00718 newth.Write()
00719 ii = ii + 1
00720
00721
00722 if thesuper[ikey].Maximum != None:
00723 astack.SetMaximum( float(thesuper[ikey].Maximum) )
00724 if thesuper[ikey].Minimum != None:
00725 astack.SetMinimum( float(thesuper[ikey].Minimum) )
00726 if thesuper[ikey].Stack == "true":
00727 astack.Draw()
00728 if thesuper[ikey].Stack == "false" or thesuper[ikey].Stack == None:
00729 astack.Draw()
00730 astack.Draw("nostack")
00731 if thesuper[ikey].XTitle != None:
00732 astack.GetHistogram().SetXTitle(thesuper[ikey].XTitle)
00733 if thesuper[ikey].YTitle != None:
00734 astack.GetHistogram().SetYTitle(thesuper[ikey].YTitle)
00735 if doFill:
00736 astack.Draw("sameaxis")
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 if dolegend:
00753 aleg.Draw()
00754 if thesuper[ikey].SetLogy == "true":
00755 cv[thesuper[ikey].name].SetLogy()
00756 if thesuper[ikey].SetGrid == "true":
00757 cv[thesuper[ikey].name].SetGrid()
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 if printBanner:
00772 tex = TLatex(0.35,0.95,Banner)
00773 tex.SetNDC()
00774 tex.SetTextSize(0.05)
00775 tex.Draw()
00776
00777 cv[thesuper[ikey].name].Update()
00778
00779
00780
00781 if option.wait:
00782 raw_input( 'Press ENTER to continue\n ' )
00783
00784
00785
00786
00787
00788
00789 thegraph = dh.graph
00790 if verbose : print "= Create graph histograms:"
00791 for ikey in thegraph:
00792 if verbose : print "== plot name: \""+thegraph[ikey].name+"\" title: \""+thegraph[ikey].title+"\""
00793 listname = thegraph[ikey].histos
00794 listcolor = thegraph[ikey].color
00795 listmarker = thegraph[ikey].marker
00796 listlegend = thegraph[ikey].legend
00797 listflavour = thegraph[ikey].flavour
00798
00799 dolegend = False
00800 for il in listlegend:
00801 if il==None: dolegend = False
00802 if verbose : print "dolegend = " +str(dolegend)
00803 doNormalize = False
00804 if thegraph[ikey].Normalize == "true":
00805 doNormalize = True
00806 if verbose : print "normalize = " +str(doNormalize)
00807 projectAxis = "no"
00808 projectBin = -1
00809 if thegraph[ikey].projection == "x": projectAxis = "x"
00810 if thegraph[ikey].projection == "y": projectAxis = "y"
00811 if thegraph[ikey].bin != None: projectBin = thegraph[ikey].bin
00812 profileAxis = "no"
00813 if thegraph[ikey].profile == "x": profileAxis = "x"
00814 if thegraph[ikey].profile == "y": profileAxis = "y"
00815 doFill = False
00816 if thegraph[ikey].Fill == "true": doFill = True
00817 if verbose : print "fill option:"+ doFill
00818
00819 cv[thegraph[ikey].name] = TCanvas(thegraph[ikey].name,thegraph[ikey].title,700,700)
00820
00821 aleg = TLegend(0.6,0.4,0.8,0.6)
00822 SetOwnership( aleg, 0 )
00823 aleg.SetMargin(0.12)
00824 aleg.SetTextSize(0.035)
00825 aleg.SetFillColor(10)
00826 aleg.SetBorderSize(0)
00827
00828 isFirst = 1
00829 ii = 0
00830
00831 stacklist[thegraph[ikey].name] = THStack("astack"+thegraph[ikey].name,thegraph[ikey].title)
00832 astack = stacklist[thegraph[ikey].name]
00833 xVal_val = TVectorD()
00834 yVal_val = TVectorD()
00835 yBin_val = std.vector(int)()
00836 xErr_val = TVectorD()
00837 yErr_val = TVectorD()
00838 zVal_val = TVectorD()
00839 zErr_val = TVectorD()
00840 nVal_val = 0
00841
00842 xVal_ref = TVectorD()
00843 yVal_ref = TVectorD()
00844 yBin_ref = std.vector(int)()
00845 xErr_ref = TVectorD()
00846 yErr_ref = TVectorD()
00847 zVal_ref = TVectorD()
00848 zErr_ref = TVectorD()
00849 nVal_ref = 0
00850
00851 RangeMax = 0.005
00852 RangeMin = 0.9
00853
00854 for ihname in listname:
00855
00856 for jkey in thedata:
00857 tmpkeys = thedata[jkey].histos.keys()
00858
00859 for tmpname in tmpkeys:
00860
00861 if tmpname == ihname:
00862
00863 ath = thedata[jkey].TH1s[tmpname]
00864 if ath is None:
00865 print "ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename
00866 exit(0)
00867 if verbose : print "=== graph histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2)
00868
00869 if listflavour[ii] == "5":
00870
00871 nBinB = 200
00872 BinWidth = (0.01+ath.GetMaximum())/nBinB
00873 BMid = 0.005+BinWidth/2
00874 Err = BinWidth
00875 for iBinB in range(1,nBinB+1):
00876
00877 BMid = BMid+Err
00878
00879 nAthBin = ath.GetNbinsX()-2
00880
00881 maxInHisto = ath.GetMaximum()
00882 minInHisto = ath.GetMinimum()
00883
00884 yClosestInit = 0
00885 iBinClosestInit = 0
00886 if BMid <= maxInHisto : yClosestInit = maxInHisto + 1
00887 else : yClosestInit = minInHisto - 1.0
00888 iBinClosest = iBinClosestInit
00889 yClosest = yClosestInit
00890 for iAthBin in range(1,nAthBin+1):
00891 yBin = ath.GetBinContent(iAthBin)
00892 dif1 = BMid-yBin
00893 if dif1 < 0 : dif1 = yBin-BMid
00894 dif2 = yClosest-BMid
00895 if dif2 < 0 : dif2 = BMid-yClosest
00896 if dif1 < dif2:
00897 yClosest = yBin
00898 iBinClosest = iAthBin
00899 min = BMid-Err/2
00900 max = BMid+Err/2
00901
00902 if yClosest < min or yClosest > max:
00903 iBinClosest = 0
00904
00905 if iBinClosest > 0 and listmarker[ii] == "8":
00906
00907 nVal_ref = nVal_ref+1
00908 xVal_ref.ResizeTo(nVal_ref)
00909
00910 xErr_ref.ResizeTo(nVal_ref)
00911 xVal_ref[nVal_ref-1] = BMid
00912 yBin_ref.push_back(iBinClosest)
00913 xErr_ref[nVal_ref-1] = ath.GetBinError ( iBinClosest )
00914 Err = xErr_ref[nVal_ref-1]
00915 if Err < BinWidth : Err = BinWidth
00916 elif iBinClosest > 0:
00917 nVal_val = nVal_val+1
00918 xVal_val.ResizeTo(nVal_val)
00919
00920 xErr_val.ResizeTo(nVal_val)
00921 xVal_val[nVal_val-1] = BMid
00922 yBin_val.push_back(iBinClosest)
00923 xErr_val[nVal_val-1] = ath.GetBinError ( iBinClosest )
00924 Err = xErr_val[nVal_val-1]
00925 if Err < BinWidth : Err = BinWidth
00926 elif listflavour[ii] == "4" and listmarker[ii] == "8":
00927 yVal_ref.ResizeTo(nVal_ref)
00928 yErr_ref.ResizeTo(nVal_ref)
00929 for iVal in range(0,nVal_ref):
00930 yVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
00931 if yVal_ref[iVal] > RangeMax : RangeMax = yVal_ref[iVal]
00932 yErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
00933 elif listflavour[ii] == "4":
00934 yVal_val.ResizeTo(nVal_val)
00935 yErr_val.ResizeTo(nVal_val)
00936 for iVal in range(0,nVal_val):
00937 yVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
00938 yErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
00939 elif listmarker[ii] == "8":
00940 zVal_ref.ResizeTo(nVal_ref)
00941 zErr_ref.ResizeTo(nVal_ref)
00942 for iVal in range(0,nVal_ref):
00943 zVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
00944 zErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
00945 if zVal_ref[iVal] < RangeMin : RangeMin = zVal_ref[iVal]
00946 else:
00947 zVal_val.ResizeTo(nVal_val)
00948 zErr_val.ResizeTo(nVal_val)
00949 for iVal in range(0,nVal_val):
00950 zVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
00951 zErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
00952 ii = ii + 1
00953
00954
00955
00956
00957
00958
00959
00960 graphs = [TGraphErrors(xVal_ref,yVal_ref,xErr_ref,yErr_ref),
00961 TGraphErrors(xVal_ref,zVal_ref,xErr_ref,zErr_ref),
00962 TGraphErrors(xVal_val,yVal_val,xErr_val,yErr_val),
00963 TGraphErrors(xVal_val,zVal_val,xErr_val,zErr_val)]
00964 ii = 0
00965
00966
00967
00968 for ii in range(0,4):
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 aweight = 1
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 col = 2
01009 mark = 22
01010 if ii == 0 or ii == 2:
01011 col = 1
01012 if ii == 0 or ii == 1:
01013 mark = 8
01014
01015 graphs[ii].SetLineColor(col)
01016 graphs[ii].SetMarkerStyle(mark)
01017 graphs[ii].SetMarkerColor(col)
01018 graphs[ii].SetTitle(thegraph[ikey].title)
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068 if isFirst==1:
01069
01070 isFirst=0
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 outputroot.cd()
01090 graphs[ii].Write()
01091 ii = ii + 1
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103 if thegraph[ikey].XTitle != None:
01104 graphs[0].GetHistogram().SetXTitle(thegraph[ikey].XTitle)
01105 if thegraph[ikey].YTitle != None:
01106 graphs[0].GetHistogram().SetYTitle(thegraph[ikey].YTitle)
01107
01108
01109 if RangeMax > 0.5 : RangeMax = 1.5
01110 if RangeMax < 0.5 : RangeMax = RangeMax + 0.05
01111
01112 RangeMin = RangeMin - 0.5*RangeMin
01113
01114
01115 if RangeMin < 0.00001 : RangeMin = 0.00005
01116 graphs[0].GetYaxis().SetRangeUser(RangeMin,RangeMax)
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133 graphs[0].Draw("AP")
01134 graphs[1].Draw("sameP")
01135 graphs[2].Draw("sameP")
01136 graphs[3].Draw("sameP")
01137 if dolegend:
01138 aleg.Draw()
01139 if thegraph[ikey].SetLogy == "true":
01140 cv[thegraph[ikey].name].SetLogy()
01141 if thegraph[ikey].SetGrid == "true":
01142 cv[thegraph[ikey].name].SetGrid()
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156 if printBanner:
01157 tex = TLatex(0.35,0.95,Banner)
01158 tex.SetNDC()
01159 tex.SetTextSize(0.05)
01160 tex.Draw()
01161
01162 cv[thegraph[ikey].name].Update()
01163 save = thegraph[ikey].name
01164 cv[thegraph[ikey].name].Print(save + ".gif")
01165
01166
01167 if option.wait:
01168 raw_input( 'Press ENTER to continue\n ' )
01169
01170
01171
01172
01173
01174 if printCanvas:
01175
01176 for ikey in theaddition:
01177 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
01178 for ikey in thesuper:
01179 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
01180 for ikey in thegraph:
01181 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "notgood." + printFormat)
01182
01183
01184
01185
01186
01187
01188 rep = ''
01189 while not rep in [ 'q', 'Q', '.q', 'qq' 'p']:
01190 rep = raw_input( '\nenter: ["q",".q" to quit] ["p" or "print" to print all canvas]: ' )
01191 if 0<len(rep):
01192 if rep=='quit': rep = 'q'
01193 if rep=='p' or rep=='print':
01194 for ikey in theaddition:
01195 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
01196 for ikey in thesuper:
01197 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
01198 for ikey in thegraph:
01199 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "." + printFormat)
01200
01201