File indexing completed on 2023-03-17 11:28:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 from __future__ import print_function
0015
0016
0017
0018
0019
0020 """
0021 cuy
0022
0023 A very simple way to make plots with ROOT via an XML file.
0024
0025 usage: %prog -x <XML configuration file>
0026 -b, --batch : run in batch mode without graphics.
0027 -c, --create = CREATE: create XML configuration file from a ROOT file.
0028 -e, --example = EXAMPLE: generate an example xml file.
0029 -f, --flag = FLAG: create a baneer
0030 -l, --list = LIST: list of objects in the ROOT file.
0031 -p, --prt = PRT: print canvas in the format specified png, ps, eps, pdf, etc.
0032 -t, --tag = TAG: tag name for XML configuration file.
0033 -v, --verbose : verbose output.
0034 -w, --wait : Pause script after plotting a new superposition of histograms.
0035 -x, --xml = XML: xml configuration file.
0036
0037 Francisco Yumiceva (yumiceva@fnal.gov)
0038 Fermilab 2008
0039
0040 """
0041
0042
0043 import os, string, re, sys, math
0044
0045 try:
0046 import ROOT
0047 except:
0048 print("\nCannot load PYROOT, make sure you have setup ROOT in the path")
0049 print("and pyroot library is also defined in the variable PYTHONPATH, try:\n")
0050 if (os.getenv("PYTHONPATH")):
0051 print(" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
0052 else:
0053 print(" setenv PYTHONPATH $ROOTSYS/lib\n")
0054 sys.exit()
0055
0056 from ROOT import TFile
0057 from ROOT import TCanvas
0058 from ROOT import TLegend
0059 from ROOT import SetOwnership
0060 from ROOT import THStack
0061 from ROOT import TLatex
0062 from ROOT import TH1
0063 from ROOT import TH1F
0064 from ROOT import TGraphErrors
0065 from ROOT import TVectorD
0066 from ROOT import std
0067
0068 from xml.sax import saxutils, make_parser, handler
0069 from xml.sax.handler import feature_namespaces
0070
0071 import Inspector
0072 import Style
0073
0074
0075 import optparse
0076
0077 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
0078
0079 def nonzero(self):
0080 "True if options were given"
0081 for v in self.__dict__.values():
0082 if v is not None: return True
0083 return False
0084
0085 optparse.Values.__nonzero__ = nonzero
0086
0087 class ParsingError(Exception): pass
0088
0089 optionstring=""
0090
0091 def exit(msg=""):
0092 raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
0093
0094 def parse(docstring, arglist=None):
0095 global optionstring
0096 optionstring = docstring
0097 match = USAGE.search(optionstring)
0098 if not match: raise ParsingError("Cannot find the option string")
0099 optlines = match.group(1).splitlines()
0100 try:
0101 p = optparse.OptionParser(optlines[0])
0102 for line in optlines[1:]:
0103 opt, help=line.split(':')[:2]
0104 short,long=opt.split(',')[:2]
0105 if '=' in opt:
0106 action='store'
0107 long=long.split('=')[0]
0108 else:
0109 action='store_true'
0110 p.add_option(short.strip(),long.strip(),
0111 action = action, help = help.strip())
0112 except (IndexError,ValueError):
0113 raise ParsingError("Cannot parse the option string correctly")
0114 return p.parse_args(arglist)
0115
0116
0117
0118 class ValElement:
0119 def __init__(self):
0120 self.type = ""
0121 self.filename = ""
0122 self.release = ""
0123 self.histos = {}
0124 self.TH1s = {}
0125 self.weight = None
0126
0127 class divideElement:
0128 def __init__(self):
0129 self.name = ""
0130 self.numerator = None
0131 self.denominator = None
0132
0133 class plotElement:
0134 def __init__(self):
0135 self.name = ""
0136 self.title = ""
0137 self.color = ""
0138
0139 class additionElement:
0140 def __init__(self):
0141 self.name = ""
0142 self.title = ""
0143 self.SetLogy = ""
0144 self.SetGrid = ""
0145 self.histos = []
0146 self.weight = []
0147
0148 class superimposeElement:
0149 def __init__(self):
0150 self.name = ""
0151 self.title = ""
0152 self.SetLogy = ""
0153 self.SetGrid = ""
0154 self.histos = []
0155 self.color = []
0156 self.marker = []
0157 self.legend = []
0158 self.weight = []
0159
0160
0161 class graphElement:
0162 def __init__(self):
0163 self.name = ""
0164 self.title = ""
0165 self.SetLogy = ""
0166 self.SetGrid = ""
0167 self.histos = []
0168 self.color = []
0169 self.marker = []
0170 self.legend = []
0171 self.weight = []
0172 self.flavour = []
0173
0174
0175 class FindIssue(handler.ContentHandler):
0176 def __init__(self):
0177 self.data = {}
0178 self.divide = {}
0179 self.addition = {}
0180 self.superimpose = {}
0181 self.graph = {}
0182 self.tmpaddname = ""
0183 self.plot = {}
0184 self.size = 0
0185 self.atype = ""
0186 self.tmpsupername = ""
0187 self.tmpgraphname = ""
0188
0189 def startElement(self, name, attrs):
0190 if name == 'validation':
0191 self.size = self.size + 1
0192 self.atype = attrs.get('type',None)
0193 self.data[self.atype] = ValElement()
0194 self.data[self.atype].type = attrs.get('type',None)
0195 self.data[self.atype].filename = attrs.get('file',None)
0196 self.data[self.atype].release = attrs.get('release',None)
0197 self.data[self.atype].weight = attrs.get('weight','')
0198 if name == 'TH1':
0199 self.data[self.atype].histos[attrs.get('name',None)] = attrs.get('source',None)
0200
0201
0202 if name == 'divide':
0203 aname = attrs.get('name',None)
0204 self.divide[aname] = divideElement()
0205 self.divide[aname].name = aname
0206 self.divide[aname].numerator = attrs.get('numerator',None)
0207 self.divide[aname].denominator = attrs.get('denominator',None)
0208 self.divide[aname].DivideOption = attrs.get('DivideOption',None)
0209 self.divide[aname].Option = attrs.get('Option',None)
0210 if name == 'addition':
0211 aname = attrs.get('name',None)
0212 self.addition[aname] = additionElement()
0213 self.tmpaddname = aname
0214 self.addition[aname].name = aname
0215 self.addition[aname].title = attrs.get('title',None)
0216 self.addition[aname].YTitle = attrs.get('YTitle',None)
0217 self.addition[aname].XTitle = attrs.get('XTitle',None)
0218 self.addition[aname].Option = attrs.get('Option',None)
0219 self.addition[aname].Weight = attrs.get('Wight',None)
0220 self.addition[aname].Normalize = attrs.get('Normalize',None)
0221 self.addition[aname].SetGrid = attrs.get('SetGrid',None)
0222 if name == 'additionItem':
0223
0224 self.addition[self.tmpaddname].histos.append(attrs.get('name',None))
0225 self.addition[self.tmpaddname].weight.append(attrs.get('weight',None))
0226 if name == 'superimpose':
0227 aname = attrs.get('name',None)
0228 self.superimpose[aname] = superimposeElement()
0229 self.superimpose[aname].name = aname
0230 self.superimpose[aname].title = attrs.get('title',None)
0231 self.superimpose[aname].SetLogy = attrs.get('SetLogy',None)
0232 self.superimpose[aname].SetGrid = attrs.get('SetGrid',None)
0233 self.superimpose[aname].Normalize = attrs.get('Normalize',None)
0234 self.superimpose[aname].Stack = attrs.get('Stack',None)
0235 self.superimpose[aname].YTitle = attrs.get('YTitle',None)
0236 self.superimpose[aname].XTitle = attrs.get('XTitle',None)
0237 self.superimpose[aname].projection = attrs.get('Projection',None)
0238 self.superimpose[aname].bin = attrs.get('bin',None)
0239 self.superimpose[aname].profile = attrs.get('Profile',None)
0240 self.superimpose[aname].Fill = attrs.get('Fill',None)
0241 self.superimpose[aname].Option = attrs.get('Option',None)
0242 self.superimpose[aname].Weight = attrs.get('Weight',None)
0243 self.superimpose[aname].Maximum = attrs.get('Maximum',None)
0244 self.superimpose[aname].Minimum = attrs.get('Minimum',None)
0245 self.superimpose[aname].Labels = attrs.get('Labels',None)
0246 self.superimpose[aname].Rebin = attrs.get('Rebin',None)
0247 self.tmpsupername = aname
0248 if name == 'graph':
0249 aname = attrs.get('name',None)
0250 self.graph[aname] = graphElement()
0251 self.graph[aname].name = aname
0252 self.graph[aname].title = attrs.get('title',None)
0253 self.graph[aname].SetLogy = attrs.get('SetLogy',None)
0254 self.graph[aname].SetGrid = attrs.get('SetGrid',None)
0255 self.graph[aname].Normalize = attrs.get('Normalize',None)
0256 self.graph[aname].Stack = attrs.get('Stack',None)
0257 self.graph[aname].YTitle = attrs.get('YTitle',None)
0258 self.graph[aname].XTitle = attrs.get('XTitle',None)
0259 self.graph[aname].projection = attrs.get('Projection',None)
0260 self.graph[aname].bin = attrs.get('bin',None)
0261 self.graph[aname].profile = attrs.get('Profile',None)
0262 self.graph[aname].Fill = attrs.get('Fill',None)
0263 self.graph[aname].Option = attrs.get('Option',None)
0264 self.graph[aname].Weight = attrs.get('Weight',None)
0265 self.graph[aname].Maximum = attrs.get('Maximum',None)
0266 self.graph[aname].Minimum = attrs.get('Minimum',None)
0267 self.graph[aname].Labels = attrs.get('Labels',None)
0268 self.tmpgraphname = aname
0269 if name == 'superimposeItem':
0270
0271 self.superimpose[self.tmpsupername].histos.append(attrs.get('name',None))
0272 self.superimpose[self.tmpsupername].color.append(attrs.get('color',None))
0273 self.superimpose[self.tmpsupername].marker.append(attrs.get('MarkerStyle',None))
0274 self.superimpose[self.tmpsupername].legend.append(attrs.get('legend',None))
0275
0276
0277 if name == 'graphItem':
0278
0279 self.graph[self.tmpgraphname].histos.append(attrs.get('name',None))
0280 self.graph[self.tmpgraphname].color.append(attrs.get('color',None))
0281 self.graph[self.tmpgraphname].marker.append(attrs.get('MarkerStyle',None))
0282 self.graph[self.tmpgraphname].legend.append(attrs.get('legend',None))
0283 self.graph[self.tmpgraphname].flavour.append(attrs.get('flavour',None))
0284
0285
0286
0287
0288 if __name__ == '__main__':
0289
0290
0291
0292
0293 thestyle = Style.Style()
0294 thestyle.SetStyle()
0295
0296 printCanvas = False
0297 printFormat = "png"
0298 printBanner = False
0299 Banner = "CMS Preliminary"
0300 verbose = False
0301
0302
0303 option,args = parse(__doc__)
0304 if not args and not option: exit()
0305
0306 if option.batch:
0307 ROOT.gROOT.SetBatch()
0308
0309 if option.verbose:
0310 verbose = True
0311
0312 if option.list:
0313 ins = Inspector.Inspector()
0314 ins.Verbose(True)
0315 ins.createXML(False)
0316 ins.SetFilename(option.list)
0317 ins.GetListObjects()
0318 sys.exit()
0319
0320 if option.create:
0321 createXML = Inspector.Inspector()
0322 createXML.Verbose(False)
0323 createXML.createXML(True)
0324 if option.tag:
0325 createXML.SetTag(option.tag)
0326 createXML.SetFilename(option.create)
0327 createXML.GetListObjects()
0328 sys.exit()
0329
0330 if not option.xml: exit()
0331 if option.prt:
0332 printCanvas = True
0333 printFormat = option.prt
0334
0335 if option.flag:
0336 printBanner = True
0337 Banner = option.flag
0338
0339
0340 try:
0341 xmlfile = open(option.xml)
0342 xmlfile.close()
0343 except:
0344 print(" ERROR: xml file \"" + option.xml + "\" does not exist")
0345 sys.exit()
0346
0347
0348 parser = make_parser()
0349
0350
0351 parser.setFeature(feature_namespaces, 0)
0352
0353
0354 dh = FindIssue()
0355
0356
0357 parser.setContentHandler(dh)
0358
0359
0360 parser.parse(option.xml)
0361
0362
0363 cv = {}
0364 afilelist = {}
0365 stacklist = {}
0366
0367
0368 outputroot = TFile("cuy.root","RECREATE")
0369
0370
0371 newTH1list = []
0372
0373
0374 thedata = dh.data
0375
0376 firstFilename = ''
0377
0378 for ikey in thedata:
0379 if verbose : print("= Processing set called: " + ikey)
0380 afilename = thedata[ikey].filename
0381 if firstFilename == '':
0382 firstFilename = afilename
0383 arelease = ""
0384 if thedata[ikey].release != None:
0385 arelease = thedata[ikey].release
0386 if verbose : print("== filename: " + afilename)
0387 if verbose : print("== release: " + arelease)
0388 if verbose : print("== weight: " + thedata[ikey].weight)
0389 thehistos = thedata[ikey].histos
0390 afilelist[afilename] = TFile(afilename)
0391 if verbose : print("== get histograms: ")
0392 histonamekeys = thehistos.keys()
0393 for ihname in histonamekeys:
0394 if verbose : print("=== Histogram name: \""+ ihname + "\" source: \""+thehistos[ihname]+"\"")
0395 thedata[ikey].TH1s[ihname] = ROOT.gDirectory.Get(thehistos[ihname])
0396
0397
0398 print(thedata[ikey].TH1s[ihname].GetName())
0399
0400
0401
0402
0403 afilelist[firstFilename].cd()
0404
0405
0406
0407 theaddition = dh.addition
0408 if verbose : print("= Create addition histograms:")
0409
0410 for ikey in theaddition:
0411 if verbose : print("== plot name: \""+theaddition[ikey].name+"\" title: \""+theaddition[ikey].title+"\"")
0412 listname = theaddition[ikey].histos
0413 listweight = theaddition[ikey].weight
0414
0415
0416 cv[theaddition[ikey].name] = TCanvas(theaddition[ikey].name,theaddition[ikey].name,700,700)
0417
0418 isFirst = True
0419 ihnameIt = 0
0420 for ihname in listname:
0421 aweight = 1
0422 if listweight[ihnameIt]:
0423
0424 aweight = float(listweight[ihnameIt])
0425
0426 for jkey in thedata:
0427 tmpkeys = thedata[jkey].histos.keys()
0428 for tmpname in tmpkeys:
0429 if tmpname == ihname:
0430 ath = thedata[jkey].TH1s[tmpname]
0431 if ath is None:
0432 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0433 exit(0)
0434 if verbose : print("=== add histogram: "+ath.GetName() + " from " + thedata[jkey].filename + " mean = " + "%.2f" % round(ath.GetMean(),2) + " weight= " + str(aweight))
0435
0436 if isFirst:
0437 newth = ath.Clone(theaddition[ikey].name)
0438 newth.Sumw2()
0439 if theaddition[ikey].Normalize == "true":
0440 newth.Scale(1/newth.Integral())
0441 newth.Scale(aweight)
0442 isFirst = False
0443 else:
0444 atmpth = ath.Clone()
0445 atmpth.Sumw2()
0446 if theaddition[ikey].Normalize == "true":
0447 atmpth.Scale(1/atmpth.Integral())
0448 atmpth.Scale(aweight)
0449 newth.Add( atmpth )
0450 ihnameIt = ihnameIt + 1
0451
0452 if theaddition[ikey].XTitle != None:
0453 newth.SetXTitle(theaddition[ikey].XTitle)
0454 if theaddition[ikey].YTitle != None:
0455 newth.SetYTitle(theaddition[ikey].YTitle)
0456
0457 if theaddition[ikey].Option:
0458 newth.Draw(theaddition[ikey].Option)
0459 else:
0460 newth.Draw()
0461
0462 if theaddition[ikey].SetGrid == "true":
0463 cv[theaddition[ikey].name].SetGrid()
0464
0465 cv[theaddition[ikey].name].Update()
0466
0467
0468 newth.SetName(theaddition[ikey].name)
0469 newTH1list.append(newth.GetName())
0470 thedata[newth.GetName()] = ValElement()
0471 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
0472 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
0473
0474
0475 outputroot.cd()
0476 newth.Write()
0477
0478
0479 if verbose : print("= Create ratio histograms:")
0480
0481 thedivition = dh.divide
0482 for ikey in thedivition:
0483 if verbose : print("== plot name: \""+thedivition[ikey].name+"\" title: \""+"\"")
0484 numerator = thedivition[ikey].numerator
0485 denominator = thedivition[ikey].denominator
0486
0487
0488 cv[thedivition[ikey].name] = TCanvas(thedivition[ikey].name,thedivition[ikey].name,700,700)
0489
0490 for jkey in thedata:
0491 tmpkeys = thedata[jkey].histos.keys()
0492 for tmpname in tmpkeys:
0493 if tmpname == numerator:
0494 numeratorth = thedata[jkey].TH1s[tmpname]
0495 if numeratorth is None:
0496 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0497 exit(0)
0498
0499
0500 if tmpname == denominator:
0501 denominatorth = thedata[jkey].TH1s[tmpname]
0502 if denominatorth is None:
0503 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0504 exit(0)
0505
0506
0507
0508
0509 numeratorth.Sumw2()
0510 denominatorth.Sumw2()
0511 newth = numeratorth.Clone()
0512 newth.Clear()
0513 if thedivition[ikey].DivideOption is None:
0514 newth.Divide(numeratorth,denominatorth)
0515 else:
0516 newth.Divide(numeratorth,denominatorth,1.,1.,thedivition[ikey].DivideOption)
0517
0518
0519
0520
0521
0522 if thedivition[ikey].Option:
0523 newth.Draw(thedivition[ikey].Option)
0524 else:
0525 newth.Draw()
0526
0527 cv[thedivition[ikey].name].Update()
0528
0529
0530
0531 if option.wait:
0532 raw_input( 'Press ENTER to continue\n ' )
0533
0534
0535 newth.SetName(thedivition[ikey].name)
0536 newTH1list.append(newth.GetName())
0537 thedata[newth.GetName()] = ValElement()
0538 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
0539 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
0540
0541
0542 outputroot.cd()
0543 newth.Write()
0544
0545
0546 thesuper = dh.superimpose
0547 if verbose : print("= Create superimpose histograms:")
0548 for ikey in thesuper:
0549 if verbose : print("== plot name: \""+thesuper[ikey].name+"\" title: \""+thesuper[ikey].title+"\"")
0550 listname = thesuper[ikey].histos
0551 listcolor = thesuper[ikey].color
0552 listmarker = thesuper[ikey].marker
0553 listlegend = thesuper[ikey].legend
0554
0555 dolegend = False
0556 for il in listlegend:
0557 if il==None: dolegend = False
0558 if verbose : print("dolegend = " +str(dolegend))
0559 doNormalize = False
0560 doRebin=thesuper[ikey].Rebin
0561 if doRebin is not None :
0562 doRebin=int(doRebin)
0563 if verbose : print("Rebin is ", doRebin)
0564 if thesuper[ikey].Normalize == "true":
0565 doNormalize = True
0566 if verbose : print("normalize = " +str(doNormalize))
0567 projectAxis = "no"
0568 projectBin = -1
0569 if thesuper[ikey].projection == "x": projectAxis = "x"
0570 if thesuper[ikey].projection == "y": projectAxis = "y"
0571 if thesuper[ikey].bin != None: projectBin = thesuper[ikey].bin
0572 profileAxis = "no"
0573 if thesuper[ikey].profile == "x": profileAxis = "x"
0574 if thesuper[ikey].profile == "y": profileAxis = "y"
0575 doFill = False
0576 if thesuper[ikey].Fill == "true": doFill = True
0577 if verbose : print("fill option:"+ doFill)
0578
0579 cv[thesuper[ikey].name] = TCanvas(thesuper[ikey].name,thesuper[ikey].title,700,700)
0580
0581 aleg = TLegend(0.6,0.4,0.8,0.6)
0582 SetOwnership( aleg, 0 )
0583 aleg.SetMargin(0.12)
0584 aleg.SetTextSize(0.035)
0585 aleg.SetFillColor(10)
0586 aleg.SetBorderSize(0)
0587
0588 isFirst = 1
0589 ii = 0
0590
0591 stacklist[thesuper[ikey].name] = THStack("astack"+thesuper[ikey].name,thesuper[ikey].title)
0592 astack = stacklist[thesuper[ikey].name]
0593 for ihname in listname:
0594
0595 for jkey in thedata:
0596 tmpkeys = thedata[jkey].histos.keys()
0597
0598 for tmpname in tmpkeys:
0599
0600 if tmpname == ihname:
0601 ath = thedata[jkey].TH1s[tmpname]
0602 if ath is None:
0603 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0604 exit(0)
0605 if verbose : print("=== superimpose histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2))
0606
0607 if projectAxis == "x":
0608 if projectBin == -1:
0609 newthpx = ath.ProjectionX(ath.GetName()+"_px",0,-1,"e")
0610 else:
0611 newthpx = ath.ProjectionX(ath.GetName()+"_px",int(projectBin),int(projectBin),"e")
0612 newth = newthpx.Clone()
0613 if projectAxis == "y":
0614 if projectBin == -1:
0615 newthpy = ath.ProjectionY(ath.GetName()+"_py",0,-1,"e")
0616 else:
0617 newthpx = ath.ProjectionY(ath.GetName()+"_py",int(projectBin),int(projectBin),"e")
0618 newth = newthpy.Clone()
0619 if profileAxis == "x":
0620 newthpx = ath.ProfileX(ath.GetName()+"_px",0,-1,"e")
0621 newth = newthpx.Clone()
0622 if profileAxis == "y":
0623 newthpy = ath.ProfileY(ath.GetName()+"_py",0,-1,"e")
0624 newth = newthpy.Clone()
0625
0626
0627 aweight = 1
0628 if thedata[jkey].weight != None and thesuper[ikey].Weight=="true":
0629 aweight = float( thedata[jkey].weight )
0630 if verbose: print(" with weight = " + str(aweight))
0631
0632
0633
0634
0635 if projectAxis == "no" and profileAxis == "no" :newth = ath.Clone()
0636
0637 if doRebin is not None and doRebin>0 :
0638 newth.Rebin(doRebin)
0639
0640 newth.Sumw2()
0641 newth.Scale(aweight)
0642
0643
0644 if not listcolor[ii]:
0645 listcolor[ii] = 1
0646
0647 newth.SetLineColor(int(listcolor[ii]))
0648 newth.SetMarkerColor(int(listcolor[ii]))
0649
0650 if doFill: newth.SetFillColor(int(listcolor[ii]))
0651
0652 if listmarker[ii] != None:
0653 newth.SetMarkerStyle(int(listmarker[ii]))
0654
0655 if doNormalize:
0656 newth.Scale(1./newth.Integral())
0657
0658
0659 if thesuper[ikey].Labels != None:
0660 thelabels = thesuper[ikey].Labels.split(',')
0661 ib = 1
0662
0663
0664 for ilabel in thelabels:
0665 newth.GetXaxis().SetBinLabel(ib,ilabel)
0666
0667
0668
0669 ib += 1
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687 if doFill:
0688 if thesuper[ikey].XTitle != None:
0689 newth.SetXTitle("")
0690 astack.Add(newth,"HIST")
0691 elif thesuper[ikey].Option:
0692 astack.Add(newth,thesuper[ikey].Option)
0693 else:
0694
0695 astack.Add(newth)
0696
0697 astack.SetTitle(thesuper[ikey].title)
0698
0699 if isFirst==1:
0700 newth.GetPainter().PaintStat(ROOT.gStyle.GetOptStat(),0);
0701 isFirst=0
0702 tmpsumth = newth.Clone()
0703 else:
0704 tmpsumth.Add(newth)
0705
0706
0707
0708
0709
0710
0711
0712 if dolegend and doFill:
0713 aleg.AddEntry(newth,listlegend[ii],"F")
0714 elif dolegend:
0715 aleg.AddEntry(newth,listlegend[ii],"P")
0716
0717 newth.SetName(tmpname)
0718 outputroot.cd()
0719 newth.Write()
0720 ii = ii + 1
0721
0722
0723 if thesuper[ikey].Maximum != None:
0724 astack.SetMaximum( float(thesuper[ikey].Maximum) )
0725 if thesuper[ikey].Minimum != None:
0726 astack.SetMinimum( float(thesuper[ikey].Minimum) )
0727 if thesuper[ikey].Stack == "true":
0728 astack.Draw()
0729 if thesuper[ikey].Stack == "false" or thesuper[ikey].Stack == None:
0730 astack.Draw()
0731 astack.Draw("nostack")
0732 if thesuper[ikey].XTitle != None:
0733 astack.GetHistogram().SetXTitle(thesuper[ikey].XTitle)
0734 if thesuper[ikey].YTitle != None:
0735 astack.GetHistogram().SetYTitle(thesuper[ikey].YTitle)
0736 if doFill:
0737 astack.Draw("sameaxis")
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753 if dolegend:
0754 aleg.Draw()
0755 if thesuper[ikey].SetLogy == "true":
0756 cv[thesuper[ikey].name].SetLogy()
0757 if thesuper[ikey].SetGrid == "true":
0758 cv[thesuper[ikey].name].SetGrid()
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772 if printBanner:
0773 tex = TLatex(0.35,0.95,Banner)
0774 tex.SetNDC()
0775 tex.SetTextSize(0.05)
0776 tex.Draw()
0777
0778 cv[thesuper[ikey].name].Update()
0779
0780
0781
0782 if option.wait:
0783 raw_input( 'Press ENTER to continue\n ' )
0784
0785
0786
0787
0788
0789
0790 thegraph = dh.graph
0791 if verbose : print("= Create graph histograms:")
0792 for ikey in thegraph:
0793 if verbose : print("== plot name: \""+thegraph[ikey].name+"\" title: \""+thegraph[ikey].title+"\"")
0794 listname = thegraph[ikey].histos
0795 listcolor = thegraph[ikey].color
0796 listmarker = thegraph[ikey].marker
0797 listlegend = thegraph[ikey].legend
0798 listflavour = thegraph[ikey].flavour
0799
0800 dolegend = False
0801 for il in listlegend:
0802 if il==None: dolegend = False
0803 if verbose : print("dolegend = " +str(dolegend))
0804 doNormalize = False
0805 if thegraph[ikey].Normalize == "true":
0806 doNormalize = True
0807 if verbose : print("normalize = " +str(doNormalize))
0808 projectAxis = "no"
0809 projectBin = -1
0810 if thegraph[ikey].projection == "x": projectAxis = "x"
0811 if thegraph[ikey].projection == "y": projectAxis = "y"
0812 if thegraph[ikey].bin != None: projectBin = thegraph[ikey].bin
0813 profileAxis = "no"
0814 if thegraph[ikey].profile == "x": profileAxis = "x"
0815 if thegraph[ikey].profile == "y": profileAxis = "y"
0816 doFill = False
0817 if thegraph[ikey].Fill == "true": doFill = True
0818 if verbose : print("fill option:"+ doFill)
0819
0820 cv[thegraph[ikey].name] = TCanvas(thegraph[ikey].name,thegraph[ikey].title,700,700)
0821
0822 aleg = TLegend(0.6,0.4,0.8,0.6)
0823 SetOwnership( aleg, 0 )
0824 aleg.SetMargin(0.12)
0825 aleg.SetTextSize(0.035)
0826 aleg.SetFillColor(10)
0827 aleg.SetBorderSize(0)
0828
0829 isFirst = 1
0830 ii = 0
0831
0832 stacklist[thegraph[ikey].name] = THStack("astack"+thegraph[ikey].name,thegraph[ikey].title)
0833 astack = stacklist[thegraph[ikey].name]
0834 xVal_val = TVectorD()
0835 yVal_val = TVectorD()
0836 yBin_val = std.vector(int)()
0837 xErr_val = TVectorD()
0838 yErr_val = TVectorD()
0839 zVal_val = TVectorD()
0840 zErr_val = TVectorD()
0841 nVal_val = 0
0842
0843 xVal_ref = TVectorD()
0844 yVal_ref = TVectorD()
0845 yBin_ref = std.vector(int)()
0846 xErr_ref = TVectorD()
0847 yErr_ref = TVectorD()
0848 zVal_ref = TVectorD()
0849 zErr_ref = TVectorD()
0850 nVal_ref = 0
0851
0852 RangeMax = 0.005
0853 RangeMin = 0.9
0854
0855 for ihname in listname:
0856
0857 for jkey in thedata:
0858 tmpkeys = thedata[jkey].histos.keys()
0859
0860 for tmpname in tmpkeys:
0861
0862 if tmpname == ihname:
0863
0864 ath = thedata[jkey].TH1s[tmpname]
0865 if ath is None:
0866 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0867 exit(0)
0868 if verbose : print("=== graph histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2))
0869
0870 if listflavour[ii] == "5":
0871
0872 nBinB = 200
0873 BinWidth = (0.01+ath.GetMaximum())/nBinB
0874 BMid = 0.005+BinWidth/2
0875 Err = BinWidth
0876 for iBinB in range(1,nBinB+1):
0877
0878 BMid = BMid+Err
0879
0880 nAthBin = ath.GetNbinsX()-2
0881
0882 maxInHisto = ath.GetMaximum()
0883 minInHisto = ath.GetMinimum()
0884
0885 yClosestInit = 0
0886 iBinClosestInit = 0
0887 if BMid <= maxInHisto : yClosestInit = maxInHisto + 1
0888 else : yClosestInit = minInHisto - 1.0
0889 iBinClosest = iBinClosestInit
0890 yClosest = yClosestInit
0891 for iAthBin in range(1,nAthBin+1):
0892 yBin = ath.GetBinContent(iAthBin)
0893 dif1 = BMid-yBin
0894 if dif1 < 0 : dif1 = yBin-BMid
0895 dif2 = yClosest-BMid
0896 if dif2 < 0 : dif2 = BMid-yClosest
0897 if dif1 < dif2:
0898 yClosest = yBin
0899 iBinClosest = iAthBin
0900 min = BMid-Err/2
0901 max = BMid+Err/2
0902
0903 if yClosest < min or yClosest > max:
0904 iBinClosest = 0
0905
0906 if iBinClosest > 0 and listmarker[ii] == "8":
0907
0908 nVal_ref = nVal_ref+1
0909 xVal_ref.ResizeTo(nVal_ref)
0910
0911 xErr_ref.ResizeTo(nVal_ref)
0912 xVal_ref[nVal_ref-1] = BMid
0913 yBin_ref.push_back(iBinClosest)
0914 xErr_ref[nVal_ref-1] = ath.GetBinError ( iBinClosest )
0915 Err = xErr_ref[nVal_ref-1]
0916 if Err < BinWidth : Err = BinWidth
0917 elif iBinClosest > 0:
0918 nVal_val = nVal_val+1
0919 xVal_val.ResizeTo(nVal_val)
0920
0921 xErr_val.ResizeTo(nVal_val)
0922 xVal_val[nVal_val-1] = BMid
0923 yBin_val.push_back(iBinClosest)
0924 xErr_val[nVal_val-1] = ath.GetBinError ( iBinClosest )
0925 Err = xErr_val[nVal_val-1]
0926 if Err < BinWidth : Err = BinWidth
0927 elif listflavour[ii] == "4" and listmarker[ii] == "8":
0928 yVal_ref.ResizeTo(nVal_ref)
0929 yErr_ref.ResizeTo(nVal_ref)
0930 for iVal in range(0,nVal_ref):
0931 yVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
0932 if yVal_ref[iVal] > RangeMax : RangeMax = yVal_ref[iVal]
0933 yErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
0934 elif listflavour[ii] == "4":
0935 yVal_val.ResizeTo(nVal_val)
0936 yErr_val.ResizeTo(nVal_val)
0937 for iVal in range(0,nVal_val):
0938 yVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
0939 yErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
0940 elif listmarker[ii] == "8":
0941 zVal_ref.ResizeTo(nVal_ref)
0942 zErr_ref.ResizeTo(nVal_ref)
0943 for iVal in range(0,nVal_ref):
0944 zVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
0945 zErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
0946 if zVal_ref[iVal] < RangeMin : RangeMin = zVal_ref[iVal]
0947 else:
0948 zVal_val.ResizeTo(nVal_val)
0949 zErr_val.ResizeTo(nVal_val)
0950 for iVal in range(0,nVal_val):
0951 zVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
0952 zErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
0953 ii = ii + 1
0954
0955
0956
0957
0958
0959
0960
0961 graphs = [TGraphErrors(xVal_ref,yVal_ref,xErr_ref,yErr_ref),
0962 TGraphErrors(xVal_ref,zVal_ref,xErr_ref,zErr_ref),
0963 TGraphErrors(xVal_val,yVal_val,xErr_val,yErr_val),
0964 TGraphErrors(xVal_val,zVal_val,xErr_val,zErr_val)]
0965 ii = 0
0966
0967
0968
0969 for ii in range(0,4):
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992 aweight = 1
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 col = 2
1010 mark = 22
1011 if ii == 0 or ii == 2:
1012 col = 1
1013 if ii == 0 or ii == 1:
1014 mark = 8
1015
1016 graphs[ii].SetLineColor(col)
1017 graphs[ii].SetMarkerStyle(mark)
1018 graphs[ii].SetMarkerColor(col)
1019 graphs[ii].SetTitle(thegraph[ikey].title)
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 if isFirst==1:
1070
1071 isFirst=0
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090 outputroot.cd()
1091 graphs[ii].Write()
1092 ii = ii + 1
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 if thegraph[ikey].XTitle != None:
1105 graphs[0].GetHistogram().SetXTitle(thegraph[ikey].XTitle)
1106 if thegraph[ikey].YTitle != None:
1107 graphs[0].GetHistogram().SetYTitle(thegraph[ikey].YTitle)
1108
1109
1110 if RangeMax > 0.5 : RangeMax = 1.5
1111 if RangeMax < 0.5 : RangeMax = RangeMax + 0.05
1112
1113 RangeMin = RangeMin - 0.5*RangeMin
1114
1115
1116 if RangeMin < 0.00001 : RangeMin = 0.00005
1117 graphs[0].GetYaxis().SetRangeUser(RangeMin,RangeMax)
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 graphs[0].Draw("AP")
1135 graphs[1].Draw("sameP")
1136 graphs[2].Draw("sameP")
1137 graphs[3].Draw("sameP")
1138 if dolegend:
1139 aleg.Draw()
1140 if thegraph[ikey].SetLogy == "true":
1141 cv[thegraph[ikey].name].SetLogy()
1142 if thegraph[ikey].SetGrid == "true":
1143 cv[thegraph[ikey].name].SetGrid()
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 if printBanner:
1158 tex = TLatex(0.35,0.95,Banner)
1159 tex.SetNDC()
1160 tex.SetTextSize(0.05)
1161 tex.Draw()
1162
1163 cv[thegraph[ikey].name].Update()
1164 save = thegraph[ikey].name
1165 cv[thegraph[ikey].name].Print(save + ".gif")
1166
1167
1168 if option.wait:
1169 raw_input( 'Press ENTER to continue\n ' )
1170
1171
1172
1173
1174
1175 if printCanvas:
1176
1177 for ikey in theaddition:
1178 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
1179 for ikey in thesuper:
1180 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
1181 for ikey in thegraph:
1182 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "notgood." + printFormat)
1183
1184
1185
1186
1187
1188
1189 rep = ''
1190 while not rep in [ 'q', 'Q', '.q', 'qq' 'p']:
1191 rep = raw_input( '\nenter: ["q",".q" to quit] ["p" or "print" to print all canvas]: ' )
1192 if 0<len(rep):
1193 if rep=='quit': rep = 'q'
1194 if rep=='p' or rep=='print':
1195 for ikey in theaddition:
1196 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
1197 for ikey in thesuper:
1198 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
1199 for ikey in thegraph:
1200 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "." + printFormat)
1201
1202